From owner-svn-src-head@freebsd.org Fri Aug 4 04:20:13 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 57418DC473C; Fri, 4 Aug 2017 04:20:13 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 2FB727CBD9; Fri, 4 Aug 2017 04:20:13 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v744KCjr019039; Fri, 4 Aug 2017 04:20:12 GMT (envelope-from imp@FreeBSD.org) Received: (from imp@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v744KCJL019036; Fri, 4 Aug 2017 04:20:12 GMT (envelope-from imp@FreeBSD.org) Message-Id: <201708040420.v744KCJL019036@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: imp set sender to imp@FreeBSD.org using -f From: Warner Losh Date: Fri, 4 Aug 2017 04:20:12 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r322040 - in head/sys/boot/efi: include libefi X-SVN-Group: head X-SVN-Commit-Author: imp X-SVN-Commit-Paths: in head/sys/boot/efi: include libefi X-SVN-Commit-Revision: 322040 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Aug 2017 04:20:13 -0000 Author: imp Date: Fri Aug 4 04:20:11 2017 New Revision: 322040 URL: https://svnweb.freebsd.org/changeset/base/322040 Log: Add EFI utility functions to libefi This patch adds additional EFI utility functions to convert errno values to EFI_STATUS errors, as well as EFI times to UNIX times. Submitted by: Eric McCorkle Differential Revision: https://reviews.freebsd.org/D11858 Modified: head/sys/boot/efi/include/efilib.h head/sys/boot/efi/libefi/errno.c head/sys/boot/efi/libefi/time.c Modified: head/sys/boot/efi/include/efilib.h ============================================================================== --- head/sys/boot/efi/include/efilib.h Fri Aug 4 04:20:06 2017 (r322039) +++ head/sys/boot/efi/include/efilib.h Fri Aug 4 04:20:11 2017 (r322040) @@ -79,6 +79,7 @@ CHAR16 *efi_devpath_name(EFI_DEVICE_PATH *); void efi_free_devpath_name(CHAR16 *); int efi_status_to_errno(EFI_STATUS); +EFI_STATUS errno_to_efi_status(int errno); void efi_time_init(void); void efi_time_fini(void); Modified: head/sys/boot/efi/libefi/errno.c ============================================================================== --- head/sys/boot/efi/libefi/errno.c Fri Aug 4 04:20:06 2017 (r322039) +++ head/sys/boot/efi/libefi/errno.c Fri Aug 4 04:20:11 2017 (r322040) @@ -30,6 +30,69 @@ __FBSDID("$FreeBSD$"); #include #include +EFI_STATUS +errno_to_efi_status(int errno) +{ + EFI_STATUS status; + + switch (errno) { + case EPERM: + status = EFI_ACCESS_DENIED; + break; + + case EOVERFLOW: + status = EFI_BUFFER_TOO_SMALL; + break; + + case EIO: + status = EFI_DEVICE_ERROR; + break; + + case EINVAL: + status = EFI_INVALID_PARAMETER; + break; + + case ESTALE: + status = EFI_MEDIA_CHANGED; + break; + + case ENXIO: + status = EFI_NO_MEDIA; + break; + + case ENOENT: + status = EFI_NOT_FOUND; + break; + + case ENOMEM: + status = EFI_OUT_OF_RESOURCES; + break; + + case ENOTSUP: + case ENODEV: + status = EFI_UNSUPPORTED; + break; + + case ENOSPC: + status = EFI_VOLUME_FULL; + break; + + case EACCES: + status = EFI_WRITE_PROTECTED; + break; + + case 0: + status = EFI_SUCCESS; + break; + + default: + status = EFI_DEVICE_ERROR; + break; + } + + return (status); +} + int efi_status_to_errno(EFI_STATUS status) { Modified: head/sys/boot/efi/libefi/time.c ============================================================================== --- head/sys/boot/efi/libefi/time.c Fri Aug 4 04:20:06 2017 (r322039) +++ head/sys/boot/efi/libefi/time.c Fri Aug 4 04:20:11 2017 (r322040) @@ -58,6 +58,41 @@ __FBSDID("$FreeBSD$"); #define SECSPERHOUR ( 60*60 ) #define SECSPERDAY (24 * SECSPERHOUR) +/* +// These arrays give the cumulative number of days up to the first of the +// month number used as the index (1 -> 12) for regular and leap years. +// The value at index 13 is for the whole year. +*/ +static const time_t CumulativeDays[2][14] = { + {0, + 0, + 31, + 31 + 28, + 31 + 28 + 31, + 31 + 28 + 31 + 30, + 31 + 28 + 31 + 30 + 31, + 31 + 28 + 31 + 30 + 31 + 30, + 31 + 28 + 31 + 30 + 31 + 30 + 31, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }, + {0, + 0, + 31, + 31 + 29, + 31 + 29 + 31, + 31 + 29 + 31 + 30, + 31 + 29 + 31 + 30 + 31, + 31 + 29 + 31 + 30 + 31 + 30, + 31 + 29 + 31 + 30 + 31 + 30 + 31, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }}; + void efi_time_init(void) { @@ -68,45 +103,46 @@ efi_time_fini(void) { } -static time_t -efi_time(EFI_TIME *ETime) +void +to_efi_time(EFI_TIME *efi_time, time_t time) { - /* - // These arrays give the cumulative number of days up to the first of the - // month number used as the index (1 -> 12) for regular and leap years. - // The value at index 13 is for the whole year. - */ - static time_t CumulativeDays[2][14] = { - {0, - 0, - 31, - 31 + 28, - 31 + 28 + 31, - 31 + 28 + 31 + 30, - 31 + 28 + 31 + 30 + 31, - 31 + 28 + 31 + 30 + 31 + 30, - 31 + 28 + 31 + 30 + 31 + 30 + 31, - 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, - 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, - 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, - 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, - 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }, - {0, - 0, - 31, - 31 + 29, - 31 + 29 + 31, - 31 + 29 + 31 + 30, - 31 + 29 + 31 + 30 + 31, - 31 + 29 + 31 + 30 + 31 + 30, - 31 + 29 + 31 + 30 + 31 + 30 + 31, - 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31, - 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30, - 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, - 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, - 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 }}; + if (time >= 0) { + for (efi_time->Year = 1970; + time > CumulativeDays[isleap(efi_time->Year)][13] * SECSPERDAY; + time -= CumulativeDays[isleap(efi_time->Year)][13] * SECSPERDAY, + efi_time->Year++); - time_t UTime; + for (efi_time->Month = 0; + time > CumulativeDays[isleap(efi_time->Year)][efi_time->Month] * + SECSPERDAY; + efi_time->Month++); + + time -= CumulativeDays[isleap(efi_time->Year)][efi_time->Month - 1] * + SECSPERDAY; + + for (efi_time->Day = 0; time > SECSPERDAY; + time -= SECSPERDAY, efi_time->Day++); + + for (efi_time->Hour = 0; time > SECSPERHOUR; + time -= SECSPERHOUR, efi_time->Hour++); + + for (efi_time->Minute = 0; time > 60; + time -= 60, efi_time->Minute++); + + efi_time->Second = time; + efi_time->Nanosecond = 0; + efi_time->TimeZone = 0; + efi_time->Daylight = 0; + } else { + memset(efi_time, 0, sizeof(EFI_TIME)); + } +} + +time_t +from_efi_time(EFI_TIME *ETime) +{ + + time_t UTime; int Year; /* @@ -196,7 +232,7 @@ EFI_GetTimeOfDay( // Convert to UNIX time (ie seconds since the epoch */ - tp->tv_sec = efi_time( &EfiTime ); + tp->tv_sec = from_efi_time( &EfiTime ); tp->tv_usec = 0; /* EfiTime.Nanosecond * 1000; */ /*