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
7071int 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,
702702static int
703703read_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 }
10401061rootdir_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
0 commit comments