Skip to content

Commit da22960

Browse files
authored
Merge pull request #896 from nicholasbishop/bishop-raw-memprot
Add raw memory protection protocol and use in `uefi`
2 parents c7e8361 + e1159d7 commit da22960

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::table::boot::MemoryAttribute;
2+
use crate::{guid, Guid, PhysicalAddress, Status};
3+
4+
#[repr(C)]
5+
pub struct MemoryAttributeProtocol {
6+
pub get_memory_attributes: unsafe extern "efiapi" fn(
7+
this: *const Self,
8+
base_address: PhysicalAddress,
9+
length: u64,
10+
attributes: *mut MemoryAttribute,
11+
) -> Status,
12+
13+
pub set_memory_attributes: unsafe extern "efiapi" fn(
14+
this: *const Self,
15+
base_address: PhysicalAddress,
16+
length: u64,
17+
attributes: MemoryAttribute,
18+
) -> Status,
19+
20+
pub clear_memory_attributes: unsafe extern "efiapi" fn(
21+
this: *const Self,
22+
base_address: PhysicalAddress,
23+
length: u64,
24+
attributes: MemoryAttribute,
25+
) -> Status,
26+
}
27+
28+
impl MemoryAttributeProtocol {
29+
pub const GUID: Guid = guid!("f4560cf6-40ec-4b4a-a192-bf1d57d0b189");
30+
}

uefi-raw/src/protocol/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ pub mod console;
99
pub mod device_path;
1010
pub mod disk;
1111
pub mod loaded_image;
12+
pub mod memory_protection;
1213
pub mod rng;

uefi/src/proto/security/memory_protection.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
use crate::data_types::PhysicalAddress;
22
use crate::proto::unsafe_protocol;
33
use crate::table::boot::MemoryAttribute;
4-
use crate::{Result, Status, StatusExt};
4+
use crate::{Result, StatusExt};
55
use core::ops::Range;
6+
use uefi_raw::protocol::memory_protection::MemoryAttributeProtocol;
67

78
/// Protocol for getting and setting memory protection attributes.
89
///
910
/// Corresponds to the C type `EFI_MEMORY_ATTRIBUTE_PROTOCOL`.
10-
#[repr(C)]
11-
#[unsafe_protocol("f4560cf6-40ec-4b4a-a192-bf1d57d0b189")]
12-
pub struct MemoryProtection {
13-
get_memory_attributes: unsafe extern "efiapi" fn(
14-
this: *const Self,
15-
base_address: PhysicalAddress,
16-
length: u64,
17-
attributes: *mut MemoryAttribute,
18-
) -> Status,
19-
20-
set_memory_attributes: unsafe extern "efiapi" fn(
21-
this: *const Self,
22-
base_address: PhysicalAddress,
23-
length: u64,
24-
attributes: MemoryAttribute,
25-
) -> Status,
26-
27-
clear_memory_attributes: unsafe extern "efiapi" fn(
28-
this: *const Self,
29-
base_address: PhysicalAddress,
30-
length: u64,
31-
attributes: MemoryAttribute,
32-
) -> Status,
33-
}
11+
#[repr(transparent)]
12+
#[unsafe_protocol(MemoryAttributeProtocol::GUID)]
13+
pub struct MemoryProtection(MemoryAttributeProtocol);
3414

3515
impl MemoryProtection {
3616
/// Get the attributes of a memory region.
@@ -47,6 +27,7 @@ impl MemoryProtection {
4727
/// [`READ_PROTECT`]: MemoryAttribute::READ_PROTECT
4828
/// [`EXECUTE_PROTECT`]: MemoryAttribute::EXECUTE_PROTECT
4929
/// [`READ_ONLY`]: MemoryAttribute::READ_ONLY
30+
/// [`Status::NO_MAPPING`]: crate::Status::NO_MAPPING
5031
/// [UEFI page size]: uefi::table::boot::PAGE_SIZE
5132
pub fn get_memory_attributes(
5233
&self,
@@ -55,7 +36,7 @@ impl MemoryProtection {
5536
let mut attributes = MemoryAttribute::empty();
5637
let (base_address, length) = range_to_base_and_len(byte_region);
5738
unsafe {
58-
(self.get_memory_attributes)(self, base_address, length, &mut attributes)
39+
(self.0.get_memory_attributes)(&self.0, base_address, length, &mut attributes)
5940
.to_result_with_val(|| attributes)
6041
}
6142
}
@@ -78,7 +59,9 @@ impl MemoryProtection {
7859
attributes: MemoryAttribute,
7960
) -> Result {
8061
let (base_address, length) = range_to_base_and_len(byte_region);
81-
unsafe { (self.set_memory_attributes)(self, base_address, length, attributes).to_result() }
62+
unsafe {
63+
(self.0.set_memory_attributes)(&self.0, base_address, length, attributes).to_result()
64+
}
8265
}
8366

8467
/// Clear the attributes of a memory region.
@@ -100,7 +83,7 @@ impl MemoryProtection {
10083
) -> Result {
10184
let (base_address, length) = range_to_base_and_len(byte_region);
10285
unsafe {
103-
(self.clear_memory_attributes)(self, base_address, length, attributes).to_result()
86+
(self.0.clear_memory_attributes)(&self.0, base_address, length, attributes).to_result()
10487
}
10588
}
10689
}

0 commit comments

Comments
 (0)