UEFI: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
m Added download section and links
m added POSIX links
Line 122:
 
A good starting point is writing a UEFI application that uses the System Table to fetch a memory map, and uses the "File" protocol to read files from FAT-formatted disks. The next step might be to use the System Table to locate ACPI tables.
 
==Developing with POSIX-UEFI==
:''Main article: [[POSIX-UEFI]]
 
An easy way to compile EFI applications on Linux (or any other POSIX compatible system) is POSIX-UEFI. It does not only provide the well-known [[libc]] API for your EFI application, but also ships a Makefile that can detect and set up the toolchain for you. Works with both GNU gcc and LLVM CLang. Normally uses the host's compiler, but it is also capable of detecting the need for cross-compilation.
 
it has POSIX-ized typedefs (like ''uintn_t'' instead of ''UINTN''), and it does not need nor ship the standard EFI headers. But if you install those from EDK2 or GNU-EFI, you can safely include those as well, there will be no conflicts.
 
The traditional "Hello, world" UEFI program is shown below. Note that instead of ''char **argv'', it has ''wchar_t **argv''.
<source lang="c">
#include <uefi.h>
int main (int argc, wchar_t **argv)
{
printf(L"Hello, world!\n");
return 0;
}
</source>
Makefile looks like this:
<source lang="make">
TARGET = main.efi
include uefi/Makefile
</source>
Just run ''make'', and the rest is taken care for you. The result of this process is a 17 kB PE executable file ''main.efi''.
 
==Developing with GNU-EFI==
Line 535 ⟶ 559:
Note that functions strictly internal to the application can use whatever calling convention the developer chooses.
 
==== POSIX-UEFI, GNU-EFI and GCC ====
{{Main|GNU-EFI}}
cdecl is the standard calling convention used by GCC, so no special attributes or modifiers are needed for writing the main entry point or calling UEFI functions in an x86 UEFI application developed with GNU-EFI. For x86-64, however, the entry point function must be declared with the "___attribute___((ms_abi))" modifier and all calls to UEFI-provided functions must be made through the "uefi_call_wrapper" thunk. This thunk is called with cdecl, but then translates to the Microsoft x86-64 calling convention before calling the requested UEFI function. This is necessary because older releases of GCC do not support specifying calling conventions for function pointers.
 
For POSIX-UEFI, your entry point looks like the standard main, and no special ABI is required for it. It still needs the same "uefi_call_wrapper" for accessing UEFI functions though.
For developer convenience, GNU-EFI provides the "EFIAPI" macro, which expands to "cdecl" when targeting x86 and "__attribute__(ms_abi))" when targeting x86-64. Additionally, the "uefi_call_wrapper" thunk will simply pass the call through on x86. This allows the same source code to target x86 and x86-64. For example, the following main function will compile with the correct calling convention on both x86 and x86-64 and the call through the "uefi_call_wrapper" thunk will select the correct calling convention to use when calling the UEFI function (in this case, printing a string).
 
For developer convenience, both POSIX-UEFI and GNU-EFI provides the "EFIAPI" macro, which expands to "cdecl" when targeting x86 and "__attribute__(ms_abi))" when targeting x86-64. Additionally, the "uefi_call_wrapper" thunk will simply pass the call through on x86. This allows the same source code to target x86 and x86-64. For example, the following main function will compile with the correct calling convention on both x86 and x86-64 and the call through the "uefi_call_wrapper" thunk will select the correct calling convention to use when calling the UEFI function (in this case, printing a string).
<source lang="c">
EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
Line 600 ⟶ 626:
* [[PE]] file format
* [[TianoCore]]
* [[POSIX-UEFI]]
* [[GNU-EFI]]
* [https://github.com/nebulaeonline/nebulae/tree/UefiBarebones Uefi Barebones MSVC/Clang/Visual Studio]
 
Line 612 ⟶ 640:
* [http://internshipatdell.wikispaces.com/file/view/How+to+build+an+UEFI+application.pptx Presentation guiding through simple UEFI application setup]
* [http://www.uefi.org/sites/default/files/resources/UEFI-Plugfest-WindowsBootEnvironment.pdf Presentation giving an overview of windows uefi booting]
* [https://gitlab.com/bztsrc/posix-uefi POSIX-UEFI] documentation and source
* [[Wikipedia:Extensible_Firmware_Interface|Wikipedia Article on EFI]]