jffs2_1pass.c 50 KB

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