multiboot2/
builder.rs

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! Module for [`Builder`].

use crate::apm::ApmTag;
use crate::bootdev::BootdevTag;
use crate::network::NetworkTag;
use crate::{
    BasicMemoryInfoTag, BootInformationHeader, BootLoaderNameTag, CommandLineTag,
    EFIBootServicesNotExitedTag, EFIImageHandle32Tag, EFIImageHandle64Tag, EFIMemoryMapTag,
    EFISdt32Tag, EFISdt64Tag, ElfSectionsTag, EndTag, FramebufferTag, ImageLoadPhysAddrTag,
    MemoryMapTag, ModuleTag, RsdpV1Tag, RsdpV2Tag, SmbiosTag, TagHeader, TagType, VBEInfoTag,
};
use alloc::boxed::Box;
use alloc::vec::Vec;
use multiboot2_common::{DynSizedStructure, MaybeDynSized, new_boxed};

/// Builder for a Multiboot2 header information.
// #[derive(Debug)]
#[derive(Debug)]
pub struct Builder {
    cmdline: Option<Box<CommandLineTag>>,
    bootloader: Option<Box<BootLoaderNameTag>>,
    modules: Vec<Box<ModuleTag>>,
    meminfo: Option<BasicMemoryInfoTag>,
    bootdev: Option<BootdevTag>,
    mmap: Option<Box<MemoryMapTag>>,
    vbe: Option<VBEInfoTag>,
    framebuffer: Option<Box<FramebufferTag>>,
    elf_sections: Option<Box<ElfSectionsTag>>,
    apm: Option<ApmTag>,
    efi32: Option<EFISdt32Tag>,
    efi64: Option<EFISdt64Tag>,
    smbios: Vec<Box<SmbiosTag>>,
    rsdpv1: Option<RsdpV1Tag>,
    rsdpv2: Option<RsdpV2Tag>,
    network: Option<Box<NetworkTag>>,
    efi_mmap: Option<Box<EFIMemoryMapTag>>,
    efi_bs: Option<EFIBootServicesNotExitedTag>,
    efi32_ih: Option<EFIImageHandle32Tag>,
    efi64_ih: Option<EFIImageHandle64Tag>,
    image_load_addr: Option<ImageLoadPhysAddrTag>,
    custom_tags: Vec<Box<DynSizedStructure<TagHeader>>>,
}

impl Default for Builder {
    fn default() -> Self {
        Self::new()
    }
}

impl Builder {
    /// Creates a new builder.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            cmdline: None,
            bootloader: None,
            modules: vec![],
            meminfo: None,
            bootdev: None,
            mmap: None,
            vbe: None,
            framebuffer: None,
            elf_sections: None,
            apm: None,
            efi32: None,
            efi64: None,
            smbios: vec![],
            rsdpv1: None,
            rsdpv2: None,
            efi_mmap: None,
            network: None,
            efi_bs: None,
            efi32_ih: None,
            efi64_ih: None,
            image_load_addr: None,
            custom_tags: vec![],
        }
    }

    /// Sets the [`CommandLineTag`] tag.
    #[must_use]
    pub fn cmdline(mut self, cmdline: Box<CommandLineTag>) -> Self {
        self.cmdline = Some(cmdline);
        self
    }

    /// Sets the [`BootLoaderNameTag`] tag.
    #[must_use]
    pub fn bootloader(mut self, bootloader: Box<BootLoaderNameTag>) -> Self {
        self.bootloader = Some(bootloader);
        self
    }

    /// Adds a [`ModuleTag`] tag.
    #[must_use]
    pub fn add_module(mut self, module: Box<ModuleTag>) -> Self {
        self.modules.push(module);
        self
    }

    /// Sets the [`BasicMemoryInfoTag`] tag.
    #[must_use]
    pub const fn meminfo(mut self, meminfo: BasicMemoryInfoTag) -> Self {
        self.meminfo = Some(meminfo);
        self
    }

    /// Sets the [`BootdevTag`] tag.
    #[must_use]
    pub const fn bootdev(mut self, bootdev: BootdevTag) -> Self {
        self.bootdev = Some(bootdev);
        self
    }

    /// Sets the [`MemoryMapTag`] tag.
    #[must_use]
    pub fn mmap(mut self, mmap: Box<MemoryMapTag>) -> Self {
        self.mmap = Some(mmap);
        self
    }

    /// Sets the [`VBEInfoTag`] tag.
    #[must_use]
    pub const fn vbe(mut self, vbe: VBEInfoTag) -> Self {
        self.vbe = Some(vbe);
        self
    }

    /// Sets the [`FramebufferTag`] tag.
    #[must_use]
    pub fn framebuffer(mut self, framebuffer: Box<FramebufferTag>) -> Self {
        self.framebuffer = Some(framebuffer);
        self
    }

    /// Sets the [`ElfSectionsTag`] tag.
    #[must_use]
    pub fn elf_sections(mut self, elf_sections: Box<ElfSectionsTag>) -> Self {
        self.elf_sections = Some(elf_sections);
        self
    }

    /// Sets the [`ApmTag`] tag.
    #[must_use]
    pub const fn apm(mut self, apm: ApmTag) -> Self {
        self.apm = Some(apm);
        self
    }

    /// Sets the [`EFISdt32Tag`] tag.
    #[must_use]
    pub const fn efi32(mut self, efi32: EFISdt32Tag) -> Self {
        self.efi32 = Some(efi32);
        self
    }

    /// Sets the [`EFISdt64Tag`] tag.
    #[must_use]
    pub const fn efi64(mut self, efi64: EFISdt64Tag) -> Self {
        self.efi64 = Some(efi64);
        self
    }

    /// Adds a [`SmbiosTag`] tag.
    #[must_use]
    pub fn add_smbios(mut self, smbios: Box<SmbiosTag>) -> Self {
        self.smbios.push(smbios);
        self
    }

    /// Sets the [`RsdpV1Tag`] tag.
    #[must_use]
    pub const fn rsdpv1(mut self, rsdpv1: RsdpV1Tag) -> Self {
        self.rsdpv1 = Some(rsdpv1);
        self
    }

    /// Sets the [`RsdpV2Tag`] tag.
    #[must_use]
    pub const fn rsdpv2(mut self, rsdpv2: RsdpV2Tag) -> Self {
        self.rsdpv2 = Some(rsdpv2);
        self
    }

    /// Sets the [`EFIMemoryMapTag`] tag.
    #[must_use]
    pub fn efi_mmap(mut self, efi_mmap: Box<EFIMemoryMapTag>) -> Self {
        self.efi_mmap = Some(efi_mmap);
        self
    }

    /// Sets the [`NetworkTag`] tag.
    #[must_use]
    pub fn network(mut self, network: Box<NetworkTag>) -> Self {
        self.network = Some(network);
        self
    }

    /// Sets the [`EFIBootServicesNotExitedTag`] tag.
    #[must_use]
    pub const fn efi_bs(mut self, efi_bs: EFIBootServicesNotExitedTag) -> Self {
        self.efi_bs = Some(efi_bs);
        self
    }

    /// Sets the [`EFIImageHandle32Tag`] tag.
    #[must_use]
    pub const fn efi32_ih(mut self, efi32_ih: EFIImageHandle32Tag) -> Self {
        self.efi32_ih = Some(efi32_ih);
        self
    }

    /// Sets the [`EFIImageHandle64Tag`] tag.
    #[must_use]
    pub const fn efi64_ih(mut self, efi64_ih: EFIImageHandle64Tag) -> Self {
        self.efi64_ih = Some(efi64_ih);
        self
    }

    /// Sets the [`ImageLoadPhysAddrTag`] tag.
    #[must_use]
    pub const fn image_load_addr(mut self, image_load_addr: ImageLoadPhysAddrTag) -> Self {
        self.image_load_addr = Some(image_load_addr);
        self
    }

    /// Adds a custom tag.
    #[must_use]
    pub fn add_custom_tag(mut self, custom_tag: Box<DynSizedStructure<TagHeader>>) -> Self {
        if let TagType::Custom(_c) = custom_tag.header().typ.into() {
            self.custom_tags.push(custom_tag);
        } else {
            panic!("Only for custom types!");
        }
        self
    }

    /// Returns properly aligned bytes on the heap representing a valid
    /// Multiboot2 header structure.
    #[must_use]
    pub fn build(self) -> Box<DynSizedStructure<BootInformationHeader>> {
        let header = BootInformationHeader::new(0);
        let mut byte_refs = Vec::new();
        if let Some(tag) = self.cmdline.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.bootloader.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        for i in &self.modules {
            byte_refs.push(i.as_bytes().as_ref());
        }
        if let Some(tag) = self.meminfo.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.bootdev.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.mmap.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.vbe.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.framebuffer.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.elf_sections.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.apm.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.efi32.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.efi64.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        for i in &self.smbios {
            byte_refs.push(i.as_bytes().as_ref());
        }
        if let Some(tag) = self.rsdpv1.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.rsdpv2.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.efi_mmap.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.efi_bs.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.efi32_ih.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.efi64_ih.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        if let Some(tag) = self.image_load_addr.as_ref() {
            byte_refs.push(tag.as_bytes().as_ref());
        }
        for i in &self.custom_tags {
            byte_refs.push(i.as_bytes().as_ref());
        }
        let end_tag = EndTag::default();
        byte_refs.push(end_tag.as_bytes().as_ref());
        new_boxed(header, byte_refs.as_slice())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        BootInformation, FramebufferType, MemoryArea, MemoryAreaType, VBEControlInfo, VBEModeInfo,
    };
    use uefi_raw::table::boot::MemoryDescriptor;

    #[test]
    fn build_and_parse() {
        let builder = Builder::new()
            .cmdline(CommandLineTag::new("this is a command line"))
            .bootloader(BootLoaderNameTag::new("this is the bootloader"))
            .add_module(ModuleTag::new(0x1000, 0x2000, "module 1"))
            .add_module(ModuleTag::new(0x3000, 0x4000, "module 2"))
            .meminfo(BasicMemoryInfoTag::new(0x4000, 0x5000))
            .bootdev(BootdevTag::new(0x00, 0x00, 0x00))
            .mmap(MemoryMapTag::new(&[MemoryArea::new(
                0x1000000,
                0x1000,
                MemoryAreaType::Available,
            )]))
            .vbe(VBEInfoTag::new(
                42,
                2,
                4,
                9,
                VBEControlInfo::default(),
                VBEModeInfo::default(),
            ))
            // Currently causes UB.
            .framebuffer(FramebufferTag::new(
                0x1000,
                1,
                756,
                1024,
                8,
                FramebufferType::Text,
            ))
            .elf_sections(ElfSectionsTag::new(0, 32, 0, &[]))
            .apm(ApmTag::new(0, 0, 0, 0, 0, 0, 0, 0, 0))
            .efi32(EFISdt32Tag::new(0x1000))
            .efi64(EFISdt64Tag::new(0x1000))
            .add_smbios(SmbiosTag::new(0, 0, &[1, 2, 3]))
            .add_smbios(SmbiosTag::new(1, 1, &[4, 5, 6]))
            .rsdpv1(RsdpV1Tag::new(0, *b"abcdef", 5, 6))
            .rsdpv2(RsdpV2Tag::new(0, *b"abcdef", 5, 6, 5, 4, 7))
            .efi_mmap(EFIMemoryMapTag::new_from_descs(&[
                MemoryDescriptor::default(),
                MemoryDescriptor::default(),
            ]))
            .network(NetworkTag::new(&[0; 1500]))
            .efi_bs(EFIBootServicesNotExitedTag::new())
            .efi32_ih(EFIImageHandle32Tag::new(0x1000))
            .efi64_ih(EFIImageHandle64Tag::new(0x1000))
            .image_load_addr(ImageLoadPhysAddrTag::new(0x1000))
            .add_custom_tag(new_boxed::<DynSizedStructure<TagHeader>>(
                TagHeader::new(TagType::Custom(0x1337), 0),
                &[],
            ));

        let structure = builder.build();

        let info = unsafe { BootInformation::load(structure.as_bytes().as_ptr().cast()) }.unwrap();
        for tag in info.tags() {
            // Mainly a test for Miri.
            dbg!(tag.header(), tag.payload().len());
        }
        eprintln!("{info:#x?}")
    }
}