From ce957faaa9bbf5785e73ba76eed232968a281547 Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Fri, 10 Feb 2023 11:45:15 +0100 Subject: [PATCH] [SC64][SW] Added firmware update overwrite safeguard --- sw/controller/src/cfg.c | 6 ++++++ sw/controller/src/cfg.h | 1 + sw/controller/src/usb.c | 10 ++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/sw/controller/src/cfg.c b/sw/controller/src/cfg.c index 3225a5e..783ec3b 100644 --- a/sw/controller/src/cfg.c +++ b/sw/controller/src/cfg.c @@ -356,6 +356,12 @@ bool cfg_update (uint32_t *args) { return false; } +bool cfg_set_rom_write_enable (bool value) { + uint32_t scr = fpga_reg_get(REG_CFG_SCR); + cfg_change_scr_bits(CFG_SCR_ROM_WRITE_ENABLED, value); + return (scr & CFG_SCR_ROM_WRITE_ENABLED); +} + save_type_t cfg_get_save_type (void) { return p.save_type; } diff --git a/sw/controller/src/cfg.h b/sw/controller/src/cfg.h index ffac1c1..4d3b409 100644 --- a/sw/controller/src/cfg.h +++ b/sw/controller/src/cfg.h @@ -19,6 +19,7 @@ typedef enum { uint32_t cfg_get_version (void); bool cfg_query (uint32_t *args); bool cfg_update (uint32_t *args); +bool cfg_set_rom_write_enable (bool value); save_type_t cfg_get_save_type (void); void cfg_get_time (uint32_t *args); void cfg_set_time (uint32_t *args); diff --git a/sw/controller/src/usb.c b/sw/controller/src/usb.c index 008ae80..9a1e367 100644 --- a/sw/controller/src/usb.c +++ b/sw/controller/src/usb.c @@ -252,15 +252,19 @@ static void usb_rx_process (void) { } break; - case 'f': + case 'f': { + bool rom_write_enable_restore = cfg_set_rom_write_enable(false); p.response_info.data[0] = update_backup(p.rx_args[0], &p.response_info.data[1]); p.rx_state = RX_STATE_IDLE; p.response_pending = true; p.response_error = (p.response_info.data[0] != UPDATE_OK); p.response_info.data_length = 8; + cfg_set_rom_write_enable(rom_write_enable_restore); break; + } - case 'F': + case 'F': { + bool rom_write_enable_restore = cfg_set_rom_write_enable(false); p.response_info.data[0] = update_prepare(p.rx_args[0], p.rx_args[1]); p.rx_state = RX_STATE_IDLE; p.response_pending = true; @@ -269,8 +273,10 @@ static void usb_rx_process (void) { p.response_info.done_callback = update_start; } else { p.response_error = true; + cfg_set_rom_write_enable(rom_write_enable_restore); } break; + } case 'p': if (p.rx_args[0]) {