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 CONFIG_SYS_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 <div64.h> 118 #include <linux/compiler.h> 119 #include <linux/stat.h> 120 #include <linux/time.h> 121 #include <watchdog.h> 122 #include <jffs2/jffs2.h> 123 #include <jffs2/jffs2_1pass.h> 124 #include <linux/compat.h> 125 #include <linux/errno.h> 126 #include <linux/mtd/mtd.h> 127 128 #include "jffs2_private.h" 129 130 131 #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */ 132 #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */ 133 134 /* Debugging switches */ 135 #undef DEBUG_DIRENTS /* print directory entry list after scan */ 136 #undef DEBUG_FRAGMENTS /* print fragment list after scan */ 137 #undef DEBUG /* enable debugging messages */ 138 139 140 #ifdef DEBUG 141 # define DEBUGF(fmt,args...) printf(fmt ,##args) 142 #else 143 # define DEBUGF(fmt,args...) 144 #endif 145 146 #include "summary.h" 147 148 /* keeps pointer to currentlu processed partition */ 149 static struct part_info *current_part; 150 151 #if (defined(CONFIG_JFFS2_NAND) && \ 152 defined(CONFIG_CMD_NAND) ) 153 #include <nand.h> 154 /* 155 * Support for jffs2 on top of NAND-flash 156 * 157 * NAND memory isn't mapped in processor's address space, 158 * so data should be fetched from flash before 159 * being processed. This is exactly what functions declared 160 * here do. 161 * 162 */ 163 164 #define NAND_PAGE_SIZE 512 165 #define NAND_PAGE_SHIFT 9 166 #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1)) 167 168 #ifndef NAND_CACHE_PAGES 169 #define NAND_CACHE_PAGES 16 170 #endif 171 #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE) 172 173 static u8* nand_cache = NULL; 174 static u32 nand_cache_off = (u32)-1; 175 176 static int read_nand_cached(u32 off, u32 size, u_char *buf) 177 { 178 struct mtdids *id = current_part->dev->id; 179 struct mtd_info *mtd; 180 u32 bytes_read = 0; 181 size_t retlen; 182 int cpy_bytes; 183 184 mtd = get_nand_dev_by_index(id->num); 185 if (!mtd) 186 return -1; 187 188 while (bytes_read < size) { 189 if ((off + bytes_read < nand_cache_off) || 190 (off + bytes_read >= nand_cache_off+NAND_CACHE_SIZE)) { 191 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK; 192 if (!nand_cache) { 193 /* This memory never gets freed but 'cause 194 it's a bootloader, nobody cares */ 195 nand_cache = malloc(NAND_CACHE_SIZE); 196 if (!nand_cache) { 197 printf("read_nand_cached: can't alloc cache size %d bytes\n", 198 NAND_CACHE_SIZE); 199 return -1; 200 } 201 } 202 203 retlen = NAND_CACHE_SIZE; 204 if (nand_read(mtd, nand_cache_off, 205 &retlen, nand_cache) != 0 || 206 retlen != NAND_CACHE_SIZE) { 207 printf("read_nand_cached: error reading nand off %#x size %d bytes\n", 208 nand_cache_off, NAND_CACHE_SIZE); 209 return -1; 210 } 211 } 212 cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read); 213 if (cpy_bytes > size - bytes_read) 214 cpy_bytes = size - bytes_read; 215 memcpy(buf + bytes_read, 216 nand_cache + off + bytes_read - nand_cache_off, 217 cpy_bytes); 218 bytes_read += cpy_bytes; 219 } 220 return bytes_read; 221 } 222 223 static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf) 224 { 225 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size); 226 227 if (NULL == buf) { 228 printf("get_fl_mem_nand: can't alloc %d bytes\n", size); 229 return NULL; 230 } 231 if (read_nand_cached(off, size, buf) < 0) { 232 if (!ext_buf) 233 free(buf); 234 return NULL; 235 } 236 237 return buf; 238 } 239 240 static void *get_node_mem_nand(u32 off, void *ext_buf) 241 { 242 struct jffs2_unknown_node node; 243 void *ret = NULL; 244 245 if (NULL == get_fl_mem_nand(off, sizeof(node), &node)) 246 return NULL; 247 248 if (!(ret = get_fl_mem_nand(off, node.magic == 249 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node), 250 ext_buf))) { 251 printf("off = %#x magic %#x type %#x node.totlen = %d\n", 252 off, node.magic, node.nodetype, node.totlen); 253 } 254 return ret; 255 } 256 257 static void put_fl_mem_nand(void *buf) 258 { 259 free(buf); 260 } 261 #endif 262 263 #if defined(CONFIG_CMD_ONENAND) 264 265 #include <linux/mtd/mtd.h> 266 #include <linux/mtd/onenand.h> 267 #include <onenand_uboot.h> 268 269 #define ONENAND_PAGE_SIZE 2048 270 #define ONENAND_PAGE_SHIFT 11 271 #define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1)) 272 273 #ifndef ONENAND_CACHE_PAGES 274 #define ONENAND_CACHE_PAGES 4 275 #endif 276 #define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE) 277 278 static u8* onenand_cache; 279 static u32 onenand_cache_off = (u32)-1; 280 281 static int read_onenand_cached(u32 off, u32 size, u_char *buf) 282 { 283 u32 bytes_read = 0; 284 size_t retlen; 285 int cpy_bytes; 286 287 while (bytes_read < size) { 288 if ((off + bytes_read < onenand_cache_off) || 289 (off + bytes_read >= onenand_cache_off + ONENAND_CACHE_SIZE)) { 290 onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK; 291 if (!onenand_cache) { 292 /* This memory never gets freed but 'cause 293 it's a bootloader, nobody cares */ 294 onenand_cache = malloc(ONENAND_CACHE_SIZE); 295 if (!onenand_cache) { 296 printf("read_onenand_cached: can't alloc cache size %d bytes\n", 297 ONENAND_CACHE_SIZE); 298 return -1; 299 } 300 } 301 302 retlen = ONENAND_CACHE_SIZE; 303 if (onenand_read(&onenand_mtd, onenand_cache_off, retlen, 304 &retlen, onenand_cache) != 0 || 305 retlen != ONENAND_CACHE_SIZE) { 306 printf("read_onenand_cached: error reading nand off %#x size %d bytes\n", 307 onenand_cache_off, ONENAND_CACHE_SIZE); 308 return -1; 309 } 310 } 311 cpy_bytes = onenand_cache_off + ONENAND_CACHE_SIZE - (off + bytes_read); 312 if (cpy_bytes > size - bytes_read) 313 cpy_bytes = size - bytes_read; 314 memcpy(buf + bytes_read, 315 onenand_cache + off + bytes_read - onenand_cache_off, 316 cpy_bytes); 317 bytes_read += cpy_bytes; 318 } 319 return bytes_read; 320 } 321 322 static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf) 323 { 324 u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size); 325 326 if (NULL == buf) { 327 printf("get_fl_mem_onenand: can't alloc %d bytes\n", size); 328 return NULL; 329 } 330 if (read_onenand_cached(off, size, buf) < 0) { 331 if (!ext_buf) 332 free(buf); 333 return NULL; 334 } 335 336 return buf; 337 } 338 339 static void *get_node_mem_onenand(u32 off, void *ext_buf) 340 { 341 struct jffs2_unknown_node node; 342 void *ret = NULL; 343 344 if (NULL == get_fl_mem_onenand(off, sizeof(node), &node)) 345 return NULL; 346 347 ret = get_fl_mem_onenand(off, node.magic == 348 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node), 349 ext_buf); 350 if (!ret) { 351 printf("off = %#x magic %#x type %#x node.totlen = %d\n", 352 off, node.magic, node.nodetype, node.totlen); 353 } 354 return ret; 355 } 356 357 358 static void put_fl_mem_onenand(void *buf) 359 { 360 free(buf); 361 } 362 #endif 363 364 365 #if defined(CONFIG_CMD_FLASH) 366 /* 367 * Support for jffs2 on top of NOR-flash 368 * 369 * NOR flash memory is mapped in processor's address space, 370 * just return address. 371 */ 372 static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf) 373 { 374 u32 addr = off; 375 struct mtdids *id = current_part->dev->id; 376 377 extern flash_info_t flash_info[]; 378 flash_info_t *flash = &flash_info[id->num]; 379 380 addr += flash->start[0]; 381 if (ext_buf) { 382 memcpy(ext_buf, (void *)(long)addr, size); 383 return ext_buf; 384 } 385 return (void*)(long)addr; 386 } 387 388 static inline void *get_node_mem_nor(u32 off, void *ext_buf) 389 { 390 struct jffs2_unknown_node *pNode; 391 392 /* pNode will point directly to flash - don't provide external buffer 393 and don't care about size */ 394 pNode = get_fl_mem_nor(off, 0, NULL); 395 return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ? 396 pNode->totlen : sizeof(*pNode), ext_buf); 397 } 398 #else 399 static void *get_fl_mem_norflash(u32 off, u32 size, void *ext_buf) 400 { 401 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size); 402 struct mtd_info *mtd; 403 size_t retlen; 404 405 mtd = get_mtd_device_nm(CONFIG_JFFS2_DEV); 406 if (!mtd) 407 return (void *)-1; 408 409 if (NULL == buf) { 410 printf("get_fl_mem_norflash: can't alloc %d bytes\n", size); 411 return NULL; 412 } 413 if (mtd_read(mtd, off, size, &retlen, buf) < 0) { 414 if (!ext_buf) 415 free(buf); 416 return NULL; 417 } 418 419 return buf; 420 } 421 422 static void *get_node_mem_norflash(u32 off, void *ext_buf) 423 { 424 struct jffs2_unknown_node node; 425 void *ret = NULL; 426 427 if (NULL == get_fl_mem_norflash(off, sizeof(node), &node)) 428 return NULL; 429 430 if (!(ret = get_fl_mem_norflash(off, node.magic == 431 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node), 432 ext_buf))) { 433 printf("off = %#x magic %#x type %#x node.totlen = %d\n", 434 off, node.magic, node.nodetype, node.totlen); 435 } 436 return ret; 437 } 438 #endif 439 440 441 /* 442 * Generic jffs2 raw memory and node read routines. 443 * 444 */ 445 static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf) 446 { 447 struct mtdids *id = current_part->dev->id; 448 449 switch(id->type) { 450 #if defined(CONFIG_CMD_FLASH) 451 case MTD_DEV_TYPE_NOR: 452 return get_fl_mem_nor(off, size, ext_buf); 453 break; 454 #else 455 case MTD_DEV_TYPE_NOR: 456 return get_fl_mem_norflash(off, size, ext_buf); 457 break; 458 #endif 459 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) 460 case MTD_DEV_TYPE_NAND: 461 return get_fl_mem_nand(off, size, ext_buf); 462 break; 463 #endif 464 #if defined(CONFIG_CMD_ONENAND) 465 case MTD_DEV_TYPE_ONENAND: 466 return get_fl_mem_onenand(off, size, ext_buf); 467 break; 468 #endif 469 default: 470 printf("get_fl_mem: unknown device type, " \ 471 "using raw offset!\n"); 472 } 473 return (void*)(long)off; 474 } 475 476 static inline void *get_node_mem(u32 off, void *ext_buf) 477 { 478 struct mtdids *id = current_part->dev->id; 479 480 switch(id->type) { 481 #if defined(CONFIG_CMD_FLASH) 482 case MTD_DEV_TYPE_NOR: 483 return get_node_mem_nor(off, ext_buf); 484 break; 485 #else 486 case MTD_DEV_TYPE_NOR: 487 return get_node_mem_norflash(off, ext_buf); 488 break; 489 #endif 490 #if defined(CONFIG_JFFS2_NAND) && \ 491 defined(CONFIG_CMD_NAND) 492 case MTD_DEV_TYPE_NAND: 493 return get_node_mem_nand(off, ext_buf); 494 break; 495 #endif 496 #if defined(CONFIG_CMD_ONENAND) 497 case MTD_DEV_TYPE_ONENAND: 498 return get_node_mem_onenand(off, ext_buf); 499 break; 500 #endif 501 default: 502 printf("get_fl_mem: unknown device type, " \ 503 "using raw offset!\n"); 504 } 505 return (void*)(long)off; 506 } 507 508 static inline void put_fl_mem(void *buf, void *ext_buf) 509 { 510 struct mtdids *id = current_part->dev->id; 511 512 /* If buf is the same as ext_buf, it was provided by the caller - 513 we shouldn't free it then. */ 514 if (buf == ext_buf) 515 return; 516 switch (id->type) { 517 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) 518 case MTD_DEV_TYPE_NAND: 519 return put_fl_mem_nand(buf); 520 #endif 521 #if defined(CONFIG_CMD_ONENAND) 522 case MTD_DEV_TYPE_ONENAND: 523 return put_fl_mem_onenand(buf); 524 #endif 525 } 526 } 527 528 /* Compression names */ 529 static char *compr_names[] = { 530 "NONE", 531 "ZERO", 532 "RTIME", 533 "RUBINMIPS", 534 "COPY", 535 "DYNRUBIN", 536 "ZLIB", 537 #if defined(CONFIG_JFFS2_LZO) 538 "LZO", 539 #endif 540 }; 541 542 /* Memory management */ 543 struct mem_block { 544 u32 index; 545 struct mem_block *next; 546 struct b_node nodes[NODE_CHUNK]; 547 }; 548 549 550 static void 551 free_nodes(struct b_list *list) 552 { 553 while (list->listMemBase != NULL) { 554 struct mem_block *next = list->listMemBase->next; 555 free( list->listMemBase ); 556 list->listMemBase = next; 557 } 558 } 559 560 static struct b_node * 561 add_node(struct b_list *list) 562 { 563 u32 index = 0; 564 struct mem_block *memBase; 565 struct b_node *b; 566 567 memBase = list->listMemBase; 568 if (memBase != NULL) 569 index = memBase->index; 570 #if 0 571 putLabeledWord("add_node: index = ", index); 572 putLabeledWord("add_node: memBase = ", list->listMemBase); 573 #endif 574 575 if (memBase == NULL || index >= NODE_CHUNK) { 576 /* we need more space before we continue */ 577 memBase = mmalloc(sizeof(struct mem_block)); 578 if (memBase == NULL) { 579 putstr("add_node: malloc failed\n"); 580 return NULL; 581 } 582 memBase->next = list->listMemBase; 583 index = 0; 584 #if 0 585 putLabeledWord("add_node: alloced a new membase at ", *memBase); 586 #endif 587 588 } 589 /* now we have room to add it. */ 590 b = &memBase->nodes[index]; 591 index ++; 592 593 memBase->index = index; 594 list->listMemBase = memBase; 595 list->listCount++; 596 return b; 597 } 598 599 static struct b_node * 600 insert_node(struct b_list *list, u32 offset) 601 { 602 struct b_node *new; 603 604 if (!(new = add_node(list))) { 605 putstr("add_node failed!\r\n"); 606 return NULL; 607 } 608 new->offset = offset; 609 new->next = NULL; 610 611 if (list->listTail != NULL) 612 list->listTail->next = new; 613 else 614 list->listHead = new; 615 list->listTail = new; 616 617 return new; 618 } 619 620 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS 621 /* Sort data entries with the latest version last, so that if there 622 * is overlapping data the latest version will be used. 623 */ 624 static int compare_inodes(struct b_node *new, struct b_node *old) 625 { 626 /* 627 * Only read in the version info from flash, not the entire inode. 628 * This can make a big difference to speed if flash is slow. 629 */ 630 u32 new_version; 631 u32 old_version; 632 get_fl_mem(new->offset + offsetof(struct jffs2_raw_inode, version), 633 sizeof(new_version), &new_version); 634 get_fl_mem(old->offset + offsetof(struct jffs2_raw_inode, version), 635 sizeof(old_version), &old_version); 636 637 return new_version > old_version; 638 } 639 640 /* Sort directory entries so all entries in the same directory 641 * with the same name are grouped together, with the latest version 642 * last. This makes it easy to eliminate all but the latest version 643 * by marking the previous version dead by setting the inode to 0. 644 */ 645 static int compare_dirents(struct b_node *new, struct b_node *old) 646 { 647 /* 648 * Using NULL as the buffer for NOR flash prevents the entire node 649 * being read. This makes most comparisons much quicker as only one 650 * or two entries from the node will be used most of the time. 651 */ 652 struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL); 653 struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL); 654 int cmp; 655 int ret; 656 657 if (jNew->pino != jOld->pino) { 658 /* ascending sort by pino */ 659 ret = jNew->pino > jOld->pino; 660 } else if (jNew->nsize != jOld->nsize) { 661 /* 662 * pino is the same, so use ascending sort by nsize, 663 * so we don't do strncmp unless we really must. 664 */ 665 ret = jNew->nsize > jOld->nsize; 666 } else { 667 /* 668 * length is also the same, so use ascending sort by name 669 */ 670 cmp = strncmp((char *)jNew->name, (char *)jOld->name, 671 jNew->nsize); 672 if (cmp != 0) { 673 ret = cmp > 0; 674 } else { 675 /* 676 * we have duplicate names in this directory, 677 * so use ascending sort by version 678 */ 679 ret = jNew->version > jOld->version; 680 } 681 } 682 put_fl_mem(jNew, NULL); 683 put_fl_mem(jOld, NULL); 684 685 return ret; 686 } 687 #endif 688 689 void 690 jffs2_free_cache(struct part_info *part) 691 { 692 struct b_lists *pL; 693 694 if (part->jffs2_priv != NULL) { 695 pL = (struct b_lists *)part->jffs2_priv; 696 free_nodes(&pL->frag); 697 free_nodes(&pL->dir); 698 free(pL->readbuf); 699 free(pL); 700 } 701 } 702 703 static u32 704 jffs_init_1pass_list(struct part_info *part) 705 { 706 struct b_lists *pL; 707 708 jffs2_free_cache(part); 709 710 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) { 711 pL = (struct b_lists *)part->jffs2_priv; 712 713 memset(pL, 0, sizeof(*pL)); 714 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS 715 pL->dir.listCompare = compare_dirents; 716 pL->frag.listCompare = compare_inodes; 717 #endif 718 } 719 return 0; 720 } 721 722 /* find the inode from the slashless name given a parent */ 723 static long 724 jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest) 725 { 726 struct b_node *b; 727 struct jffs2_raw_inode *jNode; 728 u32 totalSize = 0; 729 u32 latestVersion = 0; 730 uchar *lDest; 731 uchar *src; 732 int i; 733 u32 counter = 0; 734 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS 735 /* Find file size before loading any data, so fragments that 736 * start past the end of file can be ignored. A fragment 737 * that is partially in the file is loaded, so extra data may 738 * be loaded up to the next 4K boundary above the file size. 739 * This shouldn't cause trouble when loading kernel images, so 740 * we will live with it. 741 */ 742 for (b = pL->frag.listHead; b != NULL; b = b->next) { 743 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, 744 sizeof(struct jffs2_raw_inode), pL->readbuf); 745 if ((inode == jNode->ino)) { 746 /* get actual file length from the newest node */ 747 if (jNode->version >= latestVersion) { 748 totalSize = jNode->isize; 749 latestVersion = jNode->version; 750 } 751 } 752 put_fl_mem(jNode, pL->readbuf); 753 } 754 /* 755 * If no destination is provided, we are done. 756 * Just return the total size. 757 */ 758 if (!dest) 759 return totalSize; 760 #endif 761 762 for (b = pL->frag.listHead; b != NULL; b = b->next) { 763 /* 764 * Copy just the node and not the data at this point, 765 * since we don't yet know if we need this data. 766 */ 767 jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset, 768 sizeof(struct jffs2_raw_inode), 769 pL->readbuf); 770 if (inode == jNode->ino) { 771 #if 0 772 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen); 773 putLabeledWord("read_inode: inode = ", jNode->ino); 774 putLabeledWord("read_inode: version = ", jNode->version); 775 putLabeledWord("read_inode: isize = ", jNode->isize); 776 putLabeledWord("read_inode: offset = ", jNode->offset); 777 putLabeledWord("read_inode: csize = ", jNode->csize); 778 putLabeledWord("read_inode: dsize = ", jNode->dsize); 779 putLabeledWord("read_inode: compr = ", jNode->compr); 780 putLabeledWord("read_inode: usercompr = ", jNode->usercompr); 781 putLabeledWord("read_inode: flags = ", jNode->flags); 782 #endif 783 784 #ifndef CONFIG_SYS_JFFS2_SORT_FRAGMENTS 785 /* get actual file length from the newest node */ 786 if (jNode->version >= latestVersion) { 787 totalSize = jNode->isize; 788 latestVersion = jNode->version; 789 } 790 #endif 791 792 if(dest) { 793 /* 794 * Now that the inode has been checked, 795 * read the entire inode, including data. 796 */ 797 put_fl_mem(jNode, pL->readbuf); 798 jNode = (struct jffs2_raw_inode *) 799 get_node_mem(b->offset, pL->readbuf); 800 src = ((uchar *)jNode) + 801 sizeof(struct jffs2_raw_inode); 802 /* ignore data behind latest known EOF */ 803 if (jNode->offset > totalSize) { 804 put_fl_mem(jNode, pL->readbuf); 805 continue; 806 } 807 if (b->datacrc == CRC_UNKNOWN) 808 b->datacrc = data_crc(jNode) ? 809 CRC_OK : CRC_BAD; 810 if (b->datacrc == CRC_BAD) { 811 put_fl_mem(jNode, pL->readbuf); 812 continue; 813 } 814 815 lDest = (uchar *) (dest + jNode->offset); 816 #if 0 817 putLabeledWord("read_inode: src = ", src); 818 putLabeledWord("read_inode: dest = ", lDest); 819 #endif 820 switch (jNode->compr) { 821 case JFFS2_COMPR_NONE: 822 ldr_memcpy(lDest, src, jNode->dsize); 823 break; 824 case JFFS2_COMPR_ZERO: 825 for (i = 0; i < jNode->dsize; i++) 826 *(lDest++) = 0; 827 break; 828 case JFFS2_COMPR_RTIME: 829 rtime_decompress(src, lDest, jNode->csize, jNode->dsize); 830 break; 831 case JFFS2_COMPR_DYNRUBIN: 832 /* this is slow but it works */ 833 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize); 834 break; 835 case JFFS2_COMPR_ZLIB: 836 zlib_decompress(src, lDest, jNode->csize, jNode->dsize); 837 break; 838 #if defined(CONFIG_JFFS2_LZO) 839 case JFFS2_COMPR_LZO: 840 lzo_decompress(src, lDest, jNode->csize, jNode->dsize); 841 break; 842 #endif 843 default: 844 /* unknown */ 845 putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr); 846 put_fl_mem(jNode, pL->readbuf); 847 return -1; 848 break; 849 } 850 } 851 852 #if 0 853 putLabeledWord("read_inode: totalSize = ", totalSize); 854 #endif 855 } 856 counter++; 857 put_fl_mem(jNode, pL->readbuf); 858 } 859 860 #if 0 861 putLabeledWord("read_inode: returning = ", totalSize); 862 #endif 863 return totalSize; 864 } 865 866 /* find the inode from the slashless name given a parent */ 867 static u32 868 jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino) 869 { 870 struct b_node *b; 871 struct jffs2_raw_dirent *jDir; 872 int len; 873 u32 counter; 874 u32 version = 0; 875 u32 inode = 0; 876 877 /* name is assumed slash free */ 878 len = strlen(name); 879 880 counter = 0; 881 /* we need to search all and return the inode with the highest version */ 882 for(b = pL->dir.listHead; b; b = b->next, counter++) { 883 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, 884 pL->readbuf); 885 if ((pino == jDir->pino) && (len == jDir->nsize) && 886 (!strncmp((char *)jDir->name, name, len))) { /* a match */ 887 if (jDir->version < version) { 888 put_fl_mem(jDir, pL->readbuf); 889 continue; 890 } 891 892 if (jDir->version == version && inode != 0) { 893 /* I'm pretty sure this isn't legal */ 894 putstr(" ** ERROR ** "); 895 putnstr(jDir->name, jDir->nsize); 896 putLabeledWord(" has dup version =", version); 897 } 898 inode = jDir->ino; 899 version = jDir->version; 900 } 901 #if 0 902 putstr("\r\nfind_inode:p&l ->"); 903 putnstr(jDir->name, jDir->nsize); 904 putstr("\r\n"); 905 putLabeledWord("pino = ", jDir->pino); 906 putLabeledWord("nsize = ", jDir->nsize); 907 putLabeledWord("b = ", (u32) b); 908 putLabeledWord("counter = ", counter); 909 #endif 910 put_fl_mem(jDir, pL->readbuf); 911 } 912 return inode; 913 } 914 915 char *mkmodestr(unsigned long mode, char *str) 916 { 917 static const char *l = "xwr"; 918 int mask = 1, i; 919 char c; 920 921 switch (mode & S_IFMT) { 922 case S_IFDIR: str[0] = 'd'; break; 923 case S_IFBLK: str[0] = 'b'; break; 924 case S_IFCHR: str[0] = 'c'; break; 925 case S_IFIFO: str[0] = 'f'; break; 926 case S_IFLNK: str[0] = 'l'; break; 927 case S_IFSOCK: str[0] = 's'; break; 928 case S_IFREG: str[0] = '-'; break; 929 default: str[0] = '?'; 930 } 931 932 for(i = 0; i < 9; i++) { 933 c = l[i%3]; 934 str[9-i] = (mode & mask)?c:'-'; 935 mask = mask<<1; 936 } 937 938 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S'; 939 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S'; 940 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T'; 941 str[10] = '\0'; 942 return str; 943 } 944 945 static inline void dump_stat(struct stat *st, const char *name) 946 { 947 char str[20]; 948 char s[64], *p; 949 950 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */ 951 st->st_mtime = 1; 952 953 ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */ 954 955 if ((p = strchr(s,'\n')) != NULL) *p = '\0'; 956 if ((p = strchr(s,'\r')) != NULL) *p = '\0'; 957 958 /* 959 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str), 960 st->st_size, s, name); 961 */ 962 963 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name); 964 } 965 966 static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i) 967 { 968 char fname[256]; 969 struct stat st; 970 971 if(!d || !i) return -1; 972 973 strncpy(fname, (char *)d->name, d->nsize); 974 fname[d->nsize] = '\0'; 975 976 memset(&st,0,sizeof(st)); 977 978 st.st_mtime = i->mtime; 979 st.st_mode = i->mode; 980 st.st_ino = i->ino; 981 st.st_size = i->isize; 982 983 dump_stat(&st, fname); 984 985 if (d->type == DT_LNK) { 986 unsigned char *src = (unsigned char *) (&i[1]); 987 putstr(" -> "); 988 putnstr(src, (int)i->dsize); 989 } 990 991 putstr("\r\n"); 992 993 return 0; 994 } 995 996 /* list inodes with the given pino */ 997 static u32 998 jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino) 999 { 1000 struct b_node *b; 1001 struct jffs2_raw_dirent *jDir; 1002 1003 for (b = pL->dir.listHead; b; b = b->next) { 1004 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, 1005 pL->readbuf); 1006 if (pino == jDir->pino) { 1007 u32 i_version = 0; 1008 struct jffs2_raw_inode *jNode, *i = NULL; 1009 struct b_node *b2; 1010 1011 #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS 1012 /* Check for more recent versions of this file */ 1013 int match; 1014 do { 1015 struct b_node *next = b->next; 1016 struct jffs2_raw_dirent *jDirNext; 1017 if (!next) 1018 break; 1019 jDirNext = (struct jffs2_raw_dirent *) 1020 get_node_mem(next->offset, NULL); 1021 match = jDirNext->pino == jDir->pino && 1022 jDirNext->nsize == jDir->nsize && 1023 strncmp((char *)jDirNext->name, 1024 (char *)jDir->name, 1025 jDir->nsize) == 0; 1026 if (match) { 1027 /* Use next. It is more recent */ 1028 b = next; 1029 /* Update buffer with the new info */ 1030 *jDir = *jDirNext; 1031 } 1032 put_fl_mem(jDirNext, NULL); 1033 } while (match); 1034 #endif 1035 if (jDir->ino == 0) { 1036 /* Deleted file */ 1037 put_fl_mem(jDir, pL->readbuf); 1038 continue; 1039 } 1040 1041 for (b2 = pL->frag.listHead; b2; b2 = b2->next) { 1042 jNode = (struct jffs2_raw_inode *) 1043 get_fl_mem(b2->offset, sizeof(*jNode), 1044 NULL); 1045 if (jNode->ino == jDir->ino && 1046 jNode->version >= i_version) { 1047 i_version = jNode->version; 1048 if (i) 1049 put_fl_mem(i, NULL); 1050 1051 if (jDir->type == DT_LNK) 1052 i = get_node_mem(b2->offset, 1053 NULL); 1054 else 1055 i = get_fl_mem(b2->offset, 1056 sizeof(*i), 1057 NULL); 1058 } 1059 put_fl_mem(jNode, NULL); 1060 } 1061 1062 dump_inode(pL, jDir, i); 1063 put_fl_mem(i, NULL); 1064 } 1065 put_fl_mem(jDir, pL->readbuf); 1066 } 1067 return pino; 1068 } 1069 1070 static u32 1071 jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino) 1072 { 1073 int i; 1074 char tmp[256]; 1075 char working_tmp[256]; 1076 char *c; 1077 1078 /* discard any leading slash */ 1079 i = 0; 1080 while (fname[i] == '/') 1081 i++; 1082 strcpy(tmp, &fname[i]); 1083 1084 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ 1085 { 1086 strncpy(working_tmp, tmp, c - tmp); 1087 working_tmp[c - tmp] = '\0'; 1088 #if 0 1089 putstr("search_inode: tmp = "); 1090 putstr(tmp); 1091 putstr("\r\n"); 1092 putstr("search_inode: wtmp = "); 1093 putstr(working_tmp); 1094 putstr("\r\n"); 1095 putstr("search_inode: c = "); 1096 putstr(c); 1097 putstr("\r\n"); 1098 #endif 1099 for (i = 0; i < strlen(c) - 1; i++) 1100 tmp[i] = c[i + 1]; 1101 tmp[i] = '\0'; 1102 #if 0 1103 putstr("search_inode: post tmp = "); 1104 putstr(tmp); 1105 putstr("\r\n"); 1106 #endif 1107 1108 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) { 1109 putstr("find_inode failed for name="); 1110 putstr(working_tmp); 1111 putstr("\r\n"); 1112 return 0; 1113 } 1114 } 1115 /* this is for the bare filename, directories have already been mapped */ 1116 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { 1117 putstr("find_inode failed for name="); 1118 putstr(tmp); 1119 putstr("\r\n"); 1120 return 0; 1121 } 1122 return pino; 1123 1124 } 1125 1126 static u32 1127 jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino) 1128 { 1129 struct b_node *b; 1130 struct b_node *b2; 1131 struct jffs2_raw_dirent *jDir; 1132 struct jffs2_raw_inode *jNode; 1133 u8 jDirFoundType = 0; 1134 u32 jDirFoundIno = 0; 1135 u32 jDirFoundPino = 0; 1136 char tmp[256]; 1137 u32 version = 0; 1138 u32 pino; 1139 unsigned char *src; 1140 1141 /* we need to search all and return the inode with the highest version */ 1142 for(b = pL->dir.listHead; b; b = b->next) { 1143 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, 1144 pL->readbuf); 1145 if (ino == jDir->ino) { 1146 if (jDir->version < version) { 1147 put_fl_mem(jDir, pL->readbuf); 1148 continue; 1149 } 1150 1151 if (jDir->version == version && jDirFoundType) { 1152 /* I'm pretty sure this isn't legal */ 1153 putstr(" ** ERROR ** "); 1154 putnstr(jDir->name, jDir->nsize); 1155 putLabeledWord(" has dup version (resolve) = ", 1156 version); 1157 } 1158 1159 jDirFoundType = jDir->type; 1160 jDirFoundIno = jDir->ino; 1161 jDirFoundPino = jDir->pino; 1162 version = jDir->version; 1163 } 1164 put_fl_mem(jDir, pL->readbuf); 1165 } 1166 /* now we found the right entry again. (shoulda returned inode*) */ 1167 if (jDirFoundType != DT_LNK) 1168 return jDirFoundIno; 1169 1170 /* it's a soft link so we follow it again. */ 1171 b2 = pL->frag.listHead; 1172 while (b2) { 1173 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset, 1174 pL->readbuf); 1175 if (jNode->ino == jDirFoundIno) { 1176 src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode); 1177 1178 #if 0 1179 putLabeledWord("\t\t dsize = ", jNode->dsize); 1180 putstr("\t\t target = "); 1181 putnstr(src, jNode->dsize); 1182 putstr("\r\n"); 1183 #endif 1184 strncpy(tmp, (char *)src, jNode->dsize); 1185 tmp[jNode->dsize] = '\0'; 1186 put_fl_mem(jNode, pL->readbuf); 1187 break; 1188 } 1189 b2 = b2->next; 1190 put_fl_mem(jNode, pL->readbuf); 1191 } 1192 /* ok so the name of the new file to find is in tmp */ 1193 /* if it starts with a slash it is root based else shared dirs */ 1194 if (tmp[0] == '/') 1195 pino = 1; 1196 else 1197 pino = jDirFoundPino; 1198 1199 return jffs2_1pass_search_inode(pL, tmp, pino); 1200 } 1201 1202 static u32 1203 jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino) 1204 { 1205 int i; 1206 char tmp[256]; 1207 char working_tmp[256]; 1208 char *c; 1209 1210 /* discard any leading slash */ 1211 i = 0; 1212 while (fname[i] == '/') 1213 i++; 1214 strcpy(tmp, &fname[i]); 1215 working_tmp[0] = '\0'; 1216 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ 1217 { 1218 strncpy(working_tmp, tmp, c - tmp); 1219 working_tmp[c - tmp] = '\0'; 1220 for (i = 0; i < strlen(c) - 1; i++) 1221 tmp[i] = c[i + 1]; 1222 tmp[i] = '\0'; 1223 /* only a failure if we arent looking at top level */ 1224 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) && 1225 (working_tmp[0])) { 1226 putstr("find_inode failed for name="); 1227 putstr(working_tmp); 1228 putstr("\r\n"); 1229 return 0; 1230 } 1231 } 1232 1233 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { 1234 putstr("find_inode failed for name="); 1235 putstr(tmp); 1236 putstr("\r\n"); 1237 return 0; 1238 } 1239 /* this is for the bare filename, directories have already been mapped */ 1240 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) { 1241 putstr("find_inode failed for name="); 1242 putstr(tmp); 1243 putstr("\r\n"); 1244 return 0; 1245 } 1246 return pino; 1247 1248 } 1249 1250 unsigned char 1251 jffs2_1pass_rescan_needed(struct part_info *part) 1252 { 1253 struct b_node *b; 1254 struct jffs2_unknown_node onode; 1255 struct jffs2_unknown_node *node; 1256 struct b_lists *pL = (struct b_lists *)part->jffs2_priv; 1257 1258 if (part->jffs2_priv == 0){ 1259 DEBUGF ("rescan: First time in use\n"); 1260 return 1; 1261 } 1262 1263 /* if we have no list, we need to rescan */ 1264 if (pL->frag.listCount == 0) { 1265 DEBUGF ("rescan: fraglist zero\n"); 1266 return 1; 1267 } 1268 1269 /* but suppose someone reflashed a partition at the same offset... */ 1270 b = pL->dir.listHead; 1271 while (b) { 1272 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset, 1273 sizeof(onode), &onode); 1274 if (node->nodetype != JFFS2_NODETYPE_DIRENT) { 1275 DEBUGF ("rescan: fs changed beneath me? (%lx)\n", 1276 (unsigned long) b->offset); 1277 return 1; 1278 } 1279 b = b->next; 1280 } 1281 return 0; 1282 } 1283 1284 #ifdef CONFIG_JFFS2_SUMMARY 1285 static u32 sum_get_unaligned32(u32 *ptr) 1286 { 1287 u32 val; 1288 u8 *p = (u8 *)ptr; 1289 1290 val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24); 1291 1292 return __le32_to_cpu(val); 1293 } 1294 1295 static u16 sum_get_unaligned16(u16 *ptr) 1296 { 1297 u16 val; 1298 u8 *p = (u8 *)ptr; 1299 1300 val = *p | (*(p + 1) << 8); 1301 1302 return __le16_to_cpu(val); 1303 } 1304 1305 #define dbg_summary(...) do {} while (0); 1306 /* 1307 * Process the stored summary information - helper function for 1308 * jffs2_sum_scan_sumnode() 1309 */ 1310 1311 static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset, 1312 struct jffs2_raw_summary *summary, 1313 struct b_lists *pL) 1314 { 1315 void *sp; 1316 int i, pass; 1317 void *ret; 1318 1319 for (pass = 0; pass < 2; pass++) { 1320 sp = summary->sum; 1321 1322 for (i = 0; i < summary->sum_num; i++) { 1323 struct jffs2_sum_unknown_flash *spu = sp; 1324 dbg_summary("processing summary index %d\n", i); 1325 1326 switch (sum_get_unaligned16(&spu->nodetype)) { 1327 case JFFS2_NODETYPE_INODE: { 1328 struct jffs2_sum_inode_flash *spi; 1329 if (pass) { 1330 spi = sp; 1331 1332 ret = insert_node(&pL->frag, 1333 (u32)part->offset + 1334 offset + 1335 sum_get_unaligned32( 1336 &spi->offset)); 1337 if (ret == NULL) 1338 return -1; 1339 } 1340 1341 sp += JFFS2_SUMMARY_INODE_SIZE; 1342 1343 break; 1344 } 1345 case JFFS2_NODETYPE_DIRENT: { 1346 struct jffs2_sum_dirent_flash *spd; 1347 spd = sp; 1348 if (pass) { 1349 ret = insert_node(&pL->dir, 1350 (u32) part->offset + 1351 offset + 1352 sum_get_unaligned32( 1353 &spd->offset)); 1354 if (ret == NULL) 1355 return -1; 1356 } 1357 1358 sp += JFFS2_SUMMARY_DIRENT_SIZE( 1359 spd->nsize); 1360 1361 break; 1362 } 1363 default : { 1364 uint16_t nodetype = sum_get_unaligned16( 1365 &spu->nodetype); 1366 printf("Unsupported node type %x found" 1367 " in summary!\n", 1368 nodetype); 1369 if ((nodetype & JFFS2_COMPAT_MASK) == 1370 JFFS2_FEATURE_INCOMPAT) 1371 return -EIO; 1372 return -EBADMSG; 1373 } 1374 } 1375 } 1376 } 1377 return 0; 1378 } 1379 1380 /* Process the summary node - called from jffs2_scan_eraseblock() */ 1381 int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset, 1382 struct jffs2_raw_summary *summary, uint32_t sumsize, 1383 struct b_lists *pL) 1384 { 1385 struct jffs2_unknown_node crcnode; 1386 int ret, __maybe_unused ofs; 1387 uint32_t crc; 1388 1389 ofs = part->sector_size - sumsize; 1390 1391 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n", 1392 offset, offset + ofs, sumsize); 1393 1394 /* OK, now check for node validity and CRC */ 1395 crcnode.magic = JFFS2_MAGIC_BITMASK; 1396 crcnode.nodetype = JFFS2_NODETYPE_SUMMARY; 1397 crcnode.totlen = summary->totlen; 1398 crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4); 1399 1400 if (summary->hdr_crc != crc) { 1401 dbg_summary("Summary node header is corrupt (bad CRC or " 1402 "no summary at all)\n"); 1403 goto crc_err; 1404 } 1405 1406 if (summary->totlen != sumsize) { 1407 dbg_summary("Summary node is corrupt (wrong erasesize?)\n"); 1408 goto crc_err; 1409 } 1410 1411 crc = crc32_no_comp(0, (uchar *)summary, 1412 sizeof(struct jffs2_raw_summary)-8); 1413 1414 if (summary->node_crc != crc) { 1415 dbg_summary("Summary node is corrupt (bad CRC)\n"); 1416 goto crc_err; 1417 } 1418 1419 crc = crc32_no_comp(0, (uchar *)summary->sum, 1420 sumsize - sizeof(struct jffs2_raw_summary)); 1421 1422 if (summary->sum_crc != crc) { 1423 dbg_summary("Summary node data is corrupt (bad CRC)\n"); 1424 goto crc_err; 1425 } 1426 1427 if (summary->cln_mkr) 1428 dbg_summary("Summary : CLEANMARKER node \n"); 1429 1430 ret = jffs2_sum_process_sum_data(part, offset, summary, pL); 1431 if (ret == -EBADMSG) 1432 return 0; 1433 if (ret) 1434 return ret; /* real error */ 1435 1436 return 1; 1437 1438 crc_err: 1439 putstr("Summary node crc error, skipping summary information.\n"); 1440 1441 return 0; 1442 } 1443 #endif /* CONFIG_JFFS2_SUMMARY */ 1444 1445 #ifdef DEBUG_FRAGMENTS 1446 static void 1447 dump_fragments(struct b_lists *pL) 1448 { 1449 struct b_node *b; 1450 struct jffs2_raw_inode ojNode; 1451 struct jffs2_raw_inode *jNode; 1452 1453 putstr("\r\n\r\n******The fragment Entries******\r\n"); 1454 b = pL->frag.listHead; 1455 while (b) { 1456 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, 1457 sizeof(ojNode), &ojNode); 1458 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset); 1459 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen); 1460 putLabeledWord("\tbuild_list: inode = ", jNode->ino); 1461 putLabeledWord("\tbuild_list: version = ", jNode->version); 1462 putLabeledWord("\tbuild_list: isize = ", jNode->isize); 1463 putLabeledWord("\tbuild_list: atime = ", jNode->atime); 1464 putLabeledWord("\tbuild_list: offset = ", jNode->offset); 1465 putLabeledWord("\tbuild_list: csize = ", jNode->csize); 1466 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize); 1467 putLabeledWord("\tbuild_list: compr = ", jNode->compr); 1468 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr); 1469 putLabeledWord("\tbuild_list: flags = ", jNode->flags); 1470 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ 1471 b = b->next; 1472 } 1473 } 1474 #endif 1475 1476 #ifdef DEBUG_DIRENTS 1477 static void 1478 dump_dirents(struct b_lists *pL) 1479 { 1480 struct b_node *b; 1481 struct jffs2_raw_dirent *jDir; 1482 1483 putstr("\r\n\r\n******The directory Entries******\r\n"); 1484 b = pL->dir.listHead; 1485 while (b) { 1486 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, 1487 pL->readbuf); 1488 putstr("\r\n"); 1489 putnstr(jDir->name, jDir->nsize); 1490 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic); 1491 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype); 1492 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc); 1493 putLabeledWord("\tbuild_list: pino = ", jDir->pino); 1494 putLabeledWord("\tbuild_list: version = ", jDir->version); 1495 putLabeledWord("\tbuild_list: ino = ", jDir->ino); 1496 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime); 1497 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize); 1498 putLabeledWord("\tbuild_list: type = ", jDir->type); 1499 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc); 1500 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc); 1501 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ 1502 b = b->next; 1503 put_fl_mem(jDir, pL->readbuf); 1504 } 1505 } 1506 #endif 1507 1508 #define DEFAULT_EMPTY_SCAN_SIZE 256 1509 1510 static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) 1511 { 1512 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE) 1513 return sector_size; 1514 else 1515 return DEFAULT_EMPTY_SCAN_SIZE; 1516 } 1517 1518 static u32 1519 jffs2_1pass_build_lists(struct part_info * part) 1520 { 1521 struct b_lists *pL; 1522 struct jffs2_unknown_node *node; 1523 u32 nr_sectors; 1524 u32 i; 1525 u32 counter4 = 0; 1526 u32 counterF = 0; 1527 u32 counterN = 0; 1528 u32 max_totlen = 0; 1529 u32 buf_size; 1530 char *buf; 1531 1532 nr_sectors = lldiv(part->size, part->sector_size); 1533 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */ 1534 /* jffs2 list building enterprise nope. in newer versions the overhead is */ 1535 /* only about 5 %. not enough to inconvenience people for. */ 1536 /* lcd_off(); */ 1537 1538 /* if we are building a list we need to refresh the cache. */ 1539 jffs_init_1pass_list(part); 1540 pL = (struct b_lists *)part->jffs2_priv; 1541 buf = malloc(DEFAULT_EMPTY_SCAN_SIZE); 1542 puts ("Scanning JFFS2 FS: "); 1543 1544 /* start at the beginning of the partition */ 1545 for (i = 0; i < nr_sectors; i++) { 1546 uint32_t sector_ofs = i * part->sector_size; 1547 uint32_t buf_ofs = sector_ofs; 1548 uint32_t buf_len; 1549 uint32_t ofs, prevofs; 1550 #ifdef CONFIG_JFFS2_SUMMARY 1551 struct jffs2_sum_marker *sm; 1552 void *sumptr = NULL; 1553 uint32_t sumlen; 1554 int ret; 1555 #endif 1556 /* Indicates a sector with a CLEANMARKER was found */ 1557 int clean_sector = 0; 1558 1559 /* Set buf_size to maximum length */ 1560 buf_size = DEFAULT_EMPTY_SCAN_SIZE; 1561 WATCHDOG_RESET(); 1562 1563 #ifdef CONFIG_JFFS2_SUMMARY 1564 buf_len = sizeof(*sm); 1565 1566 /* Read as much as we want into the _end_ of the preallocated 1567 * buffer 1568 */ 1569 get_fl_mem(part->offset + sector_ofs + part->sector_size - 1570 buf_len, buf_len, buf + buf_size - buf_len); 1571 1572 sm = (void *)buf + buf_size - sizeof(*sm); 1573 if (sm->magic == JFFS2_SUM_MAGIC) { 1574 sumlen = part->sector_size - sm->offset; 1575 sumptr = buf + buf_size - sumlen; 1576 1577 /* Now, make sure the summary itself is available */ 1578 if (sumlen > buf_size) { 1579 /* Need to kmalloc for this. */ 1580 sumptr = malloc(sumlen); 1581 if (!sumptr) { 1582 putstr("Can't get memory for summary " 1583 "node!\n"); 1584 free(buf); 1585 jffs2_free_cache(part); 1586 return 0; 1587 } 1588 memcpy(sumptr + sumlen - buf_len, buf + 1589 buf_size - buf_len, buf_len); 1590 } 1591 if (buf_len < sumlen) { 1592 /* Need to read more so that the entire summary 1593 * node is present 1594 */ 1595 get_fl_mem(part->offset + sector_ofs + 1596 part->sector_size - sumlen, 1597 sumlen - buf_len, sumptr); 1598 } 1599 } 1600 1601 if (sumptr) { 1602 ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr, 1603 sumlen, pL); 1604 1605 if (buf_size && sumlen > buf_size) 1606 free(sumptr); 1607 if (ret < 0) { 1608 free(buf); 1609 jffs2_free_cache(part); 1610 return 0; 1611 } 1612 if (ret) 1613 continue; 1614 1615 } 1616 #endif /* CONFIG_JFFS2_SUMMARY */ 1617 1618 buf_len = EMPTY_SCAN_SIZE(part->sector_size); 1619 1620 get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf); 1621 1622 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */ 1623 ofs = 0; 1624 1625 /* Scan only 4KiB of 0xFF before declaring it's empty */ 1626 while (ofs < EMPTY_SCAN_SIZE(part->sector_size) && 1627 *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF) 1628 ofs += 4; 1629 1630 if (ofs == EMPTY_SCAN_SIZE(part->sector_size)) 1631 continue; 1632 1633 ofs += sector_ofs; 1634 prevofs = ofs - 1; 1635 /* 1636 * Set buf_size down to the minimum size required. 1637 * This prevents reading in chunks of flash data unnecessarily. 1638 */ 1639 buf_size = sizeof(union jffs2_node_union); 1640 1641 scan_more: 1642 while (ofs < sector_ofs + part->sector_size) { 1643 if (ofs == prevofs) { 1644 printf("offset %08x already seen, skip\n", ofs); 1645 ofs += 4; 1646 counter4++; 1647 continue; 1648 } 1649 prevofs = ofs; 1650 if (sector_ofs + part->sector_size < 1651 ofs + sizeof(*node)) 1652 break; 1653 if (buf_ofs + buf_len < ofs + sizeof(*node)) { 1654 buf_len = min_t(uint32_t, buf_size, sector_ofs 1655 + part->sector_size - ofs); 1656 get_fl_mem((u32)part->offset + ofs, buf_len, 1657 buf); 1658 buf_ofs = ofs; 1659 } 1660 1661 node = (struct jffs2_unknown_node *)&buf[ofs-buf_ofs]; 1662 1663 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) { 1664 uint32_t inbuf_ofs; 1665 uint32_t scan_end; 1666 1667 ofs += 4; 1668 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE( 1669 part->sector_size)/8, 1670 buf_len); 1671 more_empty: 1672 inbuf_ofs = ofs - buf_ofs; 1673 while (inbuf_ofs < scan_end) { 1674 if (*(uint32_t *)(&buf[inbuf_ofs]) != 1675 0xffffffff) 1676 goto scan_more; 1677 1678 inbuf_ofs += 4; 1679 ofs += 4; 1680 } 1681 /* Ran off end. */ 1682 /* 1683 * If this sector had a clean marker at the 1684 * beginning, and immediately following this 1685 * have been a bunch of FF bytes, treat the 1686 * entire sector as empty. 1687 */ 1688 if (clean_sector) 1689 break; 1690 1691 /* See how much more there is to read in this 1692 * eraseblock... 1693 */ 1694 buf_len = min_t(uint32_t, buf_size, 1695 sector_ofs + 1696 part->sector_size - ofs); 1697 if (!buf_len) { 1698 /* No more to read. Break out of main 1699 * loop without marking this range of 1700 * empty space as dirty (because it's 1701 * not) 1702 */ 1703 break; 1704 } 1705 scan_end = buf_len; 1706 get_fl_mem((u32)part->offset + ofs, buf_len, 1707 buf); 1708 buf_ofs = ofs; 1709 goto more_empty; 1710 } 1711 /* 1712 * Found something not erased in the sector, so reset 1713 * the 'clean_sector' flag. 1714 */ 1715 clean_sector = 0; 1716 if (node->magic != JFFS2_MAGIC_BITMASK || 1717 !hdr_crc(node)) { 1718 ofs += 4; 1719 counter4++; 1720 continue; 1721 } 1722 if (ofs + node->totlen > 1723 sector_ofs + part->sector_size) { 1724 ofs += 4; 1725 counter4++; 1726 continue; 1727 } 1728 /* if its a fragment add it */ 1729 switch (node->nodetype) { 1730 case JFFS2_NODETYPE_INODE: 1731 if (buf_ofs + buf_len < ofs + sizeof(struct 1732 jffs2_raw_inode)) { 1733 buf_len = min_t(uint32_t, 1734 sizeof(struct jffs2_raw_inode), 1735 sector_ofs + 1736 part->sector_size - 1737 ofs); 1738 get_fl_mem((u32)part->offset + ofs, 1739 buf_len, buf); 1740 buf_ofs = ofs; 1741 node = (void *)buf; 1742 } 1743 if (!inode_crc((struct jffs2_raw_inode *)node)) 1744 break; 1745 1746 if (insert_node(&pL->frag, (u32) part->offset + 1747 ofs) == NULL) { 1748 free(buf); 1749 jffs2_free_cache(part); 1750 return 0; 1751 } 1752 if (max_totlen < node->totlen) 1753 max_totlen = node->totlen; 1754 break; 1755 case JFFS2_NODETYPE_DIRENT: 1756 if (buf_ofs + buf_len < ofs + sizeof(struct 1757 jffs2_raw_dirent) + 1758 ((struct 1759 jffs2_raw_dirent *) 1760 node)->nsize) { 1761 buf_len = min_t(uint32_t, 1762 node->totlen, 1763 sector_ofs + 1764 part->sector_size - 1765 ofs); 1766 get_fl_mem((u32)part->offset + ofs, 1767 buf_len, buf); 1768 buf_ofs = ofs; 1769 node = (void *)buf; 1770 } 1771 1772 if (!dirent_crc((struct jffs2_raw_dirent *) 1773 node) || 1774 !dirent_name_crc( 1775 (struct 1776 jffs2_raw_dirent *) 1777 node)) 1778 break; 1779 if (! (counterN%100)) 1780 puts ("\b\b. "); 1781 if (insert_node(&pL->dir, (u32) part->offset + 1782 ofs) == NULL) { 1783 free(buf); 1784 jffs2_free_cache(part); 1785 return 0; 1786 } 1787 if (max_totlen < node->totlen) 1788 max_totlen = node->totlen; 1789 counterN++; 1790 break; 1791 case JFFS2_NODETYPE_CLEANMARKER: 1792 if (node->totlen != sizeof(struct jffs2_unknown_node)) 1793 printf("OOPS Cleanmarker has bad size " 1794 "%d != %zu\n", 1795 node->totlen, 1796 sizeof(struct jffs2_unknown_node)); 1797 if ((node->totlen == 1798 sizeof(struct jffs2_unknown_node)) && 1799 (ofs == sector_ofs)) { 1800 /* 1801 * Found a CLEANMARKER at the beginning 1802 * of the sector. It's in the correct 1803 * place with correct size and CRC. 1804 */ 1805 clean_sector = 1; 1806 } 1807 break; 1808 case JFFS2_NODETYPE_PADDING: 1809 if (node->totlen < sizeof(struct jffs2_unknown_node)) 1810 printf("OOPS Padding has bad size " 1811 "%d < %zu\n", 1812 node->totlen, 1813 sizeof(struct jffs2_unknown_node)); 1814 break; 1815 case JFFS2_NODETYPE_SUMMARY: 1816 break; 1817 default: 1818 printf("Unknown node type: %x len %d offset 0x%x\n", 1819 node->nodetype, 1820 node->totlen, ofs); 1821 } 1822 ofs += ((node->totlen + 3) & ~3); 1823 counterF++; 1824 } 1825 } 1826 1827 free(buf); 1828 #if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS) 1829 /* 1830 * Sort the lists. 1831 */ 1832 sort_list(&pL->frag); 1833 sort_list(&pL->dir); 1834 #endif 1835 putstr("\b\b done.\r\n"); /* close off the dots */ 1836 1837 /* We don't care if malloc failed - then each read operation will 1838 * allocate its own buffer as necessary (NAND) or will read directly 1839 * from flash (NOR). 1840 */ 1841 pL->readbuf = malloc(max_totlen); 1842 1843 /* turn the lcd back on. */ 1844 /* splash(); */ 1845 1846 #if 0 1847 putLabeledWord("dir entries = ", pL->dir.listCount); 1848 putLabeledWord("frag entries = ", pL->frag.listCount); 1849 putLabeledWord("+4 increments = ", counter4); 1850 putLabeledWord("+file_offset increments = ", counterF); 1851 1852 #endif 1853 1854 #ifdef DEBUG_DIRENTS 1855 dump_dirents(pL); 1856 #endif 1857 1858 #ifdef DEBUG_FRAGMENTS 1859 dump_fragments(pL); 1860 #endif 1861 1862 /* give visual feedback that we are done scanning the flash */ 1863 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */ 1864 return 1; 1865 } 1866 1867 1868 static u32 1869 jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL) 1870 { 1871 struct b_node *b; 1872 struct jffs2_raw_inode ojNode; 1873 struct jffs2_raw_inode *jNode; 1874 int i; 1875 1876 for (i = 0; i < JFFS2_NUM_COMPR; i++) { 1877 piL->compr_info[i].num_frags = 0; 1878 piL->compr_info[i].compr_sum = 0; 1879 piL->compr_info[i].decompr_sum = 0; 1880 } 1881 1882 b = pL->frag.listHead; 1883 while (b) { 1884 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, 1885 sizeof(ojNode), &ojNode); 1886 if (jNode->compr < JFFS2_NUM_COMPR) { 1887 piL->compr_info[jNode->compr].num_frags++; 1888 piL->compr_info[jNode->compr].compr_sum += jNode->csize; 1889 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize; 1890 } 1891 b = b->next; 1892 } 1893 return 0; 1894 } 1895 1896 1897 static struct b_lists * 1898 jffs2_get_list(struct part_info * part, const char *who) 1899 { 1900 /* copy requested part_info struct pointer to global location */ 1901 current_part = part; 1902 1903 if (jffs2_1pass_rescan_needed(part)) { 1904 if (!jffs2_1pass_build_lists(part)) { 1905 printf("%s: Failed to scan JFFSv2 file structure\n", who); 1906 return NULL; 1907 } 1908 } 1909 return (struct b_lists *)part->jffs2_priv; 1910 } 1911 1912 1913 /* Print directory / file contents */ 1914 u32 1915 jffs2_1pass_ls(struct part_info * part, const char *fname) 1916 { 1917 struct b_lists *pl; 1918 long ret = 1; 1919 u32 inode; 1920 1921 if (! (pl = jffs2_get_list(part, "ls"))) 1922 return 0; 1923 1924 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) { 1925 putstr("ls: Failed to scan jffs2 file structure\r\n"); 1926 return 0; 1927 } 1928 1929 1930 #if 0 1931 putLabeledWord("found file at inode = ", inode); 1932 putLabeledWord("read_inode returns = ", ret); 1933 #endif 1934 1935 return ret; 1936 } 1937 1938 1939 /* Load a file from flash into memory. fname can be a full path */ 1940 u32 1941 jffs2_1pass_load(char *dest, struct part_info * part, const char *fname) 1942 { 1943 1944 struct b_lists *pl; 1945 long ret = 1; 1946 u32 inode; 1947 1948 if (! (pl = jffs2_get_list(part, "load"))) 1949 return 0; 1950 1951 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) { 1952 putstr("load: Failed to find inode\r\n"); 1953 return 0; 1954 } 1955 1956 /* Resolve symlinks */ 1957 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) { 1958 putstr("load: Failed to resolve inode structure\r\n"); 1959 return 0; 1960 } 1961 1962 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) { 1963 putstr("load: Failed to read inode\r\n"); 1964 return 0; 1965 } 1966 1967 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname, 1968 (unsigned long) dest, ret); 1969 return ret; 1970 } 1971 1972 /* Return information about the fs on this partition */ 1973 u32 1974 jffs2_1pass_info(struct part_info * part) 1975 { 1976 struct b_jffs2_info info; 1977 struct b_lists *pl; 1978 int i; 1979 1980 if (! (pl = jffs2_get_list(part, "info"))) 1981 return 0; 1982 1983 jffs2_1pass_fill_info(pl, &info); 1984 for (i = 0; i < JFFS2_NUM_COMPR; i++) { 1985 printf ("Compression: %s\n" 1986 "\tfrag count: %d\n" 1987 "\tcompressed sum: %d\n" 1988 "\tuncompressed sum: %d\n", 1989 compr_names[i], 1990 info.compr_info[i].num_frags, 1991 info.compr_info[i].compr_sum, 1992 info.compr_info[i].decompr_sum); 1993 } 1994 return 1; 1995 } 1996