diff options
-rw-r--r-- | firmware/drivers/fat.c | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index 0a9f524c73..a2677124ff 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c | |||
@@ -238,6 +238,7 @@ static int bpb_is_sane(void); | |||
238 | static void *cache_fat_sector(int secnum); | 238 | static void *cache_fat_sector(int secnum); |
239 | static int create_dos_name(unsigned char *name, unsigned char *newname); | 239 | static int create_dos_name(unsigned char *name, unsigned char *newname); |
240 | static unsigned int find_free_cluster(unsigned int start); | 240 | static unsigned int find_free_cluster(unsigned int start); |
241 | static int transfer( unsigned int start, int count, char* buf, bool write ); | ||
241 | 242 | ||
242 | #define FAT_CACHE_SIZE 0x20 | 243 | #define FAT_CACHE_SIZE 0x20 |
243 | #define FAT_CACHE_MASK (FAT_CACHE_SIZE-1) | 244 | #define FAT_CACHE_MASK (FAT_CACHE_SIZE-1) |
@@ -1334,6 +1335,9 @@ int fat_create_dir(char* name, | |||
1334 | struct fat_dir* newdir, | 1335 | struct fat_dir* newdir, |
1335 | struct fat_dir* dir) | 1336 | struct fat_dir* dir) |
1336 | { | 1337 | { |
1338 | unsigned char buf[SECTOR_SIZE]; | ||
1339 | int i; | ||
1340 | int sector; | ||
1337 | int rc; | 1341 | int rc; |
1338 | struct fat_file dummyfile; | 1342 | struct fat_file dummyfile; |
1339 | 1343 | ||
@@ -1347,20 +1351,33 @@ int fat_create_dir(char* name, | |||
1347 | if (rc < 0) | 1351 | if (rc < 0) |
1348 | return rc * 10 - 1; | 1352 | return rc * 10 - 1; |
1349 | 1353 | ||
1350 | /* Then add the "." entry */ | 1354 | /* Allocate a new cluster for the directory */ |
1351 | newdir->file.firstcluster = find_free_cluster(fat_bpb.fsinfo.nextfree); | 1355 | newdir->file.firstcluster = find_free_cluster(fat_bpb.fsinfo.nextfree); |
1356 | if(newdir->file.firstcluster == 0) | ||
1357 | return -1; | ||
1358 | |||
1352 | update_fat_entry(newdir->file.firstcluster, FAT_EOF_MARK); | 1359 | update_fat_entry(newdir->file.firstcluster, FAT_EOF_MARK); |
1360 | |||
1361 | /* Clear the entire cluster */ | ||
1362 | memset(buf, 0, sizeof buf); | ||
1363 | sector = cluster2sec(newdir->file.firstcluster); | ||
1364 | for(i = 0;i < (int)fat_bpb.bpb_secperclus;i++) { | ||
1365 | rc = transfer( sector + fat_bpb.startsector + i, 1, buf, true ); | ||
1366 | if (rc < 0) | ||
1367 | return rc * 10 - 2; | ||
1368 | } | ||
1353 | 1369 | ||
1370 | /* Then add the "." entry */ | ||
1354 | rc = add_dir_entry(newdir, &dummyfile, ".", true, true); | 1371 | rc = add_dir_entry(newdir, &dummyfile, ".", true, true); |
1355 | if (rc < 0) | 1372 | if (rc < 0) |
1356 | return rc * 10 - 2; | 1373 | return rc * 10 - 3; |
1357 | dummyfile.firstcluster = newdir->file.firstcluster; | 1374 | dummyfile.firstcluster = newdir->file.firstcluster; |
1358 | update_short_entry(&dummyfile, 0, FAT_ATTR_DIRECTORY); | 1375 | update_short_entry(&dummyfile, 0, FAT_ATTR_DIRECTORY); |
1359 | 1376 | ||
1360 | /* and the ".." entry */ | 1377 | /* and the ".." entry */ |
1361 | rc = add_dir_entry(newdir, &dummyfile, "..", true, true); | 1378 | rc = add_dir_entry(newdir, &dummyfile, "..", true, true); |
1362 | if (rc < 0) | 1379 | if (rc < 0) |
1363 | return rc * 10 - 3; | 1380 | return rc * 10 - 4; |
1364 | 1381 | ||
1365 | /* The root cluster is cluster 0 in the ".." entry */ | 1382 | /* The root cluster is cluster 0 in the ".." entry */ |
1366 | if(dir->file.firstcluster == fat_bpb.bpb_rootclus) | 1383 | if(dir->file.firstcluster == fat_bpb.bpb_rootclus) |
@@ -1374,7 +1391,7 @@ int fat_create_dir(char* name, | |||
1374 | 1391 | ||
1375 | rc = flush_fat(); | 1392 | rc = flush_fat(); |
1376 | if (rc < 0) | 1393 | if (rc < 0) |
1377 | return rc * 10 - 4; | 1394 | return rc * 10 - 5; |
1378 | 1395 | ||
1379 | return rc; | 1396 | return rc; |
1380 | } | 1397 | } |