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