multiboot2 icon indicating copy to clipboard operation
multiboot2 copied to clipboard

multiboot2-header: construct header at build time [call for contribution]

Open phip1611 opened this issue 2 years ago • 2 comments

It would be awesome to construct multiboot2-headers at runtime with macros.

For reference: That's how you write a header in assembly language: https://github.com/rust-osdev/multiboot2/blob/main/integration-test/bins/multiboot2_payload/src/multiboot2_header.S

phip1611 avatar Dec 16 '23 14:12 phip1611

I wrote a macro for my own project here feel free to use it. There are examples in the tests module and documentation in lib.rs. It does have a couple of quirks and issues, some that can be fixed easily and one which is a huge pain to deal with.

  • This resolves types names by trimming the last ident from the path to the constructor so constructing a tag using TagName{fields} will not work, you must use TagName::new(args)
  • Tags should have #[repr(align(8))], currently some tags need to be padded manually, which is a bit of a pain because of how the macro resolves type names.
  • Multiboot2BasicHeader doesn't have a public constructor, probably because of the size argument so the macro must define it's own.
  • Pointers cannot be cast to integers at compile time. rustc managed to foil all of my attempts to get a truncated pointer. There are ways to work around this, firstly store pointers using *const () on 32bit targets. 64bit targets are kinda SOL for this method, the way I'm working around this is just specifying addresses in in a linker script and giving the same address to tag constructors. I did notice that you can get a truncated pointer using inline asm but I'm pretty sure that exploiting this requires writing the entire header in an asm block.

I did also consider adding a "native" argument for the architecture to get the macro to resolve the arch itself, but with the huge selection of i386 or MIPS I think the user can handle this themselves.

an-owl avatar May 01 '24 01:05 an-owl

Thanks @an-owl - that's awesome!

phip1611 avatar May 01 '24 10:05 phip1611