Skip to content

Commit f41410c

Browse files
committed
multiboot2: expose get_tag publicly to allow custom tags
1 parent 95e463a commit f41410c

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

multiboot2/Changelog.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
- `BootInformation` now publicly exports the `get_tag` function allowing you to
55
work with custom tags. An example is given in the function documentation.
66
(check docs.rs). There is also a small unit test that you can use to learn
7-
from.
7+
from. Existing getters for specified tags still exist.
88
- **BREAKING:** `TagType` is now split into `TagTypeId` and `TagType`
9-
- `TagTypeId` is a binary-compatible form of a Multiboot2 tag id
9+
- `TagTypeId` is a binary-compatible form of a Multiboot2 tag ID
1010
- `TagType` is a higher-level abstraction for either specified or custom tags
1111
but not ABI compatible.
12-
- There exists a seamless integration between `u32`, `TagType`, `TagType`, and
13-
`TagTypeId` via `From` and `PartialEq`-implementations.
12+
- There exists a seamless integration between `u32`, `TagType`, and `TagTypeId`
13+
via `From` and `PartialEq`-implementations.
1414

1515
## 0.14.0 (2022-06-30)
1616
- **BREAKING CHANGES** \

multiboot2/src/lib.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ pub use memory_map::{
5555
};
5656
pub use module::{ModuleIter, ModuleTag};
5757
pub use rsdp::{RsdpV1Tag, RsdpV2Tag};
58-
pub use tag_type::{Tag, TagType, TagTypeId};
5958
use tag_type::TagIter;
59+
pub use tag_type::{Tag, TagType, TagTypeId};
6060
pub use vbe_info::{
6161
VBECapabilities, VBEControlInfo, VBEDirectColorAttributes, VBEField, VBEInfoTag,
6262
VBEMemoryModel, VBEModeAttributes, VBEModeInfo, VBEWindowAttributes,
@@ -308,7 +308,42 @@ impl BootInformation {
308308
unsafe { &*self.inner }
309309
}
310310

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

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)