mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-18 04:18:10 +00:00
refactor(core): remove residual usage of old flash api
[no changelog]
This commit is contained in:
parent
c48c86d61f
commit
47f8a430e6
@ -28,9 +28,9 @@ void set_core_clock(int) {}
|
||||
|
||||
int bootloader_main(void);
|
||||
|
||||
bool sector_is_empty(uint16_t sector) {
|
||||
const uint8_t *storage = flash_get_address(sector, 0, 0);
|
||||
size_t storage_size = flash_sector_size(sector);
|
||||
bool sector_is_empty(const flash_area_t *area) {
|
||||
const uint8_t *storage = flash_area_get_address(area, 0, 0);
|
||||
size_t storage_size = flash_area_get_size(area);
|
||||
for (size_t i = 0; i < storage_size; i++) {
|
||||
if (storage[i] != 0xFF) {
|
||||
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);
|
||||
|
||||
// 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);
|
||||
(void)ret;
|
||||
}
|
||||
@ -197,8 +197,7 @@ void mpu_config_off(void) {}
|
||||
|
||||
__attribute__((noreturn)) void jump_to(void *addr) {
|
||||
bool storage_is_erased =
|
||||
sector_is_empty(STORAGE_AREAS[0].subarea[0].first_sector) &&
|
||||
sector_is_empty(STORAGE_AREAS[1].subarea[0].first_sector);
|
||||
sector_is_empty(&STORAGE_AREAS[0]) && sector_is_empty(&STORAGE_AREAS[1]);
|
||||
|
||||
if (storage_is_erased) {
|
||||
printf("STORAGE WAS ERASED\n");
|
||||
|
@ -48,6 +48,8 @@
|
||||
#include "secret.h"
|
||||
#endif
|
||||
|
||||
#define FW_HASHING_CHUNK_SIZE 1024
|
||||
|
||||
static void ui_progress(mp_obj_t ui_wait_callback, uint32_t current,
|
||||
uint32_t total) {
|
||||
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];
|
||||
}
|
||||
|
||||
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);
|
||||
for (int i = 0; i < firmware_sectors; i++) {
|
||||
uint8_t sector = flash_get_sector_num(&FIRMWARE_AREA, i);
|
||||
uint32_t size = flash_sector_size(sector);
|
||||
const void *data = flash_get_address(sector, 0, size);
|
||||
ensure((firmware_size % FW_HASHING_CHUNK_SIZE == 0) * sectrue,
|
||||
"Cannot compute FW hash.");
|
||||
|
||||
ui_progress(ui_wait_callback, 0, chunks);
|
||||
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) {
|
||||
mp_raise_msg(&mp_type_RuntimeError, "Failed to read firmware.");
|
||||
}
|
||||
blake2s_Update(&ctx, data, size);
|
||||
ui_progress(ui_wait_callback, i + 1, firmware_sectors);
|
||||
blake2s_Update(&ctx, data, FW_HASHING_CHUNK_SIZE);
|
||||
if (i % 128 == 0) {
|
||||
ui_progress(ui_wait_callback, i + 1, chunks);
|
||||
}
|
||||
}
|
||||
|
||||
ui_progress(ui_wait_callback, chunks, chunks);
|
||||
|
||||
vstr_t vstr = {0};
|
||||
vstr_init_len(&vstr, BLAKE2S_DIGEST_LENGTH);
|
||||
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);
|
||||
#else
|
||||
vendor_header vhdr = {0};
|
||||
uint32_t size = flash_sector_size(FIRMWARE_AREA.subarea[0].first_sector);
|
||||
const void *data =
|
||||
flash_get_address(FIRMWARE_AREA.subarea[0].first_sector, 0, size);
|
||||
const void *data = flash_area_get_address(&FIRMWARE_AREA, 0, 0);
|
||||
if (data == NULL || sectrue != read_vendor_header(data, &vhdr)) {
|
||||
mp_raise_msg(&mp_type_RuntimeError, "Failed to read vendor header.");
|
||||
}
|
||||
|
@ -51,10 +51,6 @@
|
||||
|
||||
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);
|
||||
|
||||
// Erases the single sector in the designated flash area
|
||||
|
@ -95,7 +95,7 @@ uint32_t flash_sector_size(uint16_t 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);
|
||||
if (address == NULL) {
|
||||
return secfalse;
|
||||
@ -115,7 +115,7 @@ secbool flash_write_byte(uint8_t sector, uint32_t offset, uint8_t data) {
|
||||
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);
|
||||
if (address == NULL) {
|
||||
return secfalse;
|
||||
|
@ -39,7 +39,4 @@
|
||||
(FLASH_SR_RDERR | FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | \
|
||||
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
|
||||
|
@ -35,6 +35,8 @@
|
||||
#include "timer.h"
|
||||
#include "util.h"
|
||||
|
||||
const void *flash_get_address(uint16_t sector, uint32_t offset, uint32_t size);
|
||||
|
||||
// legacy storage magic
|
||||
#define LEGACY_STORAGE_SECTOR 2
|
||||
static const uint32_t META_MAGIC_V10 = 0x525a5254; // 'TRZR'
|
||||
|
@ -28,6 +28,8 @@
|
||||
#define FLASH_OPTION_BYTES_1 (*(const uint64_t *)0x1FFFC000)
|
||||
#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) {
|
||||
#if PRODUCTION
|
||||
#if BOOTLOADER_QA
|
||||
|
@ -1,5 +1,11 @@
|
||||
#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) {
|
||||
uint32_t size = 0;
|
||||
for (int s = 0; s < subarea->num_sectors; s++) {
|
||||
|
@ -19,7 +19,6 @@ void flash_init(void);
|
||||
secbool __wur flash_unlock_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);
|
||||
uint16_t flash_total_sectors(const flash_area_t *area);
|
||||
int32_t flash_get_sector_num(const flash_area_t *area,
|
||||
|
Loading…
Reference in New Issue
Block a user