1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
/** @file
Copyright (c) 2006 - 2007, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
Image.c
Abstract:
Pei Core Load Image Support
**/
#include <PeiMain.h>
/*++
Routine Description:
Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
Arguments:
FileHandle - The handle to the PE/COFF file
FileOffset - The offset, in bytes, into the file to read
ReadSize - The number of bytes to read from the file starting at FileOffset
Buffer - A pointer to the buffer to read the data into.
Returns:
EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
--*/
EFI_STATUS
PeiLoadImageLoadImage (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL
OUT UINT64 *ImageSizeArg, OPTIONAL
OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
OUT UINT32 *AuthenticationState
)
/*++
Routine Description:
Routine for loading file image.
Arguments:
PeiServices - The PEI core services table.
FileHandle - Pointer to the FFS file header of the image.
ImageAddressArg - Pointer to PE/TE image.
ImageSizeArg - Size of PE/TE image.
EntryPoint - Pointer to entry point of specified image file for output.
AuthenticationState - Pointer to attestation authentication state of image.
Returns:
Status - EFI_SUCCESS - Image is successfully loaded.
EFI_NOT_FOUND - Fail to locate necessary PPI
Others - Fail to load file.
--*/
;
EFI_STATUS
EFIAPI
PeiLoadImageLoadImageWrapper (
IN CONST EFI_PEI_LOAD_FILE_PPI *This,
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL
OUT UINT64 *ImageSizeArg, OPTIONAL
OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
OUT UINT32 *AuthenticationState
)
/*++
Routine Description:
The wrapper function of PeiLoadImageLoadImage().
Arguments:
This - Pointer to EFI_PEI_LOAD_FILE_PPI.
PeiServices - The PEI core services table.
FileHandle - Pointer to the FFS file header of the image.
ImageAddressArg - Pointer to PE/TE image.
ImageSizeArg - Size of PE/TE image.
EntryPoint - Pointer to entry point of specified image file for output.
AuthenticationState - Pointer to attestation authentication state of image.
Returns:
EFI_STATUS.
--*/
;
STATIC EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi = {
PeiLoadImageLoadImageWrapper
};
STATIC EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {
(EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
&gEfiPeiLoadFilePpiGuid,
&mPeiLoadImagePpi
};
EFI_STATUS
EFIAPI
PeiImageRead (
IN VOID *FileHandle,
IN UINTN FileOffset,
IN OUT UINTN *ReadSize,
OUT VOID *Buffer
)
/*++
Routine Description:
Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
Arguments:
FileHandle - The handle to the PE/COFF file
FileOffset - The offset, in bytes, into the file to read
ReadSize - The number of bytes to read from the file starting at FileOffset
Buffer - A pointer to the buffer to read the data into.
Returns:
EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
--*/
{
CHAR8 *Destination8;
CHAR8 *Source8;
UINTN Length;
Destination8 = Buffer;
Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);
Length = *ReadSize;
while (Length--) {
*(Destination8++) = *(Source8++);
}
return EFI_SUCCESS;
}
EFI_STATUS
GetImageReadFunction (
IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
)
/*++
Routine Description:
Support routine to return the Image Read
Arguments:
PeiServices - PEI Services Table
ImageContext - The context of the image being loaded
Returns:
EFI_SUCCESS - If Image function location is found
--*/
{
VOID* MemoryBuffer;
MemoryBuffer = AllocatePages (0x400 / EFI_PAGE_SIZE + 1);
ASSERT (MemoryBuffer != NULL);
CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);
ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;
return EFI_SUCCESS;
}
STATIC
EFI_STATUS
LoadAndRelocatePeCoffImage (
IN VOID *Pe32Data,
OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
OUT UINT64 *ImageSize,
OUT EFI_PHYSICAL_ADDRESS *EntryPoint
)
/*++
Routine Description:
Loads and relocates a PE/COFF image into memory.
Arguments:
Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
ImageAddress - The base address of the relocated PE/COFF image
ImageSize - The size of the relocated PE/COFF image
EntryPoint - The entry point of the relocated PE/COFF image
Returns:
EFI_SUCCESS - The file was loaded and relocated
EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
--*/
{
EFI_STATUS Status;
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
ZeroMem (&ImageContext, sizeof (ImageContext));
ImageContext.Handle = Pe32Data;
Status = GetImageReadFunction (&ImageContext);
ASSERT_EFI_ERROR (Status);
Status = PeCoffLoaderGetImageInfo (&ImageContext);
if (EFI_ERROR (Status)) {
return Status;
}
//
// When Image has no reloc section, it can't be relocated into memory.
//
if (ImageContext.RelocationsStripped) {
DEBUG ((EFI_D_ERROR, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));
return EFI_INVALID_PARAMETER;
}
//
// Allocate Memory for the image
//
ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));
ASSERT (ImageContext.ImageAddress != 0);
//
// Skip the reserved space for the stripped PeHeader when load TeImage into memory.
//
if (ImageContext.IsTeImage) {
ImageContext.ImageAddress = ImageContext.ImageAddress +
((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -
sizeof (EFI_TE_IMAGE_HEADER);
}
//
// Load the image to our new buffer
//
Status = PeCoffLoaderLoadImage (&ImageContext);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Relocate the image in our new buffer
//
Status = PeCoffLoaderRelocateImage (&ImageContext);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Flush the instruction cache so the image data is written before we execute it
//
InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
*ImageAddress = ImageContext.ImageAddress;
*ImageSize = ImageContext.ImageSize;
*EntryPoint = ImageContext.EntryPoint;
return EFI_SUCCESS;
}
EFI_STATUS
PeiLoadImageLoadImage (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL
OUT UINT64 *ImageSizeArg, OPTIONAL
OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
OUT UINT32 *AuthenticationState
)
/*++
Routine Description:
Routine for loading file image.
Arguments:
PeiServices - The PEI core services table.
FileHandle - Pointer to the FFS file header of the image.
ImageAddressArg - Pointer to PE/TE image.
ImageSizeArg - Size of PE/TE image.
EntryPoint - Pointer to entry point of specified image file for output.
AuthenticationState - Pointer to attestation authentication state of image.
Returns:
Status - EFI_SUCCESS - Image is successfully loaded.
EFI_NOT_FOUND - Fail to locate necessary PPI
Others - Fail to load file.
--*/
{
EFI_STATUS Status;
VOID *Pe32Data;
EFI_PHYSICAL_ADDRESS ImageAddress;
UINT64 ImageSize;
EFI_PHYSICAL_ADDRESS ImageEntryPoint;
UINT16 Machine;
PEI_CORE_INSTANCE *Private;
VOID *EntryPointArg;
EFI_SECTION_TYPE SearchType1;
EFI_SECTION_TYPE SearchType2;
*EntryPoint = 0;
ImageSize = 0;
*AuthenticationState = 0;
if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {
SearchType1 = EFI_SECTION_TE;
SearchType2 = EFI_SECTION_PE32;
} else {
SearchType1 = EFI_SECTION_PE32;
SearchType2 = EFI_SECTION_TE;
}
//
// Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst
// is true, TE will be searched first).
//
Status = PeiServicesFfsFindSectionData (
SearchType1,
FileHandle,
&Pe32Data
);
//
// If we didn't find a first exe section, try to find the second exe section.
//
if (EFI_ERROR (Status)) {
Status = PeiServicesFfsFindSectionData (
SearchType2,
FileHandle,
&Pe32Data
);
if (EFI_ERROR (Status)) {
//
// PEI core only carry the loader function fro TE and PE32 executables
// If this two section does not exist, just return.
//
return Status;
}
}
Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
if (Private->PeiMemoryInstalled &&
(Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
//
// If memory is installed, perform the shadow operations
//
Status = LoadAndRelocatePeCoffImage (
Pe32Data,
&ImageAddress,
&ImageSize,
&ImageEntryPoint
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Got the entry point from the loaded Pe32Data
//
Pe32Data = (VOID *) ((UINTN) ImageAddress);
*EntryPoint = ImageEntryPoint;
} else {
//
// Retrieve the entry point from the PE/COFF or TE image header
//
ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) Pe32Data;
Status = PeCoffLoaderGetEntryPoint (Pe32Data, &EntryPointArg);
if (EFI_ERROR (Status)) {
return Status;
}
*EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) EntryPointArg;
}
Machine = PeCoffLoaderGetMachineType (Pe32Data);
if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {
return EFI_UNSUPPORTED;
}
if (ImageAddressArg != NULL) {
*ImageAddressArg = ImageAddress;
}
if (ImageSizeArg != NULL) {
*ImageSizeArg = ImageSize;
}
DEBUG_CODE_BEGIN ();
CHAR8 *AsciiString;
CHAR8 AsciiBuffer[512];
INT32 Index;
INT32 Index1;
//
// Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
//
if (Machine != IMAGE_FILE_MACHINE_IA64) {
DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%10p EntryPoint=0x%10p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));
} else {
//
// For IPF Image, the real entry point should be print.
//
DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%10p EntryPoint=0x%10p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));
}
//
// Print Module Name by PeImage PDB file name.
//
AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);
if (AsciiString != NULL) {
for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {
if (AsciiString[Index] == '\\') {
break;
}
}
if (Index != 0) {
for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {
AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];
}
AsciiBuffer [Index1] = '\0';
DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));
}
}
DEBUG_CODE_END ();
DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));
return EFI_SUCCESS;
}
EFI_STATUS
EFIAPI
PeiLoadImageLoadImageWrapper (
IN CONST EFI_PEI_LOAD_FILE_PPI *This,
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL
OUT UINT64 *ImageSizeArg, OPTIONAL
OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
OUT UINT32 *AuthenticationState
)
/*++
Routine Description:
The wrapper function of PeiLoadImageLoadImage().
Arguments:
This - Pointer to EFI_PEI_LOAD_FILE_PPI.
PeiServices - The PEI core services table.
FileHandle - Pointer to the FFS file header of the image.
ImageAddressArg - Pointer to PE/TE image.
ImageSizeArg - Size of PE/TE image.
EntryPoint - Pointer to entry point of specified image file for output.
AuthenticationState - Pointer to attestation authentication state of image.
Returns:
EFI_STATUS.
--*/
{
return PeiLoadImageLoadImage (
GetPeiServicesTablePointer (),
FileHandle,
ImageAddressArg,
ImageSizeArg,
EntryPoint,
AuthenticationState
);
}
EFI_STATUS
PeiLoadImage (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_FILE_HANDLE FileHandle,
OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
OUT UINT32 *AuthenticationState
)
/*++
Routine Description:
Routine for load image file.
Arguments:
PeiServices - The PEI core services table.
FileHandle - Pointer to the FFS file header of the image.
EntryPoint - Pointer to entry point of specified image file for output.
AuthenticationState - Pointer to attestation authentication state of image.
Returns:
Status - EFI_SUCCESS - Image is successfully loaded.
EFI_NOT_FOUND - Fail to locate necessary PPI
Others - Fail to load file.
--*/
{
EFI_STATUS PpiStatus;
EFI_STATUS Status;
UINTN Index;
EFI_PEI_LOAD_FILE_PPI *LoadFile;
EFI_PHYSICAL_ADDRESS ImageAddress;
UINT64 ImageSize;
//
// If any instances of PEI_LOAD_FILE_PPI are installed, they are called.
// one at a time, until one reports EFI_SUCCESS.
//
Index = 0;
do {
PpiStatus = PeiServicesLocatePpi (
&gEfiPeiLoadFilePpiGuid,
Index,
NULL,
(VOID **)&LoadFile
);
if (!EFI_ERROR (PpiStatus)) {
Status = LoadFile->LoadFile (
LoadFile,
FileHandle,
&ImageAddress,
&ImageSize,
EntryPoint,
AuthenticationState
);
if (!EFI_ERROR (Status)) {
return Status;
}
}
Index++;
} while (!EFI_ERROR (PpiStatus));
//
// If no instances reports EFI_SUCCESS, then build-in support for
// the PE32+/TE XIP image format is used.
//
Status = PeiLoadImageLoadImage (
PeiServices,
FileHandle,
NULL,
NULL,
EntryPoint,
AuthenticationState
);
return Status;
}
VOID
InitializeImageServices (
IN PEI_CORE_INSTANCE *PrivateData,
IN PEI_CORE_INSTANCE *OldCoreData
)
/*++
Routine Description:
Install Pei Load File PPI.
Arguments:
PrivateData - Pointer to PEI_CORE_INSTANCE.
OldCoreData - Pointer to PEI_CORE_INSTANCE.
Returns:
NONE.
--*/
{
if (OldCoreData == NULL) {
//
// The first time we are XIP (running from FLASH). We need to remember the
// FLASH address so we can reinstall the memory version that runs faster
//
PrivateData->XipLoadFile = &gPpiLoadFilePpiList;
PeiServicesInstallPpi (PrivateData->XipLoadFile);
} else {
//
// 2nd time we are running from memory so replace the XIP version with the
// new memory version.
//
PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);
}
}
|