jffs2_1pass.c 37 KB

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