uefi-elf-bootloader icon indicating copy to clipboard operation
uefi-elf-bootloader copied to clipboard

AllocateAddress in src/booloader/src/loader.c makes errors (NotFound)

Open sfiedler0 opened this issue 1 year ago • 7 comments

Recently, I wanted to start developing some kind of simple combination of bootloader and kernel in ZIG (https://ziglang.org) and I searched for an example. Because of that, I found this really cool repo and rewrote it in ZIG.
But, as I executed it, there was the error "NotFound", which, after some research, was obvious to be in src/booloader/src/loader.c:62. Because I have zero experience about programming bootloaders and I wanted (as described above) to have a good example, it would be wonderful to implement physical address handling. Or is there anything I could read about that on OSDev?

sfiedler0 avatar Nov 24 '23 18:11 sfiedler0

Hello! Are you saying that you rewrote the bootloader in Zig? According to the UEFI spec, on page 163, if you're getting an EFI_NOT_FOUND error when calling AllocatePages, it's because "The requested pages could not be found". Maybe you're trying to allocate memory you don't have. The spec isn't very clear what this means. Without seeing your code, I'm not sure what's going on.

There is some information about UEFI on osdev.org. I got most of my information from the UEFI specification. The actual 'loader' part of my code isn't much different than parsing an ELF executable in userspace.

ajxs avatar Dec 06 '23 21:12 ajxs

Well, I ran both bootloaders (this one and the one in Zig) and both had this error.
By the way, the bootloader I wrote is available on https://codeberg.org/samuel-fiedler/zig_os.
And yes, I know what EFI_NOT_FOUND means when allocating pages, but I don't know how to fix that...

sfiedler0 avatar Dec 08 '23 06:12 sfiedler0

Hmm. I'm currently traveling at the moment. When I get home in a couple of days I'll try to take a look at your kernel and see if there's anything I can find that would cause this.

ajxs avatar Dec 08 '23 06:12 ajxs

The EFI_NOT_FOUND error is fixed now in my ZIG bootloader...
I am not so good in writing C code, so you could perhaps translate it to C here...

sfiedler0 avatar Dec 17 '23 16:12 sfiedler0

That's good news that you've resolved the issue in your bootloader. What was causing the issue? I can see the commit in your repository where you resolved the issue, but there's too many changes in that commit to pin down exactly what the cause was.

ajxs avatar Dec 18 '23 22:12 ajxs

The cause for that issues is that we can't, as far as I know, assume that any address in the memory is free because of UEFI.
Basically, my solution was to call get_memory_map from src/bootloader/src/main.c:117 twice. One time to exit the boot services, and one time to find a free memory address for the kernel.
You can see that address locating in src/bootloader/main.zig:81. To actually make that address usable, src/bootloader/loader.zig:121 and, directly below, line 130, is important. Also, because I did these steps, I also needed virtual address mapping in src/bootloader/main.zig:156.
My repo had that many changes because I did not only find free memory addresses, but also because I had issues with the ELF parsing. Finally, I used the ELF header parsing function from the Zig Standard Library, because I did not find my C -> Zig translation errors :smile: .
Perhaps I will try to get a bit into C coding and make a pull request here…

sfiedler0 avatar Dec 23 '23 20:12 sfiedler0

I sincerely apologise for not replying to this earlier! The last few months have been so busy on my end. Thank you very much for your hard work in figuring this out. I appreciate your explanation. This makes perfect sense. I should have realised this would eventually become a problem. I've started some work to redesign the bootloader to resolve this issue.

ajxs avatar Feb 17 '24 20:02 ajxs