#[cfg(any(target_os = "android", target_os = "linux"))]
use std::collections::BTreeMap;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use arch::android::create_android_fdt;
use arch::apply_device_tree_overlays;
use arch::DtbOverlay;
use base::open_file_or_duplicate;
use cros_fdt::Error;
use cros_fdt::Fdt;
pub fn create_fdt(
android_fstab: Option<File>,
dump_device_tree_blob: Option<PathBuf>,
device_tree_overlays: Vec<DtbOverlay>,
) -> Result<Vec<u8>, Error> {
let mut fdt = Fdt::new(&[]);
if let Some(android_fstab) = android_fstab {
create_android_fdt(&mut fdt, android_fstab)?;
}
apply_device_tree_overlays(
&mut fdt,
device_tree_overlays,
#[cfg(any(target_os = "android", target_os = "linux"))]
vec![],
#[cfg(any(target_os = "android", target_os = "linux"))]
&BTreeMap::new(),
)?;
let fdt_final = fdt.finish()?;
if let Some(file_path) = dump_device_tree_blob {
let mut fd = open_file_or_duplicate(
&file_path,
OpenOptions::new()
.read(true)
.create(true)
.truncate(true)
.write(true),
)
.map_err(|e| Error::FdtIoError(e.into()))?;
fd.write_all(&fdt_final)
.map_err(|e| Error::FdtDumpIoError(e, file_path.clone()))?;
}
Ok(fdt_final)
}