Major overhaul of this branch.

This branch has been dedicated to purely the UEFI bootloader.
As such, all other code has been removed.

This code can be compiled with Visual Studio, gcc or llvm.
master
Curle 5 years ago
parent 352db828e7
commit d588e232c4

@ -1,10 +0,0 @@
---
DerivePointerAlignment: 'true'
SpaceBeforeParens: ControlStatements
UseTab: ForIndentation
IndentWidth: 2
TabWidth: 2
ColumnLimit: 120
BreakBeforeBraces: Attach
...

2
.gitattributes vendored

@ -0,0 +1,2 @@
*.sh eol=lf
Makefile eol=lf

71
.gitignore vendored

@ -1,59 +1,14 @@
# Created by https://www.gitignore.io/api/c
# Edit at https://www.gitignore.io/?templates=c
### C ###
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# End of https://www.gitignore.io/api/c
*.efi
*.vhd
*.sdf
*.suo
*.opensdf
*.opendb
*.fd
*.db*
arm
aa64
x86_64
x86_32
image

4
.gitmodules vendored

@ -0,0 +1,4 @@
[submodule "gnu-efi"]
path = gnu-efi
url = git://git.code.sf.net/p/gnu-efi/code
ignore = dirty

@ -0,0 +1,3 @@
{
"CurrentProjectSetting": "No Configurations"
}

@ -0,0 +1,9 @@
{
"ExpandedNodes": [
"",
"\\arch",
"\\arch\\uefi"
],
"SelectedNode": "\\.gitignore",
"PreviewInSolutionExplorer": false
}

Binary file not shown.

@ -1,9 +1,6 @@
# Sync
##Sync UEFI Bootloader
Sync, an experimental synchronising OS.
This branch represents the UEFI bootloader used by Sync.
It tries to set a nice video mode, then gets the date and time, then finds, packs and loads the kernel, giving the information gathered to it.
Currently features:
* Text mode printing with CSI support
* literally nothing else
Keep posted. Development is relatively active.
A .sln project is provided for convenience. The Windows SDK is required to compile using it, but it also works with gcc and llvm.

@ -1,28 +0,0 @@
[BITS 32] ;... somehow.
[GLOBAL load_gdt]
[EXTERN gp]
load_gdt:
lgdt [gp]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:flush ; Far jump and load the new CSD into the processor
flush:
ret
[GLOBAL load_idt]
[EXTERN idtp]
load_idt:
lidt [idtp]
ret
SECTION .bss
resb 8192
_sys_stack:

@ -1,52 +0,0 @@
/************************
*** Team Kitty, 2019 ***
*** Sync ***
***********************/
/* The entry point of the UEFI app.
* When the firmware loads the app,
* it calls the efi_main function.
*
* When it is called, the processor
* is running in 32-bit Protected mode.
* It is given a map of memory, and there
* are a bunch of Boot Services provided
* by the UEFI firmware.
*
* See uefi/docs for more information.
*/
//Example stolen from osdev.org/uefi_bare_bones
#include <efi.h>
#include <efilib.h>
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
EFI_STATUS Status;
EFI_INPUT_KEY Key;
/* Store the system table for future use in other functions */
ST = SystemTable;
/* Say hi */
Status = ST->ConOut->OutputString(ST->ConOut, L"Hello World\n\r");
if (EFI_ERROR(Status))
return Status;
/* Now wait for a keystroke before continuing, otherwise your
message will flash off the screen before you see it.
First, we need to empty the console input buffer to flush
out any keystrokes entered before this point */
Status = ST->ConIn->Reset(ST->ConIn, FALSE);
if (EFI_ERROR(Status))
return Status;
/* Now wait until a key becomes available. This is a simple
polling implementation. You could try and use the WaitForKey
event instead if you like */
while ((Status = ST->ConIn->ReadKeyStroke(ST->ConIn, &Key)) == EFI_NOT_READY) ;
return Status;
}

@ -1,25 +0,0 @@
OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}

@ -1,9 +0,0 @@
KERNEL_ARCH_CFLAGS=
KERNEL_ARCH_CPPFLAGS=
KERNEL_ARCH_LDFLAGS=
KERNEL_ARCH_LIBS=
KERNEL_ARCH_OBJS= \
$(ARCHDIR)/boot.o \
$(ARCHDIR)/sys_clock.o \
$(ARCHDIR)/tty.o

@ -1,35 +0,0 @@
/************************
*** Team Kitty, 2019 ***
*** Sync ***
***********************/
/* This file provides an interface to
* the hardware timer / RTC. Not much
* more to be said about it. */
#include <kernel/utils.h>
#include <kernel.h>
#include <kernel/serial.h>
#include <kernel/descriptor_tables.h>
size_t timer_ticks = 0;
size_t flag = 0;
void timer_handler(struct int_frame* r) {
gdb_end();
timer_ticks++;
if(timer_ticks % 18 == 0) {
if(++flag % 2 == 0) {
serial_print(0x3F8, "Tick.");
} else {
serial_print(0x3F8, "Tock.");
}
}
if(timer_ticks > 18)
timer_ticks = 0;
}
void timer_install() {
irq_install_handler(0, &timer_handler);
}

@ -1,348 +0,0 @@
/************************
*** Team Kitty, 2019 ***
*** Sync ***
***********************/
/* This file provides all of the functionality
* needed to interact with the Text-Mode VGA
* buffer, which will soon be defunct due to
* EFI and graphics.
*
* This file will be left for the forseeable
* future, because it allows for easy debugging.
* 17/07/19 Curle
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
//#include <string.h>
#include "kernel/tty.h"
#include "kernel/utils.h"
static const size_t TERM_WIDTH = 80;
static const size_t TERM_HEIGHT = 25;
static size_t terminal_row;
static size_t terminal_column;
static uint8_t current_color;
static uint16_t* term_buffer;
volatile uint16_t* vga_buffer = (uint16_t*)0xB8000;
void screen_initialize(void) {
terminal_row = 0;
terminal_column = 0;
current_color = vga_color_set(LIGHT_GREY, BLACK);
term_buffer = vga_buffer;
for (size_t y = 0; y < TERM_HEIGHT; y++) {
for (size_t x = 0; x < TERM_WIDTH; x++) {
const size_t offset = y * TERM_WIDTH + x;
term_buffer[offset] = vga_entry(' ', current_color);
}
}
}
void term_setcolor(enum vga_colors color) { current_color = color; }
void term_putentryat(char c, uint8_t color, size_t x, size_t y) {
const size_t offset = y * TERM_WIDTH + x;
term_buffer[offset] = vga_entry(c, color);
}
void term_putchar(char c) {
unsigned char uc = c;
// Handle escaped characters, such as newline, and crtn.
switch (uc) {
case '\n':
terminal_column = 0;
terminal_row += 1;
break;
default:
term_putentryat(uc, current_color, terminal_column, terminal_row);
if (++terminal_column == TERM_WIDTH) {
terminal_column = 0;
if (++terminal_row == TERM_HEIGHT) {
term_scroll(false);
terminal_row = 0;
}
}
break;
}
}
struct csi_sequence parse_csi(const char* data, size_t size) {
enum State { PARAMETER, INTERMEDIATE, FINAL, INVALID };
enum State state = PARAMETER;
struct csi_sequence sequence = {.parameter = NULL,
.parameter_len = 0,
.intermediate = NULL,
.intermediate_len = 0,
.final = NULL,
.valid = false};
for (size_t j = 0; j < size; j++) {
uint8_t c = data[j];
if (state == PARAMETER && (c >= 0x30 && c <= 0x3F)) {
if (!sequence.parameter)
sequence.parameter = data + j;
sequence.parameter_len++;
} else if (c >= 0x20 && c <= 0x2F) {
if (!sequence.intermediate)
sequence.intermediate = data + j;
sequence.intermediate_len++;
state = INTERMEDIATE;
} else if (c >= 0x40 && c <= 0x7F) {
sequence.final = data + j;
sequence.valid = true;
state = FINAL;
break;
} else {
// Parameter found in intermediate byte location, or byte out of
// range
state = INVALID;
break;
}
}
return sequence;
}
void term_write(const char* data, size_t size) {
for (size_t i = 0; i < size; i++) {
// Begin handling ANSI escape codes.
if (data[i] == 0x1b) { // The current character is ESC - the start of ANSI codes.
// term_writes("ANSI Code encountered: ");
bool string_terminated = false; // Flag used in some of the escape codes
// TODO: Should only progress if we have at least 2 more bytes
switch ((uint8_t)data[i + 1]) {
case '[': // CSI - Control Sequence Introducer (The most common one, hence it comes first)
{
struct csi_sequence sequence = parse_csi(data + i + 2, size - (i + 2));
if (sequence.valid) {
// Send it off to our handler function to keep this part clean
handleControlSequence(sequence);
i += sequence.parameter_len + sequence.intermediate_len + 2; // Move past sequence
}
} break;
// Single shifts are not handled, so just print them and exit
case 'N': // SS2 - Single Shift Two
term_writes("Single Shift Two\n");
break;
case 'O': // SS3 - Single Shift Three
term_writes("Single Shift Three\n");
break;
// Control Strings
case 'P': // DCS - Device Control String
term_writes("Device Control String");
string_terminated = false;
break;
case '\\': // ST - String Terminator
term_writes("String Terminator\n");
string_terminated = true;
break;
case ']': // OSC - Operating System Command
term_writes("Operating System Command\n");
string_terminated = false;
break;
case 'X': // SOS - Start Of String
term_writes("Start of String");
string_terminated = false;
break;
case '^': // PM - Privacy Message
term_writes("Privacy Message\n");
break;
case '_': // APC - Application Program Command
term_writes("Application Program Command\n");
break;
}
} else {
term_putchar(data[i]);
}
}
}
void term_writes(const char* data) { term_write(data, strlen(data)); }
void puts(const char* string) {
term_write(string, strlen(string));
term_putchar('\n');
}
void handleControlSequence(struct csi_sequence sequence) {
// Check for our failsafes
if (sequence.valid) {
int n = 0; // Default of the flag used for a few items
// Parse parameters, we only care about a max 2 of number only parameters
// for now
int params[2] = {0};
int param_count = 0;
if (sequence.parameter_len) {
for (size_t i = 0; i < sequence.parameter_len && param_count < 1; i++) {
char c = sequence.parameter[i];
if (isDigit(c)) {
n = (n * 10) + (sequence.parameter[i] - '0');
} else if (c == ';') {
params[param_count++] = n;
}
}
params[param_count++] = n;
}
switch (*(sequence.final)) {
case 'H':
case 'f': // CUP - Cursor Position
// TODO: Check to see if we have 2 paramaters
if (params[0])
params[0]--;
if (params[1])
params[1]--;
set_cursor(params[0], params[1]);
break;
case 'A': // CUU - Cursor Up
if (!params[0])
params[0] = 1;
set_cursor(terminal_column, terminal_row - params[0]);
break;
case 'B': // CUD - Cursor Down
if (!params[0])
params[0] = 1;
set_cursor(terminal_column, terminal_row + params[0]);
break;
case 'C': // CUF - Cursor Forward
if (!params[0])
params[0] = 1;
set_cursor(terminal_column + params[0], terminal_row);
break;
case 'D': // CUB - Cursor Back
if (!params[0])
params[0] = 1;
set_cursor(terminal_column - params[0], terminal_row);
break;
case 'E': // CNL - Cursor Next Line
if (!params[0])
params[0] = 1;
set_cursor(0, terminal_row + params[0]);
break;
case 'F': // CPL - Cursor Previous Line
if (!params[0])
params[0] = 1;
set_cursor(0, terminal_row - params[0]);
break;
case 'G': // CHA - Cursor Horizontal Absolute
if (params[0])
params[0]--;
set_cursor(params[0], terminal_row);
break;
case 'J': // ED - Erase in Display
{
// current cursor pos = y * width + x
int pos = terminal_row * 80 + terminal_column;
if (params[0] == 0) { // Clear from cursor to end of screen
for (; pos < (25 * 80); pos++) {
vga_buffer[pos] = '\0';
}
} else if (params[0] == 1) { // Clear from cursor to beginning
for (; pos > 0; pos--) {
vga_buffer[pos] = '\0';
}
} else if (params[0] == 2 || params[0] == 3) { // Clear entire screen
// TODO: Support scrollback buffer? (n = 3)
for (int i = 0; i < (25 * 80); i++) {
vga_buffer[0] = '\0';
}
}
break;
}
case 'K': // EL - Erase in Line
{
int pos = terminal_row * 80 + terminal_column;
if (params[0] == 0) { // From cursor to end of line
int endPos = (terminal_row + 1) * 80 - 1; // End of line = current row + 25 columns = current row + 1
for (; pos < endPos; pos++) {
vga_buffer[pos] = '\0';
}
} else if (params[0] == 1) { // From cursor to start of line
int endPos = terminal_row * 80; // Start of line = end of previous line + 1 == current line
for (; pos > endPos; pos--) {
vga_buffer[pos] = '\0';
}
} else if (params[0] == 2) { // Entire current line
pos = terminal_row * 80;
int endPos = (terminal_row + 1) * 80 - 1;
for (; pos < endPos; pos++) {
vga_buffer[pos] = '\0';
}
}
break;
}
case 'S': // SU - Scroll Up
term_scroll(true);
break;
case 'T': // SD - Scroll Down
term_scroll(false);
break;
}
}
}
bool isDigit(char c) { return c >= '0' && c <= '9'; }
void set_cursor(int n, int m) {
terminal_column = n;
terminal_row = m;
}
void term_scroll(bool down) {
int current_pos;
if (down) {
current_pos = 25 * 80; // Start of the last line.
} else {
current_pos = 160; // Start of the second line.
}
unsigned char* term_buffer = (unsigned char*)vga_buffer;
if (down) { // To scroll down, move every character into the one below it, or "pull" every character down
while (current_pos > 80) {
term_buffer[current_pos + 160] = vga_buffer[current_pos];
term_buffer[current_pos + 159] = vga_buffer[current_pos - 1];
current_pos -= 2;
}
} else {
while (current_pos <= 4000) {
term_buffer[current_pos - 160 /*The character immediately below it*/] =
vga_buffer[current_pos]; // Move each character up a line
term_buffer[current_pos - 159 /*The color of the character below*/] =
vga_buffer[current_pos + 1]; // As well as its color
current_pos += 2; // Move to next char
}
}
if (down) {
current_pos = 0; // Start of first line
for (; current_pos < 80; current_pos++) {
term_buffer[current_pos] = '\0';
current_pos += 2;
}
} else {
; // Start of the last line
// Wipe out the bottom line
for (current_pos = 3840; current_pos <= 3920; current_pos += 2) {
term_buffer[current_pos] = 0;
}
}
terminal_row = 24; // Start writing on the last line
terminal_column = 0;
}

@ -1,53 +0,0 @@
# configuration file generated by Bochs
plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, iodebug=1
config_interface: textconfig
display_library: x
memory: host=32, guest=32
romimage: file="/usr/share/bochs/BIOS-bochs-latest", address=0x0, options=none
vgaromimage: file="/usr/share/bochs/VGABIOS-lgpl-latest"
boot: a
floppy_bootsig_check: disabled=1
floppya: 1_44=/dev/loop0, status=inserted
# no floppyb
ata0: enabled=0
ata1: enabled=0
ata2: enabled=0
ata3: enabled=0
optromimage1: file=none
optromimage2: file=none
optromimage3: file=none
optromimage4: file=none
optramimage1: file=none
optramimage2: file=none
optramimage3: file=none
optramimage4: file=none
pci: enabled=1, chipset=i440fx
vga: extension=vbe, update_freq=5, realtime=1
cpu: count=1:1:1, ips=1000000, quantum=16, model=bx_generic, reset_on_triple_fault=0, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0
cpuid: level=6, stepping=3, model=3, family=6, vendor_string="AuthenticAMD", brand_string="AMD Athlon(tm) processor"
cpuid: mmx=1, apic=xapic, simd=sse2, sse4a=0, misaligned_sse=0, sep=1, movbe=0, adx=0
cpuid: aes=0, sha=0, xsave=0, xsaveopt=0, avx_f16c=0, avx_fma=0, bmi=0, xop=0, fma4=0
cpuid: tbm=0, x86_64=1, 1g_pages=0, pcid=0, fsgsbase=0, smep=0, smap=0, mwait=1
print_timestamps: enabled=0
debugger_log: ../red.debug
magic_break: enabled=1
port_e9_hack: enabled=0
private_colormap: enabled=0
clock: sync=none, time0=local, rtc_sync=0
# no cmosimage
# no loader
log: ../red.log
logprefix: %t%e%d
debug: action=ignore
info: action=report
error: action=report
panic: action=ask
keyboard: type=mf, serial_delay=250, paste_delay=100000, user_shortcut=none
mouse: type=ps2, enabled=0, toggle=ctrl+mbutton
speaker: enabled=1, mode=system
parport1: enabled=1, file=none
parport2: enabled=0
com1: enabled=1, mode=null
com2: enabled=0
com3: enabled=0
com4: enabled=0

@ -0,0 +1,141 @@
' Visual Studio QEMU debugging script.
'
' I like invoking vbs as much as anyone else, but we need to download and unzip our
' firmware file, as well as launch QEMU, and neither Powershell or a standard batch
' can do that without having an extra console appearing.
'
' Note: You may get a prompt from the firewall when trying to download the BIOS file
' Modify these variables as needed
QEMU_PATH = "C:\Program Files\qemu\"
' You can add something like "-S -gdb tcp:127.0.0.1:1234" if you plan to use gdb to debug
QEMU_OPTS = "-net none -monitor none -parallel none"
' Set to True if you need to download a file that might be cached locally
NO_CACHE = False
' You shouldn't have to modify anything below this
TARGET = WScript.Arguments(1)
If (TARGET = "x86") Then
UEFI_EXT = "ia32"
QEMU_ARCH = "i386"
FW_BASE = "OVMF"
ElseIf (TARGET = "x64") Then
UEFI_EXT = "x64"
QEMU_ARCH = "x86_64"
FW_BASE = "OVMF"
ElseIf (TARGET = "ARM") Then
UEFI_EXT = "arm"
QEMU_ARCH = "arm"
FW_BASE = "QEMU_EFI"
' You can also add '-device VGA' to the options below, to get graphics output.
' But if you do, be mindful that the keyboard input may not work... :(
QEMU_OPTS = "-M virt -cpu cortex-a15 " & QEMU_OPTS
ElseIf (TARGET = "ARM64") Then
UEFI_EXT = "aa64"
QEMU_ARCH = "aarch64"
FW_BASE = "QEMU_EFI"
QEMU_OPTS = "-M virt -cpu cortex-a57 " & QEMU_OPTS
Else
MsgBox("Unsupported debug target: " & TARGET)
Call WScript.Quit(1)
End If
BOOT_NAME = "boot" & UEFI_EXT & ".efi"
QEMU_EXE = "qemu-system-" & QEMU_ARCH & "w.exe"
FW_ARCH = UCase(UEFI_EXT)
FW_DIR = "https://efi.akeo.ie/" & FW_BASE & "/"
FW_ZIP = FW_BASE & "-" & FW_ARCH & ".zip"
FW_FILE = FW_BASE & "_" & FW_ARCH & ".fd"
FW_URL = FW_DIR & FW_ZIP
' Globals
Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")
' Download a file from FTP
Sub DownloadFtp(Server, Path)
Set file = fso.CreateTextFile("ftp.txt", True)
Call file.Write("open " & Server & vbCrLf &_
"anonymous" & vbCrLf & "user" & vbCrLf & "bin" & vbCrLf &_
"get " & Path & vbCrLf & "bye" & vbCrLf)
Call file.Close()
Call shell.Run("%comspec% /c ftp -s:ftp.txt > NUL", 0, True)
Call fso.DeleteFile("ftp.txt")
End Sub
' Download a file from HTTP
Sub DownloadHttp(Url, File)
Const BINARY = 1
Const OVERWRITE = 2
Set xHttp = createobject("Microsoft.XMLHTTP")
Set bStrm = createobject("Adodb.Stream")
Call xHttp.Open("GET", Url, False)
If NO_CACHE = True Then
Call xHttp.SetRequestHeader("If-None-Match", "some-random-string")
Call xHttp.SetRequestHeader("Cache-Control", "no-cache,max-age=0")
Call xHttp.SetRequestHeader("Pragma", "no-cache")
End If
Call xHttp.Send()
If Not xHttp.Status = 200 Then
Call WScript.Echo("Unable to access file - Error " & xHttp.Status)
Call WScript.Quit(1)
End If
With bStrm
.type = BINARY
.open
.write xHttp.responseBody
.savetofile File, OVERWRITE
End With
End Sub
' Unzip a specific file from an archive
Sub Unzip(Archive, File)
Const NOCONFIRMATION = &H10&
Const NOERRORUI = &H400&
Const SIMPLEPROGRESS = &H100&
unzipFlags = NOCONFIRMATION + NOERRORUI + SIMPLEPROGRESS
Set objShell = CreateObject("Shell.Application")
Set objSource = objShell.NameSpace(fso.GetAbsolutePathName(Archive)).Items()
Set objTarget = objShell.NameSpace(fso.GetAbsolutePathName("."))
' Only extract the file we are interested in
For i = 0 To objSource.Count - 1
If objSource.Item(i).Name = File Then
Call objTarget.CopyHere(objSource.Item(i), unzipFlags)
End If
Next
End Sub
' Check that QEMU is available
If Not fso.FileExists(QEMU_PATH & QEMU_EXE) Then
Call WScript.Echo("'" & QEMU_PATH & QEMU_EXE & "' was not found." & vbCrLf &_
"Please make sure QEMU is installed or edit the path in '.msvc\debug.vbs'.")
Call WScript.Quit(1)
End If
' Fetch the UEFI firmware and unzip it
If Not fso.FileExists(FW_FILE) Then
Call WScript.Echo("The UEFI firmware file, needed for QEMU, " &_
"will be downloaded from: " & FW_URL & vbCrLf & vbCrLf &_
"Note: Unless you delete the file, this should only happen once.")
Call DownloadHttp(FW_URL, FW_ZIP)
End If
If Not fso.FileExists(FW_ZIP) And Not fso.FileExists(FW_FILE) Then
Call WScript.Echo("There was a problem downloading the QEMU UEFI firmware.")
Call WScript.Quit(1)
End If
If fso.FileExists(FW_ZIP) Then
Call Unzip(FW_ZIP, FW_BASE & ".fd")
Call fso.MoveFile(FW_BASE & ".fd", FW_FILE)
Call fso.DeleteFile(FW_ZIP)
End If
If Not fso.FileExists(FW_FILE) Then
Call WScript.Echo("There was a problem unzipping the QEMU UEFI firmware.")
Call WScript.Quit(1)
End If
' Copy the app file as boot application and run it in QEMU
Call shell.Run("%COMSPEC% /c mkdir ""image\efi\boot""", 0, True)
Call fso.CopyFile(WScript.Arguments(0), "image\efi\boot\" & BOOT_NAME, True)
Call shell.Run("""" & QEMU_PATH & QEMU_EXE & """ " & QEMU_OPTS & " -L . -bios " & FW_FILE & " -hda fat:rw:image", 1, True)

@ -0,0 +1,6 @@
*.efi
*.efi.debug
*.o
*.a
*.tar.*
*.tar

File diff suppressed because it is too large Load Diff

@ -0,0 +1,183 @@
# -*- makefile -*-
# Copyright (c) 1999-2007 Hewlett-Packard Development Company, L.P.
# Contributed by David Mosberger <davidm@hpl.hp.com>
# Contributed by Stephane Eranian <eranian@hpl.hp.com>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
# * Neither the name of Hewlett-Packard Co. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
TOPDIR := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)
#
# Variables below overridable from command-line:
# make VARNAME=value ...
#
#
# Where to install the package. GNU-EFI will create and access
# lib and include under the root
#
INSTALLROOT := /
PREFIX := /usr/local
LIBDIR := $(PREFIX)/lib
INSTALL := install
# Compilation tools
HOSTCC := $(prefix)gcc
CC := $(prefix)$(CROSS_COMPILE)gcc
AS := $(prefix)$(CROSS_COMPILE)as
LD := $(prefix)$(CROSS_COMPILE)ld
AR := $(prefix)$(CROSS_COMPILE)ar
RANLIB := $(prefix)$(CROSS_COMPILE)ranlib
OBJCOPY := $(prefix)$(CROSS_COMPILE)objcopy
# Host/target identification
OS := $(shell uname -s)
HOSTARCH ?= $(shell $(HOSTCC) -dumpmachine | cut -f1 -d- | sed -e s,i[3456789]86,ia32, -e 's,armv7.*,arm,' )
ARCH ?= $(shell $(HOSTCC) -dumpmachine | cut -f1 -d- | sed -e s,i[3456789]86,ia32, -e 's,armv7.*,arm,' )
# Get ARCH from the compiler if cross compiling
ifneq ($(CROSS_COMPILE),)
override ARCH := $(shell $(CC) -dumpmachine | cut -f1 -d-| sed -e s,i[3456789]86,ia32, -e 's,armv7.*,arm,' )
endif
# FreeBSD (and possibly others) reports amd64 instead of x86_64
ifeq ($(ARCH),amd64)
override ARCH := x86_64
endif
#
# Where to build the package
#
OBJDIR := $(TOPDIR)/$(ARCH)
#
# Variables below derived from variables above
#
# Arch-specific compilation flags
CPPFLAGS += -DCONFIG_$(ARCH)
CFLAGS += -Wno-error=pragmas
ifeq ($(ARCH),ia64)
CFLAGS += -mfixed-range=f32-f127
endif
ifeq ($(ARCH),ia32)
CFLAGS += -mno-mmx -mno-sse
ifeq ($(HOSTARCH),x86_64)
ARCH3264 = -m32
endif
endif
ifeq ($(ARCH),x86_64)
GCCVERSION := $(shell $(CC) -dumpversion | cut -f1 -d.)
GCCMINOR := $(shell $(CC) -dumpversion | cut -f2 -d.)
USING_CLANG := $(shell $(CC) -v 2>&1 | grep -q 'clang version' && echo clang)
# Rely on GCC MS ABI support?
GCCNEWENOUGH := $(shell ( [ $(GCCVERSION) -gt "4" ] \
|| ( [ $(GCCVERSION) -eq "4" ] \
&& [ $(GCCMINOR) -ge "7" ] ) ) \
&& echo 1)
ifeq ($(GCCNEWENOUGH),1)
CPPFLAGS += -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args --std=c11
else ifeq ($(USING_CLANG),clang)
CPPFLAGS += -DGNU_EFI_USE_MS_ABI --std=c11
endif
CFLAGS += -mno-red-zone
ifeq ($(HOSTARCH),ia32)
ARCH3264 = -m64
endif
endif
ifneq (,$(filter $(ARCH),ia32 x86_64))
# Disable AVX, if the compiler supports that.
CC_CAN_DISABLE_AVX=$(shell $(CC) -Werror -c -o /dev/null -xc -mno-avx - </dev/null >/dev/null 2>&1 && echo 1)
ifeq ($(CC_CAN_DISABLE_AVX), 1)
CFLAGS += -mno-avx
endif
endif
ifeq ($(ARCH),mips64el)
CFLAGS += -march=mips64r2
ARCH3264 = -mabi=64
endif
#
# Set HAVE_EFI_OBJCOPY if objcopy understands --target efi-[app|bsdrv|rtdrv],
# otherwise we need to compose the PE/COFF header using the assembler
#
ifneq ($(ARCH),aarch64)
ifneq ($(ARCH),arm)
ifneq ($(ARCH),mips64el)
export HAVE_EFI_OBJCOPY=y
endif
endif
endif
ifneq ($(ARCH),arm)
export LIBGCC=$(shell $(CC) $(ARCH3264) -print-libgcc-file-name)
endif
ifeq ($(ARCH),arm)
CFLAGS += -marm
endif
# Generic compilation flags
INCDIR += -I$(SRCDIR) -I$(TOPDIR)/inc -I$(TOPDIR)/inc/$(ARCH) \
-I$(TOPDIR)/inc/protocol
# Only enable -fpic for non MinGW compilers (unneeded on MinGW)
GCCMACHINE := $(shell $(CC) -dumpmachine)
ifneq (mingw32,$(findstring mingw32, $(GCCMACHINE)))
CFLAGS += -fpic
endif
ifeq (FreeBSD, $(findstring FreeBSD, $(OS)))
CFLAGS += $(ARCH3264) -g -O2 -Wall -Wextra -Werror \
-fshort-wchar -fno-strict-aliasing \
-ffreestanding -fno-stack-protector
else
CFLAGS += $(ARCH3264) -g -O2 -Wall -Wextra -Werror \
-fshort-wchar -fno-strict-aliasing \
-ffreestanding -fno-stack-protector -fno-stack-check \
-fno-stack-check \
$(if $(findstring gcc,$(CC)),-fno-merge-all-constants,)
endif
ARFLAGS := rDv
ASFLAGS += $(ARCH3264)
LDFLAGS += -nostdlib --warn-common --no-undefined --fatal-warnings \
--build-id=sha1

@ -0,0 +1,58 @@
#
# Copyright (C) 1999-2007 Hewlett-Packard Co.
# Contributed by David Mosberger <davidm@hpl.hp.com>
# Contributed by Stephane Eranian <eranian@hpl.hp.com>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
# * Neither the name of Hewlett-Packard Co. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
%.efi: %.so
$(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel \
-j .rela -j .rel.* -j .rela.* -j .rel* -j .rela* \
-j .reloc $(FORMAT) $*.so $@
%.efi.debug: %.so
$(OBJCOPY) -j .debug_info -j .debug_abbrev -j .debug_aranges \
-j .debug_line -j .debug_str -j .debug_ranges \
-j .note.gnu.build-id \
$(FORMAT) $*.so $@
%.so: %.o
$(LD) $(LDFLAGS) $^ -o $@ $(LOADLIBES)
%.o: %.c
$(CC) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
%.S: %.c
$(CC) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -S $< -o $@
%.E: %.c
$(CC) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -E $< -o $@

@ -0,0 +1,129 @@
#
# Copyright (C) 1999-2007 Hewlett-Packard Co.
# Contributed by David Mosberger <davidm@hpl.hp.com>
# Contributed by Stephane Eranian <eranian@hpl.hp.com>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
# * Neither the name of Hewlett-Packard Co. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
VERSION = 3.0.9
MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
SRCDIR = $(dir $(MKFILE_PATH))
VPATH = $(SRCDIR)
include $(SRCDIR)/Make.defaults
SUBDIRS = lib gnuefi inc apps
gnuefi: lib
all: check_gcc $(SUBDIRS)
mkvars:
@echo AR=$(AR)
@echo ARCH=$(ARCH)
@echo ARCH3264=$(ARCH3264)
@echo AS=$(AS)
@echo ASFLAGS=$(ASFLAGS)
@echo CC=$(CC)
@echo CFLAGS=$(CFLAGS)
@echo CPPFLAGS=$(CPPFLAGS)
@echo GCCMINOR=$(GCCMINOR)
@echo GCCNEWENOUGH=$(GCCNEWENOUGH)
@echo GCCVERSION=$(GCCVERSION)
@echo HOSTARCH=$(HOSTARCH)
@echo INCDIR=$(INCDIR)
@echo INSTALL=$(INSTALL)
@echo INSTALLROOT=$(INSTALLROOT)
@echo LD=$(LD)
@echo LDFLAGS=$(LDFLAGS)
@echo LIBDIR=$(LIBDIR)
@echo OBJCOPY=$(OBJCOPY)
@echo OS=$(OS)
@echo prefix=$(prefix)
@echo PREFIX=$(PREFIX)
@echo RANLIB=$(RANLIB)
@echo SRCDIR=$(SRCDIR)
@echo TOPDIR=$(TOPDIR)
$(SUBDIRS):
mkdir -p $(OBJDIR)/$@
$(MAKE) -C $(OBJDIR)/$@ -f $(SRCDIR)/$@/Makefile SRCDIR=$(SRCDIR)/$@ ARCH=$(ARCH)
clean:
rm -f *~
@for d in $(SUBDIRS); do \
if [ -d $(OBJDIR)/$$d ]; then \
$(MAKE) -C $(OBJDIR)/$$d -f $(SRCDIR)/$$d/Makefile SRCDIR=$(SRCDIR)/$$d clean; \
fi; \
done
install:
@for d in $(SUBDIRS); do \
mkdir -p $(OBJDIR)/$$d; \
$(MAKE) -C $(OBJDIR)/$$d -f $(SRCDIR)/$$d/Makefile SRCDIR=$(SRCDIR)/$$d install; done
.PHONY: $(SUBDIRS) clean depend
#
# on both platforms you must use gcc 3.0 or higher
#
check_gcc:
ifeq ($(GCC_VERSION),2)
@echo "you need to use a version of gcc >= 3.0, you are using `$(CC) --version`"
@exit 1
endif
include $(SRCDIR)/Make.rules
test-archive:
@rm -rf /tmp/gnu-efi-$(VERSION) /tmp/gnu-efi-$(VERSION)-tmp
@mkdir -p /tmp/gnu-efi-$(VERSION)-tmp
@git archive --format=tar $(shell git branch | awk '/^*/ { print $$2 }') | ( cd /tmp/gnu-efi-$(VERSION)-tmp/ ; tar x )
@git diff | ( cd /tmp/gnu-efi-$(VERSION)-tmp/ ; patch -s -p1 -b -z .gitdiff )
@mv /tmp/gnu-efi-$(VERSION)-tmp/ /tmp/gnu-efi-$(VERSION)/
@dir=$$PWD; cd /tmp; tar -c --bzip2 -f $$dir/gnu-efi-$(VERSION).tar.bz2 gnu-efi-$(VERSION)
@rm -rf /tmp/gnu-efi-$(VERSION)
@echo "The archive is in gnu-efi-$(VERSION).tar.bz2"
tag:
git tag $(VERSION) refs/heads/master
archive: tag
@rm -rf /tmp/gnu-efi-$(VERSION) /tmp/gnu-efi-$(VERSION)-tmp
@mkdir -p /tmp/gnu-efi-$(VERSION)-tmp
@git archive --format=tar $(VERSION) | ( cd /tmp/gnu-efi-$(VERSION)-tmp/ ; tar x )
@mv /tmp/gnu-efi-$(VERSION)-tmp/ /tmp/gnu-efi-$(VERSION)/
@dir=$$PWD; cd /tmp; tar -c --bzip2 -f $$dir/gnu-efi-$(VERSION).tar.bz2 gnu-efi-$(VERSION)
@rm -rf /tmp/gnu-efi-$(VERSION)
@echo "The archive is in gnu-efi-$(VERSION).tar.bz2"

@ -0,0 +1,30 @@
The files in the "lib" and "inc" subdirectories are using the EFI Application
Toolkit distributed by Intel at http://developer.intel.com/technology/efi
This code is covered by the following agreement:
Copyright (c) 1998-2000 Intel Corporation
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. THE EFI SPECIFICATION AND ALL OTHER INFORMATION
ON THIS WEB SITE ARE PROVIDED "AS IS" WITH NO WARRANTIES, AND ARE SUBJECT
TO CHANGE WITHOUT NOTICE.

@ -0,0 +1,19 @@
IMPORTANT information related to the gnu-efi package
----------------------------------------------------
June 2001
As of version 3.0, the gnu-efi package is now split in two different packages:
-> gnu-efi-X.y: contains the EFI library, include files and crt0.
-> elilo-X.y : contains the ELILO bootloader.
Note that X.y don't need to match for both packages. However elilo-3.x
requires at least gnu-efi-3.0. EFI support for x86_64 is provided in
gnu-efi-3.0d.
Both packages can be downloaded from:
http://www.sf.net/projects/gnu-efi
http://www.sf.net/projects/elilo

@ -0,0 +1,21 @@
README.git
Generating releases from git a very simple process;
1) Edit the file "Makefile". Changing the "VERSION" line to the new version.
2) Do a "git commit" just for the version number change.
3) Then do a "make test-archive".
That will make a file in the current directory gnu-efi-$VERSION.tar.bz2 ,
with its top level directory gnu-efi-$VERSION/ and the source tree under that.
Once you've tested that and you're sure it's what you want to release,
4) Do "make archive", which will tag a release in git and generate a
final tarball from it.
You then push to the archive, being sure to include the tag:
5) "git push origin master:master --tags"
And upload the archive wherever it's supposed to go.

@ -0,0 +1,405 @@
-------------------------------------------------
Building EFI Applications Using the GNU Toolchain
-------------------------------------------------
David Mosberger <davidm@hpl.hp.com>
23 September 1999
Copyright (c) 1999-2007 Hewlett-Packard Co.
Copyright (c) 2006-2010 Intel Co.
Last update: 04/09/2007
* Introduction
This document has two parts: the first part describes how to develop
EFI applications for IA-64,x86 and x86_64 using the GNU toolchain and the EFI
development environment contained in this directory. The second part
describes some of the more subtle aspects of how this development
environment works.
* Part 1: Developing EFI Applications
** Prerequisites:
To develop x86 and x86_64 EFI applications, the following tools are needed:
- gcc-3.0 or newer (gcc 2.7.2 is NOT sufficient!)
As of gnu-efi-3.0b, the Redhat 8.0 toolchain is known to work,
but the Redhat 9.0 toolchain is not currently supported.
- A version of "objcopy" that supports EFI applications. To
check if your version includes EFI support, issue the
command:
objcopy --help
Verify that the line "supported targets" contains the string
"efi-app-ia32" and "efi-app-x86_64" and that the "-j" option
accepts wildcards. The binutils release binutils-2.24
supports Intel64 EFI and accepts wildcard section names.
- For debugging purposes, it's useful to have a version of
"objdump" that supports EFI applications as well. This
allows inspect and disassemble EFI binaries.
To develop IA-64 EFI applications, the following tools are needed:
- A version of gcc newer than July 30th 1999 (older versions
had problems with generating position independent code).
As of gnu-efi-3.0b, gcc-3.1 is known to work well.
- A version of "objcopy" that supports EFI applications. To
check if your version includes EFI support, issue the
command:
objcopy --help
Verify that the line "supported targets" contains the string
"efi-app-ia64" and that the "-j" option accepts wildcards.
- For debugging purposes, it's useful to have a version of
"objdump" that supports EFI applications as well. This
allows inspect and disassemble EFI binaries.
** Directory Structure
This EFI development environment contains the following
subdirectories:
inc: This directory contains the EFI-related include files. The
files are taken from Intel's EFI source distribution, except
that various fixes were applied to make it compile with the
GNU toolchain.
lib: This directory contains the source code for Intel's EFI library.
Again, the files are taken from Intel's EFI source
distribution, with changes to make them compile with the GNU
toolchain.
gnuefi: This directory contains the glue necessary to convert ELF64
binaries to EFI binaries. Various runtime code bits, such as
a self-relocator are included as well. This code has been
contributed by the Hewlett-Packard Company and is distributed
under the GNU GPL.
apps: This directory contains a few simple EFI test apps.
** Setup
It is necessary to edit the Makefile in the directory containing this
README file before EFI applications can be built. Specifically, you
should verify that macros CC, AS, LD, AR, RANLIB, and OBJCOPY point to
the appropriate compiler, assembler, linker, ar, and ranlib binaries,
respectively.
If you're working in a cross-development environment, be sure to set
macro ARCH to the desired target architecture ("ia32" for x86, "x86_64" for
x86_64 and "ia64" for IA-64). For convenience, this can also be done from
the make command line (e.g., "make ARCH=ia64").
** Building
To build the sample EFI applications provided in subdirectory "apps",
simply invoke "make" in the toplevel directory (the directory
containing this README file). This should build lib/libefi.a and
gnuefi/libgnuefi.a first and then all the EFI applications such as a
apps/t6.efi.
** Running
Just copy the EFI application (e.g., apps/t6.efi) to the EFI
filesystem, boot EFI, and then select "Invoke EFI application" to run
the application you want to test. Alternatively, you can invoke the
Intel-provided "nshell" application and then invoke your test binary
via the command line interface that "nshell" provides.
** Writing Your Own EFI Application
Suppose you have your own EFI application in a file called
"apps/myefiapp.c". To get this application built by the GNU EFI build
environment, simply add "myefiapp.efi" to macro TARGETS in
apps/Makefile. Once this is done, invoke "make" in the top level
directory. This should result in EFI application apps/myefiapp.efi,
ready for execution.
The GNU EFI build environment allows to write EFI applications as
described in Intel's EFI documentation, except for two differences:
- The EFI application's entry point is always called "efi_main". The
declaration of this routine is:
EFI_STATUS efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab);
- UNICODE string literals must be written as W2U(L"Sample String")
instead of just L"Sample String". The W2U() macro is defined in
<efilib.h>. This header file also declares the function W2UCpy()
which allows to convert a wide string into a UNICODE string and
store the result in a programmer-supplied buffer.
- Calls to EFI services should be made via uefi_call_wrapper(). This
ensures appropriate parameter passing for the architecture.
* Part 2: Inner Workings
WARNING: This part contains all the gory detail of how the GNU EFI
toolchain works. Normal users do not have to worry about such
details. Reading this part incurs a definite risk of inducing severe
headaches or other maladies.
The basic idea behind the GNU EFI build environment is to use the GNU
toolchain to build a normal ELF binary that, at the end, is converted
to an EFI binary. EFI binaries are really just PE32+ binaries. PE
stands for "Portable Executable" and is the object file format
Microsoft is using on its Windows platforms. PE is basically the COFF
object file format with an MS-DOS2.0 compatible header slapped on in
front of it. The "32" in PE32+ stands for 32 bits, meaning that PE32
is a 32-bit object file format. The plus in "PE32+" indicates that
this format has been hacked to allow loading a 4GB binary anywhere in
a 64-bit address space (unlike ELF64, however, this is not a full
64-bit object file format because the entire binary cannot span more
than 4GB of address space). EFI binaries are plain PE32+ binaries
except that the "subsystem id" differs from normal Windows binaries.
There are two flavors of EFI binaries: "applications" and "drivers"
and each has there own subsystem id and are identical otherwise. At
present, the GNU EFI build environment supports the building of EFI
applications only, though it would be trivial to generate drivers, as
the only difference is the subsystem id. For more details on PE32+,
see the spec at
http://msdn.microsoft.com/library/specs/msdn_pecoff.htm.
In theory, converting a suitable ELF64 binary to PE32+ is easy and
could be accomplished with the "objcopy" utility by specifying option
--target=efi-app-ia32 (x86) or --target=efi-app-ia64 (IA-64). But
life never is that easy, so here some complicating factors:
(1) COFF sections are very different from ELF sections.
ELF binaries distinguish between program headers and sections.
The program headers describe the memory segments that need to
be loaded/initialized, whereas the sections describe what
constitutes those segments. In COFF (and therefore PE32+) no
such distinction is made. Thus, COFF sections need to be page
aligned and have a size that is a multiple of the page size
(4KB for EFI), whereas ELF allows sections at arbitrary
addresses and with arbitrary sizes.
(2) EFI binaries should be relocatable.
Since EFI binaries are executed in physical mode, EFI cannot
guarantee that a given binary can be loaded at its preferred
address. EFI does _try_ to load a binary at it's preferred
address, but if it can't do so, it will load it at another
address and then relocate the binary using the contents of the
.reloc section.
(3) On IA-64, the EFI entry point needs to point to a function
descriptor, not to the code address of the entry point.
(4) The EFI specification assumes that wide characters use UNICODE
encoding.
ANSI C does not specify the size or encoding that a wide
character uses. These choices are "implementation defined".
On most UNIX systems, the GNU toolchain uses a wchar_t that is
4 bytes in size. The encoding used for such characters is
(mostly) UCS4.
In the following sections, we address how the GNU EFI build
environment addresses each of these issues.
** (1) Accommodating COFF Sections
In order to satisfy the COFF constraint of page-sized and page-aligned
sections, the GNU EFI build environment uses the special linker script
in gnuefi/elf_$(ARCH)_efi.lds where $(ARCH) is the target architecture
("ia32" for x86, "x86_64" for x86_64 and "ia64" for IA-64).
This script is set up to create only eight COFF section, each page aligned
and page sized.These eight sections are used to group together the much
greater number of sections that are typically present in ELF object files.
Specifically:
.hash (and/or .gnu.hash)
Collects the ELF .hash info (this section _must_ be the first
section in order to build a shared object file; the section is
not actually loaded or used at runtime).
GNU binutils provides a mechanism to generate different hash info
via --hash-style=<sysv|gnu|both> option. In this case output
shared object will contain .hash section, .gnu.hash section or
both. In order to generate correct output linker script preserves
both types of hash sections.
.text
Collects all sections containing executable code.
.data
Collects read-only and read-write data, literal string data,
global offset tables, the uninitialized data segment (bss) and
various other sections containing data.
The reason read-only data is placed here instead of the in
.text is to make it possible to disassemble the .text section
without getting garbage due to read-only data. Besides, since
EFI binaries execute in physical mode, differences in page
protection do not matter.
The reason the uninitialized data is placed in this section is
that the EFI loader appears to be unable to handle sections
that are allocated but not loaded from the binary.
.dynamic, .dynsym, .rela, .rel, .reloc
These sections contains the dynamic information necessary to
self-relocate the binary (see below).
A couple of more points worth noting about the linker script:
o On IA-64, the global pointer symbol (__gp) needs to be placed such
that the _entire_ EFI binary can be addressed using the signed
22-bit offset that the "addl" instruction affords. Specifically,
this means that __gp should be placed at ImageBase + 0x200000.
Strictly speaking, only a couple of symbols need to be addressable
in this fashion, so with some care it should be possible to build
binaries much larger than 4MB. To get a list of symbols that need
to be addressable in this fashion, grep the assembly files in
directory gnuefi for the string "@gprel".
o The link address (ImageBase) of the binary is (arbitrarily) set to
zero. This could be set to something larger to increase the chance
of EFI being able to load the binary without requiring relocation.
However, a start address of 0 makes debugging a wee bit easier
(great for those of us who can add, but not subtract... ;-).
o The relocation related sections (.dynamic, .rel, .rela, .reloc)
cannot be placed inside .data because some tools in the GNU
toolchain rely on the existence of these sections.
o Some sections in the ELF binary intentionally get dropped when
building the EFI binary. Particularly noteworthy are the dynamic
relocation sections for the .plabel and .reloc sections. It would
be _wrong_ to include these sections in the EFI binary because it
would result in .reloc and .plabel being relocated twice (once by
the EFI loader and once by the self-relocator; see below for a
description of the latter). Specifically, only the sections
mentioned with the -j option in the final "objcopy" command are
retained in the EFI binary (see Make.rules).
** (2) Building Relocatable Binaries
ELF binaries are normally linked for a fixed load address and are thus
not relocatable. The only kind of ELF object that is relocatable are
shared objects ("shared libraries"). However, even those objects are
usually not completely position independent and therefore require
runtime relocation by the dynamic loader. For example, IA-64 binaries
normally require relocation of the global offset table.
The approach to building relocatable binaries in the GNU EFI build
environment is to:
(a) build an ELF shared object
(b) link it together with a self-relocator that takes care of
applying the dynamic relocations that may be present in the
ELF shared object
(c) convert the resulting image to an EFI binary
The self-relocator is of course architecture dependent. The x86
version can be found in gnuefi/reloc_ia32.c, the x86_64 version
can be found in gnuefi/reloc_x86_64.c and the IA-64 version can be
found in gnuefi/reloc_ia64.S.
The self-relocator operates as follows: the startup code invokes it
right after EFI has handed off control to the EFI binary at symbol
"_start". Upon activation, the self-relocator searches the .dynamic
section (whose starting address is given by symbol _DYNAMIC) for the
dynamic relocation information, which can be found in the DT_REL,
DT_RELSZ, and DT_RELENT entries of the dynamic table (DT_RELA,
DT_RELASZ, and DT_RELAENT in the case of rela relocations, as is the
case for IA-64). The dynamic relocation information points to the ELF
relocation table. Once this table is found, the self-relocator walks
through it, applying each relocation one by one. Since the EFI
binaries are fully resolved shared objects, only a subset of all
possible relocations need to be supported. Specifically, on x86 only
the R_386_RELATIVE relocation is needed. On IA-64, the relocations
R_IA64_DIR64LSB, R_IA64_REL64LSB, and R_IA64_FPTR64LSB are needed.
Note that the R_IA64_FPTR64LSB relocation requires access to the
dynamic symbol table. This is why the .dynsym section is included in
the EFI binary. Another complication is that this relocation requires
memory to hold the function descriptors (aka "procedure labels" or
"plabels"). Each function descriptor uses 16 bytes of memory. The
IA-64 self-relocator currently reserves a static memory area that can
hold 100 of these descriptors. If the self-relocator runs out of
space, it causes the EFI binary to fail with error code 5
(EFI_BUFFER_TOO_SMALL). When this happens, the manifest constant
MAX_FUNCTION_DESCRIPTORS in gnuefi/reloc_ia64.S should be increased
and the application recompiled. An easy way to count the number of
function descriptors required by an EFI application is to run the
command:
objdump --dynamic-reloc example.so | fgrep FPTR64 | wc -l
assuming "example" is the name of the desired EFI application.