Live data from Hacker News

Booting to 'Hello Rust' on x86_64

micouy.github.io

1–10 of 37 posts

Re: Booting to 'Hello Rust' on x86_64

#4
post #2

It's my first post. I'd appreciate your feedback :)

Hey, just a couple of points

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

#5
For 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 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

#6
I did something similar many years ago using nothing but debug.exe that came with windows 95, a spare floppy and an older version of The Indispensible PC Hardware Book [0].

It 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

#7
post #5

For 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.

Re: Booting to 'Hello Rust' on x86_64

#8
post #7
post #5

For 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.

There's sort of a paucity of tutorials in that space. You're pretty much stuck reading specs and studying source code. Here's the UEFI specification and a link to the "gnu-efi" project, which provide a reasonably clean (if... idiosyncratic) environment for building and running EFI binaries with a free toolchain:

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

#10
I really appreciate posts like this and wish there were more of them. Tutorials by experts are valuable, but I also find it comforting to read that other people are sometimes as clueless as I am, and I think posts like this can end up explaining some of the pitfalls that some more expert users aren't really aware of. We need more humility in software.

(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.)

Post reply on HN