/*-1* Copyright (c) 1999, 20002* Intel Corporation.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. All advertising materials mentioning features or use of this software17* must display the following acknowledgement:18*19* This product includes software developed by Intel Corporation and20* its contributors.21*22* 4. Neither the name of Intel Corporation or its contributors may be23* used to endorse or promote products derived from this software24* without specific prior written permission.25*26* THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''27* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29* ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE30* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF36* THE POSSIBILITY OF SUCH DAMAGE.37*38*/3940#include <efi.h>41#include <efilib.h>4243#include <time.h>44#include <sys/time.h>4546/*47* Accurate only for the past couple of centuries;48* that will probably do.49*50* (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)51*/5253#define isleap(y) (((y) % 4) == 0 && \54(((y) % 100) != 0 || ((y) % 400) == 0))55#define SECSPERHOUR (60*60)56#define SECSPERDAY (24 * SECSPERHOUR)5758/*59* These arrays give the cumulative number of days up to the first of the60* month number used as the index (1 -> 12) for regular and leap years.61* The value at index 13 is for the whole year.62*/63static const time_t CumulativeDays[2][14] = {64{0,650,6631,6731 + 28,6831 + 28 + 31,6931 + 28 + 31 + 30,7031 + 28 + 31 + 30 + 31,7131 + 28 + 31 + 30 + 31 + 30,7231 + 28 + 31 + 30 + 31 + 30 + 31,7331 + 28 + 31 + 30 + 31 + 30 + 31 + 31,7431 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,7531 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,7631 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,7731 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },78{0,790,8031,8131 + 29,8231 + 29 + 31,8331 + 29 + 31 + 30,8431 + 29 + 31 + 30 + 31,8531 + 29 + 31 + 30 + 31 + 30,8631 + 29 + 31 + 30 + 31 + 30 + 31,8731 + 29 + 31 + 30 + 31 + 30 + 31 + 31,8831 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,8931 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,9031 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,9131 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};9293void94efi_time_init(void)95{96}9798void99efi_time_fini(void)100{101}102103void104to_efi_time(EFI_TIME *efi_time, time_t time)105{106int lyear, month;107time_t seconds;108109if (time >= 0) {110efi_time->Year = 1970;111lyear = isleap(efi_time->Year);112month = 13;113seconds = CumulativeDays[lyear][month] * SECSPERDAY;114while (time > seconds) {115time -= seconds;116efi_time->Year++;117lyear = isleap(efi_time->Year);118seconds = CumulativeDays[lyear][month] * SECSPERDAY;119}120121efi_time->Month = 0;122while (time >123CumulativeDays[lyear][month] * SECSPERDAY) {124efi_time->Month++;125}126127month = efi_time->Month - 1;128time -= CumulativeDays[lyear][month] * SECSPERDAY;129130for (efi_time->Day = 0; time > SECSPERDAY; efi_time->Day++)131time -= SECSPERDAY;132133for (efi_time->Hour = 0; time > SECSPERHOUR; efi_time->Hour++)134time -= SECSPERHOUR;135136for (efi_time->Minute = 0; time > 60; efi_time->Minute++)137time -= 60;138139efi_time->Second = time;140efi_time->Nanosecond = 0;141efi_time->TimeZone = 0;142efi_time->Daylight = 0;143} else {144memset(efi_time, 0, sizeof(EFI_TIME));145}146}147148time_t149from_efi_time(EFI_TIME *ETime)150{151time_t UTime;152int Year;153154/*155* Do a santity check156*/157if (ETime->Year < 1998 || ETime->Year > 2099 ||158ETime->Month == 0 || ETime->Month > 12 ||159ETime->Day == 0 || ETime->Month > 31 ||160ETime->Hour > 23 || ETime->Minute > 59 ||161ETime->Second > 59 || ETime->TimeZone < -1440 ||162(ETime->TimeZone > 1440 && ETime->TimeZone != 2047)) {163return (0);164}165166/*167* Years168*/169UTime = 0;170for (Year = 1970; Year != ETime->Year; ++Year) {171UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);172}173174/*175* UTime should now be set to 00:00:00 on Jan 1 of the file's year.176*177* Months178*/179UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] *180SECSPERDAY);181182/*183* UTime should now be set to 00:00:00 on the first of the file's184* month and year.185*186* Days -- Don't count the file's day187*/188UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);189190/*191* Hours192*/193UTime += (ETime->Hour * SECSPERHOUR);194195/*196* Minutes197*/198UTime += (ETime->Minute * 60);199200/*201* Seconds202*/203UTime += ETime->Second;204205/*206* EFI time is repored in local time. Adjust for any time zone207* offset to get true UT208*/209if (ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {210/*211* TimeZone is kept in minues...212*/213UTime += (ETime->TimeZone * 60);214}215216return (UTime);217}218219static int220EFI_GetTimeOfDay(OUT struct timeval *tp, OUT struct timezone *tzp)221{222EFI_TIME EfiTime;223EFI_TIME_CAPABILITIES Capabilities;224EFI_STATUS Status;225226/*227* Get time from EFI228*/229230Status = RS->GetTime(&EfiTime, &Capabilities);231if (EFI_ERROR(Status))232return (-1);233234/*235* Convert to UNIX time (ie seconds since the epoch236*/237238tp->tv_sec = from_efi_time(&EfiTime);239tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */240241/*242* Do something with the timezone if needed243*/244245if (tzp != NULL) {246if (EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE)247tzp->tz_minuteswest = 0;248else249tzp->tz_minuteswest = EfiTime.TimeZone;250/*251* This isn't quit right since it doesn't deal with252* EFI_TIME_IN_DAYLIGHT253*/254tzp->tz_dsttime =255EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;256}257258return (0);259}260261time_t262time(time_t *tloc)263{264struct timeval tv;265266memset(&tv, 0, sizeof(tv));267EFI_GetTimeOfDay(&tv, NULL);268269if (tloc)270*tloc = tv.tv_sec;271return (tv.tv_sec);272}273274time_t275getsecs(void)276{277278return (time(NULL));279}280281282