]>
Commit | Line | Data |
---|---|---|
1e57a462 | 1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r | |
3402aac7 | 4 | \r |
1e57a462 | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include <PrePi.h>\r | |
16 | \r | |
17 | //\r | |
18 | // Hack to work in NT32\r | |
19 | //\r | |
20 | EFI_STATUS\r | |
21 | \r | |
22 | EFIAPI\r | |
23 | \r | |
24 | SecWinNtPeiLoadFile (\r | |
25 | IN VOID *Pe32Data,\r | |
26 | IN EFI_PHYSICAL_ADDRESS *ImageAddress,\r | |
27 | IN UINT64 *ImageSize,\r | |
28 | IN EFI_PHYSICAL_ADDRESS *EntryPoint\r | |
29 | );\r | |
30 | \r | |
31 | \r | |
32 | EFI_STATUS\r | |
33 | EFIAPI\r | |
34 | LoadPeCoffImage (\r | |
35 | IN VOID *PeCoffImage,\r | |
36 | OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r | |
37 | OUT UINT64 *ImageSize,\r | |
38 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r | |
39 | )\r | |
40 | {\r | |
41 | RETURN_STATUS Status;\r | |
42 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
43 | VOID *Buffer;\r | |
44 | \r | |
45 | ZeroMem (&ImageContext, sizeof (ImageContext));\r | |
3402aac7 | 46 | \r |
1e57a462 | 47 | ImageContext.Handle = PeCoffImage;\r |
48 | ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r | |
49 | \r | |
50 | Status = PeCoffLoaderGetImageInfo (&ImageContext);\r | |
51 | ASSERT_EFI_ERROR (Status);\r | |
3402aac7 | 52 | \r |
1e57a462 | 53 | \r |
54 | //\r | |
55 | // Allocate Memory for the image\r | |
56 | //\r | |
57 | Buffer = AllocatePages (EFI_SIZE_TO_PAGES((UINT32)ImageContext.ImageSize));\r | |
58 | ASSERT (Buffer != 0);\r | |
59 | \r | |
60 | \r | |
61 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;\r | |
62 | \r | |
63 | //\r | |
64 | // Load the image to our new buffer\r | |
65 | //\r | |
66 | Status = PeCoffLoaderLoadImage (&ImageContext);\r | |
67 | ASSERT_EFI_ERROR (Status);\r | |
68 | \r | |
69 | //\r | |
70 | // Relocate the image in our new buffer\r | |
71 | //\r | |
72 | Status = PeCoffLoaderRelocateImage (&ImageContext);\r | |
73 | ASSERT_EFI_ERROR (Status);\r | |
74 | \r | |
75 | \r | |
76 | *ImageAddress = ImageContext.ImageAddress;\r | |
77 | *ImageSize = ImageContext.ImageSize;\r | |
78 | *EntryPoint = ImageContext.EntryPoint;\r | |
79 | \r | |
80 | //\r | |
81 | // Flush not needed for all architectures. We could have a processor specific\r | |
82 | // function in this library that does the no-op if needed.\r | |
83 | //\r | |
84 | InvalidateInstructionCacheRange ((VOID *)(UINTN)*ImageAddress, (UINTN)*ImageSize);\r | |
85 | \r | |
86 | return Status;\r | |
87 | }\r | |
88 | \r | |
89 | \r | |
90 | \r | |
3402aac7 | 91 | typedef\r |
1e57a462 | 92 | VOID\r |
93 | (EFIAPI *DXE_CORE_ENTRY_POINT) (\r | |
94 | IN VOID *HobStart\r | |
95 | );\r | |
96 | \r | |
97 | EFI_STATUS\r | |
98 | EFIAPI\r | |
99 | LoadDxeCoreFromFfsFile (\r | |
100 | IN EFI_PEI_FILE_HANDLE FileHandle,\r | |
101 | IN UINTN StackSize\r | |
102 | )\r | |
103 | {\r | |
104 | EFI_STATUS Status;\r | |
105 | VOID *PeCoffImage;\r | |
106 | EFI_PHYSICAL_ADDRESS ImageAddress;\r | |
107 | UINT64 ImageSize;\r | |
108 | EFI_PHYSICAL_ADDRESS EntryPoint;\r | |
109 | VOID *BaseOfStack;\r | |
110 | VOID *TopOfStack;\r | |
111 | VOID *Hob;\r | |
112 | EFI_FV_FILE_INFO FvFileInfo;\r | |
113 | \r | |
114 | Status = FfsFindSectionData (EFI_SECTION_PE32, FileHandle, &PeCoffImage);\r | |
115 | if (EFI_ERROR (Status)) {\r | |
116 | return Status;\r | |
117 | }\r | |
118 | \r | |
3402aac7 | 119 | \r |
1e57a462 | 120 | Status = LoadPeCoffImage (PeCoffImage, &ImageAddress, &ImageSize, &EntryPoint);\r |
121 | // For NT32 Debug Status = SecWinNtPeiLoadFile (PeCoffImage, &ImageAddress, &ImageSize, &EntryPoint);\r | |
122 | ASSERT_EFI_ERROR (Status);\r | |
123 | \r | |
124 | //\r | |
125 | // Extract the DxeCore GUID file name.\r | |
126 | //\r | |
127 | Status = FfsGetFileInfo (FileHandle, &FvFileInfo);\r | |
128 | ASSERT_EFI_ERROR (Status);\r | |
129 | \r | |
130 | BuildModuleHob (&FvFileInfo.FileName, (EFI_PHYSICAL_ADDRESS)(UINTN)ImageAddress, EFI_SIZE_TO_PAGES ((UINT32) ImageSize) * EFI_PAGE_SIZE, EntryPoint);\r | |
3402aac7 | 131 | \r |
1e57a462 | 132 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading DxeCore at 0x%10p EntryPoint=0x%10p\n", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)EntryPoint));\r |
133 | \r | |
134 | Hob = GetHobList ();\r | |
135 | if (StackSize == 0) {\r | |
136 | // User the current stack\r | |
3402aac7 | 137 | \r |
1e57a462 | 138 | ((DXE_CORE_ENTRY_POINT)(UINTN)EntryPoint) (Hob);\r |
139 | } else {\r | |
3402aac7 | 140 | \r |
1e57a462 | 141 | //\r |
142 | // Allocate 128KB for the Stack\r | |
143 | //\r | |
144 | BaseOfStack = AllocatePages (EFI_SIZE_TO_PAGES (StackSize));\r | |
145 | ASSERT (BaseOfStack != NULL);\r | |
3402aac7 | 146 | \r |
1e57a462 | 147 | //\r |
148 | // Compute the top of the stack we were allocated. Pre-allocate a UINTN\r | |
149 | // for safety.\r | |
150 | //\r | |
151 | TopOfStack = (VOID *) ((UINTN) BaseOfStack + EFI_SIZE_TO_PAGES (StackSize) * EFI_PAGE_SIZE - CPU_STACK_ALIGNMENT);\r | |
152 | TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r | |
153 | \r | |
154 | //\r | |
155 | // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.\r | |
3402aac7 | 156 | //\r |
1e57a462 | 157 | UpdateStackHob ((EFI_PHYSICAL_ADDRESS)(UINTN) BaseOfStack, StackSize);\r |
158 | \r | |
159 | SwitchStack (\r | |
160 | (SWITCH_STACK_ENTRY_POINT)(UINTN)EntryPoint,\r | |
161 | Hob,\r | |
162 | NULL,\r | |
163 | TopOfStack\r | |
164 | );\r | |
165 | \r | |
166 | }\r | |
3402aac7 | 167 | \r |
1e57a462 | 168 | // Should never get here as DXE Core does not return\r |
169 | DEBUG ((EFI_D_ERROR, "DxeCore returned\n"));\r | |
170 | ASSERT (FALSE);\r | |
3402aac7 | 171 | \r |
1e57a462 | 172 | return EFI_DEVICE_ERROR;\r |
173 | }\r | |
174 | \r | |
175 | \r | |
176 | \r | |
177 | EFI_STATUS\r | |
178 | EFIAPI\r | |
179 | LoadDxeCoreFromFv (\r | |
180 | IN UINTN *FvInstance, OPTIONAL\r | |
181 | IN UINTN StackSize\r | |
182 | )\r | |
183 | {\r | |
184 | EFI_STATUS Status;\r | |
185 | EFI_PEI_FV_HANDLE VolumeHandle;\r | |
186 | EFI_PEI_FILE_HANDLE FileHandle = NULL;\r | |
187 | \r | |
188 | if (FvInstance != NULL) {\r | |
189 | //\r | |
190 | // Caller passed in a specific FV to try, so only try that one\r | |
191 | //\r | |
192 | Status = FfsFindNextVolume (*FvInstance, &VolumeHandle);\r | |
193 | if (!EFI_ERROR (Status)) {\r | |
194 | Status = FfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE, VolumeHandle, &FileHandle);\r | |
195 | }\r | |
196 | } else {\r | |
197 | Status = FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_DXE_CORE, &VolumeHandle, &FileHandle);\r | |
198 | }\r | |
199 | \r | |
200 | if (!EFI_ERROR (Status)) {\r | |
201 | return LoadDxeCoreFromFfsFile (FileHandle, StackSize);\r | |
3402aac7 RC |
202 | }\r |
203 | \r | |
204 | return Status;\r | |
1e57a462 | 205 | }\r |
206 | \r | |
207 | \r | |
208 | EFI_STATUS\r | |
209 | EFIAPI\r | |
210 | DecompressFirstFv (\r | |
211 | VOID\r | |
212 | )\r | |
213 | {\r | |
214 | EFI_STATUS Status;\r | |
215 | EFI_PEI_FV_HANDLE VolumeHandle;\r | |
216 | EFI_PEI_FILE_HANDLE FileHandle;\r | |
217 | \r | |
218 | Status = FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, &VolumeHandle, &FileHandle);\r | |
219 | if (!EFI_ERROR (Status)) {\r | |
220 | Status = FfsProcessFvFile (FileHandle);\r | |
221 | }\r | |
3402aac7 | 222 | \r |
1e57a462 | 223 | return Status;\r |
224 | }\r | |
225 | \r | |
226 | \r |