From ed7a8bfa6cae2857a8b702ea86cc58364b7a14a8 Mon Sep 17 00:00:00 2001 From: Jochen Hoenicke Date: Mon, 26 Mar 2018 19:02:50 +0200 Subject: [PATCH] Fixes for emulator --- emulator/flash.c | 23 +++++++++++++++++++++++ firmware/Makefile | 2 +- firmware/storage.c | 2 -- memory.h | 7 +++++++ supervise.c | 4 ++++ supervise.h | 17 +++++++++++------ 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/emulator/flash.c b/emulator/flash.c index b168d86bb..a275cf641 100644 --- a/emulator/flash.c +++ b/emulator/flash.c @@ -18,6 +18,8 @@ */ #include +#include +#include #include "memory.h" @@ -110,3 +112,24 @@ void flash_program_word(uint32_t address, uint32_t data) { void flash_program_byte(uint32_t address, uint8_t data) { *(volatile uint8_t *)FLASH_PTR(address) = data; } + +static bool flash_locked = true; +void svc_flash_unlock(void) { + assert (flash_locked); + flash_locked = false; +} +void svc_flash_program(uint32_t size) { + (void) size; + assert (!flash_locked); +} +void svc_flash_erase_sector(uint16_t sector) { + assert (!flash_locked); + assert (sector >= FLASH_META_SECTOR_FIRST && + sector <= FLASH_META_SECTOR_LAST); + flash_erase_sector(sector, 3); +} +uint32_t svc_flash_lock(void) { + assert (!flash_locked); + flash_locked = true; + return 0; +} diff --git a/firmware/Makefile b/firmware/Makefile index 2e5b32933..1a9a33694 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -6,9 +6,9 @@ ifeq ($(EMULATOR),1) OBJS += udp.o else OBJS += usb.o +OBJS += bl_check.o endif -OBJS += bl_check.o OBJS += u2f.o OBJS += messages.o OBJS += storage.o diff --git a/firmware/storage.c b/firmware/storage.c index 36b466c54..fbcd5559f 100644 --- a/firmware/storage.c +++ b/firmware/storage.c @@ -122,12 +122,10 @@ void storage_show_error(void) void storage_check_flash_errors(uint32_t status) { -#if !EMULATOR // flash operation failed if (status & (FLASH_SR_PGAERR | FLASH_SR_PGPERR | FLASH_SR_PGSERR | FLASH_SR_WRPERR)) { storage_show_error(); } -#endif } bool storage_from_flash(void) diff --git a/memory.h b/memory.h index 8bb9aac27..99415604c 100644 --- a/memory.h +++ b/memory.h @@ -111,4 +111,11 @@ void memory_protect(void); void memory_write_unlock(void); int memory_bootloader_hash(uint8_t *hash); +inline void flash_write32(uint32_t addr, uint32_t word) { + *(volatile uint32_t *) FLASH_PTR(addr) = word; +} +inline void flash_write8(uint32_t addr, uint8_t byte) { + *(volatile uint8_t *) FLASH_PTR(addr) = byte; +} + #endif diff --git a/supervise.c b/supervise.c index 840d29d35..4a5967bdd 100644 --- a/supervise.c +++ b/supervise.c @@ -22,6 +22,8 @@ #include "supervise.h" #include "memory.h" +#if !EMULATOR + static void svhandler_flash_unlock(void) { flash_clear_status_flags(); flash_unlock(); @@ -86,3 +88,5 @@ void svc_handler_main(uint32_t *stack) { break; } } + +#endif diff --git a/supervise.h b/supervise.h index 05a0c3ffa..1457568a6 100644 --- a/supervise.h +++ b/supervise.h @@ -20,6 +20,8 @@ #ifndef __SUPERVISE_H__ #define __SUPERVISE_H__ +#if !EMULATOR + #define SVC_FLASH_UNLOCK 0 #define SVC_FLASH_ERASE 1 #define SVC_FLASH_PROGRAM 2 @@ -67,11 +69,14 @@ inline uint32_t svc_timer_ms(void) { return r0; } -inline void flash_write32(uint32_t addr, uint32_t word) { - *((volatile uint32_t *) addr) = word; -} -inline void flash_write8(uint32_t addr, uint8_t byte) { - *((volatile uint8_t *) addr) = byte; -} +#else + +extern void svc_flash_unlock(void); +extern void svc_flash_program(uint32_t program_size); +extern void svc_flash_erase_sector(uint16_t sector); +extern uint32_t svc_flash_lock(void); +extern uint32_t svc_timer_ms(void); + +#endif #endif