Skip to content

Instantly share code, notes, and snippets.

@neri neri/Makefile
Last active Nov 3, 2019

Embed
What would you like to do?
A Experimental of Minimal EFI Hello World
;; A Experimental of Minimal EFI Hello World
;; LICENSE: Public Domain
%define u(x) __utf16__(x)
%define IMAGE_BASE 0x140000000
%define RVA_TEXT 0x1000
%define ALIGN 0x10
%define VSIZE 0x1000
%define RVA0 (_TEXT - _BEGIN) - (IMAGE_BASE)
[bits 64]
[section .text]
%if 1
_BEGIN:
db 'MZ' ; e_magic - Mark Zbikowski
times 0x3C - 2 db 0
dd (_PE - _BEGIN) ; e_lfanew
_PE:
db 'PE', 0, 0
dw 0x8664 ; magic
dw 1 ; n_sections
dd 0, 0, 0 ; obsoleted
dw (_SECTION_HEADER - _OPTIONAL_HEADER) ; size_of_optional_header
dw 0x0022 ; coff_flags - IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LARGE_ADDRESS_AWARE
_OPTIONAL_HEADER:
dw 0x020B ; PE32+
db 14, 0 ; linker version
dd (_END - _BEGIN), 0, 0 ; size_of_code, size_of_data, size_of_bss
dd EfiMain - RVA0 ; entry_point
dd _TEXT - RVA0 ; base_of_code
dq IMAGE_BASE ; image_base
dd 0x1000, ALIGN ; align
dw 6, 0, 0, 0, 6, 0 ; versions
dd 0 ; reserved
dd VSIZE + RVA_TEXT, _END_HEADER - _BEGIN ; size_of_image, size_of_header
dd 0 ; checksum
dw 0x0A ; subsystem - IMAGE_SUBSYSTEM_EFI_APPLICATION
dw 0x8160 ; dll_flags - _HIGH_ENTROPY_VA, _DYNAMIC_BASE, _NX_COMPAT, _TERMINAL_SERVER_AWARE
dq 0x100000, 0x10000, 0x100000, 0x10000 ; size_of_stack_reserve, size_of_stack_commit, size_of_heap_reserve, size_of_heap_commit
dd 0 ; loader_flags - ?
dd 6 ; NumberOfRvaAndSizes - 16 formal, 6 OVMF's minimal
times 6 dd 0, 0 ; dummy
_SECTION_HEADER:
db ".text", 0, 0, 0
dd (_END - _TEXT), RVA_TEXT ; vsize, rva
dd (_END - _TEXT), _TEXT - _BEGIN ; filesize, offset
dd 0, 0, 0 ; obsoleted
dd 0x60000020 ; flags - IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ
%endif
alignb ALIGN
_END_HEADER:
org IMAGE_BASE + RVA_TEXT
_TEXT:
global EfiMain
EfiMain:
sub rsp, byte 0x28
mov rcx, [rdx + 0x40] ; EFI_SYSTEM_TABLE->ConOut
lea rdx, [rel hello_string]
call [rcx + 0x08] ; EFI_SIMPLE_OUTPUT_PROTOCOL->OutputString
xor eax, eax
add rsp, byte 0x28
ret
hello_string:
dw u("Hello, world!"), 13, 10, 0
_END:
hello.efi: hello.asm
nasm -f bin hello.asm -o hello.efi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.