1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! RP2350 bootrom (mask ROM) API access
//!
//! The bootrom publishes a table of callable functions that stay resident for
//! the lifetime of the chip. This module resolves entries in that table and
//! exposes the ones the HAL needs today; [`reboot`] is the watchdog-based reset
//! that backs [`crate::reboot`].
//!
//! ## Features
//!
//! - [`rom_version_number`] — version byte of the resident bootrom
//! - [`rom_table_lookup`] — resolve a two-character tag (`b"RB"` = reboot,
//! `b"GS"` = `get_sys_info`, …) to a function address
//! - [`rom_data_lookup`] — resolve a data tag (`b"GR"` = git revision,
//! `b"PT"` = resident partition table) to a data address
//! - [`get_sys_info`] — raw access to the bootrom's `get_sys_info` call
//! - [`flash_op`] — raw access to the bootrom's checked flash read, erase and
//! program
//! - [`reboot`] — reset into the normal, BOOTSEL, RAM image, flash update or
//! PC/SP boot path
//!
//! The typed half lives in sibling modules: [`crate::reboot`] wraps
//! [`reboot`], [`crate::sysinfo_rom`] decodes [`get_sys_info`], and
//! [`crate::flash`] wraps [`flash_op`].
//!
//! ## Example
//!
//! ```ignore
//! // ROM-level: reboot into BOOTSEL 500 ms from now, both USB interfaces
//! // enabled. `crate::reboot` is the typed, safe wrapper around this.
//! let rc = unsafe { rp2350::bootrom::reboot(0x0002, 500, 0, 0) };
//! assert!(rc >= 0, "bootrom refused to reboot: {}", rc);
//! ```
//!
//! ## Notes
//!
//! - The well-known addresses below are bootrom entry points, not peripheral
//! registers, so they cannot come from the PAC (RP2350 datasheet §5.5.1).
//! - Bootrom calls that report an `i32` use the negative error codes of
//! RP2350 datasheet §5.4.3; [`BootRomError`] names the ones this HAL knows.
//! - [`flash_op`] puts QMI into direct mode while it runs, so neither core may
//! fetch instructions or data from XIP during the call; [`crate::flash`]
//! spells out what that means in practice.
//! - Secure and Non-secure ARM callers see separate function tables. The lookup
//! call itself works in both security states, so [`reboot`] and
//! [`get_sys_info`] ask for the Secure entry first and fall back to the
//! Non-secure one instead of having to determine the current state via `TT`.
//! - `reset_usb_boot` (`b"UB"`) is RP2040-only. On RP2350 the same effect comes
//! from [`reboot`] with boot type `BOOTSEL` (`0x2`), which is what
//! [`crate::reboot::RebootKind::BootSel`] uses.
//! - This module targets the ARM (Cortex-M33) bootrom only: the HAL does not
//! support the RISC-V cores, so their well-known addresses and function flags
//! are not implemented.
/// A bootrom table tag, e.g. `b"RB"` for the reboot function.
pub type RomFnTableCode = ;
/// Flags selecting an entry type in [`rom_table_lookup`] / [`rom_data_lookup`]
/// (RP2350 datasheet §5.5.1).
/// Type of the bootrom's table lookup function.
type RomTableLookupFn = unsafe extern "C" fn ;
/// Type of the bootrom's `reboot` function.
type RomRebootFn = unsafe extern "C" fn ;
/// Type of the bootrom's `get_sys_info` function.
type RomGetSysInfoFn = unsafe extern "C" fn ;
/// Type of the bootrom's `flash_op` function.
type RomFlashOpFn =
unsafe extern "C" fn ;
/// Address of the bootrom version byte.
const ROM_VERSION_NUMBER: *const u8 = 0x0000_0013 as _;
/// Well-known pointer to the table lookup function, ROM version 2 (16-bit value).
const ROM_TABLE_LOOKUP_V2: *const u16 = 0x0000_0016 as _;
/// Well-known pointer to the table lookup function, ROM version 1 (32-bit value).
const ROM_TABLE_LOOKUP_V1: *const u32 = 0x0000_0018 as _;
/// Tag of the bootrom `reboot` function.
const TAG_REBOOT: RomFnTableCode = *b"RB";
/// Tag of the bootrom `get_sys_info` function.
const TAG_GET_SYS_INFO: RomFnTableCode = *b"GS";
/// Tag of the bootrom `flash_op` function.
const TAG_FLASH_OP: RomFnTableCode = *b"FO";
/// Tag of the bootrom git revision data entry.
const TAG_GIT_REVISION: RomFnTableCode = *b"GR";
/// Tag of the resident partition table data entry.
const TAG_PARTITION_TABLE: RomFnTableCode = *b"PT";
/// Version number of the resident bootrom (the byte at `0x13`).
/// Resolve a bootrom function table entry by tag.
///
/// `tag` is a two-character code (`b"RB"` reboot, `b"GS"` `get_sys_info`, …)
/// and `mask` is one of [`rt_flags`]. Returns `0` when the tag is not present
/// in the table.
/// Resolve a bootrom data table entry by tag.
///
/// The data table holds pointers rather than code, so pass [`rt_flags::DATA`]
/// as `mask`: `b"GR"` is the bootrom git revision, `b"PT"` the resident
/// partition table, and so on. Returns `0` when the tag is not present.
///
/// On ARM the bootrom uses a single lookup function for both tables, so this
/// differs from [`rom_table_lookup`] only in the `mask` callers pass.
/// Error codes returned by bootrom calls that report an `i32`
/// (RP2350 datasheet §5.4.3).
///
/// `0` means success and is not represented here. A negative value maps to the
/// matching variant; anything the datasheet does not define maps to
/// [`BootRomError::Unknown`].
/// Read system information into `out`, selected by `flags`.
///
/// This is the bootrom's `get_sys_info` call (tag `GS`) without any decoding:
/// `out` must have room for the whole block, and the return value is the number
/// of words the bootrom wrote — the first of which reports which requested
/// flags it satisfied — or a negative [`BootRomError`] code. See
/// [`crate::sysinfo_rom`] for typed access.
///
/// The flag values and block layouts are in RP2350 datasheet §5.5.11.1.
/// Resolve the `get_sys_info` entry, preferring the Secure table.
/// Perform a checked flash read, erase or program (tag `FO`).
///
/// `flags` is the bootrom's `CFLASH` word: an operation, an address space and
/// an effective security level (RP2350 datasheet §5.5.8.2). The bootrom checks
/// the range against the devices described by `FLASH_DEVINFO` and, when a
/// partition table is in effect, against the partition permissions, then runs
/// the operation. Erase needs 4 KiB alignment, program 256-byte alignment, and
/// read has no constraint.
///
/// Returns `0` on success or a negative [`BootRomError`] code. See
/// [`crate::flash`] for the typed, high-level wrapper.
///
/// # Safety
///
/// `buffer` must point at `size_bytes` readable bytes for a read, or
/// `size_bytes` writable bytes for a program. An erase does not touch it, so it
/// may be null with any `size_bytes`. The bootrom validates the flash range,
/// not the buffer.
///
/// QMI is in direct mode while the call runs, so neither core may fetch
/// instructions or data from XIP: run the caller from RAM.
pub unsafe
/// Resolve the `flash_op` entry, preferring the Secure table.
/// Ask the bootrom to reset the chip through the watchdog.
///
/// `flags` selects the boot path; see [`crate::reboot`] for the typed API and
/// the meaning of `p0`/`p1` (RP2350 datasheet §5.5.10.1). The reset is
/// asynchronous: it fires `delay_ms` after this call returns.
///
/// `p0`/`p1` are per boot type: `BOOTSEL` takes the flag word (interface
/// disables, activity LED) in `p0` and the activity GPIO number in `p1`,
/// whereas `PC_SP` takes PC then SP. The `bootrom.h` and
/// `picoboot_constants.h` comments have `BOOTSEL` the other way round, but the
/// SDK's own RP2350 implementation of `reset_usb_boot()` passes the flags in
/// `p0` (`pico_bootrom/bootrom.c`, the `#elif defined(ROM_FUNC_REBOOT)`
/// branch), and that is the ordering mirrored here.
///
/// Returns the bootrom result: `0` (`BOOTROM_OK`) when the reboot was
/// initiated, or a negative error code when it was refused.
///
/// # Safety
///
/// The bootrom performs no validation of `p0`/`p1`. With a `PC_SP` boot the
/// chip jumps to `p0` with stack pointer `p1`, and `RAM_IMAGE` / `FLASH_UPDATE`
/// interpret `p0` as an address, so the `(flags, p0, p1)` triple must be
/// self-consistent. Prefer [`crate::reboot::reboot`].
pub unsafe
/// Read the well-known pointer to the bootrom's table lookup function.
/// Resolve the `reboot` entry, preferring the Secure table.
/// The eight most significant hex digits of the bootrom's git revision.
///
/// Useful when a workaround depends on which bootrom build is resident.
/// Pointer to the resident partition table.
///
/// The bootrom keeps the loaded partition table in RAM and consults it for
/// flash permissions and ranges. The `PT` data entry holds a pointer to a
/// pointer to that structure, whose layout the bootrom treats as internal, so
/// this returns a raw word-aligned pointer to it.