jffs2_1pass.c 49 KB

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