jffs2_1pass.c 36 KB

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