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)
37 * Convert a string to lowercase.
42 while (*str != '\0') {
48 static block_dev_desc_t *cur_dev = NULL;
49 static unsigned long part_offset = 0;
50 static int cur_part = 1;
52 #define DOS_PART_TBL_OFFSET 0x1be
53 #define DOS_PART_MAGIC_OFFSET 0x1fe
54 #define DOS_FS_TYPE_OFFSET 0x36
56 int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
58 startblock += part_offset;
61 if (cur_dev->block_read) {
62 return cur_dev->block_read (cur_dev->dev, startblock, getsize, (unsigned long *)bufptr);
69 fat_register_device(block_dev_desc_t *dev_desc, int part_no)
71 unsigned char buffer[SECTOR_SIZE];
73 if (!dev_desc->block_read)
76 /* check if we have a MBR (on floppies we have only a PBR) */
77 if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
78 printf ("** Can't read from device %d **\n", dev_desc->dev);
81 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
82 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
83 /* no signature found */
86 if(!strncmp(&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
87 /* ok, we assume we are on a PBR only */
92 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)
93 disk_partition_t info;
94 if(!get_partition_info(dev_desc, part_no, &info)) {
95 part_offset = info.start;
99 printf ("** Partition %d not valid on device %d **\n",part_no,dev_desc->dev);
103 /* FIXME we need to determine the start block of the
104 * partition where the DOS FS resides. This can be done
105 * by using the get_partition_info routine. For this
106 * purpose the libpart must be included.
117 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
118 * Return index into string if found, -1 otherwise.
125 while (*str != '\0') {
126 if (ISDIRDELIM(*str)) return str - start;
134 * Match volume_info fs_type strings.
135 * Return 0 on match, -1 otherwise.
138 compare_sign(char *str1, char *str2)
140 char *end = str1+SIGNLEN;
142 while (str1 != end) {
143 if (*str1 != *str2) {
155 * Extract zero terminated short name from a directory entry.
157 static void get_name (dir_entry *dirent, char *s_name)
161 memcpy (s_name, dirent->name, 8);
164 while (*ptr && *ptr != ' ')
166 if (dirent->ext[0] && dirent->ext[0] != ' ') {
169 memcpy (ptr, dirent->ext, 3);
171 while (*ptr && *ptr != ' ')
175 if (*s_name == DELETED_FLAG)
177 else if (*s_name == aRING)
183 * Get the entry at index 'entry' in a FAT (12/16/32) table.
184 * On failure 0x00 is returned.
187 get_fatent(fsdata *mydata, __u32 entry)
193 switch (mydata->fatsize) {
195 bufnum = entry / FAT32BUFSIZE;
196 offset = entry - bufnum * FAT32BUFSIZE;
199 bufnum = entry / FAT16BUFSIZE;
200 offset = entry - bufnum * FAT16BUFSIZE;
203 bufnum = entry / FAT12BUFSIZE;
204 offset = entry - bufnum * FAT12BUFSIZE;
208 /* Unsupported FAT size */
212 /* Read a new block of FAT entries into the cache. */
213 if (bufnum != mydata->fatbufnum) {
214 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
215 __u8 *bufptr = mydata->fatbuf;
216 __u32 fatlength = mydata->fatlength;
217 __u32 startblock = bufnum * FATBUFBLOCKS;
219 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
220 startblock += mydata->fat_sect; /* Offset from start of disk */
222 if (getsize > fatlength) getsize = fatlength;
223 if (disk_read(startblock, getsize, bufptr) < 0) {
224 FAT_DPRINT("Error reading FAT blocks\n");
227 mydata->fatbufnum = bufnum;
230 /* Get the actual entry from the table */
231 switch (mydata->fatsize) {
233 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
236 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
239 __u32 off16 = (offset*3)/4;
242 switch (offset & 0x3) {
244 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
248 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
250 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
252 ret = (val2 << 4) | (val1 >> 12);
255 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
257 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
259 ret = (val2 << 8) | (val1 >> 8);
262 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
263 ret = (ret & 0xfff0) >> 4;
271 FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
278 * Read at most 'size' bytes from the specified cluster into 'buffer'.
279 * Return 0 on success, -1 otherwise.
282 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
288 startsect = mydata->data_begin + clustnum*mydata->clust_size;
290 startsect = mydata->rootdir_sect;
293 FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
294 if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
295 FAT_DPRINT("Error reading data\n");
298 if(size % FS_BLOCK_SIZE) {
299 __u8 tmpbuf[FS_BLOCK_SIZE];
300 idx= size/FS_BLOCK_SIZE;
301 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
302 FAT_DPRINT("Error reading data\n");
305 buffer += idx*FS_BLOCK_SIZE;
307 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
316 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
318 * Return the number of bytes read or -1 on fatal errors.
321 get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
322 unsigned long maxsize)
324 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
325 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
326 __u32 curclust = START(dentptr);
327 __u32 endclust, newclust;
328 unsigned long actsize;
330 FAT_DPRINT("Filesize: %ld bytes\n", filesize);
332 if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
334 FAT_DPRINT("Reading: %ld bytes\n", filesize);
336 actsize=bytesperclust;
339 /* search for consecutive clusters */
340 while(actsize < filesize) {
341 newclust = get_fatent(mydata, endclust);
342 if((newclust -1)!=endclust)
344 if (newclust <= 0x0001 || newclust >= 0xfff0) {
345 FAT_DPRINT("curclust: 0x%x\n", newclust);
346 FAT_DPRINT("Invalid FAT entry\n");
350 actsize+= bytesperclust;
352 /* actsize >= file size */
353 actsize -= bytesperclust;
354 /* get remaining clusters */
355 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
356 FAT_ERROR("Error reading cluster\n");
359 /* get remaining bytes */
360 gotsize += (int)actsize;
364 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
365 FAT_ERROR("Error reading cluster\n");
371 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
372 FAT_ERROR("Error reading cluster\n");
375 gotsize += (int)actsize;
378 curclust = get_fatent(mydata, endclust);
379 if (curclust <= 0x0001 || curclust >= 0xfff0) {
380 FAT_DPRINT("curclust: 0x%x\n", curclust);
381 FAT_ERROR("Invalid FAT entry\n");
384 actsize=bytesperclust;
390 #ifdef CONFIG_SUPPORT_VFAT
392 * Extract the file name information from 'slotptr' into 'l_name',
393 * starting at l_name[*idx].
394 * Return 1 if terminator (zero byte) is found, 0 otherwise.
397 slot2str(dir_slot *slotptr, char *l_name, int *idx)
401 for (j = 0; j <= 8; j += 2) {
402 l_name[*idx] = slotptr->name0_4[j];
403 if (l_name[*idx] == 0x00) return 1;
406 for (j = 0; j <= 10; j += 2) {
407 l_name[*idx] = slotptr->name5_10[j];
408 if (l_name[*idx] == 0x00) return 1;
411 for (j = 0; j <= 2; j += 2) {
412 l_name[*idx] = slotptr->name11_12[j];
413 if (l_name[*idx] == 0x00) return 1;
422 * Extract the full long filename starting at 'retdent' (which is really
423 * a slot) into 'l_name'. If successful also copy the real directory entry
425 * Return 0 on success, -1 otherwise.
428 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
429 dir_entry *retdent, char *l_name)
432 dir_slot *slotptr = (dir_slot*) retdent;
433 __u8 *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
434 __u8 counter = slotptr->id & 0xf;
437 while ((__u8*)slotptr < nextclust) {
438 if (counter == 0) break;
439 if ((slotptr->id & 0x0f) != counter) return -1;
444 if ((__u8*)slotptr >= nextclust) {
445 __u8 block[MAX_CLUSTSIZE];
449 curclust = get_fatent(mydata, curclust);
450 if (curclust <= 0x0001 || curclust >= 0xfff0) {
451 FAT_DPRINT("curclust: 0x%x\n", curclust);
452 FAT_ERROR("Invalid FAT entry\n");
455 if (get_cluster(mydata, curclust, block,
456 mydata->clust_size * SECTOR_SIZE) != 0) {
457 FAT_DPRINT("Error: reading directory block\n");
460 slotptr2 = (dir_slot*) block;
461 while (slotptr2->id > 0x01) {
464 /* Save the real directory entry */
465 realdent = (dir_entry*)slotptr2 + 1;
466 while ((__u8*)slotptr2 >= block) {
467 slot2str(slotptr2, l_name, &idx);
471 /* Save the real directory entry */
472 realdent = (dir_entry*)slotptr;
477 if (slot2str(slotptr, l_name, &idx)) break;
478 } while (!(slotptr->id & 0x40));
481 if (*l_name == DELETED_FLAG) *l_name = '\0';
482 else if (*l_name == aRING) *l_name = 'å';
485 /* Return the real directory entry */
486 memcpy(retdent, realdent, sizeof(dir_entry));
492 /* Calculate short name checksum */
494 mkcksum(const char *str)
499 for (i = 0; i < 11; i++) {
500 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
509 * Get the directory entry associated with 'filename' from the directory
510 * starting at 'startsect'
512 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
513 char *filename, dir_entry * retdent,
516 __u16 prevcksum = 0xffff;
517 __u8 block[MAX_CLUSTSIZE];
518 __u32 curclust = START (retdent);
519 int files = 0, dirs = 0;
521 FAT_DPRINT ("get_dentfromdir: %s\n", filename);
526 if (get_cluster (mydata, curclust, block,
527 mydata->clust_size * SECTOR_SIZE) != 0) {
528 FAT_DPRINT ("Error: reading directory block\n");
531 dentptr = (dir_entry *) block;
532 for (i = 0; i < DIRENTSPERCLUST; i++) {
533 char s_name[14], l_name[256];
536 if ((dentptr->attr & ATTR_VOLUME)) {
537 #ifdef CONFIG_SUPPORT_VFAT
538 if ((dentptr->attr & ATTR_VFAT) &&
539 (dentptr->name[0] & 0x40)) {
540 prevcksum = ((dir_slot *) dentptr)
542 get_vfatname (mydata, curclust, block,
545 int isdir = (dentptr->attr & ATTR_DIR);
555 if (l_name[0] != 0) {
562 printf (" %8ld %s%c\n",
563 (long) FAT2CPU32 (dentptr->size),
566 printf (" %s%c\n", l_name, dirc);
572 FAT_DPRINT ("vfatname: |%s|\n", l_name);
576 /* Volume label or VFAT entry */
581 if (dentptr->name[0] == 0) {
583 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
585 FAT_DPRINT ("Dentname == NULL - %d\n", i);
588 #ifdef CONFIG_SUPPORT_VFAT
589 if (dols && mkcksum (dentptr->name) == prevcksum) {
594 get_name (dentptr, s_name);
596 int isdir = (dentptr->attr & ATTR_DIR);
606 if (s_name[0] != 0) {
613 printf (" %8ld %s%c\n",
614 (long) FAT2CPU32 (dentptr->size), s_name,
617 printf (" %s%c\n", s_name, dirc);
623 if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
624 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
628 memcpy (retdent, dentptr, sizeof (dir_entry));
630 FAT_DPRINT ("DentName: %s", s_name);
631 FAT_DPRINT (", start: 0x%x", START (dentptr));
632 FAT_DPRINT (", size: 0x%x %s\n",
633 FAT2CPU32 (dentptr->size),
634 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
638 curclust = get_fatent (mydata, curclust);
639 if (curclust <= 0x0001 || curclust >= 0xfff0) {
640 FAT_DPRINT ("curclust: 0x%x\n", curclust);
641 FAT_ERROR ("Invalid FAT entry\n");
651 * Read boot sector and volume info from a FAT filesystem
654 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
656 __u8 block[FS_BLOCK_SIZE];
657 volume_info *vistart;
659 if (disk_read(0, 1, block) < 0) {
660 FAT_DPRINT("Error: reading block\n");
664 memcpy(bs, block, sizeof(boot_sector));
665 bs->reserved = FAT2CPU16(bs->reserved);
666 bs->fat_length = FAT2CPU16(bs->fat_length);
667 bs->secs_track = FAT2CPU16(bs->secs_track);
668 bs->heads = FAT2CPU16(bs->heads);
670 bs->hidden = FAT2CPU32(bs->hidden);
672 bs->total_sect = FAT2CPU32(bs->total_sect);
675 if (bs->fat_length == 0) {
677 bs->fat32_length = FAT2CPU32(bs->fat32_length);
678 bs->flags = FAT2CPU16(bs->flags);
679 bs->root_cluster = FAT2CPU32(bs->root_cluster);
680 bs->info_sector = FAT2CPU16(bs->info_sector);
681 bs->backup_boot = FAT2CPU16(bs->backup_boot);
682 vistart = (volume_info*) (block + sizeof(boot_sector));
685 vistart = (volume_info*) &(bs->fat32_length);
688 memcpy(volinfo, vistart, sizeof(volume_info));
690 /* Terminate fs_type string. Writing past the end of vistart
691 is ok - it's just the buffer. */
692 vistart->fs_type[8] = '\0';
694 if (*fatsize == 32) {
695 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
699 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
703 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
709 FAT_DPRINT("Error: broken fs_type sign\n");
715 do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
718 __u8 block[MAX_CLUSTSIZE]; /* Block buffer */
719 char fnamecopy[2048];
723 fsdata *mydata = &datablock;
725 __u16 prevcksum = 0xffff;
727 int rootdir_size, cursect;
729 int files = 0, dirs = 0;
733 if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
734 FAT_DPRINT ("Error: reading boot sector\n");
737 if (mydata->fatsize == 32) {
738 mydata->fatlength = bs.fat32_length;
740 mydata->fatlength = bs.fat_length;
742 mydata->fat_sect = bs.reserved;
743 cursect = mydata->rootdir_sect
744 = mydata->fat_sect + mydata->fatlength * bs.fats;
745 mydata->clust_size = bs.cluster_size;
746 if (mydata->fatsize == 32) {
747 rootdir_size = mydata->clust_size;
748 mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
749 - (mydata->clust_size * 2);
751 rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
752 * sizeof (dir_entry)) / SECTOR_SIZE;
753 mydata->data_begin = mydata->rootdir_sect + rootdir_size
754 - (mydata->clust_size * 2);
756 mydata->fatbufnum = -1;
758 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
760 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
761 "Data begins at: %d\n",
762 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
763 rootdir_size, mydata->data_begin);
764 FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
766 /* "cwd" is always the root... */
767 while (ISDIRDELIM (*filename))
769 /* Make a copy of the filename and convert it to lowercase */
770 strcpy (fnamecopy, filename);
771 downcase (fnamecopy);
772 if (*fnamecopy == '\0') {
776 } else if ((idx = dirdelim (fnamecopy)) >= 0) {
778 fnamecopy[idx] = '\0';
779 subname = fnamecopy + idx + 1;
780 /* Handle multiple delimiters */
781 while (ISDIRDELIM (*subname))
790 if (disk_read (cursect, mydata->clust_size, block) < 0) {
791 FAT_DPRINT ("Error: reading rootdir block\n");
794 dentptr = (dir_entry *) block;
795 for (i = 0; i < DIRENTSPERBLOCK; i++) {
796 char s_name[14], l_name[256];
799 if ((dentptr->attr & ATTR_VOLUME)) {
800 #ifdef CONFIG_SUPPORT_VFAT
801 if ((dentptr->attr & ATTR_VFAT) &&
802 (dentptr->name[0] & 0x40)) {
803 prevcksum = ((dir_slot *) dentptr)->alias_checksum;
804 get_vfatname (mydata, 0, block, dentptr, l_name);
805 if (dols == LS_ROOT) {
806 int isdir = (dentptr->attr & ATTR_DIR);
816 if (l_name[0] != 0) {
823 printf (" %8ld %s%c\n",
824 (long) FAT2CPU32 (dentptr->size),
827 printf (" %s%c\n", l_name, dirc);
833 FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
837 /* Volume label or VFAT entry */
841 } else if (dentptr->name[0] == 0) {
842 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
843 if (dols == LS_ROOT) {
844 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
849 #ifdef CONFIG_SUPPORT_VFAT
850 else if (dols == LS_ROOT
851 && mkcksum (dentptr->name) == prevcksum) {
856 get_name (dentptr, s_name);
857 if (dols == LS_ROOT) {
858 int isdir = (dentptr->attr & ATTR_DIR);
864 if (s_name[0] != 0) {
870 if (s_name[0] != 0) {
877 printf (" %8ld %s%c\n",
878 (long) FAT2CPU32 (dentptr->size), s_name,
881 printf (" %s%c\n", s_name, dirc);
887 if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
888 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
892 if (isdir && !(dentptr->attr & ATTR_DIR))
895 FAT_DPRINT ("RootName: %s", s_name);
896 FAT_DPRINT (", start: 0x%x", START (dentptr));
897 FAT_DPRINT (", size: 0x%x %s\n",
898 FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
900 goto rootdir_done; /* We got a match */
908 int startsect = mydata->data_begin
909 + START (dentptr) * mydata->clust_size;
911 char *nextname = NULL;
916 idx = dirdelim (subname);
919 nextname = subname + idx + 1;
920 /* Handle multiple delimiters */
921 while (ISDIRDELIM (*nextname))
923 if (dols && *nextname == '\0')
926 if (dols && firsttime) {
933 if (get_dentfromdir (mydata, startsect, subname, dentptr,
934 isdir ? 0 : dols) == NULL) {
941 if (!(dentptr->attr & ATTR_DIR))
946 ret = get_contents (mydata, dentptr, buffer, maxsize);
947 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
954 file_fat_detectfs(void)
962 printf("No current device\n");
965 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)
966 printf("Interface: ");
967 switch(cur_dev->if_type) {
968 case IF_TYPE_IDE : printf("IDE"); break;
969 case IF_TYPE_SCSI : printf("SCSI"); break;
970 case IF_TYPE_ATAPI : printf("ATAPI"); break;
971 case IF_TYPE_USB : printf("USB"); break;
972 case IF_TYPE_DOC : printf("DOC"); break;
973 case IF_TYPE_MMC : printf("MMC"); break;
974 default : printf("Unknown");
976 printf("\n Device %d: ",cur_dev->dev);
979 if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
980 printf("\nNo valid FAT fs found\n");
983 memcpy (vol_label, volinfo.volume_label, 11);
984 vol_label[11] = '\0';
985 volinfo.fs_type[5]='\0';
986 printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
992 file_fat_ls(const char *dir)
994 return do_fat_read(dir, NULL, 0, LS_YES);
999 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1001 printf("reading %s\n",filename);
1002 return do_fat_read(filename, buffer, maxsize, LS_NO);
1005 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */