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"
33 CHAR8 PrintBuffer
[256];
34 AsciiSPrint (PrintBuffer
, 256, "## FATEL ERROR ##: Fail to load DUET images! System hang!\n");
40 UINT32 BiosMemoryMapBaseAddress
43 BIOS_MEMORY_MAP
*BiosMemoryMap
;
44 EFILDR_HEADER
*EFILDRHeader
;
45 EFILDR_IMAGE
*EFILDRImage
;
46 EFI_MEMORY_DESCRIPTOR EfiMemoryDescriptor
[EFI_MAX_MEMORY_DESCRIPTORS
];
48 UINTN NumberOfMemoryMapEntries
;
49 UINT32 DestinationSize
;
53 EFI_MAIN_ENTRYPOINT EfiMainEntrypoint
;
54 CHAR8 PrintBuffer
[256];
55 static EFILDRHANDOFF Handoff
;
61 AsciiSPrint (PrintBuffer
, 256, "Enter DUET Loader ...\n", BiosMemoryMapBaseAddress
);
62 PrintString (PrintBuffer
);
64 AsciiSPrint (PrintBuffer
, 256, "BiosMemoryMapBaseAddress = 0x%x\n", BiosMemoryMapBaseAddress
);
65 PrintString (PrintBuffer
);
68 // Add all EfiConventionalMemory descriptors to the table. If there are partial pages, then
69 // round the start address up to the next page, and round the length down to a page boundry.
71 BiosMemoryMap
= (BIOS_MEMORY_MAP
*)(UINTN
)(BiosMemoryMapBaseAddress
);
72 NumberOfMemoryMapEntries
= 0;
73 GenMemoryMap (&NumberOfMemoryMapEntries
, EfiMemoryDescriptor
, BiosMemoryMap
);
75 AsciiSPrint (PrintBuffer
, 256, "Get %d entries of memory map!\n", NumberOfMemoryMapEntries
);
76 PrintString (PrintBuffer
);
79 // Get information on where the image is in memory
82 EFILDRHeader
= (EFILDR_HEADER
*)(UINTN
)(EFILDR_HEADER_ADDRESS
);
83 EFILDRImage
= (EFILDR_IMAGE
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ sizeof(EFILDR_HEADER
));
87 // Point to the 4th image (Bfv)
93 // Decompress the image
96 AsciiSPrint (PrintBuffer
, 256, "Decompress BFV image, Image Address=0x%x! Offset=0x%x\n",
97 (UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
99 PrintString (PrintBuffer
);
101 Status
= TianoGetInfo (
102 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
108 if (EFI_ERROR (Status
)) {
109 AsciiSPrint (PrintBuffer
, 256, "Fail to get decompress information for BFV!\n");
110 PrintString (PrintBuffer
);
114 Status
= TianoDecompress (
115 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
117 (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
,
119 (VOID
*)(UINTN
)((EFI_DECOMPRESSED_BUFFER_ADDRESS
+ DestinationSize
+ 0x1000) & 0xfffff000),
123 if (EFI_ERROR (Status
)) {
124 AsciiSPrint (PrintBuffer
, 256, "Fail to decompress BFV!\n");
125 PrintString (PrintBuffer
);
129 BfvPageNumber
= EFI_SIZE_TO_PAGES (DestinationSize
);
130 BfvBase
= (UINTN
) FindSpace (BfvPageNumber
, &NumberOfMemoryMapEntries
, EfiMemoryDescriptor
, EfiRuntimeServicesData
, EFI_MEMORY_WB
);
134 ZeroMem ((VOID
*)(UINTN
)BfvBase
, BfvPageNumber
* EFI_PAGE_SIZE
);
135 CopyMem ((VOID
*)(UINTN
)BfvBase
, (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
, DestinationSize
);
140 // Point to the 2nd image (DxeIpl)
146 // Decompress the image
148 AsciiSPrint (PrintBuffer
, 256, "Decompress DxeIpl image, Image Address=0x%x! Offset=0x%x\n",
149 (UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
150 EFILDRImage
->Offset
);
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
);
196 // PrintString("Image.NoPages = ");
197 // PrintValue(Image.NoPages);
198 // PrintString("\n");
203 // Point to the 3rd image (DxeMain)
209 // Decompress the image
212 Status
= TianoGetInfo (
213 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
218 if (EFI_ERROR (Status
)) {
222 Status
= TianoDecompress (
223 (VOID
*)(UINTN
)(EFILDR_HEADER_ADDRESS
+ EFILDRImage
->Offset
),
225 (VOID
*)(UINTN
)EFI_DECOMPRESSED_BUFFER_ADDRESS
,
227 (VOID
*)(UINTN
)((EFI_DECOMPRESSED_BUFFER_ADDRESS
+ DestinationSize
+ 0x1000) & 0xfffff000),
230 if (EFI_ERROR (Status
)) {
235 // Load and relocate the EFI PE/COFF Firmware Image
237 Status
= EfiLdrPeCoffLoadPeImage (
238 (VOID
*)(UINTN
)(EFI_DECOMPRESSED_BUFFER_ADDRESS
),
240 &NumberOfMemoryMapEntries
,
243 if (EFI_ERROR (Status
)) {
250 // Display the table of memory descriptors.
253 // PrintString("\nEFI Memory Descriptors\n");
257 for (Index = 0; Index < NumberOfMemoryMapEntries; Index++) {
258 PrintString("Type = ");
259 PrintValue(EfiMemoryDescriptor[Index].Type);
260 PrintString(" Start = ");
261 PrintValue((UINT32)(EfiMemoryDescriptor[Index].PhysicalStart));
262 PrintString(" NumberOfPages = ");
263 PrintValue((UINT32)(EfiMemoryDescriptor[Index].NumberOfPages));
270 // Jump to EFI Firmware
273 if (DxeIplImage
.EntryPoint
!= NULL
) {
275 Handoff
.MemDescCount
= NumberOfMemoryMapEntries
;
276 Handoff
.MemDesc
= EfiMemoryDescriptor
;
277 Handoff
.BfvBase
= (VOID
*)(UINTN
)BfvBase
;
278 Handoff
.BfvSize
= BfvPageNumber
* EFI_PAGE_SIZE
;
279 Handoff
.DxeIplImageBase
= (VOID
*)(UINTN
)DxeIplImage
.ImageBasePage
;
280 Handoff
.DxeIplImageSize
= DxeIplImage
.NoPages
* EFI_PAGE_SIZE
;
281 Handoff
.DxeCoreImageBase
= (VOID
*)(UINTN
)DxeCoreImage
.ImageBasePage
;
282 Handoff
.DxeCoreImageSize
= DxeCoreImage
.NoPages
* EFI_PAGE_SIZE
;
283 Handoff
.DxeCoreEntryPoint
= (VOID
*)(UINTN
)DxeCoreImage
.EntryPoint
;
285 EfiMainEntrypoint
= (EFI_MAIN_ENTRYPOINT
)(UINTN
)DxeIplImage
.EntryPoint
;
286 EfiMainEntrypoint (&Handoff
);
292 // There was a problem loading the image, so HALT the system.