Skip to content

Commit 3aeb2d8

Browse files
committed
multiboot2: expose get_tag publicly to allow custom tags
1 parent 6da9096 commit 3aeb2d8

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

multiboot2/Changelog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
work with custom tags. An example is given in the function documentation.
1111
(check docs.rs). There is also a small unit test that you can use to learn
1212
from.
13-
- There exists a seamless integration between `u32`, `TagType`, `TagType`, and
14-
`TagTypeId` via `From` and `PartialEq`-implementations.
13+
- There exists a seamless integration between `u32`, `TagType`, and `TagTypeId`
14+
via `From` and `PartialEq`-implementations.
1515

1616
## 0.14.2 (2023-03-17)
1717
- documentation fixes

multiboot2/src/lib.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub use memory_map::{
5454
};
5555
pub use module::{ModuleIter, ModuleTag};
5656
pub use rsdp::{RsdpV1Tag, RsdpV2Tag};
57-
pub use tag_type::{Tag, TagType, TagTypeId};
5857
use tag_type::TagIter;
58+
pub use tag_type::{Tag, TagType, TagTypeId};
5959
pub use vbe_info::{
6060
VBECapabilities, VBEControlInfo, VBEDirectColorAttributes, VBEField, VBEInfoTag,
6161
VBEMemoryModel, VBEModeAttributes, VBEModeInfo, VBEWindowAttributes,
@@ -314,7 +314,42 @@ impl BootInformation {
314314
unsafe { &*self.inner }
315315
}
316316

317-
fn get_tag(&self, typ: TagType) -> Option<&Tag> {
317+
/// Public getter to find any Multiboot tag by its type, including
318+
/// specified and custom ones.
319+
///
320+
/// # Specified or Custom Tags
321+
/// The Multiboot2 specification specifies a list of tags, see [`TagType`].
322+
/// However, it doesn't forbid to use custom tags. Because of this, there
323+
/// exists the [`TagType`] abstraction. It is recommended to use this
324+
/// getter only for custom tags. For specified tags, use getters, such as
325+
/// [`Self::efi_64_ih`].
326+
///
327+
/// ## Use Custom Tags
328+
/// The following example shows how you may use this interface to parse custom tags from
329+
/// the MBI.
330+
///
331+
/// ```ignore
332+
/// use multiboot2::TagTypeId;
333+
/// #[repr(C, align(8))]
334+
/// struct CustomTag {
335+
/// // new type from the lib: has repr(u32)
336+
/// tag: TagTypeId,
337+
/// size: u32,
338+
/// // begin of inline string
339+
/// name: u8,
340+
/// }
341+
///
342+
/// let tag = bi
343+
/// // this function is now public!
344+
/// .get_tag(0x1337.into())
345+
/// .unwrap()
346+
/// // type definition from end user; must be `Sized`!
347+
/// .cast_tag::<CustomTag>();
348+
/// let name = &tag.name as *const u8 as *const c_char;
349+
/// let str = unsafe { CStr::from_ptr(name).to_str().unwrap() };
350+
/// assert_eq!(str, "name");
351+
/// ```
352+
pub fn get_tag(&self, typ: TagType) -> Option<&Tag> {
318353
self.tags().find(|tag| tag.typ == typ)
319354
}
320355

multiboot2/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'a> Iterator for ModuleIter<'a> {
7676

7777
fn next(&mut self) -> Option<&'a ModuleTag> {
7878
self.iter
79-
.find(|x| x.typ == TagType::Module)
79+
.find(|tag| tag.typ == TagType::Module)
8080
.map(|tag| unsafe { &*(tag as *const Tag as *const ModuleTag) })
8181
}
8282
}

multiboot2/src/tag_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ mod partial_eq_impls {
291291
pub struct Tag {
292292
pub typ: TagTypeId, // u32
293293
pub size: u32,
294-
// tag specific fields
294+
// additional, tag specific fields
295295
}
296296

297297
impl Tag {

0 commit comments

Comments
 (0)