Sign in Join
Wiki copytemporary

UEFI: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: UEFI

OldNewLine
1+## Motivations behind this target
2+
3+The Unified Extensible Firmware Interface (UEFI) now replaces BIOS in most current PCs.
4+
5+Currently, there are two main options to develop UEFI applications:
6+
7+- [Tianocore](http://www.tianocore.org/)
8+- [gnu-efi](http://sourceforge.net/projects/gnu-efi/)
9+
10+Both solutions require a tedious setup process.
11+
12+Thank to its internal linker, Free Pascal can become the easiest option to develop UEFI applications, as no external tools would be required to start hacking.
13+
14+## Technical requirements
15+
16+UEFI applications are PE binaries like the Windows target, which Free Pascal already supports. The only difference is the subsystem in the PE header.
17+
18+Calling conventions under UEFI are identical to the ones under Windows on the same architecture, which Free Pascal already supports, too.
19+
20+## Current state
21+
22+*My current patch against trunk is here : <http://nan.tf/download/uefi_all_03_01_2016.diff>.*
23+
24+*It is not yet a clean patch : lots of debug output inside.*
25+
26+**I have resume my work on this target. Here is my updated patch against trunk : <http://nan.tf/download/fpc_uefi_28_04_2017.diff>**
27+
28+Currently, I am focusing on UEFI32 because i have a few cheap atom machines with that version (see <https://software.intel.com/en-us/blogs/2015/07/22/why-cheap-systems-run-32-bit-uefi-on-x64-systems>) and because i use Haiku as my main development platform, without a 64 bits freepascal yet.
29+
30+But UEFI64 support should be easy to had with careful API declarations.
31+
32+A branch is now available with the current development state at <https://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/olivier/uefi/>
33+
34+## Build instructions
35+
36+For UEFI 32 target, the build command line is :
37+
38+`make all OS_TARGET=uefi CPU_TARGET=i386`
39+
40+For UEFI 64 target, the build command line is :
41+
42+`make all OS_TARGET=uefi CPU_TARGET=x86_64`
43+
44+If you want to cross compile from a different architecture, it might be necessary to specify OS_SOURCE and CPU_SOURCE. Here is for example the command line for Haiku 32 bits.
45+
46+`make all OS_TARGET=uefi CPU_TARGET=x86_64 OS_SOURCE=haiku CPU_SOURCE=i386`
47+
48+## Known problems
49+
50+*Affectation of string constant to string variable does not work. It is currently necessary to create strings through an array of WideChar.*
51+
52+``` pascal
53+var
54+ s : string;
55+begin
56+ s := 'Hello world !';
57+end;
58+```
59+
60+*Maybe it is related to Position Independent Code generation in the case of PE executables ?*
61+
62+*Any hints in this area are welcome ;-)*
63+
64+**With current trunk (3.1.1), this bug has been fixed. It helps a lot to be able to use WriteLn('Something to write') instead of constructing an array of WideChar and call UEFI functions as before ;-).**
65+
66+Other known problem :
67+
68+``` pascal
69+ list := TStringList.Create;
70+```
71+
72+While constructing other kind of object works as expected, this one currently crash the program... I will have to investigate.
73+
74+Update : it seems related to uses : without any uses in my program, it just works... With only one, it crash.
75+
76+Update 2 : I have finally found the problem : SysUtils initialize some static constructions BEFORE unit initialization. That is why it was always crashing when adding a uses to SysUtils in a simple program, but before the **initialization** section. That explain why my debugging messages in SysUtils where never displayed...
77+
78+## Testing UEFI applications with qemu
79+
80+See [OSDev UEFI Emulation with QEMU and OVMF](http://wiki.osdev.org/UEFI#Emulation_with_QEMU_and_OVMF)
81+
82+To avoid creating an image, i use FAT emulation to a local directory where my UEFI application is located :
83+
84+``` bash
85+qemu-system-i386-x86 -pflash /path/to/OVMF.fd -hda fat:local_directory -net none
86+```
87+
88+"-net none" avoid net booting to save a few seconds in the boot process.
89+
90+From UEFI shell, then write :
91+
92+`fs0:`
93+
94+to switch to the directory where are located your application then you can launch it :
95+
96+`app.efi`
97+
98+## History
99+
100+Everything has started after reading this article while i was lurking about UEFI development : <http://blog.theincredibleholk.org/blog/2013/11/18/booting-to-rust/>
101+
102+What about doing that in Freepascal as well ?
103+
104+It appears that Freepascal is even a better target for such a project because of its internal linker for PE targets : no need to install and configure an external one.
105+
106+**August 2014**
107+
108+Proof of concept : Freepascal is able to output a PE binary that can run in an UEFI environment. It can output a static text and the version of the firmware to the screen using an UEFI method.
109+
110+**April 2017**
111+
112+After observing that an annoying bug about string constant was fixed, i have resumed my work on this target.
113+
114+Currently, here are the features that are implemented :
115+
116+`- WriteLn support (no more awkward array of WideChar allocation required)`\
117+`- Memory allocation`
118+
119+**May 2017**
120+
121+Start of x86_64 support (not functional yet). The idea is to check if the 'uses' problem is related to the use of PE32 binary format. UEFI is supposed to use PE32+ (with 64 bits relocations). UEFI specifications are not clear whether this variant is also used for UEFI32. With a x86_64 CPU and the win64 linker, this uncertainty should disappear...
122+
123+Update : after a binary comparison between two different binaries compiled with the official UEFI tools, UEFI32 binaries are plain PE32 ones, with an IMAGE_OPTIONAL_HEADER with a size of 224 bytes while an UEFI64 binary contains an IMAGE_OPTIONAL_HEADER with a size of 240 bytes (some fields are 64 bits instead of 32 bits). This is confirmed in <https://msdn.microsoft.com/en-us/library/windows/desktop/ms680339(v=vs.85>).aspx. Afterthought, it make sense : i suspect Intel has chosen PE binary format and Application Binary Interface (ABI) like the corresponding ones in Windows to minimize the work at Microsoft and make sure they will adopt the UEFI specifications...
124+
125+**August 2017**
126+
127+## External resources
128+
129+Some interesting development resources around UEFI :
130+
131+- [UEFI specifications](http://www.uefi.org/)
132+- [OS development under UEFI](http://wiki.osdev.org/UEFI)
133+- [Managing EFI Boot Loaders for Linux: Basic Principles](http://www.rodsbooks.com/efi-bootloaders/principles.html)
134+
135+---
136+*From the Free Pascal wiki, page "UEFI", as it stood on 2020-03-01 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
137+Category: Operating Systems and Platforms

Back to history