mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-16 17:42:02 +00:00
trezorhal: refactor erasing sectors
This commit is contained in:
parent
cbe8320d96
commit
3d82cca381
@ -105,9 +105,9 @@ static secbool copy_sdcard(void)
|
|||||||
|
|
||||||
// erase all flash (except boardloader)
|
// erase all flash (except boardloader)
|
||||||
static const uint8_t sectors[] = {
|
static const uint8_t sectors[] = {
|
||||||
3,
|
|
||||||
FLASH_SECTOR_STORAGE_1,
|
FLASH_SECTOR_STORAGE_1,
|
||||||
FLASH_SECTOR_STORAGE_2,
|
FLASH_SECTOR_STORAGE_2,
|
||||||
|
3,
|
||||||
FLASH_SECTOR_BOOTLOADER,
|
FLASH_SECTOR_BOOTLOADER,
|
||||||
FLASH_SECTOR_FIRMWARE_START,
|
FLASH_SECTOR_FIRMWARE_START,
|
||||||
7,
|
7,
|
||||||
@ -168,12 +168,8 @@ int main(void)
|
|||||||
periph_init();
|
periph_init();
|
||||||
|
|
||||||
if (sectrue != flash_configure_option_bytes()) {
|
if (sectrue != flash_configure_option_bytes()) {
|
||||||
static const uint8_t sectors[] = {
|
|
||||||
FLASH_SECTOR_STORAGE_1,
|
|
||||||
FLASH_SECTOR_STORAGE_2,
|
|
||||||
};
|
|
||||||
// display is not initialized so don't call ensure
|
// display is not initialized so don't call ensure
|
||||||
secbool r = flash_erase_sectors(sectors, sizeof(sectors), NULL);
|
secbool r = flash_erase_sectors(STORAGE_SECTORS, STORAGE_SECTORS_COUNT, NULL);
|
||||||
(void)r;
|
(void)r;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
@ -262,7 +262,7 @@ main_start:
|
|||||||
firmware_present = load_image_header((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), FIRMWARE_IMAGE_MAGIC, FIRMWARE_IMAGE_MAXSIZE, vhdr.vsig_m, vhdr.vsig_n, vhdr.vpub, &hdr);
|
firmware_present = load_image_header((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), FIRMWARE_IMAGE_MAGIC, FIRMWARE_IMAGE_MAXSIZE, vhdr.vsig_m, vhdr.vsig_n, vhdr.vpub, &hdr);
|
||||||
}
|
}
|
||||||
if (sectrue == firmware_present) {
|
if (sectrue == firmware_present) {
|
||||||
firmware_present = check_image_contents(&hdr, IMAGE_HEADER_SIZE + vhdr.hdrlen, firmware_sectors, FIRMWARE_SECTORS_COUNT);
|
firmware_present = check_image_contents(&hdr, IMAGE_HEADER_SIZE + vhdr.hdrlen, FIRMWARE_SECTORS, FIRMWARE_SECTORS_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// start the bootloader if no or broken firmware found ...
|
// start the bootloader if no or broken firmware found ...
|
||||||
@ -286,11 +286,7 @@ main_start:
|
|||||||
ui_fadein();
|
ui_fadein();
|
||||||
|
|
||||||
// erase storage
|
// erase storage
|
||||||
static const uint8_t sectors_storage[] = {
|
ensure(flash_erase_sectors(STORAGE_SECTORS, STORAGE_SECTORS_COUNT, NULL), NULL);
|
||||||
FLASH_SECTOR_STORAGE_1,
|
|
||||||
FLASH_SECTOR_STORAGE_2,
|
|
||||||
};
|
|
||||||
ensure(flash_erase_sectors(sectors_storage, sizeof(sectors_storage), NULL), NULL);
|
|
||||||
|
|
||||||
// and start the usb loop
|
// and start the usb loop
|
||||||
if (bootloader_usb_loop(NULL, NULL) != sectrue) {
|
if (bootloader_usb_loop(NULL, NULL) != sectrue) {
|
||||||
@ -353,7 +349,7 @@ main_start:
|
|||||||
"invalid firmware header");
|
"invalid firmware header");
|
||||||
|
|
||||||
ensure(
|
ensure(
|
||||||
check_image_contents(&hdr, IMAGE_HEADER_SIZE + vhdr.hdrlen, firmware_sectors, FIRMWARE_SECTORS_COUNT),
|
check_image_contents(&hdr, IMAGE_HEADER_SIZE + vhdr.hdrlen, FIRMWARE_SECTORS, FIRMWARE_SECTORS_COUNT),
|
||||||
"invalid firmware hash");
|
"invalid firmware hash");
|
||||||
|
|
||||||
// if all VTRUST flags are unset = ultimate trust => skip the procedure
|
// if all VTRUST flags are unset = ultimate trust => skip the procedure
|
||||||
|
@ -39,22 +39,6 @@
|
|||||||
#define MSG_HEADER1_LEN 9
|
#define MSG_HEADER1_LEN 9
|
||||||
#define MSG_HEADER2_LEN 1
|
#define MSG_HEADER2_LEN 1
|
||||||
|
|
||||||
const uint8_t firmware_sectors[FIRMWARE_SECTORS_COUNT] = {
|
|
||||||
FLASH_SECTOR_FIRMWARE_START,
|
|
||||||
7,
|
|
||||||
8,
|
|
||||||
9,
|
|
||||||
10,
|
|
||||||
FLASH_SECTOR_FIRMWARE_END,
|
|
||||||
FLASH_SECTOR_FIRMWARE_EXTRA_START,
|
|
||||||
18,
|
|
||||||
19,
|
|
||||||
20,
|
|
||||||
21,
|
|
||||||
22,
|
|
||||||
FLASH_SECTOR_FIRMWARE_EXTRA_END,
|
|
||||||
};
|
|
||||||
|
|
||||||
secbool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size)
|
secbool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size)
|
||||||
{
|
{
|
||||||
if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') {
|
if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') {
|
||||||
@ -479,13 +463,9 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
|
|||||||
|
|
||||||
// if firmware is not upgrade, erase storage
|
// if firmware is not upgrade, erase storage
|
||||||
if (sectrue != is_upgrade) {
|
if (sectrue != is_upgrade) {
|
||||||
static const uint8_t sectors_storage[] = {
|
ensure(flash_erase_sectors(STORAGE_SECTORS, STORAGE_SECTORS_COUNT, NULL), NULL);
|
||||||
FLASH_SECTOR_STORAGE_1,
|
|
||||||
FLASH_SECTOR_STORAGE_2,
|
|
||||||
};
|
|
||||||
ensure(flash_erase_sectors(sectors_storage, sizeof(sectors_storage), NULL), NULL);
|
|
||||||
}
|
}
|
||||||
ensure(flash_erase_sectors(firmware_sectors, FIRMWARE_SECTORS_COUNT, ui_screen_install_progress_erase), NULL);
|
ensure(flash_erase_sectors(FIRMWARE_SECTORS, FIRMWARE_SECTORS_COUNT, ui_screen_install_progress_erase), NULL);
|
||||||
|
|
||||||
firstskip = IMAGE_HEADER_SIZE + vhdr.hdrlen;
|
firstskip = IMAGE_HEADER_SIZE + vhdr.hdrlen;
|
||||||
}
|
}
|
||||||
@ -521,7 +501,7 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
|
|||||||
|
|
||||||
const uint32_t * const src = (const uint32_t * const)chunk_buffer;
|
const uint32_t * const src = (const uint32_t * const)chunk_buffer;
|
||||||
for (int i = 0; i < chunk_size / sizeof(uint32_t); i++) {
|
for (int i = 0; i < chunk_size / sizeof(uint32_t); i++) {
|
||||||
ensure(flash_write_word(firmware_sectors[firmware_block], i * sizeof(uint32_t), src[i]), NULL);
|
ensure(flash_write_word(FIRMWARE_SECTORS[firmware_block], i * sizeof(uint32_t), src[i]), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
ensure(flash_lock_write(), NULL);
|
ensure(flash_lock_write(), NULL);
|
||||||
@ -546,9 +526,9 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
|
|||||||
int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
|
int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
|
||||||
{
|
{
|
||||||
static const uint8_t sectors[] = {
|
static const uint8_t sectors[] = {
|
||||||
3,
|
|
||||||
FLASH_SECTOR_STORAGE_1,
|
FLASH_SECTOR_STORAGE_1,
|
||||||
FLASH_SECTOR_STORAGE_2,
|
FLASH_SECTOR_STORAGE_2,
|
||||||
|
// 3, // skip because of MPU protection
|
||||||
FLASH_SECTOR_FIRMWARE_START,
|
FLASH_SECTOR_FIRMWARE_START,
|
||||||
7,
|
7,
|
||||||
8,
|
8,
|
||||||
@ -558,7 +538,7 @@ int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
|
|||||||
FLASH_SECTOR_UNUSED_START,
|
FLASH_SECTOR_UNUSED_START,
|
||||||
13,
|
13,
|
||||||
14,
|
14,
|
||||||
FLASH_SECTOR_UNUSED_END,
|
// FLASH_SECTOR_UNUSED_END, // skip because of MPU protection
|
||||||
FLASH_SECTOR_FIRMWARE_EXTRA_START,
|
FLASH_SECTOR_FIRMWARE_EXTRA_START,
|
||||||
18,
|
18,
|
||||||
19,
|
19,
|
||||||
|
@ -28,7 +28,6 @@
|
|||||||
#define USB_PACKET_SIZE 64
|
#define USB_PACKET_SIZE 64
|
||||||
|
|
||||||
#define FIRMWARE_UPLOAD_CHUNK_RETRY_COUNT 2
|
#define FIRMWARE_UPLOAD_CHUNK_RETRY_COUNT 2
|
||||||
extern const uint8_t firmware_sectors[FIRMWARE_SECTORS_COUNT];
|
|
||||||
|
|
||||||
secbool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size);
|
secbool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size);
|
||||||
|
|
||||||
|
@ -54,6 +54,27 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
|
|||||||
[24] = 0x08200000, // last element - not a valid sector
|
[24] = 0x08200000, // last element - not a valid sector
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const uint8_t FIRMWARE_SECTORS [FIRMWARE_SECTORS_COUNT] = {
|
||||||
|
FLASH_SECTOR_FIRMWARE_START,
|
||||||
|
7,
|
||||||
|
8,
|
||||||
|
9,
|
||||||
|
10,
|
||||||
|
FLASH_SECTOR_FIRMWARE_END,
|
||||||
|
FLASH_SECTOR_FIRMWARE_EXTRA_START,
|
||||||
|
18,
|
||||||
|
19,
|
||||||
|
20,
|
||||||
|
21,
|
||||||
|
22,
|
||||||
|
FLASH_SECTOR_FIRMWARE_EXTRA_END,
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint8_t STORAGE_SECTORS[STORAGE_SECTORS_COUNT] = {
|
||||||
|
FLASH_SECTOR_STORAGE_1,
|
||||||
|
FLASH_SECTOR_STORAGE_2,
|
||||||
|
};
|
||||||
|
|
||||||
void flash_init(void)
|
void flash_init(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,13 @@
|
|||||||
// 22
|
// 22
|
||||||
#define FLASH_SECTOR_FIRMWARE_EXTRA_END 23
|
#define FLASH_SECTOR_FIRMWARE_EXTRA_END 23
|
||||||
|
|
||||||
|
#define BOOTLOADER_SECTORS_COUNT (1)
|
||||||
|
#define STORAGE_SECTORS_COUNT (2)
|
||||||
|
#define FIRMWARE_SECTORS_COUNT (6 + 7)
|
||||||
|
|
||||||
|
extern const uint8_t STORAGE_SECTORS[STORAGE_SECTORS_COUNT];
|
||||||
|
extern const uint8_t FIRMWARE_SECTORS[FIRMWARE_SECTORS_COUNT];
|
||||||
|
|
||||||
// note: FLASH_SR_RDERR is STM32F42xxx and STM32F43xxx specific (STM32F427) (reference RM0090 section 3.7.5)
|
// note: FLASH_SR_RDERR is STM32F42xxx and STM32F43xxx specific (STM32F427) (reference RM0090 section 3.7.5)
|
||||||
#ifndef STM32F427xx
|
#ifndef STM32F427xx
|
||||||
#define FLASH_SR_RDERR 0
|
#define FLASH_SR_RDERR 0
|
||||||
|
@ -27,9 +27,6 @@
|
|||||||
#define BOOTLOADER_START 0x08020000
|
#define BOOTLOADER_START 0x08020000
|
||||||
#define FIRMWARE_START 0x08040000
|
#define FIRMWARE_START 0x08040000
|
||||||
|
|
||||||
#define BOOTLOADER_SECTORS_COUNT (1)
|
|
||||||
#define FIRMWARE_SECTORS_COUNT (6 + 7)
|
|
||||||
|
|
||||||
#define IMAGE_HEADER_SIZE 0x400
|
#define IMAGE_HEADER_SIZE 0x400
|
||||||
#define IMAGE_SIG_SIZE 65
|
#define IMAGE_SIG_SIZE 65
|
||||||
#define IMAGE_CHUNK_SIZE (128 * 1024)
|
#define IMAGE_CHUNK_SIZE (128 * 1024)
|
||||||
|
@ -62,6 +62,27 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
|
|||||||
[24] = 0x08200000, // last element - not a valid sector
|
[24] = 0x08200000, // last element - not a valid sector
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const uint8_t FIRMWARE_SECTORS[FIRMWARE_SECTORS_COUNT] = {
|
||||||
|
FLASH_SECTOR_FIRMWARE_START,
|
||||||
|
7,
|
||||||
|
8,
|
||||||
|
9,
|
||||||
|
10,
|
||||||
|
FLASH_SECTOR_FIRMWARE_END,
|
||||||
|
FLASH_SECTOR_FIRMWARE_EXTRA_START,
|
||||||
|
18,
|
||||||
|
19,
|
||||||
|
20,
|
||||||
|
21,
|
||||||
|
22,
|
||||||
|
FLASH_SECTOR_FIRMWARE_EXTRA_END,
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint8_t STORAGE_SECTORS[STORAGE_SECTORS_COUNT] = {
|
||||||
|
FLASH_SECTOR_STORAGE_1,
|
||||||
|
FLASH_SECTOR_STORAGE_2,
|
||||||
|
};
|
||||||
|
|
||||||
static uint8_t *FLASH_BUFFER;
|
static uint8_t *FLASH_BUFFER;
|
||||||
static uint32_t FLASH_SIZE;
|
static uint32_t FLASH_SIZE;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user