]> git.karo-electronics.de Git - karo-tx-uboot.git/blobdiff - fs/fat/fat.c
Merge branch 'testing' into working
[karo-tx-uboot.git] / fs / fat / fat.c
index 6a6c5be0168ed4043fbe7f78404b44f2456f39b3..c068a842e8f8849f9fef811361dcadc2b2e3916b 100644 (file)
@@ -31,7 +31,7 @@
 #include <asm/byteorder.h>
 #include <part.h>
 
-#if (CONFIG_COMMANDS & CFG_CMD_FAT)
+#if defined(CONFIG_CMD_FAT)
 
 /*
  * Convert a string to lowercase.
@@ -59,7 +59,8 @@ int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
        if (cur_dev == NULL)
                return -1;
        if (cur_dev->block_read) {
-               return cur_dev->block_read (cur_dev->dev, startblock, getsize, (unsigned long *)bufptr);
+               return cur_dev->block_read (cur_dev->dev
+                       , startblock, getsize, (unsigned long *)bufptr);
        }
        return -1;
 }
@@ -69,10 +70,11 @@ int
 fat_register_device(block_dev_desc_t *dev_desc, int part_no)
 {
        unsigned char buffer[SECTOR_SIZE];
+       disk_partition_t info;
 
        if (!dev_desc->block_read)
                return -1;
-       cur_dev=dev_desc;
+       cur_dev = dev_desc;
        /* check if we have a MBR (on floppies we have only a PBR) */
        if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
                printf ("** Can't read from device %d **\n", dev_desc->dev);
@@ -83,32 +85,45 @@ fat_register_device(block_dev_desc_t *dev_desc, int part_no)
                /* no signature found */
                return -1;
        }
-       if(!strncmp(&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
+       if(!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
                /* ok, we assume we are on a PBR only */
                cur_part = 1;
                part_offset=0;
        }
        else {
-#if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)
-               disk_partition_t info;
-               if(!get_partition_info(dev_desc, part_no, &info)) {
-                       part_offset = info.start;
-                       cur_part = part_no;
-               }
-               else {
-                       printf ("** Partition %d not valid on device %d **\n",part_no,dev_desc->dev);
-                       return -1;
-               }
+#if (defined(CONFIG_CMD_IDE) || \
+     defined(CONFIG_CMD_SCSI) || \
+     defined(CONFIG_CMD_USB) || \
+     (defined(CONFIG_MMC) && defined(CONFIG_LPC2292)) || \
+     defined(CONFIG_SYSTEMACE)          )
+       /* First we assume, there is a MBR */
+       if (!get_partition_info (dev_desc, part_no, &info)) {
+               part_offset = info.start;
+               cur_part = part_no;
+       } else if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3)) {
+               /* ok, we assume we are on a PBR only */
+               cur_part = 1;
+               part_offset = 0;
+       } else {
+               printf ("** Partition %d not valid on device %d **\n", part_no, dev_desc->dev);
+               return -1;
+       }
 #else
+       if(!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
+               /* ok, we assume we are on a PBR only */
+               cur_part = 1;
+               part_offset = 0;
+               info.start = part_offset;
+       } else {
                /* FIXME we need to determine the start block of the
                 * partition where the DOS FS resides. This can be done
                 * by using the get_partition_info routine. For this
                 * purpose the libpart must be included.
                 */
-               part_offset=32;
+               part_offset = 32;
                cur_part = 1;
-#endif
        }
+#endif
        return 0;
 }
 
@@ -424,6 +439,7 @@ slot2str(dir_slot *slotptr, char *l_name, int *idx)
  * into 'retdent'
  * Return 0 on success, -1 otherwise.
  */
+__u8    get_vfatname_block[MAX_CLUSTSIZE];
 static int
 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
             dir_entry *retdent, char *l_name)
@@ -431,18 +447,18 @@ get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
        dir_entry *realdent;
        dir_slot  *slotptr = (dir_slot*) retdent;
        __u8      *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
-       __u8       counter = slotptr->id & 0xf;
+       __u8       counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
        int idx = 0;
 
        while ((__u8*)slotptr < nextclust) {
                if (counter == 0) break;
-               if ((slotptr->id & 0x0f) != counter) return -1;
+               if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
+                       return -1;
                slotptr++;
                counter--;
        }
 
        if ((__u8*)slotptr >= nextclust) {
-               __u8     block[MAX_CLUSTSIZE];
                dir_slot *slotptr2;
 
                slotptr--;
@@ -452,18 +468,18 @@ get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
                        FAT_ERROR("Invalid FAT entry\n");
                        return -1;
                }
-               if (get_cluster(mydata, curclust, block,
+               if (get_cluster(mydata, curclust, get_vfatname_block,
                                mydata->clust_size * SECTOR_SIZE) != 0) {
                        FAT_DPRINT("Error: reading directory block\n");
                        return -1;
                }
-               slotptr2 = (dir_slot*) block;
+               slotptr2 = (dir_slot*) get_vfatname_block;
                while (slotptr2->id > 0x01) {
                        slotptr2++;
                }
                /* Save the real directory entry */
                realdent = (dir_entry*)slotptr2 + 1;
-               while ((__u8*)slotptr2 >= block) {
+               while ((__u8*)slotptr2 >= get_vfatname_block) {
                        slot2str(slotptr2, l_name, &idx);
                        slotptr2--;
                }
@@ -475,7 +491,7 @@ get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
        do {
                slotptr--;
                if (slot2str(slotptr, l_name, &idx)) break;
-       } while (!(slotptr->id & 0x40));
+       } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
 
        l_name[idx] = '\0';
        if (*l_name == DELETED_FLAG) *l_name = '\0';
@@ -509,12 +525,12 @@ mkcksum(const char *str)
  * Get the directory entry associated with 'filename' from the directory
  * starting at 'startsect'
  */
+__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
                                   char *filename, dir_entry * retdent,
                                   int dols)
 {
     __u16 prevcksum = 0xffff;
-    __u8 block[MAX_CLUSTSIZE];
     __u32 curclust = START (retdent);
     int files = 0, dirs = 0;
 
@@ -523,23 +539,27 @@ static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
        dir_entry *dentptr;
        int i;
 
-       if (get_cluster (mydata, curclust, block,
+       if (get_cluster (mydata, curclust, get_dentfromdir_block,
                 mydata->clust_size * SECTOR_SIZE) != 0) {
            FAT_DPRINT ("Error: reading directory block\n");
            return NULL;
        }
-       dentptr = (dir_entry *) block;
+       dentptr = (dir_entry *) get_dentfromdir_block;
        for (i = 0; i < DIRENTSPERCLUST; i++) {
            char s_name[14], l_name[256];
 
            l_name[0] = '\0';
+           if (dentptr->name[0] == DELETED_FLAG) {
+                   dentptr++;
+                   continue;
+           }
            if ((dentptr->attr & ATTR_VOLUME)) {
 #ifdef CONFIG_SUPPORT_VFAT
                if ((dentptr->attr & ATTR_VFAT) &&
-                   (dentptr->name[0] & 0x40)) {
+                   (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
                    prevcksum = ((dir_slot *) dentptr)
                            ->alias_checksum;
-                   get_vfatname (mydata, curclust, block,
+                   get_vfatname (mydata, curclust, get_dentfromdir_block,
                                  dentptr, l_name);
                    if (dols) {
                        int isdir = (dentptr->attr & ATTR_DIR);
@@ -711,11 +731,14 @@ read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
 }
 
 
-static long
+__u8 do_fat_read_block[MAX_CLUSTSIZE];  /* Block buffer */
+long
 do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
             int dols)
 {
-    __u8 block[MAX_CLUSTSIZE];  /* Block buffer */
+#if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
+    static
+#endif
     char fnamecopy[2048];
     boot_sector bs;
     volume_info volinfo;
@@ -787,11 +810,11 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
     while (1) {
        int i;
 
-       if (disk_read (cursect, mydata->clust_size, block) < 0) {
+       if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
            FAT_DPRINT ("Error: reading rootdir block\n");
            return -1;
        }
-       dentptr = (dir_entry *) block;
+       dentptr = (dir_entry *) do_fat_read_block;
        for (i = 0; i < DIRENTSPERBLOCK; i++) {
            char s_name[14], l_name[256];
 
@@ -799,9 +822,9 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
            if ((dentptr->attr & ATTR_VOLUME)) {
 #ifdef CONFIG_SUPPORT_VFAT
                if ((dentptr->attr & ATTR_VFAT) &&
-                   (dentptr->name[0] & 0x40)) {
+                   (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
                    prevcksum = ((dir_slot *) dentptr)->alias_checksum;
-                   get_vfatname (mydata, 0, block, dentptr, l_name);
+                   get_vfatname (mydata, 0, do_fat_read_block, dentptr, l_name);
                    if (dols == LS_ROOT) {
                        int isdir = (dentptr->attr & ATTR_DIR);
                        char dirc;
@@ -860,9 +883,11 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
                int doit = 0;
 
                if (isdir) {
-                   dirs++;
                    dirc = '/';
-                   doit = 1;
+                   if (s_name[0] != 0) {
+                       dirs++;
+                       doit = 1;
+                   }
                } else {
                    dirc = ' ';
                    if (s_name[0] != 0) {
@@ -960,7 +985,10 @@ file_fat_detectfs(void)
                printf("No current device\n");
                return 1;
        }
-#if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)
+#if defined(CONFIG_CMD_IDE) || \
+    defined(CONFIG_CMD_SCSI) || \
+    defined(CONFIG_CMD_USB) || \
+    (CONFIG_MMC)
        printf("Interface:  ");
        switch(cur_dev->if_type) {
                case IF_TYPE_IDE :      printf("IDE"); break;
@@ -981,7 +1009,8 @@ file_fat_detectfs(void)
        memcpy (vol_label, volinfo.volume_label, 11);
        vol_label[11] = '\0';
        volinfo.fs_type[5]='\0';
-       printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
+       printf("Partition %d: Filesystem: %s \"%s\"\n"
+                       ,cur_part,volinfo.fs_type,vol_label);
        return 0;
 }
 
@@ -1000,4 +1029,4 @@ file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
        return do_fat_read(filename, buffer, maxsize, LS_NO);
 }
 
-#endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */
+#endif