boot_services/
allocation.rs1use core::ops::{BitOr, BitOrAssign};
2
3use r_efi::efi;
4
5use crate::{boxed::BootServicesBox, BootServices};
6
7#[derive(Debug)]
8pub enum AllocType {
9 AnyPage,
10 MaxAddress(usize),
11 Address(usize),
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[repr(transparent)]
16pub struct MemoryType(u32);
17
18impl MemoryType {
19 pub const RESERVED_MEMORY_TYPE: MemoryType = MemoryType(efi::RESERVED_MEMORY_TYPE);
20 pub const LOADER_CODE: MemoryType = MemoryType(efi::LOADER_CODE);
21 pub const LOADER_DATA: MemoryType = MemoryType(efi::LOADER_DATA);
22 pub const BOOT_SERVICES_CODE: MemoryType = MemoryType(efi::BOOT_SERVICES_CODE);
23 pub const BOOT_SERVICES_DATA: MemoryType = MemoryType(efi::BOOT_SERVICES_DATA);
24 pub const RUNTIME_SERVICES_CODE: MemoryType = MemoryType(efi::RUNTIME_SERVICES_CODE);
25 pub const RUNTIME_SERVICES_DATA: MemoryType = MemoryType(efi::RUNTIME_SERVICES_DATA);
26 pub const CONVENTIONAL_MEMORY: MemoryType = MemoryType(efi::CONVENTIONAL_MEMORY);
27 pub const UNUSABLE_MEMORY: MemoryType = MemoryType(efi::UNUSABLE_MEMORY);
28 pub const ACPI_RECLAIM_MEMORY: MemoryType = MemoryType(efi::ACPI_RECLAIM_MEMORY);
29 pub const ACPI_MEMORY_NVS: MemoryType = MemoryType(efi::ACPI_MEMORY_NVS);
30 pub const MEMORY_MAPPED_IO: MemoryType = MemoryType(efi::MEMORY_MAPPED_IO);
31 pub const MEMORY_MAPPED_IO_PORT_SPACE: MemoryType = MemoryType(efi::MEMORY_MAPPED_IO_PORT_SPACE);
32 pub const PAL_CODE: MemoryType = MemoryType(efi::PAL_CODE);
33 pub const PERSISTENT_MEMORY: MemoryType = MemoryType(efi::PERSISTENT_MEMORY);
34 pub const UNACCEPTED_MEMORY_TYPE: MemoryType = MemoryType(efi::UNACCEPTED_MEMORY_TYPE);
35}
36
37impl Into<u32> for MemoryType {
38 fn into(self) -> u32 {
39 self.0
40 }
41}
42
43#[derive(Debug)]
44pub struct MemoryMap<'a, B: BootServices + ?Sized> {
45 pub descriptors: BootServicesBox<'a, [efi::MemoryDescriptor], B>,
46 pub map_key: usize,
47 pub descriptor_version: u32,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct MemoryAttribute(u64);
52
53impl MemoryAttribute {
54 pub const UC: MemoryAttribute = MemoryAttribute(efi::MEMORY_UC);
55 pub const WC: MemoryAttribute = MemoryAttribute(efi::MEMORY_WC);
56 pub const WT: MemoryAttribute = MemoryAttribute(efi::MEMORY_WT);
57 pub const WB: MemoryAttribute = MemoryAttribute(efi::MEMORY_WB);
58 pub const UCE: MemoryAttribute = MemoryAttribute(efi::MEMORY_UCE);
59 pub const WP: MemoryAttribute = MemoryAttribute(efi::MEMORY_WP);
60 pub const RP: MemoryAttribute = MemoryAttribute(efi::MEMORY_RP);
61 pub const XP: MemoryAttribute = MemoryAttribute(efi::MEMORY_XP);
62 pub const NV: MemoryAttribute = MemoryAttribute(efi::MEMORY_NV);
63 pub const MORE_RELIABLE: MemoryAttribute = MemoryAttribute(efi::MEMORY_MORE_RELIABLE);
64 pub const RO: MemoryAttribute = MemoryAttribute(efi::MEMORY_RO);
65 pub const SP: MemoryAttribute = MemoryAttribute(efi::MEMORY_SP);
66 pub const CPU_CRYPTO: MemoryAttribute = MemoryAttribute(efi::MEMORY_CPU_CRYPTO);
67 pub const RUNTIME: MemoryAttribute = MemoryAttribute(efi::MEMORY_RUNTIME);
68 pub const ISA_VALID: MemoryAttribute = MemoryAttribute(efi::MEMORY_ISA_VALID);
69 pub const ISA_MASK: MemoryAttribute = MemoryAttribute(efi::MEMORY_ISA_MASK);
70}
71
72impl BitOr for MemoryAttribute {
73 type Output = MemoryAttribute;
74
75 fn bitor(self, rhs: Self) -> Self::Output {
76 MemoryAttribute(self.0 | rhs.0)
77 }
78}
79
80impl BitOrAssign for MemoryAttribute {
81 fn bitor_assign(&mut self, rhs: Self) {
82 self.0 |= rhs.0
83 }
84}
85
86impl Into<efi::AllocateType> for AllocType {
87 fn into(self) -> efi::AllocateType {
88 match self {
89 AllocType::AnyPage => efi::ALLOCATE_ANY_PAGES,
90 AllocType::MaxAddress(_) => efi::ALLOCATE_MAX_ADDRESS,
91 AllocType::Address(_) => efi::ALLOCATE_ADDRESS,
92 }
93 }
94}
95
96impl Into<u64> for MemoryAttribute {
97 fn into(self) -> u64 {
98 self.0
99 }
100}