1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 12:28:09 +00:00

refactor(core): remove residual usage of old flash api

[no changelog]
This commit is contained in:
tychovrahe 2023-10-16 22:01:36 +02:00 committed by TychoVrahe
parent c48c86d61f
commit 47f8a430e6
9 changed files with 35 additions and 27 deletions

View File

@ -28,9 +28,9 @@ void set_core_clock(int) {}
int bootloader_main(void); int bootloader_main(void);
bool sector_is_empty(uint16_t sector) { bool sector_is_empty(const flash_area_t *area) {
const uint8_t *storage = flash_get_address(sector, 0, 0); const uint8_t *storage = flash_area_get_address(area, 0, 0);
size_t storage_size = flash_sector_size(sector); size_t storage_size = flash_area_get_size(area);
for (size_t i = 0; i < storage_size; i++) { for (size_t i = 0; i < storage_size; i++) {
if (storage[i] != 0xFF) { if (storage[i] != 0xFF) {
return false; return false;
@ -117,7 +117,7 @@ __attribute__((noreturn)) int main(int argc, char **argv) {
FIRMWARE_START = (uint8_t *)flash_area_get_address(&FIRMWARE_AREA, 0, 0); FIRMWARE_START = (uint8_t *)flash_area_get_address(&FIRMWARE_AREA, 0, 0);
// simulate non-empty storage so that we know whether it was erased or not // simulate non-empty storage so that we know whether it was erased or not
if (sector_is_empty(STORAGE_AREAS[0].subarea[0].first_sector)) { if (sector_is_empty(&STORAGE_AREAS[0])) {
secbool ret = flash_area_write_word(&STORAGE_AREAS[0], 16, 0x12345678); secbool ret = flash_area_write_word(&STORAGE_AREAS[0], 16, 0x12345678);
(void)ret; (void)ret;
} }
@ -197,8 +197,7 @@ void mpu_config_off(void) {}
__attribute__((noreturn)) void jump_to(void *addr) { __attribute__((noreturn)) void jump_to(void *addr) {
bool storage_is_erased = bool storage_is_erased =
sector_is_empty(STORAGE_AREAS[0].subarea[0].first_sector) && sector_is_empty(&STORAGE_AREAS[0]) && sector_is_empty(&STORAGE_AREAS[1]);
sector_is_empty(STORAGE_AREAS[1].subarea[0].first_sector);
if (storage_is_erased) { if (storage_is_erased) {
printf("STORAGE WAS ERASED\n"); printf("STORAGE WAS ERASED\n");

View File

@ -48,6 +48,8 @@
#include "secret.h" #include "secret.h"
#endif #endif
#define FW_HASHING_CHUNK_SIZE 1024
static void ui_progress(mp_obj_t ui_wait_callback, uint32_t current, static void ui_progress(mp_obj_t ui_wait_callback, uint32_t current,
uint32_t total) { uint32_t total) {
if (mp_obj_is_callable(ui_wait_callback)) { if (mp_obj_is_callable(ui_wait_callback)) {
@ -172,20 +174,27 @@ STATIC mp_obj_t mod_trezorutils_firmware_hash(size_t n_args,
ui_wait_callback = args[1]; ui_wait_callback = args[1];
} }
uint16_t firmware_sectors = flash_total_sectors(&FIRMWARE_AREA); uint32_t firmware_size = flash_area_get_size(&FIRMWARE_AREA);
uint32_t chunks = firmware_size / FW_HASHING_CHUNK_SIZE;
ui_progress(ui_wait_callback, 0, firmware_sectors); ensure((firmware_size % FW_HASHING_CHUNK_SIZE == 0) * sectrue,
for (int i = 0; i < firmware_sectors; i++) { "Cannot compute FW hash.");
uint8_t sector = flash_get_sector_num(&FIRMWARE_AREA, i);
uint32_t size = flash_sector_size(sector); ui_progress(ui_wait_callback, 0, chunks);
const void *data = flash_get_address(sector, 0, size); for (int i = 0; i < chunks; i++) {
const void *data = flash_area_get_address(
&FIRMWARE_AREA, i * FW_HASHING_CHUNK_SIZE, FW_HASHING_CHUNK_SIZE);
if (data == NULL) { if (data == NULL) {
mp_raise_msg(&mp_type_RuntimeError, "Failed to read firmware."); mp_raise_msg(&mp_type_RuntimeError, "Failed to read firmware.");
} }
blake2s_Update(&ctx, data, size); blake2s_Update(&ctx, data, FW_HASHING_CHUNK_SIZE);
ui_progress(ui_wait_callback, i + 1, firmware_sectors); if (i % 128 == 0) {
ui_progress(ui_wait_callback, i + 1, chunks);
}
} }
ui_progress(ui_wait_callback, chunks, chunks);
vstr_t vstr = {0}; vstr_t vstr = {0};
vstr_init_len(&vstr, BLAKE2S_DIGEST_LENGTH); vstr_init_len(&vstr, BLAKE2S_DIGEST_LENGTH);
if (blake2s_Final(&ctx, vstr.buf, vstr.len) != 0) { if (blake2s_Final(&ctx, vstr.buf, vstr.len) != 0) {
@ -207,9 +216,7 @@ STATIC mp_obj_t mod_trezorutils_firmware_vendor(void) {
return mp_obj_new_str_copy(&mp_type_str, (const uint8_t *)"EMULATOR", 8); return mp_obj_new_str_copy(&mp_type_str, (const uint8_t *)"EMULATOR", 8);
#else #else
vendor_header vhdr = {0}; vendor_header vhdr = {0};
uint32_t size = flash_sector_size(FIRMWARE_AREA.subarea[0].first_sector); const void *data = flash_area_get_address(&FIRMWARE_AREA, 0, 0);
const void *data =
flash_get_address(FIRMWARE_AREA.subarea[0].first_sector, 0, size);
if (data == NULL || sectrue != read_vendor_header(data, &vhdr)) { if (data == NULL || sectrue != read_vendor_header(data, &vhdr)) {
mp_raise_msg(&mp_type_RuntimeError, "Failed to read vendor header."); mp_raise_msg(&mp_type_RuntimeError, "Failed to read vendor header.");
} }

View File

@ -51,10 +51,6 @@
void flash_init(void); void flash_init(void);
secbool flash_write_byte(uint16_t sector, uint32_t offset, uint8_t data);
secbool flash_write_word(uint16_t sector, uint32_t offset, uint32_t data);
uint32_t flash_wait_and_clear_status_flags(void); uint32_t flash_wait_and_clear_status_flags(void);
// Erases the single sector in the designated flash area // Erases the single sector in the designated flash area

View File

@ -95,7 +95,7 @@ uint32_t flash_sector_size(uint16_t sector) {
return FLASH_SECTOR_TABLE[sector + 1] - FLASH_SECTOR_TABLE[sector]; return FLASH_SECTOR_TABLE[sector + 1] - FLASH_SECTOR_TABLE[sector];
} }
secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data) { secbool flash_write_byte(uint16_t sector, uint32_t offset, uint8_t data) {
uint8_t *address = (uint8_t *)flash_get_address(sector, offset, 1); uint8_t *address = (uint8_t *)flash_get_address(sector, offset, 1);
if (address == NULL) { if (address == NULL) {
return secfalse; return secfalse;
@ -115,7 +115,7 @@ secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data) {
return sectrue; return sectrue;
} }
secbool flash_write_word(uint8_t sector, uint32_t offset, uint32_t data) { secbool flash_write_word(uint16_t sector, uint32_t offset, uint32_t data) {
uint32_t *address = (uint32_t *)flash_get_address(sector, offset, 4); uint32_t *address = (uint32_t *)flash_get_address(sector, offset, 4);
if (address == NULL) { if (address == NULL) {
return secfalse; return secfalse;

View File

@ -39,7 +39,4 @@
(FLASH_SR_RDERR | FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | \ (FLASH_SR_RDERR | FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | \
FLASH_SR_WRPERR | FLASH_SR_SOP | FLASH_SR_EOP) FLASH_SR_WRPERR | FLASH_SR_SOP | FLASH_SR_EOP)
secbool __wur flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data);
secbool __wur flash_write_word(uint8_t sector, uint32_t offset, uint32_t data);
#endif // FLASH_H #endif // FLASH_H

View File

@ -35,6 +35,8 @@
#include "timer.h" #include "timer.h"
#include "util.h" #include "util.h"
const void *flash_get_address(uint16_t sector, uint32_t offset, uint32_t size);
// legacy storage magic // legacy storage magic
#define LEGACY_STORAGE_SECTOR 2 #define LEGACY_STORAGE_SECTOR 2
static const uint32_t META_MAGIC_V10 = 0x525a5254; // 'TRZR' static const uint32_t META_MAGIC_V10 = 0x525a5254; // 'TRZR'

View File

@ -28,6 +28,8 @@
#define FLASH_OPTION_BYTES_1 (*(const uint64_t *)0x1FFFC000) #define FLASH_OPTION_BYTES_1 (*(const uint64_t *)0x1FFFC000)
#define FLASH_OPTION_BYTES_2 (*(const uint64_t *)0x1FFFC008) #define FLASH_OPTION_BYTES_2 (*(const uint64_t *)0x1FFFC008)
const void *flash_get_address(uint16_t sector, uint32_t offset, uint32_t size);
void memory_protect(void) { void memory_protect(void) {
#if PRODUCTION #if PRODUCTION
#if BOOTLOADER_QA #if BOOTLOADER_QA

View File

@ -1,5 +1,11 @@
#include "flash.h" #include "flash.h"
secbool flash_write_byte(uint16_t sector, uint32_t offset, uint8_t data);
secbool flash_write_word(uint16_t sector, uint32_t offset, uint32_t data);
const void *flash_get_address(uint16_t sector, uint32_t offset, uint32_t size);
static uint32_t flash_subarea_get_size(const flash_subarea_t *subarea) { static uint32_t flash_subarea_get_size(const flash_subarea_t *subarea) {
uint32_t size = 0; uint32_t size = 0;
for (int s = 0; s < subarea->num_sectors; s++) { for (int s = 0; s < subarea->num_sectors; s++) {

View File

@ -19,7 +19,6 @@ void flash_init(void);
secbool __wur flash_unlock_write(void); secbool __wur flash_unlock_write(void);
secbool __wur flash_lock_write(void); secbool __wur flash_lock_write(void);
const void *flash_get_address(uint16_t sector, uint32_t offset, uint32_t size);
uint32_t flash_sector_size(uint16_t sector); uint32_t flash_sector_size(uint16_t sector);
uint16_t flash_total_sectors(const flash_area_t *area); uint16_t flash_total_sectors(const flash_area_t *area);
int32_t flash_get_sector_num(const flash_area_t *area, int32_t flash_get_sector_num(const flash_area_t *area,