2017-10-18 16:30:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, Jan Pochyla, SatoshiLabs
|
|
|
|
*
|
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
|
|
|
*/
|
|
|
|
|
2017-03-07 13:50:09 +00:00
|
|
|
#include STM32_HAL_H
|
|
|
|
|
2017-09-27 09:08:46 +00:00
|
|
|
#include <string.h>
|
2017-10-18 16:30:53 +00:00
|
|
|
|
2017-12-16 16:54:04 +00:00
|
|
|
#include "common.h"
|
2017-09-27 09:08:46 +00:00
|
|
|
#include "flash.h"
|
|
|
|
|
2017-10-16 19:43:07 +00:00
|
|
|
// see docs/memory.md for more information
|
|
|
|
|
2017-11-05 22:51:10 +00:00
|
|
|
static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
|
2017-10-16 19:43:07 +00:00
|
|
|
[ 0] = 0x08000000, // - 0x08003FFF | 16 KiB
|
|
|
|
[ 1] = 0x08004000, // - 0x08007FFF | 16 KiB
|
|
|
|
[ 2] = 0x08008000, // - 0x0800BFFF | 16 KiB
|
|
|
|
[ 3] = 0x0800C000, // - 0x0800FFFF | 16 KiB
|
|
|
|
[ 4] = 0x08010000, // - 0x0801FFFF | 64 KiB
|
|
|
|
[ 5] = 0x08020000, // - 0x0803FFFF | 128 KiB
|
|
|
|
[ 6] = 0x08040000, // - 0x0805FFFF | 128 KiB
|
|
|
|
[ 7] = 0x08060000, // - 0x0807FFFF | 128 KiB
|
|
|
|
[ 8] = 0x08080000, // - 0x0809FFFF | 128 KiB
|
|
|
|
[ 9] = 0x080A0000, // - 0x080BFFFF | 128 KiB
|
|
|
|
[10] = 0x080C0000, // - 0x080DFFFF | 128 KiB
|
|
|
|
[11] = 0x080E0000, // - 0x080FFFFF | 128 KiB
|
|
|
|
[12] = 0x08100000, // - 0x08103FFF | 16 KiB
|
|
|
|
[13] = 0x08104000, // - 0x08107FFF | 16 KiB
|
|
|
|
[14] = 0x08108000, // - 0x0810BFFF | 16 KiB
|
|
|
|
[15] = 0x0810C000, // - 0x0810FFFF | 16 KiB
|
|
|
|
[16] = 0x08110000, // - 0x0811FFFF | 64 KiB
|
|
|
|
[17] = 0x08120000, // - 0x0813FFFF | 128 KiB
|
|
|
|
[18] = 0x08140000, // - 0x0815FFFF | 128 KiB
|
|
|
|
[19] = 0x08160000, // - 0x0817FFFF | 128 KiB
|
|
|
|
[20] = 0x08180000, // - 0x0819FFFF | 128 KiB
|
|
|
|
[21] = 0x081A0000, // - 0x081BFFFF | 128 KiB
|
|
|
|
[22] = 0x081C0000, // - 0x081DFFFF | 128 KiB
|
|
|
|
[23] = 0x081E0000, // - 0x081FFFFF | 128 KiB
|
|
|
|
[24] = 0x08200000, // last element - not a valid sector
|
|
|
|
};
|
|
|
|
|
2017-12-09 13:48:49 +00:00
|
|
|
void flash_init(void)
|
2017-10-24 11:54:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool flash_unlock(void)
|
2017-06-20 15:32:21 +00:00
|
|
|
{
|
|
|
|
HAL_FLASH_Unlock();
|
2017-10-31 13:35:41 +00:00
|
|
|
FLASH->SR |= FLASH_STATUS_ALL_FLAGS; // clear all status flags
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue;
|
2017-09-27 09:08:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool flash_lock(void)
|
2017-09-27 09:08:46 +00:00
|
|
|
{
|
|
|
|
HAL_FLASH_Lock();
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue;
|
2017-09-27 09:08:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 16:30:53 +00:00
|
|
|
const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size)
|
|
|
|
{
|
2017-10-27 15:49:30 +00:00
|
|
|
if (sector >= FLASH_SECTOR_COUNT) {
|
2017-10-18 16:30:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-10-27 15:49:30 +00:00
|
|
|
uint32_t addr = FLASH_SECTOR_TABLE[sector];
|
|
|
|
uint32_t next = FLASH_SECTOR_TABLE[sector + 1];
|
2017-11-05 22:51:10 +00:00
|
|
|
if (addr + offset + size > next) {
|
2017-10-18 16:30:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (const uint8_t *)addr + offset;
|
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len))
|
2017-09-27 09:08:46 +00:00
|
|
|
{
|
2017-12-16 17:42:06 +00:00
|
|
|
ensure(flash_unlock(), NULL);
|
2017-09-27 09:08:46 +00:00
|
|
|
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;
|
2017-10-16 18:24:28 +00:00
|
|
|
if (progress) {
|
|
|
|
progress(0, len);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
EraseInitStruct.Sector = sectors[i];
|
2017-10-27 16:45:09 +00:00
|
|
|
uint32_t SectorError;
|
2017-06-20 15:32:21 +00:00
|
|
|
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
|
2017-12-16 16:54:04 +00:00
|
|
|
ensure(flash_lock(), NULL);
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-06-20 15:32:21 +00:00
|
|
|
}
|
2017-10-16 19:43:07 +00:00
|
|
|
// check whether the sector was really deleted (contains only 0xFF)
|
2017-11-05 22:51:10 +00:00
|
|
|
const uint32_t addr_start = FLASH_SECTOR_TABLE[sectors[i]], addr_end = FLASH_SECTOR_TABLE[sectors[i] + 1];
|
2017-10-16 19:43:07 +00:00
|
|
|
for (uint32_t addr = addr_start; addr < addr_end; addr += 4) {
|
|
|
|
if (*((const uint32_t *)addr) != 0xFFFFFFFF) {
|
2017-12-16 16:54:04 +00:00
|
|
|
ensure(flash_lock(), NULL);
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-10-16 19:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-20 15:32:21 +00:00
|
|
|
if (progress) {
|
2017-10-16 18:24:28 +00:00
|
|
|
progress(i + 1, len);
|
2017-06-20 15:32:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-16 16:54:04 +00:00
|
|
|
ensure(flash_lock(), NULL);
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue;
|
2017-09-27 09:08:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 12:43:18 +00:00
|
|
|
secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data)
|
2017-09-28 09:27:58 +00:00
|
|
|
{
|
2018-01-23 12:43:18 +00:00
|
|
|
uint32_t address = (uint32_t)flash_get_address(sector, offset, 1);
|
|
|
|
if (address == 0) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue * (HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, data));
|
2017-09-28 09:27:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 12:43:18 +00:00
|
|
|
secbool flash_write_word(uint8_t sector, uint32_t offset, uint32_t data)
|
2017-10-16 16:33:05 +00:00
|
|
|
{
|
2018-01-23 12:43:18 +00:00
|
|
|
uint32_t address = (uint32_t)flash_get_address(sector, offset, 4);
|
|
|
|
if (address == 0) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2017-10-16 16:33:05 +00:00
|
|
|
if (offset % 4 != 0) {
|
2017-10-18 16:30:53 +00:00
|
|
|
return secfalse;
|
2017-10-16 16:33:05 +00:00
|
|
|
}
|
2018-01-23 12:43:18 +00:00
|
|
|
return sectrue * (HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data));
|
2017-10-16 16:33:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 09:08:46 +00:00
|
|
|
#define FLASH_OTP_LOCK_BASE 0x1FFF7A00U
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool flash_otp_read(uint8_t block, uint8_t offset, uint8_t *data, uint8_t datalen)
|
2017-09-27 10:00:35 +00:00
|
|
|
{
|
|
|
|
if (block >= FLASH_OTP_NUM_BLOCKS || offset + datalen > FLASH_OTP_BLOCK_SIZE) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-09-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
for (uint8_t i = 0; i < datalen; i++) {
|
|
|
|
data[i] = *(__IO uint8_t *)(FLASH_OTP_BASE + block * FLASH_OTP_BLOCK_SIZE + offset + i);
|
|
|
|
}
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue;
|
2017-09-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool flash_otp_write(uint8_t block, uint8_t offset, const uint8_t *data, uint8_t datalen)
|
2017-09-27 09:08:46 +00:00
|
|
|
{
|
|
|
|
if (block >= FLASH_OTP_NUM_BLOCKS || offset + datalen > FLASH_OTP_BLOCK_SIZE) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-09-27 09:08:46 +00:00
|
|
|
}
|
2017-12-16 17:42:06 +00:00
|
|
|
ensure(flash_unlock(), NULL);
|
2017-09-27 09:08:46 +00:00
|
|
|
for (uint8_t i = 0; i < datalen; i++) {
|
2018-01-23 12:43:18 +00:00
|
|
|
uint32_t address = FLASH_OTP_BASE + block * FLASH_OTP_BLOCK_SIZE + offset + i;
|
|
|
|
ensure(sectrue * (HAL_OK == HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, data[i])), NULL);
|
2017-09-27 09:08:46 +00:00
|
|
|
}
|
2017-12-16 16:54:04 +00:00
|
|
|
ensure(flash_lock(), NULL);
|
2018-01-29 07:44:55 +00:00
|
|
|
return sectrue;
|
2017-09-27 09:08:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool flash_otp_lock(uint8_t block)
|
2017-09-27 09:08:46 +00:00
|
|
|
{
|
|
|
|
if (block >= FLASH_OTP_NUM_BLOCKS) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-09-27 09:08:46 +00:00
|
|
|
}
|
2017-12-16 17:42:06 +00:00
|
|
|
ensure(flash_unlock(), NULL);
|
2017-09-27 09:08:46 +00:00
|
|
|
HAL_StatusTypeDef ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, FLASH_OTP_LOCK_BASE + block, 0x00);
|
2017-12-16 16:54:04 +00:00
|
|
|
ensure(flash_lock(), NULL);
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue * (ret == HAL_OK);
|
2017-09-27 09:08:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool flash_otp_is_locked(uint8_t block)
|
2017-09-27 09:08:46 +00:00
|
|
|
{
|
|
|
|
return *(__IO uint8_t *)(FLASH_OTP_LOCK_BASE + block) == 0x00;
|
2017-06-20 15:32:21 +00:00
|
|
|
}
|