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