]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCounter.c
Code scrub for Monotonic Counter Driver.
[mirror_edk2.git] / MdeModulePkg / Universal / MonotonicCounterRuntimeDxe / MonotonicCounter.c
index 11e747b7eee879af0589f992322d25afbd6c0b67..9a33cc68ae0bb65597b1ceab8c4eef4a3e8720b4 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
-  \r
-  Produced the Monotonic Counter Services as defined in the DXE CIS.\r
+  Produce the UEFI boot service GetNextMonotonicCount() and runtime service\r
+  GetNextHighMonotonicCount().\r
 \r
 Copyright (c) 2006 - 2008, Intel Corporation\r
 All rights reserved. This program and the accompanying materials\r
@@ -13,50 +13,59 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
+#include <Uefi.h>\r
 \r
-#include "MonotonicCounter.h"\r
+#include <Protocol/MonotonicCounter.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/UefiRuntimeLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
 \r
 //\r
-// The Monotonic Counter Handle\r
+// The handle to install Monotonic Counter Architctural Protocol\r
 //\r
 EFI_HANDLE  mMonotonicCounterHandle = NULL;\r
 \r
 //\r
-// The current Monotonic count value\r
+// The current monotonic counter value\r
 //\r
 UINT64      mEfiMtc;\r
 \r
 //\r
-// Event to use to update the Mtc's high part when wrapping\r
+// Event to update the monotonic Counter's high part when low part overflows.\r
 //\r
 EFI_EVENT   mEfiMtcEvent;\r
 \r
 //\r
-// EfiMtcName - Variable name of the MTC value\r
+// Name of the variable for the high part of monotonic counter\r
 //\r
 CHAR16      *mEfiMtcName = (CHAR16 *) L"MTC";\r
 \r
 //\r
-// EfiMtcGuid - Guid of the MTC value\r
+// Vendor GUID of the variable for the high part of monotonic counter\r
 //\r
 EFI_GUID    mEfiMtcGuid = { 0xeb704011, 0x1402, 0x11d3, { 0x8e, 0x77, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } };\r
 \r
 /**\r
-  Returns the low 32 bits of the platform's monotonic counter.\r
+  Returns a monotonically increasing count for the platform.\r
 \r
-  The platform's monotonic counter is comprised of two 32 bit quantities:  \r
-  the high 32 bits and the low 32 bits.\r
-  During boot service time the low 32 bit value is volatile:  it is reset to\r
-  zero on every system reset and is increased by 1 on every call to this function.\r
-  This function is only available at boot services time.\r
-  Before calling ExitBootServices() the operating system would call this function\r
-  to obtain the current platform monotonic count. \r
+  This function returns a 64-bit value that is numerically larger then the last\r
+  time the function was called.\r
+  The platform\92s monotonic counter is comprised of two parts: the high 32 bits\r
+  and the low 32 bits. The low 32-bit value is volatile and is reset to zero on\r
+  every system reset. It is increased by 1 on every call to GetNextMonotonicCount().\r
+  The high 32-bit value is nonvolatile and is increased by one on whenever the\r
+  system resets or the low 32-bit counter overflows.\r
 \r
   @param  Count                        Pointer to returned value.\r
 \r
-  @retval EFI_INVALID_PARAMETER If Count is NULL.\r
-  @retval EFI_SUCCESS           Operation is successful.\r
-  @retval EFI_UNSUPPORTED       If this function is called at Runtime.\r
+  @retval EFI_SUCCESS           The next monotonic count was returned.\r
+  @retval EFI_DEVICE_ERROR      The device is not functioning properly.\r
+  @retval EFI_INVALID_PARAMETER Count is NULL.\r
+  @retval EFI_UNSUPPORTED       This function is called at runtime.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -68,7 +77,7 @@ MonotonicCounterDriverGetNextMonotonicCount (
   EFI_TPL OldTpl;\r
 \r
   //\r
-  // Can not be called after ExitBootServices()\r
+  // Cannot be called after ExitBootServices()\r
   //\r
   if (EfiAtRuntime ()) {\r
     return EFI_UNSUPPORTED;\r
@@ -88,10 +97,10 @@ MonotonicCounterDriverGetNextMonotonicCount (
   gBS->RestoreTPL (OldTpl);\r
 \r
   //\r
-  // If the MSB bit of the low part toggled, then signal that the high\r
-  // part needs updated now\r
+  // If the low 32-bit counter overflows (MSB bit toggled),\r
+  // then signal that the high part needs update now.\r
   //\r
-  if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & 0x80000000) {\r
+  if ((((UINT32) mEfiMtc) ^ ((UINT32) *Count)) & BIT31) {\r
     gBS->SignalEvent (mEfiMtcEvent);\r
   }\r
 \r
@@ -124,11 +133,11 @@ MonotonicCounterDriverGetNextMonotonicCount (
 \r
   @param  HighCount                Pointer to returned value.\r
 \r
-  @retval EFI_INVALID_PARAMETER If HighCount is NULL.\r
-  @retval EFI_SUCCESS           Operation is successful.\r
+  @retval EFI_SUCCESS           The next high monotonic count was returned.\r
+  @retval EFI_INVALID_PARAMETER HighCount is NULL.\r
+  @retval EFI_DEVICE_ERROR      The variable could not be saved due to a hardware failure.\r
   @retval EFI_OUT_OF_RESOURCES  If variable service reports that not enough storage\r
                                 is available to hold the variable and its data.\r
-  @retval EFI_DEVICE_ERROR      The variable could not be saved due to a hardware failure.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -159,7 +168,7 @@ MonotonicCounterDriverGetNextHighMonotonicCount (
     mEfiMtc     = LShiftU64 (*HighCount, 32);\r
   }\r
   //\r
-  // Update the NvRam store to match the new high part\r
+  // Update the NV variable to match the new high part\r
   //\r
   return EfiSetVariable (\r
            mEfiMtcName,\r
@@ -172,13 +181,11 @@ MonotonicCounterDriverGetNextHighMonotonicCount (
 }\r
 \r
 /**\r
-  Monotonic count event handler.  This handler updates the high monotonic count.\r
+  Monotonic counter event handler.  This handler updates the high part of monotonic counter.\r
 \r
   @param Event           The event to handle.\r
   @param Context         The event context.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -193,12 +200,12 @@ EfiMtcEventHandler (
 }\r
 \r
 /**\r
-  The initial function of monotonic counter driver.\r
+  Entry point of monotonic counter driver.\r
 \r
-  @param  ImageHandle     The handle of image.\r
-  @param  SystemTable     The pointer to system table.\r
+  @param  ImageHandle   The image handle of this driver.\r
+  @param  SystemTable   The pointer of EFI_SYSTEM_TABLE.\r
 \r
-  @return EFI_SUCCESS     The initialize action is successful.\r
+  @return EFI_SUCCESS   The initialization is successful.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -213,12 +220,12 @@ MonotonicCounterDriverInitialize (
   UINTN       BufferSize;\r
 \r
   //\r
-  // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system\r
+  // Make sure the Monotonic Counter Architectural Protocol has not been installed in the system yet.\r
   //\r
   ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiMonotonicCounterArchProtocolGuid);\r
 \r
   //\r
-  // Initialize event to handle overflows\r
+  // Initialize event to handle low-part overflow\r
   //\r
   Status = gBS->CreateEvent (\r
                   EVT_NOTIFY_SIGNAL,\r
@@ -227,7 +234,6 @@ MonotonicCounterDriverInitialize (
                   NULL,\r
                   &mEfiMtcEvent\r
                   );\r
-\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
@@ -254,7 +260,7 @@ MonotonicCounterDriverInitialize (
   // Continue even if it fails.  It will only fail if the variable services are\r
   // not functional.\r
   //\r
-  Status = MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
+  MonotonicCounterDriverGetNextHighMonotonicCount (&HighCount);\r
 \r
   //\r
   // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields\r