blob: 793eaac1f866278447ed3039333730c62b7c6999 [file] [log] [blame]
// Copyright 2022, The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! High-level FDT functions.
use core::ffi::CStr;
use core::ops::Range;
use libfdt::Fdt;
use libfdt::FdtError;
/// Extract from /config the address range containing the pre-loaded kernel.
pub fn kernel_range(fdt: &libfdt::Fdt) -> libfdt::Result<Option<Range<usize>>> {
let config = CStr::from_bytes_with_nul(b"/config\0").unwrap();
let addr = CStr::from_bytes_with_nul(b"kernel-address\0").unwrap();
let size = CStr::from_bytes_with_nul(b"kernel-size\0").unwrap();
if let Some(config) = fdt.node(config)? {
if let (Some(addr), Some(size)) = (config.getprop_u32(addr)?, config.getprop_u32(size)?) {
let addr = addr as usize;
let size = size as usize;
return Ok(Some(addr..(addr + size)));
}
}
Ok(None)
}
/// Extract from /chosen the address range containing the pre-loaded ramdisk.
pub fn initrd_range(fdt: &libfdt::Fdt) -> libfdt::Result<Option<Range<usize>>> {
let start = CStr::from_bytes_with_nul(b"linux,initrd-start\0").unwrap();
let end = CStr::from_bytes_with_nul(b"linux,initrd-end\0").unwrap();
if let Some(chosen) = fdt.chosen()? {
if let (Some(start), Some(end)) = (chosen.getprop_u32(start)?, chosen.getprop_u32(end)?) {
return Ok(Some((start as usize)..(end as usize)));
}
}
Ok(None)
}
/// Modifies the input DT according to the fields of the configuration.
pub fn modify_for_next_stage(
fdt: &mut Fdt,
bcc: &[u8],
new_instance: bool,
strict_boot: bool,
) -> libfdt::Result<()> {
fdt.unpack()?;
add_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?;
set_or_clear_chosen_flag(
fdt,
CStr::from_bytes_with_nul(b"avf,strict-boot\0").unwrap(),
strict_boot,
)?;
set_or_clear_chosen_flag(
fdt,
CStr::from_bytes_with_nul(b"avf,new-instance\0").unwrap(),
new_instance,
)?;
fdt.pack()?;
Ok(())
}
/// Add a "google,open-dice"-compatible reserved-memory node to the tree.
fn add_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()> {
let reserved_memory = CStr::from_bytes_with_nul(b"/reserved-memory\0").unwrap();
// We reject DTs with missing reserved-memory node as validation should have checked that the
// "swiotlb" subnode (compatible = "restricted-dma-pool") was present.
let mut reserved_memory = fdt.node_mut(reserved_memory)?.ok_or(libfdt::FdtError::NotFound)?;
let dice = CStr::from_bytes_with_nul(b"dice\0").unwrap();
let mut dice = reserved_memory.add_subnode(dice)?;
let compatible = CStr::from_bytes_with_nul(b"compatible\0").unwrap();
dice.appendprop(compatible, b"google,open-dice\0")?;
let no_map = CStr::from_bytes_with_nul(b"no-map\0").unwrap();
dice.appendprop(no_map, &[])?;
let addr = addr.try_into().unwrap();
let size = size.try_into().unwrap();
let reg = CStr::from_bytes_with_nul(b"reg\0").unwrap();
dice.appendprop_addrrange(reg, addr, size)?;
Ok(())
}
fn set_or_clear_chosen_flag(fdt: &mut Fdt, flag: &CStr, value: bool) -> libfdt::Result<()> {
// TODO(b/249054080): Refactor to not panic if the DT doesn't contain a /chosen node.
let mut chosen = fdt.chosen_mut()?.unwrap();
if value {
chosen.setprop_empty(flag)?;
} else {
match chosen.delprop(flag) {
Ok(()) | Err(FdtError::NotFound) => (),
Err(e) => return Err(e),
}
}
Ok(())
}