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