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
161
162
163
164
165
166
167
168
169
170
//! All MBI tags related to (U)EFI.

use crate::TagType;
use crate::TagTypeId;
use core::convert::TryInto;
use core::mem::size_of;

#[cfg(feature = "builder")]
use crate::builder::traits::StructAsBytes;

/// EFI system table in 32 bit mode tag.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct EFISdt32Tag {
    typ: TagTypeId,
    size: u32,
    pointer: u32,
}

impl EFISdt32Tag {
    /// Create a new tag to pass the EFI32 System Table pointer.
    pub fn new(pointer: u32) -> Self {
        Self {
            typ: TagType::Efi32.into(),
            size: size_of::<Self>().try_into().unwrap(),
            pointer,
        }
    }

    /// The physical address of a i386 EFI system table.
    pub fn sdt_address(&self) -> usize {
        self.pointer as usize
    }
}

#[cfg(feature = "builder")]
impl StructAsBytes for EFISdt32Tag {
    fn byte_size(&self) -> usize {
        size_of::<Self>()
    }
}

/// EFI system table in 64 bit mode tag.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct EFISdt64Tag {
    typ: TagTypeId,
    size: u32,
    pointer: u64,
}

impl EFISdt64Tag {
    /// Create a new tag to pass the EFI64 System Table pointer.
    pub fn new(pointer: u64) -> Self {
        Self {
            typ: TagType::Efi64.into(),
            size: size_of::<Self>().try_into().unwrap(),
            pointer,
        }
    }

    /// The physical address of a x86_64 EFI system table.
    pub fn sdt_address(&self) -> usize {
        self.pointer as usize
    }
}

#[cfg(feature = "builder")]
impl StructAsBytes for EFISdt64Tag {
    fn byte_size(&self) -> usize {
        size_of::<Self>()
    }
}

/// Tag that contains the pointer to the boot loader's UEFI image handle
/// (32-bit).
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct EFIImageHandle32Tag {
    typ: TagTypeId,
    size: u32,
    pointer: u32,
}

impl EFIImageHandle32Tag {
    #[cfg(feature = "builder")]
    pub fn new(pointer: u32) -> Self {
        Self {
            typ: TagType::Efi32Ih.into(),
            size: size_of::<Self>().try_into().unwrap(),
            pointer,
        }
    }

    /// Returns the physical address of the EFI image handle.
    pub fn image_handle(&self) -> usize {
        self.pointer as usize
    }
}

#[cfg(feature = "builder")]
impl StructAsBytes for EFIImageHandle32Tag {
    fn byte_size(&self) -> usize {
        size_of::<Self>()
    }
}

/// Tag that contains the pointer to the boot loader's UEFI image handle
/// (64-bit).
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct EFIImageHandle64Tag {
    typ: TagTypeId,
    size: u32,
    pointer: u64,
}

impl EFIImageHandle64Tag {
    #[cfg(feature = "builder")]
    pub fn new(pointer: u64) -> Self {
        Self {
            typ: TagType::Efi64Ih.into(),
            size: size_of::<Self>().try_into().unwrap(),
            pointer,
        }
    }

    /// Returns the physical address of the EFI image handle.
    pub fn image_handle(&self) -> usize {
        self.pointer as usize
    }
}

#[cfg(feature = "builder")]
impl StructAsBytes for EFIImageHandle64Tag {
    fn byte_size(&self) -> usize {
        size_of::<Self>()
    }
}

#[cfg(all(test, feature = "builder"))]
mod tests {
    use super::{EFIImageHandle32Tag, EFIImageHandle64Tag, EFISdt32Tag, EFISdt64Tag};

    const ADDR: usize = 0xABCDEF;

    #[test]
    fn test_build_eftsdt32() {
        let tag = EFISdt32Tag::new(ADDR.try_into().unwrap());
        assert_eq!(tag.sdt_address(), ADDR);
    }

    #[test]
    fn test_build_eftsdt64() {
        let tag = EFISdt64Tag::new(ADDR.try_into().unwrap());
        assert_eq!(tag.sdt_address(), ADDR);
    }

    #[test]
    fn test_build_eftih32() {
        let tag = EFIImageHandle32Tag::new(ADDR.try_into().unwrap());
        assert_eq!(tag.image_handle(), ADDR);
    }

    #[test]
    fn test_build_eftih64() {
        let tag = EFIImageHandle64Tag::new(ADDR.try_into().unwrap());
        assert_eq!(tag.image_handle(), ADDR);
    }
}