jffs2_1pass.c 35 KB

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