mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 23:48:12 +00:00
65 lines
1.7 KiB
C
65 lines
1.7 KiB
C
#include STM32_HAL_H
|
|
|
|
int flash_init(void)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
#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);
|
|
}
|
|
}
|
|
|
|
int flash_erase_sectors(int start, int end, void (*progress)(uint16_t val))
|
|
{
|
|
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);
|
|
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) {
|
|
HAL_FLASH_Lock();
|
|
return 0;
|
|
}
|
|
if (progress) {
|
|
progress(1000 * (i - start + 1) / (end - start + 1));
|
|
}
|
|
}
|
|
HAL_FLASH_Lock();
|
|
return 1;
|
|
}
|