ajxs / uefi-elf-bootloader

UEFI ELF Bootloader example
GNU General Public License v3.0
91 stars 12 forks source link

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

Open sfiedler0 opened 7 months ago

sfiedler0 commented 7 months ago

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?

ajxs commented 6 months ago

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.

sfiedler0 commented 6 months ago

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

ajxs commented 6 months ago

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.

sfiedler0 commented 6 months ago

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

ajxs commented 6 months ago

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.

sfiedler0 commented 6 months ago

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…

ajxs commented 4 months ago

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.