Get memory map; remove heap

This commit is contained in:
Josh Holtrop 2022-03-15 23:58:37 -04:00
parent f054bab5cc
commit 704fd15f7b
3 changed files with 43 additions and 27 deletions

View File

@ -1,19 +0,0 @@
private __gshared align(4096) ubyte[512 * 1024] g_heap;
private __gshared size_t g_heap_index;
ubyte * heap_base()
{
return &g_heap[g_heap_index];
}
size_t heap_free()
{
return g_heap.sizeof - g_heap_index;
}
ubyte * heap_consume(size_t size)
{
ubyte * rv = &g_heap[g_heap_index];
g_heap_index += (size + 4095u) & 0xFFFFFFFFFFFFF000u;
return rv;
}

View File

@ -1,10 +1,11 @@
import uefi;
import output;
import heap;
import hos.bootinfo;
__gshared EFI_SYSTEM_TABLE * st;
__gshared BootInfo * bootinfo;
__gshared BootInfo bootinfo;
__gshared ubyte[512 * 1024] scratch;
__gshared UINTN memory_map_key;
private void wait_key()
{
@ -30,9 +31,9 @@ private bool in_qemu()
private bool set_graphics_mode()
{
uint max_horizontal_resolution = in_qemu() ? 1920u : 0xFFFFFFFFu;
UINTN buffer_size = heap_free();
UINTN buffer_size = scratch.sizeof;
EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
EFI_HANDLE * handles = cast(EFI_HANDLE *)heap_base();
EFI_HANDLE * handles = cast(EFI_HANDLE *)&scratch;
EFI_STATUS status = st.BootServices.LocateHandle(ByProtocol,
&gop_guid, null, &buffer_size, handles);
if (status != EFI_SUCCESS)
@ -84,6 +85,39 @@ private bool set_graphics_mode()
return true;
}
private bool get_memory_map()
{
UINTN memory_map_size = scratch.sizeof;
UINTN descriptor_size;
UINT32 descriptor_version;
UINTN status = st.BootServices.GetMemoryMap(
&memory_map_size,
cast(EFI_MEMORY_DESCRIPTOR *)&scratch,
&memory_map_key,
&descriptor_size,
&descriptor_version);
if (status != EFI_SUCCESS)
{
return false;
}
size_t n_entries = memory_map_size / descriptor_size;
size_t di;
for (size_t i = 0u; i < n_entries; i++)
{
EFI_MEMORY_DESCRIPTOR * descriptor = cast(EFI_MEMORY_DESCRIPTOR *)&scratch[i * descriptor_size];
if ((descriptor.Type == EfiConventionalMemory) ||
(descriptor.Type == EfiBootServicesCode) ||
(descriptor.Type == EfiBootServicesData))
{
bootinfo.memory_map[di].base = cast(void *)descriptor.PhysicalStart;
bootinfo.memory_map[di].size = descriptor.NumberOfPages * 4096u;
di++;
}
}
bootinfo.memory_map_count = di;
return true;
}
extern (C) EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE * st)
{
.st = st;
@ -93,14 +127,14 @@ extern (C) EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE * st)
writeln("HOS EFI loader");
writeln("Firmware vendor: '%S', version: 0x%x", st.FirmwareVendor, st.FirmwareVendor);
bootinfo = cast(BootInfo *)heap_consume(BootInfo.sizeof);
if (!set_graphics_mode())
{
wait_key();
return EFI_SUCCESS;
}
get_memory_map();
wait_key();
return EFI_SUCCESS;

View File

@ -13,7 +13,8 @@ struct BootInfo
uint stride;
uint format;
}
Framebuffer fb;
MemoryRegion * memory_map;
uint memory_map_size;
MemoryRegion[500] memory_map;
size_t memory_map_count;
}