1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use fidl::unpersist;
use fidl_cf_sc_internal_starnixkernelconfig::Config as FidlConfig;
use fuchsia_inspect::{ArrayProperty, Node};
use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
use fuchsia_zircon as zx;
#[derive(Debug)]
pub struct Config {
    pub features: Vec<String>,
    pub init: Vec<String>,
    pub kernel_cmdline: String,
    pub mounts: Vec<String>,
    pub name: String,
    pub rlimits: Vec<String>,
    pub startup_file_path: String,
}
impl Config {
    pub fn take_from_startup_handle() -> Self {
        let config_vmo: zx::Vmo =
            take_startup_handle(HandleInfo::new(HandleType::ComponentConfigVmo, 0))
                .expect("Config VMO handle must be provided and cannot already have been taken.")
                .into();
        let config_size =
            config_vmo.get_content_size().expect("must be able to read config vmo content size");
        assert_ne!(config_size, 0, "config vmo must be non-empty");
        let mut config_bytes = Vec::new();
        config_bytes.resize(config_size as usize, 0);
        config_vmo.read(&mut config_bytes, 0).expect("must be able to read config vmo");
        let checksum_length = u16::from_le_bytes([config_bytes[0], config_bytes[1]]) as usize;
        let fidl_start = 2 + checksum_length;
        let observed_checksum = &config_bytes[2..fidl_start];
        let expected_checksum = vec![
            0x65, 0x19, 0x88, 0x53, 0xf3, 0x32, 0x0a, 0x1b, 0x8b, 0xc3, 0xa9, 0x80, 0x2e, 0xc0,
            0x70, 0x2c, 0x85, 0x10, 0x0d, 0x63, 0xf5, 0x12, 0x5e, 0x91, 0xa4, 0x6a, 0x6b, 0xad,
            0xc5, 0x63, 0xb1, 0x60,
        ];
        assert_eq!(
            observed_checksum, expected_checksum,
            "checksum from config VMO does not match expected checksum"
        );
        let fidl_config: FidlConfig = unpersist(&config_bytes[fidl_start..])
            .expect("must be able to parse bytes as config FIDL");
        Self {
            features: fidl_config.features,
            init: fidl_config.init,
            kernel_cmdline: fidl_config.kernel_cmdline,
            mounts: fidl_config.mounts,
            name: fidl_config.name,
            rlimits: fidl_config.rlimits,
            startup_file_path: fidl_config.startup_file_path,
        }
    }
    pub fn record_inspect(&self, inspector_node: &Node) {
        let arr = inspector_node.create_string_array("features", self.features.len());
        for i in 0..self.features.len() {
            arr.set(i, &self.features[i]);
        }
        inspector_node.record(arr);
        let arr = inspector_node.create_string_array("init", self.init.len());
        for i in 0..self.init.len() {
            arr.set(i, &self.init[i]);
        }
        inspector_node.record(arr);
        inspector_node.record_string("kernel_cmdline", &self.kernel_cmdline);
        let arr = inspector_node.create_string_array("mounts", self.mounts.len());
        for i in 0..self.mounts.len() {
            arr.set(i, &self.mounts[i]);
        }
        inspector_node.record(arr);
        inspector_node.record_string("name", &self.name);
        let arr = inspector_node.create_string_array("rlimits", self.rlimits.len());
        for i in 0..self.rlimits.len() {
            arr.set(i, &self.rlimits[i]);
        }
        inspector_node.record(arr);
        inspector_node.record_string("startup_file_path", &self.startup_file_path);
    }
}