jffs2_1pass.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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. /* keeps pointer to currentlu processed partition */
  130. static struct part_info *current_part;
  131. #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
  132. #include <nand.h>
  133. /*
  134. * Support for jffs2 on top of NAND-flash
  135. *
  136. * NAND memory isn't mapped in processor's address space,
  137. * so data should be fetched from flash before
  138. * being processed. This is exactly what functions declared
  139. * here do.
  140. *
  141. */
  142. /* info for NAND chips, defined in drivers/nand/nand.c */
  143. extern nand_info_t nand_info[];
  144. #define NAND_PAGE_SIZE 512
  145. #define NAND_PAGE_SHIFT 9
  146. #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
  147. #ifndef NAND_CACHE_PAGES
  148. #define NAND_CACHE_PAGES 16
  149. #endif
  150. #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
  151. #ifdef CFG_NAND_LEGACY
  152. static u8* nand_cache = NULL;
  153. static u32 nand_cache_off = (u32)-1;
  154. static int read_nand_cached(u32 off, u32 size, u_char *buf)
  155. {
  156. struct mtdids *id = current_part->dev->id;
  157. u32 bytes_read = 0;
  158. ulong retlen;
  159. int cpy_bytes;
  160. while (bytes_read < size) {
  161. if ((off + bytes_read < nand_cache_off) ||
  162. (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) {
  163. nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
  164. if (!nand_cache) {
  165. /* This memory never gets freed but 'cause
  166. it's a bootloader, nobody cares */
  167. nand_cache = malloc(NAND_CACHE_SIZE);
  168. if (!nand_cache) {
  169. printf("read_nand_cached: can't alloc cache size %d bytes\n",
  170. NAND_CACHE_SIZE);
  171. return -1;
  172. }
  173. }
  174. retlen = NAND_CACHE_SIZE;
  175. if (nand_read(&nand_info[id->num], nand_cache_off,
  176. &retlen, nand_cache) != 0 ||
  177. retlen != NAND_CACHE_SIZE) {
  178. printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
  179. nand_cache_off, NAND_CACHE_SIZE);
  180. return -1;
  181. }
  182. }
  183. cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
  184. if (cpy_bytes > size - bytes_read)
  185. cpy_bytes = size - bytes_read;
  186. memcpy(buf + bytes_read,
  187. nand_cache + off + bytes_read - nand_cache_off,
  188. cpy_bytes);
  189. bytes_read += cpy_bytes;
  190. }
  191. return bytes_read;
  192. }
  193. static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
  194. {
  195. u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
  196. if (NULL == buf) {
  197. printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
  198. return NULL;
  199. }
  200. if (read_nand_cached(off, size, buf) < 0) {
  201. if (!ext_buf)
  202. free(buf);
  203. return NULL;
  204. }
  205. return buf;
  206. }
  207. static void *get_node_mem_nand(u32 off)
  208. {
  209. struct jffs2_unknown_node node;
  210. void *ret = NULL;
  211. if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
  212. return NULL;
  213. if (!(ret = get_fl_mem_nand(off, node.magic ==
  214. JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
  215. NULL))) {
  216. printf("off = %#x magic %#x type %#x node.totlen = %d\n",
  217. off, node.magic, node.nodetype, node.totlen);
  218. }
  219. return ret;
  220. }
  221. static void put_fl_mem_nand(void *buf)
  222. {
  223. free(buf);
  224. }
  225. #endif /* CFG_NAND_LEGACY */
  226. #endif /* #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
  227. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  228. /*
  229. * Support for jffs2 on top of NOR-flash
  230. *
  231. * NOR flash memory is mapped in processor's address space,
  232. * just return address.
  233. */
  234. static inline void *get_fl_mem_nor(u32 off)
  235. {
  236. u32 addr = off;
  237. struct mtdids *id = current_part->dev->id;
  238. extern flash_info_t flash_info[];
  239. flash_info_t *flash = &flash_info[id->num];
  240. addr += flash->start[0];
  241. return (void*)addr;
  242. }
  243. static inline void *get_node_mem_nor(u32 off)
  244. {
  245. return (void*)get_fl_mem_nor(off);
  246. }
  247. #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FLASH) */
  248. /*
  249. * Generic jffs2 raw memory and node read routines.
  250. *
  251. */
  252. static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
  253. {
  254. struct mtdids *id = current_part->dev->id;
  255. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  256. if (id->type == MTD_DEV_TYPE_NOR)
  257. return get_fl_mem_nor(off);
  258. #endif
  259. #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  260. if (id->type == MTD_DEV_TYPE_NAND)
  261. return get_fl_mem_nand(off, size, ext_buf);
  262. #endif
  263. printf("get_fl_mem: unknown device type, using raw offset!\n");
  264. return (void*)off;
  265. }
  266. static inline void *get_node_mem(u32 off)
  267. {
  268. struct mtdids *id = current_part->dev->id;
  269. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  270. if (id->type == MTD_DEV_TYPE_NOR)
  271. return get_node_mem_nor(off);
  272. #endif
  273. #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  274. if (id->type == MTD_DEV_TYPE_NAND)
  275. return get_node_mem_nand(off);
  276. #endif
  277. printf("get_node_mem: unknown device type, using raw offset!\n");
  278. return (void*)off;
  279. }
  280. static inline void put_fl_mem(void *buf)
  281. {
  282. #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) && defined(CFG_NAND_LEGACY)
  283. struct mtdids *id = current_part->dev->id;
  284. if (id->type == MTD_DEV_TYPE_NAND)
  285. return put_fl_mem_nand(buf);
  286. #endif
  287. }
  288. /* Compression names */
  289. static char *compr_names[] = {
  290. "NONE",
  291. "ZERO",
  292. "RTIME",
  293. "RUBINMIPS",
  294. "COPY",
  295. "DYNRUBIN",
  296. "ZLIB",
  297. #if defined(CONFIG_JFFS2_LZO_LZARI)
  298. "LZO",
  299. "LZARI",
  300. #endif
  301. };
  302. /* Spinning wheel */
  303. static char spinner[] = { '|', '/', '-', '\\' };
  304. /* Memory management */
  305. struct mem_block {
  306. u32 index;
  307. struct mem_block *next;
  308. struct b_node nodes[NODE_CHUNK];
  309. };
  310. static void
  311. free_nodes(struct b_list *list)
  312. {
  313. while (list->listMemBase != NULL) {
  314. struct mem_block *next = list->listMemBase->next;
  315. free( list->listMemBase );
  316. list->listMemBase = next;
  317. }
  318. }
  319. static struct b_node *
  320. add_node(struct b_list *list)
  321. {
  322. u32 index = 0;
  323. struct mem_block *memBase;
  324. struct b_node *b;
  325. memBase = list->listMemBase;
  326. if (memBase != NULL)
  327. index = memBase->index;
  328. #if 0
  329. putLabeledWord("add_node: index = ", index);
  330. putLabeledWord("add_node: memBase = ", list->listMemBase);
  331. #endif
  332. if (memBase == NULL || index >= NODE_CHUNK) {
  333. /* we need more space before we continue */
  334. memBase = mmalloc(sizeof(struct mem_block));
  335. if (memBase == NULL) {
  336. putstr("add_node: malloc failed\n");
  337. return NULL;
  338. }
  339. memBase->next = list->listMemBase;
  340. index = 0;
  341. #if 0
  342. putLabeledWord("add_node: alloced a new membase at ", *memBase);
  343. #endif
  344. }
  345. /* now we have room to add it. */
  346. b = &memBase->nodes[index];
  347. index ++;
  348. memBase->index = index;
  349. list->listMemBase = memBase;
  350. list->listCount++;
  351. return b;
  352. }
  353. static struct b_node *
  354. insert_node(struct b_list *list, u32 offset)
  355. {
  356. struct b_node *new;
  357. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  358. struct b_node *b, *prev;
  359. #endif
  360. if (!(new = add_node(list))) {
  361. putstr("add_node failed!\r\n");
  362. return NULL;
  363. }
  364. new->offset = offset;
  365. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  366. if (list->listTail != NULL && list->listCompare(new, list->listTail))
  367. prev = list->listTail;
  368. else if (list->listLast != NULL && list->listCompare(new, list->listLast))
  369. prev = list->listLast;
  370. else
  371. prev = NULL;
  372. for (b = (prev ? prev->next : list->listHead);
  373. b != NULL && list->listCompare(new, b);
  374. prev = b, b = b->next) {
  375. list->listLoops++;
  376. }
  377. if (b != NULL)
  378. list->listLast = prev;
  379. if (b != NULL) {
  380. new->next = b;
  381. if (prev != NULL)
  382. prev->next = new;
  383. else
  384. list->listHead = new;
  385. } else
  386. #endif
  387. {
  388. new->next = (struct b_node *) NULL;
  389. if (list->listTail != NULL) {
  390. list->listTail->next = new;
  391. list->listTail = new;
  392. } else {
  393. list->listTail = list->listHead = new;
  394. }
  395. }
  396. return new;
  397. }
  398. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  399. /* Sort data entries with the latest version last, so that if there
  400. * is overlapping data the latest version will be used.
  401. */
  402. static int compare_inodes(struct b_node *new, struct b_node *old)
  403. {
  404. struct jffs2_raw_inode ojNew;
  405. struct jffs2_raw_inode ojOld;
  406. struct jffs2_raw_inode *jNew =
  407. (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
  408. struct jffs2_raw_inode *jOld =
  409. (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
  410. return jNew->version > jOld->version;
  411. }
  412. /* Sort directory entries so all entries in the same directory
  413. * with the same name are grouped together, with the latest version
  414. * last. This makes it easy to eliminate all but the latest version
  415. * by marking the previous version dead by setting the inode to 0.
  416. */
  417. static int compare_dirents(struct b_node *new, struct b_node *old)
  418. {
  419. struct jffs2_raw_dirent ojNew;
  420. struct jffs2_raw_dirent ojOld;
  421. struct jffs2_raw_dirent *jNew =
  422. (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
  423. struct jffs2_raw_dirent *jOld =
  424. (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
  425. int cmp;
  426. /* ascending sort by pino */
  427. if (jNew->pino != jOld->pino)
  428. return jNew->pino > jOld->pino;
  429. /* pino is the same, so use ascending sort by nsize, so
  430. * we don't do strncmp unless we really must.
  431. */
  432. if (jNew->nsize != jOld->nsize)
  433. return jNew->nsize > jOld->nsize;
  434. /* length is also the same, so use ascending sort by name
  435. */
  436. cmp = strncmp((char *)jNew->name, (char *)jOld->name, jNew->nsize);
  437. if (cmp != 0)
  438. return cmp > 0;
  439. /* we have duplicate names in this directory, so use ascending
  440. * sort by version
  441. */
  442. if (jNew->version > jOld->version) {
  443. /* since jNew is newer, we know jOld is not valid, so
  444. * mark it with inode 0 and it will not be used
  445. */
  446. jOld->ino = 0;
  447. return 1;
  448. }
  449. return 0;
  450. }
  451. #endif
  452. static u32
  453. jffs2_scan_empty(u32 start_offset, struct part_info *part)
  454. {
  455. char *max = (char *)(part->offset + part->size - sizeof(struct jffs2_raw_inode));
  456. char *offset = (char *)(part->offset + start_offset);
  457. u32 off;
  458. while (offset < max &&
  459. *(u32*)get_fl_mem((u32)offset, sizeof(u32), &off) == 0xFFFFFFFF) {
  460. offset += sizeof(u32);
  461. /* return if spinning is due */
  462. if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
  463. }
  464. return (u32)offset - part->offset;
  465. }
  466. void
  467. jffs2_free_cache(struct part_info *part)
  468. {
  469. struct b_lists *pL;
  470. if (part->jffs2_priv != NULL) {
  471. pL = (struct b_lists *)part->jffs2_priv;
  472. free_nodes(&pL->frag);
  473. free_nodes(&pL->dir);
  474. free(pL);
  475. }
  476. }
  477. static u32
  478. jffs_init_1pass_list(struct part_info *part)
  479. {
  480. struct b_lists *pL;
  481. jffs2_free_cache(part);
  482. if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
  483. pL = (struct b_lists *)part->jffs2_priv;
  484. memset(pL, 0, sizeof(*pL));
  485. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  486. pL->dir.listCompare = compare_dirents;
  487. pL->frag.listCompare = compare_inodes;
  488. #endif
  489. }
  490. return 0;
  491. }
  492. /* find the inode from the slashless name given a parent */
  493. static long
  494. jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
  495. {
  496. struct b_node *b;
  497. struct jffs2_raw_inode *jNode;
  498. u32 totalSize = 0;
  499. u32 latestVersion = 0;
  500. uchar *lDest;
  501. uchar *src;
  502. long ret;
  503. int i;
  504. u32 counter = 0;
  505. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  506. /* Find file size before loading any data, so fragments that
  507. * start past the end of file can be ignored. A fragment
  508. * that is partially in the file is loaded, so extra data may
  509. * be loaded up to the next 4K boundary above the file size.
  510. * This shouldn't cause trouble when loading kernel images, so
  511. * we will live with it.
  512. */
  513. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  514. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  515. sizeof(struct jffs2_raw_inode), NULL);
  516. if ((inode == jNode->ino)) {
  517. /* get actual file length from the newest node */
  518. if (jNode->version >= latestVersion) {
  519. totalSize = jNode->isize;
  520. latestVersion = jNode->version;
  521. }
  522. }
  523. put_fl_mem(jNode);
  524. }
  525. #endif
  526. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  527. jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset);
  528. if ((inode == jNode->ino)) {
  529. #if 0
  530. putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
  531. putLabeledWord("read_inode: inode = ", jNode->ino);
  532. putLabeledWord("read_inode: version = ", jNode->version);
  533. putLabeledWord("read_inode: isize = ", jNode->isize);
  534. putLabeledWord("read_inode: offset = ", jNode->offset);
  535. putLabeledWord("read_inode: csize = ", jNode->csize);
  536. putLabeledWord("read_inode: dsize = ", jNode->dsize);
  537. putLabeledWord("read_inode: compr = ", jNode->compr);
  538. putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
  539. putLabeledWord("read_inode: flags = ", jNode->flags);
  540. #endif
  541. #ifndef CFG_JFFS2_SORT_FRAGMENTS
  542. /* get actual file length from the newest node */
  543. if (jNode->version >= latestVersion) {
  544. totalSize = jNode->isize;
  545. latestVersion = jNode->version;
  546. }
  547. #endif
  548. if(dest) {
  549. src = ((uchar *) jNode) + sizeof(struct jffs2_raw_inode);
  550. /* ignore data behind latest known EOF */
  551. if (jNode->offset > totalSize) {
  552. put_fl_mem(jNode);
  553. continue;
  554. }
  555. lDest = (uchar *) (dest + jNode->offset);
  556. #if 0
  557. putLabeledWord("read_inode: src = ", src);
  558. putLabeledWord("read_inode: dest = ", lDest);
  559. #endif
  560. switch (jNode->compr) {
  561. case JFFS2_COMPR_NONE:
  562. ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
  563. break;
  564. case JFFS2_COMPR_ZERO:
  565. ret = 0;
  566. for (i = 0; i < jNode->dsize; i++)
  567. *(lDest++) = 0;
  568. break;
  569. case JFFS2_COMPR_RTIME:
  570. ret = 0;
  571. rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
  572. break;
  573. case JFFS2_COMPR_DYNRUBIN:
  574. /* this is slow but it works */
  575. ret = 0;
  576. dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
  577. break;
  578. case JFFS2_COMPR_ZLIB:
  579. ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
  580. break;
  581. #if defined(CONFIG_JFFS2_LZO_LZARI)
  582. case JFFS2_COMPR_LZO:
  583. ret = lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
  584. break;
  585. case JFFS2_COMPR_LZARI:
  586. ret = lzari_decompress(src, lDest, jNode->csize, jNode->dsize);
  587. break;
  588. #endif
  589. default:
  590. /* unknown */
  591. putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
  592. put_fl_mem(jNode);
  593. return -1;
  594. break;
  595. }
  596. }
  597. #if 0
  598. putLabeledWord("read_inode: totalSize = ", totalSize);
  599. putLabeledWord("read_inode: compr ret = ", ret);
  600. #endif
  601. }
  602. counter++;
  603. put_fl_mem(jNode);
  604. }
  605. #if 0
  606. putLabeledWord("read_inode: returning = ", totalSize);
  607. #endif
  608. return totalSize;
  609. }
  610. /* find the inode from the slashless name given a parent */
  611. static u32
  612. jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
  613. {
  614. struct b_node *b;
  615. struct jffs2_raw_dirent *jDir;
  616. int len;
  617. u32 counter;
  618. u32 version = 0;
  619. u32 inode = 0;
  620. /* name is assumed slash free */
  621. len = strlen(name);
  622. counter = 0;
  623. /* we need to search all and return the inode with the highest version */
  624. for(b = pL->dir.listHead; b; b = b->next, counter++) {
  625. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  626. if ((pino == jDir->pino) && (len == jDir->nsize) &&
  627. (jDir->ino) && /* 0 for unlink */
  628. (!strncmp((char *)jDir->name, name, len))) { /* a match */
  629. if (jDir->version < version) {
  630. put_fl_mem(jDir);
  631. continue;
  632. }
  633. if (jDir->version == version && inode != 0) {
  634. /* I'm pretty sure this isn't legal */
  635. putstr(" ** ERROR ** ");
  636. putnstr(jDir->name, jDir->nsize);
  637. putLabeledWord(" has dup version =", version);
  638. }
  639. inode = jDir->ino;
  640. version = jDir->version;
  641. }
  642. #if 0
  643. putstr("\r\nfind_inode:p&l ->");
  644. putnstr(jDir->name, jDir->nsize);
  645. putstr("\r\n");
  646. putLabeledWord("pino = ", jDir->pino);
  647. putLabeledWord("nsize = ", jDir->nsize);
  648. putLabeledWord("b = ", (u32) b);
  649. putLabeledWord("counter = ", counter);
  650. #endif
  651. put_fl_mem(jDir);
  652. }
  653. return inode;
  654. }
  655. char *mkmodestr(unsigned long mode, char *str)
  656. {
  657. static const char *l = "xwr";
  658. int mask = 1, i;
  659. char c;
  660. switch (mode & S_IFMT) {
  661. case S_IFDIR: str[0] = 'd'; break;
  662. case S_IFBLK: str[0] = 'b'; break;
  663. case S_IFCHR: str[0] = 'c'; break;
  664. case S_IFIFO: str[0] = 'f'; break;
  665. case S_IFLNK: str[0] = 'l'; break;
  666. case S_IFSOCK: str[0] = 's'; break;
  667. case S_IFREG: str[0] = '-'; break;
  668. default: str[0] = '?';
  669. }
  670. for(i = 0; i < 9; i++) {
  671. c = l[i%3];
  672. str[9-i] = (mode & mask)?c:'-';
  673. mask = mask<<1;
  674. }
  675. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  676. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  677. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  678. str[10] = '\0';
  679. return str;
  680. }
  681. static inline void dump_stat(struct stat *st, const char *name)
  682. {
  683. char str[20];
  684. char s[64], *p;
  685. if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
  686. st->st_mtime = 1;
  687. ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
  688. if ((p = strchr(s,'\n')) != NULL) *p = '\0';
  689. if ((p = strchr(s,'\r')) != NULL) *p = '\0';
  690. /*
  691. printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
  692. st->st_size, s, name);
  693. */
  694. printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
  695. }
  696. static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
  697. {
  698. char fname[256];
  699. struct stat st;
  700. if(!d || !i) return -1;
  701. strncpy(fname, (char *)d->name, d->nsize);
  702. fname[d->nsize] = '\0';
  703. memset(&st,0,sizeof(st));
  704. st.st_mtime = i->mtime;
  705. st.st_mode = i->mode;
  706. st.st_ino = i->ino;
  707. /* neither dsize nor isize help us.. do it the long way */
  708. st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
  709. dump_stat(&st, fname);
  710. if (d->type == DT_LNK) {
  711. unsigned char *src = (unsigned char *) (&i[1]);
  712. putstr(" -> ");
  713. putnstr(src, (int)i->dsize);
  714. }
  715. putstr("\r\n");
  716. return 0;
  717. }
  718. /* list inodes with the given pino */
  719. static u32
  720. jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
  721. {
  722. struct b_node *b;
  723. struct jffs2_raw_dirent *jDir;
  724. for (b = pL->dir.listHead; b; b = b->next) {
  725. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  726. if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
  727. u32 i_version = 0;
  728. struct jffs2_raw_inode ojNode;
  729. struct jffs2_raw_inode *jNode, *i = NULL;
  730. struct b_node *b2 = pL->frag.listHead;
  731. while (b2) {
  732. jNode = (struct jffs2_raw_inode *)
  733. get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
  734. if (jNode->ino == jDir->ino && jNode->version >= i_version) {
  735. if (i)
  736. put_fl_mem(i);
  737. if (jDir->type == DT_LNK)
  738. i = get_node_mem(b2->offset);
  739. else
  740. i = get_fl_mem(b2->offset, sizeof(*i), NULL);
  741. }
  742. b2 = b2->next;
  743. }
  744. dump_inode(pL, jDir, i);
  745. put_fl_mem(i);
  746. }
  747. put_fl_mem(jDir);
  748. }
  749. return pino;
  750. }
  751. static u32
  752. jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
  753. {
  754. int i;
  755. char tmp[256];
  756. char working_tmp[256];
  757. char *c;
  758. /* discard any leading slash */
  759. i = 0;
  760. while (fname[i] == '/')
  761. i++;
  762. strcpy(tmp, &fname[i]);
  763. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  764. {
  765. strncpy(working_tmp, tmp, c - tmp);
  766. working_tmp[c - tmp] = '\0';
  767. #if 0
  768. putstr("search_inode: tmp = ");
  769. putstr(tmp);
  770. putstr("\r\n");
  771. putstr("search_inode: wtmp = ");
  772. putstr(working_tmp);
  773. putstr("\r\n");
  774. putstr("search_inode: c = ");
  775. putstr(c);
  776. putstr("\r\n");
  777. #endif
  778. for (i = 0; i < strlen(c) - 1; i++)
  779. tmp[i] = c[i + 1];
  780. tmp[i] = '\0';
  781. #if 0
  782. putstr("search_inode: post tmp = ");
  783. putstr(tmp);
  784. putstr("\r\n");
  785. #endif
  786. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
  787. putstr("find_inode failed for name=");
  788. putstr(working_tmp);
  789. putstr("\r\n");
  790. return 0;
  791. }
  792. }
  793. /* this is for the bare filename, directories have already been mapped */
  794. if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  795. putstr("find_inode failed for name=");
  796. putstr(tmp);
  797. putstr("\r\n");
  798. return 0;
  799. }
  800. return pino;
  801. }
  802. static u32
  803. jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
  804. {
  805. struct b_node *b;
  806. struct b_node *b2;
  807. struct jffs2_raw_dirent *jDir;
  808. struct jffs2_raw_inode *jNode;
  809. u8 jDirFoundType = 0;
  810. u32 jDirFoundIno = 0;
  811. u32 jDirFoundPino = 0;
  812. char tmp[256];
  813. u32 version = 0;
  814. u32 pino;
  815. unsigned char *src;
  816. /* we need to search all and return the inode with the highest version */
  817. for(b = pL->dir.listHead; b; b = b->next) {
  818. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  819. if (ino == jDir->ino) {
  820. if (jDir->version < version) {
  821. put_fl_mem(jDir);
  822. continue;
  823. }
  824. if (jDir->version == version && jDirFoundType) {
  825. /* I'm pretty sure this isn't legal */
  826. putstr(" ** ERROR ** ");
  827. putnstr(jDir->name, jDir->nsize);
  828. putLabeledWord(" has dup version (resolve) = ",
  829. version);
  830. }
  831. jDirFoundType = jDir->type;
  832. jDirFoundIno = jDir->ino;
  833. jDirFoundPino = jDir->pino;
  834. version = jDir->version;
  835. }
  836. put_fl_mem(jDir);
  837. }
  838. /* now we found the right entry again. (shoulda returned inode*) */
  839. if (jDirFoundType != DT_LNK)
  840. return jDirFoundIno;
  841. /* it's a soft link so we follow it again. */
  842. b2 = pL->frag.listHead;
  843. while (b2) {
  844. jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset);
  845. if (jNode->ino == jDirFoundIno) {
  846. src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
  847. #if 0
  848. putLabeledWord("\t\t dsize = ", jNode->dsize);
  849. putstr("\t\t target = ");
  850. putnstr(src, jNode->dsize);
  851. putstr("\r\n");
  852. #endif
  853. strncpy(tmp, (char *)src, jNode->dsize);
  854. tmp[jNode->dsize] = '\0';
  855. put_fl_mem(jNode);
  856. break;
  857. }
  858. b2 = b2->next;
  859. put_fl_mem(jNode);
  860. }
  861. /* ok so the name of the new file to find is in tmp */
  862. /* if it starts with a slash it is root based else shared dirs */
  863. if (tmp[0] == '/')
  864. pino = 1;
  865. else
  866. pino = jDirFoundPino;
  867. return jffs2_1pass_search_inode(pL, tmp, pino);
  868. }
  869. static u32
  870. jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
  871. {
  872. int i;
  873. char tmp[256];
  874. char working_tmp[256];
  875. char *c;
  876. /* discard any leading slash */
  877. i = 0;
  878. while (fname[i] == '/')
  879. i++;
  880. strcpy(tmp, &fname[i]);
  881. working_tmp[0] = '\0';
  882. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  883. {
  884. strncpy(working_tmp, tmp, c - tmp);
  885. working_tmp[c - tmp] = '\0';
  886. for (i = 0; i < strlen(c) - 1; i++)
  887. tmp[i] = c[i + 1];
  888. tmp[i] = '\0';
  889. /* only a failure if we arent looking at top level */
  890. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
  891. (working_tmp[0])) {
  892. putstr("find_inode failed for name=");
  893. putstr(working_tmp);
  894. putstr("\r\n");
  895. return 0;
  896. }
  897. }
  898. if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  899. putstr("find_inode failed for name=");
  900. putstr(tmp);
  901. putstr("\r\n");
  902. return 0;
  903. }
  904. /* this is for the bare filename, directories have already been mapped */
  905. if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
  906. putstr("find_inode failed for name=");
  907. putstr(tmp);
  908. putstr("\r\n");
  909. return 0;
  910. }
  911. return pino;
  912. }
  913. unsigned char
  914. jffs2_1pass_rescan_needed(struct part_info *part)
  915. {
  916. struct b_node *b;
  917. struct jffs2_unknown_node onode;
  918. struct jffs2_unknown_node *node;
  919. struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
  920. if (part->jffs2_priv == 0){
  921. DEBUGF ("rescan: First time in use\n");
  922. return 1;
  923. }
  924. /* if we have no list, we need to rescan */
  925. if (pL->frag.listCount == 0) {
  926. DEBUGF ("rescan: fraglist zero\n");
  927. return 1;
  928. }
  929. /* but suppose someone reflashed a partition at the same offset... */
  930. b = pL->dir.listHead;
  931. while (b) {
  932. node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
  933. sizeof(onode), &onode);
  934. if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
  935. DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
  936. (unsigned long) b->offset);
  937. return 1;
  938. }
  939. b = b->next;
  940. }
  941. return 0;
  942. }
  943. #ifdef DEBUG_FRAGMENTS
  944. static void
  945. dump_fragments(struct b_lists *pL)
  946. {
  947. struct b_node *b;
  948. struct jffs2_raw_inode ojNode;
  949. struct jffs2_raw_inode *jNode;
  950. putstr("\r\n\r\n******The fragment Entries******\r\n");
  951. b = pL->frag.listHead;
  952. while (b) {
  953. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  954. sizeof(ojNode), &ojNode);
  955. putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
  956. putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
  957. putLabeledWord("\tbuild_list: inode = ", jNode->ino);
  958. putLabeledWord("\tbuild_list: version = ", jNode->version);
  959. putLabeledWord("\tbuild_list: isize = ", jNode->isize);
  960. putLabeledWord("\tbuild_list: atime = ", jNode->atime);
  961. putLabeledWord("\tbuild_list: offset = ", jNode->offset);
  962. putLabeledWord("\tbuild_list: csize = ", jNode->csize);
  963. putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
  964. putLabeledWord("\tbuild_list: compr = ", jNode->compr);
  965. putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
  966. putLabeledWord("\tbuild_list: flags = ", jNode->flags);
  967. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  968. b = b->next;
  969. }
  970. }
  971. #endif
  972. #ifdef DEBUG_DIRENTS
  973. static void
  974. dump_dirents(struct b_lists *pL)
  975. {
  976. struct b_node *b;
  977. struct jffs2_raw_dirent *jDir;
  978. putstr("\r\n\r\n******The directory Entries******\r\n");
  979. b = pL->dir.listHead;
  980. while (b) {
  981. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  982. putstr("\r\n");
  983. putnstr(jDir->name, jDir->nsize);
  984. putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
  985. putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
  986. putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
  987. putLabeledWord("\tbuild_list: pino = ", jDir->pino);
  988. putLabeledWord("\tbuild_list: version = ", jDir->version);
  989. putLabeledWord("\tbuild_list: ino = ", jDir->ino);
  990. putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
  991. putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
  992. putLabeledWord("\tbuild_list: type = ", jDir->type);
  993. putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
  994. putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
  995. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  996. b = b->next;
  997. put_fl_mem(jDir);
  998. }
  999. }
  1000. #endif
  1001. static u32
  1002. jffs2_1pass_build_lists(struct part_info * part)
  1003. {
  1004. struct b_lists *pL;
  1005. struct jffs2_unknown_node *node;
  1006. u32 offset, oldoffset = 0;
  1007. u32 max = part->size - sizeof(struct jffs2_raw_inode);
  1008. u32 counter = 0;
  1009. u32 counter4 = 0;
  1010. u32 counterF = 0;
  1011. u32 counterN = 0;
  1012. /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
  1013. /* jffs2 list building enterprise nope. in newer versions the overhead is */
  1014. /* only about 5 %. not enough to inconvenience people for. */
  1015. /* lcd_off(); */
  1016. /* if we are building a list we need to refresh the cache. */
  1017. jffs_init_1pass_list(part);
  1018. pL = (struct b_lists *)part->jffs2_priv;
  1019. offset = 0;
  1020. puts ("Scanning JFFS2 FS: ");
  1021. /* start at the beginning of the partition */
  1022. while (offset < max) {
  1023. if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
  1024. printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
  1025. oldoffset = offset;
  1026. }
  1027. node = (struct jffs2_unknown_node *) get_node_mem((u32)part->offset + offset);
  1028. if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
  1029. /* if its a fragment add it */
  1030. if (node->nodetype == JFFS2_NODETYPE_INODE &&
  1031. inode_crc((struct jffs2_raw_inode *) node)) {
  1032. if (insert_node(&pL->frag, (u32) part->offset +
  1033. offset) == NULL) {
  1034. put_fl_mem(node);
  1035. return 0;
  1036. }
  1037. } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
  1038. dirent_crc((struct jffs2_raw_dirent *) node) &&
  1039. dirent_name_crc((struct jffs2_raw_dirent *) node)) {
  1040. if (! (counterN%100))
  1041. puts ("\b\b. ");
  1042. if (insert_node(&pL->dir, (u32) part->offset +
  1043. offset) == NULL) {
  1044. put_fl_mem(node);
  1045. return 0;
  1046. }
  1047. counterN++;
  1048. } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
  1049. if (node->totlen != sizeof(struct jffs2_unknown_node))
  1050. printf("OOPS Cleanmarker has bad size "
  1051. "%d != %d\n", node->totlen,
  1052. sizeof(struct jffs2_unknown_node));
  1053. } else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
  1054. if (node->totlen < sizeof(struct jffs2_unknown_node))
  1055. printf("OOPS Padding has bad size "
  1056. "%d < %d\n", node->totlen,
  1057. sizeof(struct jffs2_unknown_node));
  1058. } else {
  1059. printf("Unknown node type: %x len %d "
  1060. "offset 0x%x\n", node->nodetype,
  1061. node->totlen, offset);
  1062. }
  1063. offset += ((node->totlen + 3) & ~3);
  1064. counterF++;
  1065. } else if (node->magic == JFFS2_EMPTY_BITMASK &&
  1066. node->nodetype == JFFS2_EMPTY_BITMASK) {
  1067. offset = jffs2_scan_empty(offset, part);
  1068. } else { /* if we know nothing, we just step and look. */
  1069. offset += 4;
  1070. counter4++;
  1071. }
  1072. /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
  1073. put_fl_mem(node);
  1074. }
  1075. putstr("\b\b done.\r\n"); /* close off the dots */
  1076. /* turn the lcd back on. */
  1077. /* splash(); */
  1078. #if 0
  1079. putLabeledWord("dir entries = ", pL->dir.listCount);
  1080. putLabeledWord("frag entries = ", pL->frag.listCount);
  1081. putLabeledWord("+4 increments = ", counter4);
  1082. putLabeledWord("+file_offset increments = ", counterF);
  1083. #endif
  1084. #ifdef DEBUG_DIRENTS
  1085. dump_dirents(pL);
  1086. #endif
  1087. #ifdef DEBUG_FRAGMENTS
  1088. dump_fragments(pL);
  1089. #endif
  1090. /* give visual feedback that we are done scanning the flash */
  1091. led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
  1092. return 1;
  1093. }
  1094. static u32
  1095. jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
  1096. {
  1097. struct b_node *b;
  1098. struct jffs2_raw_inode ojNode;
  1099. struct jffs2_raw_inode *jNode;
  1100. int i;
  1101. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1102. piL->compr_info[i].num_frags = 0;
  1103. piL->compr_info[i].compr_sum = 0;
  1104. piL->compr_info[i].decompr_sum = 0;
  1105. }
  1106. b = pL->frag.listHead;
  1107. while (b) {
  1108. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  1109. sizeof(ojNode), &ojNode);
  1110. if (jNode->compr < JFFS2_NUM_COMPR) {
  1111. piL->compr_info[jNode->compr].num_frags++;
  1112. piL->compr_info[jNode->compr].compr_sum += jNode->csize;
  1113. piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
  1114. }
  1115. b = b->next;
  1116. }
  1117. return 0;
  1118. }
  1119. static struct b_lists *
  1120. jffs2_get_list(struct part_info * part, const char *who)
  1121. {
  1122. /* copy requested part_info struct pointer to global location */
  1123. current_part = part;
  1124. if (jffs2_1pass_rescan_needed(part)) {
  1125. if (!jffs2_1pass_build_lists(part)) {
  1126. printf("%s: Failed to scan JFFSv2 file structure\n", who);
  1127. return NULL;
  1128. }
  1129. }
  1130. return (struct b_lists *)part->jffs2_priv;
  1131. }
  1132. /* Print directory / file contents */
  1133. u32
  1134. jffs2_1pass_ls(struct part_info * part, const char *fname)
  1135. {
  1136. struct b_lists *pl;
  1137. long ret = 1;
  1138. u32 inode;
  1139. if (! (pl = jffs2_get_list(part, "ls")))
  1140. return 0;
  1141. if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
  1142. putstr("ls: Failed to scan jffs2 file structure\r\n");
  1143. return 0;
  1144. }
  1145. #if 0
  1146. putLabeledWord("found file at inode = ", inode);
  1147. putLabeledWord("read_inode returns = ", ret);
  1148. #endif
  1149. return ret;
  1150. }
  1151. /* Load a file from flash into memory. fname can be a full path */
  1152. u32
  1153. jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
  1154. {
  1155. struct b_lists *pl;
  1156. long ret = 1;
  1157. u32 inode;
  1158. if (! (pl = jffs2_get_list(part, "load")))
  1159. return 0;
  1160. if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
  1161. putstr("load: Failed to find inode\r\n");
  1162. return 0;
  1163. }
  1164. /* Resolve symlinks */
  1165. if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
  1166. putstr("load: Failed to resolve inode structure\r\n");
  1167. return 0;
  1168. }
  1169. if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
  1170. putstr("load: Failed to read inode\r\n");
  1171. return 0;
  1172. }
  1173. DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
  1174. (unsigned long) dest, ret);
  1175. return ret;
  1176. }
  1177. /* Return information about the fs on this partition */
  1178. u32
  1179. jffs2_1pass_info(struct part_info * part)
  1180. {
  1181. struct b_jffs2_info info;
  1182. struct b_lists *pl;
  1183. int i;
  1184. if (! (pl = jffs2_get_list(part, "info")))
  1185. return 0;
  1186. jffs2_1pass_fill_info(pl, &info);
  1187. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1188. printf ("Compression: %s\n"
  1189. "\tfrag count: %d\n"
  1190. "\tcompressed sum: %d\n"
  1191. "\tuncompressed sum: %d\n",
  1192. compr_names[i],
  1193. info.compr_info[i].num_frags,
  1194. info.compr_info[i].compr_sum,
  1195. info.compr_info[i].decompr_sum);
  1196. }
  1197. return 1;
  1198. }
  1199. #endif /* CFG_CMD_JFFS2 */