1 | /** @file
|
---|
2 | FIT Load Image Support
|
---|
3 | Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
|
---|
4 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
5 | **/
|
---|
6 |
|
---|
7 | #include <PiPei.h>
|
---|
8 | #include <UniversalPayload/UniversalPayload.h>
|
---|
9 | #include <Guid/UniversalPayloadBase.h>
|
---|
10 | #include <UniversalPayload/ExtraData.h>
|
---|
11 |
|
---|
12 | #include <Ppi/LoadFile.h>
|
---|
13 |
|
---|
14 | #include <Library/DebugLib.h>
|
---|
15 | #include <Library/HobLib.h>
|
---|
16 | #include <Library/PeiServicesLib.h>
|
---|
17 | #include <Library/MemoryAllocationLib.h>
|
---|
18 | #include <Library/BaseMemoryLib.h>
|
---|
19 |
|
---|
20 | #include "FitLib.h"
|
---|
21 |
|
---|
22 | /**
|
---|
23 | The wrapper function of PeiLoadImageLoadImage().
|
---|
24 | @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.
|
---|
25 | @param FileHandle - Pointer to the FFS file header of the image.
|
---|
26 | @param ImageAddressArg - Pointer to PE/TE image.
|
---|
27 | @param ImageSizeArg - Size of PE/TE image.
|
---|
28 | @param EntryPoint - Pointer to entry point of specified image file for output.
|
---|
29 | @param AuthenticationState - Pointer to attestation authentication state of image.
|
---|
30 | @return Status of PeiLoadImageLoadImage().
|
---|
31 | **/
|
---|
32 | EFI_STATUS
|
---|
33 | EFIAPI
|
---|
34 | PeiLoadFileLoadPayload (
|
---|
35 | IN CONST EFI_PEI_LOAD_FILE_PPI *This,
|
---|
36 | IN EFI_PEI_FILE_HANDLE FileHandle,
|
---|
37 | OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg OPTIONAL,
|
---|
38 | OUT UINT64 *ImageSizeArg OPTIONAL,
|
---|
39 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint,
|
---|
40 | OUT UINT32 *AuthenticationState
|
---|
41 | )
|
---|
42 | {
|
---|
43 | EFI_STATUS Status;
|
---|
44 | FIT_IMAGE_CONTEXT Context;
|
---|
45 | UINTN Instance;
|
---|
46 | VOID *Binary;
|
---|
47 | FIT_RELOCATE_ITEM *RelocateTable;
|
---|
48 | UNIVERSAL_PAYLOAD_BASE *PayloadBase;
|
---|
49 | UINTN Length;
|
---|
50 | UINTN Delta;
|
---|
51 | UINTN Index;
|
---|
52 |
|
---|
53 | Instance = 0;
|
---|
54 | do {
|
---|
55 | Status = PeiServicesFfsFindSectionData3 (EFI_SECTION_RAW, Instance++, FileHandle, &Binary, AuthenticationState);
|
---|
56 | if (EFI_ERROR (Status)) {
|
---|
57 | return Status;
|
---|
58 | }
|
---|
59 |
|
---|
60 | ZeroMem (&Context, sizeof (Context));
|
---|
61 | Status = ParseFitImage (Binary, &Context);
|
---|
62 | } while (EFI_ERROR (Status));
|
---|
63 |
|
---|
64 | if (EFI_ERROR (Status)) {
|
---|
65 | ASSERT_EFI_ERROR (Status);
|
---|
66 | return Status;
|
---|
67 | }
|
---|
68 |
|
---|
69 | DEBUG ((
|
---|
70 | DEBUG_INFO,
|
---|
71 | "Before Rebase Payload File Base: 0x%08x, File Size: 0x%08X, EntryPoint: 0x%08x\n",
|
---|
72 | Context.PayloadBaseAddress,
|
---|
73 | Context.PayloadSize,
|
---|
74 | Context.PayloadEntryPoint
|
---|
75 | ));
|
---|
76 | Context.PayloadBaseAddress = (EFI_PHYSICAL_ADDRESS)AllocatePages (EFI_SIZE_TO_PAGES (Context.PayloadSize));
|
---|
77 |
|
---|
78 | RelocateTable = (FIT_RELOCATE_ITEM *)(UINTN)(Context.PayloadBaseAddress + Context.RelocateTableOffset);
|
---|
79 | CopyMem ((VOID *)Context.PayloadBaseAddress, Binary, Context.PayloadSize);
|
---|
80 |
|
---|
81 | if (Context.PayloadBaseAddress > Context.PayloadLoadAddress) {
|
---|
82 | Delta = Context.PayloadBaseAddress - Context.PayloadLoadAddress;
|
---|
83 | Context.PayloadEntryPoint += Delta;
|
---|
84 | for (Index = 0; Index < Context.RelocateTableCount; Index++) {
|
---|
85 | if ((RelocateTable[Index].RelocateType == 10) || (RelocateTable[Index].RelocateType == 3)) {
|
---|
86 | *((UINT64 *)(Context.PayloadBaseAddress + RelocateTable[Index].Offset)) = *((UINT64 *)(Context.PayloadBaseAddress + RelocateTable[Index].Offset)) + Delta;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | } else {
|
---|
90 | Delta = Context.PayloadLoadAddress - Context.PayloadBaseAddress;
|
---|
91 | Context.PayloadEntryPoint -= Delta;
|
---|
92 | for (Index = 0; Index < Context.RelocateTableCount; Index++) {
|
---|
93 | if ((RelocateTable[Index].RelocateType == 10) || (RelocateTable[Index].RelocateType == 3)) {
|
---|
94 | *((UINT64 *)(Context.PayloadBaseAddress + RelocateTable[Index].Offset)) = *((UINT64 *)(Context.PayloadBaseAddress + RelocateTable[Index].Offset)) - Delta;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | DEBUG ((
|
---|
100 | DEBUG_INFO,
|
---|
101 | "After Rebase Payload File Base: 0x%08x, File Size: 0x%08X, EntryPoint: 0x%08x\n",
|
---|
102 | Context.PayloadBaseAddress,
|
---|
103 | Context.PayloadSize,
|
---|
104 | Context.PayloadEntryPoint
|
---|
105 | ));
|
---|
106 |
|
---|
107 | Length = sizeof (UNIVERSAL_PAYLOAD_BASE);
|
---|
108 | PayloadBase = BuildGuidHob (
|
---|
109 | &gUniversalPayloadBaseGuid,
|
---|
110 | Length
|
---|
111 | );
|
---|
112 | PayloadBase->Entry = (EFI_PHYSICAL_ADDRESS)Context.ImageBase;
|
---|
113 |
|
---|
114 | *ImageAddressArg = Context.PayloadBaseAddress;
|
---|
115 | *ImageSizeArg = Context.PayloadSize;
|
---|
116 | *EntryPoint = Context.PayloadEntryPoint;
|
---|
117 |
|
---|
118 | return EFI_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|
121 | EFI_PEI_LOAD_FILE_PPI mPeiLoadFilePpi = {
|
---|
122 | PeiLoadFileLoadPayload
|
---|
123 | };
|
---|
124 |
|
---|
125 | EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {
|
---|
126 | (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
|
---|
127 | &gEfiPeiLoadFilePpiGuid,
|
---|
128 | &mPeiLoadFilePpi
|
---|
129 | };
|
---|
130 |
|
---|
131 | /**
|
---|
132 | Install Pei Load File PPI.
|
---|
133 | @param FileHandle Handle of the file being invoked.
|
---|
134 | @param PeiServices Describes the list of possible PEI Services.
|
---|
135 | @retval EFI_SUCESS The entry point executes successfully.
|
---|
136 | @retval Others Some error occurs during the execution of this function.
|
---|
137 | **/
|
---|
138 | EFI_STATUS
|
---|
139 | EFIAPI
|
---|
140 | InitializeFitPayloadLoaderPeim (
|
---|
141 | IN EFI_PEI_FILE_HANDLE FileHandle,
|
---|
142 | IN CONST EFI_PEI_SERVICES **PeiServices
|
---|
143 | )
|
---|
144 | {
|
---|
145 | EFI_STATUS Status;
|
---|
146 |
|
---|
147 | Status = PeiServicesInstallPpi (&gPpiLoadFilePpiList);
|
---|
148 |
|
---|
149 | return Status;
|
---|
150 | }
|
---|