[PATCH] kern/efi/mm: try allocating in the lower 4GB first
On arm64 we can't restrict memory usage below 0x as we do on x86 because there is no guarantee memory in that range exists at all (an example is Apple Silicon). Instead, try to allocate in the lower 4GB range first and fall back to the entire available address space if that did not work. This fixes a bug on Snapdragon X Elite devices such as the Lenovo Thinkpad T14s which crashes when accessing memory above 0x. Signed-off-by: Tobias Heider --- For previous discussion see https://lists.gnu.org/archive/html/grub-devel/2025-04/msg00072.html In contrast to the previous patch this one affects all archs. It seems generally safe not to limit it to arm64 with the fallback to the old behaviour but I'd be interested to hear if there are any concerns. Tested on a Snapdragon X Elite and Macbook M2 Air. grub-core/kern/efi/mm.c | 16 +--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index df238b165..029d975dd 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -168,9 +168,19 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, void * grub_efi_allocate_any_pages (grub_efi_uintn_t pages) { - return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, - pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, - GRUB_EFI_LOADER_DATA); + void *ret; + + ret = grub_efi_allocate_pages_real (0x, + pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, + GRUB_EFI_LOADER_DATA); + if (ret == NULL) +{ + grub_errno = GRUB_ERR_NONE; + ret = grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, + pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, + GRUB_EFI_LOADER_DATA); +} + return ret; } void * -- 2.48.1 ___ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH] kern/efi/mm: try allocating in the lower 4GB first
How early is this bug? Do you see "Welcome to GRUB"? Does recompiling with fPIC help? Do you have memory map dump and paging table dump? Regards Vladimir 'phcoder' Serbinenko Le ven. 11 avr. 2025, 13:39, Tobias Heider a écrit : > On arm64 we can't restrict memory usage below 0x as we do > on x86 because there is no guarantee memory in that range exists > at all (an example is Apple Silicon). > Instead, try to allocate in the lower 4GB range first and fall > back to the entire available address space if that did not work. > > This fixes a bug on Snapdragon X Elite devices such as the Lenovo > Thinkpad T14s which crashes when accessing memory above 0x. > > Signed-off-by: Tobias Heider > --- > > For previous discussion see > https://lists.gnu.org/archive/html/grub-devel/2025-04/msg00072.html > > In contrast to the previous patch this one affects all archs. > It seems generally safe not to limit it to arm64 with the fallback > to the old behaviour but I'd be interested to hear if there are any > concerns. > Tested on a Snapdragon X Elite and Macbook M2 Air. > > grub-core/kern/efi/mm.c | 16 +--- > 1 file changed, 13 insertions(+), 3 deletions(-) > > diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c > index df238b165..029d975dd 100644 > --- a/grub-core/kern/efi/mm.c > +++ b/grub-core/kern/efi/mm.c > @@ -168,9 +168,19 @@ grub_efi_allocate_pages_real > (grub_efi_physical_address_t address, > void * > grub_efi_allocate_any_pages (grub_efi_uintn_t pages) > { > - return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, > - pages, > GRUB_EFI_ALLOCATE_MAX_ADDRESS, > - GRUB_EFI_LOADER_DATA); > + void *ret; > + > + ret = grub_efi_allocate_pages_real (0x, > + pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, > + GRUB_EFI_LOADER_DATA); > + if (ret == NULL) > +{ > + grub_errno = GRUB_ERR_NONE; > + ret = grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, > + pages, > GRUB_EFI_ALLOCATE_MAX_ADDRESS, > + GRUB_EFI_LOADER_DATA); > +} > + return ret; > } > > void * > -- > 2.48.1 > > ___ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH] ia64: Disable optimizations using floating-point arithmetics
On Thu, Apr 10, 2025 at 06:34:26PM +0300, Vladimir 'phcoder' Serbinenko wrote: > Regards > Vladimir 'phcoder' Serbinenko > > Le jeu. 10 avr. 2025, 18:32, Daniel Kiper a écrit : > On Thu, Apr 10, 2025 at 01:45:37PM +0300, Vladimir Serbinenko wrote: > > They don't work in ski emulator and we don't really need them > > > > Signed-off-by: Vladimir Serbinenko > > --- > > configure.ac | 4 ++-- > > grub-core/Makefile.core.def | 2 +- > > 2 files changed, 3 insertions(+), 3 deletions(-) > > > > diff --git a/configure.ac b/configure.ac > > index 25f3ff932..ecb8bc666 100644 > > --- a/configure.ac > > +++ b/configure.ac > > @@ -933,9 +933,9 @@ if test x"$platform" != xemu ; then > > [grub_cv_target_cc_soft_float="- > march=rv64imac_zicsr_zifencei -mabi=lp64"], []) > > fi > > if test "x$target_cpu" = xia64; then > > - CFLAGS="$TARGET_CFLAGS -mno-inline-float-divide -mno- > inline-sqrt -Werror" > > + CFLAGS="$TARGET_CFLAGS -mno-inline-int-divide -mno-inline- > float-divide -mno-inline-sqrt -Werror" > > AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])], > > - [grub_cv_target_cc_soft_float="-mno-inline- > float-divide -mno-inline-sqrt"], []) > > + [grub_cv_target_cc_soft_float="-mno-inline- > int-divide -mno-inline-float-divide -mno-inline-sqrt"], []) > > I think you are dealing with int instead of float. I suppose the > subject > has to be fixed. > -minline-int-divide replaced integer divisions with floating point operations. It looks there is no -minline-int-divide. Though there are -minline-int-divide-min-latency and -minline-int-divide-max-throughput. > So it does use for arithmetic despite its name So, I think it should be better explained in the commit message. Daniel ___ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel
[PATCH 1/2] Include function name on debug traces
Together with the line number, the debug trace with the function name provides a bit more context and could be useful when inspecting log. Signed-off-by: Leo Sandoval --- grub-core/kern/misc.c | 4 ++-- include/grub/misc.h | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c index 2b79223935..90c71d7cea 100644 --- a/grub-core/kern/misc.c +++ b/grub-core/kern/misc.c @@ -231,14 +231,14 @@ grub_debug_enabled (const char * condition) } void -grub_real_dprintf (const char *file, const int line, const char *condition, +grub_real_dprintf (const char *file, const char *function, const int line, const char *condition, const char *fmt, ...) { va_list args; if (grub_debug_enabled (condition)) { - grub_printf ("%s:%d:%s: ", file, line, condition); + grub_printf ("%s:%s:%d:%s: ", file, function, line, condition); va_start (args, fmt); grub_vprintf (fmt, args); va_end (args); diff --git a/include/grub/misc.h b/include/grub/misc.h index e087e7b3e8..881af5ca98 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -35,7 +35,7 @@ #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0])) #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; } -#define grub_dprintf(condition, ...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, __VA_ARGS__) +#define grub_dprintf(condition, ...) grub_real_dprintf(GRUB_FILE, __FUNCTION__, __LINE__, condition, __VA_ARGS__) void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n); char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src); @@ -410,9 +410,10 @@ grub_puts (const char *s) int EXPORT_FUNC(grub_puts_) (const char *s); int EXPORT_FUNC(grub_debug_enabled) (const char *condition); void EXPORT_FUNC(grub_real_dprintf) (const char *file, + const int function, const int line, const char *condition, - const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 4, 5))); + const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 5, 6))); int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2))); int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2))); int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args); -- 2.48.1 ___ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel
[PATCH 2/2] Prepend debug traces with absolute and relative timestamps
From: Renaud Métrich Debug traces with timestamps are useful to detect performance issues. To enable it, include '--with-debug-timestamps' argument on the configure step. Signed-off-by: Renaud Métrich Signed-off-by: Leo Sandoval --- config.h.in | 1 + configure.ac | 18 ++ grub-core/kern/misc.c | 21 + 3 files changed, 40 insertions(+) diff --git a/config.h.in b/config.h.in index 9b1d399718..e0e27a8a6a 100644 --- a/config.h.in +++ b/config.h.in @@ -16,6 +16,7 @@ /* Define to 1 to enable disk cache statistics. */ #define DISK_CACHE_STATS @DISK_CACHE_STATS@ #define BOOT_TIME_STATS @BOOT_TIME_STATS@ +#define DEBUG_WITH_TIMESTAMPS @DEBUG_WITH_TIMESTAMPS@ /* We don't need those. */ #define MINILZO_CFG_SKIP_LZO_PTR 1 diff --git a/configure.ac b/configure.ac index 83e3ddf908..34fb9a01ad 100644 --- a/configure.ac +++ b/configure.ac @@ -1657,6 +1657,17 @@ else fi AC_SUBST([BOOT_TIME_STATS]) +AC_ARG_WITH([debug-timestamps], + AS_HELP_STRING([--with-debug-timestamps], + [prepend debug traces with absolute and relative timestamps])) + +if test x$with_debug_timestamps = xyes; then + DEBUG_WITH_TIMESTAMPS=1 +else + DEBUG_WITH_TIMESTAMPS=0 +fi +AC_SUBST([DEBUG_WITH_TIMESTAMPS]) + AC_ARG_ENABLE([grub-emu-sdl2], [AS_HELP_STRING([--enable-grub-emu-sdl2], [build and install the `grub-emu' debugging utility with SDL2 support (default=guessed)])]) @@ -2245,6 +2256,7 @@ AM_CONDITIONAL([COND_APPLE_LINKER], [test x$TARGET_APPLE_LINKER = x1]) AM_CONDITIONAL([COND_ENABLE_EFIEMU], [test x$enable_efiemu = xyes]) AM_CONDITIONAL([COND_ENABLE_CACHE_STATS], [test x$DISK_CACHE_STATS = x1]) AM_CONDITIONAL([COND_ENABLE_BOOT_TIME_STATS], [test x$BOOT_TIME_STATS = x1]) +AM_CONDITIONAL([COND_DEBUG_WITH_TIMESTAMPS], [test x$DEBUG_WITH_TIMESTAMPS = x1]) AM_CONDITIONAL([COND_HAVE_CXX], [test x$HAVE_CXX = xyes]) @@ -2346,6 +2358,12 @@ else echo With boot time statistics: No fi +if [ x"$with_debug_timestamps" = xyes ]; then +echo Debug traces with timestamps: Yes +else +echo Debug traces with timestamps: No +fi + if [ x"$efiemu_excuse" = x ]; then echo efiemu runtime: Yes else diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c index 90c71d7cea..967c01588d 100644 --- a/grub-core/kern/misc.c +++ b/grub-core/kern/misc.c @@ -26,6 +26,9 @@ #include #include #include +#if DEBUG_WITH_TIMESTAMPS +#include +#endif union printf_arg { @@ -235,9 +238,27 @@ grub_real_dprintf (const char *file, const char *function, const int line, const const char *fmt, ...) { va_list args; +#if DEBUG_WITH_TIMESTAMPS + static long unsigned int last_time = 0; + static int last_had_cr = 1; +#endif if (grub_debug_enabled (condition)) { +#if DEBUG_WITH_TIMESTAMPS + /* Don't print timestamp if last printed message isn't terminated yet */ + if (last_had_cr) { +long unsigned int tmabs = (long unsigned int) grub_get_time_ms(); +long unsigned int tmrel = tmabs - last_time; +last_time = tmabs; +grub_printf ("%3lu.%03lus:%2lu.%03lus:", tmabs / 1000, tmabs % 1000, tmrel / 1000, tmrel % 1000); + } + if (fmt[grub_strlen(fmt)-1] == '\n') +last_had_cr = 1; + else +last_had_cr = 0; +#endif + grub_printf ("%s:%s:%d:%s: ", file, function, line, condition); va_start (args, fmt); grub_vprintf (fmt, args); -- 2.48.1 ___ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel
[PATCH v3 2/3] blsuki: Check for mounted /boot in emu
Irritatingly, BLS defines paths relatives to the mountpoint of the filesystem which contains its snippets, not / or any other fixed location. So grub2-emu needs to know whether /boot is a separate filesystem from / and conditionally prepend a path. Signed-off-by: Robbie Harwood Signed-off-by: Alec Brown --- grub-core/Makefile.core.def | 4 ++ grub-core/commands/blsuki.c | 92 ++--- grub-core/osdep/linux/getroot.c | 8 +++ grub-core/osdep/unix/getroot.c | 10 include/grub/emu/misc.h | 2 +- 5 files changed, 107 insertions(+), 9 deletions(-) diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index f3b776c0a..9a0e7bc55 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -367,6 +367,10 @@ kernel = { emu = kern/emu/cache_s.S; emu = kern/emu/hostdisk.c; emu = osdep/unix/hostdisk.c; + emu = osdep/relpath.c; + emu = osdep/getroot.c; + emu = osdep/unix/getroot.c; + emu = osdep/devmapper/getroot.c; emu = osdep/exec.c; extra_dist = osdep/unix/exec.c; emu = osdep/devmapper/hostdisk.c; diff --git a/grub-core/commands/blsuki.c b/grub-core/commands/blsuki.c index 0fb4f870a..7b7b3e0e4 100644 --- a/grub-core/commands/blsuki.c +++ b/grub-core/commands/blsuki.c @@ -32,6 +32,13 @@ #include #include +#ifdef GRUB_MACHINE_EMU +#include +#define GRUB_BOOT_DEVICE "/boot" +#else +#define GRUB_BOOT_DEVICE "" +#endif + GRUB_MOD_LICENSE ("GPLv3+"); #define GRUB_BLS_CONFIG_PATH "/loader/entries/" @@ -56,6 +63,40 @@ static grub_blsuki_entry_t *entries = NULL; #define FOR_BLSUKI_ENTRIES(var) FOR_LIST_ELEMENTS (var, entries) +#ifdef GRUB_MACHINE_EMU +/* + * Cache probing in blsuki_update_boot_device(). + */ +static int separate_boot = -1; +#endif + +/* + * BLS appears to make paths relative to the filesystem that snippets are + * on, not /. Attempt to cope. + */ +static char *blsuki_update_boot_device (char *tmp) +{ +#ifdef GRUB_MACHINE_EMU + char *ret = NULL; + + if (separate_boot != -1) +goto probed; + + separate_boot = 0; + + ret = grub_make_system_path_relative_to_its_root (GRUB_BOOT_DEVICE); + + if (ret != NULL && ret[0] == '\0') +separate_boot = 1; + + probed: + if (!separate_boot) +return tmp; +#endif + + return grub_stpcpy (tmp, GRUB_BOOT_DEVICE); +} + static grub_err_t blsuki_add_keyval (grub_blsuki_entry_t *entry, char *key, char *val) { @@ -561,7 +602,7 @@ bls_create_entry (grub_blsuki_entry_t *entry) goto finish; } - if (grub_add (sizeof ("linux "), grub_strlen (linux_path), &linux_size)) + if (grub_add (sizeof ("linux " GRUB_BOOT_DEVICE), grub_strlen (linux_path), &linux_size)) { grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected calculating linux buffer size"); goto finish; @@ -572,6 +613,7 @@ bls_create_entry (grub_blsuki_entry_t *entry) tmp = clinux; tmp = grub_stpcpy (tmp, "linux"); tmp = grub_stpcpy (tmp, " "); + tmp = blsuki_update_boot_device (tmp); tmp = grub_stpcpy (tmp, linux_path); /* Strip the ".conf" off the end before we make it our "id" field. */ @@ -655,7 +697,7 @@ bls_create_entry (grub_blsuki_entry_t *entry) for (i = 0; early_initrds != NULL && early_initrds[i] != NULL; i++) { - if (grub_add (initrd_size, sizeof (" "), &initrd_size) || + if (grub_add (initrd_size, sizeof (" " GRUB_BOOT_DEVICE), &initrd_size) || grub_add (initrd_size, grub_strlen (initrd_prefix), &initrd_size) || grub_add (initrd_size, grub_strlen (early_initrds[i]), &initrd_size) || grub_add (initrd_size, 1, &initrd_size)) @@ -667,7 +709,7 @@ bls_create_entry (grub_blsuki_entry_t *entry) for (i = 0; initrds != NULL && initrds[i] != NULL; i++) { - if (grub_add (initrd_size, sizeof (" "), &initrd_size) || + if (grub_add (initrd_size, sizeof (" " GRUB_BOOT_DEVICE), &initrd_size) || grub_add (initrd_size, grub_strlen (initrds[i]), &initrd_size) || grub_add (initrd_size, 1, &initrd_size)) { @@ -691,6 +733,7 @@ bls_create_entry (grub_blsuki_entry_t *entry) { grub_dprintf ("blsuki", "adding early initrd %s\n", early_initrds[i]); tmp = grub_stpcpy (tmp, " "); + tmp = blsuki_update_boot_device (tmp); tmp = grub_stpcpy (tmp, initrd_prefix); tmp = grub_stpcpy (tmp, early_initrds[i]); grub_free (early_initrds[i]); @@ -700,6 +743,7 @@ bls_create_entry (grub_blsuki_entry_t *entry) { grub_dprintf ("blsuki", "adding initrd %s\n", initrds[i]); tmp = grub_stpcpy (tmp, " "); + tmp = blsuki_update_boot_device (tmp); tmp = grub_stpcpy (tmp, initrds[i]); } tmp = grub_stpcpy (tmp, "\n"); @@ -718,7 +762,7 @@ bls_create_entry (grub_blsuki_entry_t *entry) } } - if (grub_add (sizeof ("devicetree "), grub_strlen (devicetree), &dt_size) || + if (
[PATCH v3 0/3] Add commands to load BLS and UKI files
v3: - Added --enable-fallback option to check the default directory if the --path option isn't able to find entries. - Added the function blsuki_set_find_entry_info() to help set the path and device of BLS and UKI entries. - Removed grub_strchr uses in uki_read_osrel(). - Converted the static variable "cmd_type" into a parameter and added enums. - Fixed improper handling of the output of grub_make_system_path_relative_to_its_root(). This patch set is introducing BootLoaderSpec support to upstream GRUB from Fedora GRUB. I've also added a uki command to load Unified Kernel Images since it shares similar code to loading BLS config files. Alec Brown Alec Brown (1): blsuki: Add uki command to load Unified Kernel Image entries Peter Jones (1): blsuki: Add blscfg command to parse Boot Loader Specification snippets Robbie Harwood (1): blsuki: Check for mounted /boot in emu Makefile.util.def | 16 +++ docs/grub.texi | 55 grub-core/Makefile.core.def | 14 ++ grub-core/commands/blsuki.c | 1519 +++ grub-core/commands/legacycfg.c |4 +- grub-core/commands/menuentry.c |8 +- grub-core/lib/vercmp.c | 317 + grub-core/normal/main.c |6 + grub-core/osdep/linux/getroot.c |8 ++ grub-core/osdep/unix/getroot.c | 10 ++ include/grub/emu/misc.h |2 +- include/grub/lib/vercmp.h | 35 + include/grub/menu.h | 17 +++ include/grub/normal.h |2 +- tests/vercmp_unit_test.c| 65 + 15 files changed, 2071 insertions(+), 7 deletions(-) ___ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel
[PATCH v3 1/3] blsuki: Add blscfg command to parse Boot Loader Specification snippets
The BootLoaderSpec (BLS) defines a scheme where different bootloaders can share a format for boot items and a configuration directory that accepts these common configurations as drop-in files. Signed-off-by: Peter Jones Signed-off-by: Javier Martinez Canillas Signed-off-by: Will Thompson Signed-off-by: Alec Brown --- Makefile.util.def | 16 + docs/grub.texi | 29 + grub-core/Makefile.core.def| 10 + grub-core/commands/blsuki.c| 1057 grub-core/commands/legacycfg.c |4 +- grub-core/commands/menuentry.c |8 +- grub-core/lib/vercmp.c | 317 ++ grub-core/normal/main.c|6 + include/grub/lib/vercmp.h | 35 ++ include/grub/menu.h| 15 + include/grub/normal.h |2 +- tests/vercmp_unit_test.c | 65 ++ 12 files changed, 1558 insertions(+), 6 deletions(-) create mode 100644 grub-core/commands/blsuki.c create mode 100644 grub-core/lib/vercmp.c create mode 100644 include/grub/lib/vercmp.h create mode 100644 tests/vercmp_unit_test.c diff --git a/Makefile.util.def b/Makefile.util.def index 038253b37..a911f2e0a 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -1373,6 +1373,22 @@ program = { ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; }; +program = { + testcase = native; + name = vercmp_unit_test; + common = tests/vercmp_unit_test.c; + common = tests/lib/unit_test.c; + common = grub-core/kern/list.c; + common = grub-core/kern/misc.c; + common = grub-core/tests/lib/test.c; + common = grub-core/lib/vercmp.c; + ldadd = libgrubmods.a; + ldadd = libgrubgcry.a; + ldadd = libgrubkern.a; + ldadd = grub-core/lib/gnulib/libgnu.a; + ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; +}; + program = { name = grub-menulst2cfg; mansection = 1; diff --git a/docs/grub.texi b/docs/grub.texi index d9b26fa36..ea7a13953 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -6417,6 +6417,7 @@ you forget a command, you can run the command @command{help} * background_image::Load background image for active terminal * badram:: Filter out bad regions of RAM * blocklist:: Print a block list +* blscfg:: Load Boot Loader Specification menu entries * boot::Start up your operating system * cat:: Show the contents of a file * clear:: Clear the screen @@ -6603,6 +6604,34 @@ Print a block list (@pxref{Block list syntax}) for @var{file}. @end deffn +@node blscfg +@subsection blscfg + +@deffn Command blscfg [@option{--path} dir] [@option{--enable-fallback}] [@option{--show-default}] [@option{--show-non-default}] [@option{--entry} file] +Load Boot Loader Specification entries into the GRUB menu. + +The @option{--path} option overrides the default path to the directory containing +the BLS entries. If this option isn't used, the default location is +/loader/entries in @code{$BOOT}. If no BLS entries are found, the +@option{--enable-fallback} option can be used to check for entries in the default +directory. + +The @option{--show-default} option allows the default boot entry to be added to the +GRUB menu from the BLS entries. + +The @option{--show-non-default} option allows non-default boot entries to be added to +the GRUB menu from the BLS entries. + +The @option{--entry} option allows specific boot entries to be added to the GRUB menu +from the BLS entries. + +The @option{--entry}, @option{--show-default}, and @option{--show-non-default} options +are used to filter which BLS entries are added to the GRUB menu. If none are +used, all entries in the default location or the location specified by @option{--path} +will be added to the GRUB menu. +@end deffn + + @node boot @subsection boot diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index f70e02e69..f3b776c0a 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -845,6 +845,16 @@ module = { common = commands/blocklist.c; }; +module = { + name = blsuki; + common = commands/blsuki.c; + common = lib/vercmp.c; + enable = powerpc_ieee1275; + enable = efi; + enable = i386_pc; + enable = emu; +}; + module = { name = boot; common = commands/boot.c; diff --git a/grub-core/commands/blsuki.c b/grub-core/commands/blsuki.c new file mode 100644 index 0..0fb4f870a --- /dev/null +++ b/grub-core/commands/blsuki.c @@ -0,0 +1,1057 @@ +/* + * 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 WARR
[PATCH v3 3/3] blsuki: Add uki command to load Unified Kernel Image entries
A Unified Kernel Image is a single UEFI PE file that combines a UEFI boot stub, a Linux kernel image, an initrd, and further resources. The uki command will locate where the uki file is and create a GRUB menu entry to load it. Signed-off-by: Alec Brown --- docs/grub.texi | 26 +++ grub-core/commands/blsuki.c | 438 +--- include/grub/menu.h | 2 + 3 files changed, 440 insertions(+), 26 deletions(-) diff --git a/docs/grub.texi b/docs/grub.texi index ea7a13953..df206dad3 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -6491,6 +6491,7 @@ you forget a command, you can run the command @command{help} * tpm2_key_protector_clear::Clear the TPM2 key protector * true::Do nothing, successfully * trust:: Add public key to list of trusted keys +* uki:: Load Unified Kernel Image menu entries * unset:: Unset an environment variable @comment * vbeinfo:: List available video modes * verify_detached:: Verify detached digital signature @@ -8166,6 +8167,31 @@ Unset the environment variable @var{envvar}. @end deffn +@node uki +@subsection uki + +@deffn Command uki [@option{--path} dir] [@option{--show-default}] [@option{--show-non-default}] [@option{--entry} file] +Load Unified Kernel Image entries into the GRUB menu. + +The @option{--path} option overrides the default path to the directory containing +the UKI entries. If this option isn't used, the default location is +/EFI/Linux in the EFI system partition. + +The @option{--show-default} option allows the default boot entry to be added to the +GRUB menu from the UKI entries. + +The @option{--show-non-default} option allows non-default boot entries to be added to +the GRUB menu from the UKI entries. + +The @option{--entry} option allows specific boot entries to be added to the GRUB menu +from the UKI entries. + +The @option{--entry}, @option{--show-default}, and @option{--show-non-default} options +are used to filter which UKI entries are added to the GRUB menu. If none are +used, all entries in the default location or the location specified by @option{--path} +will be added to the GRUB menu. +@end deffn + @ignore @node vbeinfo @subsection vbeinfo diff --git a/grub-core/commands/blsuki.c b/grub-core/commands/blsuki.c index 7b7b3e0e4..77f59b530 100644 --- a/grub-core/commands/blsuki.c +++ b/grub-core/commands/blsuki.c @@ -32,6 +32,12 @@ #include #include +#ifdef GRUB_MACHINE_EFI +#include +#include +#include +#endif + #ifdef GRUB_MACHINE_EMU #include #define GRUB_BOOT_DEVICE "/boot" @@ -42,6 +48,13 @@ GRUB_MOD_LICENSE ("GPLv3+"); #define GRUB_BLS_CONFIG_PATH "/loader/entries/" +#define GRUB_UKI_CONFIG_PATH "/EFI/Linux" + +enum + { +BLSUKI_BLS_CMD = 1, +BLSUKI_UKI_CMD = 2, + }; static const struct grub_arg_option bls_opt[] = { @@ -53,6 +66,18 @@ static const struct grub_arg_option bls_opt[] = {0, 0, 0, 0, 0, 0} }; +#ifdef GRUB_MACHINE_EFI +static const struct grub_arg_option uki_opt[] = + { +{"path", 'p', 0, N_("Specify path to find UKI entries."), N_("DIR"), ARG_TYPE_PATHNAME}, +{"enable-fallback", 'f', 0, "Fallback to the default BLS path if --path fails to find UKI entries.", 0, ARG_TYPE_NONE}, +{"show-default", 'd', 0, N_("Allow the default UKI entry to be added to the GRUB menu."), 0, ARG_TYPE_NONE}, +{"show-non-default", 'n', 0, N_("Allow the non-default UKI entries to be added to the GRUB menu."), 0, ARG_TYPE_NONE}, +{"entry", 'e', 0, N_("Allow specificUKII entries to be added to the GRUB menu."), N_("FILE"), ARG_TYPE_FILE}, +{0, 0, 0, 0, 0, 0} + }; +#endif + struct keyval { const char *key; @@ -291,10 +316,211 @@ bls_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) return err; } +#ifdef GRUB_MACHINE_EFI +static grub_err_t +uki_read_entry (grub_file_t f, grub_blsuki_entry_t *entry) +{ + struct grub_msdos_image_header *dos = NULL; + struct grub_pe_image_header *pe = NULL; + grub_off_t section_offset = 0; + struct grub_pe32_section_table *section = NULL; + struct grub_pe32_coff_header *coff_header = NULL; + char *val = NULL; + char *key = NULL; + const char *target[] = {".cmdline", ".osrel", ".linux", NULL}; + bool has_linux = false; + grub_err_t err = GRUB_ERR_NONE; + + dos = grub_zalloc (sizeof (*dos)); + if (dos == NULL) +return grub_errno; + if (grub_file_read (f, dos, sizeof (*dos)) < (grub_ssize_t) sizeof (*dos)) +{ + err = grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read UKI image header"); + goto fail; +} + if (dos->msdos_magic != GRUB_PE32_MAGIC) +{ + err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("plain image kernel not supported")); + goto fail; +} + + grub_dprintf ("blsuki", "PE/COFF header @ %08x\n", dos->pe_image_header_offset); + pe = grub_zalloc (sizeof (*pe)); + if (pe == NULL) +{ +
RE: [PATCH v2 3/3] blsuki: Add uki command to load Unified Kernel Image entries
On Tue, Apr 1, 2025 at 6:35 AM, Vladimir 'phcoder' Serbinenko wrote: > Le jeu. 27 mars 2025, 23:44, Alec Brown a écrit : > >> On Wed, Mar 26, 2025 at 5:43 AM, Vladimir 'phcoder' Serbinenko < >> phco...@gmail.com> wrote: >> >> >> >> >> >> >> >> +#ifdef GRUB_MACHINE_EFI >> >> +#include >> >> +#include >> >> +#include >> >> +#endif >> >> + >> >> >> > Can UKI work without EFI? I think of scenario of putting e.g. EFI disk >> into >> > coreboot or BIOS machine. >> >> No UKI only works EFI systems. >> > > Can GRUB parse image and get kernel and initrd out of it and load like > Linux? What prevents add doing so. This is not a problem for this patch per > se but I want to understand If we aren't utilizing the EFI stub in the UKI, we could parse the kernel and initrd from the section data and load it from there. In that case, I suppose it would be possible to load it for a coreboot or BIOS machine. > >> >> >> >> > What's the purpose of fallback? It's not what user/script has requested. >> It >> > needs to be at very least disableable >> >> The fallback code was from some of the old blscfg code I was working with >> and >> I added the UKI default directory. I'll add an option in case the user >> wants >> this behavior. >> >> > >> >> >> >> >> >> + } >> >> + else if (cmd_type == GRUB_UKI_CMD) >> >> + { >> >> +#ifdef GRUB_MACHINE_EFI >> >> + grub_efi_loaded_image_t *image; >> >> + image = grub_efi_get_loaded_image (grub_efi_image_handle); >> >> + devid = grub_efidisk_get_device_name (image->device_handle); >> >> +#endif >> >> >> > This uses grub image location. What about a scenario when booted from >> > external drive and I want to boot into install on primary disk? >> >> Maybe I might not understand this scenario very well, but would it be >> better >> to load the UKI directory using the "--path" option? This bit of code is >> trying to locate the EFI system partition of the default directory. I'm not >> entirely sure the best way to find the default directory if it were to be >> on >> a different drive. >> > Does --path disable default path scanning? In this version, it does not. But in the newest version, I've added an option to enable or disable a fallback that would scan for the default path. ___ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel