mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 23:48:12 +00:00
trezorhal: add flash_otp functions
This commit is contained in:
parent
027cc49f47
commit
66930f2e37
@ -60,7 +60,7 @@ bool copy_sdcard(void)
|
|||||||
display_printf("erasing flash ");
|
display_printf("erasing flash ");
|
||||||
|
|
||||||
// erase flash (except boardloader)
|
// erase flash (except boardloader)
|
||||||
if (0 != flash_erase_sectors(FLASH_SECTOR_BOARDLOADER_END + 1, FLASH_SECTOR_FIRMWARE_END, progress_callback)) {
|
if (!flash_erase_sectors(FLASH_SECTOR_BOARDLOADER_END + 1, FLASH_SECTOR_FIRMWARE_END, progress_callback)) {
|
||||||
display_printf(" failed\n");
|
display_printf(" failed\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -81,21 +81,25 @@ bool copy_sdcard(void)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
HAL_FLASH_Unlock();
|
if (!flash_unlock()) {
|
||||||
|
display_printf("could not unlock flash\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < (HEADER_SIZE + hdr.codelen) / SDCARD_BLOCK_SIZE; i++) {
|
for (int i = 0; i < (HEADER_SIZE + hdr.codelen) / SDCARD_BLOCK_SIZE; i++) {
|
||||||
sdcard_read_blocks((uint8_t *)buf, i, 1);
|
sdcard_read_blocks((uint8_t *)buf, i, 1);
|
||||||
for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
|
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 (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) {
|
||||||
display_printf("copy failed\n");
|
display_printf("copy failed\n");
|
||||||
sdcard_power_off();
|
sdcard_power_off();
|
||||||
HAL_FLASH_Lock();
|
flash_lock();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sdcard_power_off();
|
sdcard_power_off();
|
||||||
HAL_FLASH_Lock();
|
flash_lock();
|
||||||
|
|
||||||
display_printf("done\n");
|
display_printf("done\n");
|
||||||
|
|
||||||
|
@ -240,7 +240,13 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
|
|||||||
firmware_remaining = msg_recv.has_length ? msg_recv.length : 0;
|
firmware_remaining = msg_recv.has_length ? msg_recv.length : 0;
|
||||||
if (firmware_remaining > 0 && firmware_remaining % 4 == 0) {
|
if (firmware_remaining > 0 && firmware_remaining % 4 == 0) {
|
||||||
// erase flash
|
// erase flash
|
||||||
flash_erase_sectors(FLASH_SECTOR_FIRMWARE_START, FLASH_SECTOR_FIRMWARE_END, progress_erase);
|
if (!flash_erase_sectors(FLASH_SECTOR_FIRMWARE_START, FLASH_SECTOR_FIRMWARE_END, progress_erase)) {
|
||||||
|
MSG_SEND_INIT(Failure);
|
||||||
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
||||||
|
MSG_SEND_ASSIGN_STRING(message, "Could not erase flash");
|
||||||
|
MSG_SEND(Failure);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// request new firmware
|
// request new firmware
|
||||||
chunk_requested = (firmware_remaining > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_remaining;
|
chunk_requested = (firmware_remaining > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_remaining;
|
||||||
MSG_SEND_INIT(FirmwareRequest);
|
MSG_SEND_INIT(FirmwareRequest);
|
||||||
@ -284,11 +290,18 @@ static bool _read_payload(pb_istream_t *stream, const pb_field_t *field, void **
|
|||||||
|
|
||||||
void process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
|
void process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
|
||||||
{
|
{
|
||||||
HAL_FLASH_Unlock();
|
if (!flash_unlock()) {
|
||||||
|
MSG_SEND_INIT(Failure);
|
||||||
|
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
|
||||||
|
MSG_SEND_ASSIGN_STRING(message, "Could not unlock flash");
|
||||||
|
MSG_SEND(Failure);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
MSG_RECV_INIT(FirmwareUpload);
|
MSG_RECV_INIT(FirmwareUpload);
|
||||||
MSG_RECV_CALLBACK(payload, _read_payload);
|
MSG_RECV_CALLBACK(payload, _read_payload);
|
||||||
MSG_RECV(FirmwareUpload);
|
MSG_RECV(FirmwareUpload);
|
||||||
HAL_FLASH_Lock();
|
flash_lock();
|
||||||
|
|
||||||
if (chunk_size != chunk_requested) {
|
if (chunk_size != chunk_requested) {
|
||||||
MSG_SEND_INIT(Failure);
|
MSG_SEND_INIT(Failure);
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
#include STM32_HAL_H
|
#include STM32_HAL_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "flash.h"
|
||||||
|
|
||||||
int flash_init(void)
|
int flash_init(void)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
@ -39,12 +42,26 @@ void flash_set_option_bytes(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_erase_sectors(int start, int end, void (*progress)(uint16_t val))
|
bool flash_unlock(void)
|
||||||
{
|
{
|
||||||
HAL_FLASH_Unlock();
|
HAL_FLASH_Unlock();
|
||||||
FLASH_EraseInitTypeDef EraseInitStruct;
|
|
||||||
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
|
||||||
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
|
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool flash_lock(void)
|
||||||
|
{
|
||||||
|
HAL_FLASH_Lock();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool flash_erase_sectors(int start, int end, void (*progress)(uint16_t val))
|
||||||
|
{
|
||||||
|
if (!flash_unlock()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
FLASH_EraseInitTypeDef EraseInitStruct;
|
||||||
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
|
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
|
||||||
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
|
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
|
||||||
EraseInitStruct.NbSectors = 1;
|
EraseInitStruct.NbSectors = 1;
|
||||||
@ -52,13 +69,65 @@ int flash_erase_sectors(int start, int end, void (*progress)(uint16_t val))
|
|||||||
for (int i = start; i <= end; i++) {
|
for (int i = start; i <= end; i++) {
|
||||||
EraseInitStruct.Sector = i;
|
EraseInitStruct.Sector = i;
|
||||||
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
|
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
|
||||||
HAL_FLASH_Lock();
|
flash_lock();
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
if (progress) {
|
if (progress) {
|
||||||
progress(1000 * (i - start + 1) / (end - start + 1));
|
progress(1000 * (i - start + 1) / (end - start + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
HAL_FLASH_Lock();
|
flash_lock();
|
||||||
return 1;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define FLASH_OTP_LOCK_BASE 0x1FFF7A00U
|
||||||
|
#define FLASH_OTP_NUM_BLOCKS 16
|
||||||
|
#define FLASH_OTP_BLOCK_SIZE 32
|
||||||
|
|
||||||
|
bool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint8_t datalen)
|
||||||
|
{
|
||||||
|
if (block >= FLASH_OTP_NUM_BLOCKS || offset + datalen > FLASH_OTP_BLOCK_SIZE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!flash_unlock()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
HAL_StatusTypeDef 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) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
flash_lock();
|
||||||
|
return ret == HAL_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data, uint8_t datalen)
|
||||||
|
{
|
||||||
|
if (block >= FLASH_OTP_NUM_BLOCKS || offset + datalen > FLASH_OTP_BLOCK_SIZE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (uint8_t i = 0; i < datalen; i++) {
|
||||||
|
data[i] = *(__IO uint8_t *)(FLASH_OTP_BASE + block * FLASH_OTP_BLOCK_SIZE + offset + i);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool flash_otp_lock(uint8_t block)
|
||||||
|
{
|
||||||
|
if (block >= FLASH_OTP_NUM_BLOCKS) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!flash_unlock()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
HAL_StatusTypeDef ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, FLASH_OTP_LOCK_BASE + block, 0x00);
|
||||||
|
flash_lock();
|
||||||
|
return ret == HAL_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool flash_otp_is_locked(uint8_t block)
|
||||||
|
{
|
||||||
|
return *(__IO uint8_t *)(FLASH_OTP_LOCK_BASE + block) == 0x00;
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
#ifndef __TREZORHAL_FLASH_H__
|
#ifndef __TREZORHAL_FLASH_H__
|
||||||
#define __TREZORHAL_FLASH_H__
|
#define __TREZORHAL_FLASH_H__
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
int flash_init(void);
|
|
||||||
|
|
||||||
void flash_set_option_bytes(void);
|
|
||||||
|
|
||||||
#define FLASH_SECTOR_BOARDLOADER_START 0
|
#define FLASH_SECTOR_BOARDLOADER_START 0
|
||||||
#define FLASH_SECTOR_BOARDLOADER_END 1
|
#define FLASH_SECTOR_BOARDLOADER_END 1
|
||||||
|
|
||||||
@ -19,6 +16,18 @@ void flash_set_option_bytes(void);
|
|||||||
#define FLASH_SECTOR_FIRMWARE_START 5
|
#define FLASH_SECTOR_FIRMWARE_START 5
|
||||||
#define FLASH_SECTOR_FIRMWARE_END 11
|
#define FLASH_SECTOR_FIRMWARE_END 11
|
||||||
|
|
||||||
int flash_erase_sectors(int start, int end, void (*progress)(uint16_t val));
|
int flash_init(void);
|
||||||
|
|
||||||
|
void flash_set_option_bytes(void);
|
||||||
|
|
||||||
|
bool flash_unlock(void);
|
||||||
|
bool flash_lock(void);
|
||||||
|
|
||||||
|
bool flash_erase_sectors(int start, int end, void (*progress)(uint16_t val));
|
||||||
|
|
||||||
|
bool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint8_t datalen);
|
||||||
|
bool flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data, uint8_t datalen);
|
||||||
|
bool flash_otp_lock(uint8_t block);
|
||||||
|
bool flash_otp_is_locked(uint8_t block);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user