blob: 437ac4793bf3a57d9e6efdefb8e68f33c63b1697 [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 Parkc5d2ef22023-04-11 01:23:46 +090017use crate::bootargs::BootArgsIterator;
Jaewan Kim50246682024-03-11 23:18:54 +090018use crate::device_assignment::{self, DeviceAssignmentInfo, VmDtbo};
Jiyong Parkc5d2ef22023-04-11 01:23:46 +090019use crate::Box;
Jiyong Park00ceff32023-03-13 05:43:23 +000020use crate::RebootReason;
Seungjae Yoo013f4c42024-01-02 13:04:19 +090021use alloc::collections::BTreeMap;
Jiyong Parke9d87e82023-03-21 19:28:40 +090022use alloc::ffi::CString;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +000023use alloc::format;
Jiyong Parkc23426b2023-04-10 17:32:27 +090024use alloc::vec::Vec;
Jiyong Park0ee65392023-03-27 20:52:45 +090025use core::cmp::max;
26use core::cmp::min;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000027use core::ffi::CStr;
Alice Wangabc7d632023-06-14 09:10:14 +000028use core::fmt;
Jiyong Park9c63cd12023-03-21 17:53:07 +090029use core::mem::size_of;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000030use core::ops::Range;
Per Larsen7ec45d32024-11-02 00:56:46 +000031use hypervisor_backends::get_device_assigner;
32use hypervisor_backends::get_mem_sharer;
Jiyong Park00ceff32023-03-13 05:43:23 +000033use libfdt::AddressRange;
34use libfdt::CellIterator;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +000035use libfdt::Fdt;
36use libfdt::FdtError;
David Dai9bdb10c2024-02-01 22:42:54 -080037use libfdt::FdtNode;
Alice Wang56ec45b2023-06-15 08:30:32 +000038use libfdt::FdtNodeMut;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +000039use libfdt::Phandle;
Jiyong Park83316122023-03-21 09:39:39 +090040use log::debug;
Jiyong Park00ceff32023-03-13 05:43:23 +000041use log::error;
Jiyong Parkc23426b2023-04-10 17:32:27 +090042use log::info;
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +000043use log::warn;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +000044use static_assertions::const_assert;
Jiyong Park00ceff32023-03-13 05:43:23 +000045use tinyvec::ArrayVec;
Pierre-Clément Tosif2c19d42024-10-01 17:42:04 +010046use vmbase::fdt::pci::PciMemoryFlags;
47use vmbase::fdt::pci::PciRangeType;
Alice Wanga3971062023-06-13 11:48:53 +000048use vmbase::fdt::SwiotlbInfo;
Alice Wang63f4c9e2023-06-12 09:36:43 +000049use vmbase::layout::{crosvm::MEM_START, MAX_VIRT_ADDR};
Alice Wangeacb7382023-06-05 12:53:54 +000050use vmbase::memory::SIZE_4KB;
Alice Wang4be4dd02023-06-07 07:50:40 +000051use vmbase::util::RangeExt as _;
Andrew Walbran47d316e2024-11-28 18:41:09 +000052use zerocopy::IntoBytes as _;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000053
Pierre-Clément Tosi84ba1a82024-10-30 11:27:32 +000054// SAFETY: The template DT is automatically generated through DTC, which should produce valid DTBs.
55const FDT_TEMPLATE: &Fdt = unsafe { Fdt::unchecked_from_slice(pvmfw_fdt_template::RAW) };
56
Alice Wangabc7d632023-06-14 09:10:14 +000057/// An enumeration of errors that can occur during the FDT validation.
58#[derive(Clone, Debug)]
59pub enum FdtValidationError {
60 /// Invalid CPU count.
61 InvalidCpuCount(usize),
David Dai9bdb10c2024-02-01 22:42:54 -080062 /// Invalid VCpufreq Range.
63 InvalidVcpufreq(u64, u64),
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +000064 /// Forbidden /avf/untrusted property.
65 ForbiddenUntrustedProp(&'static CStr),
Alice Wangabc7d632023-06-14 09:10:14 +000066}
67
68impl fmt::Display for FdtValidationError {
69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70 match self {
71 Self::InvalidCpuCount(num_cpus) => write!(f, "Invalid CPU count: {num_cpus}"),
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +000072 Self::InvalidVcpufreq(addr, size) => {
73 write!(f, "Invalid vcpufreq region: ({addr:#x}, {size:#x})")
74 }
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +000075 Self::ForbiddenUntrustedProp(name) => {
76 write!(f, "Forbidden /avf/untrusted property '{name:?}'")
77 }
Alice Wangabc7d632023-06-14 09:10:14 +000078 }
79 }
80}
81
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +000082/// For non-standardly sized integer properties, not following <#size-cells> or <#address-cells>.
83#[derive(Copy, Clone, Debug, Eq, PartialEq)]
84enum DeviceTreeInteger {
85 SingleCell(u32),
86 DoubleCell(u64),
87}
88
89impl DeviceTreeInteger {
90 fn read_from(node: &FdtNode, name: &CStr) -> libfdt::Result<Option<Self>> {
91 if let Some(bytes) = node.getprop(name)? {
92 Ok(Some(Self::from_bytes(bytes).ok_or(FdtError::BadValue)?))
93 } else {
94 Ok(None)
95 }
96 }
97
98 fn from_bytes(bytes: &[u8]) -> Option<Self> {
99 if let Some(val) = bytes.try_into().ok().map(u32::from_be_bytes) {
100 return Some(Self::SingleCell(val));
101 } else if let Some(val) = bytes.try_into().ok().map(u64::from_be_bytes) {
102 return Some(Self::DoubleCell(val));
103 }
104 None
105 }
106
107 fn write_to(&self, node: &mut FdtNodeMut, name: &CStr) -> libfdt::Result<()> {
108 match self {
109 Self::SingleCell(value) => node.setprop(name, &value.to_be_bytes()),
110 Self::DoubleCell(value) => node.setprop(name, &value.to_be_bytes()),
111 }
112 }
113}
114
115impl From<DeviceTreeInteger> for usize {
116 fn from(i: DeviceTreeInteger) -> Self {
117 match i {
118 DeviceTreeInteger::SingleCell(v) => v.try_into().unwrap(),
119 DeviceTreeInteger::DoubleCell(v) => v.try_into().unwrap(),
120 }
121 }
122}
123
124/// Returns the pair or integers or an error if only one value is present.
125fn read_two_ints(
126 node: &FdtNode,
127 name_a: &CStr,
128 name_b: &CStr,
129) -> libfdt::Result<Option<(DeviceTreeInteger, DeviceTreeInteger)>> {
130 let a = DeviceTreeInteger::read_from(node, name_a)?;
131 let b = DeviceTreeInteger::read_from(node, name_b)?;
132
133 match (a, b) {
134 (Some(a), Some(b)) => Ok(Some((a, b))),
135 (None, None) => Ok(None),
136 _ => Err(FdtError::NotFound),
137 }
138}
139
Pierre-Clément Tosiec368b72025-02-07 11:51:44 +0000140/// Extract from /config the address range containing the pre-loaded kernel.
141///
142/// Absence of /config is not an error. However, an error is returned if only one of the two
143/// properties is present.
Pierre-Clément Tosi0d4c09b2024-11-19 17:32:15 +0000144pub fn read_kernel_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +0000145 if let Some(ref config) = fdt.node(c"/config")? {
146 if let Some((addr, size)) = read_two_ints(config, c"kernel-address", c"kernel-size")? {
147 let addr = usize::from(addr);
148 let size = usize::from(size);
149 return Ok(Some(addr..(addr + size)));
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +0000150 }
151 }
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +0000152 Ok(None)
153}
154
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +0000155fn read_initrd_range_props(
156 fdt: &Fdt,
157) -> libfdt::Result<Option<(DeviceTreeInteger, DeviceTreeInteger)>> {
158 if let Some(ref chosen) = fdt.chosen()? {
159 read_two_ints(chosen, c"linux,initrd-start", c"linux,initrd-end")
160 } else {
161 Ok(None)
162 }
163}
164
Pierre-Clément Tosiec368b72025-02-07 11:51:44 +0000165/// Extract from /chosen the address range containing the pre-loaded ramdisk.
166///
167/// Absence is not an error as there can be initrd-less VM. However, an error is returned if only
168/// one of the two properties is present.
Pierre-Clément Tosi0d4c09b2024-11-19 17:32:15 +0000169pub fn read_initrd_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +0000170 if let Some((start, end)) = read_initrd_range_props(fdt)? {
171 Ok(Some(usize::from(start)..usize::from(end)))
172 } else {
173 Ok(None)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000174 }
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000175}
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000176
Pierre-Clément Tosi3729f652024-11-19 15:25:37 +0000177/// Read /avf/untrusted/instance-id, if present.
178pub fn read_instance_id(fdt: &Fdt) -> libfdt::Result<Option<&[u8]>> {
179 read_avf_untrusted_prop(fdt, c"instance-id")
180}
181
182/// Read /avf/untrusted/defer-rollback-protection, if present.
183pub fn read_defer_rollback_protection(fdt: &Fdt) -> libfdt::Result<Option<&[u8]>> {
184 read_avf_untrusted_prop(fdt, c"defer-rollback-protection")
185}
186
187fn read_avf_untrusted_prop<'a>(fdt: &'a Fdt, prop: &CStr) -> libfdt::Result<Option<&'a [u8]>> {
188 if let Some(node) = fdt.node(c"/avf/untrusted")? {
189 node.getprop(prop)
190 } else {
191 Ok(None)
192 }
193}
194
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +0000195fn patch_initrd_range(
196 fdt: &mut Fdt,
197 start: &DeviceTreeInteger,
198 end: &DeviceTreeInteger,
199) -> libfdt::Result<()> {
Jiyong Park9c63cd12023-03-21 17:53:07 +0900200 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +0000201 start.write_to(&mut node, c"linux,initrd-start")?;
202 end.write_to(&mut node, c"linux,initrd-end")?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900203 Ok(())
204}
205
Jiyong Parke9d87e82023-03-21 19:28:40 +0900206fn read_bootargs_from(fdt: &Fdt) -> libfdt::Result<Option<CString>> {
207 if let Some(chosen) = fdt.chosen()? {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000208 if let Some(bootargs) = chosen.getprop_str(c"bootargs")? {
Jiyong Parke9d87e82023-03-21 19:28:40 +0900209 // We need to copy the string to heap because the original fdt will be invalidated
210 // by the templated DT
211 let copy = CString::new(bootargs.to_bytes()).map_err(|_| FdtError::BadValue)?;
212 return Ok(Some(copy));
213 }
214 }
215 Ok(None)
216}
217
218fn patch_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()> {
219 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
Jiyong Parkc5d2ef22023-04-11 01:23:46 +0900220 // This function is called before the verification is done. So, we just copy the bootargs to
221 // the new FDT unmodified. This will be filtered again in the modify_for_next_stage function
222 // if the VM is not debuggable.
Alan Stokesf46a17c2025-01-05 15:50:18 +0000223 node.setprop(c"bootargs", bootargs.to_bytes_with_nul())
Jiyong Parke9d87e82023-03-21 19:28:40 +0900224}
225
Alice Wang0d527472023-06-13 14:55:38 +0000226/// Reads and validates the memory range in the DT.
227///
228/// Only one memory range is expected with the crosvm setup for now.
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +0000229fn read_and_validate_memory_range(
230 fdt: &Fdt,
231 guest_page_size: usize,
232) -> Result<Range<usize>, RebootReason> {
Alice Wang0d527472023-06-13 14:55:38 +0000233 let mut memory = fdt.memory().map_err(|e| {
234 error!("Failed to read memory range from DT: {e}");
235 RebootReason::InvalidFdt
236 })?;
237 let range = memory.next().ok_or_else(|| {
238 error!("The /memory node in the DT contains no range.");
239 RebootReason::InvalidFdt
240 })?;
241 if memory.next().is_some() {
242 warn!(
243 "The /memory node in the DT contains more than one memory range, \
244 while only one is expected."
245 );
246 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900247 let base = range.start;
Alice Wange243d462023-06-06 15:18:12 +0000248 if base != MEM_START {
249 error!("Memory base address {:#x} is not {:#x}", base, MEM_START);
Jiyong Park00ceff32023-03-13 05:43:23 +0000250 return Err(RebootReason::InvalidFdt);
251 }
252
Jiyong Park6a8789a2023-03-21 14:50:59 +0900253 let size = range.len();
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +0000254 if size % guest_page_size != 0 {
255 error!("Memory size {:#x} is not a multiple of page size {:#x}", size, guest_page_size);
Jiyong Park00ceff32023-03-13 05:43:23 +0000256 return Err(RebootReason::InvalidFdt);
257 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000258
Jiyong Park6a8789a2023-03-21 14:50:59 +0900259 if size == 0 {
260 error!("Memory size is 0");
261 return Err(RebootReason::InvalidFdt);
262 }
Alice Wang0d527472023-06-13 14:55:38 +0000263 Ok(range)
Jiyong Park00ceff32023-03-13 05:43:23 +0000264}
265
Jiyong Park9c63cd12023-03-21 17:53:07 +0900266fn patch_memory_range(fdt: &mut Fdt, memory_range: &Range<usize>) -> libfdt::Result<()> {
Pierre-Clément Tosi0edc4d62024-02-05 14:13:53 +0000267 let addr = u64::try_from(MEM_START).unwrap();
268 let size = u64::try_from(memory_range.len()).unwrap();
Alan Stokesf46a17c2025-01-05 15:50:18 +0000269 fdt.node_mut(c"/memory")?
Jiyong Park0ee65392023-03-27 20:52:45 +0900270 .ok_or(FdtError::NotFound)?
Alan Stokesf46a17c2025-01-05 15:50:18 +0000271 .setprop_inplace(c"reg", [addr.to_be(), size.to_be()].as_bytes())
Jiyong Park9c63cd12023-03-21 17:53:07 +0900272}
273
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000274#[derive(Debug, Default)]
David Dai9bdb10c2024-02-01 22:42:54 -0800275struct CpuInfo {
276 opptable_info: Option<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>,
David Dai50168a32024-02-14 17:00:48 -0800277 cpu_capacity: Option<u32>,
David Dai9bdb10c2024-02-01 22:42:54 -0800278}
279
280impl CpuInfo {
David Dai622c05d2024-02-14 14:03:26 -0800281 const MAX_OPPTABLES: usize = 20;
David Dai9bdb10c2024-02-01 22:42:54 -0800282}
283
284fn read_opp_info_from(
285 opp_node: FdtNode,
286) -> libfdt::Result<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>> {
287 let mut table = ArrayVec::new();
Pierre-Clément Tosidf272a52024-04-15 16:07:58 +0100288 let mut opp_nodes = opp_node.subnodes()?;
289 for subnode in opp_nodes.by_ref().take(table.capacity()) {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000290 let prop = subnode.getprop_u64(c"opp-hz")?.ok_or(FdtError::NotFound)?;
David Dai9bdb10c2024-02-01 22:42:54 -0800291 table.push(prop);
292 }
293
Pierre-Clément Tosidf272a52024-04-15 16:07:58 +0100294 if opp_nodes.next().is_some() {
295 warn!("OPP table has more than {} entries: discarding extra nodes.", table.capacity());
296 }
297
David Dai9bdb10c2024-02-01 22:42:54 -0800298 Ok(table)
299}
Jiyong Park6a8789a2023-03-21 14:50:59 +0900300
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000301#[derive(Debug, Default)]
302struct ClusterTopology {
303 // TODO: Support multi-level clusters & threads.
304 cores: [Option<usize>; ClusterTopology::MAX_CORES_PER_CLUSTER],
305}
306
307impl ClusterTopology {
David Daib19fd082024-04-19 16:33:26 -0700308 const MAX_CORES_PER_CLUSTER: usize = 10;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000309}
310
311#[derive(Debug, Default)]
312struct CpuTopology {
313 // TODO: Support sockets.
314 clusters: [Option<ClusterTopology>; CpuTopology::MAX_CLUSTERS],
315}
316
317impl CpuTopology {
318 const MAX_CLUSTERS: usize = 3;
319}
320
321fn read_cpu_map_from(fdt: &Fdt) -> libfdt::Result<Option<BTreeMap<Phandle, (usize, usize)>>> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000322 let Some(cpu_map) = fdt.node(c"/cpus/cpu-map")? else {
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000323 return Ok(None);
324 };
325
326 let mut topology = BTreeMap::new();
327 for n in 0..CpuTopology::MAX_CLUSTERS {
328 let name = CString::new(format!("cluster{n}")).unwrap();
329 let Some(cluster) = cpu_map.subnode(&name)? else {
330 break;
331 };
332 for m in 0..ClusterTopology::MAX_CORES_PER_CLUSTER {
David Dai8f476cb2024-02-15 21:57:01 -0800333 let name = CString::new(format!("core{m}")).unwrap();
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000334 let Some(core) = cluster.subnode(&name)? else {
335 break;
336 };
Alan Stokesf46a17c2025-01-05 15:50:18 +0000337 let cpu = core.getprop_u32(c"cpu")?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000338 let prev = topology.insert(cpu.try_into()?, (n, m));
339 if prev.is_some() {
340 return Err(FdtError::BadValue);
341 }
342 }
343 }
344
345 Ok(Some(topology))
346}
347
348fn read_cpu_info_from(
349 fdt: &Fdt,
350) -> libfdt::Result<(ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>, Option<CpuTopology>)> {
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000351 let mut cpus = ArrayVec::new();
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000352
353 let cpu_map = read_cpu_map_from(fdt)?;
354 let mut topology: CpuTopology = Default::default();
355
Alan Stokesf46a17c2025-01-05 15:50:18 +0000356 let mut cpu_nodes = fdt.compatible_nodes(c"arm,armv8")?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000357 for (idx, cpu) in cpu_nodes.by_ref().take(cpus.capacity()).enumerate() {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000358 let cpu_capacity = cpu.getprop_u32(c"capacity-dmips-mhz")?;
359 let opp_phandle = cpu.getprop_u32(c"operating-points-v2")?;
David Dai9bdb10c2024-02-01 22:42:54 -0800360 let opptable_info = if let Some(phandle) = opp_phandle {
361 let phandle = phandle.try_into()?;
362 let node = fdt.node_with_phandle(phandle)?.ok_or(FdtError::NotFound)?;
363 Some(read_opp_info_from(node)?)
364 } else {
365 None
366 };
David Dai50168a32024-02-14 17:00:48 -0800367 let info = CpuInfo { opptable_info, cpu_capacity };
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000368 cpus.push(info);
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000369
370 if let Some(ref cpu_map) = cpu_map {
371 let phandle = cpu.get_phandle()?.ok_or(FdtError::NotFound)?;
David Dai8f476cb2024-02-15 21:57:01 -0800372 let (cluster, core_idx) = cpu_map.get(&phandle).ok_or(FdtError::BadValue)?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000373 let cluster = topology.clusters[*cluster].get_or_insert(Default::default());
David Dai8f476cb2024-02-15 21:57:01 -0800374 if cluster.cores[*core_idx].is_some() {
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000375 return Err(FdtError::BadValue);
376 }
David Dai8f476cb2024-02-15 21:57:01 -0800377 cluster.cores[*core_idx] = Some(idx);
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000378 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900379 }
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000380
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000381 if cpu_nodes.next().is_some() {
382 warn!("DT has more than {} CPU nodes: discarding extra nodes.", cpus.capacity());
383 }
384
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000385 Ok((cpus, cpu_map.map(|_| topology)))
Jiyong Park9c63cd12023-03-21 17:53:07 +0900386}
387
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000388fn validate_cpu_info(cpus: &[CpuInfo]) -> Result<(), FdtValidationError> {
389 if cpus.is_empty() {
390 return Err(FdtValidationError::InvalidCpuCount(0));
391 }
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000392 Ok(())
393}
394
David Dai9bdb10c2024-02-01 22:42:54 -0800395fn read_vcpufreq_info(fdt: &Fdt) -> libfdt::Result<Option<VcpufreqInfo>> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000396 let mut nodes = fdt.compatible_nodes(c"virtual,android-v-only-cpufreq")?;
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000397 let Some(node) = nodes.next() else {
398 return Ok(None);
David Dai9bdb10c2024-02-01 22:42:54 -0800399 };
400
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000401 if nodes.next().is_some() {
402 warn!("DT has more than 1 cpufreq node: discarding extra nodes.");
403 }
404
405 let mut regs = node.reg()?.ok_or(FdtError::NotFound)?;
406 let reg = regs.next().ok_or(FdtError::NotFound)?;
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000407 let size = reg.size.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000408
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000409 Ok(Some(VcpufreqInfo { addr: reg.addr, size }))
David Dai9bdb10c2024-02-01 22:42:54 -0800410}
411
412fn validate_vcpufreq_info(
413 vcpufreq_info: &VcpufreqInfo,
414 cpus: &[CpuInfo],
415) -> Result<(), FdtValidationError> {
416 const VCPUFREQ_BASE_ADDR: u64 = 0x1040000;
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000417 const VCPUFREQ_SIZE_PER_CPU: u64 = 0x8;
David Dai9bdb10c2024-02-01 22:42:54 -0800418
419 let base = vcpufreq_info.addr;
420 let size = vcpufreq_info.size;
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000421 let expected_size = VCPUFREQ_SIZE_PER_CPU * cpus.len() as u64;
422
423 if (base, size) != (VCPUFREQ_BASE_ADDR, expected_size) {
David Dai9bdb10c2024-02-01 22:42:54 -0800424 return Err(FdtValidationError::InvalidVcpufreq(base, size));
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000425 }
David Dai9bdb10c2024-02-01 22:42:54 -0800426
427 Ok(())
428}
429
430fn patch_opptable(
431 node: FdtNodeMut,
David Dai622c05d2024-02-14 14:03:26 -0800432 opptable: Option<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>,
David Dai9bdb10c2024-02-01 22:42:54 -0800433) -> libfdt::Result<()> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000434 let oppcompat = c"operating-points-v2";
David Dai9bdb10c2024-02-01 22:42:54 -0800435 let next = node.next_compatible(oppcompat)?.ok_or(FdtError::NoSpace)?;
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000436
437 let Some(opptable) = opptable else {
438 return next.nop();
439 };
440
David Dai9bdb10c2024-02-01 22:42:54 -0800441 let mut next_subnode = next.first_subnode()?;
442
443 for entry in opptable {
444 let mut subnode = next_subnode.ok_or(FdtError::NoSpace)?;
Alan Stokesf46a17c2025-01-05 15:50:18 +0000445 subnode.setprop_inplace(c"opp-hz", &entry.to_be_bytes())?;
David Dai9bdb10c2024-02-01 22:42:54 -0800446 next_subnode = subnode.next_subnode()?;
447 }
448
449 while let Some(current) = next_subnode {
450 next_subnode = current.delete_and_next_subnode()?;
451 }
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000452
David Dai9bdb10c2024-02-01 22:42:54 -0800453 Ok(())
454}
455
456// TODO(ptosi): Rework FdtNodeMut and replace this function.
457fn get_nth_compatible<'a>(
458 fdt: &'a mut Fdt,
459 n: usize,
460 compat: &CStr,
461) -> libfdt::Result<Option<FdtNodeMut<'a>>> {
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000462 let mut node = fdt.root_mut().next_compatible(compat)?;
David Dai9bdb10c2024-02-01 22:42:54 -0800463 for _ in 0..n {
464 node = node.ok_or(FdtError::NoSpace)?.next_compatible(compat)?;
465 }
466 Ok(node)
467}
468
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000469fn patch_cpus(
470 fdt: &mut Fdt,
471 cpus: &[CpuInfo],
472 topology: &Option<CpuTopology>,
473) -> libfdt::Result<()> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000474 const COMPAT: &CStr = c"arm,armv8";
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000475 let mut cpu_phandles = Vec::new();
David Dai9bdb10c2024-02-01 22:42:54 -0800476 for (idx, cpu) in cpus.iter().enumerate() {
David Dai50168a32024-02-14 17:00:48 -0800477 let mut cur = get_nth_compatible(fdt, idx, COMPAT)?.ok_or(FdtError::NoSpace)?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000478 let phandle = cur.as_node().get_phandle()?.unwrap();
479 cpu_phandles.push(phandle);
David Dai50168a32024-02-14 17:00:48 -0800480 if let Some(cpu_capacity) = cpu.cpu_capacity {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000481 cur.setprop_inplace(c"capacity-dmips-mhz", &cpu_capacity.to_be_bytes())?;
David Dai50168a32024-02-14 17:00:48 -0800482 }
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000483 patch_opptable(cur, cpu.opptable_info)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900484 }
David Dai9bdb10c2024-02-01 22:42:54 -0800485 let mut next = get_nth_compatible(fdt, cpus.len(), COMPAT)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900486 while let Some(current) = next {
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000487 next = current.delete_and_next_compatible(COMPAT)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900488 }
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000489
490 if let Some(topology) = topology {
491 for (n, cluster) in topology.clusters.iter().enumerate() {
492 let path = CString::new(format!("/cpus/cpu-map/cluster{n}")).unwrap();
493 let cluster_node = fdt.node_mut(&path)?.unwrap();
494 if let Some(cluster) = cluster {
495 let mut iter = cluster_node.first_subnode()?;
496 for core in cluster.cores {
497 let mut core_node = iter.unwrap();
498 iter = if let Some(core_idx) = core {
499 let phandle = *cpu_phandles.get(core_idx).unwrap();
500 let value = u32::from(phandle).to_be_bytes();
Alan Stokesf46a17c2025-01-05 15:50:18 +0000501 core_node.setprop_inplace(c"cpu", &value)?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000502 core_node.next_subnode()?
503 } else {
504 core_node.delete_and_next_subnode()?
505 };
506 }
507 assert!(iter.is_none());
508 } else {
509 cluster_node.nop()?;
510 }
511 }
512 } else {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000513 fdt.node_mut(c"/cpus/cpu-map")?.unwrap().nop()?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000514 }
515
Jiyong Park6a8789a2023-03-21 14:50:59 +0900516 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000517}
518
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +0000519/// Reads the /avf/untrusted DT node, which the host can use to pass properties (no subnodes) to
520/// the guest that don't require being validated by pvmfw.
521fn parse_untrusted_props(fdt: &Fdt) -> libfdt::Result<BTreeMap<CString, Vec<u8>>> {
522 let mut props = BTreeMap::new();
Alan Stokesf46a17c2025-01-05 15:50:18 +0000523 if let Some(node) = fdt.node(c"/avf/untrusted")? {
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +0000524 for property in node.properties()? {
525 let name = property.name()?;
526 let value = property.value()?;
527 props.insert(CString::from(name), value.to_vec());
528 }
529 if node.subnodes()?.next().is_some() {
530 warn!("Discarding unexpected /avf/untrusted subnodes.");
531 }
532 }
533
534 Ok(props)
535}
536
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900537/// Read candidate properties' names from DT which could be overlaid
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900538fn parse_vm_ref_dt(fdt: &Fdt) -> libfdt::Result<BTreeMap<CString, Vec<u8>>> {
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900539 let mut property_map = BTreeMap::new();
Alan Stokesf46a17c2025-01-05 15:50:18 +0000540 if let Some(avf_node) = fdt.node(c"/avf")? {
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900541 for property in avf_node.properties()? {
542 let name = property.name()?;
543 let value = property.value()?;
544 property_map.insert(
545 CString::new(name.to_bytes()).map_err(|_| FdtError::BadValue)?,
546 value.to_vec(),
547 );
Seungjae Yooed67fd52023-11-29 18:54:36 +0900548 }
549 }
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900550 Ok(property_map)
Seungjae Yooed67fd52023-11-29 18:54:36 +0900551}
552
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +0000553fn validate_untrusted_props(props: &BTreeMap<CString, Vec<u8>>) -> Result<(), FdtValidationError> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000554 const FORBIDDEN_PROPS: &[&CStr] = &[c"compatible", c"linux,phandle", c"phandle"];
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +0000555
556 for name in FORBIDDEN_PROPS {
557 if props.contains_key(*name) {
558 return Err(FdtValidationError::ForbiddenUntrustedProp(name));
559 }
560 }
561
562 Ok(())
563}
564
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900565/// Overlay VM reference DT into VM DT based on the props_info. Property is overlaid in vm_dt only
566/// when it exists both in vm_ref_dt and props_info. If the values mismatch, it returns error.
567fn validate_vm_ref_dt(
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900568 vm_dt: &mut Fdt,
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900569 vm_ref_dt: &Fdt,
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900570 props_info: &BTreeMap<CString, Vec<u8>>,
Seungjae Yoo192e99c2023-12-15 16:42:39 +0900571) -> libfdt::Result<()> {
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000572 let root_vm_dt = vm_dt.root_mut();
Alan Stokesf46a17c2025-01-05 15:50:18 +0000573 let mut avf_vm_dt = root_vm_dt.add_subnode(c"avf")?;
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900574 // TODO(b/318431677): Validate nodes beyond /avf.
Alan Stokesf46a17c2025-01-05 15:50:18 +0000575 let avf_node = vm_ref_dt.node(c"/avf")?.ok_or(FdtError::NotFound)?;
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900576 for (name, value) in props_info.iter() {
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900577 if let Some(ref_value) = avf_node.getprop(name)? {
578 if value != ref_value {
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900579 error!(
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900580 "Property mismatches while applying overlay VM reference DT. \
581 Name:{:?}, Value from host as hex:{:x?}, Value from VM reference DT as hex:{:x?}",
582 name, value, ref_value
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900583 );
584 return Err(FdtError::BadValue);
585 }
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900586 avf_vm_dt.setprop(name, ref_value)?;
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900587 }
588 }
Seungjae Yooed67fd52023-11-29 18:54:36 +0900589 Ok(())
590}
591
Jiyong Park00ceff32023-03-13 05:43:23 +0000592#[derive(Debug)]
Jiyong Park00ceff32023-03-13 05:43:23 +0000593struct PciInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900594 ranges: [PciAddrRange; 2],
595 irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]>,
596 irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000597}
598
Jiyong Park6a8789a2023-03-21 14:50:59 +0900599impl PciInfo {
600 const IRQ_MASK_CELLS: usize = 4;
601 const IRQ_MAP_CELLS: usize = 10;
Nikita Ioffe2d0969c2024-06-06 12:59:12 +0000602 const MAX_IRQS: usize = 16;
Jiyong Park00ceff32023-03-13 05:43:23 +0000603}
604
Jiyong Park6a8789a2023-03-21 14:50:59 +0900605type PciAddrRange = AddressRange<(u32, u64), u64, u64>;
606type PciIrqMask = [u32; PciInfo::IRQ_MASK_CELLS];
607type PciIrqMap = [u32; PciInfo::IRQ_MAP_CELLS];
Jiyong Park00ceff32023-03-13 05:43:23 +0000608
609/// Iterator that takes N cells as a chunk
610struct CellChunkIterator<'a, const N: usize> {
611 cells: CellIterator<'a>,
612}
613
614impl<'a, const N: usize> CellChunkIterator<'a, N> {
615 fn new(cells: CellIterator<'a>) -> Self {
616 Self { cells }
617 }
618}
619
Chris Wailes52358e92025-01-27 17:04:40 -0800620impl<const N: usize> Iterator for CellChunkIterator<'_, N> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000621 type Item = [u32; N];
622 fn next(&mut self) -> Option<Self::Item> {
623 let mut ret: Self::Item = [0; N];
624 for i in ret.iter_mut() {
625 *i = self.cells.next()?;
626 }
627 Some(ret)
628 }
629}
630
Jiyong Park6a8789a2023-03-21 14:50:59 +0900631/// Read pci host controller ranges, irq maps, and irq map masks from DT
632fn read_pci_info_from(fdt: &Fdt) -> libfdt::Result<PciInfo> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000633 let node = fdt.compatible_nodes(c"pci-host-cam-generic")?.next().ok_or(FdtError::NotFound)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900634
635 let mut ranges = node.ranges::<(u32, u64), u64, u64>()?.ok_or(FdtError::NotFound)?;
636 let range0 = ranges.next().ok_or(FdtError::NotFound)?;
637 let range1 = ranges.next().ok_or(FdtError::NotFound)?;
638
Alan Stokesf46a17c2025-01-05 15:50:18 +0000639 let irq_masks = node.getprop_cells(c"interrupt-map-mask")?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosiaa0f6552023-07-12 14:49:35 +0000640 let mut chunks = CellChunkIterator::<{ PciInfo::IRQ_MASK_CELLS }>::new(irq_masks);
641 let irq_masks = (&mut chunks).take(PciInfo::MAX_IRQS).collect();
642
643 if chunks.next().is_some() {
644 warn!("Input DT has more than {} PCI entries!", PciInfo::MAX_IRQS);
645 return Err(FdtError::NoSpace);
646 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900647
Alan Stokesf46a17c2025-01-05 15:50:18 +0000648 let irq_maps = node.getprop_cells(c"interrupt-map")?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosiaa0f6552023-07-12 14:49:35 +0000649 let mut chunks = CellChunkIterator::<{ PciInfo::IRQ_MAP_CELLS }>::new(irq_maps);
650 let irq_maps = (&mut chunks).take(PciInfo::MAX_IRQS).collect();
651
652 if chunks.next().is_some() {
653 warn!("Input DT has more than {} PCI entries!", PciInfo::MAX_IRQS);
654 return Err(FdtError::NoSpace);
655 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900656
657 Ok(PciInfo { ranges: [range0, range1], irq_masks, irq_maps })
658}
659
Jiyong Park0ee65392023-03-27 20:52:45 +0900660fn validate_pci_info(pci_info: &PciInfo, memory_range: &Range<usize>) -> Result<(), RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900661 for range in pci_info.ranges.iter() {
Jiyong Park0ee65392023-03-27 20:52:45 +0900662 validate_pci_addr_range(range, memory_range)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900663 }
664 for irq_mask in pci_info.irq_masks.iter() {
665 validate_pci_irq_mask(irq_mask)?;
666 }
667 for (idx, irq_map) in pci_info.irq_maps.iter().enumerate() {
668 validate_pci_irq_map(irq_map, idx)?;
669 }
670 Ok(())
671}
672
Jiyong Park0ee65392023-03-27 20:52:45 +0900673fn validate_pci_addr_range(
674 range: &PciAddrRange,
675 memory_range: &Range<usize>,
676) -> Result<(), RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900677 let mem_flags = PciMemoryFlags(range.addr.0);
678 let range_type = mem_flags.range_type();
Jiyong Park6a8789a2023-03-21 14:50:59 +0900679 let bus_addr = range.addr.1;
680 let cpu_addr = range.parent_addr;
681 let size = range.size;
682
683 if range_type != PciRangeType::Memory64 {
684 error!("Invalid range type {:?} for bus address {:#x} in PCI node", range_type, bus_addr);
685 return Err(RebootReason::InvalidFdt);
686 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900687 // Enforce ID bus-to-cpu mappings, as used by crosvm.
688 if bus_addr != cpu_addr {
689 error!("PCI bus address: {:#x} is different from CPU address: {:#x}", bus_addr, cpu_addr);
690 return Err(RebootReason::InvalidFdt);
691 }
692
Jiyong Park0ee65392023-03-27 20:52:45 +0900693 let Some(bus_end) = bus_addr.checked_add(size) else {
694 error!("PCI address range size {:#x} overflows", size);
695 return Err(RebootReason::InvalidFdt);
696 };
Alice Wang63f4c9e2023-06-12 09:36:43 +0000697 if bus_end > MAX_VIRT_ADDR.try_into().unwrap() {
Jiyong Park0ee65392023-03-27 20:52:45 +0900698 error!("PCI address end {:#x} is outside of translatable range", bus_end);
699 return Err(RebootReason::InvalidFdt);
700 }
701
702 let memory_start = memory_range.start.try_into().unwrap();
703 let memory_end = memory_range.end.try_into().unwrap();
704
705 if max(bus_addr, memory_start) < min(bus_end, memory_end) {
706 error!(
707 "PCI address range {:#x}-{:#x} overlaps with main memory range {:#x}-{:#x}",
708 bus_addr, bus_end, memory_start, memory_end
709 );
Jiyong Park6a8789a2023-03-21 14:50:59 +0900710 return Err(RebootReason::InvalidFdt);
711 }
712
713 Ok(())
714}
715
716fn validate_pci_irq_mask(irq_mask: &PciIrqMask) -> Result<(), RebootReason> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000717 const IRQ_MASK_ADDR_HI: u32 = 0xf800;
718 const IRQ_MASK_ADDR_ME: u32 = 0x0;
719 const IRQ_MASK_ADDR_LO: u32 = 0x0;
720 const IRQ_MASK_ANY_IRQ: u32 = 0x7;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900721 const EXPECTED: PciIrqMask =
Jiyong Park00ceff32023-03-13 05:43:23 +0000722 [IRQ_MASK_ADDR_HI, IRQ_MASK_ADDR_ME, IRQ_MASK_ADDR_LO, IRQ_MASK_ANY_IRQ];
Jiyong Park6a8789a2023-03-21 14:50:59 +0900723 if *irq_mask != EXPECTED {
724 error!("Invalid PCI irq mask {:#?}", irq_mask);
725 return Err(RebootReason::InvalidFdt);
Jiyong Park00ceff32023-03-13 05:43:23 +0000726 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900727 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000728}
729
Jiyong Park6a8789a2023-03-21 14:50:59 +0900730fn validate_pci_irq_map(irq_map: &PciIrqMap, idx: usize) -> Result<(), RebootReason> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000731 const PCI_DEVICE_IDX: usize = 11;
732 const PCI_IRQ_ADDR_ME: u32 = 0;
733 const PCI_IRQ_ADDR_LO: u32 = 0;
734 const PCI_IRQ_INTC: u32 = 1;
735 const AARCH64_IRQ_BASE: u32 = 4; // from external/crosvm/aarch64/src/lib.rs
736 const GIC_SPI: u32 = 0;
737 const IRQ_TYPE_LEVEL_HIGH: u32 = 4;
738
Jiyong Park6a8789a2023-03-21 14:50:59 +0900739 let pci_addr = (irq_map[0], irq_map[1], irq_map[2]);
740 let pci_irq_number = irq_map[3];
741 let _controller_phandle = irq_map[4]; // skipped.
742 let gic_addr = (irq_map[5], irq_map[6]); // address-cells is <2> for GIC
743 // interrupt-cells is <3> for GIC
744 let gic_peripheral_interrupt_type = irq_map[7];
745 let gic_irq_number = irq_map[8];
746 let gic_irq_type = irq_map[9];
Jiyong Park00ceff32023-03-13 05:43:23 +0000747
Jiyong Park6a8789a2023-03-21 14:50:59 +0900748 let phys_hi: u32 = (0x1 << PCI_DEVICE_IDX) * (idx + 1) as u32;
749 let expected_pci_addr = (phys_hi, PCI_IRQ_ADDR_ME, PCI_IRQ_ADDR_LO);
Jiyong Park00ceff32023-03-13 05:43:23 +0000750
Jiyong Park6a8789a2023-03-21 14:50:59 +0900751 if pci_addr != expected_pci_addr {
752 error!("PCI device address {:#x} {:#x} {:#x} in interrupt-map is different from expected address \
753 {:#x} {:#x} {:#x}",
754 pci_addr.0, pci_addr.1, pci_addr.2, expected_pci_addr.0, expected_pci_addr.1, expected_pci_addr.2);
755 return Err(RebootReason::InvalidFdt);
756 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000757
Jiyong Park6a8789a2023-03-21 14:50:59 +0900758 if pci_irq_number != PCI_IRQ_INTC {
759 error!(
760 "PCI INT# {:#x} in interrupt-map is different from expected value {:#x}",
761 pci_irq_number, PCI_IRQ_INTC
762 );
763 return Err(RebootReason::InvalidFdt);
764 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000765
Jiyong Park6a8789a2023-03-21 14:50:59 +0900766 if gic_addr != (0, 0) {
767 error!(
768 "GIC address {:#x} {:#x} in interrupt-map is different from expected address \
769 {:#x} {:#x}",
770 gic_addr.0, gic_addr.1, 0, 0
771 );
772 return Err(RebootReason::InvalidFdt);
773 }
774
775 if gic_peripheral_interrupt_type != GIC_SPI {
776 error!("GIC peripheral interrupt type {:#x} in interrupt-map is different from expected value \
777 {:#x}", gic_peripheral_interrupt_type, GIC_SPI);
778 return Err(RebootReason::InvalidFdt);
779 }
780
781 let irq_nr: u32 = AARCH64_IRQ_BASE + (idx as u32);
782 if gic_irq_number != irq_nr {
783 error!(
784 "GIC irq number {:#x} in interrupt-map is unexpected. Expected {:#x}",
785 gic_irq_number, irq_nr
786 );
787 return Err(RebootReason::InvalidFdt);
788 }
789
790 if gic_irq_type != IRQ_TYPE_LEVEL_HIGH {
791 error!(
792 "IRQ type in {:#x} is invalid. Must be LEVEL_HIGH {:#x}",
793 gic_irq_type, IRQ_TYPE_LEVEL_HIGH
794 );
795 return Err(RebootReason::InvalidFdt);
Jiyong Park00ceff32023-03-13 05:43:23 +0000796 }
797 Ok(())
798}
799
Jiyong Park9c63cd12023-03-21 17:53:07 +0900800fn patch_pci_info(fdt: &mut Fdt, pci_info: &PciInfo) -> libfdt::Result<()> {
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000801 let mut node =
Alan Stokesf46a17c2025-01-05 15:50:18 +0000802 fdt.root_mut().next_compatible(c"pci-host-cam-generic")?.ok_or(FdtError::NotFound)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900803
804 let irq_masks_size = pci_info.irq_masks.len() * size_of::<PciIrqMask>();
Alan Stokesf46a17c2025-01-05 15:50:18 +0000805 node.trimprop(c"interrupt-map-mask", irq_masks_size)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900806
807 let irq_maps_size = pci_info.irq_maps.len() * size_of::<PciIrqMap>();
Alan Stokesf46a17c2025-01-05 15:50:18 +0000808 node.trimprop(c"interrupt-map", irq_maps_size)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900809
810 node.setprop_inplace(
Alan Stokesf46a17c2025-01-05 15:50:18 +0000811 c"ranges",
Pierre-Clément Tosid0818b22024-10-30 20:09:31 +0000812 [pci_info.ranges[0].to_cells(), pci_info.ranges[1].to_cells()].as_flattened(),
Jiyong Park9c63cd12023-03-21 17:53:07 +0900813 )
814}
815
Jiyong Park00ceff32023-03-13 05:43:23 +0000816#[derive(Default, Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900817struct SerialInfo {
818 addrs: ArrayVec<[u64; Self::MAX_SERIALS]>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000819}
820
821impl SerialInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900822 const MAX_SERIALS: usize = 4;
Jiyong Park00ceff32023-03-13 05:43:23 +0000823}
824
Jiyong Park6a8789a2023-03-21 14:50:59 +0900825fn read_serial_info_from(fdt: &Fdt) -> libfdt::Result<SerialInfo> {
Pierre-Clément Tosibe893612024-02-05 14:23:44 +0000826 let mut addrs = ArrayVec::new();
827
Alan Stokesf46a17c2025-01-05 15:50:18 +0000828 let mut serial_nodes = fdt.compatible_nodes(c"ns16550a")?;
Pierre-Clément Tosibe893612024-02-05 14:23:44 +0000829 for node in serial_nodes.by_ref().take(addrs.capacity()) {
Alice Wang6ff2d0c2023-09-19 15:28:43 +0000830 let reg = node.first_reg()?;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900831 addrs.push(reg.addr);
Jiyong Park00ceff32023-03-13 05:43:23 +0000832 }
Pierre-Clément Tosibe893612024-02-05 14:23:44 +0000833 if serial_nodes.next().is_some() {
834 warn!("DT has more than {} UART nodes: discarding extra nodes.", addrs.capacity());
835 }
836
Jiyong Park6a8789a2023-03-21 14:50:59 +0900837 Ok(SerialInfo { addrs })
Jiyong Park00ceff32023-03-13 05:43:23 +0000838}
839
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000840#[derive(Default, Debug, PartialEq)]
841struct WdtInfo {
842 addr: u64,
843 size: u64,
844 irq: [u32; WdtInfo::IRQ_CELLS],
845}
846
847impl WdtInfo {
848 const IRQ_CELLS: usize = 3;
849 const IRQ_NR: u32 = 0xf;
850 const ADDR: u64 = 0x3000;
851 const SIZE: u64 = 0x1000;
852 const GIC_PPI: u32 = 1;
853 const IRQ_TYPE_EDGE_RISING: u32 = 1;
854 const GIC_FDT_IRQ_PPI_CPU_SHIFT: u32 = 8;
Pierre-Clément Tosi3ad82742024-07-04 10:23:00 +0100855 // TODO(b/350498812): Rework this for >8 vCPUs.
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000856 const GIC_FDT_IRQ_PPI_CPU_MASK: u32 = 0xff << Self::GIC_FDT_IRQ_PPI_CPU_SHIFT;
857
858 const fn get_expected(num_cpus: usize) -> Self {
859 Self {
860 addr: Self::ADDR,
861 size: Self::SIZE,
862 irq: [
863 Self::GIC_PPI,
864 Self::IRQ_NR,
865 ((((1 << num_cpus) - 1) << Self::GIC_FDT_IRQ_PPI_CPU_SHIFT)
866 & Self::GIC_FDT_IRQ_PPI_CPU_MASK)
867 | Self::IRQ_TYPE_EDGE_RISING,
868 ],
869 }
870 }
871}
872
873fn read_wdt_info_from(fdt: &Fdt) -> libfdt::Result<WdtInfo> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000874 let mut node_iter = fdt.compatible_nodes(c"qemu,vcpu-stall-detector")?;
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000875 let node = node_iter.next().ok_or(FdtError::NotFound)?;
876 let mut ranges = node.reg()?.ok_or(FdtError::NotFound)?;
877
878 let reg = ranges.next().ok_or(FdtError::NotFound)?;
879 let size = reg.size.ok_or(FdtError::NotFound)?;
880 if ranges.next().is_some() {
881 warn!("Discarding extra vmwdt <reg> entries.");
882 }
883
Alan Stokesf46a17c2025-01-05 15:50:18 +0000884 let interrupts = node.getprop_cells(c"interrupts")?.ok_or(FdtError::NotFound)?;
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000885 let mut chunks = CellChunkIterator::<{ WdtInfo::IRQ_CELLS }>::new(interrupts);
886 let irq = chunks.next().ok_or(FdtError::NotFound)?;
887
888 if chunks.next().is_some() {
889 warn!("Discarding extra vmwdt <interrupts> entries.");
890 }
891
892 Ok(WdtInfo { addr: reg.addr, size, irq })
893}
894
895fn validate_wdt_info(wdt: &WdtInfo, num_cpus: usize) -> Result<(), RebootReason> {
896 if *wdt != WdtInfo::get_expected(num_cpus) {
897 error!("Invalid watchdog timer: {wdt:?}");
898 return Err(RebootReason::InvalidFdt);
899 }
900
901 Ok(())
902}
903
904fn patch_wdt_info(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
905 let mut interrupts = WdtInfo::get_expected(num_cpus).irq;
906 for v in interrupts.iter_mut() {
907 *v = v.to_be();
908 }
909
910 let mut node = fdt
911 .root_mut()
Alan Stokesf46a17c2025-01-05 15:50:18 +0000912 .next_compatible(c"qemu,vcpu-stall-detector")?
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000913 .ok_or(libfdt::FdtError::NotFound)?;
Alan Stokesf46a17c2025-01-05 15:50:18 +0000914 node.setprop_inplace(c"interrupts", interrupts.as_bytes())?;
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000915 Ok(())
916}
917
Jiyong Park9c63cd12023-03-21 17:53:07 +0900918/// Patch the DT by deleting the ns16550a compatible nodes whose address are unknown
919fn patch_serial_info(fdt: &mut Fdt, serial_info: &SerialInfo) -> libfdt::Result<()> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000920 let name = c"ns16550a";
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000921 let mut next = fdt.root_mut().next_compatible(name);
Jiyong Park9c63cd12023-03-21 17:53:07 +0900922 while let Some(current) = next? {
Pierre-Clément Tosic01fd0d2024-01-25 22:26:22 +0000923 let reg =
924 current.as_node().reg()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900925 next = if !serial_info.addrs.contains(&reg.addr) {
926 current.delete_and_next_compatible(name)
927 } else {
928 current.next_compatible(name)
929 }
930 }
931 Ok(())
932}
933
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700934fn validate_swiotlb_info(
935 swiotlb_info: &SwiotlbInfo,
936 memory: &Range<usize>,
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +0000937 guest_page_size: usize,
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700938) -> Result<(), RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900939 let size = swiotlb_info.size;
940 let align = swiotlb_info.align;
Jiyong Park00ceff32023-03-13 05:43:23 +0000941
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +0000942 if size == 0 || (size % guest_page_size) != 0 {
Jiyong Park00ceff32023-03-13 05:43:23 +0000943 error!("Invalid swiotlb size {:#x}", size);
944 return Err(RebootReason::InvalidFdt);
945 }
946
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +0000947 if let Some(align) = align.filter(|&a| a % guest_page_size != 0) {
Jiyong Park00ceff32023-03-13 05:43:23 +0000948 error!("Invalid swiotlb alignment {:#x}", align);
949 return Err(RebootReason::InvalidFdt);
950 }
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700951
Alice Wang9cfbfd62023-06-14 11:19:03 +0000952 if let Some(addr) = swiotlb_info.addr {
953 if addr.checked_add(size).is_none() {
954 error!("Invalid swiotlb range: addr:{addr:#x} size:{size:#x}");
955 return Err(RebootReason::InvalidFdt);
956 }
957 }
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700958 if let Some(range) = swiotlb_info.fixed_range() {
959 if !range.is_within(memory) {
960 error!("swiotlb range {range:#x?} not part of memory range {memory:#x?}");
961 return Err(RebootReason::InvalidFdt);
962 }
963 }
964
Jiyong Park6a8789a2023-03-21 14:50:59 +0900965 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000966}
967
Jiyong Park9c63cd12023-03-21 17:53:07 +0900968fn patch_swiotlb_info(fdt: &mut Fdt, swiotlb_info: &SwiotlbInfo) -> libfdt::Result<()> {
969 let mut node =
Alan Stokesf46a17c2025-01-05 15:50:18 +0000970 fdt.root_mut().next_compatible(c"restricted-dma-pool")?.ok_or(FdtError::NotFound)?;
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700971
972 if let Some(range) = swiotlb_info.fixed_range() {
Pierre-Clément Tosic27c4272023-05-19 15:46:26 +0000973 node.setprop_addrrange_inplace(
Alan Stokesf46a17c2025-01-05 15:50:18 +0000974 c"reg",
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700975 range.start.try_into().unwrap(),
976 range.len().try_into().unwrap(),
977 )?;
Alan Stokesf46a17c2025-01-05 15:50:18 +0000978 node.nop_property(c"size")?;
979 node.nop_property(c"alignment")?;
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700980 } else {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000981 node.nop_property(c"reg")?;
982 node.setprop_inplace(c"size", &swiotlb_info.size.to_be_bytes())?;
983 node.setprop_inplace(c"alignment", &swiotlb_info.align.unwrap().to_be_bytes())?;
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700984 }
985
Jiyong Park9c63cd12023-03-21 17:53:07 +0900986 Ok(())
987}
988
989fn patch_gic(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
Alan Stokesf46a17c2025-01-05 15:50:18 +0000990 let node = fdt.compatible_nodes(c"arm,gic-v3")?.next().ok_or(FdtError::NotFound)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900991 let mut ranges = node.reg()?.ok_or(FdtError::NotFound)?;
992 let range0 = ranges.next().ok_or(FdtError::NotFound)?;
993 let mut range1 = ranges.next().ok_or(FdtError::NotFound)?;
994
995 let addr = range0.addr;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000996 // `read_cpu_info_from()` guarantees that we have at most MAX_CPUS.
997 const_assert!(DeviceTreeInfo::gic_patched_size(DeviceTreeInfo::MAX_CPUS).is_some());
Alice Wangabc7d632023-06-14 09:10:14 +0000998 let size = u64::try_from(DeviceTreeInfo::gic_patched_size(num_cpus).unwrap()).unwrap();
Jiyong Park9c63cd12023-03-21 17:53:07 +0900999
1000 // range1 is just below range0
1001 range1.addr = addr - size;
1002 range1.size = Some(size);
1003
Pierre-Clément Tosieea2a982024-02-05 15:10:59 +00001004 let (addr0, size0) = range0.to_cells();
1005 let (addr1, size1) = range1.to_cells();
1006 let value = [addr0, size0.unwrap(), addr1, size1.unwrap()];
Jiyong Park9c63cd12023-03-21 17:53:07 +09001007
Alan Stokesf46a17c2025-01-05 15:50:18 +00001008 let mut node = fdt.root_mut().next_compatible(c"arm,gic-v3")?.ok_or(FdtError::NotFound)?;
1009 node.setprop_inplace(c"reg", value.as_flattened())
Jiyong Park9c63cd12023-03-21 17:53:07 +09001010}
1011
1012fn patch_timer(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
1013 const NUM_INTERRUPTS: usize = 4;
1014 const CELLS_PER_INTERRUPT: usize = 3;
Alan Stokesf46a17c2025-01-05 15:50:18 +00001015 let node = fdt.compatible_nodes(c"arm,armv8-timer")?.next().ok_or(FdtError::NotFound)?;
1016 let interrupts = node.getprop_cells(c"interrupts")?.ok_or(FdtError::NotFound)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +09001017 let mut value: ArrayVec<[u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT]> =
1018 interrupts.take(NUM_INTERRUPTS * CELLS_PER_INTERRUPT).collect();
1019
1020 let num_cpus: u32 = num_cpus.try_into().unwrap();
Pierre-Clément Tosi3ad82742024-07-04 10:23:00 +01001021 // TODO(b/350498812): Rework this for >8 vCPUs.
Jiyong Park9c63cd12023-03-21 17:53:07 +09001022 let cpu_mask: u32 = (((0x1 << num_cpus) - 1) & 0xff) << 8;
Sebastian Enee8e99fa2024-05-23 14:49:41 +00001023
Jiyong Park9c63cd12023-03-21 17:53:07 +09001024 for v in value.iter_mut().skip(2).step_by(CELLS_PER_INTERRUPT) {
1025 *v |= cpu_mask;
1026 }
1027 for v in value.iter_mut() {
1028 *v = v.to_be();
1029 }
1030
Pierre-Clément Tosi0edc4d62024-02-05 14:13:53 +00001031 let value = value.into_inner();
Jiyong Park9c63cd12023-03-21 17:53:07 +09001032
Alan Stokesf46a17c2025-01-05 15:50:18 +00001033 let mut node = fdt.root_mut().next_compatible(c"arm,armv8-timer")?.ok_or(FdtError::NotFound)?;
1034 node.setprop_inplace(c"interrupts", value.as_bytes())
Jiyong Park9c63cd12023-03-21 17:53:07 +09001035}
1036
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001037fn patch_untrusted_props(fdt: &mut Fdt, props: &BTreeMap<CString, Vec<u8>>) -> libfdt::Result<()> {
Alan Stokesf46a17c2025-01-05 15:50:18 +00001038 let avf_node = if let Some(node) = fdt.node_mut(c"/avf")? {
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001039 node
1040 } else {
Alan Stokesf46a17c2025-01-05 15:50:18 +00001041 fdt.root_mut().add_subnode(c"avf")?
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001042 };
1043
1044 // The node shouldn't already be present; if it is, return the error.
Alan Stokesf46a17c2025-01-05 15:50:18 +00001045 let mut node = avf_node.add_subnode(c"untrusted")?;
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001046
1047 for (name, value) in props {
1048 node.setprop(name, value)?;
1049 }
1050
1051 Ok(())
1052}
1053
Jiyong Park00ceff32023-03-13 05:43:23 +00001054#[derive(Debug)]
David Dai9bdb10c2024-02-01 22:42:54 -08001055struct VcpufreqInfo {
1056 addr: u64,
1057 size: u64,
1058}
1059
1060fn patch_vcpufreq(fdt: &mut Fdt, vcpufreq_info: &Option<VcpufreqInfo>) -> libfdt::Result<()> {
Alan Stokesf46a17c2025-01-05 15:50:18 +00001061 let mut node = fdt.node_mut(c"/cpufreq")?.unwrap();
David Dai9bdb10c2024-02-01 22:42:54 -08001062 if let Some(info) = vcpufreq_info {
Alan Stokesf46a17c2025-01-05 15:50:18 +00001063 node.setprop_addrrange_inplace(c"reg", info.addr, info.size)
David Dai9bdb10c2024-02-01 22:42:54 -08001064 } else {
1065 node.nop()
1066 }
1067}
1068
1069#[derive(Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +09001070pub struct DeviceTreeInfo {
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +00001071 initrd_range: Option<(DeviceTreeInteger, DeviceTreeInteger)>,
Jiyong Park6a8789a2023-03-21 14:50:59 +09001072 pub memory_range: Range<usize>,
Jiyong Parke9d87e82023-03-21 19:28:40 +09001073 bootargs: Option<CString>,
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001074 cpus: ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>,
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +00001075 cpu_topology: Option<CpuTopology>,
Jiyong Park00ceff32023-03-13 05:43:23 +00001076 pci_info: PciInfo,
1077 serial_info: SerialInfo,
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -07001078 pub swiotlb_info: SwiotlbInfo,
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001079 device_assignment: Option<DeviceAssignmentInfo>,
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001080 untrusted_props: BTreeMap<CString, Vec<u8>>,
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001081 vm_ref_dt_props_info: BTreeMap<CString, Vec<u8>>,
David Dai9bdb10c2024-02-01 22:42:54 -08001082 vcpufreq_info: Option<VcpufreqInfo>,
Jiyong Park00ceff32023-03-13 05:43:23 +00001083}
1084
1085impl DeviceTreeInfo {
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001086 const MAX_CPUS: usize = 16;
1087
1088 const fn gic_patched_size(num_cpus: usize) -> Option<usize> {
Alice Wangabc7d632023-06-14 09:10:14 +00001089 const GIC_REDIST_SIZE_PER_CPU: usize = 32 * SIZE_4KB;
1090
1091 GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus)
1092 }
Jiyong Park00ceff32023-03-13 05:43:23 +00001093}
1094
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001095pub fn sanitize_device_tree(
Pierre-Clément Tosi0d4c09b2024-11-19 17:32:15 +00001096 fdt: &mut Fdt,
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001097 vm_dtbo: Option<&mut [u8]>,
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001098 vm_ref_dt: Option<&[u8]>,
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +00001099 guest_page_size: usize,
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001100) -> Result<DeviceTreeInfo, RebootReason> {
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001101 let vm_dtbo = match vm_dtbo {
1102 Some(vm_dtbo) => Some(VmDtbo::from_mut_slice(vm_dtbo).map_err(|e| {
1103 error!("Failed to load VM DTBO: {e}");
1104 RebootReason::InvalidFdt
1105 })?),
1106 None => None,
1107 };
1108
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +00001109 let info = parse_device_tree(fdt, vm_dtbo.as_deref(), guest_page_size)?;
Jiyong Park83316122023-03-21 09:39:39 +09001110
Pierre-Clément Tosi84ba1a82024-10-30 11:27:32 +00001111 fdt.clone_from(FDT_TEMPLATE).map_err(|e| {
Jiyong Parke9d87e82023-03-21 19:28:40 +09001112 error!("Failed to instantiate FDT from the template DT: {e}");
1113 RebootReason::InvalidFdt
1114 })?;
1115
Jaewan Kim9220e852023-12-01 10:58:40 +09001116 fdt.unpack().map_err(|e| {
1117 error!("Failed to unpack DT for patching: {e}");
1118 RebootReason::InvalidFdt
1119 })?;
1120
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001121 if let Some(device_assignment_info) = &info.device_assignment {
1122 let vm_dtbo = vm_dtbo.unwrap();
1123 device_assignment_info.filter(vm_dtbo).map_err(|e| {
1124 error!("Failed to filter VM DTBO: {e}");
1125 RebootReason::InvalidFdt
1126 })?;
1127 // SAFETY: Damaged VM DTBO isn't used in this API after this unsafe block.
1128 // VM DTBO can't be reused in any way as Fdt nor VmDtbo outside of this API because
1129 // it can only be instantiated after validation.
1130 unsafe {
1131 fdt.apply_overlay(vm_dtbo.as_mut()).map_err(|e| {
1132 error!("Failed to apply filtered VM DTBO: {e}");
1133 RebootReason::InvalidFdt
1134 })?;
1135 }
1136 }
1137
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001138 if let Some(vm_ref_dt) = vm_ref_dt {
1139 let vm_ref_dt = Fdt::from_slice(vm_ref_dt).map_err(|e| {
1140 error!("Failed to load VM reference DT: {e}");
Seungjae Yoo013f4c42024-01-02 13:04:19 +09001141 RebootReason::InvalidFdt
1142 })?;
1143
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001144 validate_vm_ref_dt(fdt, vm_ref_dt, &info.vm_ref_dt_props_info).map_err(|e| {
1145 error!("Failed to apply VM reference DT: {e}");
Seungjae Yoo013f4c42024-01-02 13:04:19 +09001146 RebootReason::InvalidFdt
1147 })?;
1148 }
1149
Jiyong Park9c63cd12023-03-21 17:53:07 +09001150 patch_device_tree(fdt, &info)?;
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001151
Jaewan Kim19b984f2023-12-04 15:16:50 +09001152 // TODO(b/317201360): Ensure no overlapping in <reg> among devices
1153
Jaewan Kim9220e852023-12-01 10:58:40 +09001154 fdt.pack().map_err(|e| {
1155 error!("Failed to unpack DT after patching: {e}");
1156 RebootReason::InvalidFdt
1157 })?;
1158
Jiyong Park6a8789a2023-03-21 14:50:59 +09001159 Ok(info)
Jiyong Park83316122023-03-21 09:39:39 +09001160}
1161
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +00001162fn parse_device_tree(
1163 fdt: &Fdt,
1164 vm_dtbo: Option<&VmDtbo>,
1165 guest_page_size: usize,
1166) -> Result<DeviceTreeInfo, RebootReason> {
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +00001167 let initrd_range = read_initrd_range_props(fdt).map_err(|e| {
Jiyong Park6a8789a2023-03-21 14:50:59 +09001168 error!("Failed to read initrd range from DT: {e}");
1169 RebootReason::InvalidFdt
1170 })?;
1171
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +00001172 let memory_range = read_and_validate_memory_range(fdt, guest_page_size)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +09001173
Jiyong Parke9d87e82023-03-21 19:28:40 +09001174 let bootargs = read_bootargs_from(fdt).map_err(|e| {
1175 error!("Failed to read bootargs from DT: {e}");
1176 RebootReason::InvalidFdt
1177 })?;
1178
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +00001179 let (cpus, cpu_topology) = read_cpu_info_from(fdt).map_err(|e| {
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001180 error!("Failed to read CPU info from DT: {e}");
Jiyong Park6a8789a2023-03-21 14:50:59 +09001181 RebootReason::InvalidFdt
1182 })?;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001183 validate_cpu_info(&cpus).map_err(|e| {
1184 error!("Failed to validate CPU info from DT: {e}");
Alice Wangabc7d632023-06-14 09:10:14 +00001185 RebootReason::InvalidFdt
1186 })?;
Jiyong Park6a8789a2023-03-21 14:50:59 +09001187
David Dai9bdb10c2024-02-01 22:42:54 -08001188 let vcpufreq_info = read_vcpufreq_info(fdt).map_err(|e| {
1189 error!("Failed to read vcpufreq info from DT: {e}");
1190 RebootReason::InvalidFdt
1191 })?;
1192 if let Some(ref info) = vcpufreq_info {
1193 validate_vcpufreq_info(info, &cpus).map_err(|e| {
1194 error!("Failed to validate vcpufreq info from DT: {e}");
1195 RebootReason::InvalidFdt
1196 })?;
1197 }
1198
Jiyong Park6a8789a2023-03-21 14:50:59 +09001199 let pci_info = read_pci_info_from(fdt).map_err(|e| {
1200 error!("Failed to read pci info from DT: {e}");
1201 RebootReason::InvalidFdt
1202 })?;
Jiyong Park0ee65392023-03-27 20:52:45 +09001203 validate_pci_info(&pci_info, &memory_range)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +09001204
Sebastian Enee8e99fa2024-05-23 14:49:41 +00001205 let wdt_info = read_wdt_info_from(fdt).map_err(|e| {
1206 error!("Failed to read vCPU stall detector info from DT: {e}");
1207 RebootReason::InvalidFdt
1208 })?;
1209 validate_wdt_info(&wdt_info, cpus.len())?;
1210
Jiyong Park6a8789a2023-03-21 14:50:59 +09001211 let serial_info = read_serial_info_from(fdt).map_err(|e| {
1212 error!("Failed to read serial info from DT: {e}");
1213 RebootReason::InvalidFdt
1214 })?;
1215
Pierre-Clément Tosi3c5e7a72024-11-27 20:12:37 +00001216 let swiotlb_info = SwiotlbInfo::new_from_fdt(fdt)
1217 .map_err(|e| {
1218 error!("Failed to read swiotlb info from DT: {e}");
1219 RebootReason::InvalidFdt
1220 })?
1221 .ok_or_else(|| {
1222 error!("Swiotlb info missing from DT");
1223 RebootReason::InvalidFdt
1224 })?;
Pierre-Clément Tosi938b4fb2024-11-26 12:59:47 +00001225 validate_swiotlb_info(&swiotlb_info, &memory_range, guest_page_size)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +09001226
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001227 let device_assignment = match vm_dtbo {
Jaewan Kim52477ae2023-11-21 21:20:52 +09001228 Some(vm_dtbo) => {
Per Larsen7ec45d32024-11-02 00:56:46 +00001229 if let Some(hypervisor) = get_device_assigner() {
Pierre-Clément Tosieacdd0f2024-10-22 16:31:01 +01001230 // TODO(ptosi): Cache the (single?) granule once, in vmbase.
Per Larsen7ec45d32024-11-02 00:56:46 +00001231 let granule = get_mem_sharer()
Pierre-Clément Tosieacdd0f2024-10-22 16:31:01 +01001232 .ok_or_else(|| {
1233 error!("No MEM_SHARE found during device assignment validation");
1234 RebootReason::InternalError
1235 })?
1236 .granule()
1237 .map_err(|e| {
1238 error!("Failed to get granule for device assignment validation: {e}");
1239 RebootReason::InternalError
1240 })?;
1241 DeviceAssignmentInfo::parse(fdt, vm_dtbo, hypervisor, granule).map_err(|e| {
Jaewan Kim52477ae2023-11-21 21:20:52 +09001242 error!("Failed to parse device assignment from DT and VM DTBO: {e}");
1243 RebootReason::InvalidFdt
1244 })?
1245 } else {
1246 warn!(
1247 "Device assignment is ignored because device assigning hypervisor is missing"
1248 );
1249 None
1250 }
1251 }
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001252 None => None,
1253 };
1254
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001255 let untrusted_props = parse_untrusted_props(fdt).map_err(|e| {
1256 error!("Failed to read untrusted properties: {e}");
1257 RebootReason::InvalidFdt
1258 })?;
1259 validate_untrusted_props(&untrusted_props).map_err(|e| {
1260 error!("Failed to validate untrusted properties: {e}");
1261 RebootReason::InvalidFdt
1262 })?;
1263
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001264 let vm_ref_dt_props_info = parse_vm_ref_dt(fdt).map_err(|e| {
Seungjae Yoo013f4c42024-01-02 13:04:19 +09001265 error!("Failed to read names of properties under /avf from DT: {e}");
1266 RebootReason::InvalidFdt
1267 })?;
Seungjae Yooed67fd52023-11-29 18:54:36 +09001268
Jiyong Park00ceff32023-03-13 05:43:23 +00001269 Ok(DeviceTreeInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +09001270 initrd_range,
1271 memory_range,
Jiyong Parke9d87e82023-03-21 19:28:40 +09001272 bootargs,
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001273 cpus,
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +00001274 cpu_topology,
Jiyong Park6a8789a2023-03-21 14:50:59 +09001275 pci_info,
1276 serial_info,
1277 swiotlb_info,
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001278 device_assignment,
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001279 untrusted_props,
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001280 vm_ref_dt_props_info,
David Dai9bdb10c2024-02-01 22:42:54 -08001281 vcpufreq_info,
Jiyong Park00ceff32023-03-13 05:43:23 +00001282 })
1283}
1284
Jiyong Park9c63cd12023-03-21 17:53:07 +09001285fn patch_device_tree(fdt: &mut Fdt, info: &DeviceTreeInfo) -> Result<(), RebootReason> {
Pierre-Clément Tosie6e46db2025-02-07 11:39:41 +00001286 if let Some((start, end)) = &info.initrd_range {
1287 patch_initrd_range(fdt, start, end).map_err(|e| {
Jiyong Park9c63cd12023-03-21 17:53:07 +09001288 error!("Failed to patch initrd range to DT: {e}");
1289 RebootReason::InvalidFdt
1290 })?;
1291 }
1292 patch_memory_range(fdt, &info.memory_range).map_err(|e| {
1293 error!("Failed to patch memory range to DT: {e}");
1294 RebootReason::InvalidFdt
1295 })?;
Jiyong Parke9d87e82023-03-21 19:28:40 +09001296 if let Some(bootargs) = &info.bootargs {
1297 patch_bootargs(fdt, bootargs.as_c_str()).map_err(|e| {
1298 error!("Failed to patch bootargs to DT: {e}");
1299 RebootReason::InvalidFdt
1300 })?;
1301 }
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +00001302 patch_cpus(fdt, &info.cpus, &info.cpu_topology).map_err(|e| {
Jiyong Park9c63cd12023-03-21 17:53:07 +09001303 error!("Failed to patch cpus to DT: {e}");
1304 RebootReason::InvalidFdt
1305 })?;
David Dai9bdb10c2024-02-01 22:42:54 -08001306 patch_vcpufreq(fdt, &info.vcpufreq_info).map_err(|e| {
1307 error!("Failed to patch vcpufreq info to DT: {e}");
1308 RebootReason::InvalidFdt
1309 })?;
Jiyong Park9c63cd12023-03-21 17:53:07 +09001310 patch_pci_info(fdt, &info.pci_info).map_err(|e| {
1311 error!("Failed to patch pci info to DT: {e}");
1312 RebootReason::InvalidFdt
1313 })?;
Sebastian Enee8e99fa2024-05-23 14:49:41 +00001314 patch_wdt_info(fdt, info.cpus.len()).map_err(|e| {
1315 error!("Failed to patch wdt info to DT: {e}");
1316 RebootReason::InvalidFdt
1317 })?;
Jiyong Park9c63cd12023-03-21 17:53:07 +09001318 patch_serial_info(fdt, &info.serial_info).map_err(|e| {
1319 error!("Failed to patch serial info to DT: {e}");
1320 RebootReason::InvalidFdt
1321 })?;
1322 patch_swiotlb_info(fdt, &info.swiotlb_info).map_err(|e| {
1323 error!("Failed to patch swiotlb info to DT: {e}");
1324 RebootReason::InvalidFdt
1325 })?;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001326 patch_gic(fdt, info.cpus.len()).map_err(|e| {
Jiyong Park9c63cd12023-03-21 17:53:07 +09001327 error!("Failed to patch gic info to DT: {e}");
1328 RebootReason::InvalidFdt
1329 })?;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001330 patch_timer(fdt, info.cpus.len()).map_err(|e| {
Jiyong Park9c63cd12023-03-21 17:53:07 +09001331 error!("Failed to patch timer info to DT: {e}");
1332 RebootReason::InvalidFdt
1333 })?;
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001334 if let Some(device_assignment) = &info.device_assignment {
1335 // Note: We patch values after VM DTBO is overlaid because patch may require more space
1336 // then VM DTBO's underlying slice is allocated.
1337 device_assignment.patch(fdt).map_err(|e| {
1338 error!("Failed to patch device assignment info to DT: {e}");
1339 RebootReason::InvalidFdt
1340 })?;
Jaewan Kim50246682024-03-11 23:18:54 +09001341 } else {
1342 device_assignment::clean(fdt).map_err(|e| {
1343 error!("Failed to clean pre-polulated DT nodes for device assignment: {e}");
1344 RebootReason::InvalidFdt
1345 })?;
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001346 }
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001347 patch_untrusted_props(fdt, &info.untrusted_props).map_err(|e| {
1348 error!("Failed to patch untrusted properties: {e}");
1349 RebootReason::InvalidFdt
1350 })?;
Jiyong Parke9d87e82023-03-21 19:28:40 +09001351
Jiyong Park9c63cd12023-03-21 17:53:07 +09001352 Ok(())
1353}
1354
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001355/// Modifies the input DT according to the fields of the configuration.
1356pub fn modify_for_next_stage(
1357 fdt: &mut Fdt,
1358 bcc: &[u8],
1359 new_instance: bool,
1360 strict_boot: bool,
Alan Stokes65618332023-12-15 14:09:25 +00001361 debug_policy: Option<&[u8]>,
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001362 debuggable: bool,
Pierre-Clément Tosi80251972023-07-12 12:51:12 +00001363 kaslr_seed: u64,
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001364) -> libfdt::Result<()> {
Pierre-Clément Tosieb887ac2023-05-02 13:33:37 +00001365 if let Some(debug_policy) = debug_policy {
1366 let backup = Vec::from(fdt.as_slice());
1367 fdt.unpack()?;
1368 let backup_fdt = Fdt::from_slice(backup.as_slice()).unwrap();
1369 if apply_debug_policy(fdt, backup_fdt, debug_policy)? {
1370 info!("Debug policy applied.");
1371 } else {
1372 // apply_debug_policy restored fdt to backup_fdt so unpack it again.
1373 fdt.unpack()?;
1374 }
1375 } else {
1376 info!("No debug policy found.");
1377 fdt.unpack()?;
1378 }
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001379
Jiyong Parke9d87e82023-03-21 19:28:40 +09001380 patch_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001381
Alice Wang56ec45b2023-06-15 08:30:32 +00001382 if let Some(mut chosen) = fdt.chosen_mut()? {
Alan Stokesf46a17c2025-01-05 15:50:18 +00001383 empty_or_delete_prop(&mut chosen, c"avf,strict-boot", strict_boot)?;
1384 empty_or_delete_prop(&mut chosen, c"avf,new-instance", new_instance)?;
1385 chosen.setprop_inplace(c"kaslr-seed", &kaslr_seed.to_be_bytes())?;
Alice Wang56ec45b2023-06-15 08:30:32 +00001386 };
Jiyong Park32f37ef2023-05-17 16:15:58 +09001387 if !debuggable {
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001388 if let Some(bootargs) = read_bootargs_from(fdt)? {
1389 filter_out_dangerous_bootargs(fdt, &bootargs)?;
1390 }
1391 }
1392
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001393 fdt.pack()?;
1394
1395 Ok(())
1396}
1397
Jiyong Parke9d87e82023-03-21 19:28:40 +09001398/// Patch the "google,open-dice"-compatible reserved-memory node to point to the bcc range
1399fn patch_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()> {
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001400 // We reject DTs with missing reserved-memory node as validation should have checked that the
1401 // "swiotlb" subnode (compatible = "restricted-dma-pool") was present.
Alan Stokesf46a17c2025-01-05 15:50:18 +00001402 let node = fdt.node_mut(c"/reserved-memory")?.ok_or(libfdt::FdtError::NotFound)?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001403
Alan Stokesf46a17c2025-01-05 15:50:18 +00001404 let mut node = node.next_compatible(c"google,open-dice")?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001405
Jiyong Parke9d87e82023-03-21 19:28:40 +09001406 let addr: u64 = addr.try_into().unwrap();
1407 let size: u64 = size.try_into().unwrap();
Alan Stokesf46a17c2025-01-05 15:50:18 +00001408 node.setprop_inplace(c"reg", [addr.to_be_bytes(), size.to_be_bytes()].as_flattened())
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001409}
1410
Alice Wang56ec45b2023-06-15 08:30:32 +00001411fn empty_or_delete_prop(
1412 fdt_node: &mut FdtNodeMut,
1413 prop_name: &CStr,
1414 keep_prop: bool,
1415) -> libfdt::Result<()> {
1416 if keep_prop {
1417 fdt_node.setprop_empty(prop_name)
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001418 } else {
Alice Wang56ec45b2023-06-15 08:30:32 +00001419 fdt_node
1420 .delprop(prop_name)
1421 .or_else(|e| if e == FdtError::NotFound { Ok(()) } else { Err(e) })
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001422 }
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001423}
Jiyong Parkc23426b2023-04-10 17:32:27 +09001424
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001425/// Apply the debug policy overlay to the guest DT.
1426///
1427/// Returns Ok(true) on success, Ok(false) on recovered failure and Err(_) on corruption of the DT.
Pierre-Clément Tosieb887ac2023-05-02 13:33:37 +00001428fn apply_debug_policy(
1429 fdt: &mut Fdt,
1430 backup_fdt: &Fdt,
1431 debug_policy: &[u8],
1432) -> libfdt::Result<bool> {
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001433 let mut debug_policy = Vec::from(debug_policy);
1434 let overlay = match Fdt::from_mut_slice(debug_policy.as_mut_slice()) {
Jiyong Parkc23426b2023-04-10 17:32:27 +09001435 Ok(overlay) => overlay,
1436 Err(e) => {
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001437 warn!("Corrupted debug policy found: {e}. Not applying.");
1438 return Ok(false);
Jiyong Parkc23426b2023-04-10 17:32:27 +09001439 }
1440 };
Jiyong Parkc23426b2023-04-10 17:32:27 +09001441
Andrew Walbran20bb4e42023-07-07 13:55:55 +01001442 // SAFETY: on failure, the corrupted DT is restored using the backup.
Jiyong Parkc23426b2023-04-10 17:32:27 +09001443 if let Err(e) = unsafe { fdt.apply_overlay(overlay) } {
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001444 warn!("Failed to apply debug policy: {e}. Recovering...");
Pierre-Clément Tosice0b36d2024-01-26 10:50:05 +00001445 fdt.clone_from(backup_fdt)?;
Jiyong Parkc23426b2023-04-10 17:32:27 +09001446 // A successful restoration is considered success because an invalid debug policy
1447 // shouldn't DOS the pvmfw
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001448 Ok(false)
1449 } else {
1450 Ok(true)
Jiyong Parkc23426b2023-04-10 17:32:27 +09001451 }
Jiyong Parkc23426b2023-04-10 17:32:27 +09001452}
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001453
Pierre-Clément Tosi1fbc2e92023-05-02 17:28:17 +00001454fn has_common_debug_policy(fdt: &Fdt, debug_feature_name: &CStr) -> libfdt::Result<bool> {
Alan Stokesf46a17c2025-01-05 15:50:18 +00001455 if let Some(node) = fdt.node(c"/avf/guest/common")? {
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001456 if let Some(value) = node.getprop_u32(debug_feature_name)? {
1457 return Ok(value == 1);
1458 }
1459 }
1460 Ok(false) // if the policy doesn't exist or not 1, don't enable the debug feature
1461}
1462
1463fn filter_out_dangerous_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()> {
Alan Stokesf46a17c2025-01-05 15:50:18 +00001464 let has_crashkernel = has_common_debug_policy(fdt, c"ramdump")?;
1465 let has_console = has_common_debug_policy(fdt, c"log")?;
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001466
1467 let accepted: &[(&str, Box<dyn Fn(Option<&str>) -> bool>)] = &[
1468 ("panic", Box::new(|v| if let Some(v) = v { v == "=-1" } else { false })),
1469 ("crashkernel", Box::new(|_| has_crashkernel)),
1470 ("console", Box::new(|_| has_console)),
1471 ];
1472
1473 // parse and filter out unwanted
1474 let mut filtered = Vec::new();
1475 for arg in BootArgsIterator::new(bootargs).map_err(|e| {
1476 info!("Invalid bootarg: {e}");
1477 FdtError::BadValue
1478 })? {
1479 match accepted.iter().find(|&t| t.0 == arg.name()) {
1480 Some((_, pred)) if pred(arg.value()) => filtered.push(arg),
1481 _ => debug!("Rejected bootarg {}", arg.as_ref()),
1482 }
1483 }
1484
1485 // flatten into a new C-string
1486 let mut new_bootargs = Vec::new();
1487 for (i, arg) in filtered.iter().enumerate() {
1488 if i != 0 {
1489 new_bootargs.push(b' '); // separator
1490 }
1491 new_bootargs.extend_from_slice(arg.as_ref().as_bytes());
1492 }
1493 new_bootargs.push(b'\0');
1494
1495 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
Alan Stokesf46a17c2025-01-05 15:50:18 +00001496 node.setprop(c"bootargs", new_bootargs.as_slice())
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001497}