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