-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmalloc.c
More file actions
144 lines (117 loc) · 4.06 KB
/
malloc.c
File metadata and controls
144 lines (117 loc) · 4.06 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2017 lambdadroid
#include "malloc.h"
#include <efilib.h>
#define EFI_PAGE_ALIGN(a) (((a) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK)
static EFI_STATUS load_memory_map(EFI_MEMORY_DESCRIPTOR **buf, UINTN *map_size, UINTN *desc_size) {
EFI_STATUS err;
UINTN map_key;
UINT32 desc_version;
*map_size = sizeof(**buf) * 32;
get_map:
err = uefi_call_wrapper(BS->AllocatePool, 3, EfiLoaderData, *map_size, (VOID**) buf);
if (err) {
return err;
}
err = uefi_call_wrapper(BS->GetMemoryMap, 5, map_size, *buf, &map_key, desc_size, &desc_version);
if (err) {
FreePool(*buf);
if (err == EFI_BUFFER_TOO_SMALL) {
// We might create a new descriptor by allocating memory
*map_size += sizeof(**buf);
goto get_map;
}
}
return err;
}
/*
* The following methods were originally based on parts of the Linux kernel: (GPL-2.0)
* drivers/firmware/efi/libstub/efi-stub-helper.c (efi_high_alloc/efi_low_alloc)
*
* Simplified quite a bit for use in android-efi.
*/
EFI_STATUS malloc_high(UINTN size, EFI_PHYSICAL_ADDRESS *addr) {
EFI_MEMORY_DESCRIPTOR *buf;
UINTN map_size, desc_size;
EFI_STATUS err = load_memory_map(&buf, &map_size, &desc_size);
if (err) {
return err;
}
// Align size to EFI_PAGE_SIZE
size = EFI_PAGE_ALIGN(size);
UINTN nr_pages = size / EFI_PAGE_SIZE;
// Align max to EFI_PAGE_SIZE (round down) and subtract aligned size
EFI_PHYSICAL_ADDRESS max = (*addr & ~EFI_PAGE_MASK) - size;
err = EFI_NOT_FOUND;
for (UINTN d = (UINTN) buf + map_size; d >= (UINTN) buf; d -= desc_size) {
EFI_MEMORY_DESCRIPTOR *desc = (EFI_MEMORY_DESCRIPTOR*) d;
if (desc->Type != EfiConventionalMemory || desc->NumberOfPages < nr_pages) {
continue;
}
*addr = desc->PhysicalStart + (desc->NumberOfPages * EFI_PAGE_SIZE) - size;
if (*addr > max) {
*addr = max;
}
// Make sure we're still in the memory region
if (*addr && *addr >= desc->PhysicalStart) {
err = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAddress, EfiLoaderData, nr_pages, addr);
if (err) {
Print(L"Cannot allocate at %d: %r\n", *addr, err);
}
if (err == EFI_SUCCESS) {
goto done;
}
}
}
done:
FreePool(buf);
return err;
}
EFI_STATUS malloc_low(UINTN size, UINTN align, EFI_PHYSICAL_ADDRESS *addr) {
EFI_MEMORY_DESCRIPTOR *buf;
UINTN map_size, desc_size;
EFI_STATUS err = load_memory_map(&buf, &map_size, &desc_size);
if (err) {
return err;
}
UINTN align_mask;
if (align < EFI_PAGE_SIZE) {
// Need to align to at least the EFI page size
align = EFI_PAGE_SIZE;
align_mask = EFI_PAGE_MASK;
} else {
align_mask = align - 1;
}
// Align size to EFI_PAGE_SIZE
size = EFI_PAGE_ALIGN(size);
UINTN nr_pages = size / EFI_PAGE_SIZE;
err = EFI_NOT_FOUND;
for (UINTN d = (UINTN) buf, map_end = d + map_size; d < map_end; d += desc_size) {
EFI_MEMORY_DESCRIPTOR *desc = (EFI_MEMORY_DESCRIPTOR*) d;
if (desc->Type != EfiConventionalMemory || desc->NumberOfPages < nr_pages) {
continue;
}
EFI_PHYSICAL_ADDRESS max = desc->PhysicalStart + (desc->NumberOfPages * EFI_PAGE_SIZE) - size;
*addr = desc->PhysicalStart;
// Avoid allocating at 0x0
if (*addr == 0x0) {
*addr = align;
} else {
// Align address
*addr = (*addr + align_mask) & ~align_mask;
}
// Make sure we're still in the memory region
if (*addr <= max) {
err = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAddress, EfiLoaderData, nr_pages, addr);
if (err) {
Print(L"Cannot allocate at %d: %r\n", *addr, err);
}
if (err == EFI_SUCCESS) {
goto done;
}
}
}
done:
FreePool(buf);
return err;
}