jffs2_1pass.c 35 KB

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