svn commit: r301306 - in head/sys/boot/efi: include libefi loader loader/arch/amd64 loader/arch/arm loader/arch/arm64 loader/arch/i386

Andrew Turner andrew at FreeBSD.org
Sat Jun 4 08:47:47 UTC 2016


Author: andrew
Date: Sat Jun  4 08:47:45 2016
New Revision: 301306
URL: https://svnweb.freebsd.org/changeset/base/301306

Log:
  Use the UEFI event timer to update the time on arm and arm64. The current
  code uses the GetTime function from the Runtime Service, however this has
  been shown to not return a useable time on many arm64 UEFI implementations.
  
  Reviewed by:	jhb, smh
  Sponsored by:	ABT Systems Ltd
  Differential Revision:	https://reviews.freebsd.org/D6709

Added:
  head/sys/boot/efi/libefi/time_event.c   (contents, props changed)
Modified:
  head/sys/boot/efi/include/efilib.h
  head/sys/boot/efi/libefi/Makefile
  head/sys/boot/efi/libefi/time.c
  head/sys/boot/efi/loader/arch/amd64/elf64_freebsd.c
  head/sys/boot/efi/loader/arch/arm/exec.c
  head/sys/boot/efi/loader/arch/arm64/exec.c
  head/sys/boot/efi/loader/arch/i386/elf32_freebsd.c
  head/sys/boot/efi/loader/main.c

Modified: head/sys/boot/efi/include/efilib.h
==============================================================================
--- head/sys/boot/efi/include/efilib.h	Sat Jun  4 07:29:10 2016	(r301305)
+++ head/sys/boot/efi/include/efilib.h	Sat Jun  4 08:47:45 2016	(r301306)
@@ -55,6 +55,9 @@ void efi_free_devpath_name(CHAR16 *);
 
 int efi_status_to_errno(EFI_STATUS);
 
+void efi_time_init(void);
+void efi_time_fini(void);
+
 EFI_STATUS main(int argc, CHAR16 *argv[]);
 void exit(EFI_STATUS status);
 void delay(int usecs);

Modified: head/sys/boot/efi/libefi/Makefile
==============================================================================
--- head/sys/boot/efi/libefi/Makefile	Sat Jun  4 07:29:10 2016	(r301305)
+++ head/sys/boot/efi/libefi/Makefile	Sat Jun  4 08:47:45 2016	(r301306)
@@ -5,7 +5,13 @@ INTERNALLIB=
 WARNS?=	2
 
 SRCS=	delay.c devpath.c efi_console.c efinet.c efipart.c env.c errno.c \
-	handles.c libefi.c time.c
+	handles.c libefi.c
+
+.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
+SRCS+=	time.c
+.elif ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "arm"
+SRCS+=	time_event.c
+.endif
 
 # We implement a slightly non-standard %S in that it always takes a
 # CHAR16 that's common in UEFI-land instead of a wchar_t. This only

Modified: head/sys/boot/efi/libefi/time.c
==============================================================================
--- head/sys/boot/efi/libefi/time.c	Sat Jun  4 07:29:10 2016	(r301305)
+++ head/sys/boot/efi/libefi/time.c	Sat Jun  4 08:47:45 2016	(r301306)
@@ -58,6 +58,16 @@ __FBSDID("$FreeBSD$");
 #define SECSPERHOUR ( 60*60 )
 #define SECSPERDAY	(24 * SECSPERHOUR)
 
+void
+efi_time_init(void)
+{
+}
+
+void
+efi_time_fini(void)
+{
+}
+
 static time_t
 efi_time(EFI_TIME *ETime)
 {

Added: head/sys/boot/efi/libefi/time_event.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/boot/efi/libefi/time_event.c	Sat Jun  4 08:47:45 2016	(r301306)
@@ -0,0 +1,82 @@
+/*-
+ * Copyright (c) 2016 Andrew Turner
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <efi.h>
+#include <efilib.h>
+
+#include <time.h>
+#include <sys/time.h>
+
+static EFI_EVENT time_event;
+static uint64_t curtime;
+
+static void
+time_update(EFI_EVENT event, void *context)
+{
+
+	curtime += 10;
+}
+
+void
+efi_time_init(void)
+{
+
+	/* Create a timer event */
+	BS->CreateEvent(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
+	    time_update, 0, &time_event);
+	/* Use a 10ms timer */
+	BS->SetTimer(time_event, TimerPeriodic, 100000);
+}
+
+void
+efi_time_fini(void)
+{
+
+	/* Cancel the timer */
+	BS->SetTimer(time_event, TimerCancel, 0);
+	BS->CloseEvent(time_event);
+}
+
+time_t
+time(time_t *tloc)
+{
+	time_t t;
+
+	t = curtime / 1000;
+	if (tloc != NULL)
+		*tloc = t;
+
+	return (t);
+}
+
+time_t
+getsecs()
+{
+    return time(0);
+}

Modified: head/sys/boot/efi/loader/arch/amd64/elf64_freebsd.c
==============================================================================
--- head/sys/boot/efi/loader/arch/amd64/elf64_freebsd.c	Sat Jun  4 07:29:10 2016	(r301305)
+++ head/sys/boot/efi/loader/arch/amd64/elf64_freebsd.c	Sat Jun  4 08:47:45 2016	(r301306)
@@ -186,9 +186,12 @@ elf64_exec(struct preloaded_file *fp)
 
 	printf("Start @ 0x%lx ...\n", ehdr->e_entry);
 
+	efi_time_fini();
 	err = bi_load(fp->f_args, &modulep, &kernend);
-	if (err != 0)
+	if (err != 0) {
+		efi_time_init();
 		return(err);
+	}
 
 	dev_cleanup();
 

Modified: head/sys/boot/efi/loader/arch/arm/exec.c
==============================================================================
--- head/sys/boot/efi/loader/arch/arm/exec.c	Sat Jun  4 07:29:10 2016	(r301305)
+++ head/sys/boot/efi/loader/arch/arm/exec.c	Sat Jun  4 08:47:45 2016	(r301306)
@@ -73,8 +73,11 @@ __elfN(arm_exec)(struct preloaded_file *
 
 	e = (Elf_Ehdr *)&fmp->md_data;
 
-	if ((error = bi_load(fp->f_args, &modulep, &kernend)) != 0)
+	efi_time_fini();
+	if ((error = bi_load(fp->f_args, &modulep, &kernend)) != 0) {
+		efi_time_init();
 		return (error);
+	}
 
 	entry = efi_translate(e->e_entry);
 	printf("Kernel entry at 0x%x...\n", (unsigned)entry);

Modified: head/sys/boot/efi/loader/arch/arm64/exec.c
==============================================================================
--- head/sys/boot/efi/loader/arch/arm64/exec.c	Sat Jun  4 07:29:10 2016	(r301305)
+++ head/sys/boot/efi/loader/arch/arm64/exec.c	Sat Jun  4 08:47:45 2016	(r301306)
@@ -113,9 +113,12 @@ elf64_exec(struct preloaded_file *fp)
 	ehdr = (Elf_Ehdr *)&(md->md_data);
 	entry = efi_translate(ehdr->e_entry);
 
+	efi_time_fini();
 	err = bi_load(fp->f_args, &modulep, &kernendp);
-	if (err != 0)
+	if (err != 0) {
+		efi_time_init();
 		return (err);
+	}
 
 	dev_cleanup();
 

Modified: head/sys/boot/efi/loader/arch/i386/elf32_freebsd.c
==============================================================================
--- head/sys/boot/efi/loader/arch/i386/elf32_freebsd.c	Sat Jun  4 07:29:10 2016	(r301305)
+++ head/sys/boot/efi/loader/arch/i386/elf32_freebsd.c	Sat Jun  4 08:47:45 2016	(r301306)
@@ -71,9 +71,12 @@ elf32_exec(struct preloaded_file *fp)
 	return(EFTYPE);
     ehdr = (Elf_Ehdr *)&(md->md_data);
 
+    efi_time_fini();
     err = bi_load(fp->f_args, &boothowto, &bootdev, &bootinfop, &modulep, &kernend);
-    if (err != 0)
+    if (err != 0) {
+	efi_time_init();
 	return(err);
+    }
     entry = ehdr->e_entry & 0xffffff;
 
     printf("Start @ 0x%lx ...\n", entry);

Modified: head/sys/boot/efi/loader/main.c
==============================================================================
--- head/sys/boot/efi/loader/main.c	Sat Jun  4 07:29:10 2016	(r301305)
+++ head/sys/boot/efi/loader/main.c	Sat Jun  4 08:47:45 2016	(r301306)
@@ -246,6 +246,9 @@ main(int argc, CHAR16 *argv[])
 	archsw.arch_zfs_probe = efi_zfs_probe;
 #endif
 
+	/* Init the time source */
+	efi_time_init();
+
 	has_kbd = has_keyboard();
 
 	/*


More information about the svn-src-head mailing list