Skip to content

Commit 0fb8e30

Browse files
committed
mem: Simplify MemoryRegion methods
We really only need from_bytes and as_bytes. There's no need for a generic from_slice method. Signed-off-by: Joe Richey <joerichey@google.com>
1 parent afb4d0c commit 0fb8e30

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/bzimage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn load_kernel(f: &mut dyn Read) -> Result<u64, Error> {
143143
f.read(&mut buf[0..512])?;
144144
f.read(&mut buf[512..])?;
145145

146-
let setup = crate::mem::MemoryRegion::from_slice(&buf[..]);
146+
let setup = crate::mem::MemoryRegion::from_bytes(&mut buf[..]);
147147

148148
if setup.read_u16(0x1fe) != 0xAA55 {
149149
return Err(Error::MagicMissing);

src/mem.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ pub struct MemoryRegion {
2222
}
2323

2424
impl MemoryRegion {
25-
pub fn new(base: u64, length: u64) -> MemoryRegion {
25+
pub const fn new(base: u64, length: u64) -> MemoryRegion {
2626
MemoryRegion { base, length }
2727
}
2828

2929
/// Take a slice and turn it into a region of memory
30-
pub fn from_slice<T>(data: &[T]) -> MemoryRegion {
30+
pub fn from_bytes(data: &mut [u8]) -> MemoryRegion {
3131
MemoryRegion {
3232
base: data.as_ptr() as u64,
33-
length: (data.len() * core::mem::size_of::<T>()) as u64,
33+
length: data.len() as u64,
3434
}
3535
}
3636

37+
// Expose the entire region as a byte slice
38+
pub fn as_bytes(&mut self) -> &mut [u8] {
39+
self.as_mut_slice(0, self.length)
40+
}
41+
3742
/// Expose a section of the memory region as a slice
3843
pub fn as_mut_slice<T>(&mut self, offset: u64, length: u64) -> &mut [T] {
3944
assert!((offset + (length * core::mem::size_of::<T>() as u64)) <= self.length);

src/pe.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a> Loader<'a> {
6060
Err(_) => return Err(Error::FileError),
6161
}
6262

63-
let dos_region = MemoryRegion::from_slice(&data);
63+
let dos_region = MemoryRegion::from_bytes(&mut data);
6464

6565
// 'MZ' magic
6666
if dos_region.read_u16(0) != 0x5a4d {
@@ -74,7 +74,7 @@ impl<'a> Loader<'a> {
7474
return Err(Error::InvalidExecutable);
7575
}
7676

77-
let pe_region = MemoryRegion::from_slice(&data[pe_header_offset as usize..]);
77+
let pe_region = MemoryRegion::from_bytes(&mut data[pe_header_offset as usize..]);
7878

7979
// The Microsoft specification uses offsets relative to the COFF area
8080
// which is 4 after the signature (so all offsets are +4 relative to the spec)
@@ -91,7 +91,8 @@ impl<'a> Loader<'a> {
9191
self.num_sections = pe_region.read_u16(6);
9292

9393
let optional_header_size = pe_region.read_u16(20);
94-
let optional_region = MemoryRegion::from_slice(&data[(24 + pe_header_offset) as usize..]);
94+
let optional_region =
95+
MemoryRegion::from_bytes(&mut data[(24 + pe_header_offset) as usize..]);
9596

9697
// Only support x86-64 EFI
9798
if optional_region.read_u16(0) != 0x20b {
@@ -177,7 +178,7 @@ impl<'a> Loader<'a> {
177178
let l: &mut [u8] = loaded_region
178179
.as_mut_slice(u64::from(section.virt_address), u64::from(section_size));
179180

180-
let reloc_region = MemoryRegion::from_slice(l);
181+
let reloc_region = MemoryRegion::from_bytes(l);
181182

182183
let mut section_bytes_remaining = section_size;
183184
let mut offset = 0;

0 commit comments

Comments
 (0)