jffs2_nand_1pass.c 26 KB

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