199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1999, 2000
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Intel Corporation.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3. All advertising materials mentioning features or use of this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * must display the following acknowledgement:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This product includes software developed by Intel Corporation and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * its contributors.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of Intel Corporation or its contributors may be
199767f8919635c4928607450d9e0abb932109ceToomas Soome * used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
199767f8919635c4928607450d9e0abb932109ceToomas Soome * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efi.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efilib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <time.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/time.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome// Accurate only for the past couple of centuries;
199767f8919635c4928607450d9e0abb932109ceToomas Soome// that will probably do.
199767f8919635c4928607450d9e0abb932109ceToomas Soome//
199767f8919635c4928607450d9e0abb932109ceToomas Soome// (#defines From FreeBSD 3.2 lib/libc/stdtime/tzfile.h)
199767f8919635c4928607450d9e0abb932109ceToomas Soome*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SECSPERHOUR ( 60*60 )
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SECSPERDAY (24 * SECSPERHOUR)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic time_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefi_time(EFI_TIME *ETime)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // These arrays give the cumulative number of days up to the first of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome // month number used as the index (1 -> 12) for regular and leap years.
199767f8919635c4928607450d9e0abb932109ceToomas Soome // The value at index 13 is for the whole year.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome static time_t CumulativeDays[2][14] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome {0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30 + 31 + 30,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30 + 31 + 30 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 },
199767f8919635c4928607450d9e0abb932109ceToomas Soome {0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30 + 31 + 30,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30 + 31 + 30 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome time_t UTime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int Year;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Do a santity check
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ( ETime->Year < 1998 || ETime->Year > 2099 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ETime->Month == 0 || ETime->Month > 12 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ETime->Day == 0 || ETime->Month > 31 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ETime->Hour > 23 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ETime->Minute > 59 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ETime->Second > 59 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ETime->TimeZone < -1440 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome (ETime->TimeZone > 1440 && ETime->TimeZone != 2047) ) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Years
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome UTime = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (Year = 1970; Year != ETime->Year; ++Year) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome UTime += (CumulativeDays[isleap(Year)][13] * SECSPERDAY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // UTime should now be set to 00:00:00 on Jan 1 of the file's year.
199767f8919635c4928607450d9e0abb932109ceToomas Soome //
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Months
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome UTime += (CumulativeDays[isleap(ETime->Year)][ETime->Month] * SECSPERDAY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // UTime should now be set to 00:00:00 on the first of the file's month and year
199767f8919635c4928607450d9e0abb932109ceToomas Soome //
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Days -- Don't count the file's day
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome UTime += (((ETime->Day > 0) ? ETime->Day-1:0) * SECSPERDAY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Hours
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome UTime += (ETime->Hour * SECSPERHOUR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Minutes
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome UTime += (ETime->Minute * 60);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Seconds
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome UTime += ETime->Second;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // EFI time is repored in local time. Adjust for any time zone offset to
199767f8919635c4928607450d9e0abb932109ceToomas Soome // get true UT
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ( ETime->TimeZone != EFI_UNSPECIFIED_TIMEZONE ) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // TimeZone is kept in minues...
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome UTime += (ETime->TimeZone * 60);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return UTime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomeEFI_GetTimeOfDay(
199767f8919635c4928607450d9e0abb932109ceToomas Soome OUT struct timeval *tp,
199767f8919635c4928607450d9e0abb932109ceToomas Soome OUT struct timezone *tzp
199767f8919635c4928607450d9e0abb932109ceToomas Soome )
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_TIME EfiTime;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_TIME_CAPABILITIES Capabilities;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS Status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Get time from EFI
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome Status = RS->GetTime(&EfiTime, &Capabilities);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(Status))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Convert to UNIX time (ie seconds since the epoch
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome tp->tv_sec = efi_time( &EfiTime );
199767f8919635c4928607450d9e0abb932109ceToomas Soome tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Do something with the timezone if needed
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tzp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tzp->tz_minuteswest =
199767f8919635c4928607450d9e0abb932109ceToomas Soome EfiTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE ? 0 : EfiTime.TimeZone;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome // This isn't quit right since it doesn't deal with EFI_TIME_IN_DAYLIGHT
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tzp->tz_dsttime =
199767f8919635c4928607450d9e0abb932109ceToomas Soome EfiTime.Daylight & EFI_TIME_ADJUST_DAYLIGHT ? 1 : 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soometime_t
199767f8919635c4928607450d9e0abb932109ceToomas Soometime(time_t *tloc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct timeval tv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_GetTimeOfDay(&tv, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tloc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *tloc = tv.tv_sec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return tv.tv_sec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soometime_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomegetsecs()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return time(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}