Live data from Hacker News

Booting to 'Hello Rust' on x86_64

micouy.github.io

31–37 of 37 posts

Re: Booting to 'Hello Rust' on x86_64

#31
post #8

Earlier quoted context omitted.

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/…

Personally, I haven't found the gnu efi toolchain very helpful. I just use clang with the appropriate flags, which are: -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 w…

Additionally, efi isn't useful for much beyond a bootloader, so you can use clang for the bootloader and gcc (or whatever else) for the OS itself. So you can depend on non-clang features.

Re: Booting to 'Hello Rust' on x86_64

#32
post #8
post #7

Earlier quoted context omitted.

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/…

Thanks. That specification is almost 2800 pages long - and it covers far more specific technology than I would have expected. At the end of the day, couldn't the firmware just reserve a fixed chunk of system memory for IO and divide it up between all the attached devices? Then adopt a convention that memory is initialized with the first, say, 1MB available from every device (loaded asynchronously), and execution starts at memory region N - the N being optionally chosen by the user in a pre-boot, BIOS-like screen.

Re: Booting to 'Hello Rust' on x86_64

#33
post #26
post #4

Earlier quoted context omitted.

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. Yo…

Thanks! 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 (w…

(Hacker News likes two eat single newlines. Try using two.)

Re: Booting to 'Hello Rust' on x86_64

#34
post #28
post #8

Earlier quoted context omitted.

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/…

I'm guessing this might also be of interest: 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…

Another potential point of interest that I remember is that in the Zig 0.5.0 release notes, where UEFI support is mentioned [1], they provide a few links relating to UEFI programming with Zig:

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

Re: Booting to 'Hello Rust' on x86_64

#35
post #26

Earlier quoted context omitted.

Thanks! 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 (w…

(Hacker News likes two eat single newlines. Try using two.)

Full reference here: https://news.ycombinator.com/formatdoc

Re: Booting to 'Hello Rust' on x86_64

#36
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…

For some reason (probably due to QEMU), these kinds of bootloader tutorials are still stuck in the BIOS era. UEFI remains completely under-taught, which is probably in part because it's hideously complex.

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

Re: Booting to 'Hello Rust' on x86_64

#37
post #8

Earlier quoted context omitted.

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/…

Thanks. That specification is almost 2800 pages long - and it covers far more specific technology than I would have expected. At the end of the day, couldn't the firmware just reserve a fixed chunk of system memory for IO and divide it up between all the attached devices? Then adopt a convention that memory is initialized with the first, say, 1MB available from every device (loaded asynchronously), and execution star…

Everyone starts with a simple idea. But by the time every hardware and software company has put in their input/requests/demands it'll end up at thousands of pages.
Post reply on HN