blob: d014be921e96f6761577e4c0073c9027a4828fd8 [file] [log] [blame]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +00001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! High-level FDT functions.
16
Jiyong Parkb87f3302023-03-21 10:03:11 +090017use crate::cstr;
Jiyong Park9c63cd12023-03-21 17:53:07 +090018use crate::helpers::flatten;
Jiyong Park00ceff32023-03-13 05:43:23 +000019use crate::helpers::GUEST_PAGE_SIZE;
Jiyong Park9c63cd12023-03-21 17:53:07 +090020use crate::helpers::SIZE_4KB;
Jiyong Park00ceff32023-03-13 05:43:23 +000021use crate::RebootReason;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000022use core::ffi::CStr;
Jiyong Park9c63cd12023-03-21 17:53:07 +090023use core::mem::size_of;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000024use core::ops::Range;
Jiyong Park00ceff32023-03-13 05:43:23 +000025use fdtpci::PciMemoryFlags;
26use fdtpci::PciRangeType;
27use libfdt::AddressRange;
28use libfdt::CellIterator;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +000029use libfdt::Fdt;
30use libfdt::FdtError;
Jiyong Park9c63cd12023-03-21 17:53:07 +090031use libfdt::FdtNode;
Jiyong Park83316122023-03-21 09:39:39 +090032use log::debug;
Jiyong Park00ceff32023-03-13 05:43:23 +000033use log::error;
34use tinyvec::ArrayVec;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000035
Jiyong Park6a8789a2023-03-21 14:50:59 +090036/// Extract from /config the address range containing the pre-loaded kernel. Absence of /config is
37/// not an error.
38fn read_kernel_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
Jiyong Parkb87f3302023-03-21 10:03:11 +090039 let addr = cstr!("kernel-address");
40 let size = cstr!("kernel-size");
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +000041
Jiyong Parkb87f3302023-03-21 10:03:11 +090042 if let Some(config) = fdt.node(cstr!("/config"))? {
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +000043 if let (Some(addr), Some(size)) = (config.getprop_u32(addr)?, config.getprop_u32(size)?) {
44 let addr = addr as usize;
45 let size = size as usize;
46
47 return Ok(Some(addr..(addr + size)));
48 }
49 }
50
51 Ok(None)
52}
53
Jiyong Park6a8789a2023-03-21 14:50:59 +090054/// Extract from /chosen the address range containing the pre-loaded ramdisk. Absence is not an
55/// error as there can be initrd-less VM.
56fn read_initrd_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
Jiyong Parkb87f3302023-03-21 10:03:11 +090057 let start = cstr!("linux,initrd-start");
58 let end = cstr!("linux,initrd-end");
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000059
60 if let Some(chosen) = fdt.chosen()? {
61 if let (Some(start), Some(end)) = (chosen.getprop_u32(start)?, chosen.getprop_u32(end)?) {
62 return Ok(Some((start as usize)..(end as usize)));
63 }
64 }
65
66 Ok(None)
67}
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +000068
Jiyong Park9c63cd12023-03-21 17:53:07 +090069fn patch_initrd_range(fdt: &mut Fdt, initrd_range: &Range<usize>) -> libfdt::Result<()> {
70 let start = u32::try_from(initrd_range.start).unwrap();
71 let end = u32::try_from(initrd_range.end).unwrap();
72
73 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
74 node.setprop(cstr!("linux,initrd-start"), &start.to_be_bytes())?;
75 node.setprop(cstr!("linux,initrd-end"), &end.to_be_bytes())?;
76 Ok(())
77}
78
Jiyong Park6a8789a2023-03-21 14:50:59 +090079/// Read the first range in /memory node in DT
80fn read_memory_range_from(fdt: &Fdt) -> libfdt::Result<Range<usize>> {
81 fdt.memory()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound)
82}
Jiyong Park00ceff32023-03-13 05:43:23 +000083
Jiyong Park6a8789a2023-03-21 14:50:59 +090084/// Check if memory range is ok
85fn validate_memory_range(range: &Range<usize>) -> Result<(), RebootReason> {
86 let base = range.start;
Jiyong Park00ceff32023-03-13 05:43:23 +000087 if base as u64 != DeviceTreeInfo::RAM_BASE_ADDR {
88 error!("Memory base address {:#x} is not {:#x}", base, DeviceTreeInfo::RAM_BASE_ADDR);
89 return Err(RebootReason::InvalidFdt);
90 }
91
Jiyong Park6a8789a2023-03-21 14:50:59 +090092 let size = range.len();
Jiyong Park00ceff32023-03-13 05:43:23 +000093 if size % GUEST_PAGE_SIZE != 0 {
94 error!("Memory size {:#x} is not a multiple of page size {:#x}", size, GUEST_PAGE_SIZE);
95 return Err(RebootReason::InvalidFdt);
96 }
Jiyong Park00ceff32023-03-13 05:43:23 +000097
Jiyong Park6a8789a2023-03-21 14:50:59 +090098 if size == 0 {
99 error!("Memory size is 0");
100 return Err(RebootReason::InvalidFdt);
101 }
102 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000103}
104
Jiyong Park9c63cd12023-03-21 17:53:07 +0900105fn patch_memory_range(fdt: &mut Fdt, memory_range: &Range<usize>) -> libfdt::Result<()> {
106 let size = memory_range.len() as u64;
107 fdt.node_mut(cstr!("/memory"))?.ok_or(FdtError::NotFound)?.setprop_inplace(
108 cstr!("reg"),
109 flatten(&[DeviceTreeInfo::RAM_BASE_ADDR.to_be_bytes(), size.to_be_bytes()]),
110 )
111}
112
Jiyong Park6a8789a2023-03-21 14:50:59 +0900113/// Read the number of CPUs from DT
114fn read_num_cpus_from(fdt: &Fdt) -> libfdt::Result<usize> {
115 Ok(fdt.compatible_nodes(cstr!("arm,arm-v8"))?.count())
116}
117
118/// Validate number of CPUs
119fn validate_num_cpus(num_cpus: usize) -> Result<(), RebootReason> {
120 if num_cpus == 0 {
Jiyong Park00ceff32023-03-13 05:43:23 +0000121 error!("Number of CPU can't be 0");
Jiyong Park6a8789a2023-03-21 14:50:59 +0900122 return Err(RebootReason::InvalidFdt);
123 }
Jiyong Park9c63cd12023-03-21 17:53:07 +0900124 if DeviceTreeInfo::GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus.try_into().unwrap()).is_none() {
125 error!("Too many CPUs for gic: {}", num_cpus);
126 return Err(RebootReason::InvalidFdt);
127 }
128 Ok(())
129}
130
131/// Patch DT by keeping `num_cpus` number of arm,arm-v8 compatible nodes, and pruning the rest.
132fn patch_num_cpus(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
133 let cpu = cstr!("arm,arm-v8");
134 let mut next = fdt.root_mut()?.next_compatible(cpu)?;
135 for _ in 0..num_cpus {
136 next = if let Some(current) = next {
137 current.next_compatible(cpu)?
138 } else {
139 return Err(FdtError::NoSpace);
140 };
141 }
142 while let Some(current) = next {
143 next = current.delete_and_next_compatible(cpu)?;
144 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900145 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000146}
147
148#[derive(Debug)]
Jiyong Park00ceff32023-03-13 05:43:23 +0000149struct PciInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900150 ranges: [PciAddrRange; 2],
151 irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]>,
152 irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000153}
154
Jiyong Park6a8789a2023-03-21 14:50:59 +0900155impl PciInfo {
156 const IRQ_MASK_CELLS: usize = 4;
157 const IRQ_MAP_CELLS: usize = 10;
158 const MAX_IRQS: usize = 8;
Jiyong Park00ceff32023-03-13 05:43:23 +0000159}
160
Jiyong Park6a8789a2023-03-21 14:50:59 +0900161type PciAddrRange = AddressRange<(u32, u64), u64, u64>;
162type PciIrqMask = [u32; PciInfo::IRQ_MASK_CELLS];
163type PciIrqMap = [u32; PciInfo::IRQ_MAP_CELLS];
Jiyong Park00ceff32023-03-13 05:43:23 +0000164
165/// Iterator that takes N cells as a chunk
166struct CellChunkIterator<'a, const N: usize> {
167 cells: CellIterator<'a>,
168}
169
170impl<'a, const N: usize> CellChunkIterator<'a, N> {
171 fn new(cells: CellIterator<'a>) -> Self {
172 Self { cells }
173 }
174}
175
176impl<'a, const N: usize> Iterator for CellChunkIterator<'a, N> {
177 type Item = [u32; N];
178 fn next(&mut self) -> Option<Self::Item> {
179 let mut ret: Self::Item = [0; N];
180 for i in ret.iter_mut() {
181 *i = self.cells.next()?;
182 }
183 Some(ret)
184 }
185}
186
Jiyong Park6a8789a2023-03-21 14:50:59 +0900187/// Read pci host controller ranges, irq maps, and irq map masks from DT
188fn read_pci_info_from(fdt: &Fdt) -> libfdt::Result<PciInfo> {
189 let node =
190 fdt.compatible_nodes(cstr!("pci-host-cam-generic"))?.next().ok_or(FdtError::NotFound)?;
191
192 let mut ranges = node.ranges::<(u32, u64), u64, u64>()?.ok_or(FdtError::NotFound)?;
193 let range0 = ranges.next().ok_or(FdtError::NotFound)?;
194 let range1 = ranges.next().ok_or(FdtError::NotFound)?;
195
196 let irq_masks = node.getprop_cells(cstr!("interrupt-map-mask"))?.ok_or(FdtError::NotFound)?;
197 let irq_masks = CellChunkIterator::<{ PciInfo::IRQ_MASK_CELLS }>::new(irq_masks);
198 let irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]> =
199 irq_masks.take(PciInfo::MAX_IRQS).collect();
200
201 let irq_maps = node.getprop_cells(cstr!("interrupt-map"))?.ok_or(FdtError::NotFound)?;
202 let irq_maps = CellChunkIterator::<{ PciInfo::IRQ_MAP_CELLS }>::new(irq_maps);
203 let irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]> =
204 irq_maps.take(PciInfo::MAX_IRQS).collect();
205
206 Ok(PciInfo { ranges: [range0, range1], irq_masks, irq_maps })
207}
208
209fn validate_pci_info(pci_info: &PciInfo) -> Result<(), RebootReason> {
210 for range in pci_info.ranges.iter() {
211 validate_pci_addr_range(range)?;
212 }
213 for irq_mask in pci_info.irq_masks.iter() {
214 validate_pci_irq_mask(irq_mask)?;
215 }
216 for (idx, irq_map) in pci_info.irq_maps.iter().enumerate() {
217 validate_pci_irq_map(irq_map, idx)?;
218 }
219 Ok(())
220}
221
222fn validate_pci_addr_range(range: &PciAddrRange) -> Result<(), RebootReason> {
223 let mem_flags = PciMemoryFlags(range.addr.0);
224 let range_type = mem_flags.range_type();
225 let prefetchable = mem_flags.prefetchable();
226 let bus_addr = range.addr.1;
227 let cpu_addr = range.parent_addr;
228 let size = range.size;
229
230 if range_type != PciRangeType::Memory64 {
231 error!("Invalid range type {:?} for bus address {:#x} in PCI node", range_type, bus_addr);
232 return Err(RebootReason::InvalidFdt);
233 }
234 if prefetchable {
235 error!("PCI bus address {:#x} in PCI node is prefetchable", bus_addr);
236 return Err(RebootReason::InvalidFdt);
237 }
238 // Enforce ID bus-to-cpu mappings, as used by crosvm.
239 if bus_addr != cpu_addr {
240 error!("PCI bus address: {:#x} is different from CPU address: {:#x}", bus_addr, cpu_addr);
241 return Err(RebootReason::InvalidFdt);
242 }
243
244 if bus_addr.checked_add(size).is_none() {
245 error!("PCI address range size {:#x} too big", size);
246 return Err(RebootReason::InvalidFdt);
247 }
248
249 Ok(())
250}
251
252fn validate_pci_irq_mask(irq_mask: &PciIrqMask) -> Result<(), RebootReason> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000253 const IRQ_MASK_ADDR_HI: u32 = 0xf800;
254 const IRQ_MASK_ADDR_ME: u32 = 0x0;
255 const IRQ_MASK_ADDR_LO: u32 = 0x0;
256 const IRQ_MASK_ANY_IRQ: u32 = 0x7;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900257 const EXPECTED: PciIrqMask =
Jiyong Park00ceff32023-03-13 05:43:23 +0000258 [IRQ_MASK_ADDR_HI, IRQ_MASK_ADDR_ME, IRQ_MASK_ADDR_LO, IRQ_MASK_ANY_IRQ];
Jiyong Park6a8789a2023-03-21 14:50:59 +0900259 if *irq_mask != EXPECTED {
260 error!("Invalid PCI irq mask {:#?}", irq_mask);
261 return Err(RebootReason::InvalidFdt);
Jiyong Park00ceff32023-03-13 05:43:23 +0000262 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900263 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000264}
265
Jiyong Park6a8789a2023-03-21 14:50:59 +0900266fn validate_pci_irq_map(irq_map: &PciIrqMap, idx: usize) -> Result<(), RebootReason> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000267 const PCI_DEVICE_IDX: usize = 11;
268 const PCI_IRQ_ADDR_ME: u32 = 0;
269 const PCI_IRQ_ADDR_LO: u32 = 0;
270 const PCI_IRQ_INTC: u32 = 1;
271 const AARCH64_IRQ_BASE: u32 = 4; // from external/crosvm/aarch64/src/lib.rs
272 const GIC_SPI: u32 = 0;
273 const IRQ_TYPE_LEVEL_HIGH: u32 = 4;
274
Jiyong Park6a8789a2023-03-21 14:50:59 +0900275 let pci_addr = (irq_map[0], irq_map[1], irq_map[2]);
276 let pci_irq_number = irq_map[3];
277 let _controller_phandle = irq_map[4]; // skipped.
278 let gic_addr = (irq_map[5], irq_map[6]); // address-cells is <2> for GIC
279 // interrupt-cells is <3> for GIC
280 let gic_peripheral_interrupt_type = irq_map[7];
281 let gic_irq_number = irq_map[8];
282 let gic_irq_type = irq_map[9];
Jiyong Park00ceff32023-03-13 05:43:23 +0000283
Jiyong Park6a8789a2023-03-21 14:50:59 +0900284 let phys_hi: u32 = (0x1 << PCI_DEVICE_IDX) * (idx + 1) as u32;
285 let expected_pci_addr = (phys_hi, PCI_IRQ_ADDR_ME, PCI_IRQ_ADDR_LO);
Jiyong Park00ceff32023-03-13 05:43:23 +0000286
Jiyong Park6a8789a2023-03-21 14:50:59 +0900287 if pci_addr != expected_pci_addr {
288 error!("PCI device address {:#x} {:#x} {:#x} in interrupt-map is different from expected address \
289 {:#x} {:#x} {:#x}",
290 pci_addr.0, pci_addr.1, pci_addr.2, expected_pci_addr.0, expected_pci_addr.1, expected_pci_addr.2);
291 return Err(RebootReason::InvalidFdt);
292 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000293
Jiyong Park6a8789a2023-03-21 14:50:59 +0900294 if pci_irq_number != PCI_IRQ_INTC {
295 error!(
296 "PCI INT# {:#x} in interrupt-map is different from expected value {:#x}",
297 pci_irq_number, PCI_IRQ_INTC
298 );
299 return Err(RebootReason::InvalidFdt);
300 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000301
Jiyong Park6a8789a2023-03-21 14:50:59 +0900302 if gic_addr != (0, 0) {
303 error!(
304 "GIC address {:#x} {:#x} in interrupt-map is different from expected address \
305 {:#x} {:#x}",
306 gic_addr.0, gic_addr.1, 0, 0
307 );
308 return Err(RebootReason::InvalidFdt);
309 }
310
311 if gic_peripheral_interrupt_type != GIC_SPI {
312 error!("GIC peripheral interrupt type {:#x} in interrupt-map is different from expected value \
313 {:#x}", gic_peripheral_interrupt_type, GIC_SPI);
314 return Err(RebootReason::InvalidFdt);
315 }
316
317 let irq_nr: u32 = AARCH64_IRQ_BASE + (idx as u32);
318 if gic_irq_number != irq_nr {
319 error!(
320 "GIC irq number {:#x} in interrupt-map is unexpected. Expected {:#x}",
321 gic_irq_number, irq_nr
322 );
323 return Err(RebootReason::InvalidFdt);
324 }
325
326 if gic_irq_type != IRQ_TYPE_LEVEL_HIGH {
327 error!(
328 "IRQ type in {:#x} is invalid. Must be LEVEL_HIGH {:#x}",
329 gic_irq_type, IRQ_TYPE_LEVEL_HIGH
330 );
331 return Err(RebootReason::InvalidFdt);
Jiyong Park00ceff32023-03-13 05:43:23 +0000332 }
333 Ok(())
334}
335
Jiyong Park9c63cd12023-03-21 17:53:07 +0900336fn patch_pci_info(fdt: &mut Fdt, pci_info: &PciInfo) -> libfdt::Result<()> {
337 let mut node = fdt
338 .root_mut()?
339 .next_compatible(cstr!("pci-host-cam-generic"))?
340 .ok_or(FdtError::NotFound)?;
341
342 let irq_masks_size = pci_info.irq_masks.len() * size_of::<PciIrqMask>();
343 node.trimprop(cstr!("interrupt-map-mask"), irq_masks_size)?;
344
345 let irq_maps_size = pci_info.irq_maps.len() * size_of::<PciIrqMap>();
346 node.trimprop(cstr!("interrupt-map"), irq_maps_size)?;
347
348 node.setprop_inplace(
349 cstr!("ranges"),
350 flatten(&[pci_info.ranges[0].to_cells(), pci_info.ranges[1].to_cells()]),
351 )
352}
353
Jiyong Park00ceff32023-03-13 05:43:23 +0000354#[derive(Default, Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900355struct SerialInfo {
356 addrs: ArrayVec<[u64; Self::MAX_SERIALS]>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000357}
358
359impl SerialInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900360 const MAX_SERIALS: usize = 4;
Jiyong Park00ceff32023-03-13 05:43:23 +0000361}
362
Jiyong Park6a8789a2023-03-21 14:50:59 +0900363fn read_serial_info_from(fdt: &Fdt) -> libfdt::Result<SerialInfo> {
364 let mut addrs: ArrayVec<[u64; SerialInfo::MAX_SERIALS]> = Default::default();
365 for node in fdt.compatible_nodes(cstr!("ns16550a"))?.take(SerialInfo::MAX_SERIALS) {
366 let reg = node.reg()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound)?;
367 addrs.push(reg.addr);
Jiyong Park00ceff32023-03-13 05:43:23 +0000368 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900369 Ok(SerialInfo { addrs })
Jiyong Park00ceff32023-03-13 05:43:23 +0000370}
371
Jiyong Park9c63cd12023-03-21 17:53:07 +0900372/// Patch the DT by deleting the ns16550a compatible nodes whose address are unknown
373fn patch_serial_info(fdt: &mut Fdt, serial_info: &SerialInfo) -> libfdt::Result<()> {
374 let name = cstr!("ns16550a");
375 let mut next = fdt.root_mut()?.next_compatible(name);
376 while let Some(current) = next? {
377 let reg = FdtNode::from_mut(&current)
378 .reg()?
379 .ok_or(FdtError::NotFound)?
380 .next()
381 .ok_or(FdtError::NotFound)?;
382 next = if !serial_info.addrs.contains(&reg.addr) {
383 current.delete_and_next_compatible(name)
384 } else {
385 current.next_compatible(name)
386 }
387 }
388 Ok(())
389}
390
Jiyong Park00ceff32023-03-13 05:43:23 +0000391#[derive(Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900392struct SwiotlbInfo {
Jiyong Park00ceff32023-03-13 05:43:23 +0000393 size: u64,
394 align: u64,
395}
396
Jiyong Park6a8789a2023-03-21 14:50:59 +0900397fn read_swiotlb_info_from(fdt: &Fdt) -> libfdt::Result<SwiotlbInfo> {
398 let node =
399 fdt.compatible_nodes(cstr!("restricted-dma-pool"))?.next().ok_or(FdtError::NotFound)?;
400 let size = node.getprop_u64(cstr!("size"))?.ok_or(FdtError::NotFound)?;
401 let align = node.getprop_u64(cstr!("alignment"))?.ok_or(FdtError::NotFound)?;
402 Ok(SwiotlbInfo { size, align })
403}
Jiyong Park00ceff32023-03-13 05:43:23 +0000404
Jiyong Park6a8789a2023-03-21 14:50:59 +0900405fn validate_swiotlb_info(swiotlb_info: &SwiotlbInfo) -> Result<(), RebootReason> {
406 let size = swiotlb_info.size;
407 let align = swiotlb_info.align;
Jiyong Park00ceff32023-03-13 05:43:23 +0000408
409 if size == 0 || (size % GUEST_PAGE_SIZE as u64) != 0 {
410 error!("Invalid swiotlb size {:#x}", size);
411 return Err(RebootReason::InvalidFdt);
412 }
413
414 if (align % GUEST_PAGE_SIZE as u64) != 0 {
415 error!("Invalid swiotlb alignment {:#x}", align);
416 return Err(RebootReason::InvalidFdt);
417 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900418 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000419}
420
Jiyong Park9c63cd12023-03-21 17:53:07 +0900421fn patch_swiotlb_info(fdt: &mut Fdt, swiotlb_info: &SwiotlbInfo) -> libfdt::Result<()> {
422 let mut node =
423 fdt.root_mut()?.next_compatible(cstr!("restricted-dma-pool"))?.ok_or(FdtError::NotFound)?;
424 node.setprop_inplace(cstr!("size"), &swiotlb_info.size.to_be_bytes())?;
425 node.setprop_inplace(cstr!("alignment"), &swiotlb_info.align.to_be_bytes())?;
426 Ok(())
427}
428
429fn patch_gic(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
430 let node = fdt.compatible_nodes(cstr!("arm,gic-v3"))?.next().ok_or(FdtError::NotFound)?;
431 let mut ranges = node.reg()?.ok_or(FdtError::NotFound)?;
432 let range0 = ranges.next().ok_or(FdtError::NotFound)?;
433 let mut range1 = ranges.next().ok_or(FdtError::NotFound)?;
434
435 let addr = range0.addr;
436 // SAFETY - doesn't overflow. checked in validate_num_cpus
437 let size: u64 =
438 DeviceTreeInfo::GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus.try_into().unwrap()).unwrap();
439
440 // range1 is just below range0
441 range1.addr = addr - size;
442 range1.size = Some(size);
443
444 let range0 = range0.to_cells();
445 let range1 = range1.to_cells();
446 let value = [
447 range0.0, // addr
448 range0.1.unwrap(), //size
449 range1.0, // addr
450 range1.1.unwrap(), //size
451 ];
452
453 let mut node =
454 fdt.root_mut()?.next_compatible(cstr!("arm,gic-v3"))?.ok_or(FdtError::NotFound)?;
455 node.setprop_inplace(cstr!("reg"), flatten(&value))
456}
457
458fn patch_timer(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
459 const NUM_INTERRUPTS: usize = 4;
460 const CELLS_PER_INTERRUPT: usize = 3;
461 let node = fdt.compatible_nodes(cstr!("arm,armv8-timer"))?.next().ok_or(FdtError::NotFound)?;
462 let interrupts = node.getprop_cells(cstr!("interrupts"))?.ok_or(FdtError::NotFound)?;
463 let mut value: ArrayVec<[u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT]> =
464 interrupts.take(NUM_INTERRUPTS * CELLS_PER_INTERRUPT).collect();
465
466 let num_cpus: u32 = num_cpus.try_into().unwrap();
467 let cpu_mask: u32 = (((0x1 << num_cpus) - 1) & 0xff) << 8;
468 for v in value.iter_mut().skip(2).step_by(CELLS_PER_INTERRUPT) {
469 *v |= cpu_mask;
470 }
471 for v in value.iter_mut() {
472 *v = v.to_be();
473 }
474
475 // SAFETY - array size is the same
476 let value = unsafe {
477 core::mem::transmute::<
478 [u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT],
479 [u8; NUM_INTERRUPTS * CELLS_PER_INTERRUPT * size_of::<u32>()],
480 >(value.into_inner())
481 };
482
483 let mut node =
484 fdt.root_mut()?.next_compatible(cstr!("arm,armv8-timer"))?.ok_or(FdtError::NotFound)?;
485 node.setprop_inplace(cstr!("interrupts"), value.as_slice())
486}
487
Jiyong Park00ceff32023-03-13 05:43:23 +0000488#[derive(Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900489pub struct DeviceTreeInfo {
490 pub kernel_range: Option<Range<usize>>,
491 pub initrd_range: Option<Range<usize>>,
492 pub memory_range: Range<usize>,
493 num_cpus: usize,
Jiyong Park00ceff32023-03-13 05:43:23 +0000494 pci_info: PciInfo,
495 serial_info: SerialInfo,
496 swiotlb_info: SwiotlbInfo,
497}
498
499impl DeviceTreeInfo {
500 const RAM_BASE_ADDR: u64 = 0x8000_0000;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900501 const GIC_REDIST_SIZE_PER_CPU: u64 = (32 * SIZE_4KB) as u64;
Jiyong Park00ceff32023-03-13 05:43:23 +0000502}
503
Jiyong Park9c63cd12023-03-21 17:53:07 +0900504pub fn sanitize_device_tree(fdt: &mut Fdt) -> Result<DeviceTreeInfo, RebootReason> {
Jiyong Park83316122023-03-21 09:39:39 +0900505 let info = parse_device_tree(fdt)?;
506 debug!("Device tree info: {:?}", info);
507
508 // TODO: replace fdt with the template DT
Jiyong Park9c63cd12023-03-21 17:53:07 +0900509 patch_device_tree(fdt, &info)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900510 Ok(info)
Jiyong Park83316122023-03-21 09:39:39 +0900511}
512
513fn parse_device_tree(fdt: &libfdt::Fdt) -> Result<DeviceTreeInfo, RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900514 let kernel_range = read_kernel_range_from(fdt).map_err(|e| {
515 error!("Failed to read kernel range from DT: {e}");
516 RebootReason::InvalidFdt
517 })?;
518
519 let initrd_range = read_initrd_range_from(fdt).map_err(|e| {
520 error!("Failed to read initrd range from DT: {e}");
521 RebootReason::InvalidFdt
522 })?;
523
524 let memory_range = read_memory_range_from(fdt).map_err(|e| {
525 error!("Failed to read memory range from DT: {e}");
526 RebootReason::InvalidFdt
527 })?;
528 validate_memory_range(&memory_range)?;
529
530 let num_cpus = read_num_cpus_from(fdt).map_err(|e| {
531 error!("Failed to read num cpus from DT: {e}");
532 RebootReason::InvalidFdt
533 })?;
534 validate_num_cpus(num_cpus)?;
535
536 let pci_info = read_pci_info_from(fdt).map_err(|e| {
537 error!("Failed to read pci info from DT: {e}");
538 RebootReason::InvalidFdt
539 })?;
540 validate_pci_info(&pci_info)?;
541
542 let serial_info = read_serial_info_from(fdt).map_err(|e| {
543 error!("Failed to read serial info from DT: {e}");
544 RebootReason::InvalidFdt
545 })?;
546
547 let swiotlb_info = read_swiotlb_info_from(fdt).map_err(|e| {
548 error!("Failed to read swiotlb info from DT: {e}");
549 RebootReason::InvalidFdt
550 })?;
551 validate_swiotlb_info(&swiotlb_info)?;
552
Jiyong Park00ceff32023-03-13 05:43:23 +0000553 Ok(DeviceTreeInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900554 kernel_range,
555 initrd_range,
556 memory_range,
557 num_cpus,
558 pci_info,
559 serial_info,
560 swiotlb_info,
Jiyong Park00ceff32023-03-13 05:43:23 +0000561 })
562}
563
Jiyong Park9c63cd12023-03-21 17:53:07 +0900564fn patch_device_tree(fdt: &mut Fdt, info: &DeviceTreeInfo) -> Result<(), RebootReason> {
565 if let Some(initrd_range) = &info.initrd_range {
566 patch_initrd_range(fdt, initrd_range).map_err(|e| {
567 error!("Failed to patch initrd range to DT: {e}");
568 RebootReason::InvalidFdt
569 })?;
570 }
571 patch_memory_range(fdt, &info.memory_range).map_err(|e| {
572 error!("Failed to patch memory range to DT: {e}");
573 RebootReason::InvalidFdt
574 })?;
575 patch_num_cpus(fdt, info.num_cpus).map_err(|e| {
576 error!("Failed to patch cpus to DT: {e}");
577 RebootReason::InvalidFdt
578 })?;
579 patch_pci_info(fdt, &info.pci_info).map_err(|e| {
580 error!("Failed to patch pci info to DT: {e}");
581 RebootReason::InvalidFdt
582 })?;
583 patch_serial_info(fdt, &info.serial_info).map_err(|e| {
584 error!("Failed to patch serial info to DT: {e}");
585 RebootReason::InvalidFdt
586 })?;
587 patch_swiotlb_info(fdt, &info.swiotlb_info).map_err(|e| {
588 error!("Failed to patch swiotlb info to DT: {e}");
589 RebootReason::InvalidFdt
590 })?;
591 patch_gic(fdt, info.num_cpus).map_err(|e| {
592 error!("Failed to patch gic info to DT: {e}");
593 RebootReason::InvalidFdt
594 })?;
595 patch_timer(fdt, info.num_cpus).map_err(|e| {
596 error!("Failed to patch timer info to DT: {e}");
597 RebootReason::InvalidFdt
598 })?;
599 Ok(())
600}
601
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000602/// Modifies the input DT according to the fields of the configuration.
603pub fn modify_for_next_stage(
604 fdt: &mut Fdt,
605 bcc: &[u8],
606 new_instance: bool,
607 strict_boot: bool,
608) -> libfdt::Result<()> {
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000609 fdt.unpack()?;
610
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000611 add_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?;
612
Jiyong Parkb87f3302023-03-21 10:03:11 +0900613 set_or_clear_chosen_flag(fdt, cstr!("avf,strict-boot"), strict_boot)?;
614 set_or_clear_chosen_flag(fdt, cstr!("avf,new-instance"), new_instance)?;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000615
616 fdt.pack()?;
617
618 Ok(())
619}
620
621/// Add a "google,open-dice"-compatible reserved-memory node to the tree.
622fn add_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()> {
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000623 // We reject DTs with missing reserved-memory node as validation should have checked that the
624 // "swiotlb" subnode (compatible = "restricted-dma-pool") was present.
Jiyong Parkb87f3302023-03-21 10:03:11 +0900625 let mut reserved_memory =
626 fdt.node_mut(cstr!("/reserved-memory"))?.ok_or(libfdt::FdtError::NotFound)?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000627
Jiyong Parkb87f3302023-03-21 10:03:11 +0900628 let mut dice = reserved_memory.add_subnode(cstr!("dice"))?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000629
Jiyong Parkb87f3302023-03-21 10:03:11 +0900630 dice.appendprop(cstr!("compatible"), b"google,open-dice\0")?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000631
Jiyong Parkb87f3302023-03-21 10:03:11 +0900632 dice.appendprop(cstr!("no-map"), &[])?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000633
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000634 let addr = addr.try_into().unwrap();
635 let size = size.try_into().unwrap();
Jiyong Parkb87f3302023-03-21 10:03:11 +0900636 dice.appendprop_addrrange(cstr!("reg"), addr, size)?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000637
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000638 Ok(())
639}
640
641fn set_or_clear_chosen_flag(fdt: &mut Fdt, flag: &CStr, value: bool) -> libfdt::Result<()> {
642 // TODO(b/249054080): Refactor to not panic if the DT doesn't contain a /chosen node.
643 let mut chosen = fdt.chosen_mut()?.unwrap();
644 if value {
645 chosen.setprop_empty(flag)?;
646 } else {
647 match chosen.delprop(flag) {
648 Ok(()) | Err(FdtError::NotFound) => (),
649 Err(e) => return Err(e),
650 }
651 }
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000652
653 Ok(())
654}