Page 1 of 2

UEFI - ExitBootServices

Posted: Fri Nov 13, 2020 11:46 am
by Fulgurance
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?

Re: UEFI - ExitBootServices

Posted: Fri Nov 13, 2020 12:07 pm
by Octocontrabass
Fulgurance wrote:ImageHandle
This is the first parameter passed to your main function.
Fulgurance wrote:MapKey
You can get this value using GetMemoryMap(). It's the third parameter.
Fulgurance wrote:But how can I indicate to the UEFI what code he need to execute after ExitBootServices ?
ExitBootServices() will return to wherever it was called from.
Fulgurance wrote:Can I for example copy some code at memory and jump to this code after?
If you want to, yes.
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 ?
Check the memory map returned by GetMemoryMap() to see which parts you shouldn't alter.

Re: UEFI - ExitBootServices

Posted: Fri Nov 13, 2020 12:25 pm
by Fulgurance
Yes, i know about parameters, it's just habit I have when I post question,I add details sorry :lol:

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?

Re: UEFI - ExitBootServices

Posted: Fri Nov 13, 2020 12:32 pm
by Octocontrabass
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.

Re: UEFI - ExitBootServices

Posted: Fri Nov 13, 2020 3:55 pm
by Fulgurance
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 ?

Re: UEFI - ExitBootServices

Posted: Fri Nov 13, 2020 4:27 pm
by Octocontrabass
As far as I know, you only need the system table address to call runtime services.

Re: UEFI - ExitBootServices

Posted: Fri Nov 13, 2020 4:31 pm
by kzinti
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 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.

You can just hold a pointer to the runtime services, there is no need to hold the system tables one.

Re: UEFI - ExitBootServices

Posted: Fri Dec 25, 2020 7:41 am
by Fulgurance
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

Posted: Fri Dec 25, 2020 8:10 am
by nexos
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

Posted: Fri Dec 25, 2020 8:15 am
by bzt
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 ?
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:And when i ExitBootServices and i don't alter memory, how can I call UEFI services ?
In theory you can still access all services which are marked RunTime service if you pass the SystemTable pointer to your kernel. But,
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.
Fulgurance wrote:Is it possible with flat binary code for example ? Or again with PE format?
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:Do you know some example with some "Hello world kernel" ?
Sure.
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

Re: UEFI - ExitBootServices

Posted: Fri Dec 25, 2020 11:42 am
by Fulgurance
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 ?

Re: UEFI - ExitBootServices

Posted: Fri Dec 25, 2020 4:58 pm
by Octocontrabass
Fulgurance wrote:For example, can I use EFI_PCI function to manage the use of the RAM ?
No.
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 ?
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.

Re: UEFI - ExitBootServices

Posted: Fri Dec 25, 2020 5:54 pm
by bzt
Octocontrabass wrote:
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 ?
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.
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).

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

Re: UEFI - ExitBootServices

Posted: Sun Dec 27, 2020 4:19 am
by Fulgurance
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.

Re: UEFI - ExitBootServices

Posted: Sun Dec 27, 2020 7:27 am
by nexos
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