jffs2_nand_1pass.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 nand_info_t *nand;
  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(nand, 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("UNKOWN 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(nand, 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(nand, jNode->offset + sizeof(struct jffs2_raw_inode), &len, &tmp);
  518. tmp[jNode->csize] = '\0';
  519. break;
  520. }
  521. }
  522. /* ok so the name of the new file to find is in tmp */
  523. /* if it starts with a slash it is root based else shared dirs */
  524. if (tmp[0] == '/')
  525. pino = 1;
  526. else
  527. pino = jDirFoundPino;
  528. return jffs2_1pass_search_inode(pL, tmp, pino);
  529. }
  530. static u32
  531. jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
  532. {
  533. int i;
  534. char tmp[256];
  535. char working_tmp[256];
  536. char *c;
  537. /* discard any leading slash */
  538. i = 0;
  539. while (fname[i] == '/')
  540. i++;
  541. strcpy(tmp, &fname[i]);
  542. working_tmp[0] = '\0';
  543. while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
  544. {
  545. strncpy(working_tmp, tmp, c - tmp);
  546. working_tmp[c - tmp] = '\0';
  547. for (i = 0; i < strlen(c) - 1; i++)
  548. tmp[i] = c[i + 1];
  549. tmp[i] = '\0';
  550. /* only a failure if we arent looking at top level */
  551. if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
  552. (working_tmp[0])) {
  553. putstr("find_inode failed for name=");
  554. putstr(working_tmp);
  555. putstr("\r\n");
  556. return 0;
  557. }
  558. }
  559. if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
  560. putstr("find_inode failed for name=");
  561. putstr(tmp);
  562. putstr("\r\n");
  563. return 0;
  564. }
  565. /* this is for the bare filename, directories have already been mapped */
  566. if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
  567. putstr("find_inode failed for name=");
  568. putstr(tmp);
  569. putstr("\r\n");
  570. return 0;
  571. }
  572. return pino;
  573. }
  574. unsigned char
  575. jffs2_1pass_rescan_needed(struct part_info *part)
  576. {
  577. struct b_node *b;
  578. struct jffs2_unknown_node onode;
  579. struct jffs2_unknown_node *node;
  580. struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
  581. if (part->jffs2_priv == 0){
  582. DEBUGF ("rescan: First time in use\n");
  583. return 1;
  584. }
  585. /* if we have no list, we need to rescan */
  586. if (pL->frag.listCount == 0) {
  587. DEBUGF ("rescan: fraglist zero\n");
  588. return 1;
  589. }
  590. /* or if we are scanning a new partition */
  591. if (pL->partOffset != part->offset) {
  592. DEBUGF ("rescan: different partition\n");
  593. return 1;
  594. }
  595. /* FIXME */
  596. #if 0
  597. /* but suppose someone reflashed a partition at the same offset... */
  598. b = pL->dir.listHead;
  599. while (b) {
  600. node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
  601. sizeof(onode), &onode);
  602. if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
  603. DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
  604. (unsigned long) b->offset);
  605. return 1;
  606. }
  607. b = b->next;
  608. }
  609. #endif
  610. return 0;
  611. }
  612. #ifdef DEBUG_FRAGMENTS
  613. static void
  614. dump_fragments(struct b_lists *pL)
  615. {
  616. struct b_node *b;
  617. struct jffs2_raw_inode ojNode;
  618. struct jffs2_raw_inode *jNode;
  619. putstr("\r\n\r\n******The fragment Entries******\r\n");
  620. b = pL->frag.listHead;
  621. while (b) {
  622. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  623. sizeof(ojNode), &ojNode);
  624. putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
  625. putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
  626. putLabeledWord("\tbuild_list: inode = ", jNode->ino);
  627. putLabeledWord("\tbuild_list: version = ", jNode->version);
  628. putLabeledWord("\tbuild_list: isize = ", jNode->isize);
  629. putLabeledWord("\tbuild_list: atime = ", jNode->atime);
  630. putLabeledWord("\tbuild_list: offset = ", jNode->offset);
  631. putLabeledWord("\tbuild_list: csize = ", jNode->csize);
  632. putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
  633. putLabeledWord("\tbuild_list: compr = ", jNode->compr);
  634. putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
  635. putLabeledWord("\tbuild_list: flags = ", jNode->flags);
  636. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  637. b = b->next;
  638. }
  639. }
  640. #endif
  641. #ifdef DEBUG_DIRENTS
  642. static void
  643. dump_dirents(struct b_lists *pL)
  644. {
  645. struct b_node *b;
  646. struct jffs2_raw_dirent *jDir;
  647. putstr("\r\n\r\n******The directory Entries******\r\n");
  648. b = pL->dir.listHead;
  649. while (b) {
  650. jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
  651. putstr("\r\n");
  652. putnstr(jDir->name, jDir->nsize);
  653. putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
  654. putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
  655. putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
  656. putLabeledWord("\tbuild_list: pino = ", jDir->pino);
  657. putLabeledWord("\tbuild_list: version = ", jDir->version);
  658. putLabeledWord("\tbuild_list: ino = ", jDir->ino);
  659. putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
  660. putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
  661. putLabeledWord("\tbuild_list: type = ", jDir->type);
  662. putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
  663. putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
  664. putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
  665. b = b->next;
  666. put_fl_mem(jDir);
  667. }
  668. }
  669. #endif
  670. static int
  671. jffs2_fill_scan_buf(nand_info_t *nand, unsigned char *buf,
  672. unsigned ofs, unsigned len)
  673. {
  674. int ret;
  675. unsigned olen;
  676. olen = len;
  677. ret = nand_read(nand, ofs, &olen, buf);
  678. if (ret) {
  679. printf("nand_read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret);
  680. return ret;
  681. }
  682. if (olen < len) {
  683. printf("Read at 0x%x gave only 0x%x bytes\n", ofs, olen);
  684. return -1;
  685. }
  686. return 0;
  687. }
  688. #define EMPTY_SCAN_SIZE 1024
  689. static u32
  690. jffs2_1pass_build_lists(struct part_info * part)
  691. {
  692. struct b_lists *pL;
  693. struct jffs2_unknown_node *node;
  694. unsigned nr_blocks, sectorsize, ofs, offset;
  695. char *buf;
  696. int i;
  697. u32 counter = 0;
  698. u32 counter4 = 0;
  699. u32 counterF = 0;
  700. u32 counterN = 0;
  701. struct mtdids *id = part->dev->id;
  702. nand = nand_info + id->num;
  703. /* if we are building a list we need to refresh the cache. */
  704. jffs_init_1pass_list(part);
  705. pL = (struct b_lists *)part->jffs2_priv;
  706. pL->partOffset = part->offset;
  707. puts ("Scanning JFFS2 FS: ");
  708. sectorsize = nand->erasesize;
  709. nr_blocks = part->size / sectorsize;
  710. buf = malloc(sectorsize);
  711. if (!buf)
  712. return 0;
  713. for (i = 0; i < nr_blocks; i++) {
  714. printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
  715. offset = part->offset + i * sectorsize;
  716. if (nand_block_isbad(nand, offset))
  717. continue;
  718. if (jffs2_fill_scan_buf(nand, buf, offset, EMPTY_SCAN_SIZE))
  719. return 0;
  720. ofs = 0;
  721. /* Scan only 4KiB of 0xFF before declaring it's empty */
  722. while (ofs < EMPTY_SCAN_SIZE && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
  723. ofs += 4;
  724. if (ofs == EMPTY_SCAN_SIZE)
  725. continue;
  726. if (jffs2_fill_scan_buf(nand, buf + EMPTY_SCAN_SIZE, offset + EMPTY_SCAN_SIZE, sectorsize - EMPTY_SCAN_SIZE))
  727. return 0;
  728. offset += ofs;
  729. while (ofs < sectorsize - sizeof(struct jffs2_unknown_node)) {
  730. node = (struct jffs2_unknown_node *)&buf[ofs];
  731. if (node->magic != JFFS2_MAGIC_BITMASK || !hdr_crc(node)) {
  732. offset += 4;
  733. ofs += 4;
  734. counter4++;
  735. continue;
  736. }
  737. /* if its a fragment add it */
  738. if (node->nodetype == JFFS2_NODETYPE_INODE &&
  739. inode_crc((struct jffs2_raw_inode *) node)) {
  740. if (insert_inode(&pL->frag, (struct jffs2_raw_inode *) node,
  741. offset) == NULL) {
  742. return 0;
  743. }
  744. } else if (node->nodetype == JFFS2_NODETYPE_DIRENT &&
  745. dirent_crc((struct jffs2_raw_dirent *) node) &&
  746. dirent_name_crc((struct jffs2_raw_dirent *) node)) {
  747. if (! (counterN%100))
  748. puts ("\b\b. ");
  749. if (insert_dirent(&pL->dir, (struct jffs2_raw_dirent *) node,
  750. offset) == NULL) {
  751. return 0;
  752. }
  753. counterN++;
  754. } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) {
  755. if (node->totlen != sizeof(struct jffs2_unknown_node))
  756. printf("OOPS Cleanmarker has bad size "
  757. "%d != %zu\n",
  758. node->totlen,
  759. sizeof(struct jffs2_unknown_node));
  760. } else if (node->nodetype == JFFS2_NODETYPE_PADDING) {
  761. if (node->totlen < sizeof(struct jffs2_unknown_node))
  762. printf("OOPS Padding has bad size "
  763. "%d < %zu\n",
  764. node->totlen,
  765. sizeof(struct jffs2_unknown_node));
  766. } else {
  767. printf("Unknown node type: %x len %d offset 0x%x\n",
  768. node->nodetype,
  769. node->totlen, offset);
  770. }
  771. offset += ((node->totlen + 3) & ~3);
  772. ofs += ((node->totlen + 3) & ~3);
  773. counterF++;
  774. }
  775. }
  776. putstr("\b\b done.\r\n"); /* close off the dots */
  777. #if 0
  778. putLabeledWord("dir entries = ", pL->dir.listCount);
  779. putLabeledWord("frag entries = ", pL->frag.listCount);
  780. putLabeledWord("+4 increments = ", counter4);
  781. putLabeledWord("+file_offset increments = ", counterF);
  782. #endif
  783. #ifdef DEBUG_DIRENTS
  784. dump_dirents(pL);
  785. #endif
  786. #ifdef DEBUG_FRAGMENTS
  787. dump_fragments(pL);
  788. #endif
  789. /* give visual feedback that we are done scanning the flash */
  790. led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
  791. free(buf);
  792. return 1;
  793. }
  794. static u32
  795. jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
  796. {
  797. struct b_node *b;
  798. struct jffs2_raw_inode ojNode;
  799. struct jffs2_raw_inode *jNode;
  800. int i;
  801. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  802. piL->compr_info[i].num_frags = 0;
  803. piL->compr_info[i].compr_sum = 0;
  804. piL->compr_info[i].decompr_sum = 0;
  805. }
  806. /* FIXME
  807. b = pL->frag.listHead;
  808. while (b) {
  809. jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
  810. sizeof(ojNode), &ojNode);
  811. if (jNode->compr < JFFS2_NUM_COMPR) {
  812. piL->compr_info[jNode->compr].num_frags++;
  813. piL->compr_info[jNode->compr].compr_sum += jNode->csize;
  814. piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
  815. }
  816. b = b->next;
  817. }
  818. */
  819. return 0;
  820. }
  821. static struct b_lists *
  822. jffs2_get_list(struct part_info * part, const char *who)
  823. {
  824. if (jffs2_1pass_rescan_needed(part)) {
  825. if (!jffs2_1pass_build_lists(part)) {
  826. printf("%s: Failed to scan JFFSv2 file structure\n", who);
  827. return NULL;
  828. }
  829. }
  830. return (struct b_lists *)part->jffs2_priv;
  831. }
  832. /* Print directory / file contents */
  833. u32
  834. jffs2_1pass_ls(struct part_info * part, const char *fname)
  835. {
  836. struct b_lists *pl;
  837. long ret = 0;
  838. u32 inode;
  839. if (! (pl = jffs2_get_list(part, "ls")))
  840. return 0;
  841. if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
  842. putstr("ls: Failed to scan jffs2 file structure\r\n");
  843. return 0;
  844. }
  845. #if 0
  846. putLabeledWord("found file at inode = ", inode);
  847. putLabeledWord("read_inode returns = ", ret);
  848. #endif
  849. return ret;
  850. }
  851. /* Load a file from flash into memory. fname can be a full path */
  852. u32
  853. jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
  854. {
  855. struct b_lists *pl;
  856. long ret = 0;
  857. u32 inode;
  858. if (! (pl = jffs2_get_list(part, "load")))
  859. return 0;
  860. if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
  861. putstr("load: Failed to find inode\r\n");
  862. return 0;
  863. }
  864. /* Resolve symlinks */
  865. if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
  866. putstr("load: Failed to resolve inode structure\r\n");
  867. return 0;
  868. }
  869. if ((ret = jffs2_1pass_read_inode(pl, inode, dest, NULL)) < 0) {
  870. putstr("load: Failed to read inode\r\n");
  871. return 0;
  872. }
  873. DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
  874. (unsigned long) dest, ret);
  875. return ret;
  876. }
  877. /* Return information about the fs on this partition */
  878. u32
  879. jffs2_1pass_info(struct part_info * part)
  880. {
  881. struct b_jffs2_info info;
  882. struct b_lists *pl;
  883. int i;
  884. if (! (pl = jffs2_get_list(part, "info")))
  885. return 0;
  886. jffs2_1pass_fill_info(pl, &info);
  887. for (i = 0; i < JFFS2_NUM_COMPR; i++) {
  888. printf ("Compression: %s\n"
  889. "\tfrag count: %d\n"
  890. "\tcompressed sum: %d\n"
  891. "\tuncompressed sum: %d\n",
  892. compr_names[i],
  893. info.compr_info[i].num_frags,
  894. info.compr_info[i].compr_sum,
  895. info.compr_info[i].decompr_sum);
  896. }
  897. return 1;
  898. }