jffs2_1pass.c 37 KB

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