diff options
| author | Ada Christine <adachristine18@gmail.com> | 2024-03-03 08:44:42 +0000 |
|---|---|---|
| committer | Ada Christine <adachristine18@gmail.com> | 2024-03-03 08:44:42 +0000 |
| commit | f61da8be06fdfebc598cba783e78117319231140 (patch) | |
| tree | 0ef21f89e98a985ab5d985aaac1e032c868e8b81 /boot/efi/memory.c | |
| parent | 9681d6a6cbde12b00beab62683b8db98f680ba2b (diff) | |
build changes
Diffstat (limited to 'boot/efi/memory.c')
| -rw-r--r-- | boot/efi/memory.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/boot/efi/memory.c b/boot/efi/memory.c new file mode 100644 index 0000000..7fbb4a8 --- /dev/null +++ b/boot/efi/memory.c @@ -0,0 +1,83 @@ +#include <libc/string.h> + +#include <efi/services.h> +#include <efi/error.h> + +#include <stddef.h> + +#include "kjarna_efi.h" + +void *efi_alloc_pool(size_t size) +{ + void *buffer; + + efi_errno = gBS->AllocatePool( + EfiLoaderData, + size, + &buffer); + + if (EFI_ERROR(efi_errno)) + { + return nullptr; + } + + return buffer; +} + +void *efi_calloc_pool(size_t count, size_t size) +{ + void *buffer = efi_alloc_pool(count * size); + + if (buffer != nullptr) + { + memset(buffer, 0, count * size); + } + + return buffer; +} + +void efi_free_pool(void *buffer) +{ + efi_errno = gBS->FreePool(buffer); +} + +#define KjarnaData EFIX_OS_MEMORY_TYPE(1) + +void *efi_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) +{ + (void)prot; + (void)flags; + (void)fd; + (void)offset; + + EFI_ALLOCATE_TYPE alloc_type = AllocateAnyPages; + EFI_MEMORY_TYPE memory_type = KjarnaData; + + void *buffer; + + if (addr != nullptr) + { + alloc_type = AllocateAddress; + } + + efi_errno = gBS->AllocatePages( + alloc_type, + memory_type, + EFI_SIZE_TO_PAGES(length), + (uintptr_t *)&buffer); + + if (EFI_ERROR(efi_errno)) + { + return nullptr; + } + + return buffer; +} + +int efi_munmap(void *addr, size_t length) +{ + efi_errno = gBS->FreePages((uintptr_t)addr, EFI_SIZE_TO_PAGES(length)); + + return !EFI_ERROR(efi_errno) ? 0 : -1; +} + |
