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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright © 2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Functions to access UEFI-PI defined `Hand-Off Block` (HOB) list.

use core::mem::size_of;
use scroll::Pread;

use crate::pi::hob::*;

const SIZE_4G: u64 = 0x100000000u64;

/// Validate and align to next HOB header position.
pub fn align_to_next_hob_offset(cap: usize, offset: usize, length: u16) -> Option<usize> {
    if length == 0 || length > (u16::MAX - 7) {
        None
    } else {
        let offset = offset.checked_add((length as usize + 7) / 8 * 8)?;
        if offset < cap {
            Some(offset)
        } else {
            None
        }
    }
}

/// Seek to next available HOB entry in the buffer.
pub fn seek_to_next_hob(hob_list: &'_ [u8]) -> Option<&'_ [u8]> {
    let header: Header = hob_list.pread(0).ok()?;
    let offset = align_to_next_hob_offset(hob_list.len(), 0, header.length)?;

    Some(&hob_list[offset..])
}

// Check hob length equal efi_end_of_hob_list of HandoffInfoTable
pub fn check_hob_length(hob: &[u8], hob_length: usize) -> Option<&[u8]> {
    let phit: HandoffInfoTable = hob.pread(0).ok()?;
    let end: u64 = phit.efi_end_of_hob_list;
    if phit.header.r#type == HOB_TYPE_HANDOFF
        && phit.header.length as usize >= size_of::<HandoffInfoTable>()
    {
        if hob_length as u64 == end.checked_sub(hob.as_ptr() as u64)? {
            Some(&hob[0..hob_length])
        } else {
            None
        }
    } else {
        None
    }
}

// Check the integrity of HOB list that is got from untrusted input
// and return the HOB slice with real HOB length
pub fn check_hob_integrity(hob_list: &[u8]) -> Option<&[u8]> {
    let mut offset = 0;
    let hob_list_len = hob_list.len();

    // A valid HOB should have an HandoffInfoTable at least
    if hob_list_len < size_of::<HandoffInfoTable>() {
        return None;
    }
    speculation_barrier();

    loop {
        let hob = &hob_list[offset..];
        if offset.checked_add(size_of::<Header>())? > hob_list_len {
            return None;
        }
        speculation_barrier();

        let header: Header = hob.pread(0).ok()?;

        // A valid HOB should has non-zero length and zero reserved field,
        if header.length == 0 || header.length as usize > hob.len() || header.reserved != 0 {
            return None;
        }
        speculation_barrier();

        match header.r#type {
            HOB_TYPE_HANDOFF => {
                if header.length as usize != size_of::<HandoffInfoTable>()
                    || offset + size_of::<HandoffInfoTable>() > hob_list_len
                {
                    return None;
                }
                let phit_hob: HandoffInfoTable = hob.pread(0).ok()?;

                // This address must be 4-KB aligned to meet page restrictions
                if phit_hob.efi_memory_top % 0x1000 != 0 {
                    log::info!(
                        "PHIT HOB does not hold a 4-KB aligned EFI memory top address: {:x}\n",
                        phit_hob.efi_memory_top
                    );
                    return None;
                }
            }
            HOB_TYPE_END_OF_HOB_LIST => {
                let hob_length = offset + size_of::<Header>();

                return check_hob_length(hob_list, hob_length);
            }
            HOB_TYPE_RESOURCE_DESCRIPTOR => {
                if header.length as usize != size_of::<ResourceDescription>() {
                    return None;
                }
                let resource_hob: ResourceDescription = hob.pread(0).ok()?;
                if resource_hob.resource_type >= RESOURCE_MAX_MEMORY_TYPE
                    || resource_hob.resource_attribute & (!RESOURCE_ATTRIBUTE_ALL) != 0
                {
                    log::info!("Invalid resource type or attributes:\n");
                    resource_hob.dump();
                    return None;
                }

                resource_hob
                    .physical_start
                    .checked_add(resource_hob.resource_length)?;
            }
            HOB_TYPE_MEMORY_ALLOCATION => {
                if header.length as usize != size_of::<MemoryAllocation>() {
                    return None;
                }
            }
            HOB_TYPE_FV => {
                if header.length as usize != size_of::<FirmwareVolume>() {
                    return None;
                }
            }
            HOB_TYPE_FV2 => {
                if header.length as usize != size_of::<FirmwareVolume2>() {
                    return None;
                }
            }
            HOB_TYPE_FV3 => {
                if header.length as usize != size_of::<FirmwareVolume3>() {
                    return None;
                }
            }
            HOB_TYPE_CPU => {
                if header.length as usize != size_of::<Cpu>()
                    || offset + size_of::<Cpu>() > hob_list_len
                {
                    return None;
                }

                let cpu_hob: Cpu = hob.pread(0).ok()?;
                // Reserved field is expected to be zero
                if cpu_hob.reserved != [0u8; 6] {
                    return None;
                }
            }
            HOB_TYPE_GUID_EXTENSION => {
                // GUID Extension HOB has variable length
            }
            // Unsupported types
            _ => return None,
        }
        offset = align_to_next_hob_offset(hob_list_len, offset, header.length)?;
        speculation_barrier();
    }
}

/// Dump the HOB list.
pub fn dump_hob(hob_list: &[u8]) -> Option<()> {
    let mut offset = 0;

    loop {
        let hob = &hob_list[offset..];
        let header: Header = hob.pread(0).ok()?;

        match header.r#type {
            HOB_TYPE_HANDOFF => {
                let phit_hob: HandoffInfoTable = hob.pread(0).ok()?;
                phit_hob.dump();
            }
            HOB_TYPE_RESOURCE_DESCRIPTOR => {
                let resource_hob: ResourceDescription = hob.pread(0).ok()?;
                resource_hob.dump();
            }
            HOB_TYPE_MEMORY_ALLOCATION => {
                let allocation_hob: MemoryAllocation = hob.pread(0).ok()?;
                allocation_hob.dump();
            }
            HOB_TYPE_FV => {
                let fv_hob: FirmwareVolume = hob.pread(0).ok()?;
                fv_hob.dump();
            }
            HOB_TYPE_CPU => {
                let cpu_hob: Cpu = hob.pread(0).ok()?;
                cpu_hob.dump();
            }
            HOB_TYPE_END_OF_HOB_LIST => return Some(()),
            _ => header.dump(),
        }

        offset = align_to_next_hob_offset(hob_list.len(), offset, header.length)?;
    }
}

/// Find Top of Lower Memory, which is the highest system memory address below 4G.
///
/// The low memory will be used for data storage (stack/heap/pagetable/eventlog/...)
pub fn get_system_memory_size_below_4gb(hob_list: &[u8]) -> Option<u64> {
    let mut low_mem_top = 0u64; // TOLUD (top of low usable dram)
    let mut offset = 0;

    loop {
        let hob = &hob_list[offset..];
        let header: Header = hob.pread(0).ok()?;

        match header.r#type {
            HOB_TYPE_RESOURCE_DESCRIPTOR => {
                let resource_hob: ResourceDescription = hob.pread(0).ok()?;
                if resource_hob.resource_type == RESOURCE_SYSTEM_MEMORY {
                    let end = resource_hob
                        .physical_start
                        .checked_add(resource_hob.resource_length)?;
                    if end < SIZE_4G && end > low_mem_top {
                        low_mem_top = end;
                    }
                }
            }
            HOB_TYPE_END_OF_HOB_LIST => break,
            _ => {}
        }

        offset = align_to_next_hob_offset(hob_list.len(), offset, header.length)?;
    }

    Some(low_mem_top)
}

/// Find Top of Memory, which is the highest system memory address below 4G.
pub fn get_total_memory_top(hob_list: &[u8]) -> Option<u64> {
    let mut mem_top = 0; // TOM (top of memory)
    let mut offset = 0;

    loop {
        let hob = &hob_list[offset..];
        let header: Header = hob.pread(0).ok()?;

        match header.r#type {
            HOB_TYPE_RESOURCE_DESCRIPTOR => {
                let resource_hob: ResourceDescription = hob.pread(0).ok()?;
                // TODO: why is RESOURCE_MEMORY_MAPPED_IO included for memory?
                if resource_hob.resource_type == RESOURCE_SYSTEM_MEMORY
                    || resource_hob.resource_type == RESOURCE_MEMORY_MAPPED_IO
                {
                    let end = resource_hob
                        .physical_start
                        .checked_add(resource_hob.resource_length)?;
                    if end > mem_top {
                        mem_top = end;
                    }
                }
            }
            HOB_TYPE_END_OF_HOB_LIST => break,
            _ => {}
        }
        offset = align_to_next_hob_offset(hob_list.len(), offset, header.length)?;
    }

    Some(mem_top)
}

pub fn get_fv(hob_list: &[u8]) -> Option<FirmwareVolume> {
    let mut offset = 0;

    loop {
        let hob = &hob_list[offset..];
        let header: Header = hob.pread(0).ok()?;
        match header.r#type {
            HOB_TYPE_FV => {
                let fv_hob: FirmwareVolume = hob.pread(0).ok()?;
                return Some(fv_hob);
            }
            HOB_TYPE_END_OF_HOB_LIST => break,
            _ => {}
        }
        offset = align_to_next_hob_offset(hob_list.len(), offset, header.length)?;
    }

    None
}

/// Find a GUID HOB entry matching `guid`.
pub fn get_next_extension_guid_hob<'a>(hob_list: &'a [u8], guid: &[u8]) -> Option<&'a [u8]> {
    let mut offset = 0;

    loop {
        let hob = &hob_list[offset..];
        let header: Header = hob.pread(0).ok()?;

        match header.r#type {
            HOB_TYPE_GUID_EXTENSION => {
                let guid_hob: GuidExtension = hob.pread(0).ok()?;
                if guid_hob.name == guid {
                    return Some(hob);
                }
            }
            HOB_TYPE_END_OF_HOB_LIST => break,
            _ => {}
        }
        offset = align_to_next_hob_offset(hob_list.len(), offset, header.length)?;
    }
    None
}

/// Get content of a GUID HOB entry.
pub fn get_guid_data(hob_list: &[u8]) -> Option<&[u8]> {
    let guid_hob: GuidExtension = hob_list.pread(0).ok()?;
    let offset = size_of::<GuidExtension>();
    let end = guid_hob.header.length as usize;

    if end >= offset && end <= hob_list.len() {
        Some(&hob_list[offset..end])
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pi::guid::Guid;
    use core::ptr::slice_from_raw_parts;

    #[test]
    fn test_align_to_next_hob() {
        assert!(align_to_next_hob_offset(usize::MAX, 0, 0).is_none());
        assert!(align_to_next_hob_offset(8, 8, 1).is_none());
        assert_eq!(align_to_next_hob_offset(usize::MAX, 8, 1), Some(16));
        assert_eq!(align_to_next_hob_offset(usize::MAX, 8, 9), Some(24));
        assert_eq!(
            align_to_next_hob_offset(usize::MAX, 0, u16::MAX - 8),
            Some(u16::MAX as usize - 7)
        );
        assert_eq!(
            align_to_next_hob_offset(usize::MAX, 0, u16::MAX - 7),
            Some(u16::MAX as usize - 7)
        );
        assert_eq!(
            align_to_next_hob_offset(usize::MAX, 8, u16::MAX - 7),
            Some(u16::MAX as usize + 1)
        );
        assert!(align_to_next_hob_offset(usize::MAX, 0, u16::MAX - 6).is_none());
        assert!(align_to_next_hob_offset(usize::MAX, 8, u16::MAX).is_none());
    }

    #[test]
    fn test_dump_hob() {
        assert!(dump_hob(&[]).is_none());
        assert!(dump_hob(&[0u8]).is_none());

        let memory_allocation_hob_header = Header {
            r#type: HOB_TYPE_MEMORY_ALLOCATION,
            length: size_of::<MemoryAllocation>() as u16,
            reserved: 0x0,
        };
        let memory_allocation_header = MemoryAllocationHeader {
            name: [0; 16],
            memory_base_address: 0x0,
            memory_length: 0x0,
            memory_type: 0x0,
            reserved: [0; 4],
        };
        let _ = memory_allocation_header.as_bytes();
        let hob = MemoryAllocation {
            header: memory_allocation_hob_header,
            alloc_descriptor: memory_allocation_header,
        };
        assert!(dump_hob(hob.as_bytes()).is_none());

        let fv_hob_header = Header {
            r#type: HOB_TYPE_FV,
            length: size_of::<FirmwareVolume>() as u16,
            reserved: 0x0,
        };
        let hob = FirmwareVolume {
            header: fv_hob_header,
            base_address: 0x0,
            length: 0x0,
        };
        assert!(dump_hob(hob.as_bytes()).is_none());

        let cpu_hob_header = Header {
            r#type: HOB_TYPE_CPU,
            length: size_of::<Cpu>() as u16,
            reserved: 0x0,
        };
        let hob = Cpu {
            header: cpu_hob_header,
            size_of_memory_space: 0x0,
            size_of_io_space: 0x0,
            reserved: [0; 6],
        };
        assert!(dump_hob(hob.as_bytes()).is_none());
    }

    #[test]
    fn test_check_hob_length() {
        let hob_header = Header {
            r#type: HOB_TYPE_HANDOFF,
            length: size_of::<HandoffInfoTable>() as u16,
            reserved: 0x0,
        };

        let mut hob = HandoffInfoTable {
            header: hob_header,
            version: 0,
            boot_mode: 0,
            efi_memory_top: 0,
            efi_memory_bottom: 0,
            efi_free_memory_top: 0,
            efi_free_memory_bottom: 0,
            efi_end_of_hob_list: 0,
        };
        hob.efi_end_of_hob_list =
            size_of::<HandoffInfoTable>() as u64 + hob.as_bytes().as_ptr() as u64;
        assert!(check_hob_length(hob.as_bytes(), size_of::<HandoffInfoTable>() as usize).is_some());
        // hob.efi_end_of_hob_list less than hob ptr
        hob.efi_end_of_hob_list = hob.as_bytes().as_ptr() as u64 - 1;
        assert!(check_hob_length(hob.as_bytes(), size_of::<HandoffInfoTable>() as usize).is_none());
        // hob_length + hob prt greater than u64::MAX
        hob.efi_end_of_hob_list =
            size_of::<HandoffInfoTable>() as u64 + hob.as_bytes().as_ptr() as u64;
        assert!(check_hob_length(hob.as_bytes(), u64::MAX as usize).is_none());

        // first header type is not HOB_TYPE_HANDOFF
        let hob_header = Header {
            r#type: HOB_TYPE_MEMORY_ALLOCATION,
            length: size_of::<HandoffInfoTable>() as u16,
            reserved: 0x0,
        };
        let mut hob = HandoffInfoTable {
            header: hob_header,
            version: 0,
            boot_mode: 0,
            efi_memory_top: 0,
            efi_memory_bottom: 0,
            efi_free_memory_top: 0,
            efi_free_memory_bottom: 0,
            efi_end_of_hob_list: 0,
        };
        hob.efi_end_of_hob_list =
            size_of::<HandoffInfoTable>() as u64 + hob.as_bytes().as_ptr() as u64;
        assert!(check_hob_length(hob.as_bytes(), size_of::<HandoffInfoTable>() as usize).is_none());

        // length less than HandoffInfoTable size
        let hob_header = Header {
            r#type: HOB_TYPE_HANDOFF,
            length: size_of::<HandoffInfoTable>() as u16 - 1,
            reserved: 0x0,
        };
        let mut hob = HandoffInfoTable {
            header: hob_header,
            version: 0,
            boot_mode: 0,
            efi_memory_top: 0,
            efi_memory_bottom: 0,
            efi_free_memory_top: 0,
            efi_free_memory_bottom: 0,
            efi_end_of_hob_list: 0,
        };
        hob.efi_end_of_hob_list =
            size_of::<HandoffInfoTable>() as u64 + hob.as_bytes().as_ptr() as u64;
        assert!(check_hob_length(hob.as_bytes(), size_of::<HandoffInfoTable>() as usize).is_none());
    }

    #[test]
    fn test_check_hob_integrity() {
        const EFI_END_OF_HOB_LIST_OFFSET: usize = 48;
        let hob = &include_bytes!("../fuzz/seeds/hob_parser/hob_buffer")[..];
        let mut test_hob = hob.to_vec();
        let ptr = test_hob.as_ptr() as u64;
        if test_hob.len() >= size_of::<HandoffInfoTable>() {
            test_hob[EFI_END_OF_HOB_LIST_OFFSET..size_of::<HandoffInfoTable>()]
                .copy_from_slice(&u64::to_le_bytes(ptr + hob.len() as u64)[..]);
        }

        assert!(check_hob_integrity(&test_hob).is_some());
        assert!(dump_hob(&test_hob).is_some());
    }

    #[test]
    fn test_get_total_memory_top() {
        let hob = &include_bytes!("../fuzz/seeds/hob_parser/hob_buffer")[..];

        assert!(get_total_memory_top(hob).is_some());
    }

    #[test]
    fn test_seek_to_next_hob() {
        let hob = &include_bytes!("../fuzz/seeds/hob_parser/hob_buffer")[..];

        assert!(seek_to_next_hob(hob).is_some());
    }

    #[test]
    fn test_get_system_memory_size_below_4gb() {
        assert!(get_system_memory_size_below_4gb(&[]).is_none());

        let mut buf = [0u8; 1024];
        let res = ResourceDescription {
            header: Header {
                r#type: HOB_TYPE_RESOURCE_DESCRIPTOR,
                length: size_of::<ResourceDescription>() as u16,
                reserved: 0,
            },
            owner: [0u8; 16],
            resource_type: RESOURCE_SYSTEM_MEMORY,
            resource_attribute: 0,
            physical_start: 0,
            resource_length: 0x200_0000,
        };
        let buf1 = unsafe {
            &*slice_from_raw_parts(
                &res as *const ResourceDescription as *const u8,
                size_of::<ResourceDescription>(),
            )
        };
        buf[..size_of::<ResourceDescription>()].copy_from_slice(buf1);
        let res = ResourceDescription {
            header: Header {
                r#type: HOB_TYPE_RESOURCE_DESCRIPTOR,
                length: size_of::<ResourceDescription>() as u16,
                reserved: 0,
            },
            owner: [0u8; 16],
            resource_type: RESOURCE_SYSTEM_MEMORY,
            resource_attribute: 0,
            physical_start: 0x1000_0000,
            resource_length: 0x200_0000,
        };
        let buf1 = unsafe {
            &*slice_from_raw_parts(
                &res as *const ResourceDescription as *const u8,
                size_of::<ResourceDescription>(),
            )
        };
        buf[size_of::<ResourceDescription>()..2 * size_of::<ResourceDescription>()]
            .copy_from_slice(buf1);
        let end = Header {
            r#type: HOB_TYPE_END_OF_HOB_LIST,
            length: 0,
            reserved: 0,
        };
        let buf2 = unsafe {
            &*slice_from_raw_parts(&end as *const Header as *const u8, size_of::<Header>())
        };
        buf[2 * size_of::<ResourceDescription>()
            ..2 * size_of::<ResourceDescription>() + size_of::<Header>()]
            .copy_from_slice(buf2);
        assert_eq!(get_system_memory_size_below_4gb(&buf), Some(0x1200_0000));

        let res = ResourceDescription {
            header: Header {
                r#type: HOB_TYPE_RESOURCE_DESCRIPTOR,
                length: 0,
                reserved: 0,
            },
            owner: [0u8; 16],
            resource_type: RESOURCE_SYSTEM_MEMORY,
            resource_attribute: 0,
            physical_start: 0,
            resource_length: 0x200_0000,
        };
        let buf1 = unsafe {
            &*slice_from_raw_parts(
                &res as *const ResourceDescription as *const u8,
                size_of::<ResourceDescription>(),
            )
        };
        buf[..size_of::<ResourceDescription>()].copy_from_slice(buf1);
        assert!(get_system_memory_size_below_4gb(&buf).is_none());
    }

    #[test]
    fn test_get_fv() {
        assert!(get_fv(&[]).is_none());

        let mut buf = [0u8; 1024];
        let res = FirmwareVolume {
            header: Header {
                r#type: HOB_TYPE_FV,
                length: size_of::<FirmwareVolume>() as u16,
                reserved: 0,
            },
            base_address: 0x1000000,
            length: 0,
        };
        let buf1 = unsafe {
            &*slice_from_raw_parts(
                &res as *const FirmwareVolume as *const u8,
                size_of::<FirmwareVolume>(),
            )
        };
        buf[..size_of::<FirmwareVolume>()].copy_from_slice(buf1);
        let end = Header {
            r#type: HOB_TYPE_END_OF_HOB_LIST,
            length: 0,
            reserved: 0,
        };
        let buf2 = unsafe {
            &*slice_from_raw_parts(&end as *const Header as *const u8, size_of::<Header>())
        };
        buf[size_of::<FirmwareVolume>()..size_of::<FirmwareVolume>() + size_of::<Header>()]
            .copy_from_slice(buf2);
        assert!(get_fv(&buf).is_some());

        let res = FirmwareVolume {
            header: Header {
                r#type: HOB_TYPE_FV2,
                length: u16::MAX,
                reserved: 0,
            },
            base_address: 0x1000000,
            length: 0,
        };
        let buf1 = unsafe {
            &*slice_from_raw_parts(
                &res as *const FirmwareVolume as *const u8,
                size_of::<FirmwareVolume>(),
            )
        };
        buf[..size_of::<FirmwareVolume>()].copy_from_slice(buf1);
        assert!(get_fv(&buf).is_none());
    }

    #[test]
    fn test_get_guid() {
        let guid = Guid::from_bytes(&[0u8; 16]);
        assert!(get_next_extension_guid_hob(&[], guid.as_bytes()).is_none());

        let mut buf = [0xaau8; 128];
        let res = GuidExtension {
            header: Header {
                r#type: HOB_TYPE_GUID_EXTENSION,
                length: size_of::<GuidExtension>() as u16 + 16,
                reserved: 0,
            },
            name: [0xa5u8; 16],
        };
        let buf1 = unsafe {
            &*slice_from_raw_parts(
                &res as *const GuidExtension as *const u8,
                size_of::<GuidExtension>(),
            )
        };
        assert_eq!(buf1, res.as_bytes());
        buf[..size_of::<GuidExtension>()].copy_from_slice(buf1);
        let end = Header {
            r#type: HOB_TYPE_END_OF_HOB_LIST,
            length: 0,
            reserved: 0,
        };
        let buf2 = unsafe {
            &*slice_from_raw_parts(&end as *const Header as *const u8, size_of::<Header>())
        };
        buf[size_of::<GuidExtension>() + 16..size_of::<GuidExtension>() + 16 + size_of::<Header>()]
            .copy_from_slice(buf2);
        let guid = get_next_extension_guid_hob(&buf, &[0xa5u8; 16]).unwrap();
        let data = get_guid_data(guid).unwrap();
        assert_eq!(data, &[0xaa; 16]);
    }
}

// To protect against speculative attacks, place the LFENCE instruction after the range
// check and branch, but before any code that consumes the checked value.
fn speculation_barrier() {
    unsafe { core::arch::asm!("lfence") }
}