diff options
Diffstat (limited to 'firmware/drivers/fat.c')
-rw-r--r-- | firmware/drivers/fat.c | 92 |
1 files changed, 41 insertions, 51 deletions
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index 35e79789b0..6192d4619f 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c | |||
@@ -27,6 +27,7 @@ | |||
27 | 27 | ||
28 | #include "fat.h" | 28 | #include "fat.h" |
29 | #include "ata.h" | 29 | #include "ata.h" |
30 | #include "debug.h" | ||
30 | 31 | ||
31 | #define BYTES2INT16(array,pos) \ | 32 | #define BYTES2INT16(array,pos) \ |
32 | (array[pos] | (array[pos+1] << 8 )) | 33 | (array[pos] | (array[pos+1] << 8 )) |
@@ -134,12 +135,6 @@ static unsigned char lastsector2[SECTOR_SIZE]; | |||
134 | 135 | ||
135 | #ifdef TEST_FAT | 136 | #ifdef TEST_FAT |
136 | 137 | ||
137 | #include "debug.h" | ||
138 | #define DEBUG(x) printf(x) | ||
139 | #define DEBUG1(x,y) printf(x,y) | ||
140 | #define DEBUG2(x,y1,y2) printf(x,y1,y2) | ||
141 | #define DEBUG3(x,y1,y2,y3) printf(x,y1,y2,y3) | ||
142 | |||
143 | int main(int argc, char *argv[]) | 138 | int main(int argc, char *argv[]) |
144 | { | 139 | { |
145 | struct bpb bpb; | 140 | struct bpb bpb; |
@@ -148,25 +143,20 @@ int main(int argc, char *argv[]) | |||
148 | memset(fat_cache_dirty, 0, sizeof(fat_cache_dirty)); | 143 | memset(fat_cache_dirty, 0, sizeof(fat_cache_dirty)); |
149 | 144 | ||
150 | if(ata_init()) | 145 | if(ata_init()) |
151 | DEBUG("*** Warning! The disk is uninitialized\n"); | 146 | DEBUGF("*** Warning! The disk is uninitialized\n"); |
152 | else | 147 | else |
153 | fat_mount(&bpb); | 148 | fat_mount(&bpb); |
154 | 149 | ||
155 | dbg_console(&bpb); | 150 | dbg_console(&bpb); |
156 | return 0; | 151 | return 0; |
157 | } | 152 | } |
158 | #else | ||
159 | #define DEBUG(x); | ||
160 | #define DEBUG1(x,y); | ||
161 | #define DEBUG2(x,y1,y2); | ||
162 | #define DEBUG3(x,y1,y2,y3); | ||
163 | #endif | 153 | #endif |
164 | 154 | ||
165 | static int sec2cluster(struct bpb *bpb, unsigned int sec) | 155 | static int sec2cluster(struct bpb *bpb, unsigned int sec) |
166 | { | 156 | { |
167 | if ( sec < bpb->firstdatasector ) | 157 | if ( sec < bpb->firstdatasector ) |
168 | { | 158 | { |
169 | DEBUG1( "sec2cluster() - Bad sector number (%d)\n", sec); | 159 | DEBUGF( "sec2cluster() - Bad sector number (%d)\n", sec); |
170 | return -1; | 160 | return -1; |
171 | } | 161 | } |
172 | 162 | ||
@@ -180,7 +170,7 @@ static int cluster2sec(struct bpb *bpb, unsigned int cluster) | |||
180 | 170 | ||
181 | if(cluster > max_cluster) | 171 | if(cluster > max_cluster) |
182 | { | 172 | { |
183 | DEBUG1( "cluster2sec() - Bad cluster number (%d)\n", | 173 | DEBUGF( "cluster2sec() - Bad cluster number (%d)\n", |
184 | cluster); | 174 | cluster); |
185 | return -1; | 175 | return -1; |
186 | } | 176 | } |
@@ -204,7 +194,7 @@ int fat_mount(struct bpb *bpb) | |||
204 | err = ata_read_sectors(0,1,buf); | 194 | err = ata_read_sectors(0,1,buf); |
205 | if(err) | 195 | if(err) |
206 | { | 196 | { |
207 | DEBUG1( "fat_mount() - Couldn't read BPB (error code %i)\n", | 197 | DEBUGF( "fat_mount() - Couldn't read BPB (error code %i)\n", |
208 | err); | 198 | err); |
209 | return -1; | 199 | return -1; |
210 | } | 200 | } |
@@ -256,7 +246,7 @@ int fat_mount(struct bpb *bpb) | |||
256 | if ( countofclusters < 65525 ) | 246 | if ( countofclusters < 65525 ) |
257 | #endif | 247 | #endif |
258 | { | 248 | { |
259 | DEBUG("This is not FAT32. Go away!\n"); | 249 | DEBUGF("This is not FAT32. Go away!\n"); |
260 | return -1; | 250 | return -1; |
261 | } | 251 | } |
262 | 252 | ||
@@ -277,7 +267,7 @@ int fat_mount(struct bpb *bpb) | |||
277 | 267 | ||
278 | if (bpb_is_sane(bpb) < 0) | 268 | if (bpb_is_sane(bpb) < 0) |
279 | { | 269 | { |
280 | DEBUG( "fat_mount() - BPB is not sane\n"); | 270 | DEBUGF( "fat_mount() - BPB is not sane\n"); |
281 | return -1; | 271 | return -1; |
282 | } | 272 | } |
283 | 273 | ||
@@ -290,55 +280,55 @@ static int bpb_is_sane(struct bpb *bpb) | |||
290 | { | 280 | { |
291 | if(bpb->bpb_bytspersec != 512) | 281 | if(bpb->bpb_bytspersec != 512) |
292 | { | 282 | { |
293 | DEBUG1( "bpb_is_sane() - Error: sector size is not 512 (%i)\n", | 283 | DEBUGF( "bpb_is_sane() - Error: sector size is not 512 (%i)\n", |
294 | bpb->bpb_bytspersec); | 284 | bpb->bpb_bytspersec); |
295 | return -1; | 285 | return -1; |
296 | } | 286 | } |
297 | if(bpb->bpb_secperclus * bpb->bpb_bytspersec > 32768) | 287 | if(bpb->bpb_secperclus * bpb->bpb_bytspersec > 32768) |
298 | { | 288 | { |
299 | DEBUG3( "bpb_is_sane() - Warning: cluster size is larger than 32K " | 289 | DEBUGF( "bpb_is_sane() - Warning: cluster size is larger than 32K " |
300 | "(%i * %i = %i)\n", | 290 | "(%i * %i = %i)\n", |
301 | bpb->bpb_bytspersec, bpb->bpb_secperclus, | 291 | bpb->bpb_bytspersec, bpb->bpb_secperclus, |
302 | bpb->bpb_bytspersec * bpb->bpb_secperclus); | 292 | bpb->bpb_bytspersec * bpb->bpb_secperclus); |
303 | } | 293 | } |
304 | if(bpb->bpb_rsvdseccnt != 1) | 294 | if(bpb->bpb_rsvdseccnt != 1) |
305 | { | 295 | { |
306 | DEBUG1( "bpb_is_sane() - Warning: Reserved sectors is not 1 (%i)\n", | 296 | DEBUGF( "bpb_is_sane() - Warning: Reserved sectors is not 1 (%i)\n", |
307 | bpb->bpb_rsvdseccnt); | 297 | bpb->bpb_rsvdseccnt); |
308 | } | 298 | } |
309 | if(bpb->bpb_numfats != 2) | 299 | if(bpb->bpb_numfats != 2) |
310 | { | 300 | { |
311 | DEBUG1( "bpb_is_sane() - Warning: NumFATS is not 2 (%i)\n", | 301 | DEBUGF( "bpb_is_sane() - Warning: NumFATS is not 2 (%i)\n", |
312 | bpb->bpb_numfats); | 302 | bpb->bpb_numfats); |
313 | } | 303 | } |
314 | if(bpb->bpb_rootentcnt != 512) | 304 | if(bpb->bpb_rootentcnt != 512) |
315 | { | 305 | { |
316 | DEBUG1( "bpb_is_sane() - Warning: RootEntCnt is not 512 (%i)\n", | 306 | DEBUGF( "bpb_is_sane() - Warning: RootEntCnt is not 512 (%i)\n", |
317 | bpb->bpb_rootentcnt); | 307 | bpb->bpb_rootentcnt); |
318 | } | 308 | } |
319 | if(bpb->bpb_totsec16 < 200) | 309 | if(bpb->bpb_totsec16 < 200) |
320 | { | 310 | { |
321 | if(bpb->bpb_totsec16 == 0) | 311 | if(bpb->bpb_totsec16 == 0) |
322 | { | 312 | { |
323 | DEBUG( "bpb_is_sane() - Error: TotSec16 is 0\n"); | 313 | DEBUGF( "bpb_is_sane() - Error: TotSec16 is 0\n"); |
324 | return -1; | 314 | return -1; |
325 | } | 315 | } |
326 | else | 316 | else |
327 | { | 317 | { |
328 | DEBUG1( "bpb_is_sane() - Warning: TotSec16 " | 318 | DEBUGF( "bpb_is_sane() - Warning: TotSec16 " |
329 | "is quite small (%i)\n", | 319 | "is quite small (%i)\n", |
330 | bpb->bpb_totsec16); | 320 | bpb->bpb_totsec16); |
331 | } | 321 | } |
332 | } | 322 | } |
333 | if(bpb->bpb_media != 0xf0 && bpb->bpb_media < 0xf8) | 323 | if(bpb->bpb_media != 0xf0 && bpb->bpb_media < 0xf8) |
334 | { | 324 | { |
335 | DEBUG1( "bpb_is_sane() - Warning: Non-standard " | 325 | DEBUGF( "bpb_is_sane() - Warning: Non-standard " |
336 | "media type (0x%02x)\n", | 326 | "media type (0x%02x)\n", |
337 | bpb->bpb_media); | 327 | bpb->bpb_media); |
338 | } | 328 | } |
339 | if(bpb->last_word != 0xaa55) | 329 | if(bpb->last_word != 0xaa55) |
340 | { | 330 | { |
341 | DEBUG1( "bpb_is_sane() - Error: Last word is not " | 331 | DEBUGF( "bpb_is_sane() - Error: Last word is not " |
342 | "0xaa55 (0x%04x)\n", bpb->last_word); | 332 | "0xaa55 (0x%04x)\n", bpb->last_word); |
343 | return -1; | 333 | return -1; |
344 | } | 334 | } |
@@ -356,12 +346,12 @@ static void *cache_fat_sector(struct bpb *bpb, int secnum) | |||
356 | sec = malloc(bpb->bpb_bytspersec); | 346 | sec = malloc(bpb->bpb_bytspersec); |
357 | if(!sec) | 347 | if(!sec) |
358 | { | 348 | { |
359 | DEBUG( "cache_fat_sector() - Out of memory\n"); | 349 | DEBUGF( "cache_fat_sector() - Out of memory\n"); |
360 | return NULL; | 350 | return NULL; |
361 | } | 351 | } |
362 | if(ata_read_sectors(secnum,1,sec)) | 352 | if(ata_read_sectors(secnum,1,sec)) |
363 | { | 353 | { |
364 | DEBUG1( "cache_fat_sector() - Could" | 354 | DEBUGF( "cache_fat_sector() - Could" |
365 | " not read sector %d\n", | 355 | " not read sector %d\n", |
366 | secnum); | 356 | secnum); |
367 | free(sec); | 357 | free(sec); |
@@ -388,7 +378,7 @@ static int update_entry(struct bpb *bpb, int entry, unsigned int val) | |||
388 | sec = cache_fat_sector(bpb, thisfatsecnum); | 378 | sec = cache_fat_sector(bpb, thisfatsecnum); |
389 | if(!sec) | 379 | if(!sec) |
390 | { | 380 | { |
391 | DEBUG1( "update_entry() - Could not cache sector %d\n", | 381 | DEBUGF( "update_entry() - Could not cache sector %d\n", |
392 | thisfatsecnum); | 382 | thisfatsecnum); |
393 | return -1; | 383 | return -1; |
394 | } | 384 | } |
@@ -419,7 +409,7 @@ static int read_entry(struct bpb *bpb, int entry) | |||
419 | sec = cache_fat_sector(bpb, thisfatsecnum); | 409 | sec = cache_fat_sector(bpb, thisfatsecnum); |
420 | if(!sec) | 410 | if(!sec) |
421 | { | 411 | { |
422 | DEBUG1( "update_entry() - Could not cache sector %d\n", | 412 | DEBUGF( "update_entry() - Could not cache sector %d\n", |
423 | thisfatsecnum); | 413 | thisfatsecnum); |
424 | return -1; | 414 | return -1; |
425 | } | 415 | } |
@@ -455,19 +445,19 @@ static int flush_fat(struct bpb *bpb) | |||
455 | { | 445 | { |
456 | if(fat_cache[i] && fat_cache_dirty[i]) | 446 | if(fat_cache[i] && fat_cache_dirty[i]) |
457 | { | 447 | { |
458 | DEBUG1("Flushing FAT sector %d\n", i); | 448 | DEBUGF("Flushing FAT sector %d\n", i); |
459 | sec = fat_cache[i]; | 449 | sec = fat_cache[i]; |
460 | err = ata_write_sectors(i + bpb->bpb_rsvdseccnt,1,sec); | 450 | err = ata_write_sectors(i + bpb->bpb_rsvdseccnt,1,sec); |
461 | if(err) | 451 | if(err) |
462 | { | 452 | { |
463 | DEBUG1( "flush_fat() - Couldn't write" | 453 | DEBUGF( "flush_fat() - Couldn't write" |
464 | " sector (%d)\n", i + bpb->bpb_rsvdseccnt); | 454 | " sector (%d)\n", i + bpb->bpb_rsvdseccnt); |
465 | return -1; | 455 | return -1; |
466 | } | 456 | } |
467 | err = ata_write_sectors(i + bpb->bpb_rsvdseccnt + fatsz,1,sec); | 457 | err = ata_write_sectors(i + bpb->bpb_rsvdseccnt + fatsz,1,sec); |
468 | if(err) | 458 | if(err) |
469 | { | 459 | { |
470 | DEBUG1( "flush_fat() - Couldn't write" | 460 | DEBUGF( "flush_fat() - Couldn't write" |
471 | " sector (%d)\n", i + bpb->bpb_rsvdseccnt + fatsz); | 461 | " sector (%d)\n", i + bpb->bpb_rsvdseccnt + fatsz); |
472 | return -1; | 462 | return -1; |
473 | } | 463 | } |
@@ -543,7 +533,7 @@ static int add_dir_entry(struct bpb *bpb, | |||
543 | } | 533 | } |
544 | else | 534 | else |
545 | { | 535 | { |
546 | DEBUG( "add_dir_entry() -" | 536 | DEBUGF( "add_dir_entry() -" |
547 | " Root dir is full\n"); | 537 | " Root dir is full\n"); |
548 | return -1; | 538 | return -1; |
549 | } | 539 | } |
@@ -554,9 +544,9 @@ static int add_dir_entry(struct bpb *bpb, | |||
554 | if(sec_cnt >= bpb->bpb_secperclus) | 544 | if(sec_cnt >= bpb->bpb_secperclus) |
555 | { | 545 | { |
556 | /* We have reached the end of this cluster */ | 546 | /* We have reached the end of this cluster */ |
557 | DEBUG("Moving to the next cluster..."); | 547 | DEBUGF("Moving to the next cluster..."); |
558 | currdir = get_next_cluster(bpb, currdir); | 548 | currdir = get_next_cluster(bpb, currdir); |
559 | DEBUG1("new cluster is %d\n", currdir); | 549 | DEBUGF("new cluster is %d\n", currdir); |
560 | 550 | ||
561 | if(!currdir) | 551 | if(!currdir) |
562 | { | 552 | { |
@@ -567,12 +557,12 @@ static int add_dir_entry(struct bpb *bpb, | |||
567 | } | 557 | } |
568 | } | 558 | } |
569 | 559 | ||
570 | DEBUG1("Reading sector %d...\n", sec); | 560 | DEBUGF("Reading sector %d...\n", sec); |
571 | /* Read the next sector in the current dir */ | 561 | /* Read the next sector in the current dir */ |
572 | err = ata_read_sectors(sec,1,buf); | 562 | err = ata_read_sectors(sec,1,buf); |
573 | if(err) | 563 | if(err) |
574 | { | 564 | { |
575 | DEBUG1( "add_dir_entry() - Couldn't read dir sector" | 565 | DEBUGF( "add_dir_entry() - Couldn't read dir sector" |
576 | " (error code %i)\n", err); | 566 | " (error code %i)\n", err); |
577 | return -1; | 567 | return -1; |
578 | } | 568 | } |
@@ -580,7 +570,7 @@ static int add_dir_entry(struct bpb *bpb, | |||
580 | if(need_to_update_last_empty_marker) | 570 | if(need_to_update_last_empty_marker) |
581 | { | 571 | { |
582 | /* All we need to do is to set the first entry to 0 */ | 572 | /* All we need to do is to set the first entry to 0 */ |
583 | DEBUG1("Clearing the first entry in sector %d\n", sec); | 573 | DEBUGF("Clearing the first entry in sector %d\n", sec); |
584 | buf[0] = 0; | 574 | buf[0] = 0; |
585 | done = 1; | 575 | done = 1; |
586 | } | 576 | } |
@@ -592,7 +582,7 @@ static int add_dir_entry(struct bpb *bpb, | |||
592 | firstbyte = buf[i]; | 582 | firstbyte = buf[i]; |
593 | if(firstbyte == 0xe5 || firstbyte == 0) | 583 | if(firstbyte == 0xe5 || firstbyte == 0) |
594 | { | 584 | { |
595 | DEBUG2("Found free slot at entry %d in sector %d\n", | 585 | DEBUGF("Found free slot at entry %d in sector %d\n", |
596 | i/32, sec); | 586 | i/32, sec); |
597 | eptr = &buf[i]; | 587 | eptr = &buf[i]; |
598 | memset(eptr, 0, 32); | 588 | memset(eptr, 0, 32); |
@@ -637,7 +627,7 @@ static int add_dir_entry(struct bpb *bpb, | |||
637 | err = ata_write_sectors(sec,1,buf); | 627 | err = ata_write_sectors(sec,1,buf); |
638 | if(err) | 628 | if(err) |
639 | { | 629 | { |
640 | DEBUG1( "add_dir_entry() - " | 630 | DEBUGF( "add_dir_entry() - " |
641 | " Couldn't write dir" | 631 | " Couldn't write dir" |
642 | " sector (error code %i)\n", err); | 632 | " sector (error code %i)\n", err); |
643 | return -1; | 633 | return -1; |
@@ -752,11 +742,11 @@ int fat_create_dir(struct bpb *bpb, unsigned int currdir, char *name) | |||
752 | struct fat_direntry de; | 742 | struct fat_direntry de; |
753 | int err; | 743 | int err; |
754 | 744 | ||
755 | DEBUG("fat_create_file()\n"); | 745 | DEBUGF("fat_create_file()\n"); |
756 | memset(&de, 0, sizeof(struct fat_direntry)); | 746 | memset(&de, 0, sizeof(struct fat_direntry)); |
757 | if(create_dos_name(name, de.name) < 0) | 747 | if(create_dos_name(name, de.name) < 0) |
758 | { | 748 | { |
759 | DEBUG1( "fat_create_file() - Illegal file name (%s)\n", name); | 749 | DEBUGF( "fat_create_file() - Illegal file name (%s)\n", name); |
760 | return -1; | 750 | return -1; |
761 | } | 751 | } |
762 | 752 | ||
@@ -775,11 +765,11 @@ int fat_create_file(struct bpb *bpb, unsigned int currdir, char *name) | |||
775 | struct fat_direntry de; | 765 | struct fat_direntry de; |
776 | int err; | 766 | int err; |
777 | 767 | ||
778 | DEBUG("fat_create_file()\n"); | 768 | DEBUGF("fat_create_file()\n"); |
779 | memset(&de, 0, sizeof(struct fat_direntry)); | 769 | memset(&de, 0, sizeof(struct fat_direntry)); |
780 | if(create_dos_name(name, de.name) < 0) | 770 | if(create_dos_name(name, de.name) < 0) |
781 | { | 771 | { |
782 | DEBUG1( "fat_create_file() - Illegal file name (%s)\n", name); | 772 | DEBUGF( "fat_create_file() - Illegal file name (%s)\n", name); |
783 | return -1; | 773 | return -1; |
784 | } | 774 | } |
785 | getcurrdostime(&de.crtdate, &de.crttime, &de.crttimetenth); | 775 | getcurrdostime(&de.crtdate, &de.crttime, &de.crttimetenth); |
@@ -833,7 +823,7 @@ int fat_read(struct bpb *bpb, | |||
833 | for ( i=0; i<sectorcount; i++ ) { | 823 | for ( i=0; i<sectorcount; i++ ) { |
834 | err = ata_read_sectors(sector,1,(char*)buf+(i*SECTOR_SIZE)); | 824 | err = ata_read_sectors(sector,1,(char*)buf+(i*SECTOR_SIZE)); |
835 | if(err) { | 825 | if(err) { |
836 | DEBUG2( "fat_read() - Couldn't read sector %d" | 826 | DEBUGF( "fat_read() - Couldn't read sector %d" |
837 | " (error code %i)\n", sector,err); | 827 | " (error code %i)\n", sector,err); |
838 | return -1; | 828 | return -1; |
839 | } | 829 | } |
@@ -911,7 +901,7 @@ int fat_opendir(struct bpb *bpb, | |||
911 | err = ata_read_sectors(sec,1,ent->cached_buf); | 901 | err = ata_read_sectors(sec,1,ent->cached_buf); |
912 | if(err) | 902 | if(err) |
913 | { | 903 | { |
914 | DEBUG1( "fat_getfirst() - Couldn't read dir sector" | 904 | DEBUGF( "fat_getfirst() - Couldn't read dir sector" |
915 | " (error code %i)\n", err); | 905 | " (error code %i)\n", err); |
916 | return -1; | 906 | return -1; |
917 | } | 907 | } |
@@ -1027,20 +1017,20 @@ int fat_getnext(struct bpb *bpb, | |||
1027 | { | 1017 | { |
1028 | int cluster = sec2cluster(bpb, ent->cached_sec); | 1018 | int cluster = sec2cluster(bpb, ent->cached_sec); |
1029 | if ( cluster < 0 ) { | 1019 | if ( cluster < 0 ) { |
1030 | DEBUG("sec2cluster failed\n"); | 1020 | DEBUGF("sec2cluster failed\n"); |
1031 | return -1; | 1021 | return -1; |
1032 | } | 1022 | } |
1033 | ent->num_sec = 0; | 1023 | ent->num_sec = 0; |
1034 | cluster = get_next_cluster( bpb, cluster ); | 1024 | cluster = get_next_cluster( bpb, cluster ); |
1035 | if(!cluster) | 1025 | if(!cluster) |
1036 | { | 1026 | { |
1037 | DEBUG("End of cluster chain.\n"); | 1027 | DEBUGF("End of cluster chain.\n"); |
1038 | return -1; | 1028 | return -1; |
1039 | } | 1029 | } |
1040 | ent->cached_sec = cluster2sec(bpb,cluster); | 1030 | ent->cached_sec = cluster2sec(bpb,cluster); |
1041 | if ( ent->cached_sec < 0 ) | 1031 | if ( ent->cached_sec < 0 ) |
1042 | { | 1032 | { |
1043 | DEBUG1("Invalid cluster: %d\n",cluster); | 1033 | DEBUGF("Invalid cluster: %d\n",cluster); |
1044 | return -1; | 1034 | return -1; |
1045 | } | 1035 | } |
1046 | 1036 | ||
@@ -1050,7 +1040,7 @@ int fat_getnext(struct bpb *bpb, | |||
1050 | err = ata_read_sectors(ent->cached_sec,1,ent->cached_buf); | 1040 | err = ata_read_sectors(ent->cached_sec,1,ent->cached_buf); |
1051 | if(err) | 1041 | if(err) |
1052 | { | 1042 | { |
1053 | DEBUG1( "fat_getnext() - Couldn't read dir sector" | 1043 | DEBUGF( "fat_getnext() - Couldn't read dir sector" |
1054 | " (error code %i)\n", err); | 1044 | " (error code %i)\n", err); |
1055 | return -1; | 1045 | return -1; |
1056 | } | 1046 | } |