main: refactor to keep the EFI headers from polluting the main namespace

This commit is contained in:
Edgaru089 2021-11-06 23:40:50 +08:00
parent e2a2679f6d
commit 4460661bfd
13 changed files with 89 additions and 85 deletions

19
efimain.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include "stddef.h"
#ifdef __cplusplus
extern "C" {
#include <efi.h>
}
#else
#include <efi.h>
#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;

View File

@ -9,6 +9,7 @@
#include "../runtime/stdio.h" #include "../runtime/stdio.h"
#include "../util/minmax.h" #include "../util/minmax.h"
#include "../util/stack.h" #include "../util/stack.h"
#include "../efimain.h"
#include <efiprot.h> #include <efiprot.h>
#include <assert.h> #include <assert.h>

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "../main.h" #include "../main.h"
#include "efiprot.h"
#include "color.h" #include "color.h"
#include "xcursor/xcursor.h" #include "xcursor/xcursor.h"
@ -14,12 +13,16 @@ extern "C" {
#define HELOS_GRAPHICS_TARGET_MODE_WIDTH 1600 #define HELOS_GRAPHICS_TARGET_MODE_WIDTH 1600
#define HELOS_GRAPHICS_TARGET_MODE_HEIGHT 900 #define HELOS_GRAPHICS_TARGET_MODE_HEIGHT 900
typedef enum {
graphics_PixelFormat_RGBA_8bit,
graphics_PixelFormat_BGRA_8bit,
} graphics_PixelFormat;
typedef struct { typedef struct {
int Width, Height; int Width, Height;
int PixelsPerLine; int PixelsPerLine;
EFI_GRAPHICS_PIXEL_FORMAT PixelFormat; graphics_PixelFormat PixelFormat;
} HelosGraphics_Mode; } 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. // 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) { 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); 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); graphics_SetPixel_RGB(posX, posY, color);
} }

2
main.c
View File

@ -1,5 +1,6 @@
#include "main.h" #include "main.h"
#include "efimain.h"
#include <string.h> #include <string.h>
#include "runtime/panic_assert.h" #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"); io_WriteConsole(" Welcome to " PROJECT_NAME " !\r\n\r\n");
graphics_Init(); graphics_Init();
io_PauseForKeystroke();
graphics_ClearBuffer(&HelosGraphics_Color_Black); graphics_ClearBuffer(&HelosGraphics_Color_Black);
graphics_FillPixel(0, 0, 20, 20, &HelosGraphics_Color_Black); graphics_FillPixel(0, 0, 20, 20, &HelosGraphics_Color_Black);

20
main.h
View File

@ -1,14 +1,7 @@
#pragma once #pragma once
#include "stddef.h" #include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#include <efi.h>
}
#else
#include <efi.h>
#endif
#define PROJECT_NAME "Helos1" #define PROJECT_NAME "Helos1"
#define PROJECT_NAME_LONG L"Helos1" #define PROJECT_NAME_LONG L"Helos1"
@ -33,15 +26,8 @@ extern "C" {
: "=r"(var)) : "=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 #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[]; extern const char link_TextStart[], link_TextEnd[];

View File

@ -1,5 +1,6 @@
#include "../main.h" #include "../main.h"
#include "../efimain.h"
#include "memory.h" #include "memory.h"
#include "../runtime/stdio.h" #include "../runtime/stdio.h"
#include "../extlib/dlmalloc/malloc-2.8.6.h" #include "../extlib/dlmalloc/malloc-2.8.6.h"

View File

@ -2,6 +2,7 @@
#include "../main.h" #include "../main.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus #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 // efiMallocTyped allocates from EFI_BOOT_SERVICES.AllocatePool
// with a memory type of EfiLoaderData. // with a memory type of EfiLoaderData.
void *efiMalloc(size_t size); void *efiMalloc(size_t size);
@ -61,12 +59,6 @@ void *kMalloc(size_t size);
void kFree(void *data); 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. // runtime_InitPaging initializes paging and kMalloc/kFree allocator.
// This function calls ExitBootServices()!!! which is great // This function calls ExitBootServices()!!! which is great
// Furthermore, it sets up a new stack, calls kMain() and does not return. // Furthermore, it sets up a new stack, calls kMain() and does not return.

11
memory/memory_internal.h Normal file
View File

@ -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;

View File

@ -1,5 +1,6 @@
#include "../main.h" #include "../main.h"
#include "../efimain.h"
#include "memory.h" #include "memory.h"
#include "../runtime/panic_assert.h" #include "../runtime/panic_assert.h"
#include "../runtime/stdio.h" #include "../runtime/stdio.h"
@ -13,6 +14,7 @@ void execformat_pe_ReadSystemHeader(execformat_pe_PortableExecutable *pe);
#include <string.h> #include <string.h>
#include "paging_internal.h" #include "paging_internal.h"
#include "paging_internal_efi.h"
EFI_MEMORY_DESCRIPTOR *efiMemoryMap; EFI_MEMORY_DESCRIPTOR *efiMemoryMap;
@ -111,7 +113,6 @@ void runtime_InitPaging() {
paging_UsableBytes / 1024.0 / 1024.0, paging_UsableBytes / 1024.0 / 1024.0,
paging_UsableBytes / 1024.0 / 1024.0 / 1024.0); paging_UsableBytes / 1024.0 / 1024.0 / 1024.0);
io_PauseForKeystroke();
assert(paging_LoaderCodeAddress && "EfiLoaderCode mapping not found"); assert(paging_LoaderCodeAddress && "EfiLoaderCode mapping not found");

View File

@ -9,13 +9,6 @@ extern "C" {
#endif #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 // defined in paging_init.c
extern uint64_t paging_TotalBytes, paging_UsableBytes; extern uint64_t paging_TotalBytes, paging_UsableBytes;
extern bool paging_SupportExecuteDisable; 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_4LevelPagingNX(void *pml4, int pcid); // with setting the Execute-Disalbe bit
FASTCALL_ABI void paging_modeswitch_Table(void *pml, int pcid); 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) { inline static uint64_t roundUpTo2Exponent(uint64_t v) {
uint64_t s = 1; uint64_t s = 1;

View File

@ -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

View File

@ -6,6 +6,7 @@
#include "../graphics/graphics.h" #include "../graphics/graphics.h"
#include "../driver/irq/pic/serial/serial.h" #include "../driver/irq/pic/serial/serial.h"
#include "../efimain.h"
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
@ -100,16 +101,3 @@ int io_Printf(const char *fmt, ...) {
return ret; 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
}

View File

@ -20,8 +20,6 @@ void io_WriteConsoleASCII(const char *str);
// io_Printf is a printf() replacement, printing to WriteConsole function. // io_Printf is a printf() replacement, printing to WriteConsole function.
int io_Printf(const char *format, ...); int io_Printf(const char *format, ...);
EFI_INPUT_KEY io_PauseForKeystroke();
// Debugging printing marcos // Debugging printing marcos
#ifndef NDEBUG #ifndef NDEBUG