jffs2_1pass.c 35 KB

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