From b40151055c2e79462873be478bd17eb14a948c76 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 20 Mar 2022 22:21:10 -0400 Subject: [PATCH] Rework scratch module and use qualified names for function calls --- src/hello/hello.d | 13 ++++++------- src/hello/scratch.d | 25 ++++++++++++++++++++----- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/hello/hello.d b/src/hello/hello.d index 4229fb9..b10e383 100644 --- a/src/hello/hello.d +++ b/src/hello/hello.d @@ -2,7 +2,7 @@ module hello.hello; import uefi; import console = hello.console; -import hello.scratch; +import scratch = hello.scratch; import hulk.bootinfo; import hos.page_table; import hos.cpu; @@ -28,9 +28,9 @@ private bool in_qemu() private bool set_graphics_mode() { uint max_horizontal_resolution = in_qemu() ? 1920u : 0xFFFFFFFFu; - UINTN buffer_size = scratch_free(); + UINTN buffer_size = scratch.free(); EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; - EFI_HANDLE * handles = cast(EFI_HANDLE *)scratch_current(); + EFI_HANDLE * handles = cast(EFI_HANDLE *)scratch.current(); EFI_STATUS status = st.BootServices.LocateHandle(ByProtocol, &gop_guid, null, &buffer_size, handles); if (status != EFI_SUCCESS) @@ -84,10 +84,10 @@ private bool set_graphics_mode() private ulong get_memory_map() { - UINTN memory_map_size = scratch_free(); + UINTN memory_map_size = scratch.free(); UINTN descriptor_size; UINT32 descriptor_version; - ubyte * scratch_base = scratch_current(); + ubyte * scratch_base = scratch.current(); UINTN status = st.BootServices.GetMemoryMap( &memory_map_size, cast(EFI_MEMORY_DESCRIPTOR *)scratch_base, @@ -134,7 +134,7 @@ private ulong get_memory_map() private PageTableEntry * new_page_table() { - PageTableEntry * pt = cast(PageTableEntry *)scratch_alloc(1u); + PageTableEntry * pt = cast(PageTableEntry *)scratch.alloc(1u); memset64(pt, 0u, 512); return pt; } @@ -257,7 +257,6 @@ extern (C) EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE * st) console.writeln("HELLO, HOS EFI Lightweight LOader, v0.1.0"); console.writeln("Firmware vendor: '%S', version: 0x%x", st.FirmwareVendor, st.FirmwareVendor); - wait_key(); if (!set_graphics_mode()) { diff --git a/src/hello/scratch.d b/src/hello/scratch.d index 8ee2083..d949fa9 100644 --- a/src/hello/scratch.d +++ b/src/hello/scratch.d @@ -1,19 +1,34 @@ +/** + * HELLO scratch buffer. + */ module hello.scratch; -align(4096) __gshared ubyte[1024 * 1024] scratch; -__gshared size_t scratch_used; +/* Scratch buffer. */ +private align(4096) __gshared ubyte[1024 * 1024] scratch; -size_t scratch_free() +/* Number of scratch buffer bytes used. */ +private __gshared size_t scratch_used; + +/** + * Get the number of free bytes in the scratch buffer. + */ +size_t free() { return scratch.sizeof - scratch_used; } -ubyte * scratch_current() +/** + * Get the current free scratch buffer address. + */ +ubyte * current() { return &scratch[scratch_used]; } -ubyte * scratch_alloc(size_t n = 1) +/** + * Allocate pages from the scratch buffer. + */ +ubyte * alloc(size_t n = 1) { ubyte * address = &scratch[scratch_used]; scratch_used += 4096u * n;