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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! All tags related to (U)EFI with the exception of EFI memory tags:
//!
//! - [`EFISdt32Tag`]
//! - [`EFISdt64Tag`]
//! - [`EFIImageHandle32Tag`]
//! - [`EFIImageHandle64Tag`]
//! - [`EFIBootServicesNotExitedTag`]

use crate::TagTypeId;
use crate::{Tag, TagTrait, TagType};
use core::mem::size_of;

/// 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: Self::ID.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
    }
}

impl TagTrait for EFISdt32Tag {
    const ID: TagType = TagType::Efi32;

    fn dst_size(_base_tag: &Tag) {}
}

/// 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: Self::ID.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
    }
}

impl TagTrait for EFISdt64Tag {
    const ID: TagType = TagType::Efi64;

    fn dst_size(_base_tag: &Tag) {}
}

/// 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: Self::ID.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
    }
}

impl TagTrait for EFIImageHandle32Tag {
    const ID: TagType = TagType::Efi32Ih;

    fn dst_size(_base_tag: &Tag) {}
}

/// 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: Self::ID.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
    }
}

impl TagTrait for EFIImageHandle64Tag {
    const ID: TagType = TagType::Efi64Ih;

    fn dst_size(_base_tag: &Tag) {}
}

/// EFI ExitBootServices was not called tag.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct EFIBootServicesNotExitedTag {
    typ: TagTypeId,
    size: u32,
}

impl EFIBootServicesNotExitedTag {
    #[cfg(feature = "builder")]
    pub fn new() -> Self {
        Self::default()
    }
}

#[cfg(feature = "builder")]
impl Default for EFIBootServicesNotExitedTag {
    fn default() -> Self {
        Self {
            typ: TagType::EfiBs.into(),
            size: core::mem::size_of::<Self>().try_into().unwrap(),
        }
    }
}

impl TagTrait for EFIBootServicesNotExitedTag {
    const ID: TagType = TagType::EfiBs;

    fn dst_size(_base_tag: &Tag) {}
}

#[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);
    }
}