Skip to content

Commit

Permalink
Add addresses instead of public bootrom_structs.h
Browse files Browse the repository at this point in the history
  • Loading branch information
will-v-pi committed Oct 3, 2024
1 parent 67e788f commit e009c07
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 226 deletions.
40 changes: 28 additions & 12 deletions src/rp2_common/pico_bootrom/bootrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ void __attribute__((noreturn)) rom_reset_usb_boot(uint32_t usb_activity_gpio_pin
}

#if !PICO_RP2040


// Generated from adding the following code into the bootrom
// scan_workarea_t* scan_workarea = (scan_workarea_t*)workarea;
// printf("VERSION_DOWNGRADE_ERASE_ADDR %08x\n", &(always->zero_init.version_downgrade_erase_flash_addr));
// printf("TBYB_FLAG_ADDR %08x\n", &(always->zero_init.tbyb_flag_flash_addr));
// printf("IMAGE_DEF_VERIFIED %08x\n", (uint32_t)&(scan_workarea->parsed_block_loops[0].image_def.core.verified) - (uint32_t)scan_workarea);
// printf("IMAGE_DEF_TBYB_FLAGGED %08x\n", (uint32_t)&(scan_workarea->parsed_block_loops[0].image_def.core.tbyb_flagged) - (uint32_t)scan_workarea);
// printf("IMAGE_DEF_BASE %08x\n", (uint32_t)&(scan_workarea->parsed_block_loops[0].image_def.core.enclosing_window.base) - (uint32_t)scan_workarea);
// printf("IMAGE_DEF_REL_BLOCK_OFFSET %08x\n", (uint32_t)&(scan_workarea->parsed_block_loops[0].image_def.core.window_rel_block_offset) - (uint32_t)scan_workarea);
#define VERSION_DOWNGRADE_ERASE_ADDR *(uint32_t*)0x400e0338
#define TBYB_FLAG_ADDR *(uint32_t*)0x400e0348
#define IMAGE_DEF_VERIFIED(scan_workarea) *(uint32_t*)(0x64 + (uint32_t)scan_workarea)
#define IMAGE_DEF_TBYB_FLAGGED(scan_workarea) *(uint32_t*)(0x4c + (uint32_t)scan_workarea)
#define IMAGE_DEF_BASE(scan_workarea) *(uint32_t*)(0x54 + (uint32_t)scan_workarea)
#define IMAGE_DEF_REL_BLOCK_OFFSET(scan_workarea) *(uint32_t*)(0x5c + (uint32_t)scan_workarea)

bool rom_get_boot_random(uint32_t out[4]) {
uint32_t result[5];
rom_get_sys_info_fn func = (rom_get_sys_info_fn) rom_func_lookup_inline(ROM_FUNC_GET_SYS_INFO);
Expand Down Expand Up @@ -104,7 +121,6 @@ int rom_add_flash_runtime_partition(uint32_t start_offset, uint32_t size, uint32
}

int rom_pick_ab_update_partition(uint32_t *workarea_base, uint32_t workarea_size, uint partition_a_num) {
scan_workarea_t* scan_workarea = (scan_workarea_t*)workarea_base;
uint32_t flash_update_base = 0;
bool tbyb_boot = false;
uint32_t saved_erase_addr = 0;
Expand All @@ -118,43 +134,43 @@ int rom_pick_ab_update_partition(uint32_t *workarea_base, uint32_t workarea_size
// A buy is pending, so the main software has not been bought
tbyb_boot = true;
// Save the erase address, as this will be overwritten by rom_pick_ab_partition
saved_erase_addr = always->zero_init.version_downgrade_erase_flash_addr;
saved_erase_addr = VERSION_DOWNGRADE_ERASE_ADDR;
}
}
}

int rc = rom_pick_ab_partition((uint8_t*)scan_workarea, workarea_size, partition_a_num, flash_update_base);
int rc = rom_pick_ab_partition((uint8_t*)workarea_base, workarea_size, partition_a_num, flash_update_base);

if (scan_workarea->parsed_block_loops[0].image_def.core.verified != RCP_MASK_TRUE) {
if (IMAGE_DEF_VERIFIED(workarea_base) != RCP_MASK_TRUE) {
// Chosen partition failed verification
return BOOTROM_ERROR_NOT_FOUND;
}

if (scan_workarea->parsed_block_loops[0].image_def.core.tbyb_flagged) {
if (IMAGE_DEF_TBYB_FLAGGED(workarea_base)) {
// The chosen partition is TBYB
if (tbyb_boot) {
// The boot partition is also TBYB - cannot update both, so prioritise boot partition
// Restore the erase address saved earlier
always->zero_init.version_downgrade_erase_flash_addr = saved_erase_addr;
VERSION_DOWNGRADE_ERASE_ADDR = saved_erase_addr;
return BOOTROM_ERROR_NOT_PERMITTED;
} else {
// Update the tbyb flash address, so that explicit_buy will clear the flag for the chosen partition
always->zero_init.tbyb_flag_flash_addr =
scan_workarea->parsed_block_loops[0].image_def.core.enclosing_window.base
+ scan_workarea->parsed_block_loops[0].image_def.core.window_rel_block_offset + 4;
TBYB_FLAG_ADDR =
IMAGE_DEF_BASE(workarea_base)
+ IMAGE_DEF_REL_BLOCK_OFFSET(workarea_base) + 4;
}
} else {
// The chosen partition is not TBYB
if (tbyb_boot && saved_erase_addr) {
// The boot partition was TBYB, and requires an erase
if (always->zero_init.version_downgrade_erase_flash_addr) {
if (VERSION_DOWNGRADE_ERASE_ADDR) {
// But both the chosen partition requires an erase too
// As before, prioritise the boot partition, and restore it's saved erase_address
always->zero_init.version_downgrade_erase_flash_addr = saved_erase_addr;
VERSION_DOWNGRADE_ERASE_ADDR = saved_erase_addr;
return BOOTROM_ERROR_NOT_PERMITTED;
} else {
// The chosen partition doesn't require an erase, so we're fine
always->zero_init.version_downgrade_erase_flash_addr = saved_erase_addr;
VERSION_DOWNGRADE_ERASE_ADDR = saved_erase_addr;
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/rp2_common/pico_bootrom/include/pico/bootrom.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
#ifndef __ASSEMBLER__
#include <string.h>
#include "pico/bootrom/lock.h"
#if !PICO_RP2040
#include "pico/bootrom_structs.h"
#endif
// ROM FUNCTION SIGNATURES

#if PICO_RP2040
Expand Down
211 changes: 0 additions & 211 deletions src/rp2_common/pico_bootrom/include/pico/bootrom_structs.h

This file was deleted.

0 comments on commit e009c07

Please sign in to comment.