Booting to 'Hello Rust' on x86_64
micouy.github.io
Booting to 'Hello Rust' on x86_64
1–10 of 37 posts
Re: Booting to 'Hello Rust' on x86_64
#2Re: Booting to 'Hello Rust' on x86_64
#3great job OP!
Re: Booting to 'Hello Rust' on x86_64
#4It's my first post. I'd appreciate your feedback :)
1. Don't link the bootsector and the rest of your binary together. It's more trouble than it's worth, and if you ever progress to loading off a filesystem, then you won't be linking them together anyway.
2. You can use LBA addressing in real mode with int 13h (just specify a hard disk rather than a floppy when booting):
https://wiki.osdev.org/ATA_in_x86_RealMode_(BIOS)#LBA_in_Ext...
3. You need to switch to protected mode, set up paging, and then enter long mode.
4. I'd recommend compiling your rust code to an ELF binary, and then using objcopy to create a binary:
objcopy -O binary $input.elf $output.bin
Alternatively, ELF isn't that hard to parse, so you could load it yourself and then copy sections to the right locations with your bootsector.5. OSDev.org is your friend. Maybe get a C-based kernel running first, as the toolchain when dealing with freestanding code is a lot friendlier for beginners
- https://wiki.osdev.org/Protected_Mode
- https://wiki.osdev.org/Long_Mode#Long_Mode
You might be better off starting with 32-bit rust code instead, in order to avoid paging and long mode at first (you'll still need to switch to protected mode)
Re: Booting to 'Hello Rust' on x86_64
#5While modern hardware does continue to support these modes via various detection tricks, "real" handoff from the firmware happens in a 64 bit EFI environment these days, and the framebuffer tends to be in a graphics mode and expects to be accesesed initially via the exposed UEFI API (or to be reset from scratch by an OS graphics driver, of course).
This is fun and instructive, but not really reflective of the way modern hardware boots.
Re: Booting to 'Hello Rust' on x86_64
#6It was very satisfying seeing my PC boot to printing my name.
[0] https://www.amazon.co.uk/Indispensable-PC-Hardware-Book/dp/0...
Re: Booting to 'Hello Rust' on x86_64
#7For those checking comments first: this is mostly a tutorial on how to build and link a 16/32 bit MBR loader and mode switch implementation in assembly. It's very similar to what you'll find on osdev.org. The Rust content is limited to a single function at the end which writes some characters to an assumed-pre-initialized EGA text console. While modern hardware does continue to support these modes via various detecti…
Re: Booting to 'Hello Rust' on x86_64
#8For those checking comments first: this is mostly a tutorial on how to build and link a 16/32 bit MBR loader and mode switch implementation in assembly. It's very similar to what you'll find on osdev.org. The Rust content is limited to a single function at the end which writes some characters to an assumed-pre-initialized EGA text console. While modern hardware does continue to support these modes via various detecti…
I'm curious to learn more about "real" handoffs and the EFI environment. If you had any suggestions about where to start I'd be grateful.
https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8...
https://sourceforge.net/projects/gnu-efi/
Broadly: EFI is a simple C environment (albeit one based on windows-style PE binaries and not ELF). The firmware will load and run your application binary and jump to its entry point, passing a giant table of API hooks and callbacks for you to use to interact with its services. You then read and assemble your own OS image using these tools and jump into it.
Re: Booting to 'Hello Rust' on x86_64
#9Re: Booting to 'Hello Rust' on x86_64
#10(I remember that I had tried to write a bootloader myself in assembler, maybe 10 years ago or so, and in the end was just incapable of getting it to work.)