]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/PeiRebase/PeiRebaseExe.c
e153110c312fcae63986ef6942ffcdaca8f74aef
[mirror_edk2.git] / Tools / CCode / Source / PeiRebase / PeiRebaseExe.c
1 /*++
2
3 Copyright (c) 1999-2006 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 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
13 Module Name:
14
15 PeiRebaseExe.c
16
17 Abstract:
18
19 This contains all code necessary to build the PeiRebase.exe utility.
20 This utility relies heavily on the PeiRebase DLL. Definitions for both
21 can be found in the PEI Rebase Utility Specification, review draft.
22
23 --*/
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <Common/UefiBaseTypes.h>
30 #include <Common/FirmwareVolumeImageFormat.h>
31 #include <Common/FirmwareFileSystem.h>
32 #include <Library/PeCoffLib.h>
33
34 #include "CommonLib.h"
35 #include "ParseInf.h"
36 #include "FvLib.h"
37 #include "EfiUtilityMsgs.h"
38 #include "PeiRebaseExe.h"
39
40 EFI_STATUS
41 ReadHeader (
42 IN FILE *InputFile,
43 OUT UINT32 *FvSize,
44 OUT BOOLEAN *ErasePolarity
45 );
46
47 int
48 main (
49 int argc,
50 char **argv
51 )
52 /*++
53
54 Routine Description:
55
56 This utility relocates PEI XIP PE32s in a FV.
57
58 Arguments:
59
60 argc - Number of command line arguments
61 argv[]:
62 BaseAddress The base address to use for rebasing the FV. The correct
63 format is a hex number preceded by 0x.
64 InputFileName The name of the input FV file.
65 OutputFileName The name of the output FV file.
66
67 Arguments come in pair in any order.
68 -I InputFileName
69 -O OutputFileName
70 -B BaseAddress
71
72 Returns:
73
74 0 No error conditions detected.
75 1 One or more of the input parameters is invalid.
76 2 A resource required by the utility was unavailable.
77 Most commonly this will be memory allocation or file creation.
78 3 PeiRebase.dll could not be loaded.
79 4 Error executing the PEI rebase.
80
81 --*/
82 {
83 UINT8 Index;
84 CHAR8 InputFileName[_MAX_PATH];
85 CHAR8 *OutputFileName;
86 EFI_PHYSICAL_ADDRESS XipBase, BsBase, RtBase;
87 UINT32 BaseTypes;
88 EFI_STATUS Status;
89 FILE *InputFile;
90 FILE *OutputFile;
91 FILE *LogFile;
92 UINT64 FvOffset;
93 UINT32 FileCount;
94 int BytesRead;
95 EFI_FIRMWARE_VOLUME_HEADER *FvImage;
96 UINT32 FvSize;
97 EFI_FFS_FILE_HEADER *CurrentFile;
98 BOOLEAN ErasePolarity;
99 MEMORY_FILE InfMemoryFile;
100 CHAR8 StringBuffer[0x100];
101
102 ErasePolarity = FALSE;
103 //
104 // Set utility name for error/warning reporting purposes.
105 //
106 SetUtilityName (UTILITY_NAME);
107 //
108 // Verify the correct number of arguments
109 //
110 if (argc != MAX_ARGS) {
111 PrintUsage ();
112 return STATUS_ERROR;
113 }
114
115 //
116 // Initialize variables
117 //
118 InputFileName[0] = '\0';
119 OutputFileName = NULL;
120 XipBase = BsBase = RtBase = 0;
121 BaseTypes = 0;
122 FvOffset = 0;
123 FileCount = 0;
124 ErasePolarity = FALSE;
125 InputFile = NULL;
126 OutputFile = NULL;
127 LogFile = NULL;
128 FvImage = NULL;
129
130 //
131 // Parse the command line arguments
132 //
133 for (Index = 1; Index < MAX_ARGS; Index += 2) {
134 //
135 // Make sure argument pair begin with - or /
136 //
137 if (argv[Index][0] != '-' && argv[Index][0] != '/') {
138 PrintUsage ();
139 Error (NULL, 0, 0, argv[Index], "unrecognized option");
140 return STATUS_ERROR;
141 }
142 //
143 // Make sure argument specifier is only one letter
144 //
145 if (argv[Index][2] != 0) {
146 PrintUsage ();
147 Error (NULL, 0, 0, argv[Index], "unrecognized option");
148 return STATUS_ERROR;
149 }
150 //
151 // Determine argument to read
152 //
153 switch (argv[Index][1]) {
154 case 'I':
155 case 'i':
156 if (strlen (InputFileName) == 0) {
157 strcpy (InputFileName, argv[Index + 1]);
158 } else {
159 PrintUsage ();
160 Error (NULL, 0, 0, argv[Index + 1], "only one -i InputFileName may be specified");
161 return STATUS_ERROR;
162 }
163 break;
164
165 case 'O':
166 case 'o':
167 if (OutputFileName == NULL) {
168 OutputFileName = argv[Index + 1];
169 } else {
170 PrintUsage ();
171 Error (NULL, 0, 0, argv[Index + 1], "only one -o OutputFileName may be specified");
172 return STATUS_ERROR;
173 }
174 break;
175
176 case 'F':
177 case 'f':
178 //
179 // Load INF file into memory & initialize MEMORY_FILE structure
180 //
181 Status = GetFileImage (argv[Index + 1], &InfMemoryFile.FileImage, (UINT32*)&InfMemoryFile.Eof);
182 InfMemoryFile.Eof = InfMemoryFile.FileImage + (UINT32)(UINTN)InfMemoryFile.Eof;
183 InfMemoryFile.CurrentFilePointer = InfMemoryFile.FileImage;
184 if (EFI_ERROR (Status)) {
185 Error (NULL, 0, 0, argv[Index + 1], "Error opening FvInfFile");
186 return STATUS_ERROR;
187 }
188
189 //
190 // Read BaseAddress from fv.inf file
191 //
192 FindToken (&InfMemoryFile, "[options]", "EFI_BASE_ADDRESS", 0, StringBuffer);
193
194 //
195 // Free INF file image
196 //
197 free (InfMemoryFile.FileImage);
198
199 //
200 // Point argv[Index + 1] to StringBuffer so that it could be processed as "-b"
201 //
202 argv[Index + 1] = StringBuffer;
203
204 case 'B':
205 case 'b':
206 if (BaseTypes & 1) {
207 PrintUsage ();
208 Error (NULL, 0, 0, argv[Index + 1], "XipBaseAddress may be specified only once by either -b or -f");
209 return STATUS_ERROR;
210 }
211
212 Status = AsciiStringToUint64 (argv[Index + 1], FALSE, &XipBase);
213 if (EFI_ERROR (Status)) {
214 PrintUsage ();
215 Error (NULL, 0, 0, argv[Index + 1], "invalid hex digit given for XIP base address");
216 return STATUS_ERROR;
217 }
218
219 BaseTypes |= 1;
220 break;
221
222 case 'D':
223 case 'd':
224 if (BaseTypes & 2) {
225 PrintUsage ();
226 Error (NULL, 0, 0, argv[Index + 1], "-d BsBaseAddress may be specified only once");
227 return STATUS_ERROR;
228 }
229
230 Status = AsciiStringToUint64 (argv[Index + 1], FALSE, &BsBase);
231 if (EFI_ERROR (Status)) {
232 PrintUsage ();
233 Error (NULL, 0, 0, argv[Index + 1], "invalid hex digit given for BS_DRIVER base address");
234 return STATUS_ERROR;
235 }
236
237 BaseTypes |= 2;
238 break;
239
240 case 'R':
241 case 'r':
242 if (BaseTypes & 4) {
243 PrintUsage ();
244 Error (NULL, 0, 0, argv[Index + 1], "-r RtBaseAddress may be specified only once");
245 return STATUS_ERROR;
246 }
247
248 Status = AsciiStringToUint64 (argv[Index + 1], FALSE, &RtBase);
249 if (EFI_ERROR (Status)) {
250 PrintUsage ();
251 Error (NULL, 0, 0, argv[Index + 1], "invalid hex digit given for RT_DRIVER base address");
252 return STATUS_ERROR;
253 }
254
255 BaseTypes |= 4;
256 break;
257
258 default:
259 PrintUsage ();
260 Error (NULL, 0, 0, argv[Index], "unrecognized argument");
261 return STATUS_ERROR;
262 break;
263 }
264 }
265 //
266 // Open the file containing the FV
267 //
268 InputFile = fopen (InputFileName, "rb");
269 if (InputFile == NULL) {
270 Error (NULL, 0, 0, InputFileName, "could not open input file for reading");
271 return STATUS_ERROR;
272 }
273
274 //
275 // Open the log file
276 //
277 strcat (InputFileName, ".log");
278 LogFile = fopen (InputFileName, "a");
279 if (LogFile == NULL) {
280 Error (NULL, 0, 0, InputFileName, "could not append to log file");
281 }
282
283 //
284 // Determine size of FV
285 //
286 Status = ReadHeader (InputFile, &FvSize, &ErasePolarity);
287 if (EFI_ERROR (Status)) {
288 Error (NULL, 0, 0, "could not parse the FV header", NULL);
289 goto Finish;
290 }
291 //
292 // Allocate a buffer for the FV image
293 //
294 FvImage = malloc (FvSize);
295 if (FvImage == NULL) {
296 Error (NULL, 0, 0, "application error", "memory allocation failed");
297 goto Finish;
298 }
299 //
300 // Read the entire FV to the buffer
301 //
302 BytesRead = fread (FvImage, 1, FvSize, InputFile);
303 fclose (InputFile);
304 InputFile = NULL;
305 if ((unsigned int) BytesRead != FvSize) {
306 Error (NULL, 0, 0, InputFileName, "failed to read from file");
307 goto Finish;
308 }
309 //
310 // Prepare to walk the FV image
311 //
312 InitializeFvLib (FvImage, FvSize);
313 //
314 // Get the first file
315 //
316 Status = GetNextFile (NULL, &CurrentFile);
317 if (EFI_ERROR (Status)) {
318 Error (NULL, 0, 0, "cannot find the first file in the FV image", NULL);
319 goto Finish;
320 }
321 //
322 // Check if each file should be rebased
323 //
324 while (CurrentFile != NULL) {
325 //
326 // Rebase this file
327 //
328 FfsRebase (
329 CurrentFile,
330 BaseTypes,
331 XipBase + (UINTN)CurrentFile - (UINTN)FvImage,
332 &BsBase,
333 &RtBase,
334 LogFile
335 );
336
337 if (EFI_ERROR (Status)) {
338 switch (Status) {
339
340 case EFI_INVALID_PARAMETER:
341 Error (NULL, 0, 0, "invalid parameter passed to FfsRebase", NULL);
342 break;
343
344 case EFI_ABORTED:
345 Error (NULL, 0, 0, "error detected while rebasing -- aborted", NULL);
346 break;
347
348 case EFI_OUT_OF_RESOURCES:
349 Error (NULL, 0, 0, "FfsRebase could not allocate required resources", NULL);
350 break;
351
352 case EFI_NOT_FOUND:
353 Error (NULL, 0, 0, "FfsRebase could not locate a PE32 section", NULL);
354 break;
355
356 default:
357 Error (NULL, 0, 0, "FfsRebase returned unknown status", "status=0x%08X", Status);
358 break;
359 }
360
361 goto Finish;
362 }
363 //
364 // Get the next file
365 //
366 Status = GetNextFile (CurrentFile, &CurrentFile);
367 if (EFI_ERROR (Status)) {
368 Error (NULL, 0, 0, "cannot find the next file in the FV image", NULL);
369 goto Finish;
370 }
371 }
372 //
373 // Open the output file
374 //
375 OutputFile = fopen (OutputFileName, "wb");
376 if (OutputFile == NULL) {
377 Error (NULL, 0, 0, OutputFileName, "failed to open output file");
378 goto Finish;
379 }
380
381 if (fwrite (FvImage, 1, FvSize, OutputFile) != FvSize) {
382 Error (NULL, 0, 0, "failed to write to output file", 0);
383 goto Finish;
384 }
385
386 Finish:
387 if (InputFile != NULL) {
388 fclose (InputFile);
389 }
390 //
391 // If we created an output file, and there was an error, remove it so
392 // subsequent builds will rebuild it.
393 //
394 if (OutputFile != NULL) {
395 if (GetUtilityStatus () == STATUS_ERROR) {
396 remove (OutputFileName);
397 }
398
399 fclose (OutputFile);
400 }
401
402 if (LogFile != NULL) {
403 fclose (LogFile);
404 }
405
406 if (FvImage != NULL) {
407 free (FvImage);
408 }
409
410 return GetUtilityStatus ();
411 }
412
413 EFI_STATUS
414 ReadHeader (
415 IN FILE *InputFile,
416 OUT UINT32 *FvSize,
417 OUT BOOLEAN *ErasePolarity
418 )
419 /*++
420
421 Routine Description:
422
423 This function determines the size of the FV and the erase polarity. The
424 erase polarity is the FALSE value for file state.
425
426 Arguments:
427
428 InputFile The file that contains the FV image.
429 FvSize The size of the FV.
430 ErasePolarity The FV erase polarity.
431
432 Returns:
433
434 EFI_SUCCESS Function completed successfully.
435 EFI_INVALID_PARAMETER A required parameter was NULL or is out of range.
436 EFI_ABORTED The function encountered an error.
437
438 --*/
439 {
440 EFI_FIRMWARE_VOLUME_HEADER VolumeHeader;
441 EFI_FV_BLOCK_MAP_ENTRY BlockMap;
442 UINTN Signature[2];
443 UINTN BytesRead;
444 UINT32 Size;
445
446 BytesRead = 0;
447 Size = 0;
448 //
449 // Check input parameters
450 //
451 if ((InputFile == NULL) || (FvSize == NULL) || (ErasePolarity == NULL)) {
452 Error (NULL, 0, 0, "ReadHeader()", "invalid input parameter");
453 return EFI_INVALID_PARAMETER;
454 }
455 //
456 // Read the header
457 //
458 fread (&VolumeHeader, sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile);
459 BytesRead = sizeof (EFI_FIRMWARE_VOLUME_HEADER) - sizeof (EFI_FV_BLOCK_MAP_ENTRY);
460 Signature[0] = VolumeHeader.Signature;
461 Signature[1] = 0;
462
463 //
464 // Get erase polarity
465 //
466 if (VolumeHeader.Attributes & EFI_FVB_ERASE_POLARITY) {
467 *ErasePolarity = TRUE;
468 }
469
470 do {
471 fread (&BlockMap, sizeof (EFI_FV_BLOCK_MAP_ENTRY), 1, InputFile);
472 BytesRead += sizeof (EFI_FV_BLOCK_MAP_ENTRY);
473
474 if (BlockMap.NumBlocks != 0) {
475 Size += BlockMap.NumBlocks * BlockMap.BlockLength;
476 }
477
478 } while (!(BlockMap.NumBlocks == 0 && BlockMap.BlockLength == 0));
479
480 if (VolumeHeader.FvLength != Size) {
481 Error (NULL, 0, 0, "volume size not consistant with block maps", NULL);
482 return EFI_ABORTED;
483 }
484
485 *FvSize = Size;
486
487 rewind (InputFile);
488
489 return EFI_SUCCESS;
490 }
491
492 VOID
493 PrintUtilityInfo (
494 VOID
495 )
496 /*++
497
498 Routine Description:
499
500 Displays the standard utility information to SDTOUT
501
502 Arguments:
503
504 None
505
506 Returns:
507
508 None
509
510 --*/
511 {
512 printf (
513 "%s, PEI Rebase Utility. Version %i.%i, %s.\n\n",
514 UTILITY_NAME,
515 UTILITY_MAJOR_VERSION,
516 UTILITY_MINOR_VERSION,
517 UTILITY_DATE
518 );
519 }
520
521 VOID
522 PrintUsage (
523 VOID
524 )
525 /*++
526
527 Routine Description:
528
529 Displays the utility usage syntax to STDOUT
530
531 Arguments:
532
533 None
534
535 Returns:
536
537 None
538
539 --*/
540 {
541 printf (
542 "Usage: %s -I InputFileName -O OutputFileName -B BaseAddress\n",
543 UTILITY_NAME
544 );
545 printf (" Where:\n");
546 printf (" InputFileName is the name of the EFI FV file to rebase.\n");
547 printf (" OutputFileName is the desired output file name.\n");
548 printf (" BaseAddress is the FV base address to rebase against.\n");
549 printf (" Argument pair may be in any order.\n\n");
550 }
551
552 EFI_STATUS
553 FfsRebase (
554 IN OUT EFI_FFS_FILE_HEADER *FfsFile,
555 IN UINT32 Flags,
556 IN OUT EFI_PHYSICAL_ADDRESS XipBase,
557 IN OUT EFI_PHYSICAL_ADDRESS *BsBase,
558 IN OUT EFI_PHYSICAL_ADDRESS *RtBase,
559 OUT FILE *LogFile
560 )
561 /*++
562
563 Routine Description:
564
565 This function determines if a file is XIP and should be rebased. It will
566 rebase any PE32 sections found in the file using the base address.
567
568 Arguments:
569
570 FfsFile A pointer to Ffs file image.
571 BaseAddress The base address to use for rebasing the file image.
572
573 Returns:
574
575 EFI_SUCCESS The image was properly rebased.
576 EFI_INVALID_PARAMETER An input parameter is invalid.
577 EFI_ABORTED An error occurred while rebasing the input file image.
578 EFI_OUT_OF_RESOURCES Could not allocate a required resource.
579 EFI_NOT_FOUND No compressed sections could be found.
580
581 --*/
582 {
583 EFI_STATUS Status;
584 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
585 UINTN MemoryImagePointer;
586 UINTN MemoryImagePointerAligned;
587 EFI_PHYSICAL_ADDRESS ImageAddress;
588 UINT64 ImageSize;
589 EFI_PHYSICAL_ADDRESS EntryPoint;
590 UINT32 Pe32ImageSize;
591 EFI_PHYSICAL_ADDRESS NewPe32BaseAddress;
592 UINTN Index;
593 EFI_FILE_SECTION_POINTER CurrentPe32Section;
594 EFI_FFS_FILE_STATE SavedState;
595 EFI_IMAGE_NT_HEADERS32 *PeHdr;
596 EFI_IMAGE_NT_HEADERS64 *PePlusHdr;
597 UINT32 *PeHdrSizeOfImage;
598 UINT32 *PeHdrChecksum;
599 EFI_TE_IMAGE_HEADER *TEImageHeader;
600 UINT8 *TEBuffer;
601 EFI_IMAGE_DOS_HEADER *DosHeader;
602 UINT8 FileGuidString[80];
603 UINT32 TailSize;
604 EFI_FFS_FILE_TAIL TailValue;
605 EFI_PHYSICAL_ADDRESS *BaseToUpdate;
606
607 //
608 // Verify input parameters
609 //
610 if (FfsFile == NULL) {
611 return EFI_INVALID_PARAMETER;
612 }
613 //
614 // Convert the GUID to a string so we can at least report which file
615 // if we find an error.
616 //
617 PrintGuidToBuffer (&FfsFile->Name, FileGuidString, sizeof (FileGuidString), TRUE);
618 if (FfsFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
619 TailSize = sizeof (EFI_FFS_FILE_TAIL);
620 } else {
621 TailSize = 0;
622 }
623 //
624 // Do some cursory checks on the FFS file contents
625 //
626 Status = VerifyFfsFile (FfsFile);
627 if (EFI_ERROR (Status)) {
628 Error (NULL, 0, 0, "file does not appear to be a valid FFS file, cannot be rebased", FileGuidString);
629 return EFI_INVALID_PARAMETER;
630 }
631
632 //
633 // We only process files potentially containing PE32 sections.
634 //
635 switch (FfsFile->Type) {
636 case EFI_FV_FILETYPE_SECURITY_CORE:
637 case EFI_FV_FILETYPE_PEI_CORE:
638 case EFI_FV_FILETYPE_PEIM:
639 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
640 case EFI_FV_FILETYPE_DRIVER:
641 case EFI_FV_FILETYPE_DXE_CORE:
642 break;
643 default:
644 return EFI_SUCCESS;
645 }
646
647 //
648 // Rebase each PE32 section
649 //
650 Status = EFI_SUCCESS;
651 for (Index = 1;; Index++) {
652 Status = GetSectionByType (FfsFile, EFI_SECTION_PE32, Index, &CurrentPe32Section);
653 if (EFI_ERROR (Status)) {
654 break;
655 }
656
657
658 //
659 // Initialize context
660 //
661 memset (&ImageContext, 0, sizeof (ImageContext));
662 ImageContext.Handle = (VOID *) ((UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION));
663 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;
664 Status = PeCoffLoaderGetImageInfo (&ImageContext);
665 if (EFI_ERROR (Status)) {
666 Error (NULL, 0, 0, "GetImageInfo() call failed on rebase", FileGuidString);
667 return Status;
668 }
669
670 //
671 // Calculate the PE32 base address, based on file type
672 //
673 switch (FfsFile->Type) {
674 case EFI_FV_FILETYPE_SECURITY_CORE:
675 case EFI_FV_FILETYPE_PEI_CORE:
676 case EFI_FV_FILETYPE_PEIM:
677 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
678 if ((Flags & 1) == 0) {
679 //
680 // We aren't relocating XIP code, so skip it.
681 //
682 return EFI_SUCCESS;
683 }
684
685 NewPe32BaseAddress =
686 XipBase +
687 (UINTN)CurrentPe32Section.Pe32Section +
688 sizeof (EFI_COMMON_SECTION_HEADER) -
689 (UINTN)FfsFile;
690 BaseToUpdate = &XipBase;
691 break;
692
693 case EFI_FV_FILETYPE_DRIVER:
694 PeHdr = (EFI_IMAGE_NT_HEADERS32*)(
695 (UINTN)CurrentPe32Section.Pe32Section +
696 sizeof (EFI_COMMON_SECTION_HEADER) +
697 ImageContext.PeCoffHeaderOffset
698 );
699 switch (PeHdr->OptionalHeader.Subsystem) {
700 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
701 if ((Flags & 4) == 0) {
702 //
703 // RT drivers aren't supposed to be relocated
704 //
705 continue;
706 }
707
708 NewPe32BaseAddress = *RtBase;
709 BaseToUpdate = RtBase;
710 break;
711
712 default:
713 //
714 // We treat all other subsystems the same as BS_DRIVER
715 //
716 if ((Flags & 2) == 0) {
717 //
718 // Skip all BS_DRIVER's
719 //
720 continue;
721 }
722
723 NewPe32BaseAddress = *BsBase;
724 BaseToUpdate = BsBase;
725 break;
726 }
727 break;
728
729 case EFI_FV_FILETYPE_DXE_CORE:
730 if ((Flags & 2) == 0) {
731 //
732 // Skip DXE core
733 //
734 return EFI_SUCCESS;
735 }
736
737 NewPe32BaseAddress = *BsBase;
738 BaseToUpdate = BsBase;
739 break;
740
741 default:
742 //
743 // Not supported file type
744 //
745 return EFI_SUCCESS;
746 }
747
748 //
749 // Allocate a buffer for the image to be loaded into.
750 //
751 Pe32ImageSize = GetLength (CurrentPe32Section.Pe32Section->CommonHeader.Size) - sizeof (EFI_PE32_SECTION);
752 MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x100000));
753 if (MemoryImagePointer == 0) {
754 Error (NULL, 0, 0, "memory allocation failure", NULL);
755 return EFI_OUT_OF_RESOURCES;
756 }
757 memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x100000);
758 MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFFF) & (-1 << 16);
759
760 ImageContext.ImageAddress = MemoryImagePointerAligned;
761
762 Status = PeCoffLoaderLoadImage (&ImageContext);
763 if (EFI_ERROR (Status)) {
764 Error (NULL, 0, 0, "LoadImage() call failed on rebase", FileGuidString);
765 free ((VOID *) MemoryImagePointer);
766 return Status;
767 }
768
769 //
770 // Check if section-alignment and file-alignment match or not
771 //
772 if (!(ImageContext.IsTeImage)) {
773 PeHdr = (EFI_IMAGE_NT_HEADERS *)((UINTN)ImageContext.ImageAddress +
774 ImageContext.PeCoffHeaderOffset);
775 if (PeHdr->OptionalHeader.SectionAlignment != PeHdr->OptionalHeader.FileAlignment) {
776 Error (NULL, 0, 0, "Section-Alignment and File-Alignment does not match", FileGuidString);
777 free ((VOID *) MemoryImagePointer);
778 return EFI_ABORTED;
779 }
780 }
781 else {
782 //
783 // BUGBUG: TE Image Header lack section-alignment and file-alignment info
784 //
785 }
786
787 ImageContext.DestinationAddress = NewPe32BaseAddress;
788 Status = PeCoffLoaderRelocateImage (&ImageContext);
789 if (EFI_ERROR (Status)) {
790 Error (NULL, 0, 0, "RelocateImage() call failed on rebase", FileGuidString);
791 free ((VOID *) MemoryImagePointer);
792 return Status;
793 }
794
795 ImageAddress = ImageContext.ImageAddress;
796 ImageSize = ImageContext.ImageSize;
797 EntryPoint = ImageContext.EntryPoint;
798
799 if (ImageSize > Pe32ImageSize) {
800 Error (
801 NULL,
802 0,
803 0,
804 "rebased image is larger than original PE32 image",
805 "0x%X > 0x%X, file %s",
806 ImageSize,
807 Pe32ImageSize,
808 FileGuidString
809 );
810 free ((VOID *) MemoryImagePointer);
811 return EFI_ABORTED;
812 }
813
814 //
815 // Update BASE address
816 //
817 fprintf (
818 LogFile,
819 "%s %016I64X %s\n",
820 FileGuidString,
821 ImageContext.DestinationAddress,
822 ImageContext.PdbPointer == NULL ? "*" : ImageContext.PdbPointer
823 );
824 *BaseToUpdate += EFI_SIZE_TO_PAGES (ImageContext.ImageSize) * EFI_PAGE_SIZE;
825
826 //
827 // Since we may have updated the Codeview RVA, we need to insure the PE
828 // header indicates the image is large enough to contain the Codeview data
829 // so it will be loaded properly later if the PEIM is reloaded into memory...
830 //
831 PeHdr = (VOID *) ((UINTN) ImageAddress + ImageContext.PeCoffHeaderOffset);
832 PePlusHdr = (EFI_IMAGE_NT_HEADERS64*)PeHdr;
833 if (PeHdr->FileHeader.Machine == EFI_IMAGE_MACHINE_IA32) {
834 PeHdrSizeOfImage = (UINT32 *) (&(PeHdr->OptionalHeader).SizeOfImage);
835 PeHdrChecksum = (UINT32 *) (&(PeHdr->OptionalHeader).CheckSum);
836 } else if (PeHdr->FileHeader.Machine == EFI_IMAGE_MACHINE_IA64) {
837 PeHdrSizeOfImage = (UINT32 *) (&(PePlusHdr->OptionalHeader).SizeOfImage);
838 PeHdrChecksum = (UINT32 *) (&(PePlusHdr->OptionalHeader).CheckSum);
839 } else if (PeHdr->FileHeader.Machine == EFI_IMAGE_MACHINE_X64) {
840 PeHdrSizeOfImage = (UINT32 *) (&(PePlusHdr->OptionalHeader).SizeOfImage);
841 PeHdrChecksum = (UINT32 *) (&(PePlusHdr->OptionalHeader).CheckSum);
842 } else {
843 Error (
844 NULL,
845 0,
846 0,
847 "unknown machine type in PE32 image",
848 "machine type=0x%X, file=%s",
849 (UINT32) PeHdr->FileHeader.Machine,
850 FileGuidString
851 );
852 free ((VOID *) MemoryImagePointer);
853 return EFI_ABORTED;
854 }
855
856 if (*PeHdrSizeOfImage != ImageContext.ImageSize) {
857 *PeHdrSizeOfImage = (UINT32) ImageContext.ImageSize;
858 if (*PeHdrChecksum) {
859 *PeHdrChecksum = 0;
860 }
861 }
862
863 memcpy (CurrentPe32Section.Pe32Section + 1, (VOID *) MemoryImagePointerAligned, (UINT32) ImageSize);
864
865 free ((VOID *) MemoryImagePointer);
866
867 //
868 // Now update file checksum
869 //
870 if (FfsFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
871 TailSize = sizeof (EFI_FFS_FILE_TAIL);
872 } else {
873 TailSize = 0;
874 }
875
876 if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
877 SavedState = FfsFile->State;
878 FfsFile->IntegrityCheck.Checksum.File = 0;
879 FfsFile->State = 0;
880 if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
881 FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
882 (UINT8 *) FfsFile,
883 GetLength (FfsFile->Size) - TailSize
884 );
885 } else {
886 FfsFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
887 }
888
889 FfsFile->State = SavedState;
890 }
891 //
892 // Update tail if present
893 //
894 if (FfsFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
895 TailValue = (EFI_FFS_FILE_TAIL) (~(FfsFile->IntegrityCheck.TailReference));
896 *(EFI_FFS_FILE_TAIL *) (((UINTN) FfsFile + GetLength (FfsFile->Size) - sizeof (EFI_FFS_FILE_TAIL))) = TailValue;
897 }
898 }
899
900 if ((Flags & 1) == 0 || (
901 FfsFile->Type != EFI_FV_FILETYPE_SECURITY_CORE &&
902 FfsFile->Type != EFI_FV_FILETYPE_PEI_CORE &&
903
904 FfsFile->Type != EFI_FV_FILETYPE_PEIM &&
905 FfsFile->Type != EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
906 )) {
907 //
908 // Only XIP code may have a TE section
909 //
910 return EFI_SUCCESS;
911 }
912
913 //
914 // Now process TE sections
915 //
916 for (Index = 1;; Index++) {
917 Status = GetSectionByType (FfsFile, EFI_SECTION_TE, Index, &CurrentPe32Section);
918 if (EFI_ERROR (Status)) {
919 break;
920 }
921
922 //
923 // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
924 // by GenTEImage
925 //
926 TEImageHeader = (EFI_TE_IMAGE_HEADER *) ((UINT8 *) CurrentPe32Section.Pe32Section + sizeof (EFI_COMMON_SECTION_HEADER));
927
928 NewPe32BaseAddress = ((UINT32) XipBase) +
929 (
930 (UINTN) CurrentPe32Section.Pe32Section +
931 sizeof (EFI_COMMON_SECTION_HEADER) +
932 sizeof (EFI_TE_IMAGE_HEADER) -
933 TEImageHeader->StrippedSize -
934 (UINTN) FfsFile
935 );
936
937 //
938 // Allocate a buffer to unshrink the image into.
939 //
940 Pe32ImageSize = GetLength (CurrentPe32Section.Pe32Section->CommonHeader.Size) - sizeof (EFI_PE32_SECTION) -
941 sizeof (EFI_TE_IMAGE_HEADER);
942 Pe32ImageSize += TEImageHeader->StrippedSize;
943 TEBuffer = (UINT8 *) malloc (Pe32ImageSize);
944 if (TEBuffer == NULL) {
945 Error (NULL, 0, 0, "failed to allocate memory", NULL);
946 return EFI_OUT_OF_RESOURCES;
947 }
948 //
949 // Expand the image into our buffer and fill in critical fields in the DOS header
950 // Fill in fields required by the loader.
951 // At offset 0x3C is the offset to the PE signature. We'll put it immediately following the offset value
952 // itself.
953 //
954 memset (TEBuffer, 0, Pe32ImageSize);
955 DosHeader = (EFI_IMAGE_DOS_HEADER *) TEBuffer;
956 DosHeader->e_magic = EFI_IMAGE_DOS_SIGNATURE;
957 *(UINT32 *) (TEBuffer + 0x3C) = 0x40;
958 PeHdr = (EFI_IMAGE_NT_HEADERS *) (TEBuffer + 0x40);
959 PePlusHdr = (EFI_IMAGE_NT_HEADERS64*)PeHdr;
960 PeHdr->Signature = EFI_IMAGE_NT_SIGNATURE;
961 PeHdr->FileHeader.Machine = TEImageHeader->Machine;
962 PeHdr->FileHeader.NumberOfSections = TEImageHeader->NumberOfSections;
963
964 //
965 // Say the size of the optional header is the total we stripped off less the size of a PE file header and PE signature and
966 // the 0x40 bytes for our DOS header.
967 //
968 PeHdr->FileHeader.SizeOfOptionalHeader = (UINT16) (TEImageHeader->StrippedSize - 0x40 - sizeof (UINT32) - sizeof (EFI_IMAGE_FILE_HEADER));
969 if (TEImageHeader->Machine == EFI_IMAGE_MACHINE_IA32) {
970 PeHdr->OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
971 } else if (TEImageHeader->Machine == EFI_IMAGE_MACHINE_IA64) {
972 PePlusHdr->OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
973 } else if (TEImageHeader->Machine == EFI_IMAGE_MACHINE_X64) {
974 PePlusHdr->OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
975 } else {
976 Error (
977 NULL,
978 0,
979 0,
980 "unknown machine type in TE image",
981 "machine type=0x%X, file=%s",
982 (UINT32) TEImageHeader->Machine,
983 FileGuidString
984 );
985 free (TEBuffer);
986 return EFI_ABORTED;
987 }
988
989 if (PeHdr->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
990 PeHdr->OptionalHeader.ImageBase = (UINTN) (TEImageHeader->ImageBase - TEImageHeader->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER));
991 PeHdr->OptionalHeader.SizeOfImage = Pe32ImageSize;
992 PeHdr->OptionalHeader.Subsystem = TEImageHeader->Subsystem;
993 PeHdr->OptionalHeader.SizeOfHeaders = TEImageHeader->StrippedSize + TEImageHeader->NumberOfSections *
994 sizeof (EFI_IMAGE_SECTION_HEADER) - 12;
995
996 //
997 // Set NumberOfRvaAndSizes in the optional header to what we had available in the original image
998 //
999 if ((TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress != 0) ||
1000 (TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size != 0)
1001 ) {
1002 PeHdr->OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC + 1;
1003 PeHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
1004 PeHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
1005 }
1006
1007 if ((TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) ||
1008 (TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].Size != 0)
1009 ) {
1010 PeHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress = TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
1011 PeHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].Size = TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
1012 if (PeHdr->OptionalHeader.NumberOfRvaAndSizes < EFI_IMAGE_DIRECTORY_ENTRY_DEBUG + 1) {
1013 PeHdr->OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_DIRECTORY_ENTRY_DEBUG + 1;
1014 }
1015 }
1016 //
1017 // NOTE: These values are defaults, and should be verified to be correct in the GenTE utility
1018 //
1019 PeHdr->OptionalHeader.SectionAlignment = 0x10;
1020 } else {
1021 PePlusHdr->OptionalHeader.ImageBase = (UINTN) (TEImageHeader->ImageBase - TEImageHeader->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER));
1022 PePlusHdr->OptionalHeader.SizeOfImage = Pe32ImageSize;
1023 PePlusHdr->OptionalHeader.Subsystem = TEImageHeader->Subsystem;
1024 PePlusHdr->OptionalHeader.SizeOfHeaders = TEImageHeader->StrippedSize + TEImageHeader->NumberOfSections *
1025 sizeof (EFI_IMAGE_SECTION_HEADER) - 12;
1026
1027 //
1028 // Set NumberOfRvaAndSizes in the optional header to what we had available in the original image
1029 //
1030 if ((TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress != 0) ||
1031 (TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size != 0)
1032 ) {
1033 PePlusHdr->OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC + 1;
1034 PePlusHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
1035 PePlusHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
1036 }
1037
1038 if ((TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) ||
1039 (TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].Size != 0)
1040 ) {
1041 PePlusHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress = TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
1042 PePlusHdr->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].Size = TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
1043 if (PePlusHdr->OptionalHeader.NumberOfRvaAndSizes < EFI_IMAGE_DIRECTORY_ENTRY_DEBUG + 1) {
1044 PePlusHdr->OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_DIRECTORY_ENTRY_DEBUG + 1;
1045 }
1046 }
1047 //
1048 // NOTE: These values are defaults, and should be verified to be correct in the GenTE utility
1049 //
1050 PePlusHdr->OptionalHeader.SectionAlignment = 0x10;
1051 }
1052
1053 //
1054 // Copy the rest of the image to its original offset
1055 //
1056 memcpy (
1057 TEBuffer + TEImageHeader->StrippedSize,
1058 (UINT8 *) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION) + sizeof (EFI_TE_IMAGE_HEADER),
1059 GetLength (CurrentPe32Section.Pe32Section->CommonHeader.Size) - sizeof (EFI_PE32_SECTION) -
1060 sizeof (EFI_TE_IMAGE_HEADER)
1061 );
1062
1063 //
1064 // Initialize context
1065 //
1066 memset (&ImageContext, 0, sizeof (ImageContext));
1067 ImageContext.Handle = (VOID *) TEBuffer;
1068 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;
1069
1070 Status = PeCoffLoaderGetImageInfo (&ImageContext);
1071
1072 if (EFI_ERROR (Status)) {
1073 Error (NULL, 0, 0, "GetImageInfo() call failed on rebase of TE image", FileGuidString);
1074 free (TEBuffer);
1075 return Status;
1076 }
1077 //
1078 // Allocate a buffer for the image to be loaded into.
1079 //
1080 MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x100000));
1081 if (MemoryImagePointer == 0) {
1082 Error (NULL, 0, 0, "memory allocation error on rebase of TE image", FileGuidString);
1083 free (TEBuffer);
1084 return EFI_OUT_OF_RESOURCES;
1085 }
1086 memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x100000);
1087 MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFFF) & (-1 << 16);
1088
1089
1090 ImageContext.ImageAddress = MemoryImagePointerAligned;
1091 Status = PeCoffLoaderLoadImage (&ImageContext);
1092 if (EFI_ERROR (Status)) {
1093 Error (NULL, 0, 0, "LoadImage() call failed on rebase of TE image", FileGuidString);
1094 free (TEBuffer);
1095 free ((VOID *) MemoryImagePointer);
1096 return Status;
1097 }
1098
1099 ImageContext.DestinationAddress = NewPe32BaseAddress;
1100 Status = PeCoffLoaderRelocateImage (&ImageContext);
1101 if (EFI_ERROR (Status)) {
1102 Error (NULL, 0, 0, "RelocateImage() call failed on rebase of TE image", FileGuidString);
1103 free ((VOID *) MemoryImagePointer);
1104 free (TEBuffer);
1105 return Status;
1106 }
1107
1108 ImageAddress = ImageContext.ImageAddress;
1109 ImageSize = ImageContext.ImageSize;
1110 EntryPoint = ImageContext.EntryPoint;
1111
1112 //
1113 // Since we may have updated the Codeview RVA, we need to insure the PE
1114 // header indicates the image is large enough to contain the Codeview data
1115 // so it will be loaded properly later if the PEIM is reloaded into memory...
1116 //
1117 PeHdr = (VOID *) ((UINTN) ImageAddress + ImageContext.PeCoffHeaderOffset);
1118 PePlusHdr = (EFI_IMAGE_NT_HEADERS64*)PeHdr;
1119 if (PeHdr->FileHeader.Machine == EFI_IMAGE_MACHINE_IA32) {
1120 PeHdrSizeOfImage = (UINT32 *) (&(PeHdr->OptionalHeader).SizeOfImage);
1121 PeHdrChecksum = (UINT32 *) (&(PeHdr->OptionalHeader).CheckSum);
1122 } else if (PeHdr->FileHeader.Machine == EFI_IMAGE_MACHINE_IA64) {
1123 PeHdrSizeOfImage = (UINT32 *) (&(PePlusHdr->OptionalHeader).SizeOfImage);
1124 PeHdrChecksum = (UINT32 *) (&(PePlusHdr->OptionalHeader).CheckSum);
1125 } else if (PeHdr->FileHeader.Machine == EFI_IMAGE_MACHINE_X64) {
1126 PeHdrSizeOfImage = (UINT32 *) (&(PePlusHdr->OptionalHeader).SizeOfImage);
1127 PeHdrChecksum = (UINT32 *) (&(PePlusHdr->OptionalHeader).CheckSum);
1128 } else {
1129 Error (
1130 NULL,
1131 0,
1132 0,
1133 "unknown machine type in TE image",
1134 "machine type=0x%X, file=%s",
1135 (UINT32) PeHdr->FileHeader.Machine,
1136 FileGuidString
1137 );
1138 free ((VOID *) MemoryImagePointer);
1139 free (TEBuffer);
1140 return EFI_ABORTED;
1141 }
1142
1143 if (*PeHdrSizeOfImage != ImageContext.ImageSize) {
1144 *PeHdrSizeOfImage = (UINT32) ImageContext.ImageSize;
1145 if (*PeHdrChecksum) {
1146 *PeHdrChecksum = 0;
1147 }
1148 }
1149
1150 TEImageHeader->ImageBase = (UINT64) (NewPe32BaseAddress + TEImageHeader->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER));
1151 memcpy (
1152 (UINT8 *) (CurrentPe32Section.Pe32Section + 1) + sizeof (EFI_TE_IMAGE_HEADER),
1153 (VOID *) ((UINT8 *) MemoryImagePointerAligned + TEImageHeader->StrippedSize),
1154 GetLength (CurrentPe32Section.Pe32Section->CommonHeader.Size) - sizeof (EFI_PE32_SECTION) -
1155 sizeof (EFI_TE_IMAGE_HEADER)
1156 );
1157 if (FfsFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
1158 TailSize = sizeof (EFI_FFS_FILE_TAIL);
1159 } else {
1160 TailSize = 0;
1161 }
1162 //
1163 // Now update file checksum
1164 //
1165 if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
1166 SavedState = FfsFile->State;
1167 FfsFile->IntegrityCheck.Checksum.File = 0;
1168 FfsFile->State = 0;
1169 if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
1170 FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
1171 (UINT8 *) FfsFile,
1172 GetLength (FfsFile->Size) - TailSize
1173 );
1174 } else {
1175 FfsFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
1176 }
1177
1178 FfsFile->State = SavedState;
1179 }
1180 //
1181 // Update tail if present
1182 //
1183 if (FfsFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
1184 TailValue = (EFI_FFS_FILE_TAIL) (~(FfsFile->IntegrityCheck.TailReference));
1185 *(EFI_FFS_FILE_TAIL *) (((UINTN) FfsFile + GetLength (FfsFile->Size) - sizeof (EFI_FFS_FILE_TAIL))) = TailValue;
1186 }
1187
1188 fprintf (
1189 LogFile,
1190 "%s %016I64X %s\n",
1191 FileGuidString,
1192 ImageContext.DestinationAddress,
1193 ImageContext.PdbPointer == NULL ? "*" : ImageContext.PdbPointer
1194 );
1195
1196 //
1197 // Free buffers
1198 //
1199 free ((VOID *) MemoryImagePointer);
1200 free (TEBuffer);
1201 }
1202
1203 return EFI_SUCCESS;
1204 }
1205
1206 EFI_STATUS
1207 FfsRebaseImageRead (
1208 IN VOID *FileHandle,
1209 IN UINTN FileOffset,
1210 IN OUT UINT32 *ReadSize,
1211 OUT VOID *Buffer
1212 )
1213 /*++
1214
1215 Routine Description:
1216
1217 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
1218
1219 Arguments:
1220
1221 FileHandle - The handle to the PE/COFF file
1222
1223 FileOffset - The offset, in bytes, into the file to read
1224
1225 ReadSize - The number of bytes to read from the file starting at FileOffset
1226
1227 Buffer - A pointer to the buffer to read the data into.
1228
1229 Returns:
1230
1231 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
1232
1233 --*/
1234 {
1235 CHAR8 *Destination8;
1236 CHAR8 *Source8;
1237 UINT32 Length;
1238
1239 Destination8 = Buffer;
1240 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);
1241 Length = *ReadSize;
1242 while (Length--) {
1243 *(Destination8++) = *(Source8++);
1244 }
1245
1246 return EFI_SUCCESS;
1247 }