jffs2_1pass.c 49 KB

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