]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenPage/GenPage.c
Sync BaseTools Branch (version r2271) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / C / GenPage / GenPage.c
1 /** @file
2
3 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13 GenPage.c
14
15 Abstract:
16 Pre-Create a 4G page table (2M pages).
17 It's used in DUET x64 build needed to enter LongMode.
18
19 Create 4G page table (2M pages)
20
21 Linear Address
22 63 48 47 39 38 30 29 21 20 0
23 +--------+-------+---------------+-----------+-----------------------------+
24 PML4 Directory-Ptr Directory Offset
25
26 Paging-Structures :=
27 PML4
28 (
29 Directory-Ptr Directory {512}
30 ) {4}
31 **/
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "VirtualMemory.h"
37 #include "EfiUtilityMsgs.h"
38 #include "ParseInf.h"
39
40 #define EFI_PAGE_BASE_OFFSET_IN_LDR 0x70000
41 #define EFI_PAGE_BASE_ADDRESS (EFI_PAGE_BASE_OFFSET_IN_LDR + 0x20000)
42
43 UINT32 gPageTableBaseAddress = EFI_PAGE_BASE_ADDRESS;
44 UINT32 gPageTableOffsetInFile = EFI_PAGE_BASE_OFFSET_IN_LDR;
45
46 #define EFI_MAX_ENTRY_NUM 512
47
48 #define EFI_PML4_ENTRY_NUM 1
49 #define EFI_PDPTE_ENTRY_NUM 4
50 #define EFI_PDE_ENTRY_NUM EFI_MAX_ENTRY_NUM
51
52 #define EFI_PML4_PAGE_NUM 1
53 #define EFI_PDPTE_PAGE_NUM EFI_PML4_ENTRY_NUM
54 #define EFI_PDE_PAGE_NUM (EFI_PML4_ENTRY_NUM * EFI_PDPTE_ENTRY_NUM)
55
56 #define EFI_PAGE_NUMBER (EFI_PML4_PAGE_NUM + EFI_PDPTE_PAGE_NUM + EFI_PDE_PAGE_NUM)
57
58 #define EFI_SIZE_OF_PAGE 0x1000
59 #define EFI_PAGE_SIZE_2M 0x200000
60
61 #define CONVERT_BIN_PAGE_ADDRESS(a) ((UINT8 *) a - PageTable + gPageTableBaseAddress)
62
63 //
64 // Utility Name
65 //
66 #define UTILITY_NAME "GenPage"
67
68 //
69 // Utility version information
70 //
71 #define UTILITY_MAJOR_VERSION 0
72 #define UTILITY_MINOR_VERSION 1
73
74 void
75 Version (
76 void
77 )
78 /*++
79
80 Routine Description:
81
82 Displays the standard utility information to SDTOUT
83
84 Arguments:
85
86 None
87
88 Returns:
89
90 None
91
92 --*/
93 {
94 printf ("%s v%d.%d -Utility to generate the EfiLoader image containing page table.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
95 printf ("Copyright (c) 2008 - 2011 Intel Corporation. All rights reserved.\n");
96 }
97
98 VOID
99 Usage (
100 void
101 )
102 {
103 Version();
104 printf ("\nUsage: \n\
105 GenPage\n\
106 -o, --output Filename\n\
107 The file that contains both non-page table part and\n\
108 page table\n\
109 [-b, --baseaddr baseaddress]\n\
110 The page table location\n\
111 [-f, --offset offset]\n\
112 The position that the page table will appear in the\n\
113 output file\n\
114 [-v, --verbose] Turn on verbose output with informational messages\n\
115 printed\n\
116 [--version] Print version and copyright of this program and exit\n\
117 [-q, --quiet] Disable all messages except unrecoverable errors\n\
118 [-d, --debug[#]] Enable debug messages, at level #\n\
119 [-h, --help] Print copyright, version and usage of this program\n\
120 and exit\n\
121 EfiLoaderImageName\n");
122
123 }
124
125 void *
126 CreateIdentityMappingPageTables (
127 void
128 )
129 /*++
130
131 Routine Description:
132 To create 4G PAE 2M pagetable
133
134 Return:
135 void * - buffer containing created pagetable
136
137 --*/
138 {
139 UINT64 PageAddress;
140 UINT8 *PageTable;
141 UINT8 *PageTablePtr;
142 int PML4Index;
143 int PDPTEIndex;
144 int PDEIndex;
145 X64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *PageMapLevel4Entry;
146 X64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *PageDirectoryPointerEntry;
147 X64_PAGE_TABLE_ENTRY_2M *PageDirectoryEntry2MB;
148
149 PageTable = (void *)malloc (EFI_PAGE_NUMBER * EFI_SIZE_OF_PAGE);
150 memset (PageTable, 0, (EFI_PAGE_NUMBER * EFI_SIZE_OF_PAGE));
151 PageTablePtr = PageTable;
152
153 PageAddress = 0;
154
155 //
156 // Page Table structure 3 level 2MB.
157 //
158 // Page-Map-Level-4-Table : bits 47-39
159 // Page-Directory-Pointer-Table : bits 38-30
160 //
161 // Page Table 2MB : Page-Directory(2M) : bits 29-21
162 //
163 //
164
165 PageMapLevel4Entry = (X64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *)PageTablePtr;
166
167 for (PML4Index = 0; PML4Index < EFI_PML4_ENTRY_NUM; PML4Index++, PageMapLevel4Entry++) {
168 //
169 // Each Page-Map-Level-4-Table Entry points to the base address of a Page-Directory-Pointer-Table Entry
170 //
171 PageTablePtr += EFI_SIZE_OF_PAGE;
172 PageDirectoryPointerEntry = (X64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *)PageTablePtr;
173
174 //
175 // Make a Page-Map-Level-4-Table Entry
176 //
177 PageMapLevel4Entry->Uint64 = (UINT64)(UINT32)(CONVERT_BIN_PAGE_ADDRESS (PageDirectoryPointerEntry));
178 PageMapLevel4Entry->Bits.ReadWrite = 1;
179 PageMapLevel4Entry->Bits.Present = 1;
180
181 for (PDPTEIndex = 0; PDPTEIndex < EFI_PDPTE_ENTRY_NUM; PDPTEIndex++, PageDirectoryPointerEntry++) {
182 //
183 // Each Page-Directory-Pointer-Table Entry points to the base address of a Page-Directory Entry
184 //
185 PageTablePtr += EFI_SIZE_OF_PAGE;
186 PageDirectoryEntry2MB = (X64_PAGE_TABLE_ENTRY_2M *)PageTablePtr;
187
188 //
189 // Make a Page-Directory-Pointer-Table Entry
190 //
191 PageDirectoryPointerEntry->Uint64 = (UINT64)(UINT32)(CONVERT_BIN_PAGE_ADDRESS (PageDirectoryEntry2MB));
192 PageDirectoryPointerEntry->Bits.ReadWrite = 1;
193 PageDirectoryPointerEntry->Bits.Present = 1;
194
195 for (PDEIndex = 0; PDEIndex < EFI_PDE_ENTRY_NUM; PDEIndex++, PageDirectoryEntry2MB++) {
196 //
197 // Make a Page-Directory Entry
198 //
199 PageDirectoryEntry2MB->Uint64 = (UINT64)PageAddress;
200 PageDirectoryEntry2MB->Bits.ReadWrite = 1;
201 PageDirectoryEntry2MB->Bits.Present = 1;
202 PageDirectoryEntry2MB->Bits.MustBe1 = 1;
203
204 PageAddress += EFI_PAGE_SIZE_2M;
205 }
206 }
207 }
208
209 return PageTable;
210 }
211
212 INT32
213 GenBinPage (
214 void *BaseMemory,
215 char *NoPageFileName,
216 char *PageFileName
217 )
218 /*++
219
220 Routine Description:
221 Write the buffer containing page table to file at a specified offset.
222 Here the offset is defined as EFI_PAGE_BASE_OFFSET_IN_LDR.
223
224 Arguments:
225 BaseMemory - buffer containing page table
226 NoPageFileName - file to write page table
227 PageFileName - file save to after writing
228
229 return:
230 0 : successful
231 -1 : failed
232
233 --*/
234 {
235 FILE *PageFile;
236 FILE *NoPageFile;
237 UINT8 Data;
238 unsigned long FileSize;
239
240 //
241 // Open files
242 //
243 PageFile = fopen (PageFileName, "w+b");
244 if (PageFile == NULL) {
245 Error (NoPageFileName, 0, 0x4002, "Invalid parameter option", "Output File %s open failure", PageFileName);
246 return -1;
247 }
248
249 NoPageFile = fopen (NoPageFileName, "r+b");
250 if (NoPageFile == NULL) {
251 Error (NoPageFileName, 0, 0x4002, "Invalid parameter option", "Input File %s open failure", NoPageFileName);
252 fclose (PageFile);
253 return -1;
254 }
255
256 //
257 // Check size - should not be great than EFI_PAGE_BASE_OFFSET_IN_LDR
258 //
259 fseek (NoPageFile, 0, SEEK_END);
260 FileSize = ftell (NoPageFile);
261 fseek (NoPageFile, 0, SEEK_SET);
262 if (FileSize > gPageTableOffsetInFile) {
263 Error (NoPageFileName, 0, 0x4002, "Invalid parameter option", "Input file size (0x%lx) exceeds the Page Table Offset (0x%x)", FileSize, (unsigned) gPageTableOffsetInFile);
264 fclose (PageFile);
265 fclose (NoPageFile);
266 return -1;
267 }
268
269 //
270 // Write data
271 //
272 while (fread (&Data, sizeof(UINT8), 1, NoPageFile)) {
273 fwrite (&Data, sizeof(UINT8), 1, PageFile);
274 }
275
276 //
277 // Write PageTable
278 //
279 fseek (PageFile, gPageTableOffsetInFile, SEEK_SET);
280 fwrite (BaseMemory, (EFI_PAGE_NUMBER * EFI_SIZE_OF_PAGE), 1, PageFile);
281
282 //
283 // Close files
284 //
285 fclose (PageFile);
286 fclose (NoPageFile);
287
288 return 0;
289 }
290
291 int
292 main (
293 int argc,
294 char **argv
295 )
296 {
297 VOID *BaseMemory;
298 INTN result;
299 CHAR8 *OutputFile = NULL;
300 CHAR8 *InputFile = NULL;
301 EFI_STATUS Status;
302 UINT64 TempValue;
303
304 SetUtilityName("GenPage");
305
306 if (argc == 1) {
307 Usage();
308 return STATUS_ERROR;
309 }
310
311 argc --;
312 argv ++;
313
314 if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
315 Usage();
316 return 0;
317 }
318
319 if (stricmp (argv[0], "--version") == 0) {
320 Version();
321 return 0;
322 }
323
324 while (argc > 0) {
325 if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--output") == 0)) {
326 if (argv[1] == NULL || argv[1][0] == '-') {
327 Error (NULL, 0, 1003, "Invalid option value", "Output file is missing for -o option");
328 return STATUS_ERROR;
329 }
330 OutputFile = argv[1];
331 argc -= 2;
332 argv += 2;
333 continue;
334 }
335
336 if ((stricmp (argv[0], "-b") == 0) || (stricmp (argv[0], "--baseaddr") == 0)) {
337 if (argv[1] == NULL || argv[1][0] == '-') {
338 Error (NULL, 0, 1003, "Invalid option value", "Base address is missing for -b option");
339 return STATUS_ERROR;
340 }
341 Status = AsciiStringToUint64 (argv[1], FALSE, &TempValue);
342 if (EFI_ERROR (Status)) {
343 Error (NULL, 0, 1003, "Invalid option value", "Base address is not valid intergrator");
344 return STATUS_ERROR;
345 }
346 gPageTableBaseAddress = (UINT32) TempValue;
347 argc -= 2;
348 argv += 2;
349 continue;
350 }
351
352 if ((stricmp (argv[0], "-f") == 0) || (stricmp (argv[0], "--offset") == 0)) {
353 if (argv[1] == NULL || argv[1][0] == '-') {
354 Error (NULL, 0, 1003, "Invalid option value", "Offset is missing for -f option");
355 return STATUS_ERROR;
356 }
357 Status = AsciiStringToUint64 (argv[1], FALSE, &TempValue);
358 if (EFI_ERROR (Status)) {
359 Error (NULL, 0, 1003, "Invalid option value", "Offset is not valid intergrator");
360 return STATUS_ERROR;
361 }
362 gPageTableOffsetInFile = (UINT32) TempValue;
363 argc -= 2;
364 argv += 2;
365 continue;
366 }
367
368 if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {
369 argc --;
370 argv ++;
371 continue;
372 }
373
374 if ((stricmp (argv[0], "-v") ==0) || (stricmp (argv[0], "--verbose") == 0)) {
375 argc --;
376 argv ++;
377 continue;
378 }
379
380 if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) {
381 if (argv[1] == NULL || argv[1][0] == '-') {
382 Error (NULL, 0, 1003, "Invalid option value", "Debug Level is not specified.");
383 return STATUS_ERROR;
384 }
385 Status = AsciiStringToUint64 (argv[1], FALSE, &TempValue);
386 if (EFI_ERROR (Status)) {
387 Error (NULL, 0, 1003, "Invalid option value", "Debug Level is not valid intergrator.");
388 return STATUS_ERROR;
389 }
390 if (TempValue > 9) {
391 Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", (int) TempValue);
392 return STATUS_ERROR;
393 }
394 argc -= 2;
395 argv += 2;
396 continue;
397 }
398
399 if (argv[0][0] == '-') {
400 Error (NULL, 0, 1000, "Unknown option", argv[0]);
401 return STATUS_ERROR;
402 }
403
404 //
405 // Don't recognize the paramter.
406 //
407 InputFile = argv[0];
408 argc--;
409 argv++;
410 }
411
412 if (InputFile == NULL) {
413 Error (NULL, 0, 1003, "Invalid option value", "Input file is not specified");
414 return STATUS_ERROR;
415 }
416
417 //
418 // Create X64 page table
419 //
420 BaseMemory = CreateIdentityMappingPageTables ();
421
422 //
423 // Add page table to binary file
424 //
425 result = GenBinPage (BaseMemory, InputFile, OutputFile);
426 if (result < 0) {
427 return STATUS_ERROR;
428 }
429
430 return 0;
431 }
432