blob: f9ad37427217830140f88950f20f9b92d99ce75b [file] [log] [blame]
Ard Biesheuvelf9a9d2d2015-02-28 20:26:20 +00001/** @file
2*
3* Copyright (c) 2011-2014, ARM Limited. All rights reserved.
4*
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
9*
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.
12*
13**/
14
15#include <PiPei.h>
16
17#include <Library/PrePiLib.h>
18#include <Library/PrintLib.h>
19#include <Library/PeCoffGetEntryPointLib.h>
20#include <Library/PrePiHobListPointerLib.h>
21#include <Library/TimerLib.h>
22#include <Library/PerformanceLib.h>
Ard Biesheuvelc9e56182015-04-14 11:56:56 +000023#include <Library/CacheMaintenanceLib.h>
Ard Biesheuvelf9a9d2d2015-02-28 20:26:20 +000024
25#include <Ppi/GuidedSectionExtraction.h>
26#include <Ppi/ArmMpCoreInfo.h>
27#include <Guid/LzmaDecompress.h>
28#include <Guid/ArmGlobalVariableHob.h>
29
30#include "PrePi.h"
31#include "LzmaDecompress.h"
32
33// Not used when PrePi in run in XIP mode
34UINTN mGlobalVariableBase = 0;
35
36EFI_STATUS
37EFIAPI
38ExtractGuidedSectionLibConstructor (
39 VOID
40 );
41
42EFI_STATUS
43EFIAPI
44LzmaDecompressLibConstructor (
45 VOID
46 );
47
48VOID
49EFIAPI
50BuildGlobalVariableHob (
51 IN EFI_PHYSICAL_ADDRESS GlobalVariableBase,
52 IN UINT32 GlobalVariableSize
53 )
54{
55 ARM_HOB_GLOBAL_VARIABLE *Hob;
56
57 Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, sizeof (ARM_HOB_GLOBAL_VARIABLE));
58 ASSERT(Hob != NULL);
59
60 CopyGuid (&(Hob->Header.Name), &gArmGlobalVariableGuid);
61 Hob->GlobalVariableBase = GlobalVariableBase;
62 Hob->GlobalVariableSize = GlobalVariableSize;
63}
64
65EFI_STATUS
66GetPlatformPpi (
67 IN EFI_GUID *PpiGuid,
68 OUT VOID **Ppi
69 )
70{
71 UINTN PpiListSize;
72 UINTN PpiListCount;
73 EFI_PEI_PPI_DESCRIPTOR *PpiList;
74 UINTN Index;
75
76 PpiListSize = 0;
77 ArmPlatformGetPlatformPpiList (&PpiListSize, &PpiList);
78 PpiListCount = PpiListSize / sizeof(EFI_PEI_PPI_DESCRIPTOR);
79 for (Index = 0; Index < PpiListCount; Index++, PpiList++) {
80 if (CompareGuid (PpiList->Guid, PpiGuid) == TRUE) {
81 *Ppi = PpiList->Ppi;
82 return EFI_SUCCESS;
83 }
84 }
85
86 return EFI_NOT_FOUND;
87}
88
89VOID
90PrePiMain (
91 IN UINTN UefiMemoryBase,
92 IN UINTN StacksBase,
93 IN UINTN GlobalVariableBase,
94 IN UINT64 StartTimeStamp
95 )
96{
97 EFI_HOB_HANDOFF_INFO_TABLE* HobList;
98 EFI_STATUS Status;
99 CHAR8 Buffer[100];
100 UINTN CharCount;
101 UINTN StacksSize;
102
103 // Initialize the architecture specific bits
104 ArchInitialize ();
105
Ard Biesheuvelf9a9d2d2015-02-28 20:26:20 +0000106 // Declare the PI/UEFI memory region
107 HobList = HobConstructor (
108 (VOID*)UefiMemoryBase,
109 FixedPcdGet32 (PcdSystemMemoryUefiRegionSize),
110 (VOID*)UefiMemoryBase,
111 (VOID*)StacksBase // The top of the UEFI Memory is reserved for the stacks
112 );
113 PrePeiSetHobList (HobList);
114
Ard Biesheuvelc9e56182015-04-14 11:56:56 +0000115 //
116 // Ensure that the loaded image is invalidated in the caches, so that any
117 // modifications we made with the caches and MMU off (such as the applied
118 // relocations) don't become invisible once we turn them on.
119 //
120 InvalidateDataCacheRange((VOID *)(UINTN)PcdGet64 (PcdFdBaseAddress), PcdGet32 (PcdFdSize));
121
Ard Biesheuvelf9a9d2d2015-02-28 20:26:20 +0000122 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
123 Status = MemoryPeim (UefiMemoryBase, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize));
124 ASSERT_EFI_ERROR (Status);
125
Ard Biesheuvelc9e56182015-04-14 11:56:56 +0000126 // Initialize the Serial Port
127 SerialPortInitialize ();
128 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"UEFI firmware (version %s built at %a on %a)\n\r",
129 (CHAR16*)PcdGetPtr(PcdFirmwareVersionString), __TIME__, __DATE__);
130 SerialPortWrite ((UINT8 *) Buffer, CharCount);
131
Ard Biesheuvelf9a9d2d2015-02-28 20:26:20 +0000132 // Create the Stacks HOB (reserve the memory for all stacks)
133 StacksSize = PcdGet32 (PcdCPUCorePrimaryStackSize);
134 BuildStackHob (StacksBase, StacksSize);
135
136 // Declare the Global Variable HOB
137 BuildGlobalVariableHob (GlobalVariableBase, FixedPcdGet32 (PcdPeiGlobalVariableSize));
138
139 //TODO: Call CpuPei as a library
140 BuildCpuHob (PcdGet8 (PcdPrePiCpuMemorySize), PcdGet8 (PcdPrePiCpuIoSize));
141
142 // Set the Boot Mode
143 SetBootMode (ArmPlatformGetBootMode ());
144
145 // Initialize Platform HOBs (CpuHob and FvHob)
146 Status = PlatformPeim ();
147 ASSERT_EFI_ERROR (Status);
148
149 // Now, the HOB List has been initialized, we can register performance information
150 PERF_START (NULL, "PEI", NULL, StartTimeStamp);
151
152 // SEC phase needs to run library constructors by hand.
153 ExtractGuidedSectionLibConstructor ();
154 LzmaDecompressLibConstructor ();
155
156 // Build HOBs to pass up our version of stuff the DXE Core needs to save space
157 BuildPeCoffLoaderHob ();
158 BuildExtractSectionHob (
159 &gLzmaCustomDecompressGuid,
160 LzmaGuidedSectionGetInfo,
161 LzmaGuidedSectionExtraction
162 );
163
164 // Assume the FV that contains the SEC (our code) also contains a compressed FV.
165 Status = DecompressFirstFv ();
166 ASSERT_EFI_ERROR (Status);
167
168 // Load the DXE Core and transfer control to it
169 Status = LoadDxeCoreFromFv (NULL, 0);
170 ASSERT_EFI_ERROR (Status);
171}
172
173VOID
174CEntryPoint (
175 IN UINTN MpId,
176 IN UINTN UefiMemoryBase,
177 IN UINTN StacksBase,
178 IN UINTN GlobalVariableBase
179 )
180{
181 UINT64 StartTimeStamp;
182
183 // Initialize the platform specific controllers
184 ArmPlatformInitialize (MpId);
185
186 if (PerformanceMeasurementEnabled ()) {
187 // Initialize the Timer Library to setup the Timer HW controller
188 TimerConstructor ();
189 // We cannot call yet the PerformanceLib because the HOB List has not been initialized
190 StartTimeStamp = GetPerformanceCounter ();
191 } else {
192 StartTimeStamp = 0;
193 }
194
195 // Data Cache enabled on Primary core when MMU is enabled.
196 ArmDisableDataCache ();
197 // Invalidate Data cache
198 ArmInvalidateDataCache ();
199 // Invalidate instruction cache
200 ArmInvalidateInstructionCache ();
201 // Enable Instruction Caches on all cores.
202 ArmEnableInstructionCache ();
203
204 // Define the Global Variable region
205 mGlobalVariableBase = GlobalVariableBase;
206
207 PrePiMain (UefiMemoryBase, StacksBase, GlobalVariableBase, StartTimeStamp);
208
209 // DXE Core should always load and never return
210 ASSERT (FALSE);
211}