| 1 |
|
| 2 |
/* |
| 3 |
* Copyright (C) 2013 Jerry Hoemann <[email protected]> |
| 4 |
* |
| 5 |
* |
| 6 |
* Application to allocate memory at EFI. Syntax of command |
| 7 |
* mimics the EFI Boot Service "AllocatePages." |
| 8 |
* |
| 9 |
* See UEFI spec 2.3, Section 6.2. |
| 10 |
* |
| 11 |
* |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
FS1:\> memmap |
| 17 |
Type Start End #pages Attributes |
| 18 |
BS_Code 0000000000000000-0000000000000FFF 0000000000000001 000000000000000F |
| 19 |
Available 0000000000001000-000000000008DFFF 000000000000008D 000000000000000F |
| 20 |
Reserved 000000000008E000-000000000008FFFF 0000000000000002 000000000000000F |
| 21 |
Available 0000000000090000-000000000009FFFF 0000000000000010 000000000000000F |
| 22 |
Available 0000000000100000-000000000FFFFFFF 000000000000FF00 000000000000000F |
| 23 |
BS_Code 0000000010000000-0000000010061FFF 0000000000000062 000000000000000F |
| 24 |
Available 0000000010062000-000000005CDFFFFF 000000000004CD9E 000000000000000F |
| 25 |
ACPI_NVS 000000005CE00000-000000005DDFFFFF 0000000000001000 000000000000000F |
| 26 |
BS_Data 000000005DE00000-000000005DFFFFFF 0000000000000200 000000000000000F |
| 27 |
Available 000000005E000000-000000005EF1CFFF 0000000000000F1D 000000000000000F |
| 28 |
BS_Data 000000005EF1D000-00000000709FBFFF 0000000000011ADF 000000000000000F |
| 29 |
Available 00000000709FC000-00000000710E3FFF 00000000000006E8 000000000000000F |
| 30 |
LoaderCode 00000000710E4000-00000000711FEFFF 000000000000011B 000000000000000F |
| 31 |
Available 00000000711FF000-0000000071901FFF 0000000000000703 000000000000000F |
| 32 |
BS_Code 0000000071902000-00000000721FEFFF 00000000000008FD 000000000000000F |
| 33 |
|
| 34 |
|
| 35 |
Example to allocat 5 pages type BootCode at address 20000000 (hex) |
| 36 |
|
| 37 |
|
| 38 |
FS1:\> AllocPages.efi 2 3 5 20000000 |
| 39 |
AllocatePage: __AllocType__ __MemType__ __NumPages__ [__Addr__] |
| 40 |
__AllocType__ {0,1,2} -- Any, MaxAddr, Addr |
| 41 |
__MemType__ {0..13}, Reserved ==0, LCode==1, LData==2, BSCode==3, BSData==4, ... |
| 42 |
__NumPages__ {0..F000000} |
| 43 |
[__Addr__] 0... 3FFFFFFFFFFF |
| 44 |
All numbers in hex no leading 0x |
| 45 |
|
| 46 |
AllocatPage(2,3,5,20000000) |
| 47 |
|
| 48 |
|
| 49 |
Example to allocat 5 pages type BootCode at address 30000000 (hex) |
| 50 |
|
| 51 |
|
| 52 |
FS1:\> AllocPages.efi 2 3 5 30000000 |
| 53 |
AllocatePage: __AllocType__ __MemType__ __NumPages__ [__Addr__] |
| 54 |
__AllocType__ {0,1,2} -- Any, MaxAddr, Addr |
| 55 |
__MemType__ {0..13}, Reserved ==0, LCode==1, LData==2, BSCode==3, BSData==4, ... |
| 56 |
__NumPages__ {0..F000000} |
| 57 |
[__Addr__] 0... 3FFFFFFFFFFF |
| 58 |
All numbers in hex no leading 0x |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
FS1:\> memmap |
| 63 |
Type Start End #pages Attributes |
| 64 |
BS_Code 0000000000000000-0000000000000FFF 0000000000000001 000000000000000F |
| 65 |
Available 0000000000001000-000000000008DFFF 000000000000008D 000000000000000F |
| 66 |
Reserved 000000000008E000-000000000008FFFF 0000000000000002 000000000000000F |
| 67 |
Available 0000000000090000-000000000009FFFF 0000000000000010 000000000000000F |
| 68 |
Available 0000000000100000-000000000FFFFFFF 000000000000FF00 000000000000000F |
| 69 |
BS_Code 0000000010000000-0000000010061FFF 0000000000000062 000000000000000F |
| 70 |
Available 0000000010062000-000000001FFFFFFF 000000000000FF9E 000000000000000F |
| 71 |
BS_Code 0000000020000000-0000000020004FFF 0000000000000005 000000000000000F |
| 72 |
Available 0000000020005000-000000002FFFFFFF 000000000000FFFB 000000000000000F |
| 73 |
BS_Code 0000000030000000-0000000030004FFF 0000000000000005 000000000000000F |
| 74 |
Available 0000000030005000-000000005CDFFFFF 000000000002CDFB 000000000000000F |
| 75 |
ACPI_NVS 000000005CE00000-000000005DDFFFFF 0000000000001000 000000000000000F |
| 76 |
BS_Data 000000005DE00000-000000005DFFFFFF 0000000000000200 000000000000000F |
| 77 |
Available 000000005E000000-000000005EF1CFFF 0000000000000F1D 000000000000000F |
| 78 |
BS_Data 000000005EF1D000-00000000709FBFFF 0000000000011ADF 000000000000000F |
| 79 |
Available 00000000709FC000-00000000710E3FFF 00000000000006E8 000000000000000F |
| 80 |
LoaderCode 00000000710E4000-00000000711FEFFF 000000000000011B 000000000000000F |
| 81 |
Available 00000000711FF000-0000000071901FFF 0000000000000703 000000000000000F |
| 82 |
BS_Code 0000000071902000-00000000721FEFFF 00000000000008FD 000000000000000F |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
*/ |
| 89 |
|
| 90 |
#include <efi.h> |
| 91 |
#include <efilib.h> |
| 92 |
|
| 93 |
|
| 94 |
#define MAX_NUM_PAGES 0x000000000F000000 |
| 95 |
#define MAX_ADDR ((1ULL << 46) - 1) |
| 96 |
|
| 97 |
|
| 98 |
#ifdef DEBUG |
| 99 |
#undef DEBUG |
| 100 |
#endif |
| 101 |
#define DEBUG 0 |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
EFI_STATUS |
| 106 |
efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab) |
| 107 |
{ |
| 108 |
|
| 109 |
EFI_STATUS efi_status; |
| 110 |
CHAR16 **argv; |
| 111 |
INTN argc; |
| 112 |
INTN err = 0; |
| 113 |
#if DEBUG |
| 114 |
INTN c = 0; |
| 115 |
#endif |
| 116 |
INTN AllocType = -1; |
| 117 |
INTN MemType = -1; |
| 118 |
INTN NumPages = -1; |
| 119 |
EFI_PHYSICAL_ADDRESS Addr = 0; |
| 120 |
|
| 121 |
InitializeLib(image, systab); |
| 122 |
|
| 123 |
Print(L"AllocatePage: __AllocType__ __MemType__ __NumPages__ [__Addr__]\n"); |
| 124 |
Print(L"__AllocType__ {0,1,2} -- Any, MaxAddr, Addr\n"); |
| 125 |
Print(L"__MemType__ {0..13}, Reserved ==0, LCode==1, LData==2, BSCode==3, BSData==4, ...\n"); |
| 126 |
Print(L"__NumPages__ {0..%x}\n", MAX_NUM_PAGES); |
| 127 |
Print(L"[__Addr__] 0... %llx\n", MAX_ADDR); |
| 128 |
Print(L"All numbers in hex no leading 0x\n"); |
| 129 |
Print(L"\n"); |
| 130 |
|
| 131 |
#if DEBUG |
| 132 |
Print(L"Now get argc/argv\n"); |
| 133 |
#endif |
| 134 |
argc = GetShellArgcArgv(image, &argv); |
| 135 |
#if DEBUG |
| 136 |
Print(L"argc = %d\n", argc); |
| 137 |
#endif |
| 138 |
|
| 139 |
#if DEBUG |
| 140 |
for (c = 0; c < argc; c++ ) { |
| 141 |
Print(L"argv[%d] = <%s>\n", c, argv[c]); |
| 142 |
} |
| 143 |
#endif |
| 144 |
if ( (argc < 4) || (argc > 5) ) { |
| 145 |
Print(L"Wrong argument count\n"); |
| 146 |
return EFI_SUCCESS; |
| 147 |
} |
| 148 |
|
| 149 |
AllocType = xtoi(argv[1]); |
| 150 |
MemType = xtoi(argv[2]); |
| 151 |
NumPages = xtoi(argv[3]); |
| 152 |
if ( argc == 5 ) Addr = xtoi(argv[4]); |
| 153 |
|
| 154 |
if ( (AllocType < 0) || (AllocType > 2)) { |
| 155 |
Print(L"Invalid AllocType\n"); |
| 156 |
err++; |
| 157 |
} |
| 158 |
if ( (MemType < 0) || (MemType > 13) ) { |
| 159 |
Print(L"Invalid MemType\n"); |
| 160 |
err++; |
| 161 |
} |
| 162 |
if ( (NumPages < 0) || (NumPages > MAX_NUM_PAGES) ) { |
| 163 |
Print(L"Inavlid NumPages\n"); |
| 164 |
err++; |
| 165 |
} |
| 166 |
if ( Addr > MAX_ADDR ) { |
| 167 |
Print(L"Inavlid Address\n"); |
| 168 |
err++; |
| 169 |
} |
| 170 |
if ( err ) { |
| 171 |
return EFI_INVALID_PARAMETER; |
| 172 |
} |
| 173 |
|
| 174 |
Print(L"AllocatPage(%d,%d,%d,%lx)\n", AllocType, MemType, NumPages, Addr); |
| 175 |
|
| 176 |
efi_status = uefi_call_wrapper(BS->AllocatePages, 4, AllocType, MemType, NumPages, &Addr); |
| 177 |
|
| 178 |
if ( EFI_ERROR(efi_status) ) { |
| 179 |
Print(L"Allocate Pages Failed: %d\n", efi_status); |
| 180 |
return efi_status; |
| 181 |
} |
| 182 |
|
| 183 |
return EFI_SUCCESS; |
| 184 |
} |