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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Nvram types defined in `BiosInterface.h`

use self::packed_nums::*;
use crate::hyperv::common::EfiStatus64NoErrorBit;
use bitfield_struct::bitfield;
use guid::Guid;
use open_enum::open_enum;
use zerocopy::AsBytes;
use zerocopy::FromBytes;
use zerocopy::FromZeroes;

#[allow(non_camel_case_types)]
mod packed_nums {
    pub type u64_ne = zerocopy::U64<zerocopy::NativeEndian>;
}

open_enum! {
    /// Command types for NVRAM_COMMAND_DESCRIPTOR.
    ///
    /// These correlate with the semantics of the UEFI runtime variable services.
    ///
    /// MsvmPkg: `NVRAM_COMMAND`
    #[derive(AsBytes, FromBytes, FromZeroes)]
    pub enum NvramCommand: u32 {
        GET_VARIABLE = 0,
        SET_VARIABLE = 1,
        GET_FIRST_VARIABLE_NAME = 2,
        GET_NEXT_VARIABLE_NAME = 3,
        QUERY_INFO = 4,
        SIGNAL_RUNTIME = 5,
        DEBUG_STRING = 6,
    }
}

/// MsvmPkg: `NVRAM_COMMAND_DESCRIPTOR`
#[repr(C)]
#[derive(Debug, Clone, Copy, AsBytes, FromBytes, FromZeroes)]
pub struct NvramCommandDescriptor {
    pub command: NvramCommand,
    pub status: EfiStatus64NoErrorBit,
}

/// MsvmPkg: `NVRAM_COMMAND_DESCRIPTOR`
#[repr(C)]
#[derive(Debug, AsBytes, FromBytes, FromZeroes)]
pub struct NvramDebugStringCommand {
    pub padding: u32,
    pub address: u64_ne,
    pub len: u32,
}

/// MsvmPkg: `NVRAM_COMMAND_DESCRIPTOR`
#[repr(C)]
#[derive(Debug, AsBytes, FromBytes, FromZeroes)]
pub struct NvramVariableCommand {
    /// UEFI variable attributes associated with the variable: access rights
    /// (RT/BS).
    ///
    /// Used as input for the SetVariable command. Used as output for the
    /// GetVariable command.
    pub attributes: u32,

    /// GPA of the buffer containing a 16-bit unicode variable name.
    ///
    /// Memory at this location is read for the GetVariable, SetVariable,
    /// GetNextVariable command. Memory at this location is written to for the
    /// GetNextVariable command.
    pub name_address: u64_ne,

    /// Size in bytes of the buffer at VariableNameAddress.
    ///
    /// Used as input for GetVariable, SetVariable, and GetNextVariable
    /// commands. Used as output for the GetNextVariable command.
    pub name_bytes: u32,

    /// A GUID comprising the other half of the variable name.
    ///
    /// Used as input for GetVariable, SetVariable, and GetNextVariable
    /// commands. Used as output for the GetNextVariable command.
    pub vendor_guid: Guid,

    /// GPA of the buffer containing variable data. Memory at this location is
    /// written to for the GetVariable command.
    ///
    /// Memory at this location is read for the SetVariable command.
    pub data_address: u64_ne,

    /// Size of the buffer at VariableDataAddress.
    ///
    /// Used as input for the GetVariable command. Used as output for the
    /// GetVariable and SetVariable commands.
    pub data_bytes: u32,
}

/// MsvmPkg: `NVRAM_COMMAND_DESCRIPTOR`
#[repr(C)]
#[derive(Debug, AsBytes, FromBytes, FromZeroes)]
pub struct NvramQueryInfo {
    /// Attribute mask, controls variable type for which the information is
    /// returned.
    ///
    /// Used as an input for the QueryInfo command.
    pub attributes: u32,

    // These are outputs for the QueryInfo command.
    pub maximum_variable_storage: u64_ne,
    pub remaining_variable_storage: u64_ne,
    pub maximum_variable_size: u64_ne,
}

/// MsvmPkg: `NVRAM_COMMAND_DESCRIPTOR`
#[bitfield(u64)]
#[derive(AsBytes, FromBytes, FromZeroes)]
pub struct SignalRuntimeCommandFlags {
    pub vsm_aware: bool,
    #[bits(63)]
    _reserved: u64,
}

/// MsvmPkg: `NVRAM_COMMAND_DESCRIPTOR`
///
/// ```text
/// union
/// {
///     struct
///     {
///         UINT64 VsmAware : 1;
///         UINT64 Unused   : 63;
///     } S;
///     UINT64 AsUINT64;
/// } SignalRuntimeCommand;
/// ```
#[repr(C)]
#[derive(Debug, AsBytes, FromBytes, FromZeroes)]
pub struct NvramSignalRuntimeCommand {
    pub flags: SignalRuntimeCommandFlags,
}

pub mod vars {
    use guid::Guid;

    const SECURE_BOOT_ENABLE_GUID: Guid =
        Guid::from_static_str("f0a30bc7-af08-4556-99c4-001009c93a44");

    pub const MSFT_SECURE_BOOT_PRODUCTION_GUID: Guid =
        Guid::from_static_str("77fa9abd-0359-4d32-bd60-28f4e78f784b");

    const EFI_HYPERV_PRIVATE_GUID: Guid =
        Guid::from_static_str("610b9e98-c6f6-47f8-8b47-2d2da0d52a91");

    defn_nvram_var!(SECURE_BOOT_ENABLE = (SECURE_BOOT_ENABLE_GUID, "SecureBootEnable"));
    defn_nvram_var!(CURRENT_POLICY = (MSFT_SECURE_BOOT_PRODUCTION_GUID, "CurrentPolicy"));
    defn_nvram_var!(OS_LOADER_INDICATIONS = (EFI_HYPERV_PRIVATE_GUID, "OsLoaderIndications"));
    defn_nvram_var!(
        OS_LOADER_INDICATIONS_SUPPORTED = (EFI_HYPERV_PRIVATE_GUID, "OsLoaderIndicationsSupported")
    );
}