]>
git.ipfire.org Git - people/ms/u-boot.git/blob - fs/fat/fat.c
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>
33 #if (CONFIG_COMMANDS & CFG_CMD_FAT)
36 * Convert a string to lowercase.
41 while (*str
!= '\0') {
47 int (*dev_block_read
)(int device
, __u32 blknr
, __u32 blkcnt
, __u8
*buffer
) = 0;
49 int disk_read (__u32 startblock
, __u32 getsize
, __u8
* bufptr
)
51 /* FIXME we need to determine the start block of the
52 * partition where the DOS FS resides
57 return dev_block_read (0, startblock
, getsize
, bufptr
);
64 fat_register_read (int (*block_read
)(int, __u32
, __u32
, __u8
*))
66 dev_block_read
= block_read
;
72 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
73 * Return index into string if found, -1 otherwise.
80 while (*str
!= '\0') {
81 if (ISDIRDELIM(*str
)) return str
- start
;
89 * Match volume_info fs_type strings.
90 * Return 0 on match, -1 otherwise.
93 compare_sign(char *str1
, char *str2
)
95 char *end
= str1
+SIGNLEN
;
110 * Extract zero terminated short name from a directory entry.
112 static void get_name (dir_entry
*dirent
, char *s_name
)
116 memcpy (s_name
, dirent
->name
, 8);
119 while (*ptr
&& *ptr
!= ' ')
121 if (dirent
->ext
[0] && dirent
->ext
[0] != ' ') {
124 memcpy (ptr
, dirent
->ext
, 3);
126 while (*ptr
&& *ptr
!= ' ')
130 if (*s_name
== DELETED_FLAG
)
132 else if (*s_name
== aRING
)
138 * Get the entry at index 'entry' in a FAT (12/16/32) table.
139 * On failure 0x00 is returned.
142 get_fatent(fsdata
*mydata
, __u32 entry
)
148 switch (mydata
->fatsize
) {
150 bufnum
= entry
/ FAT32BUFSIZE
;
151 offset
= entry
- bufnum
* FAT32BUFSIZE
;
154 bufnum
= entry
/ FAT16BUFSIZE
;
155 offset
= entry
- bufnum
* FAT16BUFSIZE
;
158 bufnum
= entry
/ FAT12BUFSIZE
;
159 offset
= entry
- bufnum
* FAT12BUFSIZE
;
163 /* Unsupported FAT size */
167 /* Read a new block of FAT entries into the cache. */
168 if (bufnum
!= mydata
->fatbufnum
) {
169 int getsize
= FATBUFSIZE
/FS_BLOCK_SIZE
;
170 __u8
*bufptr
= mydata
->fatbuf
;
171 __u32 fatlength
= mydata
->fatlength
;
172 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
174 fatlength
*= SECTOR_SIZE
; /* We want it in bytes now */
175 startblock
+= mydata
->fat_sect
; /* Offset from start of disk */
177 if (getsize
> fatlength
) getsize
= fatlength
;
178 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
179 FAT_DPRINT("Error reading FAT blocks\n");
182 mydata
->fatbufnum
= bufnum
;
185 /* Get the actual entry from the table */
186 switch (mydata
->fatsize
) {
188 ret
= FAT2CPU32(((__u32
*)mydata
->fatbuf
)[offset
]);
191 ret
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[offset
]);
194 __u32 off16
= (offset
*3)/4;
197 switch (offset
& 0x3) {
199 ret
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
203 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
205 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+1]);
207 ret
= (val2
<< 4) | (val1
>> 12);
210 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
212 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+1]);
214 ret
= (val2
<< 8) | (val1
>> 8);
217 ret
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);;
218 ret
= (ret
& 0xfff0) >> 4;
226 FAT_DPRINT("ret: %d, offset: %d\n", ret
, offset
);
233 * Read at most 'size' bytes from the specified cluster into 'buffer'.
234 * Return 0 on success, -1 otherwise.
237 get_cluster(fsdata
*mydata
, __u32 clustnum
, __u8
*buffer
, unsigned long size
)
243 startsect
= mydata
->data_begin
+ clustnum
*mydata
->clust_size
;
245 startsect
= mydata
->rootdir_sect
;
248 FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum
, startsect
);
250 if (size
>= FS_BLOCK_SIZE
) {
251 if (disk_read(startsect
+ idx
, 1, buffer
) < 0) {
252 FAT_DPRINT("Error reading data\n");
256 __u8 tmpbuf
[FS_BLOCK_SIZE
];
257 if (disk_read(startsect
+ idx
, 1, tmpbuf
) < 0) {
258 FAT_DPRINT("Error reading data\n");
261 memcpy(buffer
, tmpbuf
, size
);
265 buffer
+= FS_BLOCK_SIZE
;
266 size
-= FS_BLOCK_SIZE
;
275 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
277 * Return the number of bytes read or -1 on fatal errors.
280 get_contents(fsdata
*mydata
, dir_entry
*dentptr
, __u8
*buffer
,
281 unsigned long maxsize
)
283 unsigned long filesize
= FAT2CPU32(dentptr
->size
), gotsize
= 0;
284 unsigned int bytesperclust
= mydata
->clust_size
* SECTOR_SIZE
;
285 __u32 curclust
= START(dentptr
);
287 FAT_DPRINT("Filesize: %ld bytes\n", filesize
);
289 if (maxsize
> 0 && filesize
> maxsize
) filesize
= maxsize
;
291 FAT_DPRINT("Reading: %ld bytes\n", filesize
);
294 int getsize
= (filesize
> bytesperclust
) ? bytesperclust
:
297 if (get_cluster(mydata
, curclust
, buffer
, getsize
) != 0) {
298 FAT_ERROR("Error reading cluster\n");
303 if (filesize
<= 0) return gotsize
;
306 curclust
= get_fatent(mydata
, curclust
);
307 if (curclust
<= 0x0001 || curclust
>= 0xfff0) {
308 FAT_DPRINT("curclust: 0x%x\n", curclust
);
309 FAT_ERROR("Invalid FAT entry\n");
316 #ifdef CONFIG_SUPPORT_VFAT
318 * Extract the file name information from 'slotptr' into 'l_name',
319 * starting at l_name[*idx].
320 * Return 1 if terminator (zero byte) is found, 0 otherwise.
323 slot2str(dir_slot
*slotptr
, char *l_name
, int *idx
)
327 for (j
= 0; j
<= 8; j
+= 2) {
328 l_name
[*idx
] = slotptr
->name0_4
[j
];
329 if (l_name
[*idx
] == 0x00) return 1;
332 for (j
= 0; j
<= 10; j
+= 2) {
333 l_name
[*idx
] = slotptr
->name5_10
[j
];
334 if (l_name
[*idx
] == 0x00) return 1;
337 for (j
= 0; j
<= 2; j
+= 2) {
338 l_name
[*idx
] = slotptr
->name11_12
[j
];
339 if (l_name
[*idx
] == 0x00) return 1;
348 * Extract the full long filename starting at 'retdent' (which is really
349 * a slot) into 'l_name'. If successful also copy the real directory entry
351 * Return 0 on success, -1 otherwise.
354 get_vfatname(fsdata
*mydata
, int curclust
, __u8
*cluster
,
355 dir_entry
*retdent
, char *l_name
)
358 dir_slot
*slotptr
= (dir_slot
*) retdent
;
359 __u8
*nextclust
= cluster
+ mydata
->clust_size
* SECTOR_SIZE
;
360 __u8 counter
= slotptr
->id
& 0xf;
363 while ((__u8
*)slotptr
< nextclust
) {
364 if (counter
== 0) break;
365 if ((slotptr
->id
& 0x0f) != counter
) return -1;
370 if ((__u8
*)slotptr
>= nextclust
) {
371 __u8 block
[MAX_CLUSTSIZE
];
375 curclust
= get_fatent(mydata
, curclust
);
376 if (curclust
<= 0x0001 || curclust
>= 0xfff0) {
377 FAT_DPRINT("curclust: 0x%x\n", curclust
);
378 FAT_ERROR("Invalid FAT entry\n");
381 if (get_cluster(mydata
, curclust
, block
,
382 mydata
->clust_size
* SECTOR_SIZE
) != 0) {
383 FAT_DPRINT("Error: reading directory block\n");
386 slotptr2
= (dir_slot
*) block
;
387 while (slotptr2
->id
> 0x01) {
390 /* Save the real directory entry */
391 realdent
= (dir_entry
*)slotptr2
+ 1;
392 while ((__u8
*)slotptr2
>= block
) {
393 slot2str(slotptr2
, l_name
, &idx
);
397 /* Save the real directory entry */
398 realdent
= (dir_entry
*)slotptr
;
403 if (slot2str(slotptr
, l_name
, &idx
)) break;
404 } while (!(slotptr
->id
& 0x40));
407 if (*l_name
== DELETED_FLAG
) *l_name
= '\0';
408 else if (*l_name
== aRING
) *l_name
= 'å';
411 /* Return the real directory entry */
412 memcpy(retdent
, realdent
, sizeof(dir_entry
));
418 /* Calculate short name checksum */
420 mkcksum(const char *str
)
425 for (i
= 0; i
< 11; i
++) {
426 ret
= (((ret
&1)<<7)|((ret
&0xfe)>>1)) + str
[i
];
435 * Get the directory entry associated with 'filename' from the directory
436 * starting at 'startsect'
438 static dir_entry
*get_dentfromdir (fsdata
* mydata
, int startsect
,
439 char *filename
, dir_entry
* retdent
,
442 __u16 prevcksum
= 0xffff;
443 __u8 block
[MAX_CLUSTSIZE
];
444 __u32 curclust
= START (retdent
);
445 int files
= 0, dirs
= 0;
447 FAT_DPRINT ("get_dentfromdir: %s\n", filename
);
452 if (get_cluster (mydata
, curclust
, block
,
453 mydata
->clust_size
* SECTOR_SIZE
) != 0) {
454 FAT_DPRINT ("Error: reading directory block\n");
457 dentptr
= (dir_entry
*) block
;
458 for (i
= 0; i
< DIRENTSPERCLUST
; i
++) {
459 char s_name
[14], l_name
[256];
462 if ((dentptr
->attr
& ATTR_VOLUME
)) {
463 #ifdef CONFIG_SUPPORT_VFAT
464 if ((dentptr
->attr
& ATTR_VFAT
) &&
465 (dentptr
->name
[0] & 0x40)) {
466 prevcksum
= ((dir_slot
*) dentptr
)
468 get_vfatname (mydata
, curclust
, block
,
471 int isdir
= (dentptr
->attr
& ATTR_DIR
);
481 if (l_name
[0] != 0) {
488 printf (" %8ld %s%c\n",
489 (long) FAT2CPU32 (dentptr
->size
),
492 printf (" %s%c\n", l_name
, dirc
);
498 FAT_DPRINT ("vfatname: |%s|\n", l_name
);
502 /* Volume label or VFAT entry */
507 if (dentptr
->name
[0] == 0) {
509 printf ("\n%d file(s), %d dir(s)\n\n", files
, dirs
);
511 FAT_DPRINT ("Dentname == NULL - %d\n", i
);
514 #ifdef CONFIG_SUPPORT_VFAT
515 if (dols
&& mkcksum (dentptr
->name
) == prevcksum
) {
520 get_name (dentptr
, s_name
);
522 int isdir
= (dentptr
->attr
& ATTR_DIR
);
532 if (s_name
[0] != 0) {
539 printf (" %8ld %s%c\n",
540 (long) FAT2CPU32 (dentptr
->size
), s_name
,
543 printf (" %s%c\n", s_name
, dirc
);
549 if (strcmp (filename
, s_name
) && strcmp (filename
, l_name
)) {
550 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name
, l_name
);
554 memcpy (retdent
, dentptr
, sizeof (dir_entry
));
556 FAT_DPRINT ("DentName: %s", s_name
);
557 FAT_DPRINT (", start: 0x%x", START (dentptr
));
558 FAT_DPRINT (", size: 0x%x %s\n",
559 FAT2CPU32 (dentptr
->size
),
560 (dentptr
->attr
& ATTR_DIR
) ? "(DIR)" : "");
564 curclust
= get_fatent (mydata
, curclust
);
565 if (curclust
<= 0x0001 || curclust
>= 0xfff0) {
566 FAT_DPRINT ("curclust: 0x%x\n", curclust
);
567 FAT_ERROR ("Invalid FAT entry\n");
577 * Read boot sector and volume info from a FAT filesystem
580 read_bootsectandvi(boot_sector
*bs
, volume_info
*volinfo
, int *fatsize
)
582 __u8 block
[FS_BLOCK_SIZE
];
583 volume_info
*vistart
;
585 if (disk_read(0, 1, block
) < 0) {
586 FAT_DPRINT("Error: reading block\n");
590 memcpy(bs
, block
, sizeof(boot_sector
));
591 bs
->reserved
= FAT2CPU16(bs
->reserved
);
592 bs
->fat_length
= FAT2CPU16(bs
->fat_length
);
593 bs
->secs_track
= FAT2CPU16(bs
->secs_track
);
594 bs
->heads
= FAT2CPU16(bs
->heads
);
596 bs
->hidden
= FAT2CPU32(bs
->hidden
);
598 bs
->total_sect
= FAT2CPU32(bs
->total_sect
);
601 if (bs
->fat_length
== 0) {
603 bs
->fat32_length
= FAT2CPU32(bs
->fat32_length
);
604 bs
->flags
= FAT2CPU16(bs
->flags
);
605 bs
->root_cluster
= FAT2CPU32(bs
->root_cluster
);
606 bs
->info_sector
= FAT2CPU16(bs
->info_sector
);
607 bs
->backup_boot
= FAT2CPU16(bs
->backup_boot
);
608 vistart
= (volume_info
*) (block
+ sizeof(boot_sector
));
611 vistart
= (volume_info
*) &(bs
->fat32_length
);
614 memcpy(volinfo
, vistart
, sizeof(volume_info
));
616 /* Terminate fs_type string. Writing past the end of vistart
617 is ok - it's just the buffer. */
618 vistart
->fs_type
[8] = '\0';
620 if (*fatsize
== 32) {
621 if (compare_sign(FAT32_SIGN
, vistart
->fs_type
) == 0) {
625 if (compare_sign(FAT12_SIGN
, vistart
->fs_type
) == 0) {
629 if (compare_sign(FAT16_SIGN
, vistart
->fs_type
) == 0) {
635 FAT_DPRINT("Error: broken fs_type sign\n");
641 do_fat_read (const char *filename
, void *buffer
, unsigned long maxsize
,
644 __u8 block
[FS_BLOCK_SIZE
]; /* Block buffer */
645 char fnamecopy
[2048];
649 fsdata
*mydata
= &datablock
;
651 __u16 prevcksum
= 0xffff;
653 int rootdir_size
, cursect
;
655 int files
= 0, dirs
= 0;
659 if (read_bootsectandvi (&bs
, &volinfo
, &mydata
->fatsize
)) {
660 FAT_DPRINT ("Error: reading boot sector\n");
663 if (mydata
->fatsize
== 32) {
664 mydata
->fatlength
= bs
.fat32_length
;
666 mydata
->fatlength
= bs
.fat_length
;
668 mydata
->fat_sect
= bs
.reserved
;
669 cursect
= mydata
->rootdir_sect
670 = mydata
->fat_sect
+ mydata
->fatlength
* bs
.fats
;
671 mydata
->clust_size
= bs
.cluster_size
;
672 if (mydata
->fatsize
== 32) {
673 rootdir_size
= mydata
->clust_size
;
674 mydata
->data_begin
= mydata
->rootdir_sect
/* + rootdir_size */
675 - (mydata
->clust_size
* 2);
677 rootdir_size
= ((bs
.dir_entries
[1] * (int) 256 + bs
.dir_entries
[0])
678 * sizeof (dir_entry
)) / SECTOR_SIZE
;
679 mydata
->data_begin
= mydata
->rootdir_sect
+ rootdir_size
680 - (mydata
->clust_size
* 2);
682 mydata
->fatbufnum
= -1;
684 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata
->fatsize
,
686 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
687 "Data begins at: %d\n",
688 mydata
->rootdir_sect
, mydata
->rootdir_sect
* SECTOR_SIZE
,
689 rootdir_size
, mydata
->data_begin
);
690 FAT_DPRINT ("Cluster size: %d\n", mydata
->clust_size
);
692 /* "cwd" is always the root... */
693 while (ISDIRDELIM (*filename
))
695 /* Make a copy of the filename and convert it to lowercase */
696 strcpy (fnamecopy
, filename
);
697 downcase (fnamecopy
);
698 if (*fnamecopy
== '\0') {
702 } else if ((idx
= dirdelim (fnamecopy
)) >= 0) {
704 fnamecopy
[idx
] = '\0';
705 subname
= fnamecopy
+ idx
+ 1;
706 /* Handle multiple delimiters */
707 while (ISDIRDELIM (*subname
))
716 if (disk_read (cursect
, 1, block
) < 0) {
717 FAT_DPRINT ("Error: reading rootdir block\n");
720 dentptr
= (dir_entry
*) block
;
721 for (i
= 0; i
< DIRENTSPERBLOCK
; i
++) {
722 char s_name
[14], l_name
[256];
725 if ((dentptr
->attr
& ATTR_VOLUME
)) {
726 #ifdef CONFIG_SUPPORT_VFAT
727 if ((dentptr
->attr
& ATTR_VFAT
) &&
728 (dentptr
->name
[0] & 0x40)) {
729 prevcksum
= ((dir_slot
*) dentptr
)->alias_checksum
;
730 get_vfatname (mydata
, 0, block
, dentptr
, l_name
);
731 if (dols
== LS_ROOT
) {
732 int isdir
= (dentptr
->attr
& ATTR_DIR
);
742 if (l_name
[0] != 0) {
749 printf (" %8ld %s%c\n",
750 (long) FAT2CPU32 (dentptr
->size
),
753 printf (" %s%c\n", l_name
, dirc
);
759 FAT_DPRINT ("Rootvfatname: |%s|\n", l_name
);
763 /* Volume label or VFAT entry */
767 } else if (dentptr
->name
[0] == 0) {
768 FAT_DPRINT ("RootDentname == NULL - %d\n", i
);
769 if (dols
== LS_ROOT
) {
770 printf ("\n%d file(s), %d dir(s)\n\n", files
, dirs
);
775 #ifdef CONFIG_SUPPORT_VFAT
776 else if (dols
== LS_ROOT
777 && mkcksum (dentptr
->name
) == prevcksum
) {
782 get_name (dentptr
, s_name
);
783 if (dols
== LS_ROOT
) {
784 int isdir
= (dentptr
->attr
& ATTR_DIR
);
794 if (s_name
[0] != 0) {
801 printf (" %8ld %s%c\n",
802 (long) FAT2CPU32 (dentptr
->size
), s_name
,
805 printf (" %s%c\n", s_name
, dirc
);
811 if (strcmp (fnamecopy
, s_name
) && strcmp (fnamecopy
, l_name
)) {
812 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name
, l_name
);
816 if (isdir
&& !(dentptr
->attr
& ATTR_DIR
))
819 FAT_DPRINT ("RootName: %s", s_name
);
820 FAT_DPRINT (", start: 0x%x", START (dentptr
));
821 FAT_DPRINT (", size: 0x%x %s\n",
822 FAT2CPU32 (dentptr
->size
), isdir
? "(DIR)" : "");
824 goto rootdir_done
; /* We got a match */
832 int startsect
= mydata
->data_begin
833 + START (dentptr
) * mydata
->clust_size
;
835 char *nextname
= NULL
;
840 idx
= dirdelim (subname
);
843 nextname
= subname
+ idx
+ 1;
844 /* Handle multiple delimiters */
845 while (ISDIRDELIM (*nextname
))
847 if (dols
&& *nextname
== '\0')
850 if (dols
&& firsttime
) {
857 if (get_dentfromdir (mydata
, startsect
, subname
, dentptr
,
858 isdir
? 0 : dols
) == NULL
) {
865 if (!(dentptr
->attr
& ATTR_DIR
))
870 ret
= get_contents (mydata
, dentptr
, buffer
, maxsize
);
871 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr
->size
), ret
);
878 file_fat_detectfs(void)
884 return read_bootsectandvi(&bs
, &volinfo
, &fatsize
);
889 file_fat_ls(const char *dir
)
891 return do_fat_read(dir
, NULL
, 0, LS_YES
);
896 file_fat_read(const char *filename
, void *buffer
, unsigned long maxsize
)
898 return do_fat_read(filename
, buffer
, maxsize
, LS_NO
);
901 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */