Skip to content

Commit 776a980

Browse files
committed
multiboot2: add missing docs
1 parent 34abe94 commit 776a980

14 files changed

+82
-23
lines changed

multiboot2/Changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
## Unreleased
44

55
- **Breaking** All functions that returns something useful are now `#[must_use]`
6+
- **Breaking** More public fields in tags were replaced by public getters, such
7+
as `SmbiosTag::major()`
68
- updated dependencies
79
- MSRV is 1.75
8-
- doc fixes
10+
- documentation enhancements
911
- Introduced new `TagHeader` type as replacement for the `Tag` type that will
1012
be changed in the next step.
1113

multiboot2/src/boot_information.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
#[cfg(feature = "builder")]
44
use crate::builder::AsBytes;
55
use crate::framebuffer::UnknownFramebufferType;
6-
use crate::tag::TagIter;
6+
use crate::tag::{TagHeader, TagIter};
77
use crate::{
88
module, BasicMemoryInfoTag, BootLoaderNameTag, CommandLineTag, EFIBootServicesNotExitedTag,
99
EFIImageHandle32Tag, EFIImageHandle64Tag, EFIMemoryMapTag, EFISdt32Tag, EFISdt64Tag,
1010
ElfSectionIter, ElfSectionsTag, EndTag, FramebufferTag, ImageLoadPhysAddrTag, MemoryMapTag,
1111
ModuleIter, RsdpV1Tag, RsdpV2Tag, SmbiosTag, TagTrait, VBEInfoTag,
1212
};
1313
use core::fmt;
14+
use core::mem;
15+
use core::ptr;
1416
use derive_more::Display;
1517

1618
/// Error type that describes errors while loading/parsing a multiboot2 information structure
@@ -39,19 +41,25 @@ impl core::error::Error for MbiLoadError {}
3941
#[repr(C)]
4042
pub struct BootInformationHeader {
4143
// size is multiple of 8
42-
pub total_size: u32,
44+
total_size: u32,
4345
_reserved: u32,
4446
// Followed by the boot information tags.
4547
}
4648

47-
#[cfg(feature = "builder")]
4849
impl BootInformationHeader {
50+
#[cfg(feature = "builder")]
4951
pub(crate) const fn new(total_size: u32) -> Self {
5052
Self {
5153
total_size,
5254
_reserved: 0,
5355
}
5456
}
57+
58+
/// Returns the total size of the structure.
59+
#[must_use]
60+
pub const fn total_size(&self) -> u32 {
61+
self.total_size
62+
}
5563
}
5664

5765
#[cfg(feature = "builder")]
@@ -70,18 +78,18 @@ impl BootInformationInner {
7078
/// Checks if the MBI has a valid end tag by checking the end of the mbi's
7179
/// bytes.
7280
fn has_valid_end_tag(&self) -> bool {
73-
let end_tag_prototype = EndTag::default();
74-
75-
let self_ptr = unsafe { self.tags.as_ptr().sub(size_of::<BootInformationHeader>()) };
81+
let self_ptr = ptr::addr_of!(*self);
7682

7783
let end_tag_ptr = unsafe {
7884
self_ptr
85+
.cast::<u8>()
7986
.add(self.header.total_size as usize)
80-
.sub(size_of::<EndTag>())
87+
.sub(mem::size_of::<EndTag>())
88+
.cast::<TagHeader>()
8189
};
82-
let end_tag = unsafe { &*(end_tag_ptr as *const EndTag) };
90+
let end_tag = unsafe { &*end_tag_ptr };
8391

84-
end_tag.typ == end_tag_prototype.typ && end_tag.size == end_tag_prototype.size
92+
end_tag.typ == EndTag::ID && end_tag.size as usize == mem::size_of::<EndTag>()
8593
}
8694
}
8795

@@ -127,7 +135,7 @@ impl<'a> BootInformation<'a> {
127135
return Err(MbiLoadError::IllegalTotalSize(mbi.total_size));
128136
}
129137

130-
let slice_size = mbi.total_size as usize - size_of::<BootInformationHeader>();
138+
let slice_size = mbi.total_size as usize - mem::size_of::<BootInformationHeader>();
131139
// mbi: reference to full mbi
132140
let mbi = ptr_meta::from_raw_parts::<BootInformationInner>(ptr.cast(), slice_size);
133141
let mbi = &*mbi;

multiboot2/src/boot_loader_name.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct BootLoaderNameTag {
1919
}
2020

2121
impl BootLoaderNameTag {
22+
/// Constructs a new tag.
2223
#[cfg(feature = "builder")]
2324
#[must_use]
2425
pub fn new(name: &str) -> BoxedDst<Self> {
@@ -30,6 +31,18 @@ impl BootLoaderNameTag {
3031
BoxedDst::new(&bytes)
3132
}
3233

34+
/// Returns the underlying [`TagType`].
35+
#[must_use]
36+
pub fn typ(&self) -> TagType {
37+
self.header.typ.into()
38+
}
39+
40+
/// Returns the underlying tag size.
41+
#[must_use]
42+
pub const fn size(&self) -> usize {
43+
self.header.size as usize
44+
}
45+
3346
/// Reads the name of the bootloader that is booting the kernel as Rust
3447
/// string slice without the null-byte.
3548
///

multiboot2/src/builder/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ pub use boxed_dst::BoxedDst;
88
pub use information::InformationBuilder;
99

1010
/// Helper trait for all structs that need to be serialized that do not
11-
/// implement `TagTrait`.
11+
/// implement [`TagTrait`].
12+
///
13+
/// [`TagTrait`]: crate::TagTrait
1214
pub trait AsBytes: Sized {
15+
/// Returns the raw bytes of the type.
1316
fn as_bytes(&self) -> &[u8] {
1417
let ptr = core::ptr::addr_of!(*self);
1518
let size = core::mem::size_of::<Self>();

multiboot2/src/efi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub struct EFIImageHandle32Tag {
8282
}
8383

8484
impl EFIImageHandle32Tag {
85+
/// Constructs a new tag.
8586
#[cfg(feature = "builder")]
8687
#[must_use]
8788
pub fn new(pointer: u32) -> Self {
@@ -114,6 +115,7 @@ pub struct EFIImageHandle64Tag {
114115
}
115116

116117
impl EFIImageHandle64Tag {
118+
/// Constructs a new tag.
117119
#[cfg(feature = "builder")]
118120
#[must_use]
119121
pub fn new(pointer: u64) -> Self {
@@ -144,6 +146,7 @@ pub struct EFIBootServicesNotExitedTag {
144146
}
145147

146148
impl EFIBootServicesNotExitedTag {
149+
/// Constructs a new tag.
147150
#[cfg(feature = "builder")]
148151
#[must_use]
149152
pub fn new() -> Self {

multiboot2/src/end.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::{Tag, TagTrait, TagType, TagTypeId};
66
#[repr(C)]
77
#[derive(Debug)]
88
pub struct EndTag {
9-
pub typ: TagTypeId,
10-
pub size: u32,
9+
typ: TagTypeId,
10+
size: u32,
1111
}
1212

1313
impl Default for EndTag {

multiboot2/src/framebuffer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub struct FramebufferTag {
8484
}
8585

8686
impl FramebufferTag {
87+
/// Constructs a new tag.
8788
#[cfg(feature = "builder")]
8889
#[must_use]
8990
pub fn new(

multiboot2/src/image_load_addr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct ImageLoadPhysAddrTag {
1616
}
1717

1818
impl ImageLoadPhysAddrTag {
19+
/// Constructs a new tag.
1920
#[cfg(feature = "builder")]
2021
#[must_use]
2122
pub fn new(load_base_addr: u32) -> Self {

multiboot2/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// now allow a few rules which are denied by the above statement
1313
// --> They are either ridiculous, not necessary, or we can't fix them.
1414
#![allow(clippy::multiple_crate_versions)]
15-
// #![deny(missing_docs)]
15+
#![deny(missing_docs)]
1616
#![deny(missing_debug_implementations)]
1717
#![deny(rustdoc::all)]
1818
// --- END STYLE CHECKS ---

multiboot2/src/memory_map.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct MemoryMapTag {
3535
}
3636

3737
impl MemoryMapTag {
38+
/// Constructs a new tag.
3839
#[cfg(feature = "builder")]
3940
#[must_use]
4041
pub fn new(areas: &[MemoryArea]) -> BoxedDst<Self> {
@@ -260,21 +261,24 @@ pub struct BasicMemoryInfoTag {
260261
}
261262

262263
impl BasicMemoryInfoTag {
264+
/// Constructs a new tag.
263265
#[must_use]
264266
pub fn new(memory_lower: u32, memory_upper: u32) -> Self {
265267
Self {
266-
header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
268+
header: TagHeader::new(Self::ID, mem::size_of::<Self>().try_into().unwrap()),
267269
memory_lower,
268270
memory_upper,
269271
}
270272
}
271273

272274
#[must_use]
275+
/// Returns the lower memory bound.
273276
pub const fn memory_lower(&self) -> u32 {
274277
self.memory_lower
275278
}
276279

277280
#[must_use]
281+
/// Returns the upper memory bound.
278282
pub const fn memory_upper(&self) -> u32 {
279283
self.memory_upper
280284
}
@@ -317,10 +321,10 @@ pub struct EFIMemoryMapTag {
317321
}
318322

319323
impl EFIMemoryMapTag {
320-
#[cfg(feature = "builder")]
321324
/// Create a new EFI memory map tag with the given memory descriptors.
322325
/// Version and size can't be set because you're passing a slice of
323326
/// EFIMemoryDescs, not the ones you might have gotten from the firmware.
327+
#[cfg(feature = "builder")]
324328
#[must_use]
325329
pub fn new_from_descs(descs: &[EFIMemoryDesc]) -> BoxedDst<Self> {
326330
// TODO replace this EfiMemorydesc::uefi_desc_size() in the next uefi_raw
@@ -347,8 +351,8 @@ impl EFIMemoryMapTag {
347351
)
348352
}
349353

350-
#[cfg(feature = "builder")]
351354
/// Create a new EFI memory map tag from the given EFI memory map.
355+
#[cfg(feature = "builder")]
352356
#[must_use]
353357
pub fn new_from_map(desc_size: u32, desc_version: u32, efi_mmap: &[u8]) -> BoxedDst<Self> {
354358
assert!(desc_size > 0);

multiboot2/src/module.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct ModuleTag {
2323
}
2424

2525
impl ModuleTag {
26+
/// Constructs a new tag.
2627
#[cfg(feature = "builder")]
2728
#[must_use]
2829
pub fn new(start: u32, end: u32, cmdline: &str) -> BoxedDst<Self> {

multiboot2/src/rsdp.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct RsdpV1Tag {
3535
}
3636

3737
impl RsdpV1Tag {
38+
/// Constructs a new tag.
3839
#[cfg(feature = "builder")]
3940
#[must_use]
4041
pub fn new(
@@ -114,6 +115,7 @@ pub struct RsdpV2Tag {
114115
}
115116

116117
impl RsdpV2Tag {
118+
/// Constructs a new tag.
117119
#[cfg(feature = "builder")]
118120
#[allow(clippy::too_many_arguments)]
119121
#[must_use]

multiboot2/src/smbios.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,39 @@ const METADATA_SIZE: usize = core::mem::size_of::<TagTypeId>()
1515
#[repr(C)]
1616
pub struct SmbiosTag {
1717
header: TagHeader,
18-
pub major: u8,
19-
pub minor: u8,
18+
major: u8,
19+
minor: u8,
2020
_reserved: [u8; 6],
21-
pub tables: [u8],
21+
tables: [u8],
2222
}
2323

2424
impl SmbiosTag {
25+
/// Constructs a new tag.
2526
#[cfg(feature = "builder")]
2627
#[must_use]
2728
pub fn new(major: u8, minor: u8, tables: &[u8]) -> BoxedDst<Self> {
2829
let mut bytes = [major, minor, 0, 0, 0, 0, 0, 0].to_vec();
2930
bytes.extend(tables);
3031
BoxedDst::new(&bytes)
3132
}
33+
34+
/// Returns the major number.
35+
#[must_use]
36+
pub const fn major(&self) -> u8 {
37+
self.major
38+
}
39+
40+
/// Returns the major number.
41+
#[must_use]
42+
pub const fn minor(&self) -> u8 {
43+
self.minor
44+
}
45+
46+
/// Returns the raw tables.
47+
#[must_use]
48+
pub const fn tables(&self) -> &[u8] {
49+
&self.tables
50+
}
3251
}
3352

3453
impl TagTrait for SmbiosTag {

multiboot2/src/tag.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ impl core::error::Error for StringError {
4141
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
4242
#[repr(C)]
4343
pub struct TagHeader {
44+
/// The ABI-compatible [`TagType`].
4445
pub typ: TagTypeId, /* u32 */
46+
/// The total size of the tag including the header.
4547
pub size: u32,
46-
// Followed by additional, tag specific fields.
48+
// Followed by optional additional tag specific fields.
4749
}
4850

4951
impl TagHeader {
5052
/// Creates a new header.
51-
#[cfg(feature = "builder")]
5253
pub fn new(typ: impl Into<TagTypeId>, size: u32) -> Self {
5354
Self {
5455
typ: typ.into(),
@@ -67,6 +68,7 @@ impl TagHeader {
6768
/// different.
6869
#[derive(Clone, Copy)]
6970
#[repr(C)]
71+
#[allow(missing_docs)] // type will be removed soon anyway in its form
7072
pub struct Tag {
7173
pub typ: TagTypeId, // u32
7274
pub size: u32,

0 commit comments

Comments
 (0)