Skip to content

Commit ae7ba25

Browse files
authored
Merge pull request #518 from nicholasbishop/bishop-add-physicaladdr
Add PhysicalAddress and VirtualAddress type aliases
2 parents 1b69165 + 3171ffa commit ae7ba25

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
## uefi - [Unreleased]
44

5+
### Added
6+
7+
- Added `PhysicalAddress` and `VirtualAddress` type aliases.
8+
9+
### Changed
10+
11+
- Fixed the definition of `AllocateType` so that `MaxAddress` and
12+
`Address` always take a 64-bit value, regardless of target platform.
13+
514
## uefi-macros - [Unreleased]
615

716
## uefi-services - [Unreleased]

src/data_types/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ pub trait Align {
107107
}
108108
}
109109

110+
/// Physical memory address. This is always a 64-bit value, regardless
111+
/// of target platform.
112+
pub type PhysicalAddress = u64;
113+
114+
/// Virtual memory address. This is always a 64-bit value, regardless
115+
/// of target platform.
116+
pub type VirtualAddress = u64;
117+
110118
mod guid;
111119
pub use self::guid::Guid;
112120
pub use self::guid::{unsafe_guid, Identify};

src/proto/security/memory_protection.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::data_types::PhysicalAddress;
12
use crate::proto::Protocol;
23
use crate::table::boot::MemoryAttribute;
34
use crate::{unsafe_guid, Result, Status};
@@ -14,21 +15,21 @@ use core::ops::Range;
1415
pub struct MemoryProtection {
1516
get_memory_attributes: unsafe extern "efiapi" fn(
1617
this: *const Self,
17-
base_address: u64,
18+
base_address: PhysicalAddress,
1819
length: u64,
1920
attributes: *mut MemoryAttribute,
2021
) -> Status,
2122

2223
set_memory_attributes: unsafe extern "efiapi" fn(
2324
this: *const Self,
24-
base_address: u64,
25+
base_address: PhysicalAddress,
2526
length: u64,
2627
attributes: MemoryAttribute,
2728
) -> Status,
2829

2930
clear_memory_attributes: unsafe extern "efiapi" fn(
3031
this: *const Self,
31-
base_address: u64,
32+
base_address: PhysicalAddress,
3233
length: u64,
3334
attributes: MemoryAttribute,
3435
) -> Status,
@@ -46,7 +47,10 @@ impl MemoryProtection {
4647
/// [`READ_PROTECT`]: MemoryAttribute::READ_PROTECT
4748
/// [`EXECUTE_PROTECT`]: MemoryAttribute::EXECUTE_PROTECT
4849
/// [`READ_ONLY`]: MemoryAttribute::READ_ONLY
49-
pub fn get_memory_attributes(&self, byte_region: Range<u64>) -> Result<MemoryAttribute> {
50+
pub fn get_memory_attributes(
51+
&self,
52+
byte_region: Range<PhysicalAddress>,
53+
) -> Result<MemoryAttribute> {
5054
let mut attributes = MemoryAttribute::empty();
5155
let (base_address, length) = range_to_base_and_len(byte_region);
5256
unsafe {
@@ -65,7 +69,7 @@ impl MemoryProtection {
6569
/// [`READ_ONLY`]: MemoryAttribute::READ_ONLY
6670
pub fn set_memory_attributes(
6771
&self,
68-
byte_region: Range<u64>,
72+
byte_region: Range<PhysicalAddress>,
6973
attributes: MemoryAttribute,
7074
) -> Result {
7175
let (base_address, length) = range_to_base_and_len(byte_region);
@@ -82,7 +86,7 @@ impl MemoryProtection {
8286
/// [`READ_ONLY`]: MemoryAttribute::READ_ONLY
8387
pub fn clear_memory_attributes(
8488
&self,
85-
byte_region: Range<u64>,
89+
byte_region: Range<PhysicalAddress>,
8690
attributes: MemoryAttribute,
8791
) -> Result {
8892
let (base_address, length) = range_to_base_and_len(byte_region);
@@ -91,7 +95,7 @@ impl MemoryProtection {
9195
}
9296

9397
/// Convert a byte `Range` to `(base_address, length)`.
94-
fn range_to_base_and_len(r: Range<u64>) -> (u64, u64) {
98+
fn range_to_base_and_len(r: Range<PhysicalAddress>) -> (PhysicalAddress, PhysicalAddress) {
9599
(r.start, r.end.checked_sub(r.start).unwrap())
96100
}
97101

src/table/boot.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! UEFI services available during boot.
22
33
use super::{Header, Revision};
4-
use crate::data_types::Align;
4+
use crate::data_types::{Align, PhysicalAddress, VirtualAddress};
55
use crate::proto::device_path::{DevicePath, FfiDevicePath};
66
#[cfg(feature = "exts")]
77
use crate::proto::{loaded_image::LoadedImage, media::fs::SimpleFileSystem};
@@ -95,9 +95,9 @@ pub struct BootServices {
9595
alloc_ty: u32,
9696
mem_ty: MemoryType,
9797
count: usize,
98-
addr: &mut u64,
98+
addr: &mut PhysicalAddress,
9999
) -> Status,
100-
free_pages: extern "efiapi" fn(addr: u64, pages: usize) -> Status,
100+
free_pages: extern "efiapi" fn(addr: PhysicalAddress, pages: usize) -> Status,
101101
get_memory_map: unsafe extern "efiapi" fn(
102102
size: &mut usize,
103103
map: *mut MemoryDescriptor,
@@ -324,17 +324,17 @@ impl BootServices {
324324
ty: AllocateType,
325325
mem_ty: MemoryType,
326326
count: usize,
327-
) -> Result<u64> {
327+
) -> Result<PhysicalAddress> {
328328
let (ty, mut addr) = match ty {
329329
AllocateType::AnyPages => (0, 0),
330-
AllocateType::MaxAddress(addr) => (1, addr as u64),
331-
AllocateType::Address(addr) => (2, addr as u64),
330+
AllocateType::MaxAddress(addr) => (1, addr),
331+
AllocateType::Address(addr) => (2, addr),
332332
};
333333
(self.allocate_pages)(ty, mem_ty, count, &mut addr).into_with_val(|| addr)
334334
}
335335

336336
/// Frees memory pages allocated by UEFI.
337-
pub fn free_pages(&self, addr: u64, count: usize) -> Result {
337+
pub fn free_pages(&self, addr: PhysicalAddress, count: usize) -> Result {
338338
(self.free_pages)(addr, count).into()
339339
}
340340

@@ -1522,9 +1522,9 @@ pub enum AllocateType {
15221522
/// Allocate any possible pages.
15231523
AnyPages,
15241524
/// Allocate pages at any address below the given address.
1525-
MaxAddress(usize),
1525+
MaxAddress(PhysicalAddress),
15261526
/// Allocate pages at the specified address.
1527-
Address(usize),
1527+
Address(PhysicalAddress),
15281528
}
15291529

15301530
newtype_enum! {
@@ -1594,9 +1594,9 @@ pub struct MemoryDescriptor {
15941594
/// Skip 4 bytes as UEFI declares items in structs should be naturally aligned
15951595
padding: u32,
15961596
/// Starting physical address.
1597-
pub phys_start: u64,
1597+
pub phys_start: PhysicalAddress,
15981598
/// Starting virtual address.
1599-
pub virt_start: u64,
1599+
pub virt_start: VirtualAddress,
16001600
/// Number of 4 KiB pages contained in this range.
16011601
pub page_count: u64,
16021602
/// The capability attributes of this memory range.

0 commit comments

Comments
 (0)