kern/efi: Adding efi-watchdog command
PATCH v2 1/1 kern/efi: Adding efi-watchdog command
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
- reworked to be controlled via mkimage
- reworked to simplify the command line
This commit adds a new command efi-watchdog to manage efi watchdogs.
On server platforms, this allows 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
- the GRUB is unable to find a proper configuration and fails
Using --efi-watchdog-timeout option at mknetdir time allow users controlling the default value of the timer.
This set 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.
As per the UEFI specification :
The watchdog timer is only used during boot services. On successful completion of EFI_BOOT_SERVICES.ExitBootServices() the watchdog timer is disabled.
This implies this watchdog doesn't cover the the OS loading. If you want to cover this phase of the boot,
an additional hardware watchdog is required (usually set in the BIOS).
On the command line, a single argument is enough to control the watchdog. This argument defines the timeout of the watchdog (in seconds).
- If 0 > argument < 0xFFFF, the watchdog is armed with the associate timeout.
- If argument is set to 0, the watchdog is disarmed.
By default GRUB disable the watchdog and so this patch. Therefore, this commit have no impact on the default GRUB's behavior.
This patch is used in production for month on a 50K server platform with success.
--- docs/grub.texi | 13 ++++++ grub-core/kern/efi/init.c | 85 ++++++++++++++++++++++++++++++++++++- include/grub/efi/efi.h | 2 + include/grub/kernel.h | 3 +- include/grub/util/install.h | 8 +++- util/grub-install-common.c | 12 +++++- util/grub-mkimage.c | 11 ++++- util/mkimage.c | 23 +++++++++- 8 files changed, 147 insertions(+), 10 deletions(-)
docs/grub.texi +13 -0
@@ -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,18 @@ 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 @var{timeout} +Control the EFI system's watchdog timer. +Only available in EFI targeted GRUB. + +When the watchdog exceed @var{timeout}, the system is reset. + +If @var{timeout} is set to 0, the watchdog is disarmed. + +@end deffn @node eval @subsection eval
grub-core/kern/efi/init.c +83 -2
109 lines not on this page · raw message
include/grub/efi/efi.h +2 -0
@@ -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 */
include/grub/kernel.h +2 -1
@@ -30,7 +30,8 @@ enumOBJ_TYPE_PREFIX ,OBJ_TYPE_PUBKEY ,OBJ_TYPE_DTB , - OBJ_TYPE_DISABLE_SHIM_LOCK +OBJ_TYPE_DISABLE_SHIM_LOCK , + OBJ_TYPE_EFI_WATCHDOG_TIMEOUT, };/* The module header. */
include/grub/util/install.h +6 -2
@@ -67,6 +67,8 @@ N_("SBAT metadata"), 0 }, \ {"disable-shim-lock" ,GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK ,0 ,0 , \ N_("disable shim_lock verifier"), 0 }, \ + { "efi-watchdog-timeout", GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT, N_("EFI_WATCHDOG_TIMEOUT"), 0, \ + N_("arm efi watchdog at EFI_WATCHDOG_TIMEOUT seconds"), 0 }, \ { "verbose", 'v', 0, 0, \ N_("print verbose messages."), 1 }
@@ -128,7 +130,8 @@ enum grub_install_options {GRUB_INSTALL_OPTIONS_INSTALL_CORE_COMPRESS ,GRUB_INSTALL_OPTIONS_DTB ,GRUB_INSTALL_OPTIONS_SBAT , - GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK +GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK , + GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT };extern char *grub_install_source_directory;
@@ -190,7 +193,8 @@ grub_install_generate_image (const char *dir, const char *prefix,const struct grub_install_image_target_desc *image_target, int note, grub_compression_t comp, const char *dtb_file, - const char *sbat_path, const int disable_shim_lock); + const char *sbat_path, const int disable_shim_lock, + unsigned long efi_watchdog_timeout);const struct grub_install_image_target_desc *grub_install_get_image_target (const char *arg);
util/grub-install-common.c +10 -2
@@ -460,11 +460,13 @@ static char **pubkeys;static size_t npubkeys;static char *sbat;static int disable_shim_lock; +static unsigned long efi_watchdog_timeout;static grub_compression_t compression;int grub_install_parse (int key,char *arg) { +const char *const_arg = arg;switch (key) {case 'C' :
@@ -562,6 +564,11 @@ grub_install_parse (int key, char *arg)grub_util_error (_ ("Unrecognized compression `%s'" ), arg);case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE :return 1 ; +case GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT : + efi_watchdog_timeout =grub_strtoul (arg, &const_arg,0 ); +if (grub_errno) +grub_util_error (_ ("timeout argument must be an integer : %s" ), arg); +return 1 ;default :return 0 ; }
@@ -661,9 +668,10 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix," --output '%s' " " --dtb '%s' " "--sbat '%s' " +"--efi-watchdog-timeout '%lu' " "--format '%s' --compression '%s' %s %s %s\n" , dir, prefix, - outname, dtb ? :"" , sbat ? :"" , mkimage_target, + outname, dtb ? :"" , sbat ? :"" , efi_watchdog_timeout, mkimage_target, compnames[compression], note ?"--note" :"" , disable_shim_lock ? "--disable-shim-lock" : "", s);free (s);
@@ -676,7 +684,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix, modules.entries, memdisk_path, pubkeys, npubkeys, config_path, tgt, note, compression, dtb, sbat, - disable_shim_lock); + disable_shim_lock, efi_watchdog_timeout);while (dc--)grub_install_pop_module (); }
util/grub-mkimage.c +10 -1
@@ -83,6 +83,7 @@ static struct argp_option options[] = { {"compression", 'C', "(xz|none|auto)", 0, N_("choose the compression to use for core image"), 0}, {"sbat", 's', N_("FILE"), 0, N_("SBAT metadata"), 0}, {"disable-shim-lock", GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK, 0, 0, N_("disable shim_lock verifier"), 0}, + {"efi-watchdog-timeout", GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT, 0, 0, N_("efi watchdog timeout"), 0}, {"verbose", 'v', 0, 0, N_("print verbose messages."), 0}, { 0, 0, 0, 0, 0, 0 } };
@@ -128,6 +129,7 @@ struct argumentschar *sbat;int note;int disable_shim_lock; +unsigned long efi_watchdog_timeout;const struct grub_install_image_target_desc *image_target;grub_compression_t comp; };
@@ -138,6 +140,7 @@ argp_parser (int key, char *arg, struct argp_state *state)/* Get the input argument from argp_parse, which we know is a pointer to our arguments structure. */ struct arguments *arguments = state->input; +const char *const_arg = arg;switch (key) {
@@ -239,6 +242,12 @@ argp_parser (int key, char *arg, struct argp_state *state) arguments->disable_shim_lock =1 ;break ; +case GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT : + arguments->efi_watchdog_timeout =grub_strtoul (arg, &const_arg,0 ); +if (grub_errno) +grub_util_error (_ ("timeout argument must be an integer : %s" ), arg); +break ; +case 'v' : verbosity++;break ;
@@ -325,7 +334,7 @@ main (int argc, char *argv[]) arguments.npubkeys, arguments.config, arguments.image_target, arguments.note, arguments.comp, arguments.dtb, - arguments.sbat, arguments.disable_shim_lock); + arguments.sbat, arguments.disable_shim_lock, arguments.efi_watchdog_timeout);if (grub_util_file_sync (fp) <0 ) grub_util_error (_("cannot sync `%s': %s"), arguments.output ? : "stdout",
util/mkimage.c +21 -2
@@ -870,12 +870,12 @@ grub_install_generate_image (const char *dir, const char *prefix, size_t npubkeys, char *config_path, const struct grub_install_image_target_desc *image_target, int note, grub_compression_t comp, const char *dtb_path, - const char *sbat_path, int disable_shim_lock) + const char *sbat_path, int disable_shim_lock, unsigned long efi_watchdog_timeout) { char *kernel_img, *core_img;size_t total_module_size, core_size;size_t memdisk_size =0 , config_size =0 ; -size_t prefix_size =0 , dtb_size =0 , sbat_size =0 ; +size_t prefix_size =0 , dtb_size =0 , sbat_size =0 , efi_watchdog_timeout_size =0 ;char *kernel_path;size_t offset;struct grub_util_path_list *path_list, *p;
@@ -929,6 +929,12 @@ grub_install_generate_image (const char *dir, const char *prefix,if (sbat_path !=NULL && image_target->id !=IMAGE_EFI )grub_util_error (_ (".sbat section can be embedded into EFI images only" )); +if (efi_watchdog_timeout) + { + efi_watchdog_timeout_size =ALIGN_ADDR (sizeof (efi_watchdog_timeout)); + total_module_size += efi_watchdog_timeout_size +sizeof (struct grub_module_header ); + } +if (disable_shim_lock) total_module_size +=sizeof (struct grub_module_header );
@@ -1078,6 +1084,19 @@ grub_install_generate_image (const char *dir, const char *prefix, offset +=sizeof (*header); } +if (efi_watchdog_timeout) + { +struct grub_module_header *header; + + header = (struct grub_module_header *) (kernel_img + offset); + header->type =grub_host_to_target32 (OBJ_TYPE_EFI_WATCHDOG_TIMEOUT ); + header->size =grub_host_to_target32 (efi_watchdog_timeout_size +sizeof (*header)); + offset +=sizeof (*header); + +grub_memcpy (kernel_img + offset, &efi_watchdog_timeout,sizeof (efi_watchdog_timeout)); + offset += efi_watchdog_timeout_size; + } +if (config_path) {struct grub_module_header *header;
Signature · 1 line
2.25.1
Quoted 184 lines · Erwan Velu
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
- reworked to be controlled via mkimage
- reworked to simplify the command lineThis commit adds a new command efi-watchdog to manage efi watchdogs.
On server platforms, this allows 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
- the GRUB is unable to find a proper configuration and failsUsing --efi-watchdog-timeout option at mknetdir time allow users controlling the default value of the timer.
This set 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.
As per the UEFI specification :
The watchdog timer is only used during boot services. On successful completion ofEFI_BOOT_SERVICES.ExitBootServices()the watchdog timer is disabled.This implies this watchdog doesn't cover the the OS loading. If you want to cover this phase of the boot,
an additional hardware watchdog is required (usually set in the BIOS).On the command line, a single argument is enough to control the watchdog. This argument defines the timeout of the watchdog (in seconds).
- If 0 > argument <0xFFFF, the watchdog is armed with the associate timeout.
- If argument is set to 0, the watchdog is disarmed.By default GRUB disable the watchdog and so this patch. Therefore, this commit have no impact on the default GRUB's behavior.
This patch is used in production for month on a 50K server platform with success.
Signed-off-by Erwan Velu <[email protected]>Signed-off-by Arthur Mesh <[email protected]>--- docs/grub.texi | 13 ++++++ grub-core/kern/efi/init.c | 85 ++++++++++++++++++++++++++++++++++++- include/grub/efi/efi.h | 2 + include/grub/kernel.h | 3 +- include/grub/util/install.h | 8 +++- util/grub-install-common.c | 12 +++++- util/grub-mkimage.c | 11 ++++- util/mkimage.c | 23 +++++++++- 8 files changed, 147 insertions(+), 10 deletions(-)docs/grub.texi +13
-0@@ -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,18 @@ 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 @var{timeout} +Control the EFI system's watchdog timer. +Only available in EFI targeted GRUB. + +When the watchdog exceed @var{timeout}, the system is reset. + +If @var{timeout} is set to 0, the watchdog is disarmed. + +@end deffn @node eval @subsection evalgrub-core/kern/efi/init.c +78
-0@@ -28,6 +28,8 @@#include <grub/mm.h> #include <grub/kernel.h> #include <grub/stack_protector.h> +#include <grub/extcmd.h> +#include <grub/command.h> #ifdef GRUB_STACK_PROTECTOR @@ -82,6 +84,82 @@ stack_protector_init (void)grub_addr_t grub_modbase; +static grub_command_t cmd_list; + + +static grub_err_t grub_efi_set_watchdog_timer (unsigned long timeout) +{ +/* User defined code, set to BOOT (B007) for GRUB +UEFI SPEC 2.6 reports : +The numeric code to log on a watchdog timer timeout event. +The firmware reserves codes 0x0000 to 0xFFFF. +Loaders and operating systems may use other timeout codes. +*/ +grub_efi_uint64_t code =0xB007 ; +grub_efi_status_t status =0 ; + +if (timeout >=0xFFFF ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("efi-watchdog: timeout must be lower than 65535" )); + +if (timeout >0 ) +grub_printf ("grub %s: Arming EFI watchdog at %lu seconds\n" ,PACKAGE_VERSION , timeout); +else { +grub_printf ("grub %s: Disarming EFI watchdog\n" ,PACKAGE_VERSION ); + code =0 ; + } + + 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 ; +} + +static grub_err_t +grub_cmd_efi_watchdog (grub_command_t cmd __attribute__ ((unused)), +int argc,char **args) +{ +unsigned long timeout =0 ; + + +if (argc <1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("usage: efi-watchdog <timeout>" )); + +const char *ptr = args[0 ]; + timeout =grub_strtoul (ptr, &ptr,0 ); +if (grub_errno) +return grub_errno; + +return grub_efi_set_watchdog_timer (timeout); +} + +static void +grub_efi_watchdog_timer_init (void ) +{ +struct grub_module_header *header; +unsigned long efi_watchdog_timeout =0 ; + +FOR_MODULES (header) + if (header->type == OBJ_TYPE_EFI_WATCHDOG_TIMEOUT) + { +char *efi_wdt_orig_addr; +static char *efi_wdt_addr; +static grub_off_t efi_wdt_size =0 ; + + efi_wdt_orig_addr = (char *) header +sizeof (struct grub_module_header ); + efi_wdt_size = header->size -sizeof (struct grub_module_header ); + efi_wdt_addr =grub_malloc (efi_wdt_size); + +grub_memmove (efi_wdt_addr, efi_wdt_orig_addr, efi_wdt_size); + efi_watchdog_timeout = (unsigned long ) *efi_wdt_addr; +break ; + } + +grub_efi_set_watchdog_timer (efi_watchdog_timeout); +} + +void grub_efi_init (void ) {@@ -105,10 +183,12 @@ grub_efi_init (void)grub_shim_lock_verifier_setup ();
grub-core/kern/efi/init.c grub_efi_init (void) · line 191
Quoted 10 lines
grub-core/kern/efi/init.c +4
-2} -efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer, -0 ,0 ,0 ,NULL ); +grub_efi_watchdog_timer_init ();grub_efidisk_init (); + + cmd_list =grub_register_command ("efi-watchdog" , grub_cmd_efi_watchdog,0 , +N_ ("Enable/Disable system's watchdog timer." ));
I'd suggest to move this efi-watchdog command registration to grub_register_core_commands() in grub-core/kern/corecmd.c as that helps us tracing or knowing available commands in grub's rescue mode.
Also it would be great to see the explaination why this command needs to be in the core. To my understandng it could be that users may want to disarm the watchdog to not interrupt their work in rescue mode during a troubleshooting or debug session, but I am not fully clear about the purpose.
Thanks,
Michael
Quoted 228 lines
grub-core/kern/efi/init.c +1
-0} void (*grub_efi_net_config) (grub_efi_handle_t hnd,@@ -146,4 +226,5 @@ grub_efi_fini (void) {grub_efidisk_fini ();grub_console_fini (); +grub_unregister_command (cmd_list); }include/grub/efi/efi.h +2
-0@@ -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 */ include/grub/kernel.h +2
-1@@ -30,7 +30,8 @@ enumOBJ_TYPE_PREFIX ,OBJ_TYPE_PUBKEY ,OBJ_TYPE_DTB , - OBJ_TYPE_DISABLE_SHIM_LOCK +OBJ_TYPE_DISABLE_SHIM_LOCK , + OBJ_TYPE_EFI_WATCHDOG_TIMEOUT, };/* The module header. */ include/grub/util/install.h +6
-2@@ -67,6 +67,8 @@ N_("SBAT metadata"), 0 }, \ {"disable-shim-lock" ,GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK ,0 ,0 , \ N_("disable shim_lock verifier"), 0 }, \ + { "efi-watchdog-timeout", GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT, N_("EFI_WATCHDOG_TIMEOUT"), 0, \ + N_("arm efi watchdog at EFI_WATCHDOG_TIMEOUT seconds"), 0 }, \ { "verbose", 'v', 0, 0, \ N_("print verbose messages."), 1 }@@ -128,7 +130,8 @@ enum grub_install_options {GRUB_INSTALL_OPTIONS_INSTALL_CORE_COMPRESS ,GRUB_INSTALL_OPTIONS_DTB ,GRUB_INSTALL_OPTIONS_SBAT , - GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK +GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK , + GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT };extern char *grub_install_source_directory;@@ -190,7 +193,8 @@ grub_install_generate_image (const char *dir, const char *prefix,const struct grub_install_image_target_desc *image_target, int note, grub_compression_t comp, const char *dtb_file, - const char *sbat_path, const int disable_shim_lock); + const char *sbat_path, const int disable_shim_lock, + unsigned long efi_watchdog_timeout);const struct grub_install_image_target_desc *grub_install_get_image_target (const char *arg);util/grub-install-common.c +10
-2@@ -460,11 +460,13 @@ static char **pubkeys;static size_t npubkeys;static char *sbat;static int disable_shim_lock; +static unsigned long efi_watchdog_timeout;static grub_compression_t compression;int grub_install_parse (int key,char *arg) { +const char *const_arg = arg;switch (key) {case 'C' :@@ -562,6 +564,11 @@ grub_install_parse (int key, char *arg)grub_util_error (_ ("Unrecognized compression `%s'" ), arg);case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE :return 1 ; +case GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT : + efi_watchdog_timeout =grub_strtoul (arg, &const_arg,0 ); +if (grub_errno) +grub_util_error (_ ("timeout argument must be an integer : %s" ), arg); +return 1 ;default :return 0 ; }@@ -661,9 +668,10 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix," --output '%s' " " --dtb '%s' " "--sbat '%s' " +"--efi-watchdog-timeout '%lu' " "--format '%s' --compression '%s' %s %s %s\n" , dir, prefix, - outname, dtb ? :"" , sbat ? :"" , mkimage_target, + outname, dtb ? :"" , sbat ? :"" , efi_watchdog_timeout, mkimage_target, compnames[compression], note ?"--note" :"" , disable_shim_lock ? "--disable-shim-lock" : "", s);free (s);@@ -676,7 +684,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix, modules.entries, memdisk_path, pubkeys, npubkeys, config_path, tgt, note, compression, dtb, sbat, - disable_shim_lock); + disable_shim_lock, efi_watchdog_timeout);while (dc--)grub_install_pop_module (); }util/grub-mkimage.c +10
-1@@ -83,6 +83,7 @@ static struct argp_option options[] = { {"compression", 'C', "(xz|none|auto)", 0, N_("choose the compression to use for core image"), 0}, {"sbat", 's', N_("FILE"), 0, N_("SBAT metadata"), 0}, {"disable-shim-lock", GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK, 0, 0, N_("disable shim_lock verifier"), 0}, + {"efi-watchdog-timeout", GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT, 0, 0, N_("efi watchdog timeout"), 0}, {"verbose", 'v', 0, 0, N_("print verbose messages."), 0}, { 0, 0, 0, 0, 0, 0 } };@@ -128,6 +129,7 @@ struct argumentschar *sbat;int note;int disable_shim_lock; +unsigned long efi_watchdog_timeout;const struct grub_install_image_target_desc *image_target;grub_compression_t comp; };@@ -138,6 +140,7 @@ argp_parser (int key, char *arg, struct argp_state *state)/* Get the input argument from argp_parse, which we know is a pointer to our arguments structure. */ struct arguments *arguments = state->input; +const char *const_arg = arg;switch (key) {@@ -239,6 +242,12 @@ argp_parser (int key, char *arg, struct argp_state *state) arguments->disable_shim_lock =1 ;break ; +case GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT : + arguments->efi_watchdog_timeout =grub_strtoul (arg, &const_arg,0 ); +if (grub_errno) +grub_util_error (_ ("timeout argument must be an integer : %s" ), arg); +break ; +case 'v' : verbosity++;break ;@@ -325,7 +334,7 @@ main (int argc, char *argv[]) arguments.npubkeys, arguments.config, arguments.image_target, arguments.note, arguments.comp, arguments.dtb, - arguments.sbat, arguments.disable_shim_lock); + arguments.sbat, arguments.disable_shim_lock, arguments.efi_watchdog_timeout);if (grub_util_file_sync (fp) <0 ) grub_util_error (_("cannot sync `%s': %s"), arguments.output ? : "stdout",util/mkimage.c +21
-2@@ -870,12 +870,12 @@ grub_install_generate_image (const char *dir, const char *prefix, size_t npubkeys, char *config_path, const struct grub_install_image_target_desc *image_target, int note, grub_compression_t comp, const char *dtb_path, - const char *sbat_path, int disable_shim_lock) + const char *sbat_path, int disable_shim_lock, unsigned long efi_watchdog_timeout) { char *kernel_img, *core_img;size_t total_module_size, core_size;size_t memdisk_size =0 , config_size =0 ; -size_t prefix_size =0 , dtb_size =0 , sbat_size =0 ; +size_t prefix_size =0 , dtb_size =0 , sbat_size =0 , efi_watchdog_timeout_size =0 ;char *kernel_path;size_t offset;struct grub_util_path_list *path_list, *p;@@ -929,6 +929,12 @@ grub_install_generate_image (const char *dir, const char *prefix,if (sbat_path !=NULL && image_target->id !=IMAGE_EFI )grub_util_error (_ (".sbat section can be embedded into EFI images only" )); +if (efi_watchdog_timeout) + { + efi_watchdog_timeout_size =ALIGN_ADDR (sizeof (efi_watchdog_timeout)); + total_module_size += efi_watchdog_timeout_size +sizeof (struct grub_module_header ); + } +if (disable_shim_lock) total_module_size +=sizeof (struct grub_module_header );@@ -1078,6 +1084,19 @@ grub_install_generate_image (const char *dir, const char *prefix, offset +=sizeof (*header); } +if (efi_watchdog_timeout) + { +struct grub_module_header *header; + + header = (struct grub_module_header *) (kernel_img + offset); + header->type =grub_host_to_target32 (OBJ_TYPE_EFI_WATCHDOG_TIMEOUT ); + header->size =grub_host_to_target32 (efi_watchdog_timeout_size +sizeof (*header)); + offset +=sizeof (*header); + +grub_memcpy (kernel_img + offset, &efi_watchdog_timeout,sizeof (efi_watchdog_timeout)); + offset += efi_watchdog_timeout_size; + } +if (config_path) {struct grub_module_header *header;Signature · 7 lines
2.25.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Le 03/09/2021 à 04:09, Michael Chang a écrit :
[...]
Quoted 9 lines
I'd suggest to move this efi-watchdog command registration to
grub_register_core_commands()ingrub-core/kern/corecmd.cas that helps us tracing or knowing available commands in grub's rescue mode.Also it would be great to see the explaination why this command needs to be in the core. To my understandng it could be that users may want to disarm the watchdog to not interrupt their work in rescue mode during a troubleshooting or debug session, but I am not fully clear about the purpose.
Hey Michael,
I'm waiting for other reviews and will add your points into a V3.
You are perfectly right on the aim.
To be efficient, the watchdog must be enabled at soon as possible and so
cannot be set into the configuration file as we are far too late and issues
could have already hit GRUB (aka unable to read the filesystem).
So, once the option is set, every-time GRUB will boots the watchdog is enabled.
And yes, if you need to perform a debug session, you need being in a position to disable it.
That's why we need this commands in the low-level command sets of grub.
I'm not very familiar with GRUB internals, so if the corecmd is a better place and other agree on that,
I'll make the move in V3.
Erwan,
Daniel, anything more about this V2 before I prepare the V3 ?
Quoted 48 lines · Erwan Velu
Le 03/09/2021 à 04:09, Michael Chang a écrit :
[...]Quoted 9 lines
I'd suggest to move this efi-watchdog command registration to
grub_register_core_commands()ingrub-core/kern/corecmd.cas that helps us tracing or knowing available commands in grub's rescue mode.Also it would be great to see the explaination why this command needs to be in the core. To my understandng it could be that users may want to disarm the watchdog to not interrupt their work in rescue mode during a troubleshooting or debug session, but I am not fully clear about the purpose.
Hey Michael,
I'm waiting for other reviews and will add your points into a V3.
You are perfectly right on the aim.
To be efficient, the watchdog must be enabled at soon as possible and so
cannot be set into the configuration file as we are far too late and issues
could have already hit GRUB (aka unable to read the filesystem).
So, once the option is set, every-time GRUB will boots the watchdog is enabled.
And yes, if you need to perform a debug session, you need being in a position to disable it.
That's why we need this commands in the low-level command sets of grub.
I'm not very familiar with GRUB internals, so if the corecmd is a better place and other agree on that,
I'll make the move in V3.
Erwan,
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 1 line · Erwan Velu
Daniel, anything more about this V2 before I prepare the V3 ?
I will be looking at it this week.
Daniel
Thanks !
Quoted 7 lines · Daniel Kiper
Quoted 1 line · Erwan Velu
Daniel, anything more about this V2 before I prepare the V3 ?
I will be looking at it this week.
Daniel
Quoted 8 lines · Erwan Velu
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
- reworked to be controlled via mkimage
- reworked to simplify the command line
All lines above should go after SOB and "---". Good example what I mean is here [1].
Quoted 1 line
This commit adds a new command efi-watchdog to manage efi watchdogs.
I do not see any reason to name the command "efi-watchdog". It should be simply named as "watchdog". If in the future we will support more watchdog devices we can add a command to switch between them.
Quoted 1 line
On server platforms, this allows GRUB to reset the host automatically
Please drop "On server platforms".
Quoted 6 lines
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
- the GRUB is unable to find a proper configuration and failsUsing --efi-watchdog-timeout option at mknetdir time allow users
s/efi-watchdog-timeout/watchdog-timeout/
And I am not sure what you mean by "Using ... option at mknetdir time"...
Quoted 6 lines
controlling the default value of the timer.
This set 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.
The GRUB not always calls ExitBootServices(). So, this is not entirely true. E.g Xen calls ExitBootServices() instead of GRUB on UEFI platforms.
Quoted 7 lines
As per the UEFI specification :
The watchdog timer is only used during boot services. On successful completion ofEFI_BOOT_SERVICES.ExitBootServices()the watchdog timer is disabled.This implies this watchdog doesn't cover the the OS loading. If you want to cover this phase of the boot,
an additional hardware watchdog is required (usually set in the BIOS).
Ditto. Please update these two paragraphs taking into account my comments above.
Quoted 9 lines
On the command line, a single argument is enough to control the watchdog. This argument defines the timeout of the watchdog (in seconds).
- If 0 > argument <0xFFFF, the watchdog is armed with the associate timeout.
- If argument is set to 0, the watchdog is disarmed.By default GRUB disable the watchdog and so this patch. Therefore, this commit have no impact on the default GRUB's behavior.
This patch is used in production for month on a 50K server platform with success.
I think you are missing a description why this functionality must be in the GRUB core.img.
Quoted 2 lines
Signed-off-by Erwan Velu <[email protected]>Signed-off-by Arthur Mesh <[email protected]>
Arthur Mesh SOB should be before yours.
Quoted 24 lines
--- docs/grub.texi | 13 ++++++ grub-core/kern/efi/init.c | 85 ++++++++++++++++++++++++++++++++++++- include/grub/efi/efi.h | 2 + include/grub/kernel.h | 3 +- include/grub/util/install.h | 8 +++- util/grub-install-common.c | 12 +++++- util/grub-mkimage.c | 11 ++++- util/mkimage.c | 23 +++++++++- 8 files changed, 147 insertions(+), 10 deletions(-)docs/grub.texi +1
-0@@ -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
docs/grub.texi When interpreting backslash escapes, backslash followed by any other · line 4447
Quoted 5 lines
docs/grub.texi +2
-0@@ -4442,6 +4443,18 @@ When interpreting backslash escapes, backslash followed by any other character will print that character. @end deffn +@node efi-watchdog +@subsection efi-watchdog
s/efi-watchdog/watchdog/ and below please...
docs/grub.texi When interpreting backslash escapes, backslash followed by any other · line 4451
Quoted 3 lines
docs/grub.texi +3
-0+@deffn Command efi-watchdog @var{timeout} +Control the EFI system's watchdog timer. +Only available in EFI targeted GRUB.
I do not see any reason to put this in two lines.
docs/grub.texi When interpreting backslash escapes, backslash followed by any other · line 4456
Quoted 5 lines
docs/grub.texi +5
-0+ +When the watchdog exceed @var{timeout}, the system is reset. + +If @var{timeout} is set to 0, the watchdog is disarmed. +
Please drop this empty line.
grub-core/kern/efi/init.c line 31
Quoted 13 lines
docs/grub.texi +1
-0+@end deffn @node eval @subsection evalgrub-core/kern/efi/init.c +1
-0@@ -28,6 +28,8 @@#include <grub/mm.h> #include <grub/kernel.h> #include <grub/stack_protector.h> +#include <grub/extcmd.h>
I think this include is not needed.
grub-core/kern/efi/init.c stack_protector_init (void) · line 89
Quoted 11 lines
grub-core/kern/efi/init.c +4
-0+#include <grub/command.h> #ifdef GRUB_STACK_PROTECTOR @@ -82,6 +84,82 @@ stack_protector_init (void)grub_addr_t grub_modbase; +static grub_command_t cmd_list; + +
Please drop this empty line.
grub-core/kern/efi/init.c stack_protector_init (void) · line 90
Quoted 1 line
grub-core/kern/efi/init.c +1
-0+static grub_err_t grub_efi_set_watchdog_timer (unsigned long timeout)
Please use grub_efi_uintn_t instead of unsigned long. However, please remember that its size depends on architecture. So, it can be 32-bits or 64-bits...
grub-core/kern/efi/init.c stack_protector_init (void) · line 93
Quoted 3 lines
grub-core/kern/efi/init.c +3
-0+{ +/* User defined code, set to BOOT (B007) for GRUB +UEFI SPEC 2.6 reports :
Please refer to the latest UEFI spec.
grub-core/kern/efi/init.c stack_protector_init (void) · line 96
Quoted 3 lines
grub-core/kern/efi/init.c +3
-0+ The numeric code to log on a watchdog timer timeout event. + The firmware reserves codes 0x0000 to 0xFFFF. + Loaders and operating systems may use other timeout codes.
This comment says you cannot use 0xb007. I would use 0xdeadb007deadb007 by default. However, I would give an option to change the default by user.
grub-core/kern/efi/init.c stack_protector_init (void) · line 97
Quoted 1 line
grub-core/kern/efi/init.c +1
-0+*/
Please format comments according to [2].
grub-core/kern/efi/init.c stack_protector_init (void) · line 102
Quoted 5 lines
grub-core/kern/efi/init.c +5
-0+grub_efi_uint64_t code =0xB007 ; +grub_efi_status_t status =0 ; + +if (timeout >=0xFFFF ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("efi-watchdog: timeout must be lower than 65535" ));
Why do you artificially limit the timeout value? Please do not do that.
grub-core/kern/efi/init.c stack_protector_init (void) · line 105
Quoted 2 lines
grub-core/kern/efi/init.c +2
-0+if (timeout >0 ) +grub_printf ("grub %s: Arming EFI watchdog at %lu seconds\n" ,PACKAGE_VERSION , timeout);
If you want debug messages please use grub_dprintf() instead of grub_printf().
grub-core/kern/efi/init.c stack_protector_init (void) · line 114
Quoted 9 lines
grub-core/kern/efi/init.c +9
-0+else { +grub_printf ("grub %s: Disarming EFI watchdog\n" ,PACKAGE_VERSION ); + code =0 ; + } + + 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" ));
Please print status value in case of an error, e.g. "UEFI SetWatchdogTimer() error: %..."
grub-core/kern/efi/init.c stack_protector_init (void) · line 123
Quoted 9 lines
grub-core/kern/efi/init.c +9
-0+else +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_cmd_efi_watchdog (grub_command_t cmd __attribute__ ((unused)), +int argc,char **args) +{ +unsigned long timeout =0 ;
s/unsigned long/grub_efi_uintn_t/ Please use types according to the UEFI specification and convert properly between them.
grub-core/kern/efi/init.c stack_protector_init (void) · line 124
Quoted 2 lines
grub-core/kern/efi/init.c +2
-0+ +
Please drop this redundant empty line.
grub-core/kern/efi/init.c stack_protector_init (void) · line 127
Quoted 2 lines
grub-core/kern/efi/init.c +2
-0+if (argc <1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("usage: efi-watchdog <timeout>" ));
As I said earlier. I think "watchdog" command should take two arguments:
-t <timeout> and -c <code>.
grub-core/kern/efi/init.c stack_protector_init (void) · line 130
Quoted 2 lines
grub-core/kern/efi/init.c +2
-0+const char *ptr = args[0 ]; + timeout =grub_strtoul (ptr, &ptr,0 );
Please read strtoul() documentation and handle all errors properly.
grub-core/kern/efi/init.c stack_protector_init (void) · line 129
Quoted 1 line
grub-core/kern/efi/init.c +1
-0+if (grub_errno)
if (grub_errno != GRUB_ERR_NONE)
grub-core/kern/efi/init.c stack_protector_init (void) · line 134
Quoted 3 lines
grub-core/kern/efi/init.c +3
-0+return grub_errno; + +return grub_efi_set_watchdog_timer (timeout);
Please do not forget about spaces between function names and "(".
grub-core/kern/efi/init.c stack_protector_init (void) · line 141
Quoted 7 lines
grub-core/kern/efi/init.c +7
-0+} + +static void +grub_efi_watchdog_timer_init (void ) +{ +struct grub_module_header *header; +unsigned long efi_watchdog_timeout =0 ;
Again, please use correct UEFI type...
grub-core/kern/efi/init.c stack_protector_init (void) · line 141
Quoted 2 lines
grub-core/kern/efi/init.c +2
-0+ +FOR_MODULES (header)
Missing "{}" for FOR_MODULES() and...
grub-core/kern/efi/init.c stack_protector_init (void) · line 144
Quoted 1 line
grub-core/kern/efi/init.c +1
-0+if (header->type ==OBJ_TYPE_EFI_WATCHDOG_TIMEOUT )
... incorrect "if" indention. Please take a look at grub-core/kern/efi/sb.c for more details...
grub-core/kern/efi/init.c stack_protector_init (void) · line 152
Quoted 8 lines
grub-core/kern/efi/init.c +8
-0+ { +char *efi_wdt_orig_addr; +static char *efi_wdt_addr; +static grub_off_t efi_wdt_size =0 ; + + efi_wdt_orig_addr = (char *) header +sizeof (struct grub_module_header ); + efi_wdt_size = header->size -sizeof (struct grub_module_header ); + efi_wdt_addr =grub_malloc (efi_wdt_size);
Please do not ignore grub_malloc() errors. In general you need proper error handling for every function which may fail.
grub-core/kern/efi/init.c stack_protector_init (void) · line 162
Quoted 9 lines
grub-core/kern/efi/init.c +9
-0+grub_memmove (efi_wdt_addr, efi_wdt_orig_addr, efi_wdt_size); + efi_watchdog_timeout = (unsigned long ) *efi_wdt_addr; +break ; + } + +grub_efi_set_watchdog_timer (efi_watchdog_timeout); +} + +
Please drop this redundant empty line...
Quoted 101 lines
grub-core/kern/efi/init.c +5
-2void grub_efi_init (void ) {@@ -105,10 +183,12 @@ grub_efi_init (void)grub_shim_lock_verifier_setup (); } -efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer, -0 ,0 ,0 ,NULL ); +grub_efi_watchdog_timer_init ();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 +226,5 @@ grub_efi_fini (void) {grub_efidisk_fini ();grub_console_fini (); +grub_unregister_command (cmd_list); }include/grub/efi/efi.h +2
-0@@ -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 */ include/grub/kernel.h +2
-1@@ -30,7 +30,8 @@ enumOBJ_TYPE_PREFIX ,OBJ_TYPE_PUBKEY ,OBJ_TYPE_DTB , - OBJ_TYPE_DISABLE_SHIM_LOCK +OBJ_TYPE_DISABLE_SHIM_LOCK , + OBJ_TYPE_EFI_WATCHDOG_TIMEOUT, };/* The module header. */ include/grub/util/install.h +6
-2@@ -67,6 +67,8 @@ N_("SBAT metadata"), 0 }, \ {"disable-shim-lock" ,GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK ,0 ,0 , \ N_("disable shim_lock verifier"), 0 }, \ + { "efi-watchdog-timeout", GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT, N_("EFI_WATCHDOG_TIMEOUT"), 0, \ + N_("arm efi watchdog at EFI_WATCHDOG_TIMEOUT seconds"), 0 }, \ { "verbose", 'v', 0, 0, \ N_("print verbose messages."), 1 }@@ -128,7 +130,8 @@ enum grub_install_options {GRUB_INSTALL_OPTIONS_INSTALL_CORE_COMPRESS ,GRUB_INSTALL_OPTIONS_DTB ,GRUB_INSTALL_OPTIONS_SBAT , - GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK +GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK , + GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT };extern char *grub_install_source_directory;@@ -190,7 +193,8 @@ grub_install_generate_image (const char *dir, const char *prefix,const struct grub_install_image_target_desc *image_target, int note, grub_compression_t comp, const char *dtb_file, - const char *sbat_path, const int disable_shim_lock); + const char *sbat_path, const int disable_shim_lock, + unsigned long efi_watchdog_timeout);const struct grub_install_image_target_desc *grub_install_get_image_target (const char *arg);util/grub-install-common.c +2
-0@@ -460,11 +460,13 @@ static char **pubkeys;static size_t npubkeys;static char *sbat;static int disable_shim_lock; +static unsigned long efi_watchdog_timeout;static grub_compression_t compression;int grub_install_parse (int key,char *arg) { +const char *const_arg = arg;switch (key) {case 'C' :
util/grub-install-common.c grub_install_parse (int key, char *arg) · line 568
Quoted 5 lines
util/grub-install-common.c +2
-0@@ -562,6 +564,11 @@ grub_install_parse (int key, char *arg)grub_util_error (_ ("Unrecognized compression `%s'" ), arg);case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE :return 1 ; +case GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT : + efi_watchdog_timeout =grub_strtoul (arg, &const_arg,0 );
Please do not ignore errors...
Quoted 7 lines
util/grub-install-common.c +3
-0+if (grub_errno) +grub_util_error (_ ("timeout argument must be an integer : %s" ), arg); +return 1 ;default :return 0 ; }
util/grub-install-common.c grub_install_make_image_wrap_file (const char *dir, const char *prefix, · line 676
Quoted 10 lines
util/grub-install-common.c +2
-1@@ -661,9 +668,10 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix," --output '%s' " " --dtb '%s' " "--sbat '%s' " +"--efi-watchdog-timeout '%lu' " "--format '%s' --compression '%s' %s %s %s\n" , dir, prefix, - outname, dtb ? :"" , sbat ? :"" , mkimage_target, + outname, dtb ? :"" , sbat ? :"" , efi_watchdog_timeout, mkimage_target, compnames[compression], note ?"--note" :"" , disable_shim_lock ? "--disable-shim-lock" : "", s);
Could you be more consistent and add your argument behind the --disable-shim-lock one?
Quoted 93 lines
util/grub-install-common.c +1
-1free (s);@@ -676,7 +684,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix, modules.entries, memdisk_path, pubkeys, npubkeys, config_path, tgt, note, compression, dtb, sbat, - disable_shim_lock); + disable_shim_lock, efi_watchdog_timeout);while (dc--)grub_install_pop_module (); }util/grub-mkimage.c +10
-1@@ -83,6 +83,7 @@ static struct argp_option options[] = { {"compression", 'C', "(xz|none|auto)", 0, N_("choose the compression to use for core image"), 0}, {"sbat", 's', N_("FILE"), 0, N_("SBAT metadata"), 0}, {"disable-shim-lock", GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK, 0, 0, N_("disable shim_lock verifier"), 0}, + {"efi-watchdog-timeout", GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT, 0, 0, N_("efi watchdog timeout"), 0}, {"verbose", 'v', 0, 0, N_("print verbose messages."), 0}, { 0, 0, 0, 0, 0, 0 } };@@ -128,6 +129,7 @@ struct argumentschar *sbat;int note;int disable_shim_lock; +unsigned long efi_watchdog_timeout;const struct grub_install_image_target_desc *image_target;grub_compression_t comp; };@@ -138,6 +140,7 @@ argp_parser (int key, char *arg, struct argp_state *state)/* Get the input argument from argp_parse, which we know is a pointer to our arguments structure. */ struct arguments *arguments = state->input; +const char *const_arg = arg;switch (key) {@@ -239,6 +242,12 @@ argp_parser (int key, char *arg, struct argp_state *state) arguments->disable_shim_lock =1 ;break ; +case GRUB_INSTALL_OPTIONS_EFI_WATCHDOG_TIMEOUT : + arguments->efi_watchdog_timeout =grub_strtoul (arg, &const_arg,0 ); +if (grub_errno) +grub_util_error (_ ("timeout argument must be an integer : %s" ), arg); +break ; +case 'v' : verbosity++;break ;@@ -325,7 +334,7 @@ main (int argc, char *argv[]) arguments.npubkeys, arguments.config, arguments.image_target, arguments.note, arguments.comp, arguments.dtb, - arguments.sbat, arguments.disable_shim_lock); + arguments.sbat, arguments.disable_shim_lock, arguments.efi_watchdog_timeout);if (grub_util_file_sync (fp) <0 ) grub_util_error (_("cannot sync `%s': %s"), arguments.output ? : "stdout",util/mkimage.c +8
-2@@ -870,12 +870,12 @@ grub_install_generate_image (const char *dir, const char *prefix, size_t npubkeys, char *config_path, const struct grub_install_image_target_desc *image_target, int note, grub_compression_t comp, const char *dtb_path, - const char *sbat_path, int disable_shim_lock) + const char *sbat_path, int disable_shim_lock, unsigned long efi_watchdog_timeout) { char *kernel_img, *core_img;size_t total_module_size, core_size;size_t memdisk_size =0 , config_size =0 ; -size_t prefix_size =0 , dtb_size =0 , sbat_size =0 ; +size_t prefix_size =0 , dtb_size =0 , sbat_size =0 , efi_watchdog_timeout_size =0 ;char *kernel_path;size_t offset;struct grub_util_path_list *path_list, *p;@@ -929,6 +929,12 @@ grub_install_generate_image (const char *dir, const char *prefix,if (sbat_path !=NULL && image_target->id !=IMAGE_EFI )grub_util_error (_ (".sbat section can be embedded into EFI images only" )); +if (efi_watchdog_timeout) + { + efi_watchdog_timeout_size =ALIGN_ADDR (sizeof (efi_watchdog_timeout)); + total_module_size += efi_watchdog_timeout_size +sizeof (struct grub_module_header ); + } +if (disable_shim_lock) total_module_size +=sizeof (struct grub_module_header );
util/mkimage.c grub_install_generate_image (const char *dir, const char *prefix, · line 1093
Quoted 10 lines
util/mkimage.c +7
-0@@ -1078,6 +1084,19 @@ grub_install_generate_image (const char *dir, const char *prefix, offset +=sizeof (*header); } +if (efi_watchdog_timeout) + { +struct grub_module_header *header; + + header = (struct grub_module_header *) (kernel_img + offset); + header->type =grub_host_to_target32 (OBJ_TYPE_EFI_WATCHDOG_TIMEOUT ); + header->size =grub_host_to_target32 (efi_watchdog_timeout_size +sizeof (*header));
After looking at this it seems to me the timeout should be u32 thing internally in the GRUB.
Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2021-06/msg00017.html
[2] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments
Thanks for your review, I'll rework my patch according to your comments. I just kept the open points in this thread.
Le 09/09/2021 à 17:46, Daniel Kiper a écrit :
Quoted 5 lines
+static
grub_err_tgrub_efi_set_watchdog_timer(unsigned long timeout)Please usegrub_efi_uintn_tinstead of unsigned long. However, please remember that its size depends on architecture. So, it can be 32-bits or 64-bits...
I'll follow the uefi spec here so grub_efi_uintn_ is fine.
Quoted 5 lines
grub-core/kern/efi/init.c stack_protector_init (void) · line 96
Quoted 3 lines
grub-core/kern/efi/init.c +3
-0+ The numeric code to log on a watchdog timer timeout event. + The firmware reserves codes 0x0000 to 0xFFFF. + Loaders and operating systems may use other timeout codes.This comment says you cannot use
0xb007. I would use0xdeadb007deadb007by default. However, I would give an option to change the default by user.
Stupid me ....
I was wondering if the code should be configurable.
In the initial patch it was the case but though this would give an easier command line interface to users.
I mean, the aim of this code is to report mostly 'who' manipulated the watchdog, so sound good to me to get a value that is more grub depend than user centric.
Quoted 10 lines
Quoted 2 lines
+ */
Please format comments according to [2].
grub-core/kern/efi/init.c stack_protector_init (void) · line 102
Quoted 5 lines
grub-core/kern/efi/init.c +5
-0+grub_efi_uint64_t code =0xB007 ; +grub_efi_status_t status =0 ; + +if (timeout >=0xFFFF ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("efi-watchdog: timeout must be lower than 65535" ));Why do you artificially limit the timeout value? Please do not do that.
Mistake of my own, make sense to remove that.
Quoted 4 lines
grub-core/kern/efi/init.c stack_protector_init (void) · line 105
Quoted 3 lines
grub-core/kern/efi/init.c +2
-0+if (timeout >0 ) +grub_printf ("grub %s: Arming EFI watchdog at %lu seconds\n" ,PACKAGE_VERSION , timeout);If you want debug messages please use
grub_dprintf()instead ofgrub_printf().
Maybe my print isn't ideal but I found important to warn the user a watchdog is armed.
If the user is not informed and the watchdog fires, the user could think there is a hardware issue on the host.
So to me, this message is mandatory to say users : "beware, the system will be reset within x seconds if you don't boot"
Quoted 10 lines · Erwan Velu
Thanks for your review, I'll rework my patch according to your comments. I just kept the open points in this thread.
Le 09/09/2021 à 17:46, Daniel Kiper a écrit :
Quoted 5 lines
+static
grub_err_tgrub_efi_set_watchdog_timer(unsigned long timeout)Please usegrub_efi_uintn_tinstead of unsigned long. However, please remember that its size depends on architecture. So, it can be 32-bits or 64-bits...I'll follow the uefi spec here so
grub_efi_uintn_is fine.
At the end of my previous email I suggested it should be u32, i.e. grub_efi_uint32_t. I think we should stick with it and only do conversion during UEFI watchdog call. Of course it means you have to be very careful what you get from grub_strtoul().
Quoted 8 lines
Quoted 5 lines
grub-core/kern/efi/init.c stack_protector_init (void) · line 96
Quoted 3 lines
grub-core/kern/efi/init.c +3
-0+ The numeric code to log on a watchdog timer timeout event. + The firmware reserves codes 0x0000 to 0xFFFF. + Loaders and operating systems may use other timeout codes.This comment says you cannot use
0xb007. I would use0xdeadb007deadb007by default. However, I would give an option to change the default by user.Stupid me ....
Quoted 8 lines
I was wondering if the code should be configurable.
In the initial patch it was the case but though this would give an easier command line interface to users.
I mean, the aim of this code is to report mostly 'who' manipulated the watchdog, so sound good to me to get a value that is more grub depend than user centric.
The GRUB should provide default but user should be able to change it.
Quoted 11 lines
Quoted 9 lines
Quoted 1 line
+ */
Please format comments according to [2].
grub-core/kern/efi/init.c stack_protector_init (void) · line 102
Quoted 5 lines
grub-core/kern/efi/init.c +5
-0+grub_efi_uint64_t code =0xB007 ; +grub_efi_status_t status =0 ; + +if (timeout >=0xFFFF ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("efi-watchdog: timeout must be lower than 65535" ));Why do you artificially limit the timeout value? Please do not do that.
Mistake of my own, make sense to remove that.
Please be aware of my note WRT grub_efi_uint32_t type above.
Quoted 4 lines
Quoted 3 lines
grub-core/kern/efi/init.c stack_protector_init (void) · line 105
Quoted 2 lines
grub-core/kern/efi/init.c +2
-0+if (timeout >0 ) +grub_printf ("grub %s: Arming EFI watchdog at %lu seconds\n" ,PACKAGE_VERSION , timeout);If you want debug messages please use
grub_dprintf()instead ofgrub_printf().
Quoted 8 lines
Maybe my print isn't ideal but I found important to warn the user a watchdog is armed.
If the user is not informed and the watchdog fires, the user could think there is a hardware issue on the host.
So to me, this message is mandatory to say users : "beware, the system will be reset within x seconds if you don't boot"
If user have problems with watchdog they can enable debugging and see what is going on. That is why I suggested grub_dprintf() usage.
Daniel