Home | History | Annotate | Download | only in EmbeddedMonotonicCounter
      1 /** @file
      2 
      3   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
      4 
      5   This program and the accompanying materials
      6   are licensed and made available under the terms and conditions of the BSD License
      7   which accompanies this distribution.  The full text of the license may be found at
      8   http://opensource.org/licenses/bsd-license.php
      9 
     10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     12 
     13 **/
     14 
     15 #include <Uefi.h>
     16 
     17 #include <Library/BaseLib.h>
     18 #include <Library/DebugLib.h>
     19 #include <Library/UefiBootServicesTableLib.h>
     20 #include <Library/UefiRuntimeServicesTableLib.h>
     21 
     22 #include <Protocol/MonotonicCounter.h>
     23 
     24 UINT64 gCurrentMonotonicCount = 0;
     25 
     26 EFI_STATUS
     27 EFIAPI
     28 GetNextMonotonicCount (
     29   OUT UINT64  *Count
     30   )
     31 {
     32   if (Count == NULL) {
     33     return EFI_INVALID_PARAMETER;
     34   }
     35 
     36   *Count = gCurrentMonotonicCount++;
     37   return EFI_SUCCESS;
     38 }
     39 
     40 EFI_STATUS
     41 EFIAPI
     42 GetNextHighMonotonicCount (
     43   OUT UINT32  *HighCount
     44   )
     45 {
     46   if (HighCount == NULL) {
     47     return EFI_INVALID_PARAMETER;
     48   }
     49 
     50   gCurrentMonotonicCount += 0x0000000100000000ULL;
     51 
     52   *HighCount = (UINT32)RShiftU64 (gCurrentMonotonicCount, 32) & 0xFFFFFFFF;
     53 
     54   return EFI_SUCCESS;
     55 }
     56 
     57 
     58 EFI_STATUS
     59 EFIAPI
     60 MonotonicCounterDriverInitialize (
     61   IN EFI_HANDLE        ImageHandle,
     62   IN EFI_SYSTEM_TABLE  *SystemTable
     63   )
     64 {
     65   EFI_STATUS  Status;
     66   EFI_HANDLE  Handle = NULL;
     67 
     68   // Make sure the Monotonic Counter Architectural Protocol is not already installed in the system
     69   ASSERT_PROTOCOL_ALREADY_INSTALLED(NULL, &gEfiMonotonicCounterArchProtocolGuid);
     70 
     71   // Fill in the EFI Boot Services and EFI Runtime Services Monotonic Counter Fields
     72   gBS->GetNextMonotonicCount     = GetNextMonotonicCount;
     73   gRT->GetNextHighMonotonicCount = GetNextHighMonotonicCount;
     74 
     75   // Install the Monotonic Counter Architectural Protocol onto a new handle
     76   Status = gBS->InstallMultipleProtocolInterfaces (
     77                   &Handle,
     78                   &gEfiMonotonicCounterArchProtocolGuid,    NULL,
     79                   NULL
     80                   );
     81   return Status;
     82 }
     83