1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 06:18:07 +00:00
trezor-firmware/embed/trezorhal/flash.c

143 lines
3.5 KiB
C
Raw Normal View History

2017-03-07 13:50:09 +00:00
#include STM32_HAL_H
2017-09-27 09:08:46 +00:00
#include <string.h>
#include "flash.h"
2017-03-28 11:04:54 +00:00
int flash_init(void)
{
2017-03-07 14:52:19 +00:00
return 0;
2017-03-07 13:50:09 +00:00
}
#define WANTED_WRP (OB_WRP_SECTOR_0 | OB_WRP_SECTOR_1)
#define WANTED_RDP (OB_RDP_LEVEL_2)
#define WANTED_BOR (OB_BOR_LEVEL3)
void flash_set_option_bytes(void)
{
FLASH_OBProgramInitTypeDef opts;
HAL_FLASHEx_OBGetConfig(&opts);
opts.OptionType = 0;
if (opts.WRPSector != WANTED_WRP) {
opts.OptionType = OPTIONBYTE_WRP;
opts.WRPState = OB_WRPSTATE_ENABLE;
opts.WRPSector = WANTED_WRP;
opts.Banks = FLASH_BANK_1;
}
if (opts.RDPLevel != WANTED_RDP) {
opts.OptionType = OPTIONBYTE_RDP;
opts.RDPLevel = WANTED_RDP;
}
if (opts.BORLevel != WANTED_BOR) {
opts.OptionType = OPTIONBYTE_BOR;
opts.BORLevel = WANTED_BOR;
}
if (opts.OptionType != 0) {
HAL_FLASHEx_OBProgram(&opts);
}
}
2017-06-20 15:32:21 +00:00
2017-09-27 09:08:46 +00:00
bool flash_unlock(void)
2017-06-20 15:32:21 +00:00
{
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);
2017-09-27 09:08:46 +00:00
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;
2017-06-20 15:32:21 +00:00
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.NbSectors = 1;
uint32_t SectorError = 0;
for (int i = start; i <= end; i++) {
EraseInitStruct.Sector = i;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
2017-09-27 09:08:46 +00:00
flash_lock();
return false;
2017-06-20 15:32:21 +00:00
}
if (progress) {
progress(1000 * (i - start + 1) / (end - start + 1));
2017-06-20 15:32:21 +00:00
}
}
2017-09-27 09:08:46 +00:00
flash_lock();
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);
}
2017-09-27 09:08:46 +00:00
#define FLASH_OTP_LOCK_BASE 0x1FFF7A00U
#define FLASH_OTP_NUM_BLOCKS 16
#define FLASH_OTP_BLOCK_SIZE 32
2017-09-27 10:00:35 +00:00
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;
}
2017-09-27 09:08:46 +00:00
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;
}
bool ret = false;
2017-09-27 09:08:46 +00:00
for (uint8_t i = 0; i < datalen; i++) {
ret = flash_write_byte(FLASH_OTP_BASE + block * FLASH_OTP_BLOCK_SIZE + offset + i, data[i]);
if (!ret) {
2017-09-27 09:08:46 +00:00
break;
}
}
flash_lock();
return ret;
2017-09-27 09:08:46 +00:00
}
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;
2017-06-20 15:32:21 +00:00
}