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 /* FIXME we need to determine the start block of the
149 * partition where the DOS FS resides. This can be done
150 * by using the get_partition_info routine. For this
151 * purpose the libpart must be included.
163 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
164 * Return index into string if found, -1 otherwise.
171 while (*str != '\0') {
172 if (ISDIRDELIM(*str)) return str - start;
180 * Match volume_info fs_type strings.
181 * Return 0 on match, -1 otherwise.
184 compare_sign(char *str1, char *str2)
186 char *end = str1+SIGNLEN;
188 while (str1 != end) {
189 if (*str1 != *str2) {
201 * Extract zero terminated short name from a directory entry.
203 static void get_name (dir_entry *dirent, char *s_name)
207 memcpy (s_name, dirent->name, 8);
210 while (*ptr && *ptr != ' ')
212 if (dirent->ext[0] && dirent->ext[0] != ' ') {
215 memcpy (ptr, dirent->ext, 3);
217 while (*ptr && *ptr != ' ')
221 if (*s_name == DELETED_FLAG)
223 else if (*s_name == aRING)
229 * Get the entry at index 'entry' in a FAT (12/16/32) table.
230 * On failure 0x00 is returned.
233 get_fatent(fsdata *mydata, __u32 entry)
239 switch (mydata->fatsize) {
241 bufnum = entry / FAT32BUFSIZE;
242 offset = entry - bufnum * FAT32BUFSIZE;
245 bufnum = entry / FAT16BUFSIZE;
246 offset = entry - bufnum * FAT16BUFSIZE;
249 bufnum = entry / FAT12BUFSIZE;
250 offset = entry - bufnum * FAT12BUFSIZE;
254 /* Unsupported FAT size */
257 /* Read a new block of FAT entries into the cache. */
258 if (bufnum != mydata->fatbufnum) {
259 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
260 __u8 *bufptr = mydata->fatbuf;
261 __u32 fatlength = mydata->fatlength;
262 __u32 startblock = bufnum * FATBUFBLOCKS;
265 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
266 startblock += mydata->fat_sect; /* Offset from start of disk */
268 if (getsize > fatlength) getsize = fatlength;
269 if (disk_read(startblock, getsize, bufptr) < 0) {
270 FAT_DPRINT("Error reading FAT blocks\n");
273 mydata->fatbufnum = bufnum;
276 /* Get the actual entry from the table */
277 switch (mydata->fatsize) {
279 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
282 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
285 __u32 off16 = (offset*3)/4;
288 switch (offset & 0x3) {
290 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
294 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
296 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
298 ret = (val2 << 4) | (val1 >> 12);
301 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
303 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
305 ret = (val2 << 8) | (val1 >> 8);
308 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
309 ret = (ret & 0xfff0) >> 4;
317 FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
324 * Read at most 'size' bytes from the specified cluster into 'buffer'.
325 * Return 0 on success, -1 otherwise.
328 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
334 startsect = mydata->data_begin + clustnum*mydata->clust_size;
336 startsect = mydata->rootdir_sect;
339 if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
340 FAT_DPRINT("Error reading data\n");
343 if(size % FS_BLOCK_SIZE) {
344 __u8 tmpbuf[FS_BLOCK_SIZE];
345 idx= size/FS_BLOCK_SIZE;
346 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
347 FAT_DPRINT("Error reading data\n");
350 buffer += idx*FS_BLOCK_SIZE;
352 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
361 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
363 * Return the number of bytes read or -1 on fatal errors.
366 get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
367 unsigned long maxsize)
369 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
370 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
371 __u32 curclust = START(dentptr);
372 __u32 endclust, newclust;
373 unsigned long actsize;
375 FAT_DPRINT("Filesize: %ld bytes\n", filesize);
377 if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
379 FAT_DPRINT("Reading: %ld bytes\n", filesize);
381 actsize=bytesperclust;
384 /* search for consecutive clusters */
385 while(actsize < filesize) {
386 newclust = get_fatent(mydata, endclust);
387 if((newclust -1)!=endclust)
389 if (newclust <= 0x0001 || newclust >= 0xfff0) {
390 FAT_DPRINT("curclust: 0x%x\n", newclust);
391 FAT_DPRINT("Invalid FAT entry\n");
395 actsize+= bytesperclust;
397 /* actsize >= file size */
398 actsize -= bytesperclust;
399 /* get remaining clusters */
400 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
401 FAT_ERROR("Error reading cluster\n");
404 /* get remaining bytes */
405 gotsize += (int)actsize;
409 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
410 FAT_ERROR("Error reading cluster\n");
416 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
417 FAT_ERROR("Error reading cluster\n");
420 gotsize += (int)actsize;
423 curclust = get_fatent(mydata, endclust);
424 if (curclust <= 0x0001 || curclust >= 0xfff0) {
425 FAT_DPRINT("curclust: 0x%x\n", curclust);
426 FAT_ERROR("Invalid FAT entry\n");
429 actsize=bytesperclust;
435 #ifdef CONFIG_SUPPORT_VFAT
437 * Extract the file name information from 'slotptr' into 'l_name',
438 * starting at l_name[*idx].
439 * Return 1 if terminator (zero byte) is found, 0 otherwise.
442 slot2str(dir_slot *slotptr, char *l_name, int *idx)
446 for (j = 0; j <= 8; j += 2) {
447 l_name[*idx] = slotptr->name0_4[j];
448 if (l_name[*idx] == 0x00) return 1;
451 for (j = 0; j <= 10; j += 2) {
452 l_name[*idx] = slotptr->name5_10[j];
453 if (l_name[*idx] == 0x00) return 1;
456 for (j = 0; j <= 2; j += 2) {
457 l_name[*idx] = slotptr->name11_12[j];
458 if (l_name[*idx] == 0x00) return 1;
465 /* Calculate short name checksum */
467 mkcksum(const char *str)
472 for (i = 0; i < 11; i++) {
473 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
482 * Get the directory entry associated with 'filename' from the directory
483 * starting at 'startsect'
487 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
488 char *filename, dir_entry * retdent,
495 __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
496 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
497 char *filename, dir_entry * retdent,
500 __u16 prevcksum = 0xffff;
501 __u32 curclust = START (retdent);
502 int files = 0, dirs = 0;
508 if (get_cluster (mydata, curclust, get_dentfromdir_block,
509 mydata->clust_size * SECTOR_SIZE) != 0) {
510 FAT_DPRINT ("Error: reading directory block\n");
513 dentptr = (dir_entry *) get_dentfromdir_block;
514 for (i = 0; i < DIRENTSPERCLUST; i++) {
515 char s_name[14], l_name[256];
518 if (dentptr->name[0] == DELETED_FLAG) {
522 if ((dentptr->attr & ATTR_VOLUME)) {
523 /* Volume label or VFAT entry */
527 if (dentptr->name[0] == 0) {
529 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
531 FAT_DPRINT ("Dentname == NULL - %d\n", i);
534 #ifdef CONFIG_SUPPORT_VFAT
535 if (dols && mkcksum (dentptr->name) == prevcksum) {
540 get_name (dentptr, s_name);
542 int isdir = (dentptr->attr & ATTR_DIR);
552 if (s_name[0] != 0) {
559 printf (" %8ld %s%c\n",
560 (long) FAT2CPU32 (dentptr->size), s_name,
563 printf (" %s%c\n", s_name, dirc);
569 if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
570 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
574 memcpy (retdent, dentptr, sizeof (dir_entry));
576 FAT_DPRINT ("DentName: %s", s_name);
577 FAT_DPRINT (", start: 0x%x", START (dentptr));
578 FAT_DPRINT (", size: 0x%x %s\n",
579 FAT2CPU32 (dentptr->size),
580 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
584 curclust = get_fatent (mydata, curclust);
585 if (curclust <= 0x0001 || curclust >= 0xfff0) {
586 FAT_DPRINT ("curclust: 0x%x\n", curclust);
587 FAT_ERROR ("Invalid FAT entry\n");
598 * Read boot sector and volume info from a FAT filesystem
601 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
603 __u8 block[FS_BLOCK_SIZE];
604 volume_info *vistart;
606 printf("Reading boot sector\n");
608 if (disk_read(0, 1, block) < 0) {
609 FAT_DPRINT("Error: reading block\n");
613 memcpy(bs, block, sizeof(boot_sector));
614 bs->reserved = FAT2CPU16(bs->reserved);
615 bs->fat_length = FAT2CPU16(bs->fat_length);
616 bs->secs_track = FAT2CPU16(bs->secs_track);
617 bs->heads = FAT2CPU16(bs->heads);
619 bs->hidden = FAT2CPU32(bs->hidden);
621 bs->total_sect = FAT2CPU32(bs->total_sect);
624 if (bs->fat_length == 0) {
626 bs->fat32_length = FAT2CPU32(bs->fat32_length);
627 bs->flags = FAT2CPU16(bs->flags);
628 bs->root_cluster = FAT2CPU32(bs->root_cluster);
629 bs->info_sector = FAT2CPU16(bs->info_sector);
630 bs->backup_boot = FAT2CPU16(bs->backup_boot);
631 vistart = (volume_info*) (block + sizeof(boot_sector));
634 vistart = (volume_info*) &(bs->fat32_length);
637 memcpy(volinfo, vistart, sizeof(volume_info));
639 /* Terminate fs_type string. Writing past the end of vistart
640 is ok - it's just the buffer. */
641 vistart->fs_type[8] = '\0';
643 if (*fatsize == 32) {
644 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
648 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
652 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
658 FAT_DPRINT("Error: broken fs_type sign\n");
663 __u8 do_fat_read_block[MAX_CLUSTSIZE]; /* Block buffer */
666 __u8 *fnamecopy = 0x80500000;
667 __u8 *do_fat_read_block = 0x80500880;
674 do_fat_read(const char *filename, void *buffer, unsigned long maxsize,
677 #if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
680 fsdata *mydata = &datablock;
682 __u16 prevcksum = 0xffff;
684 int rootdir_size, cursect;
686 int files = 0, dirs = 0;
690 if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
691 printf ("Error: reading boot sector\n");
694 if (mydata->fatsize == 32) {
695 mydata->fatlength = bs.fat32_length;
697 mydata->fatlength = bs.fat_length;
699 mydata->fat_sect = bs.reserved;
700 cursect = mydata->rootdir_sect
701 = mydata->fat_sect + mydata->fatlength * bs.fats;
702 mydata->clust_size = bs.cluster_size;
703 if (mydata->fatsize == 32) {
704 rootdir_size = mydata->clust_size;
705 mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
706 - (mydata->clust_size * 2);
708 rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
709 * sizeof (dir_entry)) / SECTOR_SIZE;
710 mydata->data_begin = mydata->rootdir_sect + rootdir_size
711 - (mydata->clust_size * 2);
713 mydata->fatbufnum = -1;
715 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
717 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
718 "Data begins at: %d\n",
719 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
720 rootdir_size, mydata->data_begin);
721 FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
723 /* "cwd" is always the root... */
724 while (ISDIRDELIM (*filename))
726 /* Make a copy of the filename and convert it to lowercase */
727 strcpy (fnamecopy, filename);
728 downcase (fnamecopy);
729 if (*fnamecopy == '\0') {
731 printf("\n not there\n");
735 } else if ((idx = dirdelim (fnamecopy)) >= 0) {
737 fnamecopy[idx] = '\0';
738 subname = fnamecopy + idx + 1;
739 /* Handle multiple delimiters */
740 while (ISDIRDELIM (*subname))
749 if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
750 printf ("Error: reading rootdir block\n");
753 dentptr = (dir_entry *) do_fat_read_block;
754 for (i = 0; i < DIRENTSPERBLOCK; i++) {
755 char s_name[14], l_name[256];
758 if ((dentptr->attr & ATTR_VOLUME)) {
759 /* Volume label or VFAT entry */
762 } else if (dentptr->name[0] == 0) {
763 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
764 if (dols == LS_ROOT) {
765 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
770 #ifdef CONFIG_SUPPORT_VFAT
771 else if (dols == LS_ROOT
772 && mkcksum (dentptr->name) == prevcksum) {
777 get_name (dentptr, s_name);
778 if (dols == LS_ROOT) {
779 int isdir = (dentptr->attr & ATTR_DIR);
785 if (s_name[0] != 0) {
791 if (s_name[0] != 0) {
798 printf (" %8ld %s%c\n",
799 (long) FAT2CPU32 (dentptr->size), s_name,
802 printf (" %s%c\n", s_name, dirc);
808 if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
809 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
813 if (isdir && !(dentptr->attr & ATTR_DIR))
816 FAT_DPRINT ("RootName: %s", s_name);
817 FAT_DPRINT (", start: 0x%x", START (dentptr));
818 FAT_DPRINT (", size: 0x%x %s\n",
819 FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
821 goto rootdir_done; /* We got a match */
829 int startsect = mydata->data_begin
830 + START (dentptr) * mydata->clust_size;
832 char *nextname = NULL;
836 idx = dirdelim (subname);
839 nextname = subname + idx + 1;
840 /* Handle multiple delimiters */
841 while (ISDIRDELIM (*nextname))
843 if (dols && *nextname == '\0')
846 if (dols && firsttime) {
853 if (get_dentfromdir (mydata, startsect, subname, dentptr,
854 isdir ? 0 : dols) == NULL) {
861 if (!(dentptr->attr & ATTR_DIR))
866 ret = get_contents (mydata, dentptr, buffer, maxsize);
867 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
874 file_fat_detectfs(void)
882 printf("No current device\n");
885 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
886 (CONFIG_COMMANDS & CFG_CMD_USB) || (CONFIG_MMC)
887 printf("Interface: ");
888 switch(cur_dev->if_type) {
889 case IF_TYPE_IDE : printf("IDE"); break;
890 case IF_TYPE_SCSI : printf("SCSI"); break;
891 case IF_TYPE_ATAPI : printf("ATAPI"); break;
892 case IF_TYPE_USB : printf("USB"); break;
893 case IF_TYPE_DOC : printf("DOC"); break;
894 case IF_TYPE_MMC : printf("MMC"); break;
895 default : printf("Unknown");
897 printf("\n Device %d: ",cur_dev->dev);
900 if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
901 printf("\nNo valid FAT fs found\n");
904 memcpy (vol_label, volinfo.volume_label, 11);
905 vol_label[11] = '\0';
906 volinfo.fs_type[5]='\0';
907 printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
913 file_fat_ls(const char *dir)
915 return do_fat_read(dir, NULL, 0, LS_YES);
920 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
923 ret = do_fat_read(filename, buffer, maxsize, LS_NO);
927 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */