jffs2_nand_1pass.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. #include <common.h>
  2. #if defined(CONFIG_NEW_NAND_CODE) && (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 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 != %d\n", node->totlen,
  763. sizeof(struct jffs2_unknown_node));
  764. } else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
  765. if (node->totlen < sizeof(struct jffs2_unknown_node))
  766. printf("OOPS Padding has bad size "
  767. "%d < %d\n", node->totlen,
  768. sizeof(struct jffs2_unknown_node));
  769. } else {
  770. printf("Unknown node type: %x len %d "
  771. "offset 0x%x\n", node->nodetype,
  772. node->totlen, offset);
  773. }
  774. offset += ((node->totlen + 3) & ~3);
  775. ofs += ((node->totlen + 3) & ~3);
  776. counterF++;
  777. }
  778. }
  779. putstr("\b\b done.\r\n"); /* close off the dots */
  780. #if 0
  781. putLabeledWord("dir entries = ", pL->dir.listCount);
  782. putLabeledWord("frag entries = ", pL->frag.listCount);
  783. putLabeledWord("+4 increments = ", counter4);
  784. putLabeledWord("+file_offset increments = ", counterF);
  785. #endif
  786. #ifdef DEBUG_DIRENTS
  787. dump_dirents(pL);
  788. #endif
  789. #ifdef DEBUG_FRAGMENTS
  790. dump_fragments(pL);
  791. #endif
  792. /* give visual feedback that we are done scanning the flash */
  793. led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
  794. free(buf);
  795. return 1;
  796. }
  797. static u32
  798. jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
  799. {
  800. struct b_node *b;
  801. struct jffs2_raw_inode ojNode;
  802. struct jffs2_raw_inode *jNode;
  803. int i;
  804. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  805. piL->compr_info[i].num_frags = 0;
  806. piL->compr_info[i].compr_sum = 0;
  807. piL->compr_info[i].decompr_sum = 0;
  808. }
  809. /* FIXME
  810. b = pL->frag.listHead;
  811. while (b) {
  812. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  813. sizeof(ojNode), &ojNode);
  814. if (jNode->compr < JFFS2_NUM_COMPR) {
  815. piL->compr_info[jNode->compr].num_frags++;
  816. piL->compr_info[jNode->compr].compr_sum += jNode->csize;
  817. piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
  818. }
  819. b = b->next;
  820. }
  821. */
  822. return 0;
  823. }
  824. static struct b_lists *
  825. jffs2_get_list(struct part_info * part, const char *who)
  826. {
  827. if (jffs2_1pass_rescan_needed(part)) {
  828. if (!jffs2_1pass_build_lists(part)) {
  829. printf("%s: Failed to scan JFFSv2 file structure\n", who);
  830. return NULL;
  831. }
  832. }
  833. return (struct b_lists *)part->jffs2_priv;
  834. }
  835. /* Print directory / file contents */
  836. u32
  837. jffs2_1pass_ls(struct part_info * part, const char *fname)
  838. {
  839. struct b_lists *pl;
  840. long ret = 0;
  841. u32 inode;
  842. if (! (pl = jffs2_get_list(part, "ls")))
  843. return 0;
  844. if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
  845. putstr("ls: Failed to scan jffs2 file structure\r\n");
  846. return 0;
  847. }
  848. #if 0
  849. putLabeledWord("found file at inode = ", inode);
  850. putLabeledWord("read_inode returns = ", ret);
  851. #endif
  852. return ret;
  853. }
  854. /* Load a file from flash into memory. fname can be a full path */
  855. u32
  856. jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
  857. {
  858. struct b_lists *pl;
  859. long ret = 0;
  860. u32 inode;
  861. if (! (pl = jffs2_get_list(part, "load")))
  862. return 0;
  863. if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
  864. putstr("load: Failed to find inode\r\n");
  865. return 0;
  866. }
  867. /* Resolve symlinks */
  868. if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
  869. putstr("load: Failed to resolve inode structure\r\n");
  870. return 0;
  871. }
  872. if ((ret = jffs2_1pass_read_inode(pl, inode, dest, NULL)) < 0) {
  873. putstr("load: Failed to read inode\r\n");
  874. return 0;
  875. }
  876. DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
  877. (unsigned long) dest, ret);
  878. return ret;
  879. }
  880. /* Return information about the fs on this partition */
  881. u32
  882. jffs2_1pass_info(struct part_info * part)
  883. {
  884. struct b_jffs2_info info;
  885. struct b_lists *pl;
  886. int i;
  887. if (! (pl = jffs2_get_list(part, "info")))
  888. return 0;
  889. jffs2_1pass_fill_info(pl, &info);
  890. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  891. printf ("Compression: %s\n"
  892. "\tfrag count: %d\n"
  893. "\tcompressed sum: %d\n"
  894. "\tuncompressed sum: %d\n",
  895. compr_names[i],
  896. info.compr_info[i].num_frags,
  897. info.compr_info[i].compr_sum,
  898. info.compr_info[i].decompr_sum);
  899. }
  900. return 1;
  901. }
  902. #endif /* CFG_CMD_JFFS2 */