1 /* 2 ------------------------------------------------------------------------- 3 * Filename: jffs2.c 4 * Version: $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $ 5 * Copyright: Copyright (C) 2001, Russ Dill 6 * Author: Russ Dill <Russ.Dill@asu.edu> 7 * Description: Module to load kernel from jffs2 8 *-----------------------------------------------------------------------*/ 9 /* 10 * some portions of this code are taken from jffs2, and as such, the 11 * following copyright notice is included. 12 * 13 * JFFS2 -- Journalling Flash File System, Version 2. 14 * 15 * Copyright (C) 2001 Red Hat, Inc. 16 * 17 * Created by David Woodhouse <dwmw2@cambridge.redhat.com> 18 * 19 * The original JFFS, from which the design for JFFS2 was derived, 20 * was designed and implemented by Axis Communications AB. 21 * 22 * The contents of this file are subject to the Red Hat eCos Public 23 * License Version 1.1 (the "Licence"); you may not use this file 24 * except in compliance with the Licence. You may obtain a copy of 25 * the Licence at http://www.redhat.com/ 26 * 27 * Software distributed under the Licence is distributed on an "AS IS" 28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 29 * See the Licence for the specific language governing rights and 30 * limitations under the Licence. 31 * 32 * The Original Code is JFFS2 - Journalling Flash File System, version 2 33 * 34 * Alternatively, the contents of this file may be used under the 35 * terms of the GNU General Public License version 2 (the "GPL"), in 36 * which case the provisions of the GPL are applicable instead of the 37 * above. If you wish to allow the use of your version of this file 38 * only under the terms of the GPL and not to allow others to use your 39 * version of this file under the RHEPL, indicate your decision by 40 * deleting the provisions above and replace them with the notice and 41 * other provisions required by the GPL. If you do not delete the 42 * provisions above, a recipient may use your version of this file 43 * under either the RHEPL or the GPL. 44 * 45 * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $ 46 * 47 */ 48 49 /* Ok, so anyone who knows the jffs2 code will probably want to get a papar 50 * bag to throw up into before reading this code. I looked through the jffs2 51 * code, the caching scheme is very elegant. I tried to keep the version 52 * for a bootloader as small and simple as possible. Instead of worring about 53 * unneccesary data copies, node scans, etc, I just optimized for the known 54 * common case, a kernel, which looks like: 55 * (1) most pages are 4096 bytes 56 * (2) version numbers are somewhat sorted in acsending order 57 * (3) multiple compressed blocks making up one page is uncommon 58 * 59 * So I create a linked list of decending version numbers (insertions at the 60 * head), and then for each page, walk down the list, until a matching page 61 * with 4096 bytes is found, and then decompress the watching pages in 62 * reverse order. 63 * 64 */ 65 66 /* 67 * Adapted by Nye Liu <nyet@zumanetworks.com> and 68 * Rex Feany <rfeany@zumanetworks.com> 69 * on Jan/2002 for U-Boot. 70 * 71 * Clipped out all the non-1pass functions, cleaned up warnings, 72 * wrappers, etc. No major changes to the code. 73 * Please, he really means it when he said have a paper bag 74 * handy. We needed it ;). 75 * 76 */ 77 78 /* 79 * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003 80 * 81 * - overhaul of the memory management. Removed much of the "paper-bagging" 82 * in that part of the code, fixed several bugs, now frees memory when 83 * partition is changed. 84 * It's still ugly :-( 85 * - fixed a bug in jffs2_1pass_read_inode where the file length calculation 86 * was incorrect. Removed a bit of the paper-bagging as well. 87 * - removed double crc calculation for fragment headers in jffs2_private.h 88 * for speedup. 89 * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is). 90 * - spinning wheel now spins depending on how much memory has been scanned 91 * - lots of small changes all over the place to "improve" readability. 92 * - implemented fragment sorting to ensure that the newest data is copied 93 * if there are multiple copies of fragments for a certain file offset. 94 * 95 * The fragment sorting feature must be enabled by CFG_JFFS2_SORT_FRAGMENTS. 96 * Sorting is done while adding fragments to the lists, which is more or less a 97 * bubble sort. This takes a lot of time, and is most probably not an issue if 98 * the boot filesystem is always mounted readonly. 99 * 100 * You should define it if the boot filesystem is mounted writable, and updates 101 * to the boot files are done by copying files to that filesystem. 102 * 103 * 104 * There's a big issue left: endianess is completely ignored in this code. Duh! 105 * 106 * 107 * You still should have paper bags at hand :-(. The code lacks more or less 108 * any comment, and is still arcane and difficult to read in places. As this 109 * might be incompatible with any new code from the jffs2 maintainers anyway, 110 * it should probably be dumped and replaced by something like jffs2reader! 111 */ 112 113 114 #include <common.h> 115 #include <config.h> 116 #include <malloc.h> 117 #include <linux/stat.h> 118 #include <linux/time.h> 119 120 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) 121 122 #include <jffs2/jffs2.h> 123 #include <jffs2/jffs2_1pass.h> 124 125 #include "jffs2_private.h" 126 127 128 #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */ 129 #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */ 130 131 /* Debugging switches */ 132 #undef DEBUG_DIRENTS /* print directory entry list after scan */ 133 #undef DEBUG_FRAGMENTS /* print fragment list after scan */ 134 #undef DEBUG /* enable debugging messages */ 135 136 137 #ifdef DEBUG 138 # define DEBUGF(fmt,args...) printf(fmt ,##args) 139 #else 140 # define DEBUGF(fmt,args...) 141 #endif 142 143 144 /* Compression names */ 145 static char *compr_names[] = { 146 "NONE", 147 "ZERO", 148 "RTIME", 149 "RUBINMIPS", 150 "COPY", 151 "DYNRUBIN", 152 "ZLIB" 153 }; 154 155 /* Spinning wheel */ 156 static char spinner[] = { '|', '/', '-', '\\' }; 157 158 /* Memory management */ 159 struct mem_block { 160 u32 index; 161 struct mem_block *next; 162 struct b_node nodes[NODE_CHUNK]; 163 }; 164 165 166 static void 167 free_nodes(struct b_list *list) 168 { 169 while (list->listMemBase != NULL) { 170 struct mem_block *next = list->listMemBase->next; 171 free( list->listMemBase ); 172 list->listMemBase = next; 173 } 174 } 175 176 static struct b_node * 177 add_node(struct b_list *list) 178 { 179 u32 index = 0; 180 struct mem_block *memBase; 181 struct b_node *b; 182 183 memBase = list->listMemBase; 184 if (memBase != NULL) 185 index = memBase->index; 186 #if 0 187 putLabeledWord("add_node: index = ", index); 188 putLabeledWord("add_node: memBase = ", list->listMemBase); 189 #endif 190 191 if (memBase == NULL || index >= NODE_CHUNK) { 192 /* we need more space before we continue */ 193 memBase = mmalloc(sizeof(struct mem_block)); 194 if (memBase == NULL) { 195 putstr("add_node: malloc failed\n"); 196 return NULL; 197 } 198 memBase->next = list->listMemBase; 199 index = 0; 200 #if 0 201 putLabeledWord("add_node: alloced a new membase at ", *memBase); 202 #endif 203 204 } 205 /* now we have room to add it. */ 206 b = &memBase->nodes[index]; 207 index ++; 208 209 memBase->index = index; 210 list->listMemBase = memBase; 211 list->listCount++; 212 return b; 213 } 214 215 static struct b_node * 216 insert_node(struct b_list *list, u32 offset) 217 { 218 struct b_node *new; 219 #ifdef CFG_JFFS2_SORT_FRAGMENTS 220 struct b_node *b, *prev; 221 #endif 222 223 if (!(new = add_node(list))) { 224 putstr("add_node failed!\r\n"); 225 return NULL; 226 } 227 new->offset = offset; 228 229 #ifdef CFG_JFFS2_SORT_FRAGMENTS 230 if (list->listTail != NULL && list->listCompare(new, list->listTail)) 231 prev = list->listTail; 232 else if (list->listLast != NULL && list->listCompare(new, list->listLast)) 233 prev = list->listLast; 234 else 235 prev = NULL; 236 237 for (b = (prev ? prev->next : list->listHead); 238 b != NULL && list->listCompare(new, b); 239 prev = b, b = b->next) { 240 list->listLoops++; 241 } 242 if (b != NULL) 243 list->listLast = prev; 244 245 if (b != NULL) { 246 new->next = b; 247 if (prev != NULL) 248 prev->next = new; 249 else 250 list->listHead = new; 251 } else 252 #endif 253 { 254 new->next = (struct b_node *) NULL; 255 if (list->listTail != NULL) { 256 list->listTail->next = new; 257 list->listTail = new; 258 } else { 259 list->listTail = list->listHead = new; 260 } 261 } 262 263 return new; 264 } 265 266 #ifdef CFG_JFFS2_SORT_FRAGMENTS 267 /* Sort data entries with the latest version last, so that if there 268 * is overlapping data the latest version will be used. 269 */ 270 static int compare_inodes(struct b_node *new, struct b_node *old) 271 { 272 struct jffs2_raw_inode *jNew = (struct jffs2_raw_inode *)new->offset; 273 struct jffs2_raw_inode *jOld = (struct jffs2_raw_inode *)old->offset; 274 275 return jNew->version > jOld->version; 276 } 277 278 /* Sort directory entries so all entries in the same directory 279 * with the same name are grouped together, with the latest version 280 * last. This makes it easy to eliminate all but the latest version 281 * by marking the previous version dead by setting the inode to 0. 282 */ 283 static int compare_dirents(struct b_node *new, struct b_node *old) 284 { 285 struct jffs2_raw_dirent *jNew = (struct jffs2_raw_dirent *)new->offset; 286 struct jffs2_raw_dirent *jOld = (struct jffs2_raw_dirent *)old->offset; 287 int cmp; 288 289 /* ascending sort by pino */ 290 if (jNew->pino != jOld->pino) 291 return jNew->pino > jOld->pino; 292 293 /* pino is the same, so use ascending sort by nsize, so 294 * we don't do strncmp unless we really must. 295 */ 296 if (jNew->nsize != jOld->nsize) 297 return jNew->nsize > jOld->nsize; 298 299 /* length is also the same, so use ascending sort by name 300 */ 301 cmp = strncmp(jNew->name, jOld->name, jNew->nsize); 302 if (cmp != 0) 303 return cmp > 0; 304 305 /* we have duplicate names in this directory, so use ascending 306 * sort by version 307 */ 308 if (jNew->version > jOld->version) { 309 /* since jNew is newer, we know jOld is not valid, so 310 * mark it with inode 0 and it will not be used 311 */ 312 jOld->ino = 0; 313 return 1; 314 } 315 316 return 0; 317 } 318 #endif 319 320 static u32 321 jffs2_scan_empty(u32 start_offset, struct part_info *part) 322 { 323 char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode); 324 char *offset = part->offset + start_offset; 325 326 while (offset < max && *(u32 *)offset == 0xFFFFFFFF) { 327 offset += sizeof(u32); 328 /* return if spinning is due */ 329 if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break; 330 } 331 332 return offset - part->offset; 333 } 334 335 static u32 336 jffs_init_1pass_list(struct part_info *part) 337 { 338 struct b_lists *pL; 339 340 if (part->jffs2_priv != NULL) { 341 pL = (struct b_lists *)part->jffs2_priv; 342 free_nodes(&pL->frag); 343 free_nodes(&pL->dir); 344 free(pL); 345 } 346 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) { 347 pL = (struct b_lists *)part->jffs2_priv; 348 349 memset(pL, 0, sizeof(*pL)); 350 #ifdef CFG_JFFS2_SORT_FRAGMENTS 351 pL->dir.listCompare = compare_dirents; 352 pL->frag.listCompare = compare_inodes; 353 #endif 354 } 355 return 0; 356 } 357 358 /* find the inode from the slashless name given a parent */ 359 static long 360 jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest) 361 { 362 struct b_node *b; 363 struct jffs2_raw_inode *jNode; 364 u32 totalSize = 0; 365 u32 latestVersion = 0; 366 char *lDest; 367 char *src; 368 long ret; 369 int i; 370 u32 counter = 0; 371 #ifdef CFG_JFFS2_SORT_FRAGMENTS 372 /* Find file size before loading any data, so fragments that 373 * start past the end of file can be ignored. A fragment 374 * that is partially in the file is loaded, so extra data may 375 * be loaded up to the next 4K boundary above the file size. 376 * This shouldn't cause trouble when loading kernel images, so 377 * we will live with it. 378 */ 379 for (b = pL->frag.listHead; b != NULL; b = b->next) { 380 jNode = (struct jffs2_raw_inode *) (b->offset); 381 if ((inode == jNode->ino)) { 382 /* get actual file length from the newest node */ 383 if (jNode->version >= latestVersion) { 384 totalSize = jNode->isize; 385 latestVersion = jNode->version; 386 } 387 } 388 } 389 #endif 390 391 for (b = pL->frag.listHead; b != NULL; b = b->next) { 392 jNode = (struct jffs2_raw_inode *) (b->offset); 393 if ((inode == jNode->ino)) { 394 #if 0 395 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen); 396 putLabeledWord("read_inode: inode = ", jNode->ino); 397 putLabeledWord("read_inode: version = ", jNode->version); 398 putLabeledWord("read_inode: isize = ", jNode->isize); 399 putLabeledWord("read_inode: offset = ", jNode->offset); 400 putLabeledWord("read_inode: csize = ", jNode->csize); 401 putLabeledWord("read_inode: dsize = ", jNode->dsize); 402 putLabeledWord("read_inode: compr = ", jNode->compr); 403 putLabeledWord("read_inode: usercompr = ", jNode->usercompr); 404 putLabeledWord("read_inode: flags = ", jNode->flags); 405 #endif 406 407 #ifndef CFG_JFFS2_SORT_FRAGMENTS 408 /* get actual file length from the newest node */ 409 if (jNode->version >= latestVersion) { 410 totalSize = jNode->isize; 411 latestVersion = jNode->version; 412 } 413 #endif 414 415 if(dest) { 416 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode); 417 /* ignore data behind latest known EOF */ 418 if (jNode->offset > totalSize) 419 continue; 420 421 lDest = (char *) (dest + jNode->offset); 422 #if 0 423 putLabeledWord("read_inode: src = ", src); 424 putLabeledWord("read_inode: dest = ", lDest); 425 #endif 426 switch (jNode->compr) { 427 case JFFS2_COMPR_NONE: 428 ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize); 429 break; 430 case JFFS2_COMPR_ZERO: 431 ret = 0; 432 for (i = 0; i < jNode->dsize; i++) 433 *(lDest++) = 0; 434 break; 435 case JFFS2_COMPR_RTIME: 436 ret = 0; 437 rtime_decompress(src, lDest, jNode->csize, jNode->dsize); 438 break; 439 case JFFS2_COMPR_DYNRUBIN: 440 /* this is slow but it works */ 441 ret = 0; 442 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize); 443 break; 444 case JFFS2_COMPR_ZLIB: 445 ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize); 446 break; 447 default: 448 /* unknown */ 449 putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr); 450 return -1; 451 break; 452 } 453 } 454 455 #if 0 456 putLabeledWord("read_inode: totalSize = ", totalSize); 457 putLabeledWord("read_inode: compr ret = ", ret); 458 #endif 459 } 460 counter++; 461 } 462 463 #if 0 464 putLabeledWord("read_inode: returning = ", totalSize); 465 #endif 466 return totalSize; 467 } 468 469 /* find the inode from the slashless name given a parent */ 470 static u32 471 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino) 472 { 473 struct b_node *b; 474 struct jffs2_raw_dirent *jDir; 475 int len; 476 u32 counter; 477 u32 version = 0; 478 u32 inode = 0; 479 480 /* name is assumed slash free */ 481 len = strlen(name); 482 483 counter = 0; 484 /* we need to search all and return the inode with the highest version */ 485 for(b = pL->dir.listHead; b; b = b->next, counter++) { 486 jDir = (struct jffs2_raw_dirent *) (b->offset); 487 if ((pino == jDir->pino) && (len == jDir->nsize) && 488 (jDir->ino) && /* 0 for unlink */ 489 (!strncmp(jDir->name, name, len))) { /* a match */ 490 if (jDir->version < version) 491 continue; 492 493 if (jDir->version == version && inode != 0) { 494 /* I'm pretty sure this isn't legal */ 495 putstr(" ** ERROR ** "); 496 putnstr(jDir->name, jDir->nsize); 497 putLabeledWord(" has dup version =", version); 498 } 499 inode = jDir->ino; 500 version = jDir->version; 501 } 502 #if 0 503 putstr("\r\nfind_inode:p&l ->"); 504 putnstr(jDir->name, jDir->nsize); 505 putstr("\r\n"); 506 putLabeledWord("pino = ", jDir->pino); 507 putLabeledWord("nsize = ", jDir->nsize); 508 putLabeledWord("b = ", (u32) b); 509 putLabeledWord("counter = ", counter); 510 #endif 511 } 512 return inode; 513 } 514 515 char *mkmodestr(unsigned long mode, char *str) 516 { 517 static const char *l = "xwr"; 518 int mask = 1, i; 519 char c; 520 521 switch (mode & S_IFMT) { 522 case S_IFDIR: str[0] = 'd'; break; 523 case S_IFBLK: str[0] = 'b'; break; 524 case S_IFCHR: str[0] = 'c'; break; 525 case S_IFIFO: str[0] = 'f'; break; 526 case S_IFLNK: str[0] = 'l'; break; 527 case S_IFSOCK: str[0] = 's'; break; 528 case S_IFREG: str[0] = '-'; break; 529 default: str[0] = '?'; 530 } 531 532 for(i = 0; i < 9; i++) { 533 c = l[i%3]; 534 str[9-i] = (mode & mask)?c:'-'; 535 mask = mask<<1; 536 } 537 538 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S'; 539 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S'; 540 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T'; 541 str[10] = '\0'; 542 return str; 543 } 544 545 static inline void dump_stat(struct stat *st, const char *name) 546 { 547 char str[20]; 548 char s[64], *p; 549 550 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */ 551 st->st_mtime = 1; 552 553 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */ 554 555 if ((p = strchr(s,'\n')) != NULL) *p = '\0'; 556 if ((p = strchr(s,'\r')) != NULL) *p = '\0'; 557 558 /* 559 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str), 560 st->st_size, s, name); 561 */ 562 563 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name); 564 } 565 566 static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i) 567 { 568 char fname[256]; 569 struct stat st; 570 571 if(!d || !i) return -1; 572 573 strncpy(fname, d->name, d->nsize); 574 fname[d->nsize] = '\0'; 575 576 memset(&st,0,sizeof(st)); 577 578 st.st_mtime = i->mtime; 579 st.st_mode = i->mode; 580 st.st_ino = i->ino; 581 582 /* neither dsize nor isize help us.. do it the long way */ 583 st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL); 584 585 dump_stat(&st, fname); 586 587 if (d->type == DT_LNK) { 588 unsigned char *src = (unsigned char *) (&i[1]); 589 putstr(" -> "); 590 putnstr(src, (int)i->dsize); 591 } 592 593 putstr("\r\n"); 594 595 return 0; 596 } 597 598 /* list inodes with the given pino */ 599 static u32 600 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino) 601 { 602 struct b_node *b; 603 struct jffs2_raw_dirent *jDir; 604 605 for (b = pL->dir.listHead; b; b = b->next) { 606 jDir = (struct jffs2_raw_dirent *) (b->offset); 607 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */ 608 u32 i_version = 0; 609 struct jffs2_raw_inode *jNode, *i = NULL; 610 struct b_node *b2 = pL->frag.listHead; 611 612 while (b2) { 613 jNode = (struct jffs2_raw_inode *) (b2->offset); 614 if (jNode->ino == jDir->ino 615 && jNode->version >= i_version) 616 i = jNode; 617 b2 = b2->next; 618 } 619 620 dump_inode(pL, jDir, i); 621 } 622 } 623 return pino; 624 } 625 626 static u32 627 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino) 628 { 629 int i; 630 char tmp[256]; 631 char working_tmp[256]; 632 char *c; 633 634 /* discard any leading slash */ 635 i = 0; 636 while (fname[i] == '/') 637 i++; 638 strcpy(tmp, &fname[i]); 639 640 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ 641 { 642 strncpy(working_tmp, tmp, c - tmp); 643 working_tmp[c - tmp] = '\0'; 644 #if 0 645 putstr("search_inode: tmp = "); 646 putstr(tmp); 647 putstr("\r\n"); 648 putstr("search_inode: wtmp = "); 649 putstr(working_tmp); 650 putstr("\r\n"); 651 putstr("search_inode: c = "); 652 putstr(c); 653 putstr("\r\n"); 654 #endif 655 for (i = 0; i < strlen(c) - 1; i++) 656 tmp[i] = c[i + 1]; 657 tmp[i] = '\0'; 658 #if 0 659 putstr("search_inode: post tmp = "); 660 putstr(tmp); 661 putstr("\r\n"); 662 #endif 663 664 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) { 665 putstr("find_inode failed for name="); 666 putstr(working_tmp); 667 putstr("\r\n"); 668 return 0; 669 } 670 } 671 /* this is for the bare filename, directories have already been mapped */ 672 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { 673 putstr("find_inode failed for name="); 674 putstr(tmp); 675 putstr("\r\n"); 676 return 0; 677 } 678 return pino; 679 680 } 681 682 static u32 683 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino) 684 { 685 struct b_node *b; 686 struct b_node *b2; 687 struct jffs2_raw_dirent *jDir; 688 struct jffs2_raw_inode *jNode; 689 struct jffs2_raw_dirent *jDirFound = NULL; 690 char tmp[256]; 691 u32 version = 0; 692 u32 pino; 693 unsigned char *src; 694 695 /* we need to search all and return the inode with the highest version */ 696 for(b = pL->dir.listHead; b; b = b->next) { 697 jDir = (struct jffs2_raw_dirent *) (b->offset); 698 if (ino == jDir->ino) { 699 if (jDir->version < version) 700 continue; 701 702 if (jDir->version == version && jDirFound) { 703 /* I'm pretty sure this isn't legal */ 704 putstr(" ** ERROR ** "); 705 putnstr(jDir->name, jDir->nsize); 706 putLabeledWord(" has dup version (resolve) = ", 707 version); 708 } 709 710 jDirFound = jDir; 711 version = jDir->version; 712 } 713 } 714 /* now we found the right entry again. (shoulda returned inode*) */ 715 if (jDirFound->type != DT_LNK) 716 return jDirFound->ino; 717 718 /* it's a soft link so we follow it again. */ 719 b2 = pL->frag.listHead; 720 while (b2) { 721 jNode = (struct jffs2_raw_inode *) (b2->offset); 722 if (jNode->ino == jDirFound->ino) { 723 src = (unsigned char *) (b2->offset + sizeof(struct jffs2_raw_inode)); 724 725 #if 0 726 putLabeledWord("\t\t dsize = ", jNode->dsize); 727 putstr("\t\t target = "); 728 putnstr(src, jNode->dsize); 729 putstr("\r\n"); 730 #endif 731 strncpy(tmp, src, jNode->dsize); 732 tmp[jNode->dsize] = '\0'; 733 break; 734 } 735 b2 = b2->next; 736 } 737 /* ok so the name of the new file to find is in tmp */ 738 /* if it starts with a slash it is root based else shared dirs */ 739 if (tmp[0] == '/') 740 pino = 1; 741 else 742 pino = jDirFound->pino; 743 744 return jffs2_1pass_search_inode(pL, tmp, pino); 745 } 746 747 static u32 748 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino) 749 { 750 int i; 751 char tmp[256]; 752 char working_tmp[256]; 753 char *c; 754 755 /* discard any leading slash */ 756 i = 0; 757 while (fname[i] == '/') 758 i++; 759 strcpy(tmp, &fname[i]); 760 working_tmp[0] = '\0'; 761 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ 762 { 763 strncpy(working_tmp, tmp, c - tmp); 764 working_tmp[c - tmp] = '\0'; 765 for (i = 0; i < strlen(c) - 1; i++) 766 tmp[i] = c[i + 1]; 767 tmp[i] = '\0'; 768 /* only a failure if we arent looking at top level */ 769 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) && 770 (working_tmp[0])) { 771 putstr("find_inode failed for name="); 772 putstr(working_tmp); 773 putstr("\r\n"); 774 return 0; 775 } 776 } 777 778 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { 779 putstr("find_inode failed for name="); 780 putstr(tmp); 781 putstr("\r\n"); 782 return 0; 783 } 784 /* this is for the bare filename, directories have already been mapped */ 785 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) { 786 putstr("find_inode failed for name="); 787 putstr(tmp); 788 putstr("\r\n"); 789 return 0; 790 } 791 return pino; 792 793 } 794 795 unsigned char 796 jffs2_1pass_rescan_needed(struct part_info *part) 797 { 798 struct b_node *b; 799 struct jffs2_unknown_node *node; 800 struct b_lists *pL = (struct b_lists *)part->jffs2_priv; 801 802 if (part->jffs2_priv == 0){ 803 DEBUGF ("rescan: First time in use\n"); 804 return 1; 805 } 806 /* if we have no list, we need to rescan */ 807 if (pL->frag.listCount == 0) { 808 DEBUGF ("rescan: fraglist zero\n"); 809 return 1; 810 } 811 812 /* or if we are scanning a new partition */ 813 if (pL->partOffset != part->offset) { 814 DEBUGF ("rescan: different partition\n"); 815 return 1; 816 } 817 /* but suppose someone reflashed a partition at the same offset... */ 818 b = pL->dir.listHead; 819 while (b) { 820 node = (struct jffs2_unknown_node *) (b->offset); 821 if (node->nodetype != JFFS2_NODETYPE_DIRENT) { 822 DEBUGF ("rescan: fs changed beneath me? (%lx)\n", 823 (unsigned long) b->offset); 824 return 1; 825 } 826 b = b->next; 827 } 828 return 0; 829 } 830 831 #ifdef DEBUG_FRAGMENTS 832 static void 833 dump_fragments(struct b_lists *pL) 834 { 835 struct b_node *b; 836 struct jffs2_raw_inode *jNode; 837 838 putstr("\r\n\r\n******The fragment Entries******\r\n"); 839 b = pL->frag.listHead; 840 while (b) { 841 jNode = (struct jffs2_raw_inode *) (b->offset); 842 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset); 843 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen); 844 putLabeledWord("\tbuild_list: inode = ", jNode->ino); 845 putLabeledWord("\tbuild_list: version = ", jNode->version); 846 putLabeledWord("\tbuild_list: isize = ", jNode->isize); 847 putLabeledWord("\tbuild_list: atime = ", jNode->atime); 848 putLabeledWord("\tbuild_list: offset = ", jNode->offset); 849 putLabeledWord("\tbuild_list: csize = ", jNode->csize); 850 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize); 851 putLabeledWord("\tbuild_list: compr = ", jNode->compr); 852 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr); 853 putLabeledWord("\tbuild_list: flags = ", jNode->flags); 854 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ 855 b = b->next; 856 } 857 } 858 #endif 859 860 #ifdef DEBUG_DIRENTS 861 static void 862 dump_dirents(struct b_lists *pL) 863 { 864 struct b_node *b; 865 struct jffs2_raw_dirent *jDir; 866 867 putstr("\r\n\r\n******The directory Entries******\r\n"); 868 b = pL->dir.listHead; 869 while (b) { 870 jDir = (struct jffs2_raw_dirent *) (b->offset); 871 putstr("\r\n"); 872 putnstr(jDir->name, jDir->nsize); 873 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic); 874 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype); 875 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc); 876 putLabeledWord("\tbuild_list: pino = ", jDir->pino); 877 putLabeledWord("\tbuild_list: version = ", jDir->version); 878 putLabeledWord("\tbuild_list: ino = ", jDir->ino); 879 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime); 880 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize); 881 putLabeledWord("\tbuild_list: type = ", jDir->type); 882 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc); 883 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc); 884 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ 885 b = b->next; 886 } 887 } 888 #endif 889 890 static u32 891 jffs2_1pass_build_lists(struct part_info * part) 892 { 893 struct b_lists *pL; 894 struct jffs2_unknown_node *node; 895 u32 offset, oldoffset = 0; 896 u32 max = part->size - sizeof(struct jffs2_raw_inode); 897 u32 counter = 0; 898 u32 counter4 = 0; 899 u32 counterF = 0; 900 u32 counterN = 0; 901 902 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */ 903 /* jffs2 list building enterprise nope. in newer versions the overhead is */ 904 /* only about 5 %. not enough to inconvenience people for. */ 905 /* lcd_off(); */ 906 907 /* if we are building a list we need to refresh the cache. */ 908 jffs_init_1pass_list(part); 909 pL = (struct b_lists *)part->jffs2_priv; 910 pL->partOffset = part->offset; 911 offset = 0; 912 puts ("Scanning JFFS2 FS: "); 913 914 /* start at the beginning of the partition */ 915 while (offset < max) { 916 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) { 917 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]); 918 oldoffset = offset; 919 } 920 921 node = (struct jffs2_unknown_node *) (part->offset + offset); 922 if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) { 923 /* if its a fragment add it */ 924 if (node->nodetype == JFFS2_NODETYPE_INODE && 925 inode_crc((struct jffs2_raw_inode *) node)) { 926 if (insert_node(&pL->frag, (u32) part->offset + 927 offset) == NULL) 928 return 0; 929 } else if (node->nodetype == JFFS2_NODETYPE_DIRENT && 930 dirent_crc((struct jffs2_raw_dirent *) node) && 931 dirent_name_crc((struct jffs2_raw_dirent *) node)) { 932 if (! (counterN%100)) 933 puts ("\b\b. "); 934 if (insert_node(&pL->dir, (u32) part->offset + 935 offset) == NULL) 936 return 0; 937 counterN++; 938 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) { 939 if (node->totlen != sizeof(struct jffs2_unknown_node)) 940 printf("OOPS Cleanmarker has bad size " 941 "%d != %d\n", node->totlen, 942 sizeof(struct jffs2_unknown_node)); 943 } else if (node->nodetype == JFFS2_NODETYPE_PADDING) { 944 if (node->totlen < sizeof(struct jffs2_unknown_node)) 945 printf("OOPS Padding has bad size " 946 "%d < %d\n", node->totlen, 947 sizeof(struct jffs2_unknown_node)); 948 } else { 949 printf("Unknown node type: %x len %d " 950 "offset 0x%x\n", node->nodetype, 951 node->totlen, offset); 952 } 953 offset += ((node->totlen + 3) & ~3); 954 counterF++; 955 } else if (node->magic == JFFS2_EMPTY_BITMASK && 956 node->nodetype == JFFS2_EMPTY_BITMASK) { 957 offset = jffs2_scan_empty(offset, part); 958 } else { /* if we know nothing, we just step and look. */ 959 offset += 4; 960 counter4++; 961 } 962 /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */ 963 964 } 965 966 putstr("\b\b done.\r\n"); /* close off the dots */ 967 /* turn the lcd back on. */ 968 /* splash(); */ 969 970 #if 0 971 putLabeledWord("dir entries = ", pL->dir.listCount); 972 putLabeledWord("frag entries = ", pL->frag.listCount); 973 putLabeledWord("+4 increments = ", counter4); 974 putLabeledWord("+file_offset increments = ", counterF); 975 976 #endif 977 978 #ifdef DEBUG_DIRENTS 979 dump_dirents(pL); 980 #endif 981 982 #ifdef DEBUG_FRAGMENTS 983 dump_fragments(pL); 984 #endif 985 986 /* give visual feedback that we are done scanning the flash */ 987 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */ 988 return 1; 989 } 990 991 992 static u32 993 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL) 994 { 995 struct b_node *b; 996 struct jffs2_raw_inode *jNode; 997 int i; 998 999 for (i = 0; i < JFFS2_NUM_COMPR; i++) { 1000 piL->compr_info[i].num_frags = 0; 1001 piL->compr_info[i].compr_sum = 0; 1002 piL->compr_info[i].decompr_sum = 0; 1003 } 1004 1005 b = pL->frag.listHead; 1006 while (b) { 1007 jNode = (struct jffs2_raw_inode *) (b->offset); 1008 if (jNode->compr < JFFS2_NUM_COMPR) { 1009 piL->compr_info[jNode->compr].num_frags++; 1010 piL->compr_info[jNode->compr].compr_sum += jNode->csize; 1011 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize; 1012 } 1013 b = b->next; 1014 } 1015 return 0; 1016 } 1017 1018 1019 static struct b_lists * 1020 jffs2_get_list(struct part_info * part, const char *who) 1021 { 1022 if (jffs2_1pass_rescan_needed(part)) { 1023 if (!jffs2_1pass_build_lists(part)) { 1024 printf("%s: Failed to scan JFFSv2 file structure\n", who); 1025 return NULL; 1026 } 1027 } 1028 return (struct b_lists *)part->jffs2_priv; 1029 } 1030 1031 1032 /* Print directory / file contents */ 1033 u32 1034 jffs2_1pass_ls(struct part_info * part, const char *fname) 1035 { 1036 struct b_lists *pl; 1037 long ret = 0; 1038 u32 inode; 1039 1040 if (! (pl = jffs2_get_list(part, "ls"))) 1041 return 0; 1042 1043 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) { 1044 putstr("ls: Failed to scan jffs2 file structure\r\n"); 1045 return 0; 1046 } 1047 1048 1049 #if 0 1050 putLabeledWord("found file at inode = ", inode); 1051 putLabeledWord("read_inode returns = ", ret); 1052 #endif 1053 1054 return ret; 1055 } 1056 1057 1058 /* Load a file from flash into memory. fname can be a full path */ 1059 u32 1060 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname) 1061 { 1062 1063 struct b_lists *pl; 1064 long ret = 0; 1065 u32 inode; 1066 1067 if (! (pl = jffs2_get_list(part, "load"))) 1068 return 0; 1069 1070 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) { 1071 putstr("load: Failed to find inode\r\n"); 1072 return 0; 1073 } 1074 1075 /* Resolve symlinks */ 1076 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) { 1077 putstr("load: Failed to resolve inode structure\r\n"); 1078 return 0; 1079 } 1080 1081 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) { 1082 putstr("load: Failed to read inode\r\n"); 1083 return 0; 1084 } 1085 1086 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname, 1087 (unsigned long) dest, ret); 1088 return ret; 1089 } 1090 1091 /* Return information about the fs on this partition */ 1092 u32 1093 jffs2_1pass_info(struct part_info * part) 1094 { 1095 struct b_jffs2_info info; 1096 struct b_lists *pl; 1097 int i; 1098 1099 if (! (pl = jffs2_get_list(part, "info"))) 1100 return 0; 1101 1102 jffs2_1pass_fill_info(pl, &info); 1103 for (i = 0; i < JFFS2_NUM_COMPR; i++) { 1104 printf ("Compression: %s\n" 1105 "\tfrag count: %d\n" 1106 "\tcompressed sum: %d\n" 1107 "\tuncompressed sum: %d\n", 1108 compr_names[i], 1109 info.compr_info[i].num_frags, 1110 info.compr_info[i].compr_sum, 1111 info.compr_info[i].decompr_sum); 1112 } 1113 return 1; 1114 } 1115 1116 #endif /* CFG_CMD_JFFS2 */ 1117