2019-05-31 08:19:02 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
2020-11-05 02:50:01 +03:00
|
|
|
#include <lib/libc.h>
|
2020-01-22 07:02:12 +03:00
|
|
|
#include <lib/blib.h>
|
2020-05-10 01:38:27 +03:00
|
|
|
#include <lib/print.h>
|
2020-11-15 19:56:10 +03:00
|
|
|
#include <lib/trace.h>
|
2020-12-19 15:52:29 +03:00
|
|
|
#include <lib/real.h>
|
2021-02-25 13:28:14 +03:00
|
|
|
#include <fs/file.h>
|
2021-03-05 06:20:58 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-10-01 03:12:13 +03:00
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#if uefi == 1
|
2021-03-02 12:23:43 +03:00
|
|
|
EFI_SYSTEM_TABLE *gST;
|
|
|
|
EFI_BOOT_SERVICES *gBS;
|
|
|
|
EFI_RUNTIME_SERVICES *gRT;
|
2021-03-05 06:20:58 +03:00
|
|
|
EFI_HANDLE efi_image_handle;
|
2021-04-29 00:41:34 +03:00
|
|
|
EFI_MEMORY_DESCRIPTOR *efi_mmap = NULL;
|
|
|
|
UINTN efi_mmap_size = 0, efi_desc_size = 0, efi_desc_ver = 0;
|
2021-03-02 12:23:43 +03:00
|
|
|
#endif
|
|
|
|
|
2021-05-11 07:46:42 +03:00
|
|
|
bool verbose = false;
|
|
|
|
|
2020-11-09 14:31:47 +03:00
|
|
|
bool parse_resolution(int *width, int *height, int *bpp, const char *buf) {
|
|
|
|
int res[3] = {0};
|
|
|
|
|
|
|
|
const char *first = buf;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
const char *last;
|
|
|
|
int x = strtoui(first, &last, 10);
|
|
|
|
if (first == last)
|
|
|
|
break;
|
|
|
|
res[i] = x;
|
|
|
|
if (*last == 0)
|
|
|
|
break;
|
|
|
|
first = last + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res[0] == 0 || res[1] == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (res[2] == 0)
|
|
|
|
res[2] = 32;
|
|
|
|
|
|
|
|
*width = res[0], *height = res[1], *bpp = res[2];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-30 18:29:07 +03:00
|
|
|
// This integer sqrt implementation has been adapted from:
|
|
|
|
// https://stackoverflow.com/questions/1100090/looking-for-an-efficient-integer-square-root-algorithm-for-arm-thumb2
|
|
|
|
uint64_t sqrt(uint64_t a_nInput) {
|
|
|
|
uint64_t op = a_nInput;
|
|
|
|
uint64_t res = 0;
|
|
|
|
uint64_t one = (uint64_t)1 << 62;
|
|
|
|
|
|
|
|
// "one" starts at the highest power of four <= than the argument.
|
|
|
|
while (one > op) {
|
|
|
|
one >>= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (one != 0) {
|
|
|
|
if (op >= res + one) {
|
|
|
|
op = op - (res + one);
|
|
|
|
res = res + 2 * one;
|
|
|
|
}
|
|
|
|
res >>= 1;
|
|
|
|
one >>= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2021-03-05 06:20:58 +03:00
|
|
|
|
2021-07-17 09:19:03 +03:00
|
|
|
size_t get_trailing_zeros(uint64_t val) {
|
|
|
|
for (size_t i = 0; i < 64; i++) {
|
|
|
|
if ((val & 1) != 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
val >>= 1;
|
|
|
|
}
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#if uefi == 1
|
2021-03-05 06:20:58 +03:00
|
|
|
|
|
|
|
bool efi_boot_services_exited = false;
|
|
|
|
|
|
|
|
bool efi_exit_boot_services(void) {
|
2021-03-07 02:52:25 +03:00
|
|
|
EFI_STATUS status;
|
|
|
|
|
2021-03-05 06:20:58 +03:00
|
|
|
EFI_MEMORY_DESCRIPTOR tmp_mmap[1];
|
2021-07-06 08:42:35 +03:00
|
|
|
efi_mmap_size = sizeof(tmp_mmap);
|
|
|
|
UINTN mmap_key = 0;
|
2021-03-05 06:20:58 +03:00
|
|
|
|
|
|
|
uefi_call_wrapper(gBS->GetMemoryMap, 5,
|
2021-07-06 08:42:35 +03:00
|
|
|
&efi_mmap_size, tmp_mmap, &mmap_key, &efi_desc_size, &efi_desc_ver);
|
2021-03-05 06:20:58 +03:00
|
|
|
|
2021-07-06 08:42:35 +03:00
|
|
|
efi_mmap_size += 4096;
|
|
|
|
|
2021-07-07 01:44:37 +03:00
|
|
|
status = uefi_call_wrapper(gBS->FreePool, 1, efi_mmap);
|
|
|
|
if (status)
|
|
|
|
goto fail;
|
|
|
|
|
2021-07-06 08:42:35 +03:00
|
|
|
status = uefi_call_wrapper(gBS->AllocatePool, 3,
|
2021-07-20 14:35:43 +03:00
|
|
|
EfiLoaderData, efi_mmap_size, (void **)&efi_mmap);
|
2021-07-06 08:42:35 +03:00
|
|
|
if (status)
|
|
|
|
goto fail;
|
2021-03-07 02:52:25 +03:00
|
|
|
|
2021-07-06 08:42:35 +03:00
|
|
|
status = uefi_call_wrapper(gBS->GetMemoryMap, 5,
|
|
|
|
&efi_mmap_size, efi_mmap, &mmap_key, &efi_desc_size, &efi_desc_ver);
|
2021-03-07 02:52:25 +03:00
|
|
|
if (status)
|
2021-07-06 08:42:35 +03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
// Be gone, UEFI!
|
|
|
|
status = uefi_call_wrapper(gBS->ExitBootServices, 2, efi_image_handle, mmap_key);
|
2021-03-07 02:52:25 +03:00
|
|
|
|
|
|
|
asm volatile ("cli" ::: "memory");
|
2021-03-05 06:20:58 +03:00
|
|
|
|
2021-07-06 08:42:35 +03:00
|
|
|
if (status)
|
|
|
|
goto fail;
|
|
|
|
|
2021-03-10 03:23:44 +03:00
|
|
|
pmm_reclaim_uefi_mem();
|
|
|
|
|
2021-07-06 08:42:35 +03:00
|
|
|
// Go through new EFI memmap and free up bootloader entries
|
|
|
|
size_t entry_count = efi_mmap_size / efi_desc_size;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < entry_count; i++) {
|
|
|
|
EFI_MEMORY_DESCRIPTOR *entry = (void *)efi_mmap + i * efi_desc_size;
|
|
|
|
|
2021-07-06 23:45:16 +03:00
|
|
|
uint64_t base = entry->PhysicalStart;
|
|
|
|
uint64_t length = entry->NumberOfPages * 4096;
|
|
|
|
|
|
|
|
// Find for a match in the untouched memory map
|
|
|
|
for (size_t j = 0; j < untouched_memmap_entries; j++) {
|
|
|
|
if (untouched_memmap[j].type != MEMMAP_USABLE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (untouched_memmap[j].base == base && untouched_memmap[j].length == length) {
|
|
|
|
// It's a match!
|
|
|
|
entry->Type = EfiConventionalMemory;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-06 08:42:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 06:20:58 +03:00
|
|
|
efi_boot_services_exited = true;
|
|
|
|
|
2021-05-11 07:46:42 +03:00
|
|
|
printv("efi: Exited boot services.\n");
|
2021-03-05 06:20:58 +03:00
|
|
|
|
|
|
|
return true;
|
2021-07-06 08:42:35 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
panic("efi: Failed to exit boot services");
|
2021-03-05 06:20:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|