Loading...
Searching...
No Matches
bootrom.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _PICO_BOOTROM_H
8#define _PICO_BOOTROM_H
9
10#include "pico.h"
11#include "pico/bootrom_constants.h"
12
19
20#ifndef __ASSEMBLER__
21#include <string.h>
22#include "pico/bootrom/lock.h"
23#include "pico/flash.h"
24// ROM FUNCTION SIGNATURES
25
26#if PICO_RP2040
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);
35#endif
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; // kept for backwards compatibility
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);
45#if !PICO_RP2040
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);
64// Apply the address translation currently specified in QMI_ATRANSx ("rolling window" hardware
65// translation). Need to take care using this on the boot path, as the QMI may not yet have been
66// set up, but this should be suitable for translating system bus addresses into flash storage
67// addresses in user callbacks. Returns all-ones for an invalid address, which is also an invalid
68// flash storage address, so invalidity is propagated.
69typedef intptr_t (*rom_flash_runtime_to_storage_addr_fn)(uintptr_t flash_runtime_addr);
70
71// Perform the specified erase/program/read operation, translating addresses according to
72// QMI_ATRANSx if necessary, and checking flash permissions based on the resident partition table
73// and the specified effective security level. `addr` may be either a flash runtime address or a
74// flash storage address, depending on the ASPACE given in `flags`.
75//
76// NOTE: This function does not validate the buffer for NS access. This must be validated before
77// calling if the caller is reachable from a Secure Gateway.
78typedef int (*rom_flash_op_fn)(cflash_flags_t flags, uintptr_t addr, uint32_t size_bytes, uint8_t *buf);
79
80#ifndef __riscv
81typedef int (*rom_set_ns_api_permission_fn)(uint ns_api_num, bool allowed);
94typedef int (*rom_func_secure_call)(uintptr_t a0, ...);
95#endif
96
97#ifdef __riscv
104typedef struct {
105 uint32_t *base;
106 uint32_t size;
107} bootrom_stack_t;
108// passed in, and out.
109typedef int (*rom_set_bootrom_stack_fn)(bootrom_stack_t *stack);
110#endif
111#endif
112
113#ifdef __cplusplus
114extern "C" {
115#endif
116
126static inline uint32_t rom_table_code(uint8_t c1, uint8_t c2) {
127 return ROM_TABLE_CODE((uint32_t) c1, (uint32_t) c2);
128}
129
136void *rom_func_lookup(uint32_t code);
137
144void *rom_data_lookup(uint32_t code);
145
157bool rom_funcs_lookup(uint32_t *table, unsigned int count);
158
159// Bootrom function: rom_table_lookup
160// Returns the 32 bit pointer into the ROM if found or NULL otherwise.
161#if PICO_RP2040
162typedef void *(*rom_table_lookup_fn)(uint16_t *table, uint32_t code);
163#else
164typedef void *(*rom_table_lookup_fn)(uint32_t code, uint32_t mask);
165#endif
166
167#if PICO_C_COMPILER_IS_GNU && (__GNUC__ >= 12)
168// Convert a 16 bit pointer stored at the given rom address into a 32 bit pointer
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
174}
175#else
176// Convert a 16 bit pointer stored at the given rom address into a 32 bit pointer
177#define rom_hword_as_ptr(rom_address) (void *)(uintptr_t)(*(uint16_t *)(uintptr_t)(rom_address))
178#endif
179
180#ifdef __riscv
181static __force_inline bool rom_size_is_64k(void) {
182#ifdef RASPBERRYPI_AMETHYST_FPGA
183 return *(uint16_t*)0x14 >= 0x8000;
184#else
185 return false;
186#endif
187}
188#endif
189
196#pragma GCC diagnostic push
197// diagnostic: GCC thinks near-zero value is a null pointer member access, but it's not
198#pragma GCC diagnostic ignored "-Warray-bounds"
199static __force_inline void *rom_func_lookup_inline(uint32_t code) {
200#if PICO_RP2040
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);
204#else
205#ifdef __riscv
206 uint32_t rom_offset_adjust = rom_size_is_64k() ? 32 * 1024 : 0;
207 // on RISC-V the code (a jmp) is actually embedded in the table
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);
210#else
211 // on ARM the function pointer is stored in the table, so we dereference it
212 // via lookup() rather than lookup_entry()
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);
216 } else {
217 return rom_table_lookup(code, RT_FLAG_FUNC_ARM_SEC);
218 }
219#endif
220#endif
221}
222#pragma GCC diagnostic pop
223
230#pragma GCC diagnostic push
231// diagnostic: GCC thinks near-zero value is a null pointer member access, but it's not
232#pragma GCC diagnostic ignored "-Warray-bounds"
233static __force_inline void *rom_data_lookup_inline(uint32_t code) {
234#if PICO_RP2040
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);
238#else
239#ifdef __riscv
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);
242#else
243 rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) (uintptr_t)*(uint16_t*)(BOOTROM_TABLE_LOOKUP_OFFSET);
244#endif
245 return rom_table_lookup(code, RT_FLAG_DATA);
246#endif
247}
248#pragma GCC diagnostic pop
249
250#if PICO_RP2350
251// PICO_CONFIG: PICO_BOOTROM_WORKAROUND_RP2350_A2_ACTIVITY_LED_BUG, Workaround RP2350A-A2 (QFN60) bug not displaying USB boot activity LED under Arm by making rom_reset_usb_boot functions reboot to RISC-V when the activity LED is specified, type=bool, default=1, group=pico_bootrom
252#ifndef PICO_BOOTROM_WORKAROUND_RP2350_A2_ACTIVITY_LED_BUG
253#define PICO_BOOTROM_WORKAROUND_RP2350_A2_ACTIVITY_LED_BUG 1
254#endif
255#endif
256
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) {
282 rom_reset_usb_boot(usb_activity_gpio_pin_mask, disable_interface_mask);
283}
284
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);
309
322static inline void rom_connect_internal_flash(void) {
323 rom_connect_internal_flash_fn func = (rom_connect_internal_flash_fn) rom_func_lookup_inline(ROM_FUNC_CONNECT_INTERNAL_FLASH);
324 func();
325}
326
353static inline void rom_flash_exit_xip(void) {
354 rom_flash_exit_xip_fn func = (rom_flash_exit_xip_fn) rom_func_lookup_inline(ROM_FUNC_FLASH_EXIT_XIP);
355 func();
356}
357
389static inline void rom_flash_range_erase(uint32_t addr, size_t count, uint32_t block_size, uint8_t block_cmd) {
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);
392}
393
414static inline void rom_flash_range_program(uint32_t addr, const uint8_t *data, size_t count) {
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);
417}
418
436static inline void rom_flash_flush_cache(void) {
437 rom_flash_flush_cache_fn func = (rom_flash_flush_cache_fn) rom_func_lookup_inline(ROM_FUNC_FLASH_FLUSH_CACHE);
438 func();
439}
440
452static inline void rom_flash_enter_cmd_xip(void) {
453 rom_flash_enter_cmd_xip_fn func = (rom_flash_enter_cmd_xip_fn) rom_func_lookup_inline(ROM_FUNC_FLASH_ENTER_CMD_XIP);
454 func();
455}
456
457#if !PICO_RP2040 || PICO_COMBINED_DOCS
458#ifdef __riscv
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);
477 return func(stack);
478}
479#endif
480
527static inline int rom_reboot(uint32_t flags, uint32_t delay_ms, uint32_t p0, uint32_t p1) {
528#if PICO_RP2350
529 // work around bootrom bug with 0 timeout
530 if (!delay_ms) delay_ms = 1;
531#endif
532 rom_reboot_fn func = (rom_reboot_fn) rom_func_lookup_inline(ROM_FUNC_REBOOT);
533 return func(flags, delay_ms, p0, p1);
534}
535
547bool rom_get_boot_random(uint32_t out[4]);
548
573static inline void rom_bootrom_state_reset(uint32_t flags) {
574 rom_bootrom_state_reset_fn func = (rom_bootrom_state_reset_fn) rom_func_lookup_inline(ROM_FUNC_BOOTROM_STATE_RESET);
575 return func(flags);
576}
577
586static inline void rom_flash_reset_address_trans(void) {
587 rom_flash_reset_address_trans_fn func = (rom_flash_reset_address_trans_fn) rom_func_lookup_inline(ROM_FUNC_FLASH_RESET_ADDRESS_TRANS);
588 func();
589}
590
601static inline void rom_flash_select_xip_read_mode(bootrom_xip_mode_t mode, uint8_t clkdiv) {
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);
603 func(mode, clkdiv);
604}
605
612typedef struct {
613 cflash_flags_t flags;
614 uintptr_t addr;
615 uint32_t size_bytes;
616 uint8_t *buf;
617 int *res;
619
620static inline void rom_helper_flash_op(void *param) {
622 rom_flash_op_fn func = (rom_flash_op_fn) rom_func_lookup_inline(ROM_FUNC_FLASH_OP);
623 *(op->res) = func(op->flags, op->addr, op->size_bytes, op->buf);
624}
625
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;
668 int rc = 0;
670 .flags = flags,
671 .addr = addr,
672 .size_bytes = size_bytes,
673 .buf = buf,
674 .res = &rc
675 };
676 int flash_rc = flash_safe_execute(rom_helper_flash_op, &params, UINT32_MAX);
677 bootrom_release_lock(BOOTROM_LOCK_FLASH_OP);
678 if (flash_rc != PICO_OK) {
679 return flash_rc;
680 } else {
681 return rc;
682 }
683}
684
709static inline int rom_func_otp_access(uint8_t *buf, uint32_t buf_len, otp_cmd_t cmd) {
710 rom_func_otp_access_fn func = (rom_func_otp_access_fn) rom_func_lookup_inline(ROM_FUNC_OTP_ACCESS);
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);
715 return rc;
716}
717
746static inline int rom_get_partition_table_info(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t partition_and_flags) {
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);
752 return rc;
753}
754
755// todo SECURE only
771static inline int rom_load_partition_table(uint8_t *workarea_base, uint32_t workarea_size, bool force_reload) {
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);
777 return rc;
778}
779
780// todo SECURE only
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);
813 return rc;
814}
815
840int rom_pick_ab_partition_during_update(uint32_t *workarea_base, uint32_t workarea_size, uint partition_a_num);
841
851static inline int rom_get_b_partition(uint pi_a) {
852 rom_get_b_partition_fn func = (rom_get_b_partition_fn) rom_func_lookup_inline(ROM_FUNC_GET_B_PARTITION);
853 return func(pi_a);
854}
855
856// todo SECURE only
877static inline int rom_get_uf2_target_partition(uint8_t *workarea_base, uint32_t workarea_size, uint32_t family_id, resident_partition_t *partition_out) {
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);
883 return rc;
884}
885
898static inline intptr_t rom_flash_runtime_to_storage_addr(uintptr_t flash_runtime_addr) {
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);
901}
902
903// todo SECURE only
931static inline int rom_chain_image(uint8_t *workarea_base, uint32_t workarea_size, uint32_t region_base, uint32_t region_size) {
932 rom_chain_image_fn func = (rom_chain_image_fn) rom_func_lookup_inline(ROM_FUNC_CHAIN_IMAGE);
933 bootrom_release_lock(BOOTROM_LOCK_ENABLE);
934 uint32_t interrupt_flags = save_and_disable_interrupts();
935 int rc = func(workarea_base, workarea_size, region_base, region_size);
936 restore_interrupts_from_disabled(interrupt_flags);
937 bootrom_acquire_lock_blocking(BOOTROM_LOCK_ENABLE);
938 return rc;
939}
940
947typedef struct {
948 uint8_t *buffer;
949 uint32_t buffer_size;
950 int *res;
952
953static inline void rom_helper_explicit_buy(void *param) {
955 rom_explicit_buy_fn func = (rom_explicit_buy_fn) rom_func_lookup_inline(ROM_FUNC_EXPLICIT_BUY);
956 *(op->res) = func(op->buffer, op->buffer_size);
957}
958
959// todo SECURE only
985static inline int rom_explicit_buy(uint8_t *buffer, uint32_t buffer_size) {
986 int rc = 0;
988 .buffer = buffer,
989 .buffer_size = buffer_size,
990 .res = &rc
991 };
992 int flash_rc = flash_safe_execute(rom_helper_explicit_buy, &params, UINT32_MAX);
993 if (flash_rc != PICO_OK) {
994 return flash_rc;
995 } else {
996 return rc;
997 }
998}
999
1000#ifndef __riscv
1015static inline int rom_set_ns_api_permission(uint ns_api_num, bool allowed) {
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);
1018}
1019#endif
1020
1021// todo SECURE only
1041static inline void* rom_validate_ns_buffer(const void *addr, uint32_t size, uint32_t write, uint32_t *ok) {
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);
1044}
1045
1060static inline intptr_t rom_set_rom_callback(uint callback_num, bootrom_api_callback_generic_t funcptr) {
1062 return func(callback_num, funcptr);
1063}
1064
1097static inline int rom_get_sys_info(uint32_t *out_buffer, uint32_t out_buffer_word_size, uint32_t flags) {
1098 rom_get_sys_info_fn func = (rom_get_sys_info_fn)rom_func_lookup_inline(ROM_FUNC_GET_SYS_INFO);
1099 return func(out_buffer, out_buffer_word_size, flags);
1100}
1101
1108typedef struct {
1109 union {
1110 struct __packed {
1112 uint8_t boot_type;
1113 int8_t partition;
1115 };
1116 uint32_t boot_word;
1117 };
1119 uint32_t reboot_params[2];
1120} boot_info_t;
1121
1122static inline int rom_get_boot_info(boot_info_t *info) {
1123 uint32_t result[5];
1124 int words_returned = rom_get_sys_info(result, 5, SYS_INFO_BOOT_INFO);
1125 if (words_returned == (sizeof(result)/sizeof(result[0])) && result[0] == SYS_INFO_BOOT_INFO) {
1126 memcpy(info, &result[1], sizeof(boot_info_t));
1127 return true;
1128 } else {
1129 return false;
1130 }
1131}
1132
1133static inline int rom_get_last_boot_type_with_chained_flag(void) {
1134 uint32_t result[5];
1135 int words_returned = rom_get_sys_info(result, 5, SYS_INFO_BOOT_INFO);
1136 if (words_returned == count_of(result) && result[0] == SYS_INFO_BOOT_INFO) {
1137 // todo use struct
1138 return (int)((result[1] & 0xff00u) >> 8);
1139 } else {
1141 }
1142}
1143
1144// BOOT_TYPE_NORMAL 0x0
1145// BOOT_TYPE_BOOTSEL 0x2
1146// BOOT_TYPE_RAM_IMAGE 0x3
1147// BOOT_TYPE_FLASH_UPDATE 0x4
1148// BOOT_TYPE_PC_SP 0xd
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;
1152 return rc;
1153}
1154
1169int rom_add_flash_runtime_partition(uint32_t start_offset, uint32_t size, uint32_t permissions);
1170
1171#endif
1172
1173#ifdef __cplusplus
1174}
1175#endif
1176
1177#endif // !__ASSEMBLER__
1178#endif
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
#define __force_inline
Attribute to force inlining of a function regardless of optimization level.
Definition compiler.h:128
#define count_of(a)
Macro to determine the number of elements in an array.
Definition compiler.h:135
static __force_inline bool pico_processor_state_is_nonsecure(void)
Return true if executing in the NonSecure state (Arm-only).
Definition platform.h:175
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