-
Notifications
You must be signed in to change notification settings - Fork 39
/
MemResv.c
97 lines (85 loc) · 2.92 KB
/
MemResv.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
/* Time-stamp: <2017-09-23 00:06:58 andreiw>
* Copyright (C) 2016 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/UefiBootServicesTableLib.h>
#include <Pi/PiDxeCis.h>
#include <Library/UtilsLib.h>
EFI_STATUS
EFIAPI
UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
UINTN Argc;
CHAR16 **Argv;
UINTN RangeStart;
UINTN RangeLength;
EFI_STATUS Status;
EFI_MEMORY_TYPE type;
EFI_DXE_SERVICES *DS = NULL;
EfiGetSystemConfigurationTable(&gEfiDxeServicesTableGuid, (VOID **) &DS);
if (DS == NULL) {
Print(L"This program requires EDK2 DXE Services\n");
return EFI_ABORTED;
}
Status = GetShellArgcArgv(ImageHandle, &Argc, &Argv);
if (Status != EFI_SUCCESS || Argc < 1) {
Print(L"This program requires Microsoft Windows.\n"
"Just kidding...only the UEFI Shell!\n");
return EFI_ABORTED;
}
if (Argc < 3) {
Print(L"Usage: %s range-start range-pages [mmio]\n", Argv[0]);
return EFI_INVALID_PARAMETER;
}
RangeStart = StrHexToUintn(Argv[1]);
RangeLength = StrHexToUintn(Argv[2]) << EFI_PAGE_SHIFT;
if (RangeLength == 0 ||
(RangeStart + RangeLength) < RangeStart) {
Print(L"Invalid range passed\n");
return EFI_INVALID_PARAMETER;
}
type = EfiReservedMemoryType;
if (Argc == 4) {
if (!StriCmp(Argv[3], L"mmio")) {
type = EfiMemoryMappedIO;
} else {
Print(L"Warning: Unknown range type, assuming reserved\n");
}
}
DS->RemoveMemorySpace(RangeStart, RangeLength);
//
// This seems like the only reasonable way to ensure
// that a range is added if it is absent. Adding
// EfiGcdMemoryTypeMemoryMappedIo results in
// an EFI_NOT_FOUND error for AllocatePages
// and nothing is reported in the memory map.
//
Status = DS->AddMemorySpace(EfiGcdMemoryTypeSystemMemory,
RangeStart,
RangeLength,
EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
);
if (Status != EFI_SUCCESS) {
Print(L"Warning: couldn't add reserved memory space 0x%lx-0x%lx: %r\n",
RangeStart, RangeStart + RangeLength, Status);
}
Status = gBS->AllocatePages(AllocateAddress, type,
EFI_SIZE_TO_PAGES(RangeLength), &RangeStart);
if (Status != EFI_SUCCESS) {
Print(L"Warning: couldn't allocate reserved memory space 0x%lx-0x%lx: %r\n",
RangeStart, RangeStart + RangeLength, Status);
}
return Status;
}