jffs2_nand_1pass.c 26 KB

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