1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

flash: simplify/unify flash_erase_sectors usage

This commit is contained in:
Pavol Rusnak 2017-12-09 18:00:10 +01:00
parent 6ab0f03ec4
commit e268aeb8b7
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
6 changed files with 17 additions and 21 deletions

View File

@ -85,7 +85,7 @@ static secbool copy_sdcard(void)
display_printf("\n\nerasing flash:\n\n");
// erase all flash (except boardloader)
uint8_t sectors[] = {
const uint8_t sectors[] = {
FLASH_SECTOR_STORAGE_1,
FLASH_SECTOR_STORAGE_2,
FLASH_SECTOR_BOOTLOADER,
@ -108,7 +108,7 @@ static secbool copy_sdcard(void)
FLASH_SECTOR_FIRMWARE_EXTRA_END,
FLASH_SECTOR_PIN_AREA,
};
if (sectrue != flash_erase_sectors(sectors, 2 + 1 + 6 + 4 + 7 + 1, progress_callback)) {
if (sectrue != flash_erase_sectors(sectors, sizeof(sectors), progress_callback)) {
display_printf(" failed\n");
return secfalse;
}
@ -161,7 +161,7 @@ int main(void)
FLASH_SECTOR_STORAGE_1,
FLASH_SECTOR_STORAGE_2,
};
flash_erase_sectors(sectors, 2, NULL);
flash_erase_sectors(sectors, sizeof(sectors), NULL);
return 2;
}

View File

@ -247,7 +247,7 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
firmware_remaining = msg_recv.has_length ? msg_recv.length : 0;
if (firmware_remaining > 0 && firmware_remaining % 4 == 0) {
// erase flash
uint8_t sectors[] = {
const uint8_t sectors[] = {
FLASH_SECTOR_FIRMWARE_START,
7,
8,
@ -262,7 +262,7 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
22,
FLASH_SECTOR_FIRMWARE_EXTRA_END,
};
if (sectrue != flash_erase_sectors(sectors, 6 + 7, progress_erase)) {
if (sectrue != flash_erase_sectors(sectors, sizeof(sectors), progress_erase)) {
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
MSG_SEND_ASSIGN_STRING(message, "Could not erase flash");
@ -383,15 +383,12 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
}
if (sectrue != compare_to_current_vendor_header(&vhdr)) {
uint8_t sectors_storage[] = {
const uint8_t sectors_storage[] = {
FLASH_SECTOR_STORAGE_1,
FLASH_SECTOR_STORAGE_2,
};
ensure(flash_erase_sectors(sectors_storage, 2, NULL), NULL);
uint8_t sectors_pin[] = {
FLASH_SECTOR_PIN_AREA,
};
ensure(flash_erase_sectors(sectors_pin, 2, NULL), NULL);
ensure(flash_erase_sectors(sectors_storage, sizeof(sectors_storage), NULL), NULL);
ensure(flash_erase_sector(FLASH_SECTOR_PIN_AREA), NULL);
}
firstskip = IMAGE_HEADER_SIZE + vhdr.hdrlen;
@ -451,7 +448,7 @@ static void progress_wipe(int pos, int len)
int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
{
uint8_t sectors[] = {
const uint8_t sectors[] = {
FLASH_SECTOR_STORAGE_1,
FLASH_SECTOR_STORAGE_2,
FLASH_SECTOR_FIRMWARE_START,
@ -473,7 +470,7 @@ int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
FLASH_SECTOR_FIRMWARE_EXTRA_END,
FLASH_SECTOR_PIN_AREA,
};
if (sectrue != flash_erase_sectors(sectors, 2 + 6 + 4 + 7 + 1, progress_wipe)) {
if (sectrue != flash_erase_sectors(sectors, sizeof(sectors), progress_wipe)) {
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
MSG_SEND_ASSIGN_STRING(message, "Could not erase flash");

View File

@ -6,14 +6,14 @@
#include "../../trezorhal/flash.h"
#ifndef NORCOW_SECTORS
#define NORCOW_SECTORS {FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2}
#define NORCOW_SECTORS (const uint8_t []){FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2}
#endif
// NRCW = 4e524357
#define NORCOW_MAGIC ((uint32_t)0x5743524e)
#define NORCOW_MAGIC_LEN (sizeof(uint32_t))
static uint8_t norcow_sectors[NORCOW_SECTOR_COUNT] = NORCOW_SECTORS;
static const uint8_t norcow_sectors[NORCOW_SECTOR_COUNT] = NORCOW_SECTORS;
static uint8_t norcow_active_sector = 0;
static uint32_t norcow_active_offset = NORCOW_MAGIC_LEN;
@ -70,7 +70,7 @@ static secbool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, co
static void norcow_erase(uint8_t sector, secbool set_magic)
{
ensure(sectrue * (sector <= NORCOW_SECTOR_COUNT), "invalid sector");
ensure(flash_erase_sectors(&norcow_sectors[sector], 1, NULL), "erase failed");
ensure(flash_erase_sector(norcow_sectors[sector]), "erase failed");
if (sectrue == set_magic) {
ensure(norcow_write(sector, 0, NORCOW_MAGIC, NULL, 0), "set magic failed");
}

View File

@ -41,7 +41,7 @@ static void pin_fails_reset(uint32_t ofs)
// ofs points to the last word of the PIN fails area. Because there is
// no space left, we recycle the sector (set all words to 0xffffffff).
// On next unlock attempt, we start counting from the the first word.
flash_erase_sectors((uint8_t[]) { FLASH_SECTOR_PIN_AREA }, 1, NULL);
flash_erase_sector(FLASH_SECTOR_PIN_AREA);
} else {
// Mark this counter as exhausted. On next unlock attempt, pinfails_get
// seeks to the next word.

View File

@ -67,16 +67,14 @@ int main(void)
display_printf("updating boardloader + bootloader\n");
uint8_t sectors[] = {
const uint8_t sectors[] = {
FLASH_SECTOR_BOARDLOADER_START,
1,
FLASH_SECTOR_BOARDLOADER_END,
FLASH_SECTOR_BOOTLOADER,
};
display_printf("erasing sectors");
ensure(
flash_erase_sectors(sectors, sizeof(sectors), progress_callback),
"flash_erase_sectors");
ensure(flash_erase_sectors(sectors, sizeof(sectors), progress_callback), "flash_erase_sectors");
display_printf("\n");
display_printf("erased\n");

View File

@ -51,6 +51,7 @@ secbool flash_lock(void);
const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size);
secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len));
inline secbool flash_erase_sector(uint8_t sector) { return flash_erase_sectors(&sector, 1, NULL); }
secbool flash_write_byte(uint32_t address, uint8_t data);
secbool flash_write_word(uint32_t address, uint32_t data);
secbool flash_write_byte_rel(uint8_t sector, uint32_t offset, uint8_t data);