Unix version of EFI emulator
[mirror_edk2.git] / EdkUnixPkg / Pei / FlashMap / FlashMap.c
CommitLineData
c9093a06 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 FlashMap.c\r
15 \r
16Abstract:\r
17\r
18 PEIM to build GUIDed HOBs for platform specific flash map\r
19\r
20--*/\r
21\r
22\r
23#include <FlashLayout.h>\r
24\r
25EFI_STATUS\r
26EFIAPI\r
27GetAreaInfo (\r
28 IN EFI_PEI_SERVICES **PeiServices,\r
29 IN PEI_FLASH_MAP_PPI *This,\r
30 IN EFI_FLASH_AREA_TYPE AreaType,\r
31 IN EFI_GUID *AreaTypeGuid,\r
32 OUT UINT32 *NumEntries,\r
33 OUT EFI_FLASH_SUBAREA_ENTRY **Entries\r
34 );\r
35\r
36EFI_STATUS\r
37EFIAPI\r
38MemoryDiscoveredPpiNotifyCallback (\r
39 IN EFI_PEI_SERVICES **PeiServices,\r
40 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
41 IN VOID *Ppi\r
42 );\r
43\r
44//\r
45// Module globals\r
46//\r
47static PEI_FLASH_MAP_PPI mFlashMapPpi = { GetAreaInfo };\r
48\r
49static EFI_PEI_PPI_DESCRIPTOR mPpiListFlashMap = {\r
50 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
51 &gPeiFlashMapPpiGuid,\r
52 &mFlashMapPpi\r
53};\r
54\r
55static EFI_FLASH_AREA_DATA mFlashAreaData[] = {\r
56 //\r
57 // Variable area\r
58 //\r
59 {\r
60 EFI_VARIABLE_STORE_OFFSET,\r
61 EFI_VARIABLE_STORE_LENGTH,\r
62 EFI_FLASH_AREA_SUBFV | EFI_FLASH_AREA_MEMMAPPED_FV,\r
63 EFI_FLASH_AREA_EFI_VARIABLES,\r
64 0, 0, 0,\r
65 { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }\r
66 },\r
67 //\r
68 // FTW spare (backup) block\r
69 //\r
70 {\r
71 EFI_WINNT_FTW_SPARE_BLOCK_OFFSET,\r
72 EFI_WINNT_FTW_SPARE_BLOCK_LENGTH,\r
73 EFI_FLASH_AREA_SUBFV | EFI_FLASH_AREA_MEMMAPPED_FV,\r
74 EFI_FLASH_AREA_FTW_BACKUP,\r
75 0, 0, 0,\r
76 { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }\r
77 },\r
78 //\r
79 // FTW private working (state) area\r
80 //\r
81 {\r
82 EFI_FTW_WORKING_OFFSET,\r
83 EFI_FTW_WORKING_LENGTH,\r
84 EFI_FLASH_AREA_SUBFV | EFI_FLASH_AREA_MEMMAPPED_FV,\r
85 EFI_FLASH_AREA_FTW_STATE,\r
86 0, 0, 0,\r
87 { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }\r
88 },\r
89 //\r
90 // Recovery FV\r
91 //\r
92 {\r
93 EFI_WINNT_FIRMWARE_OFFSET,\r
94 EFI_WINNT_FIRMWARE_LENGTH,\r
95 EFI_FLASH_AREA_FV | EFI_FLASH_AREA_MEMMAPPED_FV,\r
96 EFI_FLASH_AREA_RECOVERY_BIOS,\r
97 0, 0, 0,\r
98 { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }\r
99 },\r
100 //\r
101 // System Non-Volatile Storage FV\r
102 //\r
103 {\r
104 EFI_WINNT_RUNTIME_UPDATABLE_OFFSET,\r
105 EFI_WINNT_RUNTIME_UPDATABLE_LENGTH + EFI_WINNT_FTW_SPARE_BLOCK_LENGTH,\r
106 EFI_FLASH_AREA_FV | EFI_FLASH_AREA_MEMMAPPED_FV,\r
107 EFI_FLASH_AREA_GUID_DEFINED,\r
108 0, 0, 0,\r
109 EFI_SYSTEM_NV_DATA_HOB_GUID\r
110 },\r
111};\r
112\r
113\r
114EFI_STATUS\r
115EFIAPI\r
116PeimInitializeFlashMap (\r
117 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
118 IN EFI_PEI_SERVICES **PeiServices\r
119 )\r
120/*++\r
121\r
122Routine Description:\r
123 Build GUIDed HOBs for platform specific flash map\r
124 \r
125Arguments:\r
126 FfsHeader - A pointer to the EFI_FFS_FILE_HEADER structure.\r
127 PeiServices - General purpose services available to every PEIM.\r
128 \r
129Returns:\r
130 EFI_STATUS\r
131\r
132--*/\r
133// TODO: EFI_SUCCESS - add return value to function comment\r
134{\r
135 EFI_STATUS Status;\r
136 UNIX_FWH_PPI *UnixFwhPpi;\r
137 EFI_PEI_PPI_DESCRIPTOR *PpiDescriptor;\r
138 EFI_PHYSICAL_ADDRESS FdBase;\r
139 UINT64 FdSize;\r
140 UINTN NumOfHobData;\r
141 UINTN Index;\r
142 EFI_FLASH_AREA_HOB_DATA FlashHobData;\r
143\r
144 DEBUG ((EFI_D_ERROR, "NT 32 Flash Map PEIM Loaded\n"));\r
145\r
146 //\r
147 // Install FlashMap PPI\r
148 //\r
149 Status = PeiServicesInstallPpi (&mPpiListFlashMap);\r
150 ASSERT_EFI_ERROR (Status);\r
151\r
152\r
153 //\r
154 // Get the Fwh Information PPI\r
155 //\r
156 Status = PeiServicesLocatePpi (\r
157 &gUnixFwhPpiGuid, // GUID\r
158 0, // INSTANCE\r
159 &PpiDescriptor, // EFI_PEI_PPI_DESCRIPTOR\r
160 (VOID **)&UnixFwhPpi // PPI\r
161 );\r
162 ASSERT_EFI_ERROR (Status);\r
163\r
164 //\r
165 // Assume that FD0 contains the Flash map.\r
166 //\r
167 Status = UnixFwhPpi->UnixFwh (0, &FdBase, &FdSize);\r
168 if (EFI_ERROR (Status)) {\r
169 return Status;\r
170 }\r
171\r
172 //\r
173 // Get number of types\r
174 //\r
175 NumOfHobData = sizeof (mFlashAreaData) / sizeof (EFI_FLASH_AREA_DATA);\r
176\r
177 //\r
178 // Build flash area entries as GUIDed HOBs.\r
179 //\r
180 for (Index = 0; Index < NumOfHobData; Index++) {\r
181 (*PeiServices)->SetMem (&FlashHobData, sizeof (EFI_FLASH_AREA_HOB_DATA), 0);\r
182\r
183 FlashHobData.AreaType = mFlashAreaData[Index].AreaType;\r
184 FlashHobData.NumberOfEntries = 1;\r
185 FlashHobData.SubAreaData.Attributes = mFlashAreaData[Index].Attributes;\r
186 FlashHobData.SubAreaData.Base = FdBase + (EFI_PHYSICAL_ADDRESS) (UINTN) mFlashAreaData[Index].Base;\r
187 FlashHobData.SubAreaData.Length = (EFI_PHYSICAL_ADDRESS) (UINTN) mFlashAreaData[Index].Length;\r
188\r
189 //\r
190 // We also update a PCD entry so that any driver that depend on\r
191 // PCD entry will get the information.\r
192 //\r
193 if (FlashHobData.AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {\r
194 PcdSet32 (PcdFlashNvStorageVariableBase, (UINT32) FlashHobData.SubAreaData.Base);\r
195 PcdSet32 (PcdFlashNvStorageVariableSize, (UINT32) FlashHobData.SubAreaData.Length);\r
196 }\r
197\r
198 if (FlashHobData.AreaType == EFI_FLASH_AREA_FTW_STATE) {\r
199 PcdSet32 (PcdFlashNvStorageFtwWorkingBase, (UINT32) FlashHobData.SubAreaData.Base);\r
200 PcdSet32 (PcdFlashNvStorageFtwWorkingSize, (UINT32) FlashHobData.SubAreaData.Length);\r
201 }\r
202\r
203 if (FlashHobData.AreaType == EFI_FLASH_AREA_FTW_BACKUP) {\r
204 PcdSet32 (PcdFlashNvStorageFtwSpareBase, (UINT32) FlashHobData.SubAreaData.Base);\r
205 PcdSet32 (PcdFlashNvStorageFtwSpareSize, (UINT32) FlashHobData.SubAreaData.Length);\r
206 }\r
207\r
208 switch (FlashHobData.AreaType) {\r
209 case EFI_FLASH_AREA_RECOVERY_BIOS:\r
210 case EFI_FLASH_AREA_MAIN_BIOS:\r
211 (*PeiServices)->CopyMem (\r
212 &FlashHobData.AreaTypeGuid,\r
213 &gEfiFirmwareFileSystemGuid,\r
214 sizeof (EFI_GUID)\r
215 );\r
216 (*PeiServices)->CopyMem (\r
217 &FlashHobData.SubAreaData.FileSystem,\r
218 &gEfiFirmwareVolumeBlockProtocolGuid,\r
219 sizeof (EFI_GUID)\r
220 );\r
221 break;\r
222\r
223 case EFI_FLASH_AREA_GUID_DEFINED:\r
224 (*PeiServices)->CopyMem (\r
225 &FlashHobData.AreaTypeGuid,\r
226 &mFlashAreaData[Index].AreaTypeGuid,\r
227 sizeof (EFI_GUID)\r
228 );\r
229 (*PeiServices)->CopyMem (\r
230 &FlashHobData.SubAreaData.FileSystem,\r
231 &gEfiFirmwareVolumeBlockProtocolGuid,\r
232 sizeof (EFI_GUID)\r
233 );\r
234 break;\r
235\r
236 default:\r
237 break;\r
238 }\r
239\r
240 BuildGuidDataHob (\r
241 &gEfiFlashMapHobGuid,\r
242 &FlashHobData,\r
243 sizeof (EFI_FLASH_AREA_HOB_DATA)\r
244 );\r
245 }\r
246\r
247 return EFI_SUCCESS;\r
248}\r
249\r
250EFI_STATUS\r
251EFIAPI\r
252GetAreaInfo (\r
253 IN EFI_PEI_SERVICES **PeiServices,\r
254 IN PEI_FLASH_MAP_PPI *This,\r
255 IN EFI_FLASH_AREA_TYPE AreaType,\r
256 IN EFI_GUID *AreaTypeGuid,\r
257 OUT UINT32 *NumEntries,\r
258 OUT EFI_FLASH_SUBAREA_ENTRY **Entries\r
259 )\r
260/*++\r
261\r
262 Routine Description: \r
263 Implementation of Flash Map PPI\r
264 \r
265--*/\r
266// TODO: function comment is missing 'Arguments:'\r
267// TODO: function comment is missing 'Returns:'\r
268// TODO: PeiServices - add argument and description to function comment\r
269// TODO: This - add argument and description to function comment\r
270// TODO: AreaType - add argument and description to function comment\r
271// TODO: AreaTypeGuid - add argument and description to function comment\r
272// TODO: NumEntries - add argument and description to function comment\r
273// TODO: Entries - add argument and description to function comment\r
274// TODO: EFI_SUCCESS - add return value to function comment\r
275// TODO: EFI_NOT_FOUND - add return value to function comment\r
276{\r
277 EFI_STATUS Status;\r
278 EFI_PEI_HOB_POINTERS Hob;\r
279 EFI_HOB_FLASH_MAP_ENTRY_TYPE *FlashMapEntry;\r
280\r
281 Status = PeiServicesGetHobList ((VOID **)&Hob.Raw);\r
282 while (!END_OF_HOB_LIST (Hob)) {\r
283 if (Hob.Header->HobType == EFI_HOB_TYPE_GUID_EXTENSION && CompareGuid (&Hob.Guid->Name, &gEfiFlashMapHobGuid)) {\r
284 FlashMapEntry = (EFI_HOB_FLASH_MAP_ENTRY_TYPE *) Hob.Raw;\r
285 if (AreaType == FlashMapEntry->AreaType) {\r
286 if (AreaType == EFI_FLASH_AREA_GUID_DEFINED) {\r
287 if (!CompareGuid (AreaTypeGuid, &FlashMapEntry->AreaTypeGuid)) {\r
288 goto NextHob;\r
289 }\r
290 }\r
291\r
292 *NumEntries = FlashMapEntry->NumEntries;\r
293 *Entries = FlashMapEntry->Entries;\r
294 return EFI_SUCCESS;\r
295 }\r
296 }\r
297 NextHob:\r
298 Hob.Raw = GET_NEXT_HOB (Hob);\r
299 }\r
300\r
301 return EFI_NOT_FOUND;\r
302}\r