Skip to content

Commit 8dcc7f5

Browse files
authored
Merge pull request #91 from rust-osdev/module-tag-cmdline-fix
Fix ModuleTag cmdline
2 parents 293bf85 + 2f20bea commit 8dcc7f5

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/module.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,28 @@ use core::fmt::{Debug, Formatter};
33

44
/// This tag indicates to the kernel what boot module was loaded along with
55
/// the kernel image, and where it can be found.
6-
#[derive(Clone, Copy, Debug)]
6+
#[derive(Clone, Copy)]
77
#[repr(C, packed)] // only repr(C) would add unwanted padding near name_byte.
88
pub struct ModuleTag {
99
typ: u32,
1010
size: u32,
1111
mod_start: u32,
1212
mod_end: u32,
13-
name_byte: u8,
13+
/// Begin of the command line string.
14+
cmdline_str: u8,
1415
}
1516

1617
impl ModuleTag {
17-
// The multiboot specification defines the module str
18-
// as valid utf-8, therefore this function produces
19-
// defined behavior
20-
/// Get the name of the module.
21-
pub fn name(&self) -> &str {
18+
// The multiboot specification defines the module str as valid utf-8 (zero terminated string),
19+
// therefore this function produces defined behavior
20+
/// Get the cmdline of the module. If the GRUB configuration contains
21+
/// `module2 /foobar/some_boot_module --test cmdline-option`, then this method
22+
/// will return `--test cmdline-option`.
23+
pub fn cmdline(&self) -> &str {
2224
use core::{mem, slice, str};
2325
let strlen = self.size as usize - mem::size_of::<ModuleTag>();
2426
unsafe {
25-
str::from_utf8_unchecked(slice::from_raw_parts(&self.name_byte as *const u8, strlen))
27+
str::from_utf8_unchecked(slice::from_raw_parts(&self.cmdline_str as *const u8, strlen))
2628
}
2729
}
2830

@@ -35,6 +37,24 @@ impl ModuleTag {
3537
pub fn end_address(&self) -> u32 {
3638
self.mod_end
3739
}
40+
41+
/// The size of the module/the BLOB in memory.
42+
pub fn module_size(&self) -> u32 {
43+
self.mod_end - self.mod_start
44+
}
45+
}
46+
47+
impl Debug for ModuleTag {
48+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
49+
f.debug_struct("ModuleTag")
50+
.field("type", &self.typ)
51+
.field("size (tag)", &self.size)
52+
.field("size (module)", &(self.module_size()))
53+
.field("mod_start", &(self.mod_start as *const usize))
54+
.field("mod_end", &(self.mod_end as *const usize))
55+
.field("cmdline", &self.cmdline())
56+
.finish()
57+
}
3858
}
3959

4060
pub fn module_iter(iter: TagIter) -> ModuleIter {

0 commit comments

Comments
 (0)