Browse Source

Fix disk1 access (unencrypted!)

Daniele Lacamera 10 months ago
parent
commit
6bc0dec5bf
3 changed files with 34 additions and 6 deletions
  1. 0 3
      src/cryptoengine.c
  2. 22 3
      src/msc_disk.c
  3. 12 0
      src/password_safe.c

+ 0 - 3
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;

+ 22 - 3
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

+ 12 - 0
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. */