summaryrefslogtreecommitdiff
path: root/firmware/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/drivers')
-rw-r--r--firmware/drivers/fat.c352
1 files changed, 183 insertions, 169 deletions
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index ef013ac481..f41f039380 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -16,7 +16,6 @@
16 * KIND, either express or implied. 16 * KIND, either express or implied.
17 * 17 *
18 ****************************************************************************/ 18 ****************************************************************************/
19
20#include <stdio.h> 19#include <stdio.h>
21#include <string.h> 20#include <string.h>
22#include <math.h> 21#include <math.h>
@@ -300,7 +299,7 @@ void fat_size(unsigned int* size, unsigned int* free)
300int fat_mount(int startsector) 299int fat_mount(int startsector)
301{ 300{
302 unsigned char buf[SECTOR_SIZE]; 301 unsigned char buf[SECTOR_SIZE];
303 int err; 302 int rc;
304 int datasec; 303 int datasec;
305 unsigned int i; 304 unsigned int i;
306 305
@@ -312,11 +311,11 @@ int fat_mount(int startsector)
312 } 311 }
313 312
314 /* Read the sector */ 313 /* Read the sector */
315 err = ata_read_sectors(startsector,1,buf); 314 rc = ata_read_sectors(startsector,1,buf);
316 if(err) 315 if(rc)
317 { 316 {
318 DEBUGF( "fat_mount() - Couldn't read BPB (error code %d)\n", err); 317 DEBUGF( "fat_mount() - Couldn't read BPB (error code %d)\n", rc);
319 return -1; 318 return rc * 10 - 1;
320 } 319 }
321 320
322 memset(&fat_bpb, 0, sizeof(struct bpb)); 321 memset(&fat_bpb, 0, sizeof(struct bpb));
@@ -385,20 +384,21 @@ int fat_mount(int startsector)
385 strncpy(fat_bpb.bs_filsystype, &buf[BS_32_FILSYSTYPE], 8); 384 strncpy(fat_bpb.bs_filsystype, &buf[BS_32_FILSYSTYPE], 8);
386 } 385 }
387 386
388 if (bpb_is_sane() < 0) 387 rc = bpb_is_sane();
388 if (rc < 0)
389 { 389 {
390 DEBUGF( "fat_mount() - BPB is not sane\n"); 390 DEBUGF( "fat_mount() - BPB is not sane\n");
391 return -3; 391 return rc * 10 - 3;
392 } 392 }
393 393
394 fat_bpb.rootdirsector = cluster2sec(fat_bpb.bpb_rootclus); 394 fat_bpb.rootdirsector = cluster2sec(fat_bpb.bpb_rootclus);
395 395
396 /* Read the fsinfo sector */ 396 /* Read the fsinfo sector */
397 err = ata_read_sectors(startsector + fat_bpb.bpb_fsinfo, 1, buf); 397 rc = ata_read_sectors(startsector + fat_bpb.bpb_fsinfo, 1, buf);
398 if (err) 398 if (rc < 0)
399 { 399 {
400 DEBUGF( "fat_mount() - Couldn't read FSInfo (error code %d)\n", err); 400 DEBUGF( "fat_mount() - Couldn't read FSInfo (error code %d)\n", rc);
401 return -4; 401 return rc * 10 - 4;
402 } 402 }
403 fat_bpb.fsinfo.freecount = BYTES2INT32(buf, FSINFO_FREECOUNT); 403 fat_bpb.fsinfo.freecount = BYTES2INT32(buf, FSINFO_FREECOUNT);
404 fat_bpb.fsinfo.nextfree = BYTES2INT32(buf, FSINFO_NEXTFREE); 404 fat_bpb.fsinfo.nextfree = BYTES2INT32(buf, FSINFO_NEXTFREE);
@@ -498,6 +498,7 @@ static void *cache_fat_sector(int fatsector)
498 int cache_index = secnum & FAT_CACHE_MASK; 498 int cache_index = secnum & FAT_CACHE_MASK;
499 struct fat_cache_entry *fce = &fat_cache[cache_index]; 499 struct fat_cache_entry *fce = &fat_cache[cache_index];
500 unsigned char *sectorbuf = &fat_cache_sectors[cache_index][0]; 500 unsigned char *sectorbuf = &fat_cache_sectors[cache_index][0];
501 int rc;
501 502
502 /* Delete the cache entry if it isn't the sector we want */ 503 /* Delete the cache entry if it isn't the sector we want */
503 if(fce->inuse && fce->secnum != secnum) 504 if(fce->inuse && fce->secnum != secnum)
@@ -505,20 +506,24 @@ static void *cache_fat_sector(int fatsector)
505 /* Write back if it is dirty */ 506 /* Write back if it is dirty */
506 if(fce->dirty) 507 if(fce->dirty)
507 { 508 {
508 if(ata_write_sectors(fce->secnum+fat_bpb.startsector, 1, 509 rc = ata_write_sectors(fce->secnum+fat_bpb.startsector, 1,
509 sectorbuf)) 510 sectorbuf);
511 if(rc < 0)
510 { 512 {
511 panicf("cache_fat_sector() - Could not write sector %d\n", 513 panicf("cache_fat_sector() - Could not write sector %d"
512 secnum); 514 " (error %d)\n",
515 secnum, rc);
513 } 516 }
514 if(fat_bpb.bpb_numfats > 1) 517 if(fat_bpb.bpb_numfats > 1)
515 { 518 {
516 /* Write to the second FAT */ 519 /* Write to the second FAT */
517 if(ata_write_sectors(fce->secnum+fat_bpb.startsector+ 520 rc = ata_write_sectors(fce->secnum+fat_bpb.startsector+
518 fat_bpb.fatsize, 1, sectorbuf)) 521 fat_bpb.fatsize, 1, sectorbuf);
522 if(rc < 0)
519 { 523 {
520 panicf("cache_fat_sector() - Could not write sector %d\n", 524 panicf("cache_fat_sector() - Could not write sector %d"
521 secnum + fat_bpb.fatsize); 525 " (error %d)\n",
526 secnum + fat_bpb.fatsize, rc);
522 } 527 }
523 } 528 }
524 } 529 }
@@ -530,10 +535,12 @@ static void *cache_fat_sector(int fatsector)
530 /* Load the sector if it is not cached */ 535 /* Load the sector if it is not cached */
531 if(!fce->inuse) 536 if(!fce->inuse)
532 { 537 {
533 if(ata_read_sectors(secnum + fat_bpb.startsector,1, 538 rc = ata_read_sectors(secnum + fat_bpb.startsector,1,
534 sectorbuf)) 539 sectorbuf);
540 if(rc < 0)
535 { 541 {
536 DEBUGF( "cache_fat_sector() - Could not read sector %d\n", secnum); 542 DEBUGF( "cache_fat_sector() - Could not read sector %d"
543 " (error %d)\n", secnum, rc);
537 return NULL; 544 return NULL;
538 } 545 }
539 fce->inuse = true; 546 fce->inuse = true;
@@ -648,14 +655,14 @@ static int update_fsinfo(void)
648{ 655{
649 unsigned char fsinfo[SECTOR_SIZE]; 656 unsigned char fsinfo[SECTOR_SIZE];
650 unsigned int* intptr; 657 unsigned int* intptr;
651 int err; 658 int rc;
652 659
653 /* update fsinfo */ 660 /* update fsinfo */
654 err = ata_read_sectors(fat_bpb.startsector + fat_bpb.bpb_fsinfo, 1,fsinfo); 661 rc = ata_read_sectors(fat_bpb.startsector + fat_bpb.bpb_fsinfo, 1,fsinfo);
655 if (err) 662 if (rc < 0)
656 { 663 {
657 DEBUGF( "flush_fat() - Couldn't read FSInfo (error code %d)\n", err); 664 DEBUGF( "flush_fat() - Couldn't read FSInfo (error code %d)\n", rc);
658 return -1; 665 return rc * 10 - 1;
659 } 666 }
660 intptr = (int*)&(fsinfo[FSINFO_FREECOUNT]); 667 intptr = (int*)&(fsinfo[FSINFO_FREECOUNT]);
661 *intptr = SWAB32(fat_bpb.fsinfo.freecount); 668 *intptr = SWAB32(fat_bpb.fsinfo.freecount);
@@ -663,11 +670,11 @@ static int update_fsinfo(void)
663 intptr = (int*)&(fsinfo[FSINFO_NEXTFREE]); 670 intptr = (int*)&(fsinfo[FSINFO_NEXTFREE]);
664 *intptr = SWAB32(fat_bpb.fsinfo.nextfree); 671 *intptr = SWAB32(fat_bpb.fsinfo.nextfree);
665 672
666 err = ata_write_sectors(fat_bpb.startsector + fat_bpb.bpb_fsinfo,1,fsinfo); 673 rc = ata_write_sectors(fat_bpb.startsector + fat_bpb.bpb_fsinfo,1,fsinfo);
667 if (err) 674 if (rc < 0)
668 { 675 {
669 DEBUGF( "flush_fat() - Couldn't write FSInfo (error code %d)\n", err); 676 DEBUGF( "flush_fat() - Couldn't write FSInfo (error code %d)\n", rc);
670 return -2; 677 return rc * 10 - 2;
671 } 678 }
672 679
673 return 0; 680 return 0;
@@ -676,7 +683,7 @@ static int update_fsinfo(void)
676static int flush_fat(void) 683static int flush_fat(void)
677{ 684{
678 int i; 685 int i;
679 int err; 686 int rc;
680 unsigned char *sec; 687 unsigned char *sec;
681 int secnum; 688 int secnum;
682 LDEBUGF("flush_fat()\n"); 689 LDEBUGF("flush_fat()\n");
@@ -690,31 +697,33 @@ static int flush_fat(void)
690 sec = fat_cache_sectors[i]; 697 sec = fat_cache_sectors[i];
691 698
692 /* Write to the first FAT */ 699 /* Write to the first FAT */
693 err = ata_write_sectors(secnum, 1, sec); 700 rc = ata_write_sectors(secnum, 1, sec);
694 if(err) 701 if(rc)
695 { 702 {
696 DEBUGF( "flush_fat() - Couldn't write" 703 DEBUGF( "flush_fat() - Couldn't write"
697 " sector (%d)\n", secnum); 704 " sector %d (error %d)\n", secnum, rc);
698 return -1; 705 return rc * 10 - 1;
699 } 706 }
700 707
701 if(fat_bpb.bpb_numfats > 1 ) 708 if(fat_bpb.bpb_numfats > 1 )
702 { 709 {
703 /* Write to the second FAT */ 710 /* Write to the second FAT */
704 err = ata_write_sectors(secnum + fat_bpb.fatsize, 1, sec); 711 rc = ata_write_sectors(secnum + fat_bpb.fatsize, 1, sec);
705 if (err) 712 if (rc)
706 { 713 {
707 DEBUGF( "flush_fat() - Couldn't write" 714 DEBUGF( "flush_fat() - Couldn't write"
708 " sector (%d)\n", secnum + fat_bpb.fatsize); 715 " sector %d (error %d)\n",
709 return -2; 716 secnum + fat_bpb.fatsize, rc);
717 return rc * 10 - 2;
710 } 718 }
711 } 719 }
712 fat_cache[i].dirty = false; 720 fat_cache[i].dirty = false;
713 } 721 }
714 } 722 }
715 723
716 if (update_fsinfo()) 724 rc = update_fsinfo();
717 return -3; 725 if (rc < 0)
726 return rc * 10 - 3;
718 727
719 return 0; 728 return 0;
720} 729}
@@ -761,11 +770,11 @@ static int write_long_name(struct fat_file* file,
761 770
762 rc = fat_seek(file, sector); 771 rc = fat_seek(file, sector);
763 if (rc<0) 772 if (rc<0)
764 return -1; 773 return rc * 10 - 1;
765 774
766 rc = fat_readwrite(file, 1, buf, false); 775 rc = fat_readwrite(file, 1, buf, false);
767 if (rc<1) 776 if (rc<1)
768 return -2; 777 return rc * 10 - 2;
769 778
770 /* calculate shortname checksum */ 779 /* calculate shortname checksum */
771 for (i=11; i>0; i--) 780 for (i=11; i>0; i--)
@@ -783,17 +792,17 @@ static int write_long_name(struct fat_file* file,
783 /* update current sector */ 792 /* update current sector */
784 rc = fat_seek(file, sector); 793 rc = fat_seek(file, sector);
785 if (rc<0) 794 if (rc<0)
786 return -3; 795 return rc * 10 - 3;
787 796
788 rc = fat_readwrite(file, 1, buf, true); 797 rc = fat_readwrite(file, 1, buf, true);
789 if (rc<1) 798 if (rc<1)
790 return -4; 799 return rc * 10 - 4;
791 800
792 /* read next sector */ 801 /* read next sector */
793 rc = fat_readwrite(file, 1, buf, false); 802 rc = fat_readwrite(file, 1, buf, false);
794 if (rc<0) { 803 if (rc<0) {
795 LDEBUGF("Failed writing new sector: %d\n",rc); 804 LDEBUGF("Failed writing new sector: %d\n",rc);
796 return -5; 805 return rc * 10 - 5;
797 } 806 }
798 if (rc==0) 807 if (rc==0)
799 /* end of dir */ 808 /* end of dir */
@@ -872,11 +881,11 @@ static int write_long_name(struct fat_file* file,
872 /* update last sector */ 881 /* update last sector */
873 rc = fat_seek(file, sector); 882 rc = fat_seek(file, sector);
874 if (rc<0) 883 if (rc<0)
875 return -5; 884 return rc * 10 - 6;
876 885
877 rc = fat_readwrite(file, 1, buf, true); 886 rc = fat_readwrite(file, 1, buf, true);
878 if (rc<1) 887 if (rc<1)
879 return -6; 888 return rc * 10 - 7;
880 889
881 return 0; 890 return 0;
882} 891}
@@ -887,7 +896,7 @@ static int add_dir_entry(struct fat_dir* dir,
887{ 896{
888 unsigned char buf[SECTOR_SIZE]; 897 unsigned char buf[SECTOR_SIZE];
889 unsigned char shortname[16]; 898 unsigned char shortname[16];
890 int err; 899 int rc;
891 unsigned int sector; 900 unsigned int sector;
892 bool done = false; 901 bool done = false;
893 bool eof = false; 902 bool eof = false;
@@ -900,8 +909,9 @@ static int add_dir_entry(struct fat_dir* dir,
900 name, file->firstcluster); 909 name, file->firstcluster);
901 910
902 /* create dos name */ 911 /* create dos name */
903 if (create_dos_name(name, shortname) < 0) 912 rc = create_dos_name(name, shortname);
904 return -1; 913 if (rc < 0)
914 return rc * 10 - 0;
905 915
906 /* one dir entry needed for every 13 bytes of filename, 916 /* one dir entry needed for every 13 bytes of filename,
907 plus one entry for the short name */ 917 plus one entry for the short name */
@@ -912,39 +922,39 @@ static int add_dir_entry(struct fat_dir* dir,
912 restart: 922 restart:
913 firstentry = 0; 923 firstentry = 0;
914 924
915 err=fat_seek(&dir->file, 0); 925 rc = fat_seek(&dir->file, 0);
916 if (err<0) 926 if (rc < 0)
917 return -1; 927 return rc * 10 - 1;
918 928
919 for (sector=0; !done; sector++) 929 for (sector=0; !done; sector++)
920 { 930 {
921 unsigned int i; 931 unsigned int i;
922 932
923 err = 0; 933 rc = 0;
924 if (!eof) { 934 if (!eof) {
925 err = fat_readwrite(&dir->file, 1, buf, false); 935 rc = fat_readwrite(&dir->file, 1, buf, false);
926 } 936 }
927 if (err==0) { 937 if (rc == 0) {
928 /* eof: add new sector */ 938 /* eof: add new sector */
929 eof = true; 939 eof = true;
930 940
931 memset(buf, 0, sizeof buf); 941 memset(buf, 0, sizeof buf);
932 LDEBUGF("Adding new sector to dir\n"); 942 LDEBUGF("Adding new sector to dir\n");
933 err=fat_seek(&dir->file, sector); 943 rc = fat_seek(&dir->file, sector);
934 if (err<0) 944 if (rc < 0)
935 return -2; 945 return rc * 10 - 2;
936 946
937 /* add sectors (we must clear the whole cluster) */ 947 /* add sectors (we must clear the whole cluster) */
938 do { 948 do {
939 err = fat_readwrite(&dir->file, 1, buf, true); 949 rc = fat_readwrite(&dir->file, 1, buf, true);
940 if (err<1) 950 if (rc < 1)
941 return -3; 951 return rc * 10 - 3;
942 } while (dir->file.sectornum < (int)fat_bpb.bpb_secperclus); 952 } while (dir->file.sectornum < (int)fat_bpb.bpb_secperclus);
943 } 953 }
944 if (err<0) { 954 if (rc < 0) {
945 DEBUGF( "add_dir_entry() - Couldn't read dir" 955 DEBUGF( "add_dir_entry() - Couldn't read dir"
946 " (error code %d)\n", err); 956 " (error code %d)\n", rc);
947 return -4; 957 return rc * 10 - 4;
948 } 958 }
949 959
950 /* look for free slots */ 960 /* look for free slots */
@@ -999,10 +1009,10 @@ static int add_dir_entry(struct fat_dir* dir,
999 LDEBUGF("Adding longname to entry %d in sector %d\n", 1009 LDEBUGF("Adding longname to entry %d in sector %d\n",
1000 firstentry, sector); 1010 firstentry, sector);
1001 1011
1002 err = write_long_name(&dir->file, firstentry, 1012 rc = write_long_name(&dir->file, firstentry,
1003 entries_needed, name, shortname); 1013 entries_needed, name, shortname);
1004 if (err < 0) 1014 if (rc < 0)
1005 return -5; 1015 return rc * 10 - 5;
1006 1016
1007 /* remember where the shortname dir entry is located */ 1017 /* remember where the shortname dir entry is located */
1008 file->direntry = firstentry + entries_needed - 1; 1018 file->direntry = firstentry + entries_needed - 1;
@@ -1022,26 +1032,26 @@ static int add_dir_entry(struct fat_dir* dir,
1022 { 1032 {
1023 int idx = (lastentry % DIR_ENTRIES_PER_SECTOR) * DIR_ENTRY_SIZE; 1033 int idx = (lastentry % DIR_ENTRIES_PER_SECTOR) * DIR_ENTRY_SIZE;
1024 1034
1025 err=fat_seek(&dir->file, lastentry / DIR_ENTRIES_PER_SECTOR); 1035 rc = fat_seek(&dir->file, lastentry / DIR_ENTRIES_PER_SECTOR);
1026 if (err<0) 1036 if (rc < 0)
1027 return -6; 1037 return rc * 10 - 6;
1028 1038
1029 err = fat_readwrite(&dir->file, 1, buf, false); 1039 rc = fat_readwrite(&dir->file, 1, buf, false);
1030 if (err<1) 1040 if (rc < 1)
1031 return -7; 1041 return rc * 10 - 7;
1032 1042
1033 /* clear last entry */ 1043 /* clear last entry */
1034 buf[idx] = 0; 1044 buf[idx] = 0;
1035 1045
1036 err=fat_seek(&dir->file, lastentry / DIR_ENTRIES_PER_SECTOR); 1046 rc = fat_seek(&dir->file, lastentry / DIR_ENTRIES_PER_SECTOR);
1037 if (err<0) 1047 if (rc < 0)
1038 return -8; 1048 return rc * 10 - 8;
1039 1049
1040 /* we must clear entire last cluster */ 1050 /* we must clear entire last cluster */
1041 do { 1051 do {
1042 err = fat_readwrite(&dir->file, 1, buf, true); 1052 rc = fat_readwrite(&dir->file, 1, buf, true);
1043 if (err<1) 1053 if (rc < 1)
1044 return -9; 1054 return rc * 10 - 9;
1045 memset(buf, 0, sizeof buf); 1055 memset(buf, 0, sizeof buf);
1046 } while (dir->file.sectornum < (int)fat_bpb.bpb_secperclus); 1056 } while (dir->file.sectornum < (int)fat_bpb.bpb_secperclus);
1047 } 1057 }
@@ -1050,9 +1060,10 @@ static int add_dir_entry(struct fat_dir* dir,
1050 /* add a new sector/cluster for last entry */ 1060 /* add a new sector/cluster for last entry */
1051 memset(buf, 0, sizeof buf); 1061 memset(buf, 0, sizeof buf);
1052 do { 1062 do {
1053 err = fat_readwrite(&dir->file, 1, buf, true); 1063 rc = fat_readwrite(&dir->file, 1, buf, true);
1054 if (err<1) 1064 if (rc < 1)
1055 return -10; 1065 return rc * 10 - 9; /* Same error code as above, can't be
1066 more than -9 */
1056 } while (dir->file.sectornum < (int)fat_bpb.bpb_secperclus); 1067 } while (dir->file.sectornum < (int)fat_bpb.bpb_secperclus);
1057 } 1068 }
1058 } 1069 }
@@ -1127,23 +1138,23 @@ static int update_short_entry( struct fat_file* file, int size, int attr )
1127 unsigned int* sizeptr; 1138 unsigned int* sizeptr;
1128 unsigned short* clusptr; 1139 unsigned short* clusptr;
1129 struct fat_file dir; 1140 struct fat_file dir;
1130 int err; 1141 int rc;
1131 1142
1132 LDEBUGF("update_file_size(cluster:%x entry:%d size:%d)\n", 1143 LDEBUGF("update_file_size(cluster:%x entry:%d size:%d)\n",
1133 file->firstcluster, file->direntry, size); 1144 file->firstcluster, file->direntry, size);
1134 1145
1135 /* create a temporary file handle for the dir holding this file */ 1146 /* create a temporary file handle for the dir holding this file */
1136 err = fat_open(file->dircluster, &dir, NULL); 1147 rc = fat_open(file->dircluster, &dir, NULL);
1137 if (err<0) 1148 if (rc < 0)
1138 return -1; 1149 return rc * 10 - 1;
1139 1150
1140 err = fat_seek( &dir, sector ); 1151 rc = fat_seek( &dir, sector );
1141 if (err<0) 1152 if (rc<0)
1142 return -2; 1153 return rc * 10 - 2;
1143 1154
1144 err = fat_readwrite(&dir, 1, buf, false); 1155 rc = fat_readwrite(&dir, 1, buf, false);
1145 if (err<1) 1156 if (rc < 1)
1146 return -3; 1157 return rc * 10 - 3;
1147 1158
1148 if (!entry[0] || entry[0] == 0xe5) 1159 if (!entry[0] || entry[0] == 0xe5)
1149 panicf("Updating size on empty dir entry %d\n", file->direntry); 1160 panicf("Updating size on empty dir entry %d\n", file->direntry);
@@ -1169,13 +1180,13 @@ static int update_short_entry( struct fat_file* file, int size, int attr )
1169 } 1180 }
1170#endif 1181#endif
1171 1182
1172 err = fat_seek( &dir, sector ); 1183 rc = fat_seek( &dir, sector );
1173 if (err<0) 1184 if (rc < 0)
1174 return -4; 1185 return rc * 10 - 4;
1175 1186
1176 err = fat_readwrite(&dir, 1, buf, true); 1187 rc = fat_readwrite(&dir, 1, buf, true);
1177 if (err<1) 1188 if (rc < 1)
1178 return -5; 1189 return rc * 10 - 5;
1179 1190
1180 return 0; 1191 return 0;
1181} 1192}
@@ -1229,11 +1240,11 @@ int fat_create_file(char* name,
1229 struct fat_file* file, 1240 struct fat_file* file,
1230 struct fat_dir* dir) 1241 struct fat_dir* dir)
1231{ 1242{
1232 int err; 1243 int rc;
1233 1244
1234 LDEBUGF("fat_create_file(\"%s\",%x,%x)\n",name,file,dir); 1245 LDEBUGF("fat_create_file(\"%s\",%x,%x)\n",name,file,dir);
1235 err = add_dir_entry(dir, file, name); 1246 rc = add_dir_entry(dir, file, name);
1236 if (!err) { 1247 if (!rc) {
1237 file->firstcluster = 0; 1248 file->firstcluster = 0;
1238 file->lastcluster = 0; 1249 file->lastcluster = 0;
1239 file->lastsector = 0; 1250 file->lastsector = 0;
@@ -1241,7 +1252,7 @@ int fat_create_file(char* name,
1241 file->eof = false; 1252 file->eof = false;
1242 } 1253 }
1243 1254
1244 return err; 1255 return rc;
1245} 1256}
1246 1257
1247int fat_truncate(struct fat_file *file) 1258int fat_truncate(struct fat_file *file)
@@ -1309,20 +1320,20 @@ static int free_direntries(int dircluster, int startentry, int numentries)
1309 unsigned int entry = startentry - numentries + 1; 1320 unsigned int entry = startentry - numentries + 1;
1310 unsigned int sector = entry / DIR_ENTRIES_PER_SECTOR; 1321 unsigned int sector = entry / DIR_ENTRIES_PER_SECTOR;
1311 int i; 1322 int i;
1312 int err; 1323 int rc;
1313 1324
1314 /* create a temporary file handle for the dir holding this file */ 1325 /* create a temporary file handle for the dir holding this file */
1315 err = fat_open(dircluster, &dir, NULL); 1326 rc = fat_open(dircluster, &dir, NULL);
1316 if (err<0) 1327 if (rc < 0)
1317 return -1; 1328 return rc * 10 - 1;
1318 1329
1319 err = fat_seek( &dir, sector ); 1330 rc = fat_seek( &dir, sector );
1320 if (err<0) 1331 if (rc < 0)
1321 return -2; 1332 return rc * 10 - 2;
1322 1333
1323 err = fat_readwrite(&dir, 1, buf, false); 1334 rc = fat_readwrite(&dir, 1, buf, false);
1324 if (err<1) 1335 if (rc < 1)
1325 return -3; 1336 return rc * 10 - 3;
1326 1337
1327 for (i=0; i < numentries; i++) { 1338 for (i=0; i < numentries; i++) {
1328 LDEBUGF("Clearing dir entry %d (%d/%d)\n", 1339 LDEBUGF("Clearing dir entry %d (%d/%d)\n",
@@ -1332,19 +1343,19 @@ static int free_direntries(int dircluster, int startentry, int numentries)
1332 1343
1333 if ( (entry % DIR_ENTRIES_PER_SECTOR) == 0 ) { 1344 if ( (entry % DIR_ENTRIES_PER_SECTOR) == 0 ) {
1334 /* flush this sector */ 1345 /* flush this sector */
1335 err = fat_seek(&dir, sector); 1346 rc = fat_seek(&dir, sector);
1336 if (err<0) 1347 if (rc < 0)
1337 return -4; 1348 return rc * 10 - 4;
1338 1349
1339 err = fat_readwrite(&dir, 1, buf, true); 1350 rc = fat_readwrite(&dir, 1, buf, true);
1340 if (err<1) 1351 if (rc < 1)
1341 return -5; 1352 return rc * 10 - 5;
1342 1353
1343 if ( i+1 < numentries ) { 1354 if ( i+1 < numentries ) {
1344 /* read next sector */ 1355 /* read next sector */
1345 err = fat_readwrite(&dir, 1, buf, false); 1356 rc = fat_readwrite(&dir, 1, buf, false);
1346 if (err<1) 1357 if (rc < 1)
1347 return -6; 1358 return rc * 10 - 6;
1348 } 1359 }
1349 sector++; 1360 sector++;
1350 } 1361 }
@@ -1352,13 +1363,13 @@ static int free_direntries(int dircluster, int startentry, int numentries)
1352 1363
1353 if ( entry % DIR_ENTRIES_PER_SECTOR ) { 1364 if ( entry % DIR_ENTRIES_PER_SECTOR ) {
1354 /* flush this sector */ 1365 /* flush this sector */
1355 err = fat_seek(&dir, sector); 1366 rc = fat_seek(&dir, sector);
1356 if (err<0) 1367 if (rc < 0)
1357 return -7; 1368 return rc * 10 - 7;
1358 1369
1359 err = fat_readwrite(&dir, 1, buf, true); 1370 rc = fat_readwrite(&dir, 1, buf, true);
1360 if (err<1) 1371 if (rc < 1)
1361 return -8; 1372 return rc * 10 - 8;
1362 } 1373 }
1363 1374
1364 return 0; 1375 return 0;
@@ -1367,6 +1378,7 @@ static int free_direntries(int dircluster, int startentry, int numentries)
1367int fat_remove(struct fat_file* file) 1378int fat_remove(struct fat_file* file)
1368{ 1379{
1369 int next, last = file->firstcluster; 1380 int next, last = file->firstcluster;
1381 int rc;
1370 1382
1371 LDEBUGF("fat_remove(%x)\n",last); 1383 LDEBUGF("fat_remove(%x)\n",last);
1372 1384
@@ -1376,11 +1388,13 @@ int fat_remove(struct fat_file* file)
1376 last = next; 1388 last = next;
1377 } 1389 }
1378 1390
1379 if ( file->dircluster ) 1391 if ( file->dircluster ) {
1380 if ( free_direntries(file->dircluster, 1392 rc = free_direntries(file->dircluster,
1381 file->direntry, 1393 file->direntry,
1382 file->direntries) < 0 ) 1394 file->direntries);
1383 return -1; 1395 if (rc < 0)
1396 return rc * 10 - 1;
1397 }
1384 1398
1385 file->firstcluster = 0; 1399 file->firstcluster = 0;
1386 file->dircluster = 0; 1400 file->dircluster = 0;
@@ -1393,7 +1407,7 @@ int fat_rename(struct fat_file* file,
1393 int size, 1407 int size,
1394 int attr) 1408 int attr)
1395{ 1409{
1396 int err; 1410 int rc;
1397 struct fat_dir dir; 1411 struct fat_dir dir;
1398 struct fat_file newfile = *file; 1412 struct fat_file newfile = *file;
1399 1413
@@ -1403,28 +1417,28 @@ int fat_rename(struct fat_file* file,
1403 } 1417 }
1404 1418
1405 /* create a temporary file handle */ 1419 /* create a temporary file handle */
1406 err = fat_opendir(&dir, file->dircluster); 1420 rc = fat_opendir(&dir, file->dircluster);
1407 if (err<0) 1421 if (rc < 0)
1408 return -2; 1422 return rc * 10 - 2;
1409 1423
1410 /* create new name */ 1424 /* create new name */
1411 err = add_dir_entry(&dir, &newfile, newname); 1425 rc = add_dir_entry(&dir, &newfile, newname);
1412 if (err<0) 1426 if (rc < 0)
1413 return -3; 1427 return rc * 10 - 3;
1414 1428
1415 /* write size and cluster link */ 1429 /* write size and cluster link */
1416 err = update_short_entry(&newfile, size, attr); 1430 rc = update_short_entry(&newfile, size, attr);
1417 if (err<0) 1431 if (rc < 0)
1418 return -4; 1432 return rc * 10 - 4;
1419 1433
1420 /* remove old name */ 1434 /* remove old name */
1421 err = free_direntries(file->dircluster, file->direntry, file->direntries); 1435 rc = free_direntries(file->dircluster, file->direntry, file->direntries);
1422 if (err<0) 1436 if (rc < 0)
1423 return -5; 1437 return rc * 10 - 5;
1424 1438
1425 err = flush_fat(); 1439 rc = flush_fat();
1426 if (err<0) 1440 if (rc < 0)
1427 return -6; 1441 return rc * 10 - 6;
1428 1442
1429 return 0; 1443 return 0;
1430} 1444}
@@ -1475,20 +1489,20 @@ static int next_write_cluster(struct fat_file* file,
1475 1489
1476static bool transfer( unsigned int start, int count, char* buf, bool write ) 1490static bool transfer( unsigned int start, int count, char* buf, bool write )
1477{ 1491{
1478 int err; 1492 int rc;
1479 1493
1480 LDEBUGF("transfer(s=%x, c=%x, %s)\n",start, count, write?"write":"read"); 1494 LDEBUGF("transfer(s=%x, c=%x, %s)\n",start, count, write?"write":"read");
1481 if (write) { 1495 if (write) {
1482 if (start < fat_bpb.firstdatasector) 1496 if (start < fat_bpb.firstdatasector)
1483 panicf("Writing before data\n"); 1497 panicf("Writing before data\n");
1484 err = ata_write_sectors(start, count, buf); 1498 rc = ata_write_sectors(start, count, buf);
1485 } 1499 }
1486 else 1500 else
1487 err = ata_read_sectors(start, count, buf); 1501 rc = ata_read_sectors(start, count, buf);
1488 if (err) { 1502 if (rc < 0) {
1489 DEBUGF( "transfer() - Couldn't %s sector %x" 1503 DEBUGF( "transfer() - Couldn't %s sector %x"
1490 " (error code %d)\n", 1504 " (error code %d)\n",
1491 write ? "write":"read", start, err); 1505 write ? "write":"read", start, rc);
1492 return false; 1506 return false;
1493 } 1507 }
1494 return true; 1508 return true;
@@ -1553,7 +1567,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
1553 (last-first+1 == 256) ) { /* max 256 sectors per ata request */ 1567 (last-first+1 == 256) ) { /* max 256 sectors per ata request */
1554 int count = last - first + 1; 1568 int count = last - first + 1;
1555 if (!transfer( first + fat_bpb.startsector, count, buf, write )) 1569 if (!transfer( first + fat_bpb.startsector, count, buf, write ))
1556 return -2; 1570 return -1;
1557 1571
1558 ((char*)buf) += count * SECTOR_SIZE; 1572 ((char*)buf) += count * SECTOR_SIZE;
1559 first = sector; 1573 first = sector;
@@ -1624,17 +1638,17 @@ int fat_seek(struct fat_file *file, unsigned int seeksector )
1624 1638
1625int fat_opendir(struct fat_dir *dir, unsigned int startcluster) 1639int fat_opendir(struct fat_dir *dir, unsigned int startcluster)
1626{ 1640{
1627 int err; 1641 int rc;
1628 1642
1629 if (startcluster == 0) 1643 if (startcluster == 0)
1630 startcluster = sec2cluster(fat_bpb.rootdirsector); 1644 startcluster = sec2cluster(fat_bpb.rootdirsector);
1631 1645
1632 err = fat_open(startcluster, &dir->file, NULL); 1646 rc = fat_open(startcluster, &dir->file, NULL);
1633 if(err) 1647 if(rc)
1634 { 1648 {
1635 DEBUGF( "fat_opendir() - Couldn't open dir" 1649 DEBUGF( "fat_opendir() - Couldn't open dir"
1636 " (error code %d)\n", err); 1650 " (error code %d)\n", rc);
1637 return -1; 1651 return rc * 10 - 1;
1638 } 1652 }
1639 1653
1640 dir->entry = 0; 1654 dir->entry = 0;
@@ -1686,7 +1700,7 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
1686{ 1700{
1687 bool done = false; 1701 bool done = false;
1688 int i; 1702 int i;
1689 int err; 1703 int rc;
1690 unsigned char firstbyte; 1704 unsigned char firstbyte;
1691 int longarray[20]; 1705 int longarray[20];
1692 int longs=0; 1706 int longs=0;
@@ -1699,16 +1713,16 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
1699 { 1713 {
1700 if ( !(dir->entry % DIR_ENTRIES_PER_SECTOR) || !dir->sector ) 1714 if ( !(dir->entry % DIR_ENTRIES_PER_SECTOR) || !dir->sector )
1701 { 1715 {
1702 err = fat_readwrite(&dir->file, 1, cached_buf, false); 1716 rc = fat_readwrite(&dir->file, 1, cached_buf, false);
1703 if (err==0) { 1717 if (rc == 0) {
1704 /* eof */ 1718 /* eof */
1705 entry->name[0] = 0; 1719 entry->name[0] = 0;
1706 break; 1720 break;
1707 } 1721 }
1708 if (err<0) { 1722 if (rc < 0) {
1709 DEBUGF( "fat_getnext() - Couldn't read dir" 1723 DEBUGF( "fat_getnext() - Couldn't read dir"
1710 " (error code %d)\n", err); 1724 " (error code %d)\n", rc);
1711 return -1; 1725 return rc * 10 - 1;
1712 } 1726 }
1713 dir->sector = dir->file.lastsector; 1727 dir->sector = dir->file.lastsector;
1714 } 1728 }