Live data from Hacker News

Booting to 'Hello Rust' on x86_64

micouy.github.io

21–30 of 37 posts

Re: Booting to 'Hello Rust' on x86_64

#21
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 for sharing on HN.

Re: Booting to 'Hello Rust' on x86_64

#22
post #14

Earlier quoted context omitted.

I think there's a typo in the final screenshot. It looks like your original code outputs "RM" (maybe "real mode" or "hello Rust" in a different language?), but the example source seems like it should output "HR." That or I'm very confused.

The HR is in the top left, it overwrites the pre-initialized text console.

Thanks, I see it now!

Re: Booting to 'Hello Rust' on x86_64

#23
post #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 assem…

Thank you! I also wish there was more content like this.

Re: Booting to 'Hello Rust' on x86_64

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

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

1. https://github.com/c-util/c-efi

Re: Booting to 'Hello Rust' on x86_64

#25
post #14
post #2

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

I think there's a typo in the final screenshot. It looks like your original code outputs "RM" (maybe "real mode" or "hello Rust" in a different language?), but the example source seems like it should output "HR." That or I'm very confused.

Sorry for that. I thought it would be visible enough in red. RM, LM and PM were printed by the bootloader.

Re: Booting to 'Hello Rust' on x86_64

#26
post #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. 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 (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.

Re: Booting to 'Hello Rust' on x86_64

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

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

Post reply on HN