1 /* 2 * fat.c 3 * 4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg 5 * 6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6 7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot 8 * 9 * SPDX-License-Identifier: GPL-2.0+ 10 */ 11 12 #include <common.h> 13 #include <config.h> 14 #include <exports.h> 15 #include <fat.h> 16 #include <asm/byteorder.h> 17 #include <part.h> 18 #include <malloc.h> 19 #include <linux/compiler.h> 20 #include <linux/ctype.h> 21 22 #ifdef CONFIG_SUPPORT_VFAT 23 static const int vfat_enabled = 1; 24 #else 25 static const int vfat_enabled = 0; 26 #endif 27 28 /* 29 * Convert a string to lowercase. 30 */ 31 static void downcase(char *str) 32 { 33 while (*str != '\0') { 34 *str = tolower(*str); 35 str++; 36 } 37 } 38 39 static block_dev_desc_t *cur_dev; 40 static disk_partition_t cur_part_info; 41 42 #define DOS_BOOT_MAGIC_OFFSET 0x1fe 43 #define DOS_FS_TYPE_OFFSET 0x36 44 #define DOS_FS32_TYPE_OFFSET 0x52 45 46 static int disk_read(__u32 block, __u32 nr_blocks, void *buf) 47 { 48 if (!cur_dev || !cur_dev->block_read) 49 return -1; 50 51 return cur_dev->block_read(cur_dev->dev, 52 cur_part_info.start + block, nr_blocks, buf); 53 } 54 55 int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info) 56 { 57 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); 58 59 cur_dev = dev_desc; 60 cur_part_info = *info; 61 62 /* Make sure it has a valid FAT header */ 63 if (disk_read(0, 1, buffer) != 1) { 64 cur_dev = NULL; 65 return -1; 66 } 67 68 /* Check if it's actually a DOS volume */ 69 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) { 70 cur_dev = NULL; 71 return -1; 72 } 73 74 /* Check for FAT12/FAT16/FAT32 filesystem */ 75 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3)) 76 return 0; 77 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5)) 78 return 0; 79 80 cur_dev = NULL; 81 return -1; 82 } 83 84 int fat_register_device(block_dev_desc_t *dev_desc, int part_no) 85 { 86 disk_partition_t info; 87 88 /* First close any currently found FAT filesystem */ 89 cur_dev = NULL; 90 91 /* Read the partition table, if present */ 92 if (get_partition_info(dev_desc, part_no, &info)) { 93 if (part_no != 0) { 94 printf("** Partition %d not valid on device %d **\n", 95 part_no, dev_desc->dev); 96 return -1; 97 } 98 99 info.start = 0; 100 info.size = dev_desc->lba; 101 info.blksz = dev_desc->blksz; 102 info.name[0] = 0; 103 info.type[0] = 0; 104 info.bootable = 0; 105 #ifdef CONFIG_PARTITION_UUIDS 106 info.uuid[0] = 0; 107 #endif 108 } 109 110 return fat_set_blk_dev(dev_desc, &info); 111 } 112 113 /* 114 * Get the first occurence of a directory delimiter ('/' or '\') in a string. 115 * Return index into string if found, -1 otherwise. 116 */ 117 static int dirdelim(char *str) 118 { 119 char *start = str; 120 121 while (*str != '\0') { 122 if (ISDIRDELIM(*str)) 123 return str - start; 124 str++; 125 } 126 return -1; 127 } 128 129 /* 130 * Extract zero terminated short name from a directory entry. 131 */ 132 static void get_name(dir_entry *dirent, char *s_name) 133 { 134 char *ptr; 135 136 memcpy(s_name, dirent->name, 8); 137 s_name[8] = '\0'; 138 ptr = s_name; 139 while (*ptr && *ptr != ' ') 140 ptr++; 141 if (dirent->ext[0] && dirent->ext[0] != ' ') { 142 *ptr = '.'; 143 ptr++; 144 memcpy(ptr, dirent->ext, 3); 145 ptr[3] = '\0'; 146 while (*ptr && *ptr != ' ') 147 ptr++; 148 } 149 *ptr = '\0'; 150 if (*s_name == DELETED_FLAG) 151 *s_name = '\0'; 152 else if (*s_name == aRING) 153 *s_name = DELETED_FLAG; 154 downcase(s_name); 155 } 156 157 /* 158 * Get the entry at index 'entry' in a FAT (12/16/32) table. 159 * On failure 0x00 is returned. 160 */ 161 static __u32 get_fatent(fsdata *mydata, __u32 entry) 162 { 163 __u32 bufnum; 164 __u32 off16, offset; 165 __u32 ret = 0x00; 166 __u16 val1, val2; 167 168 switch (mydata->fatsize) { 169 case 32: 170 bufnum = entry / FAT32BUFSIZE; 171 offset = entry - bufnum * FAT32BUFSIZE; 172 break; 173 case 16: 174 bufnum = entry / FAT16BUFSIZE; 175 offset = entry - bufnum * FAT16BUFSIZE; 176 break; 177 case 12: 178 bufnum = entry / FAT12BUFSIZE; 179 offset = entry - bufnum * FAT12BUFSIZE; 180 break; 181 182 default: 183 /* Unsupported FAT size */ 184 return ret; 185 } 186 187 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n", 188 mydata->fatsize, entry, entry, offset, offset); 189 190 /* Read a new block of FAT entries into the cache. */ 191 if (bufnum != mydata->fatbufnum) { 192 __u32 getsize = FATBUFBLOCKS; 193 __u8 *bufptr = mydata->fatbuf; 194 __u32 fatlength = mydata->fatlength; 195 __u32 startblock = bufnum * FATBUFBLOCKS; 196 197 if (startblock + getsize > fatlength) 198 getsize = fatlength - startblock; 199 200 startblock += mydata->fat_sect; /* Offset from start of disk */ 201 202 if (disk_read(startblock, getsize, bufptr) < 0) { 203 debug("Error reading FAT blocks\n"); 204 return ret; 205 } 206 mydata->fatbufnum = bufnum; 207 } 208 209 /* Get the actual entry from the table */ 210 switch (mydata->fatsize) { 211 case 32: 212 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); 213 break; 214 case 16: 215 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); 216 break; 217 case 12: 218 off16 = (offset * 3) / 4; 219 220 switch (offset & 0x3) { 221 case 0: 222 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]); 223 ret &= 0xfff; 224 break; 225 case 1: 226 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 227 val1 &= 0xf000; 228 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); 229 val2 &= 0x00ff; 230 ret = (val2 << 4) | (val1 >> 12); 231 break; 232 case 2: 233 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 234 val1 &= 0xff00; 235 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); 236 val2 &= 0x000f; 237 ret = (val2 << 8) | (val1 >> 8); 238 break; 239 case 3: 240 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 241 ret = (ret & 0xfff0) >> 4; 242 break; 243 default: 244 break; 245 } 246 break; 247 } 248 debug("FAT%d: ret: %08x, offset: %04x\n", 249 mydata->fatsize, ret, offset); 250 251 return ret; 252 } 253 254 /* 255 * Read at most 'size' bytes from the specified cluster into 'buffer'. 256 * Return 0 on success, -1 otherwise. 257 */ 258 static int 259 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size) 260 { 261 __u32 idx = 0; 262 __u32 startsect; 263 int ret; 264 265 if (clustnum > 0) { 266 startsect = mydata->data_begin + 267 clustnum * mydata->clust_size; 268 } else { 269 startsect = mydata->rootdir_sect; 270 } 271 272 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect); 273 274 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) { 275 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); 276 277 printf("FAT: Misaligned buffer address (%p)\n", buffer); 278 279 while (size >= mydata->sect_size) { 280 ret = disk_read(startsect++, 1, tmpbuf); 281 if (ret != 1) { 282 debug("Error reading data (got %d)\n", ret); 283 return -1; 284 } 285 286 memcpy(buffer, tmpbuf, mydata->sect_size); 287 buffer += mydata->sect_size; 288 size -= mydata->sect_size; 289 } 290 } else { 291 idx = size / mydata->sect_size; 292 ret = disk_read(startsect, idx, buffer); 293 if (ret != idx) { 294 debug("Error reading data (got %d)\n", ret); 295 return -1; 296 } 297 startsect += idx; 298 idx *= mydata->sect_size; 299 buffer += idx; 300 size -= idx; 301 } 302 if (size) { 303 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); 304 305 ret = disk_read(startsect, 1, tmpbuf); 306 if (ret != 1) { 307 debug("Error reading data (got %d)\n", ret); 308 return -1; 309 } 310 311 memcpy(buffer, tmpbuf, size); 312 } 313 314 return 0; 315 } 316 317 /* 318 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr' 319 * into 'buffer'. 320 * Update the number of bytes read in *gotsize or return -1 on fatal errors. 321 */ 322 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE] 323 __aligned(ARCH_DMA_MINALIGN); 324 325 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, 326 __u8 *buffer, loff_t maxsize, loff_t *gotsize) 327 { 328 loff_t filesize = FAT2CPU32(dentptr->size); 329 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; 330 __u32 curclust = START(dentptr); 331 __u32 endclust, newclust; 332 loff_t actsize; 333 334 *gotsize = 0; 335 debug("Filesize: %llu bytes\n", filesize); 336 337 if (pos >= filesize) { 338 debug("Read position past EOF: %llu\n", pos); 339 return 0; 340 } 341 342 if (maxsize > 0 && filesize > pos + maxsize) 343 filesize = pos + maxsize; 344 345 debug("%llu bytes\n", filesize); 346 347 actsize = bytesperclust; 348 349 /* go to cluster at pos */ 350 while (actsize <= pos) { 351 curclust = get_fatent(mydata, curclust); 352 if (CHECK_CLUST(curclust, mydata->fatsize)) { 353 debug("curclust: 0x%x\n", curclust); 354 debug("Invalid FAT entry\n"); 355 return 0; 356 } 357 actsize += bytesperclust; 358 } 359 360 /* actsize > pos */ 361 actsize -= bytesperclust; 362 filesize -= actsize; 363 pos -= actsize; 364 365 /* align to beginning of next cluster if any */ 366 if (pos) { 367 actsize = min(filesize, (loff_t)bytesperclust); 368 if (get_cluster(mydata, curclust, get_contents_vfatname_block, 369 (int)actsize) != 0) { 370 printf("Error reading cluster\n"); 371 return -1; 372 } 373 filesize -= actsize; 374 actsize -= pos; 375 memcpy(buffer, get_contents_vfatname_block + pos, actsize); 376 *gotsize += actsize; 377 if (!filesize) 378 return 0; 379 buffer += actsize; 380 381 curclust = get_fatent(mydata, curclust); 382 if (CHECK_CLUST(curclust, mydata->fatsize)) { 383 debug("curclust: 0x%x\n", curclust); 384 debug("Invalid FAT entry\n"); 385 return 0; 386 } 387 } 388 389 actsize = bytesperclust; 390 endclust = curclust; 391 392 do { 393 /* search for consecutive clusters */ 394 while (actsize < filesize) { 395 newclust = get_fatent(mydata, endclust); 396 if ((newclust - 1) != endclust) 397 goto getit; 398 if (CHECK_CLUST(newclust, mydata->fatsize)) { 399 debug("curclust: 0x%x\n", newclust); 400 debug("Invalid FAT entry\n"); 401 return 0; 402 } 403 endclust = newclust; 404 actsize += bytesperclust; 405 } 406 407 /* get remaining bytes */ 408 actsize = filesize; 409 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { 410 printf("Error reading cluster\n"); 411 return -1; 412 } 413 *gotsize += actsize; 414 return 0; 415 getit: 416 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { 417 printf("Error reading cluster\n"); 418 return -1; 419 } 420 *gotsize += (int)actsize; 421 filesize -= actsize; 422 buffer += actsize; 423 424 curclust = get_fatent(mydata, endclust); 425 if (CHECK_CLUST(curclust, mydata->fatsize)) { 426 debug("curclust: 0x%x\n", curclust); 427 printf("Invalid FAT entry\n"); 428 return 0; 429 } 430 actsize = bytesperclust; 431 endclust = curclust; 432 } while (1); 433 } 434 435 /* 436 * Extract the file name information from 'slotptr' into 'l_name', 437 * starting at l_name[*idx]. 438 * Return 1 if terminator (zero byte) is found, 0 otherwise. 439 */ 440 static int slot2str(dir_slot *slotptr, char *l_name, int *idx) 441 { 442 int j; 443 444 for (j = 0; j <= 8; j += 2) { 445 l_name[*idx] = slotptr->name0_4[j]; 446 if (l_name[*idx] == 0x00) 447 return 1; 448 (*idx)++; 449 } 450 for (j = 0; j <= 10; j += 2) { 451 l_name[*idx] = slotptr->name5_10[j]; 452 if (l_name[*idx] == 0x00) 453 return 1; 454 (*idx)++; 455 } 456 for (j = 0; j <= 2; j += 2) { 457 l_name[*idx] = slotptr->name11_12[j]; 458 if (l_name[*idx] == 0x00) 459 return 1; 460 (*idx)++; 461 } 462 463 return 0; 464 } 465 466 /* 467 * Extract the full long filename starting at 'retdent' (which is really 468 * a slot) into 'l_name'. If successful also copy the real directory entry 469 * into 'retdent' 470 * Return 0 on success, -1 otherwise. 471 */ 472 static int 473 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster, 474 dir_entry *retdent, char *l_name) 475 { 476 dir_entry *realdent; 477 dir_slot *slotptr = (dir_slot *)retdent; 478 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ? 479 PREFETCH_BLOCKS : 480 mydata->clust_size); 481 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff; 482 int idx = 0; 483 484 if (counter > VFAT_MAXSEQ) { 485 debug("Error: VFAT name is too long\n"); 486 return -1; 487 } 488 489 while ((__u8 *)slotptr < buflimit) { 490 if (counter == 0) 491 break; 492 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter) 493 return -1; 494 slotptr++; 495 counter--; 496 } 497 498 if ((__u8 *)slotptr >= buflimit) { 499 dir_slot *slotptr2; 500 501 if (curclust == 0) 502 return -1; 503 curclust = get_fatent(mydata, curclust); 504 if (CHECK_CLUST(curclust, mydata->fatsize)) { 505 debug("curclust: 0x%x\n", curclust); 506 printf("Invalid FAT entry\n"); 507 return -1; 508 } 509 510 if (get_cluster(mydata, curclust, get_contents_vfatname_block, 511 mydata->clust_size * mydata->sect_size) != 0) { 512 debug("Error: reading directory block\n"); 513 return -1; 514 } 515 516 slotptr2 = (dir_slot *)get_contents_vfatname_block; 517 while (counter > 0) { 518 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK) 519 & 0xff) != counter) 520 return -1; 521 slotptr2++; 522 counter--; 523 } 524 525 /* Save the real directory entry */ 526 realdent = (dir_entry *)slotptr2; 527 while ((__u8 *)slotptr2 > get_contents_vfatname_block) { 528 slotptr2--; 529 slot2str(slotptr2, l_name, &idx); 530 } 531 } else { 532 /* Save the real directory entry */ 533 realdent = (dir_entry *)slotptr; 534 } 535 536 do { 537 slotptr--; 538 if (slot2str(slotptr, l_name, &idx)) 539 break; 540 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK)); 541 542 l_name[idx] = '\0'; 543 if (*l_name == DELETED_FLAG) 544 *l_name = '\0'; 545 else if (*l_name == aRING) 546 *l_name = DELETED_FLAG; 547 downcase(l_name); 548 549 /* Return the real directory entry */ 550 memcpy(retdent, realdent, sizeof(dir_entry)); 551 552 return 0; 553 } 554 555 /* Calculate short name checksum */ 556 static __u8 mkcksum(const char name[8], const char ext[3]) 557 { 558 int i; 559 560 __u8 ret = 0; 561 562 for (i = 0; i < 8; i++) 563 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i]; 564 for (i = 0; i < 3; i++) 565 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i]; 566 567 return ret; 568 } 569 570 /* 571 * Get the directory entry associated with 'filename' from the directory 572 * starting at 'startsect' 573 */ 574 __u8 get_dentfromdir_block[MAX_CLUSTSIZE] 575 __aligned(ARCH_DMA_MINALIGN); 576 577 static dir_entry *get_dentfromdir(fsdata *mydata, int startsect, 578 char *filename, dir_entry *retdent, 579 int dols) 580 { 581 __u16 prevcksum = 0xffff; 582 __u32 curclust = START(retdent); 583 int files = 0, dirs = 0; 584 585 debug("get_dentfromdir: %s\n", filename); 586 587 while (1) { 588 dir_entry *dentptr; 589 590 int i; 591 592 if (get_cluster(mydata, curclust, get_dentfromdir_block, 593 mydata->clust_size * mydata->sect_size) != 0) { 594 debug("Error: reading directory block\n"); 595 return NULL; 596 } 597 598 dentptr = (dir_entry *)get_dentfromdir_block; 599 600 for (i = 0; i < DIRENTSPERCLUST; i++) { 601 char s_name[14], l_name[VFAT_MAXLEN_BYTES]; 602 603 l_name[0] = '\0'; 604 if (dentptr->name[0] == DELETED_FLAG) { 605 dentptr++; 606 continue; 607 } 608 if ((dentptr->attr & ATTR_VOLUME)) { 609 if (vfat_enabled && 610 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT && 611 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { 612 prevcksum = ((dir_slot *)dentptr)->alias_checksum; 613 get_vfatname(mydata, curclust, 614 get_dentfromdir_block, 615 dentptr, l_name); 616 if (dols) { 617 int isdir; 618 char dirc; 619 int doit = 0; 620 621 isdir = (dentptr->attr & ATTR_DIR); 622 623 if (isdir) { 624 dirs++; 625 dirc = '/'; 626 doit = 1; 627 } else { 628 dirc = ' '; 629 if (l_name[0] != 0) { 630 files++; 631 doit = 1; 632 } 633 } 634 if (doit) { 635 if (dirc == ' ') { 636 printf(" %8u %s%c\n", 637 FAT2CPU32(dentptr->size), 638 l_name, 639 dirc); 640 } else { 641 printf(" %s%c\n", 642 l_name, 643 dirc); 644 } 645 } 646 dentptr++; 647 continue; 648 } 649 debug("vfatname: |%s|\n", l_name); 650 } else { 651 /* Volume label or VFAT entry */ 652 dentptr++; 653 continue; 654 } 655 } 656 if (dentptr->name[0] == 0) { 657 if (dols) { 658 printf("\n%d file(s), %d dir(s)\n\n", 659 files, dirs); 660 } 661 debug("Dentname == NULL - %d\n", i); 662 return NULL; 663 } 664 if (vfat_enabled) { 665 __u8 csum = mkcksum(dentptr->name, dentptr->ext); 666 if (dols && csum == prevcksum) { 667 prevcksum = 0xffff; 668 dentptr++; 669 continue; 670 } 671 } 672 673 get_name(dentptr, s_name); 674 if (dols) { 675 int isdir = (dentptr->attr & ATTR_DIR); 676 char dirc; 677 int doit = 0; 678 679 if (isdir) { 680 dirs++; 681 dirc = '/'; 682 doit = 1; 683 } else { 684 dirc = ' '; 685 if (s_name[0] != 0) { 686 files++; 687 doit = 1; 688 } 689 } 690 691 if (doit) { 692 if (dirc == ' ') { 693 printf(" %8u %s%c\n", 694 FAT2CPU32(dentptr->size), 695 s_name, dirc); 696 } else { 697 printf(" %s%c\n", 698 s_name, dirc); 699 } 700 } 701 702 dentptr++; 703 continue; 704 } 705 706 if (strcmp(filename, s_name) 707 && strcmp(filename, l_name)) { 708 debug("Mismatch: |%s|%s|\n", s_name, l_name); 709 dentptr++; 710 continue; 711 } 712 713 memcpy(retdent, dentptr, sizeof(dir_entry)); 714 715 debug("DentName: %s", s_name); 716 debug(", start: 0x%x", START(dentptr)); 717 debug(", size: 0x%x %s\n", 718 FAT2CPU32(dentptr->size), 719 (dentptr->attr & ATTR_DIR) ? "(DIR)" : ""); 720 721 return retdent; 722 } 723 724 curclust = get_fatent(mydata, curclust); 725 if (CHECK_CLUST(curclust, mydata->fatsize)) { 726 debug("curclust: 0x%x\n", curclust); 727 printf("Invalid FAT entry\n"); 728 return NULL; 729 } 730 } 731 732 return NULL; 733 } 734 735 /* 736 * Read boot sector and volume info from a FAT filesystem 737 */ 738 static int 739 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) 740 { 741 __u8 *block; 742 volume_info *vistart; 743 int ret = 0; 744 745 if (cur_dev == NULL) { 746 debug("Error: no device selected\n"); 747 return -1; 748 } 749 750 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz); 751 if (block == NULL) { 752 debug("Error: allocating block\n"); 753 return -1; 754 } 755 756 if (disk_read(0, 1, block) < 0) { 757 debug("Error: reading block\n"); 758 goto fail; 759 } 760 761 memcpy(bs, block, sizeof(boot_sector)); 762 bs->reserved = FAT2CPU16(bs->reserved); 763 bs->fat_length = FAT2CPU16(bs->fat_length); 764 bs->secs_track = FAT2CPU16(bs->secs_track); 765 bs->heads = FAT2CPU16(bs->heads); 766 bs->total_sect = FAT2CPU32(bs->total_sect); 767 768 /* FAT32 entries */ 769 if (bs->fat_length == 0) { 770 /* Assume FAT32 */ 771 bs->fat32_length = FAT2CPU32(bs->fat32_length); 772 bs->flags = FAT2CPU16(bs->flags); 773 bs->root_cluster = FAT2CPU32(bs->root_cluster); 774 bs->info_sector = FAT2CPU16(bs->info_sector); 775 bs->backup_boot = FAT2CPU16(bs->backup_boot); 776 vistart = (volume_info *)(block + sizeof(boot_sector)); 777 *fatsize = 32; 778 } else { 779 vistart = (volume_info *)&(bs->fat32_length); 780 *fatsize = 0; 781 } 782 memcpy(volinfo, vistart, sizeof(volume_info)); 783 784 if (*fatsize == 32) { 785 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) 786 goto exit; 787 } else { 788 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { 789 *fatsize = 12; 790 goto exit; 791 } 792 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { 793 *fatsize = 16; 794 goto exit; 795 } 796 } 797 798 debug("Error: broken fs_type sign\n"); 799 fail: 800 ret = -1; 801 exit: 802 free(block); 803 return ret; 804 } 805 806 __u8 do_fat_read_at_block[MAX_CLUSTSIZE] 807 __aligned(ARCH_DMA_MINALIGN); 808 809 int do_fat_read_at(const char *filename, loff_t pos, void *buffer, 810 loff_t maxsize, int dols, int dogetsize, loff_t *size) 811 { 812 char fnamecopy[2048]; 813 boot_sector bs; 814 volume_info volinfo; 815 fsdata datablock; 816 fsdata *mydata = &datablock; 817 dir_entry *dentptr = NULL; 818 __u16 prevcksum = 0xffff; 819 char *subname = ""; 820 __u32 cursect; 821 int idx, isdir = 0; 822 int files = 0, dirs = 0; 823 int ret = -1; 824 int firsttime; 825 __u32 root_cluster = 0; 826 __u32 read_blk; 827 int rootdir_size = 0; 828 int buffer_blk_cnt; 829 int do_read; 830 __u8 *dir_ptr; 831 832 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) { 833 debug("Error: reading boot sector\n"); 834 return -1; 835 } 836 837 if (mydata->fatsize == 32) { 838 root_cluster = bs.root_cluster; 839 mydata->fatlength = bs.fat32_length; 840 } else { 841 mydata->fatlength = bs.fat_length; 842 } 843 844 mydata->fat_sect = bs.reserved; 845 846 cursect = mydata->rootdir_sect 847 = mydata->fat_sect + mydata->fatlength * bs.fats; 848 849 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; 850 mydata->clust_size = bs.cluster_size; 851 if (mydata->sect_size != cur_part_info.blksz) { 852 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n", 853 mydata->sect_size, cur_part_info.blksz); 854 return -1; 855 } 856 857 if (mydata->fatsize == 32) { 858 mydata->data_begin = mydata->rootdir_sect - 859 (mydata->clust_size * 2); 860 } else { 861 rootdir_size = ((bs.dir_entries[1] * (int)256 + 862 bs.dir_entries[0]) * 863 sizeof(dir_entry)) / 864 mydata->sect_size; 865 mydata->data_begin = mydata->rootdir_sect + 866 rootdir_size - 867 (mydata->clust_size * 2); 868 } 869 870 mydata->fatbufnum = -1; 871 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE); 872 if (mydata->fatbuf == NULL) { 873 debug("Error: allocating memory\n"); 874 return -1; 875 } 876 877 if (vfat_enabled) 878 debug("VFAT Support enabled\n"); 879 880 debug("FAT%d, fat_sect: %d, fatlength: %d\n", 881 mydata->fatsize, mydata->fat_sect, mydata->fatlength); 882 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n" 883 "Data begins at: %d\n", 884 root_cluster, 885 mydata->rootdir_sect, 886 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin); 887 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size, 888 mydata->clust_size); 889 890 /* "cwd" is always the root... */ 891 while (ISDIRDELIM(*filename)) 892 filename++; 893 894 /* Make a copy of the filename and convert it to lowercase */ 895 strcpy(fnamecopy, filename); 896 downcase(fnamecopy); 897 898 root_reparse: 899 if (*fnamecopy == '\0') { 900 if (!dols) 901 goto exit; 902 903 dols = LS_ROOT; 904 } else if ((idx = dirdelim(fnamecopy)) >= 0) { 905 isdir = 1; 906 fnamecopy[idx] = '\0'; 907 subname = fnamecopy + idx + 1; 908 909 /* Handle multiple delimiters */ 910 while (ISDIRDELIM(*subname)) 911 subname++; 912 } else if (dols) { 913 isdir = 1; 914 } 915 916 buffer_blk_cnt = 0; 917 firsttime = 1; 918 while (1) { 919 int i; 920 921 if (mydata->fatsize == 32 || firsttime) { 922 dir_ptr = do_fat_read_at_block; 923 firsttime = 0; 924 } else { 925 /** 926 * FAT16 sector buffer modification: 927 * Each loop, the second buffered block is moved to 928 * the buffer begin, and two next sectors are read 929 * next to the previously moved one. So the sector 930 * buffer keeps always 3 sectors for fat16. 931 * And the current sector is the buffer second sector 932 * beside the "firsttime" read, when it is the first one. 933 * 934 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1] 935 * n = computed root dir sector 936 * loop | cursect-1 | cursect | cursect+1 | 937 * 0 | sector n+0 | sector n+1 | none | 938 * 1 | none | sector n+0 | sector n+1 | 939 * 0 | sector n+1 | sector n+2 | sector n+3 | 940 * 1 | sector n+3 | ... 941 */ 942 dir_ptr = (do_fat_read_at_block + mydata->sect_size); 943 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size); 944 } 945 946 do_read = 1; 947 948 if (mydata->fatsize == 32 && buffer_blk_cnt) 949 do_read = 0; 950 951 if (do_read) { 952 read_blk = (mydata->fatsize == 32) ? 953 mydata->clust_size : PREFETCH_BLOCKS; 954 955 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n", 956 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK); 957 958 if (disk_read(cursect, read_blk, dir_ptr) < 0) { 959 debug("Error: reading rootdir block\n"); 960 goto exit; 961 } 962 963 dentptr = (dir_entry *)dir_ptr; 964 } 965 966 for (i = 0; i < DIRENTSPERBLOCK; i++) { 967 char s_name[14], l_name[VFAT_MAXLEN_BYTES]; 968 __u8 csum; 969 970 l_name[0] = '\0'; 971 if (dentptr->name[0] == DELETED_FLAG) { 972 dentptr++; 973 continue; 974 } 975 976 if (vfat_enabled) 977 csum = mkcksum(dentptr->name, dentptr->ext); 978 979 if (dentptr->attr & ATTR_VOLUME) { 980 if (vfat_enabled && 981 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT && 982 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { 983 prevcksum = 984 ((dir_slot *)dentptr)->alias_checksum; 985 986 get_vfatname(mydata, 987 root_cluster, 988 dir_ptr, 989 dentptr, l_name); 990 991 if (dols == LS_ROOT) { 992 char dirc; 993 int doit = 0; 994 int isdir = 995 (dentptr->attr & ATTR_DIR); 996 997 if (isdir) { 998 dirs++; 999 dirc = '/'; 1000 doit = 1; 1001 } else { 1002 dirc = ' '; 1003 if (l_name[0] != 0) { 1004 files++; 1005 doit = 1; 1006 } 1007 } 1008 if (doit) { 1009 if (dirc == ' ') { 1010 printf(" %8u %s%c\n", 1011 FAT2CPU32(dentptr->size), 1012 l_name, 1013 dirc); 1014 } else { 1015 printf(" %s%c\n", 1016 l_name, 1017 dirc); 1018 } 1019 } 1020 dentptr++; 1021 continue; 1022 } 1023 debug("Rootvfatname: |%s|\n", 1024 l_name); 1025 } else { 1026 /* Volume label or VFAT entry */ 1027 dentptr++; 1028 continue; 1029 } 1030 } else if (dentptr->name[0] == 0) { 1031 debug("RootDentname == NULL - %d\n", i); 1032 if (dols == LS_ROOT) { 1033 printf("\n%d file(s), %d dir(s)\n\n", 1034 files, dirs); 1035 ret = 0; 1036 } 1037 goto exit; 1038 } 1039 else if (vfat_enabled && 1040 dols == LS_ROOT && csum == prevcksum) { 1041 prevcksum = 0xffff; 1042 dentptr++; 1043 continue; 1044 } 1045 1046 get_name(dentptr, s_name); 1047 1048 if (dols == LS_ROOT) { 1049 int isdir = (dentptr->attr & ATTR_DIR); 1050 char dirc; 1051 int doit = 0; 1052 1053 if (isdir) { 1054 dirc = '/'; 1055 if (s_name[0] != 0) { 1056 dirs++; 1057 doit = 1; 1058 } 1059 } else { 1060 dirc = ' '; 1061 if (s_name[0] != 0) { 1062 files++; 1063 doit = 1; 1064 } 1065 } 1066 if (doit) { 1067 if (dirc == ' ') { 1068 printf(" %8u %s%c\n", 1069 FAT2CPU32(dentptr->size), 1070 s_name, dirc); 1071 } else { 1072 printf(" %s%c\n", 1073 s_name, dirc); 1074 } 1075 } 1076 dentptr++; 1077 continue; 1078 } 1079 1080 if (strcmp(fnamecopy, s_name) 1081 && strcmp(fnamecopy, l_name)) { 1082 debug("RootMismatch: |%s|%s|\n", s_name, 1083 l_name); 1084 dentptr++; 1085 continue; 1086 } 1087 1088 if (isdir && !(dentptr->attr & ATTR_DIR)) 1089 goto exit; 1090 1091 debug("RootName: %s", s_name); 1092 debug(", start: 0x%x", START(dentptr)); 1093 debug(", size: 0x%x %s\n", 1094 FAT2CPU32(dentptr->size), 1095 isdir ? "(DIR)" : ""); 1096 1097 goto rootdir_done; /* We got a match */ 1098 } 1099 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt, 1100 mydata->clust_size); 1101 1102 /* 1103 * On FAT32 we must fetch the FAT entries for the next 1104 * root directory clusters when a cluster has been 1105 * completely processed. 1106 */ 1107 ++buffer_blk_cnt; 1108 int rootdir_end = 0; 1109 if (mydata->fatsize == 32) { 1110 if (buffer_blk_cnt == mydata->clust_size) { 1111 int nxtsect = 0; 1112 int nxt_clust = 0; 1113 1114 nxt_clust = get_fatent(mydata, root_cluster); 1115 rootdir_end = CHECK_CLUST(nxt_clust, 32); 1116 1117 nxtsect = mydata->data_begin + 1118 (nxt_clust * mydata->clust_size); 1119 1120 root_cluster = nxt_clust; 1121 1122 cursect = nxtsect; 1123 buffer_blk_cnt = 0; 1124 } 1125 } else { 1126 if (buffer_blk_cnt == PREFETCH_BLOCKS) 1127 buffer_blk_cnt = 0; 1128 1129 rootdir_end = (++cursect - mydata->rootdir_sect >= 1130 rootdir_size); 1131 } 1132 1133 /* If end of rootdir reached */ 1134 if (rootdir_end) { 1135 if (dols == LS_ROOT) { 1136 printf("\n%d file(s), %d dir(s)\n\n", 1137 files, dirs); 1138 *size = 0; 1139 } 1140 goto exit; 1141 } 1142 } 1143 rootdir_done: 1144 1145 firsttime = 1; 1146 1147 while (isdir) { 1148 int startsect = mydata->data_begin 1149 + START(dentptr) * mydata->clust_size; 1150 dir_entry dent; 1151 char *nextname = NULL; 1152 1153 dent = *dentptr; 1154 dentptr = &dent; 1155 1156 idx = dirdelim(subname); 1157 1158 if (idx >= 0) { 1159 subname[idx] = '\0'; 1160 nextname = subname + idx + 1; 1161 /* Handle multiple delimiters */ 1162 while (ISDIRDELIM(*nextname)) 1163 nextname++; 1164 if (dols && *nextname == '\0') 1165 firsttime = 0; 1166 } else { 1167 if (dols && firsttime) { 1168 firsttime = 0; 1169 } else { 1170 isdir = 0; 1171 } 1172 } 1173 1174 if (get_dentfromdir(mydata, startsect, subname, dentptr, 1175 isdir ? 0 : dols) == NULL) { 1176 if (dols && !isdir) 1177 *size = 0; 1178 goto exit; 1179 } 1180 1181 if (isdir && !(dentptr->attr & ATTR_DIR)) 1182 goto exit; 1183 1184 /* 1185 * If we are looking for a directory, and found a directory 1186 * type entry, and the entry is for the root directory (as 1187 * denoted by a cluster number of 0), jump back to the start 1188 * of the function, since at least on FAT12/16, the root dir 1189 * lives in a hard-coded location and needs special handling 1190 * to parse, rather than simply following the cluster linked 1191 * list in the FAT, like other directories. 1192 */ 1193 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) { 1194 /* 1195 * Modify the filename to remove the prefix that gets 1196 * back to the root directory, so the initial root dir 1197 * parsing code can continue from where we are without 1198 * confusion. 1199 */ 1200 strcpy(fnamecopy, nextname ?: ""); 1201 /* 1202 * Set up state the same way as the function does when 1203 * first started. This is required for the root dir 1204 * parsing code operates in its expected environment. 1205 */ 1206 subname = ""; 1207 cursect = mydata->rootdir_sect; 1208 isdir = 0; 1209 goto root_reparse; 1210 } 1211 1212 if (idx >= 0) 1213 subname = nextname; 1214 } 1215 1216 if (dogetsize) { 1217 *size = FAT2CPU32(dentptr->size); 1218 ret = 0; 1219 } else { 1220 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size); 1221 } 1222 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size); 1223 1224 exit: 1225 free(mydata->fatbuf); 1226 return ret; 1227 } 1228 1229 int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols, 1230 loff_t *actread) 1231 { 1232 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread); 1233 } 1234 1235 int file_fat_detectfs(void) 1236 { 1237 boot_sector bs; 1238 volume_info volinfo; 1239 int fatsize; 1240 char vol_label[12]; 1241 1242 if (cur_dev == NULL) { 1243 printf("No current device\n"); 1244 return 1; 1245 } 1246 1247 #if defined(CONFIG_CMD_IDE) || \ 1248 defined(CONFIG_CMD_SATA) || \ 1249 defined(CONFIG_CMD_SCSI) || \ 1250 defined(CONFIG_CMD_USB) || \ 1251 defined(CONFIG_MMC) 1252 printf("Interface: "); 1253 switch (cur_dev->if_type) { 1254 case IF_TYPE_IDE: 1255 printf("IDE"); 1256 break; 1257 case IF_TYPE_SATA: 1258 printf("SATA"); 1259 break; 1260 case IF_TYPE_SCSI: 1261 printf("SCSI"); 1262 break; 1263 case IF_TYPE_ATAPI: 1264 printf("ATAPI"); 1265 break; 1266 case IF_TYPE_USB: 1267 printf("USB"); 1268 break; 1269 case IF_TYPE_DOC: 1270 printf("DOC"); 1271 break; 1272 case IF_TYPE_MMC: 1273 printf("MMC"); 1274 break; 1275 default: 1276 printf("Unknown"); 1277 } 1278 1279 printf("\n Device %d: ", cur_dev->dev); 1280 dev_print(cur_dev); 1281 #endif 1282 1283 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) { 1284 printf("\nNo valid FAT fs found\n"); 1285 return 1; 1286 } 1287 1288 memcpy(vol_label, volinfo.volume_label, 11); 1289 vol_label[11] = '\0'; 1290 volinfo.fs_type[5] = '\0'; 1291 1292 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label); 1293 1294 return 0; 1295 } 1296 1297 int file_fat_ls(const char *dir) 1298 { 1299 loff_t size; 1300 1301 return do_fat_read(dir, NULL, 0, LS_YES, &size); 1302 } 1303 1304 int fat_exists(const char *filename) 1305 { 1306 int ret; 1307 loff_t size; 1308 1309 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size); 1310 return ret == 0; 1311 } 1312 1313 int fat_size(const char *filename, loff_t *size) 1314 { 1315 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size); 1316 } 1317 1318 int file_fat_read_at(const char *filename, loff_t pos, void *buffer, 1319 loff_t maxsize, loff_t *actread) 1320 { 1321 printf("reading %s\n", filename); 1322 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0, 1323 actread); 1324 } 1325 1326 int file_fat_read(const char *filename, void *buffer, int maxsize) 1327 { 1328 loff_t actread; 1329 int ret; 1330 1331 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread); 1332 if (ret) 1333 return ret; 1334 else 1335 return actread; 1336 } 1337 1338 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len, 1339 loff_t *actread) 1340 { 1341 int ret; 1342 1343 ret = file_fat_read_at(filename, offset, buf, len, actread); 1344 if (ret) 1345 printf("** Unable to read file %s **\n", filename); 1346 1347 return ret; 1348 } 1349 1350 void fat_close(void) 1351 { 1352 } 1353