jffs2_1pass.c 37 KB

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