jffs2_1pass.c 30 KB

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