From a7394ef170475fe0ef16b6689b46ad445aeed079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 24 Sep 2021 22:43:47 +0200 Subject: [PATCH] Feature #41: Add SHA calculation to updater. Has yet to be checked against file. Code not tested --- stm-firmware/updater/ram-code/CMakeLists.txt | 5 +- stm-firmware/updater/ram-code/itoa.c | 34 ++++++++++++ stm-firmware/updater/ram-code/itoa.h | 3 + stm-firmware/updater/ram-code/main.c | 58 ++++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/stm-firmware/updater/ram-code/CMakeLists.txt b/stm-firmware/updater/ram-code/CMakeLists.txt index 65fd1ab..ddd12a2 100644 --- a/stm-firmware/updater/ram-code/CMakeLists.txt +++ b/stm-firmware/updater/ram-code/CMakeLists.txt @@ -21,13 +21,14 @@ set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/ram-link.ld) set(ELFFILE "${PROJECT_NAME}.elf") aux_source_directory("." SRCS) aux_source_directory("fatfs" FATFS_SRCS) +aux_source_directory("3rd-party/sha256" SHA256_SRCS) aux_source_directory("fatfs/shimatta_sdio_driver" SDIO_SRCS) set(STM_PERIPH_SRCS "../../stm-periph/backup-ram.c" "../../stm-periph/rcc-manager.c" "../../stm-periph/crc-unit.c") set(SAFETY_MEMORY_SRCS "../../safety/safety-memory.c") -add_executable(${ELFFILE} ${SRCS} ${FATFS_SRCS} ${SDIO_SRCS} ${STM_PERIPH_SRCS} ${SAFETY_MEMORY_SRCS}) +add_executable(${ELFFILE} ${SRCS} ${FATFS_SRCS} ${SDIO_SRCS} ${STM_PERIPH_SRCS} ${SAFETY_MEMORY_SRCS} ${SHA256_SRCS}) -target_include_directories(${ELFFILE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ../../include) +target_include_directories(${ELFFILE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ../../include ${CMAKE_CURRENT_SOURCE_DIR}/3rd-party) target_compile_options(${ELFFILE} PRIVATE -Wall -Wextra -Wold-style-declaration -Wuninitialized -Wmaybe-uninitialized -Wunused-parameter) target_compile_options(${ELFFILE} PRIVATE -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -nostartfiles -Wimplicit-fallthrough=3 -Wsign-compare -Os -g3) target_compile_definitions(${ELFFILE} PRIVATE -DGIT_VER=${GIT_DESCRIBE} -DHSE_VALUE=8000000UL -DSTM32F407xx -DSTM32F4XX -DARM_MATH_CM4 -DSAFETY_MEMORY_STRIPOUT_DUMP) diff --git a/stm-firmware/updater/ram-code/itoa.c b/stm-firmware/updater/ram-code/itoa.c index d74fed1..e645dd9 100644 --- a/stm-firmware/updater/ram-code/itoa.c +++ b/stm-firmware/updater/ram-code/itoa.c @@ -51,3 +51,37 @@ uint32_t heapless_itoa(int32_t value, char *buffer, uint32_t base) return bufferIdx; } +static char num_to_hex_digit(uint8_t num, bool capitalized) +{ + char ret = 'U'; + + switch (num) { + case 0 ... 9: + ret = '0' + num; + break; + case 0xA ... 0xF: + if (capitalized) + ret = 'A' + num - 0xA; + else + ret = 'a' + num - 0xA; + break; + default: + break; + } + + return ret; +} + +uint32_t bytes_to_hex_string(uint8_t *input, uint32_t count, char *output_buffer, bool capitalized) +{ + uint32_t idx; + uint8_t b; + + for (idx = 0; idx < count; idx++) { + b = input[idx]; + output_buffer[idx] = num_to_hex_digit(b >> 4, capitalized); + output_buffer[idx+1] = num_to_hex_digit(b & 0xF, capitalized); + } + + return 0; +} diff --git a/stm-firmware/updater/ram-code/itoa.h b/stm-firmware/updater/ram-code/itoa.h index 5397908..3b4c0a8 100644 --- a/stm-firmware/updater/ram-code/itoa.h +++ b/stm-firmware/updater/ram-code/itoa.h @@ -2,7 +2,10 @@ #define _ITOA_H_ #include +#include uint32_t heapless_itoa(int32_t value, char *buffer, uint32_t base); +uint32_t bytes_to_hex_string(uint8_t *input, uint32_t count, char *output_buffer, bool capitalized); + #endif /* _ITOA_H_ */ diff --git a/stm-firmware/updater/ram-code/main.c b/stm-firmware/updater/ram-code/main.c index f237b80..12cabd8 100644 --- a/stm-firmware/updater/ram-code/main.c +++ b/stm-firmware/updater/ram-code/main.c @@ -16,6 +16,7 @@ #include #include "uart.h" #include "itoa.h" +#include static volatile unsigned int wait_tick; @@ -197,6 +198,49 @@ exit_parser_close: return retval; } +static int compute_sha256_of_file(const char *fname, uint8_t *sha256_out) +{ + FIL _file; + int ret = 0; + FIL *file = &_file; + FRESULT fres; + static char workbuff[1024]; + UINT act_read; + struct Sha_256 sha; + + if (!fname || !sha256_out) + return -1000; + + fres = f_open(file, fname, FA_READ); + if (fres != FR_OK) { + ret = -1; + goto ret_noact; + } + + sha_256_init(&sha, sha256_out); + do { + fres = f_read(file, workbuff, sizeof(workbuff), &act_read); + if (act_read > 0) { + sha_256_write(&sha, workbuff, (size_t)act_read); + } else { + if (f_eof(file)) { + break; + } else if (f_error(file)) { + ret = -2; + goto ret_close_file; + } + } + } while (1); + + (void)sha_256_close(&sha); + ret = 0; + +ret_close_file: + (void)f_close(file); +ret_noact: + return ret; +} + int ram_code_main(void) { FRESULT fres; @@ -207,6 +251,7 @@ int ram_code_main(void) uint32_t count; uint32_t update_size; int retries = 3; + uint8_t sha_hash[SIZE_OF_SHA_256_HASH]; SysTick_Config(168000UL); @@ -237,6 +282,19 @@ int ram_code_main(void) if (res) ram_code_exit(false); + uart_send_string("Calculating SHA256 checksum\r\n"); + if (compute_sha256_of_file(filename, sha_hash)) { + uart_send_string("Calculation failed!"); + ram_code_exit(false); + } + + uart_send_string("SHA256: "); + bytes_to_hex_string(sha_hash, SIZE_OF_SHA_256_HASH, tmp_buff, false); + tmp_buff[SIZE_OF_SHA_256_HASH] = 0; + uart_send_string(tmp_buff); + uart_send_string("\r\n"); + + uart_send_string("Checking hex file "); uart_send_string(filename); uart_send_string("\r\n");