EmbeddedMonotonicCounter.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /** @file
  2. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <Uefi.h>
  6. #include <Library/BaseLib.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/UefiBootServicesTableLib.h>
  9. #include <Library/UefiRuntimeServicesTableLib.h>
  10. #include <Protocol/MonotonicCounter.h>
  11. UINT64 gCurrentMonotonicCount = 0;
  12. EFI_STATUS
  13. EFIAPI
  14. GetNextMonotonicCount (
  15. OUT UINT64 *Count
  16. )
  17. {
  18. if (Count == NULL) {
  19. return EFI_INVALID_PARAMETER;
  20. }
  21. *Count = gCurrentMonotonicCount++;
  22. return EFI_SUCCESS;
  23. }
  24. EFI_STATUS
  25. EFIAPI
  26. GetNextHighMonotonicCount (
  27. OUT UINT32 *HighCount
  28. )
  29. {
  30. if (HighCount == NULL) {
  31. return EFI_INVALID_PARAMETER;
  32. }
  33. gCurrentMonotonicCount += 0x0000000100000000ULL;
  34. *HighCount = (UINT32)RShiftU64 (gCurrentMonotonicCount, 32) & 0xFFFFFFFF;
  35. return EFI_SUCCESS;
  36. }
  37. EFI_STATUS
  38. EFIAPI
  39. MonotonicCounterDriverInitialize (
  40. IN EFI_HANDLE ImageHandle,
  41. IN EFI_SYSTEM_TABLE *SystemTable
  42. )
  43. {
  44. EFI_STATUS Status;
  45. EFI_HANDLE Handle = NULL;
  46. // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system
  47. ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);
  48. // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields
  49. gBS->GetNextMonotonicCount = GetNextMonotonicCount;
  50. gRT->GetNextHighMonotonicCount = GetNextHighMonotonicCount;
  51. // Install the Monotonic Counter Architectural Protocol onto a new handle
  52. Status = gBS->InstallMultipleProtocolInterfaces (
  53. &Handle,
  54. &gEfiMonotonicCounterArchProtocolGuid,
  55. NULL,
  56. NULL
  57. );
  58. return Status;
  59. }