1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

trezorhal: add flash_otp functions

This commit is contained in:
Pavol Rusnak 2017-09-27 11:08:46 +02:00
parent 027cc49f47
commit 66930f2e37
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 113 additions and 18 deletions

View File

@ -60,7 +60,7 @@ bool copy_sdcard(void)
display_printf("erasing flash ");
// 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");
return false;
}
@ -81,21 +81,25 @@ bool copy_sdcard(void)
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++) {
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) {
display_printf("copy failed\n");
sdcard_power_off();
HAL_FLASH_Lock();
flash_lock();
return false;
}
}
}
sdcard_power_off();
HAL_FLASH_Lock();
flash_lock();
display_printf("done\n");

View File

@ -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;
if (firmware_remaining > 0 && firmware_remaining % 4 == 0) {
// 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
chunk_requested = (firmware_remaining > FIRMWARE_CHUNK_SIZE) ? FIRMWARE_CHUNK_SIZE : firmware_remaining;
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)
{
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_CALLBACK(payload, _read_payload);
MSG_RECV(FirmwareUpload);
HAL_FLASH_Lock();
flash_lock();
if (chunk_size != chunk_requested) {
MSG_SEND_INIT(Failure);

View File

@ -1,5 +1,8 @@
#include STM32_HAL_H
#include <string.h>
#include "flash.h"
int flash_init(void)
{
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();
FLASH_EraseInitTypeDef EraseInitStruct;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
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.VoltageRange = FLASH_VOLTAGE_RANGE_3;
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++) {
EraseInitStruct.Sector = i;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
HAL_FLASH_Lock();
return 0;
flash_lock();
return false;
}
if (progress) {
progress(1000 * (i - start + 1) / (end - start + 1));
}
}
HAL_FLASH_Lock();
return 1;
flash_lock();
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;
}

View File

@ -1,12 +1,9 @@
#ifndef __TREZORHAL_FLASH_H__
#define __TREZORHAL_FLASH_H__
#include <stdbool.h>
#include <stdint.h>
int flash_init(void);
void flash_set_option_bytes(void);
#define FLASH_SECTOR_BOARDLOADER_START 0
#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_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