Skip to content

Commit

Permalink
fixup! fix(legacy): don't erase firmware and storage in intermediate …
Browse files Browse the repository at this point in the history
…firmware
  • Loading branch information
prusnak committed May 12, 2021
1 parent 044cda4 commit 8f9acc8
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions legacy/intermediate_fw/trezor.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
#include "setup.h"
#include "timer.h"
#include "util.h"
#include "norcow_config.h"

// storage magic
#define FLASH_META_START 0x08008000
static const uint32_t NORCOW_MAGIC = 0x3243524e; // 'NRC2'
static const uint32_t NORCOW_MAGIC_V0 = 0x5743524e; // NRCW
// legacy storage magic
#define LEGACY_STORAGE_SECTOR 2
static const uint32_t META_MAGIC_V10 = 0x525a5254; // 'TRZR'

// norcow storage magic
static const uint32_t NORCOW_MAGIC = 0x3243524e; // 'NRC2'
static const uint8_t norcow_sectors[NORCOW_SECTOR_COUNT] = NORCOW_SECTORS;

/** Sector erase operation extracted from libopencm3 - flash_erase_sector
* so it can run from RAM
*/
Expand Down Expand Up @@ -120,13 +123,26 @@ int main(void) {
timer_init();
check_and_replace_bootloader(false);

// storage is initialized
if (memcmp(FLASH_PTR(FLASH_META_START), &NORCOW_MAGIC,
sizeof(NORCOW_MAGIC)) == 0 ||
memcmp(FLASH_PTR(FLASH_META_START), &NORCOW_MAGIC_V0,
sizeof(NORCOW_MAGIC_V0)) == 0 ||
memcmp(FLASH_PTR(FLASH_META_START), &META_MAGIC_V10,
sizeof(META_MAGIC_V10)) == 0) {
secbool storage_initialized = secfalse;

// check legacy storage
uint32_t *magic = (uint32_t *)flash_get_address(LEGACY_STORAGE_SECTOR, 0, sizeof(META_MAGIC_V10));
if (*magic == META_MAGIC_V10) {
storage_initialized = sectrue;
}

if (storage_initialized == secfalse) {
// check norcow storage
for (uint8_t i = 0; i < NORCOW_SECTOR_COUNT; i++) {
magic = (uint32_t *)flash_get_address(norcow_sectors[i], 0, sizeof(NORCOW_MAGIC));
if (*magic == NORCOW_MAGIC) {
storage_initialized = sectrue;
break;
}
}
}

if (sectrue == storage_initialized) {
// don't erase
reboot_device();
} else {
Expand Down

0 comments on commit 8f9acc8

Please sign in to comment.