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 <watchdog.h> 118 #include <linux/stat.h> 119 #include <linux/time.h> 120 121 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) 122 123 #include <jffs2/jffs2.h> 124 #include <jffs2/jffs2_1pass.h> 125 126 #include "jffs2_private.h" 127 128 129 #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */ 130 #define SPIN_BLKSIZE 20 /* spin after having scanned 1<<BLKSIZE bytes */ 131 132 /* Debugging switches */ 133 #undef DEBUG_DIRENTS /* print directory entry list after scan */ 134 #undef DEBUG_FRAGMENTS /* print fragment list after scan */ 135 #undef DEBUG /* enable debugging messages */ 136 137 138 #ifdef DEBUG 139 # define DEBUGF(fmt,args...) printf(fmt ,##args) 140 #else 141 # define DEBUGF(fmt,args...) 142 #endif 143 144 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) 145 146 /* 147 * Support for jffs2 on top of NAND-flash 148 * 149 * NAND memory isn't mapped in processor's address space, 150 * so data should be fetched from flash before 151 * being processed. This is exactly what functions declared 152 * here do. 153 * 154 */ 155 156 /* this one defined in cmd_nand.c */ 157 int read_jffs2_nand(size_t start, size_t len, 158 size_t * retlen, u_char * buf, int nanddev); 159 160 #define NAND_PAGE_SIZE 512 161 #define NAND_PAGE_SHIFT 9 162 #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1)) 163 164 #ifndef NAND_CACHE_PAGES 165 #define NAND_CACHE_PAGES 16 166 #endif 167 #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE) 168 169 static u8* nand_cache = NULL; 170 static u32 nand_cache_off = (u32)-1; 171 static int nanddev = 0; /* nand device of current partition */ 172 173 static int read_nand_cached(u32 off, u32 size, u_char *buf) 174 { 175 u32 bytes_read = 0; 176 size_t retlen; 177 int cpy_bytes; 178 179 while (bytes_read < size) { 180 if ((off + bytes_read < nand_cache_off) || 181 (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) { 182 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK; 183 if (!nand_cache) { 184 /* This memory never gets freed but 'cause 185 it's a bootloader, nobody cares */ 186 nand_cache = malloc(NAND_CACHE_SIZE); 187 if (!nand_cache) { 188 printf("read_nand_cached: can't alloc cache size %d bytes\n", 189 NAND_CACHE_SIZE); 190 return -1; 191 } 192 } 193 if (read_jffs2_nand(nand_cache_off, NAND_CACHE_SIZE, 194 &retlen, nand_cache, nanddev) < 0 || 195 retlen != NAND_CACHE_SIZE) { 196 printf("read_nand_cached: error reading nand off %#x size %d bytes\n", 197 nand_cache_off, NAND_CACHE_SIZE); 198 return -1; 199 } 200 } 201 cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read); 202 if (cpy_bytes > size - bytes_read) 203 cpy_bytes = size - bytes_read; 204 memcpy(buf + bytes_read, 205 nand_cache + off + bytes_read - nand_cache_off, 206 cpy_bytes); 207 bytes_read += cpy_bytes; 208 } 209 return bytes_read; 210 } 211 212 static void *get_fl_mem(u32 off, u32 size, void *ext_buf) 213 { 214 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size); 215 216 if (NULL == buf) { 217 printf("get_fl_mem: can't alloc %d bytes\n", size); 218 return NULL; 219 } 220 if (read_nand_cached(off, size, buf) < 0) { 221 free(buf); 222 return NULL; 223 } 224 225 return buf; 226 } 227 228 static void *get_node_mem(u32 off) 229 { 230 struct jffs2_unknown_node node; 231 void *ret = NULL; 232 233 if (NULL == get_fl_mem(off, sizeof(node), &node)) 234 return NULL; 235 236 if (!(ret = get_fl_mem(off, node.magic == 237 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node), 238 NULL))) { 239 printf("off = %#x magic %#x type %#x node.totlen = %d\n", 240 off, node.magic, node.nodetype, node.totlen); 241 } 242 return ret; 243 } 244 245 static void put_fl_mem(void *buf) 246 { 247 free(buf); 248 } 249 250 #else /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ 251 252 static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf) 253 { 254 return (void*)off; 255 } 256 257 static inline void *get_node_mem(u32 off) 258 { 259 return (void*)off; 260 } 261 262 static inline void put_fl_mem(void *buf) 263 { 264 } 265 266 #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ 267 268 269 /* Compression names */ 270 static char *compr_names[] = { 271 "NONE", 272 "ZERO", 273 "RTIME", 274 "RUBINMIPS", 275 "COPY", 276 "DYNRUBIN", 277 "ZLIB" 278 }; 279 280 #if 0 /* Use spinning wheel */ 281 /* Spinning wheel */ 282 static char spinner[] = { '|', '/', '-', '\\' }; 283 #endif 284 285 /* Memory management */ 286 struct mem_block { 287 u32 index; 288 struct mem_block *next; 289 struct b_node nodes[NODE_CHUNK]; 290 }; 291 292 293 static void 294 free_nodes(struct b_list *list) 295 { 296 while (list->listMemBase != NULL) { 297 struct mem_block *next = list->listMemBase->next; 298 free( list->listMemBase ); 299 list->listMemBase = next; 300 } 301 } 302 303 static struct b_node * 304 add_node(struct b_list *list) 305 { 306 u32 index = 0; 307 struct mem_block *memBase; 308 struct b_node *b; 309 310 memBase = list->listMemBase; 311 if (memBase != NULL) 312 index = memBase->index; 313 #if 0 314 putLabeledWord("add_node: index = ", index); 315 putLabeledWord("add_node: memBase = ", list->listMemBase); 316 #endif 317 318 if (memBase == NULL || index >= NODE_CHUNK) { 319 /* we need more space before we continue */ 320 memBase = mmalloc(sizeof(struct mem_block)); 321 if (memBase == NULL) { 322 putstr("add_node: malloc failed\n"); 323 return NULL; 324 } 325 memBase->next = list->listMemBase; 326 index = 0; 327 #if 0 328 putLabeledWord("add_node: alloced a new membase at ", *memBase); 329 #endif 330 331 } 332 /* now we have room to add it. */ 333 b = &memBase->nodes[index]; 334 index ++; 335 336 memBase->index = index; 337 list->listMemBase = memBase; 338 list->listCount++; 339 return b; 340 } 341 342 static struct b_node * 343 insert_node(struct b_list *list, u32 offset) 344 { 345 struct b_node *new; 346 #ifdef CFG_JFFS2_SORT_FRAGMENTS 347 struct b_node *b, *prev; 348 #endif 349 350 if (!(new = add_node(list))) { 351 putstr("add_node failed!\r\n"); 352 return NULL; 353 } 354 new->offset = offset; 355 356 #ifdef CFG_JFFS2_SORT_FRAGMENTS 357 if (list->listTail != NULL && list->listCompare(new, list->listTail)) 358 prev = list->listTail; 359 else if (list->listLast != NULL && list->listCompare(new, list->listLast)) 360 prev = list->listLast; 361 else 362 prev = NULL; 363 364 for (b = (prev ? prev->next : list->listHead); 365 b != NULL && list->listCompare(new, b); 366 prev = b, b = b->next) { 367 list->listLoops++; 368 } 369 if (b != NULL) 370 list->listLast = prev; 371 372 if (b != NULL) { 373 new->next = b; 374 if (prev != NULL) 375 prev->next = new; 376 else 377 list->listHead = new; 378 } else 379 #endif 380 { 381 new->next = (struct b_node *) NULL; 382 if (list->listTail != NULL) { 383 list->listTail->next = new; 384 list->listTail = new; 385 } else { 386 list->listTail = list->listHead = new; 387 } 388 } 389 390 return new; 391 } 392 393 #ifdef CFG_JFFS2_SORT_FRAGMENTS 394 /* Sort data entries with the latest version last, so that if there 395 * is overlapping data the latest version will be used. 396 */ 397 static int compare_inodes(struct b_node *new, struct b_node *old) 398 { 399 struct jffs2_raw_inode ojNew; 400 struct jffs2_raw_inode ojOld; 401 struct jffs2_raw_inode *jNew = 402 (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew); 403 struct jffs2_raw_inode *jOld = 404 (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld); 405 406 return jNew->version > jOld->version; 407 } 408 409 /* Sort directory entries so all entries in the same directory 410 * with the same name are grouped together, with the latest version 411 * last. This makes it easy to eliminate all but the latest version 412 * by marking the previous version dead by setting the inode to 0. 413 */ 414 static int compare_dirents(struct b_node *new, struct b_node *old) 415 { 416 struct jffs2_raw_dirent ojNew; 417 struct jffs2_raw_dirent ojOld; 418 struct jffs2_raw_dirent *jNew = 419 (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew); 420 struct jffs2_raw_dirent *jOld = 421 (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld); 422 int cmp; 423 424 /* ascending sort by pino */ 425 if (jNew->pino != jOld->pino) 426 return jNew->pino > jOld->pino; 427 428 /* pino is the same, so use ascending sort by nsize, so 429 * we don't do strncmp unless we really must. 430 */ 431 if (jNew->nsize != jOld->nsize) 432 return jNew->nsize > jOld->nsize; 433 434 /* length is also the same, so use ascending sort by name 435 */ 436 cmp = strncmp(jNew->name, jOld->name, jNew->nsize); 437 if (cmp != 0) 438 return cmp > 0; 439 440 /* we have duplicate names in this directory, so use ascending 441 * sort by version 442 */ 443 if (jNew->version > jOld->version) { 444 /* since jNew is newer, we know jOld is not valid, so 445 * mark it with inode 0 and it will not be used 446 */ 447 jOld->ino = 0; 448 return 1; 449 } 450 451 return 0; 452 } 453 #endif 454 455 static u32 456 jffs2_scan_empty(u32 start_offset, struct part_info *part) 457 { 458 char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode); 459 char *offset = part->offset + start_offset; 460 int cntr = 0; 461 u32 off; 462 463 while (offset < max && 464 *(u32*)get_fl_mem((u32)offset, sizeof(u32), &off) == 0xFFFFFFFF) { 465 offset += sizeof(u32); 466 cntr++; 467 #if 0 /* Use spinning wheel */ 468 /* return if spinning is due */ 469 if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break; 470 #endif 471 if (cntr > 1024 && part->erasesize > 0) { /* 4k */ 472 /* round up to next erase block border */ 473 (u32)offset |= part->erasesize-1; 474 offset++; 475 cntr = 0; 476 } 477 } 478 return offset - part->offset; 479 } 480 481 static u32 482 jffs_init_1pass_list(struct part_info *part) 483 { 484 struct b_lists *pL; 485 486 if (part->jffs2_priv != NULL) { 487 pL = (struct b_lists *)part->jffs2_priv; 488 free_nodes(&pL->frag); 489 free_nodes(&pL->dir); 490 free(pL); 491 } 492 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) { 493 pL = (struct b_lists *)part->jffs2_priv; 494 495 memset(pL, 0, sizeof(*pL)); 496 #ifdef CFG_JFFS2_SORT_FRAGMENTS 497 pL->dir.listCompare = compare_dirents; 498 pL->frag.listCompare = compare_inodes; 499 #endif 500 } 501 return 0; 502 } 503 504 /* find the inode from the slashless name given a parent */ 505 static long 506 jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest) 507 { 508 struct b_node *b; 509 struct jffs2_raw_inode *jNode; 510 u32 totalSize = 0; 511 u32 latestVersion = 0; 512 char *lDest; 513 char *src; 514 long ret; 515 int i; 516 u32 counter = 0; 517 #ifdef CFG_JFFS2_SORT_FRAGMENTS 518 /* Find file size before loading any data, so fragments that 519 * start past the end of file can be ignored. A fragment 520 * that is partially in the file is loaded, so extra data may 521 * be loaded up to the next 4K boundary above the file size. 522 * This shouldn't cause trouble when loading kernel images, so 523 * we will live with it. 524 */ 525 for (b = pL->frag.listHead; b != NULL; b = b->next) { 526 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, 527 sizeof(struct jffs2_raw_inode), NULL); 528 if ((inode == jNode->ino)) { 529 /* get actual file length from the newest node */ 530 if (jNode->version >= latestVersion) { 531 totalSize = jNode->isize; 532 latestVersion = jNode->version; 533 } 534 } 535 put_fl_mem(jNode); 536 } 537 #endif 538 539 for (b = pL->frag.listHead; b != NULL; b = b->next) { 540 jNode = (struct jffs2_raw_inode *) get_node_mem(b->offset); 541 if ((inode == jNode->ino)) { 542 #if 0 543 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen); 544 putLabeledWord("read_inode: inode = ", jNode->ino); 545 putLabeledWord("read_inode: version = ", jNode->version); 546 putLabeledWord("read_inode: isize = ", jNode->isize); 547 putLabeledWord("read_inode: offset = ", jNode->offset); 548 putLabeledWord("read_inode: csize = ", jNode->csize); 549 putLabeledWord("read_inode: dsize = ", jNode->dsize); 550 putLabeledWord("read_inode: compr = ", jNode->compr); 551 putLabeledWord("read_inode: usercompr = ", jNode->usercompr); 552 putLabeledWord("read_inode: flags = ", jNode->flags); 553 #endif 554 555 #ifndef CFG_JFFS2_SORT_FRAGMENTS 556 /* get actual file length from the newest node */ 557 if (jNode->version >= latestVersion) { 558 totalSize = jNode->isize; 559 latestVersion = jNode->version; 560 } 561 #endif 562 563 if(dest) { 564 src = ((char *) jNode) + sizeof(struct jffs2_raw_inode); 565 /* ignore data behind latest known EOF */ 566 if (jNode->offset > totalSize) { 567 put_fl_mem(jNode); 568 continue; 569 } 570 571 lDest = (char *) (dest + jNode->offset); 572 #if 0 573 putLabeledWord("read_inode: src = ", src); 574 putLabeledWord("read_inode: dest = ", lDest); 575 #endif 576 switch (jNode->compr) { 577 case JFFS2_COMPR_NONE: 578 ret = (unsigned long) ldr_memcpy(lDest, src, jNode->dsize); 579 break; 580 case JFFS2_COMPR_ZERO: 581 ret = 0; 582 for (i = 0; i < jNode->dsize; i++) 583 *(lDest++) = 0; 584 break; 585 case JFFS2_COMPR_RTIME: 586 ret = 0; 587 rtime_decompress(src, lDest, jNode->csize, jNode->dsize); 588 break; 589 case JFFS2_COMPR_DYNRUBIN: 590 /* this is slow but it works */ 591 ret = 0; 592 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize); 593 break; 594 case JFFS2_COMPR_ZLIB: 595 ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize); 596 break; 597 default: 598 /* unknown */ 599 putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr); 600 put_fl_mem(jNode); 601 return -1; 602 break; 603 } 604 } 605 606 #if 0 607 putLabeledWord("read_inode: totalSize = ", totalSize); 608 putLabeledWord("read_inode: compr ret = ", ret); 609 #endif 610 } 611 counter++; 612 put_fl_mem(jNode); 613 } 614 615 #if 0 616 putLabeledWord("read_inode: returning = ", totalSize); 617 #endif 618 return totalSize; 619 } 620 621 /* find the inode from the slashless name given a parent */ 622 static u32 623 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino) 624 { 625 struct b_node *b; 626 struct jffs2_raw_dirent *jDir; 627 int len; 628 u32 counter; 629 u32 version = 0; 630 u32 inode = 0; 631 632 /* name is assumed slash free */ 633 len = strlen(name); 634 635 counter = 0; 636 /* we need to search all and return the inode with the highest version */ 637 for(b = pL->dir.listHead; b; b = b->next, counter++) { 638 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); 639 if ((pino == jDir->pino) && (len == jDir->nsize) && 640 (jDir->ino) && /* 0 for unlink */ 641 (!strncmp(jDir->name, name, len))) { /* a match */ 642 if (jDir->version < version) { 643 put_fl_mem(jDir); 644 continue; 645 } 646 647 if (jDir->version == version && inode != 0) { 648 /* I'm pretty sure this isn't legal */ 649 putstr(" ** ERROR ** "); 650 putnstr(jDir->name, jDir->nsize); 651 putLabeledWord(" has dup version =", version); 652 } 653 inode = jDir->ino; 654 version = jDir->version; 655 } 656 #if 0 657 putstr("\r\nfind_inode:p&l ->"); 658 putnstr(jDir->name, jDir->nsize); 659 putstr("\r\n"); 660 putLabeledWord("pino = ", jDir->pino); 661 putLabeledWord("nsize = ", jDir->nsize); 662 putLabeledWord("b = ", (u32) b); 663 putLabeledWord("counter = ", counter); 664 #endif 665 put_fl_mem(jDir); 666 } 667 return inode; 668 } 669 670 char *mkmodestr(unsigned long mode, char *str) 671 { 672 static const char *l = "xwr"; 673 int mask = 1, i; 674 char c; 675 676 switch (mode & S_IFMT) { 677 case S_IFDIR: str[0] = 'd'; break; 678 case S_IFBLK: str[0] = 'b'; break; 679 case S_IFCHR: str[0] = 'c'; break; 680 case S_IFIFO: str[0] = 'f'; break; 681 case S_IFLNK: str[0] = 'l'; break; 682 case S_IFSOCK: str[0] = 's'; break; 683 case S_IFREG: str[0] = '-'; break; 684 default: str[0] = '?'; 685 } 686 687 for(i = 0; i < 9; i++) { 688 c = l[i%3]; 689 str[9-i] = (mode & mask)?c:'-'; 690 mask = mask<<1; 691 } 692 693 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S'; 694 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S'; 695 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T'; 696 str[10] = '\0'; 697 return str; 698 } 699 700 static inline void dump_stat(struct stat *st, const char *name) 701 { 702 char str[20]; 703 char s[64], *p; 704 705 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */ 706 st->st_mtime = 1; 707 708 ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */ 709 710 if ((p = strchr(s,'\n')) != NULL) *p = '\0'; 711 if ((p = strchr(s,'\r')) != NULL) *p = '\0'; 712 713 /* 714 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str), 715 st->st_size, s, name); 716 */ 717 718 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name); 719 } 720 721 static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i) 722 { 723 char fname[256]; 724 struct stat st; 725 726 if(!d || !i) return -1; 727 728 strncpy(fname, d->name, d->nsize); 729 fname[d->nsize] = '\0'; 730 731 memset(&st,0,sizeof(st)); 732 733 st.st_mtime = i->mtime; 734 st.st_mode = i->mode; 735 st.st_ino = i->ino; 736 737 /* neither dsize nor isize help us.. do it the long way */ 738 st.st_size = jffs2_1pass_read_inode(pL, i->ino, NULL); 739 740 dump_stat(&st, fname); 741 742 if (d->type == DT_LNK) { 743 unsigned char *src = (unsigned char *) (&i[1]); 744 putstr(" -> "); 745 putnstr(src, (int)i->dsize); 746 } 747 748 putstr("\r\n"); 749 750 return 0; 751 } 752 753 /* list inodes with the given pino */ 754 static u32 755 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino) 756 { 757 struct b_node *b; 758 struct jffs2_raw_dirent *jDir; 759 760 for (b = pL->dir.listHead; b; b = b->next) { 761 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); 762 if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */ 763 u32 i_version = 0; 764 struct jffs2_raw_inode ojNode; 765 struct jffs2_raw_inode *jNode, *i = NULL; 766 struct b_node *b2 = pL->frag.listHead; 767 768 while (b2) { 769 jNode = (struct jffs2_raw_inode *) 770 get_fl_mem(b2->offset, sizeof(ojNode), &ojNode); 771 if (jNode->ino == jDir->ino 772 && jNode->version >= i_version) 773 i = get_fl_mem(b2->offset, sizeof(*i), NULL); 774 b2 = b2->next; 775 } 776 777 dump_inode(pL, jDir, i); 778 put_fl_mem(i); 779 } 780 put_fl_mem(jDir); 781 } 782 return pino; 783 } 784 785 static u32 786 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino) 787 { 788 int i; 789 char tmp[256]; 790 char working_tmp[256]; 791 char *c; 792 793 /* discard any leading slash */ 794 i = 0; 795 while (fname[i] == '/') 796 i++; 797 strcpy(tmp, &fname[i]); 798 799 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ 800 { 801 strncpy(working_tmp, tmp, c - tmp); 802 working_tmp[c - tmp] = '\0'; 803 #if 0 804 putstr("search_inode: tmp = "); 805 putstr(tmp); 806 putstr("\r\n"); 807 putstr("search_inode: wtmp = "); 808 putstr(working_tmp); 809 putstr("\r\n"); 810 putstr("search_inode: c = "); 811 putstr(c); 812 putstr("\r\n"); 813 #endif 814 for (i = 0; i < strlen(c) - 1; i++) 815 tmp[i] = c[i + 1]; 816 tmp[i] = '\0'; 817 #if 0 818 putstr("search_inode: post tmp = "); 819 putstr(tmp); 820 putstr("\r\n"); 821 #endif 822 823 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) { 824 putstr("find_inode failed for name="); 825 putstr(working_tmp); 826 putstr("\r\n"); 827 return 0; 828 } 829 } 830 /* this is for the bare filename, directories have already been mapped */ 831 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { 832 putstr("find_inode failed for name="); 833 putstr(tmp); 834 putstr("\r\n"); 835 return 0; 836 } 837 return pino; 838 839 } 840 841 static u32 842 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino) 843 { 844 struct b_node *b; 845 struct b_node *b2; 846 struct jffs2_raw_dirent *jDir; 847 struct jffs2_raw_inode *jNode; 848 u8 jDirFoundType = 0; 849 u32 jDirFoundIno = 0; 850 u32 jDirFoundPino = 0; 851 char tmp[256]; 852 u32 version = 0; 853 u32 pino; 854 unsigned char *src; 855 856 /* we need to search all and return the inode with the highest version */ 857 for(b = pL->dir.listHead; b; b = b->next) { 858 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); 859 if (ino == jDir->ino) { 860 if (jDir->version < version) { 861 put_fl_mem(jDir); 862 continue; 863 } 864 865 if (jDir->version == version && jDirFoundType) { 866 /* I'm pretty sure this isn't legal */ 867 putstr(" ** ERROR ** "); 868 putnstr(jDir->name, jDir->nsize); 869 putLabeledWord(" has dup version (resolve) = ", 870 version); 871 } 872 873 jDirFoundType = jDir->type; 874 jDirFoundIno = jDir->ino; 875 jDirFoundPino = jDir->pino; 876 version = jDir->version; 877 } 878 put_fl_mem(jDir); 879 } 880 /* now we found the right entry again. (shoulda returned inode*) */ 881 if (jDirFoundType != DT_LNK) 882 return jDirFoundIno; 883 884 /* it's a soft link so we follow it again. */ 885 b2 = pL->frag.listHead; 886 while (b2) { 887 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset); 888 if (jNode->ino == jDirFoundIno) { 889 src = (unsigned char *) (b2->offset + sizeof(struct jffs2_raw_inode)); 890 891 #if 0 892 putLabeledWord("\t\t dsize = ", jNode->dsize); 893 putstr("\t\t target = "); 894 putnstr(src, jNode->dsize); 895 putstr("\r\n"); 896 #endif 897 strncpy(tmp, src, jNode->dsize); 898 tmp[jNode->dsize] = '\0'; 899 put_fl_mem(jNode); 900 break; 901 } 902 b2 = b2->next; 903 put_fl_mem(jNode); 904 } 905 /* ok so the name of the new file to find is in tmp */ 906 /* if it starts with a slash it is root based else shared dirs */ 907 if (tmp[0] == '/') 908 pino = 1; 909 else 910 pino = jDirFoundPino; 911 912 return jffs2_1pass_search_inode(pL, tmp, pino); 913 } 914 915 static u32 916 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino) 917 { 918 int i; 919 char tmp[256]; 920 char working_tmp[256]; 921 char *c; 922 923 /* discard any leading slash */ 924 i = 0; 925 while (fname[i] == '/') 926 i++; 927 strcpy(tmp, &fname[i]); 928 working_tmp[0] = '\0'; 929 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ 930 { 931 strncpy(working_tmp, tmp, c - tmp); 932 working_tmp[c - tmp] = '\0'; 933 for (i = 0; i < strlen(c) - 1; i++) 934 tmp[i] = c[i + 1]; 935 tmp[i] = '\0'; 936 937 WATCHDOG_RESET(); 938 939 /* only a failure if we arent looking at top level */ 940 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) && 941 (working_tmp[0])) { 942 putstr("find_inode failed for name="); 943 putstr(working_tmp); 944 putstr("\r\n"); 945 return 0; 946 } 947 } 948 949 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { 950 putstr("find_inode failed for name="); 951 putstr(tmp); 952 putstr("\r\n"); 953 return 0; 954 } 955 /* this is for the bare filename, directories have already been mapped */ 956 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) { 957 putstr("find_inode failed for name="); 958 putstr(tmp); 959 putstr("\r\n"); 960 return 0; 961 } 962 return pino; 963 964 } 965 966 unsigned char 967 jffs2_1pass_rescan_needed(struct part_info *part) 968 { 969 struct b_node *b; 970 struct jffs2_unknown_node onode; 971 struct jffs2_unknown_node *node; 972 struct b_lists *pL = (struct b_lists *)part->jffs2_priv; 973 974 if (part->jffs2_priv == 0){ 975 DEBUGF ("rescan: First time in use\n"); 976 return 1; 977 } 978 /* if we have no list, we need to rescan */ 979 if (pL->frag.listCount == 0) { 980 DEBUGF ("rescan: fraglist zero\n"); 981 return 1; 982 } 983 984 /* or if we are scanning a new partition */ 985 if (pL->partOffset != part->offset) { 986 DEBUGF ("rescan: different partition\n"); 987 return 1; 988 } 989 990 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) 991 if (nanddev != (int)part->usr_priv - 1) { 992 DEBUGF ("rescan: nand device changed\n"); 993 return -1; 994 } 995 #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ 996 997 /* but suppose someone reflashed a partition at the same offset... */ 998 b = pL->dir.listHead; 999 while (b) { 1000 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset, 1001 sizeof(onode), &onode); 1002 if (node->nodetype != JFFS2_NODETYPE_DIRENT) { 1003 DEBUGF ("rescan: fs changed beneath me? (%lx)\n", 1004 (unsigned long) b->offset); 1005 return 1; 1006 } 1007 b = b->next; 1008 } 1009 return 0; 1010 } 1011 1012 #ifdef DEBUG_FRAGMENTS 1013 static void 1014 dump_fragments(struct b_lists *pL) 1015 { 1016 struct b_node *b; 1017 struct jffs2_raw_inode ojNode; 1018 struct jffs2_raw_inode *jNode; 1019 1020 putstr("\r\n\r\n******The fragment Entries******\r\n"); 1021 b = pL->frag.listHead; 1022 while (b) { 1023 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, 1024 sizeof(ojNode), &ojNode); 1025 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset); 1026 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen); 1027 putLabeledWord("\tbuild_list: inode = ", jNode->ino); 1028 putLabeledWord("\tbuild_list: version = ", jNode->version); 1029 putLabeledWord("\tbuild_list: isize = ", jNode->isize); 1030 putLabeledWord("\tbuild_list: atime = ", jNode->atime); 1031 putLabeledWord("\tbuild_list: offset = ", jNode->offset); 1032 putLabeledWord("\tbuild_list: csize = ", jNode->csize); 1033 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize); 1034 putLabeledWord("\tbuild_list: compr = ", jNode->compr); 1035 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr); 1036 putLabeledWord("\tbuild_list: flags = ", jNode->flags); 1037 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ 1038 b = b->next; 1039 } 1040 } 1041 #endif 1042 1043 #ifdef DEBUG_DIRENTS 1044 static void 1045 dump_dirents(struct b_lists *pL) 1046 { 1047 struct b_node *b; 1048 struct jffs2_raw_dirent *jDir; 1049 1050 putstr("\r\n\r\n******The directory Entries******\r\n"); 1051 b = pL->dir.listHead; 1052 while (b) { 1053 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); 1054 putstr("\r\n"); 1055 putnstr(jDir->name, jDir->nsize); 1056 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic); 1057 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype); 1058 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc); 1059 putLabeledWord("\tbuild_list: pino = ", jDir->pino); 1060 putLabeledWord("\tbuild_list: version = ", jDir->version); 1061 putLabeledWord("\tbuild_list: ino = ", jDir->ino); 1062 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime); 1063 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize); 1064 putLabeledWord("\tbuild_list: type = ", jDir->type); 1065 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc); 1066 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc); 1067 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ 1068 b = b->next; 1069 put_fl_mem(jDir); 1070 } 1071 } 1072 #endif 1073 1074 static u32 1075 jffs2_1pass_build_lists(struct part_info * part) 1076 { 1077 struct b_lists *pL; 1078 struct jffs2_unknown_node *node; 1079 struct jffs2_unknown_node crcnode; 1080 u32 offset, oldoffset = 0; 1081 u32 max = part->size - sizeof(struct jffs2_raw_inode); 1082 u32 counter = 0; 1083 u32 counter4 = 0; 1084 u32 counterF = 0; 1085 u32 counterN = 0; 1086 u32 counterCRC = 0; 1087 u32 counterUNK = 0; 1088 1089 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) 1090 nanddev = (int)part->usr_priv - 1; 1091 #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ 1092 1093 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */ 1094 /* jffs2 list building enterprise nope. in newer versions the overhead is */ 1095 /* only about 5 %. not enough to inconvenience people for. */ 1096 /* lcd_off(); */ 1097 1098 /* if we are building a list we need to refresh the cache. */ 1099 jffs_init_1pass_list(part); 1100 pL = (struct b_lists *)part->jffs2_priv; 1101 pL->partOffset = part->offset; 1102 offset = 0; 1103 puts ("Scanning JFFS2 FS: "); 1104 1105 /* start at the beginning of the partition */ 1106 while (offset < max) { 1107 if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) { 1108 WATCHDOG_RESET(); 1109 #if 0 /* Use spinning wheel */ 1110 printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]); 1111 #endif 1112 oldoffset = offset; 1113 } 1114 node = (struct jffs2_unknown_node *) get_node_mem((u32)part->offset + offset); 1115 if (node->magic == JFFS2_MAGIC_BITMASK) { 1116 /* if its a fragment add it */ 1117 /* check crc by readding a JFFS2_NODE_ACCURATE */ 1118 crcnode.magic = node->magic; 1119 crcnode.nodetype = node->nodetype | JFFS2_NODE_ACCURATE; 1120 crcnode.totlen = node->totlen; 1121 crcnode.hdr_crc = node->hdr_crc; 1122 1123 if (!hdr_crc(&crcnode)) { 1124 offset += 4; 1125 counterCRC++; 1126 continue; 1127 } else if (node->nodetype == JFFS2_NODETYPE_INODE && 1128 inode_crc((struct jffs2_raw_inode *) node)) { 1129 if (insert_node(&pL->frag, (u32) part->offset + 1130 offset) == NULL) { 1131 put_fl_mem(node); 1132 return 0; 1133 } 1134 } else if (node->nodetype == JFFS2_NODETYPE_DIRENT && 1135 dirent_crc((struct jffs2_raw_dirent *) node) && 1136 dirent_name_crc((struct jffs2_raw_dirent *) node)) { 1137 if (! (counterN%128)) 1138 #if 0 /* Use spinning wheel */ 1139 puts ("\b\b. "); 1140 #else 1141 puts ("."); 1142 #endif 1143 if (insert_node(&pL->dir, (u32) part->offset + 1144 offset) == NULL) { 1145 put_fl_mem(node); 1146 return 0; 1147 } 1148 counterN++; 1149 } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) { 1150 if (node->totlen != sizeof(struct jffs2_unknown_node)) 1151 printf("OOPS Cleanmarker has bad size " 1152 "%d != %d\n", node->totlen, 1153 sizeof(struct jffs2_unknown_node)); 1154 } else if (node->nodetype == JFFS2_NODETYPE_PADDING) { 1155 if (node->totlen < sizeof(struct jffs2_unknown_node)) 1156 printf("OOPS Padding has bad size " 1157 "%d < %d\n", node->totlen, 1158 sizeof(struct jffs2_unknown_node)); 1159 } else { 1160 counterUNK++; 1161 } 1162 offset += ((node->totlen + 3) & ~3); 1163 counterF++; 1164 } else if (node->magic == JFFS2_EMPTY_BITMASK && 1165 node->nodetype == JFFS2_EMPTY_BITMASK) { 1166 offset = jffs2_scan_empty(offset, part); 1167 } else { /* if we know nothing, we just step and look. */ 1168 offset += 4; 1169 counter4++; 1170 } 1171 /* printf("unknown node magic %4.4x %4.4x @ %lx\n", node->magic, node->nodetype, (unsigned long)node); */ 1172 put_fl_mem(node); 1173 } 1174 1175 putstr("\b\b done.\r\n"); /* close off the dots */ 1176 /* turn the lcd back on. */ 1177 /* splash(); */ 1178 1179 #if 0 1180 putLabeledWord("dir entries = ", pL->dir.listCount); 1181 putLabeledWord("frag entries = ", pL->frag.listCount); 1182 putLabeledWord("+4 increments = ", counter4); 1183 putLabeledWord("+file_offset increments = ", counterF); 1184 putLabeledWord("Unknown node types = ", counterUNK); 1185 putLabeledWord("Bad hdr_crc = ", counterCRC); 1186 1187 #endif 1188 1189 #ifdef DEBUG_DIRENTS 1190 dump_dirents(pL); 1191 #endif 1192 1193 #ifdef DEBUG_FRAGMENTS 1194 dump_fragments(pL); 1195 #endif 1196 1197 /* give visual feedback that we are done scanning the flash */ 1198 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */ 1199 return 1; 1200 } 1201 1202 1203 static u32 1204 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL) 1205 { 1206 struct b_node *b; 1207 struct jffs2_raw_inode ojNode; 1208 struct jffs2_raw_inode *jNode; 1209 int i; 1210 1211 for (i = 0; i < JFFS2_NUM_COMPR; i++) { 1212 piL->compr_info[i].num_frags = 0; 1213 piL->compr_info[i].compr_sum = 0; 1214 piL->compr_info[i].decompr_sum = 0; 1215 } 1216 1217 b = pL->frag.listHead; 1218 while (b) { 1219 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, 1220 sizeof(ojNode), &ojNode); 1221 if (jNode->compr < JFFS2_NUM_COMPR) { 1222 piL->compr_info[jNode->compr].num_frags++; 1223 piL->compr_info[jNode->compr].compr_sum += jNode->csize; 1224 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize; 1225 } 1226 b = b->next; 1227 } 1228 return 0; 1229 } 1230 1231 1232 static struct b_lists * 1233 jffs2_get_list(struct part_info * part, const char *who) 1234 { 1235 if (jffs2_1pass_rescan_needed(part)) { 1236 if (!jffs2_1pass_build_lists(part)) { 1237 printf("%s: Failed to scan JFFSv2 file structure\n", who); 1238 return NULL; 1239 } 1240 } 1241 WATCHDOG_RESET(); 1242 return (struct b_lists *)part->jffs2_priv; 1243 } 1244 1245 1246 /* Print directory / file contents */ 1247 u32 1248 jffs2_1pass_ls(struct part_info * part, const char *fname) 1249 { 1250 struct b_lists *pl; 1251 u32 inode; 1252 1253 if (! (pl = jffs2_get_list(part, "ls"))) 1254 return 0; 1255 1256 1257 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) { 1258 putstr("ls: Failed to scan jffs2 file structure\r\n"); 1259 return 0; 1260 } 1261 #if 0 1262 putLabeledWord("found file at inode = ", inode); 1263 putLabeledWord("read_inode returns = ", ret); 1264 #endif 1265 return inode; 1266 } 1267 1268 1269 /* Load a file from flash into memory. fname can be a full path */ 1270 u32 1271 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname) 1272 { 1273 1274 struct b_lists *pl; 1275 long ret = 0; 1276 u32 inode; 1277 1278 if (! (pl = jffs2_get_list(part, "load"))) 1279 return 0; 1280 1281 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) { 1282 putstr("load: Failed to find inode\r\n"); 1283 return 0; 1284 } 1285 1286 /* Resolve symlinks */ 1287 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) { 1288 putstr("load: Failed to resolve inode structure\r\n"); 1289 return 0; 1290 } 1291 1292 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) { 1293 putstr("load: Failed to read inode\r\n"); 1294 return 0; 1295 } 1296 1297 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname, 1298 (unsigned long) dest, ret); 1299 return ret; 1300 } 1301 1302 /* Return information about the fs on this partition */ 1303 u32 1304 jffs2_1pass_info(struct part_info * part) 1305 { 1306 struct b_jffs2_info info; 1307 struct b_lists *pl; 1308 int i; 1309 1310 if (! (pl = jffs2_get_list(part, "info"))) 1311 return 0; 1312 1313 jffs2_1pass_fill_info(pl, &info); 1314 for (i = 0; i < JFFS2_NUM_COMPR; i++) { 1315 printf ("Compression: %s\n" 1316 "\tfrag count: %d\n" 1317 "\tcompressed sum: %d\n" 1318 "\tuncompressed sum: %d\n", 1319 compr_names[i], 1320 info.compr_info[i].num_frags, 1321 info.compr_info[i].compr_sum, 1322 info.compr_info[i].decompr_sum); 1323 } 1324 return 1; 1325 } 1326 1327 #endif /* CFG_CMD_JFFS2 */ 1328