Booting to 'Hello Rust' on x86_64
micouy.github.io
micouy.github.io
While 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.
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.
-target x86_64-unknown-windows -ffreestanding -fno-builtin -fshort-wchar -mno-red-zone
And link, again with clang, using these flags: -target x86_64-unknown-windows -nostdlib -Wl,-entry:efi_main -Wl,-subsystem:efi_application -fuse-ld=lld-link
I believe you can do the same with gcc and gold|gnu ld without much more effort. The toolchain doesn't really justify its complexity at all.The headers, on the other hand, are invaluable. I use c-efi[1]'s headers, but would probably use gnu efi another time; c-efi has some bugs and missing structures. Worth noting: despite the name, gnu efi isn't gpl licensed. In fact, I don't think either (c-efi or gnu efi) can really be considered copyrightable; they're essentially direct translations of the spec.
https://www.kernel.org/doc/html/latest/admin-guide/efi-stub....
Seems to be a little bit out of date, though:
> The code that modifies the bzImage header, along with the EFI-specific entry point that the firmware loader jumps to are collectively known as the “EFI boot stub”, and live in arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c
I found:
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux...
But no eboot.c - there's this however:
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux...
uefi-examples - tutorials https://github.com/nrdmn/uefi-examples
uefi-paint - UEFI-bootable touch paint app https://github.com/nrdmn/uefi-paint
uefi-freetype - changing the system BIOS to use Comic Sans https://github.com/nrdmn/uefi-freetype https://twitter.com/andy_kelley/status/1176561072398098432
[1]: https://ziglang.org/download/0.5.0/release-notes.html#UEFI-S...
Note that modern computers are supposed to be dropping BIOS boot support this year[1].
[1] https://arstechnica.com/gadgets/2017/11/intel-to-kill-off-th...
(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.)
(This is linked near the end of the article, but I think it's worth pointing out.)
Full disclosure: I think he uses a library I wrote to initialize the keyboard controller.
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)
I just think that when starting out in kernel development, you're likely going to be dealing with linker scripts for likely the first time. It's been a while since I tried rust for this, but C generates pretty clean object files, so the linker scripts from the OSDev wiki can often be copied verbatim.
I don’t think that the linker script stuff ends up different but it’s been a minute since I started from scratch.
1. I have a Makefile that links it for me so I don't have to do anything special. I intend the Rust program to work like a second stage bootloader. I'm going to experiment with it and try to get a file system working to load an actual kernel from a file. 2. I'll look into that. 3. That's what the bootloader does. Sorry if I wasn't clear. 4. If I understand correctly if I compile my Rust project to a binary (which is a default output and I don't really understand why I would use objcopy for that) I won't be able to link with with the bootloader. I link them to allow the bootloader to jump to the _start function at the correct address. 5. My Rust program already works in 64-bit so I don't see a reason to change it. In this project I want to explore low level programming and understand things like paging.
I appreciate your feedback.
That or I'm very confused.
great job OP!
It was very satisfying seeing my PC boot to printing my name.
[0] https://www.amazon.co.uk/Indispensable-PC-Hardware-Book/dp/0...