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 * See file CREDITS for list of people who contributed to this 10 * project. 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License as 14 * published by the Free Software Foundation; either version 2 of 15 * the License, or (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 25 * MA 02111-1307 USA 26 */ 27 28 #include <common.h> 29 #include <config.h> 30 #include <exports.h> 31 #include <fat.h> 32 #include <asm/byteorder.h> 33 #include <part.h> 34 35 /* 36 * Convert a string to lowercase. 37 */ 38 static void downcase (char *str) 39 { 40 while (*str != '\0') { 41 TOLOWER(*str); 42 str++; 43 } 44 } 45 46 static block_dev_desc_t *cur_dev = NULL; 47 48 static unsigned long part_offset = 0; 49 50 static int cur_part = 1; 51 52 #define DOS_PART_TBL_OFFSET 0x1be 53 #define DOS_PART_MAGIC_OFFSET 0x1fe 54 #define DOS_FS_TYPE_OFFSET 0x36 55 #define DOS_FS32_TYPE_OFFSET 0x52 56 57 static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr) 58 { 59 if (cur_dev == NULL) 60 return -1; 61 62 startblock += part_offset; 63 64 if (cur_dev->block_read) { 65 return cur_dev->block_read(cur_dev->dev, startblock, getsize, 66 (unsigned long *) bufptr); 67 } 68 return -1; 69 } 70 71 int fat_register_device (block_dev_desc_t * dev_desc, int part_no) 72 { 73 unsigned char buffer[dev_desc->blksz]; 74 75 if (!dev_desc->block_read) 76 return -1; 77 78 cur_dev = dev_desc; 79 /* check if we have a MBR (on floppies we have only a PBR) */ 80 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)buffer) != 1) { 81 printf("** Can't read from device %d **\n", 82 dev_desc->dev); 83 return -1; 84 } 85 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || 86 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { 87 /* no signature found */ 88 return -1; 89 } 90 #if (defined(CONFIG_CMD_IDE) || \ 91 defined(CONFIG_CMD_MG_DISK) || \ 92 defined(CONFIG_CMD_SATA) || \ 93 defined(CONFIG_CMD_SCSI) || \ 94 defined(CONFIG_CMD_USB) || \ 95 defined(CONFIG_MMC) || \ 96 defined(CONFIG_SYSTEMACE) ) 97 { 98 disk_partition_t info; 99 100 /* First we assume there is a MBR */ 101 if (!get_partition_info(dev_desc, part_no, &info)) { 102 part_offset = info.start; 103 cur_part = part_no; 104 } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], 105 "FAT", 3) == 0) || 106 (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], 107 "FAT32", 5) == 0)) { 108 /* ok, we assume we are on a PBR only */ 109 cur_part = 1; 110 part_offset = 0; 111 } else { 112 printf("** Partition %d not valid on device %d **\n", 113 part_no, dev_desc->dev); 114 return -1; 115 } 116 } 117 #else 118 if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) || 119 (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) { 120 /* ok, we assume we are on a PBR only */ 121 cur_part = 1; 122 part_offset = 0; 123 } else { 124 /* FIXME we need to determine the start block of the 125 * partition where the DOS FS resides. This can be done 126 * by using the get_partition_info routine. For this 127 * purpose the libpart must be included. 128 */ 129 part_offset = 32; 130 cur_part = 1; 131 } 132 #endif 133 return 0; 134 } 135 136 /* 137 * Get the first occurence of a directory delimiter ('/' or '\') in a string. 138 * Return index into string if found, -1 otherwise. 139 */ 140 static int dirdelim (char *str) 141 { 142 char *start = str; 143 144 while (*str != '\0') { 145 if (ISDIRDELIM(*str)) 146 return str - start; 147 str++; 148 } 149 return -1; 150 } 151 152 /* 153 * Extract zero terminated short name from a directory entry. 154 */ 155 static void get_name (dir_entry *dirent, char *s_name) 156 { 157 char *ptr; 158 159 memcpy(s_name, dirent->name, 8); 160 s_name[8] = '\0'; 161 ptr = s_name; 162 while (*ptr && *ptr != ' ') 163 ptr++; 164 if (dirent->ext[0] && dirent->ext[0] != ' ') { 165 *ptr = '.'; 166 ptr++; 167 memcpy(ptr, dirent->ext, 3); 168 ptr[3] = '\0'; 169 while (*ptr && *ptr != ' ') 170 ptr++; 171 } 172 *ptr = '\0'; 173 if (*s_name == DELETED_FLAG) 174 *s_name = '\0'; 175 else if (*s_name == aRING) 176 *s_name = DELETED_FLAG; 177 downcase(s_name); 178 } 179 180 /* 181 * Get the entry at index 'entry' in a FAT (12/16/32) table. 182 * On failure 0x00 is returned. 183 */ 184 static __u32 get_fatent (fsdata *mydata, __u32 entry) 185 { 186 __u32 bufnum; 187 __u32 off16, offset; 188 __u32 ret = 0x00; 189 __u16 val1, val2; 190 191 switch (mydata->fatsize) { 192 case 32: 193 bufnum = entry / FAT32BUFSIZE; 194 offset = entry - bufnum * FAT32BUFSIZE; 195 break; 196 case 16: 197 bufnum = entry / FAT16BUFSIZE; 198 offset = entry - bufnum * FAT16BUFSIZE; 199 break; 200 case 12: 201 bufnum = entry / FAT12BUFSIZE; 202 offset = entry - bufnum * FAT12BUFSIZE; 203 break; 204 205 default: 206 /* Unsupported FAT size */ 207 return ret; 208 } 209 210 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n", 211 mydata->fatsize, entry, entry, offset, offset); 212 213 /* Read a new block of FAT entries into the cache. */ 214 if (bufnum != mydata->fatbufnum) { 215 __u32 getsize = FATBUFBLOCKS; 216 __u8 *bufptr = mydata->fatbuf; 217 __u32 fatlength = mydata->fatlength; 218 __u32 startblock = bufnum * FATBUFBLOCKS; 219 220 if (getsize > fatlength) 221 getsize = fatlength; 222 223 fatlength *= mydata->sect_size; /* We want it in bytes now */ 224 startblock += mydata->fat_sect; /* Offset from start of disk */ 225 226 if (disk_read(startblock, getsize, bufptr) < 0) { 227 debug("Error reading FAT blocks\n"); 228 return ret; 229 } 230 mydata->fatbufnum = bufnum; 231 } 232 233 /* Get the actual entry from the table */ 234 switch (mydata->fatsize) { 235 case 32: 236 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); 237 break; 238 case 16: 239 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); 240 break; 241 case 12: 242 off16 = (offset * 3) / 4; 243 244 switch (offset & 0x3) { 245 case 0: 246 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]); 247 ret &= 0xfff; 248 break; 249 case 1: 250 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 251 val1 &= 0xf000; 252 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); 253 val2 &= 0x00ff; 254 ret = (val2 << 4) | (val1 >> 12); 255 break; 256 case 2: 257 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 258 val1 &= 0xff00; 259 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); 260 val2 &= 0x000f; 261 ret = (val2 << 8) | (val1 >> 8); 262 break; 263 case 3: 264 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); 265 ret = (ret & 0xfff0) >> 4; 266 break; 267 default: 268 break; 269 } 270 break; 271 } 272 debug("FAT%d: ret: %08x, offset: %04x\n", 273 mydata->fatsize, ret, offset); 274 275 return ret; 276 } 277 278 /* 279 * Read at most 'size' bytes from the specified cluster into 'buffer'. 280 * Return 0 on success, -1 otherwise. 281 */ 282 static int 283 get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer, 284 unsigned long size) 285 { 286 __u32 idx = 0; 287 __u32 startsect; 288 289 if (clustnum > 0) { 290 startsect = mydata->data_begin + 291 clustnum * mydata->clust_size; 292 } else { 293 startsect = mydata->rootdir_sect; 294 } 295 296 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect); 297 298 if (disk_read(startsect, size / mydata->sect_size, buffer) < 0) { 299 debug("Error reading data\n"); 300 return -1; 301 } 302 if (size % mydata->sect_size) { 303 __u8 tmpbuf[mydata->sect_size]; 304 305 idx = size / mydata->sect_size; 306 if (disk_read(startsect + idx, 1, tmpbuf) < 0) { 307 debug("Error reading data\n"); 308 return -1; 309 } 310 buffer += idx * mydata->sect_size; 311 312 memcpy(buffer, tmpbuf, size % mydata->sect_size); 313 return 0; 314 } 315 316 return 0; 317 } 318 319 /* 320 * Read at most 'maxsize' bytes from the file associated with 'dentptr' 321 * into 'buffer'. 322 * Return the number of bytes read or -1 on fatal errors. 323 */ 324 static long 325 get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer, 326 unsigned long maxsize) 327 { 328 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0; 329 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; 330 __u32 curclust = START(dentptr); 331 __u32 endclust, newclust; 332 unsigned long actsize; 333 334 debug("Filesize: %ld bytes\n", filesize); 335 336 if (maxsize > 0 && filesize > maxsize) 337 filesize = maxsize; 338 339 debug("%ld bytes\n", filesize); 340 341 actsize = bytesperclust; 342 endclust = curclust; 343 344 do { 345 /* search for consecutive clusters */ 346 while (actsize < filesize) { 347 newclust = get_fatent(mydata, endclust); 348 if ((newclust - 1) != endclust) 349 goto getit; 350 if (CHECK_CLUST(newclust, mydata->fatsize)) { 351 debug("curclust: 0x%x\n", newclust); 352 debug("Invalid FAT entry\n"); 353 return gotsize; 354 } 355 endclust = newclust; 356 actsize += bytesperclust; 357 } 358 359 /* actsize >= file size */ 360 actsize -= bytesperclust; 361 362 /* get remaining clusters */ 363 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { 364 printf("Error reading cluster\n"); 365 return -1; 366 } 367 368 /* get remaining bytes */ 369 gotsize += (int)actsize; 370 filesize -= actsize; 371 buffer += actsize; 372 actsize = filesize; 373 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) { 374 printf("Error reading cluster\n"); 375 return -1; 376 } 377 gotsize += actsize; 378 return gotsize; 379 getit: 380 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { 381 printf("Error reading cluster\n"); 382 return -1; 383 } 384 gotsize += (int)actsize; 385 filesize -= actsize; 386 buffer += actsize; 387 388 curclust = get_fatent(mydata, endclust); 389 if (CHECK_CLUST(curclust, mydata->fatsize)) { 390 debug("curclust: 0x%x\n", curclust); 391 printf("Invalid FAT entry\n"); 392 return gotsize; 393 } 394 actsize = bytesperclust; 395 endclust = curclust; 396 } while (1); 397 } 398 399 #ifdef CONFIG_SUPPORT_VFAT 400 /* 401 * Extract the file name information from 'slotptr' into 'l_name', 402 * starting at l_name[*idx]. 403 * Return 1 if terminator (zero byte) is found, 0 otherwise. 404 */ 405 static int slot2str (dir_slot *slotptr, char *l_name, int *idx) 406 { 407 int j; 408 409 for (j = 0; j <= 8; j += 2) { 410 l_name[*idx] = slotptr->name0_4[j]; 411 if (l_name[*idx] == 0x00) 412 return 1; 413 (*idx)++; 414 } 415 for (j = 0; j <= 10; j += 2) { 416 l_name[*idx] = slotptr->name5_10[j]; 417 if (l_name[*idx] == 0x00) 418 return 1; 419 (*idx)++; 420 } 421 for (j = 0; j <= 2; j += 2) { 422 l_name[*idx] = slotptr->name11_12[j]; 423 if (l_name[*idx] == 0x00) 424 return 1; 425 (*idx)++; 426 } 427 428 return 0; 429 } 430 431 /* 432 * Extract the full long filename starting at 'retdent' (which is really 433 * a slot) into 'l_name'. If successful also copy the real directory entry 434 * into 'retdent' 435 * Return 0 on success, -1 otherwise. 436 */ 437 __attribute__ ((__aligned__ (__alignof__ (dir_entry)))) 438 __u8 get_vfatname_block[MAX_CLUSTSIZE]; 439 440 static int 441 get_vfatname (fsdata *mydata, int curclust, __u8 *cluster, 442 dir_entry *retdent, char *l_name) 443 { 444 dir_entry *realdent; 445 dir_slot *slotptr = (dir_slot *)retdent; 446 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ? 447 PREFETCH_BLOCKS : 448 mydata->clust_size); 449 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff; 450 int idx = 0; 451 452 if (counter > VFAT_MAXSEQ) { 453 debug("Error: VFAT name is too long\n"); 454 return -1; 455 } 456 457 while ((__u8 *)slotptr < buflimit) { 458 if (counter == 0) 459 break; 460 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter) 461 return -1; 462 slotptr++; 463 counter--; 464 } 465 466 if ((__u8 *)slotptr >= buflimit) { 467 dir_slot *slotptr2; 468 469 if (curclust == 0) 470 return -1; 471 curclust = get_fatent(mydata, curclust); 472 if (CHECK_CLUST(curclust, mydata->fatsize)) { 473 debug("curclust: 0x%x\n", curclust); 474 printf("Invalid FAT entry\n"); 475 return -1; 476 } 477 478 if (get_cluster(mydata, curclust, get_vfatname_block, 479 mydata->clust_size * mydata->sect_size) != 0) { 480 debug("Error: reading directory block\n"); 481 return -1; 482 } 483 484 slotptr2 = (dir_slot *)get_vfatname_block; 485 while (counter > 0) { 486 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK) 487 & 0xff) != counter) 488 return -1; 489 slotptr2++; 490 counter--; 491 } 492 493 /* Save the real directory entry */ 494 realdent = (dir_entry *)slotptr2; 495 while ((__u8 *)slotptr2 > get_vfatname_block) { 496 slotptr2--; 497 slot2str(slotptr2, l_name, &idx); 498 } 499 } else { 500 /* Save the real directory entry */ 501 realdent = (dir_entry *)slotptr; 502 } 503 504 do { 505 slotptr--; 506 if (slot2str(slotptr, l_name, &idx)) 507 break; 508 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK)); 509 510 l_name[idx] = '\0'; 511 if (*l_name == DELETED_FLAG) 512 *l_name = '\0'; 513 else if (*l_name == aRING) 514 *l_name = DELETED_FLAG; 515 downcase(l_name); 516 517 /* Return the real directory entry */ 518 memcpy(retdent, realdent, sizeof(dir_entry)); 519 520 return 0; 521 } 522 523 /* Calculate short name checksum */ 524 static __u8 mkcksum (const char *str) 525 { 526 int i; 527 528 __u8 ret = 0; 529 530 for (i = 0; i < 11; i++) { 531 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i]; 532 } 533 534 return ret; 535 } 536 #endif /* CONFIG_SUPPORT_VFAT */ 537 538 /* 539 * Get the directory entry associated with 'filename' from the directory 540 * starting at 'startsect' 541 */ 542 __attribute__ ((__aligned__ (__alignof__ (dir_entry)))) 543 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]; 544 545 static dir_entry *get_dentfromdir (fsdata *mydata, int startsect, 546 char *filename, dir_entry *retdent, 547 int dols) 548 { 549 __u16 prevcksum = 0xffff; 550 __u32 curclust = START(retdent); 551 int files = 0, dirs = 0; 552 553 debug("get_dentfromdir: %s\n", filename); 554 555 while (1) { 556 dir_entry *dentptr; 557 558 int i; 559 560 if (get_cluster(mydata, curclust, get_dentfromdir_block, 561 mydata->clust_size * mydata->sect_size) != 0) { 562 debug("Error: reading directory block\n"); 563 return NULL; 564 } 565 566 dentptr = (dir_entry *)get_dentfromdir_block; 567 568 for (i = 0; i < DIRENTSPERCLUST; i++) { 569 char s_name[14], l_name[VFAT_MAXLEN_BYTES]; 570 571 l_name[0] = '\0'; 572 if (dentptr->name[0] == DELETED_FLAG) { 573 dentptr++; 574 continue; 575 } 576 if ((dentptr->attr & ATTR_VOLUME)) { 577 #ifdef CONFIG_SUPPORT_VFAT 578 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT && 579 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { 580 prevcksum = ((dir_slot *)dentptr)->alias_checksum; 581 get_vfatname(mydata, curclust, 582 get_dentfromdir_block, 583 dentptr, l_name); 584 if (dols) { 585 int isdir; 586 char dirc; 587 int doit = 0; 588 589 isdir = (dentptr->attr & ATTR_DIR); 590 591 if (isdir) { 592 dirs++; 593 dirc = '/'; 594 doit = 1; 595 } else { 596 dirc = ' '; 597 if (l_name[0] != 0) { 598 files++; 599 doit = 1; 600 } 601 } 602 if (doit) { 603 if (dirc == ' ') { 604 printf(" %8ld %s%c\n", 605 (long)FAT2CPU32(dentptr->size), 606 l_name, 607 dirc); 608 } else { 609 printf(" %s%c\n", 610 l_name, 611 dirc); 612 } 613 } 614 dentptr++; 615 continue; 616 } 617 debug("vfatname: |%s|\n", l_name); 618 } else 619 #endif 620 { 621 /* Volume label or VFAT entry */ 622 dentptr++; 623 continue; 624 } 625 } 626 if (dentptr->name[0] == 0) { 627 if (dols) { 628 printf("\n%d file(s), %d dir(s)\n\n", 629 files, dirs); 630 } 631 debug("Dentname == NULL - %d\n", i); 632 return NULL; 633 } 634 #ifdef CONFIG_SUPPORT_VFAT 635 if (dols && mkcksum(dentptr->name) == prevcksum) { 636 dentptr++; 637 continue; 638 } 639 #endif 640 get_name(dentptr, s_name); 641 if (dols) { 642 int isdir = (dentptr->attr & ATTR_DIR); 643 char dirc; 644 int doit = 0; 645 646 if (isdir) { 647 dirs++; 648 dirc = '/'; 649 doit = 1; 650 } else { 651 dirc = ' '; 652 if (s_name[0] != 0) { 653 files++; 654 doit = 1; 655 } 656 } 657 658 if (doit) { 659 if (dirc == ' ') { 660 printf(" %8ld %s%c\n", 661 (long)FAT2CPU32(dentptr->size), 662 s_name, dirc); 663 } else { 664 printf(" %s%c\n", 665 s_name, dirc); 666 } 667 } 668 669 dentptr++; 670 continue; 671 } 672 673 if (strcmp(filename, s_name) 674 && strcmp(filename, l_name)) { 675 debug("Mismatch: |%s|%s|\n", s_name, l_name); 676 dentptr++; 677 continue; 678 } 679 680 memcpy(retdent, dentptr, sizeof(dir_entry)); 681 682 debug("DentName: %s", s_name); 683 debug(", start: 0x%x", START(dentptr)); 684 debug(", size: 0x%x %s\n", 685 FAT2CPU32(dentptr->size), 686 (dentptr->attr & ATTR_DIR) ? "(DIR)" : ""); 687 688 return retdent; 689 } 690 691 curclust = get_fatent(mydata, curclust); 692 if (CHECK_CLUST(curclust, mydata->fatsize)) { 693 debug("curclust: 0x%x\n", curclust); 694 printf("Invalid FAT entry\n"); 695 return NULL; 696 } 697 } 698 699 return NULL; 700 } 701 702 /* 703 * Read boot sector and volume info from a FAT filesystem 704 */ 705 static int 706 read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize) 707 { 708 __u8 *block; 709 volume_info *vistart; 710 int ret = 0; 711 712 if (cur_dev == NULL) { 713 debug("Error: no device selected\n"); 714 return -1; 715 } 716 717 block = malloc(cur_dev->blksz); 718 if (block == NULL) { 719 debug("Error: allocating block\n"); 720 return -1; 721 } 722 723 if (disk_read (0, 1, block) < 0) { 724 debug("Error: reading block\n"); 725 goto fail; 726 } 727 728 memcpy(bs, block, sizeof(boot_sector)); 729 bs->reserved = FAT2CPU16(bs->reserved); 730 bs->fat_length = FAT2CPU16(bs->fat_length); 731 bs->secs_track = FAT2CPU16(bs->secs_track); 732 bs->heads = FAT2CPU16(bs->heads); 733 bs->total_sect = FAT2CPU32(bs->total_sect); 734 735 /* FAT32 entries */ 736 if (bs->fat_length == 0) { 737 /* Assume FAT32 */ 738 bs->fat32_length = FAT2CPU32(bs->fat32_length); 739 bs->flags = FAT2CPU16(bs->flags); 740 bs->root_cluster = FAT2CPU32(bs->root_cluster); 741 bs->info_sector = FAT2CPU16(bs->info_sector); 742 bs->backup_boot = FAT2CPU16(bs->backup_boot); 743 vistart = (volume_info *)(block + sizeof(boot_sector)); 744 *fatsize = 32; 745 } else { 746 vistart = (volume_info *)&(bs->fat32_length); 747 *fatsize = 0; 748 } 749 memcpy(volinfo, vistart, sizeof(volume_info)); 750 751 if (*fatsize == 32) { 752 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) 753 goto exit; 754 } else { 755 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { 756 *fatsize = 12; 757 goto exit; 758 } 759 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { 760 *fatsize = 16; 761 goto exit; 762 } 763 } 764 765 debug("Error: broken fs_type sign\n"); 766 fail: 767 ret = -1; 768 exit: 769 free(block); 770 return ret; 771 } 772 773 __attribute__ ((__aligned__ (__alignof__ (dir_entry)))) 774 __u8 do_fat_read_block[MAX_CLUSTSIZE]; 775 776 long 777 do_fat_read (const char *filename, void *buffer, unsigned long maxsize, 778 int dols) 779 { 780 char fnamecopy[2048]; 781 boot_sector bs; 782 volume_info volinfo; 783 fsdata datablock; 784 fsdata *mydata = &datablock; 785 dir_entry *dentptr; 786 __u16 prevcksum = 0xffff; 787 char *subname = ""; 788 __u32 cursect; 789 int idx, isdir = 0; 790 int files = 0, dirs = 0; 791 long ret = -1; 792 int firsttime; 793 __u32 root_cluster = 0; 794 int rootdir_size = 0; 795 int j; 796 797 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) { 798 debug("Error: reading boot sector\n"); 799 return -1; 800 } 801 802 if (mydata->fatsize == 32) { 803 root_cluster = bs.root_cluster; 804 mydata->fatlength = bs.fat32_length; 805 } else { 806 mydata->fatlength = bs.fat_length; 807 } 808 809 mydata->fat_sect = bs.reserved; 810 811 cursect = mydata->rootdir_sect 812 = mydata->fat_sect + mydata->fatlength * bs.fats; 813 814 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; 815 mydata->clust_size = bs.cluster_size; 816 817 if (mydata->fatsize == 32) { 818 mydata->data_begin = mydata->rootdir_sect - 819 (mydata->clust_size * 2); 820 } else { 821 rootdir_size = ((bs.dir_entries[1] * (int)256 + 822 bs.dir_entries[0]) * 823 sizeof(dir_entry)) / 824 mydata->sect_size; 825 mydata->data_begin = mydata->rootdir_sect + 826 rootdir_size - 827 (mydata->clust_size * 2); 828 } 829 830 mydata->fatbufnum = -1; 831 mydata->fatbuf = malloc(FATBUFSIZE); 832 if (mydata->fatbuf == NULL) { 833 debug("Error: allocating memory\n"); 834 return -1; 835 } 836 837 #ifdef CONFIG_SUPPORT_VFAT 838 debug("VFAT Support enabled\n"); 839 #endif 840 debug("FAT%d, fat_sect: %d, fatlength: %d\n", 841 mydata->fatsize, mydata->fat_sect, mydata->fatlength); 842 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n" 843 "Data begins at: %d\n", 844 root_cluster, 845 mydata->rootdir_sect, 846 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin); 847 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size, 848 mydata->clust_size); 849 850 /* "cwd" is always the root... */ 851 while (ISDIRDELIM(*filename)) 852 filename++; 853 854 /* Make a copy of the filename and convert it to lowercase */ 855 strcpy(fnamecopy, filename); 856 downcase(fnamecopy); 857 858 if (*fnamecopy == '\0') { 859 if (!dols) 860 goto exit; 861 862 dols = LS_ROOT; 863 } else if ((idx = dirdelim(fnamecopy)) >= 0) { 864 isdir = 1; 865 fnamecopy[idx] = '\0'; 866 subname = fnamecopy + idx + 1; 867 868 /* Handle multiple delimiters */ 869 while (ISDIRDELIM(*subname)) 870 subname++; 871 } else if (dols) { 872 isdir = 1; 873 } 874 875 j = 0; 876 while (1) { 877 int i; 878 879 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n", 880 cursect, mydata->clust_size, DIRENTSPERBLOCK); 881 882 if (disk_read(cursect, 883 (mydata->fatsize == 32) ? 884 (mydata->clust_size) : 885 PREFETCH_BLOCKS, 886 do_fat_read_block) < 0) { 887 debug("Error: reading rootdir block\n"); 888 goto exit; 889 } 890 891 dentptr = (dir_entry *) do_fat_read_block; 892 893 for (i = 0; i < DIRENTSPERBLOCK; i++) { 894 char s_name[14], l_name[VFAT_MAXLEN_BYTES]; 895 896 l_name[0] = '\0'; 897 if (dentptr->name[0] == DELETED_FLAG) { 898 dentptr++; 899 continue; 900 } 901 if ((dentptr->attr & ATTR_VOLUME)) { 902 #ifdef CONFIG_SUPPORT_VFAT 903 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT && 904 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { 905 prevcksum = 906 ((dir_slot *)dentptr)->alias_checksum; 907 908 get_vfatname(mydata, 909 root_cluster, 910 do_fat_read_block, 911 dentptr, l_name); 912 913 if (dols == LS_ROOT) { 914 char dirc; 915 int doit = 0; 916 int isdir = 917 (dentptr->attr & ATTR_DIR); 918 919 if (isdir) { 920 dirs++; 921 dirc = '/'; 922 doit = 1; 923 } else { 924 dirc = ' '; 925 if (l_name[0] != 0) { 926 files++; 927 doit = 1; 928 } 929 } 930 if (doit) { 931 if (dirc == ' ') { 932 printf(" %8ld %s%c\n", 933 (long)FAT2CPU32(dentptr->size), 934 l_name, 935 dirc); 936 } else { 937 printf(" %s%c\n", 938 l_name, 939 dirc); 940 } 941 } 942 dentptr++; 943 continue; 944 } 945 debug("Rootvfatname: |%s|\n", 946 l_name); 947 } else 948 #endif 949 { 950 /* Volume label or VFAT entry */ 951 dentptr++; 952 continue; 953 } 954 } else if (dentptr->name[0] == 0) { 955 debug("RootDentname == NULL - %d\n", i); 956 if (dols == LS_ROOT) { 957 printf("\n%d file(s), %d dir(s)\n\n", 958 files, dirs); 959 ret = 0; 960 } 961 goto exit; 962 } 963 #ifdef CONFIG_SUPPORT_VFAT 964 else if (dols == LS_ROOT && 965 mkcksum(dentptr->name) == prevcksum) { 966 dentptr++; 967 continue; 968 } 969 #endif 970 get_name(dentptr, s_name); 971 972 if (dols == LS_ROOT) { 973 int isdir = (dentptr->attr & ATTR_DIR); 974 char dirc; 975 int doit = 0; 976 977 if (isdir) { 978 dirc = '/'; 979 if (s_name[0] != 0) { 980 dirs++; 981 doit = 1; 982 } 983 } else { 984 dirc = ' '; 985 if (s_name[0] != 0) { 986 files++; 987 doit = 1; 988 } 989 } 990 if (doit) { 991 if (dirc == ' ') { 992 printf(" %8ld %s%c\n", 993 (long)FAT2CPU32(dentptr->size), 994 s_name, dirc); 995 } else { 996 printf(" %s%c\n", 997 s_name, dirc); 998 } 999 } 1000 dentptr++; 1001 continue; 1002 } 1003 1004 if (strcmp(fnamecopy, s_name) 1005 && strcmp(fnamecopy, l_name)) { 1006 debug("RootMismatch: |%s|%s|\n", s_name, 1007 l_name); 1008 dentptr++; 1009 continue; 1010 } 1011 1012 if (isdir && !(dentptr->attr & ATTR_DIR)) 1013 goto exit; 1014 1015 debug("RootName: %s", s_name); 1016 debug(", start: 0x%x", START(dentptr)); 1017 debug(", size: 0x%x %s\n", 1018 FAT2CPU32(dentptr->size), 1019 isdir ? "(DIR)" : ""); 1020 1021 goto rootdir_done; /* We got a match */ 1022 } 1023 debug("END LOOP: j=%d clust_size=%d\n", j, 1024 mydata->clust_size); 1025 1026 /* 1027 * On FAT32 we must fetch the FAT entries for the next 1028 * root directory clusters when a cluster has been 1029 * completely processed. 1030 */ 1031 ++j; 1032 int fat32_end = 0; 1033 if ((mydata->fatsize == 32) && (j == mydata->clust_size)) { 1034 int nxtsect = 0; 1035 int nxt_clust = 0; 1036 1037 nxt_clust = get_fatent(mydata, root_cluster); 1038 fat32_end = CHECK_CLUST(nxt_clust, 32); 1039 1040 nxtsect = mydata->data_begin + 1041 (nxt_clust * mydata->clust_size); 1042 1043 root_cluster = nxt_clust; 1044 1045 cursect = nxtsect; 1046 j = 0; 1047 } else { 1048 cursect++; 1049 } 1050 1051 /* If end of rootdir reached */ 1052 if ((mydata->fatsize == 32 && fat32_end) || 1053 (mydata->fatsize != 32 && j == rootdir_size)) { 1054 if (dols == LS_ROOT) { 1055 printf("\n%d file(s), %d dir(s)\n\n", 1056 files, dirs); 1057 ret = 0; 1058 } 1059 goto exit; 1060 } 1061 } 1062 rootdir_done: 1063 1064 firsttime = 1; 1065 1066 while (isdir) { 1067 int startsect = mydata->data_begin 1068 + START(dentptr) * mydata->clust_size; 1069 dir_entry dent; 1070 char *nextname = NULL; 1071 1072 dent = *dentptr; 1073 dentptr = &dent; 1074 1075 idx = dirdelim(subname); 1076 1077 if (idx >= 0) { 1078 subname[idx] = '\0'; 1079 nextname = subname + idx + 1; 1080 /* Handle multiple delimiters */ 1081 while (ISDIRDELIM(*nextname)) 1082 nextname++; 1083 if (dols && *nextname == '\0') 1084 firsttime = 0; 1085 } else { 1086 if (dols && firsttime) { 1087 firsttime = 0; 1088 } else { 1089 isdir = 0; 1090 } 1091 } 1092 1093 if (get_dentfromdir(mydata, startsect, subname, dentptr, 1094 isdir ? 0 : dols) == NULL) { 1095 if (dols && !isdir) 1096 ret = 0; 1097 goto exit; 1098 } 1099 1100 if (idx >= 0) { 1101 if (!(dentptr->attr & ATTR_DIR)) 1102 goto exit; 1103 subname = nextname; 1104 } 1105 } 1106 1107 ret = get_contents(mydata, dentptr, buffer, maxsize); 1108 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret); 1109 1110 exit: 1111 free(mydata->fatbuf); 1112 return ret; 1113 } 1114 1115 int file_fat_detectfs (void) 1116 { 1117 boot_sector bs; 1118 volume_info volinfo; 1119 int fatsize; 1120 char vol_label[12]; 1121 1122 if (cur_dev == NULL) { 1123 printf("No current device\n"); 1124 return 1; 1125 } 1126 1127 #if defined(CONFIG_CMD_IDE) || \ 1128 defined(CONFIG_CMD_MG_DISK) || \ 1129 defined(CONFIG_CMD_SATA) || \ 1130 defined(CONFIG_CMD_SCSI) || \ 1131 defined(CONFIG_CMD_USB) || \ 1132 defined(CONFIG_MMC) 1133 printf("Interface: "); 1134 switch (cur_dev->if_type) { 1135 case IF_TYPE_IDE: 1136 printf("IDE"); 1137 break; 1138 case IF_TYPE_SATA: 1139 printf("SATA"); 1140 break; 1141 case IF_TYPE_SCSI: 1142 printf("SCSI"); 1143 break; 1144 case IF_TYPE_ATAPI: 1145 printf("ATAPI"); 1146 break; 1147 case IF_TYPE_USB: 1148 printf("USB"); 1149 break; 1150 case IF_TYPE_DOC: 1151 printf("DOC"); 1152 break; 1153 case IF_TYPE_MMC: 1154 printf("MMC"); 1155 break; 1156 default: 1157 printf("Unknown"); 1158 } 1159 1160 printf("\n Device %d: ", cur_dev->dev); 1161 dev_print(cur_dev); 1162 #endif 1163 1164 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) { 1165 printf("\nNo valid FAT fs found\n"); 1166 return 1; 1167 } 1168 1169 memcpy(vol_label, volinfo.volume_label, 11); 1170 vol_label[11] = '\0'; 1171 volinfo.fs_type[5] = '\0'; 1172 1173 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part, 1174 volinfo.fs_type, vol_label); 1175 1176 return 0; 1177 } 1178 1179 int file_fat_ls (const char *dir) 1180 { 1181 return do_fat_read(dir, NULL, 0, LS_YES); 1182 } 1183 1184 long file_fat_read (const char *filename, void *buffer, unsigned long maxsize) 1185 { 1186 printf("reading %s\n", filename); 1187 return do_fat_read(filename, buffer, maxsize, LS_NO); 1188 } 1189