11#include "pico/bootrom_constants.h"
22#include "pico/bootrom/lock.h"
27typedef uint32_t (*rom_popcount32_fn)(uint32_t);
28typedef uint32_t (*rom_reverse32_fn)(uint32_t);
29typedef uint32_t (*rom_clz32_fn)(uint32_t);
30typedef uint32_t (*rom_ctz32_fn)(uint32_t);
31typedef uint8_t *(*rom_memset_fn)(uint8_t *, uint8_t, uint32_t);
32typedef uint32_t *(*rom_memset4_fn)(uint32_t *, uint8_t, uint32_t);
33typedef uint32_t *(*rom_memcpy_fn)(uint8_t *,
const uint8_t *, uint32_t);
34typedef uint32_t *(*rom_memcpy44_fn)(uint32_t *,
const uint32_t *, uint32_t);
36typedef void __attribute__((noreturn)) (*rom_reset_usb_boot_fn)(uint32_t, uint32_t);
37typedef int (*rom_reboot_fn)(uint32_t flags, uint32_t delay_ms, uint32_t p0, uint32_t p1);
38typedef rom_reset_usb_boot_fn reset_usb_boot_fn;
39typedef void (*rom_connect_internal_flash_fn)(void);
40typedef void (*rom_flash_exit_xip_fn)(void);
41typedef void (*rom_flash_range_erase_fn)(uint32_t, size_t, uint32_t, uint8_t);
42typedef void (*rom_flash_range_program_fn)(uint32_t,
const uint8_t*, size_t);
43typedef void (*rom_flash_flush_cache_fn)(void);
44typedef void (*rom_flash_enter_cmd_xip_fn)(void);
46typedef void (*rom_bootrom_state_reset_fn)(uint32_t flags);
47typedef void (*rom_flash_reset_address_trans_fn)(void);
48typedef void (*rom_flash_select_xip_read_mode_fn)(bootrom_xip_mode_t mode, uint8_t clkdiv);
49typedef int (*rom_get_sys_info_fn)(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t flags);
50typedef int (*rom_get_partition_table_info_fn)(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t partition_and_flags);
51typedef int (*rom_explicit_buy_fn)(uint8_t *buffer, uint32_t buffer_size);
52typedef void* (*rom_validate_ns_buffer_fn)(
const void *addr, uint32_t size, uint32_t write, uint32_t *ok);
58typedef int (*rom_chain_image_fn)(uint8_t *workarea_base, uint32_t workarea_size, uint32_t window_base, uint32_t window_size);
59typedef int (*rom_load_partition_table_fn)(uint8_t *workarea_base, uint32_t workarea_size,
bool force_reload);
60typedef int (*rom_pick_ab_partition_fn)(uint8_t *workarea_base, uint32_t workarea_size, uint partition_a_num, uint32_t flash_update_boot_window_base);
61typedef int (*rom_get_b_partition_fn)(uint pi_a);
62typedef int (*rom_get_uf2_target_partition_fn)(uint8_t *workarea_base, uint32_t workarea_size, uint32_t family_id,
resident_partition_t *partition_out);
63typedef int (*rom_func_otp_access_fn)(uint8_t *buf, uint32_t buf_len, otp_cmd_t cmd);
69typedef intptr_t (*rom_flash_runtime_to_storage_addr_fn)(uintptr_t flash_runtime_addr);
78typedef int (*rom_flash_op_fn)(cflash_flags_t flags, uintptr_t addr, uint32_t size_bytes, uint8_t *buf);
81typedef int (*rom_set_ns_api_permission_fn)(uint ns_api_num,
bool allowed);
109typedef int (*rom_set_bootrom_stack_fn)(bootrom_stack_t *stack);
162typedef void *(*rom_table_lookup_fn)(uint16_t *table, uint32_t code);
164typedef void *(*rom_table_lookup_fn)(uint32_t code, uint32_t mask);
167#if PICO_C_COMPILER_IS_GNU && (__GNUC__ >= 12)
169__force_inline static void *rom_hword_as_ptr(uint16_t rom_address) {
170#pragma GCC diagnostic push
171#pragma GCC diagnostic ignored "-Warray-bounds"
172 return (
void *)(uintptr_t)*(uint16_t *)(uintptr_t)rom_address;
173#pragma GCC diagnostic pop
177#define rom_hword_as_ptr(rom_address) (void *)(uintptr_t)(*(uint16_t *)(uintptr_t)(rom_address))
182#ifdef RASPBERRYPI_AMETHYST_FPGA
183 return *(uint16_t*)0x14 >= 0x8000;
196#pragma GCC diagnostic push
198#pragma GCC diagnostic ignored "-Warray-bounds"
201 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) rom_hword_as_ptr(BOOTROM_TABLE_LOOKUP_OFFSET);
202 uint16_t *func_table = (uint16_t *) rom_hword_as_ptr(BOOTROM_FUNC_TABLE_OFFSET);
203 return rom_table_lookup(func_table, code);
206 uint32_t rom_offset_adjust = rom_size_is_64k() ? 32 * 1024 : 0;
208 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_ENTRY_OFFSET + rom_offset_adjust);
209 return rom_table_lookup(code, RT_FLAG_FUNC_RISCV);
213 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_OFFSET);
215 return rom_table_lookup(code, RT_FLAG_FUNC_ARM_NONSEC);
217 return rom_table_lookup(code, RT_FLAG_FUNC_ARM_SEC);
222#pragma GCC diagnostic pop
230#pragma GCC diagnostic push
232#pragma GCC diagnostic ignored "-Warray-bounds"
235 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) rom_hword_as_ptr(BOOTROM_TABLE_LOOKUP_OFFSET);
236 uint16_t *data_table = (uint16_t *) rom_hword_as_ptr(BOOTROM_DATA_TABLE_OFFSET);
237 return rom_table_lookup(data_table, code);
240 uint32_t rom_offset_adjust = rom_size_is_64k() ? 32 * 1024 : 0;
241 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_OFFSET + rom_offset_adjust);
243 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_OFFSET);
245 return rom_table_lookup(code, RT_FLAG_DATA);
248#pragma GCC diagnostic pop
252#ifndef PICO_BOOTROM_WORKAROUND_RP2350_A2_ACTIVITY_LED_BUG
253#define PICO_BOOTROM_WORKAROUND_RP2350_A2_ACTIVITY_LED_BUG 1
280void __attribute__((noreturn))
rom_reset_usb_boot(uint32_t usb_activity_gpio_pin_mask, uint32_t disable_interface_mask);
281static inline void __attribute__((noreturn)) reset_usb_boot(uint32_t usb_activity_gpio_pin_mask, uint32_t disable_interface_mask) {
308void __attribute__((noreturn))
rom_reset_usb_boot_extra(
int usb_activity_gpio_pin, uint32_t disable_interface_mask,
bool usb_activity_gpio_pin_active_low);
323 rom_connect_internal_flash_fn func = (rom_connect_internal_flash_fn)
rom_func_lookup_inline(ROM_FUNC_CONNECT_INTERNAL_FLASH);
390 rom_flash_range_erase_fn func = (rom_flash_range_erase_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_RANGE_ERASE);
391 func(addr, count, block_size, block_cmd);
415 rom_flash_range_program_fn func = (rom_flash_range_program_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_RANGE_PROGRAM);
416 func(addr, data, count);
437 rom_flash_flush_cache_fn func = (rom_flash_flush_cache_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_FLUSH_CACHE);
453 rom_flash_enter_cmd_xip_fn func = (rom_flash_enter_cmd_xip_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_ENTER_CMD_XIP);
457#if !PICO_RP2040 || PICO_COMBINED_DOCS
475static inline int rom_set_bootrom_stack(bootrom_stack_t *stack) {
476 rom_set_bootrom_stack_fn func = (rom_set_bootrom_stack_fn)
rom_func_lookup_inline(ROM_FUNC_SET_BOOTROM_STACK);
527static inline int rom_reboot(uint32_t flags, uint32_t delay_ms, uint32_t p0, uint32_t p1) {
530 if (!delay_ms) delay_ms = 1;
533 return func(flags, delay_ms, p0, p1);
574 rom_bootrom_state_reset_fn func = (rom_bootrom_state_reset_fn)
rom_func_lookup_inline(ROM_FUNC_BOOTROM_STATE_RESET);
587 rom_flash_reset_address_trans_fn func = (rom_flash_reset_address_trans_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_RESET_ADDRESS_TRANS);
602 rom_flash_select_xip_read_mode_fn func = (rom_flash_select_xip_read_mode_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_SELECT_XIP_READ_MODE);
620static inline void rom_helper_flash_op(
void *param) {
665static inline int rom_flash_op(cflash_flags_t flags, uintptr_t addr, uint32_t size_bytes, uint8_t *buf) {
666 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_FLASH_OP))
667 return BOOTROM_ERROR_LOCK_REQUIRED;
672 .size_bytes = size_bytes,
677 bootrom_release_lock(BOOTROM_LOCK_FLASH_OP);
711 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_OTP))
712 return BOOTROM_ERROR_LOCK_REQUIRED;
713 int rc = func(buf, buf_len, cmd);
714 bootrom_release_lock(BOOTROM_LOCK_OTP);
747 rom_get_partition_table_info_fn func = (rom_get_partition_table_info_fn)
rom_func_lookup_inline(ROM_FUNC_GET_PARTITION_TABLE_INFO);
748 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
749 return BOOTROM_ERROR_LOCK_REQUIRED;
750 int rc = func(out_buffer, out_buffer_word_size, partition_and_flags);
751 bootrom_release_lock(BOOTROM_LOCK_SHA_256);
772 rom_load_partition_table_fn func = (rom_load_partition_table_fn)
rom_func_lookup_inline(ROM_FUNC_LOAD_PARTITION_TABLE);
773 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
774 return BOOTROM_ERROR_LOCK_REQUIRED;
775 int rc = func(workarea_base, workarea_size, force_reload);
776 bootrom_release_lock(BOOTROM_LOCK_SHA_256);
807static inline int rom_pick_ab_partition(uint8_t *workarea_base, uint32_t workarea_size, uint partition_a_num, uint32_t flash_update_boot_window_base) {
808 rom_pick_ab_partition_fn func = (rom_pick_ab_partition_fn)
rom_func_lookup_inline(ROM_FUNC_PICK_AB_PARTITION);
809 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
810 return BOOTROM_ERROR_LOCK_REQUIRED;
811 int rc = func(workarea_base, workarea_size, partition_a_num, flash_update_boot_window_base);
812 bootrom_release_lock(BOOTROM_LOCK_SHA_256);
878 rom_get_uf2_target_partition_fn func = (rom_get_uf2_target_partition_fn)
rom_func_lookup_inline(ROM_FUNC_GET_UF2_TARGET_PARTITION);
879 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
880 return BOOTROM_ERROR_LOCK_REQUIRED;
881 int rc = func(workarea_base, workarea_size, family_id, partition_out);
882 bootrom_release_lock(BOOTROM_LOCK_SHA_256);
899 rom_flash_runtime_to_storage_addr_fn func = (rom_flash_runtime_to_storage_addr_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_RUNTIME_TO_STORAGE_ADDR);
900 return func(flash_runtime_addr);
931static inline int rom_chain_image(uint8_t *workarea_base, uint32_t workarea_size, uint32_t region_base, uint32_t region_size) {
933 bootrom_release_lock(BOOTROM_LOCK_ENABLE);
935 int rc = func(workarea_base, workarea_size, region_base, region_size);
937 bootrom_acquire_lock_blocking(BOOTROM_LOCK_ENABLE);
953static inline void rom_helper_explicit_buy(
void *param) {
989 .buffer_size = buffer_size,
1016 rom_set_ns_api_permission_fn func = (rom_set_ns_api_permission_fn)
rom_func_lookup_inline(ROM_FUNC_SET_NS_API_PERMISSION);
1017 return func(ns_api_num, allowed);
1042 rom_validate_ns_buffer_fn func = (rom_validate_ns_buffer_fn)
rom_func_lookup_inline(ROM_FUNC_VALIDATE_NS_BUFFER);
1043 return func(addr, size, write, ok);
1062 return func(callback_num, funcptr);
1097static inline int rom_get_sys_info(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t flags) {
1099 return func(out_buffer, out_buffer_word_size, flags);
1122static inline int rom_get_boot_info(
boot_info_t *info) {
1125 if (words_returned == (
sizeof(result)/
sizeof(result[0])) && result[0] == SYS_INFO_BOOT_INFO) {
1133static inline int rom_get_last_boot_type_with_chained_flag(
void) {
1136 if (words_returned ==
count_of(result) && result[0] == SYS_INFO_BOOT_INFO) {
1138 return (
int)((result[1] & 0xff00u) >> 8);
1149static inline int rom_get_last_boot_type(
void) {
1150 int rc = rom_get_last_boot_type_with_chained_flag();
1151 if (rc >= 0) rc &= ~BOOT_TYPE_CHAINED_FLAG;
int(* rom_func_secure_call)(uintptr_t a0,...)
Definition bootrom.h:94
intptr_t(* rom_set_rom_callback_fn)(uint callback_num, bootrom_api_callback_generic_t funcptr)
Definition bootrom.h:57
static __force_inline uint32_t save_and_disable_interrupts(void)
Disable interrupts on the calling core, returning the previous interrupt state.
Definition sync.h:252
static __force_inline void restore_interrupts_from_disabled(uint32_t status)
Restore interrupts to a specified state on the calling core with restricted transitions.
Definition sync.h:295
@ PICO_OK
No error; the operation succeeded.
Definition error.h:23
@ PICO_ERROR_INVALID_DATA
A data structure failed to validate.
Definition error.h:40
static int rom_get_sys_info(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t flags)
Get system information.
Definition bootrom.h:1097
static void rom_flash_flush_cache(void)
Flush the XIP cache.
Definition bootrom.h:436
static int rom_pick_ab_partition(uint8_t *workarea_base, uint32_t workarea_size, uint partition_a_num, uint32_t flash_update_boot_window_base)
Pick a partition from an A/B pair.
Definition bootrom.h:807
int rom_add_flash_runtime_partition(uint32_t start_offset, uint32_t size, uint32_t permissions)
Add a runtime partition to the partition table to specify flash permissions.
Definition bootrom.c:99
void rom_reset_usb_boot(uint32_t usb_activity_gpio_pin_mask, uint32_t disable_interface_mask)
Reboot the device into BOOTSEL mode.
Definition bootrom.c:48
int rom_pick_ab_partition_during_update(uint32_t *workarea_base, uint32_t workarea_size, uint partition_a_num)
Pick A/B partition without disturbing any in progress Flash Update boot or TBYB boot.
Definition bootrom.c:128
static uint32_t rom_table_code(uint8_t c1, uint8_t c2)
Return a bootrom lookup code based on two ASCII characters.
Definition bootrom.h:126
static int rom_get_partition_table_info(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t partition_and_flags)
Fills a buffer with information from the partition table.
Definition bootrom.h:746
bool rom_funcs_lookup(uint32_t *table, unsigned int count)
Helper function to lookup the addresses of multiple bootrom functions.
Definition bootrom.c:25
static int rom_explicit_buy(uint8_t *buffer, uint32_t buffer_size)
Buy an image.
Definition bootrom.h:985
static void rom_connect_internal_flash(void)
Connect the SSI/QMI to the QSPI pads.
Definition bootrom.h:322
static void rom_flash_exit_xip(void)
Return the QSPI device from its XIP state to a serial command state.
Definition bootrom.h:353
static void rom_flash_enter_cmd_xip(void)
Configure the SSI/QMI with a standard command.
Definition bootrom.h:452
static void rom_bootrom_state_reset(uint32_t flags)
Reset bootrom state.
Definition bootrom.h:573
#define ROM_TABLE_CODE(c1, c2)
Return a bootrom lookup code based on two ASCII characters.
Definition bootrom_constants.h:162
static void rom_flash_reset_address_trans(void)
Reset address translation.
Definition bootrom.h:586
static intptr_t rom_flash_runtime_to_storage_addr(uintptr_t flash_runtime_addr)
Translate runtime to storage address.
Definition bootrom.h:898
static void rom_flash_range_program(uint32_t addr, const uint8_t *data, size_t count)
Program bytes in flash.
Definition bootrom.h:414
static int rom_func_otp_access(uint8_t *buf, uint32_t buf_len, otp_cmd_t cmd)
Writes data from a buffer into OTP, or reads data from OTP into a buffer.
Definition bootrom.h:709
static int rom_flash_op(cflash_flags_t flags, uintptr_t addr, uint32_t size_bytes, uint8_t *buf)
Perform a flash read, erase, or program operation.
Definition bootrom.h:665
static __force_inline void * rom_func_lookup_inline(uint32_t code)
Lookup a bootrom function by code. This method is forcibly inlined into the caller for FLASH/RAM sens...
Definition bootrom.h:199
static int rom_get_b_partition(uint pi_a)
Get B partition.
Definition bootrom.h:851
void * rom_func_lookup(uint32_t code)
Lookup a bootrom function by its code.
Definition bootrom.c:16
static void * rom_validate_ns_buffer(const void *addr, uint32_t size, uint32_t write, uint32_t *ok)
Validate NS Buffer.
Definition bootrom.h:1041
static void rom_flash_select_xip_read_mode(bootrom_xip_mode_t mode, uint8_t clkdiv)
Configure QMI in a XIP read mode.
Definition bootrom.h:601
static __force_inline void * rom_data_lookup_inline(uint32_t code)
Lookup a bootrom data address by its code. This method is forcibly inlined into the caller for FLASH/...
Definition bootrom.h:233
bool rom_get_boot_random(uint32_t out[4])
Get the per boot random number.
Definition bootrom.c:87
static int rom_get_uf2_target_partition(uint8_t *workarea_base, uint32_t workarea_size, uint32_t family_id, resident_partition_t *partition_out)
Get UF2 Target Partition.
Definition bootrom.h:877
static intptr_t rom_set_rom_callback(uint callback_num, bootrom_api_callback_generic_t funcptr)
Set ROM callback function.
Definition bootrom.h:1060
static void rom_flash_range_erase(uint32_t addr, size_t count, uint32_t block_size, uint8_t block_cmd)
Erase bytes in flash.
Definition bootrom.h:389
void * rom_data_lookup(uint32_t code)
Lookup a bootrom data address by its code.
Definition bootrom.c:20
static int rom_reboot(uint32_t flags, uint32_t delay_ms, uint32_t p0, uint32_t p1)
Reboot using the watchdog.
Definition bootrom.h:527
void rom_reset_usb_boot_extra(int usb_activity_gpio_pin, uint32_t disable_interface_mask, bool usb_activity_gpio_pin_active_low)
Reboot the device into BOOTSEL mode.
Definition bootrom.c:66
static int rom_set_ns_api_permission(uint ns_api_num, bool allowed)
Set NS API Permission.
Definition bootrom.h:1015
static int rom_chain_image(uint8_t *workarea_base, uint32_t workarea_size, uint32_t region_base, uint32_t region_size)
Chain into a launchable image.
Definition bootrom.h:931
static int rom_load_partition_table(uint8_t *workarea_base, uint32_t workarea_size, bool force_reload)
Loads the current partition table from flash, if present.
Definition bootrom.h:771
int flash_safe_execute(void(*func)(void *), void *param, uint32_t enter_exit_timeout_ms)
Execute a function with IRQs disabled and with the other core also not executing/reading flash.
Definition flash.c:75
Boot information returned by the bootrom SYS_INFO_BOOT_INFO query.
Definition bootrom.h:1108
int8_t partition
The partition that was booted, or -1 if not applicable.
Definition bootrom.h:1113
uint32_t boot_word
The four boot fields packed into a single 32-bit word.
Definition bootrom.h:1116
uint32_t reboot_params[2]
Parameters passed to the reboot call that initiated this boot.
Definition bootrom.h:1119
uint8_t tbyb_and_update_info
Try-before-you-buy and flash update status flags.
Definition bootrom.h:1114
int8_t diagnostic_partition_index
Partition index used for diagnostics; uses BOOT_PARTITION constants.
Definition bootrom.h:1111
uint8_t boot_type
The type of boot that occurred (e.g. BOOT_TYPE_NORMAL, BOOT_TYPE_BOOTSEL).
Definition bootrom.h:1112
uint32_t boot_diagnostic
Diagnostic word describing the outcome of the most recent boot attempt.
Definition bootrom.h:1118
Definition bootrom_constants.h:276
Parameters for the explicit buy helper used with flash_safe_execute.
Definition bootrom.h:947
uint8_t * buffer
Word-aligned base address of the scratch space buffer.
Definition bootrom.h:948
uint32_t buffer_size
Size of the scratch space buffer in bytes (at least 4 KiB).
Definition bootrom.h:949
int * res
Pointer to store the return code from the explicit buy operation.
Definition bootrom.h:950
Parameters for the flash operation helper used with flash_safe_execute.
Definition bootrom.h:612
int * res
Pointer to store the return code from the flash operation.
Definition bootrom.h:617
uint8_t * buf
Buffer for data to be written to or read from flash.
Definition bootrom.h:616
cflash_flags_t flags
Flags controlling the security level, address space, and flash operation.
Definition bootrom.h:613
uint32_t size_bytes
Size of the buffer in bytes.
Definition bootrom.h:615
uintptr_t addr
Address of the first flash byte to be accessed.
Definition bootrom.h:614