jffs2_1pass.c 49 KB

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