jffs2_nand_1pass.c 26 KB

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