]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/gnu-efi/lib/contrib/apps/AllocPages.c
update
[l4.git] / l4 / pkg / gnu-efi / lib / contrib / apps / AllocPages.c
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
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 #define MAX_ARGS 256
98
99 #define CHAR_SPACE L' '
100
101 #define DEBUG 0
102
103 INTN
104 argify(CHAR16 *buf, UINTN len, CHAR16 **argv)   
105 {
106
107         UINTN     i=0, j=0;
108         CHAR16   *p = buf;
109         
110         if (buf == 0) { 
111                 argv[0] = NULL;
112                 return 0;
113         }
114         /* len represents the number of bytes, not the number of 16 bytes chars */
115         len = len >> 1;
116
117         /*
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.
122          */
123         for(;;) {
124                 while (buf[i] == CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
125
126                 if (buf[i] == CHAR_NULL || i == len) goto end;
127
128                 p = buf+i;
129                 i++;
130
131                 while (buf[i] != CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
132
133                 argv[j++] = p;
134
135                 if (buf[i] == CHAR_NULL) goto end;
136
137                 buf[i]  = CHAR_NULL;
138
139                 if (i == len)  goto end;
140
141                 i++;
142
143                 if (j == MAX_ARGS-1) {
144                         Print(L"too many arguments (%d) truncating\n", j);
145                         goto end;
146                 }
147         }
148 end:
149         argv[j] = NULL;
150         return j;
151 }
152
153
154 EFI_STATUS
155 efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
156 {
157
158         EFI_STATUS efi_status;
159         EFI_GUID LoadedImageProtocol = LOADED_IMAGE_PROTOCOL;
160         EFI_LOADED_IMAGE *info;
161
162         CHAR16 arglist[MAX_ARGS+1] = {0};
163         CHAR16 *argv[MAX_ARGS];
164         INTN argc = 0;
165         INTN err = 0;
166
167         INTN AllocType = -1;
168         INTN MemType = -1;
169         INTN NumPages = -1;
170         UINTN Addr = 0;
171
172         InitializeLib(image, systab);
173
174         efi_status = uefi_call_wrapper( BS->HandleProtocol, 3, image,
175                 &LoadedImageProtocol, &info);
176
177
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");
184         Print(L"\n");
185
186 #if DEBUG
187         Print(L"%s\n", info->LoadOptions);
188 #endif
189
190
191 #if DEBUG
192         Print(L"Set up arglist\n");
193 #endif
194         CopyMem(arglist, info->LoadOptions, info->LoadOptionsSize);
195 #if DEBUG
196         Print(L"arglist = <%s>\n", arglist);
197 #endif
198         
199 #if DEBUG
200         Print(L"Now try argify\n");
201 #endif
202         argc = argify(arglist, info->LoadOptionsSize, argv);
203 #if DEBUG
204         Print(L"argc = %d\n", argc);
205 #endif
206
207 #if DEBUG
208         for (c = 0;  c < argc;  c++ ) {
209                 Print(L"argv[%d] = <%s>\n", c, argv[c]);
210         }
211 #endif
212         if ( (argc < 3) || (argc > 5) ) {
213                 Print(L"Wrong argument count\n");
214                 return EFI_SUCCESS;
215         }
216
217         AllocType = xtoi(argv[1]);
218         MemType   = xtoi(argv[2]);
219         NumPages  = xtoi(argv[3]);
220         if ( argc == 5 ) Addr = xtoi(argv[4]);
221
222         if ( (AllocType < 0) || (AllocType > 2)) {
223                 Print(L"Invalid AllocType\n");
224                 err++;
225         }
226         if ( (MemType < 0) || (MemType > 13) ) {
227                 Print(L"Invalid MemType\n");
228                 err++;
229         }
230         if ( (NumPages < 0) || (NumPages > MAX_NUM_PAGES) ) {
231                 Print(L"Inavlid NumPages\n");
232                 err++;
233         }
234         if ( Addr > MAX_ADDR ) {
235                 Print(L"Inavlid Address\n");
236                 err++;
237         }
238         if ( err ) {
239                 return EFI_INVALID_PARAMETER;
240         }
241
242         Print(L"AllocatPage(%d,%d,%d,%lx)\n", AllocType, MemType, NumPages, Addr);
243
244         efi_status = uefi_call_wrapper(BS->AllocatePages, 4, AllocType, MemType, NumPages, &Addr);
245
246         if ( EFI_ERROR(efi_status) ) {
247                 Print(L"Allocate Pages Failed: %d\n", efi_status);
248                 return efi_status;
249         }
250
251         return EFI_SUCCESS;
252 }