1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 23:08:14 +00:00

trezorhal: introduce flash_write_{byte,word}

This commit is contained in:
Pavol Rusnak 2017-09-28 11:27:58 +02:00
parent ef353f7bc6
commit d50eeb581a
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
6 changed files with 29 additions and 12 deletions

View File

@ -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();

View File

@ -1,5 +1,3 @@
#include STM32_HAL_H
#include <string.h>
#include <pb_decode.h>
@ -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;
}
}

View File

@ -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

View File

@ -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)

View File

@ -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);

2
vendor/norcow vendored

@ -1 +1 @@
Subproject commit 81c00e0a53792c5d2c29895824c861e5d4341737
Subproject commit 8d2843aeb58f8fd0fc5162a11d5be1e2575776ca