| Index: embios/trunk/storage_nand.h | 
| — | — | @@ -41,7 +41,6 @@ | 
| 42 | 42 | int  nand_flush(void); | 
| 43 | 43 | #endif | 
| 44 | 44 | void nand_spin(void); | 
| 45 |  | -int  nand_spinup_time(void); /* ticks */
 | 
| 46 | 45 |  | 
| 47 | 46 | #ifdef STORAGE_GET_INFO | 
| 48 | 47 | void nand_get_info(IF_MD2(int drive,) struct storage_info *info); | 
| Index: embios/trunk/disk.h | 
| — | — | @@ -21,6 +21,7 @@ | 
| 22 | 22 | #ifndef _DISK_H_ | 
| 23 | 23 | #define _DISK_H_ | 
| 24 | 24 |  | 
|  | 25 | +#include "global.h" | 
| 25 | 26 | #include "mv.h" /* for volume definitions */ | 
| 26 | 27 |  | 
| 27 | 28 | struct partinfo { | 
| — | — | @@ -43,9 +44,4 @@ | 
| 44 | 45 | int disk_mount(int drive); | 
| 45 | 46 | int disk_unmount(int drive); | 
| 46 | 47 |  | 
| 47 |  | -/* The number of 512-byte sectors in a "logical" sector. Needed for ipod 5.5G */ | 
| 48 |  | -#ifdef MAX_LOG_SECTOR_SIZE | 
| 49 |  | -extern int disk_sector_multiplier; | 
| 50 | 48 | #endif | 
| 51 |  | - | 
| 52 |  | -#endif | 
| Index: embios/trunk/storage.c | 
| — | — | @@ -34,62 +34,9 @@ | 
| 35 | 35 | #endif | 
| 36 | 36 |  | 
| 37 | 37 |  | 
| 38 |  | -#ifdef HAVE_IO_PRIORITY | 
| 39 |  | - | 
| 40 |  | -/* Same for flash? */ | 
| 41 |  | -#define STORAGE_MINIMUM_IDLE_TIME (HZ/10) | 
| 42 |  | -#define STORAGE_DELAY_UNIT  (HZ/20) | 
| 43 |  | - | 
| 44 |  | -static unsigned int storage_last_thread[NUM_DRIVES]; | 
| 45 |  | -static unsigned int storage_last_activity[NUM_DRIVES]; | 
| 46 |  | - | 
| 47 |  | -static bool storage_should_wait(int drive, int prio) | 
| 48 |  | -{ | 
| 49 |  | -    int other_prio = thread_get_io_priority(storage_last_thread[drive]); | 
| 50 |  | -    if(TIME_BEFORE(current_tick,storage_last_activity[drive]+STORAGE_MINIMUM_IDLE_TIME)) | 
| 51 |  | -    { | 
| 52 |  | -        if(prio<=other_prio) | 
| 53 |  | -        { | 
| 54 |  | -            /* There is another active thread, but we have lower priority */ | 
| 55 |  | -            return false; | 
| 56 |  | -        } | 
| 57 |  | -        else | 
| 58 |  | -        { | 
| 59 |  | -            /* There is another active thread, but it has lower priority */ | 
| 60 |  | -            return true; | 
| 61 |  | -        } | 
| 62 |  | -    } | 
| 63 |  | -    else | 
| 64 |  | -    { | 
| 65 |  | -        /* There's nothing going on anyway */ | 
| 66 |  | -        return false; | 
| 67 |  | -    } | 
| 68 |  | -} | 
| 69 |  | - | 
| 70 |  | -static void storage_wait_turn(IF_MD_NONVOID(int drive)) | 
| 71 |  | -{ | 
| 72 |  | -#ifndef HAVE_MULTIDRIVE | 
| 73 |  | -    int drive=0; | 
| 74 |  | -#endif | 
| 75 |  | -    int my_prio = thread_get_io_priority(THREAD_ID_CURRENT); | 
| 76 |  | -    int loops=my_prio; | 
| 77 |  | -    while(storage_should_wait(drive, my_prio) && (loops--)>=0) | 
| 78 |  | -    { | 
| 79 |  | -        sleep(STORAGE_DELAY_UNIT); | 
| 80 |  | -    } | 
| 81 |  | - | 
| 82 |  | -    storage_last_thread[drive] = thread_get_current(); | 
| 83 |  | -    storage_last_activity[drive] = current_tick; | 
| 84 |  | -} | 
| 85 |  | -#endif | 
| 86 |  | - | 
| 87 | 38 | int storage_read_sectors(IF_MD2(int drive,) unsigned long start, int count, | 
| 88 | 39 | void* buf) | 
| 89 | 40 | { | 
| 90 |  | -#ifdef HAVE_IO_PRIORITY | 
| 91 |  | -    storage_wait_turn(IF_MD(drive)); | 
| 92 |  | -#endif | 
| 93 |  | - | 
| 94 | 41 | #ifdef CONFIG_STORAGE_MULTI | 
| 95 | 42 | int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET; | 
| 96 | 43 | int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET; | 
| — | — | @@ -132,10 +79,6 @@ | 
| 133 | 80 | int storage_write_sectors(IF_MD2(int drive,) unsigned long start, int count, | 
| 134 | 81 | const void* buf) | 
| 135 | 82 | { | 
| 136 |  | -#ifdef HAVE_IO_PRIORITY | 
| 137 |  | -    storage_wait_turn(IF_MD(drive)); | 
| 138 |  | -#endif | 
| 139 |  | - | 
| 140 | 83 | #ifdef CONFIG_STORAGE_MULTI | 
| 141 | 84 | int driver=(storage_drivers[drive] & DRIVER_MASK)>>DRIVER_OFFSET; | 
| 142 | 85 | int ldrive=(storage_drivers[drive] & DRIVE_MASK)>>DRIVE_OFFSET; | 
| — | — | @@ -509,39 +452,6 @@ | 
| 510 | 453 | return max; | 
| 511 | 454 | } | 
| 512 | 455 |  | 
| 513 |  | -int storage_spinup_time(void) | 
| 514 |  | -{ | 
| 515 |  | -    int max=0; | 
| 516 |  | -    int t; | 
| 517 |  | -     | 
| 518 |  | -#if (CONFIG_STORAGE & STORAGE_ATA) | 
| 519 |  | -    t=ata_spinup_time(); | 
| 520 |  | -    if (t>max) max=t; | 
| 521 |  | -#endif | 
| 522 |  | - | 
| 523 |  | -#if (CONFIG_STORAGE & STORAGE_MMC) | 
| 524 |  | -    t=mmc_spinup_time(); | 
| 525 |  | -    if (t>max) max=t; | 
| 526 |  | -#endif | 
| 527 |  | - | 
| 528 |  | -#if (CONFIG_STORAGE & STORAGE_SD) | 
| 529 |  | -    //t=sd_spinup_time(); | 
| 530 |  | -    //if (t>max) max=t; | 
| 531 |  | -#endif | 
| 532 |  | - | 
| 533 |  | -#if (CONFIG_STORAGE & STORAGE_NAND) | 
| 534 |  | -    t=nand_spinup_time(); | 
| 535 |  | -    if (t>max) max=t; | 
| 536 |  | -#endif | 
| 537 |  | - | 
| 538 |  | -#if (CONFIG_STORAGE & STORAGE_RAMDISK) | 
| 539 |  | -    t=ramdisk_spinup_time(); | 
| 540 |  | -    if (t>max) max=t; | 
| 541 |  | -#endif | 
| 542 |  | - | 
| 543 |  | -    return max; | 
| 544 |  | -} | 
| 545 |  | - | 
| 546 | 456 | #ifdef STORAGE_GET_INFO | 
| 547 | 457 | void storage_get_info(int drive, struct storage_info *info) | 
| 548 | 458 | { | 
| Index: embios/trunk/target/ipodnano2g/storage_nand.c | 
| — | — | @@ -86,7 +86,7 @@ | 
| 87 | 87 | int nand_flush(void) | 
| 88 | 88 | { | 
| 89 | 89 | int rc = ftl_sync(); | 
| 90 |  | -    if (rc != 0) panicf("Failed to unmount flash: %X", rc); | 
|  | 90 | +    if (rc != 0) panicf(PANIC_FATAL, "Failed to unmount flash: %X", rc); | 
| 91 | 91 | return rc; | 
| 92 | 92 | } | 
| 93 | 93 | #endif | 
| Index: embios/trunk/target/ipodnano2g/target.h | 
| — | — | @@ -37,6 +37,7 @@ | 
| 38 | 38 |  | 
| 39 | 39 |  | 
| 40 | 40 | #define HAVE_FLASH_STORAGE | 
|  | 41 | +#define HAVE_STORAGE_FLUSH | 
| 41 | 42 | #define CONFIG_STORAGE STORAGE_NAND | 
| 42 | 43 | #define SECTOR_SIZE 2048 | 
| 43 | 44 |  | 
| Index: embios/trunk/usb/usb.c | 
| — | — | @@ -463,8 +463,9 @@ | 
| 464 | 464 | dbgsendbuf[0] = 1; | 
| 465 | 465 | dbgsendbuf[1] = SCHEDULER_THREAD_INFO_VERSION; | 
| 466 | 466 | dbgsendbuf[2] = MAX_THREADS * sizeof(struct scheduler_thread); | 
| 467 |  | -			memcpy(&dbgsendbuf[4], (void*)((uint32_t)scheduler_threads + dbgrecvbuf[1]), dbgrecvbuf[2]);
 | 
| 468 |  | -			size = dbgrecvbuf[2] + 16;
 | 
|  | 467 | +            memcpy(&dbgsendbuf[4], (void*)((uint32_t)scheduler_threads + dbgrecvbuf[1]), | 
|  | 468 | +                   dbgrecvbuf[2]); | 
|  | 469 | +            size = dbgrecvbuf[2] + 16; | 
| 469 | 470 | break; | 
| 470 | 471 | case 16:  // FREEZE SCHEDULER | 
| 471 | 472 | dbgsendbuf[1] = scheduler_freeze(dbgrecvbuf[1]); | 
| — | — | @@ -490,7 +491,7 @@ | 
| 491 | 492 | dbgsendbuf[0] = 1; | 
| 492 | 493 | size = 16; | 
| 493 | 494 | break; | 
| 494 |  | -        case 19:  // CREATE THREAD
 | 
|  | 495 | +        case 19:  // KILL THREAD | 
| 495 | 496 | dbgsendbuf[0] = 1; | 
| 496 | 497 | dbgsendbuf[1] = thread_create((const char*)dbgsendbuf[1], (const void*)dbgsendbuf[2], | 
| 497 | 498 | (char*)dbgsendbuf[3], dbgsendbuf[4], dbgsendbuf[5], | 
| Index: embios/trunk/storage.h | 
| — | — | @@ -71,7 +71,6 @@ | 
| 72 | 72 | #define storage_flush() (void)0 | 
| 73 | 73 | #endif | 
| 74 | 74 | #define storage_last_disk_activity() ata_last_disk_activity() | 
| 75 |  | -        #define storage_spinup_time() ata_spinup_time() | 
| 76 | 75 | #define storage_get_identify() ata_get_identify() | 
| 77 | 76 |  | 
| 78 | 77 | #ifdef STORAGE_GET_INFO | 
| — | — | @@ -96,7 +95,6 @@ | 
| 97 | 96 | #define storage_flush() (void)0 | 
| 98 | 97 | #endif | 
| 99 | 98 | #define storage_last_disk_activity() sd_last_disk_activity() | 
| 100 |  | -        #define storage_spinup_time() 0 | 
| 101 | 99 | #define storage_get_identify() sd_get_identify() | 
| 102 | 100 |  | 
| 103 | 101 | #ifdef STORAGE_GET_INFO | 
| — | — | @@ -121,7 +119,6 @@ | 
| 122 | 120 | #define storage_flush() (void)0 | 
| 123 | 121 | #endif | 
| 124 | 122 | #define storage_last_disk_activity() mmc_last_disk_activity() | 
| 125 |  | -        #define storage_spinup_time() 0 | 
| 126 | 123 | #define storage_get_identify() mmc_get_identify() | 
| 127 | 124 |  | 
| 128 | 125 | #ifdef STORAGE_GET_INFO | 
| — | — | @@ -146,7 +143,6 @@ | 
| 147 | 144 | #define storage_flush() nand_flush() | 
| 148 | 145 | #endif | 
| 149 | 146 | #define storage_last_disk_activity() nand_last_disk_activity() | 
| 150 |  | -        #define storage_spinup_time() 0 | 
| 151 | 147 | #define storage_get_identify() nand_get_identify() | 
| 152 | 148 |  | 
| 153 | 149 | #ifdef STORAGE_GET_INFO | 
| — | — | @@ -171,7 +167,6 @@ | 
| 172 | 168 | #define storage_flush() (void)0 | 
| 173 | 169 | #endif | 
| 174 | 170 | #define storage_last_disk_activity() ramdisk_last_disk_activity() | 
| 175 |  | -        #define storage_spinup_time() 0 | 
| 176 | 171 | #define storage_get_identify() ramdisk_get_identify() | 
| 177 | 172 |  | 
| 178 | 173 | #ifdef STORAGE_GET_INFO | 
| — | — | @@ -184,7 +179,7 @@ | 
| 185 | 180 | #else | 
| 186 | 181 | //#error No storage driver! | 
| 187 | 182 | #endif | 
| 188 |  | -#else /* NOT CONFIG_STORAGE_MULTI and PLATFORM_NATIVE*/ | 
|  | 183 | +#else /* NOT CONFIG_STORAGE_MULTI */ | 
| 189 | 184 |  | 
| 190 | 185 | /* Simulator and multi-driver use normal functions */ | 
| 191 | 186 |  | 
| — | — | @@ -198,7 +193,6 @@ | 
| 199 | 194 | void storage_spin(void); | 
| 200 | 195 | void storage_spindown(int seconds); | 
| 201 | 196 | long storage_last_disk_activity(void); | 
| 202 |  | -int storage_spinup_time(void); | 
| 203 | 197 | int storage_num_drives(void); | 
| 204 | 198 | #ifdef STORAGE_GET_INFO | 
| 205 | 199 | void storage_get_info(int drive, struct storage_info *info); | 
| — | — | @@ -208,7 +202,7 @@ | 
| 209 | 203 | bool storage_present(int drive); | 
| 210 | 204 | #endif | 
| 211 | 205 |  | 
| 212 |  | -#endif /* NOT CONFIG_STORAGE_MULTI and NOT SIMULATOR*/ | 
|  | 206 | +#endif /* NOT CONFIG_STORAGE_MULTI */ | 
| 213 | 207 |  | 
| 214 | 208 | int storage_read_sectors(IF_MD2(int drive,) unsigned long start, int count, void* buf); | 
| 215 | 209 | int storage_write_sectors(IF_MD2(int drive,) unsigned long start, int count, const void* buf); | 
| Index: embios/trunk/fat.c | 
| — | — | @@ -186,6 +186,8 @@ | 
| 187 | 187 | static int bpb_is_sane(IF_MV_NONVOID(struct bpb* fat_bpb)); | 
| 188 | 188 | static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,) | 
| 189 | 189 | long secnum, bool dirty); | 
|  | 190 | +static void unlock_fat_sector(IF_MV2(struct bpb* fat_bpb,) | 
|  | 191 | +                              long secnum); | 
| 190 | 192 | static void create_dos_name(const unsigned char *name, unsigned char *newname); | 
| 191 | 193 | static void randomize_dos_name(unsigned char *name); | 
| 192 | 194 | static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) | 
| — | — | @@ -193,22 +195,26 @@ | 
| 194 | 196 | static int transfer(IF_MV2(struct bpb* fat_bpb,) unsigned long start, | 
| 195 | 197 | long count, char* buf, bool write ); | 
| 196 | 198 |  | 
| 197 |  | -#define FAT_CACHE_SIZE 0x20 | 
|  | 199 | +#define FAT_CACHE_SIZE 4 | 
| 198 | 200 | #define FAT_CACHE_MASK (FAT_CACHE_SIZE-1) | 
| 199 | 201 |  | 
| 200 | 202 | struct fat_cache_entry | 
| 201 | 203 | { | 
| 202 | 204 | long secnum; | 
| 203 |  | -    bool inuse; | 
|  | 205 | +    bool valid; | 
|  | 206 | +    int locked; | 
| 204 | 207 | bool dirty; | 
| 205 | 208 | #ifdef HAVE_MULTIVOLUME | 
| 206 |  | -    struct bpb* fat_vol ; /* shared cache for all volumes */ | 
|  | 209 | +    struct bpb* fat_vol; /* shared cache for all volumes */ | 
| 207 | 210 | #endif | 
| 208 | 211 | }; | 
| 209 | 212 |  | 
| 210 |  | -static char fat_cache_sectors[FAT_CACHE_SIZE][SECTOR_SIZE]; | 
|  | 213 | +static char fat_cache_sectors[FAT_CACHE_SIZE][SECTOR_SIZE] CACHEALIGN_ATTR; | 
| 211 | 214 | static struct fat_cache_entry fat_cache[FAT_CACHE_SIZE]; | 
| 212 | 215 | static struct mutex cache_mutex; | 
|  | 216 | +static struct mutex tempbuf_mutex; | 
|  | 217 | +static char fat_tempbuf[SECTOR_SIZE] CACHEALIGN_ATTR; | 
|  | 218 | +static bool tempbuf_locked; | 
| 213 | 219 |  | 
| 214 | 220 | #if defined(HAVE_HOTSWAP) && !(CONFIG_STORAGE & STORAGE_MMC) /* A better condition ?? */ | 
| 215 | 221 | void fat_lock(void) | 
| — | — | @@ -264,19 +270,16 @@ | 
| 265 | 271 | { | 
| 266 | 272 | initialized = true; | 
| 267 | 273 | mutex_init(&cache_mutex); | 
|  | 274 | +        mutex_init(&tempbuf_mutex); | 
|  | 275 | +        tempbuf_locked = false; | 
| 268 | 276 | } | 
| 269 | 277 |  | 
| 270 |  | -#ifdef HAVE_PRIORITY_SCHEDULING | 
| 271 |  | -    /* Disable this because it is dangerous due to the assumption that | 
| 272 |  | -     * mutex_unlock won't yield */ | 
| 273 |  | -    mutex_set_preempt(&cache_mutex, false); | 
| 274 |  | -#endif | 
| 275 |  | - | 
| 276 | 278 | /* mark the FAT cache as unused */ | 
| 277 | 279 | for(i = 0;i < FAT_CACHE_SIZE;i++) | 
| 278 | 280 | { | 
| 279 |  | -        fat_cache[i].secnum = 8; /* We use a "safe" sector just in case */ | 
| 280 |  | -        fat_cache[i].inuse = false; | 
|  | 281 | +        fat_cache[i].secnum = -1; | 
|  | 282 | +        fat_cache[i].valid = false; | 
|  | 283 | +        fat_cache[i].locked = 0; | 
| 281 | 284 | fat_cache[i].dirty = false; | 
| 282 | 285 | #ifdef HAVE_MULTIVOLUME | 
| 283 | 286 | fat_cache[i].fat_vol = NULL; | 
| — | — | @@ -297,7 +300,6 @@ | 
| 298 | 301 | const int volume = 0; | 
| 299 | 302 | #endif | 
| 300 | 303 | struct bpb* fat_bpb = &fat_bpbs[volume]; | 
| 301 |  | -    unsigned char buf[SECTOR_SIZE]; | 
| 302 | 304 | int rc; | 
| 303 | 305 | int secmult; | 
| 304 | 306 | long datasec; | 
| — | — | @@ -306,9 +308,11 @@ | 
| 307 | 309 | #endif | 
| 308 | 310 |  | 
| 309 | 311 | /* Read the sector */ | 
|  | 312 | +    unsigned char* buf = fat_get_sector_buffer(); | 
| 310 | 313 | rc = storage_read_sectors(IF_MD2(drive,) startsector,1,buf); | 
| 311 | 314 | if(rc) | 
| 312 | 315 | { | 
|  | 316 | +        fat_release_sector_buffer(); | 
| 313 | 317 | DEBUGF( "fat_mount() - Couldn't read BPB (error code %d)", rc); | 
| 314 | 318 | return rc * 10 - 1; | 
| 315 | 319 | } | 
| — | — | @@ -347,7 +351,10 @@ | 
| 348 | 352 | #ifdef HAVE_FAT16SUPPORT | 
| 349 | 353 | fat_bpb->bpb_rootentcnt = BYTES2INT16(buf,BPB_ROOTENTCNT); | 
| 350 | 354 | if (!fat_bpb->bpb_bytspersec) | 
|  | 355 | +    { | 
|  | 356 | +        fat_release_sector_buffer(); | 
| 351 | 357 | return -2; | 
|  | 358 | +    } | 
| 352 | 359 | rootdirsectors = secmult * ((fat_bpb->bpb_rootentcnt * DIR_ENTRY_SIZE | 
| 353 | 360 | + fat_bpb->bpb_bytspersec - 1) / fat_bpb->bpb_bytspersec); | 
| 354 | 361 | #endif /* #ifdef HAVE_FAT16SUPPORT */ | 
| — | — | @@ -363,7 +370,10 @@ | 
| 364 | 371 | if (fat_bpb->bpb_secperclus) | 
| 365 | 372 | fat_bpb->dataclusters = datasec / fat_bpb->bpb_secperclus; | 
| 366 | 373 | else | 
| 367 |  | -       return -2; | 
|  | 374 | +    { | 
|  | 375 | +        fat_release_sector_buffer(); | 
|  | 376 | +        return -2; | 
|  | 377 | +    } | 
| 368 | 378 |  | 
| 369 | 379 | #ifdef TEST_FAT | 
| 370 | 380 | /* | 
| — | — | @@ -379,10 +389,12 @@ | 
| 380 | 390 | fat_bpb->is_fat16 = true; | 
| 381 | 391 | if (fat_bpb->dataclusters < 4085) | 
| 382 | 392 | { /* FAT12 */ | 
|  | 393 | +            fat_release_sector_buffer(); | 
| 383 | 394 | DEBUGF("This is FAT12. Go away!"); | 
| 384 | 395 | return -2; | 
| 385 | 396 | } | 
| 386 | 397 | #else /* #ifdef HAVE_FAT16SUPPORT */ | 
|  | 398 | +        fat_release_sector_buffer(); | 
| 387 | 399 | DEBUGF("This is not FAT32. Go away!"); | 
| 388 | 400 | return -2; | 
| 389 | 401 | #endif /* #ifndef HAVE_FAT16SUPPORT */ | 
| — | — | @@ -414,6 +426,7 @@ | 
| 415 | 427 | rc = bpb_is_sane(IF_MV(fat_bpb)); | 
| 416 | 428 | if (rc < 0) | 
| 417 | 429 | { | 
|  | 430 | +        fat_release_sector_buffer(); | 
| 418 | 431 | DEBUGF( "fat_mount() - BPB is not sane"); | 
| 419 | 432 | return rc * 10 - 3; | 
| 420 | 433 | } | 
| — | — | @@ -432,6 +445,7 @@ | 
| 433 | 446 | startsector + fat_bpb->bpb_fsinfo, 1, buf); | 
| 434 | 447 | if (rc < 0) | 
| 435 | 448 | { | 
|  | 449 | +            fat_release_sector_buffer(); | 
| 436 | 450 | DEBUGF( "fat_mount() - Couldn't read FSInfo (error code %d)", rc); | 
| 437 | 451 | return rc * 10 - 4; | 
| 438 | 452 | } | 
| — | — | @@ -438,6 +452,7 @@ | 
| 439 | 453 | fat_bpb->fsinfo.freecount = BYTES2INT32(buf, FSINFO_FREECOUNT); | 
| 440 | 454 | fat_bpb->fsinfo.nextfree = BYTES2INT32(buf, FSINFO_NEXTFREE); | 
| 441 | 455 | } | 
|  | 456 | +    fat_release_sector_buffer(); | 
| 442 | 457 |  | 
| 443 | 458 | /* calculate freecount if unset */ | 
| 444 | 459 | if ( fat_bpb->fsinfo.freecount == 0xffffffff ) | 
| — | — | @@ -485,7 +500,8 @@ | 
| 486 | 501 | #endif | 
| 487 | 502 | ) | 
| 488 | 503 | { | 
| 489 |  | -                fce->inuse = false; /* discard all from that volume */ | 
|  | 504 | +                fce->valid = false; /* discard all from that volume */ | 
|  | 505 | +                fce->locked = 0; | 
| 490 | 506 | fce->dirty = false; | 
| 491 | 507 | } | 
| 492 | 508 | } | 
| — | — | @@ -524,6 +540,7 @@ | 
| 525 | 541 | fat_bpb->fsinfo.nextfree = c; | 
| 526 | 542 | } | 
| 527 | 543 | } | 
|  | 544 | +            unlock_fat_sector(IF_MV2(fat_bpb,) i); | 
| 528 | 545 | } | 
| 529 | 546 | } | 
| 530 | 547 | else | 
| — | — | @@ -543,6 +560,7 @@ | 
| 544 | 561 | fat_bpb->fsinfo.nextfree = c; | 
| 545 | 562 | } | 
| 546 | 563 | } | 
|  | 564 | +            unlock_fat_sector(IF_MV2(fat_bpb,) i); | 
| 547 | 565 | } | 
| 548 | 566 | } | 
| 549 | 567 | fat_bpb->fsinfo.freecount = free; | 
| — | — | @@ -646,8 +664,6 @@ | 
| 647 | 665 | fce->dirty = false; | 
| 648 | 666 | } | 
| 649 | 667 |  | 
| 650 |  | -/* Note: The returned pointer is only safely valid until the next | 
| 651 |  | -         task switch! (Any subsequent ata read/write may yield.) */ | 
| 652 | 668 | static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,) | 
| 653 | 669 | long fatsector, bool dirty) | 
| 654 | 670 | { | 
| — | — | @@ -663,22 +679,23 @@ | 
| 664 | 680 | mutex_lock(&cache_mutex, TIMEOUT_BLOCK); /* make changes atomic */ | 
| 665 | 681 |  | 
| 666 | 682 | /* Delete the cache entry if it isn't the sector we want */ | 
| 667 |  | -    if(fce->inuse && (fce->secnum != secnum | 
|  | 683 | +    if(fce->valid && (fce->secnum != secnum | 
| 668 | 684 | #ifdef HAVE_MULTIVOLUME | 
| 669 | 685 | || fce->fat_vol != fat_bpb | 
| 670 | 686 | #endif | 
| 671 | 687 | )) | 
| 672 | 688 | { | 
|  | 689 | +        while (fce->locked) sleep(1000); | 
| 673 | 690 | /* Write back if it is dirty */ | 
| 674 | 691 | if(fce->dirty) | 
| 675 | 692 | { | 
| 676 | 693 | flush_fat_sector(fce, sectorbuf); | 
| 677 | 694 | } | 
| 678 |  | -        fce->inuse = false; | 
|  | 695 | +        fce->valid = false; | 
| 679 | 696 | } | 
| 680 | 697 |  | 
| 681 | 698 | /* Load the sector if it is not cached */ | 
| 682 |  | -    if(!fce->inuse) | 
|  | 699 | +    if(!fce->valid) | 
| 683 | 700 | { | 
| 684 | 701 | rc = storage_read_sectors(IF_MD2(fat_bpb->drive,) | 
| 685 | 702 | secnum + fat_bpb->startsector,1, | 
| — | — | @@ -690,12 +707,13 @@ | 
| 691 | 708 | mutex_unlock(&cache_mutex); | 
| 692 | 709 | return NULL; | 
| 693 | 710 | } | 
| 694 |  | -        fce->inuse = true; | 
|  | 711 | +        fce->valid = true; | 
| 695 | 712 | fce->secnum = secnum; | 
| 696 | 713 | #ifdef HAVE_MULTIVOLUME | 
| 697 | 714 | fce->fat_vol = fat_bpb; | 
| 698 | 715 | #endif | 
| 699 | 716 | } | 
|  | 717 | +    fce->locked++; | 
| 700 | 718 | if (dirty) | 
| 701 | 719 | fce->dirty = true; /* dirt remains, sticky until flushed */ | 
| 702 | 720 | mutex_unlock(&cache_mutex); | 
| — | — | @@ -702,6 +720,31 @@ | 
| 703 | 721 | return sectorbuf; | 
| 704 | 722 | } | 
| 705 | 723 |  | 
|  | 724 | +static void unlock_fat_sector(IF_MV2(struct bpb* fat_bpb,) long fatsector) | 
|  | 725 | +{ | 
|  | 726 | +#ifndef HAVE_MULTIVOLUME | 
|  | 727 | +    struct bpb* fat_bpb = &fat_bpbs[0]; | 
|  | 728 | +#endif | 
|  | 729 | +    long secnum = fatsector + fat_bpb->bpb_rsvdseccnt; | 
|  | 730 | +    int cache_index = secnum & FAT_CACHE_MASK; | 
|  | 731 | +    fat_cache[cache_index].locked--; | 
|  | 732 | +} | 
|  | 733 | + | 
|  | 734 | +void* fat_get_sector_buffer() | 
|  | 735 | +{ | 
|  | 736 | +    mutex_lock(&tempbuf_mutex, TIMEOUT_BLOCK); | 
|  | 737 | +    if (tempbuf_locked) | 
|  | 738 | +        panicf(PANIC_KILLUSERTHREADS, "FAT: Tried to lock temporary sector buffer twice!"); | 
|  | 739 | +    tempbuf_locked = true; | 
|  | 740 | +    return fat_tempbuf; | 
|  | 741 | +} | 
|  | 742 | + | 
|  | 743 | +void fat_release_sector_buffer() | 
|  | 744 | +{ | 
|  | 745 | +    tempbuf_locked = false; | 
|  | 746 | +    mutex_unlock(&tempbuf_mutex); | 
|  | 747 | +} | 
|  | 748 | + | 
| 706 | 749 | static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) | 
| 707 | 750 | unsigned long startcluster) | 
| 708 | 751 | { | 
| — | — | @@ -732,11 +775,13 @@ | 
| 733 | 776 | cluster numbers out of bounds */ | 
| 734 | 777 | if ( c < 2 || c > fat_bpb->dataclusters+1 ) | 
| 735 | 778 | continue; | 
|  | 779 | +                    unlock_fat_sector(IF_MV2(fat_bpb,) nr); | 
| 736 | 780 | DEBUGF("find_free_cluster(%x) == %x",startcluster,c); | 
| 737 | 781 | fat_bpb->fsinfo.nextfree = c; | 
| 738 | 782 | return c; | 
| 739 | 783 | } | 
| 740 | 784 | } | 
|  | 785 | +            unlock_fat_sector(IF_MV2(fat_bpb,) nr); | 
| 741 | 786 | offset = 0; | 
| 742 | 787 | } | 
| 743 | 788 | } | 
| — | — | @@ -760,11 +805,13 @@ | 
| 761 | 806 | cluster numbers out of bounds */ | 
| 762 | 807 | if ( c < 2 || c > fat_bpb->dataclusters+1 ) | 
| 763 | 808 | continue; | 
|  | 809 | +                    unlock_fat_sector(IF_MV2(fat_bpb,) nr); | 
| 764 | 810 | DEBUGF("find_free_cluster(%lx) == %lx",startcluster,c); | 
| 765 | 811 | fat_bpb->fsinfo.nextfree = c; | 
| 766 | 812 | return c; | 
| 767 | 813 | } | 
| 768 | 814 | } | 
|  | 815 | +            unlock_fat_sector(IF_MV2(fat_bpb,) nr); | 
| 769 | 816 | offset = 0; | 
| 770 | 817 | } | 
| 771 | 818 | } | 
| — | — | @@ -816,6 +863,7 @@ | 
| 817 | 864 | fat_bpb->fsinfo.freecount); | 
| 818 | 865 |  | 
| 819 | 866 | sec[offset] = htole16(val); | 
|  | 867 | +        unlock_fat_sector(IF_MV2(fat_bpb,) sector); | 
| 820 | 868 | } | 
| 821 | 869 | else | 
| 822 | 870 | #endif /* #ifdef HAVE_FAT16SUPPORT */ | 
| — | — | @@ -855,6 +903,7 @@ | 
| 856 | 904 | /* don't change top 4 bits */ | 
| 857 | 905 | sec[offset] &= htole32(0xf0000000); | 
| 858 | 906 | sec[offset] |= htole32(val & 0x0fffffff); | 
|  | 907 | +        unlock_fat_sector(IF_MV2(fat_bpb,) sector); | 
| 859 | 908 | } | 
| 860 | 909 |  | 
| 861 | 910 | return 0; | 
| — | — | @@ -879,7 +928,10 @@ | 
| 880 | 929 | return -1; | 
| 881 | 930 | } | 
| 882 | 931 |  | 
| 883 |  | -        return letoh16(sec[offset]); | 
|  | 932 | +        long val = letoh16(sec[offset]); | 
|  | 933 | +        unlock_fat_sector(IF_MV2(fat_bpb,) sector); | 
|  | 934 | + | 
|  | 935 | +        return val; | 
| 884 | 936 | } | 
| 885 | 937 | else | 
| 886 | 938 | #endif /* #ifdef HAVE_FAT16SUPPORT */ | 
| — | — | @@ -895,7 +947,10 @@ | 
| 896 | 948 | return -1; | 
| 897 | 949 | } | 
| 898 | 950 |  | 
| 899 |  | -        return letoh32(sec[offset]) & 0x0fffffff; | 
|  | 951 | +        long val = letoh32(sec[offset]) & 0x0fffffff; | 
|  | 952 | +        unlock_fat_sector(IF_MV2(fat_bpb,) sector); | 
|  | 953 | + | 
|  | 954 | +        return val; | 
| 900 | 955 | } | 
| 901 | 956 | } | 
| 902 | 957 |  | 
| — | — | @@ -929,7 +984,6 @@ | 
| 930 | 985 | #ifndef HAVE_MULTIVOLUME | 
| 931 | 986 | struct bpb* fat_bpb = &fat_bpbs[0]; | 
| 932 | 987 | #endif | 
| 933 |  | -    unsigned char fsinfo[SECTOR_SIZE]; | 
| 934 | 988 | unsigned long* intptr; | 
| 935 | 989 | int rc; | 
| 936 | 990 |  | 
| — | — | @@ -939,11 +993,13 @@ | 
| 940 | 994 | #endif /* #ifdef HAVE_FAT16SUPPORT */ | 
| 941 | 995 |  | 
| 942 | 996 | /* update fsinfo */ | 
|  | 997 | +    unsigned char* fsinfo = fat_get_sector_buffer(); | 
| 943 | 998 | rc = storage_read_sectors(IF_MD2(fat_bpb->drive,) | 
| 944 | 999 | fat_bpb->startsector + fat_bpb->bpb_fsinfo, 1,fsinfo); | 
| 945 | 1000 | if (rc < 0) | 
| 946 | 1001 | { | 
| 947 |  | -        DEBUGF( "flush_fat() - Couldn't read FSInfo (error code %d)", rc); | 
|  | 1002 | +        fat_release_sector_buffer(); | 
|  | 1003 | +        DEBUGF( "update_fsinfo() - Couldn't read FSInfo (error code %d)", rc); | 
| 948 | 1004 | return rc * 10 - 1; | 
| 949 | 1005 | } | 
| 950 | 1006 | intptr = (long*)&(fsinfo[FSINFO_FREECOUNT]); | 
| — | — | @@ -954,9 +1010,10 @@ | 
| 955 | 1011 |  | 
| 956 | 1012 | rc = storage_write_sectors(IF_MD2(fat_bpb->drive,) | 
| 957 | 1013 | fat_bpb->startsector + fat_bpb->bpb_fsinfo,1,fsinfo); | 
|  | 1014 | +    fat_release_sector_buffer(); | 
| 958 | 1015 | if (rc < 0) | 
| 959 | 1016 | { | 
| 960 |  | -        DEBUGF( "flush_fat() - Couldn't write FSInfo (error code %d)", rc); | 
|  | 1017 | +        DEBUGF( "update_fsinfo() - Couldn't write FSInfo (error code %d)", rc); | 
| 961 | 1018 | return rc * 10 - 2; | 
| 962 | 1019 | } | 
| 963 | 1020 |  | 
| — | — | @@ -974,7 +1031,7 @@ | 
| 975 | 1032 | for(i = 0;i < FAT_CACHE_SIZE;i++) | 
| 976 | 1033 | { | 
| 977 | 1034 | struct fat_cache_entry *fce = &fat_cache[i]; | 
| 978 |  | -        if(fce->inuse  | 
|  | 1035 | +        if(fce->valid | 
| 979 | 1036 | #ifdef HAVE_MULTIVOLUME | 
| 980 | 1037 | && fce->fat_vol == fat_bpb | 
| 981 | 1038 | #endif | 
| — | — | @@ -1028,7 +1085,6 @@ | 
| 1029 | 1086 | const unsigned char* shortname, | 
| 1030 | 1087 | bool is_directory) | 
| 1031 | 1088 | { | 
| 1032 |  | -    unsigned char buf[SECTOR_SIZE]; | 
| 1033 | 1089 | unsigned char* entry; | 
| 1034 | 1090 | unsigned int idx = firstentry % DIR_ENTRIES_PER_SECTOR; | 
| 1035 | 1091 | unsigned int sector = firstentry / DIR_ENTRIES_PER_SECTOR; | 
| — | — | @@ -1045,9 +1101,13 @@ | 
| 1046 | 1102 | if (rc<0) | 
| 1047 | 1103 | return rc * 10 - 1; | 
| 1048 | 1104 |  | 
|  | 1105 | +    unsigned char* buf = fat_get_sector_buffer(); | 
| 1049 | 1106 | rc = fat_readwrite(file, 1, buf, false); | 
| 1050 | 1107 | if (rc<1) | 
|  | 1108 | +    { | 
|  | 1109 | +        fat_release_sector_buffer(); | 
| 1051 | 1110 | return rc * 10 - 2; | 
|  | 1111 | +    } | 
| 1052 | 1112 |  | 
| 1053 | 1113 | /* calculate shortname checksum */ | 
| 1054 | 1114 | for (i=11; i>0; i--) | 
| — | — | @@ -1070,15 +1130,22 @@ | 
| 1071 | 1131 | /* update current sector */ | 
| 1072 | 1132 | rc = fat_seek(file, sector); | 
| 1073 | 1133 | if (rc<0) | 
|  | 1134 | +            { | 
|  | 1135 | +                fat_release_sector_buffer(); | 
| 1074 | 1136 | return rc * 10 - 3; | 
|  | 1137 | +            } | 
| 1075 | 1138 |  | 
| 1076 | 1139 | rc = fat_readwrite(file, 1, buf, true); | 
| 1077 | 1140 | if (rc<1) | 
|  | 1141 | +            { | 
|  | 1142 | +                fat_release_sector_buffer(); | 
| 1078 | 1143 | return rc * 10 - 4; | 
|  | 1144 | +            } | 
| 1079 | 1145 |  | 
| 1080 | 1146 | /* read next sector */ | 
| 1081 | 1147 | rc = fat_readwrite(file, 1, buf, false); | 
| 1082 | 1148 | if (rc<0) { | 
|  | 1149 | +                fat_release_sector_buffer(); | 
| 1083 | 1150 | DEBUGF("Failed writing new sector: %d",rc); | 
| 1084 | 1151 | return rc * 10 - 5; | 
| 1085 | 1152 | } | 
| — | — | @@ -1094,10 +1161,13 @@ | 
| 1095 | 1162 |  | 
| 1096 | 1163 | /* verify this entry is free */ | 
| 1097 | 1164 | if (entry[0] && entry[0] != 0xe5 ) | 
|  | 1165 | +        { | 
|  | 1166 | +            fat_release_sector_buffer(); | 
| 1098 | 1167 | panicf(PANIC_KILLUSERTHREADS, "Dir entry %d in sector %x is not free! " | 
| 1099 | 1168 | "%02x %02x %02x %02x", | 
| 1100 | 1169 | idx, sector, | 
| 1101 | 1170 | entry[0], entry[1], entry[2], entry[3]); | 
|  | 1171 | +        } | 
| 1102 | 1172 |  | 
| 1103 | 1173 | memset(entry, 0, DIR_ENTRY_SIZE); | 
| 1104 | 1174 | if ( i+1 < numentries ) { | 
| — | — | @@ -1157,9 +1227,13 @@ | 
| 1158 | 1228 | /* update last sector */ | 
| 1159 | 1229 | rc = fat_seek(file, sector); | 
| 1160 | 1230 | if (rc<0) | 
|  | 1231 | +    { | 
|  | 1232 | +        fat_release_sector_buffer(); | 
| 1161 | 1233 | return rc * 10 - 6; | 
|  | 1234 | +    } | 
| 1162 | 1235 |  | 
| 1163 | 1236 | rc = fat_readwrite(file, 1, buf, true); | 
|  | 1237 | +    fat_release_sector_buffer(); | 
| 1164 | 1238 | if (rc<1) | 
| 1165 | 1239 | return rc * 10 - 7; | 
| 1166 | 1240 |  | 
| — | — | @@ -1199,7 +1273,6 @@ | 
| 1200 | 1274 | #else | 
| 1201 | 1275 | struct bpb* fat_bpb = &fat_bpbs[0]; | 
| 1202 | 1276 | #endif | 
| 1203 |  | -    unsigned char buf[SECTOR_SIZE]; | 
| 1204 | 1277 | unsigned char shortname[12]; | 
| 1205 | 1278 | int rc; | 
| 1206 | 1279 | unsigned int sector; | 
| — | — | @@ -1240,12 +1313,16 @@ | 
| 1241 | 1314 | / NAME_BYTES_PER_ENTRY + 1; | 
| 1242 | 1315 | } | 
| 1243 | 1316 |  | 
|  | 1317 | +    unsigned char* buf = fat_get_sector_buffer(); | 
| 1244 | 1318 | restart: | 
| 1245 | 1319 | firstentry = -1; | 
| 1246 | 1320 |  | 
| 1247 | 1321 | rc = fat_seek(&dir->file, 0); | 
| 1248 | 1322 | if (rc < 0) | 
|  | 1323 | +    { | 
|  | 1324 | +        fat_release_sector_buffer(); | 
| 1249 | 1325 | return rc * 10 - 2; | 
|  | 1326 | +    } | 
| 1250 | 1327 |  | 
| 1251 | 1328 | /* step 1: search for free entries and check for duplicate shortname */ | 
| 1252 | 1329 | for (sector = 0; !done; sector++) | 
| — | — | @@ -1254,6 +1331,7 @@ | 
| 1255 | 1332 |  | 
| 1256 | 1333 | rc = fat_readwrite(&dir->file, 1, buf, false); | 
| 1257 | 1334 | if (rc < 0) { | 
|  | 1335 | +            fat_release_sector_buffer(); | 
| 1258 | 1336 | DEBUGF( "add_dir_entry() - Couldn't read dir" | 
| 1259 | 1337 | " (error code %d)", rc); | 
| 1260 | 1338 | return rc * 10 - 3; | 
| — | — | @@ -1310,7 +1388,10 @@ | 
| 1311 | 1389 | DEBUGF("Adding new sector(s) to dir"); | 
| 1312 | 1390 | rc = fat_seek(&dir->file, sector); | 
| 1313 | 1391 | if (rc < 0) | 
|  | 1392 | +        { | 
|  | 1393 | +            fat_release_sector_buffer(); | 
| 1314 | 1394 | return rc * 10 - 4; | 
|  | 1395 | +        } | 
| 1315 | 1396 | memset(buf, 0, sizeof buf); | 
| 1316 | 1397 |  | 
| 1317 | 1398 | /* we must clear whole clusters */ | 
| — | — | @@ -1318,11 +1399,17 @@ | 
| 1319 | 1400 | (dir->file.sectornum < (int)fat_bpb->bpb_secperclus); sector++) | 
| 1320 | 1401 | { | 
| 1321 | 1402 | if (sector >= (65536/DIR_ENTRIES_PER_SECTOR)) | 
|  | 1403 | +            { | 
|  | 1404 | +                fat_release_sector_buffer(); | 
| 1322 | 1405 | return -5; /* dir too large -- FAT specification */ | 
|  | 1406 | +            } | 
| 1323 | 1407 |  | 
| 1324 | 1408 | rc = fat_readwrite(&dir->file, 1, buf, true); | 
| 1325 | 1409 | if (rc < 1)  /* No more room or something went wrong */ | 
|  | 1410 | +            { | 
|  | 1411 | +                fat_release_sector_buffer(); | 
| 1326 | 1412 | return rc * 10 - 6; | 
|  | 1413 | +            } | 
| 1327 | 1414 |  | 
| 1328 | 1415 | entries_found += DIR_ENTRIES_PER_SECTOR; | 
| 1329 | 1416 | } | 
| — | — | @@ -1329,6 +1416,7 @@ | 
| 1330 | 1417 |  | 
| 1331 | 1418 | firstentry = sector * DIR_ENTRIES_PER_SECTOR - entries_found; | 
| 1332 | 1419 | } | 
|  | 1420 | +    fat_release_sector_buffer(); | 
| 1333 | 1421 |  | 
| 1334 | 1422 | /* step 3: add entry */ | 
| 1335 | 1423 | sector = firstentry / DIR_ENTRIES_PER_SECTOR; | 
| — | — | @@ -1456,10 +1544,7 @@ | 
| 1457 | 1545 |  | 
| 1458 | 1546 | static int update_short_entry( struct fat_file* file, long size, int attr ) | 
| 1459 | 1547 | { | 
| 1460 |  | -    unsigned char buf[SECTOR_SIZE]; | 
| 1461 | 1548 | int sector = file->direntry / DIR_ENTRIES_PER_SECTOR; | 
| 1462 |  | -    unsigned char* entry = | 
| 1463 |  | -        buf + DIR_ENTRY_SIZE * (file->direntry % DIR_ENTRIES_PER_SECTOR); | 
| 1464 | 1549 | unsigned long* sizeptr; | 
| 1465 | 1550 | unsigned short* clusptr; | 
| 1466 | 1551 | struct fat_file dir; | 
| — | — | @@ -1477,12 +1562,21 @@ | 
| 1478 | 1563 | if (rc<0) | 
| 1479 | 1564 | return rc * 10 - 2; | 
| 1480 | 1565 |  | 
|  | 1566 | +    unsigned char* buf = fat_get_sector_buffer(); | 
|  | 1567 | +    unsigned char* entry = | 
|  | 1568 | +        buf + DIR_ENTRY_SIZE * (file->direntry % DIR_ENTRIES_PER_SECTOR); | 
| 1481 | 1569 | rc = fat_readwrite(&dir, 1, buf, false); | 
| 1482 | 1570 | if (rc < 1) | 
|  | 1571 | +    { | 
|  | 1572 | +        fat_release_sector_buffer(); | 
| 1483 | 1573 | return rc * 10 - 3; | 
|  | 1574 | +    } | 
| 1484 | 1575 |  | 
| 1485 | 1576 | if (!entry[0] || entry[0] == 0xe5) | 
|  | 1577 | +    { | 
|  | 1578 | +        fat_release_sector_buffer(); | 
| 1486 | 1579 | panicf(PANIC_KILLUSERTHREADS, "Updating size on empty dir entry %d", file->direntry); | 
|  | 1580 | +    } | 
| 1487 | 1581 |  | 
| 1488 | 1582 | entry[FATDIR_ATTR] = attr & 0xFF; | 
| 1489 | 1583 |  | 
| — | — | @@ -1512,9 +1606,13 @@ | 
| 1513 | 1607 |  | 
| 1514 | 1608 | rc = fat_seek( &dir, sector ); | 
| 1515 | 1609 | if (rc < 0) | 
|  | 1610 | +    { | 
|  | 1611 | +        fat_release_sector_buffer(); | 
| 1516 | 1612 | return rc * 10 - 4; | 
|  | 1613 | +    } | 
| 1517 | 1614 |  | 
| 1518 | 1615 | rc = fat_readwrite(&dir, 1, buf, true); | 
|  | 1616 | +    fat_release_sector_buffer(); | 
| 1519 | 1617 | if (rc < 1) | 
| 1520 | 1618 | return rc * 10 - 5; | 
| 1521 | 1619 |  | 
| — | — | @@ -1624,7 +1722,7 @@ | 
| 1625 | 1723 | #else | 
| 1626 | 1724 | struct bpb* fat_bpb = &fat_bpbs[0]; | 
| 1627 | 1725 | #endif | 
| 1628 |  | -    unsigned char buf[SECTOR_SIZE]; | 
|  | 1726 | +    unsigned char buf[4]; | 
| 1629 | 1727 | int i; | 
| 1630 | 1728 | long sector; | 
| 1631 | 1729 | int rc; | 
| — | — | @@ -1762,7 +1860,6 @@ | 
| 1763 | 1861 |  | 
| 1764 | 1862 | static int free_direntries(struct fat_file* file) | 
| 1765 | 1863 | { | 
| 1766 |  | -    unsigned char buf[SECTOR_SIZE]; | 
| 1767 | 1864 | struct fat_file dir; | 
| 1768 | 1865 | int numentries = file->direntries; | 
| 1769 | 1866 | unsigned int entry = file->direntry - numentries + 1; | 
| — | — | @@ -1779,9 +1876,13 @@ | 
| 1780 | 1877 | if (rc < 0) | 
| 1781 | 1878 | return rc * 10 - 2; | 
| 1782 | 1879 |  | 
|  | 1880 | +    unsigned char* buf = fat_get_sector_buffer(); | 
| 1783 | 1881 | rc = fat_readwrite(&dir, 1, buf, false); | 
| 1784 | 1882 | if (rc < 1) | 
|  | 1883 | +    { | 
|  | 1884 | +        fat_release_sector_buffer(); | 
| 1785 | 1885 | return rc * 10 - 3; | 
|  | 1886 | +    } | 
| 1786 | 1887 |  | 
| 1787 | 1888 | for (i=0; i < numentries; i++) { | 
| 1788 | 1889 | DEBUGF("Clearing dir entry %d (%d/%d)", | 
| — | — | @@ -1793,17 +1894,26 @@ | 
| 1794 | 1895 | /* flush this sector */ | 
| 1795 | 1896 | rc = fat_seek(&dir, sector); | 
| 1796 | 1897 | if (rc < 0) | 
|  | 1898 | +            { | 
|  | 1899 | +                fat_release_sector_buffer(); | 
| 1797 | 1900 | return rc * 10 - 4; | 
|  | 1901 | +            } | 
| 1798 | 1902 |  | 
| 1799 | 1903 | rc = fat_readwrite(&dir, 1, buf, true); | 
| 1800 | 1904 | if (rc < 1) | 
|  | 1905 | +            { | 
|  | 1906 | +                fat_release_sector_buffer(); | 
| 1801 | 1907 | return rc * 10 - 5; | 
|  | 1908 | +            } | 
| 1802 | 1909 |  | 
| 1803 | 1910 | if ( i+1 < numentries ) { | 
| 1804 | 1911 | /* read next sector */ | 
| 1805 | 1912 | rc = fat_readwrite(&dir, 1, buf, false); | 
| 1806 | 1913 | if (rc < 1) | 
|  | 1914 | +                { | 
|  | 1915 | +                    fat_release_sector_buffer(); | 
| 1807 | 1916 | return rc * 10 - 6; | 
|  | 1917 | +                } | 
| 1808 | 1918 | } | 
| 1809 | 1919 | sector++; | 
| 1810 | 1920 | } | 
| — | — | @@ -1813,13 +1923,20 @@ | 
| 1814 | 1924 | /* flush this sector */ | 
| 1815 | 1925 | rc = fat_seek(&dir, sector); | 
| 1816 | 1926 | if (rc < 0) | 
|  | 1927 | +        { | 
|  | 1928 | +            fat_release_sector_buffer(); | 
| 1817 | 1929 | return rc * 10 - 7; | 
|  | 1930 | +        } | 
| 1818 | 1931 |  | 
| 1819 | 1932 | rc = fat_readwrite(&dir, 1, buf, true); | 
| 1820 | 1933 | if (rc < 1) | 
|  | 1934 | +        { | 
|  | 1935 | +            fat_release_sector_buffer(); | 
| 1821 | 1936 | return rc * 10 - 8; | 
|  | 1937 | +        } | 
| 1822 | 1938 | } | 
| 1823 | 1939 |  | 
|  | 1940 | +    fat_release_sector_buffer(); | 
| 1824 | 1941 | return 0; | 
| 1825 | 1942 | } | 
| 1826 | 1943 |  | 
| — | — | @@ -1864,7 +1981,6 @@ | 
| 1865 | 1982 | int rc; | 
| 1866 | 1983 | struct fat_dir olddir; | 
| 1867 | 1984 | struct fat_file newfile = *file; | 
| 1868 |  | -    unsigned char buf[SECTOR_SIZE]; | 
| 1869 | 1985 | unsigned char* entry = NULL; | 
| 1870 | 1986 | unsigned short* clusptr = NULL; | 
| 1871 | 1987 | unsigned int parentcluster; | 
| — | — | @@ -1922,9 +2038,13 @@ | 
| 1923 | 2039 | if (rc < 0) | 
| 1924 | 2040 | return rc * 10 - 7; | 
| 1925 | 2041 |  | 
|  | 2042 | +        unsigned char* buf = fat_get_sector_buffer(); | 
| 1926 | 2043 | rc = fat_readwrite(&olddir.file, 1, buf, false); | 
| 1927 | 2044 | if (rc < 0) | 
|  | 2045 | +        { | 
|  | 2046 | +            fat_release_sector_buffer(); | 
| 1928 | 2047 | return rc * 10 - 8; | 
|  | 2048 | +        } | 
| 1929 | 2049 |  | 
| 1930 | 2050 | /* parent cluster is 0 if parent dir is the root - FAT spec (p.29) */ | 
| 1931 | 2051 | if(dir->file.firstcluster == fat_bpb->bpb_rootclus) | 
| — | — | @@ -1935,6 +2055,7 @@ | 
| 1936 | 2056 | entry = buf + DIR_ENTRY_SIZE; | 
| 1937 | 2057 | if(strncmp("..         ", entry, 11)) | 
| 1938 | 2058 | { | 
|  | 2059 | +            fat_release_sector_buffer(); | 
| 1939 | 2060 | /* .. entry must be second entry according to FAT spec (p.29) */ | 
| 1940 | 2061 | DEBUGF("Second dir entry is not double-dot!"); | 
| 1941 | 2062 | return rc * 10 - 9; | 
| — | — | @@ -1948,9 +2069,13 @@ | 
| 1949 | 2070 | /* write back this sector */ | 
| 1950 | 2071 | rc = fat_seek(&olddir.file, 0); | 
| 1951 | 2072 | if (rc < 0) | 
|  | 2073 | +        { | 
|  | 2074 | +            fat_release_sector_buffer(); | 
| 1952 | 2075 | return rc * 10 - 7; | 
|  | 2076 | +        } | 
| 1953 | 2077 |  | 
| 1954 | 2078 | rc = fat_readwrite(&olddir.file, 1, buf, true); | 
|  | 2079 | +        fat_release_sector_buffer(); | 
| 1955 | 2080 | if (rc < 1) | 
| 1956 | 2081 | return rc * 10 - 8; | 
| 1957 | 2082 | } | 
| Index: embios/trunk/fat.h | 
| — | — | @@ -22,16 +22,9 @@ | 
| 23 | 23 | #ifndef FAT_H | 
| 24 | 24 | #define FAT_H | 
| 25 | 25 |  | 
| 26 |  | -#include <stdbool.h> | 
|  | 26 | +#include "global.h" | 
| 27 | 27 | #include "mv.h" /* for volume definitions */ | 
| 28 |  | -#include "config.h" | 
| 29 | 28 |  | 
| 30 |  | -/* This value can be overwritten by a target in config-[target].h, but | 
| 31 |  | -   that behaviour is still experimental */ | 
| 32 |  | -#ifndef SECTOR_SIZE | 
| 33 |  | -#define SECTOR_SIZE 512 | 
| 34 |  | -#endif | 
| 35 |  | - | 
| 36 | 29 | /* Number of bytes reserved for a file name (including the trailing \0). | 
| 37 | 30 | Since names are stored in the entry as UTF-8, we won't be able to | 
| 38 | 31 | store all names allowed by FAT. In FAT, a name can have max 255 | 
| — | — | @@ -132,5 +125,7 @@ | 
| 133 | 126 | extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry); | 
| 134 | 127 | extern unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume)); /* public for debug info screen */ | 
| 135 | 128 | extern bool fat_ismounted(int volume); | 
|  | 129 | +extern void* fat_get_sector_buffer(void); | 
|  | 130 | +extern void fat_release_sector_buffer(void); | 
| 136 | 131 |  | 
| 137 | 132 | #endif | 
| Index: embios/trunk/dir.h | 
| — | — | @@ -21,7 +21,7 @@ | 
| 22 | 22 | #ifndef __DIR_H__ | 
| 23 | 23 | #define __DIR_H__ | 
| 24 | 24 |  | 
| 25 |  | -#include <stdbool.h> | 
|  | 25 | +#include "global.h" | 
| 26 | 26 | #include "file.h" | 
| 27 | 27 |  | 
| 28 | 28 | #define ATTR_READ_ONLY   0x01 |