Skip to main content

guest_emulation_transport/
api.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Friendly Rust representations of the data sent over the GET.
5
6// These types are re-exported as-is, in order to avoid requiring consumers of
7// the GET and GED to also import get_protocol.
8pub use get_protocol::CreateRamGpaRangeFlags;
9pub use get_protocol::EventLogId;
10pub use get_protocol::GSP_CIPHERTEXT_MAX;
11pub use get_protocol::GspCiphertextContent;
12pub use get_protocol::GspCleartextContent;
13pub use get_protocol::GspExtendedStatusFlags;
14pub use get_protocol::IGVM_ATTEST_MSG_REQ_AGENT_DATA_MAX_SIZE;
15pub use get_protocol::MAX_TRANSFER_SIZE;
16pub use get_protocol::NUMBER_GSP;
17pub use get_protocol::ProtocolVersion;
18pub use get_protocol::SaveGuestVtl2StateFlags;
19pub use get_protocol::VmgsIoStatus;
20
21use guid::Guid;
22use mesh::MeshPayload;
23use std::time::Duration;
24use zerocopy::FromZeros;
25
26/// Device platform settings.
27#[expect(missing_docs)]
28pub mod platform_settings {
29    pub use get_protocol::dps_json::PcatBootDevice;
30
31    use get_protocol::dps_json::EfiDiagnosticsLogLevelType;
32    use get_protocol::dps_json::GuestStateEncryptionPolicy;
33    use get_protocol::dps_json::GuestStateLifetime;
34    use get_protocol::dps_json::HardwareSealingPolicy;
35    use get_protocol::dps_json::ManagementVtlFeatures;
36    use guid::Guid;
37    use inspect::Inspect;
38
39    /// All available device platform settings.
40    #[derive(Debug, Inspect)]
41    pub struct DevicePlatformSettings {
42        pub smbios: Smbios,
43        pub general: General,
44        #[inspect(with = "inspect::iter_by_index")]
45        pub acpi_tables: Vec<Vec<u8>>,
46    }
47
48    /// All available SMBIOS related config.
49    #[derive(Debug, Inspect)]
50    pub struct Smbios {
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        pub system_manufacturer: String,
57        pub system_product_name: String,
58        pub system_version: String,
59        pub system_sku_number: String,
60        pub system_family: String,
61        pub bios_lock_string: String,
62        pub memory_device_serial_number: String,
63
64        // These two arrive base64-encoded as raw bytes in the DPS JSON, so they
65        // are kept as `Vec<u8>` rather than forcing a UTF-8 conversion.
66        pub processor_manufacturer: Vec<u8>,
67        pub processor_version: Vec<u8>,
68        pub processor_id: u64,
69        pub external_clock: u16,
70        pub max_speed: u16,
71        pub current_speed: u16,
72        pub processor_characteristics: u16,
73        pub processor_family2: u16,
74        pub processor_type: u8,
75        pub voltage: u8,
76        pub status: u8,
77        pub processor_upgrade: u8,
78    }
79
80    /// All available general device platform configuration.
81    // DEVNOTE: "general" is code for "not well organized", so if you've got a
82    // better way to organize these settings, do consider cleaning this up a bit!
83    #[derive(Debug, Inspect)]
84    pub struct General {
85        pub secure_boot_enabled: bool,
86        pub secure_boot_template: SecureBootTemplateType,
87        pub bios_guid: Guid,
88        pub console_mode: UefiConsoleMode,
89        pub battery_enabled: bool,
90        pub processor_idle_enabled: bool,
91        pub tpm_enabled: bool,
92        pub ipmi_enabled: bool,
93
94        pub com1_enabled: bool,
95        pub com1_debugger_mode: bool,
96        pub com1_vmbus_redirector: bool,
97        pub com2_enabled: bool,
98        pub com2_debugger_mode: bool,
99        pub com2_vmbus_redirector: bool,
100
101        pub firmware_debugging_enabled: bool,
102        pub hibernation_enabled: bool,
103
104        pub suppress_attestation: Option<bool>,
105        pub generation_id: Option<[u8; 16]>,
106
107        pub legacy_memory_map: bool,
108        pub pause_after_boot_failure: bool,
109        pub pxe_ip_v6: bool,
110        pub measure_additional_pcrs: bool,
111        pub disable_frontpage: bool,
112        pub disable_sha384_pcr: bool,
113        pub media_present_enabled_by_default: bool,
114        pub vpci_boot_enabled: bool,
115        pub memory_protection_mode: MemoryProtectionMode,
116        pub default_boot_always_attempt: bool,
117        pub num_lock_enabled: bool,
118        #[inspect(with = "|x| inspect::iter_by_index(x).map_value(inspect::AsDebug)")]
119        pub pcat_boot_device_order: [PcatBootDevice; 4],
120
121        pub vpci_instance_filter: Option<Guid>,
122        pub nvdimm_count: u16,
123        pub psp_enabled: bool,
124
125        pub vmbus_redirection_enabled: bool,
126        pub always_relay_host_mmio: bool,
127        pub vtl2_settings: Option<underhill_config::Vtl2Settings>,
128
129        pub is_servicing_scenario: bool,
130        pub watchdog_enabled: bool,
131        pub firmware_mode_is_pcat: bool,
132        pub imc_enabled: bool,
133        pub cxl_memory_enabled: bool,
134        #[inspect(debug)]
135        pub efi_diagnostics_log_level: EfiDiagnosticsLogLevelType,
136        #[inspect(debug)]
137        pub guest_state_lifetime: GuestStateLifetime,
138        #[inspect(debug)]
139        pub guest_state_encryption_policy: GuestStateEncryptionPolicy,
140        #[inspect(debug)]
141        pub management_vtl_features: ManagementVtlFeatures,
142        pub force_dma_bounce_enabled: bool,
143        #[inspect(debug)]
144        pub hardware_sealing_policy: HardwareSealingPolicy,
145    }
146
147    #[derive(Copy, Clone, Debug, Inspect)]
148    pub enum MemoryProtectionMode {
149        Disabled = 0,
150        Default = 1,
151        Strict = 2,
152        Relaxed = 3,
153    }
154
155    #[derive(Debug, Inspect)]
156    pub enum UefiConsoleMode {
157        /// video+kbd (having a head)
158        Default = 0,
159        /// headless with COM1 serial console
160        COM1 = 1,
161        /// headless with COM2 serial console
162        COM2 = 2,
163        /// headless
164        None = 3,
165    }
166
167    #[derive(Debug, Inspect)]
168    pub enum SecureBootTemplateType {
169        /// No template to apply.
170        None,
171        /// Apply the Windows only CA.
172        MicrosoftWindows,
173        /// Apply the Microsoft UEFI CA.
174        MicrosoftUefiCertificateAuthority,
175    }
176}
177
178/// Response fields for Guest State Protection sent from the host
179pub struct GuestStateProtection {
180    /// Guest State Protection ciphertext content
181    pub encrypted_gsp: GspCiphertextContent,
182    /// Guest State Protection cleartext content
183    pub decrypted_gsp: [GspCleartextContent; NUMBER_GSP as usize],
184    /// Extended status flags
185    pub extended_status_flags: GspExtendedStatusFlags,
186    /// Randomized new_gsp sent in the GuestStateProtectionRequest message to
187    /// the host
188    pub new_gsp: GspCleartextContent,
189}
190
191impl GuestStateProtection {
192    /// Construct a blank instance of `GuestStateProtection`
193    pub fn new_zeroed() -> GuestStateProtection {
194        GuestStateProtection {
195            encrypted_gsp: GspCiphertextContent::new_zeroed(),
196            decrypted_gsp: [GspCleartextContent::new_zeroed(); NUMBER_GSP as usize],
197            extended_status_flags: GspExtendedStatusFlags::new_zeroed(),
198            new_gsp: GspCleartextContent::new_zeroed(),
199        }
200    }
201}
202
203/// Response fields for Guest State Protection by ID from the host
204#[derive(Copy, Clone)]
205pub struct GuestStateProtectionById {
206    /// Guest State Protection cleartext content
207    pub seed: GspCleartextContent,
208    /// Extended status flags
209    pub extended_status_flags: GspExtendedStatusFlags,
210}
211
212impl GuestStateProtectionById {
213    /// Construct a blank instance of `GuestStateProtectionById`
214    pub fn new_zeroed() -> GuestStateProtectionById {
215        GuestStateProtectionById {
216            seed: GspCleartextContent::new_zeroed(),
217            extended_status_flags: GspExtendedStatusFlags::new_zeroed(),
218        }
219    }
220}
221
222/// Response for IGVM Attest from the host
223#[derive(Clone)]
224pub struct IgvmAttest {
225    /// Response data
226    pub response: Vec<u8>,
227}
228
229/// Response fields for VMGS Get Device Info from the host
230pub struct VmgsGetDeviceInfo {
231    /// Status of the request
232    pub status: VmgsIoStatus,
233    /// Logical sectors
234    pub capacity: u64,
235    /// Bytes per logical sector
236    pub bytes_per_logical_sector: u16,
237    /// Bytes per physical sector
238    pub bytes_per_physical_sector: u16,
239    /// Maximum transfer size bytes
240    pub maximum_transfer_size_bytes: u32,
241}
242
243/// Response fields from Time from the host
244#[derive(Debug, Copy, Clone)]
245pub struct Time {
246    /// UTC, in 100ns units since Jan 1 1601.
247    ///
248    /// (corresponds to `RtlGetSystemTime()` on the Host)
249    pub utc: i64,
250    /// Time zone (as minutes from UTC)
251    pub time_zone: i16,
252}
253
254impl Time {
255    /// Convert this time to a `jiff::Zoned`.
256    pub fn to_jiff(self) -> jiff::Zoned {
257        const NANOS_IN_SECOND: i64 = 1_000_000_000;
258        const NANOS_100_IN_SECOND: i64 = NANOS_IN_SECOND / 100;
259
260        let windows_epoch_unix_seconds = jiff::civil::date(1601, 1, 1)
261            .at(0, 0, 0, 0)
262            .to_zoned(jiff::tz::TimeZone::UTC)
263            .unwrap()
264            .timestamp();
265
266        let host_time_secs = self.utc / NANOS_100_IN_SECOND;
267        let host_time_nanos = (self.utc % NANOS_100_IN_SECOND) * 100;
268
269        let host_time_utc = jiff::Timestamp::new(
270            windows_epoch_unix_seconds.as_second() + host_time_secs,
271            host_time_nanos as i32,
272        )
273        .unwrap();
274
275        let offset_seconds = -self.time_zone as i32 * 60;
276        let tz = jiff::tz::TimeZone::fixed(jiff::tz::Offset::from_seconds(offset_seconds).unwrap());
277        host_time_utc.to_zoned(tz)
278    }
279}
280
281/// A handle returned by `CreateRamGpaRange`, which can be passed to
282/// `ResetRamGpaRange` in order to reset the associated range.
283#[derive(Debug)]
284pub struct RemoteRamGpaRangeHandle(u32);
285
286impl RemoteRamGpaRangeHandle {
287    /// Return a raw u32 that represents this handle
288    pub fn as_raw(&self) -> u32 {
289        self.0
290    }
291
292    /// Create a new [`RemoteRamGpaRangeHandle`] from a raw u32 previously
293    /// returned from `into_raw`.
294    pub fn from_raw(handle: u32) -> Self {
295        RemoteRamGpaRangeHandle(handle)
296    }
297}
298
299/// Request to save Guest state during servicing.
300#[derive(MeshPayload)]
301pub struct GuestSaveRequest {
302    /// GUID associated with the request.
303    pub correlation_id: Guid,
304    /// When to complete the request.
305    pub timeout_hint: Duration,
306    /// Flags bitfield.
307    #[mesh(encoding = "mesh::payload::encoding::ZeroCopyEncoding")]
308    pub capabilities_flags: SaveGuestVtl2StateFlags,
309}