Page 1 of 3
Efi bootloader, PE Loading Image = really stuck.
Posted: Tue Nov 17, 2015 3:33 am
by albus95
I'm just say am not an expert.
So i'm trying to develop an os that biot using uefi instead if the normal bootloader. So for now following different source in the internet i'm now able to load a ntfs partion where i load a second uefi imaga that do the load of the operating system.
I'm using windows so i'm using visusl studio for compilation and here is where all the problems begin.
In The second stage loader i'm able to load an pe image that contain the kernel of the os that for now is a just a function like:
Code: Select all
VOID __stdcall EntryPoint (EFI_LOADER_INFO *LoaderInfo)
{
for (; ;)
;
}
i'm passing a paramater that contain information optained ftom the uefi
Enviroment.
Any way i really cannot understand iwhat i need to for launche the kernel file in the second stage efi bootloader.
As i understand i need to use BS->AllocatePages for getting the efi memory ready for loading my pe image than before run the image i have call BS->ExitBootServices to stop allthe efi services. Of course i gave get the memory map by calling BS->GetMemoryMap.
So can someone help me on how to load an pe image an launch it? I'm able to get tge address of entry point by reading the image but than i don't know what to do.
I compiling in 64bit mode.
Also what are the first think that kernel have to do?
Do the interrupt table is arleady set up on the efi side?
I'm arleady in long mode right?
Thanks for the help, albus95.
Re: Efi bootloader + operating system, really stuck.
Posted: Tue Nov 17, 2015 10:01 am
by intx13
You don't necessarily need multiple stages with UEFI, since there's no tightly constraining size limit on the PE files that a UEFI BIOS loads. If you want to pack everything into a single PE file and let UEFI load it into memory for you, you can do that. Heck, it could be one giant main() function if you want.
Code: Select all
VOID __stdcall EntryPoint (EFI_LOADER_INFO *LoaderInfo)
{
// Do bootloader stuff here - fetch memory map, etc.
// Don't need UEFI BIOS-provided functions anymore? Call ExitBootServices to free them
// from memory. Or don't, doesn't really matter.
// Do OS stuff here - initialize process list / context structures, scan PCI bus, etc.
}
If you want to use multiple UEFI applications though, the UEFI BIOS provides functions that will load them for you. Check out the
LoadImage and
StartImage functions.
If you want to load non-UEFI PE files (maybe you want to store your drivers in PE files or something, but they're not UEFI applications) then you will need to implement PE loading, or link against an open source library that will do it for you.
albus95 wrote:
Do the interrupt table is arleady set up on the efi side?
I'm arleady in long mode right?
Yes, on an x86-64 bit CPU the UEFI BIOS will put the system into long mode before launching any UEFI applications. The interrupt table is already set up, and will include handlers for fatal errors (GPF, etc.) that will print out a "screen of death" and hang. The PIT will be configured for use as a watchdog timer (but you can disable this and reconfigure it for your own use). The keyboard and screen will be configured for "teletype" text output (but if you don't plan on returning back to UEFI or using UEFI-provided keyboard and screen functions, then you can reconfigure it however you'd like). The PCI(E) bus will be configured with BARs assigned and bridges configured. AHCI controllers will be configured (but your OS may want to reset/reconfigure them anyway).
Take a look at the
OSDEV wiki page on UEFI for more introductory information.
Also what are the first think that kernel have to do?
That's really up to you, the style of kernel you are interested in developing, etc. One starting point would be to read the UEFI memory map, store it in memory in some format you like, and write simple implementations of kmalloc and kfree so that the rest of your kernel can dynamically allocate memory after you've called ExitBootServices.
Re: Efi bootloader + operating system, really stuck.
Posted: Tue Nov 17, 2015 1:44 pm
by albus95
Thanky You! for your answer.
Re: Efi bootloader + operating system, really stuck.
Posted: Tue Nov 17, 2015 2:27 pm
by albus95
I still have no idea on how I can launch a PE image. Can someone walk through the necessary code?
I'm following in part this microswoft example
https://msdn.microsoft.com/en-us/library/ms836780.aspx, but it miss part of code unfortunally.
I'compiling a 64bit excutable using visual studio.
Just get the image loaded and launched would be a milestone for me! Ah Ah
Re: Efi bootloader + operating system, really stuck.
Posted: Tue Nov 17, 2015 3:11 pm
by intx13
albus95 wrote:I still have no idea on how I can launch a PE image. Can someone walk through the necessary code?
I'm following in part this microswoft example
https://msdn.microsoft.com/en-us/library/ms836780.aspx, but it miss part of code unfortunally.
I'compiling a 64bit excutable using visual studio.
Just get the image loaded and launched would be a milestone for me! Ah Ah
In your initial post you implied that you had successfully compiled and executed a UEFI application (a PE image). Is that true? If so, what development environment did you use (GNU-EFI or TianoCore)? What platform did you use to test the application (real hardware or KVM or OVMF)? What is in the PE image that you'd like to launch, another UEFI application or something else?
If I misunderstood you and you
haven't successfully compiled and executed a UEFI application, I would recommend starting with GNU-EFI, as described on the link I gave previously and (in more depth) in the
UEFI Bare Bones.
Additionally, the MSDN link you referenced is intended for OEM developers integrating Windows CE onto a custom UEFI-based platform. Does that describe you? If not, that's probably not a good reference.
Re: Efi bootloader + operating system, really stuck.
Posted: Tue Nov 17, 2015 3:21 pm
by albus95
Yes yes I'm able to compile a efi application, I think I express wrong the concept.
I want to load a normal exe excutable that contain the kernel not an efi image ( I think is like windows does).
I'm using gnu-efi library, it would be good to use tiano core but it not yet compatible with visual studio 2015 unfortunally.
for test I'm using hyper-v. I know I can use vmware but I use hyper-v for other virtual machine unfortunally.
Re: Efi bootloader + operating system, really stuck.
Posted: Wed Nov 18, 2015 3:39 am
by Combuster
A normal .exe differs in many details from an EFI binary (subsystem, relocatable). You're probably not going to convince EFI to actually load one so either you'll have to make it a true EFI binary (which makes it look like a redundant step because your loader is already just that), or you manually parse the exe file yourself.
Re: Efi bootloader + operating system, really stuck.
Posted: Wed Nov 18, 2015 6:03 am
by albus95
In fact i'm trying to parse the exe file and is there where i really have no idea on what to do. I can read the header of the file and get the address of the entry point but from there i have no idea on how to make it work.
Re: Efi bootloader + operating system, really stuck.
Posted: Wed Nov 18, 2015 6:30 am
by intx13
The title of this thread should really be "How to implement PE loader?" No connection to UEFI really.
If you search the forums for "PE vs ELF" you'll find a few threads where folks have posted the full source of PE (and ELF) loaders. A simple PE loader, minus dynamic linking, can be implemented very simply it seems.
What are you using as a reference for the PE file format?
Re: Efi bootloader + operating system, really stuck.
Posted: Wed Nov 18, 2015 7:28 am
by albus95
Thanks i will search for it
Re: Efi bootloader + operating system, really stuck.
Posted: Wed Nov 18, 2015 7:59 am
by intx13
The forum search tool won't search for two-letter keywords. Here's the thread I meant:
http://f.osdev.org/viewtopic.php?f=1&t=17686
The second page has some useful information. PE files don't look too complicated though, could you provide more information on what you're stuck on? Just not sure where to start after parsing the header? You might find this site useful:
http://win32assembly.programminghorizon ... -tut1.html
Re: Efi bootloader + operating system, really stuck.
Posted: Wed Nov 18, 2015 12:36 pm
by albus95
I'm stuck because don't kno PE fomat as I though ah ah
Any way I read the header of the PE 64 bit image and can get the address of the entry point, but from than I don't kno what I need to load the be able to launch the entry function of the PE file.
All the tutorials/posts use memory map file but that is not, as far as I know, in UEFI.
It is difficult implement file mapping memory or is better use another way to get be able to run the PE file?
Re: Efi bootloader + operating system, really stuck.
Posted: Wed Nov 18, 2015 3:45 pm
by albus95
So when I ge the entry point address, how I can call it?
Right now I'm doing this:
Code: Select all
VOID *EntryPoint = OptionalHeader->ImageBase + OptionalHeader->AddressOfEntryPoint;
void (*EntryPoint)(LOADER_EFI_BLOCK *) = LoadedInfo->EntryPoint;
But than if I excute the function the virtual machine restart.
I'm really doing something stupid!, Right?
Re: Efi bootloader + operating system, really stuck.
Posted: Wed Nov 18, 2015 4:35 pm
by intx13
albus95 wrote:I'm stuck because don't kno PE fomat as I though ah ah
Any way I read the header of the PE 64 bit image and can get the address of the entry point, but from than I don't kno what I need to load the be able to launch the entry function of the PE file.
All the tutorials/posts use memory map file but that is not, as far as I know, in UEFI.
It is difficult implement file mapping memory or is better use another way to get be able to run the PE file?
Memory mapping is what user space programs typically do to load a PE file from disk into memory. For your purposes, "memory mapping" just means loading the file into memory using normal UEFI file IO functions.
albus95 wrote:So when I ge the entry point address, how I can call it?
Right now I'm doing this:
Code: Select all
VOID *EntryPoint = OptionalHeader->ImageBase + OptionalHeader->AddressOfEntryPoint;
void (*EntryPoint)(LOADER_EFI_BLOCK *) = LoadedInfo->EntryPoint;
But than if I excute the function the virtual machine restart.
I'm really doing something stupid!, Right?
(
Warning, I know basically nothing about PE!) If you don't load the PE file into memory at the correct virtual address so its sections line up in memory at the addresses they expect, you'll need to rebase them. How are you deciding where to load the PE file into memory? Since you don't have any other PE files loaded, you can probably inspect the PE file to see where it would like to be loaded, then load it there. There's nothing to conflict with, other than UEFI BIOS and your UEFI application.
What's in your PE file?
This site might be useful too:
https://msdn.microsoft.com/en-us/library/ms809762.aspx
Re: Efi bootloader, PE Loading Image = really stuck.
Posted: Wed Nov 18, 2015 4:38 pm
by albus95
It will contain the kernel.
So for now there is only one function that is the entry point of course.
I set the image base at 0x00400000 (in the linker option).
So I, as arleady thought, I need to allocated pages using BS->AllocatesPages right?
I need than to know the number of pages but I think I can get them from the header of the PE file, right?