]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/PrePi/PrePi.c
c4060d9b6369543482edd7c7f54cd94f38aebb66
[mirror_edk2.git] / ArmPlatformPkg / PrePi / PrePi.c
1 /** @file
2 *
3 * Copyright (c) 2011, 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/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>
26
27 #include <Ppi/GuidedSectionExtraction.h>
28 #include <Guid/LzmaDecompress.h>
29
30 #include "PrePi.h"
31 #include "LzmaDecompress.h"
32
33 VOID
34 PrePiCommonExceptionEntry (
35 IN UINT32 Entry,
36 IN UINT32 LR
37 );
38
39 EFI_STATUS
40 EFIAPI
41 ExtractGuidedSectionLibConstructor (
42 VOID
43 );
44
45 EFI_STATUS
46 EFIAPI
47 LzmaDecompressLibConstructor (
48 VOID
49 );
50
51 VOID
52 PrePiMain (
53 IN UINTN UefiMemoryBase,
54 IN UINT64 StartTimeStamp
55 )
56 {
57 EFI_HOB_HANDOFF_INFO_TABLE* HobList;
58 EFI_STATUS Status;
59 CHAR8 Buffer[100];
60 UINTN CharCount;
61 UINTN UefiMemoryTop;
62 UINTN StacksSize;
63 UINTN StacksBase;
64
65 // Enable program flow prediction, if supported.
66 ArmEnableBranchPrediction ();
67
68 if (FixedPcdGet32(PcdVFPEnabled)) {
69 ArmEnableVFP();
70 }
71
72 // Initialize the Serial Port
73 SerialPortInitialize ();
74 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"UEFI firmware built at %a on %a\n\r",__TIME__, __DATE__);
75 SerialPortWrite ((UINT8 *) Buffer, CharCount);
76
77 // Initialize the Debug Agent for Source Level Debugging
78 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, NULL, NULL);
79 SaveAndSetDebugTimerInterrupt (TRUE);
80
81 UefiMemoryTop = UefiMemoryBase + FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);
82 StacksSize = PcdGet32 (PcdCPUCoresNonSecStackSize) * PcdGet32 (PcdMPCoreMaxCores);
83 StacksBase = UefiMemoryTop - StacksSize;
84
85 // Check the PcdCPUCoresNonSecStackBase match with the calculated StackBase
86 ASSERT (StacksBase == PcdGet32 (PcdCPUCoresNonSecStackBase));
87
88 // Declare the PI/UEFI memory region
89 HobList = HobConstructor (
90 (VOID*)UefiMemoryBase,
91 FixedPcdGet32 (PcdSystemMemoryUefiRegionSize),
92 (VOID*)UefiMemoryBase,
93 (VOID*)StacksBase // The top of the UEFI Memory is reserved for the stacks
94 );
95 PrePeiSetHobList (HobList);
96
97 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
98 Status = MemoryPeim (UefiMemoryBase, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize));
99 ASSERT_EFI_ERROR (Status);
100
101 // Create the Stacks HOB (reserve the memory for all stacks)
102 BuildStackHob (StacksBase, StacksSize);
103
104 // Set the Boot Mode
105 SetBootMode (ArmPlatformGetBootMode ());
106
107 // Initialize Platform HOBs (CpuHob and FvHob)
108 Status = PlatformPeim ();
109 ASSERT_EFI_ERROR (Status);
110
111 BuildMemoryTypeInformationHob ();
112
113 // Now, the HOB List has been initialized, we can register performance information
114 PERF_START (NULL, "PEI", NULL, StartTimeStamp);
115
116 // SEC phase needs to run library constructors by hand.
117 ExtractGuidedSectionLibConstructor ();
118 LzmaDecompressLibConstructor ();
119
120 // Build HOBs to pass up our version of stuff the DXE Core needs to save space
121 BuildPeCoffLoaderHob ();
122 BuildExtractSectionHob (
123 &gLzmaCustomDecompressGuid,
124 LzmaGuidedSectionGetInfo,
125 LzmaGuidedSectionExtraction
126 );
127
128 // Assume the FV that contains the SEC (our code) also contains a compressed FV.
129 Status = DecompressFirstFv ();
130 ASSERT_EFI_ERROR (Status);
131
132 // Load the DXE Core and transfer control to it
133 Status = LoadDxeCoreFromFv (NULL, 0);
134 ASSERT_EFI_ERROR (Status);
135 }
136
137 VOID
138 CEntryPoint (
139 IN UINTN CoreId,
140 IN UINTN UefiMemoryBase
141 )
142 {
143 UINT64 StartTimeStamp;
144
145 if ((CoreId == ARM_PRIMARY_CORE) && PerformanceMeasurementEnabled ()) {
146 // Initialize the Timer Library to setup the Timer HW controller
147 TimerConstructor ();
148 // We cannot call yet the PerformanceLib because the HOB List has not been initialized
149 StartTimeStamp = GetPerformanceCounter ();
150 } else {
151 StartTimeStamp = 0;
152 }
153
154 // Clean Data cache
155 ArmCleanInvalidateDataCache ();
156
157 // Invalidate instruction cache
158 ArmInvalidateInstructionCache ();
159
160 //TODO:Drain Write Buffer
161
162 // Enable Instruction & Data caches
163 ArmEnableDataCache ();
164 ArmEnableInstructionCache ();
165
166 // Write VBAR - The Vector table must be 32-byte aligned
167 ASSERT (((UINT32)PrePiVectorTable & ((1 << 5)-1)) == 0);
168 ArmWriteVBar ((UINT32)PrePiVectorTable);
169
170 // If not primary Jump to Secondary Main
171 if (CoreId == ARM_PRIMARY_CORE) {
172 // Goto primary Main.
173 PrimaryMain (UefiMemoryBase, StartTimeStamp);
174 } else {
175 SecondaryMain (CoreId);
176 }
177
178 // DXE Core should always load and never return
179 ASSERT (FALSE);
180 }
181
182 VOID
183 PrePiCommonExceptionEntry (
184 IN UINT32 Entry,
185 IN UINT32 LR
186 )
187 {
188 CHAR8 Buffer[100];
189 UINTN CharCount;
190
191 switch (Entry) {
192 case 0:
193 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"Reset Exception at 0x%X\n\r",LR);
194 break;
195 case 1:
196 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"Undefined Exception at 0x%X\n\r",LR);
197 break;
198 case 2:
199 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"SWI Exception at 0x%X\n\r",LR);
200 break;
201 case 3:
202 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"PrefetchAbort Exception at 0x%X\n\r",LR);
203 break;
204 case 4:
205 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"DataAbort Exception at 0x%X\n\r",LR);
206 break;
207 case 5:
208 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"Reserved Exception at 0x%X\n\r",LR);
209 break;
210 case 6:
211 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"IRQ Exception at 0x%X\n\r",LR);
212 break;
213 case 7:
214 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"FIQ Exception at 0x%X\n\r",LR);
215 break;
216 default:
217 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"Unknown Exception at 0x%X\n\r",LR);
218 break;
219 }
220 SerialPortWrite ((UINT8 *) Buffer, CharCount);
221 while(1);
222 }