From da94dc471a2c603c4c07daace7e6b602fad21670 Mon Sep 17 00:00:00 2001 From: Ian Seyler Date: Wed, 14 Dec 2022 02:57:46 +0000 Subject: [PATCH 1/4] Find ACPI data location from configuration table --- src/boot/uefi.asm | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/boot/uefi.asm b/src/boot/uefi.asm index b1df399..201b46b 100644 --- a/src/boot/uefi.asm +++ b/src/boot/uefi.asm @@ -113,6 +113,28 @@ EntryPoint: mov rax, [EFI_SYSTEM_TABLE] mov rax, [rax + EFI_SYSTEM_TABLE_RUNTIMESERVICES] mov [RTS], rax + mov rax, [EFI_SYSTEM_TABLE] + mov rcx, [rax + EFI_SYSTEM_TABLE_NUMBEROFENTRIES] + shl rcx, 3 ; Quick multiply by 4 + mov rax, [EFI_SYSTEM_TABLE] + mov rax, [rax + EFI_SYSTEM_TABLE_CONFIGURATION_TABLE] + mov [CONFIG], rax + + ; Find the address of the ACPI data from the UEFI configuration table + mov rsi, rax +nextentry: + dec rcx + cmp rcx, 0 + je failure + mov rbx, [ACPI_TABLE_GUID] ; First 64 bits of the GUID + lodsq + cmp rax, rbx + jne nextentry + mov rbx, [ACPI_TABLE_GUID+8] ; Seconds 64 bits of the GUID + lodsq + jne nextentry + lodsq ; Load the address of the ACPI table + mov [ACPI], rax ; Find the interface to output services via its GUID mov rcx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID ; IN EFI_GUID *Protocol @@ -325,6 +347,8 @@ EFI_SYSTEM_TABLE: dq 0 ; And this in RDX EFI_RETURN: dq 0 ; And this in RSP BS: dq 0 ; Boot services RTS: dq 0 ; Runtime services +CONFIG: dq 0 ; Config Table address +ACPI: dq 0 ; ACPI table address OUTPUT: dq 0 ; Output services VIDEO: dq 0 ; Video services FB: dq 0 ; Frame buffer base address @@ -336,15 +360,20 @@ memmapkey: dq 0 memmapdescsize: dq 0 memmapdescver: dq 0 +ACPI_TABLE_GUID: +dd 0xeb9d2d30 +dw 0x2d88, 0x11d3 +db 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID: dd 0x387477c2 dw 0x69c7,0x11d2 -db 0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b +db 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID: dd 0x9042a9de dw 0x23dc, 0x4a38 -db 0x96,0xfb,0x7a,0xde,0xd0,0x80,0x51,0x6a +db 0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a hextable: db '0123456789ABCDEF' msg_start: dw u('UEFI '), 0 @@ -392,6 +421,8 @@ EFI_NOT_FOUND equ 14 EFI_SYSTEM_TABLE_RUNTIMESERVICES equ 88 EFI_SYSTEM_TABLE_BOOTSERVICES equ 96 +EFI_SYSTEM_TABLE_NUMBEROFENTRIES equ 104 +EFI_SYSTEM_TABLE_CONFIGURATION_TABLE equ 112 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_RESET equ 0 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_OUTPUTSTRING equ 8 From d7846ff23fa0d1f804e987f1de11842ab083714b Mon Sep 17 00:00:00 2001 From: Ian Seyler Date: Wed, 14 Dec 2022 17:58:51 +0000 Subject: [PATCH 2/4] Set a Boot Mode for Pure64 --- src/boot/uefi.asm | 4 ++++ src/init/acpi.asm | 10 ++++++++++ src/pure64.asm | 4 ++++ src/sysvar.asm | 1 + 4 files changed, 19 insertions(+) diff --git a/src/boot/uefi.asm b/src/boot/uefi.asm index 201b46b..df6b82b 100644 --- a/src/boot/uefi.asm +++ b/src/boot/uefi.asm @@ -240,6 +240,10 @@ get_memmap: cmp ax, 0x3436 ; Match against the Pure64 binary jne sig_fail + ; Signal to Pure64 that it was booted via UEFI + mov al, 'U' + mov [0x8005], al + ; Exit Boot services as EFI is no longer needed mov rcx, [EFI_IMAGE_HANDLE] ; IN EFI_HANDLE ImageHandle mov rdx, [memmapkey] ; IN UINTN MapKey diff --git a/src/init/acpi.asm b/src/init/acpi.asm index 64ca813..bddba0a 100644 --- a/src/init/acpi.asm +++ b/src/init/acpi.asm @@ -7,6 +7,9 @@ init_acpi: + mov al, [BootMode] + cmp al, 'U' + je foundACPIfromUEFI mov esi, 0x000E0000 ; Start looking for the Root System Description Pointer Structure mov rbx, 'RSD PTR ' ; This in the Signature for the ACPI Structure Table (0x2052545020445352) searchingforACPI: @@ -17,6 +20,13 @@ searchingforACPI: jge noACPI ; ACPI tables couldn't be found, Fail. jmp searchingforACPI +foundACPIfromUEFI: + mov rsi, [0x400830] ; TODO This should be passed properly + mov rbx, 'RSD PTR ' ; This in the Signature for the ACPI Structure Table (0x2052545020445352) + lodsq + cmp rax, rbx + jne noACPI + foundACPI: ; Found a Pointer Structure, verify the checksum push rsi xor ebx, ebx diff --git a/src/pure64.asm b/src/pure64.asm index c4dca09..0a9733a 100644 --- a/src/pure64.asm +++ b/src/pure64.asm @@ -257,6 +257,10 @@ clearcs64: lgdt [GDTR64] ; Reload the GDT +; Save the Boot Mode (it will be 'U' if started via UEFI) + mov al, [0x8005] + mov [BootMode], al ; Save the byte as a Boot Mode flag + ; Patch Pure64 AP code ; The AP's will be told to start execution at 0x8000 mov edi, start ; We need to remove the BSP Jump call to get the AP's mov eax, 0x90909090 ; to fall through to the AP Init code diff --git a/src/sysvar.asm b/src/sysvar.asm index f94e264..45432ec 100644 --- a/src/sysvar.asm +++ b/src/sysvar.asm @@ -37,6 +37,7 @@ cpu_detected: equ SystemVariables + 260 ; DB - Starting at offset 384, increments by 1 os_IOAPICCount: equ SystemVariables + 384 +BootMode: equ SystemVariables + 385 align 16 From e269e3e167962c7b8e91670f6eaa4f546ce0765a Mon Sep 17 00:00:00 2001 From: Ian Seyler Date: Wed, 14 Dec 2022 19:39:58 +0000 Subject: [PATCH 3/4] Kernel boots correctly via UEFI --- src/boot/uefi.asm | 15 ++++++++++++++- src/pure64.asm | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/boot/uefi.asm b/src/boot/uefi.asm index df6b82b..121b802 100644 --- a/src/boot/uefi.asm +++ b/src/boot/uefi.asm @@ -234,7 +234,7 @@ get_memmap: ; Copy Pure64 to the correct memory address mov rsi, PAYLOAD mov rdi, 0x8000 - mov rcx, 16384 ; Copy 16KB + mov rcx, 61440 ; Copy 60KB rep movsb mov ax, [0x8006] cmp ax, 0x3436 ; Match against the Pure64 binary @@ -244,6 +244,19 @@ get_memmap: mov al, 'U' mov [0x8005], al + ; Save video values to the area of memory where Pure64 expects them + mov rdi, 0x00005C00 + 40 ; VBEModeInfoBlock.PhysBasePtr + mov rax, [FB] + stosd + mov rdi, 0x00005C00 + 18 ; VBEModeInfoBlock.XResolution & .YResolution + mov rax, [HR] + stosw + mov rax, [VR] + stosw + mov rdi, 0x00005C00 + 25 ; VBEModeInfoBlock.BitsPerPixel + mov rax, 32 + stosb + ; Exit Boot services as EFI is no longer needed mov rcx, [EFI_IMAGE_HANDLE] ; IN EFI_HANDLE ImageHandle mov rdx, [memmapkey] ; IN UINTN MapKey diff --git a/src/pure64.asm b/src/pure64.asm index 0a9733a..626974a 100644 --- a/src/pure64.asm +++ b/src/pure64.asm @@ -267,6 +267,9 @@ clearcs64: stosd stosd ; Write 8 bytes in total to overwrite the 'far jump' and marker + mov al, [BootMode] + cmp al, 'U' + je uefi_memmap ; Process the E820 memory map to find all possible 2MiB pages that are free to use ; Build a map at 0x400000 xor ecx, ecx @@ -308,6 +311,16 @@ end820: shl ebx, 1 mov dword [mem_amount], ebx shr ebx, 1 + jmp memmap_end + +uefi_memmap: ; TODO fix this as it is a terrible hack + mov rdi, 0x400000 + mov al, 1 + mov rcx, 32 + rep stosb + mov ebx, 64 + mov dword [mem_amount], ebx +memmap_end: ; Create the high memory map mov rcx, rbx From 47f4f0c84feee591c5e32cc6aff1c70887141c1f Mon Sep 17 00:00:00 2001 From: Ian Seyler Date: Thu, 15 Dec 2022 19:22:56 +0000 Subject: [PATCH 4/4] Cleanup --- src/boot/uefi.asm | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/boot/uefi.asm b/src/boot/uefi.asm index 121b802..dd15943 100644 --- a/src/boot/uefi.asm +++ b/src/boot/uefi.asm @@ -100,7 +100,7 @@ EntryPoint: mov [EFI_IMAGE_HANDLE], rcx mov [EFI_SYSTEM_TABLE], rdx mov [EFI_RETURN], rsp - sub rsp, 6*8+8 ; Fix stack +; sub rsp, 6*8+8 ; Fix stack ; When calling an EFI function the caller must pass the first 4 integer values in registers ; via RCX, RDX, R8, and R9 @@ -125,16 +125,17 @@ EntryPoint: nextentry: dec rcx cmp rcx, 0 - je failure - mov rbx, [ACPI_TABLE_GUID] ; First 64 bits of the GUID + je failure ; Bail out as no ACPI data was detected + mov rbx, [ACPI_TABLE_GUID] ; First 64 bits of the ACPI GUID lodsq - cmp rax, rbx + cmp rax, rbx ; Compare the table data to the expected GUID data jne nextentry - mov rbx, [ACPI_TABLE_GUID+8] ; Seconds 64 bits of the GUID + mov rbx, [ACPI_TABLE_GUID+8] ; Second 64 bits of the ACPI GUID lodsq + cmp rax, rbx ; Compare the table data to the expected GUID data jne nextentry lodsq ; Load the address of the ACPI table - mov [ACPI], rax + mov [ACPI], rax ; Save the address ; Find the interface to output services via its GUID mov rcx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID ; IN EFI_GUID *Protocol @@ -260,8 +261,8 @@ get_memmap: ; Exit Boot services as EFI is no longer needed mov rcx, [EFI_IMAGE_HANDLE] ; IN EFI_HANDLE ImageHandle mov rdx, [memmapkey] ; IN UINTN MapKey - mov rbx, [BS] - call [rbx + EFI_BOOT_SERVICES_EXITBOOTSERVICES] + mov rax, [BS] + call [rax + EFI_BOOT_SERVICES_EXITBOOTSERVICES] cmp rax, EFI_SUCCESS jne failure