jffs2_1pass.c 35 KB

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