summaryrefslogtreecommitdiff
path: root/kjarna/efi/memory.c
blob: 7fbb4a8c307cf3e53869e4083ef766437169e865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
}