Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1mKhxo-00051C-St for mharc-grub-devel@gnu.org; Mon, 30 Aug 2021 10:08:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48330) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mKhxn-0004z7-MC for grub-devel@gnu.org; Mon, 30 Aug 2021 10:08:39 -0400 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]:54865) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mKhxh-0003so-Gg for grub-devel@gnu.org; Mon, 30 Aug 2021 10:08:39 -0400 Received: by mail-wm1-x32a.google.com with SMTP id g138so8908110wmg.4 for ; Mon, 30 Aug 2021 07:08:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AoHDXDTwX2Fh4dr8kdDKgVKw2kyiuVuC3fdUPc3m2Es=; b=fisNeYH6lHShqMhc3rJbcH+DSV76nyfsd6ig+eOC3+OnbIjMUjiV89+eCAUZDYLmwN mtTxGjU+AQfRKc4StCUWR5tInD2yrX+ypEMhTyFr2OW1Rkt8zvI17fYUzLqKanrg5j7c vNcOfxOUTvRVnox4vRfhxyjL4KpKH4Ie+r/kezBc+u6SNzZXWY9BpuOFwWTl0ujcfaLA aEeph08wC2v8tCPhMxivYh7j67Nhqe/W4lYeW5hFVE+cFQSY2P8dzdKvLLub4ePaXh4Y N0QENnF3UX7hBwSiAbLJAG1rdvKoZPwqpu09meMegSs0PycL1NzmYN7fyeNnlxvXZyeL 8bPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AoHDXDTwX2Fh4dr8kdDKgVKw2kyiuVuC3fdUPc3m2Es=; b=I72adnc7LV3eBfumGp1Mc4xg9B3HkJ0oq6bREJ3QPEWRs8OUZ/ZyefzdfOJt4dAQ1K vxDyDaA3QfBKDOfoMZt3sdP5jHWA1jgRbhpQ9iDRDOL89OSUMiZZ1tV4VmQCqtZdDbrL cK6Dvw37M9LwMBAK2hbINdMfqYpsHFOLFav/CeAIZQ6/jtdSGJgT3MfVzLp3UGqPEtn/ KIOCMGMILba+SJMpUErJ/6ABLInQL/n5Nna4CxCtJGLk5G7W180pTZk5Q816Nw3aHET7 H0OntY5cK78QhgI0GFyb9B5g3m1giawK2aj+B1RzOS9LS666GZwFYk4vO7eaZFyyHTdK gWFA== X-Gm-Message-State: AOAM5336Zmk3EElyPOEOs687EUhLCMAyvyR65HVZ2lzrK3K5jWqF/QQB DLjA3naiC8mRlEv6TYFwXeJIyD5GshjHBQ== X-Google-Smtp-Source: ABdhPJybSKJZ4/BP94VqO3bdWe50IAWL7br6/t40dgm75embldD9l5q2Enek0uNP61QIQZaOhmUEqA== X-Received: by 2002:a1c:4cd:: with SMTP id 196mr22513569wme.10.1630332512034; Mon, 30 Aug 2021 07:08:32 -0700 (PDT) Received: from R90YT7WC.crto.in (2a01cb000de88800ec89b47bf31bb793.ipv6.abo.wanadoo.fr. [2a01:cb00:de8:8800:ec89:b47b:f31b:b793]) by smtp.gmail.com with ESMTPSA id s7sm15611747wra.75.2021.08.30.07.08.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Aug 2021 07:08:31 -0700 (PDT) From: Erwan Velu X-Google-Original-From: Erwan Velu To: grub-devel@gnu.org Cc: dkiper@net-space.pl, daniel.kiper@oracle.com, alexander.burmashev@oracle.com, phcoder@gmail.com, Erwan Velu Subject: [PATCH] kern/efi: Adding efi-watchdog command Date: Mon, 30 Aug 2021 16:08:07 +0200 Message-Id: <20210830140807.1328406-1-e.velu@criteo.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::32a; envelope-from=erwanaliasr1@gmail.com; helo=mail-wm1-x32a.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 30 Aug 2021 14:08:39 -0000 This patch got written by Arthur Mesh from Juniper (now at Apple Sec team). It was extracted from https://lists.gnu.org/archive/html/grub-devel/2015-09/msg00065.html Since this email, the this patch was : - rebased against the current tree - added a default timeout value This commit adds a new command efi-watchdog to manage efi watchdogs. On server platforms, this allow grub to reset the host automatically if it wasn't able to succeed at booting in a given timeframe. This usually covers the following issues : - net boot is too slow and never ends - grub is unable to find a proper configuration and fails If EFI_WATCHDOG_MINUTES is set a compile time, this enable the watchdog behavior at the early init of GRUB meaning that even if grub is not able to load its configuration file, the watchdog will be triggered automatically. Please note that watchdog only covers GRUB itself. Additional hardware watchdog are required if some want to protect the early operating system loading phase. By default grub disable the watchdog and so this patch. Therefore, this commit have no impact on grub's behavior. This patch is used in production for month on a 50K server platform with success. Signed-off-by: Erwan Velu --- docs/grub.texi | 15 ++++++++++ grub-core/kern/efi/init.c | 63 ++++++++++++++++++++++++++++++++++++++- include/grub/efi/efi.h | 2 ++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/docs/grub.texi b/docs/grub.texi index f8b4b3b21a7f..b52161a19b3b 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -3991,6 +3991,7 @@ you forget a command, you can run the command @command{help} * distrust:: Remove a pubkey from trusted keys * drivemap:: Map a drive to another * echo:: Display a line of text +* efi-watchdog:: Manipulate EFI watchdog * eval:: Evaluate agruments as GRUB commands * export:: Export an environment variable * false:: Do nothing, unsuccessfully @@ -4442,6 +4443,20 @@ When interpreting backslash escapes, backslash followed by any other character will print that character. @end deffn +@node efi-watchdog +@subsection efi-watchdog + +@deffn Command efi-watchdog @option{enable}|@option{disable} code timeout +Enable or disable the system's watchdog timer. +Only available in EFI targeted GRUB. + +If @option{enable} is used, the @var{code} is logged upon +watchdog timeout event. The UEFI BIOS reserves codes 0x0000 to 0xFFFF. +The @var{timeout} represents number of seconds to set the watchdog timeout to. +When the watchdog exceed the timeout, the system is reset. + +if @option{disable} is used, the EFI watchdog is disarmed. +@end deffn @node eval @subsection eval diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c index 7facacf09c7b..c8cda3854ce7 100644 --- a/grub-core/kern/efi/init.c +++ b/grub-core/kern/efi/init.c @@ -28,6 +28,8 @@ #include #include #include +#include +#include #ifdef GRUB_STACK_PROTECTOR @@ -82,6 +84,60 @@ stack_protector_init (void) grub_addr_t grub_modbase; +static grub_command_t cmd_list; + +static grub_err_t +grub_cmd_efi_watchdog (grub_command_t cmd __attribute__ ((unused)), + int argc, char **args) +{ + long input; + grub_efi_status_t status; + grub_efi_uintn_t timeout; + grub_efi_uint64_t code; + + if (argc < 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, + N_("usage: efi-watchdog (enable|disable) ")); + + if (grub_strcasecmp (args[0], "enable") == 0) { + + if (argc != 3) + return grub_error (GRUB_ERR_BAD_ARGUMENT, + N_("usage: efi-watchdog enable ")); + + input = grub_strtol (args[1], 0, 0); + + if (input >= 0) { + code = input; + } else { + return grub_error (GRUB_ERR_BAD_ARGUMENT, + N_(" must be non-negative")); + } + + } else if (grub_strcasecmp (args[0], "disable") == 0) { + + if (argc != 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, + N_("usage: efi-watchdog disable")); + timeout = 0; + code = 0; + + } else { + return grub_error (GRUB_ERR_BAD_ARGUMENT, + N_("usage: efi-watchdog (enable|disable) ")); + } + + status = efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer, + timeout, code, sizeof(L"GRUB"), L"GRUB"); + + if (status != GRUB_EFI_SUCCESS) + return grub_error (GRUB_ERR_BUG, + N_("Unexpected UEFI SetWatchdogTimer() error")); + else + return GRUB_ERR_NONE; +} + + void grub_efi_init (void) { @@ -105,10 +161,14 @@ grub_efi_init (void) grub_shim_lock_verifier_setup (); } + grub_printf("grub %s: Arming EFI watchdog at %d minutes\n", PACKAGE_VERSION, EFI_WATCHDOG_MINUTES); efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer, - 0, 0, 0, NULL); + 60*EFI_WATCHDOG_MINUTES, 0, sizeof(L"GRUB"), L"GRUB"); grub_efidisk_init (); + + cmd_list = grub_register_command ("efi-watchdog", grub_cmd_efi_watchdog, 0, + N_("Enable/Disable system's watchdog timer.")); } void (*grub_efi_net_config) (grub_efi_handle_t hnd, @@ -146,4 +206,5 @@ grub_efi_fini (void) { grub_efidisk_fini (); grub_console_fini (); + grub_unregister_command (cmd_list); } diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index 83d958f9945e..372f995b74f4 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -124,4 +124,6 @@ struct grub_net_card; grub_efi_handle_t grub_efinet_get_device_handle (struct grub_net_card *card); +/* EFI Watchdog armed by grub, in minutes */ +#define EFI_WATCHDOG_MINUTES 0 #endif /* ! GRUB_EFI_EFI_HEADER */ -- 2.25.1