trezor-core/embed/trezorhal/flash.c

110 lines
2.7 KiB
C
Raw Normal View History

2017-03-07 05:50:09 -08:00
#include STM32_HAL_H
2017-09-27 02:08:46 -07:00
#include <string.h>
#include "flash.h"
2017-03-28 04:04:54 -07:00
int flash_init(void)
{
2017-03-07 06:52:19 -08:00
return 0;
2017-03-07 05:50:09 -08:00
}
2017-09-27 02:08:46 -07:00
bool flash_unlock(void)
2017-06-20 08:32:21 -07: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 02:08:46 -07:00
return true;
}
bool flash_lock(void)
{
HAL_FLASH_Lock();
return true;
}
2017-10-16 11:24:28 -07:00
bool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len))
2017-09-27 02:08:46 -07:00
{
if (!flash_unlock()) {
return false;
}
FLASH_EraseInitTypeDef EraseInitStruct;
2017-06-20 08:32:21 -07:00
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.NbSectors = 1;
uint32_t SectorError = 0;
2017-10-16 11:24:28 -07:00
if (progress) {
progress(0, len);
}
for (int i = 0; i < len; i++) {
EraseInitStruct.Sector = sectors[i];
2017-06-20 08:32:21 -07:00
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
2017-09-27 02:08:46 -07:00
flash_lock();
return false;
2017-06-20 08:32:21 -07:00
}
if (progress) {
2017-10-16 11:24:28 -07:00
progress(i + 1, len);
2017-06-20 08:32:21 -07:00
}
}
2017-09-27 02:08:46 -07: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 02:08:46 -07:00
#define FLASH_OTP_LOCK_BASE 0x1FFF7A00U
2017-09-27 03:00:35 -07: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 02:08:46 -07: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 02:08:46 -07: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 02:08:46 -07:00
break;
}
}
flash_lock();
return ret;
2017-09-27 02:08:46 -07: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 08:32:21 -07:00
}