mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-05 18:20:34 +00:00
0a91c96437
This removes some unsafe code, improving the interface so that it cannot be misused (e.g. previously, different Vec<u8> instances could be passed to fdt functions that did not validate the contents). The new implementation also adds some extra error checking to catch invalid string values in all API entry points that accept strings, as well as out-of-order node nesting that would result in DTB data that did not conform to the spec. BUG=b:175729255 TEST=cargo test -p arch TEST=Boot Crostini on kevin (arm) TEST=diff old and new dts from `dtc -I fs -O dts /proc/device-tree` Change-Id: I567d562ed1b447aa0d282b68c71758edea13ffc0 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2713569 Reviewed-by: Zach Reizner <zachr@chromium.org> Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org> Reviewed-by: Dylan Reid <dgreid@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
58 lines
2.1 KiB
Rust
58 lines
2.1 KiB
Rust
// Copyright 2019 The Chromium OS Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
use std::fs::File;
|
|
use std::io::BufRead;
|
|
use std::io::BufReader;
|
|
|
|
use crate::fdt::{Error, FdtWriter, Result};
|
|
|
|
fn parse_fstab_line(line: &str) -> Result<Vec<String>> {
|
|
let vec: Vec<&str> = line.split_whitespace().collect();
|
|
if vec.len() != 5 {
|
|
return Err(Error::FdtFileParseError);
|
|
}
|
|
Ok(vec.iter().map(|s| s.to_string()).collect())
|
|
}
|
|
|
|
/// Creates a flattened device tree containing all of the parameters used
|
|
/// by Android.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `fdt` - The DTB to modify. The top-most node should be open.
|
|
/// * `android-fstab` - A text file of Android fstab entries to add to the DTB
|
|
pub fn create_android_fdt(fdt: &mut FdtWriter, fstab: File) -> Result<()> {
|
|
let vecs = BufReader::new(fstab)
|
|
.lines()
|
|
.map(|l| parse_fstab_line(&l.map_err(Error::FdtIoError)?))
|
|
.collect::<Result<Vec<Vec<String>>>>()?;
|
|
let firmware_node = fdt.begin_node("firmware")?;
|
|
let android_node = fdt.begin_node("android")?;
|
|
fdt.property_string("compatible", "android,firmware")?;
|
|
|
|
let (dtprop, fstab): (_, Vec<_>) = vecs.into_iter().partition(|x| x[0] == "#dt-vendor");
|
|
let vendor_node = fdt.begin_node("vendor")?;
|
|
for vec in dtprop {
|
|
let content = std::fs::read_to_string(&vec[2]).map_err(Error::FdtIoError)?;
|
|
fdt.property_string(&vec[1], &content)?;
|
|
}
|
|
fdt.end_node(vendor_node)?;
|
|
let fstab_node = fdt.begin_node("fstab")?;
|
|
fdt.property_string("compatible", "android,fstab")?;
|
|
for vec in fstab {
|
|
let partition = &vec[1][1..];
|
|
let partition_node = fdt.begin_node(partition)?;
|
|
fdt.property_string("compatible", &("android,".to_owned() + partition))?;
|
|
fdt.property_string("dev", &vec[0])?;
|
|
fdt.property_string("type", &vec[2])?;
|
|
fdt.property_string("mnt_flags", &vec[3])?;
|
|
fdt.property_string("fsmgr_flags", &vec[4])?;
|
|
fdt.end_node(partition_node)?;
|
|
}
|
|
fdt.end_node(fstab_node)?;
|
|
fdt.end_node(android_node)?;
|
|
fdt.end_node(firmware_node)?;
|
|
Ok(())
|
|
}
|