Variable PEIM use to PCD entry to get the base address of NV Storegae region.
[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 switch (FlashHobData.AreaType) {
180 case EFI_FLASH_AREA_RECOVERY_BIOS:
181 case EFI_FLASH_AREA_MAIN_BIOS:
182 (*PeiServices)->CopyMem (
183 &FlashHobData.AreaTypeGuid,
184 &gEfiFirmwareFileSystemGuid,
185 sizeof (EFI_GUID)
186 );
187 (*PeiServices)->CopyMem (
188 &FlashHobData.SubAreaData.FileSystem,
189 &gEfiFirmwareVolumeBlockProtocolGuid,
190 sizeof (EFI_GUID)
191 );
192 break;
193
194 case EFI_FLASH_AREA_GUID_DEFINED:
195 (*PeiServices)->CopyMem (
196 &FlashHobData.AreaTypeGuid,
197 &gEfiSystemNvDataHobGuid,
198 sizeof (EFI_GUID)
199 );
200 (*PeiServices)->CopyMem (
201 &FlashHobData.SubAreaData.FileSystem,
202 &gEfiFirmwareVolumeBlockProtocolGuid,
203 sizeof (EFI_GUID)
204 );
205
206 //
207 // We also update a PCD entry so that any driver that depend on
208 // PCD entry PcdFlashNvStorageVariableBase will get the information.
209 //
210 PcdSet32 (PcdFlashNvStorageVariableBase, (UINT32) FlashHobData.SubAreaData.Base);
211 break;
212
213 default:
214 break;
215 }
216
217 BuildGuidDataHob (
218 &gEfiFlashMapHobGuid,
219 &FlashHobData,
220 sizeof (EFI_FLASH_AREA_HOB_DATA)
221 );
222 }
223
224 return EFI_SUCCESS;
225 }
226
227 EFI_STATUS
228 EFIAPI
229 GetAreaInfo (
230 IN EFI_PEI_SERVICES **PeiServices,
231 IN PEI_FLASH_MAP_PPI *This,
232 IN EFI_FLASH_AREA_TYPE AreaType,
233 IN EFI_GUID *AreaTypeGuid,
234 OUT UINT32 *NumEntries,
235 OUT EFI_FLASH_SUBAREA_ENTRY **Entries
236 )
237 /*++
238
239 Routine Description:
240 Implementation of Flash Map PPI
241
242 --*/
243 // TODO: function comment is missing 'Arguments:'
244 // TODO: function comment is missing 'Returns:'
245 // TODO: PeiServices - add argument and description to function comment
246 // TODO: This - add argument and description to function comment
247 // TODO: AreaType - add argument and description to function comment
248 // TODO: AreaTypeGuid - add argument and description to function comment
249 // TODO: NumEntries - add argument and description to function comment
250 // TODO: Entries - add argument and description to function comment
251 // TODO: EFI_SUCCESS - add return value to function comment
252 // TODO: EFI_NOT_FOUND - add return value to function comment
253 {
254 EFI_STATUS Status;
255 EFI_PEI_HOB_POINTERS Hob;
256 EFI_HOB_FLASH_MAP_ENTRY_TYPE *FlashMapEntry;
257
258 Status = PeiServicesGetHobList (&Hob.Raw);
259 while (!END_OF_HOB_LIST (Hob)) {
260 if (Hob.Header->HobType == EFI_HOB_TYPE_GUID_EXTENSION && CompareGuid (&Hob.Guid->Name, &gEfiFlashMapHobGuid)) {
261 FlashMapEntry = (EFI_HOB_FLASH_MAP_ENTRY_TYPE *) Hob.Raw;
262 if (AreaType == FlashMapEntry->AreaType) {
263 if (AreaType == EFI_FLASH_AREA_GUID_DEFINED) {
264 if (!CompareGuid (AreaTypeGuid, &FlashMapEntry->AreaTypeGuid)) {
265 continue;
266 }
267 }
268
269 *NumEntries = FlashMapEntry->NumEntries;
270 *Entries = FlashMapEntry->Entries;
271 return EFI_SUCCESS;
272 }
273 }
274
275 Hob.Raw = GET_NEXT_HOB (Hob);
276 }
277
278 return EFI_NOT_FOUND;
279 }