Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 1 | // 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 Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 17 | use crate::cstr; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 18 | use crate::helpers::flatten; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 19 | use crate::helpers::GUEST_PAGE_SIZE; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 20 | use crate::helpers::SIZE_4KB; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 21 | use crate::RebootReason; |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 22 | use alloc::ffi::CString; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 23 | use core::ffi::CStr; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 24 | use core::mem::size_of; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 25 | use core::ops::Range; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 26 | use fdtpci::PciMemoryFlags; |
| 27 | use fdtpci::PciRangeType; |
| 28 | use libfdt::AddressRange; |
| 29 | use libfdt::CellIterator; |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 30 | use libfdt::Fdt; |
| 31 | use libfdt::FdtError; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 32 | use libfdt::FdtNode; |
Jiyong Park | 8331612 | 2023-03-21 09:39:39 +0900 | [diff] [blame] | 33 | use log::debug; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 34 | use log::error; |
| 35 | use tinyvec::ArrayVec; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 36 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 37 | /// Extract from /config the address range containing the pre-loaded kernel. Absence of /config is |
| 38 | /// not an error. |
| 39 | fn read_kernel_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> { |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 40 | let addr = cstr!("kernel-address"); |
| 41 | let size = cstr!("kernel-size"); |
Pierre-Clément Tosi | c3811b8 | 2022-11-29 11:24:16 +0000 | [diff] [blame] | 42 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 43 | if let Some(config) = fdt.node(cstr!("/config"))? { |
Pierre-Clément Tosi | c3811b8 | 2022-11-29 11:24:16 +0000 | [diff] [blame] | 44 | 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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 55 | /// Extract from /chosen the address range containing the pre-loaded ramdisk. Absence is not an |
| 56 | /// error as there can be initrd-less VM. |
| 57 | fn read_initrd_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> { |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 58 | let start = cstr!("linux,initrd-start"); |
| 59 | let end = cstr!("linux,initrd-end"); |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 60 | |
| 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 Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 69 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 70 | fn 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 Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 80 | fn 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 | |
| 92 | fn 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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 98 | /// Read the first range in /memory node in DT |
| 99 | fn read_memory_range_from(fdt: &Fdt) -> libfdt::Result<Range<usize>> { |
| 100 | fdt.memory()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound) |
| 101 | } |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 102 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 103 | /// Check if memory range is ok |
| 104 | fn validate_memory_range(range: &Range<usize>) -> Result<(), RebootReason> { |
| 105 | let base = range.start; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 106 | 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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 111 | let size = range.len(); |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 112 | 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 116 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 117 | if size == 0 { |
| 118 | error!("Memory size is 0"); |
| 119 | return Err(RebootReason::InvalidFdt); |
| 120 | } |
| 121 | Ok(()) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 124 | fn 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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 132 | /// Read the number of CPUs from DT |
| 133 | fn 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 |
| 138 | fn validate_num_cpus(num_cpus: usize) -> Result<(), RebootReason> { |
| 139 | if num_cpus == 0 { |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 140 | error!("Number of CPU can't be 0"); |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 141 | return Err(RebootReason::InvalidFdt); |
| 142 | } |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 143 | 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. |
| 151 | fn 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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 164 | Ok(()) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | #[derive(Debug)] |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 168 | struct PciInfo { |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 169 | ranges: [PciAddrRange; 2], |
| 170 | irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]>, |
| 171 | irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]>, |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 174 | impl PciInfo { |
| 175 | const IRQ_MASK_CELLS: usize = 4; |
| 176 | const IRQ_MAP_CELLS: usize = 10; |
| 177 | const MAX_IRQS: usize = 8; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 180 | type PciAddrRange = AddressRange<(u32, u64), u64, u64>; |
| 181 | type PciIrqMask = [u32; PciInfo::IRQ_MASK_CELLS]; |
| 182 | type PciIrqMap = [u32; PciInfo::IRQ_MAP_CELLS]; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 183 | |
| 184 | /// Iterator that takes N cells as a chunk |
| 185 | struct CellChunkIterator<'a, const N: usize> { |
| 186 | cells: CellIterator<'a>, |
| 187 | } |
| 188 | |
| 189 | impl<'a, const N: usize> CellChunkIterator<'a, N> { |
| 190 | fn new(cells: CellIterator<'a>) -> Self { |
| 191 | Self { cells } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl<'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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 206 | /// Read pci host controller ranges, irq maps, and irq map masks from DT |
| 207 | fn 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 | |
| 228 | fn 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 | |
| 241 | fn 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 | |
| 271 | fn validate_pci_irq_mask(irq_mask: &PciIrqMask) -> Result<(), RebootReason> { |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 272 | 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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 276 | const EXPECTED: PciIrqMask = |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 277 | [IRQ_MASK_ADDR_HI, IRQ_MASK_ADDR_ME, IRQ_MASK_ADDR_LO, IRQ_MASK_ANY_IRQ]; |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 278 | if *irq_mask != EXPECTED { |
| 279 | error!("Invalid PCI irq mask {:#?}", irq_mask); |
| 280 | return Err(RebootReason::InvalidFdt); |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 281 | } |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 282 | Ok(()) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 285 | fn validate_pci_irq_map(irq_map: &PciIrqMap, idx: usize) -> Result<(), RebootReason> { |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 286 | 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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 294 | 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 302 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 303 | 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 305 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 306 | 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 312 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 313 | 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 320 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 321 | 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 351 | } |
| 352 | Ok(()) |
| 353 | } |
| 354 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 355 | fn 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 373 | #[derive(Default, Debug)] |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 374 | struct SerialInfo { |
| 375 | addrs: ArrayVec<[u64; Self::MAX_SERIALS]>, |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | impl SerialInfo { |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 379 | const MAX_SERIALS: usize = 4; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 382 | fn 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 387 | } |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 388 | Ok(SerialInfo { addrs }) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 391 | /// Patch the DT by deleting the ns16550a compatible nodes whose address are unknown |
| 392 | fn 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(¤t) |
| 397 | .reg()? |
| 398 | .ok_or(FdtError::NotFound)? |
| 399 | .next() |
| 400 | .ok_or(FdtError::NotFound)?; |
| 401 | next = if !serial_info.addrs.contains(®.addr) { |
| 402 | current.delete_and_next_compatible(name) |
| 403 | } else { |
| 404 | current.next_compatible(name) |
| 405 | } |
| 406 | } |
| 407 | Ok(()) |
| 408 | } |
| 409 | |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 410 | #[derive(Debug)] |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 411 | struct SwiotlbInfo { |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 412 | size: u64, |
| 413 | align: u64, |
| 414 | } |
| 415 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 416 | fn 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 423 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 424 | fn validate_swiotlb_info(swiotlb_info: &SwiotlbInfo) -> Result<(), RebootReason> { |
| 425 | let size = swiotlb_info.size; |
| 426 | let align = swiotlb_info.align; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 427 | |
| 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 Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 437 | Ok(()) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 440 | fn 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 | |
| 448 | fn 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 | |
| 477 | fn 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 507 | #[derive(Debug)] |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 508 | pub struct DeviceTreeInfo { |
| 509 | pub kernel_range: Option<Range<usize>>, |
| 510 | pub initrd_range: Option<Range<usize>>, |
| 511 | pub memory_range: Range<usize>, |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 512 | bootargs: Option<CString>, |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 513 | num_cpus: usize, |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 514 | pci_info: PciInfo, |
| 515 | serial_info: SerialInfo, |
| 516 | swiotlb_info: SwiotlbInfo, |
| 517 | } |
| 518 | |
| 519 | impl DeviceTreeInfo { |
| 520 | const RAM_BASE_ADDR: u64 = 0x8000_0000; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 521 | const GIC_REDIST_SIZE_PER_CPU: u64 = (32 * SIZE_4KB) as u64; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 524 | pub fn sanitize_device_tree(fdt: &mut Fdt) -> Result<DeviceTreeInfo, RebootReason> { |
Jiyong Park | 8331612 | 2023-03-21 09:39:39 +0900 | [diff] [blame] | 525 | let info = parse_device_tree(fdt)?; |
| 526 | debug!("Device tree info: {:?}", info); |
| 527 | |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 528 | 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 Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 533 | patch_device_tree(fdt, &info)?; |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 534 | Ok(info) |
Jiyong Park | 8331612 | 2023-03-21 09:39:39 +0900 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | fn parse_device_tree(fdt: &libfdt::Fdt) -> Result<DeviceTreeInfo, RebootReason> { |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 538 | 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 Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 554 | let bootargs = read_bootargs_from(fdt).map_err(|e| { |
| 555 | error!("Failed to read bootargs from DT: {e}"); |
| 556 | RebootReason::InvalidFdt |
| 557 | })?; |
| 558 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 559 | 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 Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 582 | Ok(DeviceTreeInfo { |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 583 | kernel_range, |
| 584 | initrd_range, |
| 585 | memory_range, |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 586 | bootargs, |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 587 | num_cpus, |
| 588 | pci_info, |
| 589 | serial_info, |
| 590 | swiotlb_info, |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 591 | }) |
| 592 | } |
| 593 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 594 | fn patch_device_tree(fdt: &mut Fdt, info: &DeviceTreeInfo) -> Result<(), RebootReason> { |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 595 | fdt.unpack().map_err(|e| { |
| 596 | error!("Failed to unpack DT for patching: {e}"); |
| 597 | RebootReason::InvalidFdt |
| 598 | })?; |
| 599 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 600 | 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 Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 610 | 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 Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 616 | 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 Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 640 | |
| 641 | fdt.pack().map_err(|e| { |
| 642 | error!("Failed to pack DT after patching: {e}"); |
| 643 | RebootReason::InvalidFdt |
| 644 | })?; |
| 645 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 646 | Ok(()) |
| 647 | } |
| 648 | |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 649 | /// Modifies the input DT according to the fields of the configuration. |
| 650 | pub 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 Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 656 | fdt.unpack()?; |
| 657 | |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 658 | patch_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?; |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 659 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 660 | 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 Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 662 | |
| 663 | fdt.pack()?; |
| 664 | |
| 665 | Ok(()) |
| 666 | } |
| 667 | |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 668 | /// Patch the "google,open-dice"-compatible reserved-memory node to point to the bcc range |
| 669 | fn patch_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()> { |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 670 | // 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 Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 672 | let node = fdt.node_mut(cstr!("/reserved-memory"))?.ok_or(libfdt::FdtError::NotFound)?; |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 673 | |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 674 | let mut node = node.next_compatible(cstr!("google,open-dice"))?.ok_or(FdtError::NotFound)?; |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 675 | |
Jiyong Park | e9d87e8 | 2023-03-21 19:28:40 +0900 | [diff] [blame^] | 676 | 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 Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | fn 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 Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 692 | |
| 693 | Ok(()) |
| 694 | } |