From ef0123946841026cbf241dc0a8897abbe6830cd9 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 18 Mar 2022 16:03:50 -0400 Subject: [PATCH] HELLO: Add scratch module --- src/hello/hello.d | 13 +++++++------ src/hello/scratch.d | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 src/hello/scratch.d diff --git a/src/hello/hello.d b/src/hello/hello.d index 7e0006d..5d8f617 100644 --- a/src/hello/hello.d +++ b/src/hello/hello.d @@ -1,10 +1,10 @@ import uefi; import output; +import scratch; import hulk.bootinfo; __gshared EFI_SYSTEM_TABLE * st; __gshared BootInfo bootinfo; -__gshared ubyte[512 * 1024] scratch; __gshared UINTN memory_map_key; private void wait_key() @@ -31,9 +31,9 @@ private bool in_qemu() private bool set_graphics_mode() { uint max_horizontal_resolution = in_qemu() ? 1920u : 0xFFFFFFFFu; - UINTN buffer_size = scratch.sizeof; + UINTN buffer_size = scratch_free(); EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; - EFI_HANDLE * handles = cast(EFI_HANDLE *)&scratch; + 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) @@ -87,12 +87,13 @@ private bool set_graphics_mode() private bool get_memory_map() { - UINTN memory_map_size = scratch.sizeof; + UINTN memory_map_size = scratch_free(); UINTN descriptor_size; UINT32 descriptor_version; + ubyte * scratch_base = scratch_current(); UINTN status = st.BootServices.GetMemoryMap( &memory_map_size, - cast(EFI_MEMORY_DESCRIPTOR *)&scratch, + cast(EFI_MEMORY_DESCRIPTOR *)scratch_base, &memory_map_key, &descriptor_size, &descriptor_version); @@ -105,7 +106,7 @@ private bool get_memory_map() size_t di; for (size_t i = 0u; i < n_entries; i++) { - EFI_MEMORY_DESCRIPTOR * descriptor = cast(EFI_MEMORY_DESCRIPTOR *)&scratch[i * descriptor_size]; + EFI_MEMORY_DESCRIPTOR * descriptor = cast(EFI_MEMORY_DESCRIPTOR *)&scratch_base[i * descriptor_size]; if ((descriptor.Type == EfiConventionalMemory) || (descriptor.Type == EfiBootServicesCode) || (descriptor.Type == EfiBootServicesData)) diff --git a/src/hello/scratch.d b/src/hello/scratch.d new file mode 100644 index 0000000..8463100 --- /dev/null +++ b/src/hello/scratch.d @@ -0,0 +1,19 @@ +__gshared ubyte[512 * 1024] scratch; +__gshared size_t scratch_used; + +size_t scratch_free() +{ + return scratch.sizeof - scratch_used; +} + +ubyte * scratch_current() +{ + return &scratch[scratch_used]; +} + +ubyte * scratch_alloc(size_t n = 1) +{ + ubyte * address = &scratch[scratch_used]; + scratch_used += 4096u * n; + return address; +}