]> git.proxmox.com Git - mirror_edk2.git/blob - DuetPkg/EfiLdr/EfiLoader.c
Add missing module for duet package.
[mirror_edk2.git] / DuetPkg / EfiLdr / EfiLoader.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. 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 EfiLoader.c
14
15 Abstract:
16
17 Revision History:
18
19 --*/
20
21 #include "EfiLdr.h"
22 #include "Support.h"
23 #include "Debug.h"
24 #include "PeLoader.h"
25
26 VOID
27 EfiLoader (
28 UINT32 BiosMemoryMapBaseAddress
29 )
30 {
31 BIOS_MEMORY_MAP *BiosMemoryMap;
32 EFILDR_HEADER *EFILDRHeader;
33 EFILDR_IMAGE *EFILDRImage;
34 EFI_MEMORY_DESCRIPTOR EfiMemoryDescriptor[EFI_MAX_MEMORY_DESCRIPTORS];
35 EFI_STATUS Status;
36 UINTN NumberOfMemoryMapEntries;
37 UINT32 DestinationSize;
38 UINT32 ScratchSize;
39 UINTN BfvPageNumber;
40 UINTN BfvBase;
41 EFI_MAIN_ENTRYPOINT EfiMainEntrypoint;
42 static EFILDRHANDOFF Handoff;
43
44 PrintHeader ('A');
45
46 ClearScreen();
47 PrintString("EFI Loader\n");
48
49 // PrintString("&BiosMemoryMapBaseAddress = ");
50 // PrintValue64 ((UINT64)(&BiosMemoryMapBaseAddress));
51 // PrintString(" BiosMemoryMapBaseAddress = ");
52 // PrintValue(BiosMemoryMapBaseAddress);
53 // PrintString("\n");
54
55 //
56 // Add all EfiConventionalMemory descriptors to the table. If there are partial pages, then
57 // round the start address up to the next page, and round the length down to a page boundry.
58 //
59 BiosMemoryMap = (BIOS_MEMORY_MAP *)(UINTN)(BiosMemoryMapBaseAddress);
60 NumberOfMemoryMapEntries = 0;
61 GenMemoryMap (&NumberOfMemoryMapEntries, EfiMemoryDescriptor, BiosMemoryMap);
62
63 //
64 // Get information on where the image is in memory
65 //
66
67 EFILDRHeader = (EFILDR_HEADER *)(UINTN)(EFILDR_HEADER_ADDRESS);
68 EFILDRImage = (EFILDR_IMAGE *)(UINTN)(EFILDR_HEADER_ADDRESS + sizeof(EFILDR_HEADER));
69
70 PrintHeader ('D');
71
72 //
73 // Point to the 4th image (Bfv)
74 //
75
76 EFILDRImage += 3;
77
78 //
79 // Decompress the image
80 //
81
82 Status = UefiDecompressGetInfo (
83 NULL,
84 (VOID *)(UINTN)(EFILDR_HEADER_ADDRESS + EFILDRImage->Offset),
85 EFILDRImage->Length,
86 &DestinationSize,
87 &ScratchSize
88 );
89 if (EFI_ERROR (Status)) {
90 CpuDeadLoop();
91 }
92
93 Status = TianoDecompress (
94 NULL,
95 (VOID *)(UINTN)(EFILDR_HEADER_ADDRESS + EFILDRImage->Offset),
96 EFILDRImage->Length,
97 (VOID *)(UINTN)EFI_DECOMPRESSED_BUFFER_ADDRESS,
98 DestinationSize,
99 (VOID *)(UINTN)((EFI_DECOMPRESSED_BUFFER_ADDRESS + DestinationSize + 0x1000) & 0xfffff000),
100 ScratchSize
101 );
102 if (EFI_ERROR (Status)) {
103 CpuDeadLoop();
104 }
105
106 BfvPageNumber = EFI_SIZE_TO_PAGES (DestinationSize);
107 BfvBase = (UINTN) FindSpace (BfvPageNumber, &NumberOfMemoryMapEntries, EfiMemoryDescriptor, EfiRuntimeServicesData, EFI_MEMORY_WB);
108 if (BfvBase == 0) {
109 CpuDeadLoop();
110 }
111 ZeroMem ((VOID *)(UINTN)BfvBase, BfvPageNumber * EFI_PAGE_SIZE);
112 CopyMem ((VOID *)(UINTN)BfvBase, (VOID *)(UINTN)EFI_DECOMPRESSED_BUFFER_ADDRESS, DestinationSize);
113
114 PrintHeader ('B');
115
116 //
117 // Point to the 2nd image (DxeIpl)
118 //
119
120 EFILDRImage -= 2;
121
122 //
123 // Decompress the image
124 //
125
126 Status = UefiDecompressGetInfo (
127 NULL,
128 (VOID *)(UINTN)(EFILDR_HEADER_ADDRESS + EFILDRImage->Offset),
129 EFILDRImage->Length,
130 &DestinationSize,
131 &ScratchSize
132 );
133 if (EFI_ERROR (Status)) {
134 CpuDeadLoop();
135 }
136
137 Status = TianoDecompress (
138 NULL,
139 (VOID *)(UINTN)(EFILDR_HEADER_ADDRESS + EFILDRImage->Offset),
140 EFILDRImage->Length,
141 (VOID *)(UINTN)EFI_DECOMPRESSED_BUFFER_ADDRESS,
142 DestinationSize,
143 (VOID *)(UINTN)((EFI_DECOMPRESSED_BUFFER_ADDRESS + DestinationSize + 0x1000) & 0xfffff000),
144 ScratchSize
145 );
146 if (EFI_ERROR (Status)) {
147 CpuDeadLoop();
148 }
149
150 //
151 // Load and relocate the EFI PE/COFF Firmware Image
152 //
153 Status = EfiLdrPeCoffLoadPeImage (
154 (VOID *)(UINTN)(EFI_DECOMPRESSED_BUFFER_ADDRESS),
155 &DxeIplImage,
156 &NumberOfMemoryMapEntries,
157 EfiMemoryDescriptor
158 );
159 if (EFI_ERROR (Status)) {
160 CpuDeadLoop();
161 }
162
163 // PrintString("Image.NoPages = ");
164 // PrintValue(Image.NoPages);
165 // PrintString("\n");
166
167 PrintHeader ('C');
168
169 //
170 // Point to the 3rd image (DxeMain)
171 //
172
173 EFILDRImage++;
174
175 //
176 // Decompress the image
177 //
178
179 Status = UefiDecompressGetInfo (
180 NULL,
181 (VOID *)(UINTN)(EFILDR_HEADER_ADDRESS + EFILDRImage->Offset),
182 EFILDRImage->Length,
183 &DestinationSize,
184 &ScratchSize
185 );
186 if (EFI_ERROR (Status)) {
187 CpuDeadLoop();
188 }
189
190 Status = TianoDecompress (
191 NULL,
192 (VOID *)(UINTN)(EFILDR_HEADER_ADDRESS + EFILDRImage->Offset),
193 EFILDRImage->Length,
194 (VOID *)(UINTN)EFI_DECOMPRESSED_BUFFER_ADDRESS,
195 DestinationSize,
196 (VOID *)(UINTN)((EFI_DECOMPRESSED_BUFFER_ADDRESS + DestinationSize + 0x1000) & 0xfffff000),
197 ScratchSize
198 );
199 if (EFI_ERROR (Status)) {
200 CpuDeadLoop();
201 }
202
203 //
204 // Load and relocate the EFI PE/COFF Firmware Image
205 //
206 Status = EfiLdrPeCoffLoadPeImage (
207 (VOID *)(UINTN)(EFI_DECOMPRESSED_BUFFER_ADDRESS),
208 &DxeCoreImage,
209 &NumberOfMemoryMapEntries,
210 EfiMemoryDescriptor
211 );
212 if (EFI_ERROR (Status)) {
213 CpuDeadLoop();
214 }
215
216 PrintHeader ('E');
217
218 //
219 // Display the table of memory descriptors.
220 //
221
222 // PrintString("\nEFI Memory Descriptors\n");
223 /*
224 {
225 UINTN Index;
226 for (Index = 0; Index < NumberOfMemoryMapEntries; Index++) {
227 PrintString("Type = ");
228 PrintValue(EfiMemoryDescriptor[Index].Type);
229 PrintString(" Start = ");
230 PrintValue((UINT32)(EfiMemoryDescriptor[Index].PhysicalStart));
231 PrintString(" NumberOfPages = ");
232 PrintValue((UINT32)(EfiMemoryDescriptor[Index].NumberOfPages));
233 PrintString("\n");
234 }
235 }
236 */
237
238 //
239 // Jump to EFI Firmware
240 //
241
242 if (DxeIplImage.EntryPoint != NULL) {
243
244 Handoff.MemDescCount = NumberOfMemoryMapEntries;
245 Handoff.MemDesc = EfiMemoryDescriptor;
246 Handoff.BfvBase = (VOID *)(UINTN)BfvBase;
247 Handoff.BfvSize = BfvPageNumber * EFI_PAGE_SIZE;
248 Handoff.DxeIplImageBase = (VOID *)(UINTN)DxeIplImage.ImageBasePage;
249 Handoff.DxeIplImageSize = DxeIplImage.NoPages * EFI_PAGE_SIZE;
250 Handoff.DxeCoreImageBase = (VOID *)(UINTN)DxeCoreImage.ImageBasePage;
251 Handoff.DxeCoreImageSize = DxeCoreImage.NoPages * EFI_PAGE_SIZE;
252 Handoff.DxeCoreEntryPoint = (VOID *)(UINTN)DxeCoreImage.EntryPoint;
253
254 EfiMainEntrypoint = (EFI_MAIN_ENTRYPOINT)(UINTN)DxeIplImage.EntryPoint;
255 EfiMainEntrypoint (&Handoff);
256 }
257
258 PrintHeader ('F');
259
260 //
261 // There was a problem loading the image, so HALT the system.
262 //
263
264 CpuDeadLoop();
265 }
266