From 4460661bfd76891d62ff51803e737b6bf273a139 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Sat, 6 Nov 2021 23:40:50 +0800 Subject: [PATCH] main: refactor to keep the EFI headers from polluting the main namespace --- efimain.h | 19 ++++++++++++++++ graphics/graphics.c | 1 + graphics/graphics.h | 11 ++++++---- main.c | 2 +- main.h | 20 +++-------------- memory/memory.c | 1 + memory/memory.h | 10 +-------- memory/memory_internal.h | 11 ++++++++++ memory/paging_init.c | 3 ++- memory/paging_internal.h | 38 -------------------------------- memory/paging_internal_efi.h | 42 ++++++++++++++++++++++++++++++++++++ runtime/stdio.c | 14 +----------- runtime/stdio.h | 2 -- 13 files changed, 89 insertions(+), 85 deletions(-) create mode 100644 efimain.h create mode 100644 memory/memory_internal.h create mode 100644 memory/paging_internal_efi.h diff --git a/efimain.h b/efimain.h new file mode 100644 index 0000000..73c8429 --- /dev/null +++ b/efimain.h @@ -0,0 +1,19 @@ +#pragma once + +#include "stddef.h" + +#ifdef __cplusplus +extern "C" { +#include +} +#else +#include +#endif + + +extern EFI_HANDLE efiImageHandle; +extern EFI_SYSTEM_TABLE * efiSystemTable; +extern EFI_BOOT_SERVICES *efiBootServices; + +extern SIMPLE_TEXT_OUTPUT_INTERFACE *efiStdout, *efiStderr; +extern SIMPLE_INPUT_INTERFACE * efiStdin; diff --git a/graphics/graphics.c b/graphics/graphics.c index 25986a2..df34057 100644 --- a/graphics/graphics.c +++ b/graphics/graphics.c @@ -9,6 +9,7 @@ #include "../runtime/stdio.h" #include "../util/minmax.h" #include "../util/stack.h" +#include "../efimain.h" #include #include diff --git a/graphics/graphics.h b/graphics/graphics.h index 0c63e99..953e00c 100644 --- a/graphics/graphics.h +++ b/graphics/graphics.h @@ -1,7 +1,6 @@ #pragma once #include "../main.h" -#include "efiprot.h" #include "color.h" #include "xcursor/xcursor.h" @@ -14,12 +13,16 @@ extern "C" { #define HELOS_GRAPHICS_TARGET_MODE_WIDTH 1600 #define HELOS_GRAPHICS_TARGET_MODE_HEIGHT 900 +typedef enum { + graphics_PixelFormat_RGBA_8bit, + graphics_PixelFormat_BGRA_8bit, +} graphics_PixelFormat; typedef struct { int Width, Height; int PixelsPerLine; - EFI_GRAPHICS_PIXEL_FORMAT PixelFormat; + graphics_PixelFormat PixelFormat; } HelosGraphics_Mode; @@ -48,9 +51,9 @@ void graphics_SetPixel_BGR(int posX, int posY, const HelosGraphics_Color *color) // graphics_SetPixel calls one of SetPixel_RGB/BGR according to the framebuffer format. static inline void graphics_SetPixel(int posX, int posY, const HelosGraphics_Color *color) { - if (graphics_SystemVideoMode.PixelFormat == PixelBlueGreenRedReserved8BitPerColor) + if (graphics_SystemVideoMode.PixelFormat == graphics_PixelFormat_BGRA_8bit) graphics_SetPixel_BGR(posX, posY, color); - else if (graphics_SystemVideoMode.PixelFormat == PixelRedGreenBlueReserved8BitPerColor) + else if (graphics_SystemVideoMode.PixelFormat == graphics_PixelFormat_RGBA_8bit) graphics_SetPixel_RGB(posX, posY, color); } diff --git a/main.c b/main.c index 50b7208..932cb78 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include "main.h" +#include "efimain.h" #include #include "runtime/panic_assert.h" @@ -28,7 +29,6 @@ FASTCALL_ABI EFI_STATUS efiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *System io_WriteConsole(" Welcome to " PROJECT_NAME " !\r\n\r\n"); graphics_Init(); - io_PauseForKeystroke(); graphics_ClearBuffer(&HelosGraphics_Color_Black); graphics_FillPixel(0, 0, 20, 20, &HelosGraphics_Color_Black); diff --git a/main.h b/main.h index f981abe..75d9274 100644 --- a/main.h +++ b/main.h @@ -1,14 +1,7 @@ #pragma once -#include "stddef.h" - -#ifdef __cplusplus -extern "C" { -#include -} -#else -#include -#endif +#include +#include #define PROJECT_NAME "Helos1" #define PROJECT_NAME_LONG L"Helos1" @@ -33,15 +26,8 @@ extern "C" { : "=r"(var)) -extern EFI_HANDLE efiImageHandle; -extern EFI_SYSTEM_TABLE * efiSystemTable; -extern EFI_BOOT_SERVICES *efiBootServices; - -extern SIMPLE_TEXT_OUTPUT_INTERFACE *efiStdout, *efiStderr; -extern SIMPLE_INPUT_INTERFACE * efiStdin; - #define HELOS_BUFFER_SIZE 16384 -extern char Buffer[HELOS_BUFFER_SIZE] ALIGN(4096); // general-purpose buffer, user saved (volatile), not used in interrupt handlers +extern char Buffer[HELOS_BUFFER_SIZE] ALIGNED(4096); // general-purpose buffer, user saved (volatile), not used in interrupt handlers extern const char link_TextStart[], link_TextEnd[]; diff --git a/memory/memory.c b/memory/memory.c index e822b6c..6e2a6f5 100644 --- a/memory/memory.c +++ b/memory/memory.c @@ -1,5 +1,6 @@ #include "../main.h" +#include "../efimain.h" #include "memory.h" #include "../runtime/stdio.h" #include "../extlib/dlmalloc/malloc-2.8.6.h" diff --git a/memory/memory.h b/memory/memory.h index a4df4c6..ca45893 100644 --- a/memory/memory.h +++ b/memory/memory.h @@ -2,6 +2,7 @@ #include "../main.h" #include +#include #ifdef __cplusplus @@ -43,9 +44,6 @@ static inline uint64_t paging_MapFunction(void *func) { } -// efiMallocTyped allocates from EFI_BOOT_SERVICES.AllocatePool. -void *efiMallocTyped(size_t size, EFI_MEMORY_TYPE type); - // efiMallocTyped allocates from EFI_BOOT_SERVICES.AllocatePool // with a memory type of EfiLoaderData. void *efiMalloc(size_t size); @@ -61,12 +59,6 @@ void *kMalloc(size_t size); void kFree(void *data); -extern EFI_MEMORY_DESCRIPTOR *efiMemoryMap; -extern UINTN efiMemoryMapSize; -extern UINTN efiMemoryMapKey; -extern UINTN efiDescriptorSize; -extern UINT32 efiDescriptorVertion; - // runtime_InitPaging initializes paging and kMalloc/kFree allocator. // This function calls ExitBootServices()!!! which is great // Furthermore, it sets up a new stack, calls kMain() and does not return. diff --git a/memory/memory_internal.h b/memory/memory_internal.h new file mode 100644 index 0000000..5477e19 --- /dev/null +++ b/memory/memory_internal.h @@ -0,0 +1,11 @@ +#pragma once + +#include "memory.h" +#include "../efimain.h" + + +extern EFI_MEMORY_DESCRIPTOR *efiMemoryMap; +extern UINTN efiMemoryMapSize; +extern UINTN efiMemoryMapKey; +extern UINTN efiDescriptorSize; +extern UINT32 efiDescriptorVertion; diff --git a/memory/paging_init.c b/memory/paging_init.c index 831c33a..40dd5f0 100644 --- a/memory/paging_init.c +++ b/memory/paging_init.c @@ -1,5 +1,6 @@ #include "../main.h" +#include "../efimain.h" #include "memory.h" #include "../runtime/panic_assert.h" #include "../runtime/stdio.h" @@ -13,6 +14,7 @@ void execformat_pe_ReadSystemHeader(execformat_pe_PortableExecutable *pe); #include #include "paging_internal.h" +#include "paging_internal_efi.h" EFI_MEMORY_DESCRIPTOR *efiMemoryMap; @@ -111,7 +113,6 @@ void runtime_InitPaging() { paging_UsableBytes / 1024.0 / 1024.0, paging_UsableBytes / 1024.0 / 1024.0 / 1024.0); - io_PauseForKeystroke(); assert(paging_LoaderCodeAddress && "EfiLoaderCode mapping not found"); diff --git a/memory/paging_internal.h b/memory/paging_internal.h index bdc3f07..0a1849c 100644 --- a/memory/paging_internal.h +++ b/memory/paging_internal.h @@ -9,13 +9,6 @@ extern "C" { #endif -// defined in paging_init.c -extern EFI_MEMORY_DESCRIPTOR *efiMemoryMap; -extern UINTN efiMemoryMapSize; -extern UINTN efiMemoryMapKey; -extern UINTN efiDescriptorSize; -extern UINT32 efiDescriptorVertion; - // defined in paging_init.c extern uint64_t paging_TotalBytes, paging_UsableBytes; extern bool paging_SupportExecuteDisable; @@ -67,37 +60,6 @@ FASTCALL_ABI void paging_modeswitch_4LevelPaging(void *pml4, int pcid); FASTCALL_ABI void paging_modeswitch_4LevelPagingNX(void *pml4, int pcid); // with setting the Execute-Disalbe bit FASTCALL_ABI void paging_modeswitch_Table(void *pml, int pcid); -static inline const char * - memoryTypeName(EFI_MEMORY_TYPE type) { -#define CASE(c) \ - case c: \ - return #c; - switch (type) { - CASE(EfiReservedMemoryType) - CASE(EfiLoaderCode) - CASE(EfiLoaderData) - CASE(EfiBootServicesCode) - CASE(EfiBootServicesData) - CASE(EfiRuntimeServicesCode) - CASE(EfiRuntimeServicesData) - CASE(EfiConventionalMemory) - CASE(EfiUnusableMemory) - CASE(EfiACPIReclaimMemory) - CASE(EfiACPIMemoryNVS) - CASE(EfiMemoryMappedIO) - CASE(EfiMemoryMappedIOPortSpace) - CASE(EfiPalCode) - case EfiMaxMemoryType: - return "EfiPersistentMemory"; - } - return "(unknown)"; -#undef CASE -} - -#ifndef NEXT_MEMORY_DESCRITOR -#define NEXT_MEMORY_DESCRITOR(desc, size) ((EFI_MEMORY_DESCRIPTOR *)((char *)desc + size))) -#endif - inline static uint64_t roundUpTo2Exponent(uint64_t v) { uint64_t s = 1; diff --git a/memory/paging_internal_efi.h b/memory/paging_internal_efi.h new file mode 100644 index 0000000..ff860cd --- /dev/null +++ b/memory/paging_internal_efi.h @@ -0,0 +1,42 @@ +#pragma once + +#include "../efimain.h" + + +// defined in paging_init.c +extern EFI_MEMORY_DESCRIPTOR *efiMemoryMap; +extern UINTN efiMemoryMapSize; +extern UINTN efiMemoryMapKey; +extern UINTN efiDescriptorSize; +extern UINT32 efiDescriptorVertion; + +static inline const char * + memoryTypeName(EFI_MEMORY_TYPE type) { +#define CASE(c) \ + case c: \ + return #c; + switch (type) { + CASE(EfiReservedMemoryType) + CASE(EfiLoaderCode) + CASE(EfiLoaderData) + CASE(EfiBootServicesCode) + CASE(EfiBootServicesData) + CASE(EfiRuntimeServicesCode) + CASE(EfiRuntimeServicesData) + CASE(EfiConventionalMemory) + CASE(EfiUnusableMemory) + CASE(EfiACPIReclaimMemory) + CASE(EfiACPIMemoryNVS) + CASE(EfiMemoryMappedIO) + CASE(EfiMemoryMappedIOPortSpace) + CASE(EfiPalCode) + case EfiMaxMemoryType: + return "EfiPersistentMemory"; + } + return "(unknown)"; +#undef CASE +} + +#ifndef NEXT_MEMORY_DESCRITOR +#define NEXT_MEMORY_DESCRITOR(desc, size) ((EFI_MEMORY_DESCRIPTOR *)((char *)desc + size))) +#endif diff --git a/runtime/stdio.c b/runtime/stdio.c index 92d3642..950f8cb 100644 --- a/runtime/stdio.c +++ b/runtime/stdio.c @@ -6,6 +6,7 @@ #include "../graphics/graphics.h" #include "../driver/irq/pic/serial/serial.h" +#include "../efimain.h" #include #include @@ -100,16 +101,3 @@ int io_Printf(const char *fmt, ...) { return ret; } - -EFI_INPUT_KEY io_PauseForKeystroke() { -#ifdef HELOS_RUNTIME_QUIET - EFI_INPUT_KEY k = {0, 0}; - return k; -#else - UINTN index; - EFI_INPUT_KEY key; - efiBootServices->WaitForEvent(1, &efiStdin->WaitForKey, &index); - efiSystemTable->ConIn->ReadKeyStroke(efiSystemTable->ConIn, &key); - return key; -#endif -} diff --git a/runtime/stdio.h b/runtime/stdio.h index 3bbca1b..aefa4dc 100644 --- a/runtime/stdio.h +++ b/runtime/stdio.h @@ -20,8 +20,6 @@ void io_WriteConsoleASCII(const char *str); // io_Printf is a printf() replacement, printing to WriteConsole function. int io_Printf(const char *format, ...); -EFI_INPUT_KEY io_PauseForKeystroke(); - // Debugging printing marcos #ifndef NDEBUG