jffs2_1pass.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. -------------------------------------------------------------------------
  4. * Filename: jffs2.c
  5. * Version: $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
  6. * Copyright: Copyright (C) 2001, Russ Dill
  7. * Author: Russ Dill <Russ.Dill@asu.edu>
  8. * Description: Module to load kernel from jffs2
  9. *-----------------------------------------------------------------------*/
  10. /*
  11. * some portions of this code are taken from jffs2, and as such, the
  12. * following copyright notice is included.
  13. *
  14. * JFFS2 -- Journalling Flash File System, Version 2.
  15. *
  16. * Copyright (C) 2001 Red Hat, Inc.
  17. *
  18. * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
  19. *
  20. * The original JFFS, from which the design for JFFS2 was derived,
  21. * was designed and implemented by Axis Communications AB.
  22. *
  23. * The contents of this file are subject to the Red Hat eCos Public
  24. * License Version 1.1 (the "Licence"); you may not use this file
  25. * except in compliance with the Licence. You may obtain a copy of
  26. * the Licence at http://www.redhat.com/
  27. *
  28. * Software distributed under the Licence is distributed on an "AS IS"
  29. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
  30. * See the Licence for the specific language governing rights and
  31. * limitations under the Licence.
  32. *
  33. * The Original Code is JFFS2 - Journalling Flash File System, version 2
  34. *
  35. * Alternatively, the contents of this file may be used under the
  36. * terms of the GNU General Public License version 2 (the "GPL"), in
  37. * which case the provisions of the GPL are applicable instead of the
  38. * above. If you wish to allow the use of your version of this file
  39. * only under the terms of the GPL and not to allow others to use your
  40. * version of this file under the RHEPL, indicate your decision by
  41. * deleting the provisions above and replace them with the notice and
  42. * other provisions required by the GPL. If you do not delete the
  43. * provisions above, a recipient may use your version of this file
  44. * under either the RHEPL or the GPL.
  45. *
  46. * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
  47. *
  48. */
  49. /* Ok, so anyone who knows the jffs2 code will probably want to get a papar
  50. * bag to throw up into before reading this code. I looked through the jffs2
  51. * code, the caching scheme is very elegant. I tried to keep the version
  52. * for a bootloader as small and simple as possible. Instead of worring about
  53. * unneccesary data copies, node scans, etc, I just optimized for the known
  54. * common case, a kernel, which looks like:
  55. * (1) most pages are 4096 bytes
  56. * (2) version numbers are somewhat sorted in acsending order
  57. * (3) multiple compressed blocks making up one page is uncommon
  58. *
  59. * So I create a linked list of decending version numbers (insertions at the
  60. * head), and then for each page, walk down the list, until a matching page
  61. * with 4096 bytes is found, and then decompress the watching pages in
  62. * reverse order.
  63. *
  64. */
  65. /*
  66. * Adapted by Nye Liu <nyet@zumanetworks.com> and
  67. * Rex Feany <rfeany@zumanetworks.com>
  68. * on Jan/2002 for U-Boot.
  69. *
  70. * Clipped out all the non-1pass functions, cleaned up warnings,
  71. * wrappers, etc. No major changes to the code.
  72. * Please, he really means it when he said have a paper bag
  73. * handy. We needed it ;).
  74. *
  75. */
  76. /*
  77. * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
  78. *
  79. * - overhaul of the memory management. Removed much of the "paper-bagging"
  80. * in that part of the code, fixed several bugs, now frees memory when
  81. * partition is changed.
  82. * It's still ugly :-(
  83. * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
  84. * was incorrect. Removed a bit of the paper-bagging as well.
  85. * - removed double crc calculation for fragment headers in jffs2_private.h
  86. * for speedup.
  87. * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
  88. * - spinning wheel now spins depending on how much memory has been scanned
  89. * - lots of small changes all over the place to "improve" readability.
  90. * - implemented fragment sorting to ensure that the newest data is copied
  91. * if there are multiple copies of fragments for a certain file offset.
  92. *
  93. * The fragment sorting feature must be enabled by CFG_JFFS2_SORT_FRAGMENTS.
  94. * Sorting is done while adding fragments to the lists, which is more or less a
  95. * bubble sort. This takes a lot of time, and is most probably not an issue if
  96. * the boot filesystem is always mounted readonly.
  97. *
  98. * You should define it if the boot filesystem is mounted writable, and updates
  99. * to the boot files are done by copying files to that filesystem.
  100. *
  101. *
  102. * There's a big issue left: endianess is completely ignored in this code. Duh!
  103. *
  104. *
  105. * You still should have paper bags at hand :-(. The code lacks more or less
  106. * any comment, and is still arcane and difficult to read in places. As this
  107. * is incompatible with any new code from the jffs2 maintainers anyway, it
  108. * should probably be dumped and replaced by something like jffs2reader!
  109. */
  110. #include <common.h>
  111. #include <config.h>
  112. #include <malloc.h>
  113. #include <linux/stat.h>
  114. #include <linux/time.h>
  115. #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
  116. #include <jffs2/jffs2.h>
  117. #include <jffs2/jffs2_1pass.h>
  118. #include "jffs2_private.h"
  119. #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
  120. #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
  121. /* Debugging switches */
  122. #undef DEBUG_DIRENTS /* print directory entry list after scan */
  123. #undef DEBUG_FRAGMENTS /* print fragment list after scan */
  124. #undef DEBUG /* enable debugging messages */
  125. #ifdef DEBUG
  126. # define DEBUGF(fmt,args...) printf(fmt ,##args)
  127. #else
  128. # define DEBUGF(fmt,args...)
  129. #endif
  130. /* Compression names */
  131. static char *compr_names[] = {
  132. "NONE",
  133. "ZERO",
  134. "RTIME",
  135. "RUBINMIPS",
  136. "COPY",
  137. "DYNRUBIN",
  138. "ZLIB"
  139. };
  140. /* Spinning wheel */
  141. static char spinner[] = { '|', '/', '-', '\\' };
  142. /* Memory management */
  143. struct mem_block {
  144. u32 index;
  145. struct mem_block *next;
  146. struct b_node nodes[NODE_CHUNK];
  147. };
  148. static void
  149. free_nodes(struct b_list *list)
  150. {
  151. while (list->listMemBase != NULL) {
  152. struct mem_block *next = list->listMemBase->next;
  153. free( list->listMemBase );
  154. list->listMemBase = next;
  155. }
  156. }
  157. static struct b_node *
  158. add_node(struct b_list *list)
  159. {
  160. u32 index = 0;
  161. struct mem_block *memBase;
  162. struct b_node *b;
  163. memBase = list->listMemBase;
  164. if (memBase != NULL)
  165. index = memBase->index;
  166. #if 0
  167. putLabeledWord("add_node: index = ", index);
  168. putLabeledWord("add_node: memBase = ", list->listMemBase);
  169. #endif
  170. if (memBase == NULL || index >= NODE_CHUNK) {
  171. /* we need more space before we continue */
  172. memBase = mmalloc(sizeof(struct mem_block));
  173. if (memBase == NULL) {
  174. putstr("add_node: malloc failed\n");
  175. return NULL;
  176. }
  177. memBase->next = list->listMemBase;
  178. index = 0;
  179. #if 0
  180. putLabeledWord("add_node: alloced a new membase at ", *memBase);
  181. #endif
  182. }
  183. /* now we have room to add it. */
  184. b = &memBase->nodes[index];
  185. index ++;
  186. memBase->index = index;
  187. list->listMemBase = memBase;
  188. list->listCount++;
  189. return b;
  190. }
  191. static struct b_node *
  192. insert_node(struct b_list *list, u32 offset)
  193. {
  194. struct b_node *new;
  195. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  196. struct b_node *b, *prev;
  197. #endif
  198. if (!(new = add_node(list))) {
  199. putstr("add_node failed!\r\n");
  200. return NULL;
  201. }
  202. new->offset = offset;
  203. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  204. if (list->listTail != NULL && list->listCompare(new, list->listTail))
  205. prev = list->listTail;
  206. else if (list->listLast != NULL && list->listCompare(new, list->listLast))
  207. prev = list->listLast;
  208. else
  209. prev = NULL;
  210. for (b = (prev ? prev->next : list->listHead);
  211. b != NULL && list->listCompare(new, b);
  212. prev = b, b = b->next) {
  213. list->listLoops++;
  214. }
  215. if (b != NULL)
  216. list->listLast = prev;
  217. if (b != NULL) {
  218. new->next = b;
  219. if (prev != NULL)
  220. prev->next = new;
  221. else
  222. list->listHead = new;
  223. } else
  224. #endif
  225. {
  226. new->next = (struct b_node *) NULL;
  227. if (list->listTail != NULL) {
  228. list->listTail->next = new;
  229. list->listTail = new;
  230. } else {
  231. list->listTail = list->listHead = new;
  232. }
  233. }
  234. return new;
  235. }
  236. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  237. static int compare_inodes(struct b_node *new, struct b_node *old)
  238. {
  239. struct jffs2_raw_inode *jNew = (struct jffs2_raw_inode *)new->offset;
  240. struct jffs2_raw_inode *jOld = (struct jffs2_raw_inode *)old->offset;
  241. return jNew->version < jOld->version;
  242. }
  243. static int compare_dirents(struct b_node *new, struct b_node *old)
  244. {
  245. struct jffs2_raw_dirent *jNew = (struct jffs2_raw_dirent *)new->offset;
  246. struct jffs2_raw_dirent *jOld = (struct jffs2_raw_dirent *)old->offset;
  247. return jNew->version > jOld->version;
  248. }
  249. #endif
  250. static u32
  251. jffs2_scan_empty(u32 start_offset, struct part_info *part)
  252. {
  253. char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
  254. char *offset = part->offset + start_offset;
  255. while (offset < max && *(u32 *)offset == 0xFFFFFFFF) {
  256. offset += sizeof(u32);
  257. /* return if spinning is due */
  258. if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
  259. }
  260. return offset - part->offset;
  261. }
  262. static u32
  263. jffs_init_1pass_list(struct part_info *part)
  264. {
  265. struct b_lists *pL;
  266. if (part->jffs2_priv != NULL) {
  267. pL = (struct b_lists *)part->jffs2_priv;
  268. free_nodes(&pL->frag);
  269. free_nodes(&pL->dir);
  270. free(pL);
  271. }
  272. if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
  273. pL = (struct b_lists *)part->jffs2_priv;
  274. memset(pL, 0, sizeof(*pL));
  275. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  276. pL->dir.listCompare = compare_dirents;
  277. pL->frag.listCompare = compare_inodes;
  278. #endif
  279. }
  280. return 0;
  281. }
  282. /* find the inode from the slashless name given a parent */
  283. static long
  284. jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
  285. {
  286. struct b_node *b;
  287. struct jffs2_raw_inode *jNode;
  288. u32 totalSize = 0;
  289. u16 latestVersion = 0;
  290. char *lDest;
  291. char *src;
  292. long ret;
  293. int i;
  294. u32 counter = 0;
  295. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  296. jNode = (struct jffs2_raw_inode *) (b->offset);
  297. if ((inode == jNode->ino)) {
  298. #if 0
  299. putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
  300. putLabeledWord("read_inode: inode = ", jNode->ino);
  301. putLabeledWord("read_inode: version = ", jNode->version);
  302. putLabeledWord("read_inode: isize = ", jNode->isize);
  303. putLabeledWord("read_inode: offset = ", jNode->offset);
  304. putLabeledWord("read_inode: csize = ", jNode->csize);
  305. putLabeledWord("read_inode: dsize = ", jNode->dsize);
  306. putLabeledWord("read_inode: compr = ", jNode->compr);
  307. putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
  308. putLabeledWord("read_inode: flags = ", jNode->flags);
  309. #endif
  310. /* get actual file length from the newest node */
  311. if (jNode->version >= latestVersion) {
  312. totalSize = jNode->isize;
  313. latestVersion = jNode->version;
  314. }
  315. if(dest) {
  316. src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
  317. /* ignore data behind latest known EOF */
  318. if (jNode->offset > totalSize)
  319. continue;
  320. lDest = (char *) (dest + jNode->offset);
  321. #if 0
  322. putLabeledWord("read_inode: src = ", src);
  323. putLabeledWord("read_inode: dest = ", lDest);
  324. #endif
  325. switch (jNode->compr) {
  326. case JFFS2_COMPR_NONE:
  327. ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
  328. break;
  329. case JFFS2_COMPR_ZERO:
  330. ret = 0;
  331. for (i = 0; i < jNode->dsize; i++)
  332. *(lDest++) = 0;
  333. break;
  334. case JFFS2_COMPR_RTIME:
  335. ret = 0;
  336. rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
  337. break;
  338. case JFFS2_COMPR_DYNRUBIN:
  339. /* this is slow but it works */
  340. ret = 0;
  341. dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
  342. break;
  343. case JFFS2_COMPR_ZLIB:
  344. ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
  345. break;
  346. default:
  347. /* unknown */
  348. putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
  349. return -1;
  350. break;
  351. }
  352. }
  353. #if 0
  354. putLabeledWord("read_inode: totalSize = ", totalSize);
  355. putLabeledWord("read_inode: compr ret = ", ret);
  356. #endif
  357. }
  358. counter++;
  359. }
  360. #if 0
  361. putLabeledWord("read_inode: returning = ", totalSize);
  362. #endif
  363. return totalSize;
  364. }
  365. /* find the inode from the slashless name given a parent */
  366. static u32
  367. jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
  368. {
  369. struct b_node *b;
  370. struct jffs2_raw_dirent *jDir;
  371. int len;
  372. u32 counter;
  373. u32 version = 0;
  374. u32 inode = 0;
  375. /* name is assumed slash free */
  376. len = strlen(name);
  377. counter = 0;
  378. /* we need to search all and return the inode with the highest version */
  379. for(b = pL->dir.listHead; b; b = b->next, counter++) {
  380. jDir = (struct jffs2_raw_dirent *) (b->offset);
  381. if ((pino == jDir->pino) && (len == jDir->nsize) &&
  382. (jDir->ino) && /* 0 for unlink */
  383. (!strncmp(jDir->name, name, len))) { /* a match */
  384. if (jDir->version < version) continue;
  385. if(jDir->version == 0) {
  386. /* Is this legal? */
  387. putstr(" ** WARNING ** ");
  388. putnstr(jDir->name, jDir->nsize);
  389. putstr(" is version 0 (in find, ignoring)\r\n");
  390. } else if(jDir->version == version) {
  391. /* Im pretty sure this isn't ... */
  392. putstr(" ** ERROR ** ");
  393. putnstr(jDir->name, jDir->nsize);
  394. putLabeledWord(" has dup version =", version);
  395. }
  396. inode = jDir->ino;
  397. version = jDir->version;
  398. }
  399. #if 0
  400. putstr("\r\nfind_inode:p&l ->");
  401. putnstr(jDir->name, jDir->nsize);
  402. putstr("\r\n");
  403. putLabeledWord("pino = ", jDir->pino);
  404. putLabeledWord("nsize = ", jDir->nsize);
  405. putLabeledWord("b = ", (u32) b);
  406. putLabeledWord("counter = ", counter);
  407. #endif
  408. }
  409. return inode;
  410. }
  411. static char *mkmodestr(unsigned long mode, char *str)
  412. {
  413. static const char *l = "xwr";
  414. int mask = 1, i;
  415. char c;
  416. switch (mode & S_IFMT) {
  417. case S_IFDIR: str[0] = 'd'; break;
  418. case S_IFBLK: str[0] = 'b'; break;
  419. case S_IFCHR: str[0] = 'c'; break;
  420. case S_IFIFO: str[0] = 'f'; break;
  421. case S_IFLNK: str[0] = 'l'; break;
  422. case S_IFSOCK: str[0] = 's'; break;
  423. case S_IFREG: str[0] = '-'; break;
  424. default: str[0] = '?';
  425. }
  426. for(i = 0; i < 9; i++) {
  427. c = l[i%3];
  428. str[9-i] = (mode & mask)?c:'-';
  429. mask = mask<<1;
  430. }
  431. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  432. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  433. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  434. str[10] = '\0';
  435. return str;
  436. }
  437. static inline void dump_stat(struct stat *st, const char *name)
  438. {
  439. char str[20];
  440. char s[64], *p;
  441. if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
  442. st->st_mtime = 1;
  443. ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
  444. if ((p = strchr(s,'\n')) != NULL) *p = '\0';
  445. if ((p = strchr(s,'\r')) != NULL) *p = '\0';
  446. /*
  447. printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
  448. st->st_size, s, name);
  449. */
  450. printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
  451. }
  452. static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
  453. {
  454. char fname[256];
  455. struct stat st;
  456. if(!d || !i) return -1;
  457. strncpy(fname, d->name, d->nsize);
  458. fname[d->nsize] = '\0';
  459. memset(&st,0,sizeof(st));
  460. st.st_mtime = i->mtime;
  461. st.st_mode = i->mode;
  462. st.st_ino = i->ino;
  463. /* neither dsize nor isize help us.. do it the long way */
  464. st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
  465. dump_stat(&st, fname);
  466. if (d->type == DT_LNK) {
  467. unsigned char *src = (unsigned char *) (&i[1]);
  468. putstr(" -> ");
  469. putnstr(src, (int)i->dsize);
  470. }
  471. putstr("\r\n");
  472. return 0;
  473. }
  474. /* list inodes with the given pino */
  475. static u32
  476. jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
  477. {
  478. struct b_node *b;
  479. struct jffs2_raw_dirent *jDir;
  480. for (b = pL->dir.listHead; b; b = b->next) {
  481. jDir = (struct jffs2_raw_dirent *) (b->offset);
  482. if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
  483. u32 i_version = 0;
  484. struct jffs2_raw_inode *jNode, *i = NULL;
  485. struct b_node *b2 = pL->frag.listHead;
  486. while (b2) {
  487. jNode = (struct jffs2_raw_inode *) (b2->offset);
  488. if (jNode->ino == jDir->ino
  489. && jNode->version >= i_version)
  490. i = jNode;
  491. b2 = b2->next;
  492. }
  493. dump_inode(pL, jDir, i);
  494. }
  495. }
  496. return pino;
  497. }
  498. static u32
  499. jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
  500. {
  501. int i;
  502. char tmp[256];
  503. char working_tmp[256];
  504. char *c;
  505. /* discard any leading slash */
  506. i = 0;
  507. while (fname[i] == '/')
  508. i++;
  509. strcpy(tmp, &fname[i]);
  510. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  511. {
  512. strncpy(working_tmp, tmp, c - tmp);
  513. working_tmp[c - tmp] = '\0';
  514. #if 0
  515. putstr("search_inode: tmp = ");
  516. putstr(tmp);
  517. putstr("\r\n");
  518. putstr("search_inode: wtmp = ");
  519. putstr(working_tmp);
  520. putstr("\r\n");
  521. putstr("search_inode: c = ");
  522. putstr(c);
  523. putstr("\r\n");
  524. #endif
  525. for (i = 0; i < strlen(c) - 1; i++)
  526. tmp[i] = c[i + 1];
  527. tmp[i] = '\0';
  528. #if 0
  529. putstr("search_inode: post tmp = ");
  530. putstr(tmp);
  531. putstr("\r\n");
  532. #endif
  533. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
  534. putstr("find_inode failed for name=");
  535. putstr(working_tmp);
  536. putstr("\r\n");
  537. return 0;
  538. }
  539. }
  540. /* this is for the bare filename, directories have already been mapped */
  541. if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  542. putstr("find_inode failed for name=");
  543. putstr(tmp);
  544. putstr("\r\n");
  545. return 0;
  546. }
  547. return pino;
  548. }
  549. static u32
  550. jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
  551. {
  552. struct b_node *b;
  553. struct b_node *b2;
  554. struct jffs2_raw_dirent *jDir;
  555. struct jffs2_raw_inode *jNode;
  556. struct jffs2_raw_dirent *jDirFound = NULL;
  557. char tmp[256];
  558. u32 version = 0;
  559. u32 pino;
  560. unsigned char *src;
  561. /* we need to search all and return the inode with the highest version */
  562. for(b = pL->dir.listHead; b; b = b->next) {
  563. jDir = (struct jffs2_raw_dirent *) (b->offset);
  564. if (ino == jDir->ino) {
  565. if(jDir->version < version) continue;
  566. if(jDir->version == 0) {
  567. /* Is this legal? */
  568. putstr(" ** WARNING ** ");
  569. putnstr(jDir->name, jDir->nsize);
  570. putstr(" is version 0 (in resolve, ignoring)\r\n");
  571. } else if(jDir->version == version) {
  572. /* Im pretty sure this isn't ... */
  573. putstr(" ** ERROR ** ");
  574. putnstr(jDir->name, jDir->nsize);
  575. putLabeledWord(" has dup version (resolve) = ",
  576. version);
  577. }
  578. jDirFound = jDir;
  579. version = jDir->version;
  580. }
  581. }
  582. /* now we found the right entry again. (shoulda returned inode*) */
  583. if (jDirFound->type != DT_LNK)
  584. return jDirFound->ino;
  585. /* it's a soft link so we follow it again. */
  586. b2 = pL->frag.listHead;
  587. while (b2) {
  588. jNode = (struct jffs2_raw_inode *) (b2->offset);
  589. if (jNode->ino == jDirFound->ino) {
  590. src = (unsigned char *) (b2->offset + sizeof(struct jffs2_raw_inode));
  591. #if 0
  592. putLabeledWord("\t\t dsize = ", jNode->dsize);
  593. putstr("\t\t target = ");
  594. putnstr(src, jNode->dsize);
  595. putstr("\r\n");
  596. #endif
  597. strncpy(tmp, src, jNode->dsize);
  598. tmp[jNode->dsize] = '\0';
  599. break;
  600. }
  601. b2 = b2->next;
  602. }
  603. /* ok so the name of the new file to find is in tmp */
  604. /* if it starts with a slash it is root based else shared dirs */
  605. if (tmp[0] == '/')
  606. pino = 1;
  607. else
  608. pino = jDirFound->pino;
  609. return jffs2_1pass_search_inode(pL, tmp, pino);
  610. }
  611. static u32
  612. jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
  613. {
  614. int i;
  615. char tmp[256];
  616. char working_tmp[256];
  617. char *c;
  618. /* discard any leading slash */
  619. i = 0;
  620. while (fname[i] == '/')
  621. i++;
  622. strcpy(tmp, &fname[i]);
  623. working_tmp[0] = '\0';
  624. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  625. {
  626. strncpy(working_tmp, tmp, c - tmp);
  627. working_tmp[c - tmp] = '\0';
  628. for (i = 0; i < strlen(c) - 1; i++)
  629. tmp[i] = c[i + 1];
  630. tmp[i] = '\0';
  631. /* only a failure if we arent looking at top level */
  632. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
  633. (working_tmp[0])) {
  634. putstr("find_inode failed for name=");
  635. putstr(working_tmp);
  636. putstr("\r\n");
  637. return 0;
  638. }
  639. }
  640. if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  641. putstr("find_inode failed for name=");
  642. putstr(tmp);
  643. putstr("\r\n");
  644. return 0;
  645. }
  646. /* this is for the bare filename, directories have already been mapped */
  647. if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
  648. putstr("find_inode failed for name=");
  649. putstr(tmp);
  650. putstr("\r\n");
  651. return 0;
  652. }
  653. return pino;
  654. }
  655. unsigned char
  656. jffs2_1pass_rescan_needed(struct part_info *part)
  657. {
  658. struct b_node *b;
  659. struct jffs2_unknown_node *node;
  660. struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
  661. if (part->jffs2_priv == 0){
  662. DEBUGF ("rescan: First time in use\n");
  663. return 1;
  664. }
  665. /* if we have no list, we need to rescan */
  666. if (pL->frag.listCount == 0) {
  667. DEBUGF ("rescan: fraglist zero\n");
  668. return 1;
  669. }
  670. /* or if we are scanning a new partition */
  671. if (pL->partOffset != part->offset) {
  672. DEBUGF ("rescan: different partition\n");
  673. return 1;
  674. }
  675. /* but suppose someone reflashed a partition at the same offset... */
  676. b = pL->dir.listHead;
  677. while (b) {
  678. node = (struct jffs2_unknown_node *) (b->offset);
  679. if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
  680. DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
  681. (unsigned long) b->offset);
  682. return 1;
  683. }
  684. b = b->next;
  685. }
  686. return 0;
  687. }
  688. #ifdef DEBUG_FRAGMENTS
  689. static void
  690. dump_fragments(struct b_lists *pL)
  691. {
  692. struct b_node *b;
  693. struct jffs2_raw_inode *jNode;
  694. putstr("\r\n\r\n******The fragment Entries******\r\n");
  695. b = pL->frag.listHead;
  696. while (b) {
  697. jNode = (struct jffs2_raw_inode *) (b->offset);
  698. putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
  699. putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
  700. putLabeledWord("\tbuild_list: inode = ", jNode->ino);
  701. putLabeledWord("\tbuild_list: version = ", jNode->version);
  702. putLabeledWord("\tbuild_list: isize = ", jNode->isize);
  703. putLabeledWord("\tbuild_list: atime = ", jNode->atime);
  704. putLabeledWord("\tbuild_list: offset = ", jNode->offset);
  705. putLabeledWord("\tbuild_list: csize = ", jNode->csize);
  706. putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
  707. putLabeledWord("\tbuild_list: compr = ", jNode->compr);
  708. putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
  709. putLabeledWord("\tbuild_list: flags = ", jNode->flags);
  710. putLabeledWord("\tbuild_list: offset = ", b->offset); // FIXME: ? [RS]
  711. b = b->next;
  712. }
  713. }
  714. #endif
  715. #ifdef DEBUG_DIRENTS
  716. static void
  717. dump_dirents(struct b_lists *pL)
  718. {
  719. struct b_node *b;
  720. struct jffs2_raw_dirent *jDir;
  721. putstr("\r\n\r\n******The directory Entries******\r\n");
  722. b = pL->dir.listHead;
  723. while (b) {
  724. jDir = (struct jffs2_raw_dirent *) (b->offset);
  725. putstr("\r\n");
  726. putnstr(jDir->name, jDir->nsize);
  727. putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
  728. putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
  729. putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
  730. putLabeledWord("\tbuild_list: pino = ", jDir->pino);
  731. putLabeledWord("\tbuild_list: version = ", jDir->version);
  732. putLabeledWord("\tbuild_list: ino = ", jDir->ino);
  733. putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
  734. putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
  735. putLabeledWord("\tbuild_list: type = ", jDir->type);
  736. putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
  737. putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
  738. putLabeledWord("\tbuild_list: offset = ", b->offset); // FIXME: ? [RS]
  739. b = b->next;
  740. }
  741. }
  742. #endif
  743. static u32
  744. jffs2_1pass_build_lists(struct part_info * part)
  745. {
  746. struct b_lists *pL;
  747. struct jffs2_unknown_node *node;
  748. u32 offset, oldoffset = 0;
  749. u32 max = part->size - sizeof(struct jffs2_raw_inode);
  750. u32 counter = 0;
  751. u32 counter4 = 0;
  752. u32 counterF = 0;
  753. u32 counterN = 0;
  754. /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
  755. /* jffs2 list building enterprise nope. in newer versions the overhead is */
  756. /* only about 5 %. not enough to inconvenience people for. */
  757. /* lcd_off(); */
  758. /* if we are building a list we need to refresh the cache. */
  759. jffs_init_1pass_list(part);
  760. pL = (struct b_lists *)part->jffs2_priv;
  761. pL->partOffset = part->offset;
  762. offset = 0;
  763. printf("Scanning JFFS2 FS: ");
  764. /* start at the beginning of the partition */
  765. while (offset < max) {
  766. if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
  767. printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
  768. oldoffset = offset;
  769. }
  770. node = (struct jffs2_unknown_node *) (part->offset + offset);
  771. if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
  772. /* if its a fragment add it */
  773. if (node->nodetype == JFFS2_NODETYPE_INODE &&
  774. inode_crc((struct jffs2_raw_inode *) node)) {
  775. if (insert_node(&pL->frag, (u32) part->offset +
  776. offset) == NULL)
  777. return 0;
  778. } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
  779. dirent_crc((struct jffs2_raw_dirent *) node) &&
  780. dirent_name_crc((struct jffs2_raw_dirent *) node)) {
  781. if (! (counterN%100))
  782. printf("\b\b. ");
  783. if (insert_node(&pL->dir, (u32) part->offset +
  784. offset) == NULL)
  785. return 0;
  786. counterN++;
  787. } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
  788. if (node->totlen != sizeof(struct jffs2_unknown_node))
  789. printf("OOPS Cleanmarker has bad size "
  790. "%d != %d\n", node->totlen,
  791. sizeof(struct jffs2_unknown_node));
  792. } else {
  793. printf("Unknown node type: %x len %d "
  794. "offset 0x%x\n", node->nodetype,
  795. node->totlen, offset);
  796. }
  797. offset += ((node->totlen + 3) & ~3);
  798. counterF++;
  799. } else if (node->magic == JFFS2_EMPTY_BITMASK &&
  800. node->nodetype == JFFS2_EMPTY_BITMASK) {
  801. offset = jffs2_scan_empty(offset, part);
  802. } else { /* if we know nothing, we just step and look. */
  803. offset += 4;
  804. counter4++;
  805. }
  806. /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
  807. }
  808. putstr("\b\b done.\r\n"); /* close off the dots */
  809. /* turn the lcd back on. */
  810. /* splash(); */
  811. #if 0
  812. putLabeledWord("dir entries = ", pL->dir.listCount);
  813. putLabeledWord("frag entries = ", pL->frag.listCount);
  814. putLabeledWord("+4 increments = ", counter4);
  815. putLabeledWord("+file_offset increments = ", counterF);
  816. #endif
  817. #ifdef DEBUG_DIRENTS
  818. dump_dirents(pL);
  819. #endif
  820. #ifdef DEBUG_FRAGMENTS
  821. dump_fragments(pL);
  822. #endif
  823. /* give visual feedback that we are done scanning the flash */
  824. led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
  825. return 1;
  826. }
  827. static u32
  828. jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
  829. {
  830. struct b_node *b;
  831. struct jffs2_raw_inode *jNode;
  832. int i;
  833. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  834. piL->compr_info[i].num_frags = 0;
  835. piL->compr_info[i].compr_sum = 0;
  836. piL->compr_info[i].decompr_sum = 0;
  837. }
  838. b = pL->frag.listHead;
  839. while (b) {
  840. jNode = (struct jffs2_raw_inode *) (b->offset);
  841. if (jNode->compr < JFFS2_NUM_COMPR) {
  842. piL->compr_info[jNode->compr].num_frags++;
  843. piL->compr_info[jNode->compr].compr_sum += jNode->csize;
  844. piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
  845. }
  846. b = b->next;
  847. }
  848. return 0;
  849. }
  850. static struct b_lists *
  851. jffs2_get_list(struct part_info * part, const char *who)
  852. {
  853. if (jffs2_1pass_rescan_needed(part)) {
  854. if (!jffs2_1pass_build_lists(part)) {
  855. printf("%s: Failed to scan JFFSv2 file structure\n", who);
  856. return NULL;
  857. }
  858. }
  859. return (struct b_lists *)part->jffs2_priv;
  860. }
  861. /* Print directory / file contents */
  862. u32
  863. jffs2_1pass_ls(struct part_info * part, const char *fname)
  864. {
  865. struct b_lists *pl;
  866. long ret = 0;
  867. u32 inode;
  868. if (! (pl = jffs2_get_list(part, "ls")))
  869. return 0;
  870. if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
  871. putstr("ls: Failed to scan jffs2 file structure\r\n");
  872. return 0;
  873. }
  874. #if 0
  875. putLabeledWord("found file at inode = ", inode);
  876. putLabeledWord("read_inode returns = ", ret);
  877. #endif
  878. return ret;
  879. }
  880. /* Load a file from flash into memory. fname can be a full path */
  881. u32
  882. jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
  883. {
  884. struct b_lists *pl;
  885. long ret = 0;
  886. u32 inode;
  887. if (! (pl = jffs2_get_list(part, "load")))
  888. return 0;
  889. if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
  890. putstr("load: Failed to find inode\r\n");
  891. return 0;
  892. }
  893. /* Resolve symlinks */
  894. if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
  895. putstr("load: Failed to resolve inode structure\r\n");
  896. return 0;
  897. }
  898. if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
  899. putstr("load: Failed to read inode\r\n");
  900. return 0;
  901. }
  902. DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
  903. (unsigned long) dest, ret);
  904. return ret;
  905. }
  906. /* Return information about the fs on this partition */
  907. u32
  908. jffs2_1pass_info(struct part_info * part)
  909. {
  910. struct b_jffs2_info info;
  911. struct b_lists *pl;
  912. int i;
  913. if (! (pl = jffs2_get_list(part, "info")))
  914. return 0;
  915. jffs2_1pass_fill_info(pl, &info);
  916. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  917. printf("Compression: %s\n", compr_names[i]);
  918. printf("\tfrag count: %d\n", info.compr_info[i].num_frags);
  919. printf("\tcompressed sum: %d\n", info.compr_info[i].compr_sum);
  920. printf("\tuncompressed sum: %d\n", info.compr_info[i].decompr_sum);
  921. }
  922. return 1;
  923. }
  924. #endif /* CFG_CMD_JFFS2 */