nros_platform_api/boot_config.rs
1// ============================================================================
2// BakedBootConfig — RFC-0045 "Single embedded bake site"
3//
4// Lives in nros-platform-api (no deps, no_std) so that nros-platform's
5// DeployOverlay can hold a `&'static BakedBootConfig` without creating a
6// dependency cycle back through nros-node.
7// ============================================================================
8
9/// 0x4E524243 = ASCII "NRBC". A post-link tool scans for this magic to locate
10/// the struct in a firmware image.
11pub const NROS_BOOT_CONFIG_MAGIC: u32 = 0x4E52_4243;
12
13/// Layout version — lets the resolver reject a mismatched baked struct.
14///
15/// **2** since `rmw` (bit 4) was appended. The bump is what
16/// the field is for: a reader compiled against v1 rejects a v2 struct rather
17/// than reading 32 bytes of `rmw` as whatever followed `namespace` in its own
18/// layout.
19pub const NROS_BOOT_CONFIG_VERSION: u16 = 2;
20
21// `set_flags` bit assignments in `BakedBootConfig`.
22/// Bit 0 — `node_name` field is set.
23pub const BOOT_SET_NODE_NAME: u16 = 1 << 0;
24/// Bit 1 — `locator` field is set.
25pub const BOOT_SET_LOCATOR: u16 = 1 << 1;
26/// Bit 2 — `domain_id` field is set.
27pub const BOOT_SET_DOMAIN: u16 = 1 << 2;
28/// Bit 3 — `namespace` field is set.
29pub const BOOT_SET_NAMESPACE: u16 = 1 << 3;
30/// Bit 4 — `rmw` field is set (layout version 2).
31pub const BOOT_SET_RMW: u16 = 1 << 4;
32
33/// Build-time-baked boot config, emitted into the `.nros_boot_config`
34/// linker section by the entry macro / cmake. Fixed-size + pointer-free so a
35/// future post-link tool can patch it in place. The resolver reads
36/// it on embedded via `BootConfig::from_baked` (in `nros-node`).
37#[repr(C)]
38#[derive(Debug, Clone, Copy)]
39pub struct BakedBootConfig {
40 /// 0x4E524243 = b"NRBC". A post-link tool scans for this to locate the struct.
41 pub magic: u32,
42 /// Layout version (start at 1) — lets the tool/reader reject mismatched layouts.
43 pub version: u16,
44 /// One bit per field that is baked-set (else the reader yields `None` → resolver
45 /// default). bit0 = node_name, bit1 = locator, bit2 = domain_id, bit3 = namespace,
46 /// bit4 = rmw.
47 pub set_flags: u16,
48 /// ROS 2 domain ID (valid only when `BOOT_SET_DOMAIN` bit is set).
49 pub domain_id: u32,
50 /// NUL-padded UTF-8; the trailing NUL bytes are not part of the value.
51 pub node_name: [u8; 64],
52 /// NUL-padded UTF-8 middleware locator; the trailing NUL bytes are not part of
53 /// the value.
54 pub locator: [u8; 96],
55 /// NUL-padded UTF-8 node namespace; the trailing NUL bytes are not part of the
56 /// value.
57 pub namespace: [u8; 64],
58 /// NUL-padded UTF-8 RMW backend selector (the name
59 /// the registry is looked up by, e.g. `"uorb"`). Appended in layout
60 /// version 2.
61 ///
62 /// 32 bytes because that is the registry's own `BACKEND_NAME_MAX`; a longer
63 /// name could never resolve, so accepting one here would only move the
64 /// failure later.
65 pub rmw: [u8; 32],
66}
67
68/// Copy `s` bytes into a zero-padded `[u8; N]` array at compile time.
69///
70/// A string longer than `N` bytes is a **compile-time** error (const panic).
71/// Never silently truncated.
72const fn pack<const N: usize>(s: &str) -> [u8; N] {
73 let bytes = s.as_bytes();
74 if bytes.len() > N {
75 panic!("BakedBootConfig: string field exceeds its fixed-size buffer");
76 }
77 let mut buf = [0u8; N];
78 let mut i = 0;
79 while i < bytes.len() {
80 buf[i] = bytes[i];
81 i += 1;
82 }
83 buf
84}
85
86impl BakedBootConfig {
87 /// Pack baked fields at compile time. `None` → field unset (bit clear,
88 /// bytes zeroed). A string longer than its fixed buffer is a
89 /// **compile-time** error (const panic) — never silently truncated.
90 ///
91 /// `must be const fn` so W4b's entry macro can use it in a `static` initializer.
92 pub const fn new(
93 node_name: Option<&str>,
94 locator: Option<&str>,
95 domain_id: Option<u32>,
96 namespace: Option<&str>,
97 ) -> BakedBootConfig {
98 Self::new_with_rmw(node_name, locator, domain_id, namespace, None)
99 }
100
101 /// [`new`](Self::new) plus the layout-version-2 `rmw` selector.
102 ///
103 /// Kept as a separate constructor rather than a fifth parameter on `new`
104 /// because `new` is called from entry macros in board crates and from
105 /// generated code; a signature change there is a break for every one of
106 /// them, and this field is `None` at all but a handful of call sites.
107 pub const fn new_with_rmw(
108 node_name: Option<&str>,
109 locator: Option<&str>,
110 domain_id: Option<u32>,
111 namespace: Option<&str>,
112 rmw: Option<&str>,
113 ) -> BakedBootConfig {
114 let mut flags: u16 = 0;
115
116 let node_name_bytes: [u8; 64] = match node_name {
117 Some(s) => {
118 flags |= BOOT_SET_NODE_NAME;
119 pack::<64>(s)
120 }
121 None => [0u8; 64],
122 };
123
124 let locator_bytes: [u8; 96] = match locator {
125 Some(s) => {
126 flags |= BOOT_SET_LOCATOR;
127 pack::<96>(s)
128 }
129 None => [0u8; 96],
130 };
131
132 let domain_id_val: u32 = match domain_id {
133 Some(d) => {
134 flags |= BOOT_SET_DOMAIN;
135 d
136 }
137 None => 0,
138 };
139
140 let namespace_bytes: [u8; 64] = match namespace {
141 Some(s) => {
142 flags |= BOOT_SET_NAMESPACE;
143 pack::<64>(s)
144 }
145 None => [0u8; 64],
146 };
147
148 let rmw_bytes: [u8; 32] = match rmw {
149 Some(s) => {
150 flags |= BOOT_SET_RMW;
151 pack::<32>(s)
152 }
153 None => [0u8; 32],
154 };
155
156 BakedBootConfig {
157 magic: NROS_BOOT_CONFIG_MAGIC,
158 version: NROS_BOOT_CONFIG_VERSION,
159 set_flags: flags,
160 domain_id: domain_id_val,
161 node_name: node_name_bytes,
162 locator: locator_bytes,
163 namespace: namespace_bytes,
164 rmw: rmw_bytes,
165 }
166 }
167}
168
169// ============================================================================
170// BakedBootConfig unit tests (no_std-compatible, run under std test runner)
171//
172// These tests verify BakedBootConfig::new / pack directly (struct-field
173// inspection only — no BootConfig::from_baked, which lives in nros-node).
174// Round-trip tests (new → from_baked) live in nros-node/src/executor/types.rs.
175// ============================================================================
176
177#[cfg(test)]
178mod baked_boot_config_tests {
179 use super::*;
180
181 // ── T-BB8: set_flags bit-pattern is exact ─────────────────────────────────
182
183 /// Verify the set_flags bitmask matches the expected bit positions.
184 #[test]
185 fn set_flags_bits_correct() {
186 let baked = BakedBootConfig::new(
187 Some("n"), // bit 0
188 None,
189 Some(0), // bit 2
190 Some(""), // bit 3
191 );
192 assert_eq!(
193 baked.set_flags,
194 BOOT_SET_NODE_NAME | BOOT_SET_DOMAIN | BOOT_SET_NAMESPACE
195 );
196 }
197
198 // ── T-BB-MAGIC: magic and version are populated ───────────────────────────
199
200 /// `BakedBootConfig::new` must embed the correct magic word and version.
201 #[test]
202 fn magic_and_version_populated() {
203 let baked = BakedBootConfig::new(None, None, None, None);
204 assert_eq!(baked.magic, NROS_BOOT_CONFIG_MAGIC);
205 assert_eq!(baked.version, NROS_BOOT_CONFIG_VERSION);
206 }
207
208 // ── T-BB-PACK: short string is NUL-padded ────────────────────────────────
209
210 /// A node_name shorter than 64 bytes must be stored in the leading bytes,
211 /// with the remaining bytes zeroed (NUL-padded).
212 #[test]
213 fn short_name_is_nul_padded() {
214 let name = "robot";
215 let baked = BakedBootConfig::new(Some(name), None, None, None);
216 assert_eq!(&baked.node_name[..name.len()], name.as_bytes());
217 assert!(baked.node_name[name.len()..].iter().all(|&b| b == 0));
218 assert_eq!(baked.set_flags & BOOT_SET_NODE_NAME, BOOT_SET_NODE_NAME);
219 }
220
221 // ── T-BB-NONE: all-None zeroes bytes and clears flags ────────────────────
222
223 /// When every argument is None the set_flags must be zero, domain_id zero,
224 /// and all byte arrays zeroed.
225 #[test]
226 fn all_none_zeroes_fields() {
227 let baked = BakedBootConfig::new(None, None, None, None);
228 assert_eq!(baked.set_flags, 0);
229 assert_eq!(baked.domain_id, 0);
230 assert!(baked.node_name.iter().all(|&b| b == 0));
231 assert!(baked.locator.iter().all(|&b| b == 0));
232 assert!(baked.namespace.iter().all(|&b| b == 0));
233 }
234
235 // ── Compile-failure comment ───────────────────────────────────────────────
236 // Uncommenting the line below must FAIL to compile because the string
237 // exceeds the 64-byte node_name buffer. Do NOT uncomment in CI.
238 //
239 // const _: BakedBootConfig = BakedBootConfig::new(
240 // Some("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), // 65 A's (> 64-byte buffer)
241 // None, None, None,
242 // );
243
244 // ── T-LAYOUT: layout-drift guard (mirrors C header static_asserts) ────────
245 //
246 // If `BakedBootConfig` ever changes size or field offsets, this test fails
247 // and forces the C header `include/rclc/detail/boot_config.h` to be updated in
248 // lockstep. The C header carries matching _Static_assert / static_assert
249 // guards on the C/C++ side.
250
251 /// Size and field offsets of `BakedBootConfig` must match the C header's
252 /// documented layout (total 268 bytes, no padding).
253 ///
254 /// 236 -> 268 with layout version 2 (issue 1050 defect (3), `rmw @ 236`).
255 /// This test is the reason that append could not be a one-sided edit: the
256 /// blob has a C mirror (`nros-c/include/rclc/detail/boot_config.h`) whose own
257 /// `static_assert`s carry the same numbers, so a change here fails until
258 /// both sides move.
259 #[test]
260 fn baked_boot_config_layout() {
261 use core::mem::{offset_of, size_of};
262 assert_eq!(size_of::<BakedBootConfig>(), 268, "total size must be 268");
263 assert_eq!(offset_of!(BakedBootConfig, magic), 0, "magic @ 0");
264 assert_eq!(offset_of!(BakedBootConfig, version), 4, "version @ 4");
265 assert_eq!(offset_of!(BakedBootConfig, set_flags), 6, "set_flags @ 6");
266 assert_eq!(offset_of!(BakedBootConfig, domain_id), 8, "domain_id @ 8");
267 assert_eq!(offset_of!(BakedBootConfig, node_name), 12, "node_name @ 12");
268 assert_eq!(offset_of!(BakedBootConfig, locator), 76, "locator @ 76");
269 assert_eq!(
270 offset_of!(BakedBootConfig, namespace),
271 172,
272 "namespace @ 172"
273 );
274 assert_eq!(offset_of!(BakedBootConfig, rmw), 236, "rmw @ 236");
275 }
276}