From nobody Fri Apr 28 01:29:58 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q6w5M1CnNz47khW; Fri, 28 Apr 2023 01:29:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q6w5L6FX0z3rrS; Fri, 28 Apr 2023 01:29:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682645398; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Bxjg1HVB7a8WGV31tM6tF3w2fFv9Qq+mlLvt2O9uGjU=; b=Ac7lXbnBluWnZQimCAztYdheQfANOMufJDklWNfPsQNyE9JqAVmHTX5cnkQCmA8xu3q9Sa 3WXBxrKWVWO/k7lS+VPB/QJ4Mfs1oJu95hp0cloYel9GDgcX6qk5WMWzu7llsd4b3KI0Eq egj8cY5jcVYui3YlyBSBsI7Aiyo/yFQBkDAedAdLK55X3LLSuVlVkjoE94z3X9ZeMu0aL8 4z1bXaqZsEAGaAi4yCMEN9q5aDkbM+yT22WUNKnwBJgnaZ56HAW/GqrC1dc3zZ+B9VUJse +xW8eC5oCwLbRrmiusLJRmyD0p2VMaWAVrVsv7bfEljab6RbYtvWGEtS6z2L1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682645398; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Bxjg1HVB7a8WGV31tM6tF3w2fFv9Qq+mlLvt2O9uGjU=; b=vkEela9kB+C4e7128w/C3AmmvQ/yESh9uJWzQhVYKK91XqDs7I8ubUhPXqnwLrXuvQamUV frUirld65PwbqwyDdUgLGG1QX6GTEKnroolNnsSs4LKIThS0L0PbVSI0TlVfo7s0lmqv6A fOHFkf38anW7qmORSkhOkxfncIT3K06GzPyF9O5dW+BULr4sS5Z9myjd7cei5dtmhhH9oh OII5V4vB+WPm37c87SddTZGH8nS/c6A1iTpDDm3BQkjRZS3PcXgcFdMrtcYdcbIOGT9Uir y1yYzuDGSNjqeBmE3vUaZs8gbzIfko0zBfgD9rmyeo1NsNhxTD4/UfKa6PUv5Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1682645398; a=rsa-sha256; cv=none; b=q5AoCzYSn7q5YUaTIWFjxn90wVXdCvh/W4/qKpC2dj8D+wl0YY9cBzzoyivC7BWHfDd04G 5QSglMMdN99Jp92sufBYHDPhvJ7sp+deAuTjRL4pSYr5ZH9gh8oFVXNdgvOG/rSGIEGPZK QgpSU6pNtKS14Cf6Zfbc+BA4r5r694jPKPg3X4W28EifmGikovbtVcMwg7Lc4XSxciLcHh r2ywtk5AWJq33igUYVJiJFkxxtWOlgAhBCCx1XFnHUIyeQUdSurK5Vpa0cqwN7T1aOGoPO gqIYLKv0rxDFdJzmH6PSwnvQW0SRbyG0gajX+YxqrKk3A1nPCv96Nr0UOWiN/Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Q6w5L56DjzgVh; Fri, 28 Apr 2023 01:29:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 33S1TwwX075233; Fri, 28 Apr 2023 01:29:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33S1TwGg075232; Fri, 28 Apr 2023 01:29:58 GMT (envelope-from git) Date: Fri, 28 Apr 2023 01:29:58 GMT Message-Id: <202304280129.33S1TwGg075232@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 1808b5577da8 - main - Add efiwake tool List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1808b5577da8d3fcffc70973afef250f428f9381 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=1808b5577da8d3fcffc70973afef250f428f9381 commit 1808b5577da8d3fcffc70973afef250f428f9381 Author: Johannes Totz AuthorDate: 2023-04-26 16:16:55 +0000 Commit: Konstantin Belousov CommitDate: 2023-04-28 01:28:51 +0000 Add efiwake tool Reviewed by: kib MFC after: 1 week Differential revision: https://reviews.freebsd.org/D36714 --- usr.sbin/Makefile | 2 +- usr.sbin/efiwake/Makefile | 13 +++++ usr.sbin/efiwake/efiwake.c | 134 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/usr.sbin/Makefile b/usr.sbin/Makefile index 9fa1dc05a1cc..fb707e515eaa 100644 --- a/usr.sbin/Makefile +++ b/usr.sbin/Makefile @@ -129,7 +129,7 @@ SUBDIR.${MK_OPENSSL}+= certctl .endif SUBDIR.${MK_CXGBETOOL}+= cxgbetool SUBDIR.${MK_DIALOG}+= bsdconfig -SUBDIR.${MK_EFI}+= efivar efidp efibootmgr efitable +SUBDIR.${MK_EFI}+= efivar efidp efibootmgr efitable efiwake .if ${MK_OPENSSL} != "no" SUBDIR.${MK_EFI}+= uefisign .endif diff --git a/usr.sbin/efiwake/Makefile b/usr.sbin/efiwake/Makefile new file mode 100644 index 000000000000..ed2292f8a3ac --- /dev/null +++ b/usr.sbin/efiwake/Makefile @@ -0,0 +1,13 @@ +# $FreeBSD$ + +PACKAGE= efi-tools + +PROG= efiwake +MAN= + +SRCS= efiwake.c + +EFIBOOT=${SRCTOP}/stand/efi +CFLAGS+=-I${EFIBOOT}/include + +.include diff --git a/usr.sbin/efiwake/efiwake.c b/usr.sbin/efiwake/efiwake.c new file mode 100644 index 000000000000..67497e2ede03 --- /dev/null +++ b/usr.sbin/efiwake/efiwake.c @@ -0,0 +1,134 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2023 Johannes Totz + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void usage(void) +{ + printf("Usage:\n" + " efiwake -- print out current EFI time and wake time\n" + " efiwake -d -- disable wake time\n" + " efiwake -e yyyy-mm-ddThh:mm:ss -- enable wake time\n" + ); + + exit(EX_USAGE); +} + +int main(int argc, char **argv) +{ + struct efi_waketime_ioc waketime; + int error, ch; + bool enable = false, disable = false; + + memset(&waketime, 0, sizeof(waketime)); + + while ((ch = getopt(argc, argv, "de:")) != -1) { + switch (ch) { + case 'd': + disable = true; + break; + case 'e': + if (sscanf(optarg, + "%hu-%02hhu-%02hhuT%02hhu:%02hhu:%02hhu", + &waketime.waketime.tm_year, &waketime.waketime.tm_mon, + &waketime.waketime.tm_mday, &waketime.waketime.tm_hour, + &waketime.waketime.tm_min, &waketime.waketime.tm_sec) + != 6) { + usage(); + } + enable = true; + break; + case '?': + default: + usage(); + } + } + argc -= optind; + argv += optind; + + if (argc > 0) + usage(); + if (disable && enable) + usage(); + + int efi_fd = open("/dev/efi", O_RDWR); + if (efi_fd < 0) + err(EX_OSERR, "cannot open /dev/efi"); + + struct efi_tm now; + error = ioctl(efi_fd, EFIIOC_GET_TIME, &now); + if (error != 0) + err(EX_OSERR, "cannot get EFI time"); + + /* EFI's time can be different from kernel's time. */ + printf("Current EFI time: %u-%02u-%02uT%02u:%02u:%02u\n", + now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, + now.tm_sec); + + if (disable) { + /* It's tempting to preserve the current timer value. + * However, wonky EFI implementations sometimes return bogus + * dates for the wake timer and would then fail disabling it + * here. + * A safe timer value is the current EFI time. + */ + waketime.waketime = now; + waketime.enabled = 0; + error = ioctl(efi_fd, EFIIOC_SET_WAKETIME, &waketime); + if (error != 0) + err(EX_OSERR, "cannot disable EFI wake time"); + } + if (enable) { + waketime.enabled = 1; + error = ioctl(efi_fd, EFIIOC_SET_WAKETIME, &waketime); + if (error != 0) + err(EX_OSERR, "cannot enable EFI wake time"); + } + + /* Confirm to user what the wake time has been set to. */ + error = ioctl(efi_fd, EFIIOC_GET_WAKETIME, &waketime); + if (error != 0) + err(EX_OSERR, "cannot get EFI wake time"); + + printf("EFI wake time: %u-%02u-%02uT%02u:%02u:%02u; enabled=%i, pending=%i\n", + waketime.waketime.tm_year, waketime.waketime.tm_mon, + waketime.waketime.tm_mday, waketime.waketime.tm_hour, + waketime.waketime.tm_min, waketime.waketime.tm_sec, + waketime.enabled, waketime.pending); + + close(efi_fd); + return (0); +}