Skip to main content

fshost_config/
fshost_config_rust_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_fshostconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0xe6, 0x38, 0xd3, 0x49, 0x49, 0x76, 0x9e, 0xac, 0x89, 0x48, 0x46, 0x54, 0xcd, 0xaa, 0x46, 0xe2,
8    0x99, 0x33, 0x32, 0x99, 0xdf, 0x43, 0x9c, 0xe2, 0xd1, 0xbd, 0x43, 0x38, 0x9d, 0x32, 0x90, 0xd9,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub blob_max_bytes: u64,
14    pub blobfs: bool,
15    pub blobfs_initial_inodes: u64,
16    pub blobfs_use_deprecated_padded_format: bool,
17    pub check_filesystems: bool,
18    pub data: bool,
19    pub data_filesystem_format: String,
20    pub data_max_bytes: u64,
21    pub disable_automount: bool,
22    pub disable_block_watcher: bool,
23    pub factory: bool,
24    pub format_data_on_corruption: bool,
25    pub fvm: bool,
26    pub fvm_slice_size: u64,
27    pub fxfs_blob: bool,
28    pub fxfs_crypt_url: String,
29    pub gpt: bool,
30    pub gpt_all: bool,
31    pub inline_crypto: bool,
32    pub merge_super_and_userdata: bool,
33    pub no_zxcrypt: bool,
34    pub provision_fxfs: bool,
35    pub ramdisk_image: bool,
36    pub starnix_volume_name: String,
37    pub startup_profiling_seconds: u32,
38    pub use_driver_blob_paging: bool,
39}
40impl Config {
41    #[doc = r" Take the config startup handle and parse its contents."]
42    #[doc = r""]
43    #[doc = r" # Panics"]
44    #[doc = r""]
45    #[doc = r" If the config startup handle was already taken or if it is not valid."]
46    pub fn take_from_startup_handle() -> Self {
47        <Self as ComponentConfig>::take_from_startup_handle()
48    }
49    #[doc = r" Parse `Self` from `vmo`."]
50    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
51        <Self as ComponentConfig>::from_vmo(vmo)
52    }
53    #[doc = r" Parse `Self` from `bytes`."]
54    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
55        <Self as ComponentConfig>::from_bytes(bytes)
56    }
57    pub fn record_inspect(&self, inspector_node: &Node) {
58        <Self as ComponentConfig>::record_inspect(self, inspector_node)
59    }
60}
61impl ComponentConfig for Config {
62    #[doc = r" Parse `Self` from `bytes`."]
63    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
64        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
65        let checksum_len_bytes: [u8; 2] =
66            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
67        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
68        let (observed_checksum, bytes) =
69            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
70        if observed_checksum != EXPECTED_CHECKSUM {
71            return Err(Error::ChecksumMismatch {
72                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
73                observed_checksum: observed_checksum.to_vec(),
74            });
75        }
76        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
77        Ok(Self {
78            blob_max_bytes: fidl_config.blob_max_bytes,
79            blobfs: fidl_config.blobfs,
80            blobfs_initial_inodes: fidl_config.blobfs_initial_inodes,
81            blobfs_use_deprecated_padded_format: fidl_config.blobfs_use_deprecated_padded_format,
82            check_filesystems: fidl_config.check_filesystems,
83            data: fidl_config.data,
84            data_filesystem_format: fidl_config.data_filesystem_format,
85            data_max_bytes: fidl_config.data_max_bytes,
86            disable_automount: fidl_config.disable_automount,
87            disable_block_watcher: fidl_config.disable_block_watcher,
88            factory: fidl_config.factory,
89            format_data_on_corruption: fidl_config.format_data_on_corruption,
90            fvm: fidl_config.fvm,
91            fvm_slice_size: fidl_config.fvm_slice_size,
92            fxfs_blob: fidl_config.fxfs_blob,
93            fxfs_crypt_url: fidl_config.fxfs_crypt_url,
94            gpt: fidl_config.gpt,
95            gpt_all: fidl_config.gpt_all,
96            inline_crypto: fidl_config.inline_crypto,
97            merge_super_and_userdata: fidl_config.merge_super_and_userdata,
98            no_zxcrypt: fidl_config.no_zxcrypt,
99            provision_fxfs: fidl_config.provision_fxfs,
100            ramdisk_image: fidl_config.ramdisk_image,
101            starnix_volume_name: fidl_config.starnix_volume_name,
102            startup_profiling_seconds: fidl_config.startup_profiling_seconds,
103            use_driver_blob_paging: fidl_config.use_driver_blob_paging,
104        })
105    }
106    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
107        let fidl_config = FidlConfig {
108            blob_max_bytes: self.blob_max_bytes.clone(),
109            blobfs: self.blobfs.clone(),
110            blobfs_initial_inodes: self.blobfs_initial_inodes.clone(),
111            blobfs_use_deprecated_padded_format: self.blobfs_use_deprecated_padded_format.clone(),
112            check_filesystems: self.check_filesystems.clone(),
113            data: self.data.clone(),
114            data_filesystem_format: self.data_filesystem_format.clone(),
115            data_max_bytes: self.data_max_bytes.clone(),
116            disable_automount: self.disable_automount.clone(),
117            disable_block_watcher: self.disable_block_watcher.clone(),
118            factory: self.factory.clone(),
119            format_data_on_corruption: self.format_data_on_corruption.clone(),
120            fvm: self.fvm.clone(),
121            fvm_slice_size: self.fvm_slice_size.clone(),
122            fxfs_blob: self.fxfs_blob.clone(),
123            fxfs_crypt_url: self.fxfs_crypt_url.clone(),
124            gpt: self.gpt.clone(),
125            gpt_all: self.gpt_all.clone(),
126            inline_crypto: self.inline_crypto.clone(),
127            merge_super_and_userdata: self.merge_super_and_userdata.clone(),
128            no_zxcrypt: self.no_zxcrypt.clone(),
129            provision_fxfs: self.provision_fxfs.clone(),
130            ramdisk_image: self.ramdisk_image.clone(),
131            starnix_volume_name: self.starnix_volume_name.clone(),
132            startup_profiling_seconds: self.startup_profiling_seconds.clone(),
133            use_driver_blob_paging: self.use_driver_blob_paging.clone(),
134        };
135        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
136        let mut bytes = Vec::with_capacity(
137            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
138        );
139        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
140        bytes.extend_from_slice(EXPECTED_CHECKSUM);
141        bytes.append(&mut fidl_bytes);
142        Ok(bytes)
143    }
144    fn record_inspect(&self, inspector_node: &Node) {
145        inspector_node.record_uint("blob_max_bytes", self.blob_max_bytes);
146        inspector_node.record_bool("blobfs", self.blobfs);
147        inspector_node.record_uint("blobfs_initial_inodes", self.blobfs_initial_inodes);
148        inspector_node.record_bool(
149            "blobfs_use_deprecated_padded_format",
150            self.blobfs_use_deprecated_padded_format,
151        );
152        inspector_node.record_bool("check_filesystems", self.check_filesystems);
153        inspector_node.record_bool("data", self.data);
154        inspector_node.record_string("data_filesystem_format", &self.data_filesystem_format);
155        inspector_node.record_uint("data_max_bytes", self.data_max_bytes);
156        inspector_node.record_bool("disable_automount", self.disable_automount);
157        inspector_node.record_bool("disable_block_watcher", self.disable_block_watcher);
158        inspector_node.record_bool("factory", self.factory);
159        inspector_node.record_bool("format_data_on_corruption", self.format_data_on_corruption);
160        inspector_node.record_bool("fvm", self.fvm);
161        inspector_node.record_uint("fvm_slice_size", self.fvm_slice_size);
162        inspector_node.record_bool("fxfs_blob", self.fxfs_blob);
163        inspector_node.record_string("fxfs_crypt_url", &self.fxfs_crypt_url);
164        inspector_node.record_bool("gpt", self.gpt);
165        inspector_node.record_bool("gpt_all", self.gpt_all);
166        inspector_node.record_bool("inline_crypto", self.inline_crypto);
167        inspector_node.record_bool("merge_super_and_userdata", self.merge_super_and_userdata);
168        inspector_node.record_bool("no_zxcrypt", self.no_zxcrypt);
169        inspector_node.record_bool("provision_fxfs", self.provision_fxfs);
170        inspector_node.record_bool("ramdisk_image", self.ramdisk_image);
171        inspector_node.record_string("starnix_volume_name", &self.starnix_volume_name);
172        inspector_node
173            .record_uint("startup_profiling_seconds", self.startup_profiling_seconds as u64);
174        inspector_node.record_bool("use_driver_blob_paging", self.use_driver_blob_paging);
175    }
176}