jffs2_1pass.c 40 KB

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