From 201a4d023a3723dbabdc9679948f5f7f27f04a2a Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Wed, 17 Aug 2022 12:37:30 +0200 Subject: [PATCH] small fixes --- sw/controller/src/lcmxo2.c | 46 +++++++++++++++++++++----------------- sw/controller/src/update.c | 17 +++++++++----- sw/controller/src/update.h | 1 + sw/controller/src/vendor.h | 3 ++- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/sw/controller/src/lcmxo2.c b/sw/controller/src/lcmxo2.c index e0747e0..e0b78b7 100644 --- a/sw/controller/src/lcmxo2.c +++ b/sw/controller/src/lcmxo2.c @@ -157,6 +157,31 @@ static vendor_error_t lcmxo2_fail (vendor_error_t error) { return error; } +uint32_t vendor_flash_size (void) { + return (FLASH_PAGE_SIZE * FLASH_NUM_PAGES); +} + +vendor_error_t vendor_backup (uint32_t address, uint32_t *length) { + uint8_t buffer[FLASH_PAGE_SIZE]; + + *length = 0; + + lcmxo2_reset_bus(); + if (lcmxo2_enable_flash()) { + return lcmxo2_fail(VENDOR_ERROR_INIT); + } + lcmxo2_reset_flash_address(); + for (int i = 0; i < (FLASH_PAGE_SIZE * FLASH_NUM_PAGES); i += FLASH_PAGE_SIZE) { + lcmxo2_read_flash_page(buffer); + fpga_mem_write(address + i, FLASH_PAGE_SIZE, buffer); + *length += FLASH_PAGE_SIZE; + } + lcmxo2_disable_flash(); + lcmxo2_cleanup(); + + return VENDOR_OK; +} + vendor_error_t vendor_update (uint32_t address, uint32_t length) { uint8_t buffer[FLASH_PAGE_SIZE]; uint8_t verify_buffer[FLASH_PAGE_SIZE]; @@ -202,27 +227,6 @@ vendor_error_t vendor_update (uint32_t address, uint32_t length) { return VENDOR_OK; } -vendor_error_t vendor_backup (uint32_t address, uint32_t *length) { - uint8_t buffer[FLASH_PAGE_SIZE]; - - *length = 0; - - lcmxo2_reset_bus(); - if (lcmxo2_enable_flash()) { - return lcmxo2_fail(VENDOR_ERROR_INIT); - } - lcmxo2_reset_flash_address(); - for (int i = 0; i < (FLASH_PAGE_SIZE * FLASH_NUM_PAGES); i += FLASH_PAGE_SIZE) { - lcmxo2_read_flash_page(buffer); - fpga_mem_write(address + i, FLASH_PAGE_SIZE, buffer); - *length += FLASH_PAGE_SIZE; - } - lcmxo2_disable_flash(); - lcmxo2_cleanup(); - - return VENDOR_OK; -} - vendor_error_t vendor_reconfigure (void) { lcmxo2_reset_bus(); lcmxo2_refresh(); diff --git a/sw/controller/src/update.c b/sw/controller/src/update.c index a8c2c99..9277d22 100644 --- a/sw/controller/src/update.c +++ b/sw/controller/src/update.c @@ -9,7 +9,6 @@ typedef enum { - UPDATE_STATUS_NONE = 0, UPDATE_STATUS_START = 1, UPDATE_STATUS_MCU_START = 2, UPDATE_STATUS_MCU_DONE = 3, @@ -31,7 +30,7 @@ static const uint8_t update_token[16] = "SC64 Update v2.0"; static uint8_t status_data[12] = { 'P', 'K', 'T', PACKET_CMD_UPDATE_STATUS, 0, 0, 0, 4, - 0, 0, 0, UPDATE_STATUS_NONE, + 0, 0, 0, UPDATE_STATUS_START, }; @@ -132,16 +131,18 @@ static void update_status_notify (update_status_t status) { update_error_t update_backup (uint32_t address, uint32_t *length) { hw_flash_t buffer; + uint32_t mcu_length; uint32_t fpga_length; *length += update_write_token(&address); *length += update_prepare_chunk(&address, CHUNK_ID_MCU_DATA); - for (uint32_t offset = 0; offset < hw_flash_size(); offset += sizeof(hw_flash_t)) { + mcu_length = hw_flash_size(); + for (uint32_t offset = 0; offset < mcu_length; offset += sizeof(hw_flash_t)) { buffer = hw_flash_read(offset); fpga_mem_write(address + offset, sizeof(hw_flash_t), (uint8_t *) (&buffer)); } - *length += update_finalize_chunk(&address, hw_flash_size()); + *length += update_finalize_chunk(&address, mcu_length); *length += update_prepare_chunk(&address, CHUNK_ID_FPGA_DATA); if (vendor_backup(address, &fpga_length) != VENDOR_OK) { @@ -153,7 +154,7 @@ update_error_t update_backup (uint32_t address, uint32_t *length) { } update_error_t update_prepare (uint32_t address, uint32_t length) { - uint32_t end_address = address + length; + uint32_t end_address = (address + length); chunk_id_t id; uint32_t data_address; uint32_t data_length; @@ -177,11 +178,17 @@ update_error_t update_prepare (uint32_t address, uint32_t length) { break; case CHUNK_ID_MCU_DATA: + if (data_length > hw_flash_size()) { + return UPDATE_ERROR_SIZE; + } parameters.mcu_address = data_address; parameters.mcu_length = data_length; break; case CHUNK_ID_FPGA_DATA: + if (data_length > vendor_flash_size()) { + return UPDATE_ERROR_SIZE; + } parameters.fpga_address = data_address; parameters.fpga_length = data_length; break; diff --git a/sw/controller/src/update.h b/sw/controller/src/update.h index 264770b..b6f029c 100644 --- a/sw/controller/src/update.h +++ b/sw/controller/src/update.h @@ -10,6 +10,7 @@ typedef enum { UPDATE_OK, UPDATE_ERROR_TOKEN, UPDATE_ERROR_CHECKSUM, + UPDATE_ERROR_SIZE, UPDATE_ERROR_UNKNOWN_CHUNK, UPDATE_ERROR_READ, } update_error_t; diff --git a/sw/controller/src/vendor.h b/sw/controller/src/vendor.h index 984954e..016fac6 100644 --- a/sw/controller/src/vendor.h +++ b/sw/controller/src/vendor.h @@ -15,8 +15,9 @@ typedef enum { } vendor_error_t; -vendor_error_t vendor_update (uint32_t address, uint32_t length); +uint32_t vendor_flash_size (void); vendor_error_t vendor_backup (uint32_t address, uint32_t *length); +vendor_error_t vendor_update (uint32_t address, uint32_t length); vendor_error_t vendor_reconfigure (void);