summaryrefslogtreecommitdiff
path: root/fs/fat/fat.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/fat/fat.c')
-rw-r--r--fs/fat/fat.c67
1 files changed, 49 insertions, 18 deletions
diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index 4b722fc5ca..b08949d370 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -260,7 +260,7 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
- printf("FAT: Misaligned buffer address (%p)\n", buffer);
+ debug("FAT: Misaligned buffer address (%p)\n", buffer);
while (size >= mydata->sect_size) {
ret = disk_read(startsect++, 1, tmpbuf);
@@ -465,15 +465,6 @@ static __u8 mkcksum(const char name[8], const char ext[3])
}
/*
- * TODO these should go away once fat_write is reworked to use the
- * directory iterator
- */
-__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
- __aligned(ARCH_DMA_MINALIGN);
-__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
- __aligned(ARCH_DMA_MINALIGN);
-
-/*
* Read boot sector and volume info from a FAT filesystem
*/
static int
@@ -558,10 +549,17 @@ static int get_fs_info(fsdata *mydata)
if (mydata->fatsize == 32) {
mydata->fatlength = bs.fat32_length;
+ mydata->total_sect = bs.total_sect;
} else {
mydata->fatlength = bs.fat_length;
+ mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
+ if (!mydata->total_sect)
+ mydata->total_sect = bs.total_sect;
}
+ if (!mydata->total_sect) /* unlikely */
+ mydata->total_sect = (u32)cur_part_info.size;
+ mydata->fats = bs.fats;
mydata->fat_sect = bs.reserved;
mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
@@ -633,7 +631,9 @@ static int get_fs_info(fsdata *mydata)
typedef struct {
fsdata *fsdata; /* filesystem parameters */
+ unsigned start_clust; /* first cluster */
unsigned clust; /* current cluster */
+ unsigned next_clust; /* next cluster if remaining == 0 */
int last_cluster; /* set once we've read last cluster */
int is_root; /* is iterator at root directory */
int remaining; /* remaining dent's in current cluster */
@@ -664,7 +664,9 @@ static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
return -ENXIO;
itr->fsdata = fsdata;
+ itr->start_clust = 0;
itr->clust = fsdata->root_cluster;
+ itr->next_clust = fsdata->root_cluster;
itr->dent = NULL;
itr->remaining = 0;
itr->last_cluster = 0;
@@ -698,11 +700,14 @@ static void fat_itr_child(fat_itr *itr, fat_itr *parent)
assert(fat_itr_isdir(parent));
itr->fsdata = parent->fsdata;
+ itr->start_clust = clustnum;
if (clustnum > 0) {
itr->clust = clustnum;
+ itr->next_clust = clustnum;
itr->is_root = 0;
} else {
itr->clust = parent->fsdata->root_cluster;
+ itr->next_clust = parent->fsdata->root_cluster;
itr->is_root = 1;
}
itr->dent = NULL;
@@ -720,7 +725,7 @@ static void *next_cluster(fat_itr *itr)
if (itr->last_cluster)
return NULL;
- sect = clust_to_sect(itr->fsdata, itr->clust);
+ sect = clust_to_sect(itr->fsdata, itr->next_clust);
debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
sect, itr->fsdata->clust_size, DIRENTSPERBLOCK);
@@ -741,18 +746,19 @@ static void *next_cluster(fat_itr *itr)
return NULL;
}
+ itr->clust = itr->next_clust;
if (itr->is_root && itr->fsdata->fatsize != 32) {
- itr->clust++;
- sect = clust_to_sect(itr->fsdata, itr->clust);
+ itr->next_clust++;
+ sect = clust_to_sect(itr->fsdata, itr->next_clust);
if (sect - itr->fsdata->rootdir_sect >=
itr->fsdata->rootdir_size) {
- debug("cursect: 0x%x\n", itr->clust);
+ debug("nextclust: 0x%x\n", itr->next_clust);
itr->last_cluster = 1;
}
} else {
- itr->clust = get_fatent(itr->fsdata, itr->clust);
- if (CHECK_CLUST(itr->clust, itr->fsdata->fatsize)) {
- debug("cursect: 0x%x\n", itr->clust);
+ itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
+ if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
+ debug("nextclust: 0x%x\n", itr->next_clust);
itr->last_cluster = 1;
}
}
@@ -768,8 +774,11 @@ static dir_entry *next_dent(fat_itr *itr)
itr->fsdata->clust_size;
/* have we reached the last cluster? */
- if (!dent)
+ if (!dent) {
+ /* a sign for no more entries left */
+ itr->dent = NULL;
return NULL;
+ }
itr->remaining = nbytes / sizeof(dir_entry) - 1;
itr->dent = dent;
@@ -924,6 +933,28 @@ static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
while (next[0] && !ISDIRDELIM(next[0]))
next++;
+ if (itr->is_root) {
+ /* root dir doesn't have "." nor ".." */
+ if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
+ (((next - path) == 2) && !strncmp(path, "..", 2))) {
+ /* point back to itself */
+ itr->clust = itr->fsdata->root_cluster;
+ itr->next_clust = itr->fsdata->root_cluster;
+ itr->dent = NULL;
+ itr->remaining = 0;
+ itr->last_cluster = 0;
+
+ if (next[0] == 0) {
+ if (type & TYPE_DIR)
+ return 0;
+ else
+ return -ENOENT;
+ }
+
+ return fat_itr_resolve(itr, next, type);
+ }
+ }
+
while (fat_itr_next(itr)) {
int match = 0;
unsigned n = max(strlen(itr->name), (size_t)(next - path));