use fidl::unpersist;
use fidl_cf_sc_internal_fshostconfig::Config as FidlConfig;
use fuchsia_inspect::Node;
use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
#[derive(Debug)]
pub struct Config {
pub blobfs: bool,
pub blobfs_cache_eviction_policy: String,
pub blobfs_initial_inodes: u64,
pub blobfs_max_bytes: u64,
pub blobfs_use_deprecated_padded_format: bool,
pub blobfs_write_compression_algorithm: String,
pub bootpart: bool,
pub check_filesystems: bool,
pub data: bool,
pub data_filesystem_format: String,
pub data_max_bytes: u64,
pub disable_automount: bool,
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 storage_host: 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 config_bytes =
config_vmo.read_to_vec(0, config_size).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![
0x10, 0xaf, 0x40, 0x1f, 0x33, 0x1a, 0xaf, 0x9b, 0x47, 0x88, 0x1c, 0x63, 0xd1, 0x4f,
0x00, 0x77, 0x9c, 0xd5, 0x7f, 0x8c, 0xc6, 0x1f, 0x37, 0x46, 0x35, 0xaa, 0x3b, 0xf8,
0x0a, 0x69, 0xda, 0x89,
];
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_cache_eviction_policy: fidl_config.blobfs_cache_eviction_policy,
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,
blobfs_write_compression_algorithm: fidl_config.blobfs_write_compression_algorithm,
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_automount: fidl_config.disable_automount,
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,
storage_host: fidl_config.storage_host,
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_string("blobfs_cache_eviction_policy", &self.blobfs_cache_eviction_policy);
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_string(
"blobfs_write_compression_algorithm",
&self.blobfs_write_compression_algorithm,
);
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_automount", self.disable_automount);
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("storage_host", self.storage_host);
inspector_node.record_bool("use_disk_migration", self.use_disk_migration);
}
}