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

@@ -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,10 +47,7 @@ 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);
@@ -71,10 +63,8 @@ con_initialize (const CHAR16 *version)
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;

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,11 +28,7 @@ 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,
&file,
kernel_name,
EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM); EFI_FILE_READ_ONLY | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM);
if (!EFI_ERROR(status)) { if (!EFI_ERROR(status)) {
@@ -57,7 +53,8 @@ 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");

View File

@@ -51,13 +51,8 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
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;
@@ -80,20 +75,15 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
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,42 +25,42 @@ const CHAR16 *memory_type_names[] = {
L"EfiPersistentMemory", L"EfiPersistentMemory",
}; };
static const CHAR16 *memory_type_name(UINT32 value) { static const CHAR16 *
memory_type_name(UINT32 value)
{
if (value >= (sizeof(memory_type_names) / sizeof(CHAR16 *))) { if (value >= (sizeof(memory_type_names) / sizeof(CHAR16 *))) {
if (value == KERNEL_MEMTYPE) if (value == KERNEL_MEMTYPE) return L"Kernel Image";
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;
@@ -69,19 +69,21 @@ memory_virtualize(
// 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;
@@ -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))
@@ -28,7 +28,5 @@ const CHAR16 *util_error_message(EFI_STATUS status);
"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

@@ -13,42 +13,55 @@ 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,11 +22,15 @@ 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
vga_entry(unsigned char uc, uint8_t color)
{
return (uint16_t)uc | (uint16_t)color << 8; return (uint16_t)uc | (uint16_t)color << 8;
} }