jffs2_nand_1pass.c 25 KB

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