From 37fafd9431983f5cb596531e45bdf31ed8be1c34 Mon Sep 17 00:00:00 2001 From: pyao12 Date: Sat, 9 May 2026 20:35:21 +0800 Subject: [PATCH] [feat] Use gnu-efi --- .gitignore | 3 ++- .gitmodules | 3 +++ Makefile | 14 +++++++------- boot.c | 27 +++++++-------------------- gnu-efi | 1 + 5 files changed, 20 insertions(+), 28 deletions(-) create mode 100644 .gitmodules create mode 160000 gnu-efi diff --git a/.gitignore b/.gitignore index 5620f0e..21f8aea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/ vdir/ -.vscode/ \ No newline at end of file +.vscode/ +serial.log \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d93643e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "gnu-efi"] + path = gnu-efi + url = https://github.com/ncroxon/gnu-efi.git diff --git a/Makefile b/Makefile index b9113ba..914fc47 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,8 @@ -MINGW_GCC = x86_64-w64-mingw32-gcc -QEMU = qemu-system-x86_64 -MINGW_GCC_FLAGS = -Wall -Wextra -e efi_main -nostdinc -nostdlib -fno-builtin -Wl,--subsystem,10 - all: mkdir -p build - x86_64-w64-mingw32-gcc $(MINGW_GCC_FLAGS) -o build/BOOTX64.EFI boot.c + gcc -Ignu-efi/inc -fpic -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -c boot.c -o build/boot.o + ld -shared -Bsymbolic -Lgnu-efi/x86_64/lib -Lgnu-efi/x86_64/gnuefi -Tgnu-efi/gnuefi/elf_x86_64_efi.lds gnu-efi/x86_64/gnuefi/crt0-efi-x86_64.o build/boot.o -o build/boot.so -lgnuefi -lefi + objcopy -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --output-target efi-app-x86_64 --subsystem=10 build/boot.so build/BOOTX64.EFI vdir: all mkdir -p vdir @@ -12,7 +10,9 @@ vdir: all cp build/BOOTX64.EFI vdir/EFI/BOOT run: vdir - $(QEMU) -bios /usr/share/ovmf/OVMF.fd -net none -drive file=fat:rw:vdir,index=0,format=vvfat -serial stdio + qemu-system-x86_64 -bios /usr/share/ovmf/OVMF.fd -net none -drive file=fat:rw:vdir,index=0,format=vvfat -serial file:serial.log clean: - rm -rf build vdir \ No newline at end of file + rm -rf build vdir + +.PHONY: all vdir run clean \ No newline at end of file diff --git a/boot.c b/boot.c index 3b253fb..09d6972 100644 --- a/boot.c +++ b/boot.c @@ -1,22 +1,9 @@ -#define ull unsigned long long +#include +#include -struct EFI_SYSTEM_TABLE { - char _buf[60]; - struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL { - ull _buf; - ull (*OutputString)( - struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, - unsigned short *String); - ull _buf2[4]; - ull (*ClearScreen)( - struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This); - } *ConOut; -}; - -void efi_main(void *ImageHandle __attribute__ ((unused)), struct EFI_SYSTEM_TABLE *SystemTable) -{ - SystemTable->ConOut->ClearScreen(SystemTable->ConOut); - SystemTable->ConOut->OutputString(SystemTable->ConOut, L"Hello UEFI!\n"); +EFI_STATUS EFIAPI efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { + InitializeLib(ImageHandle, SystemTable); + Print(L"Hello, world!\n"); while (1); -} - + return EFI_SUCCESS; +} \ No newline at end of file diff --git a/gnu-efi b/gnu-efi new file mode 160000 index 0000000..8a45cdb --- /dev/null +++ b/gnu-efi @@ -0,0 +1 @@ +Subproject commit 8a45cdb08cfb9653fc5a2fb659487d04a8af4f45