#header-parser #bootloader #multiboot2 #kernel #boot #header-file #request-headers

无std multiboot2-header

包含对Multiboot2头文件类型定义和解析函数的库。该库为no_std,可用于引导加载程序。

9个版本 (5个重大更新)

新版本 0.5.0 2024年8月20日
0.4.0 2024年5月1日
0.3.2 2023年12月16日
0.3.1 2023年6月28日
0.0.0 2021年10月1日

#666解析器实现

MIT/Apache

295KB
6K SLoC

multiboot2-header

Build crates.io docs

Rust库,包含对Multiboot2头文件类型定义和解析函数的支持,以及用于在运行时构建它们的构建器。该库为no_std,可用于引导加载程序。

此库的优点

  • 在运行时构建Multiboot2头文件(在构建时使用宏构建尚未实现,欢迎贡献!)
  • 编写解析Multiboot2头文件的Multiboot2引导加载程序
  • 更好地理解Multiboot2头文件
  • 在运行时分析Multiboot2头文件

功能和no_std兼容性

此库始终为no_std,不包含alloc。然而,默认的builder功能需要alloc-crate和一个可用的#[global_allocator]。您只需要builder来在运行时构建新的头文件。对于解析,该功能不相关,您可以禁用它。

# without `builder`-feature (and without `alloc`-crate)
multiboot2-header = { version = "<latest>", default-features = false }
# else (requires `alloc`-crate)
multiboot2-header = "<latest>"

示例1:Builder + 解析

use multiboot2_header::builder::{InformationRequestHeaderTagBuilder, Multiboot2HeaderBuilder};
use multiboot2_header::{HeaderTagFlag, HeaderTagISA, MbiTagType, RelocatableHeaderTag, RelocatableHeaderTagPreference, Multiboot2Header};

/// Small example that creates a Multiboot2 header and parses it afterwards.
fn main() {
    // We create a Multiboot2 header during runtime here. A practical example is that your
    // program gets the header from a file and parses it afterwards.
    let mb2_hdr_bytes = Multiboot2HeaderBuilder::new(HeaderTagISA::I386)
        .relocatable_tag(RelocatableHeaderTag::new(
            HeaderTagFlag::Required,
            0x1337,
            0xdeadbeef,
            4096,
            RelocatableHeaderTagPreference::None,
        ))
        .information_request_tag(
            InformationRequestHeaderTagBuilder::new(HeaderTagFlag::Required)
                .add_irs(&[MbiTagType::Cmdline, MbiTagType::BootLoaderName]),
        )
        .build();

    // Cast bytes in vector to Multiboot2 information structure
    let mb2_hdr = unsafe { Multiboot2Header::from_addr(mb2_hdr_bytes.as_ptr().cast()) };
    println!("{:#?}", mb2_hdr);
}

示例2:Rust文件中的静态数据Multiboot2头文件

您可以使用构建器,构建一个Multiboot2头文件,将其写入文件并按如下方式包含它

#[used]
#[no_mangle]
#[link_section = ".text.multiboot2_header"]
static MULTIBOOT2_HDR: [u8; 64] = *include_bytes!("mb2_hdr_dump.bin");

您可能需要一个特殊的链接脚本,以将此符号放置在ELF的前32768字节处。请参阅Multiboot2规范。

MSRV

MSRV是1.70.0稳定版。

许可 & 贡献

请参阅主README文件。

依赖项

~2MB
~43K SLoC