jffs2_1pass.c 49 KB

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