v2.0.0
picobin.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 _BOOT_PICOBIN_H
8#define _BOOT_PICOBIN_H
9
10#ifndef NO_PICO_PLATFORM
11#include "pico/platform.h"
12#else
13#ifndef _u
14#define _u(x) x ## u
15#endif
16#endif
17
24// these are designed to not look like (likely) 16/32-bit ARM or RISC-V instructions or look like valid pointers
25#define PICOBIN_BLOCK_MARKER_START _u(0xffffded3)
26#define PICOBIN_BLOCK_MARKER_END _u(0xab123579)
27
28#define PICOBIN_MAX_BLOCK_SIZE _u(0x280)
29#define PICOBIN_MAX_IMAGE_DEF_BLOCK_SIZE _u(0x180)
30#define PICOBIN_MAX_PARTITION_TABLE_BLOCK_SIZE _u(0x280)
31
32// note bit 6 is used to make parity even
33#define PICOBIN_BLOCK_ITEM_1BS_NEXT_BLOCK_OFFSET _u(0x41)
34#define PICOBIN_BLOCK_ITEM_1BS_IMAGE_TYPE _u(0x42)
35#define PICOBIN_BLOCK_ITEM_1BS_VECTOR_TABLE _u(0x03)
36#define PICOBIN_BLOCK_ITEM_1BS_ENTRY_POINT _u(0x44)
37#define PICOBIN_BLOCK_ITEM_1BS_ROLLING_WINDOW_DELTA _u(0x05)
38#define PICOBIN_BLOCK_ITEM_LOAD_MAP _u(0x06)
39#define PICOBIN_BLOCK_ITEM_1BS_HASH_DEF _u(0x47)
40#define PICOBIN_BLOCK_ITEM_1BS_VERSION _u(0x48)
41#define PICOBIN_BLOCK_ITEM_SIGNATURE _u(0x09)
42#define PICOBIN_BLOCK_ITEM_PARTITION_TABLE _u(0x0a)
43#define PICOBIN_BLOCK_ITEM_HASH_VALUE _u(0x4b)
44#define PICOBIN_BLOCK_ITEM_SALT _u(0x0c)
45
46#define PICOBIN_BLOCK_ITEM_2BS_IGNORED (_u(0x80) | _u(0x7e))
47#define PICOBIN_BLOCK_ITEM_2BS_LAST (_u(0x80) | _u(0x7f))
48
49// ----
50
51#define PICOBIN_INDEX_TO_BITS(y, x) (y ## _ ## x << y ## _LSB)
52
53#define PICOBIN_IMAGE_TYPE_IMAGE_TYPE_LSB _u(0)
54#define PICOBIN_IMAGE_TYPE_IMAGE_TYPE_BITS _u(0x000f)
55#define PICOBIN_IMAGE_TYPE_IMAGE_TYPE_INVALID _u(0x0)
56#define PICOBIN_IMAGE_TYPE_IMAGE_TYPE_EXE _u(0x1)
57#define PICOBIN_IMAGE_TYPE_IMAGE_TYPE_DATA _u(0x2)
58#define PICOBIN_IMAGE_TYPE_IMAGE_TYPE_AS_BITS(x) PICOBIN_INDEX_TO_BITS(PICOBIN_IMAGE_TYPE_IMAGE_TYPE, x)
59
60#define PICOBIN_IMAGE_TYPE_EXE_SECURITY_LSB _u(4)
61#define PICOBIN_IMAGE_TYPE_EXE_SECURITY_BITS _u(0x0030)
62#define PICOBIN_IMAGE_TYPE_EXE_SECURITY_UNSPECIFIED _u(0x0)
63#define PICOBIN_IMAGE_TYPE_EXE_SECURITY_NS _u(0x1)
64#define PICOBIN_IMAGE_TYPE_EXE_SECURITY_S _u(0x2)
65#define PICOBIN_IMAGE_TYPE_EXE_SECURITY_AS_BITS(x) PICOBIN_INDEX_TO_BITS(PICOBIN_IMAGE_TYPE_EXE_SECURITY, x)
66
67#define PICOBIN_IMAGE_TYPE_EXE_CPU_LSB _u(8)
68#define PICOBIN_IMAGE_TYPE_EXE_CPU_BITS _u(0x0700)
69#define PICOBIN_IMAGE_TYPE_EXE_CPU_ARM _u(0)
70#define PICOBIN_IMAGE_TYPE_EXE_CPU_RISCV _u(1)
71#define PICOBIN_IMAGE_TYPE_EXE_CPU_VARMULET _u(2)
72#define PICOBIN_IMAGE_TYPE_EXE_CPU_AS_BITS(x) PICOBIN_INDEX_TO_BITS(PICOBIN_IMAGE_TYPE_EXE_CPU, x)
73
74#define PICOBIN_IMAGE_TYPE_EXE_CHIP_LSB _u(12)
75#define PICOBIN_IMAGE_TYPE_EXE_CHIP_BITS _u(0x7000)
76#define PICOBIN_IMAGE_TYPE_EXE_CHIP_RP2040 _u(0)
77#define PICOBIN_IMAGE_TYPE_EXE_CHIP_RP2350 _u(1)
78#define PICOBIN_IMAGE_TYPE_EXE_CHIP_AS_BITS(x) PICOBIN_INDEX_TO_BITS(PICOBIN_IMAGE_TYPE_EXE_CHIP, x)
79
80#define PICOBIN_IMAGE_TYPE_EXE_TBYB_BITS _u(0x8000)
81
82// todo assert no overlap ^
83
84#define PICOBIN_PARTITION_PERMISSIONS_LSB _u(26)
85#define PICOBIN_PARTITION_PERMISSIONS_BITS _u(0xfc000000)
86
87#define PICOBIN_PARTITION_PERMISSION_S_R_BITS _u(0x04000000)
88#define PICOBIN_PARTITION_PERMISSION_S_W_BITS _u(0x08000000)
89#define PICOBIN_PARTITION_PERMISSION_NS_R_BITS _u(0x10000000)
90#define PICOBIN_PARTITION_PERMISSION_NS_W_BITS _u(0x20000000)
91#define PICOBIN_PARTITION_PERMISSION_NSBOOT_R_BITS _u(0x40000000)
92#define PICOBIN_PARTITION_PERMISSION_NSBOOT_W_BITS _u(0x80000000)
93
94#define PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_LSB _u(0)
95#define PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_BITS _u(0x00001fff)
96#define PICOBIN_PARTITION_LOCATION_LAST_SECTOR_LSB _u(13)
97#define PICOBIN_PARTITION_LOCATION_LAST_SECTOR_BITS _u(0x03ffe000)
98
99#define PICOBIN_PARTITION_FLAGS_HAS_ID_BITS _u(0x00000001)
100#define PICOBIN_PARTITION_FLAGS_LINK_TYPE_LSB _u(1)
101#define PICOBIN_PARTITION_FLAGS_LINK_TYPE_BITS _u(0x00000006)
102#define PICOBIN_PARTITION_FLAGS_LINK_VALUE_LSB _u(3)
103#define PICOBIN_PARTITION_FLAGS_LINK_VALUE_BITS _u(0x00000078)
104
105#define PICOBIN_PARTITION_MAX_EXTRA_FAMILIES _u(3)
106#define PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_LSB _u(7)
107#define PICOBIN_PARTITION_FLAGS_ACCEPTS_NUM_EXTRA_FAMILIES_BITS _u(0x00000180)
108// these are an optimization when booting in either ARM or RISC-V, to avoid looking at partitions
109// which are known not to contain the right sort of binary, OR as a way to prevent
110// auto-architecture-switch. NOTE: the first partition that can be booted, will be,
111// so if you have a RISC-V binary in the first partition, and auto-arhcitecture-switch enabled, then
112// even if booting under ARM, with an ARM binary in a later partition, the RISC-V binary
113// will be booted by default; setting PICOBIN_PARTITION_FLAGS_IGNORED_DURING_ARM_BOOT_BITS
114// on the partition, will have the RISC-V binary containing partition ignored under ARM
115// boot
116#define PICOBIN_PARTITION_FLAGS_IGNORED_DURING_ARM_BOOT_BITS _u(0x00000200)
117#define PICOBIN_PARTITION_FLAGS_IGNORED_DURING_RISCV_BOOT_BITS _u(0x00000400)
118#define PICOBIN_PARTITION_FLAGS_UF2_DOWNLOAD_AB_NON_BOOTABLE_OWNER_AFFINITY _u(0x00000800)
119#define PICOBIN_PARTITION_FLAGS_HAS_NAME_BITS _u(0x00001000)
120#define PICOBIN_PARTITION_FLAGS_UF2_DOWNLOAD_NO_REBOOT_BITS _u(0x00002000)
121// we have a bit for each well known family-id .. note we expect there to be more in the future with new chips,
122// but we have plenty of space for now.
123#define PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILIES_LSB _u(14)
124#define PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_RP2040_BITS _u(0x00004000)
125#define PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_ABSOLUTE_BITS _u(0x00008000)
126#define PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_DATA_BITS _u(0x00010000)
127#define PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_RP2350_ARM_S_BITS _u(0x00020000)
128#define PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_RP2350_RISCV_BITS _u(0x00040000)
129#define PICOBIN_PARTITION_FLAGS_ACCEPTS_DEFAULT_FAMILY_RP2350_ARM_NS_BITS _u(0x00080000)
130
131#define PICOBIN_PARTITION_FLAGS_LINK_TYPE_NONE _u(0)
132#define PICOBIN_PARTITION_FLAGS_LINK_TYPE_A_PARTITION _u(1)
133#define PICOBIN_PARTITION_FLAGS_LINK_TYPE_OWNER_PARTITION _u(2)
134#define PICOBIN_PARTITION_FLAGS_LINK_TYPE_AS_BITS(x) PICOBIN_INDEX_TO_BITS(PICOBIN_PARTITION_FLAGS_LINK_TYPE, x)
135
136
137#define PICOBIN_HASH_SHA256 _u(0x01)
138
139#define PICOBIN_SIGNATURE_SECP256K1 _u(0x01)
140
141#ifndef __ASSEMBLER__
142
143#include <stdbool.h>
144
145 typedef struct {
146 // these must all be word aligned
147 uint32_t storage_address_rel;
148 uint32_t runtime_address;
149 uint32_t size;
150} picobin_load_map_entry;
151
152 typedef struct {
153 uint32_t header;
154 picobin_load_map_entry entries[];
155} picobin_load_map;
156
157static inline unsigned int picobin_load_map_entry_count(const picobin_load_map *lm) {
158 return (lm->header << 1) >> 25;
159}
160
161static inline bool picobin_load_map_is_relative(const picobin_load_map *lm) {
162 return (int32_t)lm->header >= 0;
163}
164
165#endif
166
167#endif
platform.h
picobin_load_map_entry
Definition: picobin.h:145
picobin_load_map
Definition: picobin.h:152