PlatformSecureLib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /** @file
  2. Provides a secure platform-specific method to detect physically present user.
  3. Copyright (c) 2013 - 2016 Intel Corporation.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Library/PlatformHelperLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/UefiBootServicesTableLib.h>
  10. #include <Library/I2cLib.h>
  11. #include <PlatformBoards.h>
  12. #include <Pcal9555.h>
  13. #include <QNCAccess.h>
  14. //
  15. // Global variable to cache pointer to I2C protocol.
  16. //
  17. EFI_PLATFORM_TYPE mPlatformType = TypeUnknown;
  18. BOOLEAN
  19. CheckResetButtonState (
  20. VOID
  21. )
  22. {
  23. EFI_STATUS Status;
  24. EFI_I2C_DEVICE_ADDRESS I2CSlaveAddress;
  25. UINTN Length;
  26. UINTN ReadLength;
  27. UINT8 Buffer[2];
  28. DEBUG ((EFI_D_INFO, "CheckResetButtonState(): mPlatformType == %d\n", mPlatformType));
  29. if (mPlatformType == GalileoGen2) {
  30. //
  31. // Read state of Reset Button - EXP2.P1_7
  32. // This GPIO is pulled high when the button is not pressed
  33. // This GPIO reads low when button is pressed
  34. //
  35. return PlatformPcal9555GpioGetState (
  36. GALILEO_GEN2_IOEXP2_7BIT_SLAVE_ADDR, // IO Expander 2.
  37. 15 // P1-7.
  38. );
  39. }
  40. if (mPlatformType == Galileo) {
  41. //
  42. // Detect the I2C Slave Address of the GPIO Expander
  43. //
  44. if (PlatformLegacyGpioGetLevel (R_QNC_GPIO_RGLVL_RESUME_WELL, GALILEO_DETERMINE_IOEXP_SLA_RESUMEWELL_GPIO)) {
  45. I2CSlaveAddress.I2CDeviceAddress = GALILEO_IOEXP_J2HI_7BIT_SLAVE_ADDR;
  46. } else {
  47. I2CSlaveAddress.I2CDeviceAddress = GALILEO_IOEXP_J2LO_7BIT_SLAVE_ADDR;
  48. }
  49. DEBUG ((EFI_D_INFO, "Galileo GPIO Expender Slave Address = %02x\n", I2CSlaveAddress.I2CDeviceAddress));
  50. //
  51. // Read state of RESET_N_SHLD (GPORT5_BIT0)
  52. //
  53. Buffer[1] = 5;
  54. Length = 1;
  55. ReadLength = 1;
  56. Status = I2cReadMultipleByte (
  57. I2CSlaveAddress,
  58. EfiI2CSevenBitAddrMode,
  59. &Length,
  60. &ReadLength,
  61. &Buffer[1]
  62. );
  63. ASSERT_EFI_ERROR (Status);
  64. //
  65. // Return the state of GPORT5_BIT0
  66. //
  67. return ((Buffer[1] & BIT0) != 0);
  68. }
  69. return TRUE;
  70. }
  71. /**
  72. This function provides a platform-specific method to detect whether the platform
  73. is operating by a physically present user.
  74. Programmatic changing of platform security policy (such as disable Secure Boot,
  75. or switch between Standard/Custom Secure Boot mode) MUST NOT be possible during
  76. Boot Services or after exiting EFI Boot Services. Only a physically present user
  77. is allowed to perform these operations.
  78. NOTE THAT: This function cannot depend on any EFI Variable Service since they are
  79. not available when this function is called in AuthenticateVariable driver.
  80. @retval TRUE The platform is operated by a physically present user.
  81. @retval FALSE The platform is NOT operated by a physically present user.
  82. **/
  83. BOOLEAN
  84. EFIAPI
  85. UserPhysicalPresent (
  86. VOID
  87. )
  88. {
  89. EFI_STATUS Status;
  90. //
  91. // If user has already been detected as present, then return TRUE
  92. //
  93. if (PcdGetBool (PcdUserIsPhysicallyPresent)) {
  94. return TRUE;
  95. }
  96. //
  97. // Check to see if user is present now
  98. //
  99. if (CheckResetButtonState ()) {
  100. //
  101. // User is still not present, then return FALSE
  102. //
  103. return FALSE;
  104. }
  105. //
  106. // User has gone from not present to present state, so set
  107. // PcdUserIsPhysicallyPresent to TRUE
  108. //
  109. Status = PcdSetBoolS (PcdUserIsPhysicallyPresent, TRUE);
  110. ASSERT_EFI_ERROR (Status);
  111. return TRUE;
  112. }
  113. /**
  114. Determines if a user is physically present by reading the reset button state.
  115. @param ImageHandle The image handle of this driver.
  116. @param SystemTable A pointer to the EFI System Table.
  117. @retval EFI_SUCCESS Install the Secure Boot Helper Protocol successfully.
  118. **/
  119. EFI_STATUS
  120. EFIAPI
  121. PlatformSecureLibInitialize (
  122. IN EFI_HANDLE ImageHandle,
  123. IN EFI_SYSTEM_TABLE *SystemTable
  124. )
  125. {
  126. EFI_STATUS Status;
  127. //
  128. // Get the platform type
  129. //
  130. mPlatformType = (EFI_PLATFORM_TYPE)PcdGet16 (PcdPlatformType);
  131. //
  132. // Read the state of the reset button when the library is initialized
  133. //
  134. Status = PcdSetBoolS (PcdUserIsPhysicallyPresent, !CheckResetButtonState ());
  135. ASSERT_EFI_ERROR (Status);
  136. return EFI_SUCCESS;
  137. }