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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Types shared between multiple parts of the Hyper-V UEFI protocols.

use crate::uefi::common::EfiStatus;
use core::fmt::Debug;
use zerocopy::AsBytes;
use zerocopy::FromBytes;
use zerocopy::FromZeroes;
use zerocopy::LittleEndian;
use zerocopy::U64;

/// A 64-bit, unaligned, little-endian encoding of [`EfiStatus`] that does not
/// include the error bit.
///
/// This should be used for the Hyper-V NVRAM and crypto protocols only.
///
/// This encoding cannot be round tripped, nor can it be used to return warnings
/// to the guest. UEFI warning values (non-zero status values that do not have
/// the error bit set) will be turned into errors.
///
/// Luckily, such warnings statuses are rare in practice and are unused by
/// Hyper-V UEFI protocols.
#[repr(transparent)]
#[derive(Copy, Clone, AsBytes, FromBytes, FromZeroes)]
pub struct EfiStatus64NoErrorBit(pub U64<LittleEndian>);

impl From<EfiStatus> for EfiStatus64NoErrorBit {
    fn from(value: EfiStatus) -> Self {
        Self((value.0 & !EfiStatus::ERROR_BIT).into())
    }
}

impl From<EfiStatus64NoErrorBit> for EfiStatus {
    fn from(value: EfiStatus64NoErrorBit) -> Self {
        if value.0.get() == 0 {
            Self::SUCCESS
        } else {
            Self(value.0.get() | Self::ERROR_BIT)
        }
    }
}

impl Debug for EfiStatus64NoErrorBit {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        EfiStatus::from(*self).fmt(f)
    }
}