-
Notifications
You must be signed in to change notification settings - Fork 39
/
RangeCheck.c
185 lines (156 loc) · 4.75 KB
/
RangeCheck.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (C) 2017 Andrei Evgenievich Warkentin
*
* This program and the accompanying materials
* are licensed and made available under the terms and conditions of the BSD License
* which accompanies this distribution. The full text of the license may be found at
* http://opensource.org/licenses/bsd-license.php
*
* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/SortLib.h>
#include <Library/UtilsLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
STATIC INTN EFIAPI
MemoryMapSort (
IN CONST VOID *Buffer1,
IN CONST VOID *Buffer2
)
{
CONST EFI_MEMORY_DESCRIPTOR *D1 = Buffer1;
CONST EFI_MEMORY_DESCRIPTOR *D2 = Buffer2;
if (D1->PhysicalStart < D2->PhysicalStart) {
return -1;
} else if (D1->PhysicalStart == D2->PhysicalStart) {
return 0;
} else {
return 1;
}
}
VOID
CleanRangeCheckContext (
IN OUT RANGE_CHECK_CONTEXT *Context
)
{
if (!Context->Enabled) {
return;
}
if (Context->MapPages != 0) {
FreePages(Context->Map, Context->MapPages);
}
Context->Enabled = FALSE;
Context->WarnIfNotFound = FALSE;
Context->MapSize = 0;
Context->MapPages = 0;
Context->DescriptorSize = 0;
Context->Map = NULL;
}
EFI_STATUS
InitRangeCheckContext (
IN BOOLEAN Enabled,
IN BOOLEAN WarnIfNotFound,
OUT RANGE_CHECK_CONTEXT *Context
)
{
UINTN MapKey;
EFI_STATUS Status;
UINT32 DescriptorVersion;
Context->Enabled = Enabled;
Context->WarnIfNotFound = WarnIfNotFound;
Context->MapSize = 0;
Context->MapPages = 0;
Context->DescriptorSize = 0;
Context->Map = NULL;
if (!Enabled) {
return EFI_SUCCESS;
}
Status = gBS->GetMemoryMap(&Context->MapSize, NULL, &MapKey,
&Context->DescriptorSize, &DescriptorVersion);
if (Status != EFI_BUFFER_TOO_SMALL) {
Print(L"%a: gBS->GetMemoryMap failed to size: %r\n",
__FUNCTION__, Status);
return EFI_UNSUPPORTED;
}
do {
//
// The UEFI specification advises to allocate more memory for
// the MemoryMap buffer between successive calls to GetMemoryMap(),
// since allocation of the new buffer may potentially increase
// memory map size.
//
Context->MapPages = EFI_SIZE_TO_PAGES(Context->MapSize) + 1;
Context->Map = AllocatePages(Context->MapPages);
if (Context->Map == NULL) {
Print(L"%a: AllocatePages failed\n", __FUNCTION__);
return EFI_OUT_OF_RESOURCES;
}
Status = gBS->GetMemoryMap(&Context->MapSize, Context->Map, &MapKey,
&Context->DescriptorSize,
&DescriptorVersion);
if (!EFI_ERROR(Status)) {
break;
}
if (EFI_ERROR(Status)) {
CleanRangeCheckContext(Context);
if (Status != EFI_BUFFER_TOO_SMALL) {
Print(L"%a: gBS->GetMemoryMap failed: %r\n", __FUNCTION__,
Status);
return Status;
}
}
} while (1);
PerformQuickSort(Context->Map, Context->MapSize / Context->DescriptorSize,
Context->DescriptorSize, MemoryMapSort);
return EFI_SUCCESS;
}
EFI_STATUS
RangeIsMapped (
IN RANGE_CHECK_CONTEXT *Context,
IN UINTN RangeStart,
IN UINTN RangeLength
)
{
UINTN Index;
UINTN RangeNext;
UINTN RangePages;
EFI_MEMORY_DESCRIPTOR *Next;
if (!Context->Enabled) {
return EFI_SUCCESS;
}
if (RangeLength == 0) {
Print(L"0x%lx-0x%lx is zero length\n", RangeStart, RangeStart);
return EFI_INVALID_PARAMETER;
}
RangePages = EFI_SIZE_TO_PAGES(RangeLength);
for (RangeNext = RangeStart, Next = Context->Map, Index = 0;
Index < (Context->MapSize / Context->DescriptorSize) &&
RangePages != 0;
Index++, Next = (VOID *)((UINTN)Next + Context->DescriptorSize)) {
UINTN NextLast = Next->PhysicalStart - 1 +
(Next->NumberOfPages * EFI_PAGE_SIZE);
if (RangeNext < Next->PhysicalStart) {
break;
}
if (RangeNext >= Next->PhysicalStart &&
RangeNext <= NextLast) {
UINTN RemPages = EFI_SIZE_TO_PAGES(NextLast - RangeNext + 1);
RemPages = MIN(RemPages, RangePages);
RangePages -= RemPages;
RangeNext += RemPages * EFI_PAGE_SIZE;
}
}
if (RangePages != 0) {
if (Context->WarnIfNotFound) {
Print(L"0x%lx-0x%lx not in memory map (starting at 0x%lx)\n",
RangeStart,
RangeLength - 1 + RangeStart,
RangeNext);
}
return EFI_NOT_FOUND;
}
return EFI_SUCCESS;
}