feat(core/bootloader_emu): print the model and flash layout with -p [no changelog]
What changed, and why it matters
This change adds a new command-line option (-p) to the Trezor bootloader emulator that prints the device model and flash storage layout as JSON, then exits. It is a developer/testing convenience feature and does not change how real hardware wallets work or how secrets are protected.
No security action required. Treat as a normal feature commit; routine review/merge process is sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit introduces a print_properties() helper in the bootloader emulator and a new -p flag. When invoked with -p, the emulator prints a JSON object containing model and storage_areas (offset/size) and exits before initializing the display or mapping the flash file. To support this, the Unix flash layer’s sector-table initialization is split into a separate flash_layout_init() routine so layout metadata can be queried without creating/mapping the flash backing file. No cryptographic, authentication, or access-control logic is modified.
Changed components
core/embed/projects/bootloader/emulator.ccore/embed/sys/flash/unix/flash.cInspect captured patch +51 / −4
### core/embed/projects/bootloader/emulator.c
@@ -69,6 +69,7 @@ void usage(void) {
#ifdef LOCKABLE_BOOTLOADER
printf(" -l lock bootloader\n");
#endif
+ printf(" -p print the emulator properties as JSON and exit\n");
printf(" -h show this help\n");
printf("\n");
printf("On exit, the boot command for the next run is written to\n");
@@ -136,6 +137,21 @@ bool preload_firmware_image(const char *filename) {
read, 0x0, FIRMWARE_MAXSIZE);
}
+// Prints what a test harness needs to interpret the flash file: the
+// model and where the storage areas sit inside it. Works from the sector
+// layout alone, before the flash file is mapped.
+static void print_properties(void) {
+ printf("{\"model\": \"%s\", \"storage_areas\": [", MODEL_INTERNAL_NAME);
+ for (size_t i = 0; i < ARRAY_LENGTH(STORAGE_AREAS); i++) {
+ // storage areas are single subareas, see storage_empty()
+ const flash_area_t *area = &STORAGE_AREAS[i];
+ printf("%s{\"offset\": %u, \"size\": %u}", i > 0 ? ", " : "",
+ (unsigned)flash_sector_size(0, area->subarea[0].first_sector),
+ (unsigned)flash_area_get_size(area));
+ }
+ printf("]}\n");
+}
+
static bool sdl_event_filter(void *userdata, SDL_Event *event) {
switch (event->type) {
case SDL_EVENT_QUIT:
@@ -158,7 +174,27 @@ static bool sdl_event_filter(void *userdata, SDL_Event *event) {
return true;
}
+#define EMULATOR_OPTSTRING "ahpslewc:b:f:i:r:B:WR"
+
int main(int argc, char **argv) {
+ int opt;
+
+ // The informational options answer before the display opens and before
+ // the flash file is mapped, which would create it if missing.
+ opterr = 0;
+ while ((opt = getopt(argc, argv, EMULATOR_OPTSTRING)) != -1) {
+ if (opt == 'p') {
+ print_properties();
+ exit(0);
+ }
+ if (opt == 'h') {
+ usage();
+ exit(0);
+ }
+ }
+ opterr = 1;
+ optind = 1;
+
SDL_SetEventFilter(sdl_event_filter, NULL);
display_init(DISPLAY_RESET_CONTENT);
@@ -167,12 +203,11 @@ int main(int argc, char **argv) {
FIRMWARE_START = (uintptr_t)flash_area_get_address(&FIRMWARE_AREA, 0, 0);
- int opt;
bool display_error = false;
uint8_t set_variant = 0xff;
uint8_t color_variant = 0;
uint8_t bitcoin_only = 0;
- while ((opt = getopt(argc, argv, "ahslewc:b:f:i:r:B:WR")) != -1) {
+ while ((opt = getopt(argc, argv, EMULATOR_OPTSTRING)) != -1) {
switch (opt) {
case 'a':
disable_animation(true);
### core/embed/sys/flash/unix/flash.c
@@ -82,8 +82,12 @@ static uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
static uint8_t *FLASH_BUFFER = NULL;
static uint32_t FLASH_SIZE;
-void flash_init(void) {
- if (FLASH_BUFFER) return;
+// The sector layout is known without the flash file, so that the layout
+// can be queried before (or without) mapping it.
+static void flash_layout_init(void) {
+ static bool initialized = false;
+ if (initialized) return;
+ initialized = true;
#if defined STM32U585xx || defined STM32U5G9xx
for (size_t i = 0; i < FLASH_SECTOR_COUNT; i++) {
@@ -93,6 +97,12 @@ void flash_init(void) {
#endif
FLASH_SIZE = FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0];
+}
+
+void flash_init(void) {
+ if (FLASH_BUFFER) return;
+
+ flash_layout_init();
// check whether the file exists and it has the correct size
struct stat sb;
@@ -145,6 +155,7 @@ const void *flash_get_address(uint16_t sector, uint32_t offset, uint32_t size) {
}
uint32_t flash_sector_size(uint16_t first_sector, uint16_t sector_count) {
+ flash_layout_init();
if (first_sector + sector_count > FLASH_SECTOR_COUNT) {
return 0;
}
@@ -153,6 +164,7 @@ uint32_t flash_sector_size(uint16_t first_sector, uint16_t sector_count) {
}
uint16_t flash_sector_find(uint16_t first_sector, uint32_t offset) {
+ flash_layout_init();
uint16_t sector = first_sector;
while (sector < FLASH_SECTOR_COUNT) {Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.