Add Xen command line parsing
cover Add Xen command line parsing
The purpose of this patch is to allow the Xen hypervisor to pass extra data to GRUB in the form of a kernel command line, allowing the host to customize the boot process of the guest. The command line from Xen is parsed, and any variables within that start with the string xen_grub_env_ are exposed as environment variables. The grub.cfg script can then use those environment variables as it sees fit.
The main reason for doing this is to allow implementing boot modes in Qubes OS while also using in-VM kernels. For more context on Qubes boot modes, see [1]. In order for this to work with in-VM kernels, it is necessary for dom0 to pass kernel parameters to the guest without modifying the guest's grub.cfg manually. This patch allows this to be done, by allowing dom0 to pass kernel parameters to GRUB, which then provides them to grub.cfg as an environment variable. The grub.cfg script within the VM can then append those variables to the kernel command line.
This is a significantly more robust version of the Xen command line parsing draft sent previously [2]. The patch has been tested with both PV and PVH virtual machines, using an otherwise unpatched GRUB source tree, building the patch on top of the tip of git master at the time of this writing (commit 4abac0a). My testing environment is a fully updated Arch Linux system with Xen built from the stable-4.20 branch.
Some changes from the initial draft sent previously:
* Only parameters starting with xen_grub_env_ are exposed as environment variables. This solves two of the previous patch's issues
- dom0 can no longer clobber arbitrary variables within grub.cfg (as it cannot define variables with names that don't start with xen_grub_env), and API breakage won't occur if in the future more features are added to GRUB that use the Xen-provided kernel command line.
* Testing has been done as described above.
[1] https://github.com/QubesOS/qubes-linux-pvgrub2/pull/16
[2] https://lists.gnu.org/archive/html/grub-devel/2025-04/msg00050.html
Aaron Rainbolt (1):
Add Xen command line parsing
grub-core/Makefile.core.def | 2 + grub-core/kern/i386/xen/pvh.c | 16 ++ grub-core/kern/main.c | 12 ++ grub-core/kern/xen/cmdline.c | 270 ++++++++++++++++++++++++++++++++++ include/grub/xen.h | 2 + 5 files changed, 302 insertions(+) create mode 100644 grub-core/kern/xen/cmdline.c
Signature · 1 line
2.49.0
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 1/1 Add Xen command line parsing
Enable GRUB to parse the Xen command line for parameters, and expose certain of those parameters to the GRUB config file (or rescue shell) as environment variables.
--- grub-core/Makefile.core.def | 2 + grub-core/kern/i386/xen/pvh.c | 16 ++ grub-core/kern/main.c | 12 ++ grub-core/kern/xen/cmdline.c | 270 ++++++++++++++++++++++++++++++++++ include/grub/xen.h | 2 + 5 files changed, 302 insertions(+) create mode 100644 grub-core/kern/xen/cmdline.c
grub-core/Makefile.core.def +2 -0
@@ -248,6 +248,7 @@ kernel = { xen = term/xen/console.c; xen = disk/xen/xendisk.c; xen = commands/boot.c; + xen = kern/xen/cmdline.c; i386_xen_pvh = commands/boot.c; i386_xen_pvh = disk/xen/xendisk.c;
@@ -255,6 +256,7 @@ kernel = { i386_xen_pvh = kern/i386/xen/tsc.c; i386_xen_pvh = kern/i386/xen/pvh.c; i386_xen_pvh = kern/xen/init.c; + i386_xen_pvh = kern/xen/cmdline.c; i386_xen_pvh = term/xen/console.c; ia64_efi = kern/ia64/efi/startup.S;
grub-core/kern/i386/xen/pvh.c +16 -0
@@ -352,6 +352,22 @@ grub_xen_setup_pvh (void)grub_xen_mm_init_regions (); grub_rsdp_addr = pvh_start_info->rsdp_paddr; +int xen_cmdline_fits =0 ; +char *xen_cmdline = (char *) pvh_start_info->cmdline_paddr; +for (int i =0 ; i <1024 ; i++) + { +if (xen_cmdline[i] =='\0' ) + { + xen_cmdline_fits =1 ; +break ; + } + } +if (xen_cmdline_fits ==1 ) + { +grub_strncpy ((char *) grub_xen_start_page_addr->cmd_line, + (char *) pvh_start_info->cmdline_paddr, +MAX_GUEST_CMDLINE ); + } } grub_err_t
grub-core/kern/main.c +12 -0
@@ -40,6 +40,10 @@static bool cli_disabled = false;static bool cli_need_auth = false; +#if defined (GRUB_MACHINE_XEN ) || defined (GRUB_MACHINE_XEN_PVH ) +#include <grub/xen.h> +#endif +grub_addr_t grub_modules_get_end (void ) {
@@ -351,6 +355,14 @@ grub_main (void)grub_env_export ("root" );grub_env_export ("prefix" ); +/* +* Parse command line parameters from Xen and export them as environment +* variables +*/ +#if defined (GRUB_MACHINE_XEN ) || defined (GRUB_MACHINE_XEN_PVH ) +grub_parse_xen_cmdline (); +#endif +/* Reclaim space used for modules. */ reclaim_module_space ();
grub-core/kern/xen/cmdline.c new file +270 -0
270 lines not on this page · raw message
include/grub/xen.h +2 -0
@@ -89,6 +89,8 @@ void grub_console_init (void);void grub_xendisk_fini (void );void grub_xendisk_init (void ); +void grub_parse_xen_cmdline (void ); +#ifdef __x86_64__typedef grub_uint64_t grub_xen_mfn_t ;#else
Signature · 1 line
2.49.0
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
First of all, next time please add Xen-devel ML to recipients too...
Quoted 3 lines · Aaron Rainbolt
Enable GRUB to parse the Xen command line for parameters, and expose certain of those parameters to the GRUB config file (or rescue shell) as environment variables.
Please make the commit message more verbose, i.e., explain why this feature it is needed, how it works, etc.
Quoted 6 lines
--- grub-core/Makefile.core.def | 2 + grub-core/kern/i386/xen/pvh.c | 16 ++ grub-core/kern/main.c | 12 ++ grub-core/kern/xen/cmdline.c | 270 ++++++++++++++++++++++++++++++++++ include/grub/xen.h | 2 +
I think this feature should have a description in the GRUB docs.
Quoted 28 lines
5 files changed, 302 insertions(+) create mode 100644 grub-core/kern/xen/cmdline.cgrub-core/Makefile.core.def +2
-0@@ -248,6 +248,7 @@ kernel = { xen = term/xen/console.c; xen = disk/xen/xendisk.c; xen = commands/boot.c; + xen = kern/xen/cmdline.c; i386_xen_pvh = commands/boot.c; i386_xen_pvh = disk/xen/xendisk.c;@@ -255,6 +256,7 @@ kernel = { i386_xen_pvh = kern/i386/xen/tsc.c; i386_xen_pvh = kern/i386/xen/pvh.c; i386_xen_pvh = kern/xen/init.c; + i386_xen_pvh = kern/xen/cmdline.c; i386_xen_pvh = term/xen/console.c; ia64_efi = kern/ia64/efi/startup.S;grub-core/kern/i386/xen/pvh.c +0
-0
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 355
Quoted 4 lines
grub-core/kern/i386/xen/pvh.c +1
-0@@ -352,6 +352,22 @@ grub_xen_setup_pvh (void)grub_xen_mm_init_regions (); grub_rsdp_addr = pvh_start_info->rsdp_paddr; +int xen_cmdline_fits =0 ;
We have bool type in the GRUB.
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 356
Quoted 1 line
grub-core/kern/i386/xen/pvh.c +1
-0+char *xen_cmdline = (char *) pvh_start_info->cmdline_paddr;
const char?
And please add an empty line here.
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 357
Quoted 1 line
grub-core/kern/i386/xen/pvh.c +1
-0+for (int i =0 ; i <1024 ; i++)
1024? Where this number come from? And I suggest using constants where possible.
Additionally, please do not mix code with variable definitions. Define all variables at the beginning of a function or block.
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 365
Quoted 8 lines
grub-core/kern/i386/xen/pvh.c +8
-0+ { +if (xen_cmdline[i] =='\0' ) + { + xen_cmdline_fits =1 ; +break ; + } + } +if (xen_cmdline_fits ==1 )
Probably this and xen_cmdline_fits variable is not needed if you merge the code below with the code above.
Quoted 61 lines
grub-core/kern/i386/xen/pvh.c +5
-0+ { +grub_strncpy ((char *) grub_xen_start_page_addr->cmd_line, + (char *) pvh_start_info->cmdline_paddr, +MAX_GUEST_CMDLINE ); + } } grub_err_tgrub-core/kern/main.c +12
-0@@ -40,6 +40,10 @@static bool cli_disabled = false;static bool cli_need_auth = false; +#if defined (GRUB_MACHINE_XEN ) || defined (GRUB_MACHINE_XEN_PVH ) +#include <grub/xen.h> +#endif +grub_addr_t grub_modules_get_end (void ) {@@ -351,6 +355,14 @@ grub_main (void)grub_env_export ("root" );grub_env_export ("prefix" ); +/* +* Parse command line parameters from Xen and export them as environment +* variables +*/ +#if defined (GRUB_MACHINE_XEN ) || defined (GRUB_MACHINE_XEN_PVH ) +grub_parse_xen_cmdline (); +#endif +/* Reclaim space used for modules. */ reclaim_module_space ();grub-core/kern/xen/cmdline.c new file +17
-0@@ -0,0 +1,270 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2025 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/
grub-core/kern/xen/cmdline.c line 27
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ +#include <grub/env.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/xen.h> + +#define PARSER_HIT_BACKSLASH 0x1 +#define PARSER_IN_SINGLE_QUOTES 0x2 +#define PARSER_IN_DOUBLE_QUOTES 0x4 +#define PARSER_BASE_WORD_LEN 16
I think it could be enum type.
grub-core/kern/xen/cmdline.c line 37
Quoted 9 lines
grub-core/kern/xen/cmdline.c new file +9
-0+grub_size_t word_list_len; +char **word_list; +grub_size_t current_word_len; +grub_size_t current_word_pos; +char *current_word; +char current_char; +grub_size_t param_dict_len; +char **param_keys; +char **param_vals;
Probably all of this should be static.
grub-core/kern/xen/cmdline.c line 42
Quoted 4 lines
grub-core/kern/xen/cmdline.c new file +4
-0+static bool +append_char_to_word (bool allow_null) +{ +if (!(current_char >=0x20 ) || !(current_char <=0x7E ))
I know what you mean but please comment why you accept only limited set of characters.
grub-core/kern/xen/cmdline.c line 42
Quoted 2 lines
grub-core/kern/xen/cmdline.c new file +2
-0+ { +if (current_char !='\0' || !allow_null)
allow_null == false please here and in other places too...
grub-core/kern/xen/cmdline.c line 47
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ { +return false; + }
Please drop unneeded curly brackets...
grub-core/kern/xen/cmdline.c line 52
Quoted 5 lines
grub-core/kern/xen/cmdline.c new file +5
-0+ } +if (current_word_pos == current_word_len) + { + current_word_len *=2 ; + current_word =grub_realloc (current_word, current_word_len);
grub_realloc() may fail...
grub-core/kern/xen/cmdline.c line 63
Quoted 11 lines
grub-core/kern/xen/cmdline.c new file +11
-0+ } + + current_word[current_word_pos] = current_char; + current_word_pos++; +return true; +} + +static void +append_word_to_list (void ) +{ +for (grub_size_t i =0 ; i < current_word_pos; i++)
Again, please do not mix variable definitions with the code.
grub-core/kern/xen/cmdline.c line 72
Quoted 9 lines
grub-core/kern/xen/cmdline.c new file +9
-0+ { +if (current_word[i] <=0x1F || current_word[i] >=0x7F ) + { +grub_free (current_word); + goto reset_word; + } + } + current_char ='\0' ; +if (!append_char_to_word (true))
append_char_to_word (true) == false
grub-core/kern/xen/cmdline.c line 72
Quoted 2 lines
grub-core/kern/xen/cmdline.c new file +2
-0+ { +grub_error (GRUB_ERR_BUG ,N_ ("couldn't append null terminator to word during Xen cmdline parsing" ));
Please drop N_() from here...
grub-core/kern/xen/cmdline.c line 79
Quoted 5 lines
grub-core/kern/xen/cmdline.c new file +5
-0+grub_print_error (); +grub_exit (); + } + current_word_len =grub_strlen (current_word) +1 ; + current_word =grub_realloc (current_word, current_word_len);
Please check for errors when you alloc/realloc memory.
grub-core/kern/xen/cmdline.c line 81
Quoted 2 lines
grub-core/kern/xen/cmdline.c new file +2
-0+ word_list_len++; + word_list =grub_realloc (word_list, word_list_len *sizeof (char *));
Ditto.
grub-core/kern/xen/cmdline.c line 84
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ word_list[word_list_len -1 ] = current_word; + +reset_word:
Missing space before label.
grub-core/kern/xen/cmdline.c line 87
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ current_word_len =PARSER_BASE_WORD_LEN ; + current_word_pos =0 ; + current_word =grub_malloc (current_word_len);
Again, missing error checks are unacceptable.
grub-core/kern/xen/cmdline.c line 90
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+} + +int
s/int/bool/
grub-core/kern/xen/cmdline.c line 100
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+check_key_is_safe (char *key, grub_size_t len) +{ +/* +* Ensure only a-z, A-Z, and _ are allowed. +*/ +for (grub_size_t i =0 ; i < len; i++) + { +if (! ((key[i] >='A' && key[i] <='Z' ) + || (key[i] >='a' && key[i] <='z' ) + || (key[i] =='_' ) ) )
This "if" begs for comment.
grub-core/kern/xen/cmdline.c line 103
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ { +return 0 ; + }
Redundant curly braces.
grub-core/kern/xen/cmdline.c line 111
Quoted 8 lines
grub-core/kern/xen/cmdline.c new file +8
-0+ } +return 1 ; +} + +void +grub_parse_xen_cmdline (void ) +{ + word_list =grub_malloc (0 );
word_list = NULL;
grub-core/kern/xen/cmdline.c line 114
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ current_word_len =PARSER_BASE_WORD_LEN ; + current_word =grub_malloc (current_word_len); + param_keys =grub_malloc (0 );
Ditto.
grub-core/kern/xen/cmdline.c line 115
Quoted 1 line
grub-core/kern/xen/cmdline.c new file +1
-0+ param_vals =grub_malloc (0 );
Ditto.
And why these variables have to be global?
grub-core/kern/xen/cmdline.c line 117
Quoted 1 line
grub-core/kern/xen/cmdline.c new file +1
-0+grub_uint8_t parse_flags =0 ;
Please use enum type instead of grub_uint8_t.
grub-core/kern/xen/cmdline.c line 118
Quoted 1 line
grub-core/kern/xen/cmdline.c new file +1
-0+char *cmdline = (char *)grub_xen_start_page_addr->cmd_line;
const char?
And missing space after ")".
grub-core/kern/xen/cmdline.c line 120
Quoted 1 line
grub-core/kern/xen/cmdline.c new file +1
-0+for (grub_size_t i =0 ; i <grub_strlen (cmdline); i++)
Is cmdline always NUL terminated? Should not we impose a (sensible) command line length limit in the GRUB code?
grub-core/kern/xen/cmdline.c line 130
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ { + current_char = cmdline[i]; + +if (parse_flags &PARSER_HIT_BACKSLASH ) + { + parse_flags ^=PARSER_HIT_BACKSLASH ; +if (!append_char_to_word (false)) + { + goto cleanup; + }
Please reduce curly braces to the minimum required...
grub-core/kern/xen/cmdline.c line 134
Quoted 4 lines
grub-core/kern/xen/cmdline.c new file +4
-0+continue ; + } + +if (current_char =='\\' )
I think parser code should have comments with explanations why we treat characters in a given way...
grub-core/kern/xen/cmdline.c line 140
Quoted 6 lines
grub-core/kern/xen/cmdline.c new file +6
-0+ { + parse_flags ^=PARSER_HIT_BACKSLASH ; +continue ; + } + +if (current_char =='\'' )
Would not switch/case be better here?
Quoted 5 lines
grub-core/kern/xen/cmdline.c new file +5
-0+ { +if (parse_flags &PARSER_IN_DOUBLE_QUOTES ) + { +if (!append_char_to_word (false)) + {
grub-core/kern/xen/cmdline.c line 155
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ goto cleanup; + } +continue ; + } + + parse_flags ^=PARSER_IN_SINGLE_QUOTES ; +continue ; + } + +if (current_char =='"' )
Ditto and below/everywhere here...
Quoted 33 lines
grub-core/kern/xen/cmdline.c new file +33
-0+ { +if (parse_flags &PARSER_IN_SINGLE_QUOTES ) + { +if (!append_char_to_word (false)) + { + goto cleanup; + } +continue ; + } + + parse_flags ^=PARSER_IN_DOUBLE_QUOTES ; +continue ; + } + +if (current_char ==' ' ) + { +if (parse_flags &PARSER_IN_SINGLE_QUOTES + || parse_flags &PARSER_IN_DOUBLE_QUOTES ) + { +if (!append_char_to_word (false)) + { + goto cleanup; + } +continue ; + } + +append_word_to_list (); +continue ; + } + +if (!append_char_to_word (false)) + { + goto cleanup;
grub-core/kern/xen/cmdline.c line 198
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ } + } + +if (current_word_pos >0 ) + { +append_word_to_list (); + } + + param_keys =grub_realloc (param_keys, word_list_len *sizeof (char *)); + param_vals =grub_realloc (param_vals, word_list_len *sizeof (char *));
Again, missing error checks. This kind of code has to be fixed everywhere.
grub-core/kern/xen/cmdline.c line 203
Quoted 5 lines
grub-core/kern/xen/cmdline.c new file +5
-0+for (grub_size_t i =0 ; i < word_list_len; i++) + { + current_word = word_list[i]; + current_word_len =grub_strlen (current_word) +1 ; +char *current_word_eq_ptr =grub_strchr (current_word,'=' );
Missing empty line.
grub-core/kern/xen/cmdline.c line 209
Quoted 6 lines
grub-core/kern/xen/cmdline.c new file +6
-0+if (current_word_eq_ptr) + { +grub_size_t eq_idx + = (grub_size_t)(current_word_eq_ptr - current_word); +grub_size_t pre_eq_len + = current_word_len - (current_word_len - eq_idx);
I am OK with lines (a bit) longer than 80 characters.
Quoted 42 lines
grub-core/kern/xen/cmdline.c new file +42
-0+grub_size_t post_eq_len = current_word_len - eq_idx -1 ; +if (check_key_is_safe (current_word, pre_eq_len)) + { + param_keys[param_dict_len] =grub_malloc (pre_eq_len +1 ); + param_vals[param_dict_len] =grub_malloc (post_eq_len +1 ); +grub_strncpy (param_keys[param_dict_len], + current_word, pre_eq_len); +grub_strncpy (param_vals[param_dict_len], + current_word + pre_eq_len +1 , post_eq_len); + param_keys[param_dict_len][pre_eq_len] ='\0' ; + param_vals[param_dict_len][post_eq_len] ='\0' ; + param_dict_len++; + } + } +else + { +if (check_key_is_safe (current_word, current_word_len -1 )) + { + param_keys[param_dict_len] =grub_malloc (current_word_len +1 ); + param_vals[param_dict_len] =grub_malloc (2 ); +grub_strncpy (param_keys[param_dict_len], + current_word, current_word_len); + param_keys[param_dict_len][current_word_len] ='\0' ; +grub_strcpy (param_vals[param_dict_len],"1\0" ); + param_dict_len++; + } + } + } + +for (grub_size_t i =0 ; i < param_dict_len; i++) + { +/* +* Find keys that start with "xen_grub_env_" and export them +* as environment variables. +*/ +if (grub_strlen (param_keys[i]) < (sizeof ("xen_grub_env_" ) -1 )) + { +continue ; + } +if (grub_strncmp (param_keys[i], +"xen_grub_env_" , +sizeof ("xen_grub_env_" ) -1 ) !=0 )
grub-core/kern/xen/cmdline.c line 261
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ { +continue ; + } +grub_env_set (param_keys[i], param_vals[i]); +grub_env_export (param_keys[i]); +grub_free (param_keys[i]); +grub_free (param_vals[i]); + } + +cleanup:
Missing space before label.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 1 line · On Wed, 28 May 2025 17:12:19 +0200 Daniel Kiper <[email protected]> wrote:
First of all, next time please add Xen-devel ML to recipients too...
Apologies, didn't realize that list would be appropriate. I will add them in the next version of the patch.
Quoted 55 lines · Aaron Rainbolt
Quoted 3 lines
Enable GRUB to parse the Xen command line for parameters, and expose certain of those parameters to the GRUB config file (or rescue shell) as environment variables.
Please make the commit message more verbose, i.e., explain why this feature it is needed, how it works, etc.
Quoted 7 lines
--- grub-core/Makefile.core.def | 2 + grub-core/kern/i386/xen/pvh.c | 16 ++ grub-core/kern/main.c | 12 ++ grub-core/kern/xen/cmdline.c | 270++++++++++++++++++++++++++++++++++
include/grub/xen.h|
2 +I think this feature should have a description in the GRUB docs.
Quoted 28 lines
5 files changed, 302 insertions(+) create mode 100644 grub-core/kern/xen/cmdline.c(quoted diff) +0
-0b/grub-core/Makefile.core.def index
f70e02e69..79e681a7b100644grub-core/Makefile.core.def +2
-0@@ -248,6 +248,7 @@ kernel = { xen = term/xen/console.c; xen = disk/xen/xendisk.c; xen = commands/boot.c; + xen = kern/xen/cmdline.c; i386_xen_pvh = commands/boot.c; i386_xen_pvh = disk/xen/xendisk.c;@@ -255,6 +256,7 @@ kernel = { i386_xen_pvh = kern/i386/xen/tsc.c; i386_xen_pvh = kern/i386/xen/pvh.c; i386_xen_pvh = kern/xen/init.c; + i386_xen_pvh = kern/xen/cmdline.c; i386_xen_pvh = term/xen/console.c; ia64_efi = kern/ia64/efi/startup.S;(quoted diff) +0
-0
b/grub-core/kern/i386/xen/pvh.cindex91fbca859..9fb048082100644grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 355
Quoted 4 lines
grub-core/kern/i386/xen/pvh.c +1
-0@@ -352,6 +352,22 @@ grub_xen_setup_pvh (void)grub_xen_mm_init_regions (); grub_rsdp_addr = pvh_start_info->rsdp_paddr; +int xen_cmdline_fits =0 ;We have bool type in the GRUB.
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 356
Quoted 1 line
grub-core/kern/i386/xen/pvh.c +1
-0+char *xen_cmdline = (char *) pvh_start_info->cmdline_paddr;
Quoted 8 lines
const char?
And please add an empty line here.
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 357
Quoted 1 line
grub-core/kern/i386/xen/pvh.c +1
-0+for (int i =0 ; i <1024 ; i++)1024? Where this number come from? And I suggest using constants where possible.
1024 is the maximum Xen command line length specified by include/xen/xen.h. I think I can just use the MAX_GUEST_CMDLINE define set by that file here, is that acceptable?
Quoted 2 lines
Additionally, please do not mix code with variable definitions. Define all variables at the beginning of a function or block.
Will do, but this should probably be added to the coding style documentation at some point in the future. (It looks like a lot of the issues with this patch are coding style issues that I could have avoided had the details been documented.)
Quoted 110 lines
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 365
Quoted 8 lines
grub-core/kern/i386/xen/pvh.c +8
-0+ { +if (xen_cmdline[i] =='\0' ) + { + xen_cmdline_fits =1 ; +break ; + } + } +if (xen_cmdline_fits ==1 )Probably this and
xen_cmdline_fitsvariable is not needed if you merge the code below with the code above.Quoted 65 lines
grub-core/kern/i386/xen/pvh.c +5
-0+ { +grub_strncpy ((char *) grub_xen_start_page_addr->cmd_line, + (char *) pvh_start_info->cmdline_paddr, +MAX_GUEST_CMDLINE ); + } } grub_err_tgrub-core/kern/main.c +6
-0@@ -40,6 +40,10 @@static bool cli_disabled = false;static bool cli_need_auth = false; +#if defined (GRUB_MACHINE_XEN ) || defined (GRUB_MACHINE_XEN_PVH ) +#include <grub/xen.h> +#endif +grub_addr_t grub_modules_get_end (void ) {@@ -351,6 +355,14 @@ grub_main (void)grub_env_export ("root" );grub_env_export ("prefix" ); +/* +* Parse command line parameters from Xen and export them as environment
grub-core/kern/main.c +6
-0+* variables +*/ +#if defined (GRUB_MACHINE_XEN ) || defined (GRUB_MACHINE_XEN_PVH ) +grub_parse_xen_cmdline (); +#endif +/* Reclaim space used for modules. */ reclaim_module_space ();(quoted diff) +0
-0
b/grub-core/kern/xen/cmdline.cnew file mode 100644 index 000000000..0084f3e77grub-core/kern/xen/cmdline.c new file +6
-0@@ -0,0 +1,270 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2025 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *+ * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details.+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */grub-core/kern/xen/cmdline.c line 27
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ +#include <grub/env.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/xen.h> + +#define PARSER_HIT_BACKSLASH 0x1 +#define PARSER_IN_SINGLE_QUOTES 0x2 +#define PARSER_IN_DOUBLE_QUOTES 0x4 +#define PARSER_BASE_WORD_LEN 16I think it could be enum type.
grub-core/kern/xen/cmdline.c line 37
Quoted 9 lines
grub-core/kern/xen/cmdline.c new file +9
-0+grub_size_t word_list_len; +char **word_list; +grub_size_t current_word_len; +grub_size_t current_word_pos; +char *current_word; +char current_char; +grub_size_t param_dict_len; +char **param_keys; +char **param_vals;Probably all of this should be static.
grub-core/kern/xen/cmdline.c line 42
Quoted 4 lines
grub-core/kern/xen/cmdline.c new file +4
-0+static bool +append_char_to_word (bool allow_null) +{ +if (!(current_char >=0x20 ) || !(current_char <=0x7E ))I know what you mean but please comment why you accept only limited set of characters.
Quoted 4 lines
grub-core/kern/xen/cmdline.c
Quoted 2 lines
grub-core/kern/xen/cmdline.c new file +2
-0+ { +if (current_char !='\0' || !allow_null)
allow_null== false please here and in other places too...
+1, also might be useful to document.
Quoted 5 lines
grub-core/kern/xen/cmdline.c line 47
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ { +return false; + }Please drop unneeded curly brackets...
+1, also might be useful to document.
Quoted 8 lines
Quoted 6 lines
grub-core/kern/xen/cmdline.c new file +5
-0+ } +if (current_word_pos == current_word_len) + { + current_word_len *=2 ; + current_word = grub_realloc (current_word,
current_word_len);
grub_realloc()may fail...
I'm embarrassed I forgot to error-check every single memory allocation, I'm too used to languages that immediately crash the program if memory runs out and forget that C will just keep moving forward with a failed allocation if you don't handle it yourself. Thanks for your patience, I'll make sure all these are resolved in the next iteration of the patch.
Quoted 24 lines
grub-core/kern/xen/cmdline.c line 63
Quoted 11 lines
grub-core/kern/xen/cmdline.c new file +11
-0+ } + + current_word[current_word_pos] = current_char; + current_word_pos++; +return true; +} + +static void +append_word_to_list (void ) +{ +for (grub_size_t i =0 ; i < current_word_pos; i++)Again, please do not mix variable definitions with the code.
grub-core/kern/xen/cmdline.c line 72
Quoted 9 lines
grub-core/kern/xen/cmdline.c new file +9
-0+ { +if (current_word[i] <=0x1F || current_word[i] >=0x7F ) + { +grub_free (current_word); + goto reset_word; + } + } + current_char ='\0' ; +if (!append_char_to_word (true))
Quoted 7 lines
append_char_to_word(true) == falseQuoted 3 lines
grub-core/kern/xen/cmdline.c new file +2
-0+ { + grub_error (GRUB_ERR_BUG, N_("couldn't append nullterminator to word during Xen cmdline parsing"));
Please drop
N_()from here...
Will do, when I wrote this I thought this message needed to be translatable, but in retrospect this should be unreachable code (barring an allocation failure), so if it ever is reached, having an exact string that can be grepped for easily will be valuable.
Quoted 12 lines
grub-core/kern/xen/cmdline.c line 79
Quoted 5 lines
grub-core/kern/xen/cmdline.c new file +5
-0+grub_print_error (); +grub_exit (); + } + current_word_len =grub_strlen (current_word) +1 ; + current_word =grub_realloc (current_word, current_word_len);Please check for errors when you alloc/realloc memory.
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +2
-0+ word_list_len++; + word_list = grub_realloc (word_list, word_list_len * sizeof(char *));
Quoted 7 lines
Ditto.
grub-core/kern/xen/cmdline.c line 84
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ word_list[word_list_len -1 ] = current_word; + +reset_word:Missing space before label.
Also would be good to document as a coding style requirement.
Quoted 12 lines
grub-core/kern/xen/cmdline.c line 87
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ current_word_len =PARSER_BASE_WORD_LEN ; + current_word_pos =0 ; + current_word =grub_malloc (current_word_len);Again, missing error checks are unacceptable.
grub-core/kern/xen/cmdline.c line 90
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+} + +int s/int/bool/
Quoted 12 lines
grub-core/kern/xen/cmdline.c line 100
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+check_key_is_safe (char *key, grub_size_t len) +{ +/* +* Ensure only a-z, A-Z, and _ are allowed. +*/ +for (grub_size_t i =0 ; i < len; i++) + { +if (! ((key[i] >='A' && key[i] <='Z' ) + || (key[i] >='a' && key[i] <='z' ) + || (key[i] =='_' ) ) )This "if" begs for comment.
The comment is above the "for" loop one line up. I can move it inside the loop if that's preferable.
Quoted 6 lines
grub-core/kern/xen/cmdline.c line 103
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ { +return 0 ; + }Redundant curly braces.
Quoted 10 lines
grub-core/kern/xen/cmdline.c line 111
Quoted 8 lines
grub-core/kern/xen/cmdline.c new file +8
-0+ } +return 1 ; +} + +void +grub_parse_xen_cmdline (void ) +{ + word_list =grub_malloc (0 );
word_list= NULL;
I was going off of the documentation in man 3 malloc, which states "if size is 0, then malloc() returns a unique pointer value that can later be successfully passed to free(). Looking closer at the memory management code, I see grub_free() can handle a null pointer, but wasn't aware of that when writing this, and was trying to avoid a memory management error since this and the other grub_malloc (0)'d variables are unconditionally grub_free`'d in the cleanup section.
Quoted 11 lines
grub-core/kern/xen/cmdline.c line 114
Quoted 3 lines
grub-core/kern/xen/cmdline.c new file +3
-0+ current_word_len =PARSER_BASE_WORD_LEN ; + current_word =grub_malloc (current_word_len); + param_keys =grub_malloc (0 );Ditto.
Quoted 1 line
+
param_vals=grub_malloc(0);Ditto.
And why these variables have to be global?
It looks like they don't have to be, I thought it was clearer to have them declared in the same place as the other command line parsing state variables though. I'll move them into the function where they are used.
Quoted 9 lines
Quoted 1 line
+
grub_uint8_tparse_flags= 0;Please use enum type instead of
grub_uint8_t.Quoted 1 line
+ char *cmdline = (char *)grub_xen_start_page_addr->cmd_line;
const char?
And missing space after ")".
Huh, looks like clang-format didn't catch that one for me... or I forgot to re-run it before submission, I don't remember now. In any event, will fix.
Quoted 4 lines
Quoted 1 line
+ for (
grub_size_ti = 0; i <grub_strlen(cmdline); i++)Is cmdline always NUL terminated? Should not we impose a (sensible) command line length limit in the GRUB code?
If GRUB is started in a PVH VM, grub_xen_setup_pvh will check the command line to ensure it is NUL-terminated before loading it into the grub_xen_start_page_addr->cmd_line. In a PV VM, this struct is populated by Xen itself and GRUB simply grabs a pointer to it that Xen passes it, so I guess in theory a Xen bug could result in this command line not being NUL-terminated. That would be a pretty severe bug on Xen's part, but I suppose it would be good to double-check just in case.
Quoted 21 lines
grub-core/kern/xen/cmdline.c line 130
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ { + current_char = cmdline[i]; + +if (parse_flags &PARSER_HIT_BACKSLASH ) + { + parse_flags ^=PARSER_HIT_BACKSLASH ; +if (!append_char_to_word (false)) + { + goto cleanup; + }Please reduce curly braces to the minimum required...
grub-core/kern/xen/cmdline.c line 134
Quoted 4 lines
grub-core/kern/xen/cmdline.c new file +4
-0+continue ; + } + +if (current_char =='\\' )I think parser code should have comments with explanations why we treat characters in a given way...
Quoted 8 lines
grub-core/kern/xen/cmdline.c line 140
Quoted 6 lines
grub-core/kern/xen/cmdline.c new file +6
-0+ { + parse_flags ^=PARSER_HIT_BACKSLASH ; +continue ; + } + +if (current_char =='\'' )Would not switch/case be better here?
Yes, looking at it now I agree it would be.
Quoted 146 lines
Quoted 5 lines
grub-core/kern/xen/cmdline.c new file +5
-0+ { +if (parse_flags &PARSER_IN_DOUBLE_QUOTES ) + { +if (!append_char_to_word (false)) + {grub-core/kern/xen/cmdline.c line 155
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ goto cleanup; + } +continue ; + } + + parse_flags ^=PARSER_IN_SINGLE_QUOTES ; +continue ; + } + +if (current_char =='"' )Ditto and below/everywhere here...
Quoted 42 lines
grub-core/kern/xen/cmdline.c new file +42
-0+ { +if (parse_flags &PARSER_IN_SINGLE_QUOTES ) + { +if (!append_char_to_word (false)) + { + goto cleanup; + } +continue ; + } + + parse_flags ^=PARSER_IN_DOUBLE_QUOTES ; +continue ; + } + +if (current_char ==' ' ) + { +if (parse_flags &PARSER_IN_SINGLE_QUOTES + || parse_flags &PARSER_IN_DOUBLE_QUOTES ) + { +if (!append_char_to_word (false)) + { + goto cleanup; + } +continue ; + } + +append_word_to_list (); +continue ; + } + +if (!append_char_to_word (false)) + { + goto cleanup; + } + } + +if (current_word_pos >0 ) + { +append_word_to_list (); + } + + param_keys = grub_realloc (param_keys, word_list_len * sizeofQuoted 3 lines
(char *));
+param_vals=grub_realloc(param_vals,word_list_len* sizeof (char *));Again, missing error checks. This kind of code has to be fixed everywhere.
grub-core/kern/xen/cmdline.c line 203
Quoted 6 lines
grub-core/kern/xen/cmdline.c new file +5
-0+for (grub_size_t i =0 ; i < word_list_len; i++) + { + current_word = word_list[i]; + current_word_len =grub_strlen (current_word) +1 ; +char *current_word_eq_ptr =grub_strchr (current_word,'=' );Missing empty line.
grub-core/kern/xen/cmdline.c line 209
Quoted 6 lines
grub-core/kern/xen/cmdline.c new file +6
-0+if (current_word_eq_ptr) + { +grub_size_t eq_idx + = (grub_size_t)(current_word_eq_ptr - current_word); +grub_size_t pre_eq_len + = current_word_len - (current_word_len - eq_idx);I am OK with lines (a bit) longer than 80 characters.
Quoted 48 lines
grub-core/kern/xen/cmdline.c new file +6
-0+grub_size_t post_eq_len = current_word_len - eq_idx -1 ; +if (check_key_is_safe (current_word, pre_eq_len)) + { + param_keys[param_dict_len] =grub_malloc (pre_eq_len + 1); + param_vals[param_dict_len] = grub_malloc(
post_eq_len+ 1);grub-core/kern/xen/cmdline.c new file +4
-0+grub_strncpy (param_keys[param_dict_len], + current_word, pre_eq_len); + grub_strncpy (param_vals[param_dict_len], + current_word + pre_eq_len + 1,
post_eq_len);grub-core/kern/xen/cmdline.c new file +8
-0+ param_keys[param_dict_len][pre_eq_len] ='\0' ; + param_vals[param_dict_len][post_eq_len] ='\0' ; + param_dict_len++; + } + } +else + { + if (check_key_is_safe (current_word, current_word_len -1))
grub-core/kern/xen/cmdline.c new file +2
-0+ { + param_keys[param_dict_len] = grub_malloc(
current_word_len+ 1);grub-core/kern/xen/cmdline.c new file +23
-1+ param_vals[param_dict_len] =grub_malloc (2 ); +grub_strncpy (param_keys[param_dict_len], + current_word, current_word_len); + param_keys[param_dict_len][current_word_len] ='\0' ; +grub_strcpy (param_vals[param_dict_len],"1\0" ); + param_dict_len++; + } + } + } + +for (grub_size_t i =0 ; i < param_dict_len; i++) + { +/* +* Find keys that start with "xen_grub_env_" and export them +* as environment variables. +*/ + if (grub_strlen (param_keys[i]) < (sizeof ("xen_grub_env_") - 1)) + { + continue; + } + if (grub_strncmp (param_keys[i], + "xen_grub_env_", + sizeof ("xen_grub_env_") - 1) != 0)grub-core/kern/xen/cmdline.c line 261
Quoted 10 lines
grub-core/kern/xen/cmdline.c new file +10
-0+ { +continue ; + } +grub_env_set (param_keys[i], param_vals[i]); +grub_env_export (param_keys[i]); +grub_free (param_keys[i]); +grub_free (param_vals[i]); + } + +cleanup:Missing space before label.
Quoted 1 line
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 6 lines · Aaron Rainbolt
Quoted 1 line · On Wed, 28 May 2025 17:12:19 +0200 Daniel Kiper <[email protected]> wrote:
First of all, next time please add Xen-devel ML to recipients too...
Apologies, didn't realize that list would be appropriate. I will add them in the next version of the patch.
Thank you!
Quoted 59 lines
Quoted 59 lines · Aaron Rainbolt
Quoted 3 lines
Enable GRUB to parse the Xen command line for parameters, and expose certain of those parameters to the GRUB config file (or rescue shell) as environment variables.
Please make the commit message more verbose, i.e., explain why this feature it is needed, how it works, etc.
Quoted 7 lines
--- grub-core/Makefile.core.def | 2 + grub-core/kern/i386/xen/pvh.c | 16 ++ grub-core/kern/main.c | 12 ++ grub-core/kern/xen/cmdline.c | 270++++++++++++++++++++++++++++++++++
include/grub/xen.h|
2 +I think this feature should have a description in the GRUB docs.
Quoted 28 lines
5 files changed, 302 insertions(+) create mode 100644 grub-core/kern/xen/cmdline.c(quoted diff) +0
-0b/grub-core/Makefile.core.def index
f70e02e69..79e681a7b100644grub-core/Makefile.core.def +2
-0@@ -248,6 +248,7 @@ kernel = { xen = term/xen/console.c; xen = disk/xen/xendisk.c; xen = commands/boot.c; + xen = kern/xen/cmdline.c; i386_xen_pvh = commands/boot.c; i386_xen_pvh = disk/xen/xendisk.c;@@ -255,6 +256,7 @@ kernel = { i386_xen_pvh = kern/i386/xen/tsc.c; i386_xen_pvh = kern/i386/xen/pvh.c; i386_xen_pvh = kern/xen/init.c; + i386_xen_pvh = kern/xen/cmdline.c; i386_xen_pvh = term/xen/console.c; ia64_efi = kern/ia64/efi/startup.S;(quoted diff) +0
-0
b/grub-core/kern/i386/xen/pvh.cindex91fbca859..9fb048082100644grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 355
Quoted 4 lines
grub-core/kern/i386/xen/pvh.c +1
-0@@ -352,6 +352,22 @@ grub_xen_setup_pvh (void)grub_xen_mm_init_regions (); grub_rsdp_addr = pvh_start_info->rsdp_paddr; +int xen_cmdline_fits =0 ;We have bool type in the GRUB.
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 356
Quoted 1 line
grub-core/kern/i386/xen/pvh.c +1
-0+char *xen_cmdline = (char *) pvh_start_info->cmdline_paddr;const char?
And please add an empty line here.
Quoted 8 lines
Quoted 4 lines
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 357
Quoted 1 line
grub-core/kern/i386/xen/pvh.c +1
-0+for (int i =0 ; i <1024 ; i++)1024? Where this number come from? And I suggest using constants where possible.
1024 is the maximum Xen command line length specified by
include/xen/xen.h. I think I can just use theMAX_GUEST_CMDLINEdefine set by that file here, is that acceptable?
OK...
Quoted 7 lines
Quoted 2 lines
Additionally, please do not mix code with variable definitions. Define all variables at the beginning of a function or block.
Will do, but this should probably be added to the coding style documentation at some point in the future. (It looks like a lot of the issues with this patch are coding style issues that I could have avoided had the details been documented.)
Yeah, that is good point...
[...]
Quoted 11 lines
Quoted 6 lines
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 361
Quoted 4 lines
grub-core/kern/i386/xen/pvh.c +4
-0+void +grub_parse_xen_cmdline (void ) +{ + word_list =grub_malloc (0 );
word_list= NULL;I was going off of the documentation in
man 3 malloc, which states "if size is 0, thenmalloc()returns a unique pointer value that can later be successfully passed tofree(). Looking closer at the memory management code, I seegrub_free()` can handle a null pointer, but
POSIX free() accepts NULL too...
[...]
Quoted 5 lines
Quoted 4 lines
grub-core/kern/i386/xen/pvh.c grub_xen_setup_pvh (void) · line 362
Quoted 1 line
grub-core/kern/i386/xen/pvh.c +1
-0+for (grub_size_t i =0 ; i <grub_strlen (cmdline); i++)Is cmdline always NUL terminated? Should not we impose a (sensible) command line length limit in the GRUB code?
Quoted 8 lines
If GRUB is started in a PVH VM,
grub_xen_setup_pvhwill check the command line to ensure it is NUL-terminated before loading it into thegrub_xen_start_page_addr->cmd_line. In a PV VM, this struct is populated by Xen itself and GRUB simply grabs a pointer to it that Xen passes it, so I guess in theory a Xen bug could result in this command line not being NUL-terminated. That would be a pretty severe bug on Xen's part, but I suppose it would be good to double-check just in case.
Yep!
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
I'm unsure about exporting all random parameters to env namespace. PHP went down this road and result was bunch of bad vulnerabilities. Maybe instead of blindly exporting it have some kind of export or query command? Or at least add some prefix to exported vars to identify them as such?
Regards
Vladimir 'phcoder' Serbinenko
Quoted 49 lines · Aaron Rainbolt
The purpose of this patch is to allow the Xen hypervisor to pass extra data to GRUB in the form of a kernel command line, allowing the host to customize the boot process of the guest. The command line from Xen is parsed, and any variables within that start with the string
xen_grub_env_are exposed as environment variables. The grub.cfg script can then use those environment variables as it sees fit.The main reason for doing this is to allow implementing boot modes in Qubes OS while also using in-VM kernels. For more context on Qubes boot modes, see [1]. In order for this to work with in-VM kernels, it is necessary for dom0 to pass kernel parameters to the guest without modifying the guest's grub.cfg manually. This patch allows this to be done, by allowing dom0 to pass kernel parameters to GRUB, which then provides them to grub.cfg as an environment variable. The grub.cfg script within the VM can then append those variables to the kernel command line.
This is a significantly more robust version of the Xen command line parsing draft sent previously [2]. The patch has been tested with both PV and PVH virtual machines, using an otherwise unpatched GRUB source tree, building the patch on top of the tip of git master at the time of this writing (commit 4abac0a). My testing environment is a fully updated Arch Linux system with Xen built from the stable-4.20 branch.
Some changes from the initial draft sent previously:
* Only parameters starting with
xen_grub_env_are exposed as environment variables. This solves two of the previous patch's issues
- dom0 can no longer clobber arbitrary variables within grub.cfg (as it cannot define variables with names that don't start withxen_grub_env), and API breakage won't occur if in the future more features are added to GRUB that use the Xen-provided kernel command line.
* Testing has been done as described above.[1] https://github.com/QubesOS/qubes-linux-pvgrub2/pull/16
[2] https://lists.gnu.org/archive/html/grub-devel/2025-04/msg00050.htmlAaron Rainbolt (1):
Add Xen command line parsinggrub-core/Makefile.core.def | 2 + grub-core/kern/i386/xen/pvh.c | 16 ++ grub-core/kern/main.c | 12 ++ grub-core/kern/xen/cmdline.c | 270 ++++++++++++++++++++++++++++++++++ include/grub/xen.h | 2 + 5 files changed, 302 insertions(+) create mode 100644 grub-core/kern/xen/cmdline.c
Quoted 8 lines
--
2.49.0_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 5 lines · On Wed, 28 May 2025 18:19:20 +0300 "Vladimir 'phcoder' Serbinenko"…
I'm unsure about exporting all random parameters to env namespace. PHP went down this road and result was bunch of bad vulnerabilities. Maybe instead of blindly exporting it have some kind of export or query command? Or at least add some prefix to exported vars to identify them as such?
There is a prefix, xen_grub_env_. I documented it in the first paragraph of the patch cover letter and in a code comment. I'll document it in the commit message as well though.
Quoted 67 lines
Regards
Vladimir 'phcoder' SerbinenkoLe jeu. 24 avr. 2025, 05:47, Aaron Rainbolt <[email protected]> a écrit :
Quoted 61 lines
The purpose of this patch is to allow the Xen hypervisor to pass extra data to GRUB in the form of a kernel command line, allowing the host to customize the boot process of the guest. The command line from Xen is parsed, and any variables within that start with the string
xen_grub_env_are exposed as environment variables. The grub.cfg script can then use those environment variables as it sees fit.The main reason for doing this is to allow implementing boot modes in Qubes OS while also using in-VM kernels. For more context on Qubes boot modes, see [1]. In order for this to work with in-VM kernels, it is necessary for dom0 to pass kernel parameters to the guest without modifying the guest's grub.cfg manually. This patch allows this to be done, by allowing dom0 to pass kernel parameters to GRUB, which then provides them to grub.cfg as an environment variable. The grub.cfg script within the VM can then append those variables to the kernel command line.
This is a significantly more robust version of the Xen command line parsing draft sent previously [2]. The patch has been tested with both PV and PVH virtual machines, using an otherwise unpatched GRUB source tree, building the patch on top of the tip of git master at the time of this writing (commit 4abac0a). My testing environment is a fully updated Arch Linux system with Xen built from the stable-4.20 branch.
Some changes from the initial draft sent previously:
* Only parameters starting with
xen_grub_env_are exposed as environment variables. This solves two of the previous patch's
issues
- dom0 can no longer clobber arbitrary variables within grub.cfg
(as it cannot define variables with names that don't start with
xen_grub_env), and API breakage won't occur if in the future
more features are added to GRUB that use the Xen-provided kernel command line.
* Testing has been done as described above.[1] https://github.com/QubesOS/qubes-linux-pvgrub2/pull/16
[2]
https://lists.gnu.org/archive/html/grub-devel/2025-04/msg00050.htmlAaron Rainbolt (1):
Add Xen command line parsinggrub-core/Makefile.core.def | 2 + grub-core/kern/i386/xen/pvh.c | 16 ++ grub-core/kern/main.c | 12 ++ grub-core/kern/xen/cmdline.c | 270 ++++++++++++++++++++++++++++++++++ include/grub/xen.h | 2 + 5 files changed, 302 insertions(+) create mode 100644 grub-core/kern/xen/cmdline.c--
2.49.0_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel