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