Skip to content
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

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

Open
sfiedler0 opened this issue Nov 24, 2023 · 7 comments
Open

Comments

@sfiedler0
Copy link

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
Copy link
Owner

ajxs commented Dec 6, 2023

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
Copy link
Author

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
Copy link
Owner

ajxs commented Dec 8, 2023

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
Copy link
Author

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
Copy link
Owner

ajxs commented Dec 18, 2023

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
Copy link
Author

sfiedler0 commented Dec 23, 2023

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 😄 .
Perhaps I will try to get a bit into C coding and make a pull request here…

@ajxs
Copy link
Owner

ajxs commented Feb 17, 2024

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.

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

No branches or pull requests

2 participants