4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
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
9 * See file CREDITS for list of people who contributed to this
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.
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.
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,
31 #include <asm/byteorder.h>
34 #if (CONFIG_COMMANDS & CFG_CMD_FAT)
36 #ifdef CONFIG_AUTO_UPDATE
37 /* the VFAT code has a bug which breaks auto update */
38 #undef CONFIG_SUPPORT_VFAT
42 * Convert a string to lowercase.
47 while (*str != '\0') {
53 static block_dev_desc_t *cur_dev = NULL;
54 static unsigned long part_offset = 0;
55 static int cur_part = 1;
57 #define DOS_PART_TBL_OFFSET 0x1be
58 #define DOS_PART_MAGIC_OFFSET 0x1fe
59 #define DOS_FS_TYPE_OFFSET 0x36
61 int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
63 startblock += part_offset;
66 if (cur_dev->block_read) {
67 return cur_dev->block_read (cur_dev->dev, startblock, getsize, (unsigned long *)bufptr);
74 fat_register_device(block_dev_desc_t *dev_desc, int part_no)
76 unsigned char buffer[SECTOR_SIZE];
78 if (!dev_desc->block_read)
81 /* check if we have a MBR (on floppies we have only a PBR) */
82 if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
83 printf ("** Can't read from device %d **\n", dev_desc->dev);
86 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
87 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
88 /* no signature found */
91 if(!strncmp(&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
92 /* ok, we assume we are on a PBR only */
97 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)
98 disk_partition_t info;
99 if(!get_partition_info(dev_desc, part_no, &info)) {
100 part_offset = info.start;
104 printf ("** Partition %d not valid on device %d **\n",part_no,dev_desc->dev);
108 /* FIXME we need to determine the start block of the
109 * partition where the DOS FS resides. This can be done
110 * by using the get_partition_info routine. For this
111 * purpose the libpart must be included.
122 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
123 * Return index into string if found, -1 otherwise.
130 while (*str != '\0') {
131 if (ISDIRDELIM(*str)) return str - start;
139 * Match volume_info fs_type strings.
140 * Return 0 on match, -1 otherwise.
143 compare_sign(char *str1, char *str2)
145 char *end = str1+SIGNLEN;
147 while (str1 != end) {
148 if (*str1 != *str2) {
160 * Extract zero terminated short name from a directory entry.
162 static void get_name (dir_entry *dirent, char *s_name)
166 memcpy (s_name, dirent->name, 8);
169 while (*ptr && *ptr != ' ')
171 if (dirent->ext[0] && dirent->ext[0] != ' ') {
174 memcpy (ptr, dirent->ext, 3);
176 while (*ptr && *ptr != ' ')
180 if (*s_name == DELETED_FLAG)
182 else if (*s_name == aRING)
188 * Get the entry at index 'entry' in a FAT (12/16/32) table.
189 * On failure 0x00 is returned.
192 get_fatent(fsdata *mydata, __u32 entry)
198 switch (mydata->fatsize) {
200 bufnum = entry / FAT32BUFSIZE;
201 offset = entry - bufnum * FAT32BUFSIZE;
204 bufnum = entry / FAT16BUFSIZE;
205 offset = entry - bufnum * FAT16BUFSIZE;
208 bufnum = entry / FAT12BUFSIZE;
209 offset = entry - bufnum * FAT12BUFSIZE;
213 /* Unsupported FAT size */
217 /* Read a new block of FAT entries into the cache. */
218 if (bufnum != mydata->fatbufnum) {
219 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
220 __u8 *bufptr = mydata->fatbuf;
221 __u32 fatlength = mydata->fatlength;
222 __u32 startblock = bufnum * FATBUFBLOCKS;
224 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
225 startblock += mydata->fat_sect; /* Offset from start of disk */
227 if (getsize > fatlength) getsize = fatlength;
228 if (disk_read(startblock, getsize, bufptr) < 0) {
229 FAT_DPRINT("Error reading FAT blocks\n");
232 mydata->fatbufnum = bufnum;
235 /* Get the actual entry from the table */
236 switch (mydata->fatsize) {
238 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
241 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
244 __u32 off16 = (offset*3)/4;
247 switch (offset & 0x3) {
249 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
253 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
255 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
257 ret = (val2 << 4) | (val1 >> 12);
260 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
262 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
264 ret = (val2 << 8) | (val1 >> 8);
267 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
268 ret = (ret & 0xfff0) >> 4;
276 FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
283 * Read at most 'size' bytes from the specified cluster into 'buffer'.
284 * Return 0 on success, -1 otherwise.
287 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
293 startsect = mydata->data_begin + clustnum*mydata->clust_size;
295 startsect = mydata->rootdir_sect;
298 FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
299 if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
300 FAT_DPRINT("Error reading data\n");
303 if(size % FS_BLOCK_SIZE) {
304 __u8 tmpbuf[FS_BLOCK_SIZE];
305 idx= size/FS_BLOCK_SIZE;
306 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
307 FAT_DPRINT("Error reading data\n");
310 buffer += idx*FS_BLOCK_SIZE;
312 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
321 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
323 * Return the number of bytes read or -1 on fatal errors.
326 get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
327 unsigned long maxsize)
329 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
330 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
331 __u32 curclust = START(dentptr);
332 __u32 endclust, newclust;
333 unsigned long actsize;
335 FAT_DPRINT("Filesize: %ld bytes\n", filesize);
337 if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
339 FAT_DPRINT("Reading: %ld bytes\n", filesize);
341 actsize=bytesperclust;
344 /* search for consecutive clusters */
345 while(actsize < filesize) {
346 newclust = get_fatent(mydata, endclust);
347 if((newclust -1)!=endclust)
349 if (newclust <= 0x0001 || newclust >= 0xfff0) {
350 FAT_DPRINT("curclust: 0x%x\n", newclust);
351 FAT_DPRINT("Invalid FAT entry\n");
355 actsize+= bytesperclust;
357 /* actsize >= file size */
358 actsize -= bytesperclust;
359 /* get remaining clusters */
360 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
361 FAT_ERROR("Error reading cluster\n");
364 /* get remaining bytes */
365 gotsize += (int)actsize;
369 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
370 FAT_ERROR("Error reading cluster\n");
376 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
377 FAT_ERROR("Error reading cluster\n");
380 gotsize += (int)actsize;
383 curclust = get_fatent(mydata, endclust);
384 if (curclust <= 0x0001 || curclust >= 0xfff0) {
385 FAT_DPRINT("curclust: 0x%x\n", curclust);
386 FAT_ERROR("Invalid FAT entry\n");
389 actsize=bytesperclust;
395 #ifdef CONFIG_SUPPORT_VFAT
397 * Extract the file name information from 'slotptr' into 'l_name',
398 * starting at l_name[*idx].
399 * Return 1 if terminator (zero byte) is found, 0 otherwise.
402 slot2str(dir_slot *slotptr, char *l_name, int *idx)
406 for (j = 0; j <= 8; j += 2) {
407 l_name[*idx] = slotptr->name0_4[j];
408 if (l_name[*idx] == 0x00) return 1;
411 for (j = 0; j <= 10; j += 2) {
412 l_name[*idx] = slotptr->name5_10[j];
413 if (l_name[*idx] == 0x00) return 1;
416 for (j = 0; j <= 2; j += 2) {
417 l_name[*idx] = slotptr->name11_12[j];
418 if (l_name[*idx] == 0x00) return 1;
427 * Extract the full long filename starting at 'retdent' (which is really
428 * a slot) into 'l_name'. If successful also copy the real directory entry
430 * Return 0 on success, -1 otherwise.
433 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
434 dir_entry *retdent, char *l_name)
437 dir_slot *slotptr = (dir_slot*) retdent;
438 __u8 *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
439 __u8 counter = slotptr->id & 0xf;
442 while ((__u8*)slotptr < nextclust) {
443 if (counter == 0) break;
444 if ((slotptr->id & 0x0f) != counter) return -1;
449 if ((__u8*)slotptr >= nextclust) {
450 __u8 block[MAX_CLUSTSIZE];
454 curclust = get_fatent(mydata, curclust);
455 if (curclust <= 0x0001 || curclust >= 0xfff0) {
456 FAT_DPRINT("curclust: 0x%x\n", curclust);
457 FAT_ERROR("Invalid FAT entry\n");
460 if (get_cluster(mydata, curclust, block,
461 mydata->clust_size * SECTOR_SIZE) != 0) {
462 FAT_DPRINT("Error: reading directory block\n");
465 slotptr2 = (dir_slot*) block;
466 while (slotptr2->id > 0x01) {
469 /* Save the real directory entry */
470 realdent = (dir_entry*)slotptr2 + 1;
471 while ((__u8*)slotptr2 >= block) {
472 slot2str(slotptr2, l_name, &idx);
476 /* Save the real directory entry */
477 realdent = (dir_entry*)slotptr;
482 if (slot2str(slotptr, l_name, &idx)) break;
483 } while (!(slotptr->id & 0x40));
486 if (*l_name == DELETED_FLAG) *l_name = '\0';
487 else if (*l_name == aRING) *l_name = 'å';
490 /* Return the real directory entry */
491 memcpy(retdent, realdent, sizeof(dir_entry));
497 /* Calculate short name checksum */
499 mkcksum(const char *str)
504 for (i = 0; i < 11; i++) {
505 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
514 * Get the directory entry associated with 'filename' from the directory
515 * starting at 'startsect'
517 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
518 char *filename, dir_entry * retdent,
521 __u16 prevcksum = 0xffff;
522 __u8 block[MAX_CLUSTSIZE];
523 __u32 curclust = START (retdent);
524 int files = 0, dirs = 0;
526 FAT_DPRINT ("get_dentfromdir: %s\n", filename);
531 if (get_cluster (mydata, curclust, block,
532 mydata->clust_size * SECTOR_SIZE) != 0) {
533 FAT_DPRINT ("Error: reading directory block\n");
536 dentptr = (dir_entry *) block;
537 for (i = 0; i < DIRENTSPERCLUST; i++) {
538 char s_name[14], l_name[256];
541 if ((dentptr->attr & ATTR_VOLUME)) {
542 #ifdef CONFIG_SUPPORT_VFAT
543 if ((dentptr->attr & ATTR_VFAT) &&
544 (dentptr->name[0] & 0x40)) {
545 prevcksum = ((dir_slot *) dentptr)
547 get_vfatname (mydata, curclust, block,
550 int isdir = (dentptr->attr & ATTR_DIR);
560 if (l_name[0] != 0) {
567 printf (" %8ld %s%c\n",
568 (long) FAT2CPU32 (dentptr->size),
571 printf (" %s%c\n", l_name, dirc);
577 FAT_DPRINT ("vfatname: |%s|\n", l_name);
581 /* Volume label or VFAT entry */
586 if (dentptr->name[0] == 0) {
588 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
590 FAT_DPRINT ("Dentname == NULL - %d\n", i);
593 #ifdef CONFIG_SUPPORT_VFAT
594 if (dols && mkcksum (dentptr->name) == prevcksum) {
599 get_name (dentptr, s_name);
601 int isdir = (dentptr->attr & ATTR_DIR);
611 if (s_name[0] != 0) {
618 printf (" %8ld %s%c\n",
619 (long) FAT2CPU32 (dentptr->size), s_name,
622 printf (" %s%c\n", s_name, dirc);
628 if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
629 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
633 memcpy (retdent, dentptr, sizeof (dir_entry));
635 FAT_DPRINT ("DentName: %s", s_name);
636 FAT_DPRINT (", start: 0x%x", START (dentptr));
637 FAT_DPRINT (", size: 0x%x %s\n",
638 FAT2CPU32 (dentptr->size),
639 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
643 curclust = get_fatent (mydata, curclust);
644 if (curclust <= 0x0001 || curclust >= 0xfff0) {
645 FAT_DPRINT ("curclust: 0x%x\n", curclust);
646 FAT_ERROR ("Invalid FAT entry\n");
656 * Read boot sector and volume info from a FAT filesystem
659 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
661 __u8 block[FS_BLOCK_SIZE];
662 volume_info *vistart;
664 if (disk_read(0, 1, block) < 0) {
665 FAT_DPRINT("Error: reading block\n");
669 memcpy(bs, block, sizeof(boot_sector));
670 bs->reserved = FAT2CPU16(bs->reserved);
671 bs->fat_length = FAT2CPU16(bs->fat_length);
672 bs->secs_track = FAT2CPU16(bs->secs_track);
673 bs->heads = FAT2CPU16(bs->heads);
675 bs->hidden = FAT2CPU32(bs->hidden);
677 bs->total_sect = FAT2CPU32(bs->total_sect);
680 if (bs->fat_length == 0) {
682 bs->fat32_length = FAT2CPU32(bs->fat32_length);
683 bs->flags = FAT2CPU16(bs->flags);
684 bs->root_cluster = FAT2CPU32(bs->root_cluster);
685 bs->info_sector = FAT2CPU16(bs->info_sector);
686 bs->backup_boot = FAT2CPU16(bs->backup_boot);
687 vistart = (volume_info*) (block + sizeof(boot_sector));
690 vistart = (volume_info*) &(bs->fat32_length);
693 memcpy(volinfo, vistart, sizeof(volume_info));
695 /* Terminate fs_type string. Writing past the end of vistart
696 is ok - it's just the buffer. */
697 vistart->fs_type[8] = '\0';
699 if (*fatsize == 32) {
700 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
704 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
708 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
714 FAT_DPRINT("Error: broken fs_type sign\n");
720 do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
723 __u8 block[MAX_CLUSTSIZE]; /* Block buffer */
724 char fnamecopy[2048];
728 fsdata *mydata = &datablock;
730 __u16 prevcksum = 0xffff;
732 int rootdir_size, cursect;
734 int files = 0, dirs = 0;
738 if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
739 FAT_DPRINT ("Error: reading boot sector\n");
742 if (mydata->fatsize == 32) {
743 mydata->fatlength = bs.fat32_length;
745 mydata->fatlength = bs.fat_length;
747 mydata->fat_sect = bs.reserved;
748 cursect = mydata->rootdir_sect
749 = mydata->fat_sect + mydata->fatlength * bs.fats;
750 mydata->clust_size = bs.cluster_size;
751 if (mydata->fatsize == 32) {
752 rootdir_size = mydata->clust_size;
753 mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
754 - (mydata->clust_size * 2);
756 rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
757 * sizeof (dir_entry)) / SECTOR_SIZE;
758 mydata->data_begin = mydata->rootdir_sect + rootdir_size
759 - (mydata->clust_size * 2);
761 mydata->fatbufnum = -1;
763 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
765 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
766 "Data begins at: %d\n",
767 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
768 rootdir_size, mydata->data_begin);
769 FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
771 /* "cwd" is always the root... */
772 while (ISDIRDELIM (*filename))
774 /* Make a copy of the filename and convert it to lowercase */
775 strcpy (fnamecopy, filename);
776 downcase (fnamecopy);
777 if (*fnamecopy == '\0') {
781 } else if ((idx = dirdelim (fnamecopy)) >= 0) {
783 fnamecopy[idx] = '\0';
784 subname = fnamecopy + idx + 1;
785 /* Handle multiple delimiters */
786 while (ISDIRDELIM (*subname))
795 if (disk_read (cursect, mydata->clust_size, block) < 0) {
796 FAT_DPRINT ("Error: reading rootdir block\n");
799 dentptr = (dir_entry *) block;
800 for (i = 0; i < DIRENTSPERBLOCK; i++) {
801 char s_name[14], l_name[256];
804 if ((dentptr->attr & ATTR_VOLUME)) {
805 #ifdef CONFIG_SUPPORT_VFAT
806 if ((dentptr->attr & ATTR_VFAT) &&
807 (dentptr->name[0] & 0x40)) {
808 prevcksum = ((dir_slot *) dentptr)->alias_checksum;
809 get_vfatname (mydata, 0, block, dentptr, l_name);
810 if (dols == LS_ROOT) {
811 int isdir = (dentptr->attr & ATTR_DIR);
821 if (l_name[0] != 0) {
828 printf (" %8ld %s%c\n",
829 (long) FAT2CPU32 (dentptr->size),
832 printf (" %s%c\n", l_name, dirc);
838 FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
842 /* Volume label or VFAT entry */
846 } else if (dentptr->name[0] == 0) {
847 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
848 if (dols == LS_ROOT) {
849 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
854 #ifdef CONFIG_SUPPORT_VFAT
855 else if (dols == LS_ROOT
856 && mkcksum (dentptr->name) == prevcksum) {
861 get_name (dentptr, s_name);
862 if (dols == LS_ROOT) {
863 int isdir = (dentptr->attr & ATTR_DIR);
869 if (s_name[0] != 0) {
875 if (s_name[0] != 0) {
882 printf (" %8ld %s%c\n",
883 (long) FAT2CPU32 (dentptr->size), s_name,
886 printf (" %s%c\n", s_name, dirc);
892 if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
893 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
897 if (isdir && !(dentptr->attr & ATTR_DIR))
900 FAT_DPRINT ("RootName: %s", s_name);
901 FAT_DPRINT (", start: 0x%x", START (dentptr));
902 FAT_DPRINT (", size: 0x%x %s\n",
903 FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
905 goto rootdir_done; /* We got a match */
913 int startsect = mydata->data_begin
914 + START (dentptr) * mydata->clust_size;
916 char *nextname = NULL;
921 idx = dirdelim (subname);
924 nextname = subname + idx + 1;
925 /* Handle multiple delimiters */
926 while (ISDIRDELIM (*nextname))
928 if (dols && *nextname == '\0')
931 if (dols && firsttime) {
938 if (get_dentfromdir (mydata, startsect, subname, dentptr,
939 isdir ? 0 : dols) == NULL) {
946 if (!(dentptr->attr & ATTR_DIR))
951 ret = get_contents (mydata, dentptr, buffer, maxsize);
952 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
959 file_fat_detectfs(void)
967 printf("No current device\n");
970 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)
971 printf("Interface: ");
972 switch(cur_dev->if_type) {
973 case IF_TYPE_IDE : printf("IDE"); break;
974 case IF_TYPE_SCSI : printf("SCSI"); break;
975 case IF_TYPE_ATAPI : printf("ATAPI"); break;
976 case IF_TYPE_USB : printf("USB"); break;
977 case IF_TYPE_DOC : printf("DOC"); break;
978 case IF_TYPE_MMC : printf("MMC"); break;
979 default : printf("Unknown");
981 printf("\n Device %d: ",cur_dev->dev);
984 if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
985 printf("\nNo valid FAT fs found\n");
988 memcpy (vol_label, volinfo.volume_label, 11);
989 vol_label[11] = '\0';
990 volinfo.fs_type[5]='\0';
991 printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
997 file_fat_ls(const char *dir)
999 return do_fat_read(dir, NULL, 0, LS_YES);
1004 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1006 printf("reading %s\n",filename);
1007 return do_fat_read(filename, buffer, maxsize, LS_NO);
1010 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */