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 AsciiSPrint (PrintBuffer
, 256, "BFV decompress: DestinationSize=0x%X, ScratchSize=0x%X!\n",
114 DestinationSize
, ScratchSize
);
115 PrintString (PrintBuffer
);
117 Status
= TianoDecompress (
118 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
120 (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
,
122 (VOID
*)(UINTN
)((EFI_DECOMPRESSED_BUFFER_ADDRESS
+ DestinationSize
+ 0x1000) & 0xfffff000),
126 if (EFI_ERROR (Status
)) {
127 AsciiSPrint (PrintBuffer
, 256, "Fail to decompress BFV!\n");
128 PrintString (PrintBuffer
);
132 BfvPageNumber
= EFI_SIZE_TO_PAGES (DestinationSize
);
133 BfvBase
= (UINTN
) FindSpace (BfvPageNumber
, &NumberOfMemoryMapEntries
, EfiMemoryDescriptor
, EfiRuntimeServicesData
, EFI_MEMORY_WB
);
137 ZeroMem ((VOID
*)(UINTN
)BfvBase
, BfvPageNumber
* EFI_PAGE_SIZE
);
138 CopyMem ((VOID
*)(UINTN
)BfvBase
, (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
, DestinationSize
);
143 // Point to the 2nd image (DxeIpl)
149 // Decompress the image
151 AsciiSPrint (PrintBuffer
, 256, "Decompress DxeIpl image, Image Address=0x%x Offset=0x%x\n",
152 (UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
153 EFILDRImage
->Offset
);
154 PrintString (PrintBuffer
);
156 Status
= TianoGetInfo (
157 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
162 if (EFI_ERROR (Status
)) {
163 AsciiSPrint (PrintBuffer
, 256, "Fail to get decompress information for DxeIpl!\n");
164 PrintString (PrintBuffer
);
168 Status
= TianoDecompress (
169 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
171 (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
,
173 (VOID
*)(UINTN
)((EFI_DECOMPRESSED_BUFFER_ADDRESS
+ DestinationSize
+ 0x1000) & 0xfffff000),
176 if (EFI_ERROR (Status
)) {
177 AsciiSPrint (PrintBuffer
, 256, "Fail to decompress DxeIpl image\n");
178 PrintString (PrintBuffer
);
182 AsciiSPrint (PrintBuffer
, 256, "Start load DxeIpl PE image\n");
183 PrintString (PrintBuffer
);
186 // Load and relocate the EFI PE/COFF Firmware Image
188 Status
= EfiLdrPeCoffLoadPeImage (
189 (VOID
*)(UINTN
)(EFI_DECOMPRESSED_BUFFER_ADDRESS
),
191 &NumberOfMemoryMapEntries
,
194 if (EFI_ERROR (Status
)) {
195 AsciiSPrint (PrintBuffer
, 256, "Fail to load and relocate DxeIpl PE image!\n");
196 PrintString (PrintBuffer
);
199 AsciiSPrint (PrintBuffer
, 256, "DxeIpl PE image is successed loaded at 0x%x, entry=0x%x\n",
200 (UINTN
)DxeIplImage
.ImageBasePage
, (UINTN
)DxeIplImage
.EntryPoint
);
201 PrintString (PrintBuffer
);
203 // PrintString("Image.NoPages = ");
204 // PrintValue(Image.NoPages);
205 // PrintString("\n");
210 // Point to the 3rd image (DxeMain)
216 // Decompress the image
218 AsciiSPrint (PrintBuffer
, 256, "Decompress DXEMain FV image, Image Address=0x%x! Offset=0x%x\n",
219 (UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
220 EFILDRImage
->Offset
);
221 PrintString (PrintBuffer
);
223 Status
= TianoGetInfo (
224 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
229 if (EFI_ERROR (Status
)) {
230 AsciiSPrint (PrintBuffer
, 256, "Fail to get decompress information for DXEMain FV image!\n");
231 PrintString (PrintBuffer
);
235 Status
= TianoDecompress (
236 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
238 (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
,
240 (VOID
*)(UINTN
)((EFI_DECOMPRESSED_BUFFER_ADDRESS
+ DestinationSize
+ 0x1000) & 0xfffff000),
243 if (EFI_ERROR (Status
)) {
248 // Load and relocate the EFI PE/COFF Firmware Image
250 Status
= EfiLdrPeCoffLoadPeImage (
251 (VOID
*)(UINTN
)(EFI_DECOMPRESSED_BUFFER_ADDRESS
),
253 &NumberOfMemoryMapEntries
,
256 if (EFI_ERROR (Status
)) {
259 AsciiSPrint (PrintBuffer
, 256, "DxeCore PE image is successed loaded at 0x%x, entry=0x%x\n",
260 (UINTN
)DxeCoreImage
.ImageBasePage
, (UINTN
)DxeCoreImage
.EntryPoint
);
261 PrintString (PrintBuffer
);
266 // Display the table of memory descriptors.
269 // PrintString("\nEFI Memory Descriptors\n");
273 for (Index = 0; Index < NumberOfMemoryMapEntries; Index++) {
274 PrintString("Type = ");
275 PrintValue(EfiMemoryDescriptor[Index].Type);
276 PrintString(" Start = ");
277 PrintValue((UINT32)(EfiMemoryDescriptor[Index].PhysicalStart));
278 PrintString(" NumberOfPages = ");
279 PrintValue((UINT32)(EfiMemoryDescriptor[Index].NumberOfPages));
286 // Jump to EFI Firmware
289 if (DxeIplImage
.EntryPoint
!= NULL
) {
291 Handoff
.MemDescCount
= NumberOfMemoryMapEntries
;
292 Handoff
.MemDesc
= EfiMemoryDescriptor
;
293 Handoff
.BfvBase
= (VOID
*)(UINTN
)BfvBase
;
294 Handoff
.BfvSize
= BfvPageNumber
* EFI_PAGE_SIZE
;
295 Handoff
.DxeIplImageBase
= (VOID
*)(UINTN
)DxeIplImage
.ImageBasePage
;
296 Handoff
.DxeIplImageSize
= DxeIplImage
.NoPages
* EFI_PAGE_SIZE
;
297 Handoff
.DxeCoreImageBase
= (VOID
*)(UINTN
)DxeCoreImage
.ImageBasePage
;
298 Handoff
.DxeCoreImageSize
= DxeCoreImage
.NoPages
* EFI_PAGE_SIZE
;
299 Handoff
.DxeCoreEntryPoint
= (VOID
*)(UINTN
)DxeCoreImage
.EntryPoint
;
301 AsciiSPrint (PrintBuffer
, 256, "Transfer to DxeIpl ...Address=0x%x\n", (UINTN
)DxeIplImage
.EntryPoint
);
302 PrintString (PrintBuffer
);
304 EfiMainEntrypoint
= (EFI_MAIN_ENTRYPOINT
)(UINTN
)DxeIplImage
.EntryPoint
;
305 EfiMainEntrypoint (&Handoff
);
311 // There was a problem loading the image, so HALT the system.
320 UINT32 BiosMemoryMapBaseAddress
323 EfiLoader(BiosMemoryMapBaseAddress
);