OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 6:59 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: How to access files when booting from UEFI?
PostPosted: Fri Jan 31, 2020 7:57 am 
Offline

Joined: Fri Jan 31, 2020 7:51 am
Posts: 5
Hello everyone,
I am trying to write a simple UEFI bootloader which loads the kernel from the same directory as the loader. However, I cannot figure out how to load the kernel from the disk. I am using gnu-efi, if that matters.


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Fri Jan 31, 2020 9:53 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
This is rather more complex than I think it should be, but here is an example:

https://github.com/kiznit/rainbow-os/bl ... fifile.cpp

I use this code to load my kernel from an EFI bootloader.

Calling code looks like this:

Code:

    void* kernelData;
    size_t kernelSize;

    status = LoadFile(L"\\EFI\\rainbow\\kernel", kernelData, kernelSize);
    if (EFI_ERROR(status))
    {
        Fatal("Failed to load kernel: %p\n", status);
    }

    Log("Kernel loaded at: %p, size: %x\n", kernelData, kernelSize);

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Sun Feb 02, 2020 4:07 am 
Offline

Joined: Fri Jan 31, 2020 7:51 am
Posts: 5
Thanks, but it still does not work. I don't know what I am doing wrong here...
Code:
#include <efi/efi.h>
#include <efi/efilib.h>
#include <efi/x86_64/efibind.h>
EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {

   InitializeLib(ImageHandle, SystemTable);

    EFI_LOADED_IMAGE_PROTOCOL* lip=NULL;
    EFI_GUID lipguid=EFI_LOADED_IMAGE_PROTOCOL_GUID;

    EFI_STATUS status=BS->HandleProtocol(ImageHandle,&lipguid,(void**)&lip);

    if(EFI_ERROR(status) || !lip)
        Print(L"An error occured");

   while(1){
   }

   return EFI_SUCCESS;
}

It always shows an error.


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Sun Feb 02, 2020 11:49 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
This code looks fine to me. Any chance you can print out what that error is so that it points us in the right direction?

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Tue Feb 04, 2020 4:44 am 
Offline

Joined: Fri Jan 31, 2020 7:51 am
Posts: 5
It is error code 2: EFI_INVALID_PARAMETER


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Tue Feb 04, 2020 10:36 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
Are you not supposed to use uefi_call_wrapper() when using gnu-efi? (I am not using that library by the way.)

See example here: https://sourceforge.net/p/gnu-efi/code/ ... /apps/t6.c

In your case, this would mean something like:

Code:
EFI_STATUS status = uefi_call_wrapper(BS->HandleProtocol, 3, ImageHandle, &lipguid, (void**)&lip);


See if you can get that "t6.c" example to work, it's part of the gnu-efi source distribution.

If this doesn't work, then something is probably wrong with the way you build the code.

1) Build t6.c with the gnu-efi makefile and verify that it works correctly.
2) Copy the t6.c code into your own project, build it, verify if it still works.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Wed Feb 05, 2020 10:12 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
kzinti wrote:
Are you not supposed to use uefi_call_wrapper() when using gnu-efi?
Depends on which ABI you have configured your toolchain to compile for. The big advantage of gnu-efi is that it allows you to use EFI ABI (in which case you don't need uefi_call_wrapper) but also to simply use your host environment's native ABI (in which case you should use uefi_call_wrapper). It's up to you. Other SDK's only allow EFI ABI, and demand specific cross-platform toolchain. With gnu-efi, you have an option.

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Wed Feb 05, 2020 11:45 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
I actually use the EFI ABI with my Linux host compiler that wasn't configured to build for EFI ABI. So I don't need to rely on uefi_call_wrapper() or anything like it.

I am just trying to help the OP. His code looks ok, yet it doesn't work. So I suspect it indeed has something to do with the default ABI of his toolchain, hence my question about using uefi_call_wrapper().

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Thu Feb 06, 2020 6:55 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
kzinti wrote:
I actually use the EFI ABI with my Linux host compiler that wasn't configured to build for EFI ABI. So I don't need to rely on uefi_call_wrapper() or anything like it.
Uhm, if your compiler is not configured for EFI ABI, that's when you have to use uefi_call_wrapper. :-) I'm pretty sure your cross-toolchain is configured properly.

kzinti wrote:
I am just trying to help the OP. His code looks ok, yet it doesn't work.
And you are doing a great job. I agree this is strange, the code looks just fine indeed, it should work.

kzinti wrote:
So I suspect it indeed has something to do with the default ABI of his toolchain, hence my question about using uefi_call_wrapper().
You are right. Although it's not a must, but better to use uefi_call_wrapper with gnu-efi. Wouldn't hurt, and it makes the source less compiler config dependent. I did not wanted to suggest that your advice is incorrect, I just wanted to say uefi_call_wrapper is not a must, rather compiler config dependent.

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Thu Feb 06, 2020 9:49 am 
Offline

Joined: Fri Jan 31, 2020 7:51 am
Posts: 5
YES!! It works now. But why do we need to use uefi_call_wrapper?


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Thu Feb 06, 2020 11:49 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
bzt wrote:
Uhm, if your compiler is not configured for EFI ABI, that's when you have to use uefi_call_wrapper. :-) I'm pretty sure your cross-toolchain is configured properly.

And you are wrong. I am not even using a cross-toolchain. I am using my host Linux compiler.

How can that be? Well turns out GCC allows you to specify the ABI per function using a function attribute. So what I do is simply mark the UEFI protocol methods to use the EFI ABI even though the rest of my code is not using it.

I did end up having to use some trickery inspired from gnu-efi to generate a PE file and do relocations... But the end result is that I don't need a cross-compiler and I am not using gnu-efi.

_________________
https://github.com/kiznit/rainbow-os


Last edited by kzinti on Thu Feb 06, 2020 11:50 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Thu Feb 06, 2020 11:50 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
chocabloc wrote:
YES!! It works now. But why do we need to use uefi_call_wrapper?

If it works, then you don't need to use uefi_call_wrapper() and can ignore it.

I'm curious, how did you solve the problem? What was the issue?

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Thu Feb 06, 2020 7:45 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
kzinti wrote:
bzt wrote:
Uhm, if your compiler is not configured for EFI ABI, that's when you have to use uefi_call_wrapper. :-) I'm pretty sure your cross-toolchain is configured properly.
And you are wrong.
Nope, I'm not. You're not using the native ABI, which means you had to tell that to the compiler somehow ;-) Either it's through command line options, function attributes, linker script options, or spelling out uefi_call_wrapper each time, doesn't matter.
kzinti wrote:
I am not even using a cross-toolchain. I am using my host Linux compiler.

How can that be? Well turns out GCC allows you to specify the ABI per function using a function attribute.
Which means you have configured your cross-toolchain properly. And since your code won't run on the host system, you're surely cross-compiling. :-) The uefi_call_wrapper is needed, you just told to the compiler to inline it for you on every call automatically. It's a nicer solution than writing out uefi_call_wrapper each time imho, but less portable I think. Is it a gcc-only solution? (There's nothing wrong with saying you have to compile this code with gcc, that's perfectly fine, and the code is more readable.)

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Thu Feb 06, 2020 8:09 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
Hey bzt,

Can you stop derailing threads with your need to have the final word in every side discussion? It's not remotely relevant, interesting or useful. I am pretty sure the OP doesn't care whether you or me is right.

If you feel the need to have endless argumentative debates with people, maybe an os dev forum is not the best place for this. It can be very off-putting to people asking for help and to people trying to help them.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: How to access files when booting from UEFI?
PostPosted: Fri Feb 07, 2020 12:18 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
Dear kzinti,

I have no need to have the final word, I have a need to be technically correct. As long as you are spreading misinformation (like one can compile EFI apps without compiling for a special ABI) I'll keep correcting you, because I believe having technically correct information is essential for this community. There are way too many misleading information on the net and way too many incorrect tutorials already. Designing and implementing an operating system is a notoriously hard task enough, there's no need to mislead people.

Best regards,
bzt


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 49 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group