3 * Copyright (c) 2011, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/DebugAgentLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/PrePiLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/PrintLib.h>
22 #include <Library/PeCoffGetEntryPointLib.h>
23 #include <Library/PrePiHobListPointerLib.h>
24 #include <Library/TimerLib.h>
25 #include <Library/PerformanceLib.h>
27 #include <Ppi/GuidedSectionExtraction.h>
28 #include <Guid/LzmaDecompress.h>
29 #include <Guid/ArmGlobalVariableHob.h>
32 #include "LzmaDecompress.h"
34 #define IS_XIP() ((FixedPcdGet32 (PcdFdBaseAddress) > (FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemorySize))) || \
35 ((FixedPcdGet32 (PcdFdBaseAddress) + FixedPcdGet32 (PcdFdSize)) < FixedPcdGet32 (PcdSystemMemoryBase)))
37 // Not used when PrePi in run in XIP mode
38 UINTN mGlobalVariableBase
= 0;
41 PrePiCommonExceptionEntry (
48 ExtractGuidedSectionLibConstructor (
54 LzmaDecompressLibConstructor (
60 BuildGlobalVariableHob (
61 IN EFI_PHYSICAL_ADDRESS GlobalVariableBase
,
62 IN UINT32 GlobalVariableSize
65 ARM_HOB_GLOBAL_VARIABLE
*Hob
;
67 Hob
= CreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, sizeof (ARM_HOB_GLOBAL_VARIABLE
));
70 CopyGuid (&(Hob
->Header
.Name
), &gArmGlobalVariableGuid
);
71 Hob
->GlobalVariableBase
= GlobalVariableBase
;
72 Hob
->GlobalVariableSize
= GlobalVariableSize
;
77 IN UINTN UefiMemoryBase
,
79 IN UINTN GlobalVariableBase
,
80 IN UINT64 StartTimeStamp
83 EFI_HOB_HANDOFF_INFO_TABLE
* HobList
;
89 // If ensure the FD is either part of the System Memory or totally outside of the System Memory (XIP)
91 ((FixedPcdGet32 (PcdFdBaseAddress
) >= FixedPcdGet32 (PcdSystemMemoryBase
)) &&
92 ((FixedPcdGet32 (PcdFdBaseAddress
) + FixedPcdGet32 (PcdFdSize
)) <= (FixedPcdGet32 (PcdSystemMemoryBase
) + FixedPcdGet32 (PcdSystemMemorySize
)))));
94 // Enable program flow prediction, if supported.
95 ArmEnableBranchPrediction ();
97 if (FixedPcdGet32(PcdVFPEnabled
)) {
101 // Initialize the Serial Port
102 SerialPortInitialize ();
103 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"UEFI firmware built at %a on %a\n\r",__TIME__
, __DATE__
);
104 SerialPortWrite ((UINT8
*) Buffer
, CharCount
);
106 // Initialize the Debug Agent for Source Level Debugging
107 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC
, NULL
, NULL
);
108 SaveAndSetDebugTimerInterrupt (TRUE
);
111 mGlobalVariableBase
= GlobalVariableBase
;
114 // Declare the PI/UEFI memory region
115 HobList
= HobConstructor (
116 (VOID
*)UefiMemoryBase
,
117 FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
),
118 (VOID
*)UefiMemoryBase
,
119 (VOID
*)StacksBase
// The top of the UEFI Memory is reserved for the stacks
121 PrePeiSetHobList (HobList
);
123 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
124 Status
= MemoryPeim (UefiMemoryBase
, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
));
125 ASSERT_EFI_ERROR (Status
);
127 // Create the Stacks HOB (reserve the memory for all stacks)
128 StacksSize
= PcdGet32 (PcdCPUCorePrimaryStackSize
) + (FixedPcdGet32(PcdClusterCount
) * 4 * FixedPcdGet32(PcdCPUCoreSecondaryStackSize
));
129 BuildStackHob (StacksBase
, StacksSize
);
131 // Declare the Global Variable HOB
132 BuildGlobalVariableHob (GlobalVariableBase
, FixedPcdGet32 (PcdPeiGlobalVariableSize
));
134 //TODO: Call CpuPei as a library
135 BuildCpuHob (PcdGet8 (PcdPrePiCpuMemorySize
), PcdGet8 (PcdPrePiCpuIoSize
));
138 SetBootMode (ArmPlatformGetBootMode ());
140 // Initialize Platform HOBs (CpuHob and FvHob)
141 Status
= PlatformPeim ();
142 ASSERT_EFI_ERROR (Status
);
144 BuildMemoryTypeInformationHob ();
146 // Now, the HOB List has been initialized, we can register performance information
147 PERF_START (NULL
, "PEI", NULL
, StartTimeStamp
);
149 // SEC phase needs to run library constructors by hand.
150 ExtractGuidedSectionLibConstructor ();
151 LzmaDecompressLibConstructor ();
153 // Build HOBs to pass up our version of stuff the DXE Core needs to save space
154 BuildPeCoffLoaderHob ();
155 BuildExtractSectionHob (
156 &gLzmaCustomDecompressGuid
,
157 LzmaGuidedSectionGetInfo
,
158 LzmaGuidedSectionExtraction
161 // Assume the FV that contains the SEC (our code) also contains a compressed FV.
162 Status
= DecompressFirstFv ();
163 ASSERT_EFI_ERROR (Status
);
165 // Load the DXE Core and transfer control to it
166 Status
= LoadDxeCoreFromFv (NULL
, 0);
167 ASSERT_EFI_ERROR (Status
);
173 IN UINTN UefiMemoryBase
,
175 IN UINTN GlobalVariableBase
178 UINT64 StartTimeStamp
;
180 if (IS_PRIMARY_CORE(MpId
) && PerformanceMeasurementEnabled ()) {
181 // Initialize the Timer Library to setup the Timer HW controller
183 // We cannot call yet the PerformanceLib because the HOB List has not been initialized
184 StartTimeStamp
= GetPerformanceCounter ();
190 ArmCleanInvalidateDataCache ();
192 // Invalidate instruction cache
193 ArmInvalidateInstructionCache ();
195 //TODO:Drain Write Buffer
197 // Enable Instruction & Data caches
198 ArmEnableDataCache ();
199 ArmEnableInstructionCache ();
201 // Write VBAR - The Vector table must be 32-byte aligned
202 ASSERT (((UINT32
)PrePiVectorTable
& ((1 << 5)-1)) == 0);
203 ArmWriteVBar ((UINT32
)PrePiVectorTable
);
205 // If not primary Jump to Secondary Main
206 if (IS_PRIMARY_CORE(MpId
)) {
207 // Goto primary Main.
208 PrimaryMain (UefiMemoryBase
, StacksBase
, GlobalVariableBase
, StartTimeStamp
);
210 SecondaryMain (MpId
);
213 // DXE Core should always load and never return
218 PrePiCommonExceptionEntry (
228 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"Reset Exception at 0x%X\n\r",LR
);
231 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"Undefined Exception at 0x%X\n\r",LR
);
234 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"SWI Exception at 0x%X\n\r",LR
);
237 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"PrefetchAbort Exception at 0x%X\n\r",LR
);
240 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"DataAbort Exception at 0x%X\n\r",LR
);
243 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"Reserved Exception at 0x%X\n\r",LR
);
246 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"IRQ Exception at 0x%X\n\r",LR
);
249 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"FIQ Exception at 0x%X\n\r",LR
);
252 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"Unknown Exception at 0x%X\n\r",LR
);
255 SerialPortWrite ((UINT8
*) Buffer
, CharCount
);