jffs2_1pass.c 37 KB

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