1fde2f61 |
1 | /** @file\r |
2 | C Entry point for the SEC. First C code after the reset vector.\r |
3 | \r |
4 | Copyright (c) 2008-2009, Apple Inc. All rights reserved.\r |
5 | \r |
6 | All rights reserved. This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r |
8 | which accompanies this distribution. The full text of the license may be found at\r |
9 | http://opensource.org/licenses/bsd-license.php\r |
10 | \r |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
13 | \r |
14 | **/\r |
15 | \r |
16 | #include <PiPei.h>\r |
17 | \r |
18 | #include <Library/DebugLib.h>\r |
19 | #include <Library/PrePiLib.h>\r |
20 | #include <Library/PcdLib.h>\r |
21 | #include <Library/IoLib.h>\r |
22 | #include <Library/ArmLib.h>\r |
23 | #include <Library/PeCoffGetEntryPointLib.h>\r |
24 | \r |
25 | #include <Ppi/GuidedSectionExtraction.h>\r |
26 | #include <Guid/LzmaDecompress.h>\r |
27 | \r |
28 | #include "LzmaDecompress.h"\r |
29 | \r |
30 | VOID\r |
31 | EFIAPI \r |
32 | _ModuleEntryPoint(\r |
33 | VOID\r |
34 | );\r |
35 | \r |
36 | CHAR8 *\r |
37 | DeCygwinPathIfNeeded (\r |
38 | IN CHAR8 *Name\r |
39 | );\r |
40 | \r |
41 | RETURN_STATUS\r |
42 | EFIAPI\r |
43 | SerialPortInitialize (\r |
44 | VOID\r |
45 | );\r |
46 | \r |
47 | \r |
48 | VOID\r |
49 | UartInit (\r |
50 | VOID\r |
51 | )\r |
52 | {\r |
53 | // SEC phase needs to run library constructors by hand.\r |
54 | // This assumes we are linked agains the SerialLib\r |
55 | // In non SEC modules the init call is in autogenerated code.\r |
56 | SerialPortInitialize ();\r |
57 | }\r |
58 | \r |
59 | VOID\r |
60 | InitCache (\r |
61 | IN UINT32 MemoryBase,\r |
62 | IN UINT32 MemoryLength\r |
63 | );\r |
64 | \r |
65 | EFI_STATUS\r |
66 | EFIAPI\r |
67 | ExtractGuidedSectionLibConstructor (\r |
68 | VOID\r |
69 | );\r |
70 | \r |
71 | EFI_STATUS\r |
72 | EFIAPI\r |
73 | LzmaDecompressLibConstructor (\r |
74 | VOID\r |
75 | );\r |
76 | \r |
77 | /**\r |
78 | If the build is done on cygwin the paths are cygpaths. \r |
79 | /cygdrive/c/tmp.txt vs c:\tmp.txt so we need to convert\r |
80 | them to work with RVD commands\r |
81 | \r |
82 | This is just code to help print out RVD symbol load command.\r |
83 | If you build with cygwin paths aren't compatible with RVD.\r |
84 | \r |
85 | @param Name Path to convert if needed\r |
86 | \r |
87 | **/\r |
88 | CHAR8 *\r |
89 | SecDeCygwinPathIfNeeded (\r |
90 | IN CHAR8 *Name\r |
91 | )\r |
92 | {\r |
93 | CHAR8 *Ptr;\r |
94 | UINTN Index;\r |
95 | UINTN Len;\r |
96 | \r |
97 | Ptr = AsciiStrStr (Name, "/cygdrive/");\r |
98 | if (Ptr == NULL) {\r |
99 | return Name;\r |
100 | }\r |
101 | \r |
102 | Len = AsciiStrLen (Ptr);\r |
103 | \r |
104 | // convert "/cygdrive" to spaces\r |
105 | for (Index = 0; Index < 9; Index++) {\r |
106 | Ptr[Index] = ' ';\r |
107 | }\r |
108 | \r |
109 | // convert /c to c:\r |
110 | Ptr[9] = Ptr[10];\r |
111 | Ptr[10] = ':';\r |
112 | \r |
113 | // switch path seperators\r |
114 | for (Index = 11; Index < Len; Index++) {\r |
115 | if (Ptr[Index] == '/') {\r |
116 | Ptr[Index] = '\\' ;\r |
117 | }\r |
118 | }\r |
119 | \r |
120 | return Name;\r |
121 | }\r |
122 | \r |
123 | \r |
124 | VOID\r |
125 | CEntryPoint (\r |
126 | IN VOID *MemoryBase,\r |
127 | IN UINTN MemorySize,\r |
128 | IN VOID *StackBase,\r |
129 | IN UINTN StackSize\r |
130 | )\r |
131 | {\r |
132 | VOID *HobBase;\r |
133 | \r |
134 | // Build a basic HOB list\r |
135 | HobBase = (VOID *)(UINTN)(FixedPcdGet32(PcdEmbeddedFdBaseAddress) + FixedPcdGet32(PcdEmbeddedFdSize));\r |
136 | CreateHobList (MemoryBase, MemorySize, HobBase, StackBase);\r |
137 | \r |
138 | \r |
139 | // Enable program flow prediction, if supported.\r |
140 | ArmEnableBranchPrediction ();\r |
141 | \r |
142 | // Initialize CPU cache\r |
143 | InitCache ((UINT32)MemoryBase, (UINT32)MemorySize);\r |
144 | \r |
145 | // Add memory allocation hob for relocated FD\r |
146 | BuildMemoryAllocationHob (FixedPcdGet32(PcdEmbeddedFdBaseAddress), FixedPcdGet32(PcdEmbeddedFdSize), EfiBootServicesData);\r |
147 | \r |
148 | // Add the FVs to the hob list\r |
149 | BuildFvHob (PcdGet32(PcdFlashFvMainBase), PcdGet32(PcdFlashFvMainSize));\r |
150 | \r |
151 | // Start talking\r |
152 | UartInit ();\r |
153 | DEBUG ((EFI_D_ERROR, "UART Enabled\n"));\r |
154 | \r |
155 | DEBUG_CODE_BEGIN ();\r |
156 | //\r |
157 | // On a debug build print out information about the SEC. This is really info about\r |
158 | // the PE/COFF file we are currently running from. Useful for loading symbols in a\r |
159 | // debugger. Remember our image is really part of the FV.\r |
160 | //\r |
161 | RETURN_STATUS Status;\r |
162 | EFI_PEI_FV_HANDLE VolumeHandle;\r |
163 | EFI_PEI_FILE_HANDLE FileHandle;\r |
164 | VOID *PeCoffImage;\r |
165 | UINT32 Offset;\r |
166 | CHAR8 *FilePath;\r |
167 | FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_SECURITY_CORE, &VolumeHandle, &FileHandle);\r |
168 | Status = FfsFindSectionData (EFI_SECTION_TE, FileHandle, &PeCoffImage);\r |
169 | if (EFI_ERROR (Status)) {\r |
170 | // Usually is a TE (PI striped down PE/COFF), but could be a full PE/COFF\r |
171 | Status = FfsFindSectionData (EFI_SECTION_PE32, FileHandle, &PeCoffImage);\r |
172 | }\r |
173 | if (!EFI_ERROR (Status)) {\r |
174 | Offset = PeCoffGetSizeOfHeaders (PeCoffImage);\r |
175 | FilePath = PeCoffLoaderGetPdbPointer (PeCoffImage);\r |
176 | if (FilePath != NULL) {\r |
177 | \r |
178 | // \r |
179 | // In general you should never have to use #ifdef __CC_ARM in the code. It\r |
180 | // is hidden in the away in the MdePkg. But here we would like to print differnt things\r |
181 | // for different toolchains. \r |
182 | //\r |
183 | #ifdef __CC_ARM\r |
184 | // Print out the command for the RVD debugger to load symbols for this image\r |
185 | DEBUG ((EFI_D_ERROR, "load /a /ni /np %a &0x%08x\n", SecDeCygwinPathIfNeeded (FilePath), (CHAR8 *)PeCoffImage + Offset));\r |
186 | #elif __GNUC__\r |
187 | // This may not work correctly if you generate PE/COFF directlyas then the Offset would not be required\r |
188 | DEBUG ((EFI_D_ERROR, "add-symbol-file %a 0x%08x\n", FilePath, PeCoffImage + Offset));\r |
189 | #else\r |
190 | DEBUG ((EFI_D_ERROR, "SEC starts at 0x%08x with an entry point at 0x%08x %a\n", PeCoffImage, _ModuleEntryPoint, FilePath));\r |
191 | #endif\r |
192 | }\r |
193 | }\r |
194 | \r |
195 | \r |
196 | DEBUG_CODE_END ();\r |
197 | \r |
198 | \r |
199 | // SEC phase needs to run library constructors by hand.\r |
200 | ExtractGuidedSectionLibConstructor ();\r |
201 | LzmaDecompressLibConstructor ();\r |
202 | \r |
203 | // Build HOBs to pass up our version of stuff the DXE Core needs to save space\r |
204 | BuildPeCoffLoaderHob ();\r |
205 | BuildExtractSectionHob (\r |
206 | &gLzmaCustomDecompressGuid,\r |
207 | LzmaGuidedSectionGetInfo,\r |
208 | LzmaGuidedSectionExtraction\r |
209 | );\r |
210 | \r |
211 | // Assume the FV that contains the SEC (our code) also contains a compressed FV.\r |
212 | DecompressFirstFv ();\r |
213 | \r |
214 | // Load the DXE Core and transfer control to it\r |
215 | LoadDxeCoreFromFv (NULL, 0);\r |
216 | \r |
217 | // DXE Core should always load and never return\r |
218 | ASSERT (FALSE);\r |
219 | }\r |
220 | \r |