3 * Copyright (C) 2013 Jerry Hoemann <jerry.hoemann@hp.com>
6 * Application to allocate memory at EFI. Syntax of command
7 * mimics the EFI Boot Service "AllocatePages."
9 * See UEFI spec 2.3, Section 6.2.
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
35 Example to allocat 5 pages type BootCode at address 20000000 (hex)
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
46 AllocatPage(2,3,5,20000000)
49 Example to allocat 5 pages type BootCode at address 30000000 (hex)
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
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
94 #define MAX_NUM_PAGES 0x000000000F000000
95 #define MAX_ADDR ((1ULL << 46) - 1)
99 #define CHAR_SPACE L' '
104 argify(CHAR16 *buf, UINTN len, CHAR16 **argv)
114 /* len represents the number of bytes, not the number of 16 bytes chars */
118 * Here we use CHAR_NULL as the terminator rather than the length
119 * because it seems like the EFI shell return rather bogus values for it.
120 * Apparently, we are guaranteed to find the '\0' character in the buffer
121 * where the real input arguments stop, so we use it instead.
124 while (buf[i] == CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
126 if (buf[i] == CHAR_NULL || i == len) goto end;
131 while (buf[i] != CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
135 if (buf[i] == CHAR_NULL) goto end;
139 if (i == len) goto end;
143 if (j == MAX_ARGS-1) {
144 Print(L"too many arguments (%d) truncating\n", j);
155 efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
158 EFI_STATUS efi_status;
159 EFI_GUID LoadedImageProtocol = LOADED_IMAGE_PROTOCOL;
160 EFI_LOADED_IMAGE *info;
162 CHAR16 arglist[MAX_ARGS+1] = {0};
163 CHAR16 *argv[MAX_ARGS];
172 InitializeLib(image, systab);
174 efi_status = uefi_call_wrapper( BS->HandleProtocol, 3, image,
175 &LoadedImageProtocol, &info);
178 Print(L"AllocatePage: __AllocType__ __MemType__ __NumPages__ [__Addr__]\n");
179 Print(L"__AllocType__ {0,1,2} -- Any, MaxAddr, Addr\n");
180 Print(L"__MemType__ {0..13}, Reserved ==0, LCode==1, LData==2, BSCode==3, BSData==4, ...\n");
181 Print(L"__NumPages__ {0..%x}\n", MAX_NUM_PAGES);
182 Print(L"[__Addr__] 0... %llx\n", MAX_ADDR);
183 Print(L"All numbers in hex no leading 0x\n");
187 Print(L"%s\n", info->LoadOptions);
192 Print(L"Set up arglist\n");
194 CopyMem(arglist, info->LoadOptions, info->LoadOptionsSize);
196 Print(L"arglist = <%s>\n", arglist);
200 Print(L"Now try argify\n");
202 argc = argify(arglist, info->LoadOptionsSize, argv);
204 Print(L"argc = %d\n", argc);
208 for (c = 0; c < argc; c++ ) {
209 Print(L"argv[%d] = <%s>\n", c, argv[c]);
212 if ( (argc < 3) || (argc > 5) ) {
213 Print(L"Wrong argument count\n");
217 AllocType = xtoi(argv[1]);
218 MemType = xtoi(argv[2]);
219 NumPages = xtoi(argv[3]);
220 if ( argc == 5 ) Addr = xtoi(argv[4]);
222 if ( (AllocType < 0) || (AllocType > 2)) {
223 Print(L"Invalid AllocType\n");
226 if ( (MemType < 0) || (MemType > 13) ) {
227 Print(L"Invalid MemType\n");
230 if ( (NumPages < 0) || (NumPages > MAX_NUM_PAGES) ) {
231 Print(L"Inavlid NumPages\n");
234 if ( Addr > MAX_ADDR ) {
235 Print(L"Inavlid Address\n");
239 return EFI_INVALID_PARAMETER;
242 Print(L"AllocatPage(%d,%d,%d,%lx)\n", AllocType, MemType, NumPages, Addr);
244 efi_status = uefi_call_wrapper(BS->AllocatePages, 4, AllocType, MemType, NumPages, &Addr);
246 if ( EFI_ERROR(efi_status) ) {
247 Print(L"Allocate Pages Failed: %d\n", efi_status);