jffs2_1pass.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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 (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. ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
  687. break;
  688. case JFFS2_COMPR_ZERO:
  689. ret = 0;
  690. for (i = 0; i < jNode->dsize; i++)
  691. *(lDest++) = 0;
  692. break;
  693. case JFFS2_COMPR_RTIME:
  694. ret = 0;
  695. rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
  696. break;
  697. case JFFS2_COMPR_DYNRUBIN:
  698. /* this is slow but it works */
  699. ret = 0;
  700. dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
  701. break;
  702. case JFFS2_COMPR_ZLIB:
  703. ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
  704. break;
  705. #if defined(CONFIG_JFFS2_LZO_LZARI)
  706. case JFFS2_COMPR_LZO:
  707. ret = lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
  708. break;
  709. case JFFS2_COMPR_LZARI:
  710. ret = lzari_decompress(src, lDest, jNode->csize, jNode->dsize);
  711. break;
  712. #endif
  713. default:
  714. /* unknown */
  715. putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
  716. put_fl_mem(jNode, pL->readbuf);
  717. return -1;
  718. break;
  719. }
  720. }
  721. #if 0
  722. putLabeledWord("read_inode: totalSize = ", totalSize);
  723. putLabeledWord("read_inode: compr ret = ", ret);
  724. #endif
  725. }
  726. counter++;
  727. put_fl_mem(jNode, pL->readbuf);
  728. }
  729. #if 0
  730. putLabeledWord("read_inode: returning = ", totalSize);
  731. #endif
  732. return totalSize;
  733. }
  734. /* find the inode from the slashless name given a parent */
  735. static u32
  736. jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
  737. {
  738. struct b_node *b;
  739. struct jffs2_raw_dirent *jDir;
  740. int len;
  741. u32 counter;
  742. u32 version = 0;
  743. u32 inode = 0;
  744. /* name is assumed slash free */
  745. len = strlen(name);
  746. counter = 0;
  747. /* we need to search all and return the inode with the highest version */
  748. for(b = pL->dir.listHead; b; b = b->next, counter++) {
  749. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  750. pL->readbuf);
  751. if ((pino == jDir->pino) && (len == jDir->nsize) &&
  752. (jDir->ino) && /* 0 for unlink */
  753. (!strncmp((char *)jDir->name, name, len))) { /* a match */
  754. if (jDir->version < version) {
  755. put_fl_mem(jDir, pL->readbuf);
  756. continue;
  757. }
  758. if (jDir->version == version && inode != 0) {
  759. /* I'm pretty sure this isn't legal */
  760. putstr(" ** ERROR ** ");
  761. putnstr(jDir->name, jDir->nsize);
  762. putLabeledWord(" has dup version =", version);
  763. }
  764. inode = jDir->ino;
  765. version = jDir->version;
  766. }
  767. #if 0
  768. putstr("\r\nfind_inode:p&l ->");
  769. putnstr(jDir->name, jDir->nsize);
  770. putstr("\r\n");
  771. putLabeledWord("pino = ", jDir->pino);
  772. putLabeledWord("nsize = ", jDir->nsize);
  773. putLabeledWord("b = ", (u32) b);
  774. putLabeledWord("counter = ", counter);
  775. #endif
  776. put_fl_mem(jDir, pL->readbuf);
  777. }
  778. return inode;
  779. }
  780. char *mkmodestr(unsigned long mode, char *str)
  781. {
  782. static const char *l = "xwr";
  783. int mask = 1, i;
  784. char c;
  785. switch (mode & S_IFMT) {
  786. case S_IFDIR: str[0] = 'd'; break;
  787. case S_IFBLK: str[0] = 'b'; break;
  788. case S_IFCHR: str[0] = 'c'; break;
  789. case S_IFIFO: str[0] = 'f'; break;
  790. case S_IFLNK: str[0] = 'l'; break;
  791. case S_IFSOCK: str[0] = 's'; break;
  792. case S_IFREG: str[0] = '-'; break;
  793. default: str[0] = '?';
  794. }
  795. for(i = 0; i < 9; i++) {
  796. c = l[i%3];
  797. str[9-i] = (mode & mask)?c:'-';
  798. mask = mask<<1;
  799. }
  800. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  801. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  802. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  803. str[10] = '\0';
  804. return str;
  805. }
  806. static inline void dump_stat(struct stat *st, const char *name)
  807. {
  808. char str[20];
  809. char s[64], *p;
  810. if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
  811. st->st_mtime = 1;
  812. ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
  813. if ((p = strchr(s,'\n')) != NULL) *p = '\0';
  814. if ((p = strchr(s,'\r')) != NULL) *p = '\0';
  815. /*
  816. printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
  817. st->st_size, s, name);
  818. */
  819. printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
  820. }
  821. static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
  822. {
  823. char fname[256];
  824. struct stat st;
  825. if(!d || !i) return -1;
  826. strncpy(fname, (char *)d->name, d->nsize);
  827. fname[d->nsize] = '\0';
  828. memset(&st,0,sizeof(st));
  829. st.st_mtime = i->mtime;
  830. st.st_mode = i->mode;
  831. st.st_ino = i->ino;
  832. st.st_size = i->isize;
  833. dump_stat(&st, fname);
  834. if (d->type == DT_LNK) {
  835. unsigned char *src = (unsigned char *) (&i[1]);
  836. putstr(" -> ");
  837. putnstr(src, (int)i->dsize);
  838. }
  839. putstr("\r\n");
  840. return 0;
  841. }
  842. /* list inodes with the given pino */
  843. static u32
  844. jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
  845. {
  846. struct b_node *b;
  847. struct jffs2_raw_dirent *jDir;
  848. for (b = pL->dir.listHead; b; b = b->next) {
  849. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  850. pL->readbuf);
  851. if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
  852. u32 i_version = 0;
  853. struct jffs2_raw_inode ojNode;
  854. struct jffs2_raw_inode *jNode, *i = NULL;
  855. struct b_node *b2 = pL->frag.listHead;
  856. while (b2) {
  857. jNode = (struct jffs2_raw_inode *)
  858. get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
  859. if (jNode->ino == jDir->ino && jNode->version >= i_version) {
  860. i_version = jNode->version;
  861. if (i)
  862. put_fl_mem(i, NULL);
  863. if (jDir->type == DT_LNK)
  864. i = get_node_mem(b2->offset,
  865. NULL);
  866. else
  867. i = get_fl_mem(b2->offset,
  868. sizeof(*i),
  869. NULL);
  870. }
  871. b2 = b2->next;
  872. }
  873. dump_inode(pL, jDir, i);
  874. put_fl_mem(i, NULL);
  875. }
  876. put_fl_mem(jDir, pL->readbuf);
  877. }
  878. return pino;
  879. }
  880. static u32
  881. jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
  882. {
  883. int i;
  884. char tmp[256];
  885. char working_tmp[256];
  886. char *c;
  887. /* discard any leading slash */
  888. i = 0;
  889. while (fname[i] == '/')
  890. i++;
  891. strcpy(tmp, &fname[i]);
  892. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  893. {
  894. strncpy(working_tmp, tmp, c - tmp);
  895. working_tmp[c - tmp] = '\0';
  896. #if 0
  897. putstr("search_inode: tmp = ");
  898. putstr(tmp);
  899. putstr("\r\n");
  900. putstr("search_inode: wtmp = ");
  901. putstr(working_tmp);
  902. putstr("\r\n");
  903. putstr("search_inode: c = ");
  904. putstr(c);
  905. putstr("\r\n");
  906. #endif
  907. for (i = 0; i < strlen(c) - 1; i++)
  908. tmp[i] = c[i + 1];
  909. tmp[i] = '\0';
  910. #if 0
  911. putstr("search_inode: post tmp = ");
  912. putstr(tmp);
  913. putstr("\r\n");
  914. #endif
  915. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
  916. putstr("find_inode failed for name=");
  917. putstr(working_tmp);
  918. putstr("\r\n");
  919. return 0;
  920. }
  921. }
  922. /* this is for the bare filename, directories have already been mapped */
  923. if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  924. putstr("find_inode failed for name=");
  925. putstr(tmp);
  926. putstr("\r\n");
  927. return 0;
  928. }
  929. return pino;
  930. }
  931. static u32
  932. jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
  933. {
  934. struct b_node *b;
  935. struct b_node *b2;
  936. struct jffs2_raw_dirent *jDir;
  937. struct jffs2_raw_inode *jNode;
  938. u8 jDirFoundType = 0;
  939. u32 jDirFoundIno = 0;
  940. u32 jDirFoundPino = 0;
  941. char tmp[256];
  942. u32 version = 0;
  943. u32 pino;
  944. unsigned char *src;
  945. /* we need to search all and return the inode with the highest version */
  946. for(b = pL->dir.listHead; b; b = b->next) {
  947. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  948. pL->readbuf);
  949. if (ino == jDir->ino) {
  950. if (jDir->version < version) {
  951. put_fl_mem(jDir, pL->readbuf);
  952. continue;
  953. }
  954. if (jDir->version == version && jDirFoundType) {
  955. /* I'm pretty sure this isn't legal */
  956. putstr(" ** ERROR ** ");
  957. putnstr(jDir->name, jDir->nsize);
  958. putLabeledWord(" has dup version (resolve) = ",
  959. version);
  960. }
  961. jDirFoundType = jDir->type;
  962. jDirFoundIno = jDir->ino;
  963. jDirFoundPino = jDir->pino;
  964. version = jDir->version;
  965. }
  966. put_fl_mem(jDir, pL->readbuf);
  967. }
  968. /* now we found the right entry again. (shoulda returned inode*) */
  969. if (jDirFoundType != DT_LNK)
  970. return jDirFoundIno;
  971. /* it's a soft link so we follow it again. */
  972. b2 = pL->frag.listHead;
  973. while (b2) {
  974. jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
  975. pL->readbuf);
  976. if (jNode->ino == jDirFoundIno) {
  977. src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
  978. #if 0
  979. putLabeledWord("\t\t dsize = ", jNode->dsize);
  980. putstr("\t\t target = ");
  981. putnstr(src, jNode->dsize);
  982. putstr("\r\n");
  983. #endif
  984. strncpy(tmp, (char *)src, jNode->dsize);
  985. tmp[jNode->dsize] = '\0';
  986. put_fl_mem(jNode, pL->readbuf);
  987. break;
  988. }
  989. b2 = b2->next;
  990. put_fl_mem(jNode, pL->readbuf);
  991. }
  992. /* ok so the name of the new file to find is in tmp */
  993. /* if it starts with a slash it is root based else shared dirs */
  994. if (tmp[0] == '/')
  995. pino = 1;
  996. else
  997. pino = jDirFoundPino;
  998. return jffs2_1pass_search_inode(pL, tmp, pino);
  999. }
  1000. static u32
  1001. jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
  1002. {
  1003. int i;
  1004. char tmp[256];
  1005. char working_tmp[256];
  1006. char *c;
  1007. /* discard any leading slash */
  1008. i = 0;
  1009. while (fname[i] == '/')
  1010. i++;
  1011. strcpy(tmp, &fname[i]);
  1012. working_tmp[0] = '\0';
  1013. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  1014. {
  1015. strncpy(working_tmp, tmp, c - tmp);
  1016. working_tmp[c - tmp] = '\0';
  1017. for (i = 0; i < strlen(c) - 1; i++)
  1018. tmp[i] = c[i + 1];
  1019. tmp[i] = '\0';
  1020. /* only a failure if we arent looking at top level */
  1021. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
  1022. (working_tmp[0])) {
  1023. putstr("find_inode failed for name=");
  1024. putstr(working_tmp);
  1025. putstr("\r\n");
  1026. return 0;
  1027. }
  1028. }
  1029. if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  1030. putstr("find_inode failed for name=");
  1031. putstr(tmp);
  1032. putstr("\r\n");
  1033. return 0;
  1034. }
  1035. /* this is for the bare filename, directories have already been mapped */
  1036. if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
  1037. putstr("find_inode failed for name=");
  1038. putstr(tmp);
  1039. putstr("\r\n");
  1040. return 0;
  1041. }
  1042. return pino;
  1043. }
  1044. unsigned char
  1045. jffs2_1pass_rescan_needed(struct part_info *part)
  1046. {
  1047. struct b_node *b;
  1048. struct jffs2_unknown_node onode;
  1049. struct jffs2_unknown_node *node;
  1050. struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
  1051. if (part->jffs2_priv == 0){
  1052. DEBUGF ("rescan: First time in use\n");
  1053. return 1;
  1054. }
  1055. /* if we have no list, we need to rescan */
  1056. if (pL->frag.listCount == 0) {
  1057. DEBUGF ("rescan: fraglist zero\n");
  1058. return 1;
  1059. }
  1060. /* but suppose someone reflashed a partition at the same offset... */
  1061. b = pL->dir.listHead;
  1062. while (b) {
  1063. node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
  1064. sizeof(onode), &onode);
  1065. if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
  1066. DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
  1067. (unsigned long) b->offset);
  1068. return 1;
  1069. }
  1070. b = b->next;
  1071. }
  1072. return 0;
  1073. }
  1074. #define dbg_summary(...) do {} while (0);
  1075. /* Process the stored summary information - helper function for
  1076. * jffs2_sum_scan_sumnode()
  1077. */
  1078. static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
  1079. struct jffs2_raw_summary *summary,
  1080. struct b_lists *pL)
  1081. {
  1082. void *sp;
  1083. int i;
  1084. sp = summary->sum;
  1085. for (i = 0; i < summary->sum_num; i++) {
  1086. dbg_summary("processing summary index %d\n", i);
  1087. switch (((struct jffs2_sum_unknown_flash *)sp)->nodetype) {
  1088. case JFFS2_NODETYPE_INODE: {
  1089. struct jffs2_sum_inode_flash *spi;
  1090. spi = sp;
  1091. dbg_summary("Inode at 0x%08x-0x%08x\n",
  1092. offset + spi->offset,
  1093. offset + spi->offset + spi->totlen);
  1094. if (insert_node(&pL->frag, (u32) part->offset +
  1095. offset + spi->offset) == NULL)
  1096. return -1;
  1097. sp += JFFS2_SUMMARY_INODE_SIZE;
  1098. break;
  1099. }
  1100. case JFFS2_NODETYPE_DIRENT: {
  1101. struct jffs2_sum_dirent_flash *spd;
  1102. spd = sp;
  1103. dbg_summary("Dirent at 0x%08x-0x%08x\n",
  1104. offset + spd->offset,
  1105. offset + spd->offset + spd->totlen);
  1106. if (insert_node(&pL->dir, (u32) part->offset +
  1107. offset + spd->offset) == NULL)
  1108. return -1;
  1109. sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
  1110. break;
  1111. }
  1112. default : {
  1113. uint16_t nodetype =
  1114. ((struct jffs2_sum_unknown_flash *)
  1115. sp)->nodetype;
  1116. printf("Unsupported node type %x found in "
  1117. "summary!\n", nodetype);
  1118. break;
  1119. }
  1120. }
  1121. }
  1122. return 0;
  1123. }
  1124. /* Process the summary node - called from jffs2_scan_eraseblock() */
  1125. int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
  1126. struct jffs2_raw_summary *summary, uint32_t sumsize,
  1127. struct b_lists *pL)
  1128. {
  1129. struct jffs2_unknown_node crcnode;
  1130. int ret, ofs;
  1131. uint32_t crc;
  1132. ofs = part->sector_size - sumsize;
  1133. dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
  1134. offset, offset + ofs, sumsize);
  1135. /* OK, now check for node validity and CRC */
  1136. crcnode.magic = JFFS2_MAGIC_BITMASK;
  1137. crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
  1138. crcnode.totlen = summary->totlen;
  1139. crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
  1140. if (summary->hdr_crc != crc) {
  1141. dbg_summary("Summary node header is corrupt (bad CRC or "
  1142. "no summary at all)\n");
  1143. goto crc_err;
  1144. }
  1145. if (summary->totlen != sumsize) {
  1146. dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
  1147. goto crc_err;
  1148. }
  1149. crc = crc32_no_comp(0, (uchar *)summary,
  1150. sizeof(struct jffs2_raw_summary)-8);
  1151. if (summary->node_crc != crc) {
  1152. dbg_summary("Summary node is corrupt (bad CRC)\n");
  1153. goto crc_err;
  1154. }
  1155. crc = crc32_no_comp(0, (uchar *)summary->sum,
  1156. sumsize - sizeof(struct jffs2_raw_summary));
  1157. if (summary->sum_crc != crc) {
  1158. dbg_summary("Summary node data is corrupt (bad CRC)\n");
  1159. goto crc_err;
  1160. }
  1161. if (summary->cln_mkr)
  1162. dbg_summary("Summary : CLEANMARKER node \n");
  1163. ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
  1164. if (ret)
  1165. return ret; /* real error */
  1166. return 1;
  1167. crc_err:
  1168. putstr("Summary node crc error, skipping summary information.\n");
  1169. return 0;
  1170. }
  1171. #ifdef DEBUG_FRAGMENTS
  1172. static void
  1173. dump_fragments(struct b_lists *pL)
  1174. {
  1175. struct b_node *b;
  1176. struct jffs2_raw_inode ojNode;
  1177. struct jffs2_raw_inode *jNode;
  1178. putstr("\r\n\r\n******The fragment Entries******\r\n");
  1179. b = pL->frag.listHead;
  1180. while (b) {
  1181. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  1182. sizeof(ojNode), &ojNode);
  1183. putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
  1184. putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
  1185. putLabeledWord("\tbuild_list: inode = ", jNode->ino);
  1186. putLabeledWord("\tbuild_list: version = ", jNode->version);
  1187. putLabeledWord("\tbuild_list: isize = ", jNode->isize);
  1188. putLabeledWord("\tbuild_list: atime = ", jNode->atime);
  1189. putLabeledWord("\tbuild_list: offset = ", jNode->offset);
  1190. putLabeledWord("\tbuild_list: csize = ", jNode->csize);
  1191. putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
  1192. putLabeledWord("\tbuild_list: compr = ", jNode->compr);
  1193. putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
  1194. putLabeledWord("\tbuild_list: flags = ", jNode->flags);
  1195. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  1196. b = b->next;
  1197. }
  1198. }
  1199. #endif
  1200. #ifdef DEBUG_DIRENTS
  1201. static void
  1202. dump_dirents(struct b_lists *pL)
  1203. {
  1204. struct b_node *b;
  1205. struct jffs2_raw_dirent *jDir;
  1206. putstr("\r\n\r\n******The directory Entries******\r\n");
  1207. b = pL->dir.listHead;
  1208. while (b) {
  1209. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
  1210. pL->readbuf);
  1211. putstr("\r\n");
  1212. putnstr(jDir->name, jDir->nsize);
  1213. putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
  1214. putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
  1215. putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
  1216. putLabeledWord("\tbuild_list: pino = ", jDir->pino);
  1217. putLabeledWord("\tbuild_list: version = ", jDir->version);
  1218. putLabeledWord("\tbuild_list: ino = ", jDir->ino);
  1219. putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
  1220. putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
  1221. putLabeledWord("\tbuild_list: type = ", jDir->type);
  1222. putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
  1223. putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
  1224. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  1225. b = b->next;
  1226. put_fl_mem(jDir, pL->readbuf);
  1227. }
  1228. }
  1229. #endif
  1230. #define min_t(type, x, y) ({ \
  1231. type __min1 = (x); \
  1232. type __min2 = (y); \
  1233. __min1 < __min2 ? __min1: __min2; })
  1234. #define DEFAULT_EMPTY_SCAN_SIZE 4096
  1235. static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
  1236. {
  1237. if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
  1238. return sector_size;
  1239. else
  1240. return DEFAULT_EMPTY_SCAN_SIZE;
  1241. }
  1242. static u32
  1243. jffs2_1pass_build_lists(struct part_info * part)
  1244. {
  1245. struct b_lists *pL;
  1246. struct jffs2_unknown_node *node;
  1247. u32 nr_sectors = part->size/part->sector_size;
  1248. u32 i;
  1249. u32 counter4 = 0;
  1250. u32 counterF = 0;
  1251. u32 counterN = 0;
  1252. u32 max_totlen = 0;
  1253. u32 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
  1254. char *buf;
  1255. /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
  1256. /* jffs2 list building enterprise nope. in newer versions the overhead is */
  1257. /* only about 5 %. not enough to inconvenience people for. */
  1258. /* lcd_off(); */
  1259. /* if we are building a list we need to refresh the cache. */
  1260. jffs_init_1pass_list(part);
  1261. pL = (struct b_lists *)part->jffs2_priv;
  1262. buf = malloc(buf_size);
  1263. puts ("Scanning JFFS2 FS: ");
  1264. /* start at the beginning of the partition */
  1265. for (i = 0; i < nr_sectors; i++) {
  1266. uint32_t sector_ofs = i * part->sector_size;
  1267. uint32_t buf_ofs = sector_ofs;
  1268. uint32_t buf_len;
  1269. uint32_t ofs, prevofs;
  1270. struct jffs2_sum_marker *sm;
  1271. void *sumptr = NULL;
  1272. uint32_t sumlen;
  1273. int ret;
  1274. WATCHDOG_RESET();
  1275. buf_len = sizeof(*sm);
  1276. /* Read as much as we want into the _end_ of the preallocated
  1277. * buffer
  1278. */
  1279. get_fl_mem(part->offset + sector_ofs + part->sector_size -
  1280. buf_len, buf_len, buf + buf_size - buf_len);
  1281. sm = (void *)buf + buf_size - sizeof(*sm);
  1282. if (sm->magic == JFFS2_SUM_MAGIC) {
  1283. sumlen = part->sector_size - sm->offset;
  1284. sumptr = buf + buf_size - sumlen;
  1285. /* Now, make sure the summary itself is available */
  1286. if (sumlen > buf_size) {
  1287. /* Need to kmalloc for this. */
  1288. sumptr = malloc(sumlen);
  1289. if (!sumptr) {
  1290. putstr("Can't get memory for summary "
  1291. "node!\n");
  1292. return 0;
  1293. }
  1294. memcpy(sumptr + sumlen - buf_len, buf +
  1295. buf_size - buf_len, buf_len);
  1296. }
  1297. if (buf_len < sumlen) {
  1298. /* Need to read more so that the entire summary
  1299. * node is present
  1300. */
  1301. get_fl_mem(part->offset + sector_ofs +
  1302. part->sector_size - sumlen,
  1303. sumlen - buf_len, sumptr);
  1304. }
  1305. }
  1306. if (sumptr) {
  1307. ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
  1308. sumlen, pL);
  1309. if (buf_size && sumlen > buf_size)
  1310. free(sumptr);
  1311. if (ret < 0)
  1312. return 0;
  1313. if (ret)
  1314. continue;
  1315. }
  1316. buf_len = EMPTY_SCAN_SIZE(part->sector_size);
  1317. get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
  1318. /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
  1319. ofs = 0;
  1320. /* Scan only 4KiB of 0xFF before declaring it's empty */
  1321. while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
  1322. *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
  1323. ofs += 4;
  1324. if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
  1325. continue;
  1326. ofs += sector_ofs;
  1327. prevofs = ofs - 1;
  1328. scan_more:
  1329. while (ofs < sector_ofs + part->sector_size) {
  1330. if (ofs == prevofs) {
  1331. printf("offset %08x already seen, skip\n", ofs);
  1332. ofs += 4;
  1333. counter4++;
  1334. continue;
  1335. }
  1336. prevofs = ofs;
  1337. if (sector_ofs + part->sector_size <
  1338. ofs + sizeof(*node))
  1339. break;
  1340. if (buf_ofs + buf_len < ofs + sizeof(*node)) {
  1341. buf_len = min_t(uint32_t, buf_size, sector_ofs
  1342. + part->sector_size - ofs);
  1343. get_fl_mem((u32)part->offset + ofs, buf_len,
  1344. buf);
  1345. buf_ofs = ofs;
  1346. }
  1347. node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs];
  1348. if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
  1349. uint32_t inbuf_ofs;
  1350. uint32_t empty_start, scan_end;
  1351. empty_start = ofs;
  1352. ofs += 4;
  1353. scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
  1354. part->sector_size)/8,
  1355. buf_len);
  1356. more_empty:
  1357. inbuf_ofs = ofs - buf_ofs;
  1358. while (inbuf_ofs < scan_end) {
  1359. if (*(uint32_t *)(&buf[inbuf_ofs]) !=
  1360. 0xffffffff)
  1361. goto scan_more;
  1362. inbuf_ofs += 4;
  1363. ofs += 4;
  1364. }
  1365. /* Ran off end. */
  1366. /* See how much more there is to read in this
  1367. * eraseblock...
  1368. */
  1369. buf_len = min_t(uint32_t, buf_size,
  1370. sector_ofs +
  1371. part->sector_size - ofs);
  1372. if (!buf_len) {
  1373. /* No more to read. Break out of main
  1374. * loop without marking this range of
  1375. * empty space as dirty (because it's
  1376. * not)
  1377. */
  1378. break;
  1379. }
  1380. scan_end = buf_len;
  1381. get_fl_mem((u32)part->offset + ofs, buf_len,
  1382. buf);
  1383. buf_ofs = ofs;
  1384. goto more_empty;
  1385. }
  1386. if (node->magic != JFFS2_MAGIC_BITMASK ||
  1387. !hdr_crc(node)) {
  1388. ofs += 4;
  1389. counter4++;
  1390. continue;
  1391. }
  1392. if (ofs + node->totlen >
  1393. sector_ofs + part->sector_size) {
  1394. ofs += 4;
  1395. counter4++;
  1396. continue;
  1397. }
  1398. /* if its a fragment add it */
  1399. switch (node->nodetype) {
  1400. case JFFS2_NODETYPE_INODE:
  1401. if (buf_ofs + buf_len < ofs + sizeof(struct
  1402. jffs2_raw_inode)) {
  1403. get_fl_mem((u32)part->offset + ofs,
  1404. buf_len, buf);
  1405. buf_ofs = ofs;
  1406. node = (void *)buf;
  1407. }
  1408. if (!inode_crc((struct jffs2_raw_inode *) node))
  1409. break;
  1410. if (insert_node(&pL->frag, (u32) part->offset +
  1411. ofs) == NULL)
  1412. return 0;
  1413. if (max_totlen < node->totlen)
  1414. max_totlen = node->totlen;
  1415. break;
  1416. case JFFS2_NODETYPE_DIRENT:
  1417. if (buf_ofs + buf_len < ofs + sizeof(struct
  1418. jffs2_raw_dirent) +
  1419. ((struct
  1420. jffs2_raw_dirent *)
  1421. node)->nsize) {
  1422. get_fl_mem((u32)part->offset + ofs,
  1423. buf_len, buf);
  1424. buf_ofs = ofs;
  1425. node = (void *)buf;
  1426. }
  1427. if (!dirent_crc((struct jffs2_raw_dirent *)
  1428. node) ||
  1429. !dirent_name_crc(
  1430. (struct
  1431. jffs2_raw_dirent *)
  1432. node))
  1433. break;
  1434. if (! (counterN%100))
  1435. puts ("\b\b. ");
  1436. if (insert_node(&pL->dir, (u32) part->offset +
  1437. ofs) == NULL)
  1438. return 0;
  1439. if (max_totlen < node->totlen)
  1440. max_totlen = node->totlen;
  1441. counterN++;
  1442. break;
  1443. case JFFS2_NODETYPE_CLEANMARKER:
  1444. if (node->totlen != sizeof(struct jffs2_unknown_node))
  1445. printf("OOPS Cleanmarker has bad size "
  1446. "%d != %zu\n",
  1447. node->totlen,
  1448. sizeof(struct jffs2_unknown_node));
  1449. break;
  1450. case JFFS2_NODETYPE_PADDING:
  1451. if (node->totlen < sizeof(struct jffs2_unknown_node))
  1452. printf("OOPS Padding has bad size "
  1453. "%d < %zu\n",
  1454. node->totlen,
  1455. sizeof(struct jffs2_unknown_node));
  1456. break;
  1457. case JFFS2_NODETYPE_SUMMARY:
  1458. break;
  1459. default:
  1460. printf("Unknown node type: %x len %d offset 0x%x\n",
  1461. node->nodetype,
  1462. node->totlen, ofs);
  1463. }
  1464. ofs += ((node->totlen + 3) & ~3);
  1465. counterF++;
  1466. }
  1467. }
  1468. free(buf);
  1469. putstr("\b\b done.\r\n"); /* close off the dots */
  1470. /* We don't care if malloc failed - then each read operation will
  1471. * allocate its own buffer as necessary (NAND) or will read directly
  1472. * from flash (NOR).
  1473. */
  1474. pL->readbuf = malloc(max_totlen);
  1475. /* turn the lcd back on. */
  1476. /* splash(); */
  1477. #if 0
  1478. putLabeledWord("dir entries = ", pL->dir.listCount);
  1479. putLabeledWord("frag entries = ", pL->frag.listCount);
  1480. putLabeledWord("+4 increments = ", counter4);
  1481. putLabeledWord("+file_offset increments = ", counterF);
  1482. #endif
  1483. #ifdef DEBUG_DIRENTS
  1484. dump_dirents(pL);
  1485. #endif
  1486. #ifdef DEBUG_FRAGMENTS
  1487. dump_fragments(pL);
  1488. #endif
  1489. /* give visual feedback that we are done scanning the flash */
  1490. led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
  1491. return 1;
  1492. }
  1493. static u32
  1494. jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
  1495. {
  1496. struct b_node *b;
  1497. struct jffs2_raw_inode ojNode;
  1498. struct jffs2_raw_inode *jNode;
  1499. int i;
  1500. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1501. piL->compr_info[i].num_frags = 0;
  1502. piL->compr_info[i].compr_sum = 0;
  1503. piL->compr_info[i].decompr_sum = 0;
  1504. }
  1505. b = pL->frag.listHead;
  1506. while (b) {
  1507. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  1508. sizeof(ojNode), &ojNode);
  1509. if (jNode->compr < JFFS2_NUM_COMPR) {
  1510. piL->compr_info[jNode->compr].num_frags++;
  1511. piL->compr_info[jNode->compr].compr_sum += jNode->csize;
  1512. piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
  1513. }
  1514. b = b->next;
  1515. }
  1516. return 0;
  1517. }
  1518. static struct b_lists *
  1519. jffs2_get_list(struct part_info * part, const char *who)
  1520. {
  1521. /* copy requested part_info struct pointer to global location */
  1522. current_part = part;
  1523. if (jffs2_1pass_rescan_needed(part)) {
  1524. if (!jffs2_1pass_build_lists(part)) {
  1525. printf("%s: Failed to scan JFFSv2 file structure\n", who);
  1526. return NULL;
  1527. }
  1528. }
  1529. return (struct b_lists *)part->jffs2_priv;
  1530. }
  1531. /* Print directory / file contents */
  1532. u32
  1533. jffs2_1pass_ls(struct part_info * part, const char *fname)
  1534. {
  1535. struct b_lists *pl;
  1536. long ret = 1;
  1537. u32 inode;
  1538. if (! (pl = jffs2_get_list(part, "ls")))
  1539. return 0;
  1540. if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
  1541. putstr("ls: Failed to scan jffs2 file structure\r\n");
  1542. return 0;
  1543. }
  1544. #if 0
  1545. putLabeledWord("found file at inode = ", inode);
  1546. putLabeledWord("read_inode returns = ", ret);
  1547. #endif
  1548. return ret;
  1549. }
  1550. /* Load a file from flash into memory. fname can be a full path */
  1551. u32
  1552. jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
  1553. {
  1554. struct b_lists *pl;
  1555. long ret = 1;
  1556. u32 inode;
  1557. if (! (pl = jffs2_get_list(part, "load")))
  1558. return 0;
  1559. if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
  1560. putstr("load: Failed to find inode\r\n");
  1561. return 0;
  1562. }
  1563. /* Resolve symlinks */
  1564. if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
  1565. putstr("load: Failed to resolve inode structure\r\n");
  1566. return 0;
  1567. }
  1568. if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
  1569. putstr("load: Failed to read inode\r\n");
  1570. return 0;
  1571. }
  1572. DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
  1573. (unsigned long) dest, ret);
  1574. return ret;
  1575. }
  1576. /* Return information about the fs on this partition */
  1577. u32
  1578. jffs2_1pass_info(struct part_info * part)
  1579. {
  1580. struct b_jffs2_info info;
  1581. struct b_lists *pl;
  1582. int i;
  1583. if (! (pl = jffs2_get_list(part, "info")))
  1584. return 0;
  1585. jffs2_1pass_fill_info(pl, &info);
  1586. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1587. printf ("Compression: %s\n"
  1588. "\tfrag count: %d\n"
  1589. "\tcompressed sum: %d\n"
  1590. "\tuncompressed sum: %d\n",
  1591. compr_names[i],
  1592. info.compr_info[i].num_frags,
  1593. info.compr_info[i].compr_sum,
  1594. info.compr_info[i].decompr_sum);
  1595. }
  1596. return 1;
  1597. }