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