jffs2_1pass.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  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 <div64.h>
  113. #include <linux/compiler.h>
  114. #include <linux/stat.h>
  115. #include <linux/time.h>
  116. #include <u-boot/crc.h>
  117. #include <watchdog.h>
  118. #include <jffs2/jffs2.h>
  119. #include <jffs2/jffs2_1pass.h>
  120. #include <linux/compat.h>
  121. #include <linux/errno.h>
  122. #include "jffs2_private.h"
  123. #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
  124. #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
  125. /* Debugging switches */
  126. #undef DEBUG_DIRENTS /* print directory entry list after scan */
  127. #undef DEBUG_FRAGMENTS /* print fragment list after scan */
  128. #undef DEBUG /* enable debugging messages */
  129. #ifdef DEBUG
  130. # define DEBUGF(fmt,args...) printf(fmt ,##args)
  131. #else
  132. # define DEBUGF(fmt,args...)
  133. #endif
  134. #include "summary.h"
  135. /* keeps pointer to currentlu processed partition */
  136. static struct part_info *current_part;
  137. #if (defined(CONFIG_JFFS2_NAND) && \
  138. defined(CONFIG_CMD_NAND) )
  139. #include <nand.h>
  140. /*
  141. * Support for jffs2 on top of NAND-flash
  142. *
  143. * NAND memory isn't mapped in processor's address space,
  144. * so data should be fetched from flash before
  145. * being processed. This is exactly what functions declared
  146. * here do.
  147. *
  148. */
  149. #define NAND_PAGE_SIZE 512
  150. #define NAND_PAGE_SHIFT 9
  151. #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
  152. #ifndef NAND_CACHE_PAGES
  153. #define NAND_CACHE_PAGES 16
  154. #endif
  155. #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
  156. static u8* nand_cache = NULL;
  157. static u32 nand_cache_off = (u32)-1;
  158. static int read_nand_cached(u32 off, u32 size, u_char *buf)
  159. {
  160. struct mtdids *id = current_part->dev->id;
  161. struct mtd_info *mtd;
  162. u32 bytes_read = 0;
  163. size_t retlen;
  164. int cpy_bytes;
  165. mtd = get_nand_dev_by_index(id->num);
  166. if (!mtd)
  167. return -1;
  168. while (bytes_read < size) {
  169. if ((off + bytes_read < nand_cache_off) ||
  170. (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) {
  171. nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
  172. if (!nand_cache) {
  173. /* This memory never gets freed but 'cause
  174. it's a bootloader, nobody cares */
  175. nand_cache = malloc(NAND_CACHE_SIZE);
  176. if (!nand_cache) {
  177. printf("read_nand_cached: can't alloc cache size %d bytes\n",
  178. NAND_CACHE_SIZE);
  179. return -1;
  180. }
  181. }
  182. retlen = NAND_CACHE_SIZE;
  183. if (nand_read(mtd, nand_cache_off,
  184. &retlen, nand_cache) < 0 ||
  185. retlen != NAND_CACHE_SIZE) {
  186. printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
  187. nand_cache_off, NAND_CACHE_SIZE);
  188. return -1;
  189. }
  190. }
  191. cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
  192. if (cpy_bytes > size - bytes_read)
  193. cpy_bytes = size - bytes_read;
  194. memcpy(buf + bytes_read,
  195. nand_cache + off + bytes_read - nand_cache_off,
  196. cpy_bytes);
  197. bytes_read += cpy_bytes;
  198. }
  199. return bytes_read;
  200. }
  201. static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
  202. {
  203. u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
  204. if (NULL == buf) {
  205. printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
  206. return NULL;
  207. }
  208. if (read_nand_cached(off, size, buf) < 0) {
  209. if (!ext_buf)
  210. free(buf);
  211. return NULL;
  212. }
  213. return buf;
  214. }
  215. static void *get_node_mem_nand(u32 off, void *ext_buf)
  216. {
  217. struct jffs2_unknown_node node;
  218. void *ret = NULL;
  219. if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
  220. return NULL;
  221. if (!(ret = get_fl_mem_nand(off, node.magic ==
  222. JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
  223. ext_buf))) {
  224. printf("off = %#x magic %#x type %#x node.totlen = %d\n",
  225. off, node.magic, node.nodetype, node.totlen);
  226. }
  227. return ret;
  228. }
  229. static void put_fl_mem_nand(void *buf)
  230. {
  231. free(buf);
  232. }
  233. #endif
  234. #if defined(CONFIG_CMD_ONENAND)
  235. #include <linux/mtd/mtd.h>
  236. #include <linux/mtd/onenand.h>
  237. #include <onenand_uboot.h>
  238. #define ONENAND_PAGE_SIZE 2048
  239. #define ONENAND_PAGE_SHIFT 11
  240. #define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
  241. #ifndef ONENAND_CACHE_PAGES
  242. #define ONENAND_CACHE_PAGES 4
  243. #endif
  244. #define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
  245. static u8* onenand_cache;
  246. static u32 onenand_cache_off = (u32)-1;
  247. static int read_onenand_cached(u32 off, u32 size, u_char *buf)
  248. {
  249. u32 bytes_read = 0;
  250. size_t retlen;
  251. int cpy_bytes;
  252. while (bytes_read < size) {
  253. if ((off + bytes_read < onenand_cache_off) ||
  254. (off + bytes_read >= onenand_cache_off + ONENAND_CACHE_SIZE)) {
  255. onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
  256. if (!onenand_cache) {
  257. /* This memory never gets freed but 'cause
  258. it's a bootloader, nobody cares */
  259. onenand_cache = malloc(ONENAND_CACHE_SIZE);
  260. if (!onenand_cache) {
  261. printf("read_onenand_cached: can't alloc cache size %d bytes\n",
  262. ONENAND_CACHE_SIZE);
  263. return -1;
  264. }
  265. }
  266. retlen = ONENAND_CACHE_SIZE;
  267. if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
  268. &retlen, onenand_cache) < 0 ||
  269. retlen != ONENAND_CACHE_SIZE) {
  270. printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
  271. onenand_cache_off, ONENAND_CACHE_SIZE);
  272. return -1;
  273. }
  274. }
  275. cpy_bytes = onenand_cache_off + ONENAND_CACHE_SIZE - (off + bytes_read);
  276. if (cpy_bytes > size - bytes_read)
  277. cpy_bytes = size - bytes_read;
  278. memcpy(buf + bytes_read,
  279. onenand_cache + off + bytes_read - onenand_cache_off,
  280. cpy_bytes);
  281. bytes_read += cpy_bytes;
  282. }
  283. return bytes_read;
  284. }
  285. static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
  286. {
  287. u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
  288. if (NULL == buf) {
  289. printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
  290. return NULL;
  291. }
  292. if (read_onenand_cached(off, size, buf) < 0) {
  293. if (!ext_buf)
  294. free(buf);
  295. return NULL;
  296. }
  297. return buf;
  298. }
  299. static void *get_node_mem_onenand(u32 off, void *ext_buf)
  300. {
  301. struct jffs2_unknown_node node;
  302. void *ret = NULL;
  303. if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
  304. return NULL;
  305. ret = get_fl_mem_onenand(off, node.magic ==
  306. JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
  307. ext_buf);
  308. if (!ret) {
  309. printf("off = %#x magic %#x type %#x node.totlen = %d\n",
  310. off, node.magic, node.nodetype, node.totlen);
  311. }
  312. return ret;
  313. }
  314. static void put_fl_mem_onenand(void *buf)
  315. {
  316. free(buf);
  317. }
  318. #endif
  319. #if defined(CONFIG_CMD_FLASH)
  320. /*
  321. * Support for jffs2 on top of NOR-flash
  322. *
  323. * NOR flash memory is mapped in processor's address space,
  324. * just return address.
  325. */
  326. static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
  327. {
  328. u32 addr = off;
  329. struct mtdids *id = current_part->dev->id;
  330. extern flash_info_t flash_info[];
  331. flash_info_t *flash = &flash_info[id->num];
  332. addr += flash->start[0];
  333. if (ext_buf) {
  334. memcpy(ext_buf, (void *)addr, size);
  335. return ext_buf;
  336. }
  337. return (void*)addr;
  338. }
  339. static inline void *get_node_mem_nor(u32 off, void *ext_buf)
  340. {
  341. struct jffs2_unknown_node *pNode;
  342. /* pNode will point directly to flash - don't provide external buffer
  343. and don't care about size */
  344. pNode = get_fl_mem_nor(off, 0, NULL);
  345. return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
  346. pNode->totlen : sizeof(*pNode), ext_buf);
  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. switch(id->type) {
  357. #if defined(CONFIG_CMD_FLASH)
  358. case MTD_DEV_TYPE_NOR:
  359. return get_fl_mem_nor(off, size, ext_buf);
  360. break;
  361. #endif
  362. #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
  363. case MTD_DEV_TYPE_NAND:
  364. return get_fl_mem_nand(off, size, ext_buf);
  365. break;
  366. #endif
  367. #if defined(CONFIG_CMD_ONENAND)
  368. case MTD_DEV_TYPE_ONENAND:
  369. return get_fl_mem_onenand(off, size, ext_buf);
  370. break;
  371. #endif
  372. default:
  373. printf("get_fl_mem: unknown device type, " \
  374. "using raw offset!\n");
  375. }
  376. return (void*)off;
  377. }
  378. static inline void *get_node_mem(u32 off, void *ext_buf)
  379. {
  380. struct mtdids *id = current_part->dev->id;
  381. switch(id->type) {
  382. #if defined(CONFIG_CMD_FLASH)
  383. case MTD_DEV_TYPE_NOR:
  384. return get_node_mem_nor(off, ext_buf);
  385. break;
  386. #endif
  387. #if defined(CONFIG_JFFS2_NAND) && \
  388. defined(CONFIG_CMD_NAND)
  389. case MTD_DEV_TYPE_NAND:
  390. return get_node_mem_nand(off, ext_buf);
  391. break;
  392. #endif
  393. #if defined(CONFIG_CMD_ONENAND)
  394. case MTD_DEV_TYPE_ONENAND:
  395. return get_node_mem_onenand(off, ext_buf);
  396. break;
  397. #endif
  398. default:
  399. printf("get_fl_mem: unknown device type, " \
  400. "using raw offset!\n");
  401. }
  402. return (void*)off;
  403. }
  404. static inline void put_fl_mem(void *buf, void *ext_buf)
  405. {
  406. struct mtdids *id = current_part->dev->id;
  407. /* If buf is the same as ext_buf, it was provided by the caller -
  408. we shouldn't free it then. */
  409. if (buf == ext_buf)
  410. return;
  411. switch (id->type) {
  412. #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
  413. case MTD_DEV_TYPE_NAND:
  414. return put_fl_mem_nand(buf);
  415. #endif
  416. #if defined(CONFIG_CMD_ONENAND)
  417. case MTD_DEV_TYPE_ONENAND:
  418. return put_fl_mem_onenand(buf);
  419. #endif
  420. }
  421. }
  422. /* Compression names */
  423. static char *compr_names[] = {
  424. "NONE",
  425. "ZERO",
  426. "RTIME",
  427. "RUBINMIPS",
  428. "COPY",
  429. "DYNRUBIN",
  430. "ZLIB",
  431. #if defined(CONFIG_JFFS2_LZO)
  432. "LZO",
  433. #endif
  434. };
  435. /* Memory management */
  436. struct mem_block {
  437. u32 index;
  438. struct mem_block *next;
  439. struct b_node nodes[NODE_CHUNK];
  440. };
  441. static void
  442. free_nodes(struct b_list *list)
  443. {
  444. while (list->listMemBase != NULL) {
  445. struct mem_block *next = list->listMemBase->next;
  446. free( list->listMemBase );
  447. list->listMemBase = next;
  448. }
  449. }
  450. static struct b_node *
  451. add_node(struct b_list *list)
  452. {
  453. u32 index = 0;
  454. struct mem_block *memBase;
  455. struct b_node *b;
  456. memBase = list->listMemBase;
  457. if (memBase != NULL)
  458. index = memBase->index;
  459. #if 0
  460. putLabeledWord("add_node: index = ", index);
  461. putLabeledWord("add_node: memBase = ", list->listMemBase);
  462. #endif
  463. if (memBase == NULL || index >= NODE_CHUNK) {
  464. /* we need more space before we continue */
  465. memBase = mmalloc(sizeof(struct mem_block));
  466. if (memBase == NULL) {
  467. putstr("add_node: malloc failed\n");
  468. return NULL;
  469. }
  470. memBase->next = list->listMemBase;
  471. index = 0;
  472. #if 0
  473. putLabeledWord("add_node: alloced a new membase at ", *memBase);
  474. #endif
  475. }
  476. /* now we have room to add it. */
  477. b = &memBase->nodes[index];
  478. index ++;
  479. memBase->index = index;
  480. list->listMemBase = memBase;
  481. list->listCount++;
  482. return b;
  483. }
  484. static struct b_node *
  485. insert_node(struct b_list *list, u32 offset)
  486. {
  487. struct b_node *new;
  488. if (!(new = add_node(list))) {
  489. putstr("add_node failed!\r\n");
  490. return NULL;
  491. }
  492. new->offset = offset;
  493. new->next = NULL;
  494. if (list->listTail != NULL)
  495. list->listTail->next = new;
  496. else
  497. list->listHead = new;
  498. list->listTail = new;
  499. return new;
  500. }
  501. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  502. /* Sort data entries with the latest version last, so that if there
  503. * is overlapping data the latest version will be used.
  504. */
  505. static int compare_inodes(struct b_node *new, struct b_node *old)
  506. {
  507. /*
  508. * Only read in the version info from flash, not the entire inode.
  509. * This can make a big difference to speed if flash is slow.
  510. */
  511. u32 new_version;
  512. u32 old_version;
  513. get_fl_mem(new->offset + offsetof(struct jffs2_raw_inode, version),
  514. sizeof(new_version), &new_version);
  515. get_fl_mem(old->offset + offsetof(struct jffs2_raw_inode, version),
  516. sizeof(old_version), &old_version);
  517. return new_version > old_version;
  518. }
  519. /* Sort directory entries so all entries in the same directory
  520. * with the same name are grouped together, with the latest version
  521. * last. This makes it easy to eliminate all but the latest version
  522. * by marking the previous version dead by setting the inode to 0.
  523. */
  524. static int compare_dirents(struct b_node *new, struct b_node *old)
  525. {
  526. /*
  527. * Using NULL as the buffer for NOR flash prevents the entire node
  528. * being read. This makes most comparisons much quicker as only one
  529. * or two entries from the node will be used most of the time.
  530. */
  531. struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL);
  532. struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL);
  533. int cmp;
  534. int ret;
  535. if (jNew->pino != jOld->pino) {
  536. /* ascending sort by pino */
  537. ret = jNew->pino > jOld->pino;
  538. } else if (jNew->nsize != jOld->nsize) {
  539. /*
  540. * pino is the same, so use ascending sort by nsize,
  541. * so we don't do strncmp unless we really must.
  542. */
  543. ret = jNew->nsize > jOld->nsize;
  544. } else {
  545. /*
  546. * length is also the same, so use ascending sort by name
  547. */
  548. cmp = strncmp((char *)jNew->name, (char *)jOld->name,
  549. jNew->nsize);
  550. if (cmp != 0) {
  551. ret = cmp > 0;
  552. } else {
  553. /*
  554. * we have duplicate names in this directory,
  555. * so use ascending sort by version
  556. */
  557. ret = jNew->version > jOld->version;
  558. }
  559. }
  560. put_fl_mem(jNew, NULL);
  561. put_fl_mem(jOld, NULL);
  562. return ret;
  563. }
  564. #endif
  565. void
  566. jffs2_free_cache(struct part_info *part)
  567. {
  568. struct b_lists *pL;
  569. if (part->jffs2_priv != NULL) {
  570. pL = (struct b_lists *)part->jffs2_priv;
  571. free_nodes(&pL->frag);
  572. free_nodes(&pL->dir);
  573. free(pL->readbuf);
  574. free(pL);
  575. }
  576. }
  577. static u32
  578. jffs_init_1pass_list(struct part_info *part)
  579. {
  580. struct b_lists *pL;
  581. jffs2_free_cache(part);
  582. if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
  583. pL = (struct b_lists *)part->jffs2_priv;
  584. memset(pL, 0, sizeof(*pL));
  585. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  586. pL->dir.listCompare = compare_dirents;
  587. pL->frag.listCompare = compare_inodes;
  588. #endif
  589. }
  590. return 0;
  591. }
  592. /* find the inode from the slashless name given a parent */
  593. static long
  594. jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
  595. {
  596. struct b_node *b;
  597. struct jffs2_raw_inode *jNode;
  598. u32 totalSize = 0;
  599. u32 latestVersion = 0;
  600. uchar *lDest;
  601. uchar *src;
  602. int i;
  603. u32 counter = 0;
  604. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  605. /* Find file size before loading any data, so fragments that
  606. * start past the end of file can be ignored. A fragment
  607. * that is partially in the file is loaded, so extra data may
  608. * be loaded up to the next 4K boundary above the file size.
  609. * This shouldn't cause trouble when loading kernel images, so
  610. * we will live with it.
  611. */
  612. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  613. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  614. sizeof(struct jffs2_raw_inode), pL->readbuf);
  615. if ((inode == jNode->ino)) {
  616. /* get actual file length from the newest node */
  617. if (jNode->version >= latestVersion) {
  618. totalSize = jNode->isize;
  619. latestVersion = jNode->version;
  620. }
  621. }
  622. put_fl_mem(jNode, pL->readbuf);
  623. }
  624. /*
  625. * If no destination is provided, we are done.
  626. * Just return the total size.
  627. */
  628. if (!dest)
  629. return totalSize;
  630. #endif
  631. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  632. /*
  633. * Copy just the node and not the data at this point,
  634. * since we don't yet know if we need this data.
  635. */
  636. jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset,
  637. sizeof(struct jffs2_raw_inode),
  638. pL->readbuf);
  639. if (inode == jNode->ino) {
  640. #if 0
  641. putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
  642. putLabeledWord("read_inode: inode = ", jNode->ino);
  643. putLabeledWord("read_inode: version = ", jNode->version);
  644. putLabeledWord("read_inode: isize = ", jNode->isize);
  645. putLabeledWord("read_inode: offset = ", jNode->offset);
  646. putLabeledWord("read_inode: csize = ", jNode->csize);
  647. putLabeledWord("read_inode: dsize = ", jNode->dsize);
  648. putLabeledWord("read_inode: compr = ", jNode->compr);
  649. putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
  650. putLabeledWord("read_inode: flags = ", jNode->flags);
  651. #endif
  652. #ifndef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  653. /* get actual file length from the newest node */
  654. if (jNode->version >= latestVersion) {
  655. totalSize = jNode->isize;
  656. latestVersion = jNode->version;
  657. }
  658. #endif
  659. if(dest) {
  660. /*
  661. * Now that the inode has been checked,
  662. * read the entire inode, including data.
  663. */
  664. put_fl_mem(jNode, pL->readbuf);
  665. jNode = (struct jffs2_raw_inode *)
  666. get_node_mem(b->offset, pL->readbuf);
  667. src = ((uchar *)jNode) +
  668. sizeof(struct jffs2_raw_inode);
  669. /* ignore data behind latest known EOF */
  670. if (jNode->offset > totalSize) {
  671. put_fl_mem(jNode, pL->readbuf);
  672. continue;
  673. }
  674. if (b->datacrc == CRC_UNKNOWN)
  675. b->datacrc = data_crc(jNode) ?
  676. CRC_OK : CRC_BAD;
  677. if (b->datacrc == CRC_BAD) {
  678. put_fl_mem(jNode, pL->readbuf);
  679. continue;
  680. }
  681. lDest = (uchar *) (dest + jNode->offset);
  682. #if 0
  683. putLabeledWord("read_inode: src = ", src);
  684. putLabeledWord("read_inode: dest = ", lDest);
  685. #endif
  686. switch (jNode->compr) {
  687. case JFFS2_COMPR_NONE:
  688. ldr_memcpy(lDest, src, jNode->dsize);
  689. break;
  690. case JFFS2_COMPR_ZERO:
  691. for (i = 0; i < jNode->dsize; i++)
  692. *(lDest++) = 0;
  693. break;
  694. case JFFS2_COMPR_RTIME:
  695. rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
  696. break;
  697. case JFFS2_COMPR_DYNRUBIN:
  698. /* this is slow but it works */
  699. dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
  700. break;
  701. case JFFS2_COMPR_ZLIB:
  702. zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
  703. break;
  704. #if defined(CONFIG_JFFS2_LZO)
  705. case JFFS2_COMPR_LZO:
  706. lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
  707. break;
  708. #endif
  709. default:
  710. /* unknown */
  711. putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr);
  712. put_fl_mem(jNode, pL->readbuf);
  713. return -1;
  714. break;
  715. }
  716. }
  717. #if 0
  718. putLabeledWord("read_inode: totalSize = ", totalSize);
  719. #endif
  720. }
  721. counter++;
  722. put_fl_mem(jNode, pL->readbuf);
  723. }
  724. #if 0
  725. putLabeledWord("read_inode: returning = ", totalSize);
  726. #endif
  727. return totalSize;
  728. }
  729. /* find the inode from the slashless name given a parent */
  730. static u32
  731. jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
  732. {
  733. struct b_node *b;
  734. struct jffs2_raw_dirent *jDir;
  735. int len;
  736. u32 counter;
  737. u32 version = 0;
  738. u32 inode = 0;
  739. /* name is assumed slash free */
  740. len = strlen(name);
  741. counter = 0;
  742. /* we need to search all and return the inode with the highest version */
  743. for(b = pL->dir.listHead; b; b = b->next, counter++) {
  744. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  745. pL->readbuf);
  746. if ((pino == jDir->pino) && (len == jDir->nsize) &&
  747. (!strncmp((char *)jDir->name, name, len))) { /* a match */
  748. if (jDir->version < version) {
  749. put_fl_mem(jDir, pL->readbuf);
  750. continue;
  751. }
  752. if (jDir->version == version && inode != 0) {
  753. /* I'm pretty sure this isn't legal */
  754. putstr(" ** ERROR ** ");
  755. putnstr(jDir->name, jDir->nsize);
  756. putLabeledWord(" has dup version =", version);
  757. }
  758. inode = jDir->ino;
  759. version = jDir->version;
  760. }
  761. #if 0
  762. putstr("\r\nfind_inode:p&l ->");
  763. putnstr(jDir->name, jDir->nsize);
  764. putstr("\r\n");
  765. putLabeledWord("pino = ", jDir->pino);
  766. putLabeledWord("nsize = ", jDir->nsize);
  767. putLabeledWord("b = ", (u32) b);
  768. putLabeledWord("counter = ", counter);
  769. #endif
  770. put_fl_mem(jDir, pL->readbuf);
  771. }
  772. return inode;
  773. }
  774. char *mkmodestr(unsigned long mode, char *str)
  775. {
  776. static const char *l = "xwr";
  777. int mask = 1, i;
  778. char c;
  779. switch (mode & S_IFMT) {
  780. case S_IFDIR: str[0] = 'd'; break;
  781. case S_IFBLK: str[0] = 'b'; break;
  782. case S_IFCHR: str[0] = 'c'; break;
  783. case S_IFIFO: str[0] = 'f'; break;
  784. case S_IFLNK: str[0] = 'l'; break;
  785. case S_IFSOCK: str[0] = 's'; break;
  786. case S_IFREG: str[0] = '-'; break;
  787. default: str[0] = '?';
  788. }
  789. for(i = 0; i < 9; i++) {
  790. c = l[i%3];
  791. str[9-i] = (mode & mask)?c:'-';
  792. mask = mask<<1;
  793. }
  794. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  795. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  796. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  797. str[10] = '\0';
  798. return str;
  799. }
  800. static inline void dump_stat(struct stat *st, const char *name)
  801. {
  802. char str[20];
  803. char s[64], *p;
  804. if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
  805. st->st_mtime = 1;
  806. ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
  807. if ((p = strchr(s,'\n')) != NULL) *p = '\0';
  808. if ((p = strchr(s,'\r')) != NULL) *p = '\0';
  809. /*
  810. printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
  811. st->st_size, s, name);
  812. */
  813. printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
  814. }
  815. static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
  816. {
  817. char fname[256];
  818. struct stat st;
  819. if(!d || !i) return -1;
  820. strncpy(fname, (char *)d->name, d->nsize);
  821. fname[d->nsize] = '\0';
  822. memset(&st,0,sizeof(st));
  823. st.st_mtime = i->mtime;
  824. st.st_mode = i->mode;
  825. st.st_ino = i->ino;
  826. st.st_size = i->isize;
  827. dump_stat(&st, fname);
  828. if (d->type == DT_LNK) {
  829. unsigned char *src = (unsigned char *) (&i[1]);
  830. putstr(" -> ");
  831. putnstr(src, (int)i->dsize);
  832. }
  833. putstr("\r\n");
  834. return 0;
  835. }
  836. /* list inodes with the given pino */
  837. static u32
  838. jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
  839. {
  840. struct b_node *b;
  841. struct jffs2_raw_dirent *jDir;
  842. for (b = pL->dir.listHead; b; b = b->next) {
  843. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  844. pL->readbuf);
  845. if (pino == jDir->pino) {
  846. u32 i_version = 0;
  847. struct jffs2_raw_inode *jNode, *i = NULL;
  848. struct b_node *b2;
  849. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  850. /* Check for more recent versions of this file */
  851. int match;
  852. do {
  853. struct b_node *next = b->next;
  854. struct jffs2_raw_dirent *jDirNext;
  855. if (!next)
  856. break;
  857. jDirNext = (struct jffs2_raw_dirent *)
  858. get_node_mem(next->offset, NULL);
  859. match = jDirNext->pino == jDir->pino &&
  860. jDirNext->nsize == jDir->nsize &&
  861. strncmp((char *)jDirNext->name,
  862. (char *)jDir->name,
  863. jDir->nsize) == 0;
  864. if (match) {
  865. /* Use next. It is more recent */
  866. b = next;
  867. /* Update buffer with the new info */
  868. *jDir = *jDirNext;
  869. }
  870. put_fl_mem(jDirNext, NULL);
  871. } while (match);
  872. #endif
  873. if (jDir->ino == 0) {
  874. /* Deleted file */
  875. put_fl_mem(jDir, pL->readbuf);
  876. continue;
  877. }
  878. for (b2 = pL->frag.listHead; b2; b2 = b2->next) {
  879. jNode = (struct jffs2_raw_inode *)
  880. get_fl_mem(b2->offset, sizeof(*jNode),
  881. NULL);
  882. if (jNode->ino == jDir->ino &&
  883. jNode->version >= i_version) {
  884. i_version = jNode->version;
  885. if (i)
  886. put_fl_mem(i, NULL);
  887. if (jDir->type == DT_LNK)
  888. i = get_node_mem(b2->offset,
  889. NULL);
  890. else
  891. i = get_fl_mem(b2->offset,
  892. sizeof(*i),
  893. NULL);
  894. }
  895. put_fl_mem(jNode, NULL);
  896. }
  897. dump_inode(pL, jDir, i);
  898. put_fl_mem(i, NULL);
  899. }
  900. put_fl_mem(jDir, pL->readbuf);
  901. }
  902. return pino;
  903. }
  904. static u32
  905. jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
  906. {
  907. int i;
  908. char tmp[256];
  909. char working_tmp[256];
  910. char *c;
  911. /* discard any leading slash */
  912. i = 0;
  913. while (fname[i] == '/')
  914. i++;
  915. strcpy(tmp, &fname[i]);
  916. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  917. {
  918. strncpy(working_tmp, tmp, c - tmp);
  919. working_tmp[c - tmp] = '\0';
  920. #if 0
  921. putstr("search_inode: tmp = ");
  922. putstr(tmp);
  923. putstr("\r\n");
  924. putstr("search_inode: wtmp = ");
  925. putstr(working_tmp);
  926. putstr("\r\n");
  927. putstr("search_inode: c = ");
  928. putstr(c);
  929. putstr("\r\n");
  930. #endif
  931. for (i = 0; i < strlen(c) - 1; i++)
  932. tmp[i] = c[i + 1];
  933. tmp[i] = '\0';
  934. #if 0
  935. putstr("search_inode: post tmp = ");
  936. putstr(tmp);
  937. putstr("\r\n");
  938. #endif
  939. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
  940. putstr("find_inode failed for name=");
  941. putstr(working_tmp);
  942. putstr("\r\n");
  943. return 0;
  944. }
  945. }
  946. /* this is for the bare filename, directories have already been mapped */
  947. if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  948. putstr("find_inode failed for name=");
  949. putstr(tmp);
  950. putstr("\r\n");
  951. return 0;
  952. }
  953. return pino;
  954. }
  955. static u32
  956. jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
  957. {
  958. struct b_node *b;
  959. struct b_node *b2;
  960. struct jffs2_raw_dirent *jDir;
  961. struct jffs2_raw_inode *jNode;
  962. u8 jDirFoundType = 0;
  963. u32 jDirFoundIno = 0;
  964. u32 jDirFoundPino = 0;
  965. char tmp[256];
  966. u32 version = 0;
  967. u32 pino;
  968. unsigned char *src;
  969. /* we need to search all and return the inode with the highest version */
  970. for(b = pL->dir.listHead; b; b = b->next) {
  971. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  972. pL->readbuf);
  973. if (ino == jDir->ino) {
  974. if (jDir->version < version) {
  975. put_fl_mem(jDir, pL->readbuf);
  976. continue;
  977. }
  978. if (jDir->version == version && jDirFoundType) {
  979. /* I'm pretty sure this isn't legal */
  980. putstr(" ** ERROR ** ");
  981. putnstr(jDir->name, jDir->nsize);
  982. putLabeledWord(" has dup version (resolve) = ",
  983. version);
  984. }
  985. jDirFoundType = jDir->type;
  986. jDirFoundIno = jDir->ino;
  987. jDirFoundPino = jDir->pino;
  988. version = jDir->version;
  989. }
  990. put_fl_mem(jDir, pL->readbuf);
  991. }
  992. /* now we found the right entry again. (shoulda returned inode*) */
  993. if (jDirFoundType != DT_LNK)
  994. return jDirFoundIno;
  995. /* it's a soft link so we follow it again. */
  996. b2 = pL->frag.listHead;
  997. while (b2) {
  998. jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
  999. pL->readbuf);
  1000. if (jNode->ino == jDirFoundIno) {
  1001. src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
  1002. #if 0
  1003. putLabeledWord("\t\t dsize = ", jNode->dsize);
  1004. putstr("\t\t target = ");
  1005. putnstr(src, jNode->dsize);
  1006. putstr("\r\n");
  1007. #endif
  1008. strncpy(tmp, (char *)src, jNode->dsize);
  1009. tmp[jNode->dsize] = '\0';
  1010. put_fl_mem(jNode, pL->readbuf);
  1011. break;
  1012. }
  1013. b2 = b2->next;
  1014. put_fl_mem(jNode, pL->readbuf);
  1015. }
  1016. /* ok so the name of the new file to find is in tmp */
  1017. /* if it starts with a slash it is root based else shared dirs */
  1018. if (tmp[0] == '/')
  1019. pino = 1;
  1020. else
  1021. pino = jDirFoundPino;
  1022. return jffs2_1pass_search_inode(pL, tmp, pino);
  1023. }
  1024. static u32
  1025. jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
  1026. {
  1027. int i;
  1028. char tmp[256];
  1029. char working_tmp[256];
  1030. char *c;
  1031. /* discard any leading slash */
  1032. i = 0;
  1033. while (fname[i] == '/')
  1034. i++;
  1035. strcpy(tmp, &fname[i]);
  1036. working_tmp[0] = '\0';
  1037. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  1038. {
  1039. strncpy(working_tmp, tmp, c - tmp);
  1040. working_tmp[c - tmp] = '\0';
  1041. for (i = 0; i < strlen(c) - 1; i++)
  1042. tmp[i] = c[i + 1];
  1043. tmp[i] = '\0';
  1044. /* only a failure if we arent looking at top level */
  1045. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
  1046. (working_tmp[0])) {
  1047. putstr("find_inode failed for name=");
  1048. putstr(working_tmp);
  1049. putstr("\r\n");
  1050. return 0;
  1051. }
  1052. }
  1053. if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  1054. putstr("find_inode failed for name=");
  1055. putstr(tmp);
  1056. putstr("\r\n");
  1057. return 0;
  1058. }
  1059. /* this is for the bare filename, directories have already been mapped */
  1060. if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
  1061. putstr("find_inode failed for name=");
  1062. putstr(tmp);
  1063. putstr("\r\n");
  1064. return 0;
  1065. }
  1066. return pino;
  1067. }
  1068. unsigned char
  1069. jffs2_1pass_rescan_needed(struct part_info *part)
  1070. {
  1071. struct b_node *b;
  1072. struct jffs2_unknown_node onode;
  1073. struct jffs2_unknown_node *node;
  1074. struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
  1075. if (part->jffs2_priv == 0){
  1076. DEBUGF ("rescan: First time in use\n");
  1077. return 1;
  1078. }
  1079. /* if we have no list, we need to rescan */
  1080. if (pL->frag.listCount == 0) {
  1081. DEBUGF ("rescan: fraglist zero\n");
  1082. return 1;
  1083. }
  1084. /* but suppose someone reflashed a partition at the same offset... */
  1085. b = pL->dir.listHead;
  1086. while (b) {
  1087. node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
  1088. sizeof(onode), &onode);
  1089. if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
  1090. DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
  1091. (unsigned long) b->offset);
  1092. return 1;
  1093. }
  1094. b = b->next;
  1095. }
  1096. return 0;
  1097. }
  1098. #ifdef CONFIG_JFFS2_SUMMARY
  1099. static u32 sum_get_unaligned32(u32 *ptr)
  1100. {
  1101. u32 val;
  1102. u8 *p = (u8 *)ptr;
  1103. val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
  1104. return __le32_to_cpu(val);
  1105. }
  1106. static u16 sum_get_unaligned16(u16 *ptr)
  1107. {
  1108. u16 val;
  1109. u8 *p = (u8 *)ptr;
  1110. val = *p | (*(p + 1) << 8);
  1111. return __le16_to_cpu(val);
  1112. }
  1113. #define dbg_summary(...) do {} while (0);
  1114. /*
  1115. * Process the stored summary information - helper function for
  1116. * jffs2_sum_scan_sumnode()
  1117. */
  1118. static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
  1119. struct jffs2_raw_summary *summary,
  1120. struct b_lists *pL)
  1121. {
  1122. void *sp;
  1123. int i, pass;
  1124. void *ret;
  1125. for (pass = 0; pass < 2; pass++) {
  1126. sp = summary->sum;
  1127. for (i = 0; i < summary->sum_num; i++) {
  1128. struct jffs2_sum_unknown_flash *spu = sp;
  1129. dbg_summary("processing summary index %d\n", i);
  1130. switch (sum_get_unaligned16(&spu->nodetype)) {
  1131. case JFFS2_NODETYPE_INODE: {
  1132. struct jffs2_sum_inode_flash *spi;
  1133. if (pass) {
  1134. spi = sp;
  1135. ret = insert_node(&pL->frag,
  1136. (u32)part->offset +
  1137. offset +
  1138. sum_get_unaligned32(
  1139. &spi->offset));
  1140. if (ret == NULL)
  1141. return -1;
  1142. }
  1143. sp += JFFS2_SUMMARY_INODE_SIZE;
  1144. break;
  1145. }
  1146. case JFFS2_NODETYPE_DIRENT: {
  1147. struct jffs2_sum_dirent_flash *spd;
  1148. spd = sp;
  1149. if (pass) {
  1150. ret = insert_node(&pL->dir,
  1151. (u32) part->offset +
  1152. offset +
  1153. sum_get_unaligned32(
  1154. &spd->offset));
  1155. if (ret == NULL)
  1156. return -1;
  1157. }
  1158. sp += JFFS2_SUMMARY_DIRENT_SIZE(
  1159. spd->nsize);
  1160. break;
  1161. }
  1162. default : {
  1163. uint16_t nodetype = sum_get_unaligned16(
  1164. &spu->nodetype);
  1165. printf("Unsupported node type %x found"
  1166. " in summary!\n",
  1167. nodetype);
  1168. if ((nodetype & JFFS2_COMPAT_MASK) ==
  1169. JFFS2_FEATURE_INCOMPAT)
  1170. return -EIO;
  1171. return -EBADMSG;
  1172. }
  1173. }
  1174. }
  1175. }
  1176. return 0;
  1177. }
  1178. /* Process the summary node - called from jffs2_scan_eraseblock() */
  1179. int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
  1180. struct jffs2_raw_summary *summary, uint32_t sumsize,
  1181. struct b_lists *pL)
  1182. {
  1183. struct jffs2_unknown_node crcnode;
  1184. int ret, __maybe_unused ofs;
  1185. uint32_t crc;
  1186. ofs = part->sector_size - sumsize;
  1187. dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
  1188. offset, offset + ofs, sumsize);
  1189. /* OK, now check for node validity and CRC */
  1190. crcnode.magic = JFFS2_MAGIC_BITMASK;
  1191. crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
  1192. crcnode.totlen = summary->totlen;
  1193. crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
  1194. if (summary->hdr_crc != crc) {
  1195. dbg_summary("Summary node header is corrupt (bad CRC or "
  1196. "no summary at all)\n");
  1197. goto crc_err;
  1198. }
  1199. if (summary->totlen != sumsize) {
  1200. dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
  1201. goto crc_err;
  1202. }
  1203. crc = crc32_no_comp(0, (uchar *)summary,
  1204. sizeof(struct jffs2_raw_summary)-8);
  1205. if (summary->node_crc != crc) {
  1206. dbg_summary("Summary node is corrupt (bad CRC)\n");
  1207. goto crc_err;
  1208. }
  1209. crc = crc32_no_comp(0, (uchar *)summary->sum,
  1210. sumsize - sizeof(struct jffs2_raw_summary));
  1211. if (summary->sum_crc != crc) {
  1212. dbg_summary("Summary node data is corrupt (bad CRC)\n");
  1213. goto crc_err;
  1214. }
  1215. if (summary->cln_mkr)
  1216. dbg_summary("Summary : CLEANMARKER node \n");
  1217. ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
  1218. if (ret == -EBADMSG)
  1219. return 0;
  1220. if (ret)
  1221. return ret; /* real error */
  1222. return 1;
  1223. crc_err:
  1224. putstr("Summary node crc error, skipping summary information.\n");
  1225. return 0;
  1226. }
  1227. #endif /* CONFIG_JFFS2_SUMMARY */
  1228. #ifdef DEBUG_FRAGMENTS
  1229. static void
  1230. dump_fragments(struct b_lists *pL)
  1231. {
  1232. struct b_node *b;
  1233. struct jffs2_raw_inode ojNode;
  1234. struct jffs2_raw_inode *jNode;
  1235. putstr("\r\n\r\n******The fragment Entries******\r\n");
  1236. b = pL->frag.listHead;
  1237. while (b) {
  1238. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  1239. sizeof(ojNode), &ojNode);
  1240. putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
  1241. putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
  1242. putLabeledWord("\tbuild_list: inode = ", jNode->ino);
  1243. putLabeledWord("\tbuild_list: version = ", jNode->version);
  1244. putLabeledWord("\tbuild_list: isize = ", jNode->isize);
  1245. putLabeledWord("\tbuild_list: atime = ", jNode->atime);
  1246. putLabeledWord("\tbuild_list: offset = ", jNode->offset);
  1247. putLabeledWord("\tbuild_list: csize = ", jNode->csize);
  1248. putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
  1249. putLabeledWord("\tbuild_list: compr = ", jNode->compr);
  1250. putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
  1251. putLabeledWord("\tbuild_list: flags = ", jNode->flags);
  1252. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  1253. b = b->next;
  1254. }
  1255. }
  1256. #endif
  1257. #ifdef DEBUG_DIRENTS
  1258. static void
  1259. dump_dirents(struct b_lists *pL)
  1260. {
  1261. struct b_node *b;
  1262. struct jffs2_raw_dirent *jDir;
  1263. putstr("\r\n\r\n******The directory Entries******\r\n");
  1264. b = pL->dir.listHead;
  1265. while (b) {
  1266. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  1267. pL->readbuf);
  1268. putstr("\r\n");
  1269. putnstr(jDir->name, jDir->nsize);
  1270. putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
  1271. putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
  1272. putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
  1273. putLabeledWord("\tbuild_list: pino = ", jDir->pino);
  1274. putLabeledWord("\tbuild_list: version = ", jDir->version);
  1275. putLabeledWord("\tbuild_list: ino = ", jDir->ino);
  1276. putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
  1277. putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
  1278. putLabeledWord("\tbuild_list: type = ", jDir->type);
  1279. putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
  1280. putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
  1281. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  1282. b = b->next;
  1283. put_fl_mem(jDir, pL->readbuf);
  1284. }
  1285. }
  1286. #endif
  1287. #define DEFAULT_EMPTY_SCAN_SIZE 256
  1288. static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
  1289. {
  1290. if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
  1291. return sector_size;
  1292. else
  1293. return DEFAULT_EMPTY_SCAN_SIZE;
  1294. }
  1295. static u32
  1296. jffs2_1pass_build_lists(struct part_info * part)
  1297. {
  1298. struct b_lists *pL;
  1299. struct jffs2_unknown_node *node;
  1300. u32 nr_sectors;
  1301. u32 i;
  1302. u32 counter4 = 0;
  1303. u32 counterF = 0;
  1304. u32 counterN = 0;
  1305. u32 max_totlen = 0;
  1306. u32 buf_size;
  1307. char *buf;
  1308. nr_sectors = lldiv(part->size, part->sector_size);
  1309. /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
  1310. /* jffs2 list building enterprise nope. in newer versions the overhead is */
  1311. /* only about 5 %. not enough to inconvenience people for. */
  1312. /* lcd_off(); */
  1313. /* if we are building a list we need to refresh the cache. */
  1314. jffs_init_1pass_list(part);
  1315. pL = (struct b_lists *)part->jffs2_priv;
  1316. buf = malloc(DEFAULT_EMPTY_SCAN_SIZE);
  1317. puts ("Scanning JFFS2 FS: ");
  1318. /* start at the beginning of the partition */
  1319. for (i = 0; i < nr_sectors; i++) {
  1320. uint32_t sector_ofs = i * part->sector_size;
  1321. uint32_t buf_ofs = sector_ofs;
  1322. uint32_t buf_len;
  1323. uint32_t ofs, prevofs;
  1324. #ifdef CONFIG_JFFS2_SUMMARY
  1325. struct jffs2_sum_marker *sm;
  1326. void *sumptr = NULL;
  1327. uint32_t sumlen;
  1328. int ret;
  1329. #endif
  1330. /* Indicates a sector with a CLEANMARKER was found */
  1331. int clean_sector = 0;
  1332. /* Set buf_size to maximum length */
  1333. buf_size = DEFAULT_EMPTY_SCAN_SIZE;
  1334. WATCHDOG_RESET();
  1335. #ifdef CONFIG_JFFS2_SUMMARY
  1336. buf_len = sizeof(*sm);
  1337. /* Read as much as we want into the _end_ of the preallocated
  1338. * buffer
  1339. */
  1340. get_fl_mem(part->offset + sector_ofs + part->sector_size -
  1341. buf_len, buf_len, buf + buf_size - buf_len);
  1342. sm = (void *)buf + buf_size - sizeof(*sm);
  1343. if (sm->magic == JFFS2_SUM_MAGIC) {
  1344. sumlen = part->sector_size - sm->offset;
  1345. sumptr = buf + buf_size - sumlen;
  1346. /* Now, make sure the summary itself is available */
  1347. if (sumlen > buf_size) {
  1348. /* Need to kmalloc for this. */
  1349. sumptr = malloc(sumlen);
  1350. if (!sumptr) {
  1351. putstr("Can't get memory for summary "
  1352. "node!\n");
  1353. free(buf);
  1354. jffs2_free_cache(part);
  1355. return 0;
  1356. }
  1357. memcpy(sumptr + sumlen - buf_len, buf +
  1358. buf_size - buf_len, buf_len);
  1359. }
  1360. if (buf_len < sumlen) {
  1361. /* Need to read more so that the entire summary
  1362. * node is present
  1363. */
  1364. get_fl_mem(part->offset + sector_ofs +
  1365. part->sector_size - sumlen,
  1366. sumlen - buf_len, sumptr);
  1367. }
  1368. }
  1369. if (sumptr) {
  1370. ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
  1371. sumlen, pL);
  1372. if (buf_size && sumlen > buf_size)
  1373. free(sumptr);
  1374. if (ret < 0) {
  1375. free(buf);
  1376. jffs2_free_cache(part);
  1377. return 0;
  1378. }
  1379. if (ret)
  1380. continue;
  1381. }
  1382. #endif /* CONFIG_JFFS2_SUMMARY */
  1383. buf_len = EMPTY_SCAN_SIZE(part->sector_size);
  1384. get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
  1385. /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
  1386. ofs = 0;
  1387. /* Scan only 4KiB of 0xFF before declaring it's empty */
  1388. while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
  1389. *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
  1390. ofs += 4;
  1391. if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
  1392. continue;
  1393. ofs += sector_ofs;
  1394. prevofs = ofs - 1;
  1395. /*
  1396. * Set buf_size down to the minimum size required.
  1397. * This prevents reading in chunks of flash data unnecessarily.
  1398. */
  1399. buf_size = sizeof(union jffs2_node_union);
  1400. scan_more:
  1401. while (ofs < sector_ofs + part->sector_size) {
  1402. if (ofs == prevofs) {
  1403. printf("offset %08x already seen, skip\n", ofs);
  1404. ofs += 4;
  1405. counter4++;
  1406. continue;
  1407. }
  1408. prevofs = ofs;
  1409. if (sector_ofs + part->sector_size <
  1410. ofs + sizeof(*node))
  1411. break;
  1412. if (buf_ofs + buf_len < ofs + sizeof(*node)) {
  1413. buf_len = min_t(uint32_t, buf_size, sector_ofs
  1414. + part->sector_size - ofs);
  1415. get_fl_mem((u32)part->offset + ofs, buf_len,
  1416. buf);
  1417. buf_ofs = ofs;
  1418. }
  1419. node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
  1420. if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
  1421. uint32_t inbuf_ofs;
  1422. uint32_t scan_end;
  1423. ofs += 4;
  1424. scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
  1425. part->sector_size)/8,
  1426. buf_len);
  1427. more_empty:
  1428. inbuf_ofs = ofs - buf_ofs;
  1429. while (inbuf_ofs < scan_end) {
  1430. if (*(uint32_t *)(&buf[inbuf_ofs]) !=
  1431. 0xffffffff)
  1432. goto scan_more;
  1433. inbuf_ofs += 4;
  1434. ofs += 4;
  1435. }
  1436. /* Ran off end. */
  1437. /*
  1438. * If this sector had a clean marker at the
  1439. * beginning, and immediately following this
  1440. * have been a bunch of FF bytes, treat the
  1441. * entire sector as empty.
  1442. */
  1443. if (clean_sector)
  1444. break;
  1445. /* See how much more there is to read in this
  1446. * eraseblock...
  1447. */
  1448. buf_len = min_t(uint32_t, buf_size,
  1449. sector_ofs +
  1450. part->sector_size - ofs);
  1451. if (!buf_len) {
  1452. /* No more to read. Break out of main
  1453. * loop without marking this range of
  1454. * empty space as dirty (because it's
  1455. * not)
  1456. */
  1457. break;
  1458. }
  1459. scan_end = buf_len;
  1460. get_fl_mem((u32)part->offset + ofs, buf_len,
  1461. buf);
  1462. buf_ofs = ofs;
  1463. goto more_empty;
  1464. }
  1465. /*
  1466. * Found something not erased in the sector, so reset
  1467. * the 'clean_sector' flag.
  1468. */
  1469. clean_sector = 0;
  1470. if (node->magic != JFFS2_MAGIC_BITMASK ||
  1471. !hdr_crc(node)) {
  1472. ofs += 4;
  1473. counter4++;
  1474. continue;
  1475. }
  1476. if (ofs + node->totlen >
  1477. sector_ofs + part->sector_size) {
  1478. ofs += 4;
  1479. counter4++;
  1480. continue;
  1481. }
  1482. /* if its a fragment add it */
  1483. switch (node->nodetype) {
  1484. case JFFS2_NODETYPE_INODE:
  1485. if (buf_ofs + buf_len < ofs + sizeof(struct
  1486. jffs2_raw_inode)) {
  1487. buf_len = min_t(uint32_t,
  1488. sizeof(struct jffs2_raw_inode),
  1489. sector_ofs +
  1490. part->sector_size -
  1491. ofs);
  1492. get_fl_mem((u32)part->offset + ofs,
  1493. buf_len, buf);
  1494. buf_ofs = ofs;
  1495. node = (void *)buf;
  1496. }
  1497. if (!inode_crc((struct jffs2_raw_inode *)node))
  1498. break;
  1499. if (insert_node(&pL->frag, (u32) part->offset +
  1500. ofs) == NULL) {
  1501. free(buf);
  1502. jffs2_free_cache(part);
  1503. return 0;
  1504. }
  1505. if (max_totlen < node->totlen)
  1506. max_totlen = node->totlen;
  1507. break;
  1508. case JFFS2_NODETYPE_DIRENT:
  1509. if (buf_ofs + buf_len < ofs + sizeof(struct
  1510. jffs2_raw_dirent) +
  1511. ((struct
  1512. jffs2_raw_dirent *)
  1513. node)->nsize) {
  1514. buf_len = min_t(uint32_t,
  1515. node->totlen,
  1516. sector_ofs +
  1517. part->sector_size -
  1518. ofs);
  1519. get_fl_mem((u32)part->offset + ofs,
  1520. buf_len, buf);
  1521. buf_ofs = ofs;
  1522. node = (void *)buf;
  1523. }
  1524. if (!dirent_crc((struct jffs2_raw_dirent *)
  1525. node) ||
  1526. !dirent_name_crc(
  1527. (struct
  1528. jffs2_raw_dirent *)
  1529. node))
  1530. break;
  1531. if (! (counterN%100))
  1532. puts ("\b\b. ");
  1533. if (insert_node(&pL->dir, (u32) part->offset +
  1534. ofs) == NULL) {
  1535. free(buf);
  1536. jffs2_free_cache(part);
  1537. return 0;
  1538. }
  1539. if (max_totlen < node->totlen)
  1540. max_totlen = node->totlen;
  1541. counterN++;
  1542. break;
  1543. case JFFS2_NODETYPE_CLEANMARKER:
  1544. if (node->totlen != sizeof(struct jffs2_unknown_node))
  1545. printf("OOPS Cleanmarker has bad size "
  1546. "%d != %zu\n",
  1547. node->totlen,
  1548. sizeof(struct jffs2_unknown_node));
  1549. if ((node->totlen ==
  1550. sizeof(struct jffs2_unknown_node)) &&
  1551. (ofs == sector_ofs)) {
  1552. /*
  1553. * Found a CLEANMARKER at the beginning
  1554. * of the sector. It's in the correct
  1555. * place with correct size and CRC.
  1556. */
  1557. clean_sector = 1;
  1558. }
  1559. break;
  1560. case JFFS2_NODETYPE_PADDING:
  1561. if (node->totlen < sizeof(struct jffs2_unknown_node))
  1562. printf("OOPS Padding has bad size "
  1563. "%d < %zu\n",
  1564. node->totlen,
  1565. sizeof(struct jffs2_unknown_node));
  1566. break;
  1567. case JFFS2_NODETYPE_SUMMARY:
  1568. break;
  1569. default:
  1570. printf("Unknown node type: %x len %d offset 0x%x\n",
  1571. node->nodetype,
  1572. node->totlen, ofs);
  1573. }
  1574. ofs += ((node->totlen + 3) & ~3);
  1575. counterF++;
  1576. }
  1577. }
  1578. free(buf);
  1579. #if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS)
  1580. /*
  1581. * Sort the lists.
  1582. */
  1583. sort_list(&pL->frag);
  1584. sort_list(&pL->dir);
  1585. #endif
  1586. putstr("\b\b done.\r\n"); /* close off the dots */
  1587. /* We don't care if malloc failed - then each read operation will
  1588. * allocate its own buffer as necessary (NAND) or will read directly
  1589. * from flash (NOR).
  1590. */
  1591. pL->readbuf = malloc(max_totlen);
  1592. /* turn the lcd back on. */
  1593. /* splash(); */
  1594. #if 0
  1595. putLabeledWord("dir entries = ", pL->dir.listCount);
  1596. putLabeledWord("frag entries = ", pL->frag.listCount);
  1597. putLabeledWord("+4 increments = ", counter4);
  1598. putLabeledWord("+file_offset increments = ", counterF);
  1599. #endif
  1600. #ifdef DEBUG_DIRENTS
  1601. dump_dirents(pL);
  1602. #endif
  1603. #ifdef DEBUG_FRAGMENTS
  1604. dump_fragments(pL);
  1605. #endif
  1606. /* give visual feedback that we are done scanning the flash */
  1607. led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
  1608. return 1;
  1609. }
  1610. static u32
  1611. jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
  1612. {
  1613. struct b_node *b;
  1614. struct jffs2_raw_inode ojNode;
  1615. struct jffs2_raw_inode *jNode;
  1616. int i;
  1617. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1618. piL->compr_info[i].num_frags = 0;
  1619. piL->compr_info[i].compr_sum = 0;
  1620. piL->compr_info[i].decompr_sum = 0;
  1621. }
  1622. b = pL->frag.listHead;
  1623. while (b) {
  1624. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  1625. sizeof(ojNode), &ojNode);
  1626. if (jNode->compr < JFFS2_NUM_COMPR) {
  1627. piL->compr_info[jNode->compr].num_frags++;
  1628. piL->compr_info[jNode->compr].compr_sum += jNode->csize;
  1629. piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
  1630. }
  1631. b = b->next;
  1632. }
  1633. return 0;
  1634. }
  1635. static struct b_lists *
  1636. jffs2_get_list(struct part_info * part, const char *who)
  1637. {
  1638. /* copy requested part_info struct pointer to global location */
  1639. current_part = part;
  1640. if (jffs2_1pass_rescan_needed(part)) {
  1641. if (!jffs2_1pass_build_lists(part)) {
  1642. printf("%s: Failed to scan JFFSv2 file structure\n", who);
  1643. return NULL;
  1644. }
  1645. }
  1646. return (struct b_lists *)part->jffs2_priv;
  1647. }
  1648. /* Print directory / file contents */
  1649. u32
  1650. jffs2_1pass_ls(struct part_info * part, const char *fname)
  1651. {
  1652. struct b_lists *pl;
  1653. long ret = 1;
  1654. u32 inode;
  1655. if (! (pl = jffs2_get_list(part, "ls")))
  1656. return 0;
  1657. if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
  1658. putstr("ls: Failed to scan jffs2 file structure\r\n");
  1659. return 0;
  1660. }
  1661. #if 0
  1662. putLabeledWord("found file at inode = ", inode);
  1663. putLabeledWord("read_inode returns = ", ret);
  1664. #endif
  1665. return ret;
  1666. }
  1667. /* Load a file from flash into memory. fname can be a full path */
  1668. u32
  1669. jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
  1670. {
  1671. struct b_lists *pl;
  1672. long ret = 1;
  1673. u32 inode;
  1674. if (! (pl = jffs2_get_list(part, "load")))
  1675. return 0;
  1676. if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
  1677. putstr("load: Failed to find inode\r\n");
  1678. return 0;
  1679. }
  1680. /* Resolve symlinks */
  1681. if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
  1682. putstr("load: Failed to resolve inode structure\r\n");
  1683. return 0;
  1684. }
  1685. if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
  1686. putstr("load: Failed to read inode\r\n");
  1687. return 0;
  1688. }
  1689. DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
  1690. (unsigned long) dest, ret);
  1691. return ret;
  1692. }
  1693. /* Return information about the fs on this partition */
  1694. u32
  1695. jffs2_1pass_info(struct part_info * part)
  1696. {
  1697. struct b_jffs2_info info;
  1698. struct b_lists *pl;
  1699. int i;
  1700. if (! (pl = jffs2_get_list(part, "info")))
  1701. return 0;
  1702. jffs2_1pass_fill_info(pl, &info);
  1703. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1704. printf ("Compression: %s\n"
  1705. "\tfrag count: %d\n"
  1706. "\tcompressed sum: %d\n"
  1707. "\tuncompressed sum: %d\n",
  1708. compr_names[i],
  1709. info.compr_info[i].num_frags,
  1710. info.compr_info[i].compr_sum,
  1711. info.compr_info[i].decompr_sum);
  1712. }
  1713. return 1;
  1714. }