@web-font-path: "roboto-debian.css";
Loading...
Searching...
No Matches
bootrom_constants.h
1/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _PICO_BOOTROM_CONSTANTS_H
8#define _PICO_BOOTROM_CONSTANTS_H
9
10#ifndef NO_PICO_PLATFORM
11#include "pico/platform.h"
12#endif
13
14// ROOT ADDRESSES
15#define BOOTROM_MAGIC_OFFSET 0x10
16#define BOOTROM_FUNC_TABLE_OFFSET 0x14
17#if PICO_RP2040
18#define BOOTROM_DATA_TABLE_OFFSET 0x16
19#endif
20
21#if PICO_RP2040
22#define BOOTROM_VTABLE_OFFSET 0x00
23#define BOOTROM_TABLE_LOOKUP_OFFSET 0x18
24#else
25// todo remove this (or #ifdef it for A1/A2)
26#define BOOTROM_IS_A2() ((*(volatile uint8_t *)0x13) == 2)
27#define BOOTROM_WELL_KNOWN_PTR_SIZE (BOOTROM_IS_A2() ? 2 : 4)
28#if defined(__riscv)
29#define BOOTROM_ENTRY_OFFSET 0x7dfc
30#define BOOTROM_TABLE_LOOKUP_ENTRY_OFFSET (BOOTROM_ENTRY_OFFSET - BOOTROM_WELL_KNOWN_PTR_SIZE)
31#define BOOTROM_TABLE_LOOKUP_OFFSET (BOOTROM_ENTRY_OFFSET - BOOTROM_WELL_KNOWN_PTR_SIZE*2)
32#else
33#define BOOTROM_VTABLE_OFFSET 0x00
34#define BOOTROM_TABLE_LOOKUP_OFFSET (BOOTROM_FUNC_TABLE_OFFSET + BOOTROM_WELL_KNOWN_PTR_SIZE)
35#endif
36#endif
37
38#if !PICO_RP2040 || PICO_COMBINED_DOCS
39
40#define BOOTROM_OK 0
41//#define BOOTROM_ERROR_TIMEOUT (-1)
42//#define BOOTROM_ERROR_GENERIC (-2)
43//#define BOOTROM_ERROR_NO_DATA (-3) // E.g. read from an empty buffer/FIFO
44#define BOOTROM_ERROR_NOT_PERMITTED (-4) // Permission violation e.g. write to read-only flash partition
45#define BOOTROM_ERROR_INVALID_ARG (-5) // Argument is outside of range of supported values`
46//#define BOOTROM_ERROR_IO (-6)
47//#define BOOTROM_ERROR_BADAUTH (-7)
48//#define BOOTROM_ERROR_CONNECT_FAILED (-8)
49//#define BOOTROM_ERROR_INSUFFICIENT_RESOURCES (-9) // Dynamic allocation of resources failed
50#define BOOTROM_ERROR_INVALID_ADDRESS (-10) // Address argument was out-of-bounds or was determined to be an address that the caller may not access
51#define BOOTROM_ERROR_BAD_ALIGNMENT (-11) // Address modulo transfer chunk size was nonzero (e.g. word-aligned transfer with address % 4 != 0)
52#define BOOTROM_ERROR_INVALID_STATE (-12) // Something happened or failed to happen in the past, and consequently we (currently) can't service the request
53#define BOOTROM_ERROR_BUFFER_TOO_SMALL (-13) // A user-allocated buffer was too small to hold the result or working state of this function
54#define BOOTROM_ERROR_PRECONDITION_NOT_MET (-14) // This call failed because another ROM function must be called first
55#define BOOTROM_ERROR_MODIFIED_DATA (-15) // Cached data was determined to be inconsistent with the full version of the data it was calculated from
56#define BOOTROM_ERROR_INVALID_DATA (-16) // A data structure failed to validate
57#define BOOTROM_ERROR_NOT_FOUND (-17) // Attempted to access something that does not exist; or, a search failed
58#define BOOTROM_ERROR_UNSUPPORTED_MODIFICATION (-18) // Write is impossible based on previous writes; e.g. attempted to clear an OTP bit
59#define BOOTROM_ERROR_LOCK_REQUIRED (-19) // A required lock is not owned
60#define BOOTROM_ERROR_LAST (-19)
61
62#define RT_FLAG_FUNC_RISCV 0x0001
63#define RT_FLAG_FUNC_RISCV_FAR 0x0003
64#define RT_FLAG_FUNC_ARM_SEC 0x0004
65// reserved for 32-bit pointer: 0x0008
66#define RT_FLAG_FUNC_ARM_NONSEC 0x0010
67// reserved for 32-bit pointer: 0x0020
68#define RT_FLAG_DATA 0x0040
69// reserved for 32-bit pointer: 0x0080
70
71#define PARTITION_TABLE_MAX_PARTITIONS 16
72// note this is deliberately > MAX_PARTITIONs is likely to be, and also -1 as a signed byte
73#define PARTITION_TABLE_NO_PARTITION_INDEX 0xff
74
75// todo these are duplicated in picoboot_constants.h
76// values 0-7 are secure/non-secure
77#define BOOT_TYPE_NORMAL 0
78#define BOOT_TYPE_BOOTSEL 2
79#define BOOT_TYPE_RAM_IMAGE 3
80#define BOOT_TYPE_FLASH_UPDATE 4
81
82// values 8-15 are secure only
83#define BOOT_TYPE_PC_SP 0xd
84
85// ORed in if a bootloader chained into the image
86#define BOOT_TYPE_CHAINED_FLAG 0x80
87
88// call from NS to S
89#ifndef __riscv
90#define BOOTROM_API_CALLBACK_secure_call 0
91#endif
92#define BOOTROM_API_CALLBACK_COUNT 1
93
94#define BOOTROM_LOCK_SHA_256 0
95#define BOOTROM_LOCK_FLASH_OP 1
96#define BOOTROM_LOCK_OTP 2
97#define BOOTROM_LOCK_MAX 2
98
99#define BOOTROM_LOCK_ENABLE 7
100
101#define BOOT_PARTITION_NONE (-1)
102#define BOOT_PARTITION_SLOT0 (-2)
103#define BOOT_PARTITION_SLOT1 (-3)
104#define BOOT_PARTITION_WINDOW (-4)
105
106#define BOOT_DIAGNOSTIC_WINDOW_SEARCHED 0x01
107// note if both BOOT_DIAGNOSTIC_INVALID_BLOCK_LOOP and BOOT_DIAGNOSTIC_VALID_BLOCK_LOOP then the block loop was valid
108// but it has a PARTITION_TABLE which while it passed the initial verification (and hash/sig) had invalid contents
109// (discovered when it was later loaded)
110#define BOOT_DIAGNOSTIC_INVALID_BLOCK_LOOP 0x02
111#define BOOT_DIAGNOSTIC_VALID_BLOCK_LOOP 0x04
112#define BOOT_DIAGNOSTIC_VALID_IMAGE_DEF 0x08
113#define BOOT_DIAGNOSTIC_HAS_PARTITION_TABLE 0x10
114#define BOOT_DIAGNOSTIC_CONSIDERED 0x20
115#define BOOT_DIAGNOSTIC_CHOSEN 0x40
116#define BOOT_DIAGNOSTIC_PARTITION_TABLE_LSB 7
117#define BOOT_DIAGNOSTIC_PARTITION_TABLE_MATCHING_KEY_FOR_VERIFY 0x80
118#define BOOT_DIAGNOSTIC_PARTITION_TABLE_HASH_FOR_VERIFY 0x100
119#define BOOT_DIAGNOSTIC_PARTITION_TABLE_VERIFIED_OK 0x200
120#define BOOT_DIAGNOSTIC_IMAGE_DEF_LSB 10
121#define BOOT_DIAGNOSTIC_IMAGE_DEF_MATCHING_KEY_FOR_VERIFY 0x400
122#define BOOT_DIAGNOSTIC_IMAGE_DEF_HASH_FOR_VERIFY 0x800
123#define BOOT_DIAGNOSTIC_IMAGE_DEF_VERIFIED_OK 0x1000
124
125#define BOOT_DIAGNOSTIC_LOAD_MAP_ENTRIES_LOADED 0x2000
126#define BOOT_DIAGNOSTIC_IMAGE_LAUNCHED 0x4000
127#define BOOT_DIAGNOSTIC_IMAGE_CONDITION_FAILURE 0x8000
128
129#define BOOT_PARSED_BLOCK_DIAGNOSTIC_MATCHING_KEY_FOR_VERIFY 0x1 // if this is present and VERIFIED_OK isn't the sig check failed
130#define BOOT_PARSED_BLOCK_DIAGNOSTIC_HASH_FOR_VERIFY 0x2 // if this is present and VERIFIED_OL isn't then hash check failed
131#define BOOT_PARSED_BLOCK_DIAGNOSTIC_VERIFIED_OK 0x4
132
133#define BOOT_TBYB_AND_UPDATE_FLAG_BUY_PENDING 0x1
134#define BOOT_TBYB_AND_UPDATE_FLAG_OTP_VERSION_APPLIED 0x2
135#define BOOT_TBYB_AND_UPDATE_FLAG_OTHER_ERASED 0x4
136
137#ifndef __ASSEMBLER__
138// Limited to 3 arguments in case of varm multiplex hint (trashes Arm r3)
139typedef int (*bootrom_api_callback_generic_t)(uint32_t r0, uint32_t r1, uint32_t r2);
140// Return negative for error, else number of bytes transferred:
141//typedef int (*bootrom_api_callback_stdout_put_blocking_t)(const uint8_t *buffer, uint32_t size);
142//typedef int (*bootrom_api_callback_stdin_get_t)(uint8_t *buffer, uint32_t size);
143//typedef void (*bootrom_api_callback_core1_security_setup_t)(void);
144#endif
145
146#endif
147
157#define ROM_TABLE_CODE(c1, c2) ((c1) | ((c2) << 8))
158
159// ROM FUNCTIONS
160
161// RP2040 & RP2350
162#define ROM_DATA_SOFTWARE_GIT_REVISION ROM_TABLE_CODE('G', 'R')
163#define ROM_FUNC_FLASH_ENTER_CMD_XIP ROM_TABLE_CODE('C', 'X')
164#define ROM_FUNC_FLASH_EXIT_XIP ROM_TABLE_CODE('E', 'X')
165#define ROM_FUNC_FLASH_FLUSH_CACHE ROM_TABLE_CODE('F', 'C')
166#define ROM_FUNC_CONNECT_INTERNAL_FLASH ROM_TABLE_CODE('I', 'F')
167#define ROM_FUNC_FLASH_RANGE_ERASE ROM_TABLE_CODE('R', 'E')
168#define ROM_FUNC_FLASH_RANGE_PROGRAM ROM_TABLE_CODE('R', 'P')
169
170
171#if PICO_RP2040
172// RP2040 only
173#define ROM_FUNC_MEMCPY44 ROM_TABLE_CODE('C', '4')
174#define ROM_DATA_COPYRIGHT ROM_TABLE_CODE('C', 'R')
175#define ROM_FUNC_CLZ32 ROM_TABLE_CODE('L', '3')
176#define ROM_FUNC_MEMCPY ROM_TABLE_CODE('M', 'C')
177#define ROM_FUNC_MEMSET ROM_TABLE_CODE('M', 'S')
178#define ROM_FUNC_POPCOUNT32 ROM_TABLE_CODE('P', '3')
179#define ROM_FUNC_REVERSE32 ROM_TABLE_CODE('R', '3')
180#define ROM_FUNC_MEMSET4 ROM_TABLE_CODE('S', '4')
181#define ROM_FUNC_CTZ32 ROM_TABLE_CODE('T', '3')
182#define ROM_FUNC_RESET_USB_BOOT ROM_TABLE_CODE('U', 'B')
183#endif
184
185#if !PICO_RP2040 || PICO_COMBINED_DOCS
186// RP2350 only
187#define ROM_FUNC_PICK_AB_PARTITION ROM_TABLE_CODE('A', 'B')
188#define ROM_FUNC_CHAIN_IMAGE ROM_TABLE_CODE('C', 'I')
189#define ROM_FUNC_EXPLICIT_BUY ROM_TABLE_CODE('E', 'B')
190#define ROM_FUNC_FLASH_RUNTIME_TO_STORAGE_ADDR ROM_TABLE_CODE('F', 'A')
191#define ROM_DATA_FLASH_DEVINFO16_PTR ROM_TABLE_CODE('F', 'D')
192#define ROM_FUNC_FLASH_OP ROM_TABLE_CODE('F', 'O')
193#define ROM_FUNC_GET_B_PARTITION ROM_TABLE_CODE('G', 'B')
194#define ROM_FUNC_GET_PARTITION_TABLE_INFO ROM_TABLE_CODE('G', 'P')
195#define ROM_FUNC_GET_SYS_INFO ROM_TABLE_CODE('G', 'S')
196#define ROM_FUNC_GET_UF2_TARGET_PARTITION ROM_TABLE_CODE('G', 'U')
197#define ROM_FUNC_LOAD_PARTITION_TABLE ROM_TABLE_CODE('L', 'P')
198#define ROM_FUNC_OTP_ACCESS ROM_TABLE_CODE('O', 'A')
199#define ROM_DATA_PARTITION_TABLE_PTR ROM_TABLE_CODE('P', 'T')
200#define ROM_FUNC_FLASH_RESET_ADDRESS_TRANS ROM_TABLE_CODE('R', 'A')
201#define ROM_FUNC_REBOOT ROM_TABLE_CODE('R', 'B')
202#define ROM_FUNC_SET_ROM_CALLBACK ROM_TABLE_CODE('R', 'C')
203#define ROM_FUNC_SECURE_CALL ROM_TABLE_CODE('S', 'C')
204#define ROM_FUNC_SET_NS_API_PERMISSION ROM_TABLE_CODE('S', 'P')
205#define ROM_FUNC_BOOTROM_STATE_RESET ROM_TABLE_CODE('S', 'R')
206#define ROM_FUNC_SET_BOOTROM_STACK ROM_TABLE_CODE('S', 'S')
207#define ROM_DATA_SAVED_XIP_SETUP_FUNC_PTR ROM_TABLE_CODE('X', 'F')
208#define ROM_FUNC_FLASH_SELECT_XIP_READ_MODE ROM_TABLE_CODE('X', 'M')
209#define ROM_FUNC_VALIDATE_NS_BUFFER ROM_TABLE_CODE('V', 'B')
210#endif
211
212// these form a bit set
213#define BOOTROM_STATE_RESET_CURRENT_CORE 0x01
214#define BOOTROM_STATE_RESET_OTHER_CORE 0x02
215#define BOOTROM_STATE_RESET_GLOBAL_STATE 0x04 // reset any global state (e.g. permissions)
216
217// partition level stuff is returned first (note PT_INFO flags is only 16 bits)
218
219// 3 words: pt_count, unpartitioned_perm_loc, unpartioned_perm_flags
220#define PT_INFO_PT_INFO 0x0001
221#define PT_INFO_SINGLE_PARTITION 0x8000 // marker to just include a single partition in the results)
222
223// then in order per partition selected
224
225// 2 words: unpartitioned_perm_loc, unpartioned_perm_flags
226#define PT_INFO_PARTITION_LOCATION_AND_FLAGS 0x0010
227// 2 words: id lsb first
228#define PT_INFO_PARTITION_ID 0x0020
229// n+1 words: n, family_id...
230#define PT_INFO_PARTITION_FAMILY_IDS 0x0040
231// (n+3)/4 words... bytes are: n (len), c0, c1, ... cn-1 padded to word boundary with zeroes
232#define PT_INFO_PARTITION_NAME 0x0080
233
234// items are returned in order
235// 3 words package_id, device_id, wafer_id
236#define SYS_INFO_CHIP_INFO 0x0001
237// 1 word: chip specific critical bits
238#define SYS_INFO_CRITICAL 0x0002
239// 1 word: bytes: cpu_type, supported_cpu_type_bitfield
240#define SYS_INFO_CPU_INFO 0x0004
241// 1 word: same as FLASH_DEVINFO row in OTP
242#define SYS_INFO_FLASH_DEV_INFO 0x0008
243// 4 words
244#define SYS_INFO_BOOT_RANDOM 0x0010
245// 2 words lsb first
246#define SYS_INFO_NONCE 0x0020
247// 4 words boot_info, boot_diagnostic, boot_param0, boot_param1
248#define SYS_INFO_BOOT_INFO 0x0040
249
250#define BOOTROM_NS_API_get_sys_info 0
251#define BOOTROM_NS_API_checked_flash_op 1
252#define BOOTROM_NS_API_flash_runtime_to_storage_addr 2
253#define BOOTROM_NS_API_get_partition_table_info 3
254#define BOOTROM_NS_API_secure_call 4
255#define BOOTROM_NS_API_otp_access 5
256#define BOOTROM_NS_API_reboot 6
257#define BOOTROM_NS_API_get_b_partition 7
258#define BOOTROM_NS_API_COUNT 8
259
260#ifndef __ASSEMBLER__
261
262typedef struct {
263 uint32_t permissions_and_location;
264 uint32_t permissions_and_flags;
266static_assert(sizeof(resident_partition_t) == 8, "");
267
268#define OTP_CMD_ROW_BITS 0x0000ffffu
269#define OTP_CMD_ROW_LSB 0u
270#define OTP_CMD_WRITE_BITS 0x00010000u
271#define OTP_CMD_ECC_BITS 0x00020000u
272
273typedef struct otp_cmd {
274 uint32_t flags;
275} otp_cmd_t;
276
277typedef enum {
278 BOOTROM_XIP_MODE_03H_SERIAL = 0,
279 BOOTROM_XIP_MODE_0BH_SERIAL,
280 BOOTROM_XIP_MODE_BBH_DUAL,
281 BOOTROM_XIP_MODE_EBH_QUAD,
282 BOOTROM_XIP_MODE_N_MODES
283} bootrom_xip_mode_t;
284
285// The checked flash API wraps the low-level flash routines from generic_flash, adding bounds
286// checking, permission checking against the resident partition table, and simple address
287// translation. The low-level API deals with flash offsets (i.e. distance from the start of the
288// first flash device, measured in bytes) but the checked flash API accepts one of two types of
289// address:
290//
291// - Flash runtime addresses: the address of some flash-resident data or code in the currently
292// running image. The flash addresses your binary is "linked at" by the linker.
293// - Flash storage addresses: a flash offset, plus the address base where QSPI hardware is first
294// mapped on the system bus (XIP_BASE constant from addressmap.h)
295//
296// These addresses are one and the same *if* the currently running program is stored at the
297// beginning of flash. They are different if the start of your image has been "rolled" by the flash
298// boot path to make it appear at the address it was linked at even though it is stored at a
299// different location in flash, which is necessary when you have A/B images for example.
300//
301// The address translation between flash runtime and flash storage addresses is configured in
302// hardware by the QMI_ATRANSx registers, and this API assumes those registers contain a valid
303// address mapping which it can use to translate runtime to storage addresses.
304
305typedef struct cflash_flags {
306 uint32_t flags;
308
309// Bits which are permitted to be set in a flags variable -- any other bits being set is an error
310#define CFLASH_FLAGS_BITS 0x00070301u
311
312// Used to tell checked flash API which space a given address belongs to
313#define CFLASH_ASPACE_BITS 0x00000001u
314#define CFLASH_ASPACE_LSB 0u
315#define CFLASH_ASPACE_VALUE_STORAGE 0u
316#define CFLASH_ASPACE_VALUE_RUNTIME 1u
317
318// Used to tell checked flash APIs the effective security level of a flash access (may be forced to
319// one of these values for the NonSecure-exported version of this API)
320#define CFLASH_SECLEVEL_BITS 0x00000300u
321#define CFLASH_SECLEVEL_LSB 8u
322// Zero is not a valid security level:
323#define CFLASH_SECLEVEL_VALUE_SECURE 1u
324#define CFLASH_SECLEVEL_VALUE_NONSECURE 2u
325#define CFLASH_SECLEVEL_VALUE_BOOTLOADER 3u
326
327#define CFLASH_OP_BITS 0x00070000u
328#define CFLASH_OP_LSB 16u
329// Erase size_bytes bytes of flash, starting at address addr. Both addr and size_bytes must be a
330// multiple of 4096 bytes (one flash sector).
331#define CFLASH_OP_VALUE_ERASE 0u
332// Program size_bytes bytes of flash, starting at address addr. Both addr and size_bytes must be a
333// multiple of 256 bytes (one flash page).
334#define CFLASH_OP_VALUE_PROGRAM 1u
335// Read size_bytes bytes of flash, starting at address addr. There are no alignment restrictions on
336// addr or size_bytes.
337#define CFLASH_OP_VALUE_READ 2u
338#define CFLASH_OP_MAX 2u
339
340#endif
341
342#endif
Definition bootrom_constants.h:305
Definition bootrom_constants.h:273
Definition bootrom_constants.h:262