Skip to content

Commit 8b3dfe4

Browse files
committed
multiboot2: Get a mutable reference to the memory map
1 parent 5962548 commit 8b3dfe4

File tree

4 files changed

+152
-38
lines changed

4 files changed

+152
-38
lines changed

multiboot2/src/builder/information.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
use crate::builder::BoxedDst;
1111
use alloc::vec::Vec;
1212
use core::mem::size_of;
13-
use core::ops::Deref;
13+
use core::ops::{Deref, DerefMut};
1414

1515
/// Holds the raw bytes of a boot information built with [`InformationBuilder`]
1616
/// on the heap. The bytes returned by [`BootInformationBytes::as_bytes`] are
@@ -33,6 +33,15 @@ impl BootInformationBytes {
3333
assert_eq!(slice.as_ptr().align_offset(8), 0);
3434
slice
3535
}
36+
37+
/// Returns the bytes mutably. They are guaranteed to be correctly aligned.
38+
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
39+
let slice = &mut self.bytes[self.offset..self.offset + self.structure_len];
40+
// At this point, the alignment is guaranteed. If not, something is
41+
// broken fundamentally.
42+
assert_eq!(slice.as_ptr().align_offset(8), 0);
43+
slice
44+
}
3645
}
3746

3847
impl Deref for BootInformationBytes {
@@ -43,6 +52,12 @@ impl Deref for BootInformationBytes {
4352
}
4453
}
4554

55+
impl DerefMut for BootInformationBytes {
56+
fn deref_mut(&mut self) -> &mut Self::Target {
57+
self.as_bytes_mut()
58+
}
59+
}
60+
4661
/// Builder to construct a valid Multiboot2 information dynamically at runtime.
4762
/// The tags will appear in the order of their corresponding enumeration,
4863
/// except for the END tag.
@@ -411,10 +426,10 @@ mod tests {
411426
#[cfg_attr(miri, ignore)]
412427
fn test_builder() {
413428
// Step 1/2: Build MBI
414-
let mb2i_data = create_builder().build();
429+
let mut mb2i_data = create_builder().build();
415430

416431
// Step 2/2: Test the built MBI
417-
let mb2i = unsafe { BootInformation::load(mb2i_data.as_ptr().cast()) }
432+
let mb2i = unsafe { BootInformation::load(mb2i_data.as_mut_ptr().cast()) }
418433
.expect("generated information should be readable");
419434

420435
assert_eq!(mb2i.basic_memory_info_tag().unwrap().memory_lower(), 640);

0 commit comments

Comments
 (0)