get_protocol/
dps_json.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! The schema defined in this file must match the one defined in
5//! `onecore/vm/schema/mars/Config/Config.Devices.Chipset.mars`.
6
7use bitfield_struct::bitfield;
8use guid::Guid;
9use open_enum::open_enum;
10use serde::Deserialize;
11use serde::Serialize;
12
13/// A type-alias to mark fields as _temporarily_ optional to preserve
14/// build-to-compat compatibility during internal testing.
15///
16/// i.e: a newly added field should be marked as `DevLoopCompatOption` until
17/// we're sure that all hosts that we expect this new underhill version to run
18/// on are updated to send the new field.
19///
20/// It would be **very bad form** to ship a library/binary that includes
21/// `DevLoopCompatOption` fields!
22pub type DevLoopCompatOption<T> = Option<T>;
23
24#[derive(Debug, Default, Deserialize, Serialize)]
25#[serde(rename_all = "PascalCase")]
26pub struct DevicePlatformSettingsV2Json {
27    pub v1: HclDevicePlatformSettings,
28    pub v2: HclDevicePlatformSettingsV2,
29}
30
31// The legacy DPS response's mars schema specifies all fields as [OmitEmpty],
32// which we handle by setting `serde(default)` at the struct level.
33//
34// This is _not_ the case in the newer DPS packet, whereby all fields must be
35// present, specifying "empty values" if the data is not set.
36#[derive(Debug, Default, Deserialize, Serialize)]
37#[serde(default, rename_all = "PascalCase")]
38pub struct HclDevicePlatformSettings {
39    pub secure_boot_enabled: bool,
40    pub secure_boot_template_id: HclSecureBootTemplateId,
41    pub enable_battery: bool,
42    pub enable_processor_idle: bool,
43    pub enable_tpm: bool,
44    pub com1: HclUartSettings,
45    pub com2: HclUartSettings,
46    #[serde(with = "serde_helpers::as_string")]
47    pub bios_guid: Guid,
48    pub console_mode: u8,
49    pub enable_firmware_debugging: bool,
50    pub enable_hibernation: bool,
51    pub serial_number: String,
52    pub base_board_serial_number: String,
53    pub chassis_serial_number: String,
54    pub chassis_asset_tag: String,
55}
56
57// requires a `Default` derive, due to [OmitEmpty] used in parent struct
58#[derive(Debug, Default, Deserialize, Serialize)]
59#[serde(rename_all = "PascalCase")]
60pub enum HclSecureBootTemplateId {
61    #[serde(rename = "None")]
62    #[default]
63    None,
64    #[serde(rename = "MicrosoftWindows")]
65    MicrosoftWindows,
66    #[serde(rename = "MicrosoftUEFICertificateAuthority")]
67    MicrosoftUEFICertificateAuthority,
68}
69
70// requires a `Default` derive, due to [OmitEmpty] used in parent struct
71#[derive(Debug, Default, Deserialize, Serialize)]
72#[serde(default, rename_all = "PascalCase")]
73pub struct HclUartSettings {
74    pub enable_port: bool,
75    pub debugger_mode: bool,
76    pub enable_vmbus_redirector: bool,
77}
78
79#[derive(Debug, Default, Deserialize, Serialize)]
80#[serde(rename_all = "PascalCase")]
81pub struct HclDevicePlatformSettingsV2 {
82    pub r#static: HclDevicePlatformSettingsV2Static,
83    pub dynamic: HclDevicePlatformSettingsV2Dynamic,
84}
85
86/// Boot device order entry used by the PCAT Bios.
87#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
88pub enum PcatBootDevice {
89    Floppy,
90    Optical,
91    HardDrive,
92    Network,
93}
94
95/// Guest state lifetime
96#[derive(Debug, Copy, Clone, Deserialize, Serialize, Default)]
97pub enum GuestStateLifetime {
98    #[default]
99    Default,
100    ReprovisionOnFailure,
101    Reprovision,
102    Ephemeral,
103}
104
105/// Guest state encryption policy
106#[derive(Debug, Copy, Clone, Deserialize, Serialize, Default)]
107pub enum GuestStateEncryptionPolicy {
108    /// Use the best encryption available, allowing fallback.
109    ///
110    /// VMs will be created as or migrated to the best encryption available,
111    /// attempting GspKey, then GspById, and finally leaving the data
112    /// unencrypted if neither are available.
113    #[default]
114    Auto,
115    /// Prefer (or require, if strict) no encryption.
116    ///
117    /// Do not encrypt the guest state unless it is already encrypted and
118    /// strict encryption policy is disabled.
119    None,
120    /// Prefer (or require, if strict) GspById.
121    ///
122    /// This prevents a VM from being created as or migrated to GspKey even
123    /// if it is available. Exisiting GspKey encryption will be used unless
124    /// strict encryption policy is enabled. Fails if the data cannot be
125    /// encrypted.
126    GspById,
127    /// Prefer (or require, if strict) GspKey.
128    ///
129    /// VMs will be created as or migrated to GspKey. GspById encryption will
130    /// be used if GspKey is unavailable unless strict encryption policy is
131    /// enabled. Fails if the data cannot be encrypted.
132    GspKey,
133    /// Use hardware sealing
134    // TODO: update this doc comment once hardware sealing is implemented
135    HardwareSealing,
136}
137
138open_enum! {
139    /// EFI Diagnostics Log Level Filter
140    #[derive(Default, Deserialize, Serialize)]
141    pub enum EfiDiagnosticsLogLevelType: u32 {
142        /// Default log level
143        DEFAULT = 0,
144        /// Include INFO logs
145        INFO = 1,
146        /// All logs
147        FULL = 2,
148    }
149}
150
151/// Management VTL Feature Flags
152#[bitfield(u64)]
153#[derive(Deserialize, Serialize)]
154#[serde(transparent)]
155pub struct ManagementVtlFeatures {
156    pub strict_encryption_policy: bool,
157    pub _reserved1: bool,
158    pub attempt_ak_cert_callback: bool,
159    #[bits(61)]
160    pub _reserved2: u64,
161}
162
163#[derive(Debug, Default, Deserialize, Serialize)]
164#[serde(rename_all = "PascalCase")]
165pub struct HclDevicePlatformSettingsV2Static {
166    //UEFI flags
167    pub legacy_memory_map: bool,
168    pub pause_after_boot_failure: bool,
169    pub pxe_ip_v6: bool,
170    pub measure_additional_pcrs: bool,
171    pub disable_frontpage: bool,
172    pub disable_sha384_pcr: bool,
173    pub media_present_enabled_by_default: bool,
174    pub memory_protection_mode: u8,
175    #[serde(default)]
176    pub default_boot_always_attempt: bool,
177
178    // UEFI info
179    pub vpci_boot_enabled: bool,
180    #[serde(default)]
181    #[serde(with = "serde_helpers::opt_guid_str")]
182    pub vpci_instance_filter: Option<Guid>,
183
184    // PCAT info
185    pub num_lock_enabled: bool,
186    pub pcat_boot_device_order: Option<[PcatBootDevice; 4]>,
187
188    pub smbios: HclDevicePlatformSettingsV2StaticSmbios,
189
190    // Per field serde(default) is required here because that
191    // we can't reply on serde's normal behavior for optional
192    // fields (put None if not present in json) because we're
193    // using custom serialize/deserialize methods
194    #[serde(default)]
195    #[serde(with = "serde_helpers::opt_base64_vec")]
196    pub vtl2_settings: Option<Vec<u8>>,
197
198    pub vmbus_redirection_enabled: bool,
199    pub no_persistent_secrets: bool,
200    pub watchdog_enabled: bool,
201    // this `#[serde(default)]` shouldn't have been necessary, but we let a
202    // `[OmitEmpty]` marker slip past in code review...
203    #[serde(default)]
204    pub firmware_mode_is_pcat: bool,
205    #[serde(default)]
206    pub always_relay_host_mmio: bool,
207    #[serde(default)]
208    pub imc_enabled: bool,
209    #[serde(default)]
210    pub cxl_memory_enabled: bool,
211    #[serde(default)]
212    pub guest_state_lifetime: GuestStateLifetime,
213    #[serde(default)]
214    pub guest_state_encryption_policy: GuestStateEncryptionPolicy,
215    #[serde(default)]
216    pub efi_diagnostics_log_level: EfiDiagnosticsLogLevelType,
217    #[serde(default)]
218    pub management_vtl_features: ManagementVtlFeatures,
219}
220
221#[derive(Debug, Default, Deserialize, Serialize)]
222#[serde(rename_all = "PascalCase")]
223pub struct HclDevicePlatformSettingsV2StaticSmbios {
224    pub system_manufacturer: String,
225    pub system_product_name: String,
226    pub system_version: String,
227    #[serde(rename = "SystemSKUNumber")]
228    pub system_sku_number: String,
229    pub system_family: String,
230    pub bios_lock_string: String,
231    pub memory_device_serial_number: String,
232}
233
234#[derive(Debug, Default, Deserialize, Serialize)]
235#[serde(rename_all = "PascalCase")]
236pub struct HclDevicePlatformSettingsV2Dynamic {
237    pub nvdimm_count: u16,
238    pub enable_psp: bool,
239    pub generation_id_low: u64,
240    pub generation_id_high: u64,
241    pub smbios: HclDevicePlatformSettingsV2DynamicSmbios,
242    pub is_servicing_scenario: bool,
243
244    #[serde(default)]
245    #[serde(with = "serde_helpers::vec_base64_vec")]
246    pub acpi_tables: Vec<Vec<u8>>,
247}
248
249#[derive(Debug, Default, Deserialize, Serialize)]
250#[serde(rename_all = "PascalCase")]
251pub struct HclDevicePlatformSettingsV2DynamicSmbios {
252    #[serde(with = "serde_helpers::base64_vec")]
253    pub processor_manufacturer: Vec<u8>,
254    #[serde(with = "serde_helpers::base64_vec")]
255    pub processor_version: Vec<u8>,
256
257    #[serde(rename = "ProcessorID")]
258    pub processor_id: u64,
259    pub external_clock: u16,
260    pub max_speed: u16,
261    pub current_speed: u16,
262    pub processor_characteristics: u16,
263    pub processor_family2: u16,
264    pub processor_type: u8,
265    pub voltage: u8,
266    pub status: u8,
267    pub processor_upgrade: u8,
268}
269
270#[cfg(test)]
271mod test {
272    use super::*;
273
274    #[test]
275    fn smoke_test_sample() {
276        serde_json::from_slice::<DevicePlatformSettingsV2Json>(include_bytes!(
277            "dps_test_json.json"
278        ))
279        .unwrap();
280    }
281
282    #[test]
283    fn smoke_test_sample_with_vtl2settings() {
284        serde_json::from_slice::<DevicePlatformSettingsV2Json>(include_bytes!(
285            "dps_test_json_with_vtl2settings.json"
286        ))
287        .unwrap();
288    }
289}