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);
57typedef intptr_t (*rom_set_rom_callback_fn)(uint callback_num, bootrom_api_callback_generic_t funcptr);
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);
94typedef int (*rom_func_secure_call)(uintptr_t a0, ...);
103typedef int (*rom_set_bootrom_stack_fn)(bootrom_stack_t *stack);
156typedef void *(*rom_table_lookup_fn)(uint16_t *table, uint32_t code);
158typedef void *(*rom_table_lookup_fn)(uint32_t code, uint32_t mask);
161#if PICO_C_COMPILER_IS_GNU && (__GNUC__ >= 12)
163__force_inline static void *rom_hword_as_ptr(uint16_t rom_address) {
164#pragma GCC diagnostic push
165#pragma GCC diagnostic ignored "-Warray-bounds"
166 return (
void *)(uintptr_t)*(uint16_t *)(uintptr_t)rom_address;
167#pragma GCC diagnostic pop
171#define rom_hword_as_ptr(rom_address) (void *)(uintptr_t)(*(uint16_t *)(uintptr_t)(rom_address))
176#ifdef RASPBERRYPI_AMETHYST_FPGA
177 return *(uint16_t*)0x14 >= 0x8000;
190#pragma GCC diagnostic push
192#pragma GCC diagnostic ignored "-Warray-bounds"
195 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) rom_hword_as_ptr(BOOTROM_TABLE_LOOKUP_OFFSET);
196 uint16_t *func_table = (uint16_t *) rom_hword_as_ptr(BOOTROM_FUNC_TABLE_OFFSET);
197 return rom_table_lookup(func_table, code);
200 uint32_t rom_offset_adjust = rom_size_is_64k() ? 32 * 1024 : 0;
202 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_ENTRY_OFFSET + rom_offset_adjust);
203 return rom_table_lookup(code, RT_FLAG_FUNC_RISCV);
207 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_OFFSET);
208 if (pico_processor_state_is_nonsecure()) {
209 return rom_table_lookup(code, RT_FLAG_FUNC_ARM_NONSEC);
211 return rom_table_lookup(code, RT_FLAG_FUNC_ARM_SEC);
216#pragma GCC diagnostic pop
224#pragma GCC diagnostic push
226#pragma GCC diagnostic ignored "-Warray-bounds"
229 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) rom_hword_as_ptr(BOOTROM_TABLE_LOOKUP_OFFSET);
230 uint16_t *data_table = (uint16_t *) rom_hword_as_ptr(BOOTROM_DATA_TABLE_OFFSET);
231 return rom_table_lookup(data_table, code);
234 uint32_t rom_offset_adjust = rom_size_is_64k() ? 32 * 1024 : 0;
235 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_OFFSET + rom_offset_adjust);
237 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_OFFSET);
239 return rom_table_lookup(code, RT_FLAG_DATA);
242#pragma GCC diagnostic pop
261void __attribute__((noreturn))
rom_reset_usb_boot(uint32_t usb_activity_gpio_pin_mask, uint32_t disable_interface_mask);
262static inline void __attribute__((noreturn)) reset_usb_boot(uint32_t usb_activity_gpio_pin_mask, uint32_t disable_interface_mask) {
283void __attribute__((noreturn))
rom_reset_usb_boot_extra(
int usb_activity_gpio_pin, uint32_t disable_interface_mask,
bool usb_activity_gpio_pin_active_low);
298 rom_connect_internal_flash_fn func = (rom_connect_internal_flash_fn)
rom_func_lookup_inline(ROM_FUNC_CONNECT_INTERNAL_FLASH);
365 rom_flash_range_erase_fn func = (rom_flash_range_erase_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_RANGE_ERASE);
366 func(addr, count, block_size, block_cmd);
390 rom_flash_range_program_fn func = (rom_flash_range_program_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_RANGE_PROGRAM);
391 func(addr, data, count);
412 rom_flash_flush_cache_fn func = (rom_flash_flush_cache_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_FLUSH_CACHE);
428 rom_flash_enter_cmd_xip_fn func = (rom_flash_enter_cmd_xip_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_ENTER_CMD_XIP);
432#if !PICO_RP2040 || PICO_COMBINED_DOCS
450static inline int rom_set_bootrom_stack(bootrom_stack_t *stack) {
451 rom_set_bootrom_stack_fn func = (rom_set_bootrom_stack_fn)
rom_func_lookup_inline(ROM_FUNC_SET_BOOTROM_STACK);
502static inline int rom_reboot(uint32_t flags, uint32_t delay_ms, uint32_t p0, uint32_t p1) {
504 return func(flags, delay_ms, p0, p1);
507bool rom_get_boot_random(uint32_t out[4]);
533static inline void rom_bootrom_state_reset(uint32_t flags) {
534 rom_bootrom_state_reset_fn func = (rom_bootrom_state_reset_fn)
rom_func_lookup_inline(ROM_FUNC_BOOTROM_STATE_RESET);
546static inline void rom_flash_reset_address_trans(
void) {
547 rom_flash_reset_address_trans_fn func = (rom_flash_reset_address_trans_fn)
rom_func_lookup_inline(ROM_FUNC_FLASH_RESET_ADDRESS_TRANS);
561static inline void rom_flash_select_xip_read_mode(bootrom_xip_mode_t mode, uint8_t clkdiv) {
562 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);
572} rom_helper_flash_op_params_t;
574static inline void rom_helper_flash_op(
void *param) {
575 const rom_helper_flash_op_params_t *op = (
const rom_helper_flash_op_params_t *)param;
577 *(op->res) = func(op->flags, op->addr, op->size_bytes, op->buf);
619static inline int rom_flash_op(
cflash_flags_t flags, uintptr_t addr, uint32_t size_bytes, uint8_t *buf) {
620 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_FLASH_OP))
621 return BOOTROM_ERROR_LOCK_REQUIRED;
623 rom_helper_flash_op_params_t params = {
626 .size_bytes = size_bytes,
631 bootrom_release_lock(BOOTROM_LOCK_FLASH_OP);
663static inline int rom_func_otp_access(uint8_t *buf, uint32_t buf_len,
otp_cmd_t cmd) {
665 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_OTP))
666 return BOOTROM_ERROR_LOCK_REQUIRED;
667 int rc = func(buf, buf_len, cmd);
668 bootrom_release_lock(BOOTROM_LOCK_OTP);
700static inline int rom_get_partition_table_info(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t partition_and_flags) {
701 rom_get_partition_table_info_fn func = (rom_get_partition_table_info_fn)
rom_func_lookup_inline(ROM_FUNC_GET_PARTITION_TABLE_INFO);
702 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
703 return BOOTROM_ERROR_LOCK_REQUIRED;
704 int rc = func(out_buffer, out_buffer_word_size, partition_and_flags);
705 bootrom_release_lock(BOOTROM_LOCK_SHA_256);
725static inline int rom_load_partition_table(uint8_t *workarea_base, uint32_t workarea_size,
bool force_reload) {
726 rom_load_partition_table_fn func = (rom_load_partition_table_fn)
rom_func_lookup_inline(ROM_FUNC_LOAD_PARTITION_TABLE);
727 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
728 return BOOTROM_ERROR_LOCK_REQUIRED;
729 int rc = func(workarea_base, workarea_size, force_reload);
730 bootrom_release_lock(BOOTROM_LOCK_SHA_256);
757static 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) {
758 rom_pick_ab_partition_fn func = (rom_pick_ab_partition_fn)
rom_func_lookup_inline(ROM_FUNC_PICK_AB_PARTITION);
759 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
760 return BOOTROM_ERROR_LOCK_REQUIRED;
761 int rc = func(workarea_base, workarea_size, partition_a_num, flash_update_boot_window_base);
762 bootrom_release_lock(BOOTROM_LOCK_SHA_256);
775static inline int rom_get_b_partition(uint pi_a) {
801static inline int rom_get_uf2_target_partition(uint8_t *workarea_base, uint32_t workarea_size, uint32_t family_id,
resident_partition_t *partition_out) {
802 rom_get_uf2_target_partition_fn func = (rom_get_uf2_target_partition_fn)
rom_func_lookup_inline(ROM_FUNC_GET_UF2_TARGET_PARTITION);
803 if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
804 return BOOTROM_ERROR_LOCK_REQUIRED;
805 int rc = func(workarea_base, workarea_size, family_id, partition_out);
806 bootrom_release_lock(BOOTROM_LOCK_SHA_256);
822static inline intptr_t rom_flash_runtime_to_storage_addr(uintptr_t flash_runtime_addr) {
823 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);
824 return func(flash_runtime_addr);
855static inline int rom_chain_image(uint8_t *workarea_base, uint32_t workarea_size, uint32_t region_base, uint32_t region_size) {
857 bootrom_release_lock(BOOTROM_LOCK_ENABLE);
859 int rc = func(workarea_base, workarea_size, region_base, region_size);
861 bootrom_acquire_lock_blocking(BOOTROM_LOCK_ENABLE);
867 uint32_t buffer_size;
869} rom_helper_explicit_buy_params_t;
871static inline void rom_helper_explicit_buy(
void *param) {
872 const rom_helper_explicit_buy_params_t *op = (
const rom_helper_explicit_buy_params_t *)param;
874 *(op->res) = func(op->buffer, op->buffer_size);
903static inline int rom_explicit_buy(uint8_t *buffer, uint32_t buffer_size) {
905 rom_helper_explicit_buy_params_t params = {
907 .buffer_size = buffer_size,
933static inline int rom_set_ns_api_permission(uint ns_api_num,
bool allowed) {
934 rom_set_ns_api_permission_fn func = (rom_set_ns_api_permission_fn)
rom_func_lookup_inline(ROM_FUNC_SET_NS_API_PERMISSION);
935 return func(ns_api_num, allowed);
959static inline void* rom_validate_ns_buffer(
const void *addr, uint32_t size, uint32_t write, uint32_t *ok) {
960 rom_validate_ns_buffer_fn func = (rom_validate_ns_buffer_fn)
rom_func_lookup_inline(ROM_FUNC_VALIDATE_NS_BUFFER);
961 return func(addr, size, write, ok);
978static inline intptr_t rom_set_rom_callback(uint callback_num, bootrom_api_callback_generic_t funcptr) {
979 rom_set_rom_callback_fn func = (rom_set_rom_callback_fn)
rom_func_lookup_inline(ROM_FUNC_SET_ROM_CALLBACK);
980 return func(callback_num, funcptr);
983#define BOOT_TYPE_NORMAL 0
984#define BOOT_TYPE_BOOTSEL 2
985#define BOOT_TYPE_RAM_IMAGE 3
986#define BOOT_TYPE_FLASH_UPDATE 4
989#define BOOT_TYPE_PC_SP 0xd
992#define BOOT_TYPE_CHAINED_FLAG 0x80
1026static inline int rom_get_sys_info(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t flags) {
1028 return func(out_buffer, out_buffer_word_size, flags);
1034 int8_t diagnostic_partition_index;
1037 uint8_t tbyb_and_update_info;
1041 uint32_t boot_diagnostic;
1042 uint32_t reboot_params[2];
1045static inline int rom_get_boot_info(boot_info_t *info) {
1047 int words_returned = rom_get_sys_info(result, 5, SYS_INFO_BOOT_INFO);
1048 if (words_returned == (
sizeof(result)/
sizeof(result[0])) && result[0] == SYS_INFO_BOOT_INFO) {
1049 memcpy(info, &result[1],
sizeof(boot_info_t));
1056static inline int rom_get_last_boot_type_with_chained_flag(
void) {
1058 int words_returned = rom_get_sys_info(result, 5, SYS_INFO_BOOT_INFO);
1059 if (words_returned ==
count_of(result) && result[0] == SYS_INFO_BOOT_INFO) {
1061 return (
int)((result[1] & 0xff00u) >> 8);
1072static inline int rom_get_last_boot_type(
void) {
1073 int rc = rom_get_last_boot_type_with_chained_flag();
1074 if (rc >= 0) rc &= ~BOOT_TYPE_CHAINED_FLAG;
1092int rom_add_flash_runtime_partition(uint32_t start_offset, uint32_t size, uint32_t permissions);
static __force_inline uint32_t save_and_disable_interrupts(void)
Disable interrupts on the calling core, returning the previous interrupt state.
Definition sync.h:237
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:280
@ 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 void rom_flash_flush_cache(void)
Flush the XIP cache.
Definition bootrom.h:411
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:32
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:120
bool rom_funcs_lookup(uint32_t *table, unsigned int count)
Helper function to lookup the addresses of multiple bootrom functions.
Definition bootrom.c:22
static void rom_connect_internal_flash(void)
Connect the SSI/QMI to the QSPI pads.
Definition bootrom.h:297
static void rom_flash_exit_xip(void)
Return the QSPI device from its XIP state to a serial command state.
Definition bootrom.h:328
static void rom_flash_enter_cmd_xip(void)
Configure the SSI/QMI with a standard command.
Definition bootrom.h:427
#define ROM_TABLE_CODE(c1, c2)
Return a bootrom lookup code based on two ASCII characters.
Definition bootrom_constants.h:155
static void rom_flash_range_program(uint32_t addr, const uint8_t *data, size_t count)
Program bytes in flash.
Definition bootrom.h:389
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:193
void * rom_func_lookup(uint32_t code)
Lookup a bootrom function by its code.
Definition bootrom.c:13
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:227
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:364
void * rom_data_lookup(uint32_t code)
Lookup a bootrom data address by its code.
Definition bootrom.c:17
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:50
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
Definition bootrom_constants.h:307
Definition bootrom_constants.h:275
Definition bootrom_constants.h:269