[pvmfw][fdt] Do not panic if the DT has no /chosen node
When clearing the flag in the DT's /chosen node, this cl skips
the clearing instead of panic if the /chose node doesn't exist.
This cl also made a small refactoring of the flag clearing
function.
Bug: 249054080
Test: m pvmfw_img
Change-Id: Ibdee67835bbe1ef2825a2d0a1fcdbaa8ee9fde36
diff --git a/pvmfw/src/fdt.rs b/pvmfw/src/fdt.rs
index 8310504..efb354c 100644
--- a/pvmfw/src/fdt.rs
+++ b/pvmfw/src/fdt.rs
@@ -33,6 +33,7 @@
use libfdt::Fdt;
use libfdt::FdtError;
use libfdt::FdtNode;
+use libfdt::FdtNodeMut;
use log::debug;
use log::error;
use log::info;
@@ -729,9 +730,10 @@
patch_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?;
- set_or_clear_chosen_flag(fdt, cstr!("avf,strict-boot"), strict_boot)?;
- set_or_clear_chosen_flag(fdt, cstr!("avf,new-instance"), new_instance)?;
-
+ if let Some(mut chosen) = fdt.chosen_mut()? {
+ empty_or_delete_prop(&mut chosen, cstr!("avf,strict-boot"), strict_boot)?;
+ empty_or_delete_prop(&mut chosen, cstr!("avf,new-instance"), new_instance)?;
+ };
if !debuggable {
if let Some(bootargs) = read_bootargs_from(fdt)? {
filter_out_dangerous_bootargs(fdt, &bootargs)?;
@@ -756,19 +758,18 @@
node.setprop_inplace(cstr!("reg"), flatten(&[addr.to_be_bytes(), size.to_be_bytes()]))
}
-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)?;
+fn empty_or_delete_prop(
+ fdt_node: &mut FdtNodeMut,
+ prop_name: &CStr,
+ keep_prop: bool,
+) -> libfdt::Result<()> {
+ if keep_prop {
+ fdt_node.setprop_empty(prop_name)
} else {
- match chosen.delprop(flag) {
- Ok(()) | Err(FdtError::NotFound) => (),
- Err(e) => return Err(e),
- }
+ fdt_node
+ .delprop(prop_name)
+ .or_else(|e| if e == FdtError::NotFound { Ok(()) } else { Err(e) })
}
-
- Ok(())
}
/// Apply the debug policy overlay to the guest DT.