--- /dev/null
+/** @file\r
+\r
+ Copyright (c) 2008-2009, Apple Inc. All rights reserved.\r
+ \r
+ All rights reserved. This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Uefi.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+\r
+#include <Protocol/MonotonicCounter.h>\r
+\r
+UINT64 gCurrentMonotonicCount = 0;\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GetNextMonotonicCount (\r
+ OUT UINT64 *Count\r
+ )\r
+{\r
+ if (Count == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ \r
+ *Count = gCurrentMonotonicCount++;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GetNextHighMonotonicCount (\r
+ OUT UINT32 *HighCount\r
+ )\r
+{\r
+ if (HighCount == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ \r
+ gCurrentMonotonicCount += 0x0000000100000000ULL;\r
+ \r
+ *HighCount = RShiftU64 (gCurrentMonotonicCount, 32) & 0xFFFFFFFF;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+MonotonicCounterDriverInitialize (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_HANDLE Handle = NULL;\r
+\r
+ // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system\r
+ ASSERT_PROTOCOL_ALREADY_INSTALLED(NULL, &gEfiMonotonicCounterArchProtocolGuid);\r
+\r
+ // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r
+ gBS->GetNextMonotonicCount = GetNextMonotonicCount;\r
+ gRT->GetNextHighMonotonicCount = GetNextHighMonotonicCount;\r
+\r
+ // Install the Monotonic Counter Architctural Protocol onto a new handle\r
+ Status = gBS->InstallMultipleProtocolInterfaces (\r
+ &Handle,\r
+ &gEfiMonotonicCounterArchProtocolGuid, NULL,\r
+ NULL\r
+ );\r
+ return Status;\r
+}\r