Skip to content

Commit ac49777

Browse files
Sergei Shtylyovwdenx
authored andcommitted
fat: fix crash with big sector size
Apple iPod nanos have sector sizes of 2 or 4 KiB, which crashes U-Boot when it tries to read the boot sector into 512-byte buffer situated on stack. Make the FAT code indifferent to the sector size. Signed-off-by: Sergei Shtylyov <sshtylyov@mvista.com>
1 parent 1e02c20 commit ac49777

2 files changed

Lines changed: 67 additions & 50 deletions

File tree

fs/fat/fat.c

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <common.h>
2929
#include <config.h>
30+
#include <exports.h>
3031
#include <fat.h>
3132
#include <asm/byteorder.h>
3233
#include <part.h>
@@ -69,8 +70,7 @@ static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
6970

7071
int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
7172
{
72-
unsigned char buffer[SECTOR_SIZE];
73-
73+
unsigned char buffer[dev_desc->blksz];
7474
disk_partition_t info;
7575

7676
if (!dev_desc->block_read)
@@ -209,12 +209,12 @@ static __u32 get_fatent (fsdata *mydata, __u32 entry)
209209

210210
/* Read a new block of FAT entries into the cache. */
211211
if (bufnum != mydata->fatbufnum) {
212-
__u32 getsize = FATBUFSIZE / FS_BLOCK_SIZE;
212+
__u32 getsize = FATBUFSIZE / mydata->sect_size;
213213
__u8 *bufptr = mydata->fatbuf;
214214
__u32 fatlength = mydata->fatlength;
215215
__u32 startblock = bufnum * FATBUFBLOCKS;
216216

217-
fatlength *= SECTOR_SIZE; /* We want it in bytes now */
217+
fatlength *= mydata->sect_size; /* We want it in bytes now */
218218
startblock += mydata->fat_sect; /* Offset from start of disk */
219219

220220
if (getsize > fatlength)
@@ -291,21 +291,21 @@ get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
291291

292292
debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
293293

294-
if (disk_read(startsect, size / FS_BLOCK_SIZE, buffer) < 0) {
294+
if (disk_read(startsect, size / mydata->sect_size, buffer) < 0) {
295295
debug("Error reading data\n");
296296
return -1;
297297
}
298-
if (size % FS_BLOCK_SIZE) {
299-
__u8 tmpbuf[FS_BLOCK_SIZE];
298+
if (size % mydata->sect_size) {
299+
__u8 tmpbuf[mydata->sect_size];
300300

301-
idx = size / FS_BLOCK_SIZE;
301+
idx = size / mydata->sect_size;
302302
if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
303303
debug("Error reading data\n");
304304
return -1;
305305
}
306-
buffer += idx * FS_BLOCK_SIZE;
306+
buffer += idx * mydata->sect_size;
307307

308-
memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
308+
memcpy(buffer, tmpbuf, size % mydata->sect_size);
309309
return 0;
310310
}
311311

@@ -322,7 +322,7 @@ get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
322322
unsigned long maxsize)
323323
{
324324
unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
325-
unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
325+
unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
326326
__u32 curclust = START(dentptr);
327327
__u32 endclust, newclust;
328328
unsigned long actsize;
@@ -441,7 +441,7 @@ get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
441441
dir_slot *slotptr = (dir_slot *)retdent;
442442
__u8 *buflimit = cluster + ((curclust == 0) ?
443443
LINEAR_PREFETCH_SIZE :
444-
(mydata->clust_size * SECTOR_SIZE)
444+
(mydata->clust_size * mydata->sect_size)
445445
);
446446
__u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
447447
int idx = 0;
@@ -473,7 +473,7 @@ get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
473473
}
474474

475475
if (get_cluster(mydata, curclust, get_vfatname_block,
476-
mydata->clust_size * SECTOR_SIZE) != 0) {
476+
mydata->clust_size * mydata->sect_size) != 0) {
477477
debug("Error: reading directory block\n");
478478
return -1;
479479
}
@@ -555,7 +555,7 @@ static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
555555
int i;
556556

557557
if (get_cluster(mydata, curclust, get_dentfromdir_block,
558-
mydata->clust_size * SECTOR_SIZE) != 0) {
558+
mydata->clust_size * mydata->sect_size) != 0) {
559559
debug("Error: reading directory block\n");
560560
return NULL;
561561
}
@@ -702,13 +702,24 @@ static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
702702
static int
703703
read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
704704
{
705-
__u8 block[FS_BLOCK_SIZE];
706-
705+
__u8 *block;
707706
volume_info *vistart;
707+
int ret = 0;
708+
709+
if (cur_dev == NULL) {
710+
debug("Error: no device selected\n");
711+
return -1;
712+
}
713+
714+
block = malloc(cur_dev->blksz);
715+
if (block == NULL) {
716+
debug("Error: allocating block\n");
717+
return -1;
718+
}
708719

709720
if (disk_read (0, 1, block) < 0) {
710721
debug("Error: reading block\n");
711-
return -1;
722+
goto fail;
712723
}
713724

714725
memcpy(bs, block, sizeof(boot_sector));
@@ -736,20 +747,24 @@ read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
736747

737748
if (*fatsize == 32) {
738749
if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
739-
return 0;
750+
goto exit;
740751
} else {
741752
if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
742753
*fatsize = 12;
743-
return 0;
754+
goto exit;
744755
}
745756
if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
746757
*fatsize = 16;
747-
return 0;
758+
goto exit;
748759
}
749760
}
750761

751762
debug("Error: broken fs_type sign\n");
752-
return -1;
763+
fail:
764+
ret = -1;
765+
exit:
766+
free(block);
767+
return ret;
753768
}
754769

755770
__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
@@ -770,7 +785,7 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
770785
__u32 cursect;
771786
int idx, isdir = 0;
772787
int files = 0, dirs = 0;
773-
long ret = 0;
788+
long ret = -1;
774789
int firsttime;
775790
__u32 root_cluster;
776791
int rootdir_size = 0;
@@ -793,6 +808,7 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
793808
cursect = mydata->rootdir_sect
794809
= mydata->fat_sect + mydata->fatlength * bs.fats;
795810

811+
mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
796812
mydata->clust_size = bs.cluster_size;
797813

798814
if (mydata->fatsize == 32) {
@@ -802,13 +818,18 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
802818
rootdir_size = ((bs.dir_entries[1] * (int)256 +
803819
bs.dir_entries[0]) *
804820
sizeof(dir_entry)) /
805-
SECTOR_SIZE;
821+
mydata->sect_size;
806822
mydata->data_begin = mydata->rootdir_sect +
807823
rootdir_size -
808824
(mydata->clust_size * 2);
809825
}
810826

811827
mydata->fatbufnum = -1;
828+
mydata->fatbuf = malloc(FATBUFSIZE);
829+
if (mydata->fatbuf == NULL) {
830+
debug("Error: allocating memory\n");
831+
return -1;
832+
}
812833

813834
#ifdef CONFIG_SUPPORT_VFAT
814835
debug("VFAT Support enabled\n");
@@ -819,8 +840,9 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
819840
"Data begins at: %d\n",
820841
root_cluster,
821842
mydata->rootdir_sect,
822-
mydata->rootdir_sect * SECTOR_SIZE, mydata->data_begin);
823-
debug("Cluster size: %d\n", mydata->clust_size);
843+
mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
844+
debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
845+
mydata->clust_size);
824846

825847
/* "cwd" is always the root... */
826848
while (ISDIRDELIM(*filename))
@@ -832,7 +854,7 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
832854

833855
if (*fnamecopy == '\0') {
834856
if (!dols)
835-
return -1;
857+
goto exit;
836858

837859
dols = LS_ROOT;
838860
} else if ((idx = dirdelim(fnamecopy)) >= 0) {
@@ -857,10 +879,10 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
857879
if (disk_read(cursect,
858880
(mydata->fatsize == 32) ?
859881
(mydata->clust_size) :
860-
LINEAR_PREFETCH_SIZE / SECTOR_SIZE,
882+
LINEAR_PREFETCH_SIZE / mydata->sect_size,
861883
do_fat_read_block) < 0) {
862884
debug("Error: reading rootdir block\n");
863-
return -1;
885+
goto exit;
864886
}
865887

866888
dentptr = (dir_entry *) do_fat_read_block;
@@ -933,9 +955,9 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
933955
if (dols == LS_ROOT) {
934956
printf("\n%d file(s), %d dir(s)\n\n",
935957
files, dirs);
936-
return 0;
958+
ret = 0;
937959
}
938-
return -1;
960+
goto exit;
939961
}
940962
#ifdef CONFIG_SUPPORT_VFAT
941963
else if (dols == LS_ROOT &&
@@ -987,7 +1009,7 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
9871009
}
9881010

9891011
if (isdir && !(dentptr->attr & ATTR_DIR))
990-
return -1;
1012+
goto exit;
9911013

9921014
debug("RootName: %s", s_name);
9931015
debug(", start: 0x%x", START(dentptr));
@@ -1031,10 +1053,9 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
10311053
if (dols == LS_ROOT) {
10321054
printf("\n%d file(s), %d dir(s)\n\n",
10331055
files, dirs);
1034-
return 0;
1035-
} else {
1036-
return -1;
1056+
ret = 0;
10371057
}
1058+
goto exit;
10381059
}
10391060
}
10401061
rootdir_done:
@@ -1071,20 +1092,22 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
10711092
if (get_dentfromdir(mydata, startsect, subname, dentptr,
10721093
isdir ? 0 : dols) == NULL) {
10731094
if (dols && !isdir)
1074-
return 0;
1075-
return -1;
1095+
ret = 0;
1096+
goto exit;
10761097
}
10771098

10781099
if (idx >= 0) {
10791100
if (!(dentptr->attr & ATTR_DIR))
1080-
return -1;
1101+
goto exit;
10811102
subname = nextname;
10821103
}
10831104
}
10841105

10851106
ret = get_contents(mydata, dentptr, buffer, maxsize);
10861107
debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
10871108

1109+
exit:
1110+
free(mydata->fatbuf);
10881111
return ret;
10891112
}
10901113

include/fat.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,15 @@
3333
/* Maximum Long File Name length supported here is 128 UTF-16 code units */
3434
#define VFAT_MAXLEN_BYTES 256 /* Maximum LFN buffer in bytes */
3535
#define VFAT_MAXSEQ 9 /* Up to 9 of 13 2-byte UTF-16 entries */
36-
#define LINEAR_PREFETCH_SIZE (SECTOR_SIZE*2) /* Prefetch buffer size */
37-
38-
#define SECTOR_SIZE FS_BLOCK_SIZE
39-
40-
#define FS_BLOCK_SIZE 512
41-
42-
#if FS_BLOCK_SIZE != SECTOR_SIZE
43-
#error FS_BLOCK_SIZE != SECTOR_SIZE - This code needs to be fixed!
44-
#endif
36+
#define LINEAR_PREFETCH_SIZE (mydata->sect_size*2) /* Prefetch buffer size */
4537

4638
#define MAX_CLUSTSIZE 65536
47-
#define DIRENTSPERBLOCK (FS_BLOCK_SIZE/sizeof(dir_entry))
48-
#define DIRENTSPERCLUST ((mydata->clust_size*SECTOR_SIZE)/sizeof(dir_entry))
39+
#define DIRENTSPERBLOCK (mydata->sect_size / sizeof(dir_entry))
40+
#define DIRENTSPERCLUST ((mydata->clust_size * mydata->sect_size) / \
41+
sizeof(dir_entry))
4942

5043
#define FATBUFBLOCKS 6
51-
#define FATBUFSIZE (FS_BLOCK_SIZE*FATBUFBLOCKS)
44+
#define FATBUFSIZE (mydata->sect_size * FATBUFBLOCKS)
5245
#define FAT12BUFSIZE ((FATBUFSIZE*2)/3)
5346
#define FAT16BUFSIZE (FATBUFSIZE/2)
5447
#define FAT32BUFSIZE (FATBUFSIZE/4)
@@ -181,11 +174,12 @@ typedef struct dir_slot {
181174
* (see FAT32 accesses)
182175
*/
183176
typedef struct {
184-
__u8 fatbuf[FATBUFSIZE]; /* Current FAT buffer */
177+
__u8 *fatbuf; /* Current FAT buffer */
185178
int fatsize; /* Size of FAT in bits */
186179
__u16 fatlength; /* Length of FAT in sectors */
187180
__u16 fat_sect; /* Starting sector of the FAT */
188181
__u16 rootdir_sect; /* Start sector of root directory */
182+
__u16 sect_size; /* Size of sectors in bytes */
189183
__u16 clust_size; /* Size of clusters in sectors */
190184
short data_begin; /* The sector of the first cluster, can be negative */
191185
int fatbufnum; /* Used by get_fatent, init to -1 */

0 commit comments

Comments
 (0)