diff options
| author | Ian M. <ian@aptel.org> | 2026-09-11 05:40:51 +0000 |
|---|---|---|
| committer | Ian M. <ian@aptel.org> | 2026-09-11 05:40:51 +0000 |
| commit | 9bf419b4e0516ac23c3e49af0a10cb79ac1fd063 (patch) | |
| tree | 2cf48d860bae74a613864134d391e041033c3c81 | |
| parent | d1ae146b1fcecdd6ae688cedf523a5be0d7980c5 (diff) | |
core: Get platform memory map and exit boot services
Signed-off-by: Ian M. <ian@aptel.org>
| -rw-r--r-- | core/memmap.c | 63 | ||||
| -rw-r--r-- | core/sboot.c | 19 | ||||
| -rw-r--r-- | head/sboot/memmap.h | 30 |
3 files changed, 112 insertions, 0 deletions
diff --git a/core/memmap.c b/core/memmap.c new file mode 100644 index 0000000..f02e538 --- /dev/null +++ b/core/memmap.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +#include <sboot/memmap.h> + +EFI_STATUS +SBootGetMemmap(OUT PLATFORM_MEMMAP *Map) +{ + EFI_STATUS Status; + + if (Map == NULL) { + return EFI_INVALID_PARAMETER; + } + + Map->Map = NULL; + Map->MapSize = 0; + Map->MapKey = 0; + Map->DescriptorSize = 0; + Map->DescriptorVersion = 0; + + Status = uefi_call_wrapper( + gBS->GetMemoryMap, + 5, + &Map->MapSize, + Map->Map, + &Map->MapKey, + &Map->DescriptorSize, + &Map->DescriptorVersion + ); + + Map->Map = AllocatePool(Map->MapSize); + if (Map->Map == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + /* + * We have to call this twice as the act of allocating memory + * alters the memory map. + */ + for (UINTN Idx = 0; Idx < 2; ++Idx) { + Status = uefi_call_wrapper( + gBS->GetMemoryMap, + 5, + &Map->MapSize, + Map->Map, + &Map->MapKey, + &Map->DescriptorSize, + &Map->DescriptorVersion + ); + + if (!EFI_ERROR(Status)) { + break; + } + } + + if (EFI_ERROR(Status)) { + Print(L"fatal: failed to get memory map\n"); + } + + return EFI_SUCCESS; +} diff --git a/core/sboot.c b/core/sboot.c index 604c768..53366a0 100644 --- a/core/sboot.c +++ b/core/sboot.c @@ -7,11 +7,13 @@ #include <efilib.h> #include <sboot/sboot.h> #include <sboot/config.h> +#include <sboot/memmap.h> #include <sboot/fwi.h> #include <sboot/file.h> /* Private globals */ static UINT8 *KernelBin = NULL; +static PLATFORM_MEMMAP MemMap; /* Globals */ EFI_FILE_HANDLE gBootVolume; @@ -50,5 +52,22 @@ efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) Hcf(); } + Status = SBootGetMemmap(&MemMap); + if (EFI_ERROR(Status)) { + Hcf(); + } + + /* Get out of here! */ + Status = uefi_call_wrapper( + gBS->ExitBootServices, + 2, + ImageHandle, + MemMap.MapKey + ); + + if (EFI_ERROR(Status)) { + Hcf(); + } + for (;;); } diff --git a/head/sboot/memmap.h b/head/sboot/memmap.h new file mode 100644 index 0000000..daad502 --- /dev/null +++ b/head/sboot/memmap.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +#ifndef _SBOOT_MEMMAP_H_ +#define _SBOOT_MEMMAP_H_ 1 + +#include <efi.h> +#include <efilib.h> + +/* + * EFI memory map + */ +typedef struct { + EFI_MEMORY_DESCRIPTOR *Map; + UINTN MapSize; + UINTN MapKey; + UINTN DescriptorSize; + UINT32 DescriptorVersion; +} PLATFORM_MEMMAP; + +/* + * Obtain the platform memory map + * + * @Map: Result is written here + */ +EFI_STATUS SBootGetMemmap(OUT PLATFORM_MEMMAP *Map); + +#endif /* !_SBOOT_MEMMAP_H_ */ |
