diff --git a/embed/boardloader/main.c b/embed/boardloader/main.c index 4d05436829..59f05e4c92 100644 --- a/embed/boardloader/main.c +++ b/embed/boardloader/main.c @@ -89,7 +89,7 @@ bool copy_sdcard(void) for (int i = 0; i < (HEADER_SIZE + hdr.codelen) / SDCARD_BLOCK_SIZE; i++) { sdcard_read_blocks((uint8_t *)buf, i, 1); for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) { + if (!flash_write_word(BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j])) { display_printf("copy failed\n"); sdcard_power_off(); flash_lock(); diff --git a/embed/bootloader/messages.c b/embed/bootloader/messages.c index 0f4931905b..3d50ca66ff 100644 --- a/embed/bootloader/messages.c +++ b/embed/bootloader/messages.c @@ -1,5 +1,3 @@ -#include STM32_HAL_H - #include #include @@ -279,7 +277,7 @@ static bool _read_payload(pb_istream_t *stream, const pb_field_t *field, void ** } // write data for (int i = 0; i < BUFSIZE / sizeof(uint32_t); i++) { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FIRMWARE_START + firmware_flashed + chunk_written + i * sizeof(uint32_t), buf[i]) != HAL_OK) { + if (!flash_write_word(FIRMWARE_START + firmware_flashed + chunk_written + i * sizeof(uint32_t), buf[i])) { return false; } } diff --git a/embed/extmod/modtrezorconfig/norcow_config.h b/embed/extmod/modtrezorconfig/norcow_config.h index 49d68833c4..826011f905 100644 --- a/embed/extmod/modtrezorconfig/norcow_config.h +++ b/embed/extmod/modtrezorconfig/norcow_config.h @@ -1,10 +1,18 @@ #if defined TREZOR_STM32 + #define NORCOW_STM32 1 -#define NORCOW_START_SECTOR 2 + +#define NORCOW_START_SECTOR 2 #define NORCOW_START_ADDRESS 0x08008000 + #elif defined TREZOR_UNIX + #define NORCOW_UNIX 1 + #define NORCOW_FILE "/var/tmp/trezor.config" + #else + #error Unsupported TREZOR port. Only STM32 and UNIX ports are supported. + #endif diff --git a/embed/trezorhal/flash.c b/embed/trezorhal/flash.c index 01f02873fd..1b5436b036 100644 --- a/embed/trezorhal/flash.c +++ b/embed/trezorhal/flash.c @@ -45,8 +45,7 @@ void flash_set_option_bytes(void) bool flash_unlock(void) { HAL_FLASH_Unlock(); - __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | - FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); return true; } @@ -80,6 +79,16 @@ bool flash_erase_sectors(int start, int end, void (*progress)(uint16_t val)) return true; } +bool flash_write_byte(uint32_t address, uint8_t data) +{ + return HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, data); +} + +bool flash_write_word(uint32_t address, uint32_t data) +{ + return HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data); +} + #define FLASH_OTP_LOCK_BASE 0x1FFF7A00U #define FLASH_OTP_NUM_BLOCKS 16 #define FLASH_OTP_BLOCK_SIZE 32 @@ -103,15 +112,15 @@ bool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint8_t if (!flash_unlock()) { return false; } - HAL_StatusTypeDef ret; + bool ret; for (uint8_t i = 0; i < datalen; i++) { - ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, FLASH_OTP_BASE + block * FLASH_OTP_BLOCK_SIZE + offset + i, data[i]); - if (ret != HAL_OK) { + ret = flash_write_byte(FLASH_OTP_BASE + block * FLASH_OTP_BLOCK_SIZE + offset + i, data[i]); + if (!ret) { break; } } flash_lock(); - return ret == HAL_OK; + return ret; } bool flash_otp_lock(uint8_t block) diff --git a/embed/trezorhal/flash.h b/embed/trezorhal/flash.h index fb0fcc4145..c589d12d0a 100644 --- a/embed/trezorhal/flash.h +++ b/embed/trezorhal/flash.h @@ -24,6 +24,8 @@ bool flash_unlock(void); bool flash_lock(void); bool flash_erase_sectors(int start, int end, void (*progress)(uint16_t val)); +bool flash_write_byte(uint32_t address, uint8_t data); +bool flash_write_word(uint32_t address, uint32_t data); bool flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data, uint8_t datalen); bool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint8_t datalen); diff --git a/vendor/norcow b/vendor/norcow index 81c00e0a53..8d2843aeb5 160000 --- a/vendor/norcow +++ b/vendor/norcow @@ -1 +1 @@ -Subproject commit 81c00e0a53792c5d2c29895824c861e5d4341737 +Subproject commit 8d2843aeb58f8fd0fc5162a11d5be1e2575776ca