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 = (__u8 *)mydata->fatbuf;
259 __u32 fatlength = mydata->fatlength;
260 __u32 startblock = bufnum * FATBUFBLOCKS;
262 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
263 startblock += mydata->fat_sect; /* Offset from start of disk */
265 if (getsize > fatlength) getsize = fatlength;
266 if (disk_read(startblock, getsize, bufptr) < 0) {
267 FAT_DPRINT("Error reading FAT blocks\n");
270 mydata->fatbufnum = bufnum;
273 /* Get the actual entry from the table */
274 switch (mydata->fatsize) {
276 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
279 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
282 __u32 off16 = (offset*3)/4;
285 switch (offset & 0x3) {
287 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
291 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
293 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
295 ret = (val2 << 4) | (val1 >> 12);
298 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
300 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
302 ret = (val2 << 8) | (val1 >> 8);
305 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
306 ret = (ret & 0xfff0) >> 4;
314 FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
321 * Read at most 'size' bytes from the specified cluster into 'buffer'.
322 * Return 0 on success, -1 otherwise.
325 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
331 startsect = mydata->data_begin + clustnum*mydata->clust_size;
333 startsect = mydata->rootdir_sect;
336 if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
337 FAT_DPRINT("Error reading data\n");
340 if(size % FS_BLOCK_SIZE) {
341 __u8 tmpbuf[FS_BLOCK_SIZE];
342 idx= size/FS_BLOCK_SIZE;
343 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
344 FAT_DPRINT("Error reading data\n");
347 buffer += idx*FS_BLOCK_SIZE;
349 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
358 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
360 * Return the number of bytes read or -1 on fatal errors.
363 get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
364 unsigned long maxsize)
366 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
367 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
368 __u32 curclust = START(dentptr);
369 __u32 endclust, newclust;
370 unsigned long actsize;
372 FAT_DPRINT("Filesize: %ld bytes\n", filesize);
374 if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
376 FAT_DPRINT("Reading: %ld bytes\n", filesize);
378 actsize=bytesperclust;
381 /* search for consecutive clusters */
382 while(actsize < filesize) {
383 newclust = get_fatent(mydata, endclust);
384 if((newclust -1)!=endclust)
386 if (newclust <= 0x0001 || newclust >= 0xfffff0) {
387 FAT_DPRINT("curclust: 0x%x\n", newclust);
388 FAT_DPRINT("Invalid FAT entry\n");
392 actsize+= bytesperclust;
394 /* actsize >= file size */
395 actsize -= bytesperclust;
396 /* get remaining clusters */
397 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
398 FAT_ERROR("Error reading cluster\n");
401 /* get remaining bytes */
402 gotsize += (int)actsize;
406 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
407 FAT_ERROR("Error reading cluster\n");
413 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
414 FAT_ERROR("Error reading cluster\n");
417 gotsize += (int)actsize;
420 curclust = get_fatent(mydata, endclust);
421 if (curclust <= 0x0001 || curclust >= 0xfffff0) {
422 FAT_DPRINT("curclust: 0x%x\n", curclust);
423 FAT_ERROR("Invalid FAT entry\n");
426 actsize=bytesperclust;
432 #ifdef CONFIG_SUPPORT_VFAT
434 /* Calculate short name checksum */
436 mkcksum(const char *str)
441 for (i = 0; i < 11; i++) {
442 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
451 * Get the directory entry associated with 'filename' from the directory
452 * starting at 'startsect'
456 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
457 char *filename, dir_entry * retdent,
464 __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
465 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
466 char *filename, dir_entry * retdent,
469 __u16 prevcksum = 0xffff;
470 __u32 curclust = START (retdent);
471 int files = 0, dirs = 0;
477 if (get_cluster (mydata, curclust, get_dentfromdir_block,
478 mydata->clust_size * SECTOR_SIZE) != 0) {
479 FAT_DPRINT ("Error: reading directory block\n");
482 dentptr = (dir_entry *) get_dentfromdir_block;
483 for (i = 0; i < DIRENTSPERCLUST; i++) {
484 char s_name[14], l_name[256];
487 if (dentptr->name[0] == DELETED_FLAG) {
491 if ((dentptr->attr & ATTR_VOLUME)) {
492 /* Volume label or VFAT entry */
496 if (dentptr->name[0] == 0) {
498 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
500 FAT_DPRINT ("Dentname == NULL - %d\n", i);
503 #ifdef CONFIG_SUPPORT_VFAT
504 if (dols && mkcksum (dentptr->name) == prevcksum) {
509 get_name (dentptr, s_name);
511 int isdir = (dentptr->attr & ATTR_DIR);
521 if (s_name[0] != 0) {
528 printf (" %8ld %s%c\n",
529 (long) FAT2CPU32 (dentptr->size), s_name,
532 printf (" %s%c\n", s_name, dirc);
538 if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
539 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
543 memcpy (retdent, dentptr, sizeof (dir_entry));
545 FAT_DPRINT ("DentName: %s", s_name);
546 FAT_DPRINT (", start: 0x%x", START (dentptr));
547 FAT_DPRINT (", size: 0x%x %s\n",
548 FAT2CPU32 (dentptr->size),
549 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
553 curclust = get_fatent (mydata, curclust);
554 if (curclust <= 0x0001 || curclust >= 0xfffff0) {
555 FAT_DPRINT ("curclust: 0x%x\n", curclust);
556 FAT_ERROR ("Invalid FAT entry\n");
567 * Read boot sector and volume info from a FAT filesystem
570 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
572 __u8 block[FS_BLOCK_SIZE];
573 volume_info *vistart;
576 printf("Reading boot sector\n");
578 if (disk_read(0, 1, block) < 0) {
579 FAT_DPRINT("Error: reading block\n");
583 memcpy(bs, block, sizeof(boot_sector));
584 bs->reserved = FAT2CPU16(bs->reserved);
585 bs->fat_length = FAT2CPU16(bs->fat_length);
586 bs->secs_track = FAT2CPU16(bs->secs_track);
587 bs->heads = FAT2CPU16(bs->heads);
589 bs->hidden = FAT2CPU32(bs->hidden);
591 bs->total_sect = FAT2CPU32(bs->total_sect);
594 if (bs->fat_length == 0) {
596 bs->fat32_length = FAT2CPU32(bs->fat32_length);
597 bs->flags = FAT2CPU16(bs->flags);
598 bs->root_cluster = FAT2CPU32(bs->root_cluster);
599 bs->info_sector = FAT2CPU16(bs->info_sector);
600 bs->backup_boot = FAT2CPU16(bs->backup_boot);
601 vistart = (volume_info*) (block + sizeof(boot_sector));
604 vistart = (volume_info*) &(bs->fat32_length);
607 memcpy(volinfo, vistart, sizeof(volume_info));
609 /* Terminate fs_type string. Writing past the end of vistart
610 is ok - it's just the buffer. */
611 p = (char *)&vistart->fs_type[0];
614 if (*fatsize == 32) {
615 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
619 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
623 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
629 FAT_DPRINT("Error: broken fs_type sign\n");
634 __u8 do_fat_read_block[MAX_CLUSTSIZE]; /* Block buffer */
637 __u8 *fnamecopy = (__u8 *)0x80500000;
638 __u8 *do_fat_read_block = (__u8 *)0x80500880;
645 do_fat_read(const char *filename, void *buffer, unsigned long maxsize,
648 #if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
651 fsdata *mydata = &datablock;
653 __u16 prevcksum = 0xffff;
655 int rootdir_size, cursect, curclus;
657 int files = 0, dirs = 0;
661 if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
662 printf ("Error: reading boot sector\n");
665 if (mydata->fatsize == 32) {
666 mydata->fatlength = bs.fat32_length;
668 mydata->fatlength = bs.fat_length;
670 mydata->fat_sect = bs.reserved;
671 cursect = mydata->rootdir_sect
672 = mydata->fat_sect + mydata->fatlength * bs.fats;
673 curclus = bs.root_cluster; // For FAT32 only
674 mydata->clust_size = bs.cluster_size;
675 if (mydata->fatsize == 32) {
676 rootdir_size = mydata->clust_size;
677 mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
678 - (mydata->clust_size * 2);
680 rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
681 * sizeof (dir_entry)) / SECTOR_SIZE;
682 mydata->data_begin = mydata->rootdir_sect + rootdir_size
683 - (mydata->clust_size * 2);
685 mydata->fatbufnum = -1;
687 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
689 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
690 "Data begins at: %d\n",
691 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
692 rootdir_size, mydata->data_begin);
693 FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
695 /* "cwd" is always the root... */
696 while (ISDIRDELIM (*filename))
698 /* Make a copy of the filename and convert it to lowercase */
699 strcpy ((char *)fnamecopy, filename);
700 downcase ((char *)fnamecopy);
701 if (*fnamecopy == '\0') {
703 printf("\n not there\n");
707 } else if ((idx = dirdelim ((char *)fnamecopy)) >= 0) {
709 fnamecopy[idx] = '\0';
710 subname = (char *)fnamecopy + idx + 1;
711 /* Handle multiple delimiters */
712 while (ISDIRDELIM (*subname))
721 if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
722 printf ("Error: reading rootdir block\n");
725 dentptr = (dir_entry *) do_fat_read_block;
726 for (i = 0; i < DIRENTSPERBLOCK; i++) {
727 char s_name[14], l_name[256];
730 if ((dentptr->attr & ATTR_VOLUME)) {
731 /* Volume label or VFAT entry */
734 } else if (dentptr->name[0] == 0) {
735 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
736 if (dols == LS_ROOT) {
737 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
742 #ifdef CONFIG_SUPPORT_VFAT
743 else if (dols == LS_ROOT
744 && mkcksum (dentptr->name) == prevcksum) {
749 get_name (dentptr, s_name);
750 if (dols == LS_ROOT) {
751 int isdir = (dentptr->attr & ATTR_DIR);
757 if (s_name[0] != 0) {
763 if (s_name[0] != 0) {
770 printf (" %8ld %s%c\n",
771 (long) FAT2CPU32 (dentptr->size), s_name,
774 printf (" %s%c\n", s_name, dirc);
780 if (strcmp ((char *)fnamecopy, s_name) &&
781 strcmp ((char *)fnamecopy, l_name)) {
782 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
786 if (isdir && !(dentptr->attr & ATTR_DIR))
789 FAT_DPRINT ("RootName: %s", s_name);
790 FAT_DPRINT (", start: 0x%x", START (dentptr));
791 FAT_DPRINT (", size: 0x%x %s\n",
792 FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
794 goto rootdir_done; /* We got a match */
797 if (mydata->fatsize != 32)
800 // FAT32 does not guarantee contiguous root directory
801 curclus = get_fatent (mydata, curclus);
802 cursect = (curclus * mydata->clust_size) + mydata->data_begin;
804 FAT_DPRINT ("root clus %d sector %d\n", curclus, cursect);
811 int startsect = mydata->data_begin
812 + START (dentptr) * mydata->clust_size;
814 char *nextname = NULL;
818 idx = dirdelim (subname);
821 nextname = subname + idx + 1;
822 /* Handle multiple delimiters */
823 while (ISDIRDELIM (*nextname))
825 if (dols && *nextname == '\0')
828 if (dols && firsttime) {
835 if (get_dentfromdir (mydata, startsect, subname, dentptr,
836 isdir ? 0 : dols) == NULL) {
843 if (!(dentptr->attr & ATTR_DIR))
848 ret = get_contents (mydata, dentptr, buffer, maxsize);
849 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
856 file_fat_detectfs(void)
864 printf("No current device\n");
867 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
868 (CONFIG_COMMANDS & CFG_CMD_USB) || (CONFIG_MMC)
869 printf("Interface: ");
870 switch(cur_dev->if_type) {
871 case IF_TYPE_IDE : printf("IDE"); break;
872 case IF_TYPE_SCSI : printf("SCSI"); break;
873 case IF_TYPE_ATAPI : printf("ATAPI"); break;
874 case IF_TYPE_USB : printf("USB"); break;
875 case IF_TYPE_DOC : printf("DOC"); break;
876 case IF_TYPE_MMC : printf("MMC"); break;
877 default : printf("Unknown");
879 printf("\n Device %d: ",cur_dev->dev);
882 if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
883 printf("\nNo valid FAT fs found\n");
886 memcpy (vol_label, volinfo.volume_label, 11);
887 vol_label[11] = '\0';
888 volinfo.fs_type[5]='\0';
889 printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
895 file_fat_ls(const char *dir)
897 return do_fat_read(dir, NULL, 0, LS_YES);
902 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
905 ret = do_fat_read(filename, buffer, maxsize, LS_NO);
909 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */