jffs2_1pass.c 50 KB

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