Skip to content

Implement GPT support #12

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 5 commits into
base: main
Choose a base branch
from
Open

Implement GPT support #12

wants to merge 5 commits into from

Conversation

bnuuydev
Copy link

@bnuuydev bnuuydev commented Jun 3, 2025

A few things need doing before this is good to merge:

  • Write some tests
  • Decide which partition types to define (and add their definitions)
  • Some written documentation could be good, but I've given a syntactic definition of how to use it to start with :)
  • Really the GPT code should call into the MBR code to make the protective MBR. I can have a look in a few days at making this change, but happy for someone else to if they get to it before I do. I forgot I already did this.
  • Partition attributes can't be set (custom attributes pose a problem here) (but this can be forgone honestly; it's pretty niche and the current code is sufficient for bootable disk images, which is the main purpose of this I guess)
  • Only supports GPT with a protective MBR for now, no hybrid MBR. Hybrid MBRs are problematic for a number of reasons, so it might be best just to forget about them for now.

The code might not compile since as I was working on master. I think I've made sure the code is right now, but be sure that they are correct before merging :)

Copy link
Collaborator

@Khitiara Khitiara left a comment

Choose a reason for hiding this comment

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

mostly LGTM (also scooped me, nice), got a few minor nits is all

@Khitiara Khitiara self-assigned this Jun 3, 2025
@Khitiara
Copy link
Collaborator

Khitiara commented Jun 3, 2025

also if you want and run out of time for this I can work on any finishing stuff

@bnuuydev
Copy link
Author

bnuuydev commented Jun 3, 2025

Would appreciate that! Sorry for stepping on your toes a bit with this btw, I'd written it a few weeks ago but had been too busy to make the PR >_<

@Khitiara
Copy link
Collaborator

Khitiara commented Jun 3, 2025

no worries, you got a heck of a lot further than i did

@Khitiara
Copy link
Collaborator

Khitiara commented Jun 3, 2025

addressed the nits and pushed an update to the PR

@Khitiara Khitiara requested a review from ikskuh June 3, 2025 21:23
@bnuuydev
Copy link
Author

bnuuydev commented Jun 3, 2025

Also make sure not to merge this before we have some tests. I'll write some up in a couple of days if you don't get to it first :)

@Khitiara
Copy link
Collaborator

Khitiara commented Jun 4, 2025

I'll also be switching my os over to this pr branch at some point in a few days to try it out in a real scenario

@Khitiara Khitiara mentioned this pull request Jun 5, 2025
@Khitiara
Copy link
Collaborator

found a bug in that the BuildInterface doesnt provide quotes around the partition name

@Khitiara
Copy link
Collaborator

i have my os project now successfully booting off of an efi/gpt disk setup using this PR

@Khitiara
Copy link
Collaborator

thoughts on having a flag to mark the protective MBR as bootable? this is required for bios/gpt boots with some bioses

@Khitiara
Copy link
Collaborator

my proposal would be to add a legacy-bootable optional keyword that just makes it do that if present

@bnuuydev
Copy link
Author

thoughts on having a flag to mark the protective MBR as bootable? this is required for bios/gpt boots with some bioses

I think that should be part of a future PR if it is decided hybrid MBR/GPT would be worth doing.

std.mem.writeInt(u64, gpt_header[0x30..0x38], max_partition_lba, .little); // Last usable LBA
(table.disk_id orelse Guid.rand(random)).write(gpt_header[0x38..0x48]);
std.mem.writeInt(u64, gpt_header[0x48..0x50], 2, .little); // First LBA of the partition entry array
std.mem.writeInt(u32, gpt_header[0x50..0x54], @intCast(table.partitions.len), .little); // Number of partition entries
Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure about this change you made, I'm pretty sure there was a reason I set this to 128. Please don't merge this before I've had a further look.

@bnuuydev
Copy link
Author

bnuuydev commented Jun 14, 2025

thoughts on having a flag to mark the protective MBR as bootable? this is required for bios/gpt boots with some bioses

Sorry but I'm seeing what you've pushed here, and to me it doesn't make sense. The protective MBR shouldn't ever have the bootable flag set for a GPT system.

To quote the UEFI spec: "If set to any value other than 0x00 the behavior of this flag on non-UEFI systems is undefined. Must be ignored by UEFI implementations."

@Khitiara
Copy link
Collaborator

thoughts on having a flag to mark the protective MBR as bootable? this is required for bios/gpt boots with some bioses

Sorry but I'm seeing what you've pushed here, and to me it doesn't make sense. The protective MBR shouldn't ever have the bootable flag set for a GPT system.

To quote the UEFI spec: "If set to any value other than 0x00 the behavior of this flag on non-UEFI systems is undefined. Must be ignored by UEFI implementations."

when booting BIOS/GPT, the value may still be checked by the bios, the efi spec just doesnt define anything about it; i can confirm that seabios (as used by qemu) checks it and setting the flag was required to make my os bootable as bios/gpt, otherwise nothing gets booted.

@bnuuydev
Copy link
Author

bnuuydev commented Jun 15, 2025

Wait, you're using this with BIOS? But there's no provision to set the bootloader in this, and I think any BIOS support for this GPT implementation should be dealt with in a separate PR (if at all – why can't you just use MBR?)

To clarify, it should be in a separate PR as if we do go ahead with hybrid MBR, this should be considered together, not least because it would possibly require breaking the syntax for the config flag you've used here. This PR's scope is strictly for booting GPT with UEFI, which by the spec should not require the protective MBR bootable flag set.

@ikskuh
Copy link
Contributor

ikskuh commented Jun 15, 2025

To clarify, it should be in a separate PR as if we do go ahead with hybrid MBR, this should be considered together, not least because it would possibly require breaking the syntax for the config flag you've used here. This PR's scope is strictly for booting GPT with UEFI, which by the spec should not require the protective MBR bootable flag set.

Not really tbh. if it works on qemu, i'm fine with keeping it. We can always change it later and adjust if we figured out it's a problem with real hw.

Imho we gain a lot if it works both with BIOS and UEFI

(if at all – why can't you just use MBR?

because then you can't boot the same disk image on UEFI systems. this way, you can construct a universal boot stick/disk

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.

3 participants