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