blob: fca4583c669bd8f80ca203f08e682987ebe2c36a [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;
Jiyong Parke9d87e82023-03-21 19:28:40 +090022use alloc::ffi::CString;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000023use core::ffi::CStr;
Jiyong Park9c63cd12023-03-21 17:53:07 +090024use core::mem::size_of;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000025use core::ops::Range;
Jiyong Park00ceff32023-03-13 05:43:23 +000026use fdtpci::PciMemoryFlags;
27use fdtpci::PciRangeType;
28use libfdt::AddressRange;
29use libfdt::CellIterator;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +000030use libfdt::Fdt;
31use libfdt::FdtError;
Jiyong Park9c63cd12023-03-21 17:53:07 +090032use libfdt::FdtNode;
Jiyong Park83316122023-03-21 09:39:39 +090033use log::debug;
Jiyong Park00ceff32023-03-13 05:43:23 +000034use log::error;
35use tinyvec::ArrayVec;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000036
Jiyong Park6a8789a2023-03-21 14:50:59 +090037/// Extract from /config the address range containing the pre-loaded kernel. Absence of /config is
38/// not an error.
39fn read_kernel_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
Jiyong Parkb87f3302023-03-21 10:03:11 +090040 let addr = cstr!("kernel-address");
41 let size = cstr!("kernel-size");
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +000042
Jiyong Parkb87f3302023-03-21 10:03:11 +090043 if let Some(config) = fdt.node(cstr!("/config"))? {
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +000044 if let (Some(addr), Some(size)) = (config.getprop_u32(addr)?, config.getprop_u32(size)?) {
45 let addr = addr as usize;
46 let size = size as usize;
47
48 return Ok(Some(addr..(addr + size)));
49 }
50 }
51
52 Ok(None)
53}
54
Jiyong Park6a8789a2023-03-21 14:50:59 +090055/// Extract from /chosen the address range containing the pre-loaded ramdisk. Absence is not an
56/// error as there can be initrd-less VM.
57fn read_initrd_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
Jiyong Parkb87f3302023-03-21 10:03:11 +090058 let start = cstr!("linux,initrd-start");
59 let end = cstr!("linux,initrd-end");
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000060
61 if let Some(chosen) = fdt.chosen()? {
62 if let (Some(start), Some(end)) = (chosen.getprop_u32(start)?, chosen.getprop_u32(end)?) {
63 return Ok(Some((start as usize)..(end as usize)));
64 }
65 }
66
67 Ok(None)
68}
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +000069
Jiyong Park9c63cd12023-03-21 17:53:07 +090070fn patch_initrd_range(fdt: &mut Fdt, initrd_range: &Range<usize>) -> libfdt::Result<()> {
71 let start = u32::try_from(initrd_range.start).unwrap();
72 let end = u32::try_from(initrd_range.end).unwrap();
73
74 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
75 node.setprop(cstr!("linux,initrd-start"), &start.to_be_bytes())?;
76 node.setprop(cstr!("linux,initrd-end"), &end.to_be_bytes())?;
77 Ok(())
78}
79
Jiyong Parke9d87e82023-03-21 19:28:40 +090080fn read_bootargs_from(fdt: &Fdt) -> libfdt::Result<Option<CString>> {
81 if let Some(chosen) = fdt.chosen()? {
82 if let Some(bootargs) = chosen.getprop_str(cstr!("bootargs"))? {
83 // We need to copy the string to heap because the original fdt will be invalidated
84 // by the templated DT
85 let copy = CString::new(bootargs.to_bytes()).map_err(|_| FdtError::BadValue)?;
86 return Ok(Some(copy));
87 }
88 }
89 Ok(None)
90}
91
92fn patch_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()> {
93 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
94 // TODO(b/275306568) filter out dangerous options
95 node.setprop(cstr!("bootargs"), bootargs.to_bytes_with_nul())
96}
97
Jiyong Park6a8789a2023-03-21 14:50:59 +090098/// Read the first range in /memory node in DT
99fn read_memory_range_from(fdt: &Fdt) -> libfdt::Result<Range<usize>> {
100 fdt.memory()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound)
101}
Jiyong Park00ceff32023-03-13 05:43:23 +0000102
Jiyong Park6a8789a2023-03-21 14:50:59 +0900103/// Check if memory range is ok
104fn validate_memory_range(range: &Range<usize>) -> Result<(), RebootReason> {
105 let base = range.start;
Jiyong Park00ceff32023-03-13 05:43:23 +0000106 if base as u64 != DeviceTreeInfo::RAM_BASE_ADDR {
107 error!("Memory base address {:#x} is not {:#x}", base, DeviceTreeInfo::RAM_BASE_ADDR);
108 return Err(RebootReason::InvalidFdt);
109 }
110
Jiyong Park6a8789a2023-03-21 14:50:59 +0900111 let size = range.len();
Jiyong Park00ceff32023-03-13 05:43:23 +0000112 if size % GUEST_PAGE_SIZE != 0 {
113 error!("Memory size {:#x} is not a multiple of page size {:#x}", size, GUEST_PAGE_SIZE);
114 return Err(RebootReason::InvalidFdt);
115 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000116
Jiyong Park6a8789a2023-03-21 14:50:59 +0900117 if size == 0 {
118 error!("Memory size is 0");
119 return Err(RebootReason::InvalidFdt);
120 }
121 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000122}
123
Jiyong Park9c63cd12023-03-21 17:53:07 +0900124fn patch_memory_range(fdt: &mut Fdt, memory_range: &Range<usize>) -> libfdt::Result<()> {
125 let size = memory_range.len() as u64;
126 fdt.node_mut(cstr!("/memory"))?.ok_or(FdtError::NotFound)?.setprop_inplace(
127 cstr!("reg"),
128 flatten(&[DeviceTreeInfo::RAM_BASE_ADDR.to_be_bytes(), size.to_be_bytes()]),
129 )
130}
131
Jiyong Park6a8789a2023-03-21 14:50:59 +0900132/// Read the number of CPUs from DT
133fn read_num_cpus_from(fdt: &Fdt) -> libfdt::Result<usize> {
134 Ok(fdt.compatible_nodes(cstr!("arm,arm-v8"))?.count())
135}
136
137/// Validate number of CPUs
138fn validate_num_cpus(num_cpus: usize) -> Result<(), RebootReason> {
139 if num_cpus == 0 {
Jiyong Park00ceff32023-03-13 05:43:23 +0000140 error!("Number of CPU can't be 0");
Jiyong Park6a8789a2023-03-21 14:50:59 +0900141 return Err(RebootReason::InvalidFdt);
142 }
Jiyong Park9c63cd12023-03-21 17:53:07 +0900143 if DeviceTreeInfo::GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus.try_into().unwrap()).is_none() {
144 error!("Too many CPUs for gic: {}", num_cpus);
145 return Err(RebootReason::InvalidFdt);
146 }
147 Ok(())
148}
149
150/// Patch DT by keeping `num_cpus` number of arm,arm-v8 compatible nodes, and pruning the rest.
151fn patch_num_cpus(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
152 let cpu = cstr!("arm,arm-v8");
153 let mut next = fdt.root_mut()?.next_compatible(cpu)?;
154 for _ in 0..num_cpus {
155 next = if let Some(current) = next {
156 current.next_compatible(cpu)?
157 } else {
158 return Err(FdtError::NoSpace);
159 };
160 }
161 while let Some(current) = next {
162 next = current.delete_and_next_compatible(cpu)?;
163 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900164 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000165}
166
167#[derive(Debug)]
Jiyong Park00ceff32023-03-13 05:43:23 +0000168struct PciInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900169 ranges: [PciAddrRange; 2],
170 irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]>,
171 irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000172}
173
Jiyong Park6a8789a2023-03-21 14:50:59 +0900174impl PciInfo {
175 const IRQ_MASK_CELLS: usize = 4;
176 const IRQ_MAP_CELLS: usize = 10;
177 const MAX_IRQS: usize = 8;
Jiyong Park00ceff32023-03-13 05:43:23 +0000178}
179
Jiyong Park6a8789a2023-03-21 14:50:59 +0900180type PciAddrRange = AddressRange<(u32, u64), u64, u64>;
181type PciIrqMask = [u32; PciInfo::IRQ_MASK_CELLS];
182type PciIrqMap = [u32; PciInfo::IRQ_MAP_CELLS];
Jiyong Park00ceff32023-03-13 05:43:23 +0000183
184/// Iterator that takes N cells as a chunk
185struct CellChunkIterator<'a, const N: usize> {
186 cells: CellIterator<'a>,
187}
188
189impl<'a, const N: usize> CellChunkIterator<'a, N> {
190 fn new(cells: CellIterator<'a>) -> Self {
191 Self { cells }
192 }
193}
194
195impl<'a, const N: usize> Iterator for CellChunkIterator<'a, N> {
196 type Item = [u32; N];
197 fn next(&mut self) -> Option<Self::Item> {
198 let mut ret: Self::Item = [0; N];
199 for i in ret.iter_mut() {
200 *i = self.cells.next()?;
201 }
202 Some(ret)
203 }
204}
205
Jiyong Park6a8789a2023-03-21 14:50:59 +0900206/// Read pci host controller ranges, irq maps, and irq map masks from DT
207fn read_pci_info_from(fdt: &Fdt) -> libfdt::Result<PciInfo> {
208 let node =
209 fdt.compatible_nodes(cstr!("pci-host-cam-generic"))?.next().ok_or(FdtError::NotFound)?;
210
211 let mut ranges = node.ranges::<(u32, u64), u64, u64>()?.ok_or(FdtError::NotFound)?;
212 let range0 = ranges.next().ok_or(FdtError::NotFound)?;
213 let range1 = ranges.next().ok_or(FdtError::NotFound)?;
214
215 let irq_masks = node.getprop_cells(cstr!("interrupt-map-mask"))?.ok_or(FdtError::NotFound)?;
216 let irq_masks = CellChunkIterator::<{ PciInfo::IRQ_MASK_CELLS }>::new(irq_masks);
217 let irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]> =
218 irq_masks.take(PciInfo::MAX_IRQS).collect();
219
220 let irq_maps = node.getprop_cells(cstr!("interrupt-map"))?.ok_or(FdtError::NotFound)?;
221 let irq_maps = CellChunkIterator::<{ PciInfo::IRQ_MAP_CELLS }>::new(irq_maps);
222 let irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]> =
223 irq_maps.take(PciInfo::MAX_IRQS).collect();
224
225 Ok(PciInfo { ranges: [range0, range1], irq_masks, irq_maps })
226}
227
228fn validate_pci_info(pci_info: &PciInfo) -> Result<(), RebootReason> {
229 for range in pci_info.ranges.iter() {
230 validate_pci_addr_range(range)?;
231 }
232 for irq_mask in pci_info.irq_masks.iter() {
233 validate_pci_irq_mask(irq_mask)?;
234 }
235 for (idx, irq_map) in pci_info.irq_maps.iter().enumerate() {
236 validate_pci_irq_map(irq_map, idx)?;
237 }
238 Ok(())
239}
240
241fn validate_pci_addr_range(range: &PciAddrRange) -> Result<(), RebootReason> {
242 let mem_flags = PciMemoryFlags(range.addr.0);
243 let range_type = mem_flags.range_type();
244 let prefetchable = mem_flags.prefetchable();
245 let bus_addr = range.addr.1;
246 let cpu_addr = range.parent_addr;
247 let size = range.size;
248
249 if range_type != PciRangeType::Memory64 {
250 error!("Invalid range type {:?} for bus address {:#x} in PCI node", range_type, bus_addr);
251 return Err(RebootReason::InvalidFdt);
252 }
253 if prefetchable {
254 error!("PCI bus address {:#x} in PCI node is prefetchable", bus_addr);
255 return Err(RebootReason::InvalidFdt);
256 }
257 // Enforce ID bus-to-cpu mappings, as used by crosvm.
258 if bus_addr != cpu_addr {
259 error!("PCI bus address: {:#x} is different from CPU address: {:#x}", bus_addr, cpu_addr);
260 return Err(RebootReason::InvalidFdt);
261 }
262
263 if bus_addr.checked_add(size).is_none() {
264 error!("PCI address range size {:#x} too big", size);
265 return Err(RebootReason::InvalidFdt);
266 }
267
268 Ok(())
269}
270
271fn validate_pci_irq_mask(irq_mask: &PciIrqMask) -> Result<(), RebootReason> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000272 const IRQ_MASK_ADDR_HI: u32 = 0xf800;
273 const IRQ_MASK_ADDR_ME: u32 = 0x0;
274 const IRQ_MASK_ADDR_LO: u32 = 0x0;
275 const IRQ_MASK_ANY_IRQ: u32 = 0x7;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900276 const EXPECTED: PciIrqMask =
Jiyong Park00ceff32023-03-13 05:43:23 +0000277 [IRQ_MASK_ADDR_HI, IRQ_MASK_ADDR_ME, IRQ_MASK_ADDR_LO, IRQ_MASK_ANY_IRQ];
Jiyong Park6a8789a2023-03-21 14:50:59 +0900278 if *irq_mask != EXPECTED {
279 error!("Invalid PCI irq mask {:#?}", irq_mask);
280 return Err(RebootReason::InvalidFdt);
Jiyong Park00ceff32023-03-13 05:43:23 +0000281 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900282 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000283}
284
Jiyong Park6a8789a2023-03-21 14:50:59 +0900285fn validate_pci_irq_map(irq_map: &PciIrqMap, idx: usize) -> Result<(), RebootReason> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000286 const PCI_DEVICE_IDX: usize = 11;
287 const PCI_IRQ_ADDR_ME: u32 = 0;
288 const PCI_IRQ_ADDR_LO: u32 = 0;
289 const PCI_IRQ_INTC: u32 = 1;
290 const AARCH64_IRQ_BASE: u32 = 4; // from external/crosvm/aarch64/src/lib.rs
291 const GIC_SPI: u32 = 0;
292 const IRQ_TYPE_LEVEL_HIGH: u32 = 4;
293
Jiyong Park6a8789a2023-03-21 14:50:59 +0900294 let pci_addr = (irq_map[0], irq_map[1], irq_map[2]);
295 let pci_irq_number = irq_map[3];
296 let _controller_phandle = irq_map[4]; // skipped.
297 let gic_addr = (irq_map[5], irq_map[6]); // address-cells is <2> for GIC
298 // interrupt-cells is <3> for GIC
299 let gic_peripheral_interrupt_type = irq_map[7];
300 let gic_irq_number = irq_map[8];
301 let gic_irq_type = irq_map[9];
Jiyong Park00ceff32023-03-13 05:43:23 +0000302
Jiyong Park6a8789a2023-03-21 14:50:59 +0900303 let phys_hi: u32 = (0x1 << PCI_DEVICE_IDX) * (idx + 1) as u32;
304 let expected_pci_addr = (phys_hi, PCI_IRQ_ADDR_ME, PCI_IRQ_ADDR_LO);
Jiyong Park00ceff32023-03-13 05:43:23 +0000305
Jiyong Park6a8789a2023-03-21 14:50:59 +0900306 if pci_addr != expected_pci_addr {
307 error!("PCI device address {:#x} {:#x} {:#x} in interrupt-map is different from expected address \
308 {:#x} {:#x} {:#x}",
309 pci_addr.0, pci_addr.1, pci_addr.2, expected_pci_addr.0, expected_pci_addr.1, expected_pci_addr.2);
310 return Err(RebootReason::InvalidFdt);
311 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000312
Jiyong Park6a8789a2023-03-21 14:50:59 +0900313 if pci_irq_number != PCI_IRQ_INTC {
314 error!(
315 "PCI INT# {:#x} in interrupt-map is different from expected value {:#x}",
316 pci_irq_number, PCI_IRQ_INTC
317 );
318 return Err(RebootReason::InvalidFdt);
319 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000320
Jiyong Park6a8789a2023-03-21 14:50:59 +0900321 if gic_addr != (0, 0) {
322 error!(
323 "GIC address {:#x} {:#x} in interrupt-map is different from expected address \
324 {:#x} {:#x}",
325 gic_addr.0, gic_addr.1, 0, 0
326 );
327 return Err(RebootReason::InvalidFdt);
328 }
329
330 if gic_peripheral_interrupt_type != GIC_SPI {
331 error!("GIC peripheral interrupt type {:#x} in interrupt-map is different from expected value \
332 {:#x}", gic_peripheral_interrupt_type, GIC_SPI);
333 return Err(RebootReason::InvalidFdt);
334 }
335
336 let irq_nr: u32 = AARCH64_IRQ_BASE + (idx as u32);
337 if gic_irq_number != irq_nr {
338 error!(
339 "GIC irq number {:#x} in interrupt-map is unexpected. Expected {:#x}",
340 gic_irq_number, irq_nr
341 );
342 return Err(RebootReason::InvalidFdt);
343 }
344
345 if gic_irq_type != IRQ_TYPE_LEVEL_HIGH {
346 error!(
347 "IRQ type in {:#x} is invalid. Must be LEVEL_HIGH {:#x}",
348 gic_irq_type, IRQ_TYPE_LEVEL_HIGH
349 );
350 return Err(RebootReason::InvalidFdt);
Jiyong Park00ceff32023-03-13 05:43:23 +0000351 }
352 Ok(())
353}
354
Jiyong Park9c63cd12023-03-21 17:53:07 +0900355fn patch_pci_info(fdt: &mut Fdt, pci_info: &PciInfo) -> libfdt::Result<()> {
356 let mut node = fdt
357 .root_mut()?
358 .next_compatible(cstr!("pci-host-cam-generic"))?
359 .ok_or(FdtError::NotFound)?;
360
361 let irq_masks_size = pci_info.irq_masks.len() * size_of::<PciIrqMask>();
362 node.trimprop(cstr!("interrupt-map-mask"), irq_masks_size)?;
363
364 let irq_maps_size = pci_info.irq_maps.len() * size_of::<PciIrqMap>();
365 node.trimprop(cstr!("interrupt-map"), irq_maps_size)?;
366
367 node.setprop_inplace(
368 cstr!("ranges"),
369 flatten(&[pci_info.ranges[0].to_cells(), pci_info.ranges[1].to_cells()]),
370 )
371}
372
Jiyong Park00ceff32023-03-13 05:43:23 +0000373#[derive(Default, Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900374struct SerialInfo {
375 addrs: ArrayVec<[u64; Self::MAX_SERIALS]>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000376}
377
378impl SerialInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900379 const MAX_SERIALS: usize = 4;
Jiyong Park00ceff32023-03-13 05:43:23 +0000380}
381
Jiyong Park6a8789a2023-03-21 14:50:59 +0900382fn read_serial_info_from(fdt: &Fdt) -> libfdt::Result<SerialInfo> {
383 let mut addrs: ArrayVec<[u64; SerialInfo::MAX_SERIALS]> = Default::default();
384 for node in fdt.compatible_nodes(cstr!("ns16550a"))?.take(SerialInfo::MAX_SERIALS) {
385 let reg = node.reg()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound)?;
386 addrs.push(reg.addr);
Jiyong Park00ceff32023-03-13 05:43:23 +0000387 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900388 Ok(SerialInfo { addrs })
Jiyong Park00ceff32023-03-13 05:43:23 +0000389}
390
Jiyong Park9c63cd12023-03-21 17:53:07 +0900391/// Patch the DT by deleting the ns16550a compatible nodes whose address are unknown
392fn patch_serial_info(fdt: &mut Fdt, serial_info: &SerialInfo) -> libfdt::Result<()> {
393 let name = cstr!("ns16550a");
394 let mut next = fdt.root_mut()?.next_compatible(name);
395 while let Some(current) = next? {
396 let reg = FdtNode::from_mut(&current)
397 .reg()?
398 .ok_or(FdtError::NotFound)?
399 .next()
400 .ok_or(FdtError::NotFound)?;
401 next = if !serial_info.addrs.contains(&reg.addr) {
402 current.delete_and_next_compatible(name)
403 } else {
404 current.next_compatible(name)
405 }
406 }
407 Ok(())
408}
409
Jiyong Park00ceff32023-03-13 05:43:23 +0000410#[derive(Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900411struct SwiotlbInfo {
Jiyong Park00ceff32023-03-13 05:43:23 +0000412 size: u64,
413 align: u64,
414}
415
Jiyong Park6a8789a2023-03-21 14:50:59 +0900416fn read_swiotlb_info_from(fdt: &Fdt) -> libfdt::Result<SwiotlbInfo> {
417 let node =
418 fdt.compatible_nodes(cstr!("restricted-dma-pool"))?.next().ok_or(FdtError::NotFound)?;
419 let size = node.getprop_u64(cstr!("size"))?.ok_or(FdtError::NotFound)?;
420 let align = node.getprop_u64(cstr!("alignment"))?.ok_or(FdtError::NotFound)?;
421 Ok(SwiotlbInfo { size, align })
422}
Jiyong Park00ceff32023-03-13 05:43:23 +0000423
Jiyong Park6a8789a2023-03-21 14:50:59 +0900424fn validate_swiotlb_info(swiotlb_info: &SwiotlbInfo) -> Result<(), RebootReason> {
425 let size = swiotlb_info.size;
426 let align = swiotlb_info.align;
Jiyong Park00ceff32023-03-13 05:43:23 +0000427
428 if size == 0 || (size % GUEST_PAGE_SIZE as u64) != 0 {
429 error!("Invalid swiotlb size {:#x}", size);
430 return Err(RebootReason::InvalidFdt);
431 }
432
433 if (align % GUEST_PAGE_SIZE as u64) != 0 {
434 error!("Invalid swiotlb alignment {:#x}", align);
435 return Err(RebootReason::InvalidFdt);
436 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900437 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000438}
439
Jiyong Park9c63cd12023-03-21 17:53:07 +0900440fn patch_swiotlb_info(fdt: &mut Fdt, swiotlb_info: &SwiotlbInfo) -> libfdt::Result<()> {
441 let mut node =
442 fdt.root_mut()?.next_compatible(cstr!("restricted-dma-pool"))?.ok_or(FdtError::NotFound)?;
443 node.setprop_inplace(cstr!("size"), &swiotlb_info.size.to_be_bytes())?;
444 node.setprop_inplace(cstr!("alignment"), &swiotlb_info.align.to_be_bytes())?;
445 Ok(())
446}
447
448fn patch_gic(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
449 let node = fdt.compatible_nodes(cstr!("arm,gic-v3"))?.next().ok_or(FdtError::NotFound)?;
450 let mut ranges = node.reg()?.ok_or(FdtError::NotFound)?;
451 let range0 = ranges.next().ok_or(FdtError::NotFound)?;
452 let mut range1 = ranges.next().ok_or(FdtError::NotFound)?;
453
454 let addr = range0.addr;
455 // SAFETY - doesn't overflow. checked in validate_num_cpus
456 let size: u64 =
457 DeviceTreeInfo::GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus.try_into().unwrap()).unwrap();
458
459 // range1 is just below range0
460 range1.addr = addr - size;
461 range1.size = Some(size);
462
463 let range0 = range0.to_cells();
464 let range1 = range1.to_cells();
465 let value = [
466 range0.0, // addr
467 range0.1.unwrap(), //size
468 range1.0, // addr
469 range1.1.unwrap(), //size
470 ];
471
472 let mut node =
473 fdt.root_mut()?.next_compatible(cstr!("arm,gic-v3"))?.ok_or(FdtError::NotFound)?;
474 node.setprop_inplace(cstr!("reg"), flatten(&value))
475}
476
477fn patch_timer(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
478 const NUM_INTERRUPTS: usize = 4;
479 const CELLS_PER_INTERRUPT: usize = 3;
480 let node = fdt.compatible_nodes(cstr!("arm,armv8-timer"))?.next().ok_or(FdtError::NotFound)?;
481 let interrupts = node.getprop_cells(cstr!("interrupts"))?.ok_or(FdtError::NotFound)?;
482 let mut value: ArrayVec<[u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT]> =
483 interrupts.take(NUM_INTERRUPTS * CELLS_PER_INTERRUPT).collect();
484
485 let num_cpus: u32 = num_cpus.try_into().unwrap();
486 let cpu_mask: u32 = (((0x1 << num_cpus) - 1) & 0xff) << 8;
487 for v in value.iter_mut().skip(2).step_by(CELLS_PER_INTERRUPT) {
488 *v |= cpu_mask;
489 }
490 for v in value.iter_mut() {
491 *v = v.to_be();
492 }
493
494 // SAFETY - array size is the same
495 let value = unsafe {
496 core::mem::transmute::<
497 [u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT],
498 [u8; NUM_INTERRUPTS * CELLS_PER_INTERRUPT * size_of::<u32>()],
499 >(value.into_inner())
500 };
501
502 let mut node =
503 fdt.root_mut()?.next_compatible(cstr!("arm,armv8-timer"))?.ok_or(FdtError::NotFound)?;
504 node.setprop_inplace(cstr!("interrupts"), value.as_slice())
505}
506
Jiyong Park00ceff32023-03-13 05:43:23 +0000507#[derive(Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900508pub struct DeviceTreeInfo {
509 pub kernel_range: Option<Range<usize>>,
510 pub initrd_range: Option<Range<usize>>,
511 pub memory_range: Range<usize>,
Jiyong Parke9d87e82023-03-21 19:28:40 +0900512 bootargs: Option<CString>,
Jiyong Park6a8789a2023-03-21 14:50:59 +0900513 num_cpus: usize,
Jiyong Park00ceff32023-03-13 05:43:23 +0000514 pci_info: PciInfo,
515 serial_info: SerialInfo,
516 swiotlb_info: SwiotlbInfo,
517}
518
519impl DeviceTreeInfo {
520 const RAM_BASE_ADDR: u64 = 0x8000_0000;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900521 const GIC_REDIST_SIZE_PER_CPU: u64 = (32 * SIZE_4KB) as u64;
Jiyong Park00ceff32023-03-13 05:43:23 +0000522}
523
Jiyong Park9c63cd12023-03-21 17:53:07 +0900524pub fn sanitize_device_tree(fdt: &mut Fdt) -> Result<DeviceTreeInfo, RebootReason> {
Jiyong Park83316122023-03-21 09:39:39 +0900525 let info = parse_device_tree(fdt)?;
526 debug!("Device tree info: {:?}", info);
527
Jiyong Parke9d87e82023-03-21 19:28:40 +0900528 fdt.copy_from_slice(pvmfw_fdt_template::RAW).map_err(|e| {
529 error!("Failed to instantiate FDT from the template DT: {e}");
530 RebootReason::InvalidFdt
531 })?;
532
Jiyong Park9c63cd12023-03-21 17:53:07 +0900533 patch_device_tree(fdt, &info)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900534 Ok(info)
Jiyong Park83316122023-03-21 09:39:39 +0900535}
536
537fn parse_device_tree(fdt: &libfdt::Fdt) -> Result<DeviceTreeInfo, RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900538 let kernel_range = read_kernel_range_from(fdt).map_err(|e| {
539 error!("Failed to read kernel range from DT: {e}");
540 RebootReason::InvalidFdt
541 })?;
542
543 let initrd_range = read_initrd_range_from(fdt).map_err(|e| {
544 error!("Failed to read initrd range from DT: {e}");
545 RebootReason::InvalidFdt
546 })?;
547
548 let memory_range = read_memory_range_from(fdt).map_err(|e| {
549 error!("Failed to read memory range from DT: {e}");
550 RebootReason::InvalidFdt
551 })?;
552 validate_memory_range(&memory_range)?;
553
Jiyong Parke9d87e82023-03-21 19:28:40 +0900554 let bootargs = read_bootargs_from(fdt).map_err(|e| {
555 error!("Failed to read bootargs from DT: {e}");
556 RebootReason::InvalidFdt
557 })?;
558
Jiyong Park6a8789a2023-03-21 14:50:59 +0900559 let num_cpus = read_num_cpus_from(fdt).map_err(|e| {
560 error!("Failed to read num cpus from DT: {e}");
561 RebootReason::InvalidFdt
562 })?;
563 validate_num_cpus(num_cpus)?;
564
565 let pci_info = read_pci_info_from(fdt).map_err(|e| {
566 error!("Failed to read pci info from DT: {e}");
567 RebootReason::InvalidFdt
568 })?;
569 validate_pci_info(&pci_info)?;
570
571 let serial_info = read_serial_info_from(fdt).map_err(|e| {
572 error!("Failed to read serial info from DT: {e}");
573 RebootReason::InvalidFdt
574 })?;
575
576 let swiotlb_info = read_swiotlb_info_from(fdt).map_err(|e| {
577 error!("Failed to read swiotlb info from DT: {e}");
578 RebootReason::InvalidFdt
579 })?;
580 validate_swiotlb_info(&swiotlb_info)?;
581
Jiyong Park00ceff32023-03-13 05:43:23 +0000582 Ok(DeviceTreeInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900583 kernel_range,
584 initrd_range,
585 memory_range,
Jiyong Parke9d87e82023-03-21 19:28:40 +0900586 bootargs,
Jiyong Park6a8789a2023-03-21 14:50:59 +0900587 num_cpus,
588 pci_info,
589 serial_info,
590 swiotlb_info,
Jiyong Park00ceff32023-03-13 05:43:23 +0000591 })
592}
593
Jiyong Park9c63cd12023-03-21 17:53:07 +0900594fn patch_device_tree(fdt: &mut Fdt, info: &DeviceTreeInfo) -> Result<(), RebootReason> {
Jiyong Parke9d87e82023-03-21 19:28:40 +0900595 fdt.unpack().map_err(|e| {
596 error!("Failed to unpack DT for patching: {e}");
597 RebootReason::InvalidFdt
598 })?;
599
Jiyong Park9c63cd12023-03-21 17:53:07 +0900600 if let Some(initrd_range) = &info.initrd_range {
601 patch_initrd_range(fdt, initrd_range).map_err(|e| {
602 error!("Failed to patch initrd range to DT: {e}");
603 RebootReason::InvalidFdt
604 })?;
605 }
606 patch_memory_range(fdt, &info.memory_range).map_err(|e| {
607 error!("Failed to patch memory range to DT: {e}");
608 RebootReason::InvalidFdt
609 })?;
Jiyong Parke9d87e82023-03-21 19:28:40 +0900610 if let Some(bootargs) = &info.bootargs {
611 patch_bootargs(fdt, bootargs.as_c_str()).map_err(|e| {
612 error!("Failed to patch bootargs to DT: {e}");
613 RebootReason::InvalidFdt
614 })?;
615 }
Jiyong Park9c63cd12023-03-21 17:53:07 +0900616 patch_num_cpus(fdt, info.num_cpus).map_err(|e| {
617 error!("Failed to patch cpus to DT: {e}");
618 RebootReason::InvalidFdt
619 })?;
620 patch_pci_info(fdt, &info.pci_info).map_err(|e| {
621 error!("Failed to patch pci info to DT: {e}");
622 RebootReason::InvalidFdt
623 })?;
624 patch_serial_info(fdt, &info.serial_info).map_err(|e| {
625 error!("Failed to patch serial info to DT: {e}");
626 RebootReason::InvalidFdt
627 })?;
628 patch_swiotlb_info(fdt, &info.swiotlb_info).map_err(|e| {
629 error!("Failed to patch swiotlb info to DT: {e}");
630 RebootReason::InvalidFdt
631 })?;
632 patch_gic(fdt, info.num_cpus).map_err(|e| {
633 error!("Failed to patch gic info to DT: {e}");
634 RebootReason::InvalidFdt
635 })?;
636 patch_timer(fdt, info.num_cpus).map_err(|e| {
637 error!("Failed to patch timer info to DT: {e}");
638 RebootReason::InvalidFdt
639 })?;
Jiyong Parke9d87e82023-03-21 19:28:40 +0900640
641 fdt.pack().map_err(|e| {
642 error!("Failed to pack DT after patching: {e}");
643 RebootReason::InvalidFdt
644 })?;
645
Jiyong Park9c63cd12023-03-21 17:53:07 +0900646 Ok(())
647}
648
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000649/// Modifies the input DT according to the fields of the configuration.
650pub fn modify_for_next_stage(
651 fdt: &mut Fdt,
652 bcc: &[u8],
653 new_instance: bool,
654 strict_boot: bool,
655) -> libfdt::Result<()> {
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000656 fdt.unpack()?;
657
Jiyong Parke9d87e82023-03-21 19:28:40 +0900658 patch_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000659
Jiyong Parkb87f3302023-03-21 10:03:11 +0900660 set_or_clear_chosen_flag(fdt, cstr!("avf,strict-boot"), strict_boot)?;
661 set_or_clear_chosen_flag(fdt, cstr!("avf,new-instance"), new_instance)?;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000662
663 fdt.pack()?;
664
665 Ok(())
666}
667
Jiyong Parke9d87e82023-03-21 19:28:40 +0900668/// Patch the "google,open-dice"-compatible reserved-memory node to point to the bcc range
669fn patch_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()> {
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000670 // We reject DTs with missing reserved-memory node as validation should have checked that the
671 // "swiotlb" subnode (compatible = "restricted-dma-pool") was present.
Jiyong Parke9d87e82023-03-21 19:28:40 +0900672 let node = fdt.node_mut(cstr!("/reserved-memory"))?.ok_or(libfdt::FdtError::NotFound)?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000673
Jiyong Parke9d87e82023-03-21 19:28:40 +0900674 let mut node = node.next_compatible(cstr!("google,open-dice"))?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000675
Jiyong Parke9d87e82023-03-21 19:28:40 +0900676 let addr: u64 = addr.try_into().unwrap();
677 let size: u64 = size.try_into().unwrap();
678 node.setprop_inplace(cstr!("reg"), flatten(&[addr.to_be_bytes(), size.to_be_bytes()]))
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +0000679}
680
681fn set_or_clear_chosen_flag(fdt: &mut Fdt, flag: &CStr, value: bool) -> libfdt::Result<()> {
682 // TODO(b/249054080): Refactor to not panic if the DT doesn't contain a /chosen node.
683 let mut chosen = fdt.chosen_mut()?.unwrap();
684 if value {
685 chosen.setprop_empty(flag)?;
686 } else {
687 match chosen.delprop(flag) {
688 Ok(()) | Err(FdtError::NotFound) => (),
689 Err(e) => return Err(e),
690 }
691 }
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000692
693 Ok(())
694}