#include <config.h>
#include <fat.h>
#include <asm/byteorder.h>
-
-#if (CONFIG_COMMANDS & CFG_CMD_FAT)
+#include <part.h>
/*
* Convert a string to lowercase.
}
}
-int (*dev_block_read)(int device, __u32 blknr, __u32 blkcnt, __u8 *buffer) = 0;
+static block_dev_desc_t *cur_dev = NULL;
+static unsigned long part_offset = 0;
+static int cur_part = 1;
+
+#define DOS_PART_TBL_OFFSET 0x1be
+#define DOS_PART_MAGIC_OFFSET 0x1fe
+#define DOS_FS_TYPE_OFFSET 0x36
int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
{
- /* FIXME we need to determine the start block of the
- * partition where the DOS FS resides
- */
- startblock += 32;
-
- if (dev_block_read) {
- return dev_block_read (0, startblock, getsize, bufptr);
+ startblock += part_offset;
+ 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 -1;
}
int
-fat_register_read (int (*block_read)(int, __u32, __u32, __u8 *))
+fat_register_device(block_dev_desc_t *dev_desc, int part_no)
{
- dev_block_read = block_read;
+ unsigned char buffer[SECTOR_SIZE];
+ disk_partition_t info;
+
+ if (!dev_desc->block_read)
+ return -1;
+ 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);
+ return -1;
+ }
+ if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
+ buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
+ /* no signature found */
+ return -1;
+ }
+#if (defined(CONFIG_CMD_IDE) || \
+ defined(CONFIG_CMD_MG_DISK) || \
+ defined(CONFIG_CMD_SATA) || \
+ defined(CONFIG_CMD_SCSI) || \
+ defined(CONFIG_CMD_USB) || \
+ defined(CONFIG_MMC) || \
+ 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;
+ cur_part = 1;
+ }
+#endif
return 0;
}
return -1;
}
-
-/*
- * Match volume_info fs_type strings.
- * Return 0 on match, -1 otherwise.
- */
-static int
-compare_sign(char *str1, char *str2)
-{
- char *end = str1+SIGNLEN;
-
- while (str1 != end) {
- if (*str1 != *str2) {
- return -1;
- }
- str1++;
- str2++;
- }
-
- return 0;
-}
-
-
/*
* Extract zero terminated short name from a directory entry.
*/
if (*s_name == DELETED_FLAG)
*s_name = '\0';
else if (*s_name == aRING)
- *s_name = 'å';
+ *s_name = DELETED_FLAG;
downcase (s_name);
}
}
FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
- while (size > 0) {
- if (size >= FS_BLOCK_SIZE) {
- if (disk_read(startsect + idx, 1, buffer) < 0) {
- FAT_DPRINT("Error reading data\n");
- return -1;
- }
- } else {
- __u8 tmpbuf[FS_BLOCK_SIZE];
- if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
- FAT_DPRINT("Error reading data\n");
- return -1;
- }
- memcpy(buffer, tmpbuf, size);
-
- return 0;
+ if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
+ FAT_DPRINT("Error reading data\n");
+ return -1;
+ }
+ if(size % FS_BLOCK_SIZE) {
+ __u8 tmpbuf[FS_BLOCK_SIZE];
+ idx= size/FS_BLOCK_SIZE;
+ if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
+ FAT_DPRINT("Error reading data\n");
+ return -1;
}
- buffer += FS_BLOCK_SIZE;
- size -= FS_BLOCK_SIZE;
- idx++;
+ buffer += idx*FS_BLOCK_SIZE;
+
+ memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
+ return 0;
}
return 0;
unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
__u32 curclust = START(dentptr);
+ __u32 endclust, newclust;
+ unsigned long actsize;
FAT_DPRINT("Filesize: %ld bytes\n", filesize);
FAT_DPRINT("Reading: %ld bytes\n", filesize);
+ actsize=bytesperclust;
+ endclust=curclust;
do {
- int getsize = (filesize > bytesperclust) ? bytesperclust :
- filesize;
-
- if (get_cluster(mydata, curclust, buffer, getsize) != 0) {
+ /* search for consecutive clusters */
+ while(actsize < filesize) {
+ newclust = get_fatent(mydata, endclust);
+ if((newclust -1)!=endclust)
+ goto getit;
+ if (CHECK_CLUST(newclust, mydata->fatsize)) {
+ FAT_DPRINT("curclust: 0x%x\n", newclust);
+ FAT_DPRINT("Invalid FAT entry\n");
+ return gotsize;
+ }
+ endclust=newclust;
+ actsize+= bytesperclust;
+ }
+ /* actsize >= file size */
+ actsize -= bytesperclust;
+ /* get remaining clusters */
+ if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
FAT_ERROR("Error reading cluster\n");
return -1;
}
- gotsize += getsize;
- filesize -= getsize;
- if (filesize <= 0) return gotsize;
- buffer += getsize;
-
- curclust = get_fatent(mydata, curclust);
- if (curclust <= 0x0001 || curclust >= 0xfff0) {
+ /* get remaining bytes */
+ gotsize += (int)actsize;
+ filesize -= actsize;
+ buffer += actsize;
+ actsize= filesize;
+ if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
+ FAT_ERROR("Error reading cluster\n");
+ return -1;
+ }
+ gotsize+=actsize;
+ return gotsize;
+getit:
+ if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
+ FAT_ERROR("Error reading cluster\n");
+ return -1;
+ }
+ gotsize += (int)actsize;
+ filesize -= actsize;
+ buffer += actsize;
+ curclust = get_fatent(mydata, endclust);
+ if (CHECK_CLUST(curclust, mydata->fatsize)) {
FAT_DPRINT("curclust: 0x%x\n", curclust);
FAT_ERROR("Invalid FAT entry\n");
return gotsize;
}
+ actsize=bytesperclust;
+ endclust=curclust;
} while (1);
}
* into 'retdent'
* Return 0 on success, -1 otherwise.
*/
+__attribute__ ((__aligned__(__alignof__(dir_entry))))
+__u8 get_vfatname_block[MAX_CLUSTSIZE];
static int
get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
dir_entry *retdent, char *l_name)
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--;
curclust = get_fatent(mydata, curclust);
- if (curclust <= 0x0001 || curclust >= 0xfff0) {
+ if (CHECK_CLUST(curclust, mydata->fatsize)) {
FAT_DPRINT("curclust: 0x%x\n", curclust);
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--;
}
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';
- else if (*l_name == aRING) *l_name = 'å';
+ else if (*l_name == aRING) *l_name = DELETED_FLAG;
downcase(l_name);
/* Return the real directory entry */
* Get the directory entry associated with 'filename' from the directory
* starting at 'startsect'
*/
+__attribute__ ((__aligned__(__alignof__(dir_entry))))
+__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;
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);
return retdent;
}
curclust = get_fatent (mydata, curclust);
- if (curclust <= 0x0001 || curclust >= 0xfff0) {
+ if (CHECK_CLUST(curclust, mydata->fatsize)) {
FAT_DPRINT ("curclust: 0x%x\n", curclust);
FAT_ERROR ("Invalid FAT entry\n");
return NULL;
}
memcpy(volinfo, vistart, sizeof(volume_info));
- /* Terminate fs_type string. Writing past the end of vistart
- is ok - it's just the buffer. */
- vistart->fs_type[8] = '\0';
-
if (*fatsize == 32) {
- if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
+ if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) {
return 0;
}
} else {
- if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
+ if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
*fatsize = 12;
return 0;
}
- if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
+ if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
*fatsize = 16;
return 0;
}
return -1;
}
-
-static long
+__attribute__ ((__aligned__(__alignof__(dir_entry))))
+__u8 do_fat_read_block[MAX_CLUSTSIZE];
+long
do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
int dols)
{
- __u8 block[FS_BLOCK_SIZE]; /* Block buffer */
+#if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
+ static
+#endif
char fnamecopy[2048];
boot_sector bs;
volume_info volinfo;
while (1) {
int i;
- if (disk_read (cursect, 1, 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];
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;
int doit = 0;
if (isdir) {
- dirs++;
dirc = '/';
- doit = 1;
+ if (s_name[0] != 0) {
+ dirs++;
+ doit = 1;
+ }
} else {
dirc = ' ';
if (s_name[0] != 0) {
boot_sector bs;
volume_info volinfo;
int fatsize;
+ char vol_label[12];
- return read_bootsectandvi(&bs, &volinfo, &fatsize);
+ if(cur_dev==NULL) {
+ printf("No current device\n");
+ return 1;
+ }
+#if defined(CONFIG_CMD_IDE) || \
+ defined(CONFIG_CMD_MG_DISK) || \
+ defined(CONFIG_CMD_SATA) || \
+ defined(CONFIG_CMD_SCSI) || \
+ defined(CONFIG_CMD_USB) || \
+ defined(CONFIG_MMC)
+ printf("Interface: ");
+ switch(cur_dev->if_type) {
+ case IF_TYPE_IDE : printf("IDE"); break;
+ case IF_TYPE_SATA : printf("SATA"); break;
+ case IF_TYPE_SCSI : printf("SCSI"); break;
+ case IF_TYPE_ATAPI : printf("ATAPI"); break;
+ case IF_TYPE_USB : printf("USB"); break;
+ case IF_TYPE_DOC : printf("DOC"); break;
+ case IF_TYPE_MMC : printf("MMC"); break;
+ default : printf("Unknown");
+ }
+ printf("\n Device %d: ",cur_dev->dev);
+ dev_print(cur_dev);
+#endif
+ if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
+ printf("\nNo valid FAT fs found\n");
+ return 1;
+ }
+ 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);
+ return 0;
}
long
file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
{
+ printf("reading %s\n",filename);
return do_fat_read(filename, buffer, maxsize, LS_NO);
}
-
-#endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */