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