TcgMor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /** @file
  2. TCG MOR (Memory Overwrite Request) Control Driver.
  3. This driver initialize MemoryOverwriteRequestControl variable. It
  4. will clear MOR_CLEAR_MEMORY_BIT bit if it is set. It will also do TPer Reset for
  5. those encrypted drives through EFI_STORAGE_SECURITY_COMMAND_PROTOCOL at EndOfDxe.
  6. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include "TcgMor.h"
  10. UINT8 mMorControl;
  11. /**
  12. Ready to Boot Event notification handler.
  13. @param[in] Event Event whose notification function is being invoked
  14. @param[in] Context Pointer to the notification function's context
  15. **/
  16. VOID
  17. EFIAPI
  18. OnReadyToBoot (
  19. IN EFI_EVENT Event,
  20. IN VOID *Context
  21. )
  22. {
  23. EFI_STATUS Status;
  24. UINTN DataSize;
  25. if (MOR_CLEAR_MEMORY_VALUE (mMorControl) == 0x0) {
  26. //
  27. // MorControl is expected, directly return to avoid unnecessary variable operation
  28. //
  29. return ;
  30. }
  31. //
  32. // Clear MOR_CLEAR_MEMORY_BIT
  33. //
  34. DEBUG ((EFI_D_INFO, "TcgMor: Clear MorClearMemory bit\n"));
  35. mMorControl &= 0xFE;
  36. DataSize = sizeof (mMorControl);
  37. Status = gRT->SetVariable (
  38. MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
  39. &gEfiMemoryOverwriteControlDataGuid,
  40. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  41. DataSize,
  42. &mMorControl
  43. );
  44. if (EFI_ERROR (Status)) {
  45. DEBUG ((EFI_D_ERROR, "TcgMor: Clear MOR_CLEAR_MEMORY_BIT failure, Status = %r\n"));
  46. }
  47. }
  48. /**
  49. Send TPer Reset command to reset eDrive to lock all protected bands.
  50. Typically, there are 2 mechanism for resetting eDrive. They are:
  51. 1. TPer Reset through IEEE 1667 protocol.
  52. 2. TPer Reset through native TCG protocol.
  53. This routine will detect what protocol the attached eDrive comform to, TCG or
  54. IEEE 1667 protocol. Then send out TPer Reset command separately.
  55. @param[in] Ssp The pointer to EFI_STORAGE_SECURITY_COMMAND_PROTOCOL instance.
  56. @param[in] MediaId ID of the medium to receive data from or send data to.
  57. **/
  58. VOID
  59. InitiateTPerReset (
  60. IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *Ssp,
  61. IN UINT32 MediaId
  62. )
  63. {
  64. EFI_STATUS Status;
  65. UINT8 *Buffer;
  66. UINTN XferSize;
  67. UINTN Len;
  68. UINTN Index;
  69. BOOLEAN TcgFlag;
  70. BOOLEAN IeeeFlag;
  71. SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA *Data;
  72. Buffer = NULL;
  73. TcgFlag = FALSE;
  74. IeeeFlag = FALSE;
  75. //
  76. // ATA8-ACS 7.57.6.1 indicates the Transfer Length field requirements a multiple of 512.
  77. // If the length of the TRUSTED RECEIVE parameter data is greater than the Transfer Length,
  78. // then the device shall return the TRUSTED RECEIVE parameter data truncated to the requested Transfer Length.
  79. //
  80. Len = ROUNDUP512(sizeof(SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA));
  81. Buffer = AllocateZeroPool(Len);
  82. if (Buffer == NULL) {
  83. return;
  84. }
  85. //
  86. // When the Security Protocol field is set to 00h, and SP Specific is set to 0000h in a TRUSTED RECEIVE
  87. // command, the device basic information data shall be returned.
  88. //
  89. Status = Ssp->ReceiveData (
  90. Ssp,
  91. MediaId,
  92. 100000000, // Timeout 10-sec
  93. 0, // SecurityProtocol
  94. 0, // SecurityProtocolSpecifcData
  95. Len, // PayloadBufferSize,
  96. Buffer, // PayloadBuffer
  97. &XferSize
  98. );
  99. if (EFI_ERROR (Status)) {
  100. goto Exit;
  101. }
  102. //
  103. // In returned data, the ListLength field indicates the total length, in bytes,
  104. // of the supported security protocol list.
  105. //
  106. Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)Buffer;
  107. Len = ROUNDUP512(sizeof (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA) +
  108. (Data->SupportedSecurityListLength[0] << 8) +
  109. (Data->SupportedSecurityListLength[1])
  110. );
  111. //
  112. // Free original buffer and allocate new buffer.
  113. //
  114. FreePool(Buffer);
  115. Buffer = AllocateZeroPool(Len);
  116. if (Buffer == NULL) {
  117. return;
  118. }
  119. //
  120. // Read full supported security protocol list from device.
  121. //
  122. Status = Ssp->ReceiveData (
  123. Ssp,
  124. MediaId,
  125. 100000000, // Timeout 10-sec
  126. 0, // SecurityProtocol
  127. 0, // SecurityProtocolSpecifcData
  128. Len, // PayloadBufferSize,
  129. Buffer, // PayloadBuffer
  130. &XferSize
  131. );
  132. if (EFI_ERROR (Status)) {
  133. goto Exit;
  134. }
  135. Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)Buffer;
  136. Len = (Data->SupportedSecurityListLength[0] << 8) + Data->SupportedSecurityListLength[1];
  137. //
  138. // Iterate full supported security protocol list to check if TCG or IEEE 1667 protocol
  139. // is supported.
  140. //
  141. for (Index = 0; Index < Len; Index++) {
  142. if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_TCG) {
  143. //
  144. // Found a TCG device.
  145. //
  146. TcgFlag = TRUE;
  147. DEBUG ((EFI_D_INFO, "This device is a TCG protocol device\n"));
  148. break;
  149. }
  150. if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_IEEE1667) {
  151. //
  152. // Found a IEEE 1667 device.
  153. //
  154. IeeeFlag = TRUE;
  155. DEBUG ((EFI_D_INFO, "This device is a IEEE 1667 protocol device\n"));
  156. break;
  157. }
  158. }
  159. if (!TcgFlag && !IeeeFlag) {
  160. DEBUG ((EFI_D_INFO, "Neither a TCG nor IEEE 1667 protocol device is found\n"));
  161. goto Exit;
  162. }
  163. if (TcgFlag) {
  164. //
  165. // As long as TCG protocol is supported, send out a TPer Reset
  166. // TCG command to the device via the TrustedSend command with a non-zero Transfer Length.
  167. //
  168. Status = Ssp->SendData (
  169. Ssp,
  170. MediaId,
  171. 100000000, // Timeout 10-sec
  172. SECURITY_PROTOCOL_TCG, // SecurityProtocol
  173. 0x0400, // SecurityProtocolSpecifcData
  174. 512, // PayloadBufferSize,
  175. Buffer // PayloadBuffer
  176. );
  177. if (!EFI_ERROR (Status)) {
  178. DEBUG ((EFI_D_INFO, "Send TPer Reset Command Successfully !\n"));
  179. } else {
  180. DEBUG ((EFI_D_INFO, "Send TPer Reset Command Fail !\n"));
  181. }
  182. }
  183. if (IeeeFlag) {
  184. //
  185. // TBD : Perform a TPer Reset via IEEE 1667 Protocol
  186. //
  187. DEBUG ((EFI_D_INFO, "IEEE 1667 Protocol didn't support yet!\n"));
  188. }
  189. Exit:
  190. if (Buffer != NULL) {
  191. FreePool(Buffer);
  192. }
  193. }
  194. /**
  195. Notification function of END_OF_DXE.
  196. @param[in] Event Event whose notification function is being invoked.
  197. @param[in] Context Pointer to the notification function's context.
  198. **/
  199. VOID
  200. EFIAPI
  201. TPerResetAtEndOfDxe (
  202. IN EFI_EVENT Event,
  203. IN VOID *Context
  204. )
  205. {
  206. EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *Ssp;
  207. EFI_BLOCK_IO_PROTOCOL *BlockIo;
  208. EFI_STATUS Status;
  209. UINTN HandleCount;
  210. EFI_HANDLE *HandleBuffer;
  211. UINTN Index;
  212. //
  213. // Locate all SSP protocol instances.
  214. //
  215. HandleCount = 0;
  216. HandleBuffer = NULL;
  217. Status = gBS->LocateHandleBuffer (
  218. ByProtocol,
  219. &gEfiStorageSecurityCommandProtocolGuid,
  220. NULL,
  221. &HandleCount,
  222. &HandleBuffer
  223. );
  224. if (EFI_ERROR (Status) || (HandleCount == 0) || (HandleBuffer == NULL)) {
  225. return;
  226. }
  227. for (Index = 0; Index < HandleCount; Index ++) {
  228. //
  229. // Get the SSP interface.
  230. //
  231. Status = gBS->HandleProtocol(
  232. HandleBuffer[Index],
  233. &gEfiStorageSecurityCommandProtocolGuid,
  234. (VOID **) &Ssp
  235. );
  236. if (EFI_ERROR (Status)) {
  237. continue;
  238. }
  239. Status = gBS->HandleProtocol(
  240. HandleBuffer[Index],
  241. &gEfiBlockIoProtocolGuid,
  242. (VOID **) &BlockIo
  243. );
  244. if (EFI_ERROR (Status)) {
  245. continue;
  246. }
  247. InitiateTPerReset (Ssp, BlockIo->Media->MediaId);
  248. }
  249. FreePool (HandleBuffer);
  250. }
  251. /**
  252. Entry Point for TCG MOR Control driver.
  253. @param[in] ImageHandle Image handle of this driver.
  254. @param[in] SystemTable A Pointer to the EFI System Table.
  255. @retval EFI_SUCCESS
  256. @return Others Some error occurs.
  257. **/
  258. EFI_STATUS
  259. EFIAPI
  260. MorDriverEntryPoint (
  261. IN EFI_HANDLE ImageHandle,
  262. IN EFI_SYSTEM_TABLE *SystemTable
  263. )
  264. {
  265. EFI_STATUS Status;
  266. UINTN DataSize;
  267. EFI_EVENT Event;
  268. ///
  269. /// The firmware is required to create the MemoryOverwriteRequestControl UEFI variable.
  270. ///
  271. DataSize = sizeof (mMorControl);
  272. Status = gRT->GetVariable (
  273. MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
  274. &gEfiMemoryOverwriteControlDataGuid,
  275. NULL,
  276. &DataSize,
  277. &mMorControl
  278. );
  279. if (EFI_ERROR (Status)) {
  280. //
  281. // Set default value to 0
  282. //
  283. mMorControl = 0;
  284. Status = gRT->SetVariable (
  285. MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,
  286. &gEfiMemoryOverwriteControlDataGuid,
  287. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
  288. DataSize,
  289. &mMorControl
  290. );
  291. DEBUG ((EFI_D_INFO, "TcgMor: Create MOR variable! Status = %r\n", Status));
  292. } else {
  293. //
  294. // Create a Ready To Boot Event and Clear the MorControl bit in the call back function.
  295. //
  296. DEBUG ((DEBUG_INFO, "TcgMor: Create ReadyToBoot Event for MorControl Bit cleaning!\n"));
  297. Status = EfiCreateEventReadyToBootEx (
  298. TPL_CALLBACK,
  299. OnReadyToBoot,
  300. NULL,
  301. &Event
  302. );
  303. if (EFI_ERROR (Status)) {
  304. return Status;
  305. }
  306. //
  307. // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.
  308. //
  309. DEBUG ((EFI_D_INFO, "TcgMor: Create EndofDxe Event for Mor TPer Reset!\n"));
  310. Status = gBS->CreateEventEx (
  311. EVT_NOTIFY_SIGNAL,
  312. TPL_CALLBACK,
  313. TPerResetAtEndOfDxe,
  314. NULL,
  315. &gEfiEndOfDxeEventGroupGuid,
  316. &Event
  317. );
  318. if (EFI_ERROR (Status)) {
  319. return Status;
  320. }
  321. }
  322. return Status;
  323. }