Skip to content

Commit 26d62bc

Browse files
committed
multiboot2: Rework boxed_dst_tag
Passing a size was not a good idea as it can be inferred from the slice.
1 parent e9ed160 commit 26d62bc

File tree

7 files changed

+17
-38
lines changed

7 files changed

+17
-38
lines changed

multiboot2/src/boot_loader_name.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ impl BootLoaderNameTag {
3636
// allocate a C string
3737
let cstr = CString::new(name)
3838
.expect("failed to create CString");
39-
let bytes = cstr.to_bytes_with_nul();
40-
let size = (bytes.len() + METADATA_SIZE).try_into().unwrap();
4139
let tag = boxed_dst_tag(
42-
TagType::BootLoaderName, size, Some(cstr.as_bytes_with_nul())
40+
TagType::BootLoaderName, cstr.as_bytes_with_nul(),
4341
);
4442
unsafe { Box::from_raw(Box::into_raw(tag) as *mut Self) }
4543
}

multiboot2/src/builder/mod.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,26 @@ use alloc::boxed::Box;
1414

1515
use crate::{TagType, Tag};
1616

17-
/// Create a boxed tag with the given size. This includes type and size.
18-
pub(super) fn boxed_dst_tag(
19-
typ: TagType, size: u32, content: Option<&[u8]>
20-
) -> Box<Tag> {
17+
/// Create a boxed tag with the given content.
18+
pub(super) fn boxed_dst_tag(typ: TagType, content: &[u8]) -> Box<Tag> {
2119
// based on https://stackoverflow.com/a/64121094/2192464
2220
let (layout, size_offset) = Layout::new::<TagType>()
2321
.extend(Layout::new::<u32>()).unwrap();
2422
let (layout, inner_offset) = layout.extend(
25-
Layout::array::<usize>(
26-
size as usize - size_of::<TagType>() - size_of::<u32>()
27-
).unwrap()
23+
Layout::array::<usize>(content.len()).unwrap()
2824
).unwrap();
2925
let ptr = unsafe { alloc(layout) };
3026
assert!(!ptr.is_null());
3127
unsafe {
3228
// initialize the content as good as we can
3329
ptr.cast::<TagType>().write(typ);
34-
ptr.add(size_offset).cast::<u32>().write(size);
30+
ptr.add(size_offset).cast::<u32>().write((
31+
content.len() + size_of::<TagType>() + size_of::<u32>()
32+
).try_into().unwrap());
3533
// initialize body
36-
if let Some(c) = content {
37-
let content_ptr = ptr.add(inner_offset);
38-
for (idx, val) in c.iter().enumerate() {
39-
content_ptr.add(idx).write(*val);
40-
}
34+
let content_ptr = ptr.add(inner_offset);
35+
for (idx, val) in content.iter().enumerate() {
36+
content_ptr.add(idx).write(*val);
4137
}
4238
Box::from_raw(
4339
core::ptr::from_raw_parts_mut(

multiboot2/src/command_line.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ impl CommandLineTag {
3737
// allocate a C string
3838
let cstr = CString::new(command_line)
3939
.expect("failed to create CString");
40-
let bytes = cstr.to_bytes_with_nul();
41-
let size = (bytes.len() + METADATA_SIZE).try_into().unwrap();
42-
let tag = boxed_dst_tag(TagType::Cmdline, size, Some(cstr.as_bytes_with_nul()));
40+
let tag = boxed_dst_tag(TagType::Cmdline, cstr.as_bytes_with_nul());
4341
unsafe { Box::from_raw(Box::into_raw(tag) as *mut Self) }
4442
}
4543

multiboot2/src/elf_sections.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use crate::tag_type::{Tag, TagType};
1+
use crate::tag_type::TagType;
22
#[cfg(feature = "builder")]
33
use crate::builder::boxed_dst_tag;
44
#[cfg(feature = "builder")]
55
use crate::builder::traits::StructAsBytes;
66

77
use core::convert::TryInto;
8-
use core::{fmt::{Debug, Formatter}, mem};
8+
use core::fmt::{Debug, Formatter};
99
#[cfg(feature = "builder")]
1010
use alloc::boxed::Box;
1111

12-
const METADATA_SIZE: usize = mem::size_of::<TagType>() + mem::size_of::<u32>();
13-
1412
/// This tag contains section header table from an ELF kernel.
1513
///
1614
/// The sections iterator is provided via the `sections` method.
@@ -28,15 +26,12 @@ pub struct ElfSectionsTag {
2826
impl ElfSectionsTag {
2927
#[cfg(feature = "builder")]
3028
pub fn new(number_of_sections: u32, entry_size: u32, shndx: u32, sections: &[u8]) -> Box<Self> {
31-
let size = (sections.len() + METADATA_SIZE).try_into().unwrap();
3229
let mut bytes = [
3330
number_of_sections.to_le_bytes(), entry_size.to_le_bytes(),
3431
shndx.to_le_bytes(),
3532
].concat();
3633
bytes.extend_from_slice(sections);
37-
let tag = boxed_dst_tag(
38-
TagType::ElfSections, size, Some(bytes.as_slice())
39-
);
34+
let tag = boxed_dst_tag(TagType::ElfSections, bytes.as_slice());
4035
unsafe { Box::from_raw(Box::into_raw(tag) as *mut Self) }
4136
}
4237

multiboot2/src/framebuffer.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ impl FramebufferTag {
6262
bytes.extend(bpp.to_le_bytes());
6363
bytes.extend(buffer_type.to_bytes());
6464

65-
let size = (bytes.len() + METADATA_SIZE).try_into().unwrap();
66-
let tag = boxed_dst_tag(
67-
TagType::Framebuffer, size, Some(&bytes)
68-
);
65+
let tag = boxed_dst_tag(TagType::Framebuffer, &bytes);
6966
unsafe { Box::from_raw(Box::into_raw(tag) as *mut Self) }
7067
}
7168

multiboot2/src/memory_map.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ impl MemoryMapTag {
4242
for area in areas {
4343
bytes.extend(area.struct_as_bytes());
4444
}
45-
let tag = boxed_dst_tag(
46-
TagType::Mmap, bytes.len().try_into().unwrap(),
47-
Some(bytes.as_slice()),
48-
);
45+
let tag = boxed_dst_tag(TagType::Mmap, bytes.as_slice());
4946
unsafe { Box::from_raw(Box::into_raw(tag) as *mut Self) }
5047
}
5148

multiboot2/src/module.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ impl ModuleTag {
3737

3838
let cstr = CString::new(cmdline)
3939
.expect("failed to create CString");
40-
let bytes = cstr.to_bytes_with_nul();
41-
let size = (bytes.len() + METADATA_SIZE).try_into().unwrap();
4240
let start_bytes = start.to_le_bytes();
4341
let end_bytes = end.to_le_bytes();
4442
let mut content_bytes = [start_bytes, end_bytes].concat();
4543
content_bytes.extend_from_slice(cstr.as_bytes_with_nul());
46-
let tag = boxed_dst_tag(TagType::Module, size, Some(content_bytes.as_slice()));
44+
let tag = boxed_dst_tag(TagType::Module, content_bytes.as_slice());
4745
unsafe { Box::from_raw(Box::into_raw(tag) as *mut Self) }
4846
}
4947

0 commit comments

Comments
 (0)