|
3 | 3 |
|
4 | 4 | use super::*;
|
5 | 5 | use crate::table::system_table_boot;
|
| 6 | +use core::fmt::{Debug, Display, Formatter}; |
6 | 7 | use core::ops::{Index, IndexMut};
|
7 | 8 | use core::ptr::NonNull;
|
8 | 9 | use core::{mem, ptr};
|
9 | 10 | use uefi_raw::PhysicalAddress;
|
10 | 11 |
|
| 12 | +/// Errors that may happen when constructing a [`MemoryMapRef`] or |
| 13 | +/// [`MemoryMapRefMut`]. |
| 14 | +#[derive(Copy, Clone, Debug)] |
| 15 | +pub enum MemoryMapError { |
| 16 | + /// The buffer is not 8-byte aligned. |
| 17 | + Misaligned, |
| 18 | + /// The memory map size is invalid. |
| 19 | + InvalidSize, |
| 20 | +} |
| 21 | + |
| 22 | +impl Display for MemoryMapError { |
| 23 | + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { |
| 24 | + Debug::fmt(self, f) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[cfg(feature = "unstable")] |
| 29 | +impl core::error::Error for MemoryMapError {} |
| 30 | + |
11 | 31 | /// Implementation of [`MemoryMap`] for the given buffer.
|
12 | 32 | #[derive(Debug)]
|
13 |
| -#[allow(dead_code)] // TODO: github.com/rust-osdev/uefi-rs/issues/1247 |
14 | 33 | pub struct MemoryMapRef<'a> {
|
15 | 34 | buf: &'a [u8],
|
16 | 35 | meta: MemoryMapMeta,
|
17 | 36 | len: usize,
|
18 | 37 | }
|
19 | 38 |
|
| 39 | +impl<'a> MemoryMapRef<'a> { |
| 40 | + /// Constructs a new [`MemoryMapRef`]. |
| 41 | + /// |
| 42 | + /// The underlying memory might contain an invalid/malformed memory map |
| 43 | + /// which can't be checked during construction of this type. The entry |
| 44 | + /// iterator might yield unexpected results. |
| 45 | + pub fn new(buffer: &'a [u8], meta: MemoryMapMeta) -> Result<Self, MemoryMapError> { |
| 46 | + if buffer.as_ptr().align_offset(8) != 0 { |
| 47 | + return Err(MemoryMapError::Misaligned); |
| 48 | + } |
| 49 | + if buffer.len() < meta.map_size { |
| 50 | + return Err(MemoryMapError::InvalidSize); |
| 51 | + } |
| 52 | + Ok(Self { |
| 53 | + buf: buffer, |
| 54 | + meta, |
| 55 | + len: meta.entry_count(), |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
20 | 60 | impl<'a> MemoryMap for MemoryMapRef<'a> {
|
21 | 61 | fn meta(&self) -> MemoryMapMeta {
|
22 | 62 | self.meta
|
@@ -58,6 +98,27 @@ pub struct MemoryMapRefMut<'a> {
|
58 | 98 | len: usize,
|
59 | 99 | }
|
60 | 100 |
|
| 101 | +impl<'a> MemoryMapRefMut<'a> { |
| 102 | + /// Constructs a new [`MemoryMapRefMut`]. |
| 103 | + /// |
| 104 | + /// The underlying memory might contain an invalid/malformed memory map |
| 105 | + /// which can't be checked during construction of this type. The entry |
| 106 | + /// iterator might yield unexpected results. |
| 107 | + pub fn new(buffer: &'a mut [u8], meta: MemoryMapMeta) -> Result<Self, MemoryMapError> { |
| 108 | + if buffer.as_ptr().align_offset(8) != 0 { |
| 109 | + return Err(MemoryMapError::Misaligned); |
| 110 | + } |
| 111 | + if buffer.len() < meta.map_size { |
| 112 | + return Err(MemoryMapError::InvalidSize); |
| 113 | + } |
| 114 | + Ok(Self { |
| 115 | + buf: buffer, |
| 116 | + meta, |
| 117 | + len: meta.entry_count(), |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
61 | 122 | impl<'a> MemoryMap for MemoryMapRefMut<'a> {
|
62 | 123 | fn meta(&self) -> MemoryMapMeta {
|
63 | 124 | self.meta
|
|
0 commit comments