From 0a8d3a24018c00b688691678f566e3a5178791ce Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Tue, 20 Dec 2016 19:03:42 -0500 Subject: cli_receive_data: re-enable UART DMA before returning. Based on Fredrik's fix to cli-test, commit ae8ebce. --- projects/hsm/mgmt-misc.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/projects/hsm/mgmt-misc.c b/projects/hsm/mgmt-misc.c index 31a1c49..250dc7a 100644 --- a/projects/hsm/mgmt-misc.c +++ b/projects/hsm/mgmt-misc.c @@ -51,14 +51,14 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal if (! control_mgmt_uart_dma_rx(DMA_RX_STOP)) { cli_print(cli, "Failed stopping DMA"); - return CLI_OK; + goto okay; } cli_print(cli, "OK, write size (4 bytes), data in %li byte chunks, CRC-32 (4 bytes)", (uint32_t) n); if (uart_receive_bytes(STM_UART_MGMT, (void *) &filesize, 4, 1000) != HAL_OK) { cli_print(cli, "Receive timed out"); - return CLI_ERROR; + goto fail; } cli_print(cli, "Send %li bytes of data", filesize); @@ -73,7 +73,7 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal if (uart_receive_bytes(STM_UART_MGMT, (void *) buf, n, 1000) != HAL_OK) { cli_print(cli, "Receive timed out"); - return CLI_ERROR; + goto fail; } filesize -= n; my_crc = update_crc(my_crc, buf, n); @@ -83,7 +83,7 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal */ if (data_callback != NULL && ! data_callback(buf, (size_t) n)) { cli_print(cli, "Data processing failed"); - return CLI_OK; + goto okay; } counter++; @@ -99,7 +99,13 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal cli_print(cli, "CRC checksum did NOT match"); } + okay: + control_mgmt_uart_dma_rx(DMA_RX_START); return CLI_OK; + + fail: + control_mgmt_uart_dma_rx(DMA_RX_START); + return CLI_ERROR; } static int cmd_reboot(struct cli_def *cli, const char *command, char *argv[], int argc) -- cgit v1.2.3 From bebc5595be1679bfcecc70a2be975c461c75524a Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Tue, 20 Dec 2016 19:05:11 -0500 Subject: Fix bootloader upload callback routine to write the correct number of bytes to flash. While we're at it, propagate error returns. --- projects/hsm/mgmt-bootloader.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/projects/hsm/mgmt-bootloader.c b/projects/hsm/mgmt-bootloader.c index a4783cc..738686e 100644 --- a/projects/hsm/mgmt-bootloader.c +++ b/projects/hsm/mgmt-bootloader.c @@ -52,7 +52,8 @@ static uint32_t dfu_offset; static int _flash_write_callback(uint8_t *buf, size_t len) { - stm_flash_write32(dfu_offset, (uint32_t *)buf, sizeof(buf)/4); + if (stm_flash_write32(dfu_offset, (uint32_t *)buf, (uint32_t)len/4) != 1) + return 0; dfu_offset += DFU_UPLOAD_CHUNK_SIZE; return 1; } @@ -67,10 +68,12 @@ static int cmd_bootloader_upload(struct cli_def *cli, const char *command, char uint8_t buf[DFU_UPLOAD_CHUNK_SIZE]; dfu_offset = DFU_BOOTLOADER_ADDR; - cli_receive_data(cli, buf, sizeof(buf), _flash_write_callback); - - cli_print(cli, "DFU offset now: %li (%li chunks)", dfu_offset, dfu_offset / DFU_UPLOAD_CHUNK_SIZE); - return CLI_OK; + int ret = cli_receive_data(cli, buf, sizeof(buf), _flash_write_callback); + if (ret == CLI_OK) { + cli_print(cli, "\nRebooting\n"); + HAL_NVIC_SystemReset(); + } + return ret; } void configure_cli_bootloader(struct cli_def *cli) -- cgit v1.2.3 From fa731c8e3730596c618367014ce6d30d6e4c265a Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Tue, 20 Dec 2016 19:06:20 -0500 Subject: The bootloader upgrade reboots now, so we don't need to log out of the CLI. --- projects/hsm/cryptech_upload | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/projects/hsm/cryptech_upload b/projects/hsm/cryptech_upload index 0c3f6fc..f5fefaa 100755 --- a/projects/hsm/cryptech_upload +++ b/projects/hsm/cryptech_upload @@ -212,10 +212,8 @@ def send_file(src, size, args, dst): if args.fpga: # tell the fpga to read its new configuration _execute(dst, "fpga reset") - - if args.fpga or args.bootloader: # log out of the CLI - # firmware upgrade reboots, doesn't need an exit + # (firmware/bootloader upgrades reboot, don't need an exit) _execute(dst, "exit") return True -- cgit v1.2.3 From fe94d97f705846ef809d4109b310c96d0b3bb1ac Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Tue, 20 Dec 2016 19:06:29 -0500 Subject: Make stm_flash_sector_num a little more efficient. --- stm-flash.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/stm-flash.c b/stm-flash.c index 991379b..fc79ea0 100644 --- a/stm-flash.c +++ b/stm-flash.c @@ -72,14 +72,15 @@ uint32_t flash_sector_offsets[FLASH_NUM_SECTORS + 1] = { int stm_flash_sector_num(const uint32_t offset) { - int i = FLASH_NUM_SECTORS; + int i; - while (i-- >= 0) { - if (offset >= flash_sector_offsets[i] && - offset < flash_sector_offsets[i + 1]) { + if (offset < flash_sector_offsets[0]) + return -1; + + for (i = 0; i < FLASH_NUM_SECTORS; ++i) + if (offset < flash_sector_offsets[i + 1]) return i; - } - } + return -1; } -- cgit v1.2.3