2019-01-25 10:58:23 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2019-01-25 10:58:23 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libopencm3/stm32/flash.h>
|
2019-03-29 16:10:31 +00:00
|
|
|
#include <string.h>
|
2019-01-25 10:58:23 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "flash.h"
|
2023-11-01 12:40:50 +00:00
|
|
|
#include "flash_area.h"
|
2019-01-25 10:58:23 +00:00
|
|
|
#include "memory.h"
|
|
|
|
#include "supervise.h"
|
|
|
|
|
2024-02-26 13:56:30 +00:00
|
|
|
#define FLASH_BURST_WORDS (4 * 8)
|
|
|
|
#define FLASH_BURST_SIZE (FLASH_BURST_WORDS * sizeof(uint32_t))
|
|
|
|
|
2023-06-30 10:12:55 +00:00
|
|
|
#define STORAGE_AREAS_COUNT 2
|
|
|
|
|
2019-01-25 10:58:23 +00:00
|
|
|
static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
|
2019-03-29 16:10:31 +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, // last element - not a valid sector
|
2019-01-25 10:58:23 +00:00
|
|
|
};
|
|
|
|
|
2023-06-30 10:12:55 +00:00
|
|
|
const flash_area_t STORAGE_AREAS[STORAGE_AREAS_COUNT] = {
|
|
|
|
{
|
|
|
|
.num_subareas = 1,
|
|
|
|
.subarea[0] =
|
|
|
|
{
|
|
|
|
.first_sector = 2,
|
|
|
|
.num_sectors = 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.num_subareas = 1,
|
|
|
|
.subarea[0] =
|
|
|
|
{
|
|
|
|
.first_sector = 3,
|
|
|
|
.num_sectors = 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static secbool flash_check_success(uint32_t status) {
|
|
|
|
return (status & (FLASH_SR_PGAERR | FLASH_SR_PGPERR | FLASH_SR_PGSERR |
|
|
|
|
FLASH_SR_WRPERR))
|
|
|
|
? secfalse
|
|
|
|
: sectrue;
|
2019-01-25 10:58:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
secbool flash_unlock_write(void) {
|
|
|
|
svc_flash_unlock();
|
|
|
|
return sectrue;
|
2019-01-25 10:58:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
secbool flash_lock_write(void) { return flash_check_success(svc_flash_lock()); }
|
|
|
|
|
2023-06-30 10:12:55 +00:00
|
|
|
const void *flash_get_address(uint16_t sector, uint32_t offset, uint32_t size) {
|
2019-03-29 16:10:31 +00:00
|
|
|
if (sector >= FLASH_SECTOR_COUNT) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const uint32_t addr = FLASH_SECTOR_TABLE[sector] + offset;
|
|
|
|
const uint32_t next = FLASH_SECTOR_TABLE[sector + 1];
|
|
|
|
if (addr + size > next) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (const void *)FLASH_PTR(addr);
|
2019-01-25 10:58:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 12:40:50 +00:00
|
|
|
uint32_t flash_sector_size(uint16_t first_sector, uint16_t sector_count) {
|
|
|
|
if (first_sector + sector_count >= FLASH_SECTOR_COUNT) {
|
2022-04-26 17:41:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2023-11-01 12:40:50 +00:00
|
|
|
return FLASH_SECTOR_TABLE[first_sector + sector_count] -
|
|
|
|
FLASH_SECTOR_TABLE[first_sector];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t flash_sector_find(uint16_t first_sector, uint32_t offset) {
|
|
|
|
uint16_t sector = first_sector;
|
|
|
|
|
|
|
|
while (sector < FLASH_SECTOR_COUNT) {
|
|
|
|
uint32_t sector_size =
|
|
|
|
FLASH_SECTOR_TABLE[sector + 1] - FLASH_SECTOR_TABLE[sector];
|
|
|
|
|
|
|
|
if (offset < sector_size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset -= sector_size;
|
|
|
|
sector++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sector;
|
2022-04-26 17:41:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 20:01:36 +00:00
|
|
|
secbool flash_write_byte(uint16_t sector, uint32_t offset, uint8_t data) {
|
2019-03-29 16:10:31 +00:00
|
|
|
uint8_t *address = (uint8_t *)flash_get_address(sector, offset, 1);
|
|
|
|
if (address == NULL) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
if ((*address & data) != data) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
svc_flash_program(FLASH_CR_PROGRAM_X8);
|
|
|
|
*(volatile uint8_t *)address = data;
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
if (*address != data) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
return sectrue;
|
2019-01-25 10:58:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 20:01:36 +00:00
|
|
|
secbool flash_write_word(uint16_t sector, uint32_t offset, uint32_t data) {
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t *address = (uint32_t *)flash_get_address(sector, offset, 4);
|
|
|
|
if (address == NULL) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
if (offset % 4 != 0) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
if ((*address & data) != data) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
svc_flash_program(FLASH_CR_PROGRAM_X32);
|
|
|
|
*(volatile uint32_t *)address = data;
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
if (*address != data) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
return sectrue;
|
2019-01-25 10:58:23 +00:00
|
|
|
}
|
2023-06-30 10:12:55 +00:00
|
|
|
|
2024-02-26 13:56:30 +00:00
|
|
|
secbool flash_write_burst(uint16_t sector, uint32_t offset,
|
|
|
|
const uint32_t *data) {
|
|
|
|
if ((offset % FLASH_BURST_SIZE) != 0) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < FLASH_BURST_WORDS; i++) {
|
|
|
|
if (sectrue !=
|
|
|
|
flash_write_word(sector, offset + i * sizeof(uint32_t), data[i])) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sectrue;
|
|
|
|
}
|
|
|
|
|
2023-12-27 22:29:22 +00:00
|
|
|
secbool flash_write_block(uint16_t sector, uint32_t offset,
|
|
|
|
const flash_block_t block) {
|
|
|
|
return flash_write_word(sector, offset, block[0]);
|
|
|
|
}
|
|
|
|
|
2023-11-01 12:40:50 +00:00
|
|
|
secbool flash_sector_erase(uint16_t sector) {
|
|
|
|
svc_flash_erase_sector(sector);
|
2023-06-30 10:12:55 +00:00
|
|
|
return sectrue;
|
|
|
|
}
|