10c18794ea4289f03fefc7117b56740414cc0536cgdong/** @file
20c18794ea4289f03fefc7117b56740414cc0536cgdong  The module entry point for Tcg configuration module.
30c18794ea4289f03fefc7117b56740414cc0536cgdong
42bc363714e6bb2192a10b7b921c682eddd7cf8ecDong GuoCopyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
50c18794ea4289f03fefc7117b56740414cc0536cgdongThis program and the accompanying materials
60c18794ea4289f03fefc7117b56740414cc0536cgdongare licensed and made available under the terms and conditions of the BSD License
70c18794ea4289f03fefc7117b56740414cc0536cgdongwhich accompanies this distribution.  The full text of the license may be found at
80c18794ea4289f03fefc7117b56740414cc0536cgdonghttp://opensource.org/licenses/bsd-license.php
90c18794ea4289f03fefc7117b56740414cc0536cgdong
100c18794ea4289f03fefc7117b56740414cc0536cgdongTHE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
110c18794ea4289f03fefc7117b56740414cc0536cgdongWITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
120c18794ea4289f03fefc7117b56740414cc0536cgdong
130c18794ea4289f03fefc7117b56740414cc0536cgdong**/
140c18794ea4289f03fefc7117b56740414cc0536cgdong
150c18794ea4289f03fefc7117b56740414cc0536cgdong#include "TcgConfigImpl.h"
16c1d932429ef9700a2da64452546be14e92468b07jyao#include <Guid/TpmInstance.h>
170c18794ea4289f03fefc7117b56740414cc0536cgdong
180c18794ea4289f03fefc7117b56740414cc0536cgdong/**
190c18794ea4289f03fefc7117b56740414cc0536cgdong  The entry point for Tcg configuration driver.
200c18794ea4289f03fefc7117b56740414cc0536cgdong
210c18794ea4289f03fefc7117b56740414cc0536cgdong  @param[in]  ImageHandle        The image handle of the driver.
220c18794ea4289f03fefc7117b56740414cc0536cgdong  @param[in]  SystemTable        The system table.
230c18794ea4289f03fefc7117b56740414cc0536cgdong
240c18794ea4289f03fefc7117b56740414cc0536cgdong  @retval EFI_ALREADY_STARTED    The driver already exists in system.
250c18794ea4289f03fefc7117b56740414cc0536cgdong  @retval EFI_OUT_OF_RESOURCES   Fail to execute entry point due to lack of resources.
260c18794ea4289f03fefc7117b56740414cc0536cgdong  @retval EFI_SUCCES             All the related protocols are installed on the driver.
270c18794ea4289f03fefc7117b56740414cc0536cgdong  @retval Others                 Fail to install protocols as indicated.
280c18794ea4289f03fefc7117b56740414cc0536cgdong
290c18794ea4289f03fefc7117b56740414cc0536cgdong**/
300c18794ea4289f03fefc7117b56740414cc0536cgdongEFI_STATUS
310c18794ea4289f03fefc7117b56740414cc0536cgdongEFIAPI
320c18794ea4289f03fefc7117b56740414cc0536cgdongTcgConfigDriverEntryPoint (
330c18794ea4289f03fefc7117b56740414cc0536cgdong  IN EFI_HANDLE          ImageHandle,
340c18794ea4289f03fefc7117b56740414cc0536cgdong  IN EFI_SYSTEM_TABLE    *SystemTable
350c18794ea4289f03fefc7117b56740414cc0536cgdong  )
360c18794ea4289f03fefc7117b56740414cc0536cgdong{
370c18794ea4289f03fefc7117b56740414cc0536cgdong  EFI_STATUS                Status;
380c18794ea4289f03fefc7117b56740414cc0536cgdong  TCG_CONFIG_PRIVATE_DATA   *PrivateData;
390c18794ea4289f03fefc7117b56740414cc0536cgdong  EFI_TCG_PROTOCOL          *TcgProtocol;
400c18794ea4289f03fefc7117b56740414cc0536cgdong
41c1d932429ef9700a2da64452546be14e92468b07jyao  if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){
42c1d932429ef9700a2da64452546be14e92468b07jyao    DEBUG ((EFI_D_ERROR, "No TPM12 instance required!\n"));
43c1d932429ef9700a2da64452546be14e92468b07jyao    return EFI_UNSUPPORTED;
44c1d932429ef9700a2da64452546be14e92468b07jyao  }
45c1d932429ef9700a2da64452546be14e92468b07jyao
460c18794ea4289f03fefc7117b56740414cc0536cgdong  Status = TisPcRequestUseTpm ((TIS_TPM_HANDLE) (UINTN) TPM_BASE_ADDRESS);
470c18794ea4289f03fefc7117b56740414cc0536cgdong  if (EFI_ERROR (Status)) {
480c18794ea4289f03fefc7117b56740414cc0536cgdong    DEBUG ((EFI_D_ERROR, "TPM not detected!\n"));
490c18794ea4289f03fefc7117b56740414cc0536cgdong    return Status;
500c18794ea4289f03fefc7117b56740414cc0536cgdong  }
510c18794ea4289f03fefc7117b56740414cc0536cgdong
520c18794ea4289f03fefc7117b56740414cc0536cgdong  Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);
530c18794ea4289f03fefc7117b56740414cc0536cgdong  if (EFI_ERROR (Status)) {
540c18794ea4289f03fefc7117b56740414cc0536cgdong    TcgProtocol = NULL;
550c18794ea4289f03fefc7117b56740414cc0536cgdong  }
560c18794ea4289f03fefc7117b56740414cc0536cgdong
570c18794ea4289f03fefc7117b56740414cc0536cgdong  Status = gBS->OpenProtocol (
580c18794ea4289f03fefc7117b56740414cc0536cgdong                  ImageHandle,
59a0c56a8219ec268d8ac4e051035f1636545cc478lgao                  &gEfiCallerIdGuid,
600c18794ea4289f03fefc7117b56740414cc0536cgdong                  NULL,
610c18794ea4289f03fefc7117b56740414cc0536cgdong                  ImageHandle,
620c18794ea4289f03fefc7117b56740414cc0536cgdong                  ImageHandle,
630c18794ea4289f03fefc7117b56740414cc0536cgdong                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
640c18794ea4289f03fefc7117b56740414cc0536cgdong                  );
650c18794ea4289f03fefc7117b56740414cc0536cgdong  if (!EFI_ERROR (Status)) {
660c18794ea4289f03fefc7117b56740414cc0536cgdong    return EFI_ALREADY_STARTED;
670c18794ea4289f03fefc7117b56740414cc0536cgdong  }
680c18794ea4289f03fefc7117b56740414cc0536cgdong
690c18794ea4289f03fefc7117b56740414cc0536cgdong  //
700c18794ea4289f03fefc7117b56740414cc0536cgdong  // Create a private data structure.
710c18794ea4289f03fefc7117b56740414cc0536cgdong  //
720c18794ea4289f03fefc7117b56740414cc0536cgdong  PrivateData = AllocateCopyPool (sizeof (TCG_CONFIG_PRIVATE_DATA), &mTcgConfigPrivateDateTemplate);
730c18794ea4289f03fefc7117b56740414cc0536cgdong  if (PrivateData == NULL) {
740c18794ea4289f03fefc7117b56740414cc0536cgdong    return EFI_OUT_OF_RESOURCES;
750c18794ea4289f03fefc7117b56740414cc0536cgdong  }
760c18794ea4289f03fefc7117b56740414cc0536cgdong
770c18794ea4289f03fefc7117b56740414cc0536cgdong  PrivateData->TcgProtocol = TcgProtocol;
780c18794ea4289f03fefc7117b56740414cc0536cgdong
790c18794ea4289f03fefc7117b56740414cc0536cgdong  //
800c18794ea4289f03fefc7117b56740414cc0536cgdong  // Install TCG configuration form
810c18794ea4289f03fefc7117b56740414cc0536cgdong  //
820c18794ea4289f03fefc7117b56740414cc0536cgdong  Status = InstallTcgConfigForm (PrivateData);
830c18794ea4289f03fefc7117b56740414cc0536cgdong  if (EFI_ERROR (Status)) {
840c18794ea4289f03fefc7117b56740414cc0536cgdong    goto ErrorExit;
850c18794ea4289f03fefc7117b56740414cc0536cgdong  }
860c18794ea4289f03fefc7117b56740414cc0536cgdong
870c18794ea4289f03fefc7117b56740414cc0536cgdong  //
880c18794ea4289f03fefc7117b56740414cc0536cgdong  // Install private GUID.
890c18794ea4289f03fefc7117b56740414cc0536cgdong  //
900c18794ea4289f03fefc7117b56740414cc0536cgdong  Status = gBS->InstallMultipleProtocolInterfaces (
910c18794ea4289f03fefc7117b56740414cc0536cgdong                  &ImageHandle,
92a0c56a8219ec268d8ac4e051035f1636545cc478lgao                  &gEfiCallerIdGuid,
930c18794ea4289f03fefc7117b56740414cc0536cgdong                  PrivateData,
940c18794ea4289f03fefc7117b56740414cc0536cgdong                  NULL
950c18794ea4289f03fefc7117b56740414cc0536cgdong                  );
960c18794ea4289f03fefc7117b56740414cc0536cgdong
970c18794ea4289f03fefc7117b56740414cc0536cgdong  if (EFI_ERROR (Status)) {
980c18794ea4289f03fefc7117b56740414cc0536cgdong    goto ErrorExit;
990c18794ea4289f03fefc7117b56740414cc0536cgdong  }
1000c18794ea4289f03fefc7117b56740414cc0536cgdong
1010c18794ea4289f03fefc7117b56740414cc0536cgdong  return EFI_SUCCESS;
1020c18794ea4289f03fefc7117b56740414cc0536cgdong
1030c18794ea4289f03fefc7117b56740414cc0536cgdongErrorExit:
1040c18794ea4289f03fefc7117b56740414cc0536cgdong  if (PrivateData != NULL) {
1050c18794ea4289f03fefc7117b56740414cc0536cgdong    UninstallTcgConfigForm (PrivateData);
1060c18794ea4289f03fefc7117b56740414cc0536cgdong  }
1070c18794ea4289f03fefc7117b56740414cc0536cgdong
1080c18794ea4289f03fefc7117b56740414cc0536cgdong  return Status;
1090c18794ea4289f03fefc7117b56740414cc0536cgdong}
1100c18794ea4289f03fefc7117b56740414cc0536cgdong
1110c18794ea4289f03fefc7117b56740414cc0536cgdong/**
1120c18794ea4289f03fefc7117b56740414cc0536cgdong  Unload the Tcg configuration form.
1130c18794ea4289f03fefc7117b56740414cc0536cgdong
1140c18794ea4289f03fefc7117b56740414cc0536cgdong  @param[in]  ImageHandle         The driver's image handle.
1150c18794ea4289f03fefc7117b56740414cc0536cgdong
1160c18794ea4289f03fefc7117b56740414cc0536cgdong  @retval     EFI_SUCCESS         The Tcg configuration form is unloaded.
1170c18794ea4289f03fefc7117b56740414cc0536cgdong  @retval     Others              Failed to unload the form.
1180c18794ea4289f03fefc7117b56740414cc0536cgdong
1190c18794ea4289f03fefc7117b56740414cc0536cgdong**/
1200c18794ea4289f03fefc7117b56740414cc0536cgdongEFI_STATUS
1210c18794ea4289f03fefc7117b56740414cc0536cgdongEFIAPI
1220c18794ea4289f03fefc7117b56740414cc0536cgdongTcgConfigDriverUnload (
1230c18794ea4289f03fefc7117b56740414cc0536cgdong  IN EFI_HANDLE  ImageHandle
1240c18794ea4289f03fefc7117b56740414cc0536cgdong  )
1250c18794ea4289f03fefc7117b56740414cc0536cgdong{
1260c18794ea4289f03fefc7117b56740414cc0536cgdong  EFI_STATUS                  Status;
1270c18794ea4289f03fefc7117b56740414cc0536cgdong  TCG_CONFIG_PRIVATE_DATA   *PrivateData;
1280c18794ea4289f03fefc7117b56740414cc0536cgdong
1290c18794ea4289f03fefc7117b56740414cc0536cgdong  Status = gBS->HandleProtocol (
1300c18794ea4289f03fefc7117b56740414cc0536cgdong                  ImageHandle,
131a0c56a8219ec268d8ac4e051035f1636545cc478lgao                  &gEfiCallerIdGuid,
1320c18794ea4289f03fefc7117b56740414cc0536cgdong                  (VOID **) &PrivateData
1330c18794ea4289f03fefc7117b56740414cc0536cgdong                  );
1340c18794ea4289f03fefc7117b56740414cc0536cgdong  if (EFI_ERROR (Status)) {
1350c18794ea4289f03fefc7117b56740414cc0536cgdong    return Status;
1360c18794ea4289f03fefc7117b56740414cc0536cgdong  }
1370c18794ea4289f03fefc7117b56740414cc0536cgdong
1380c18794ea4289f03fefc7117b56740414cc0536cgdong  ASSERT (PrivateData->Signature == TCG_CONFIG_PRIVATE_DATA_SIGNATURE);
1390c18794ea4289f03fefc7117b56740414cc0536cgdong
1400c18794ea4289f03fefc7117b56740414cc0536cgdong  gBS->UninstallMultipleProtocolInterfaces (
1410c18794ea4289f03fefc7117b56740414cc0536cgdong         &ImageHandle,
142a0c56a8219ec268d8ac4e051035f1636545cc478lgao         &gEfiCallerIdGuid,
1430c18794ea4289f03fefc7117b56740414cc0536cgdong         PrivateData,
1440c18794ea4289f03fefc7117b56740414cc0536cgdong         NULL
1450c18794ea4289f03fefc7117b56740414cc0536cgdong         );
1460c18794ea4289f03fefc7117b56740414cc0536cgdong
1470c18794ea4289f03fefc7117b56740414cc0536cgdong  UninstallTcgConfigForm (PrivateData);
1480c18794ea4289f03fefc7117b56740414cc0536cgdong
1490c18794ea4289f03fefc7117b56740414cc0536cgdong  return EFI_SUCCESS;
1500c18794ea4289f03fefc7117b56740414cc0536cgdong}
151