Format using clang-format

This commit is contained in:
Justin C. Miller
2018-03-25 14:06:25 -07:00
parent 64a6d88e5c
commit fd5780610b
12 changed files with 173 additions and 161 deletions

21
.clang-format Normal file
View File

@@ -0,0 +1,21 @@
---
Language: Cpp
BasedOnStyle: LLVM
ColumnLimit: 100
IndentWidth: 4
TabWidth: 4
UseTab: Always
AccessModifierOffset: -4
AlignEscapedNewlinesLeft: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: AllDefinitions
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BreakBeforeBraces: Linux

View File

@@ -9,7 +9,7 @@ UINTN ROWS = 0;
UINTN COLS = 0; UINTN COLS = 0;
EFI_STATUS EFI_STATUS
con_initialize (const CHAR16 *version) con_initialize(const CHAR16 *version)
{ {
EFI_STATUS status; EFI_STATUS status;
@@ -18,13 +18,11 @@ con_initialize (const CHAR16 *version)
EFI_GRAPHICS_OUTPUT_PROTOCOL *gfx_out_proto; EFI_GRAPHICS_OUTPUT_PROTOCOL *gfx_out_proto;
EFI_GUID gfx_out_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; EFI_GUID gfx_out_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
status = ST->BootServices->LocateProtocol( status = ST->BootServices->LocateProtocol(&gfx_out_guid, NULL, (void **)&gfx_out_proto);
&gfx_out_guid, NULL, (void**)&gfx_out_proto);
CHECK_EFI_STATUS_OR_RETURN(status, "LocateProtocol gfx"); CHECK_EFI_STATUS_OR_RETURN(status, "LocateProtocol gfx");
const uint32_t modes = gfx_out_proto->Mode->MaxMode; const uint32_t modes = gfx_out_proto->Mode->MaxMode;
uint32_t res = uint32_t res = gfx_out_proto->Mode->Info->HorizontalResolution *
gfx_out_proto->Mode->Info->HorizontalResolution *
gfx_out_proto->Mode->Info->VerticalResolution; gfx_out_proto->Mode->Info->VerticalResolution;
uint32_t best = gfx_out_proto->Mode->Mode; uint32_t best = gfx_out_proto->Mode->Mode;
@@ -35,13 +33,10 @@ con_initialize (const CHAR16 *version)
CHECK_EFI_STATUS_OR_RETURN(status, "QueryMode"); CHECK_EFI_STATUS_OR_RETURN(status, "QueryMode");
#ifdef MAX_HRES #ifdef MAX_HRES
if (info->HorizontalResolution > MAX_HRES) if (info->HorizontalResolution > MAX_HRES) continue;
continue;
#endif #endif
const uint32_t new_res = const uint32_t new_res = info->HorizontalResolution * info->VerticalResolution;
info->HorizontalResolution *
info->VerticalResolution;
if (new_res > res) { if (new_res > res) {
best = i; best = i;
@@ -52,69 +47,64 @@ con_initialize (const CHAR16 *version)
status = gfx_out_proto->SetMode(gfx_out_proto, best); status = gfx_out_proto->SetMode(gfx_out_proto, best);
CHECK_EFI_STATUS_OR_RETURN(status, "SetMode %d/%d", best, modes); CHECK_EFI_STATUS_OR_RETURN(status, "SetMode %d/%d", best, modes);
status = ST->ConOut->QueryMode( status = ST->ConOut->QueryMode(ST->ConOut, ST->ConOut->Mode->Mode, &COLS, &ROWS);
ST->ConOut,
ST->ConOut->Mode->Mode,
&COLS, &ROWS);
CHECK_EFI_STATUS_OR_RETURN(status, "QueryMode"); CHECK_EFI_STATUS_OR_RETURN(status, "QueryMode");
status = ST->ConOut->ClearScreen(ST->ConOut); status = ST->ConOut->ClearScreen(ST->ConOut);
CHECK_EFI_STATUS_OR_RETURN(status, "ClearScreen"); CHECK_EFI_STATUS_OR_RETURN(status, "ClearScreen");
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTCYAN); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTCYAN);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"Popcorn loader "); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"Popcorn loader ");
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTMAGENTA); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTMAGENTA);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)version); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)version);
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L" booting...\r\n\n"); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L" booting...\r\n\n");
con_status_begin(L"Setting console display mode: "); con_status_begin(L"Setting console display mode: ");
Print(L"\n %ux%u (%ux%u chars)", Print(L"\n %ux%u (%ux%u chars)", gfx_out_proto->Mode->Info->HorizontalResolution,
gfx_out_proto->Mode->Info->HorizontalResolution, gfx_out_proto->Mode->Info->VerticalResolution, ROWS, COLS);
gfx_out_proto->Mode->Info->VerticalResolution,
ROWS, COLS);
con_status_ok(); con_status_ok();
return status; return status;
} }
void void
con_status_begin (const CHAR16 *message) con_status_begin(const CHAR16 *message)
{ {
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)message); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)message);
} }
void void
con_status_ok () con_status_ok()
{ {
UINTN row = ST->ConOut->Mode->CursorRow; UINTN row = ST->ConOut->Mode->CursorRow;
ST->ConOut->SetCursorPosition(ST->ConOut, 4, ++row); ST->ConOut->SetCursorPosition(ST->ConOut, 4, ++row);
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"["); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"[");
ST->ConOut->SetAttribute(ST->ConOut, EFI_GREEN); ST->ConOut->SetAttribute(ST->ConOut, EFI_GREEN);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L" ok "); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L" ok ");
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"]\r"); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"]\r");
ST->ConOut->SetCursorPosition(ST->ConOut, 0, ++row+1); ST->ConOut->SetCursorPosition(ST->ConOut, 0, ++row + 1);
} }
void void
con_status_fail (const CHAR16 *error) con_status_fail(const CHAR16 *error)
{ {
UINTN row = ST->ConOut->Mode->CursorRow; UINTN row = ST->ConOut->Mode->CursorRow;
ST->ConOut->SetCursorPosition(ST->ConOut, COLS-8, row); ST->ConOut->SetCursorPosition(ST->ConOut, COLS - 8, row);
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"["); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"[");
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTRED); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTRED);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"failed"); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"failed");
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)L"]\r"); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"]\r");
ST->ConOut->SetCursorPosition(ST->ConOut, 2, row+1); ST->ConOut->SetCursorPosition(ST->ConOut, 2, row + 1);
ST->ConOut->SetAttribute(ST->ConOut, EFI_RED); ST->ConOut->SetAttribute(ST->ConOut, EFI_RED);
ST->ConOut->OutputString(ST->ConOut, (CHAR16*)error); ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)error);
ST->ConOut->SetCursorPosition(ST->ConOut, 0, row+2); ST->ConOut->SetCursorPosition(ST->ConOut, 0, row + 2);
ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY); ST->ConOut->SetAttribute(ST->ConOut, EFI_LIGHTGRAY);
} }

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <efi.h> #include <efi.h>
EFI_STATUS con_initialize (const CHAR16 *version); EFI_STATUS con_initialize(const CHAR16 *version);
void con_status_begin (const CHAR16 *message); void con_status_begin(const CHAR16 *message);
void con_status_ok (); void con_status_ok();
void con_status_fail (const CHAR16 *error); void con_status_fail(const CHAR16 *error);

View File

@@ -3,7 +3,9 @@
static CHAR16 kernel_name[] = KERNEL_FILENAME; static CHAR16 kernel_name[] = KERNEL_FILENAME;
EFI_STATUS loader_load_kernel(void **kernel_image, uint64_t *length) { EFI_STATUS
loader_load_kernel(void **kernel_image, uint64_t *length)
{
if (kernel_image == 0 || length == 0) if (kernel_image == 0 || length == 0)
CHECK_EFI_STATUS_OR_RETURN(EFI_INVALID_PARAMETER, "NULL kernel_image or length pointer"); CHECK_EFI_STATUS_OR_RETURN(EFI_INVALID_PARAMETER, "NULL kernel_image or length pointer");
@@ -12,15 +14,13 @@ EFI_STATUS loader_load_kernel(void **kernel_image, uint64_t *length) {
EFI_HANDLE *handles = NULL; EFI_HANDLE *handles = NULL;
UINTN handleCount = 0; UINTN handleCount = 0;
status = ST->BootServices->LocateHandleBuffer( status = ST->BootServices->LocateHandleBuffer(ByProtocol, &guid, NULL, &handleCount, &handles);
ByProtocol, &guid, NULL, &handleCount, &handles);
CHECK_EFI_STATUS_OR_RETURN(status, "LocateHandleBuffer"); CHECK_EFI_STATUS_OR_RETURN(status, "LocateHandleBuffer");
for (unsigned i=0; i<handleCount; ++i) { for (unsigned i = 0; i < handleCount; ++i) {
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fileSystem = NULL; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fileSystem = NULL;
status = ST->BootServices->HandleProtocol( status = ST->BootServices->HandleProtocol(handles[i], &guid, (void **)&fileSystem);
handles[i], &guid, (void**)&fileSystem);
CHECK_EFI_STATUS_OR_RETURN(status, "HandleProtocol"); CHECK_EFI_STATUS_OR_RETURN(status, "HandleProtocol");
EFI_FILE_PROTOCOL *root = NULL; EFI_FILE_PROTOCOL *root = NULL;
@@ -28,12 +28,8 @@ EFI_STATUS loader_load_kernel(void **kernel_image, uint64_t *length) {
CHECK_EFI_STATUS_OR_RETURN(status, "OpenVolume"); CHECK_EFI_STATUS_OR_RETURN(status, "OpenVolume");
EFI_FILE_PROTOCOL *file = NULL; EFI_FILE_PROTOCOL *file = NULL;
status = root->Open( status = root->Open(root, &file, kernel_name, EFI_FILE_MODE_READ,
root, EFI_FILE_READ_ONLY | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM);
&file,
kernel_name,
EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY|EFI_FILE_HIDDEN|EFI_FILE_SYSTEM);
if (!EFI_ERROR(status)) { if (!EFI_ERROR(status)) {
void *buffer = NULL; void *buffer = NULL;
@@ -46,7 +42,7 @@ EFI_STATUS loader_load_kernel(void **kernel_image, uint64_t *length) {
status = file->GetInfo(file, &file_info_guid, &buffer_size, buffer); status = file->GetInfo(file, &file_info_guid, &buffer_size, buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Getting kernel file info"); CHECK_EFI_STATUS_OR_RETURN(status, "Getting kernel file info");
buffer_size = ((EFI_FILE_INFO*)buffer)->FileSize; buffer_size = ((EFI_FILE_INFO *)buffer)->FileSize;
status = ST->BootServices->FreePool(buffer); status = ST->BootServices->FreePool(buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Freeing kernel file info memory"); CHECK_EFI_STATUS_OR_RETURN(status, "Freeing kernel file info memory");
@@ -57,11 +53,12 @@ EFI_STATUS loader_load_kernel(void **kernel_image, uint64_t *length) {
status = ST->BootServices->AllocatePages(AllocateAddress, mem_type, page_count, &addr); status = ST->BootServices->AllocatePages(AllocateAddress, mem_type, page_count, &addr);
if (status == EFI_NOT_FOUND) { if (status == EFI_NOT_FOUND) {
// couldn't get the address we wanted, try loading the kernel anywhere // couldn't get the address we wanted, try loading the kernel anywhere
status = ST->BootServices->AllocatePages(AllocateAnyPages, mem_type, page_count, &addr); status =
ST->BootServices->AllocatePages(AllocateAnyPages, mem_type, page_count, &addr);
} }
CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel pages"); CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel pages");
buffer = (void*)addr; buffer = (void *)addr;
status = file->Read(file, &buffer_size, buffer); status = file->Read(file, &buffer_size, buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Reading kernel file"); CHECK_EFI_STATUS_OR_RETURN(status, "Reading kernel file");

View File

@@ -7,7 +7,7 @@
#include "utility.h" #include "utility.h"
#ifndef GIT_VERSION #ifndef GIT_VERSION
#define GIT_VERSION L"no version" #define GIT_VERSION L"no version"
#endif #endif
#define KERNEL_MAGIC 0x600db007 #define KERNEL_MAGIC 0x600db007
@@ -24,7 +24,7 @@ struct kernel_version {
#pragma pack(pop) #pragma pack(pop)
EFI_STATUS EFI_STATUS
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{ {
EFI_STATUS status; EFI_STATUS status;
@@ -45,19 +45,14 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
CHECK_EFI_STATUS_OR_FAIL(status); CHECK_EFI_STATUS_OR_FAIL(status);
Print(L"\n %u bytes at 0x%x", kernel_length, kernel_image); Print(L"\n %u bytes at 0x%x", kernel_length, kernel_image);
struct kernel_version *version = (struct kernel_version*)kernel_image; struct kernel_version *version = (struct kernel_version *)kernel_image;
if (version->magic != KERNEL_MAGIC) { if (version->magic != KERNEL_MAGIC) {
Print(L"\n bad magic %x", version->magic); Print(L"\n bad magic %x", version->magic);
CHECK_EFI_STATUS_OR_FAIL(EFI_CRC_ERROR); CHECK_EFI_STATUS_OR_FAIL(EFI_CRC_ERROR);
} }
Print(L"\n Kernel version %d.%d.%d %x%s", Print(L"\n Kernel version %d.%d.%d %x%s", version->major, version->minor, version->patch,
version->major, version->gitsha & 0x0fffffff, version->gitsha & 0xf0000000 ? "*" : "");
version->minor,
version->patch,
version->gitsha & 0x0fffffff,
version->gitsha & 0xf0000000 ? "*" : ""
);
Print(L"\n Entrypoint %d", version->entrypoint); Print(L"\n Entrypoint %d", version->entrypoint);
void (*kernel_main)() = version->entrypoint; void (*kernel_main)() = version->entrypoint;
@@ -71,29 +66,24 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
UINT32 desc_version = 0; UINT32 desc_version = 0;
EFI_MEMORY_DESCRIPTOR *memory_map; EFI_MEMORY_DESCRIPTOR *memory_map;
status = memory_mark_address_for_update((void**)&ST); status = memory_mark_address_for_update((void **)&ST);
CHECK_EFI_STATUS_OR_FAIL(status); CHECK_EFI_STATUS_OR_FAIL(status);
status = memory_mark_address_for_update((void**)&kernel_image); status = memory_mark_address_for_update((void **)&kernel_image);
CHECK_EFI_STATUS_OR_FAIL(status); CHECK_EFI_STATUS_OR_FAIL(status);
status = memory_mark_address_for_update((void**)&kernel_main); status = memory_mark_address_for_update((void **)&kernel_main);
CHECK_EFI_STATUS_OR_FAIL(status); CHECK_EFI_STATUS_OR_FAIL(status);
status = memory_get_map(&memory_map, status = memory_get_map(&memory_map, &memmap_size, &memmap_key, &desc_size, &desc_version);
&memmap_size, &memmap_key,
&desc_size, &desc_version);
CHECK_EFI_STATUS_OR_FAIL(status); CHECK_EFI_STATUS_OR_FAIL(status);
status = ST->BootServices->ExitBootServices(ImageHandle, memmap_key); status = ST->BootServices->ExitBootServices(ImageHandle, memmap_key);
CHECK_EFI_STATUS_OR_ASSERT(status, 0); CHECK_EFI_STATUS_OR_ASSERT(status, 0);
status = memory_virtualize( status = memory_virtualize(memory_map, memmap_size, desc_size, desc_version);
memory_map, memmap_size,
desc_size, desc_version);
CHECK_EFI_STATUS_OR_ASSERT(status, 0); CHECK_EFI_STATUS_OR_ASSERT(status, 0);
kernel_main(); kernel_main();
return EFI_LOAD_ERROR; return EFI_LOAD_ERROR;
} }

View File

@@ -1,8 +1,8 @@
#include <efi.h> #include <efi.h>
#include <efilib.h> #include <efilib.h>
#include "memory.h"
#include "loader.h" #include "loader.h"
#include "memory.h"
#include "utility.h" #include "utility.h"
const UINTN PAGE_SIZE = 4096; const UINTN PAGE_SIZE = 4096;
@@ -25,63 +25,65 @@ const CHAR16 *memory_type_names[] = {
L"EfiPersistentMemory", L"EfiPersistentMemory",
}; };
static const CHAR16 *memory_type_name(UINT32 value) { static const CHAR16 *
if (value >= (sizeof(memory_type_names)/sizeof(CHAR16*))) { memory_type_name(UINT32 value)
if (value == KERNEL_MEMTYPE) {
return L"Kernel Image"; if (value >= (sizeof(memory_type_names) / sizeof(CHAR16 *))) {
if (value == KERNEL_MEMTYPE) return L"Kernel Image";
return L"Bad Type Value"; return L"Bad Type Value";
} }
return memory_type_names[value]; return memory_type_names[value];
} }
void EFIAPI memory_update_addresses(EFI_EVENT UNUSED *event, void *context) { void EFIAPI
memory_update_addresses(EFI_EVENT UNUSED *event, void *context)
{
EFI_STATUS status; EFI_STATUS status;
status = ST->RuntimeServices->ConvertPointer(0, (void **)context); status = ST->RuntimeServices->ConvertPointer(0, (void **)context);
CHECK_EFI_STATUS_OR_ASSERT(status, *((void**)context)); CHECK_EFI_STATUS_OR_ASSERT(status, *((void **)context));
} }
EFI_STATUS memory_mark_address_for_update(void **pointer) { EFI_STATUS
memory_mark_address_for_update(void **pointer)
{
EFI_EVENT event; EFI_EVENT event;
EFI_STATUS status; EFI_STATUS status;
status = ST->BootServices->CreateEvent( status = ST->BootServices->CreateEvent(EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, TPL_CALLBACK,
EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
TPL_CALLBACK,
(EFI_EVENT_NOTIFY)&memory_update_addresses, (EFI_EVENT_NOTIFY)&memory_update_addresses,
(void*)pointer, (void *)pointer, &event);
&event);
CHECK_EFI_STATUS_OR_ASSERT(status, pointer); CHECK_EFI_STATUS_OR_ASSERT(status, pointer);
} }
EFI_STATUS EFI_STATUS
memory_virtualize( memory_virtualize(EFI_MEMORY_DESCRIPTOR *memory_map, UINTN memmap_size, UINTN desc_size,
EFI_MEMORY_DESCRIPTOR *memory_map, UINT32 desc_version)
UINTN memmap_size, {
UINTN desc_size,
UINT32 desc_version) {
EFI_MEMORY_DESCRIPTOR *end = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)memory_map + memmap_size); EFI_MEMORY_DESCRIPTOR *end = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)memory_map + memmap_size);
EFI_MEMORY_DESCRIPTOR *d = memory_map; EFI_MEMORY_DESCRIPTOR *d = memory_map;
while (d < end) { while (d < end) {
if (d->Type == KERNEL_MEMTYPE) { if (d->Type == KERNEL_MEMTYPE) {
//d->VirtualStart = (EFI_VIRTUAL_ADDRESS)KERNEL_VIRT_ADDRESS; // d->VirtualStart = (EFI_VIRTUAL_ADDRESS)KERNEL_VIRT_ADDRESS;
d->VirtualStart = (EFI_VIRTUAL_ADDRESS)d->PhysicalStart; d->VirtualStart = (EFI_VIRTUAL_ADDRESS)d->PhysicalStart;
d->Attribute |= EFI_MEMORY_RUNTIME; d->Attribute |= EFI_MEMORY_RUNTIME;
} } else /*if (d->Attribute & EFI_MEMORY_RUNTIME)*/ {
else /*if (d->Attribute & EFI_MEMORY_RUNTIME)*/ {
d->VirtualStart = (EFI_VIRTUAL_ADDRESS)d->PhysicalStart; d->VirtualStart = (EFI_VIRTUAL_ADDRESS)d->PhysicalStart;
} }
d = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)d + desc_size); d = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)d + desc_size);
} }
return ST->RuntimeServices->SetVirtualAddressMap(memmap_size, desc_size, desc_version, memory_map); return ST->RuntimeServices->SetVirtualAddressMap(memmap_size, desc_size, desc_version,
memory_map);
} }
EFI_STATUS memory_get_map(EFI_MEMORY_DESCRIPTOR **buffer, UINTN *buffer_size, EFI_STATUS
UINTN *key, UINTN *desc_size, UINT32 *desc_version) { memory_get_map(EFI_MEMORY_DESCRIPTOR **buffer, UINTN *buffer_size, UINTN *key, UINTN *desc_size,
UINT32 *desc_version)
{
EFI_STATUS status; EFI_STATUS status;
UINTN needs_size = 0; UINTN needs_size = 0;
@@ -92,7 +94,7 @@ EFI_STATUS memory_get_map(EFI_MEMORY_DESCRIPTOR **buffer, UINTN *buffer_size,
// Give some extra buffer to account for changes. // Give some extra buffer to account for changes.
*buffer_size = needs_size + 256; *buffer_size = needs_size + 256;
status = ST->BootServices->AllocatePool(EfiLoaderData, *buffer_size, (void**)buffer); status = ST->BootServices->AllocatePool(EfiLoaderData, *buffer_size, (void **)buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to allocate space for memory map"); CHECK_EFI_STATUS_OR_RETURN(status, "Failed to allocate space for memory map");
status = ST->BootServices->GetMemoryMap(buffer_size, *buffer, key, desc_size, desc_version); status = ST->BootServices->GetMemoryMap(buffer_size, *buffer, key, desc_size, desc_version);
@@ -100,13 +102,14 @@ EFI_STATUS memory_get_map(EFI_MEMORY_DESCRIPTOR **buffer, UINTN *buffer_size,
return EFI_SUCCESS; return EFI_SUCCESS;
} }
EFI_STATUS memory_dump_map() { EFI_STATUS
memory_dump_map()
{
EFI_MEMORY_DESCRIPTOR *buffer; EFI_MEMORY_DESCRIPTOR *buffer;
UINTN buffer_size, desc_size, key; UINTN buffer_size, desc_size, key;
UINT32 desc_version; UINT32 desc_version;
EFI_STATUS status = memory_get_map(&buffer, &buffer_size, &key, EFI_STATUS status = memory_get_map(&buffer, &buffer_size, &key, &desc_size, &desc_version);
&desc_size, &desc_version);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to get memory map"); CHECK_EFI_STATUS_OR_RETURN(status, "Failed to get memory map");
const UINTN count = buffer_size / desc_size; const UINTN count = buffer_size / desc_size;

View File

@@ -3,17 +3,10 @@
EFI_STATUS memory_mark_address_for_update(void **pointer); EFI_STATUS memory_mark_address_for_update(void **pointer);
EFI_STATUS memory_virtualize( EFI_STATUS memory_virtualize(EFI_MEMORY_DESCRIPTOR *memory_map, UINTN memmap_size, UINTN desc_size,
EFI_MEMORY_DESCRIPTOR *memory_map,
UINTN memmap_size,
UINTN desc_size,
UINT32 desc_version); UINT32 desc_version);
EFI_STATUS memory_get_map( EFI_STATUS memory_get_map(EFI_MEMORY_DESCRIPTOR **buffer, UINTN *buffer_size, UINTN *key,
EFI_MEMORY_DESCRIPTOR **buffer, UINTN *desc_size, UINT32 *desc_version);
UINTN *buffer_size,
UINTN *key,
UINTN *desc_size,
UINT32 *desc_version);
EFI_STATUS memory_dump_map(); EFI_STATUS memory_dump_map();

View File

@@ -7,11 +7,12 @@ struct ErrorCode {
extern struct ErrorCode ErrorCodeTable[]; extern struct ErrorCode ErrorCodeTable[];
const CHAR16 *util_error_message(EFI_STATUS status) { const CHAR16 *
util_error_message(EFI_STATUS status)
{
int32_t i = -1; int32_t i = -1;
while (ErrorCodeTable[++i].desc != NULL) { while (ErrorCodeTable[++i].desc != NULL) {
if (ErrorCodeTable[i].code == status) if (ErrorCodeTable[i].code == status) return ErrorCodeTable[i].desc;
return ErrorCodeTable[i].desc;
} }
return L"Unknown"; return L"Unknown";

View File

@@ -1,6 +1,6 @@
#include "console.h"
#include <efi.h> #include <efi.h>
#include <efilib.h> #include <efilib.h>
#include "console.h"
#define UNUSED __attribute__((unused)) #define UNUSED __attribute__((unused))
@@ -8,7 +8,7 @@ const CHAR16 *util_error_message(EFI_STATUS status);
#define CHECK_EFI_STATUS_OR_RETURN(s, msg, ...) \ #define CHECK_EFI_STATUS_OR_RETURN(s, msg, ...) \
if (EFI_ERROR((s))) { \ if (EFI_ERROR((s))) { \
Print(L"EFI_ERROR: " msg L": %s\n", ## __VA_ARGS__, util_error_message(s)); \ Print(L"EFI_ERROR: " msg L": %s\n", ##__VA_ARGS__, util_error_message(s)); \
return (s); \ return (s); \
} }
@@ -20,15 +20,13 @@ const CHAR16 *util_error_message(EFI_STATUS status);
#define CHECK_EFI_STATUS_OR_ASSERT(s, d) \ #define CHECK_EFI_STATUS_OR_ASSERT(s, d) \
if (EFI_ERROR((s))) { \ if (EFI_ERROR((s))) { \
__asm__ __volatile__ ( \ __asm__ __volatile__( \
"movq %0, %%r8;" \ "movq %0, %%r8;" \
"movq %1, %%r9;" \ "movq %1, %%r9;" \
"movq %2, %%r10;" \ "movq %2, %%r10;" \
"movq $0, %%rdx;" \ "movq $0, %%rdx;" \
"divq %%rdx;" \ "divq %%rdx;" \
: \ : \
:"r"((uint64_t)s), "r"((uint64_t)d), "r"((uint64_t)__LINE__) \ : "r"((uint64_t)s), "r"((uint64_t)d), "r"((uint64_t)__LINE__) \
:"rax", "rdx", "r8", "r9", "r10" \ : "rax", "rdx", "r8", "r9", "r10"); \
); \
} }

View File

@@ -2,7 +2,9 @@
void do_the_set_registers(); void do_the_set_registers();
void kernel_main() { void
kernel_main()
{
volatile register int foo = 0x1a1b1c10; volatile register int foo = 0x1a1b1c10;
volatile register int bar = 0; volatile register int bar = 0;

View File

@@ -8,47 +8,60 @@ static size_t terminal_column;
static uint8_t terminal_color; static uint8_t terminal_color;
/* Note the use of the volatile keyword to prevent the compiler from eliminating dead stores. */ /* Note the use of the volatile keyword to prevent the compiler from eliminating dead stores. */
static volatile uint16_t* terminal_buffer; static volatile uint16_t *terminal_buffer;
uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg); uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg);
uint16_t vga_entry(unsigned char uc, uint8_t color); uint16_t vga_entry(unsigned char uc, uint8_t color);
static size_t strlen(const char* str) { static size_t
strlen(const char *str)
{
size_t len = 0; size_t len = 0;
while (str[len++]); while (str[len++])
;
return len; return len;
} }
void terminal_initialize(size_t startrow) { void
terminal_initialize(size_t startrow)
{
terminal_row = startrow; terminal_row = startrow;
terminal_column = 0; terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
terminal_buffer = (uint16_t*) 0xB8000; terminal_buffer = (uint16_t *)0xB8000;
} }
void terminal_setcolor(uint8_t color) { void
terminal_setcolor(uint8_t color)
{
terminal_color = color; terminal_color = color;
} }
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { void
terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x; const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color); terminal_buffer[index] = vga_entry(c, color);
} }
void terminal_putchar(char c) { void
terminal_putchar(char c)
{
terminal_putentryat(c, terminal_color, terminal_column, terminal_row); terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH) { if (++terminal_column == VGA_WIDTH) {
terminal_column = 0; terminal_column = 0;
if (++terminal_row == VGA_HEIGHT) if (++terminal_row == VGA_HEIGHT) terminal_row = 0;
terminal_row = 0;
} }
} }
void terminal_write(const char* data, size_t size) { void
for (size_t i = 0; i < size; i++) terminal_write(const char *data, size_t size)
terminal_putchar(data[i]); {
for (size_t i = 0; i < size; i++) terminal_putchar(data[i]);
} }
void terminal_writestring(const char* data) { void
terminal_writestring(const char *data)
{
terminal_write(data, strlen(data)); terminal_write(data, strlen(data));
} }

View File

@@ -22,17 +22,21 @@ enum vga_color {
VGA_COLOR_WHITE = 15, VGA_COLOR_WHITE = 15,
}; };
inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { inline uint8_t
vga_entry_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4; return fg | bg << 4;
} }
inline uint16_t vga_entry(unsigned char uc, uint8_t color) { inline uint16_t
return (uint16_t) uc | (uint16_t) color << 8; vga_entry(unsigned char uc, uint8_t color)
{
return (uint16_t)uc | (uint16_t)color << 8;
} }
void terminal_initialize(size_t rowstart); void terminal_initialize(size_t rowstart);
void terminal_setcolor(uint8_t color); void terminal_setcolor(uint8_t color);
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y); void terminal_putentryat(char c, uint8_t color, size_t x, size_t y);
void terminal_putchar(char c); void terminal_putchar(char c);
void terminal_write(const char* data, size_t size); void terminal_write(const char *data, size_t size);
void terminal_writestring(const char* data); void terminal_writestring(const char *data);