1pub const MAGIC: u64 = 0x544F_4F42_4D56_3953;
39pub const VERSION: u32 = 1;
41pub const SIZE: usize = 512;
43pub const MAX_MEMORY_ENTRIES: usize = 16;
45pub const MAX_DEVICE_ENTRIES: usize = 4;
47
48pub mod memory_kind {
50 pub const RAM: u32 = 1;
52 pub const RESERVED: u32 = 2;
54 pub const MMIO: u32 = 3;
56}
57
58pub mod device_kind {
60 pub const CONSOLE: u32 = 1;
62 pub const BLOCK: u32 = 2;
64 pub const NETWORK: u32 = 3;
66}
67
68pub mod flags {
70 pub const COMPLETE: u32 = 1;
72 pub const HAS_CONSOLE: u32 = 2;
74 pub const HAS_BLOCK: u32 = 4;
76 pub const HAS_NETWORK: u32 = 8;
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub struct MemoryEntry {
83 pub base: u64,
85 pub len: u64,
87 pub kind: u32,
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub struct DeviceEntry {
94 pub kind: u32,
96 pub irq_line: u32,
98 pub mmio_base: u64,
100 pub config: u64,
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum BootInfoError {
107 BadMagic,
109 BadVersion,
111 BadSize,
113 TooManyEntries,
115 Truncated,
117}
118
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
121pub struct BootInfo {
122 pub flags: u32,
124 pub memory: [MemoryEntry; MAX_MEMORY_ENTRIES],
126 pub memory_count: u8,
128 pub devices: [DeviceEntry; MAX_DEVICE_ENTRIES],
130 pub device_count: u8,
132}
133
134impl BootInfo {
135 pub const fn new() -> Self {
137 Self {
138 flags: 0,
139 memory: [MemoryEntry {
140 base: 0,
141 len: 0,
142 kind: 0,
143 }; MAX_MEMORY_ENTRIES],
144 memory_count: 0,
145 devices: [DeviceEntry {
146 kind: 0,
147 irq_line: 0,
148 mmio_base: 0,
149 config: 0,
150 }; MAX_DEVICE_ENTRIES],
151 device_count: 0,
152 }
153 }
154
155 pub fn add_memory(&mut self, base: u64, len: u64, kind: u32) -> bool {
157 let index = self.memory_count as usize;
158 if index >= MAX_MEMORY_ENTRIES {
159 return false;
160 }
161 self.memory[index] = MemoryEntry { base, len, kind };
162 self.memory_count += 1;
163 true
164 }
165
166 pub fn add_device(&mut self, kind: u32, irq_line: u32, mmio_base: u64, config: u64) -> bool {
168 let index = self.device_count as usize;
169 if index >= MAX_DEVICE_ENTRIES {
170 return false;
171 }
172 self.devices[index] = DeviceEntry {
173 kind,
174 irq_line,
175 mmio_base,
176 config,
177 };
178 self.device_count += 1;
179 true
180 }
181
182 pub fn encode(&self, out: &mut [u8; SIZE]) {
184 out.fill(0);
185 put_u64(out, 0x000, MAGIC);
186 put_u32(out, 0x008, VERSION);
187 put_u32(out, 0x00C, SIZE as u32);
188 put_u32(out, 0x010, self.flags);
189 put_u32(out, 0x014, u32::from(self.memory_count));
190 put_u32(out, 0x018, u32::from(self.device_count));
191 let memory_base = 0x020;
192 for (index, entry) in self.memory[..usize::from(self.memory_count)]
193 .iter()
194 .enumerate()
195 {
196 let offset = memory_base + index * 24;
197 put_u64(out, offset, entry.base);
198 put_u64(out, offset + 8, entry.len);
199 put_u32(out, offset + 16, entry.kind);
200 }
201 let device_base = memory_base + MAX_MEMORY_ENTRIES * 24;
202 for (index, entry) in self.devices[..usize::from(self.device_count)]
203 .iter()
204 .enumerate()
205 {
206 let offset = device_base + index * 24;
207 put_u32(out, offset, entry.kind);
208 put_u32(out, offset + 4, entry.irq_line);
209 put_u64(out, offset + 8, entry.mmio_base);
210 put_u64(out, offset + 16, entry.config);
211 }
212 }
213
214 pub fn decode(bytes: &[u8]) -> Result<Self, BootInfoError> {
216 if bytes.len() < SIZE {
217 return Err(BootInfoError::Truncated);
218 }
219 if get_u64(bytes, 0x000) != MAGIC {
220 return Err(BootInfoError::BadMagic);
221 }
222 if get_u32(bytes, 0x008) != VERSION {
223 return Err(BootInfoError::BadVersion);
224 }
225 if get_u32(bytes, 0x00C) as usize != SIZE {
226 return Err(BootInfoError::BadSize);
227 }
228 let mut info = Self::new();
229 info.flags = get_u32(bytes, 0x010);
230 let memory_count = get_u32(bytes, 0x014) as usize;
231 let device_count = get_u32(bytes, 0x018) as usize;
232 if memory_count > MAX_MEMORY_ENTRIES || device_count > MAX_DEVICE_ENTRIES {
233 return Err(BootInfoError::TooManyEntries);
234 }
235 let memory_base = 0x020;
236 for index in 0..memory_count {
237 let offset = memory_base + index * 24;
238 info.memory[index] = MemoryEntry {
239 base: get_u64(bytes, offset),
240 len: get_u64(bytes, offset + 8),
241 kind: get_u32(bytes, offset + 16),
242 };
243 }
244 info.memory_count = memory_count as u8;
245 let device_base = memory_base + MAX_MEMORY_ENTRIES * 24;
246 for index in 0..device_count {
247 let offset = device_base + index * 24;
248 info.devices[index] = DeviceEntry {
249 kind: get_u32(bytes, offset),
250 irq_line: get_u32(bytes, offset + 4),
251 mmio_base: get_u64(bytes, offset + 8),
252 config: get_u64(bytes, offset + 16),
253 };
254 }
255 info.device_count = device_count as u8;
256 Ok(info)
257 }
258}
259
260impl Default for BootInfo {
261 fn default() -> Self {
262 Self::new()
263 }
264}
265
266fn put_u32(out: &mut [u8; SIZE], offset: usize, value: u32) {
268 out[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
269}
270
271fn put_u64(out: &mut [u8; SIZE], offset: usize, value: u64) {
273 out[offset..offset + 8].copy_from_slice(&value.to_le_bytes());
274}
275
276fn get_u32(bytes: &[u8], offset: usize) -> u32 {
278 u32::from_le_bytes([
279 bytes[offset],
280 bytes[offset + 1],
281 bytes[offset + 2],
282 bytes[offset + 3],
283 ])
284}
285
286fn get_u64(bytes: &[u8], offset: usize) -> u64 {
288 u64::from_le_bytes([
289 bytes[offset],
290 bytes[offset + 1],
291 bytes[offset + 2],
292 bytes[offset + 3],
293 bytes[offset + 4],
294 bytes[offset + 5],
295 bytes[offset + 6],
296 bytes[offset + 7],
297 ])
298}