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
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.
25 #include "TianoDecompress.h"
32 CHAR8 PrintBuffer
[256];
33 AsciiSPrint (PrintBuffer
, 256, "## FATEL ERROR ##: Fail to load DUET images! System hang!\n");
39 UINT32 BiosMemoryMapBaseAddress
42 BIOS_MEMORY_MAP
*BiosMemoryMap
;
43 EFILDR_HEADER
*EFILDRHeader
;
44 EFILDR_IMAGE
*EFILDRImage
;
45 EFI_MEMORY_DESCRIPTOR EfiMemoryDescriptor
[EFI_MAX_MEMORY_DESCRIPTORS
];
47 UINTN NumberOfMemoryMapEntries
;
48 UINT32 DestinationSize
;
52 EFI_MAIN_ENTRYPOINT EfiMainEntrypoint
;
53 CHAR8 PrintBuffer
[256];
54 STATIC EFILDRHANDOFF Handoff
;
60 AsciiSPrint (PrintBuffer
, 256, "Enter DUET Loader ...\n", BiosMemoryMapBaseAddress
);
61 PrintString (PrintBuffer
);
63 AsciiSPrint (PrintBuffer
, 256, "BiosMemoryMapBaseAddress = 0x%x\n", BiosMemoryMapBaseAddress
);
64 PrintString (PrintBuffer
);
67 // Add all EfiConventionalMemory descriptors to the table. If there are partial pages, then
68 // round the start address up to the next page, and round the length down to a page boundry.
70 BiosMemoryMap
= (BIOS_MEMORY_MAP
*)(UINTN
)(BiosMemoryMapBaseAddress
);
71 NumberOfMemoryMapEntries
= 0;
72 GenMemoryMap (&NumberOfMemoryMapEntries
, EfiMemoryDescriptor
, BiosMemoryMap
);
74 AsciiSPrint (PrintBuffer
, 256, "Get %d entries of memory map!\n", NumberOfMemoryMapEntries
);
75 PrintString (PrintBuffer
);
78 // Get information on where the image is in memory
81 EFILDRHeader
= (EFILDR_HEADER
*)(UINTN
)(EFILDR_HEADER_ADDRESS
);
82 EFILDRImage
= (EFILDR_IMAGE
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ sizeof(EFILDR_HEADER
));
86 // Point to the 4th image (Bfv)
92 // Decompress the image
95 AsciiSPrint (PrintBuffer
, 256, "Decompress BFV image, Image Address=0x%x Offset=0x%x\n",
96 (UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
98 PrintString (PrintBuffer
);
100 Status
= TianoGetInfo (
101 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
107 if (EFI_ERROR (Status
)) {
108 AsciiSPrint (PrintBuffer
, 256, "Fail to get decompress information for BFV!\n");
109 PrintString (PrintBuffer
);
113 Status
= TianoDecompress (
114 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
116 (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
,
118 (VOID
*)(UINTN
)((EFI_DECOMPRESSED_BUFFER_ADDRESS
+ DestinationSize
+ 0x1000) & 0xfffff000),
122 if (EFI_ERROR (Status
)) {
123 AsciiSPrint (PrintBuffer
, 256, "Fail to decompress BFV!\n");
124 PrintString (PrintBuffer
);
128 BfvPageNumber
= EFI_SIZE_TO_PAGES (DestinationSize
);
129 BfvBase
= (UINTN
) FindSpace (BfvPageNumber
, &NumberOfMemoryMapEntries
, EfiMemoryDescriptor
, EfiRuntimeServicesData
, EFI_MEMORY_WB
);
133 ZeroMem ((VOID
*)(UINTN
)BfvBase
, BfvPageNumber
* EFI_PAGE_SIZE
);
134 CopyMem ((VOID
*)(UINTN
)BfvBase
, (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
, DestinationSize
);
139 // Point to the 2nd image (DxeIpl)
145 // Decompress the image
147 AsciiSPrint (PrintBuffer
, 256, "Decompress DxeIpl image, Image Address=0x%x Offset=0x%x\n",
148 (UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
149 EFILDRImage
->Offset
);
150 PrintString (PrintBuffer
);
152 Status
= TianoGetInfo (
153 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
158 if (EFI_ERROR (Status
)) {
159 AsciiSPrint (PrintBuffer
, 256, "Fail to get decompress information for DxeIpl!\n");
160 PrintString (PrintBuffer
);
164 Status
= TianoDecompress (
165 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
167 (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
,
169 (VOID
*)(UINTN
)((EFI_DECOMPRESSED_BUFFER_ADDRESS
+ DestinationSize
+ 0x1000) & 0xfffff000),
172 if (EFI_ERROR (Status
)) {
173 AsciiSPrint (PrintBuffer
, 256, "Fail to decompress DxeIpl image\n");
174 PrintString (PrintBuffer
);
178 AsciiSPrint (PrintBuffer
, 256, "Start load DxeIpl PE image\n");
179 PrintString (PrintBuffer
);
182 // Load and relocate the EFI PE/COFF Firmware Image
184 Status
= EfiLdrPeCoffLoadPeImage (
185 (VOID
*)(UINTN
)(EFI_DECOMPRESSED_BUFFER_ADDRESS
),
187 &NumberOfMemoryMapEntries
,
190 if (EFI_ERROR (Status
)) {
191 AsciiSPrint (PrintBuffer
, 256, "Fail to load and relocate DxeIpl PE image!\n");
192 PrintString (PrintBuffer
);
195 AsciiSPrint (PrintBuffer
, 256, "DxeIpl PE image is successed loaded at 0x%x, entry=0x%x\n",
196 (UINTN
)DxeIplImage
.ImageBasePage
, (UINTN
)DxeIplImage
.EntryPoint
);
197 PrintString (PrintBuffer
);
199 // PrintString("Image.NoPages = ");
200 // PrintValue(Image.NoPages);
201 // PrintString("\n");
206 // Point to the 3rd image (DxeMain)
212 // Decompress the image
214 AsciiSPrint (PrintBuffer
, 256, "Decompress DXEMain FV image, Image Address=0x%x! Offset=0x%x\n",
215 (UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
216 EFILDRImage
->Offset
);
217 PrintString (PrintBuffer
);
219 Status
= TianoGetInfo (
220 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
225 if (EFI_ERROR (Status
)) {
226 AsciiSPrint (PrintBuffer
, 256, "Fail to get decompress information for DXEMain FV image!\n");
227 PrintString (PrintBuffer
);
231 Status
= TianoDecompress (
232 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
234 (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
,
236 (VOID
*)(UINTN
)((EFI_DECOMPRESSED_BUFFER_ADDRESS
+ DestinationSize
+ 0x1000) & 0xfffff000),
239 if (EFI_ERROR (Status
)) {
244 // Load and relocate the EFI PE/COFF Firmware Image
246 Status
= EfiLdrPeCoffLoadPeImage (
247 (VOID
*)(UINTN
)(EFI_DECOMPRESSED_BUFFER_ADDRESS
),
249 &NumberOfMemoryMapEntries
,
252 if (EFI_ERROR (Status
)) {
255 AsciiSPrint (PrintBuffer
, 256, "DxeCore PE image is successed loaded at 0x%x, entry=0x%x\n",
256 (UINTN
)DxeCoreImage
.ImageBasePage
, (UINTN
)DxeCoreImage
.EntryPoint
);
257 PrintString (PrintBuffer
);
262 // Display the table of memory descriptors.
265 // PrintString("\nEFI Memory Descriptors\n");
269 for (Index = 0; Index < NumberOfMemoryMapEntries; Index++) {
270 PrintString("Type = ");
271 PrintValue(EfiMemoryDescriptor[Index].Type);
272 PrintString(" Start = ");
273 PrintValue((UINT32)(EfiMemoryDescriptor[Index].PhysicalStart));
274 PrintString(" NumberOfPages = ");
275 PrintValue((UINT32)(EfiMemoryDescriptor[Index].NumberOfPages));
282 // Jump to EFI Firmware
285 if (DxeIplImage
.EntryPoint
!= NULL
) {
287 Handoff
.MemDescCount
= NumberOfMemoryMapEntries
;
288 Handoff
.MemDesc
= EfiMemoryDescriptor
;
289 Handoff
.BfvBase
= (VOID
*)(UINTN
)BfvBase
;
290 Handoff
.BfvSize
= BfvPageNumber
* EFI_PAGE_SIZE
;
291 Handoff
.DxeIplImageBase
= (VOID
*)(UINTN
)DxeIplImage
.ImageBasePage
;
292 Handoff
.DxeIplImageSize
= DxeIplImage
.NoPages
* EFI_PAGE_SIZE
;
293 Handoff
.DxeCoreImageBase
= (VOID
*)(UINTN
)DxeCoreImage
.ImageBasePage
;
294 Handoff
.DxeCoreImageSize
= DxeCoreImage
.NoPages
* EFI_PAGE_SIZE
;
295 Handoff
.DxeCoreEntryPoint
= (VOID
*)(UINTN
)DxeCoreImage
.EntryPoint
;
297 AsciiSPrint (PrintBuffer
, 256, "Transfer to DxeIpl ...Address=0x%x\n", (UINTN
)DxeIplImage
.EntryPoint
);
298 PrintString (PrintBuffer
);
300 EfiMainEntrypoint
= (EFI_MAIN_ENTRYPOINT
)(UINTN
)DxeIplImage
.EntryPoint
;
301 EfiMainEntrypoint (&Handoff
);
307 // There was a problem loading the image, so HALT the system.
316 UINT32 BiosMemoryMapBaseAddress
319 EfiLoader(BiosMemoryMapBaseAddress
);