]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.c
SecurityPkg: Fix incorrect return value when File is NULL
[mirror_edk2.git] / SecurityPkg / Library / DxeTpm2MeasureBootLib / DxeTpm2MeasureBootLib.c
CommitLineData
c1d93242
JY
1/** @file\r
2 The library instance provides security service of TPM2 measure boot.\r
3\r
4 Caution: This file requires additional review when modified.\r
5 This library will have external input - PE/COFF image and GPT partition.\r
6 This external input must be validated carefully to avoid security issue like\r
7 buffer overflow, integer overflow.\r
8\r
9 DxeTpm2MeasureBootLibImageRead() function will make sure the PE/COFF image content\r
10 read is within the image buffer.\r
11\r
1abfa4ce 12 Tcg2MeasurePeImage() function will accept untrusted PE/COFF image and validate its\r
c1d93242
JY
13 data structure within this image buffer before use.\r
14\r
1abfa4ce 15 Tcg2MeasureGptTable() function will receive untrusted GPT partition table, and parse\r
c1d93242
JY
16 partition data carefully.\r
17\r
b3548d32 18Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
6aaac383 19(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
289b714b 20SPDX-License-Identifier: BSD-2-Clause-Patent\r
c1d93242
JY
21\r
22**/\r
23\r
24#include <PiDxe.h>\r
25\r
1abfa4ce 26#include <Protocol/Tcg2Protocol.h>\r
c1d93242
JY
27#include <Protocol/BlockIo.h>\r
28#include <Protocol/DiskIo.h>\r
29#include <Protocol/DevicePathToText.h>\r
30#include <Protocol/FirmwareVolumeBlock.h>\r
31\r
32#include <Guid/MeasuredFvHob.h>\r
33\r
34#include <Library/BaseLib.h>\r
35#include <Library/DebugLib.h>\r
36#include <Library/BaseMemoryLib.h>\r
37#include <Library/MemoryAllocationLib.h>\r
38#include <Library/DevicePathLib.h>\r
39#include <Library/UefiBootServicesTableLib.h>\r
40#include <Library/BaseCryptLib.h>\r
41#include <Library/PeCoffLib.h>\r
42#include <Library/SecurityManagementLib.h>\r
43#include <Library/HobLib.h>\r
44\r
45//\r
46// Flag to check GPT partition. It only need be measured once.\r
47//\r
1abfa4ce
JY
48BOOLEAN mTcg2MeasureGptTableFlag = FALSE;\r
49UINTN mTcg2MeasureGptCount = 0;\r
50VOID *mTcg2FileBuffer;\r
51UINTN mTcg2ImageSize;\r
c1d93242
JY
52//\r
53// Measured FV handle cache\r
54//\r
1abfa4ce
JY
55EFI_HANDLE mTcg2CacheMeasuredHandle = NULL;\r
56MEASURED_HOB_DATA *mTcg2MeasuredHobData = NULL;\r
c1d93242
JY
57\r
58/**\r
59 Reads contents of a PE/COFF image in memory buffer.\r
60\r
61 Caution: This function may receive untrusted input.\r
62 PE/COFF image is external input, so this function will make sure the PE/COFF image content\r
63 read is within the image buffer.\r
64\r
65 @param FileHandle Pointer to the file handle to read the PE/COFF image.\r
66 @param FileOffset Offset into the PE/COFF image to begin the read operation.\r
b3548d32 67 @param ReadSize On input, the size in bytes of the requested read operation.\r
c1d93242
JY
68 On output, the number of bytes actually read.\r
69 @param Buffer Output buffer that contains the data read from the PE/COFF image.\r
b3548d32
LG
70\r
71 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size\r
c1d93242
JY
72**/\r
73EFI_STATUS\r
74EFIAPI\r
75DxeTpm2MeasureBootLibImageRead (\r
76 IN VOID *FileHandle,\r
77 IN UINTN FileOffset,\r
78 IN OUT UINTN *ReadSize,\r
79 OUT VOID *Buffer\r
80 )\r
81{\r
82 UINTN EndPosition;\r
83\r
84 if (FileHandle == NULL || ReadSize == NULL || Buffer == NULL) {\r
85 return EFI_INVALID_PARAMETER;\r
86 }\r
87\r
88 if (MAX_ADDRESS - FileOffset < *ReadSize) {\r
89 return EFI_INVALID_PARAMETER;\r
90 }\r
91\r
92 EndPosition = FileOffset + *ReadSize;\r
1abfa4ce
JY
93 if (EndPosition > mTcg2ImageSize) {\r
94 *ReadSize = (UINT32)(mTcg2ImageSize - FileOffset);\r
c1d93242
JY
95 }\r
96\r
1abfa4ce 97 if (FileOffset >= mTcg2ImageSize) {\r
c1d93242
JY
98 *ReadSize = 0;\r
99 }\r
100\r
101 CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize);\r
102\r
103 return EFI_SUCCESS;\r
104}\r
105\r
106/**\r
107 Measure GPT table data into TPM log.\r
108\r
109 Caution: This function may receive untrusted input.\r
110 The GPT partition table is external input, so this function should parse partition data carefully.\r
111\r
1abfa4ce 112 @param Tcg2Protocol Pointer to the located TCG2 protocol instance.\r
c1d93242
JY
113 @param GptHandle Handle that GPT partition was installed.\r
114\r
115 @retval EFI_SUCCESS Successfully measure GPT table.\r
116 @retval EFI_UNSUPPORTED Not support GPT table on the given handle.\r
117 @retval EFI_DEVICE_ERROR Can't get GPT table because device error.\r
118 @retval EFI_OUT_OF_RESOURCES No enough resource to measure GPT table.\r
119 @retval other error value\r
120**/\r
121EFI_STATUS\r
122EFIAPI\r
1abfa4ce
JY
123Tcg2MeasureGptTable (\r
124 IN EFI_TCG2_PROTOCOL *Tcg2Protocol,\r
c1d93242
JY
125 IN EFI_HANDLE GptHandle\r
126 )\r
127{\r
128 EFI_STATUS Status;\r
129 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
130 EFI_DISK_IO_PROTOCOL *DiskIo;\r
131 EFI_PARTITION_TABLE_HEADER *PrimaryHeader;\r
132 EFI_PARTITION_ENTRY *PartitionEntry;\r
133 UINT8 *EntryPtr;\r
134 UINTN NumberOfPartition;\r
135 UINT32 Index;\r
1abfa4ce 136 EFI_TCG2_EVENT *Tcg2Event;\r
c1d93242
JY
137 EFI_GPT_DATA *GptData;\r
138 UINT32 EventSize;\r
139\r
1abfa4ce 140 if (mTcg2MeasureGptCount > 0) {\r
c1d93242
JY
141 return EFI_SUCCESS;\r
142 }\r
143\r
144 Status = gBS->HandleProtocol (GptHandle, &gEfiBlockIoProtocolGuid, (VOID**)&BlockIo);\r
145 if (EFI_ERROR (Status)) {\r
146 return EFI_UNSUPPORTED;\r
147 }\r
148 Status = gBS->HandleProtocol (GptHandle, &gEfiDiskIoProtocolGuid, (VOID**)&DiskIo);\r
149 if (EFI_ERROR (Status)) {\r
150 return EFI_UNSUPPORTED;\r
151 }\r
152 //\r
153 // Read the EFI Partition Table Header\r
b3548d32 154 //\r
c1d93242
JY
155 PrimaryHeader = (EFI_PARTITION_TABLE_HEADER *) AllocatePool (BlockIo->Media->BlockSize);\r
156 if (PrimaryHeader == NULL) {\r
157 return EFI_OUT_OF_RESOURCES;\r
b3548d32 158 }\r
c1d93242
JY
159 Status = DiskIo->ReadDisk (\r
160 DiskIo,\r
161 BlockIo->Media->MediaId,\r
162 1 * BlockIo->Media->BlockSize,\r
163 BlockIo->Media->BlockSize,\r
164 (UINT8 *)PrimaryHeader\r
165 );\r
166 if (EFI_ERROR (Status)) {\r
167 DEBUG ((EFI_D_ERROR, "Failed to Read Partition Table Header!\n"));\r
168 FreePool (PrimaryHeader);\r
169 return EFI_DEVICE_ERROR;\r
b3548d32 170 }\r
c1d93242
JY
171 //\r
172 // Read the partition entry.\r
173 //\r
174 EntryPtr = (UINT8 *)AllocatePool (PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry);\r
175 if (EntryPtr == NULL) {\r
176 FreePool (PrimaryHeader);\r
177 return EFI_OUT_OF_RESOURCES;\r
178 }\r
179 Status = DiskIo->ReadDisk (\r
180 DiskIo,\r
181 BlockIo->Media->MediaId,\r
182 MultU64x32(PrimaryHeader->PartitionEntryLBA, BlockIo->Media->BlockSize),\r
183 PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry,\r
184 EntryPtr\r
185 );\r
186 if (EFI_ERROR (Status)) {\r
187 FreePool (PrimaryHeader);\r
188 FreePool (EntryPtr);\r
189 return EFI_DEVICE_ERROR;\r
190 }\r
b3548d32 191\r
c1d93242
JY
192 //\r
193 // Count the valid partition\r
194 //\r
195 PartitionEntry = (EFI_PARTITION_ENTRY *)EntryPtr;\r
196 NumberOfPartition = 0;\r
197 for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
965268ea 198 if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r
b3548d32 199 NumberOfPartition++;\r
c1d93242
JY
200 }\r
201 PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r
202 }\r
203\r
204 //\r
205 // Prepare Data for Measurement\r
b3548d32
LG
206 //\r
207 EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions)\r
c1d93242 208 + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry);\r
1abfa4ce
JY
209 Tcg2Event = (EFI_TCG2_EVENT *) AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event));\r
210 if (Tcg2Event == NULL) {\r
c1d93242
JY
211 FreePool (PrimaryHeader);\r
212 FreePool (EntryPtr);\r
213 return EFI_OUT_OF_RESOURCES;\r
214 }\r
215\r
1abfa4ce
JY
216 Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event);\r
217 Tcg2Event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);\r
218 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r
219 Tcg2Event->Header.PCRIndex = 5;\r
220 Tcg2Event->Header.EventType = EV_EFI_GPT_EVENT;\r
b3548d32 221 GptData = (EFI_GPT_DATA *) Tcg2Event->Event;\r
c1d93242
JY
222\r
223 //\r
224 // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition\r
b3548d32 225 //\r
c1d93242
JY
226 CopyMem ((UINT8 *)GptData, (UINT8*)PrimaryHeader, sizeof (EFI_PARTITION_TABLE_HEADER));\r
227 GptData->NumberOfPartitions = NumberOfPartition;\r
228 //\r
229 // Copy the valid partition entry\r
230 //\r
231 PartitionEntry = (EFI_PARTITION_ENTRY*)EntryPtr;\r
232 NumberOfPartition = 0;\r
233 for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
965268ea 234 if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r
c1d93242
JY
235 CopyMem (\r
236 (UINT8 *)&GptData->Partitions + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry,\r
237 (UINT8 *)PartitionEntry,\r
238 PrimaryHeader->SizeOfPartitionEntry\r
239 );\r
240 NumberOfPartition++;\r
241 }\r
242 PartitionEntry =(EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r
243 }\r
244\r
245 //\r
246 // Measure the GPT data\r
247 //\r
1abfa4ce
JY
248 Status = Tcg2Protocol->HashLogExtendEvent (\r
249 Tcg2Protocol,\r
c1d93242
JY
250 0,\r
251 (EFI_PHYSICAL_ADDRESS) (UINTN) (VOID *) GptData,\r
252 (UINT64) EventSize,\r
1abfa4ce 253 Tcg2Event\r
c1d93242
JY
254 );\r
255 if (!EFI_ERROR (Status)) {\r
1abfa4ce 256 mTcg2MeasureGptCount++;\r
c1d93242
JY
257 }\r
258\r
259 FreePool (PrimaryHeader);\r
260 FreePool (EntryPtr);\r
1abfa4ce 261 FreePool (Tcg2Event);\r
c1d93242
JY
262\r
263 return Status;\r
264}\r
265\r
266/**\r
267 Measure PE image into TPM log based on the authenticode image hashing in\r
268 PE/COFF Specification 8.0 Appendix A.\r
269\r
270 Caution: This function may receive untrusted input.\r
271 PE/COFF image is external input, so this function will validate its data structure\r
272 within this image buffer before use.\r
273\r
1abfa4ce 274 @param[in] Tcg2Protocol Pointer to the located TCG2 protocol instance.\r
c1d93242
JY
275 @param[in] ImageAddress Start address of image buffer.\r
276 @param[in] ImageSize Image size\r
277 @param[in] LinkTimeBase Address that the image is loaded into memory.\r
278 @param[in] ImageType Image subsystem type.\r
279 @param[in] FilePath File path is corresponding to the input image.\r
280\r
281 @retval EFI_SUCCESS Successfully measure image.\r
282 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.\r
b3548d32 283 @retval EFI_UNSUPPORTED ImageType is unsupported or PE image is mal-format.\r
c1d93242
JY
284 @retval other error value\r
285\r
286**/\r
287EFI_STATUS\r
288EFIAPI\r
1abfa4ce
JY
289Tcg2MeasurePeImage (\r
290 IN EFI_TCG2_PROTOCOL *Tcg2Protocol,\r
c1d93242
JY
291 IN EFI_PHYSICAL_ADDRESS ImageAddress,\r
292 IN UINTN ImageSize,\r
293 IN UINTN LinkTimeBase,\r
294 IN UINT16 ImageType,\r
295 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
296 )\r
297{\r
298 EFI_STATUS Status;\r
1abfa4ce 299 EFI_TCG2_EVENT *Tcg2Event;\r
c1d93242
JY
300 EFI_IMAGE_LOAD_EVENT *ImageLoad;\r
301 UINT32 FilePathSize;\r
302 UINT32 EventSize;\r
303\r
304 Status = EFI_UNSUPPORTED;\r
305 ImageLoad = NULL;\r
306 FilePathSize = (UINT32) GetDevicePathSize (FilePath);\r
307\r
308 //\r
309 // Determine destination PCR by BootPolicy\r
310 //\r
311 EventSize = sizeof (*ImageLoad) - sizeof (ImageLoad->DevicePath) + FilePathSize;\r
1abfa4ce
JY
312 Tcg2Event = AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event));\r
313 if (Tcg2Event == NULL) {\r
c1d93242
JY
314 return EFI_OUT_OF_RESOURCES;\r
315 }\r
316\r
1abfa4ce
JY
317 Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event);\r
318 Tcg2Event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);\r
319 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r
320 ImageLoad = (EFI_IMAGE_LOAD_EVENT *) Tcg2Event->Event;\r
c1d93242
JY
321\r
322 switch (ImageType) {\r
323 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r
1abfa4ce
JY
324 Tcg2Event->Header.EventType = EV_EFI_BOOT_SERVICES_APPLICATION;\r
325 Tcg2Event->Header.PCRIndex = 4;\r
c1d93242
JY
326 break;\r
327 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r
1abfa4ce
JY
328 Tcg2Event->Header.EventType = EV_EFI_BOOT_SERVICES_DRIVER;\r
329 Tcg2Event->Header.PCRIndex = 2;\r
c1d93242
JY
330 break;\r
331 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r
1abfa4ce
JY
332 Tcg2Event->Header.EventType = EV_EFI_RUNTIME_SERVICES_DRIVER;\r
333 Tcg2Event->Header.PCRIndex = 2;\r
c1d93242
JY
334 break;\r
335 default:\r
336 DEBUG ((\r
337 EFI_D_ERROR,\r
1abfa4ce 338 "Tcg2MeasurePeImage: Unknown subsystem type %d",\r
c1d93242
JY
339 ImageType\r
340 ));\r
341 goto Finish;\r
342 }\r
343\r
344 ImageLoad->ImageLocationInMemory = ImageAddress;\r
345 ImageLoad->ImageLengthInMemory = ImageSize;\r
346 ImageLoad->ImageLinkTimeAddress = LinkTimeBase;\r
347 ImageLoad->LengthOfDevicePath = FilePathSize;\r
7a1f792d
ED
348 if ((FilePath != NULL) && (FilePathSize != 0)) {\r
349 CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);\r
350 }\r
c1d93242
JY
351\r
352 //\r
353 // Log the PE data\r
354 //\r
1abfa4ce
JY
355 Status = Tcg2Protocol->HashLogExtendEvent (\r
356 Tcg2Protocol,\r
c1d93242
JY
357 PE_COFF_IMAGE,\r
358 ImageAddress,\r
359 ImageSize,\r
1abfa4ce 360 Tcg2Event\r
c1d93242
JY
361 );\r
362 if (Status == EFI_VOLUME_FULL) {\r
363 //\r
364 // Volume full here means the image is hashed and its result is extended to PCR.\r
d6b926e7 365 // But the event log can't be saved since log area is full.\r
c1d93242
JY
366 // Just return EFI_SUCCESS in order not to block the image load.\r
367 //\r
368 Status = EFI_SUCCESS;\r
369 }\r
370\r
371Finish:\r
1abfa4ce 372 FreePool (Tcg2Event);\r
c1d93242
JY
373\r
374 return Status;\r
375}\r
376\r
377/**\r
b3548d32
LG
378 The security handler is used to abstract platform-specific policy\r
379 from the DXE core response to an attempt to use a file that returns a\r
380 given status for the authentication check from the section extraction protocol.\r
c1d93242 381\r
b3548d32
LG
382 The possible responses in a given SAP implementation may include locking\r
383 flash upon failure to authenticate, attestation logging for all signed drivers,\r
384 and other exception operations. The File parameter allows for possible logging\r
c1d93242
JY
385 within the SAP of the driver.\r
386\r
4b026f0d 387 If File is NULL, then EFI_ACCESS_DENIED is returned.\r
c1d93242 388\r
b3548d32 389 If the file specified by File with an authentication status specified by\r
c1d93242
JY
390 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.\r
391\r
b3548d32
LG
392 If the file specified by File with an authentication status specified by\r
393 AuthenticationStatus is not safe for the DXE Core to use under any circumstances,\r
c1d93242
JY
394 then EFI_ACCESS_DENIED is returned.\r
395\r
b3548d32
LG
396 If the file specified by File with an authentication status specified by\r
397 AuthenticationStatus is not safe for the DXE Core to use right now, but it\r
398 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is\r
c1d93242
JY
399 returned.\r
400\r
401 @param[in] AuthenticationStatus This is the authentication status returned\r
402 from the securitymeasurement services for the\r
403 input file.\r
404 @param[in] File This is a pointer to the device path of the file that is\r
405 being dispatched. This will optionally be used for logging.\r
406 @param[in] FileBuffer File buffer matches the input file device path.\r
407 @param[in] FileSize Size of File buffer matches the input file device path.\r
408 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
409\r
410 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL\r
411 FileBuffer did authenticate, and the platform policy dictates\r
412 that the DXE Foundation may use the file.\r
413 @retval other error value\r
414**/\r
415EFI_STATUS\r
416EFIAPI\r
417DxeTpm2MeasureBootHandler (\r
418 IN UINT32 AuthenticationStatus,\r
419 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,\r
420 IN VOID *FileBuffer,\r
421 IN UINTN FileSize,\r
422 IN BOOLEAN BootPolicy\r
423 )\r
424{\r
1abfa4ce 425 EFI_TCG2_PROTOCOL *Tcg2Protocol;\r
c1d93242 426 EFI_STATUS Status;\r
1abfa4ce 427 EFI_TCG2_BOOT_SERVICE_CAPABILITY ProtocolCapability;\r
c1d93242
JY
428 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
429 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r
430 EFI_HANDLE Handle;\r
431 EFI_HANDLE TempHandle;\r
432 BOOLEAN ApplicationRequired;\r
433 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
434 EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;\r
435 EFI_PHYSICAL_ADDRESS FvAddress;\r
436 UINT32 Index;\r
4b026f0d
PMD
437\r
438 //\r
439 // Check for invalid parameters.\r
440 //\r
441 if (File == NULL) {\r
442 return EFI_ACCESS_DENIED;\r
443 }\r
c1d93242 444\r
1abfa4ce 445 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &Tcg2Protocol);\r
c1d93242
JY
446 if (EFI_ERROR (Status)) {\r
447 //\r
1abfa4ce 448 // Tcg2 protocol is not installed. So, TPM2 is not present.\r
c1d93242
JY
449 // Don't do any measurement, and directly return EFI_SUCCESS.\r
450 //\r
6aaac383 451 DEBUG ((EFI_D_VERBOSE, "DxeTpm2MeasureBootHandler - Tcg2 - %r\n", Status));\r
c1d93242
JY
452 return EFI_SUCCESS;\r
453 }\r
454\r
455 ProtocolCapability.Size = (UINT8) sizeof (ProtocolCapability);\r
1abfa4ce 456 Status = Tcg2Protocol->GetCapability (\r
b3548d32 457 Tcg2Protocol,\r
c1d93242
JY
458 &ProtocolCapability\r
459 );\r
1abfa4ce 460 if (EFI_ERROR (Status) || (!ProtocolCapability.TPMPresentFlag)) {\r
c1d93242
JY
461 //\r
462 // TPM device doesn't work or activate.\r
463 //\r
1abfa4ce 464 DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler (%r) - TPMPresentFlag - %x\n", Status, ProtocolCapability.TPMPresentFlag));\r
c1d93242
JY
465 return EFI_SUCCESS;\r
466 }\r
467\r
468 //\r
469 // Copy File Device Path\r
470 //\r
471 OrigDevicePathNode = DuplicateDevicePath (File);\r
b3548d32 472\r
c1d93242
JY
473 //\r
474 // 1. Check whether this device path support BlockIo protocol.\r
475 // Is so, this device path may be a GPT device path.\r
476 //\r
477 DevicePathNode = OrigDevicePathNode;\r
478 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle);\r
1abfa4ce 479 if (!EFI_ERROR (Status) && !mTcg2MeasureGptTableFlag) {\r
c1d93242
JY
480 //\r
481 // Find the gpt partion on the given devicepath\r
482 //\r
483 DevicePathNode = OrigDevicePathNode;\r
484 ASSERT (DevicePathNode != NULL);\r
485 while (!IsDevicePathEnd (DevicePathNode)) {\r
486 //\r
487 // Find the Gpt partition\r
488 //\r
489 if (DevicePathType (DevicePathNode) == MEDIA_DEVICE_PATH &&\r
490 DevicePathSubType (DevicePathNode) == MEDIA_HARDDRIVE_DP) {\r
491 //\r
492 // Check whether it is a gpt partition or not\r
b3548d32
LG
493 //\r
494 if (((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER &&\r
c1d93242
JY
495 ((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->SignatureType == SIGNATURE_TYPE_GUID) {\r
496\r
497 //\r
498 // Change the partition device path to its parent device path (disk) and get the handle.\r
499 //\r
500 DevicePathNode->Type = END_DEVICE_PATH_TYPE;\r
501 DevicePathNode->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
502 DevicePathNode = OrigDevicePathNode;\r
503 Status = gBS->LocateDevicePath (\r
504 &gEfiDiskIoProtocolGuid,\r
505 &DevicePathNode,\r
506 &Handle\r
507 );\r
508 if (!EFI_ERROR (Status)) {\r
509 //\r
510 // Measure GPT disk.\r
511 //\r
1abfa4ce
JY
512 Status = Tcg2MeasureGptTable (Tcg2Protocol, Handle);\r
513 DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - Tcg2MeasureGptTable - %r\n", Status));\r
c1d93242
JY
514 if (!EFI_ERROR (Status)) {\r
515 //\r
516 // GPT disk check done.\r
517 //\r
1abfa4ce 518 mTcg2MeasureGptTableFlag = TRUE;\r
c1d93242
JY
519 }\r
520 }\r
521 FreePool (OrigDevicePathNode);\r
522 OrigDevicePathNode = DuplicateDevicePath (File);\r
523 ASSERT (OrigDevicePathNode != NULL);\r
524 break;\r
525 }\r
526 }\r
527 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
528 }\r
529 }\r
b3548d32 530\r
c1d93242
JY
531 //\r
532 // 2. Measure PE image.\r
533 //\r
534 ApplicationRequired = FALSE;\r
535\r
536 //\r
537 // Check whether this device path support FVB protocol.\r
538 //\r
539 DevicePathNode = OrigDevicePathNode;\r
540 Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &DevicePathNode, &Handle);\r
541 if (!EFI_ERROR (Status)) {\r
542 //\r
543 // Don't check FV image, and directly return EFI_SUCCESS.\r
544 // It can be extended to the specific FV authentication according to the different requirement.\r
545 //\r
546 if (IsDevicePathEnd (DevicePathNode)) {\r
547 return EFI_SUCCESS;\r
548 }\r
549 //\r
550 // The PE image from unmeasured Firmware volume need be measured\r
d6b926e7 551 // The PE image from measured Firmware volume will be measured according to policy below.\r
c1d93242
JY
552 // If it is driver, do not measure\r
553 // If it is application, still measure.\r
554 //\r
555 ApplicationRequired = TRUE;\r
556\r
1abfa4ce 557 if (mTcg2CacheMeasuredHandle != Handle && mTcg2MeasuredHobData != NULL) {\r
c1d93242
JY
558 //\r
559 // Search for Root FV of this PE image\r
560 //\r
561 TempHandle = Handle;\r
562 do {\r
563 Status = gBS->HandleProtocol(\r
b3548d32 564 TempHandle,\r
c1d93242
JY
565 &gEfiFirmwareVolumeBlockProtocolGuid,\r
566 (VOID**)&FvbProtocol\r
567 );\r
568 TempHandle = FvbProtocol->ParentHandle;\r
569 } while (!EFI_ERROR(Status) && FvbProtocol->ParentHandle != NULL);\r
570\r
571 //\r
572 // Search in measured FV Hob\r
573 //\r
574 Status = FvbProtocol->GetPhysicalAddress(FvbProtocol, &FvAddress);\r
575 if (EFI_ERROR(Status)){\r
576 return Status;\r
577 }\r
578\r
579 ApplicationRequired = FALSE;\r
580\r
1abfa4ce
JY
581 for (Index = 0; Index < mTcg2MeasuredHobData->Num; Index++) {\r
582 if(mTcg2MeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {\r
c1d93242
JY
583 //\r
584 // Cache measured FV for next measurement\r
585 //\r
1abfa4ce 586 mTcg2CacheMeasuredHandle = Handle;\r
c1d93242
JY
587 ApplicationRequired = TRUE;\r
588 break;\r
589 }\r
590 }\r
591 }\r
592 }\r
593\r
594 //\r
595 // File is not found.\r
596 //\r
597 if (FileBuffer == NULL) {\r
598 Status = EFI_SECURITY_VIOLATION;\r
599 goto Finish;\r
600 }\r
601\r
1abfa4ce
JY
602 mTcg2ImageSize = FileSize;\r
603 mTcg2FileBuffer = FileBuffer;\r
c1d93242
JY
604\r
605 //\r
606 // Measure PE Image\r
607 //\r
608 DevicePathNode = OrigDevicePathNode;\r
609 ZeroMem (&ImageContext, sizeof (ImageContext));\r
610 ImageContext.Handle = (VOID *) FileBuffer;\r
611 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) DxeTpm2MeasureBootLibImageRead;\r
612\r
613 //\r
614 // Get information about the image being loaded\r
615 //\r
616 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
617 if (EFI_ERROR (Status)) {\r
618 //\r
619 // The information can't be got from the invalid PeImage\r
620 //\r
621 goto Finish;\r
622 }\r
b3548d32 623\r
c1d93242
JY
624 //\r
625 // Measure only application if Application flag is set\r
626 // Measure drivers and applications if Application flag is not set\r
627 //\r
b3548d32
LG
628 if ((!ApplicationRequired) ||\r
629 (ApplicationRequired && ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)) {\r
c1d93242
JY
630 //\r
631 // Print the image path to be measured.\r
b3548d32 632 //\r
c1d93242
JY
633 DEBUG_CODE_BEGIN ();\r
634 CHAR16 *ToText;\r
635 ToText = ConvertDevicePathToText (\r
636 DevicePathNode,\r
637 FALSE,\r
638 TRUE\r
639 );\r
640 if (ToText != NULL) {\r
641 DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));\r
642 FreePool (ToText);\r
643 }\r
644 DEBUG_CODE_END ();\r
645\r
646 //\r
647 // Measure PE image into TPM log.\r
648 //\r
1abfa4ce
JY
649 Status = Tcg2MeasurePeImage (\r
650 Tcg2Protocol,\r
b3548d32
LG
651 (EFI_PHYSICAL_ADDRESS) (UINTN) FileBuffer,\r
652 FileSize,\r
653 (UINTN) ImageContext.ImageAddress,\r
654 ImageContext.ImageType,\r
c1d93242
JY
655 DevicePathNode\r
656 );\r
1abfa4ce 657 DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - Tcg2MeasurePeImage - %r\n", Status));\r
c1d93242
JY
658 }\r
659\r
660 //\r
661 // Done, free the allocated resource.\r
662 //\r
663Finish:\r
664 if (OrigDevicePathNode != NULL) {\r
665 FreePool (OrigDevicePathNode);\r
666 }\r
667\r
33985e3b 668 DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - %r\n", Status));\r
c1d93242
JY
669\r
670 return Status;\r
671}\r
672\r
673/**\r
674 Register the security handler to provide TPM measure boot service.\r
675\r
676 @param ImageHandle ImageHandle of the loaded driver.\r
677 @param SystemTable Pointer to the EFI System Table.\r
678\r
679 @retval EFI_SUCCESS Register successfully.\r
680 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.\r
681**/\r
682EFI_STATUS\r
683EFIAPI\r
684DxeTpm2MeasureBootLibConstructor (\r
685 IN EFI_HANDLE ImageHandle,\r
686 IN EFI_SYSTEM_TABLE *SystemTable\r
687 )\r
688{\r
689 EFI_HOB_GUID_TYPE *GuidHob;\r
690\r
691 GuidHob = NULL;\r
692\r
693 GuidHob = GetFirstGuidHob (&gMeasuredFvHobGuid);\r
694\r
695 if (GuidHob != NULL) {\r
1abfa4ce 696 mTcg2MeasuredHobData = GET_GUID_HOB_DATA (GuidHob);\r
c1d93242
JY
697 }\r
698\r
699 return RegisterSecurity2Handler (\r
700 DxeTpm2MeasureBootHandler,\r
701 EFI_AUTH_OPERATION_MEASURE_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED\r
702 );\r
703}\r