]>
Commit | Line | Data |
---|---|---|
162ed594 | 1 | /** @file\r |
022c6d45 | 2 | Implementations for Firmware Volume Block protocol.\r |
3 | \r | |
d613c2a8 | 4 | It consumes FV HOBs and creates read-only Firmare Volume Block protocol\r |
e94a9ff7 | 5 | instances for each of them.\r |
23c98c94 | 6 | \r |
cd5ebaa0 HT |
7 | Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>\r |
8 | This program and the accompanying materials\r | |
23c98c94 | 9 | are licensed and made available under the terms and conditions of the BSD License\r |
10 | which accompanies this distribution. The full text of the license may be found at\r | |
11 | http://opensource.org/licenses/bsd-license.php\r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
28a00297 | 15 | \r |
797a9d67 | 16 | **/\r |
28a00297 | 17 | \r |
9c4ac31c | 18 | #include "DxeMain.h"\r |
ec90508b | 19 | #include "FwVolBlock.h"\r |
28a00297 | 20 | \r |
84266565 | 21 | FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {\r |
28a00297 | 22 | {\r |
23 | {\r | |
84266565 | 24 | HARDWARE_DEVICE_PATH,\r |
25 | HW_MEMMAP_DP,\r | |
28a00297 | 26 | {\r |
84266565 | 27 | (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),\r |
28 | (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)\r | |
29 | }\r | |
28a00297 | 30 | },\r |
84266565 | 31 | EfiMemoryMappedIO,\r |
32 | (EFI_PHYSICAL_ADDRESS) 0,\r | |
33 | (EFI_PHYSICAL_ADDRESS) 0,\r | |
34 | },\r | |
35 | {\r | |
36 | END_DEVICE_PATH_TYPE,\r | |
37 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
28a00297 | 38 | {\r |
84266565 | 39 | END_DEVICE_PATH_LENGTH,\r |
40 | 0\r | |
41 | }\r | |
42 | }\r | |
43 | };\r | |
44 | \r | |
45 | FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate = {\r | |
46 | {\r | |
47 | {\r | |
48 | MEDIA_DEVICE_PATH,\r | |
49 | MEDIA_PIWG_FW_VOL_DP,\r | |
022c6d45 | 50 | {\r |
84266565 | 51 | (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH)),\r |
52 | (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH) >> 8)\r | |
022c6d45 | 53 | }\r |
28a00297 | 54 | },\r |
84266565 | 55 | { 0 }\r |
28a00297 | 56 | },\r |
84266565 | 57 | {\r |
58 | END_DEVICE_PATH_TYPE,\r | |
59 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
60 | {\r | |
61 | END_DEVICE_PATH_LENGTH,\r | |
62 | 0\r | |
63 | }\r | |
64 | }\r | |
65 | };\r | |
66 | \r | |
67 | EFI_FW_VOL_BLOCK_DEVICE mFwVolBlock = {\r | |
68 | FVB_DEVICE_SIGNATURE,\r | |
69 | NULL,\r | |
70 | NULL,\r | |
28a00297 | 71 | {\r |
72 | FwVolBlockGetAttributes,\r | |
73 | (EFI_FVB_SET_ATTRIBUTES)FwVolBlockSetAttributes,\r | |
74 | FwVolBlockGetPhysicalAddress,\r | |
75 | FwVolBlockGetBlockSize,\r | |
76 | FwVolBlockReadBlock,\r | |
77 | (EFI_FVB_WRITE)FwVolBlockWriteBlock,\r | |
78 | (EFI_FVB_ERASE_BLOCKS)FwVolBlockEraseBlock,\r | |
022c6d45 | 79 | NULL\r |
28a00297 | 80 | },\r |
81 | 0,\r | |
82 | NULL,\r | |
83 | 0,\r | |
84 | 0\r | |
85 | };\r | |
86 | \r | |
87 | \r | |
88 | \r | |
162ed594 | 89 | /**\r |
90 | Retrieves Volume attributes. No polarity translations are done.\r | |
91 | \r | |
022c6d45 | 92 | @param This Calling context\r |
93 | @param Attributes output buffer which contains attributes\r | |
162ed594 | 94 | \r |
95 | @retval EFI_SUCCESS The firmware volume attributes were returned.\r | |
96 | \r | |
97 | **/\r | |
28a00297 | 98 | EFI_STATUS\r |
99 | EFIAPI\r | |
100 | FwVolBlockGetAttributes (\r | |
101 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
8ee3a199 | 102 | OUT EFI_FVB_ATTRIBUTES_2 *Attributes\r |
28a00297 | 103 | )\r |
28a00297 | 104 | {\r |
105 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
022c6d45 | 106 | \r |
28a00297 | 107 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r |
108 | \r | |
109 | //\r | |
110 | // Since we are read only, it's safe to get attributes data from our in-memory copy.\r | |
111 | //\r | |
8a9e0b72 | 112 | *Attributes = FvbDevice->FvbAttributes & ~EFI_FVB2_WRITE_STATUS;\r |
28a00297 | 113 | \r |
114 | return EFI_SUCCESS;\r | |
115 | }\r | |
116 | \r | |
117 | \r | |
162ed594 | 118 | \r |
119 | /**\r | |
120 | Modifies the current settings of the firmware volume according to the input parameter.\r | |
121 | \r | |
022c6d45 | 122 | @param This Calling context\r |
123 | @param Attributes input buffer which contains attributes\r | |
162ed594 | 124 | \r |
022c6d45 | 125 | @retval EFI_SUCCESS The firmware volume attributes were returned.\r |
126 | @retval EFI_INVALID_PARAMETER The attributes requested are in conflict with\r | |
127 | the capabilities as declared in the firmware\r | |
128 | volume header.\r | |
162ed594 | 129 | @retval EFI_UNSUPPORTED Not supported.\r |
130 | \r | |
131 | **/\r | |
28a00297 | 132 | EFI_STATUS\r |
133 | EFIAPI\r | |
134 | FwVolBlockSetAttributes (\r | |
135 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
8ee3a199 | 136 | IN CONST EFI_FVB_ATTRIBUTES_2 *Attributes\r |
28a00297 | 137 | )\r |
28a00297 | 138 | {\r |
139 | return EFI_UNSUPPORTED;\r | |
140 | }\r | |
141 | \r | |
142 | \r | |
162ed594 | 143 | \r |
144 | /**\r | |
145 | The EraseBlock() function erases one or more blocks as denoted by the\r | |
146 | variable argument list. The entire parameter list of blocks must be verified\r | |
147 | prior to erasing any blocks. If a block is requested that does not exist\r | |
148 | within the associated firmware volume (it has a larger index than the last\r | |
149 | block of the firmware volume), the EraseBlock() function must return\r | |
150 | EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.\r | |
151 | \r | |
022c6d45 | 152 | @param This Calling context\r |
153 | @param ... Starting LBA followed by Number of Lba to erase.\r | |
154 | a -1 to terminate the list.\r | |
155 | \r | |
156 | @retval EFI_SUCCESS The erase request was successfully completed.\r | |
157 | @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled\r | |
158 | state.\r | |
159 | @retval EFI_DEVICE_ERROR The block device is not functioning correctly\r | |
160 | and could not be written. The firmware device\r | |
161 | may have been partially erased.\r | |
162 | @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the variable\r | |
163 | argument list do\r | |
162ed594 | 164 | @retval EFI_UNSUPPORTED Not supported.\r |
165 | \r | |
166 | **/\r | |
28a00297 | 167 | EFI_STATUS\r |
168 | EFIAPI\r | |
169 | FwVolBlockEraseBlock (\r | |
170 | IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
171 | ...\r | |
172 | )\r | |
28a00297 | 173 | {\r |
174 | return EFI_UNSUPPORTED;\r | |
175 | }\r | |
176 | \r | |
177 | \r | |
162ed594 | 178 | \r |
179 | /**\r | |
180 | Read the specified number of bytes from the block to the input buffer.\r | |
181 | \r | |
022c6d45 | 182 | @param This Indicates the calling context.\r |
183 | @param Lba The starting logical block index to read.\r | |
184 | @param Offset Offset into the block at which to begin reading.\r | |
185 | @param NumBytes Pointer to a UINT32. At entry, *NumBytes\r | |
186 | contains the total size of the buffer. At exit,\r | |
187 | *NumBytes contains the total number of bytes\r | |
188 | actually read.\r | |
189 | @param Buffer Pinter to a caller-allocated buffer that\r | |
190 | contains the destine for the read.\r | |
191 | \r | |
192 | @retval EFI_SUCCESS The firmware volume was read successfully.\r | |
193 | @retval EFI_BAD_BUFFER_SIZE The read was attempted across an LBA boundary.\r | |
194 | @retval EFI_ACCESS_DENIED Access denied.\r | |
195 | @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not\r | |
162ed594 | 196 | be read.\r |
197 | \r | |
198 | **/\r | |
28a00297 | 199 | EFI_STATUS\r |
200 | EFIAPI\r | |
201 | FwVolBlockReadBlock (\r | |
202 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
203 | IN CONST EFI_LBA Lba,\r | |
204 | IN CONST UINTN Offset,\r | |
205 | IN OUT UINTN *NumBytes,\r | |
206 | IN OUT UINT8 *Buffer\r | |
207 | )\r | |
28a00297 | 208 | {\r |
209 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
210 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
211 | UINT8 *LbaOffset;\r | |
212 | UINTN LbaStart;\r | |
213 | UINTN NumOfBytesRead;\r | |
214 | UINTN LbaIndex;\r | |
022c6d45 | 215 | \r |
28a00297 | 216 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r |
217 | \r | |
218 | //\r | |
219 | // Check if This FW can be read\r | |
220 | //\r | |
797a9d67 | 221 | if ((FvbDevice->FvbAttributes & EFI_FVB2_READ_STATUS) == 0) {\r |
28a00297 | 222 | return EFI_ACCESS_DENIED;\r |
223 | }\r | |
022c6d45 | 224 | \r |
e94a9ff7 | 225 | LbaIndex = (UINTN) Lba;\r |
28a00297 | 226 | if (LbaIndex >= FvbDevice->NumBlocks) {\r |
227 | //\r | |
228 | // Invalid Lba, read nothing.\r | |
229 | //\r | |
230 | *NumBytes = 0;\r | |
231 | return EFI_BAD_BUFFER_SIZE;\r | |
232 | }\r | |
022c6d45 | 233 | \r |
28a00297 | 234 | if (Offset > FvbDevice->LbaCache[LbaIndex].Length) {\r |
235 | //\r | |
236 | // all exceed boundry, read nothing.\r | |
237 | //\r | |
238 | *NumBytes = 0;\r | |
239 | return EFI_BAD_BUFFER_SIZE;\r | |
240 | }\r | |
022c6d45 | 241 | \r |
28a00297 | 242 | NumOfBytesRead = *NumBytes;\r |
243 | if (Offset + NumOfBytesRead > FvbDevice->LbaCache[LbaIndex].Length) {\r | |
244 | //\r | |
245 | // partial exceed boundry, read data from current postion to end.\r | |
246 | //\r | |
247 | NumOfBytesRead = FvbDevice->LbaCache[LbaIndex].Length - Offset;\r | |
248 | }\r | |
022c6d45 | 249 | \r |
28a00297 | 250 | LbaStart = FvbDevice->LbaCache[LbaIndex].Base;\r |
e94a9ff7 | 251 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN) FvbDevice->BaseAddress);\r |
252 | LbaOffset = (UINT8 *) FwVolHeader + LbaStart + Offset;\r | |
28a00297 | 253 | \r |
254 | //\r | |
255 | // Perform read operation\r | |
256 | //\r | |
257 | CopyMem (Buffer, LbaOffset, NumOfBytesRead);\r | |
022c6d45 | 258 | \r |
28a00297 | 259 | if (NumOfBytesRead == *NumBytes) {\r |
260 | return EFI_SUCCESS;\r | |
261 | }\r | |
022c6d45 | 262 | \r |
28a00297 | 263 | *NumBytes = NumOfBytesRead;\r |
264 | return EFI_BAD_BUFFER_SIZE;\r | |
265 | }\r | |
022c6d45 | 266 | \r |
28a00297 | 267 | \r |
162ed594 | 268 | \r |
269 | /**\r | |
270 | Writes the specified number of bytes from the input buffer to the block.\r | |
271 | \r | |
022c6d45 | 272 | @param This Indicates the calling context.\r |
273 | @param Lba The starting logical block index to write to.\r | |
274 | @param Offset Offset into the block at which to begin writing.\r | |
275 | @param NumBytes Pointer to a UINT32. At entry, *NumBytes\r | |
276 | contains the total size of the buffer. At exit,\r | |
277 | *NumBytes contains the total number of bytes\r | |
278 | actually written.\r | |
279 | @param Buffer Pinter to a caller-allocated buffer that\r | |
280 | contains the source for the write.\r | |
281 | \r | |
282 | @retval EFI_SUCCESS The firmware volume was written successfully.\r | |
283 | @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundary.\r | |
284 | On output, NumBytes contains the total number of\r | |
285 | bytes actually written.\r | |
286 | @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled\r | |
287 | state.\r | |
288 | @retval EFI_DEVICE_ERROR The block device is malfunctioning and could not\r | |
289 | be written.\r | |
162ed594 | 290 | @retval EFI_UNSUPPORTED Not supported.\r |
291 | \r | |
292 | **/\r | |
28a00297 | 293 | EFI_STATUS\r |
294 | EFIAPI\r | |
295 | FwVolBlockWriteBlock (\r | |
296 | IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
297 | IN EFI_LBA Lba,\r | |
298 | IN UINTN Offset,\r | |
299 | IN OUT UINTN *NumBytes,\r | |
300 | IN UINT8 *Buffer\r | |
301 | )\r | |
28a00297 | 302 | {\r |
303 | return EFI_UNSUPPORTED;\r | |
304 | }\r | |
022c6d45 | 305 | \r |
28a00297 | 306 | \r |
162ed594 | 307 | \r |
308 | /**\r | |
309 | Get Fvb's base address.\r | |
310 | \r | |
022c6d45 | 311 | @param This Indicates the calling context.\r |
312 | @param Address Fvb device base address.\r | |
162ed594 | 313 | \r |
022c6d45 | 314 | @retval EFI_SUCCESS Successfully got Fvb's base address.\r |
162ed594 | 315 | @retval EFI_UNSUPPORTED Not supported.\r |
316 | \r | |
317 | **/\r | |
28a00297 | 318 | EFI_STATUS\r |
319 | EFIAPI\r | |
320 | FwVolBlockGetPhysicalAddress (\r | |
321 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
322 | OUT EFI_PHYSICAL_ADDRESS *Address\r | |
323 | )\r | |
28a00297 | 324 | {\r |
325 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
022c6d45 | 326 | \r |
28a00297 | 327 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r |
022c6d45 | 328 | \r |
6e536468 | 329 | if ((FvbDevice->FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {\r |
28a00297 | 330 | *Address = FvbDevice->BaseAddress;\r |
331 | return EFI_SUCCESS;\r | |
332 | }\r | |
022c6d45 | 333 | \r |
28a00297 | 334 | return EFI_UNSUPPORTED;\r |
335 | }\r | |
336 | \r | |
337 | \r | |
162ed594 | 338 | \r |
339 | /**\r | |
340 | Retrieves the size in bytes of a specific block within a firmware volume.\r | |
341 | \r | |
022c6d45 | 342 | @param This Indicates the calling context.\r |
343 | @param Lba Indicates the block for which to return the\r | |
344 | size.\r | |
345 | @param BlockSize Pointer to a caller-allocated UINTN in which the\r | |
346 | size of the block is returned.\r | |
347 | @param NumberOfBlocks Pointer to a caller-allocated UINTN in which the\r | |
348 | number of consecutive blocks starting with Lba\r | |
349 | is returned. All blocks in this range have a\r | |
350 | size of BlockSize.\r | |
351 | \r | |
352 | @retval EFI_SUCCESS The firmware volume base address is returned.\r | |
162ed594 | 353 | @retval EFI_INVALID_PARAMETER The requested LBA is out of range.\r |
354 | \r | |
355 | **/\r | |
28a00297 | 356 | EFI_STATUS\r |
357 | EFIAPI\r | |
358 | FwVolBlockGetBlockSize (\r | |
359 | IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,\r | |
360 | IN CONST EFI_LBA Lba,\r | |
361 | IN OUT UINTN *BlockSize,\r | |
362 | IN OUT UINTN *NumberOfBlocks\r | |
363 | )\r | |
28a00297 | 364 | {\r |
365 | UINTN TotalBlocks;\r | |
366 | EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;\r | |
367 | EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r | |
368 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
022c6d45 | 369 | \r |
28a00297 | 370 | FvbDevice = FVB_DEVICE_FROM_THIS (This);\r |
022c6d45 | 371 | \r |
28a00297 | 372 | //\r |
373 | // Do parameter checking\r | |
374 | //\r | |
375 | if (Lba >= FvbDevice->NumBlocks) {\r | |
376 | return EFI_INVALID_PARAMETER;\r | |
377 | }\r | |
022c6d45 | 378 | \r |
28a00297 | 379 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN)FvbDevice->BaseAddress);\r |
022c6d45 | 380 | \r |
28a00297 | 381 | PtrBlockMapEntry = FwVolHeader->BlockMap;\r |
022c6d45 | 382 | \r |
28a00297 | 383 | //\r |
384 | // Search the block map for the given block\r | |
385 | //\r | |
386 | TotalBlocks = 0;\r | |
387 | while ((PtrBlockMapEntry->NumBlocks != 0) || (PtrBlockMapEntry->Length !=0 )) {\r | |
388 | TotalBlocks += PtrBlockMapEntry->NumBlocks;\r | |
389 | if (Lba < TotalBlocks) {\r | |
390 | //\r | |
391 | // We find the range\r | |
392 | //\r | |
393 | break;\r | |
394 | }\r | |
022c6d45 | 395 | \r |
28a00297 | 396 | PtrBlockMapEntry++;\r |
397 | }\r | |
022c6d45 | 398 | \r |
28a00297 | 399 | *BlockSize = PtrBlockMapEntry->Length;\r |
400 | *NumberOfBlocks = TotalBlocks - (UINTN)Lba;\r | |
022c6d45 | 401 | \r |
28a00297 | 402 | return EFI_SUCCESS;\r |
403 | }\r | |
404 | \r | |
405 | \r | |
162ed594 | 406 | \r |
407 | /**\r | |
408 | This routine produces a firmware volume block protocol on a given\r | |
409 | buffer.\r | |
410 | \r | |
022c6d45 | 411 | @param BaseAddress base address of the firmware volume image\r |
412 | @param Length length of the firmware volume image\r | |
413 | @param ParentHandle handle of parent firmware volume, if this image\r | |
414 | came from an FV image file in another firmware\r | |
415 | volume (ala capsules)\r | |
416 | @param FvProtocol Firmware volume block protocol produced.\r | |
162ed594 | 417 | \r |
022c6d45 | 418 | @retval EFI_VOLUME_CORRUPTED Volume corrupted.\r |
419 | @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.\r | |
420 | @retval EFI_SUCCESS Successfully produced a FVB protocol on given\r | |
162ed594 | 421 | buffer.\r |
422 | \r | |
423 | **/\r | |
28a00297 | 424 | EFI_STATUS\r |
425 | ProduceFVBProtocolOnBuffer (\r | |
426 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
427 | IN UINT64 Length,\r | |
428 | IN EFI_HANDLE ParentHandle,\r | |
429 | OUT EFI_HANDLE *FvProtocol OPTIONAL\r | |
430 | )\r | |
28a00297 | 431 | {\r |
432 | EFI_STATUS Status;\r | |
433 | EFI_FW_VOL_BLOCK_DEVICE *FvbDev;\r | |
434 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
435 | UINTN BlockIndex;\r | |
436 | UINTN BlockIndex2;\r | |
437 | UINTN LinearOffset;\r | |
38837959 | 438 | UINT32 FvAlignment;\r |
28a00297 | 439 | EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r |
022c6d45 | 440 | \r |
38837959 | 441 | FvAlignment = 0;\r |
e94a9ff7 | 442 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN) BaseAddress;\r |
28a00297 | 443 | //\r |
444 | // Validate FV Header, if not as expected, return\r | |
445 | //\r | |
446 | if (FwVolHeader->Signature != EFI_FVH_SIGNATURE) {\r | |
447 | return EFI_VOLUME_CORRUPTED;\r | |
448 | }\r | |
449 | //\r | |
38837959 LG |
450 | // Get FvHeader alignment\r |
451 | //\r | |
452 | FvAlignment = 1 << ((FwVolHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);\r | |
6d9a0f28 LG |
453 | //\r |
454 | // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value. \r | |
455 | //\r | |
38837959 LG |
456 | if (FvAlignment < 8) {\r |
457 | FvAlignment = 8;\r | |
458 | }\r | |
459 | if ((UINTN)BaseAddress % FvAlignment != 0) {\r | |
460 | //\r | |
461 | // FvImage buffer is not at its required alignment.\r | |
462 | //\r | |
463 | return EFI_VOLUME_CORRUPTED;\r | |
464 | }\r | |
465 | //\r | |
022c6d45 | 466 | // Allocate EFI_FW_VOL_BLOCK_DEVICE\r |
28a00297 | 467 | //\r |
9c4ac31c | 468 | FvbDev = AllocateCopyPool (sizeof (EFI_FW_VOL_BLOCK_DEVICE), &mFwVolBlock);\r |
28a00297 | 469 | if (FvbDev == NULL) {\r |
470 | return EFI_OUT_OF_RESOURCES;\r | |
471 | }\r | |
472 | \r | |
473 | FvbDev->BaseAddress = BaseAddress;\r | |
474 | FvbDev->FvbAttributes = FwVolHeader->Attributes;\r | |
475 | FvbDev->FwVolBlockInstance.ParentHandle = ParentHandle;\r | |
476 | \r | |
477 | //\r | |
478 | // Init the block caching fields of the device\r | |
479 | // First, count the number of blocks\r | |
480 | //\r | |
481 | FvbDev->NumBlocks = 0;\r | |
482 | for (PtrBlockMapEntry = FwVolHeader->BlockMap;\r | |
e94a9ff7 | 483 | PtrBlockMapEntry->NumBlocks != 0;\r |
484 | PtrBlockMapEntry++) {\r | |
28a00297 | 485 | FvbDev->NumBlocks += PtrBlockMapEntry->NumBlocks;\r |
486 | }\r | |
487 | //\r | |
488 | // Second, allocate the cache\r | |
489 | //\r | |
9c4ac31c | 490 | FvbDev->LbaCache = AllocatePool (FvbDev->NumBlocks * sizeof (LBA_CACHE));\r |
28a00297 | 491 | if (FvbDev->LbaCache == NULL) {\r |
492 | CoreFreePool (FvbDev);\r | |
493 | return EFI_OUT_OF_RESOURCES;\r | |
494 | }\r | |
84266565 | 495 | \r |
28a00297 | 496 | //\r |
497 | // Last, fill in the cache with the linear address of the blocks\r | |
498 | //\r | |
499 | BlockIndex = 0;\r | |
500 | LinearOffset = 0;\r | |
501 | for (PtrBlockMapEntry = FwVolHeader->BlockMap;\r | |
e94a9ff7 | 502 | PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r |
28a00297 | 503 | for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r |
504 | FvbDev->LbaCache[BlockIndex].Base = LinearOffset;\r | |
505 | FvbDev->LbaCache[BlockIndex].Length = PtrBlockMapEntry->Length;\r | |
506 | LinearOffset += PtrBlockMapEntry->Length;\r | |
507 | BlockIndex++;\r | |
508 | }\r | |
509 | }\r | |
510 | \r | |
511 | //\r | |
84266565 | 512 | // Judget whether FV name guid is produced in Fv extension header\r |
28a00297 | 513 | //\r |
84266565 | 514 | if (FwVolHeader->ExtHeaderOffset == 0) {\r |
515 | //\r | |
516 | // FV does not contains extension header, then produce MEMMAP_DEVICE_PATH\r | |
517 | //\r | |
518 | FvbDev->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_MEMMAP_DEVICE_PATH), &mFvMemmapDevicePathTemplate);\r | |
18fcd6a7 LG |
519 | if (FvbDev->DevicePath == NULL) {\r |
520 | FreePool (FvbDev);\r | |
521 | return EFI_OUT_OF_RESOURCES;\r | |
522 | }\r | |
84266565 | 523 | ((FV_MEMMAP_DEVICE_PATH *) FvbDev->DevicePath)->MemMapDevPath.StartingAddress = BaseAddress;\r |
524 | ((FV_MEMMAP_DEVICE_PATH *) FvbDev->DevicePath)->MemMapDevPath.EndingAddress = BaseAddress + FwVolHeader->FvLength - 1;\r | |
525 | } else {\r | |
526 | //\r | |
527 | // FV contains extension header, then produce MEDIA_FW_VOL_DEVICE_PATH\r | |
528 | //\r | |
529 | FvbDev->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) AllocateCopyPool (sizeof (FV_PIWG_DEVICE_PATH), &mFvPIWGDevicePathTemplate);\r | |
18fcd6a7 LG |
530 | if (FvbDev->DevicePath == NULL) {\r |
531 | FreePool (FvbDev);\r | |
532 | return EFI_OUT_OF_RESOURCES;\r | |
533 | }\r | |
84266565 | 534 | CopyGuid (\r |
535 | &((FV_PIWG_DEVICE_PATH *)FvbDev->DevicePath)->FvDevPath.FvName, \r | |
536 | (GUID *)(UINTN)(BaseAddress + FwVolHeader->ExtHeaderOffset)\r | |
537 | );\r | |
538 | }\r | |
539 | \r | |
28a00297 | 540 | //\r |
541 | //\r | |
542 | // Attach FvVolBlock Protocol to new handle\r | |
543 | //\r | |
544 | Status = CoreInstallMultipleProtocolInterfaces (\r | |
e94a9ff7 | 545 | &FvbDev->Handle,\r |
546 | &gEfiFirmwareVolumeBlockProtocolGuid, &FvbDev->FwVolBlockInstance,\r | |
84266565 | 547 | &gEfiDevicePathProtocolGuid, FvbDev->DevicePath,\r |
e94a9ff7 | 548 | NULL\r |
549 | );\r | |
28a00297 | 550 | \r |
551 | //\r | |
552 | // If they want the handle back, set it.\r | |
553 | //\r | |
554 | if (FvProtocol != NULL) {\r | |
555 | *FvProtocol = FvbDev->Handle;\r | |
556 | }\r | |
557 | \r | |
558 | return Status;\r | |
559 | }\r | |
560 | \r | |
561 | \r | |
162ed594 | 562 | \r |
563 | /**\r | |
ec90508b | 564 | This routine consumes FV hobs and produces instances of FW_VOL_BLOCK_PROTOCOL as appropriate.\r |
162ed594 | 565 | \r |
022c6d45 | 566 | @param ImageHandle The image handle.\r |
567 | @param SystemTable The system table.\r | |
162ed594 | 568 | \r |
022c6d45 | 569 | @retval EFI_SUCCESS Successfully initialized firmware volume block\r |
162ed594 | 570 | driver.\r |
571 | \r | |
572 | **/\r | |
28a00297 | 573 | EFI_STATUS\r |
574 | EFIAPI\r | |
575 | FwVolBlockDriverInit (\r | |
576 | IN EFI_HANDLE ImageHandle,\r | |
577 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
578 | )\r | |
28a00297 | 579 | {\r |
580 | EFI_PEI_HOB_POINTERS FvHob;\r | |
e94a9ff7 | 581 | \r |
28a00297 | 582 | //\r |
583 | // Core Needs Firmware Volumes to function\r | |
584 | //\r | |
585 | FvHob.Raw = GetHobList ();\r | |
586 | while ((FvHob.Raw = GetNextHob (EFI_HOB_TYPE_FV, FvHob.Raw)) != NULL) {\r | |
587 | //\r | |
588 | // Produce an FVB protocol for it\r | |
589 | //\r | |
022c6d45 | 590 | ProduceFVBProtocolOnBuffer (FvHob.FirmwareVolume->BaseAddress, FvHob.FirmwareVolume->Length, NULL, NULL);\r |
28a00297 | 591 | FvHob.Raw = GET_NEXT_HOB (FvHob);\r |
592 | }\r | |
e94a9ff7 | 593 | \r |
28a00297 | 594 | return EFI_SUCCESS;\r |
595 | }\r | |
596 | \r | |
597 | \r | |
162ed594 | 598 | \r |
599 | /**\r | |
600 | This DXE service routine is used to process a firmware volume. In\r | |
601 | particular, it can be called by BDS to process a single firmware\r | |
602 | volume found in a capsule.\r | |
603 | \r | |
022c6d45 | 604 | @param FvHeader pointer to a firmware volume header\r |
605 | @param Size the size of the buffer pointed to by FvHeader\r | |
606 | @param FVProtocolHandle the handle on which a firmware volume protocol\r | |
607 | was produced for the firmware volume passed in.\r | |
162ed594 | 608 | \r |
022c6d45 | 609 | @retval EFI_OUT_OF_RESOURCES if an FVB could not be produced due to lack of\r |
610 | system resources\r | |
611 | @retval EFI_VOLUME_CORRUPTED if the volume was corrupted\r | |
612 | @retval EFI_SUCCESS a firmware volume protocol was produced for the\r | |
162ed594 | 613 | firmware volume\r |
614 | \r | |
615 | **/\r | |
28a00297 | 616 | EFI_STATUS\r |
3b6ffb6a | 617 | EFIAPI\r |
28a00297 | 618 | CoreProcessFirmwareVolume (\r |
619 | IN VOID *FvHeader,\r | |
022c6d45 | 620 | IN UINTN Size,\r |
28a00297 | 621 | OUT EFI_HANDLE *FVProtocolHandle\r |
622 | )\r | |
28a00297 | 623 | {\r |
624 | VOID *Ptr;\r | |
625 | EFI_STATUS Status;\r | |
626 | \r | |
627 | *FVProtocolHandle = NULL;\r | |
022c6d45 | 628 | Status = ProduceFVBProtocolOnBuffer (\r |
629 | (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r | |
630 | (UINT64)Size,\r | |
631 | NULL,\r | |
28a00297 | 632 | FVProtocolHandle\r |
633 | );\r | |
634 | //\r | |
635 | // Since in our implementation we use register-protocol-notify to put a\r | |
636 | // FV protocol on the FVB protocol handle, we can't directly verify that\r | |
637 | // the FV protocol was produced. Therefore here we will check the handle\r | |
022c6d45 | 638 | // and make sure an FV protocol is on it. This indicates that all went\r |
639 | // well. Otherwise we have to assume that the volume was corrupted\r | |
28a00297 | 640 | // somehow.\r |
641 | //\r | |
642 | if (!EFI_ERROR(Status)) {\r | |
643 | Ptr = NULL;\r | |
e94a9ff7 | 644 | Status = CoreHandleProtocol (*FVProtocolHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **) &Ptr);\r |
28a00297 | 645 | if (EFI_ERROR(Status) || (Ptr == NULL)) {\r |
646 | return EFI_VOLUME_CORRUPTED;\r | |
647 | }\r | |
648 | return EFI_SUCCESS;\r | |
649 | }\r | |
650 | return Status;\r | |
651 | }\r | |
652 | \r | |
653 | \r | |
162ed594 | 654 | \r |