summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter D'Hoye <peter.dhoye@gmail.com>2007-10-28 23:44:49 +0000
committerPeter D'Hoye <peter.dhoye@gmail.com>2007-10-28 23:44:49 +0000
commitbf9615f4e14561af6ff5cae8aba104312144676d (patch)
tree12cc4f931b30fa75e66a303703b8a5d42778fc06
parent4feab10a0c0a314f49860a1e29b4c52aebb31682 (diff)
downloadrockbox-bf9615f4e14561af6ff5cae8aba104312144676d.tar.gz
rockbox-bf9615f4e14561af6ff5cae8aba104312144676d.zip
Code police, no functional changes (yet)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15352 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/drivers/fat.c150
1 files changed, 85 insertions, 65 deletions
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index cfd4767032..dd0d4d650e 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -88,7 +88,7 @@
88#define FAT_ATTR_LONG_NAME_MASK (FAT_ATTR_READ_ONLY | FAT_ATTR_HIDDEN | \ 88#define FAT_ATTR_LONG_NAME_MASK (FAT_ATTR_READ_ONLY | FAT_ATTR_HIDDEN | \
89 FAT_ATTR_SYSTEM | FAT_ATTR_VOLUME_ID | \ 89 FAT_ATTR_SYSTEM | FAT_ATTR_VOLUME_ID | \
90 FAT_ATTR_DIRECTORY | FAT_ATTR_ARCHIVE ) 90 FAT_ATTR_DIRECTORY | FAT_ATTR_ARCHIVE )
91 91
92/* NTRES flags */ 92/* NTRES flags */
93#define FAT_NTRES_LC_NAME 0x08 93#define FAT_NTRES_LC_NAME 0x08
94#define FAT_NTRES_LC_EXT 0x10 94#define FAT_NTRES_LC_EXT 0x10
@@ -176,11 +176,14 @@ static struct bpb fat_bpbs[NUM_VOLUMES]; /* mounted partition info */
176static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb)); 176static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb));
177static int flush_fat(IF_MV_NONVOID(struct bpb* fat_bpb)); 177static int flush_fat(IF_MV_NONVOID(struct bpb* fat_bpb));
178static int bpb_is_sane(IF_MV_NONVOID(struct bpb* fat_bpb)); 178static int bpb_is_sane(IF_MV_NONVOID(struct bpb* fat_bpb));
179static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,) long secnum, bool dirty); 179static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,)
180 long secnum, bool dirty);
180static void create_dos_name(const unsigned char *name, unsigned char *newname); 181static void create_dos_name(const unsigned char *name, unsigned char *newname);
181static void randomize_dos_name(unsigned char *name); 182static void randomize_dos_name(unsigned char *name);
182static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned long start); 183static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,)
183static int transfer(IF_MV2(struct bpb* fat_bpb,) unsigned long start, long count, char* buf, bool write ); 184 unsigned long start);
185static int transfer(IF_MV2(struct bpb* fat_bpb,) unsigned long start,
186 long count, char* buf, bool write );
184 187
185#define FAT_CACHE_SIZE 0x20 188#define FAT_CACHE_SIZE 0x20
186#define FAT_CACHE_MASK (FAT_CACHE_SIZE-1) 189#define FAT_CACHE_MASK (FAT_CACHE_SIZE-1)
@@ -285,7 +288,7 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
285#ifdef HAVE_MULTIVOLUME 288#ifdef HAVE_MULTIVOLUME
286 fat_bpb->drive = drive; 289 fat_bpb->drive = drive;
287#endif 290#endif
288 291
289 fat_bpb->bpb_bytspersec = BYTES2INT16(buf,BPB_BYTSPERSEC); 292 fat_bpb->bpb_bytspersec = BYTES2INT16(buf,BPB_BYTSPERSEC);
290 secmult = fat_bpb->bpb_bytspersec / SECTOR_SIZE; 293 secmult = fat_bpb->bpb_bytspersec / SECTOR_SIZE;
291 /* Sanity check is performed later */ 294 /* Sanity check is performed later */
@@ -318,7 +321,7 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
318 rootdirsectors = secmult * ((fat_bpb->bpb_rootentcnt * DIR_ENTRY_SIZE 321 rootdirsectors = secmult * ((fat_bpb->bpb_rootentcnt * DIR_ENTRY_SIZE
319 + fat_bpb->bpb_bytspersec - 1) / fat_bpb->bpb_bytspersec); 322 + fat_bpb->bpb_bytspersec - 1) / fat_bpb->bpb_bytspersec);
320#endif /* #ifdef HAVE_FAT16SUPPORT */ 323#endif /* #ifdef HAVE_FAT16SUPPORT */
321 324
322 fat_bpb->firstdatasector = fat_bpb->bpb_rsvdseccnt 325 fat_bpb->firstdatasector = fat_bpb->bpb_rsvdseccnt
323#ifdef HAVE_FAT16SUPPORT 326#ifdef HAVE_FAT16SUPPORT
324 + rootdirsectors 327 + rootdirsectors
@@ -365,7 +368,7 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
365 / fat_bpb->bpb_secperclus); /* rounded up, to full clusters */ 368 / fat_bpb->bpb_secperclus); /* rounded up, to full clusters */
366 /* I assign negative pseudo cluster numbers for the root directory, 369 /* I assign negative pseudo cluster numbers for the root directory,
367 their range is counted upward until -1. */ 370 their range is counted upward until -1. */
368 fat_bpb->bpb_rootclus = 0 - dirclusters; /* backwards, before the data */ 371 fat_bpb->bpb_rootclus = 0 - dirclusters; /* backwards, before the data*/
369 fat_bpb->rootdiroffset = dirclusters * fat_bpb->bpb_secperclus 372 fat_bpb->rootdiroffset = dirclusters * fat_bpb->bpb_secperclus
370 - rootdirsectors; 373 - rootdirsectors;
371 } 374 }
@@ -374,7 +377,8 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
374 { /* FAT32 specific part of BPB */ 377 { /* FAT32 specific part of BPB */
375 fat_bpb->bpb_rootclus = BYTES2INT32(buf,BPB_ROOTCLUS); 378 fat_bpb->bpb_rootclus = BYTES2INT32(buf,BPB_ROOTCLUS);
376 fat_bpb->bpb_fsinfo = secmult * BYTES2INT16(buf,BPB_FSINFO); 379 fat_bpb->bpb_fsinfo = secmult * BYTES2INT16(buf,BPB_FSINFO);
377 fat_bpb->rootdirsector = cluster2sec(IF_MV2(fat_bpb,) fat_bpb->bpb_rootclus); 380 fat_bpb->rootdirsector = cluster2sec(IF_MV2(fat_bpb,)
381 fat_bpb->bpb_rootclus);
378 } 382 }
379 383
380 rc = bpb_is_sane(IF_MV(fat_bpb)); 384 rc = bpb_is_sane(IF_MV(fat_bpb));
@@ -473,7 +477,7 @@ void fat_recalc_free(IF_MV_NONVOID(int volume))
473 unsigned int c = i * CLUSTERS_PER_FAT16_SECTOR + j; 477 unsigned int c = i * CLUSTERS_PER_FAT16_SECTOR + j;
474 if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */ 478 if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */
475 break; 479 break;
476 480
477 if (letoh16(fat[j]) == 0x0000) { 481 if (letoh16(fat[j]) == 0x0000) {
478 free++; 482 free++;
479 if ( fat_bpb->fsinfo.nextfree == 0xffffffff ) 483 if ( fat_bpb->fsinfo.nextfree == 0xffffffff )
@@ -492,7 +496,7 @@ void fat_recalc_free(IF_MV_NONVOID(int volume))
492 unsigned long c = i * CLUSTERS_PER_FAT_SECTOR + j; 496 unsigned long c = i * CLUSTERS_PER_FAT_SECTOR + j;
493 if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */ 497 if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */
494 break; 498 break;
495 499
496 if (!(letoh32(fat[j]) & 0x0fffffff)) { 500 if (!(letoh32(fat[j]) & 0x0fffffff)) {
497 free++; 501 free++;
498 if ( fat_bpb->fsinfo.nextfree == 0xffffffff ) 502 if ( fat_bpb->fsinfo.nextfree == 0xffffffff )
@@ -516,7 +520,8 @@ static int bpb_is_sane(IF_MV_NONVOID(struct bpb* fat_bpb))
516 fat_bpb->bpb_bytspersec); 520 fat_bpb->bpb_bytspersec);
517 return -1; 521 return -1;
518 } 522 }
519 if((long)fat_bpb->bpb_secperclus * (long)fat_bpb->bpb_bytspersec > 128L*1024L) 523 if((long)fat_bpb->bpb_secperclus * (long)fat_bpb->bpb_bytspersec
524 > 128L*1024L)
520 { 525 {
521 DEBUGF( "bpb_is_sane() - Error: cluster size is larger than 128K " 526 DEBUGF( "bpb_is_sane() - Error: cluster size is larger than 128K "
522 "(%d * %d = %d)\n", 527 "(%d * %d = %d)\n",
@@ -601,7 +606,7 @@ static void flush_fat_sector(struct fat_cache_entry *fce,
601 fce->dirty = false; 606 fce->dirty = false;
602} 607}
603 608
604/* Note: The returned pointer is only safely valid until the next 609/* Note: The returned pointer is only safely valid until the next
605 task switch! (Any subsequent ata read/write may yield.) */ 610 task switch! (Any subsequent ata read/write may yield.) */
606static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,) 611static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,)
607 long fatsector, bool dirty) 612 long fatsector, bool dirty)
@@ -657,7 +662,8 @@ static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,)
657 return sectorbuf; 662 return sectorbuf;
658} 663}
659 664
660static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned long startcluster) 665static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,)
666 unsigned long startcluster)
661{ 667{
662#ifndef HAVE_MULTIVOLUME 668#ifndef HAVE_MULTIVOLUME
663 struct bpb* fat_bpb = &fat_bpbs[0]; 669 struct bpb* fat_bpb = &fat_bpbs[0];
@@ -671,7 +677,7 @@ static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned lon
671 { 677 {
672 sector = startcluster / CLUSTERS_PER_FAT16_SECTOR; 678 sector = startcluster / CLUSTERS_PER_FAT16_SECTOR;
673 offset = startcluster % CLUSTERS_PER_FAT16_SECTOR; 679 offset = startcluster % CLUSTERS_PER_FAT16_SECTOR;
674 680
675 for (i = 0; i<fat_bpb->fatsize; i++) { 681 for (i = 0; i<fat_bpb->fatsize; i++) {
676 unsigned int j; 682 unsigned int j;
677 unsigned int nr = (i + sector) % fat_bpb->fatsize; 683 unsigned int nr = (i + sector) % fat_bpb->fatsize;
@@ -699,7 +705,7 @@ static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned lon
699 { 705 {
700 sector = startcluster / CLUSTERS_PER_FAT_SECTOR; 706 sector = startcluster / CLUSTERS_PER_FAT_SECTOR;
701 offset = startcluster % CLUSTERS_PER_FAT_SECTOR; 707 offset = startcluster % CLUSTERS_PER_FAT_SECTOR;
702 708
703 for (i = 0; i<fat_bpb->fatsize; i++) { 709 for (i = 0; i<fat_bpb->fatsize; i++) {
704 unsigned int j; 710 unsigned int j;
705 unsigned long nr = (i + sector) % fat_bpb->fatsize; 711 unsigned long nr = (i + sector) % fat_bpb->fatsize;
@@ -727,7 +733,8 @@ static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned lon
727 return 0; /* 0 is an illegal cluster number */ 733 return 0; /* 0 is an illegal cluster number */
728} 734}
729 735
730static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry, unsigned long val) 736static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry,
737 unsigned long val)
731{ 738{
732#ifndef HAVE_MULTIVOLUME 739#ifndef HAVE_MULTIVOLUME
733 struct bpb* fat_bpb = &fat_bpbs[0]; 740 struct bpb* fat_bpb = &fat_bpbs[0];
@@ -765,7 +772,8 @@ static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry, un
765 fat_bpb->fsinfo.freecount++; 772 fat_bpb->fsinfo.freecount++;
766 } 773 }
767 774
768 LDEBUGF("update_fat_entry: %d free clusters\n", fat_bpb->fsinfo.freecount); 775 LDEBUGF("update_fat_entry: %d free clusters\n",
776 fat_bpb->fsinfo.freecount);
769 777
770 sec[offset] = htole16(val); 778 sec[offset] = htole16(val);
771 } 779 }
@@ -787,7 +795,7 @@ static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry, un
787 sec = cache_fat_sector(IF_MV2(fat_bpb,) sector, true); 795 sec = cache_fat_sector(IF_MV2(fat_bpb,) sector, true);
788 if (!sec) 796 if (!sec)
789 { 797 {
790 DEBUGF( "update_fat_entry() - Could not cache sector %ld\n", sector); 798 DEBUGF("update_fat_entry() - Could not cache sector %ld\n", sector);
791 return -1; 799 return -1;
792 } 800 }
793 801
@@ -801,7 +809,8 @@ static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry, un
801 fat_bpb->fsinfo.freecount++; 809 fat_bpb->fsinfo.freecount++;
802 } 810 }
803 811
804 LDEBUGF("update_fat_entry: %ld free clusters\n", fat_bpb->fsinfo.freecount); 812 LDEBUGF("update_fat_entry: %ld free clusters\n",
813 fat_bpb->fsinfo.freecount);
805 814
806 /* don't change top 4 bits */ 815 /* don't change top 4 bits */
807 sec[offset] &= htole32(0xf0000000); 816 sec[offset] &= htole32(0xf0000000);
@@ -854,7 +863,7 @@ static long get_next_cluster(IF_MV2(struct bpb* fat_bpb,) long cluster)
854{ 863{
855 long next_cluster; 864 long next_cluster;
856 long eof_mark = FAT_EOF_MARK; 865 long eof_mark = FAT_EOF_MARK;
857 866
858#ifdef HAVE_FAT16SUPPORT 867#ifdef HAVE_FAT16SUPPORT
859#ifndef HAVE_MULTIVOLUME 868#ifndef HAVE_MULTIVOLUME
860 struct bpb* fat_bpb = &fat_bpbs[0]; 869 struct bpb* fat_bpb = &fat_bpbs[0];
@@ -883,12 +892,12 @@ static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb))
883 unsigned char fsinfo[SECTOR_SIZE]; 892 unsigned char fsinfo[SECTOR_SIZE];
884 unsigned long* intptr; 893 unsigned long* intptr;
885 int rc; 894 int rc;
886 895
887#ifdef HAVE_FAT16SUPPORT 896#ifdef HAVE_FAT16SUPPORT
888 if (fat_bpb->is_fat16) 897 if (fat_bpb->is_fat16)
889 return 0; /* FAT16 has no FsInfo */ 898 return 0; /* FAT16 has no FsInfo */
890#endif /* #ifdef HAVE_FAT16SUPPORT */ 899#endif /* #ifdef HAVE_FAT16SUPPORT */
891 900
892 /* update fsinfo */ 901 /* update fsinfo */
893 rc = ata_read_sectors(IF_MV2(fat_bpb->drive,) 902 rc = ata_read_sectors(IF_MV2(fat_bpb->drive,)
894 fat_bpb->startsector + fat_bpb->bpb_fsinfo, 1,fsinfo); 903 fat_bpb->startsector + fat_bpb->bpb_fsinfo, 1,fsinfo);
@@ -967,7 +976,7 @@ static void fat_time(unsigned short* date,
967 /* non-RTC version returns an increment from the supplied time, or a 976 /* non-RTC version returns an increment from the supplied time, or a
968 * fixed standard time/date if no time given as input */ 977 * fixed standard time/date if no time given as input */
969 bool next_day = false; 978 bool next_day = false;
970 979
971 if (time) 980 if (time)
972 { 981 {
973 if (0 == *time) 982 if (0 == *time)
@@ -992,7 +1001,7 @@ static void fat_time(unsigned short* date,
992 *time = (hours << 11) | (mins << 5); 1001 *time = (hours << 11) | (mins << 5);
993 } 1002 }
994 } 1003 }
995 1004
996 if (date) 1005 if (date)
997 { 1006 {
998 if (0 == *date) 1007 if (0 == *date)
@@ -1004,8 +1013,8 @@ static void fat_time(unsigned short* date,
1004#define S100(x) 1 ## x 1013#define S100(x) 1 ## x
1005#define C2DIG2DEC(x) (S100(x)-100) 1014#define C2DIG2DEC(x) (S100(x)-100)
1006 /* set to build date */ 1015 /* set to build date */
1007 *date = ((YEAR - 1980) << 9) | (C2DIG2DEC(MONTH) << 5) 1016 *date = ((YEAR - 1980) << 9) | (C2DIG2DEC(MONTH) << 5)
1008 | C2DIG2DEC(DAY); 1017 | C2DIG2DEC(DAY);
1009 } 1018 }
1010 else 1019 else
1011 { 1020 {
@@ -1174,7 +1183,7 @@ static int write_long_name(struct fat_file* file,
1174 rc = fat_readwrite(file, 1, buf, true); 1183 rc = fat_readwrite(file, 1, buf, true);
1175 if (rc<1) 1184 if (rc<1)
1176 return rc * 10 - 7; 1185 return rc * 10 - 7;
1177 1186
1178 return 0; 1187 return 0;
1179} 1188}
1180 1189
@@ -1237,7 +1246,7 @@ static int add_dir_entry(struct fat_dir* dir,
1237 strncpy(shortname, name, 12); 1246 strncpy(shortname, name, 12);
1238 for(i = strlen(shortname); i < 12; i++) 1247 for(i = strlen(shortname); i < 12; i++)
1239 shortname[i] = ' '; 1248 shortname[i] = ' ';
1240 1249
1241 entries_needed = 1; 1250 entries_needed = 1;
1242 } else { 1251 } else {
1243 create_dos_name(name, shortname); 1252 create_dos_name(name, shortname);
@@ -1250,7 +1259,7 @@ static int add_dir_entry(struct fat_dir* dir,
1250 1259
1251 restart: 1260 restart:
1252 firstentry = -1; 1261 firstentry = -1;
1253 1262
1254 rc = fat_seek(&dir->file, 0); 1263 rc = fat_seek(&dir->file, 0);
1255 if (rc < 0) 1264 if (rc < 0)
1256 return rc * 10 - 2; 1265 return rc * 10 - 2;
@@ -1342,7 +1351,7 @@ static int add_dir_entry(struct fat_dir* dir,
1342 sector = firstentry / DIR_ENTRIES_PER_SECTOR; 1351 sector = firstentry / DIR_ENTRIES_PER_SECTOR;
1343 LDEBUGF("Adding longname to entry %d in sector %d\n", 1352 LDEBUGF("Adding longname to entry %d in sector %d\n",
1344 firstentry, sector); 1353 firstentry, sector);
1345 1354
1346 rc = write_long_name(&dir->file, firstentry, 1355 rc = write_long_name(&dir->file, firstentry,
1347 entries_needed, name, shortname, is_directory); 1356 entries_needed, name, shortname, is_directory);
1348 if (rc < 0) 1357 if (rc < 0)
@@ -1361,7 +1370,7 @@ static int add_dir_entry(struct fat_dir* dir,
1361static unsigned char char2dos(unsigned char c, int* randomize) 1370static unsigned char char2dos(unsigned char c, int* randomize)
1362{ 1371{
1363 static const char invalid_chars[] = "\"*+,./:;<=>?[\\]|"; 1372 static const char invalid_chars[] = "\"*+,./:;<=>?[\\]|";
1364 1373
1365 if (c <= 0x20) 1374 if (c <= 0x20)
1366 c = 0; /* Illegal char, remove */ 1375 c = 0; /* Illegal char, remove */
1367 else if (strchr(invalid_chars, c) != NULL) 1376 else if (strchr(invalid_chars, c) != NULL)
@@ -1379,7 +1388,7 @@ static unsigned char char2dos(unsigned char c, int* randomize)
1379static void create_dos_name(const unsigned char *name, unsigned char *newname) 1388static void create_dos_name(const unsigned char *name, unsigned char *newname)
1380{ 1389{
1381 int i; 1390 int i;
1382 unsigned char *ext; 1391 unsigned char *ext;
1383 int randomize = 0; 1392 int randomize = 0;
1384 1393
1385 /* Find extension part */ 1394 /* Find extension part */
@@ -1491,12 +1500,12 @@ static int update_short_entry( struct fat_file* file, long size, int attr )
1491 1500
1492 if (!entry[0] || entry[0] == 0xe5) 1501 if (!entry[0] || entry[0] == 0xe5)
1493 panicf("Updating size on empty dir entry %d\n", file->direntry); 1502 panicf("Updating size on empty dir entry %d\n", file->direntry);
1494 1503
1495 entry[FATDIR_ATTR] = attr & 0xFF; 1504 entry[FATDIR_ATTR] = attr & 0xFF;
1496 1505
1497 clusptr = (short*)(entry + FATDIR_FSTCLUSHI); 1506 clusptr = (short*)(entry + FATDIR_FSTCLUSHI);
1498 *clusptr = htole16(file->firstcluster >> 16); 1507 *clusptr = htole16(file->firstcluster >> 16);
1499 1508
1500 clusptr = (short*)(entry + FATDIR_FSTCLUSLO); 1509 clusptr = (short*)(entry + FATDIR_FSTCLUSLO);
1501 *clusptr = htole16(file->firstcluster & 0xffff); 1510 *clusptr = htole16(file->firstcluster & 0xffff);
1502 1511
@@ -1509,8 +1518,8 @@ static int update_short_entry( struct fat_file* file, long size, int attr )
1509 unsigned short date = 0; 1518 unsigned short date = 0;
1510#else 1519#else
1511 /* get old time to increment from */ 1520 /* get old time to increment from */
1512 unsigned short time = htole16(*(unsigned short*)(entry + FATDIR_WRTTIME)); 1521 unsigned short time = htole16(*(unsigned short*)(entry+FATDIR_WRTTIME));
1513 unsigned short date = htole16(*(unsigned short*)(entry + FATDIR_WRTDATE)); 1522 unsigned short date = htole16(*(unsigned short*)(entry+FATDIR_WRTDATE));
1514#endif 1523#endif
1515 fat_time(&date, &time, NULL); 1524 fat_time(&date, &time, NULL);
1516 *(unsigned short*)(entry + FATDIR_WRTTIME) = htole16(time); 1525 *(unsigned short*)(entry + FATDIR_WRTTIME) = htole16(time);
@@ -1547,7 +1556,7 @@ static int parse_direntry(struct fat_direntry *de, const unsigned char *buf)
1547 ((long)(unsigned)BYTES2INT16(buf,FATDIR_FSTCLUSHI) << 16); 1556 ((long)(unsigned)BYTES2INT16(buf,FATDIR_FSTCLUSHI) << 16);
1548 /* The double cast is to prevent a sign-extension to be done on CalmRISC16. 1557 /* The double cast is to prevent a sign-extension to be done on CalmRISC16.
1549 (the result of the shift is always considered signed) */ 1558 (the result of the shift is always considered signed) */
1550 1559
1551 /* fix the name */ 1560 /* fix the name */
1552 lowercase = (buf[FATDIR_NTRES] & FAT_NTRES_LC_NAME); 1561 lowercase = (buf[FATDIR_NTRES] & FAT_NTRES_LC_NAME);
1553 c = buf[FATDIR_NAME]; 1562 c = buf[FATDIR_NAME];
@@ -1646,7 +1655,8 @@ int fat_create_dir(const char* name,
1646 return rc * 10 - 1; 1655 return rc * 10 - 1;
1647 1656
1648 /* Allocate a new cluster for the directory */ 1657 /* Allocate a new cluster for the directory */
1649 newdir->file.firstcluster = find_free_cluster(IF_MV2(fat_bpb,) fat_bpb->fsinfo.nextfree); 1658 newdir->file.firstcluster = find_free_cluster(IF_MV2(fat_bpb,)
1659 fat_bpb->fsinfo.nextfree);
1650 if(newdir->file.firstcluster == 0) 1660 if(newdir->file.firstcluster == 0)
1651 return -1; 1661 return -1;
1652 1662
@@ -1660,7 +1670,7 @@ int fat_create_dir(const char* name,
1660 if (rc < 0) 1670 if (rc < 0)
1661 return rc * 10 - 2; 1671 return rc * 10 - 2;
1662 } 1672 }
1663 1673
1664 /* Then add the "." entry */ 1674 /* Then add the "." entry */
1665 rc = add_dir_entry(newdir, &dummyfile, ".", true, true); 1675 rc = add_dir_entry(newdir, &dummyfile, ".", true, true);
1666 if (rc < 0) 1676 if (rc < 0)
@@ -1679,10 +1689,10 @@ int fat_create_dir(const char* name,
1679 else 1689 else
1680 dummyfile.firstcluster = dir->file.firstcluster; 1690 dummyfile.firstcluster = dir->file.firstcluster;
1681 update_short_entry(&dummyfile, 0, FAT_ATTR_DIRECTORY); 1691 update_short_entry(&dummyfile, 0, FAT_ATTR_DIRECTORY);
1682 1692
1683 /* Set the firstcluster field in the direntry */ 1693 /* Set the firstcluster field in the direntry */
1684 update_short_entry(&newdir->file, 0, FAT_ATTR_DIRECTORY); 1694 update_short_entry(&newdir->file, 0, FAT_ATTR_DIRECTORY);
1685 1695
1686 rc = flush_fat(IF_MV(fat_bpb)); 1696 rc = flush_fat(IF_MV(fat_bpb));
1687 if (rc < 0) 1697 if (rc < 0)
1688 return rc * 10 - 5; 1698 return rc * 10 - 5;
@@ -1874,7 +1884,7 @@ int fat_rename(struct fat_file* file,
1874 unsigned int parentcluster; 1884 unsigned int parentcluster;
1875#ifdef HAVE_MULTIVOLUME 1885#ifdef HAVE_MULTIVOLUME
1876 struct bpb* fat_bpb = &fat_bpbs[file->volume]; 1886 struct bpb* fat_bpb = &fat_bpbs[file->volume];
1877 1887
1878 if (file->volume != dir->file.volume) { 1888 if (file->volume != dir->file.volume) {
1879 DEBUGF("No rename across volumes!\n"); 1889 DEBUGF("No rename across volumes!\n");
1880 return -1; 1890 return -1;
@@ -1983,7 +1993,8 @@ static long next_write_cluster(struct fat_file* file,
1983 if (oldcluster > 0) 1993 if (oldcluster > 0)
1984 cluster = find_free_cluster(IF_MV2(fat_bpb,) oldcluster+1); 1994 cluster = find_free_cluster(IF_MV2(fat_bpb,) oldcluster+1);
1985 else if (oldcluster == 0) 1995 else if (oldcluster == 0)
1986 cluster = find_free_cluster(IF_MV2(fat_bpb,) fat_bpb->fsinfo.nextfree); 1996 cluster = find_free_cluster(IF_MV2(fat_bpb,)
1997 fat_bpb->fsinfo.nextfree);
1987#ifdef HAVE_FAT16SUPPORT 1998#ifdef HAVE_FAT16SUPPORT
1988 else /* negative, pseudo-cluster of the root dir */ 1999 else /* negative, pseudo-cluster of the root dir */
1989 return 0; /* impossible to append something to the root */ 2000 return 0; /* impossible to append something to the root */
@@ -1991,7 +2002,7 @@ static long next_write_cluster(struct fat_file* file,
1991 2002
1992 if (cluster) { 2003 if (cluster) {
1993 if (oldcluster) 2004 if (oldcluster)
1994 update_fat_entry(IF_MV2(fat_bpb,) oldcluster, cluster); 2005 update_fat_entry(IF_MV2(fat_bpb,) oldcluster, cluster);
1995 else 2006 else
1996 file->firstcluster = cluster; 2007 file->firstcluster = cluster;
1997 update_fat_entry(IF_MV2(fat_bpb,) cluster, FAT_EOF_MARK); 2008 update_fat_entry(IF_MV2(fat_bpb,) cluster, FAT_EOF_MARK);
@@ -2032,7 +2043,7 @@ static int transfer(IF_MV2(struct bpb* fat_bpb,)
2032 else 2043 else
2033#endif 2044#endif
2034 firstallowed = fat_bpb->firstdatasector; 2045 firstallowed = fat_bpb->firstdatasector;
2035 2046
2036 if (start < firstallowed) 2047 if (start < firstallowed)
2037 panicf("Write %ld before data\n", firstallowed - start); 2048 panicf("Write %ld before data\n", firstallowed - start);
2038 if (start + count > fat_bpb->totalsectors) 2049 if (start + count > fat_bpb->totalsectors)
@@ -2099,7 +2110,7 @@ long fat_readwrite( struct fat_file *file, long sectorcount,
2099 if (!cluster) { 2110 if (!cluster) {
2100 eof = true; 2111 eof = true;
2101 if ( write ) { 2112 if ( write ) {
2102 /* remember last cluster, in case 2113 /* remember last cluster, in case
2103 we want to append to the file */ 2114 we want to append to the file */
2104 sector = oldsector; 2115 sector = oldsector;
2105 cluster = oldcluster; 2116 cluster = oldcluster;
@@ -2207,7 +2218,7 @@ int fat_seek(struct fat_file *file, unsigned long seeksector )
2207 return -1; 2218 return -1;
2208 } 2219 }
2209 } 2220 }
2210 2221
2211 sector = cluster2sec(IF_MV2(fat_bpb,) cluster) + sectornum; 2222 sector = cluster2sec(IF_MV2(fat_bpb,) cluster) + sectornum;
2212 } 2223 }
2213 else { 2224 else {
@@ -2362,14 +2373,16 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
2362 /* replace shortname with longname? */ 2373 /* replace shortname with longname? */
2363 if ( longs ) { 2374 if ( longs ) {
2364 int j; 2375 int j;
2365 /* This should be enough to hold any name segment utf8-encoded */ 2376 /* This should be enough to hold any name segment
2377 utf8-encoded */
2366 unsigned char shortname[13]; /* 8+3+dot+\0 */ 2378 unsigned char shortname[13]; /* 8+3+dot+\0 */
2367 unsigned char longname_utf8segm[6*4 + 1]; /* Add 1 for trailing \0 */ 2379 /* Add 1 for trailing \0 */
2380 unsigned char longname_utf8segm[6*4 + 1];
2368 int longname_utf8len = 0; 2381 int longname_utf8len = 0;
2369 2382 /* Temporarily store it */
2370 strcpy(shortname, entry->name); /* Temporarily store it */ 2383 strcpy(shortname, entry->name);
2371 entry->name[0] = 0; 2384 entry->name[0] = 0;
2372 2385
2373 /* iterate backwards through the dir entries */ 2386 /* iterate backwards through the dir entries */
2374 for (j=longs-1; j>=0; j--) { 2387 for (j=longs-1; j>=0; j--) {
2375 unsigned char* ptr = cached_buf; 2388 unsigned char* ptr = cached_buf;
@@ -2391,11 +2404,13 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
2391 index &= SECTOR_SIZE-1; 2404 index &= SECTOR_SIZE-1;
2392 } 2405 }
2393 2406
2394 /* Try to append each segment of the long name. Check if we'd 2407 /* Try to append each segment of the long name.
2395 exceed the buffer. Also check for FAT padding characters 0xFFFF. */ 2408 Check if we'd exceed the buffer.
2409 Also check for FAT padding characters 0xFFFF. */
2396 if (fat_copy_long_name_segment(ptr + index + 1, 5, 2410 if (fat_copy_long_name_segment(ptr + index + 1, 5,
2397 longname_utf8segm) == 0) break; 2411 longname_utf8segm) == 0) break;
2398 // logf("SG: %s, EN: %s", longname_utf8segm, entry->name); 2412 /* logf("SG: %s, EN: %s", longname_utf8segm,
2413 entry->name); */
2399 longname_utf8len += strlen(longname_utf8segm); 2414 longname_utf8len += strlen(longname_utf8segm);
2400 if (longname_utf8len < FAT_FILENAME_BYTES) 2415 if (longname_utf8len < FAT_FILENAME_BYTES)
2401 strcat(entry->name, longname_utf8segm); 2416 strcat(entry->name, longname_utf8segm);
@@ -2404,7 +2419,8 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
2404 2419
2405 if (fat_copy_long_name_segment(ptr + index + 14, 6, 2420 if (fat_copy_long_name_segment(ptr + index + 14, 6,
2406 longname_utf8segm) == 0) break; 2421 longname_utf8segm) == 0) break;
2407 // logf("SG: %s, EN: %s", longname_utf8segm, entry->name); 2422 /* logf("SG: %s, EN: %s", longname_utf8segm,
2423 entry->name); */
2408 longname_utf8len += strlen(longname_utf8segm); 2424 longname_utf8len += strlen(longname_utf8segm);
2409 if (longname_utf8len < FAT_FILENAME_BYTES) 2425 if (longname_utf8len < FAT_FILENAME_BYTES)
2410 strcat(entry->name, longname_utf8segm); 2426 strcat(entry->name, longname_utf8segm);
@@ -2413,7 +2429,8 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
2413 2429
2414 if (fat_copy_long_name_segment(ptr + index + 28, 2, 2430 if (fat_copy_long_name_segment(ptr + index + 28, 2,
2415 longname_utf8segm) == 0) break; 2431 longname_utf8segm) == 0) break;
2416 // logf("SG: %s, EN: %s", longname_utf8segm, entry->name); 2432 /* logf("SG: %s, EN: %s", longname_utf8segm,
2433 entry->name); */
2417 longname_utf8len += strlen(longname_utf8segm); 2434 longname_utf8len += strlen(longname_utf8segm);
2418 if (longname_utf8len < FAT_FILENAME_BYTES) 2435 if (longname_utf8len < FAT_FILENAME_BYTES)
2419 strcat(entry->name, longname_utf8segm); 2436 strcat(entry->name, longname_utf8segm);
@@ -2423,19 +2440,22 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
2423 2440
2424 /* Does the utf8-encoded name fit into the entry? */ 2441 /* Does the utf8-encoded name fit into the entry? */
2425 if (longname_utf8len >= FAT_FILENAME_BYTES) { 2442 if (longname_utf8len >= FAT_FILENAME_BYTES) {
2426 /* Take the short DOS name. Need to utf8-encode it since 2443 /* Take the short DOS name. Need to utf8-encode it
2427 it may contain chars from the upper half of the OEM 2444 since it may contain chars from the upper half of
2428 code page which wouldn't be a valid utf8. Beware: this 2445 the OEM code page which wouldn't be a valid utf8.
2429 file will be shown with strange glyphs in file browser 2446 Beware: this file will be shown with strange
2430 since unicode 0x80 to 0x9F are control characters. */ 2447 glyphs in file browser since unicode 0x80 to 0x9F
2448 are control characters. */
2431 logf("SN-DOS: %s", shortname); 2449 logf("SN-DOS: %s", shortname);
2432 unsigned char *utf8; 2450 unsigned char *utf8;
2433 utf8 = iso_decode(shortname, entry->name, -1, strlen(shortname)); 2451 utf8 = iso_decode(shortname, entry->name, -1,
2452 strlen(shortname));
2434 *utf8 = 0; 2453 *utf8 = 0;
2435 logf("SN: %s", entry->name); 2454 logf("SN: %s", entry->name);
2436 } else { 2455 } else {
2437 // logf("LN: %s", entry->name); 2456 /* logf("LN: %s", entry->name);
2438 // logf("LNLen: %d (%c)", longname_utf8len, entry->name[0]); 2457 logf("LNLen: %d (%c)", longname_utf8len,
2458 entry->name[0]); */
2439 } 2459 }
2440 } 2460 }
2441 done = true; 2461 done = true;