diff --git a/src/cryptoengine.c b/src/cryptoengine.c index 6c5d2fa..6fad831 100644 --- a/src/cryptoengine.c +++ b/src/cryptoengine.c @@ -66,9 +66,6 @@ int flash_encrypt_write_sector(uint32_t sector, uint8_t *buf) return -1; wc_Chacha_SetIV(&cha, hdr_cache.host_seed, sector); wc_Chacha_Process(&cha, buf, buf, SPI_FLASH_SECTOR_SIZE); - - flash_sector_erase(DRIVE_FLASH_OFFSET + sector * SPI_FLASH_SECTOR_SIZE); - flash_write(DRIVE_FLASH_OFFSET + sector * SPI_FLASH_SECTOR_SIZE, buf, SPI_FLASH_SECTOR_SIZE); return 0; diff --git a/src/msc_disk.c b/src/msc_disk.c index 57824e4..76a9879 100644 --- a/src/msc_disk.c +++ b/src/msc_disk.c @@ -98,6 +98,8 @@ static bool ejected = false; static struct disk_lba sector_cache[BLOCKS_PER_SECTOR]; static int sector_cached = -1; +static volatile int cache_busy = 1; + static int sector_cache_load(uint32_t sector) { int ret; @@ -115,15 +117,24 @@ static int sector_cache_load(uint32_t sector) sector_cached = sector; } +static inline void sector_cache_sched_commit(void) +{ + cache_busy = 1; +} + static void sector_cache_commit(void) { int ret; uint32_t sector = sector_cached; - if (sector * SPI_FLASH_SECTOR_SIZE > DRIVE_FLASH_SIZE) + if (sector * SPI_FLASH_SECTOR_SIZE > DRIVE_FLASH_SIZE) { + cache_busy = 0; return; + } //flash_encrypt_write_sector(sector, sector_cache[0].data); + flash_sector_erase(sector * SPI_FLASH_SECTOR_SIZE + DRIVE_FLASH_OFFSET); flash_write(sector * SPI_FLASH_SECTOR_SIZE + DRIVE_FLASH_OFFSET, sector_cache[0].data, SPI_FLASH_SECTOR_SIZE); + cache_busy = 0; } struct drive { @@ -254,7 +265,8 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buff sec_off = lba - (sector * BLOCKS_PER_SECTOR); if ( sector != sector_cached) { - sector_cache_load(sector); + if (!cache_busy) + sector_cache_load(sector); return 0; } memcpy(buffer, sector_cache[sec_off].data + offset, bufsize); @@ -291,10 +303,12 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* sector = lba / BLOCKS_PER_SECTOR; sec_off = lba - (sector * BLOCKS_PER_SECTOR); if ( sector != sector_cached) { + while(cache_busy) + sleep_ms(1); sector_cache_load(sector); } memcpy(sector_cache[sec_off].data + offset, buffer, bufsize); - sector_cache_commit(); + sector_cache_sched_commit(); asm volatile("DMB"); return bufsize; } @@ -380,4 +394,9 @@ void disk_crypto_activate(int status) } } +void msc_task(void) { + if (cache_busy) + sector_cache_commit(); +} + #endif diff --git a/src/password_safe.c b/src/password_safe.c index 8195747..6527791 100644 --- a/src/password_safe.c +++ b/src/password_safe.c @@ -304,8 +304,19 @@ void poll_buttons(void) uint8_t *fbuf = NULL; +void tud_mount_cb(void) +{ + gpio_put(RED_LED, 0); +} + +void tud_umount_cb(void) +{ + gpio_put(RED_LED, 1); +} + static uint8_t flash_buf[4]; +extern void msc_task(void); int main(void) { int i; system_boot(); @@ -322,6 +333,7 @@ int main(void) { ui_task(); } } + msc_task(); } /* End of session. Goodbye. */