[edk2-devel] [PATCH 1/2] EmbeddedPkg/TimeBaseLib: Add function to check Timezone and Daylight

Nhi Pham via groups.io nhi=os.amperecomputing.com at groups.io
Wed Jan 6 10:55:57 UTC 2021


This adds two functions IsValidTimeZone() and IsValidDaylight() to check
the time zone and daylight value from EFI time. These functions are
retrieved from the RealTimeClockRuntimeDxe module as they reduce
duplicated code in RTC modules.

Cc: Leif Lindholm <leif at nuviainc.com>
Cc: Ard Biesheuvel <ard.biesheuvel at arm.com>
Signed-off-by: Nhi Pham <nhi at os.amperecomputing.com>
---
 EmbeddedPkg/Include/Library/TimeBaseLib.h     | 13 ++++++
 EmbeddedPkg/Library/TimeBaseLib/TimeBaseLib.c | 47 ++++++++++++++------
 2 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/EmbeddedPkg/Include/Library/TimeBaseLib.h b/EmbeddedPkg/Include/Library/TimeBaseLib.h
index 90853c3f4b93..8bebf5886db8 100644
--- a/EmbeddedPkg/Include/Library/TimeBaseLib.h
+++ b/EmbeddedPkg/Include/Library/TimeBaseLib.h
@@ -2,6 +2,7 @@
 *
 *  Copyright (c) 2016, Hisilicon Limited. All rights reserved.
 *  Copyright (c) 2016-2019, Linaro Limited. All rights reserved.
+*  Copyright (c) 2020, Ampere Computing LLC. All rights reserved.
 *
 *  SPDX-License-Identifier: BSD-2-Clause-Patent
 *
@@ -64,6 +65,18 @@ IsDayValid (
   IN  EFI_TIME  *Time
   );
 
+BOOLEAN
+EFIAPI
+IsValidTimeZone (
+  IN  INT16  TimeZone
+  );
+
+BOOLEAN
+EFIAPI
+IsValidDaylight (
+  IN  INT8  Daylight
+  );
+
 BOOLEAN
 EFIAPI
 IsTimeValid (
diff --git a/EmbeddedPkg/Library/TimeBaseLib/TimeBaseLib.c b/EmbeddedPkg/Library/TimeBaseLib/TimeBaseLib.c
index 78fc7b6cd2e5..02d9901338b9 100644
--- a/EmbeddedPkg/Library/TimeBaseLib/TimeBaseLib.c
+++ b/EmbeddedPkg/Library/TimeBaseLib/TimeBaseLib.c
@@ -2,6 +2,7 @@
 *
 *  Copyright (c) 2016, Hisilicon Limited. All rights reserved.
 *  Copyright (c) 2016-2019, Linaro Limited. All rights reserved.
+*  Copyright (c) 2020, Ampere Computing LLC. All rights reserved.
 *
 *  SPDX-License-Identifier: BSD-2-Clause-Patent
 *
@@ -173,23 +174,43 @@ IsDayValid (
 
 BOOLEAN
 EFIAPI
-IsTimeValid(
+IsValidTimeZone (
+  IN  INT16  TimeZone
+  )
+{
+  return TimeZone == EFI_UNSPECIFIED_TIMEZONE ||
+         (TimeZone >= -1440 && TimeZone <= 1440);
+}
+
+BOOLEAN
+EFIAPI
+IsValidDaylight (
+  IN  INT8  Daylight
+  )
+{
+  return Daylight == 0 ||
+         Daylight == EFI_TIME_ADJUST_DAYLIGHT ||
+         Daylight == (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
+}
+
+BOOLEAN
+EFIAPI
+IsTimeValid (
   IN EFI_TIME *Time
   )
 {
   // Check the input parameters are within the range specified by UEFI
-  if ((Time->Year   < 2000) ||
-     (Time->Year   > 2099) ||
-     (Time->Month  < 1   ) ||
-     (Time->Month  > 12  ) ||
-     (!IsDayValid (Time)    ) ||
-     (Time->Hour   > 23  ) ||
-     (Time->Minute > 59  ) ||
-     (Time->Second > 59  ) ||
-     (Time->Nanosecond > 999999999) ||
-     (!((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= -1440) && (Time->TimeZone <= 1440)))) ||
-     (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))
-  ) {
+  if ((Time->Year  < 2000)              ||
+     (Time->Year   > 2099)              ||
+     (Time->Month  < 1   )              ||
+     (Time->Month  > 12  )              ||
+     (!IsDayValid (Time) )              ||
+     (Time->Hour   > 23  )              ||
+     (Time->Minute > 59  )              ||
+     (Time->Second > 59  )              ||
+     (Time->Nanosecond > 999999999)     ||
+     (!IsValidTimeZone(Time->TimeZone)) ||
+     (!IsValidDaylight(Time->Daylight))) {
     return FALSE;
   }
 
-- 
2.17.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#69811): https://edk2.groups.io/g/devel/message/69811
Mute This Topic: https://groups.io/mt/79472334/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-





More information about the edk2-devel-archive mailing list