From nobody Sat Nov 2 10:28:04 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; dkim=fail spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1493847505025447.7788243584945; Wed, 3 May 2017 14:38:25 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A1AF421955D8D; Wed, 3 May 2017 14:38:18 -0700 (PDT) Received: from mail-wr0-x22b.google.com (mail-wr0-x22b.google.com [IPv6:2a00:1450:400c:c0c::22b]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id EAD8521A13482 for ; Wed, 3 May 2017 14:38:16 -0700 (PDT) Received: by mail-wr0-x22b.google.com with SMTP id w50so1153851wrc.0 for ; Wed, 03 May 2017 14:38:16 -0700 (PDT) Received: from mohikan.hemma.eciton.net ([2a01:348:6:8735:e23f:49ff:fe6d:9d26]) by smtp.gmail.com with ESMTPSA id z14sm41380wmz.2.2017.05.03.14.38.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 May 2017 14:38:14 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PhnczJxqONsNCfE7D4U/JJ8JkA6VXqK++U0Uc86qAN4=; b=XERZaJMgyD/7Ii9XwO3o8hCBX7Hk+wzWugu5P3veetMf+1UWJvIs9hGu0Ti99YIihf Q8gWpm6hPa/05/3FPDAHUQA9/+HQC8LNh3A+zJ7h8JQ5GDyl7PN85EPUe0XqTLZsGptu RuWCAlNUFOIJUZNp3dIpXiXN12nnExgAeu74Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PhnczJxqONsNCfE7D4U/JJ8JkA6VXqK++U0Uc86qAN4=; b=gBywLx+9mT/yllV4zOR+WgcazSrdloMaIKjjG15e+2ynys2uAYvKq46qmMkyIkXYdX mokZzhLB469M4N4qwCpUlyW+ltLo06eHkDTO2aXqat6bcqYqvCQoBCi8yHb/xZicLjn8 el9fYUvAyMciIu5FsqTniIk+yA3cr1w3ncwMolCelhyZMKzexFtdlFr23O9HDpc2IrMc CWW3mzY26c7zQlXPKVywbseppEIrZSUcQ4YajvL6NIrI/EuUGdDxIssM7h9NX+7lf0Ti oEAscttOoxGX1NyNklXz7UyKL6eom407xLxLCt4NLP6Gq0G3cr5clfmIQeaRXX3dQihT gvPw== X-Gm-Message-State: AN3rC/5dY1znhhyCkhRyDqWzY+/lL1qEb8XQE8xeqiC+Zkhh45xsW3WY rgG9Ieo72VAlErKa X-Received: by 10.223.151.6 with SMTP id r6mr25502589wrb.189.1493847495442; Wed, 03 May 2017 14:38:15 -0700 (PDT) From: Leif Lindholm To: edk2-devel@lists.01.org Date: Wed, 3 May 2017 22:38:11 +0100 Message-Id: <20170503213812.5971-3-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170503213812.5971-1-leif.lindholm@linaro.org> References: <20170503213812.5971-1-leif.lindholm@linaro.org> Subject: [edk2] [PATCH 2/3] EmbeddedPkg: import EfiTimeBaseLib X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Some generic RTC helper functions were broken out from the PL031 library for use with other RTCs in OpenPlatformPkg. Import the code back here, realigning it with the current state of PL031RealTimeClockLib to simplify comparisons. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Leif Lindholm --- EmbeddedPkg/Include/Library/EfiTimeBaseLib.h | 68 ++++++++ .../Library/EfiTimeBaseLib/EfiTimeBaseLib.c | 171 +++++++++++++++++= ++++ .../Library/EfiTimeBaseLib/EfiTimeBaseLib.inf | 34 ++++ 3 files changed, 273 insertions(+) create mode 100644 EmbeddedPkg/Include/Library/EfiTimeBaseLib.h create mode 100644 EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.c create mode 100644 EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf diff --git a/EmbeddedPkg/Include/Library/EfiTimeBaseLib.h b/EmbeddedPkg/Inc= lude/Library/EfiTimeBaseLib.h new file mode 100644 index 0000000000..63db6c6eb8 --- /dev/null +++ b/EmbeddedPkg/Include/Library/EfiTimeBaseLib.h @@ -0,0 +1,68 @@ +/** @file +* +* Copyright (c) 2016, Hisilicon Limited. All rights reserved. +* Copyright (c) 2016, Linaro Limited. All rights reserved. +* +* This program and the accompanying materials +* are licensed and made available under the terms and conditions of the B= SD License +* which accompanies this distribution. The full text of the license may = be found at +* http://opensource.org/licenses/bsd-license.php +* +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +* +**/ + +#ifndef _EFI_TIME_BASE_LIB_H_ +#define _EFI_TIME_BASE_LIB_H_ + +#include + +// Define EPOCH (1970-JANUARY-01) in the Julian Date representation +#define EPOCH_JULIAN_DATE 2440588 + +// Seconds per unit +#define SEC_PER_MIN ((UINTN) 60) +#define SEC_PER_HOUR ((UINTN) 3600) +#define SEC_PER_DAY ((UINTN) 86400) +#define SEC_PER_MONTH ((UINTN) 2,592,00= 0) +#define SEC_PER_YEAR ((UINTN) 31,536,00= 0) + +BOOLEAN +EFIAPI +IsLeapYear ( + IN EFI_TIME *Time + ); + +BOOLEAN +EFIAPI +IsDayValid ( + IN EFI_TIME *Time + ); + +BOOLEAN +EFIAPI +IsTimeValid ( + IN EFI_TIME *Time + ); + +/** + Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to = EFI_TIME + **/ +VOID +EFIAPI +EpochToEfiTime ( + IN UINTN EpochSeconds, + OUT EFI_TIME *Time + ); + +/** + Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00= :00 UTC) + **/ +UINTN +EFIAPI +EfiTimeToEpoch ( + IN EFI_TIME *Time + ); + +#endif diff --git a/EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.c b/Embedded= Pkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.c new file mode 100644 index 0000000000..5a6dee6cdd --- /dev/null +++ b/EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.c @@ -0,0 +1,171 @@ +/** @file +* +* Copyright (c) 2016, Hisilicon Limited. All rights reserved. +* Copyright (c) 2016, Linaro Limited. All rights reserved. +* +* This program and the accompanying materials +* are licensed and made available under the terms and conditions of the B= SD License +* which accompanies this distribution. The full text of the license may = be found at +* http://opensource.org/licenses/bsd-license.php +* +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +* +**/ + +#include +#include +#include +#include + +/** + Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to = EFI_TIME + **/ +VOID +EFIAPI +EpochToEfiTime ( + IN UINTN EpochSeconds, + OUT EFI_TIME *Time + ) +{ + UINTN a; + UINTN b; + UINTN c; + UINTN d; + UINTN g; + UINTN j; + UINTN m; + UINTN y; + UINTN da; + UINTN db; + UINTN dc; + UINTN dg; + UINTN hh; + UINTN mm; + UINTN ss; + UINTN J; + + J =3D (EpochSeconds / 86400) + 2440588; + j =3D J + 32044; + g =3D j / 146097; + dg =3D j % 146097; + c =3D (((dg / 36524) + 1) * 3) / 4; + dc =3D dg - (c * 36524); + b =3D dc / 1461; + db =3D dc % 1461; + a =3D (((db / 365) + 1) * 3) / 4; + da =3D db - (a * 365); + y =3D (g * 400) + (c * 100) + (b * 4) + a; + m =3D (((da * 5) + 308) / 153) - 2; + d =3D da - (((m + 4) * 153) / 5) + 122; + + Time->Year =3D y - 4800 + ((m + 2) / 12); + Time->Month =3D ((m + 2) % 12) + 1; + Time->Day =3D d + 1; + + ss =3D EpochSeconds % 60; + a =3D (EpochSeconds - ss) / 60; + mm =3D a % 60; + b =3D (a - mm) / 60; + hh =3D b % 24; + + Time->Hour =3D hh; + Time->Minute =3D mm; + Time->Second =3D ss; + Time->Nanosecond =3D 0; + +} + +/** + Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00= :00 UTC) + **/ +UINTN +EFIAPI +EfiTimeToEpoch ( + IN EFI_TIME *Time + ) +{ + UINTN a; + UINTN y; + UINTN m; + UINTN JulianDate; // Absolute Julian Date representation of the supplie= d Time + UINTN EpochDays; // Number of days elapsed since EPOCH_JULIAN_DAY + UINTN EpochSeconds; + + a =3D (14 - Time->Month) / 12 ; + y =3D Time->Year + 4800 - a; + m =3D Time->Month + (12*a) - 3; + + JulianDate =3D Time->Day + ((153*m + 2)/5) + (365*y) + (y/4) - (y/100) += (y/400) - 32045; + + ASSERT (JulianDate >=3D EPOCH_JULIAN_DATE); + EpochDays =3D JulianDate - EPOCH_JULIAN_DATE; + + EpochSeconds =3D (EpochDays * SEC_PER_DAY) + ((UINTN)Time->Hour * SEC_PE= R_HOUR) + (Time->Minute * SEC_PER_MIN) + Time->Second; + + return EpochSeconds; +} + +BOOLEAN +EFIAPI +IsLeapYear ( + IN EFI_TIME *Time + ) +{ + if (Time->Year % 4 =3D=3D 0) { + if (Time->Year % 100 =3D=3D 0) { + if (Time->Year % 400 =3D=3D 0) { + return TRUE; + } else { + return FALSE; + } + } else { + return TRUE; + } + } else { + return FALSE; + } +} + +BOOLEAN +EFIAPI +IsDayValid ( + IN EFI_TIME *Time + ) +{ + STATIC CONST INTN DayOfMonth[12] =3D { 31, 29, 31, 30, 31, 30, 31, 31, 3= 0, 31, 30, 31 }; + + if (Time->Day < 1 || + Time->Day > DayOfMonth[Time->Month - 1] || + (Time->Month =3D=3D 2 && (!IsLeapYear (Time) && Time->Day > 28)) + ) { + return FALSE; + } + + return TRUE; +} + +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 =3D=3D EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZo= ne >=3D -1440) && (Time->TimeZone <=3D 1440)))) || + (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)= )) + ) { + return FALSE; + } + + return TRUE; +} diff --git a/EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf b/Embedd= edPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf new file mode 100644 index 0000000000..bd962ae7d2 --- /dev/null +++ b/EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf @@ -0,0 +1,34 @@ +#/** @file +# +# Copyright (c) 2016, Hisilicon Limited. All rights reserved. +# Copyright (c) 2016, Linaro Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the= BSD License +# which accompanies this distribution. The full text of the license may= be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR = IMPLIED. +# +#**/ + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D EfiTimeBaseLib + FILE_GUID =3D B1B07E01-6896-448C-8E75-F0E119ABDF49 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D EfiTimeBaseLib + +[Sources.common] + EfiTimeBaseLib.c + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + +[LibraryClasses] + DebugLib + +[Pcd] --=20 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel