jffs2_1pass.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. /*
  2. -------------------------------------------------------------------------
  3. * Filename: jffs2.c
  4. * Version: $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
  5. * Copyright: Copyright (C) 2001, Russ Dill
  6. * Author: Russ Dill <Russ.Dill@asu.edu>
  7. * Description: Module to load kernel from jffs2
  8. *-----------------------------------------------------------------------*/
  9. /*
  10. * some portions of this code are taken from jffs2, and as such, the
  11. * following copyright notice is included.
  12. *
  13. * JFFS2 -- Journalling Flash File System, Version 2.
  14. *
  15. * Copyright (C) 2001 Red Hat, Inc.
  16. *
  17. * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
  18. *
  19. * The original JFFS, from which the design for JFFS2 was derived,
  20. * was designed and implemented by Axis Communications AB.
  21. *
  22. * The contents of this file are subject to the Red Hat eCos Public
  23. * License Version 1.1 (the "Licence"); you may not use this file
  24. * except in compliance with the Licence. You may obtain a copy of
  25. * the Licence at http://www.redhat.com/
  26. *
  27. * Software distributed under the Licence is distributed on an "AS IS"
  28. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
  29. * See the Licence for the specific language governing rights and
  30. * limitations under the Licence.
  31. *
  32. * The Original Code is JFFS2 - Journalling Flash File System, version 2
  33. *
  34. * Alternatively, the contents of this file may be used under the
  35. * terms of the GNU General Public License version 2 (the "GPL"), in
  36. * which case the provisions of the GPL are applicable instead of the
  37. * above. If you wish to allow the use of your version of this file
  38. * only under the terms of the GPL and not to allow others to use your
  39. * version of this file under the RHEPL, indicate your decision by
  40. * deleting the provisions above and replace them with the notice and
  41. * other provisions required by the GPL. If you do not delete the
  42. * provisions above, a recipient may use your version of this file
  43. * under either the RHEPL or the GPL.
  44. *
  45. * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
  46. *
  47. */
  48. /* Ok, so anyone who knows the jffs2 code will probably want to get a papar
  49. * bag to throw up into before reading this code. I looked through the jffs2
  50. * code, the caching scheme is very elegant. I tried to keep the version
  51. * for a bootloader as small and simple as possible. Instead of worring about
  52. * unneccesary data copies, node scans, etc, I just optimized for the known
  53. * common case, a kernel, which looks like:
  54. * (1) most pages are 4096 bytes
  55. * (2) version numbers are somewhat sorted in acsending order
  56. * (3) multiple compressed blocks making up one page is uncommon
  57. *
  58. * So I create a linked list of decending version numbers (insertions at the
  59. * head), and then for each page, walk down the list, until a matching page
  60. * with 4096 bytes is found, and then decompress the watching pages in
  61. * reverse order.
  62. *
  63. */
  64. /*
  65. * Adapted by Nye Liu <nyet@zumanetworks.com> and
  66. * Rex Feany <rfeany@zumanetworks.com>
  67. * on Jan/2002 for U-Boot.
  68. *
  69. * Clipped out all the non-1pass functions, cleaned up warnings,
  70. * wrappers, etc. No major changes to the code.
  71. * Please, he really means it when he said have a paper bag
  72. * handy. We needed it ;).
  73. *
  74. */
  75. /*
  76. * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
  77. *
  78. * - overhaul of the memory management. Removed much of the "paper-bagging"
  79. * in that part of the code, fixed several bugs, now frees memory when
  80. * partition is changed.
  81. * It's still ugly :-(
  82. * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
  83. * was incorrect. Removed a bit of the paper-bagging as well.
  84. * - removed double crc calculation for fragment headers in jffs2_private.h
  85. * for speedup.
  86. * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
  87. * - spinning wheel now spins depending on how much memory has been scanned
  88. * - lots of small changes all over the place to "improve" readability.
  89. * - implemented fragment sorting to ensure that the newest data is copied
  90. * if there are multiple copies of fragments for a certain file offset.
  91. *
  92. * The fragment sorting feature must be enabled by CONFIG_SYS_JFFS2_SORT_FRAGMENTS.
  93. * Sorting is done while adding fragments to the lists, which is more or less a
  94. * bubble sort. This takes a lot of time, and is most probably not an issue if
  95. * the boot filesystem is always mounted readonly.
  96. *
  97. * You should define it if the boot filesystem is mounted writable, and updates
  98. * to the boot files are done by copying files to that filesystem.
  99. *
  100. *
  101. * There's a big issue left: endianess is completely ignored in this code. Duh!
  102. *
  103. *
  104. * You still should have paper bags at hand :-(. The code lacks more or less
  105. * any comment, and is still arcane and difficult to read in places. As this
  106. * might be incompatible with any new code from the jffs2 maintainers anyway,
  107. * it should probably be dumped and replaced by something like jffs2reader!
  108. */
  109. #include <common.h>
  110. #include <config.h>
  111. #include <malloc.h>
  112. #include <linux/stat.h>
  113. #include <linux/time.h>
  114. #include <watchdog.h>
  115. #include <jffs2/jffs2.h>
  116. #include <jffs2/jffs2_1pass.h>
  117. #include "jffs2_private.h"
  118. #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
  119. #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
  120. /* Debugging switches */
  121. #undef DEBUG_DIRENTS /* print directory entry list after scan */
  122. #undef DEBUG_FRAGMENTS /* print fragment list after scan */
  123. #undef DEBUG /* enable debugging messages */
  124. #ifdef DEBUG
  125. # define DEBUGF(fmt,args...) printf(fmt ,##args)
  126. #else
  127. # define DEBUGF(fmt,args...)
  128. #endif
  129. #include "summary.h"
  130. /* keeps pointer to currentlu processed partition */
  131. static struct part_info *current_part;
  132. #if (defined(CONFIG_JFFS2_NAND) && \
  133. defined(CONFIG_CMD_NAND) )
  134. #if defined(CONFIG_NAND_LEGACY)
  135. #include <linux/mtd/nand_legacy.h>
  136. #else
  137. #include <nand.h>
  138. #endif
  139. /*
  140. * Support for jffs2 on top of NAND-flash
  141. *
  142. * NAND memory isn't mapped in processor's address space,
  143. * so data should be fetched from flash before
  144. * being processed. This is exactly what functions declared
  145. * here do.
  146. *
  147. */
  148. #if defined(CONFIG_NAND_LEGACY)
  149. /* this one defined in nand_legacy.c */
  150. int read_jffs2_nand(size_t start, size_t len,
  151. size_t * retlen, u_char * buf, int nanddev);
  152. #endif
  153. #define NAND_PAGE_SIZE 512
  154. #define NAND_PAGE_SHIFT 9
  155. #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
  156. #ifndef NAND_CACHE_PAGES
  157. #define NAND_CACHE_PAGES 16
  158. #endif
  159. #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
  160. static u8* nand_cache = NULL;
  161. static u32 nand_cache_off = (u32)-1;
  162. static int read_nand_cached(u32 off, u32 size, u_char *buf)
  163. {
  164. struct mtdids *id = current_part->dev->id;
  165. u32 bytes_read = 0;
  166. size_t retlen;
  167. int cpy_bytes;
  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. #if defined(CONFIG_NAND_LEGACY)
  183. if (read_jffs2_nand(nand_cache_off, NAND_CACHE_SIZE,
  184. &retlen, nand_cache, id->num) < 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. #else
  191. retlen = NAND_CACHE_SIZE;
  192. if (nand_read(&nand_info[id->num], nand_cache_off,
  193. &retlen, nand_cache) != 0 ||
  194. retlen != NAND_CACHE_SIZE) {
  195. printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
  196. nand_cache_off, NAND_CACHE_SIZE);
  197. return -1;
  198. }
  199. #endif
  200. }
  201. cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
  202. if (cpy_bytes > size - bytes_read)
  203. cpy_bytes = size - bytes_read;
  204. memcpy(buf + bytes_read,
  205. nand_cache + off + bytes_read - nand_cache_off,
  206. cpy_bytes);
  207. bytes_read += cpy_bytes;
  208. }
  209. return bytes_read;
  210. }
  211. static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
  212. {
  213. u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
  214. if (NULL == buf) {
  215. printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
  216. return NULL;
  217. }
  218. if (read_nand_cached(off, size, buf) < 0) {
  219. if (!ext_buf)
  220. free(buf);
  221. return NULL;
  222. }
  223. return buf;
  224. }
  225. static void *get_node_mem_nand(u32 off, void *ext_buf)
  226. {
  227. struct jffs2_unknown_node node;
  228. void *ret = NULL;
  229. if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
  230. return NULL;
  231. if (!(ret = get_fl_mem_nand(off, node.magic ==
  232. JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
  233. ext_buf))) {
  234. printf("off = %#x magic %#x type %#x node.totlen = %d\n",
  235. off, node.magic, node.nodetype, node.totlen);
  236. }
  237. return ret;
  238. }
  239. static void put_fl_mem_nand(void *buf)
  240. {
  241. free(buf);
  242. }
  243. #endif
  244. #if defined(CONFIG_CMD_ONENAND)
  245. #include <linux/mtd/mtd.h>
  246. #include <linux/mtd/onenand.h>
  247. #include <onenand_uboot.h>
  248. #define ONENAND_PAGE_SIZE 2048
  249. #define ONENAND_PAGE_SHIFT 11
  250. #define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
  251. #ifndef ONENAND_CACHE_PAGES
  252. #define ONENAND_CACHE_PAGES 4
  253. #endif
  254. #define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
  255. static u8* onenand_cache;
  256. static u32 onenand_cache_off = (u32)-1;
  257. static int read_onenand_cached(u32 off, u32 size, u_char *buf)
  258. {
  259. u32 bytes_read = 0;
  260. size_t retlen;
  261. int cpy_bytes;
  262. while (bytes_read < size) {
  263. if ((off + bytes_read < onenand_cache_off) ||
  264. (off + bytes_read >= onenand_cache_off + ONENAND_CACHE_SIZE)) {
  265. onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
  266. if (!onenand_cache) {
  267. /* This memory never gets freed but 'cause
  268. it's a bootloader, nobody cares */
  269. onenand_cache = malloc(ONENAND_CACHE_SIZE);
  270. if (!onenand_cache) {
  271. printf("read_onenand_cached: can't alloc cache size %d bytes\n",
  272. ONENAND_CACHE_SIZE);
  273. return -1;
  274. }
  275. }
  276. retlen = ONENAND_CACHE_SIZE;
  277. if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
  278. &retlen, onenand_cache) != 0 ||
  279. retlen != ONENAND_CACHE_SIZE) {
  280. printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
  281. onenand_cache_off, ONENAND_CACHE_SIZE);
  282. return -1;
  283. }
  284. }
  285. cpy_bytes = onenand_cache_off + ONENAND_CACHE_SIZE - (off + bytes_read);
  286. if (cpy_bytes > size - bytes_read)
  287. cpy_bytes = size - bytes_read;
  288. memcpy(buf + bytes_read,
  289. onenand_cache + off + bytes_read - onenand_cache_off,
  290. cpy_bytes);
  291. bytes_read += cpy_bytes;
  292. }
  293. return bytes_read;
  294. }
  295. static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
  296. {
  297. u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
  298. if (NULL == buf) {
  299. printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
  300. return NULL;
  301. }
  302. if (read_onenand_cached(off, size, buf) < 0) {
  303. if (!ext_buf)
  304. free(buf);
  305. return NULL;
  306. }
  307. return buf;
  308. }
  309. static void *get_node_mem_onenand(u32 off, void *ext_buf)
  310. {
  311. struct jffs2_unknown_node node;
  312. void *ret = NULL;
  313. if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
  314. return NULL;
  315. ret = get_fl_mem_onenand(off, node.magic ==
  316. JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
  317. ext_buf);
  318. if (!ret) {
  319. printf("off = %#x magic %#x type %#x node.totlen = %d\n",
  320. off, node.magic, node.nodetype, node.totlen);
  321. }
  322. return ret;
  323. }
  324. static void put_fl_mem_onenand(void *buf)
  325. {
  326. free(buf);
  327. }
  328. #endif
  329. #if defined(CONFIG_CMD_FLASH)
  330. /*
  331. * Support for jffs2 on top of NOR-flash
  332. *
  333. * NOR flash memory is mapped in processor's address space,
  334. * just return address.
  335. */
  336. static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
  337. {
  338. u32 addr = off;
  339. struct mtdids *id = current_part->dev->id;
  340. extern flash_info_t flash_info[];
  341. flash_info_t *flash = &flash_info[id->num];
  342. addr += flash->start[0];
  343. if (ext_buf) {
  344. memcpy(ext_buf, (void *)addr, size);
  345. return ext_buf;
  346. }
  347. return (void*)addr;
  348. }
  349. static inline void *get_node_mem_nor(u32 off, void *ext_buf)
  350. {
  351. struct jffs2_unknown_node *pNode;
  352. /* pNode will point directly to flash - don't provide external buffer
  353. and don't care about size */
  354. pNode = get_fl_mem_nor(off, 0, NULL);
  355. return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
  356. pNode->totlen : sizeof(*pNode), ext_buf);
  357. }
  358. #endif
  359. /*
  360. * Generic jffs2 raw memory and node read routines.
  361. *
  362. */
  363. static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
  364. {
  365. struct mtdids *id = current_part->dev->id;
  366. #if defined(CONFIG_CMD_FLASH)
  367. if (id->type == MTD_DEV_TYPE_NOR) {
  368. return get_fl_mem_nor(off, size, ext_buf);
  369. }
  370. #endif
  371. #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
  372. if (id->type == MTD_DEV_TYPE_NAND)
  373. return get_fl_mem_nand(off, size, ext_buf);
  374. #endif
  375. #if defined(CONFIG_CMD_ONENAND)
  376. if (id->type == MTD_DEV_TYPE_ONENAND)
  377. return get_fl_mem_onenand(off, size, ext_buf);
  378. #endif
  379. printf("get_fl_mem: unknown device type, using raw offset!\n");
  380. return (void*)off;
  381. }
  382. static inline void *get_node_mem(u32 off, void *ext_buf)
  383. {
  384. struct mtdids *id = current_part->dev->id;
  385. #if defined(CONFIG_CMD_FLASH)
  386. if (id->type == MTD_DEV_TYPE_NOR)
  387. return get_node_mem_nor(off, ext_buf);
  388. #endif
  389. #if defined(CONFIG_JFFS2_NAND) && \
  390. defined(CONFIG_CMD_NAND)
  391. if (id->type == MTD_DEV_TYPE_NAND)
  392. return get_node_mem_nand(off, ext_buf);
  393. #endif
  394. #if defined(CONFIG_CMD_ONENAND)
  395. if (id->type == MTD_DEV_TYPE_ONENAND)
  396. return get_node_mem_onenand(off, ext_buf);
  397. #endif
  398. printf("get_node_mem: unknown device type, using raw offset!\n");
  399. return (void*)off;
  400. }
  401. static inline void put_fl_mem(void *buf, void *ext_buf)
  402. {
  403. struct mtdids *id = current_part->dev->id;
  404. /* If buf is the same as ext_buf, it was provided by the caller -
  405. we shouldn't free it then. */
  406. if (buf == ext_buf)
  407. return;
  408. switch (id->type) {
  409. #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
  410. case MTD_DEV_TYPE_NAND:
  411. return put_fl_mem_nand(buf);
  412. #endif
  413. #if defined(CONFIG_CMD_ONENAND)
  414. case MTD_DEV_TYPE_ONENAND:
  415. return put_fl_mem_onenand(buf);
  416. #endif
  417. }
  418. }
  419. /* Compression names */
  420. static char *compr_names[] = {
  421. "NONE",
  422. "ZERO",
  423. "RTIME",
  424. "RUBINMIPS",
  425. "COPY",
  426. "DYNRUBIN",
  427. "ZLIB",
  428. #if defined(CONFIG_JFFS2_LZO_LZARI)
  429. "LZO",
  430. "LZARI",
  431. #endif
  432. };
  433. /* Memory management */
  434. struct mem_block {
  435. u32 index;
  436. struct mem_block *next;
  437. struct b_node nodes[NODE_CHUNK];
  438. };
  439. static void
  440. free_nodes(struct b_list *list)
  441. {
  442. while (list->listMemBase != NULL) {
  443. struct mem_block *next = list->listMemBase->next;
  444. free( list->listMemBase );
  445. list->listMemBase = next;
  446. }
  447. }
  448. static struct b_node *
  449. add_node(struct b_list *list)
  450. {
  451. u32 index = 0;
  452. struct mem_block *memBase;
  453. struct b_node *b;
  454. memBase = list->listMemBase;
  455. if (memBase != NULL)
  456. index = memBase->index;
  457. #if 0
  458. putLabeledWord("add_node: index = ", index);
  459. putLabeledWord("add_node: memBase = ", list->listMemBase);
  460. #endif
  461. if (memBase == NULL || index >= NODE_CHUNK) {
  462. /* we need more space before we continue */
  463. memBase = mmalloc(sizeof(struct mem_block));
  464. if (memBase == NULL) {
  465. putstr("add_node: malloc failed\n");
  466. return NULL;
  467. }
  468. memBase->next = list->listMemBase;
  469. index = 0;
  470. #if 0
  471. putLabeledWord("add_node: alloced a new membase at ", *memBase);
  472. #endif
  473. }
  474. /* now we have room to add it. */
  475. b = &memBase->nodes[index];
  476. index ++;
  477. memBase->index = index;
  478. list->listMemBase = memBase;
  479. list->listCount++;
  480. return b;
  481. }
  482. static struct b_node *
  483. insert_node(struct b_list *list, u32 offset)
  484. {
  485. struct b_node *new;
  486. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  487. struct b_node *b, *prev;
  488. #endif
  489. if (!(new = add_node(list))) {
  490. putstr("add_node failed!\r\n");
  491. return NULL;
  492. }
  493. new->offset = offset;
  494. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  495. if (list->listTail != NULL && list->listCompare(new, list->listTail))
  496. prev = list->listTail;
  497. else if (list->listLast != NULL && list->listCompare(new, list->listLast))
  498. prev = list->listLast;
  499. else
  500. prev = NULL;
  501. for (b = (prev ? prev->next : list->listHead);
  502. b != NULL && list->listCompare(new, b);
  503. prev = b, b = b->next) {
  504. list->listLoops++;
  505. }
  506. if (b != NULL)
  507. list->listLast = prev;
  508. if (b != NULL) {
  509. new->next = b;
  510. if (prev != NULL)
  511. prev->next = new;
  512. else
  513. list->listHead = new;
  514. } else
  515. #endif
  516. {
  517. new->next = (struct b_node *) NULL;
  518. if (list->listTail != NULL) {
  519. list->listTail->next = new;
  520. list->listTail = new;
  521. } else {
  522. list->listTail = list->listHead = new;
  523. }
  524. }
  525. return new;
  526. }
  527. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  528. /* Sort data entries with the latest version last, so that if there
  529. * is overlapping data the latest version will be used.
  530. */
  531. static int compare_inodes(struct b_node *new, struct b_node *old)
  532. {
  533. struct jffs2_raw_inode ojNew;
  534. struct jffs2_raw_inode ojOld;
  535. struct jffs2_raw_inode *jNew =
  536. (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
  537. struct jffs2_raw_inode *jOld =
  538. (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
  539. return jNew->version > jOld->version;
  540. }
  541. /* Sort directory entries so all entries in the same directory
  542. * with the same name are grouped together, with the latest version
  543. * last. This makes it easy to eliminate all but the latest version
  544. * by marking the previous version dead by setting the inode to 0.
  545. */
  546. static int compare_dirents(struct b_node *new, struct b_node *old)
  547. {
  548. struct jffs2_raw_dirent ojNew;
  549. struct jffs2_raw_dirent ojOld;
  550. struct jffs2_raw_dirent *jNew =
  551. (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
  552. struct jffs2_raw_dirent *jOld =
  553. (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
  554. int cmp;
  555. /* ascending sort by pino */
  556. if (jNew->pino != jOld->pino)
  557. return jNew->pino > jOld->pino;
  558. /* pino is the same, so use ascending sort by nsize, so
  559. * we don't do strncmp unless we really must.
  560. */
  561. if (jNew->nsize != jOld->nsize)
  562. return jNew->nsize > jOld->nsize;
  563. /* length is also the same, so use ascending sort by name
  564. */
  565. cmp = strncmp((char *)jNew->name, (char *)jOld->name, jNew->nsize);
  566. if (cmp != 0)
  567. return cmp > 0;
  568. /* we have duplicate names in this directory, so use ascending
  569. * sort by version
  570. */
  571. if (jNew->version > jOld->version) {
  572. /* since jNew is newer, we know jOld is not valid, so
  573. * mark it with inode 0 and it will not be used
  574. */
  575. jOld->ino = 0;
  576. return 1;
  577. }
  578. return 0;
  579. }
  580. #endif
  581. void
  582. jffs2_free_cache(struct part_info *part)
  583. {
  584. struct b_lists *pL;
  585. if (part->jffs2_priv != NULL) {
  586. pL = (struct b_lists *)part->jffs2_priv;
  587. free_nodes(&pL->frag);
  588. free_nodes(&pL->dir);
  589. free(pL->readbuf);
  590. free(pL);
  591. }
  592. }
  593. static u32
  594. jffs_init_1pass_list(struct part_info *part)
  595. {
  596. struct b_lists *pL;
  597. jffs2_free_cache(part);
  598. if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
  599. pL = (struct b_lists *)part->jffs2_priv;
  600. memset(pL, 0, sizeof(*pL));
  601. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  602. pL->dir.listCompare = compare_dirents;
  603. pL->frag.listCompare = compare_inodes;
  604. #endif
  605. }
  606. return 0;
  607. }
  608. /* find the inode from the slashless name given a parent */
  609. static long
  610. jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
  611. {
  612. struct b_node *b;
  613. struct jffs2_raw_inode *jNode;
  614. u32 totalSize = 0;
  615. u32 latestVersion = 0;
  616. uchar *lDest;
  617. uchar *src;
  618. long ret;
  619. int i;
  620. u32 counter = 0;
  621. #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  622. /* Find file size before loading any data, so fragments that
  623. * start past the end of file can be ignored. A fragment
  624. * that is partially in the file is loaded, so extra data may
  625. * be loaded up to the next 4K boundary above the file size.
  626. * This shouldn't cause trouble when loading kernel images, so
  627. * we will live with it.
  628. */
  629. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  630. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  631. sizeof(struct jffs2_raw_inode), pL->readbuf);
  632. if ((inode == jNode->ino)) {
  633. /* get actual file length from the newest node */
  634. if (jNode->version >= latestVersion) {
  635. totalSize = jNode->isize;
  636. latestVersion = jNode->version;
  637. }
  638. }
  639. put_fl_mem(jNode, pL->readbuf);
  640. }
  641. #endif
  642. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  643. jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset,
  644. pL->readbuf);
  645. if ((inode == jNode->ino)) {
  646. #if 0
  647. putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
  648. putLabeledWord("read_inode: inode = ", jNode->ino);
  649. putLabeledWord("read_inode: version = ", jNode->version);
  650. putLabeledWord("read_inode: isize = ", jNode->isize);
  651. putLabeledWord("read_inode: offset = ", jNode->offset);
  652. putLabeledWord("read_inode: csize = ", jNode->csize);
  653. putLabeledWord("read_inode: dsize = ", jNode->dsize);
  654. putLabeledWord("read_inode: compr = ", jNode->compr);
  655. putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
  656. putLabeledWord("read_inode: flags = ", jNode->flags);
  657. #endif
  658. #ifndef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
  659. /* get actual file length from the newest node */
  660. if (jNode->version >= latestVersion) {
  661. totalSize = jNode->isize;
  662. latestVersion = jNode->version;
  663. }
  664. #endif
  665. if(dest) {
  666. src = ((uchar *) jNode) + 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 (!data_crc(jNode)) {
  673. put_fl_mem(jNode, pL->readbuf);
  674. continue;
  675. }
  676. lDest = (uchar *) (dest + jNode->offset);
  677. #if 0
  678. putLabeledWord("read_inode: src = ", src);
  679. putLabeledWord("read_inode: dest = ", lDest);
  680. #endif
  681. switch (jNode->compr) {
  682. case JFFS2_COMPR_NONE:
  683. ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
  684. break;
  685. case JFFS2_COMPR_ZERO:
  686. ret = 0;
  687. for (i = 0; i < jNode->dsize; i++)
  688. *(lDest++) = 0;
  689. break;
  690. case JFFS2_COMPR_RTIME:
  691. ret = 0;
  692. rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
  693. break;
  694. case JFFS2_COMPR_DYNRUBIN:
  695. /* this is slow but it works */
  696. ret = 0;
  697. dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
  698. break;
  699. case JFFS2_COMPR_ZLIB:
  700. ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
  701. break;
  702. #if defined(CONFIG_JFFS2_LZO_LZARI)
  703. case JFFS2_COMPR_LZO:
  704. ret = lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
  705. break;
  706. case JFFS2_COMPR_LZARI:
  707. ret = lzari_decompress(src, lDest, jNode->csize, jNode->dsize);
  708. break;
  709. #endif
  710. default:
  711. /* unknown */
  712. putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
  713. put_fl_mem(jNode, pL->readbuf);
  714. return -1;
  715. break;
  716. }
  717. }
  718. #if 0
  719. putLabeledWord("read_inode: totalSize = ", totalSize);
  720. putLabeledWord("read_inode: compr ret = ", ret);
  721. #endif
  722. }
  723. counter++;
  724. put_fl_mem(jNode, pL->readbuf);
  725. }
  726. #if 0
  727. putLabeledWord("read_inode: returning = ", totalSize);
  728. #endif
  729. return totalSize;
  730. }
  731. /* find the inode from the slashless name given a parent */
  732. static u32
  733. jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
  734. {
  735. struct b_node *b;
  736. struct jffs2_raw_dirent *jDir;
  737. int len;
  738. u32 counter;
  739. u32 version = 0;
  740. u32 inode = 0;
  741. /* name is assumed slash free */
  742. len = strlen(name);
  743. counter = 0;
  744. /* we need to search all and return the inode with the highest version */
  745. for(b = pL->dir.listHead; b; b = b->next, counter++) {
  746. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  747. pL->readbuf);
  748. if ((pino == jDir->pino) && (len == jDir->nsize) &&
  749. (jDir->ino) && /* 0 for unlink */
  750. (!strncmp((char *)jDir->name, name, len))) { /* a match */
  751. if (jDir->version < version) {
  752. put_fl_mem(jDir, pL->readbuf);
  753. continue;
  754. }
  755. if (jDir->version == version && inode != 0) {
  756. /* I'm pretty sure this isn't legal */
  757. putstr(" ** ERROR ** ");
  758. putnstr(jDir->name, jDir->nsize);
  759. putLabeledWord(" has dup version =", version);
  760. }
  761. inode = jDir->ino;
  762. version = jDir->version;
  763. }
  764. #if 0
  765. putstr("\r\nfind_inode:p&l ->");
  766. putnstr(jDir->name, jDir->nsize);
  767. putstr("\r\n");
  768. putLabeledWord("pino = ", jDir->pino);
  769. putLabeledWord("nsize = ", jDir->nsize);
  770. putLabeledWord("b = ", (u32) b);
  771. putLabeledWord("counter = ", counter);
  772. #endif
  773. put_fl_mem(jDir, pL->readbuf);
  774. }
  775. return inode;
  776. }
  777. char *mkmodestr(unsigned long mode, char *str)
  778. {
  779. static const char *l = "xwr";
  780. int mask = 1, i;
  781. char c;
  782. switch (mode & S_IFMT) {
  783. case S_IFDIR: str[0] = 'd'; break;
  784. case S_IFBLK: str[0] = 'b'; break;
  785. case S_IFCHR: str[0] = 'c'; break;
  786. case S_IFIFO: str[0] = 'f'; break;
  787. case S_IFLNK: str[0] = 'l'; break;
  788. case S_IFSOCK: str[0] = 's'; break;
  789. case S_IFREG: str[0] = '-'; break;
  790. default: str[0] = '?';
  791. }
  792. for(i = 0; i < 9; i++) {
  793. c = l[i%3];
  794. str[9-i] = (mode & mask)?c:'-';
  795. mask = mask<<1;
  796. }
  797. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  798. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  799. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  800. str[10] = '\0';
  801. return str;
  802. }
  803. static inline void dump_stat(struct stat *st, const char *name)
  804. {
  805. char str[20];
  806. char s[64], *p;
  807. if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
  808. st->st_mtime = 1;
  809. ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
  810. if ((p = strchr(s,'\n')) != NULL) *p = '\0';
  811. if ((p = strchr(s,'\r')) != NULL) *p = '\0';
  812. /*
  813. printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
  814. st->st_size, s, name);
  815. */
  816. printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
  817. }
  818. static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
  819. {
  820. char fname[256];
  821. struct stat st;
  822. if(!d || !i) return -1;
  823. strncpy(fname, (char *)d->name, d->nsize);
  824. fname[d->nsize] = '\0';
  825. memset(&st,0,sizeof(st));
  826. st.st_mtime = i->mtime;
  827. st.st_mode = i->mode;
  828. st.st_ino = i->ino;
  829. st.st_size = i->isize;
  830. dump_stat(&st, fname);
  831. if (d->type == DT_LNK) {
  832. unsigned char *src = (unsigned char *) (&i[1]);
  833. putstr(" -> ");
  834. putnstr(src, (int)i->dsize);
  835. }
  836. putstr("\r\n");
  837. return 0;
  838. }
  839. /* list inodes with the given pino */
  840. static u32
  841. jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
  842. {
  843. struct b_node *b;
  844. struct jffs2_raw_dirent *jDir;
  845. for (b = pL->dir.listHead; b; b = b->next) {
  846. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  847. pL->readbuf);
  848. if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
  849. u32 i_version = 0;
  850. struct jffs2_raw_inode ojNode;
  851. struct jffs2_raw_inode *jNode, *i = NULL;
  852. struct b_node *b2 = pL->frag.listHead;
  853. while (b2) {
  854. jNode = (struct jffs2_raw_inode *)
  855. get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
  856. if (jNode->ino == jDir->ino && jNode->version >= i_version) {
  857. i_version = jNode->version;
  858. if (i)
  859. put_fl_mem(i, NULL);
  860. if (jDir->type == DT_LNK)
  861. i = get_node_mem(b2->offset,
  862. NULL);
  863. else
  864. i = get_fl_mem(b2->offset,
  865. sizeof(*i),
  866. NULL);
  867. }
  868. b2 = b2->next;
  869. }
  870. dump_inode(pL, jDir, i);
  871. put_fl_mem(i, NULL);
  872. }
  873. put_fl_mem(jDir, pL->readbuf);
  874. }
  875. return pino;
  876. }
  877. static u32
  878. jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
  879. {
  880. int i;
  881. char tmp[256];
  882. char working_tmp[256];
  883. char *c;
  884. /* discard any leading slash */
  885. i = 0;
  886. while (fname[i] == '/')
  887. i++;
  888. strcpy(tmp, &fname[i]);
  889. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  890. {
  891. strncpy(working_tmp, tmp, c - tmp);
  892. working_tmp[c - tmp] = '\0';
  893. #if 0
  894. putstr("search_inode: tmp = ");
  895. putstr(tmp);
  896. putstr("\r\n");
  897. putstr("search_inode: wtmp = ");
  898. putstr(working_tmp);
  899. putstr("\r\n");
  900. putstr("search_inode: c = ");
  901. putstr(c);
  902. putstr("\r\n");
  903. #endif
  904. for (i = 0; i < strlen(c) - 1; i++)
  905. tmp[i] = c[i + 1];
  906. tmp[i] = '\0';
  907. #if 0
  908. putstr("search_inode: post tmp = ");
  909. putstr(tmp);
  910. putstr("\r\n");
  911. #endif
  912. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
  913. putstr("find_inode failed for name=");
  914. putstr(working_tmp);
  915. putstr("\r\n");
  916. return 0;
  917. }
  918. }
  919. /* this is for the bare filename, directories have already been mapped */
  920. if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  921. putstr("find_inode failed for name=");
  922. putstr(tmp);
  923. putstr("\r\n");
  924. return 0;
  925. }
  926. return pino;
  927. }
  928. static u32
  929. jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
  930. {
  931. struct b_node *b;
  932. struct b_node *b2;
  933. struct jffs2_raw_dirent *jDir;
  934. struct jffs2_raw_inode *jNode;
  935. u8 jDirFoundType = 0;
  936. u32 jDirFoundIno = 0;
  937. u32 jDirFoundPino = 0;
  938. char tmp[256];
  939. u32 version = 0;
  940. u32 pino;
  941. unsigned char *src;
  942. /* we need to search all and return the inode with the highest version */
  943. for(b = pL->dir.listHead; b; b = b->next) {
  944. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  945. pL->readbuf);
  946. if (ino == jDir->ino) {
  947. if (jDir->version < version) {
  948. put_fl_mem(jDir, pL->readbuf);
  949. continue;
  950. }
  951. if (jDir->version == version && jDirFoundType) {
  952. /* I'm pretty sure this isn't legal */
  953. putstr(" ** ERROR ** ");
  954. putnstr(jDir->name, jDir->nsize);
  955. putLabeledWord(" has dup version (resolve) = ",
  956. version);
  957. }
  958. jDirFoundType = jDir->type;
  959. jDirFoundIno = jDir->ino;
  960. jDirFoundPino = jDir->pino;
  961. version = jDir->version;
  962. }
  963. put_fl_mem(jDir, pL->readbuf);
  964. }
  965. /* now we found the right entry again. (shoulda returned inode*) */
  966. if (jDirFoundType != DT_LNK)
  967. return jDirFoundIno;
  968. /* it's a soft link so we follow it again. */
  969. b2 = pL->frag.listHead;
  970. while (b2) {
  971. jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
  972. pL->readbuf);
  973. if (jNode->ino == jDirFoundIno) {
  974. src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
  975. #if 0
  976. putLabeledWord("\t\t dsize = ", jNode->dsize);
  977. putstr("\t\t target = ");
  978. putnstr(src, jNode->dsize);
  979. putstr("\r\n");
  980. #endif
  981. strncpy(tmp, (char *)src, jNode->dsize);
  982. tmp[jNode->dsize] = '\0';
  983. put_fl_mem(jNode, pL->readbuf);
  984. break;
  985. }
  986. b2 = b2->next;
  987. put_fl_mem(jNode, pL->readbuf);
  988. }
  989. /* ok so the name of the new file to find is in tmp */
  990. /* if it starts with a slash it is root based else shared dirs */
  991. if (tmp[0] == '/')
  992. pino = 1;
  993. else
  994. pino = jDirFoundPino;
  995. return jffs2_1pass_search_inode(pL, tmp, pino);
  996. }
  997. static u32
  998. jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
  999. {
  1000. int i;
  1001. char tmp[256];
  1002. char working_tmp[256];
  1003. char *c;
  1004. /* discard any leading slash */
  1005. i = 0;
  1006. while (fname[i] == '/')
  1007. i++;
  1008. strcpy(tmp, &fname[i]);
  1009. working_tmp[0] = '\0';
  1010. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  1011. {
  1012. strncpy(working_tmp, tmp, c - tmp);
  1013. working_tmp[c - tmp] = '\0';
  1014. for (i = 0; i < strlen(c) - 1; i++)
  1015. tmp[i] = c[i + 1];
  1016. tmp[i] = '\0';
  1017. /* only a failure if we arent looking at top level */
  1018. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
  1019. (working_tmp[0])) {
  1020. putstr("find_inode failed for name=");
  1021. putstr(working_tmp);
  1022. putstr("\r\n");
  1023. return 0;
  1024. }
  1025. }
  1026. if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  1027. putstr("find_inode failed for name=");
  1028. putstr(tmp);
  1029. putstr("\r\n");
  1030. return 0;
  1031. }
  1032. /* this is for the bare filename, directories have already been mapped */
  1033. if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
  1034. putstr("find_inode failed for name=");
  1035. putstr(tmp);
  1036. putstr("\r\n");
  1037. return 0;
  1038. }
  1039. return pino;
  1040. }
  1041. unsigned char
  1042. jffs2_1pass_rescan_needed(struct part_info *part)
  1043. {
  1044. struct b_node *b;
  1045. struct jffs2_unknown_node onode;
  1046. struct jffs2_unknown_node *node;
  1047. struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
  1048. if (part->jffs2_priv == 0){
  1049. DEBUGF ("rescan: First time in use\n");
  1050. return 1;
  1051. }
  1052. /* if we have no list, we need to rescan */
  1053. if (pL->frag.listCount == 0) {
  1054. DEBUGF ("rescan: fraglist zero\n");
  1055. return 1;
  1056. }
  1057. /* but suppose someone reflashed a partition at the same offset... */
  1058. b = pL->dir.listHead;
  1059. while (b) {
  1060. node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
  1061. sizeof(onode), &onode);
  1062. if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
  1063. DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
  1064. (unsigned long) b->offset);
  1065. return 1;
  1066. }
  1067. b = b->next;
  1068. }
  1069. return 0;
  1070. }
  1071. #define dbg_summary(...) do {} while (0);
  1072. /* Process the stored summary information - helper function for
  1073. * jffs2_sum_scan_sumnode()
  1074. */
  1075. static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
  1076. struct jffs2_raw_summary *summary,
  1077. struct b_lists *pL)
  1078. {
  1079. void *sp;
  1080. int i;
  1081. sp = summary->sum;
  1082. for (i = 0; i < summary->sum_num; i++) {
  1083. dbg_summary("processing summary index %d\n", i);
  1084. switch (((struct jffs2_sum_unknown_flash *)sp)->nodetype) {
  1085. case JFFS2_NODETYPE_INODE: {
  1086. struct jffs2_sum_inode_flash *spi;
  1087. spi = sp;
  1088. dbg_summary("Inode at 0x%08x-0x%08x\n",
  1089. offset + spi->offset,
  1090. offset + spi->offset + spi->totlen);
  1091. if (insert_node(&pL->frag, (u32) part->offset +
  1092. offset + spi->offset) == NULL)
  1093. return -1;
  1094. sp += JFFS2_SUMMARY_INODE_SIZE;
  1095. break;
  1096. }
  1097. case JFFS2_NODETYPE_DIRENT: {
  1098. struct jffs2_sum_dirent_flash *spd;
  1099. spd = sp;
  1100. dbg_summary("Dirent at 0x%08x-0x%08x\n",
  1101. offset + spd->offset,
  1102. offset + spd->offset + spd->totlen);
  1103. if (insert_node(&pL->dir, (u32) part->offset +
  1104. offset + spd->offset) == NULL)
  1105. return -1;
  1106. sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
  1107. break;
  1108. }
  1109. default : {
  1110. uint16_t nodetype =
  1111. ((struct jffs2_sum_unknown_flash *)
  1112. sp)->nodetype;
  1113. printf("Unsupported node type %x found in "
  1114. "summary!\n", nodetype);
  1115. break;
  1116. }
  1117. }
  1118. }
  1119. return 0;
  1120. }
  1121. /* Process the summary node - called from jffs2_scan_eraseblock() */
  1122. int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
  1123. struct jffs2_raw_summary *summary, uint32_t sumsize,
  1124. struct b_lists *pL)
  1125. {
  1126. struct jffs2_unknown_node crcnode;
  1127. int ret, ofs;
  1128. uint32_t crc;
  1129. ofs = part->sector_size - sumsize;
  1130. dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
  1131. offset, offset + ofs, sumsize);
  1132. /* OK, now check for node validity and CRC */
  1133. crcnode.magic = JFFS2_MAGIC_BITMASK;
  1134. crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
  1135. crcnode.totlen = summary->totlen;
  1136. crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
  1137. if (summary->hdr_crc != crc) {
  1138. dbg_summary("Summary node header is corrupt (bad CRC or "
  1139. "no summary at all)\n");
  1140. goto crc_err;
  1141. }
  1142. if (summary->totlen != sumsize) {
  1143. dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
  1144. goto crc_err;
  1145. }
  1146. crc = crc32_no_comp(0, (uchar *)summary,
  1147. sizeof(struct jffs2_raw_summary)-8);
  1148. if (summary->node_crc != crc) {
  1149. dbg_summary("Summary node is corrupt (bad CRC)\n");
  1150. goto crc_err;
  1151. }
  1152. crc = crc32_no_comp(0, (uchar *)summary->sum,
  1153. sumsize - sizeof(struct jffs2_raw_summary));
  1154. if (summary->sum_crc != crc) {
  1155. dbg_summary("Summary node data is corrupt (bad CRC)\n");
  1156. goto crc_err;
  1157. }
  1158. if (summary->cln_mkr)
  1159. dbg_summary("Summary : CLEANMARKER node \n");
  1160. ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
  1161. if (ret)
  1162. return ret; /* real error */
  1163. return 1;
  1164. crc_err:
  1165. putstr("Summary node crc error, skipping summary information.\n");
  1166. return 0;
  1167. }
  1168. #ifdef DEBUG_FRAGMENTS
  1169. static void
  1170. dump_fragments(struct b_lists *pL)
  1171. {
  1172. struct b_node *b;
  1173. struct jffs2_raw_inode ojNode;
  1174. struct jffs2_raw_inode *jNode;
  1175. putstr("\r\n\r\n******The fragment Entries******\r\n");
  1176. b = pL->frag.listHead;
  1177. while (b) {
  1178. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  1179. sizeof(ojNode), &ojNode);
  1180. putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
  1181. putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
  1182. putLabeledWord("\tbuild_list: inode = ", jNode->ino);
  1183. putLabeledWord("\tbuild_list: version = ", jNode->version);
  1184. putLabeledWord("\tbuild_list: isize = ", jNode->isize);
  1185. putLabeledWord("\tbuild_list: atime = ", jNode->atime);
  1186. putLabeledWord("\tbuild_list: offset = ", jNode->offset);
  1187. putLabeledWord("\tbuild_list: csize = ", jNode->csize);
  1188. putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
  1189. putLabeledWord("\tbuild_list: compr = ", jNode->compr);
  1190. putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
  1191. putLabeledWord("\tbuild_list: flags = ", jNode->flags);
  1192. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  1193. b = b->next;
  1194. }
  1195. }
  1196. #endif
  1197. #ifdef DEBUG_DIRENTS
  1198. static void
  1199. dump_dirents(struct b_lists *pL)
  1200. {
  1201. struct b_node *b;
  1202. struct jffs2_raw_dirent *jDir;
  1203. putstr("\r\n\r\n******The directory Entries******\r\n");
  1204. b = pL->dir.listHead;
  1205. while (b) {
  1206. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  1207. pL->readbuf);
  1208. putstr("\r\n");
  1209. putnstr(jDir->name, jDir->nsize);
  1210. putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
  1211. putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
  1212. putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
  1213. putLabeledWord("\tbuild_list: pino = ", jDir->pino);
  1214. putLabeledWord("\tbuild_list: version = ", jDir->version);
  1215. putLabeledWord("\tbuild_list: ino = ", jDir->ino);
  1216. putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
  1217. putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
  1218. putLabeledWord("\tbuild_list: type = ", jDir->type);
  1219. putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
  1220. putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
  1221. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  1222. b = b->next;
  1223. put_fl_mem(jDir, pL->readbuf);
  1224. }
  1225. }
  1226. #endif
  1227. #define min_t(type, x, y) ({ \
  1228. type __min1 = (x); \
  1229. type __min2 = (y); \
  1230. __min1 < __min2 ? __min1: __min2; })
  1231. #define DEFAULT_EMPTY_SCAN_SIZE 4096
  1232. static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
  1233. {
  1234. if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
  1235. return sector_size;
  1236. else
  1237. return DEFAULT_EMPTY_SCAN_SIZE;
  1238. }
  1239. static u32
  1240. jffs2_1pass_build_lists(struct part_info * part)
  1241. {
  1242. struct b_lists *pL;
  1243. struct jffs2_unknown_node *node;
  1244. u32 nr_sectors = part->size/part->sector_size;
  1245. u32 i;
  1246. u32 counter4 = 0;
  1247. u32 counterF = 0;
  1248. u32 counterN = 0;
  1249. u32 max_totlen = 0;
  1250. u32 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
  1251. char *buf;
  1252. /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
  1253. /* jffs2 list building enterprise nope. in newer versions the overhead is */
  1254. /* only about 5 %. not enough to inconvenience people for. */
  1255. /* lcd_off(); */
  1256. /* if we are building a list we need to refresh the cache. */
  1257. jffs_init_1pass_list(part);
  1258. pL = (struct b_lists *)part->jffs2_priv;
  1259. buf = malloc(buf_size);
  1260. puts ("Scanning JFFS2 FS: ");
  1261. /* start at the beginning of the partition */
  1262. for (i = 0; i < nr_sectors; i++) {
  1263. uint32_t sector_ofs = i * part->sector_size;
  1264. uint32_t buf_ofs = sector_ofs;
  1265. uint32_t buf_len;
  1266. uint32_t ofs, prevofs;
  1267. struct jffs2_sum_marker *sm;
  1268. void *sumptr = NULL;
  1269. uint32_t sumlen;
  1270. int ret;
  1271. WATCHDOG_RESET();
  1272. buf_len = sizeof(*sm);
  1273. /* Read as much as we want into the _end_ of the preallocated
  1274. * buffer
  1275. */
  1276. get_fl_mem(part->offset + sector_ofs + part->sector_size -
  1277. buf_len, buf_len, buf + buf_size - buf_len);
  1278. sm = (void *)buf + buf_size - sizeof(*sm);
  1279. if (sm->magic == JFFS2_SUM_MAGIC) {
  1280. sumlen = part->sector_size - sm->offset;
  1281. sumptr = buf + buf_size - sumlen;
  1282. /* Now, make sure the summary itself is available */
  1283. if (sumlen > buf_size) {
  1284. /* Need to kmalloc for this. */
  1285. sumptr = malloc(sumlen);
  1286. if (!sumptr) {
  1287. putstr("Can't get memory for summary "
  1288. "node!\n");
  1289. return 0;
  1290. }
  1291. memcpy(sumptr + sumlen - buf_len, buf +
  1292. buf_size - buf_len, buf_len);
  1293. }
  1294. if (buf_len < sumlen) {
  1295. /* Need to read more so that the entire summary
  1296. * node is present
  1297. */
  1298. get_fl_mem(part->offset + sector_ofs +
  1299. part->sector_size - sumlen,
  1300. sumlen - buf_len, sumptr);
  1301. }
  1302. }
  1303. if (sumptr) {
  1304. ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
  1305. sumlen, pL);
  1306. if (buf_size && sumlen > buf_size)
  1307. free(sumptr);
  1308. if (ret < 0)
  1309. return 0;
  1310. if (ret)
  1311. continue;
  1312. }
  1313. buf_len = EMPTY_SCAN_SIZE(part->sector_size);
  1314. get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
  1315. /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
  1316. ofs = 0;
  1317. /* Scan only 4KiB of 0xFF before declaring it's empty */
  1318. while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
  1319. *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
  1320. ofs += 4;
  1321. if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
  1322. continue;
  1323. ofs += sector_ofs;
  1324. prevofs = ofs - 1;
  1325. scan_more:
  1326. while (ofs < sector_ofs + part->sector_size) {
  1327. if (ofs == prevofs) {
  1328. printf("offset %08x already seen, skip\n", ofs);
  1329. ofs += 4;
  1330. counter4++;
  1331. continue;
  1332. }
  1333. prevofs = ofs;
  1334. if (sector_ofs + part->sector_size <
  1335. ofs + sizeof(*node))
  1336. break;
  1337. if (buf_ofs + buf_len < ofs + sizeof(*node)) {
  1338. buf_len = min_t(uint32_t, buf_size, sector_ofs
  1339. + part->sector_size - ofs);
  1340. get_fl_mem((u32)part->offset + ofs, buf_len,
  1341. buf);
  1342. buf_ofs = ofs;
  1343. }
  1344. node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
  1345. if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
  1346. uint32_t inbuf_ofs;
  1347. uint32_t empty_start, scan_end;
  1348. empty_start = ofs;
  1349. ofs += 4;
  1350. scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
  1351. part->sector_size)/8,
  1352. buf_len);
  1353. more_empty:
  1354. inbuf_ofs = ofs - buf_ofs;
  1355. while (inbuf_ofs < scan_end) {
  1356. if (*(uint32_t *)(&buf[inbuf_ofs]) !=
  1357. 0xffffffff)
  1358. goto scan_more;
  1359. inbuf_ofs += 4;
  1360. ofs += 4;
  1361. }
  1362. /* Ran off end. */
  1363. /* See how much more there is to read in this
  1364. * eraseblock...
  1365. */
  1366. buf_len = min_t(uint32_t, buf_size,
  1367. sector_ofs +
  1368. part->sector_size - ofs);
  1369. if (!buf_len) {
  1370. /* No more to read. Break out of main
  1371. * loop without marking this range of
  1372. * empty space as dirty (because it's
  1373. * not)
  1374. */
  1375. break;
  1376. }
  1377. scan_end = buf_len;
  1378. get_fl_mem((u32)part->offset + ofs, buf_len,
  1379. buf);
  1380. buf_ofs = ofs;
  1381. goto more_empty;
  1382. }
  1383. if (node->magic != JFFS2_MAGIC_BITMASK ||
  1384. !hdr_crc(node)) {
  1385. ofs += 4;
  1386. counter4++;
  1387. continue;
  1388. }
  1389. if (ofs + node->totlen >
  1390. sector_ofs + part->sector_size) {
  1391. ofs += 4;
  1392. counter4++;
  1393. continue;
  1394. }
  1395. /* if its a fragment add it */
  1396. switch (node->nodetype) {
  1397. case JFFS2_NODETYPE_INODE:
  1398. if (buf_ofs + buf_len < ofs + sizeof(struct
  1399. jffs2_raw_inode)) {
  1400. get_fl_mem((u32)part->offset + ofs,
  1401. buf_len, buf);
  1402. buf_ofs = ofs;
  1403. node = (void *)buf;
  1404. }
  1405. if (!inode_crc((struct jffs2_raw_inode *) node))
  1406. break;
  1407. if (insert_node(&pL->frag, (u32) part->offset +
  1408. ofs) == NULL)
  1409. return 0;
  1410. if (max_totlen < node->totlen)
  1411. max_totlen = node->totlen;
  1412. break;
  1413. case JFFS2_NODETYPE_DIRENT:
  1414. if (buf_ofs + buf_len < ofs + sizeof(struct
  1415. jffs2_raw_dirent) +
  1416. ((struct
  1417. jffs2_raw_dirent *)
  1418. node)->nsize) {
  1419. get_fl_mem((u32)part->offset + ofs,
  1420. buf_len, buf);
  1421. buf_ofs = ofs;
  1422. node = (void *)buf;
  1423. }
  1424. if (!dirent_crc((struct jffs2_raw_dirent *)
  1425. node) ||
  1426. !dirent_name_crc(
  1427. (struct
  1428. jffs2_raw_dirent *)
  1429. node))
  1430. break;
  1431. if (! (counterN%100))
  1432. puts ("\b\b. ");
  1433. if (insert_node(&pL->dir, (u32) part->offset +
  1434. ofs) == NULL)
  1435. return 0;
  1436. if (max_totlen < node->totlen)
  1437. max_totlen = node->totlen;
  1438. counterN++;
  1439. break;
  1440. case JFFS2_NODETYPE_CLEANMARKER:
  1441. if (node->totlen != sizeof(struct jffs2_unknown_node))
  1442. printf("OOPS Cleanmarker has bad size "
  1443. "%d != %zu\n",
  1444. node->totlen,
  1445. sizeof(struct jffs2_unknown_node));
  1446. break;
  1447. case JFFS2_NODETYPE_PADDING:
  1448. if (node->totlen < sizeof(struct jffs2_unknown_node))
  1449. printf("OOPS Padding has bad size "
  1450. "%d < %zu\n",
  1451. node->totlen,
  1452. sizeof(struct jffs2_unknown_node));
  1453. break;
  1454. case JFFS2_NODETYPE_SUMMARY:
  1455. break;
  1456. default:
  1457. printf("Unknown node type: %x len %d offset 0x%x\n",
  1458. node->nodetype,
  1459. node->totlen, ofs);
  1460. }
  1461. ofs += ((node->totlen + 3) & ~3);
  1462. counterF++;
  1463. }
  1464. }
  1465. free(buf);
  1466. putstr("\b\b done.\r\n"); /* close off the dots */
  1467. /* We don't care if malloc failed - then each read operation will
  1468. * allocate its own buffer as necessary (NAND) or will read directly
  1469. * from flash (NOR).
  1470. */
  1471. pL->readbuf = malloc(max_totlen);
  1472. /* turn the lcd back on. */
  1473. /* splash(); */
  1474. #if 0
  1475. putLabeledWord("dir entries = ", pL->dir.listCount);
  1476. putLabeledWord("frag entries = ", pL->frag.listCount);
  1477. putLabeledWord("+4 increments = ", counter4);
  1478. putLabeledWord("+file_offset increments = ", counterF);
  1479. #endif
  1480. #ifdef DEBUG_DIRENTS
  1481. dump_dirents(pL);
  1482. #endif
  1483. #ifdef DEBUG_FRAGMENTS
  1484. dump_fragments(pL);
  1485. #endif
  1486. /* give visual feedback that we are done scanning the flash */
  1487. led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
  1488. return 1;
  1489. }
  1490. static u32
  1491. jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
  1492. {
  1493. struct b_node *b;
  1494. struct jffs2_raw_inode ojNode;
  1495. struct jffs2_raw_inode *jNode;
  1496. int i;
  1497. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1498. piL->compr_info[i].num_frags = 0;
  1499. piL->compr_info[i].compr_sum = 0;
  1500. piL->compr_info[i].decompr_sum = 0;
  1501. }
  1502. b = pL->frag.listHead;
  1503. while (b) {
  1504. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  1505. sizeof(ojNode), &ojNode);
  1506. if (jNode->compr < JFFS2_NUM_COMPR) {
  1507. piL->compr_info[jNode->compr].num_frags++;
  1508. piL->compr_info[jNode->compr].compr_sum += jNode->csize;
  1509. piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
  1510. }
  1511. b = b->next;
  1512. }
  1513. return 0;
  1514. }
  1515. static struct b_lists *
  1516. jffs2_get_list(struct part_info * part, const char *who)
  1517. {
  1518. /* copy requested part_info struct pointer to global location */
  1519. current_part = part;
  1520. if (jffs2_1pass_rescan_needed(part)) {
  1521. if (!jffs2_1pass_build_lists(part)) {
  1522. printf("%s: Failed to scan JFFSv2 file structure\n", who);
  1523. return NULL;
  1524. }
  1525. }
  1526. return (struct b_lists *)part->jffs2_priv;
  1527. }
  1528. /* Print directory / file contents */
  1529. u32
  1530. jffs2_1pass_ls(struct part_info * part, const char *fname)
  1531. {
  1532. struct b_lists *pl;
  1533. long ret = 1;
  1534. u32 inode;
  1535. if (! (pl = jffs2_get_list(part, "ls")))
  1536. return 0;
  1537. if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
  1538. putstr("ls: Failed to scan jffs2 file structure\r\n");
  1539. return 0;
  1540. }
  1541. #if 0
  1542. putLabeledWord("found file at inode = ", inode);
  1543. putLabeledWord("read_inode returns = ", ret);
  1544. #endif
  1545. return ret;
  1546. }
  1547. /* Load a file from flash into memory. fname can be a full path */
  1548. u32
  1549. jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
  1550. {
  1551. struct b_lists *pl;
  1552. long ret = 1;
  1553. u32 inode;
  1554. if (! (pl = jffs2_get_list(part, "load")))
  1555. return 0;
  1556. if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
  1557. putstr("load: Failed to find inode\r\n");
  1558. return 0;
  1559. }
  1560. /* Resolve symlinks */
  1561. if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
  1562. putstr("load: Failed to resolve inode structure\r\n");
  1563. return 0;
  1564. }
  1565. if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
  1566. putstr("load: Failed to read inode\r\n");
  1567. return 0;
  1568. }
  1569. DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
  1570. (unsigned long) dest, ret);
  1571. return ret;
  1572. }
  1573. /* Return information about the fs on this partition */
  1574. u32
  1575. jffs2_1pass_info(struct part_info * part)
  1576. {
  1577. struct b_jffs2_info info;
  1578. struct b_lists *pl;
  1579. int i;
  1580. if (! (pl = jffs2_get_list(part, "info")))
  1581. return 0;
  1582. jffs2_1pass_fill_info(pl, &info);
  1583. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1584. printf ("Compression: %s\n"
  1585. "\tfrag count: %d\n"
  1586. "\tcompressed sum: %d\n"
  1587. "\tuncompressed sum: %d\n",
  1588. compr_names[i],
  1589. info.compr_info[i].num_frags,
  1590. info.compr_info[i].compr_sum,
  1591. info.compr_info[i].decompr_sum);
  1592. }
  1593. return 1;
  1594. }