Skip to content

Implement Linux bootable image support #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

epilys
Copy link
Contributor

@epilys epilys commented Jul 27, 2025

Follow the https://docs.kernel.org/arch/arm64/booting.html protocol to allow building a bootable linux-like image.

This'd be ideal for a compile once, run "anywhere" use-case.

I've been able to create linux bootable images with this diff.

file(1) recognizes the image as Linux kernel ARM64 boot executable Image, little-endian, 4K pages and QEMU can boot it with direct kernel boot. Also with Xen under QEMU by loading the image as the dom0 with the guest-loader device.

Finally, I added the UEFI MZ magic as the first instruction but I haven't had time to look into writing a proper stub.

TODOs:

  1. make the header configurable with a cargo feature instead of embedding it in entry.S
  2. configurable page sizes, endianness
  3. document it in README

Copy link

google-cla bot commented Jul 27, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@qwandor qwandor self-requested a review July 27, 2025 18:05
Copy link
Collaborator

@qwandor qwandor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I like the idea of supporting the Linux boot protocol (so far I've only used this for binaries loaded at a fixed address, like a bootloader), but I'm not sure that copying to a fixed address will always work. You'll also need to sign the CLA before I can accept anything.

@epilys epilys force-pushed the linux-bootable branch 2 times, most recently from cb2893c to 36296ef Compare August 8, 2025 16:38
@epilys
Copy link
Contributor Author

epilys commented Aug 8, 2025

Was busy with work but had time to look into this just now.

I got the relocate solution working, with the following caveats:

  • It requires -z notext and -pie linker args
  • Uses RELA relocations, not RELR
  • Image ORIGIN in linker script must be 0, an alternative is to calculate the origin with an adrp+add and then compute the difference with the load address.

Also fixed the CLA check, and guarded the relocate logic behind a relocate cargo feature.

EDIT: I made an example by copying the qemu_el1.rs example not for merging but to aid review: https://github.com/epilys/aarch64-rt/tree/linux-bootable-ex Check the ex subdirectory.

@epilys epilys requested a review from qwandor August 8, 2025 16:40
@epilys epilys changed the title RFC: Implement Linux bootable image support Implement Linux bootable image support Aug 8, 2025
@epilys epilys marked this pull request as ready for review August 8, 2025 16:41
@epilys epilys force-pushed the linux-bootable branch 2 times, most recently from 3cae637 to 60e7a0f Compare August 8, 2025 17:28
@epilys
Copy link
Contributor Author

epilys commented Aug 8, 2025

One thing missing is that the initial_pagetables macro cannot be configured to depend on discovered memory layouts e.g. /memory DT node or EFI memory map.

@epilys
Copy link
Contributor Author

epilys commented Aug 19, 2025

Rebased against main to include the naked functions refactoring, now everything is guarded behind a cargo feature and there's no unnecessary code added when it's disabled.

Follow the https://docs.kernel.org/arch/arm64/booting.html protocol to
allow building a bootable linux-like image.

To allow for any load address, perform relocations before jumping to the
rust entrypoint.

Guard relocation logic behind "relocation" Cargo feature.

Signed-off-by: Manos Pitsidianakis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants