Add description of module
[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 break;
206
207 default:
208 break;
209 }
210
211 BuildGuidDataHob (
212 &gEfiFlashMapHobGuid,
213 &FlashHobData,
214 sizeof (EFI_FLASH_AREA_HOB_DATA)
215 );
216 }
217
218 return EFI_SUCCESS;
219 }
220
221 EFI_STATUS
222 EFIAPI
223 GetAreaInfo (
224 IN EFI_PEI_SERVICES **PeiServices,
225 IN PEI_FLASH_MAP_PPI *This,
226 IN EFI_FLASH_AREA_TYPE AreaType,
227 IN EFI_GUID *AreaTypeGuid,
228 OUT UINT32 *NumEntries,
229 OUT EFI_FLASH_SUBAREA_ENTRY **Entries
230 )
231 /*++
232
233 Routine Description:
234 Implementation of Flash Map PPI
235
236 --*/
237 // TODO: function comment is missing 'Arguments:'
238 // TODO: function comment is missing 'Returns:'
239 // TODO: PeiServices - add argument and description to function comment
240 // TODO: This - add argument and description to function comment
241 // TODO: AreaType - add argument and description to function comment
242 // TODO: AreaTypeGuid - add argument and description to function comment
243 // TODO: NumEntries - add argument and description to function comment
244 // TODO: Entries - add argument and description to function comment
245 // TODO: EFI_SUCCESS - add return value to function comment
246 // TODO: EFI_NOT_FOUND - add return value to function comment
247 {
248 EFI_STATUS Status;
249 EFI_PEI_HOB_POINTERS Hob;
250 EFI_HOB_FLASH_MAP_ENTRY_TYPE *FlashMapEntry;
251
252 Status = PeiServicesGetHobList (&Hob.Raw);
253 while (!END_OF_HOB_LIST (Hob)) {
254 if (Hob.Header->HobType == EFI_HOB_TYPE_GUID_EXTENSION && CompareGuid (&Hob.Guid->Name, &gEfiFlashMapHobGuid)) {
255 FlashMapEntry = (EFI_HOB_FLASH_MAP_ENTRY_TYPE *) Hob.Raw;
256 if (AreaType == FlashMapEntry->AreaType) {
257 if (AreaType == EFI_FLASH_AREA_GUID_DEFINED) {
258 if (!CompareGuid (AreaTypeGuid, &FlashMapEntry->AreaTypeGuid)) {
259 continue;
260 }
261 }
262
263 *NumEntries = FlashMapEntry->NumEntries;
264 *Entries = FlashMapEntry->Entries;
265 return EFI_SUCCESS;
266 }
267 }
268
269 Hob.Raw = GET_NEXT_HOB (Hob);
270 }
271
272 return EFI_NOT_FOUND;
273 }