Skip to content

Commit

Permalink
Merge pull request #273 from bcressey/grub-bli
Browse files Browse the repository at this point in the history
set LoaderTimeInitUSec and LoaderTimeExecUSec in GRUB
  • Loading branch information
bcressey authored Nov 25, 2024
2 parents f2b8e9a + 4c1c18e commit 50c1aeb
Show file tree
Hide file tree
Showing 8 changed files with 569 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
From 37cd5dfd522882e1d95c4205635f9b2763256707 Mon Sep 17 00:00:00 2001
From: Oliver Steffen <[email protected]>
Date: Fri, 26 May 2023 13:35:42 +0200
Subject: [PATCH] efi: Add grub_efi_set_variable_with_attributes()

Add a function to the EFI module that allows setting EFI variables
with specific attributes.

This is useful for marking variables as volatile, for example.

Signed-off-by: Oliver Steffen <[email protected]>
Reviewed-by: Daniel Kiper <[email protected]>
[bcressey:
- backport to 2.06
- avoid changes from bb4aa6e0 ("efi: Drop all uses of efi_call_XX() wrappers")]
Signed-off-by: Ben Cressey <[email protected]>

(cherry picked from commit 7e4da6fb2d03ea20f7e11efc496e2e6cf360048b)
---
grub-core/kern/efi/efi.c | 20 +++++++++++++-------
include/grub/efi/efi.h | 6 ++++++
2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index 3a4475c5c..ab9a53966 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -211,8 +211,8 @@ grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size,
}

grub_err_t
-grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
- void *data, grub_size_t datasize)
+grub_efi_set_variable_with_attributes (const char *var, const grub_efi_guid_t *guid,
+ void *data, grub_size_t datasize, grub_efi_uint32_t attributes)
{
grub_efi_status_t status;
grub_efi_runtime_services_t *r;
@@ -229,11 +229,7 @@ grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,

r = grub_efi_system_table->runtime_services;

- status = efi_call_5 (r->set_variable, var16, guid,
- (GRUB_EFI_VARIABLE_NON_VOLATILE
- | GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
- | GRUB_EFI_VARIABLE_RUNTIME_ACCESS),
- datasize, data);
+ status = efi_call_5 (r->set_variable, var16, guid, attributes, datasize, data);
grub_free (var16);
if (status == GRUB_EFI_SUCCESS)
return GRUB_ERR_NONE;
@@ -244,6 +240,16 @@ grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
}

+grub_err_t
+grub_efi_set_variable (const char *var, const grub_efi_guid_t *guid,
+ void *data, grub_size_t datasize)
+{
+ return grub_efi_set_variable_with_attributes (var, guid, data, datasize,
+ GRUB_EFI_VARIABLE_NON_VOLATILE
+ | GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
+ | GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
+}
+
grub_efi_status_t
grub_efi_get_variable_with_attributes (const char *var,
const grub_efi_guid_t *guid,
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index d580b6bd9..a08f9474d 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -128,6 +128,12 @@ grub_efi_status_t EXPORT_FUNC (grub_efi_get_variable) (const char *variable,
grub_size_t *datasize_out,
void **data_out);
grub_err_t
+EXPORT_FUNC (grub_efi_set_variable_with_attributes) (const char *var,
+ const grub_efi_guid_t *guid,
+ void *data,
+ grub_size_t datasize,
+ grub_efi_uint32_t attributes);
+grub_err_t
EXPORT_FUNC (grub_efi_set_variable) (const char *var,
const grub_efi_guid_t *guid,
void *data,
--
2.47.0

40 changes: 40 additions & 0 deletions packages/grub/0050-include-grub-types.h-Add-GRUB_SSIZE_MAX.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
From abacbb46f1a73e33a6b7644a8a8081244c43c28f Mon Sep 17 00:00:00 2001
From: Oliver Steffen <[email protected]>
Date: Fri, 26 May 2023 13:35:46 +0200
Subject: [PATCH] include/grub/types.h: Add GRUB_SSIZE_MAX

In the same way as GRUB_SIZE_MAX, add GRUB_SSIZE_MAX.

Signed-off-by: Oliver Steffen <[email protected]>
Reviewed-by: Daniel Kiper <[email protected]>
[bcressey: backport to 2.06]
Signed-off-by: Ben Cressey <[email protected]>

(cherry picked from commit 389d3dc835a37c42184d2fab978ccd902a2399f7)
---
include/grub/types.h | 2 ++
1 file changed, 2 insertions(+)

diff --git a/include/grub/types.h b/include/grub/types.h
index ba446d990..ebf672b46 100644
--- a/include/grub/types.h
+++ b/include/grub/types.h
@@ -122,6 +122,7 @@ typedef grub_uint64_t grub_size_t;
typedef grub_int64_t grub_ssize_t;

# define GRUB_SIZE_MAX 18446744073709551615UL
+# define GRUB_SSIZE_MAX 9223372036854775807L

# if GRUB_CPU_SIZEOF_LONG == 8
# define PRIxGRUB_SIZE "lx"
@@ -140,6 +141,7 @@ typedef grub_uint32_t grub_size_t;
typedef grub_int32_t grub_ssize_t;

# define GRUB_SIZE_MAX 4294967295UL
+# define GRUB_SSIZE_MAX 2147483647L

# define PRIxGRUB_SIZE "x"
# define PRIxGRUB_ADDR "x"
--
2.47.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
From 55011a89968539f9d54d3425dc7ab73a0812851e Mon Sep 17 00:00:00 2001
From: Oliver Steffen <[email protected]>
Date: Fri, 26 May 2023 13:35:47 +0200
Subject: [PATCH] kern/misc, kern/efi: Extract UTF-8 to UTF-16 code

Create a new function for UTF-8 to UTF-16 conversion called
grub_utf8_to_utf16_alloc() in the grub-code/kern/misc.c and replace
charset conversion code used in some places in the EFI code. It is
modeled after the grub_utf8_to_ucs4_alloc() like functions in
include/grub/charset.h. It can't live in include/grub/charset.h,
because it needs to be reachable from the kern/efi code.

Add a check for integer overflow and remove redundant NUL-termination.

Signed-off-by: Oliver Steffen <[email protected]>
Reviewed-by: Daniel Kiper <[email protected]>
[bcressey: backport to 2.06]
Signed-off-by: Ben Cressey <[email protected]>

(cherry picked from commit a0b16564ee2e8eb7f597926bf60c4de2d696cd66)
---
grub-core/kern/efi/efi.c | 21 ++++++---------------
grub-core/kern/misc.c | 32 ++++++++++++++++++++++++++++++++
include/grub/misc.h | 3 +++
3 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index ab9a53966..354343935 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -217,15 +217,11 @@ grub_efi_set_variable_with_attributes (const char *var, const grub_efi_guid_t *g
grub_efi_status_t status;
grub_efi_runtime_services_t *r;
grub_efi_char16_t *var16;
- grub_size_t len, len16;

- len = grub_strlen (var);
- len16 = len * GRUB_MAX_UTF16_PER_UTF8;
- var16 = grub_calloc (len16 + 1, sizeof (var16[0]));
- if (!var16)
+ grub_utf8_to_utf16_alloc (var, &var16, NULL);
+
+ if (var16 == NULL)
return grub_errno;
- len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
- var16[len16] = 0;

r = grub_efi_system_table->runtime_services;

@@ -262,18 +258,13 @@ grub_efi_get_variable_with_attributes (const char *var,
grub_efi_runtime_services_t *r;
grub_efi_char16_t *var16;
void *data;
- grub_size_t len, len16;

*data_out = NULL;
*datasize_out = 0;

- len = grub_strlen (var);
- len16 = len * GRUB_MAX_UTF16_PER_UTF8;
- var16 = grub_calloc (len16 + 1, sizeof (var16[0]));
- if (!var16)
- return GRUB_EFI_OUT_OF_RESOURCES;
- len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
- var16[len16] = 0;
+ grub_utf8_to_utf16_alloc (var, &var16, NULL);
+ if (var16 == NULL)
+ return grub_errno;

r = grub_efi_system_table->runtime_services;

diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
index a95d182ba..f9be124d9 100644
--- a/grub-core/kern/misc.c
+++ b/grub-core/kern/misc.c
@@ -28,6 +28,8 @@
#if DEBUG_WITH_TIMESTAMPS
#include <grub/time.h>
#endif
+#include <grub/types.h>
+#include <grub/charset.h>

static void
parse_printf_args (const char *fmt0, struct grub_printf_args *args,
@@ -1280,6 +1282,36 @@ grub_fatal (const char *fmt, ...)
grub_abort ();
}

+grub_ssize_t
+grub_utf8_to_utf16_alloc (const char *str8, grub_uint16_t **utf16_msg, grub_uint16_t **last_position)
+{
+ grub_size_t len;
+ grub_size_t len16;
+
+ len = grub_strlen (str8);
+
+ /* Check for integer overflow */
+ if (len > GRUB_SSIZE_MAX / GRUB_MAX_UTF16_PER_UTF8 - 1)
+ {
+ grub_error (GRUB_ERR_BAD_ARGUMENT, N_("string too long"));
+ *utf16_msg = NULL;
+ return -1;
+ }
+
+ len16 = len * GRUB_MAX_UTF16_PER_UTF8;
+
+ *utf16_msg = grub_calloc (len16 + 1, sizeof (*utf16_msg[0]));
+ if (*utf16_msg == NULL)
+ return -1;
+
+ len16 = grub_utf8_to_utf16 (*utf16_msg, len16, (grub_uint8_t *) str8, len, NULL);
+
+ if (last_position != NULL)
+ *last_position = *utf16_msg + len16;
+
+ return len16;
+}
+
#if BOOT_TIME_STATS

#include <grub/time.h>
diff --git a/include/grub/misc.h b/include/grub/misc.h
index a359b0dee..8716e486d 100644
--- a/include/grub/misc.h
+++ b/include/grub/misc.h
@@ -536,4 +536,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,

#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) - __builtin_clzll (n) - 1)

+grub_ssize_t
+EXPORT_FUNC(grub_utf8_to_utf16_alloc) (const char *str8, grub_uint16_t **utf16_msg, grub_uint16_t **last_position);
+
#endif /* ! GRUB_MISC_HEADER */
--
2.47.0

71 changes: 71 additions & 0 deletions packages/grub/0052-efi-Add-grub_efi_set_variable_to_string.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
From 28bf9df5e7146610fd1890ea5856c0c0a86dac1c Mon Sep 17 00:00:00 2001
From: Oliver Steffen <[email protected]>
Date: Fri, 26 May 2023 13:35:48 +0200
Subject: [PATCH] efi: Add grub_efi_set_variable_to_string()

Add a function that sets an EFI variable to a string value.
The string is converted from UTF-8 to UTF-16.

Signed-off-by: Oliver Steffen <[email protected]>
Reviewed-by: Daniel Kiper <[email protected]>
[bcressey:
- backport to 2.06
- avoid changes from 06edd40d ("guid: Unify GUID types")]
Signed-off-by: Ben Cressey <[email protected]>

(cherry picked from commit e83a88f6ea7f97d643387681fe044f45dcd732b9)
---
grub-core/kern/efi/efi.c | 22 ++++++++++++++++++++++
include/grub/efi/efi.h | 3 +++
2 files changed, 25 insertions(+)

diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index 354343935..a65ef27bd 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -297,6 +297,28 @@ grub_efi_get_variable_with_attributes (const char *var,
return status;
}

+grub_err_t
+grub_efi_set_variable_to_string (const char *name, const grub_efi_guid_t *guid,
+ const char *value, grub_efi_uint32_t attributes)
+{
+ grub_efi_char16_t *value_16;
+ grub_ssize_t len16;
+ grub_err_t status;
+
+ len16 = grub_utf8_to_utf16_alloc (value, &value_16, NULL);
+
+ if (len16 < 0)
+ return grub_errno;
+
+ status = grub_efi_set_variable_with_attributes (name, guid,
+ (void *) value_16, (len16 + 1) * sizeof (value_16[0]),
+ attributes);
+
+ grub_free (value_16);
+
+ return status;
+}
+
grub_efi_status_t
grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
grub_size_t *datasize_out, void **data_out)
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index a08f9474d..a82741ef2 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -138,6 +138,9 @@ EXPORT_FUNC (grub_efi_set_variable) (const char *var,
const grub_efi_guid_t *guid,
void *data,
grub_size_t datasize);
+grub_err_t
+EXPORT_FUNC (grub_efi_set_variable_to_string) (const char *name, const grub_efi_guid_t *guid,
+ const char *value, grub_efi_uint32_t attributes);
int
EXPORT_FUNC (grub_efi_compare_device_paths) (const grub_efi_device_path_t *dp1,
const grub_efi_device_path_t *dp2);
--
2.47.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
From ec2bbc9d87079cd080feaad6b7512624a52ee555 Mon Sep 17 00:00:00 2001
From: Ben Cressey <[email protected]>
Date: Thu, 14 Nov 2024 19:24:42 +0000
Subject: [PATCH] efi: add vendor GUID for Boot Loader Interface

Backports the relevant part of upstream commit e0fa7dc8 ("bli: Add a
module for the Boot Loader Interface").

Signed-off-by: Ben Cressey <[email protected]>
---
include/grub/efi/api.h | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
index 464842ba3..f301913e6 100644
--- a/include/grub/efi/api.h
+++ b/include/grub/efi/api.h
@@ -368,6 +368,11 @@
{ 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89 } \
}

+#define GRUB_EFI_VENDOR_BOOT_LOADER_INTERFACE_GUID \
+ { 0x4a67b082, 0x0a4c, 0x41cf, \
+ { 0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f } \
+ }
+
struct grub_efi_sal_system_table
{
grub_uint32_t signature;
--
2.47.0

Loading

0 comments on commit 50c1aeb

Please sign in to comment.