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