Compare commits

..

4 Commits

8 changed files with 72 additions and 152 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
*.o *.o
*.d
*.out
*.efi *.efi
*.vhd *.vhd
*.sdf *.sdf
@ -16,3 +18,5 @@ image/
.vs/ .vs/
x64/ x64/
*.vbs *.vbs
*.list
*.map

View File

@ -11,25 +11,21 @@ cd ./x64
rm objects.list rm objects.list
HeaderFiles=-I$CurrentDir/inc/ \ HeaderFiles="-I$CurrentDir/inc/ -I$CurrentDir/gnu-efi/inc -I$CurrentDir/gnu-efi/inc/x86_64 -I$CurrentDir/gnu-efi/inc/protocol -I$CurrentDir/gnu-efi/lib"
-I$CurrentDir/gnu-efi/inc \
-I$CurrentDir/gnu-efi/inc/x86_64 \
-I$CurrentDir/gnu-efi/inc/protocol \
-I$CurrentDir/gnu-efi/lib
set -v set -v
for f in $CurrentDir/src/*.c; do for f in $CurrentDir/src/*.c; do
echo "$GCC_NAME-gcc" -ffreestanding -fpic -fshort-wchar -fno-stack-protector -fno-stack-check -fno-strict-aliasing -fno-merge-all-constants -m64 -mno-redzone -DGNU_USE_MS_ABI -maccumulate-outgoing-args --std=c11 $HeaderFiles -Og -g3 -Wall -Wextra -Wdouble-promotion -fmessage-length=0 -c -MMD -MP -Wa,-adhln="${f%.*}.out" -MF"${f%.*}.d" -MT"${f%.*}.o" -o "${f%.*}.o" "$f" echo "$GCC_NAME-gcc" -ffreestanding -fpic -fshort-wchar -fno-stack-protector -fno-stack-check -fno-strict-aliasing -fno-merge-all-constants -m64 -mno-red-zone -DGNU_USE_MS_ABI -maccumulate-outgoing-args --std=c11 $HeaderFiles -Og -g3 -Wall -Wextra -Wdouble-promotion -fmessage-length=0 -c -MMD -MP -Wa,-adhln="${f%.*}.out" -MT"${f%.*}.o" -o "${f%.*}.o" "$f"
"$GCC_NAME-gcc" -ffreestanding -fpic -fshort-wchar -fno-stack-protector -fno-stack-check -fno-strict-aliasing -fno-merge-all-constants -m64 -mno-redzone -DGNU_USE_MS_ABI -maccumulate-outgoing-args --std=c11 $HeaderFiles -Og -g3 -Wall -Wextra -Wdouble-promotion -fmessage-length=0 -c -MMD -MP -Wa,-adhln="${f%.*}.out" -MF"${f%.*}.d" -MT"${f%.*}.o" -o "${f%.*}.o" "$f" "$GCC_NAME-gcc" -ffreestanding -fpic -fshort-wchar -fno-stack-protector -fno-stack-check -fno-strict-aliasing -fno-merge-all-constants -m64 -mno-red-zone -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args --std=c11 $HeaderFiles -Og -g3 -Wall -Wextra -Wdouble-promotion -fmessage-length=0 -c -MMD -MP -Wa,-adhln="${f%.*}.out" -MT"${f%.*}.o" -o "${f%.*}.o" "$f"
done done
set +v set +v
set -v set -v
while read f; do while read f; do
echo "$GCC_NAME-gcc" -ffreestanding -fpic -fshort-wchar -fno-stack-protector -fno-stack-check -fno-strict-aliasing -fno-merge-all-constants -m64 -mno-red-zone -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args --std=c11 $HeaderFILES -Og -g3 -Wall -Wextra -Wdouble-promotion -fmessage-length=0 -c -MMD -MP -Wa,-adhln="${f%.*}.out" -MF"${f%.*}.d" -MT"${f%.*}.o" -o "${f%.*}.o" "$f" echo "$GCC_NAME-gcc" -ffreestanding -fpic -fshort-wchar -fno-stack-protector -fno-stack-check -fno-strict-aliasing -fno-merge-all-constants -m64 -mno-red-zone -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args --std=c11 $HeaderFiles -Og -g3 -Wall -Wextra -Wdouble-promotion -fmessage-length=0 -MMD -MP -Wa,-adhln="$CurrentDir${f%.*}.out" -MT"$CurrentDir${f%.*}.o" -o "$CurrentDir${f%.*}.o" -c "$CurrentDir$f"
"$GCC_NAME-gcc" -ffreestanding -fpic -fshort-wchar -fno-stack-protector -fno-stack-check -fno-strict-aliasing -fno-merge-all-constants -m64 -mno-red-zone -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args --std=c11 $HeaderFILES -Og -g3 -Wall -Wextra -Wdouble-promotion -fmessage-length=0 -c -MMD -MP -Wa,-adhln="${f%.*}.out" -MF"${f%.*}.d" -MT"${f%.*}.o" -o "${f%.*}.o" "$f" "$GCC_NAME-gcc" -ffreestanding -fpic -fshort-wchar -fno-stack-protector -fno-stack-check -fno-strict-aliasing -fno-merge-all-constants -m64 -mno-red-zone -DGNU_EFI_USE_MS_ABI -maccumulate-outgoing-args --std=c11 $HeaderFiles -Og -g3 -Wall -Wextra -Wdouble-promotion -fmessage-length=0 -MMD -MP -Wa,-adhln="$CurrentDir${f%.*}.out" -MT"$CurrentDir${f%.*}.o" -o "$CurrentDir${f%.*}.o" -c "$CurrentDir$f"
done < $CurrentDir/c_files.txt done < $CurrentDir/c_files.txt
set +v set +v
@ -43,8 +39,11 @@ for f in $CurrentDir/src/*.o; do
done done
set -v set -v
"$GCC_NAME-ld" -T$Linker -nostdlib --warn-common --no-undefined -znocombreloc -s -shared -Bsymbolic -Map=output.map -o "program.so" @"objects.list" "ld" -T$Linker -nostdlib --warn-common --no-undefined -znocombreloc -shared -Bsymbolic -Map=output.map -o "program.so" @"objects.list"
set +v set +v
rm image/efi/boot/BOOTx64.efi rm image/efi/boot/BOOTx64.efi
objcopy -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .rel* -j .reloc --target=efi-app-x86_64 "program.so" "image/efi/boot/BOOTx64.efi" objcopy -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .rel* -j .reloc --target=efi-app-x86_64 "program.so" "image/efi/boot/BOOTx64.efi"
objcopy --only-keep-debug image/efi/boot/BOOTx64.efi BOOTX64.debug
objcopy --add-gnu-debuglink=BOOTX64.debug image/efi/boot/BOOTx64.efi
read -n 1

View File

@ -1,3 +1,7 @@
src/main.c
src/gfx.c
src/bootloader.c
src/memory.c
gnu-efi/lib/misc.c gnu-efi/lib/misc.c
gnu-efi/lib/print.c gnu-efi/lib/print.c
gnu-efi/lib/smbios.c gnu-efi/lib/smbios.c

45
compile.py Normal file
View File

@ -0,0 +1,45 @@
import os
import subprocess
CurrentPath = os.getcwd()
print(CurrentPath)
# Here goes the absolute path to your x86_64-mingw-w64 folder.
# There should be a \bin\gcc.exe relative to that folder.
GCC_FOLDER = "C:\\Users\Gem\Desktop\comp\mingw64"
if os.path.exists('objects.list'):
os.remove("objects.list")
HeaderFiles="-Iinc/ -Ignu-efi/inc -Ignu-efi/inc/x86_64 -Ignu-efi/inc/protocol -Ignu-efi/lib"
LinkScript = "gnu-efi/gnuefi/elf_x86_64_efi.lds"
OBJECTS = ""
with open('c_files.txt') as file:
line = file.readline().strip()
while line:
line = line[:-2]
print(f'{HeaderFiles}')
if line == "gnu-efi/lib/x86_64/setjmp" or line == "gnu-efi/lib/x86_64/efi_stub":
print(f'Assembling file {line}.s')
subprocess.run([f'{GCC_FOLDER}/bin/as.exe', '-64', '-I/inc/', '-g', '-o', f'{line}.o', f'{line}.s'], shell=True)
else:
print(f'Compiling file {line}.c')
subprocess.run([f'{GCC_FOLDER}/bin/gcc.exe', '-ffreestanding', '-fpic', '-fshort-wchar', '-fno-stack-protector', '-fno-stack-check', '-fno-strict-aliasing', '-fno-merge-all-constants', '-m64', '-mno-red-zone', '-DGNU_EFI_USE_MS_ABI', '-maccumulate-outgoing-args', '--std=c11', '-Iinc/', '-Ignu-efi/inc', '-Ignu-efi/inc/x86_64', '-Ignu-efi/inc/protocol', '-Ignu-efi/lib', '-Og', '-g3', '-Wall', '-Wextra', '-Wdouble-promotion', '-Wpedantic', '-fmessage-length=0', '-c', '-MMD', '-MP', f'-Wa,-adghlmns={line}.out', f'-MT{line}.o', '-o', f'{line}.o', f'{line}.c'], shell=True)
OBJECTS = line + ".o "
os.path.normpath(OBJECTS)
with open('objects.list', 'a') as objectsfile:
ofile = OBJECTS.replace('\\', '/')
objectsfile.write(ofile + "\n")
line = file.readline().strip()
subprocess.run([f'{GCC_FOLDER}/bin/ld.exe', f'-T{LinkScript}', "-nostdlib", "--warn-common", "--no-undefined", "-shared", "--subsystem", '10', "-Bsymbolic", "--gc-sections", "-Map=output.map" , "-o Syncboot.efi", "@objects.list"])
subprocess.run(['objcopy', '-j .text', '-j .sdata', '-j .data', '-j .dynamic', '-j .dynsym', '-j .rel', '-j .rela', '-j .rel.*', '-j .rel*', '-j .reloc', '--target=efi-app-x86_64', 'Syncboot.efi', 'image/efi/boot/BOOTX64.efi'])
subprocess.run(['objcopy', '--only-keep-debug', 'image/efi/boot/BOOTX64.efi', 'BOOTX64.debug'])
subprocess.run(['--add-gnu-debuglink=BOOTX64.debug', 'image/efi/boot/BOOTX64.efi'])

141
debug.vbs
View File

@ -1,141 +0,0 @@
' 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 -vga std -display sdl -serial file:qemu.log -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)

View File

@ -2,6 +2,7 @@
#include <efi.h> #include <efi.h>
#include <efilib.h> #include <efilib.h>
#include <stddef.h>
#include <pe.h> #include <pe.h>
#include <ELF.h> #include <ELF.h>

BIN
program.so.debug Normal file

Binary file not shown.

View File

@ -44,6 +44,14 @@ EFI_STATUS LoadKernel(EFI_HANDLE ImageHandle, GFX_INFO* Graphics, EFI_CONFIGURAT
return KernelStatus; return KernelStatus;
} }
Print(L"Syncboot loaded at 0x%llx\n", LoadedImage->ImageBase);
AwaitKey("\0");
int Loop = 1;
while (Loop) {
__asm volatile("pause");
}
/* Get the path to the device */ /* Get the path to the device */
CHAR16* ESPRootTemp = DevicePathToStr(DevicePathFromHandle(LoadedImage->DeviceHandle)); CHAR16* ESPRootTemp = DevicePathToStr(DevicePathFromHandle(LoadedImage->DeviceHandle));
size_t ESPRootSize = StrSize(ESPRootTemp); size_t ESPRootSize = StrSize(ESPRootTemp);