jffs2_1pass.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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 CFG_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 <linux/stat.h>
  113. #include <linux/time.h>
  114. #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
  115. #include <jffs2/jffs2.h>
  116. #include <jffs2/jffs2_1pass.h>
  117. #include "jffs2_private.h"
  118. #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
  119. #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
  120. /* Debugging switches */
  121. #undef DEBUG_DIRENTS /* print directory entry list after scan */
  122. #undef DEBUG_FRAGMENTS /* print fragment list after scan */
  123. #undef DEBUG /* enable debugging messages */
  124. #ifdef DEBUG
  125. # define DEBUGF(fmt,args...) printf(fmt ,##args)
  126. #else
  127. # define DEBUGF(fmt,args...)
  128. #endif
  129. /* keeps pointer to currentlu processed partition */
  130. static struct part_info *current_part;
  131. #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
  132. #if defined(CFG_NAND_LEGACY)
  133. #include <linux/mtd/nand_legacy.h>
  134. #else
  135. #include <nand.h>
  136. #endif
  137. /*
  138. * Support for jffs2 on top of NAND-flash
  139. *
  140. * NAND memory isn't mapped in processor's address space,
  141. * so data should be fetched from flash before
  142. * being processed. This is exactly what functions declared
  143. * here do.
  144. *
  145. */
  146. #if defined(CFG_NAND_LEGACY)
  147. /* this one defined in nand_legacy.c */
  148. int read_jffs2_nand(size_t start, size_t len,
  149. size_t * retlen, u_char * buf, int nanddev);
  150. #else
  151. /* info for NAND chips, defined in drivers/nand/nand.c */
  152. extern nand_info_t nand_info[];
  153. #endif
  154. #define NAND_PAGE_SIZE 512
  155. #define NAND_PAGE_SHIFT 9
  156. #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
  157. #ifndef NAND_CACHE_PAGES
  158. #define NAND_CACHE_PAGES 16
  159. #endif
  160. #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
  161. static u8* nand_cache = NULL;
  162. static u32 nand_cache_off = (u32)-1;
  163. static int read_nand_cached(u32 off, u32 size, u_char *buf)
  164. {
  165. struct mtdids *id = current_part->dev->id;
  166. u32 bytes_read = 0;
  167. #if defined(CFG_NAND_LEGACY)
  168. size_t retlen;
  169. #else
  170. ulong retlen;
  171. #endif
  172. int cpy_bytes;
  173. while (bytes_read < size) {
  174. if ((off + bytes_read < nand_cache_off) ||
  175. (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) {
  176. nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
  177. if (!nand_cache) {
  178. /* This memory never gets freed but 'cause
  179. it's a bootloader, nobody cares */
  180. nand_cache = malloc(NAND_CACHE_SIZE);
  181. if (!nand_cache) {
  182. printf("read_nand_cached: can't alloc cache size %d bytes\n",
  183. NAND_CACHE_SIZE);
  184. return -1;
  185. }
  186. }
  187. #if defined(CFG_NAND_LEGACY)
  188. if (read_jffs2_nand(nand_cache_off, NAND_CACHE_SIZE,
  189. &retlen, nand_cache, id->num) < 0 ||
  190. retlen != NAND_CACHE_SIZE) {
  191. printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
  192. nand_cache_off, NAND_CACHE_SIZE);
  193. return -1;
  194. }
  195. #else
  196. retlen = NAND_CACHE_SIZE;
  197. if (nand_read(&nand_info[id->num], nand_cache_off,
  198. &retlen, nand_cache) != 0 ||
  199. retlen != NAND_CACHE_SIZE) {
  200. printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
  201. nand_cache_off, NAND_CACHE_SIZE);
  202. return -1;
  203. }
  204. #endif
  205. }
  206. cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
  207. if (cpy_bytes > size - bytes_read)
  208. cpy_bytes = size - bytes_read;
  209. memcpy(buf + bytes_read,
  210. nand_cache + off + bytes_read - nand_cache_off,
  211. cpy_bytes);
  212. bytes_read += cpy_bytes;
  213. }
  214. return bytes_read;
  215. }
  216. static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
  217. {
  218. u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
  219. if (NULL == buf) {
  220. printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
  221. return NULL;
  222. }
  223. if (read_nand_cached(off, size, buf) < 0) {
  224. if (!ext_buf)
  225. free(buf);
  226. return NULL;
  227. }
  228. return buf;
  229. }
  230. static void *get_node_mem_nand(u32 off)
  231. {
  232. struct jffs2_unknown_node node;
  233. void *ret = NULL;
  234. if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
  235. return NULL;
  236. if (!(ret = get_fl_mem_nand(off, node.magic ==
  237. JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
  238. NULL))) {
  239. printf("off = %#x magic %#x type %#x node.totlen = %d\n",
  240. off, node.magic, node.nodetype, node.totlen);
  241. }
  242. return ret;
  243. }
  244. static void put_fl_mem_nand(void *buf)
  245. {
  246. free(buf);
  247. }
  248. #endif /* #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
  249. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  250. /*
  251. * Support for jffs2 on top of NOR-flash
  252. *
  253. * NOR flash memory is mapped in processor's address space,
  254. * just return address.
  255. */
  256. static inline void *get_fl_mem_nor(u32 off)
  257. {
  258. u32 addr = off;
  259. struct mtdids *id = current_part->dev->id;
  260. extern flash_info_t flash_info[];
  261. flash_info_t *flash = &flash_info[id->num];
  262. addr += flash->start[0];
  263. return (void*)addr;
  264. }
  265. static inline void *get_node_mem_nor(u32 off)
  266. {
  267. return (void*)get_fl_mem_nor(off);
  268. }
  269. #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FLASH) */
  270. /*
  271. * Generic jffs2 raw memory and node read routines.
  272. *
  273. */
  274. static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
  275. {
  276. struct mtdids *id = current_part->dev->id;
  277. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  278. if (id->type == MTD_DEV_TYPE_NOR)
  279. return get_fl_mem_nor(off);
  280. #endif
  281. #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
  282. if (id->type == MTD_DEV_TYPE_NAND)
  283. return get_fl_mem_nand(off, size, ext_buf);
  284. #endif
  285. printf("get_fl_mem: unknown device type, using raw offset!\n");
  286. return (void*)off;
  287. }
  288. static inline void *get_node_mem(u32 off)
  289. {
  290. struct mtdids *id = current_part->dev->id;
  291. #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
  292. if (id->type == MTD_DEV_TYPE_NOR)
  293. return get_node_mem_nor(off);
  294. #endif
  295. #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
  296. if (id->type == MTD_DEV_TYPE_NAND)
  297. return get_node_mem_nand(off);
  298. #endif
  299. printf("get_node_mem: unknown device type, using raw offset!\n");
  300. return (void*)off;
  301. }
  302. static inline void put_fl_mem(void *buf)
  303. {
  304. #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
  305. struct mtdids *id = current_part->dev->id;
  306. if (id->type == MTD_DEV_TYPE_NAND)
  307. return put_fl_mem_nand(buf);
  308. #endif
  309. }
  310. /* Compression names */
  311. static char *compr_names[] = {
  312. "NONE",
  313. "ZERO",
  314. "RTIME",
  315. "RUBINMIPS",
  316. "COPY",
  317. "DYNRUBIN",
  318. "ZLIB",
  319. #if defined(CONFIG_JFFS2_LZO_LZARI)
  320. "LZO",
  321. "LZARI",
  322. #endif
  323. };
  324. /* Spinning wheel */
  325. static char spinner[] = { '|', '/', '-', '\\' };
  326. /* Memory management */
  327. struct mem_block {
  328. u32 index;
  329. struct mem_block *next;
  330. struct b_node nodes[NODE_CHUNK];
  331. };
  332. static void
  333. free_nodes(struct b_list *list)
  334. {
  335. while (list->listMemBase != NULL) {
  336. struct mem_block *next = list->listMemBase->next;
  337. free( list->listMemBase );
  338. list->listMemBase = next;
  339. }
  340. }
  341. static struct b_node *
  342. add_node(struct b_list *list)
  343. {
  344. u32 index = 0;
  345. struct mem_block *memBase;
  346. struct b_node *b;
  347. memBase = list->listMemBase;
  348. if (memBase != NULL)
  349. index = memBase->index;
  350. #if 0
  351. putLabeledWord("add_node: index = ", index);
  352. putLabeledWord("add_node: memBase = ", list->listMemBase);
  353. #endif
  354. if (memBase == NULL || index >= NODE_CHUNK) {
  355. /* we need more space before we continue */
  356. memBase = mmalloc(sizeof(struct mem_block));
  357. if (memBase == NULL) {
  358. putstr("add_node: malloc failed\n");
  359. return NULL;
  360. }
  361. memBase->next = list->listMemBase;
  362. index = 0;
  363. #if 0
  364. putLabeledWord("add_node: alloced a new membase at ", *memBase);
  365. #endif
  366. }
  367. /* now we have room to add it. */
  368. b = &memBase->nodes[index];
  369. index ++;
  370. memBase->index = index;
  371. list->listMemBase = memBase;
  372. list->listCount++;
  373. return b;
  374. }
  375. static struct b_node *
  376. insert_node(struct b_list *list, u32 offset)
  377. {
  378. struct b_node *new;
  379. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  380. struct b_node *b, *prev;
  381. #endif
  382. if (!(new = add_node(list))) {
  383. putstr("add_node failed!\r\n");
  384. return NULL;
  385. }
  386. new->offset = offset;
  387. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  388. if (list->listTail != NULL && list->listCompare(new, list->listTail))
  389. prev = list->listTail;
  390. else if (list->listLast != NULL && list->listCompare(new, list->listLast))
  391. prev = list->listLast;
  392. else
  393. prev = NULL;
  394. for (b = (prev ? prev->next : list->listHead);
  395. b != NULL && list->listCompare(new, b);
  396. prev = b, b = b->next) {
  397. list->listLoops++;
  398. }
  399. if (b != NULL)
  400. list->listLast = prev;
  401. if (b != NULL) {
  402. new->next = b;
  403. if (prev != NULL)
  404. prev->next = new;
  405. else
  406. list->listHead = new;
  407. } else
  408. #endif
  409. {
  410. new->next = (struct b_node *) NULL;
  411. if (list->listTail != NULL) {
  412. list->listTail->next = new;
  413. list->listTail = new;
  414. } else {
  415. list->listTail = list->listHead = new;
  416. }
  417. }
  418. return new;
  419. }
  420. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  421. /* Sort data entries with the latest version last, so that if there
  422. * is overlapping data the latest version will be used.
  423. */
  424. static int compare_inodes(struct b_node *new, struct b_node *old)
  425. {
  426. struct jffs2_raw_inode ojNew;
  427. struct jffs2_raw_inode ojOld;
  428. struct jffs2_raw_inode *jNew =
  429. (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
  430. struct jffs2_raw_inode *jOld =
  431. (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
  432. return jNew->version > jOld->version;
  433. }
  434. /* Sort directory entries so all entries in the same directory
  435. * with the same name are grouped together, with the latest version
  436. * last. This makes it easy to eliminate all but the latest version
  437. * by marking the previous version dead by setting the inode to 0.
  438. */
  439. static int compare_dirents(struct b_node *new, struct b_node *old)
  440. {
  441. struct jffs2_raw_dirent ojNew;
  442. struct jffs2_raw_dirent ojOld;
  443. struct jffs2_raw_dirent *jNew =
  444. (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew);
  445. struct jffs2_raw_dirent *jOld =
  446. (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld);
  447. int cmp;
  448. /* ascending sort by pino */
  449. if (jNew->pino != jOld->pino)
  450. return jNew->pino > jOld->pino;
  451. /* pino is the same, so use ascending sort by nsize, so
  452. * we don't do strncmp unless we really must.
  453. */
  454. if (jNew->nsize != jOld->nsize)
  455. return jNew->nsize > jOld->nsize;
  456. /* length is also the same, so use ascending sort by name
  457. */
  458. cmp = strncmp((char *)jNew->name, (char *)jOld->name, jNew->nsize);
  459. if (cmp != 0)
  460. return cmp > 0;
  461. /* we have duplicate names in this directory, so use ascending
  462. * sort by version
  463. */
  464. if (jNew->version > jOld->version) {
  465. /* since jNew is newer, we know jOld is not valid, so
  466. * mark it with inode 0 and it will not be used
  467. */
  468. jOld->ino = 0;
  469. return 1;
  470. }
  471. return 0;
  472. }
  473. #endif
  474. static u32
  475. jffs2_scan_empty(u32 start_offset, struct part_info *part)
  476. {
  477. char *max = (char *)(part->offset + part->size - sizeof(struct jffs2_raw_inode));
  478. char *offset = (char *)(part->offset + start_offset);
  479. u32 off;
  480. while (offset < max &&
  481. *(u32*)get_fl_mem((u32)offset, sizeof(u32), &off) == 0xFFFFFFFF) {
  482. offset += sizeof(u32);
  483. /* return if spinning is due */
  484. if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
  485. }
  486. return (u32)offset - part->offset;
  487. }
  488. void
  489. jffs2_free_cache(struct part_info *part)
  490. {
  491. struct b_lists *pL;
  492. if (part->jffs2_priv != NULL) {
  493. pL = (struct b_lists *)part->jffs2_priv;
  494. free_nodes(&pL->frag);
  495. free_nodes(&pL->dir);
  496. free(pL);
  497. }
  498. }
  499. static u32
  500. jffs_init_1pass_list(struct part_info *part)
  501. {
  502. struct b_lists *pL;
  503. jffs2_free_cache(part);
  504. if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
  505. pL = (struct b_lists *)part->jffs2_priv;
  506. memset(pL, 0, sizeof(*pL));
  507. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  508. pL->dir.listCompare = compare_dirents;
  509. pL->frag.listCompare = compare_inodes;
  510. #endif
  511. }
  512. return 0;
  513. }
  514. /* find the inode from the slashless name given a parent */
  515. static long
  516. jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
  517. {
  518. struct b_node *b;
  519. struct jffs2_raw_inode *jNode;
  520. u32 totalSize = 0;
  521. u32 latestVersion = 0;
  522. uchar *lDest;
  523. uchar *src;
  524. long ret;
  525. int i;
  526. u32 counter = 0;
  527. #ifdef CFG_JFFS2_SORT_FRAGMENTS
  528. /* Find file size before loading any data, so fragments that
  529. * start past the end of file can be ignored. A fragment
  530. * that is partially in the file is loaded, so extra data may
  531. * be loaded up to the next 4K boundary above the file size.
  532. * This shouldn't cause trouble when loading kernel images, so
  533. * we will live with it.
  534. */
  535. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  536. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  537. sizeof(struct jffs2_raw_inode), NULL);
  538. if ((inode == jNode->ino)) {
  539. /* get actual file length from the newest node */
  540. if (jNode->version >= latestVersion) {
  541. totalSize = jNode->isize;
  542. latestVersion = jNode->version;
  543. }
  544. }
  545. put_fl_mem(jNode);
  546. }
  547. #endif
  548. for (b = pL->frag.listHead; b != NULL; b = b->next) {
  549. jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset);
  550. if ((inode == jNode->ino)) {
  551. #if 0
  552. putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
  553. putLabeledWord("read_inode: inode = ", jNode->ino);
  554. putLabeledWord("read_inode: version = ", jNode->version);
  555. putLabeledWord("read_inode: isize = ", jNode->isize);
  556. putLabeledWord("read_inode: offset = ", jNode->offset);
  557. putLabeledWord("read_inode: csize = ", jNode->csize);
  558. putLabeledWord("read_inode: dsize = ", jNode->dsize);
  559. putLabeledWord("read_inode: compr = ", jNode->compr);
  560. putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
  561. putLabeledWord("read_inode: flags = ", jNode->flags);
  562. #endif
  563. #ifndef CFG_JFFS2_SORT_FRAGMENTS
  564. /* get actual file length from the newest node */
  565. if (jNode->version >= latestVersion) {
  566. totalSize = jNode->isize;
  567. latestVersion = jNode->version;
  568. }
  569. #endif
  570. if(dest) {
  571. src = ((uchar *) jNode) + sizeof(struct jffs2_raw_inode);
  572. /* ignore data behind latest known EOF */
  573. if (jNode->offset > totalSize) {
  574. put_fl_mem(jNode);
  575. continue;
  576. }
  577. lDest = (uchar *) (dest + jNode->offset);
  578. #if 0
  579. putLabeledWord("read_inode: src = ", src);
  580. putLabeledWord("read_inode: dest = ", lDest);
  581. #endif
  582. switch (jNode->compr) {
  583. case JFFS2_COMPR_NONE:
  584. ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize);
  585. break;
  586. case JFFS2_COMPR_ZERO:
  587. ret = 0;
  588. for (i = 0; i < jNode->dsize; i++)
  589. *(lDest++) = 0;
  590. break;
  591. case JFFS2_COMPR_RTIME:
  592. ret = 0;
  593. rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
  594. break;
  595. case JFFS2_COMPR_DYNRUBIN:
  596. /* this is slow but it works */
  597. ret = 0;
  598. dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
  599. break;
  600. case JFFS2_COMPR_ZLIB:
  601. ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
  602. break;
  603. #if defined(CONFIG_JFFS2_LZO_LZARI)
  604. case JFFS2_COMPR_LZO:
  605. ret = lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
  606. break;
  607. case JFFS2_COMPR_LZARI:
  608. ret = lzari_decompress(src, lDest, jNode->csize, jNode->dsize);
  609. break;
  610. #endif
  611. default:
  612. /* unknown */
  613. putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
  614. put_fl_mem(jNode);
  615. return -1;
  616. break;
  617. }
  618. }
  619. #if 0
  620. putLabeledWord("read_inode: totalSize = ", totalSize);
  621. putLabeledWord("read_inode: compr ret = ", ret);
  622. #endif
  623. }
  624. counter++;
  625. put_fl_mem(jNode);
  626. }
  627. #if 0
  628. putLabeledWord("read_inode: returning = ", totalSize);
  629. #endif
  630. return totalSize;
  631. }
  632. /* find the inode from the slashless name given a parent */
  633. static u32
  634. jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
  635. {
  636. struct b_node *b;
  637. struct jffs2_raw_dirent *jDir;
  638. int len;
  639. u32 counter;
  640. u32 version = 0;
  641. u32 inode = 0;
  642. /* name is assumed slash free */
  643. len = strlen(name);
  644. counter = 0;
  645. /* we need to search all and return the inode with the highest version */
  646. for(b = pL->dir.listHead; b; b = b->next, counter++) {
  647. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  648. if ((pino == jDir->pino) && (len == jDir->nsize) &&
  649. (jDir->ino) && /* 0 for unlink */
  650. (!strncmp((char *)jDir->name, name, len))) { /* a match */
  651. if (jDir->version < version) {
  652. put_fl_mem(jDir);
  653. continue;
  654. }
  655. if (jDir->version == version && inode != 0) {
  656. /* I'm pretty sure this isn't legal */
  657. putstr(" ** ERROR ** ");
  658. putnstr(jDir->name, jDir->nsize);
  659. putLabeledWord(" has dup version =", version);
  660. }
  661. inode = jDir->ino;
  662. version = jDir->version;
  663. }
  664. #if 0
  665. putstr("\r\nfind_inode:p&l ->");
  666. putnstr(jDir->name, jDir->nsize);
  667. putstr("\r\n");
  668. putLabeledWord("pino = ", jDir->pino);
  669. putLabeledWord("nsize = ", jDir->nsize);
  670. putLabeledWord("b = ", (u32) b);
  671. putLabeledWord("counter = ", counter);
  672. #endif
  673. put_fl_mem(jDir);
  674. }
  675. return inode;
  676. }
  677. char *mkmodestr(unsigned long mode, char *str)
  678. {
  679. static const char *l = "xwr";
  680. int mask = 1, i;
  681. char c;
  682. switch (mode & S_IFMT) {
  683. case S_IFDIR: str[0] = 'd'; break;
  684. case S_IFBLK: str[0] = 'b'; break;
  685. case S_IFCHR: str[0] = 'c'; break;
  686. case S_IFIFO: str[0] = 'f'; break;
  687. case S_IFLNK: str[0] = 'l'; break;
  688. case S_IFSOCK: str[0] = 's'; break;
  689. case S_IFREG: str[0] = '-'; break;
  690. default: str[0] = '?';
  691. }
  692. for(i = 0; i < 9; i++) {
  693. c = l[i%3];
  694. str[9-i] = (mode & mask)?c:'-';
  695. mask = mask<<1;
  696. }
  697. if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
  698. if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
  699. if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
  700. str[10] = '\0';
  701. return str;
  702. }
  703. static inline void dump_stat(struct stat *st, const char *name)
  704. {
  705. char str[20];
  706. char s[64], *p;
  707. if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
  708. st->st_mtime = 1;
  709. ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
  710. if ((p = strchr(s,'\n')) != NULL) *p = '\0';
  711. if ((p = strchr(s,'\r')) != NULL) *p = '\0';
  712. /*
  713. printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
  714. st->st_size, s, name);
  715. */
  716. printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
  717. }
  718. static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
  719. {
  720. char fname[256];
  721. struct stat st;
  722. if(!d || !i) return -1;
  723. strncpy(fname, (char *)d->name, d->nsize);
  724. fname[d->nsize] = '\0';
  725. memset(&st,0,sizeof(st));
  726. st.st_mtime = i->mtime;
  727. st.st_mode = i->mode;
  728. st.st_ino = i->ino;
  729. /* neither dsize nor isize help us.. do it the long way */
  730. st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL);
  731. dump_stat(&st, fname);
  732. if (d->type == DT_LNK) {
  733. unsigned char *src = (unsigned char *) (&i[1]);
  734. putstr(" -> ");
  735. putnstr(src, (int)i->dsize);
  736. }
  737. putstr("\r\n");
  738. return 0;
  739. }
  740. /* list inodes with the given pino */
  741. static u32
  742. jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
  743. {
  744. struct b_node *b;
  745. struct jffs2_raw_dirent *jDir;
  746. for (b = pL->dir.listHead; b; b = b->next) {
  747. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  748. if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */
  749. u32 i_version = 0;
  750. struct jffs2_raw_inode ojNode;
  751. struct jffs2_raw_inode *jNode, *i = NULL;
  752. struct b_node *b2 = pL->frag.listHead;
  753. while (b2) {
  754. jNode = (struct jffs2_raw_inode *)
  755. get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
  756. if (jNode->ino == jDir->ino && jNode->version >= i_version) {
  757. if (i)
  758. put_fl_mem(i);
  759. if (jDir->type == DT_LNK)
  760. i = get_node_mem(b2->offset);
  761. else
  762. i = get_fl_mem(b2->offset, sizeof(*i), NULL);
  763. }
  764. b2 = b2->next;
  765. }
  766. dump_inode(pL, jDir, i);
  767. put_fl_mem(i);
  768. }
  769. put_fl_mem(jDir);
  770. }
  771. return pino;
  772. }
  773. static u32
  774. jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
  775. {
  776. int i;
  777. char tmp[256];
  778. char working_tmp[256];
  779. char *c;
  780. /* discard any leading slash */
  781. i = 0;
  782. while (fname[i] == '/')
  783. i++;
  784. strcpy(tmp, &fname[i]);
  785. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  786. {
  787. strncpy(working_tmp, tmp, c - tmp);
  788. working_tmp[c - tmp] = '\0';
  789. #if 0
  790. putstr("search_inode: tmp = ");
  791. putstr(tmp);
  792. putstr("\r\n");
  793. putstr("search_inode: wtmp = ");
  794. putstr(working_tmp);
  795. putstr("\r\n");
  796. putstr("search_inode: c = ");
  797. putstr(c);
  798. putstr("\r\n");
  799. #endif
  800. for (i = 0; i < strlen(c) - 1; i++)
  801. tmp[i] = c[i + 1];
  802. tmp[i] = '\0';
  803. #if 0
  804. putstr("search_inode: post tmp = ");
  805. putstr(tmp);
  806. putstr("\r\n");
  807. #endif
  808. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
  809. putstr("find_inode failed for name=");
  810. putstr(working_tmp);
  811. putstr("\r\n");
  812. return 0;
  813. }
  814. }
  815. /* this is for the bare filename, directories have already been mapped */
  816. if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  817. putstr("find_inode failed for name=");
  818. putstr(tmp);
  819. putstr("\r\n");
  820. return 0;
  821. }
  822. return pino;
  823. }
  824. static u32
  825. jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
  826. {
  827. struct b_node *b;
  828. struct b_node *b2;
  829. struct jffs2_raw_dirent *jDir;
  830. struct jffs2_raw_inode *jNode;
  831. u8 jDirFoundType = 0;
  832. u32 jDirFoundIno = 0;
  833. u32 jDirFoundPino = 0;
  834. char tmp[256];
  835. u32 version = 0;
  836. u32 pino;
  837. unsigned char *src;
  838. /* we need to search all and return the inode with the highest version */
  839. for(b = pL->dir.listHead; b; b = b->next) {
  840. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  841. if (ino == jDir->ino) {
  842. if (jDir->version < version) {
  843. put_fl_mem(jDir);
  844. continue;
  845. }
  846. if (jDir->version == version && jDirFoundType) {
  847. /* I'm pretty sure this isn't legal */
  848. putstr(" ** ERROR ** ");
  849. putnstr(jDir->name, jDir->nsize);
  850. putLabeledWord(" has dup version (resolve) = ",
  851. version);
  852. }
  853. jDirFoundType = jDir->type;
  854. jDirFoundIno = jDir->ino;
  855. jDirFoundPino = jDir->pino;
  856. version = jDir->version;
  857. }
  858. put_fl_mem(jDir);
  859. }
  860. /* now we found the right entry again. (shoulda returned inode*) */
  861. if (jDirFoundType != DT_LNK)
  862. return jDirFoundIno;
  863. /* it's a soft link so we follow it again. */
  864. b2 = pL->frag.listHead;
  865. while (b2) {
  866. jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset);
  867. if (jNode->ino == jDirFoundIno) {
  868. src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
  869. #if 0
  870. putLabeledWord("\t\t dsize = ", jNode->dsize);
  871. putstr("\t\t target = ");
  872. putnstr(src, jNode->dsize);
  873. putstr("\r\n");
  874. #endif
  875. strncpy(tmp, (char *)src, jNode->dsize);
  876. tmp[jNode->dsize] = '\0';
  877. put_fl_mem(jNode);
  878. break;
  879. }
  880. b2 = b2->next;
  881. put_fl_mem(jNode);
  882. }
  883. /* ok so the name of the new file to find is in tmp */
  884. /* if it starts with a slash it is root based else shared dirs */
  885. if (tmp[0] == '/')
  886. pino = 1;
  887. else
  888. pino = jDirFoundPino;
  889. return jffs2_1pass_search_inode(pL, tmp, pino);
  890. }
  891. static u32
  892. jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
  893. {
  894. int i;
  895. char tmp[256];
  896. char working_tmp[256];
  897. char *c;
  898. /* discard any leading slash */
  899. i = 0;
  900. while (fname[i] == '/')
  901. i++;
  902. strcpy(tmp, &fname[i]);
  903. working_tmp[0] = '\0';
  904. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  905. {
  906. strncpy(working_tmp, tmp, c - tmp);
  907. working_tmp[c - tmp] = '\0';
  908. for (i = 0; i < strlen(c) - 1; i++)
  909. tmp[i] = c[i + 1];
  910. tmp[i] = '\0';
  911. /* only a failure if we arent looking at top level */
  912. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
  913. (working_tmp[0])) {
  914. putstr("find_inode failed for name=");
  915. putstr(working_tmp);
  916. putstr("\r\n");
  917. return 0;
  918. }
  919. }
  920. if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  921. putstr("find_inode failed for name=");
  922. putstr(tmp);
  923. putstr("\r\n");
  924. return 0;
  925. }
  926. /* this is for the bare filename, directories have already been mapped */
  927. if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
  928. putstr("find_inode failed for name=");
  929. putstr(tmp);
  930. putstr("\r\n");
  931. return 0;
  932. }
  933. return pino;
  934. }
  935. unsigned char
  936. jffs2_1pass_rescan_needed(struct part_info *part)
  937. {
  938. struct b_node *b;
  939. struct jffs2_unknown_node onode;
  940. struct jffs2_unknown_node *node;
  941. struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
  942. if (part->jffs2_priv == 0){
  943. DEBUGF ("rescan: First time in use\n");
  944. return 1;
  945. }
  946. /* if we have no list, we need to rescan */
  947. if (pL->frag.listCount == 0) {
  948. DEBUGF ("rescan: fraglist zero\n");
  949. return 1;
  950. }
  951. /* but suppose someone reflashed a partition at the same offset... */
  952. b = pL->dir.listHead;
  953. while (b) {
  954. node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
  955. sizeof(onode), &onode);
  956. if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
  957. DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
  958. (unsigned long) b->offset);
  959. return 1;
  960. }
  961. b = b->next;
  962. }
  963. return 0;
  964. }
  965. #ifdef DEBUG_FRAGMENTS
  966. static void
  967. dump_fragments(struct b_lists *pL)
  968. {
  969. struct b_node *b;
  970. struct jffs2_raw_inode ojNode;
  971. struct jffs2_raw_inode *jNode;
  972. putstr("\r\n\r\n******The fragment Entries******\r\n");
  973. b = pL->frag.listHead;
  974. while (b) {
  975. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  976. sizeof(ojNode), &ojNode);
  977. putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
  978. putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
  979. putLabeledWord("\tbuild_list: inode = ", jNode->ino);
  980. putLabeledWord("\tbuild_list: version = ", jNode->version);
  981. putLabeledWord("\tbuild_list: isize = ", jNode->isize);
  982. putLabeledWord("\tbuild_list: atime = ", jNode->atime);
  983. putLabeledWord("\tbuild_list: offset = ", jNode->offset);
  984. putLabeledWord("\tbuild_list: csize = ", jNode->csize);
  985. putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
  986. putLabeledWord("\tbuild_list: compr = ", jNode->compr);
  987. putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
  988. putLabeledWord("\tbuild_list: flags = ", jNode->flags);
  989. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  990. b = b->next;
  991. }
  992. }
  993. #endif
  994. #ifdef DEBUG_DIRENTS
  995. static void
  996. dump_dirents(struct b_lists *pL)
  997. {
  998. struct b_node *b;
  999. struct jffs2_raw_dirent *jDir;
  1000. putstr("\r\n\r\n******The directory Entries******\r\n");
  1001. b = pL->dir.listHead;
  1002. while (b) {
  1003. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  1004. putstr("\r\n");
  1005. putnstr(jDir->name, jDir->nsize);
  1006. putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
  1007. putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
  1008. putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
  1009. putLabeledWord("\tbuild_list: pino = ", jDir->pino);
  1010. putLabeledWord("\tbuild_list: version = ", jDir->version);
  1011. putLabeledWord("\tbuild_list: ino = ", jDir->ino);
  1012. putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
  1013. putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
  1014. putLabeledWord("\tbuild_list: type = ", jDir->type);
  1015. putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
  1016. putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
  1017. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  1018. b = b->next;
  1019. put_fl_mem(jDir);
  1020. }
  1021. }
  1022. #endif
  1023. static u32
  1024. jffs2_1pass_build_lists(struct part_info * part)
  1025. {
  1026. struct b_lists *pL;
  1027. struct jffs2_unknown_node *node;
  1028. u32 offset, oldoffset = 0;
  1029. u32 max = part->size - sizeof(struct jffs2_raw_inode);
  1030. u32 counter = 0;
  1031. u32 counter4 = 0;
  1032. u32 counterF = 0;
  1033. u32 counterN = 0;
  1034. /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
  1035. /* jffs2 list building enterprise nope. in newer versions the overhead is */
  1036. /* only about 5 %. not enough to inconvenience people for. */
  1037. /* lcd_off(); */
  1038. /* if we are building a list we need to refresh the cache. */
  1039. jffs_init_1pass_list(part);
  1040. pL = (struct b_lists *)part->jffs2_priv;
  1041. offset = 0;
  1042. puts ("Scanning JFFS2 FS: ");
  1043. /* start at the beginning of the partition */
  1044. while (offset < max) {
  1045. if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
  1046. printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
  1047. oldoffset = offset;
  1048. }
  1049. node = (struct jffs2_unknown_node *) get_node_mem((u32)part->offset + offset);
  1050. if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
  1051. /* if its a fragment add it */
  1052. if (node->nodetype == JFFS2_NODETYPE_INODE &&
  1053. inode_crc((struct jffs2_raw_inode *) node) &&
  1054. data_crc((struct jffs2_raw_inode *) node)) {
  1055. if (insert_node(&pL->frag, (u32) part->offset +
  1056. offset) == NULL) {
  1057. put_fl_mem(node);
  1058. return 0;
  1059. }
  1060. } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
  1061. dirent_crc((struct jffs2_raw_dirent *) node) &&
  1062. dirent_name_crc((struct jffs2_raw_dirent *) node)) {
  1063. if (! (counterN%100))
  1064. puts ("\b\b. ");
  1065. if (insert_node(&pL->dir, (u32) part->offset +
  1066. offset) == NULL) {
  1067. put_fl_mem(node);
  1068. return 0;
  1069. }
  1070. counterN++;
  1071. } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
  1072. if (node->totlen != sizeof(struct jffs2_unknown_node))
  1073. printf("OOPS Cleanmarker has bad size "
  1074. "%d != %d\n", node->totlen,
  1075. sizeof(struct jffs2_unknown_node));
  1076. } else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
  1077. if (node->totlen < sizeof(struct jffs2_unknown_node))
  1078. printf("OOPS Padding has bad size "
  1079. "%d < %d\n", node->totlen,
  1080. sizeof(struct jffs2_unknown_node));
  1081. } else {
  1082. printf("Unknown node type: %x len %d "
  1083. "offset 0x%x\n", node->nodetype,
  1084. node->totlen, offset);
  1085. }
  1086. offset += ((node->totlen + 3) & ~3);
  1087. counterF++;
  1088. } else if (node->magic == JFFS2_EMPTY_BITMASK &&
  1089. node->nodetype == JFFS2_EMPTY_BITMASK) {
  1090. offset = jffs2_scan_empty(offset, part);
  1091. } else { /* if we know nothing, we just step and look. */
  1092. offset += 4;
  1093. counter4++;
  1094. }
  1095. /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */
  1096. put_fl_mem(node);
  1097. }
  1098. putstr("\b\b done.\r\n"); /* close off the dots */
  1099. /* turn the lcd back on. */
  1100. /* splash(); */
  1101. #if 0
  1102. putLabeledWord("dir entries = ", pL->dir.listCount);
  1103. putLabeledWord("frag entries = ", pL->frag.listCount);
  1104. putLabeledWord("+4 increments = ", counter4);
  1105. putLabeledWord("+file_offset increments = ", counterF);
  1106. #endif
  1107. #ifdef DEBUG_DIRENTS
  1108. dump_dirents(pL);
  1109. #endif
  1110. #ifdef DEBUG_FRAGMENTS
  1111. dump_fragments(pL);
  1112. #endif
  1113. /* give visual feedback that we are done scanning the flash */
  1114. led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
  1115. return 1;
  1116. }
  1117. static u32
  1118. jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
  1119. {
  1120. struct b_node *b;
  1121. struct jffs2_raw_inode ojNode;
  1122. struct jffs2_raw_inode *jNode;
  1123. int i;
  1124. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1125. piL->compr_info[i].num_frags = 0;
  1126. piL->compr_info[i].compr_sum = 0;
  1127. piL->compr_info[i].decompr_sum = 0;
  1128. }
  1129. b = pL->frag.listHead;
  1130. while (b) {
  1131. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  1132. sizeof(ojNode), &ojNode);
  1133. if (jNode->compr < JFFS2_NUM_COMPR) {
  1134. piL->compr_info[jNode->compr].num_frags++;
  1135. piL->compr_info[jNode->compr].compr_sum += jNode->csize;
  1136. piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
  1137. }
  1138. b = b->next;
  1139. }
  1140. return 0;
  1141. }
  1142. static struct b_lists *
  1143. jffs2_get_list(struct part_info * part, const char *who)
  1144. {
  1145. /* copy requested part_info struct pointer to global location */
  1146. current_part = part;
  1147. if (jffs2_1pass_rescan_needed(part)) {
  1148. if (!jffs2_1pass_build_lists(part)) {
  1149. printf("%s: Failed to scan JFFSv2 file structure\n", who);
  1150. return NULL;
  1151. }
  1152. }
  1153. return (struct b_lists *)part->jffs2_priv;
  1154. }
  1155. /* Print directory / file contents */
  1156. u32
  1157. jffs2_1pass_ls(struct part_info * part, const char *fname)
  1158. {
  1159. struct b_lists *pl;
  1160. long ret = 1;
  1161. u32 inode;
  1162. if (! (pl = jffs2_get_list(part, "ls")))
  1163. return 0;
  1164. if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
  1165. putstr("ls: Failed to scan jffs2 file structure\r\n");
  1166. return 0;
  1167. }
  1168. #if 0
  1169. putLabeledWord("found file at inode = ", inode);
  1170. putLabeledWord("read_inode returns = ", ret);
  1171. #endif
  1172. return ret;
  1173. }
  1174. /* Load a file from flash into memory. fname can be a full path */
  1175. u32
  1176. jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
  1177. {
  1178. struct b_lists *pl;
  1179. long ret = 1;
  1180. u32 inode;
  1181. if (! (pl = jffs2_get_list(part, "load")))
  1182. return 0;
  1183. if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
  1184. putstr("load: Failed to find inode\r\n");
  1185. return 0;
  1186. }
  1187. /* Resolve symlinks */
  1188. if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
  1189. putstr("load: Failed to resolve inode structure\r\n");
  1190. return 0;
  1191. }
  1192. if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
  1193. putstr("load: Failed to read inode\r\n");
  1194. return 0;
  1195. }
  1196. DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
  1197. (unsigned long) dest, ret);
  1198. return ret;
  1199. }
  1200. /* Return information about the fs on this partition */
  1201. u32
  1202. jffs2_1pass_info(struct part_info * part)
  1203. {
  1204. struct b_jffs2_info info;
  1205. struct b_lists *pl;
  1206. int i;
  1207. if (! (pl = jffs2_get_list(part, "info")))
  1208. return 0;
  1209. jffs2_1pass_fill_info(pl, &info);
  1210. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  1211. printf ("Compression: %s\n"
  1212. "\tfrag count: %d\n"
  1213. "\tcompressed sum: %d\n"
  1214. "\tuncompressed sum: %d\n",
  1215. compr_names[i],
  1216. info.compr_info[i].num_frags,
  1217. info.compr_info[i].compr_sum,
  1218. info.compr_info[i].decompr_sum);
  1219. }
  1220. return 1;
  1221. }
  1222. #endif /* CFG_CMD_JFFS2 */