UEFI Loading a File

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
shipsimfan
Posts: 2
Joined: Thu Mar 23, 2017 1:14 pm

UEFI Loading a File

Post by shipsimfan »

Hi, I am new here and have been searching the internet high and low for an answer to this question and I am unable to find it. How do I load a file off of my boot disk using UEFI? I have a file called "kernel.elf" and I want to load it into memory address 0x100000. From there I know how to read the elf file and execute it. I am using GNU-EFI.
User avatar
zaval
Member
Member
Posts: 659
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: UEFI Loading a File

Post by zaval »

shipsimfan wrote:Hi, I am new here and have been searching the internet high and low for an answer to this question and I am unable to find it. How do I load a file off of my boot disk using UEFI? I have a file called "kernel.elf" and I want to load it into memory address 0x100000. From there I know how to read the elf file and execute it. I am using GNU-EFI.
That's awesome. But have you tried to read the UEFI specification? Particularly - section 6.4? I'd recommend to read the whole section 2 at least.
Finally, as the name implies, that is your kernel, right? that means you are at the stage where your OS loader took control of the UEFI and it's up to you how you are going to load your file. Before you call ExitBootServices() function you can rely on the FW Boot services in its entirity. LoadImage() function is of particular interest for you. Read its description carefully. But I think you need to read much more than that in the specification.
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
shipsimfan
Posts: 2
Joined: Thu Mar 23, 2017 1:14 pm

Re: UEFI Loading a File

Post by shipsimfan »

One issue I have with using the LoadImage function is that I am unable to specify a specific memory location for the file to be loaded to and I am also unable to get a pointer to where the file was loaded. One other question is how the Device Path type works and how to specify one.
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: UEFI Loading a File

Post by Rusky »

Instead of LoadImage, you can use SimpleFileSystemProtocol- lets you traverse the file system, open files, and read their contents into arbitrary buffers.

However, be careful just flinging data at arbitrary memory addresses- you need to make sure the firmware didn't already put something there. You can, however, request a particular address from BootServices->AllocatePages, if that makes things easier to get up and running.

Here is a simple ELF loader I use: https://github.com/rpjohnst/kernel/blob ... src/boot.c
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: UEFI Loading a File

Post by Roman »

You can use the simple file system protocol. If you want to access the ESP, then see this.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
zaval
Member
Member
Posts: 659
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: UEFI Loading a File

Post by zaval »

One issue I have with using the LoadImage function is that I am unable to specify a specific memory location for the file to be loaded to and I am also unable to get a pointer to where the file was loaded. One other question is how the Device Path type works and how to specify one.
In fact, if you want to control a location where to load your image you need 1) to allocate the buffer at that location (AllocatePages()), and only after successful allocation, load there your image. This is done by EFI_FILE_PROTOCOL. First you obtain instance of EFI_FILE_PROTOCOL for the root directory of the volume where your image resides (through the volume's instance EFI_SIMPLE_FILE SYSTEM_PROTOCOL OpenVolume() function), the volume should be a FAT partition. then find your file, using EFI_FILE_PROTOCOL functions. Then read your file, using them as well. Read EFI_FILE_PROTOCOL.
But it's not that easy. You should behave as a proper OSLoader - taking over the control of the FW, calling GetMemoryMap() and ExitBootServices(). This phase is not as easy as just loading the file and jumping at it. For example, if you won't take control over, calling ExitBootServices(), FW still will run (the timer ISR, also watchdog, which might reset the board).
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
Post Reply