arch/
android.rs

1// Copyright 2019 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::fs::File;
6use std::io::BufRead;
7use std::io::BufReader;
8
9use cros_fdt::Error;
10use cros_fdt::Fdt;
11use cros_fdt::Result;
12
13fn parse_fstab_line(line: &str) -> Result<Vec<String>> {
14    let vec: Vec<&str> = line.split_whitespace().collect();
15    if vec.len() != 5 {
16        return Err(Error::FdtParseError("invalid fstab format".into()));
17    }
18    Ok(vec.iter().map(|s| s.to_string()).collect())
19}
20
21/// Creates a flattened device tree containing all of the parameters used
22/// by Android.
23///
24/// # Arguments
25///
26/// * `fdt` - The DTB to modify. The root node will be modified.
27/// * `android-fstab` - A text file of Android fstab entries to add to the DTB
28pub fn create_android_fdt(fdt: &mut Fdt, fstab: File) -> Result<()> {
29    let vecs = BufReader::new(fstab)
30        .lines()
31        .map(|l| parse_fstab_line(l?.as_str()))
32        .collect::<Result<Vec<Vec<String>>>>()?;
33    let firmware_node = fdt.root_mut().subnode_mut("firmware")?;
34    let android_node = firmware_node.subnode_mut("android")?;
35    android_node.set_prop("compatible", "android,firmware")?;
36
37    let (dtprop, fstab): (_, Vec<_>) = vecs.into_iter().partition(|x| x[0] == "#dt-vendor");
38    let vendor_node = android_node.subnode_mut("vendor")?;
39    for vec in dtprop {
40        let content = std::fs::read_to_string(&vec[2])?;
41        vendor_node.set_prop(&vec[1], content)?;
42    }
43    let fstab_node = android_node.subnode_mut("fstab")?;
44    fstab_node.set_prop("compatible", "android,fstab")?;
45    for vec in fstab {
46        let partition = &vec[1][1..];
47        let partition_node = fstab_node.subnode_mut(partition)?;
48        partition_node.set_prop("compatible", "android,".to_owned() + partition)?;
49        partition_node.set_prop("dev", vec[0].as_str())?;
50        partition_node.set_prop("type", vec[2].as_str())?;
51        partition_node.set_prop("mnt_flags", vec[3].as_str())?;
52        partition_node.set_prop("fsmgr_flags", vec[4].as_str())?;
53    }
54    Ok(())
55}