UEFI - ExitBootServices
-
- Posts: 16
- Joined: Thu Apr 02, 2020 5:24 pm
UEFI - ExitBootServices
Hello, i have question about ExitBootServices function from UEFI. (I'm french)
I seen this function can pass the hand to a true OS, but i don't understand how this function work.
I need 2 arguments:
ImageHandle
MapKey.
But how can I indicate to the UEFI what code he need to execute after ExitBootServices ?
Can I for example copy some code at memory and jump to this code after?
Do you know some example with some "Hello world kernel" ?
And other question: I have read I can access UEFI function after ExitBootServices call if I don't altier a specific part of memory, but what part ? How can I do that ? Is there some UEFI function for do that before ExitBootServices?
I seen this function can pass the hand to a true OS, but i don't understand how this function work.
I need 2 arguments:
ImageHandle
MapKey.
But how can I indicate to the UEFI what code he need to execute after ExitBootServices ?
Can I for example copy some code at memory and jump to this code after?
Do you know some example with some "Hello world kernel" ?
And other question: I have read I can access UEFI function after ExitBootServices call if I don't altier a specific part of memory, but what part ? How can I do that ? Is there some UEFI function for do that before ExitBootServices?
-
- Member
- Posts: 5568
- Joined: Mon Mar 25, 2013 7:01 pm
Re: UEFI - ExitBootServices
This is the first parameter passed to your main function.Fulgurance wrote:ImageHandle
You can get this value using GetMemoryMap(). It's the third parameter.Fulgurance wrote:MapKey
ExitBootServices() will return to wherever it was called from.Fulgurance wrote:But how can I indicate to the UEFI what code he need to execute after ExitBootServices ?
If you want to, yes.Fulgurance wrote:Can I for example copy some code at memory and jump to this code after?
Check the memory map returned by GetMemoryMap() to see which parts you shouldn't alter.Fulgurance wrote:I have read I can access UEFI function after ExitBootServices call if I don't altier a specific part of memory, but what part ?
-
- Posts: 16
- Joined: Thu Apr 02, 2020 5:24 pm
Re: UEFI - ExitBootServices
Yes, i know about parameters, it's just habit I have when I post question,I add details sorry
Okay, thanks for your replies, it's clear now ! Except just for check the memory map returned by GetMemoryMap().
You say me: when i call GetMemoryMap(), i can see which parts I shouldn't alter. But how ? This function return all of memory, but how can I know which parts of memory are the UEFI firmware?
Or this function return only UEFI memory?
And when i ExitBootServices and i don't alter memory, how can I call UEFI services ? Is it possible with flat binary code for example ? Or again with PE format?
Okay, thanks for your replies, it's clear now ! Except just for check the memory map returned by GetMemoryMap().
You say me: when i call GetMemoryMap(), i can see which parts I shouldn't alter. But how ? This function return all of memory, but how can I know which parts of memory are the UEFI firmware?
Or this function return only UEFI memory?
And when i ExitBootServices and i don't alter memory, how can I call UEFI services ? Is it possible with flat binary code for example ? Or again with PE format?
-
- Member
- Posts: 5568
- Joined: Mon Mar 25, 2013 7:01 pm
Re: UEFI - ExitBootServices
Each entry in the memory map has a type, and the type tells you whether it's safe to modify that range of memory. Take a look at Table 30 on page 160 of the current version of the UEFI specification (2.8 Errata B) for details.
-
- Posts: 16
- Joined: Thu Apr 02, 2020 5:24 pm
Re: UEFI - ExitBootServices
Oh perfect thanks you ! It's exactly what I need !
Just last question ! If after I do ExitBootServices and I want to call UEFI function, I finally just need to save UEFI Handle address and SystemTable address ?
Just last question ! If after I do ExitBootServices and I want to call UEFI function, I finally just need to save UEFI Handle address and SystemTable address ?
-
- Member
- Posts: 5568
- Joined: Mon Mar 25, 2013 7:01 pm
Re: UEFI - ExitBootServices
As far as I know, you only need the system table address to call runtime services.
Re: UEFI - ExitBootServices
You can only use the runtime services after calling ExitBootServices, not the boot services. The runtime services functions don't take a UEFI handle as the first parameter like the boot services functions do.Fulgurance wrote:Just last question ! If after I do ExitBootServices and I want to call UEFI function, I finally just need to save UEFI Handle address and SystemTable address ?
You can just hold a pointer to the runtime services, there is no need to hold the system tables one.
-
- Posts: 16
- Joined: Thu Apr 02, 2020 5:24 pm
Re: UEFI - ExitBootServices
I have just a question, after I exit the boot services, how I can copy memory to the RAM or use AllocatePool service, because I can't will access to the CopyMem function ... Is there other way with UEFI protocols ?
Re: UEFI - ExitBootServices
Once you've exited boot services, UEFI relinquishes control of memory management to you. You must write your own memory allocator. Generally, writing an full on memory allocator is a bit much for a bootloader, so what I did was before exiting boot services, I allocated a big glob of memory with AllocatePages. Then, I wrote a simple placement allocator for this memory area.
Re: UEFI - ExitBootServices
No there isn't, and you can't use AllocatePool either after ExitBootServices. You should implement your own memory allocator, using pages marked as free from GetMemoryMap.Fulgurance wrote:I have just a question, after I exit the boot services, how I can copy memory to the RAM or use AllocatePool service, because I can't will access to the CopyMem function ... Is there other way with UEFI protocols ?
In theory you can still access all services which are marked RunTime service if you pass the SystemTable pointer to your kernel. But,Fulgurance wrote:And when i ExitBootServices and i don't alter memory, how can I call UEFI services ?
a) there aren't many useful runtime services (I dunno, reset perhaps?)
b) all the useful services are boot time services (memory management, file system access, GOP, etc. etc. etc.)
c) you'll notice serious incompatibility issues with different manufacturers
So all in all it's better just to forget about UEFI services after you call ExitBootServices.
Absolutely yes. My boot loader can load both PE32+ and ELF64 binaries. Passing the SystemTable to your kernel depends on the ABI you choose for the handover, it has nothing to do with the file format.Fulgurance wrote:Is it possible with flat binary code for example ? Or again with PE format?
Sure.Fulgurance wrote:Do you know some example with some "Hello world kernel" ?
x86_64-efi/bootboot.c is where I call ExitBootServices
mykernel is an example, very minimal "Hello world kernel" in C (Rust example also available)
Cheers,
bzt
-
- Posts: 16
- Joined: Thu Apr 02, 2020 5:24 pm
Re: UEFI - ExitBootServices
For example, can I use EFI_PCI function to manage the use of the RAM ?
If UEFI isn't available, how can I know how to manage the RAM, is it the same when before I made OS for the BIOS, or it's different specification ?
If UEFI isn't available, how can I know how to manage the RAM, is it the same when before I made OS for the BIOS, or it's different specification ?
-
- Member
- Posts: 5568
- Joined: Mon Mar 25, 2013 7:01 pm
Re: UEFI - ExitBootServices
No.Fulgurance wrote:For example, can I use EFI_PCI function to manage the use of the RAM ?
It's almost exactly the same. The memory map format is a bit different since you're using GetMemoryMap() instead of INT 0x15 EAX=0xE820.Fulgurance wrote:If UEFI isn't available, how can I know how to manage the RAM, is it the same when before I made OS for the BIOS, or it's different specification ?
Re: UEFI - ExitBootServices
Yeah, they are so similar that in legacy CSM mode UEFI actually converts the output of GetMemoryMap into E820 on-the-fly (see LegacyBiosBuildE820 in TianoCore).Octocontrabass wrote:It's almost exactly the same. The memory map format is a bit different since you're using GetMemoryMap() instead of INT 0x15 EAX=0xE820.Fulgurance wrote:If UEFI isn't available, how can I know how to manage the RAM, is it the same when before I made OS for the BIOS, or it's different specification ?
About managing the RAM, you'll have to do it yourself. A simple overview:
1. create a list of free pages at boot time (using GetMemoryMap or E820) pass that to your kernel
2. manage those pages yourself with a physical page allocator in your kernel (allocate / free one physical RAM page at a time)
3. in your libc, implement userspace malloc (which can allocate / free arbitrary amounts of memory, and calls the page allocator when needed through a syscall)
For the 3., you can use an already written library like dlmalloc, ptmalloc, jemalloc etc. provided you've implemented the POSIX syscalls they require (usually sbrk or mmap).
Cheers,
bzt
-
- Posts: 16
- Joined: Thu Apr 02, 2020 5:24 pm
Re: UEFI - ExitBootServices
Okay, I have lasts questions
What website explain UEFI good ? For example, paging is already enabled or I need to enable it ? Is there section into UEFI documentation explain what settings need to be set before exit boot services ?
And if I want to get the size of the RAM, is it possible? I'm not sure, but I think memory map contain just the used memory.
What website explain UEFI good ? For example, paging is already enabled or I need to enable it ? Is there section into UEFI documentation explain what settings need to be set before exit boot services ?
And if I want to get the size of the RAM, is it possible? I'm not sure, but I think memory map contain just the used memory.
Re: UEFI - ExitBootServices
UEFI is very new. Your best bets for info on it are:
This forum (i.e., looking at older posts)
The wiki
Maybe SO?
The official spec, which can be found with a quick Google search
This forum (i.e., looking at older posts)
The wiki
Maybe SO?
The official spec, which can be found with a quick Google search