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,
32 #include <asm/byteorder.h>
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 0x52
56 int strncmp(const char * cs,const char * ct,size_t count)
58 register signed char __res = 0;
61 if ((__res = *cs - *ct++) != 0 || !*cs++)
69 char * strcpy(char * dest,const char *src)
73 while ((*dest++ = *src++) != '\0')
78 int strcmp(const char * cs,const char * ct)
80 register signed char __res;
83 if ((__res = *cs - *ct++) != 0 || !*cs++)
89 void * memcpy(void * dest,const void *src,size_t count)
91 char *tmp = (char *) dest, *s = (char *) src;
100 int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
102 startblock += part_offset;
105 if (cur_dev->block_read) {
106 return cur_dev->block_read (cur_dev->dev, startblock, getsize, (unsigned long *)bufptr);
113 fat_register_device(block_dev_desc_t *dev_desc, int part_no)
115 unsigned char buffer[SECTOR_SIZE];
117 if (!dev_desc->block_read)
120 /* check if we have a MBR (on floppies we have only a PBR) */
121 if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
122 printf ("** Can't read from device %d **\n", dev_desc->dev);
125 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
126 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
127 /* no signature found */
130 if(!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
131 /* ok, we assume we are on a PBR only */
136 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
137 (CONFIG_COMMANDS & CFG_CMD_USB) || (CONFIG_COMMANDS & CFG_CMD_MMC) || defined(CONFIG_SYSTEMACE)
138 disk_partition_t info;
139 if(!get_partition_info(dev_desc, part_no, &info)) {
140 part_offset = info.start;
144 printf ("** Partition %d not valid on device %d **\n",part_no,dev_desc->dev);
148 part_offset = buffer[DOS_PART_TBL_OFFSET+8] |
149 buffer[DOS_PART_TBL_OFFSET+9] <<8 |
150 buffer[DOS_PART_TBL_OFFSET+10]<<16 |
151 buffer[DOS_PART_TBL_OFFSET+11]<<24;
161 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
162 * Return index into string if found, -1 otherwise.
169 while (*str != '\0') {
170 if (ISDIRDELIM(*str)) return str - start;
178 * Match volume_info fs_type strings.
179 * Return 0 on match, -1 otherwise.
182 compare_sign(char *str1, char *str2)
184 char *end = str1+SIGNLEN;
186 while (str1 != end) {
187 if (*str1 != *str2) {
199 * Extract zero terminated short name from a directory entry.
201 static void get_name (dir_entry *dirent, char *s_name)
205 memcpy (s_name, dirent->name, 8);
208 while (*ptr && *ptr != ' ')
210 if (dirent->ext[0] && dirent->ext[0] != ' ') {
213 memcpy (ptr, dirent->ext, 3);
215 while (*ptr && *ptr != ' ')
219 if (*s_name == DELETED_FLAG)
221 else if (*s_name == aRING)
227 * Get the entry at index 'entry' in a FAT (12/16/32) table.
228 * On failure 0x00 is returned.
231 get_fatent(fsdata *mydata, __u32 entry)
237 switch (mydata->fatsize) {
239 bufnum = entry / FAT32BUFSIZE;
240 offset = entry - bufnum * FAT32BUFSIZE;
243 bufnum = entry / FAT16BUFSIZE;
244 offset = entry - bufnum * FAT16BUFSIZE;
247 bufnum = entry / FAT12BUFSIZE;
248 offset = entry - bufnum * FAT12BUFSIZE;
252 /* Unsupported FAT size */
255 /* Read a new block of FAT entries into the cache. */
256 if (bufnum != mydata->fatbufnum) {
257 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
258 __u8 *bufptr = mydata->fatbuf;
259 __u32 fatlength = mydata->fatlength;
260 __u32 startblock = bufnum * FATBUFBLOCKS;
263 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
264 startblock += mydata->fat_sect; /* Offset from start of disk */
266 if (getsize > fatlength) getsize = fatlength;
267 if (disk_read(startblock, getsize, bufptr) < 0) {
268 FAT_DPRINT("Error reading FAT blocks\n");
271 mydata->fatbufnum = bufnum;
274 /* Get the actual entry from the table */
275 switch (mydata->fatsize) {
277 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
280 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
283 __u32 off16 = (offset*3)/4;
286 switch (offset & 0x3) {
288 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
292 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
294 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
296 ret = (val2 << 4) | (val1 >> 12);
299 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
301 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
303 ret = (val2 << 8) | (val1 >> 8);
306 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
307 ret = (ret & 0xfff0) >> 4;
315 FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
322 * Read at most 'size' bytes from the specified cluster into 'buffer'.
323 * Return 0 on success, -1 otherwise.
326 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
332 startsect = mydata->data_begin + clustnum*mydata->clust_size;
334 startsect = mydata->rootdir_sect;
337 if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
338 FAT_DPRINT("Error reading data\n");
341 if(size % FS_BLOCK_SIZE) {
342 __u8 tmpbuf[FS_BLOCK_SIZE];
343 idx= size/FS_BLOCK_SIZE;
344 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
345 FAT_DPRINT("Error reading data\n");
348 buffer += idx*FS_BLOCK_SIZE;
350 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
359 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
361 * Return the number of bytes read or -1 on fatal errors.
364 get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
365 unsigned long maxsize)
367 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
368 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
369 __u32 curclust = START(dentptr);
370 __u32 endclust, newclust;
371 unsigned long actsize;
373 FAT_DPRINT("Filesize: %ld bytes\n", filesize);
375 if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
377 FAT_DPRINT("Reading: %ld bytes\n", filesize);
379 actsize=bytesperclust;
382 /* search for consecutive clusters */
383 while(actsize < filesize) {
384 newclust = get_fatent(mydata, endclust);
385 if((newclust -1)!=endclust)
387 if (newclust <= 0x0001 || newclust >= 0xfffff0) {
388 FAT_DPRINT("curclust: 0x%x\n", newclust);
389 FAT_DPRINT("Invalid FAT entry\n");
393 actsize+= bytesperclust;
395 /* actsize >= file size */
396 actsize -= bytesperclust;
397 /* get remaining clusters */
398 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
399 FAT_ERROR("Error reading cluster\n");
402 /* get remaining bytes */
403 gotsize += (int)actsize;
407 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
408 FAT_ERROR("Error reading cluster\n");
414 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
415 FAT_ERROR("Error reading cluster\n");
418 gotsize += (int)actsize;
421 curclust = get_fatent(mydata, endclust);
422 if (curclust <= 0x0001 || curclust >= 0xfffff0) {
423 FAT_DPRINT("curclust: 0x%x\n", curclust);
424 FAT_ERROR("Invalid FAT entry\n");
427 actsize=bytesperclust;
433 #ifdef CONFIG_SUPPORT_VFAT
435 * Extract the file name information from 'slotptr' into 'l_name',
436 * starting at l_name[*idx].
437 * Return 1 if terminator (zero byte) is found, 0 otherwise.
440 slot2str(dir_slot *slotptr, char *l_name, int *idx)
444 for (j = 0; j <= 8; j += 2) {
445 l_name[*idx] = slotptr->name0_4[j];
446 if (l_name[*idx] == 0x00) return 1;
449 for (j = 0; j <= 10; j += 2) {
450 l_name[*idx] = slotptr->name5_10[j];
451 if (l_name[*idx] == 0x00) return 1;
454 for (j = 0; j <= 2; j += 2) {
455 l_name[*idx] = slotptr->name11_12[j];
456 if (l_name[*idx] == 0x00) return 1;
463 /* Calculate short name checksum */
465 mkcksum(const char *str)
470 for (i = 0; i < 11; i++) {
471 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
480 * Get the directory entry associated with 'filename' from the directory
481 * starting at 'startsect'
485 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
486 char *filename, dir_entry * retdent,
493 __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
494 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
495 char *filename, dir_entry * retdent,
498 __u16 prevcksum = 0xffff;
499 __u32 curclust = START (retdent);
500 int files = 0, dirs = 0;
506 if (get_cluster (mydata, curclust, get_dentfromdir_block,
507 mydata->clust_size * SECTOR_SIZE) != 0) {
508 FAT_DPRINT ("Error: reading directory block\n");
511 dentptr = (dir_entry *) get_dentfromdir_block;
512 for (i = 0; i < DIRENTSPERCLUST; i++) {
513 char s_name[14], l_name[256];
516 if (dentptr->name[0] == DELETED_FLAG) {
520 if ((dentptr->attr & ATTR_VOLUME)) {
521 /* Volume label or VFAT entry */
525 if (dentptr->name[0] == 0) {
527 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
529 FAT_DPRINT ("Dentname == NULL - %d\n", i);
532 #ifdef CONFIG_SUPPORT_VFAT
533 if (dols && mkcksum (dentptr->name) == prevcksum) {
538 get_name (dentptr, s_name);
540 int isdir = (dentptr->attr & ATTR_DIR);
550 if (s_name[0] != 0) {
557 printf (" %8ld %s%c\n",
558 (long) FAT2CPU32 (dentptr->size), s_name,
561 printf (" %s%c\n", s_name, dirc);
567 if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
568 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
572 memcpy (retdent, dentptr, sizeof (dir_entry));
574 FAT_DPRINT ("DentName: %s", s_name);
575 FAT_DPRINT (", start: 0x%x", START (dentptr));
576 FAT_DPRINT (", size: 0x%x %s\n",
577 FAT2CPU32 (dentptr->size),
578 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
582 curclust = get_fatent (mydata, curclust);
583 if (curclust <= 0x0001 || curclust >= 0xfffff0) {
584 FAT_DPRINT ("curclust: 0x%x\n", curclust);
585 FAT_ERROR ("Invalid FAT entry\n");
596 * Read boot sector and volume info from a FAT filesystem
599 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
601 __u8 block[FS_BLOCK_SIZE];
602 volume_info *vistart;
604 printf("Reading boot sector\n");
606 if (disk_read(0, 1, block) < 0) {
607 FAT_DPRINT("Error: reading block\n");
611 memcpy(bs, block, sizeof(boot_sector));
612 bs->reserved = FAT2CPU16(bs->reserved);
613 bs->fat_length = FAT2CPU16(bs->fat_length);
614 bs->secs_track = FAT2CPU16(bs->secs_track);
615 bs->heads = FAT2CPU16(bs->heads);
617 bs->hidden = FAT2CPU32(bs->hidden);
619 bs->total_sect = FAT2CPU32(bs->total_sect);
622 if (bs->fat_length == 0) {
624 bs->fat32_length = FAT2CPU32(bs->fat32_length);
625 bs->flags = FAT2CPU16(bs->flags);
626 bs->root_cluster = FAT2CPU32(bs->root_cluster);
627 bs->info_sector = FAT2CPU16(bs->info_sector);
628 bs->backup_boot = FAT2CPU16(bs->backup_boot);
629 vistart = (volume_info*) (block + sizeof(boot_sector));
632 vistart = (volume_info*) &(bs->fat32_length);
635 memcpy(volinfo, vistart, sizeof(volume_info));
637 /* Terminate fs_type string. Writing past the end of vistart
638 is ok - it's just the buffer. */
639 vistart->fs_type[8] = '\0';
641 if (*fatsize == 32) {
642 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
646 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
650 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
656 FAT_DPRINT("Error: broken fs_type sign\n");
661 __u8 do_fat_read_block[MAX_CLUSTSIZE]; /* Block buffer */
664 __u8 *fnamecopy = 0x80500000;
665 __u8 *do_fat_read_block = 0x80500880;
672 do_fat_read(const char *filename, void *buffer, unsigned long maxsize,
675 #if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
678 fsdata *mydata = &datablock;
680 __u16 prevcksum = 0xffff;
682 int rootdir_size, cursect, curclus;
684 int files = 0, dirs = 0;
688 if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
689 printf ("Error: reading boot sector\n");
692 if (mydata->fatsize == 32) {
693 mydata->fatlength = bs.fat32_length;
695 mydata->fatlength = bs.fat_length;
697 mydata->fat_sect = bs.reserved;
698 cursect = mydata->rootdir_sect
699 = mydata->fat_sect + mydata->fatlength * bs.fats;
700 curclus = bs.root_cluster; // For FAT32 only
701 mydata->clust_size = bs.cluster_size;
702 if (mydata->fatsize == 32) {
703 rootdir_size = mydata->clust_size;
704 mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
705 - (mydata->clust_size * 2);
707 rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
708 * sizeof (dir_entry)) / SECTOR_SIZE;
709 mydata->data_begin = mydata->rootdir_sect + rootdir_size
710 - (mydata->clust_size * 2);
712 mydata->fatbufnum = -1;
714 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
716 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
717 "Data begins at: %d\n",
718 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
719 rootdir_size, mydata->data_begin);
720 FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
722 /* "cwd" is always the root... */
723 while (ISDIRDELIM (*filename))
725 /* Make a copy of the filename and convert it to lowercase */
726 strcpy (fnamecopy, filename);
727 downcase (fnamecopy);
728 if (*fnamecopy == '\0') {
730 printf("\n not there\n");
734 } else if ((idx = dirdelim (fnamecopy)) >= 0) {
736 fnamecopy[idx] = '\0';
737 subname = fnamecopy + idx + 1;
738 /* Handle multiple delimiters */
739 while (ISDIRDELIM (*subname))
748 if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
749 printf ("Error: reading rootdir block\n");
752 dentptr = (dir_entry *) do_fat_read_block;
753 for (i = 0; i < DIRENTSPERBLOCK; i++) {
754 char s_name[14], l_name[256];
757 if ((dentptr->attr & ATTR_VOLUME)) {
758 /* Volume label or VFAT entry */
761 } else if (dentptr->name[0] == 0) {
762 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
763 if (dols == LS_ROOT) {
764 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
769 #ifdef CONFIG_SUPPORT_VFAT
770 else if (dols == LS_ROOT
771 && mkcksum (dentptr->name) == prevcksum) {
776 get_name (dentptr, s_name);
777 if (dols == LS_ROOT) {
778 int isdir = (dentptr->attr & ATTR_DIR);
784 if (s_name[0] != 0) {
790 if (s_name[0] != 0) {
797 printf (" %8ld %s%c\n",
798 (long) FAT2CPU32 (dentptr->size), s_name,
801 printf (" %s%c\n", s_name, dirc);
807 if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
808 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
812 if (isdir && !(dentptr->attr & ATTR_DIR))
815 FAT_DPRINT ("RootName: %s", s_name);
816 FAT_DPRINT (", start: 0x%x", START (dentptr));
817 FAT_DPRINT (", size: 0x%x %s\n",
818 FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
820 goto rootdir_done; /* We got a match */
823 if (mydata->fatsize != 32)
826 // FAT32 does not guarantee contiguous root directory
827 curclus = get_fatent (mydata, curclus);
828 cursect = (curclus * mydata->clust_size) + mydata->data_begin;
830 FAT_DPRINT ("root clus %d sector %d\n", curclus, cursect);
837 int startsect = mydata->data_begin
838 + START (dentptr) * mydata->clust_size;
840 char *nextname = NULL;
844 idx = dirdelim (subname);
847 nextname = subname + idx + 1;
848 /* Handle multiple delimiters */
849 while (ISDIRDELIM (*nextname))
851 if (dols && *nextname == '\0')
854 if (dols && firsttime) {
861 if (get_dentfromdir (mydata, startsect, subname, dentptr,
862 isdir ? 0 : dols) == NULL) {
869 if (!(dentptr->attr & ATTR_DIR))
874 ret = get_contents (mydata, dentptr, buffer, maxsize);
875 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
882 file_fat_detectfs(void)
890 printf("No current device\n");
893 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
894 (CONFIG_COMMANDS & CFG_CMD_USB) || (CONFIG_MMC)
895 printf("Interface: ");
896 switch(cur_dev->if_type) {
897 case IF_TYPE_IDE : printf("IDE"); break;
898 case IF_TYPE_SCSI : printf("SCSI"); break;
899 case IF_TYPE_ATAPI : printf("ATAPI"); break;
900 case IF_TYPE_USB : printf("USB"); break;
901 case IF_TYPE_DOC : printf("DOC"); break;
902 case IF_TYPE_MMC : printf("MMC"); break;
903 default : printf("Unknown");
905 printf("\n Device %d: ",cur_dev->dev);
908 if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
909 printf("\nNo valid FAT fs found\n");
912 memcpy (vol_label, volinfo.volume_label, 11);
913 vol_label[11] = '\0';
914 volinfo.fs_type[5]='\0';
915 printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
921 file_fat_ls(const char *dir)
923 return do_fat_read(dir, NULL, 0, LS_YES);
928 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
931 ret = do_fat_read(filename, buffer, maxsize, LS_NO);
935 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */