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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use fidl::unpersist;
use fidl_cf_sc_internal_fshostconfig::Config as FidlConfig;
use fuchsia_inspect::Node;
use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
use fuchsia_zircon as zx;
#[derive(Debug)]
pub struct Config {
    pub blobfs: bool,
    pub blobfs_initial_inodes: u64,
    pub blobfs_max_bytes: u64,
    pub blobfs_use_deprecated_padded_format: bool,
    pub bootpart: bool,
    pub check_filesystems: bool,
    pub data: bool,
    pub data_filesystem_format: String,
    pub data_max_bytes: u64,
    pub disable_block_watcher: bool,
    pub factory: bool,
    pub format_data_on_corruption: bool,
    pub fvm: bool,
    pub fvm_slice_size: u64,
    pub fxfs_blob: bool,
    pub fxfs_crypt_url: String,
    pub gpt: bool,
    pub gpt_all: bool,
    pub mbr: bool,
    pub nand: bool,
    pub netboot: bool,
    pub no_zxcrypt: bool,
    pub ramdisk_image: bool,
    pub use_disk_migration: bool,
}
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![
            0x42, 0x53, 0x23, 0xc0, 0xd1, 0xa7, 0xae, 0x0a, 0x51, 0x10, 0x20, 0x7f, 0x3a, 0x83,
            0x41, 0x58, 0x39, 0xa4, 0x60, 0x8d, 0x42, 0x53, 0xb8, 0x2a, 0x4e, 0x6a, 0x80, 0x67,
            0xed, 0x62, 0x4f, 0x85,
        ];
        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 {
            blobfs: fidl_config.blobfs,
            blobfs_initial_inodes: fidl_config.blobfs_initial_inodes,
            blobfs_max_bytes: fidl_config.blobfs_max_bytes,
            blobfs_use_deprecated_padded_format: fidl_config.blobfs_use_deprecated_padded_format,
            bootpart: fidl_config.bootpart,
            check_filesystems: fidl_config.check_filesystems,
            data: fidl_config.data,
            data_filesystem_format: fidl_config.data_filesystem_format,
            data_max_bytes: fidl_config.data_max_bytes,
            disable_block_watcher: fidl_config.disable_block_watcher,
            factory: fidl_config.factory,
            format_data_on_corruption: fidl_config.format_data_on_corruption,
            fvm: fidl_config.fvm,
            fvm_slice_size: fidl_config.fvm_slice_size,
            fxfs_blob: fidl_config.fxfs_blob,
            fxfs_crypt_url: fidl_config.fxfs_crypt_url,
            gpt: fidl_config.gpt,
            gpt_all: fidl_config.gpt_all,
            mbr: fidl_config.mbr,
            nand: fidl_config.nand,
            netboot: fidl_config.netboot,
            no_zxcrypt: fidl_config.no_zxcrypt,
            ramdisk_image: fidl_config.ramdisk_image,
            use_disk_migration: fidl_config.use_disk_migration,
        }
    }
    pub fn record_inspect(&self, inspector_node: &Node) {
        inspector_node.record_bool("blobfs", self.blobfs);
        inspector_node.record_uint("blobfs_initial_inodes", self.blobfs_initial_inodes);
        inspector_node.record_uint("blobfs_max_bytes", self.blobfs_max_bytes);
        inspector_node.record_bool(
            "blobfs_use_deprecated_padded_format",
            self.blobfs_use_deprecated_padded_format,
        );
        inspector_node.record_bool("bootpart", self.bootpart);
        inspector_node.record_bool("check_filesystems", self.check_filesystems);
        inspector_node.record_bool("data", self.data);
        inspector_node.record_string("data_filesystem_format", &self.data_filesystem_format);
        inspector_node.record_uint("data_max_bytes", self.data_max_bytes);
        inspector_node.record_bool("disable_block_watcher", self.disable_block_watcher);
        inspector_node.record_bool("factory", self.factory);
        inspector_node.record_bool("format_data_on_corruption", self.format_data_on_corruption);
        inspector_node.record_bool("fvm", self.fvm);
        inspector_node.record_uint("fvm_slice_size", self.fvm_slice_size);
        inspector_node.record_bool("fxfs_blob", self.fxfs_blob);
        inspector_node.record_string("fxfs_crypt_url", &self.fxfs_crypt_url);
        inspector_node.record_bool("gpt", self.gpt);
        inspector_node.record_bool("gpt_all", self.gpt_all);
        inspector_node.record_bool("mbr", self.mbr);
        inspector_node.record_bool("nand", self.nand);
        inspector_node.record_bool("netboot", self.netboot);
        inspector_node.record_bool("no_zxcrypt", self.no_zxcrypt);
        inspector_node.record_bool("ramdisk_image", self.ramdisk_image);
        inspector_node.record_bool("use_disk_migration", self.use_disk_migration);
    }
}