OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:37 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: UEFI - ExitBootServices
PostPosted: Fri Nov 13, 2020 11:46 am 
Offline

Joined: Thu Apr 02, 2020 5:24 pm
Posts: 16
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?


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Nov 13, 2020 12:07 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
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.


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Nov 13, 2020 12:25 pm 
Offline

Joined: Thu Apr 02, 2020 5:24 pm
Posts: 16
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?


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Nov 13, 2020 12:32 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
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.


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Nov 13, 2020 3:55 pm 
Offline

Joined: Thu Apr 02, 2020 5:24 pm
Posts: 16
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 ?


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Nov 13, 2020 4:27 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
As far as I know, you only need the system table address to call runtime services.


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Nov 13, 2020 4:31 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
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.

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


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Dec 25, 2020 7:41 am 
Offline

Joined: Thu Apr 02, 2020 5:24 pm
Posts: 16
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 ?


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Dec 25, 2020 8:10 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
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.

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Dec 25, 2020 8:15 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Dec 25, 2020 11:42 am 
Offline

Joined: Thu Apr 02, 2020 5:24 pm
Posts: 16
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 ?


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Dec 25, 2020 4:58 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
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.


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Fri Dec 25, 2020 5:54 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Sun Dec 27, 2020 4:19 am 
Offline

Joined: Thu Apr 02, 2020 5:24 pm
Posts: 16
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.


Top
 Profile  
 
 Post subject: Re: UEFI - ExitBootServices
PostPosted: Sun Dec 27, 2020 7:27 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
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

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 125 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