efi/libstub: Avoid UTF-16 conversion busywork
Subsystems (3)
v2 cover efi/libstub: Avoid UTF-16 conversion busywork
From: Ard Biesheuvel <[email protected]>
The EFI libstub performs some redundant conversions between UTF-16 and UTF-8 and back again, which includes dealing with surrogate pairs, which UEFI implementations themselves simply ignore.
So drop all the homegrown code, and use the existing UCS-2 (== UTF-16 without surrogate pairs) library code where conversion to UTF-8 is actually needed (the kernel command line).
The remaining handling involves the EFI console, which supports wide characters natively, so just use those directly.
Changes since v1 [0]:
- drop size limit from ucs2_strscpy() instead of just the WARN()
- suppress modinfo sections from ucs2_string when __DISABLE_EXPORTS is defined
- allow the input limit and max output size to be passed separately to ucs2_to_utf8()
- reimplement efi_convert_cmdline() to optimize the common case, and only process the input character by character if its size exceeds COMMAND_LINE_SIZE
- use memcpy() to avoid strscpy() semantics in handling of %ls
- incorporate Vincent's patch (which inspired this work) for completeness
[0] https://lore.kernel.org/all/[email protected]
Ard Biesheuvel (9):
x86/boot: Drop pointless re-implementation of panic() lib/ucs2_string: Drop arbitrary input size limit and associated WARN() lib/ucs2_string: Suppress modinfo when __DISABLE_EXPORTS is set lib/ucs2_string: Split out ucs2_as_utf8_l() taking a separate limit efi/libstub: Use ucs2_string library for UTF-16 to UTF-8 conversion efi/libstub: Avoid efi_puts() for compile time constant strings efi/libstub: Output UTF-16 directly from vsnprintf() efi/libstub: Add support for printing human readable GUIDs efi/libstub: Add efi_snprintf() to construct wide strings
Vincent Mailhol (1):
efi/libstub: add initial Boot Loader Interface support
arch/x86/boot/compressed/error.c | 19 --- arch/x86/boot/compressed/error.h | 1 - arch/x86/boot/compressed/mem.c | 2 +- drivers/firmware/efi/libstub/Makefile | 3 +- drivers/firmware/efi/libstub/bli.c | 87 ++++++++++++ drivers/firmware/efi/libstub/efi-stub-helper.c | 100 +++++-------- drivers/firmware/efi/libstub/efi-stub.c | 1 + drivers/firmware/efi/libstub/efistub.h | 8 +- drivers/firmware/efi/libstub/gop.c | 6 +- drivers/firmware/efi/libstub/printk.c | 95 ++----------- drivers/firmware/efi/libstub/vsprintf.c | 150 +++++++------------- drivers/firmware/efi/libstub/x86-stub.c | 1 + include/linux/efi.h | 22 +++ include/linux/ucs2_string.h | 11 +- lib/ucs2_string.c | 10 +- 15 files changed, 241 insertions(+), 275 deletions(-) create mode 100644 drivers/firmware/efi/libstub/bli.c
Signature · 1 line
2.55.0.1003.g10538fe699-goog
PATCH v2 1/10 x86/boot: Drop pointless re-implementation of panic()
From: Ard Biesheuvel <[email protected]>
The decompressor has its own implementation of panic(), which is based on the vsnprintf() routine provided by the EFI stub.
Relying on the EFI stub from code that does not execute in the context of the EFI boot services is a bad idea. It is also completely pointless in this case, given that the only user of this version of panic() only passes a compile time constant string, without any printf conversions.
So use error() instead of panic() in that case, and drop the panic() implementation entirely. This is needed so that the EFI stub's vsnprintf() can be modified in a manner that is incompatible with the expectations of this caller.
--- arch/x86/boot/compressed/error.c | 19 ------------------- arch/x86/boot/compressed/error.h | 1 - arch/x86/boot/compressed/mem.c | 2 +- 3 files changed, 1 insertion(+), 21 deletions(-)
arch/x86/boot/compressed/error.c +0 -19
@@ -22,22 +22,3 @@ void error(char *m)while (1 ) asm("hlt" ); } - -/* EFI libstub provides vsnprintf() */ -#ifdef CONFIG_EFI_STUB -void panic (const char *fmt, ...) -{ -static char buf[1024 ]; -va_list args; -int len; - -va_start (args, fmt); - len =vsnprintf (buf,sizeof (buf), fmt, args); -va_end (args); - -if (len && buf[len -1 ] =='\n' ) - buf[len -1 ] ='\0' ; - -error (buf); -} -#endif
arch/x86/boot/compressed/error.h +0 -1
@@ -6,6 +6,5 @@void warn (const char *m);void error (char *m)__noreturn ; -void panic (const char *fmt, ...)__noreturn __cold ;#endif /* BOOT_COMPRESSED_ERROR_H */
arch/x86/boot/compressed/mem.c +1 -1
@@ -37,7 +37,7 @@ void arch_accept_memory(phys_addr_t start, phys_addr_t end)/* Platform-specific memory-acceptance call goes here */ if (early_is_tdx_guest ()) {if (!tdx_accept_memory (start, end)) -panic ("TDX: Failed to accept memory\n" ); +error ("TDX: Failed to accept memory\n" ); }else if (early_is_sevsnp_guest ()) {snp_accept_memory (start, end); }else {
Signature · 1 line
2.55.0.1003.g10538fe699-goog
Borislav Petkov
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 27 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The decompressor has its own implementation of
panic(), which is based on thevsnprintf()routine provided by the EFI stub.Relying on the EFI stub from code that does not execute in the context of the EFI boot services is a bad idea. It is also completely pointless in this case, given that the only user of this version of
panic()only passes a compile time constant string, without any printf conversions.So use
error()instead ofpanic()in that case, and drop thepanic()implementation entirely. This is needed so that the EFI stub'svsnprintf()can be modified in a manner that is incompatible with the expectations of this caller.Signed-off-by Ard Biesheuvel <[email protected]>--- arch/x86/boot/compressed/error.c | 19 ------------------- arch/x86/boot/compressed/error.h | 1 - arch/x86/boot/compressed/mem.c | 2 +- 3 files changed, 1 insertion(+), 21 deletions(-)arch/x86/boot/compressed/error.c +0
-0
arch/x86/boot/compressed/error.c void error(char *m) · line 28
Quoted 7 lines
arch/x86/boot/compressed/error.c +0
-4@@ -22,22 +22,3 @@ void error(char *m)while (1 ) asm("hlt" ); } - -/* EFI libstub provides vsnprintf() */ -#ifdef CONFIG_EFI_STUB -void panic (const char *fmt, ...)
So this thing appeared magically in v8 of the TDX unaccepted memory patches and I don't think we questioned it back then.
v7's tdx_accept_memory() does error():
https://lore.kernel.org/all/[email protected]/
and v8 started doing panic():
+void tdx_accept_memory(phys_addr_t start, phys_addr_t end) +{
+ if (!tdx_enc_status_changed_phys(start, end, true))
+ panic("Accepting memory failed: %#llx-%#llx\n", start, end);
+}
https://lore.kernel.org/all/[email protected]/
and it switched to it being a vsnprintf() wrapper because it wanted to dump start and end perhaps.
But then it ended up dropping the params in v13 and landed upstream with a single string as an argument.
Anyway, adding Kiryl/Kirill for comment and leaving in the rest for reference.
Quoted 41 lines
arch/x86/boot/compressed/error.c +0
-15-{ -static char buf[1024 ]; -va_list args; -int len; - -va_start (args, fmt); - len =vsnprintf (buf,sizeof (buf), fmt, args); -va_end (args); - -if (len && buf[len -1 ] =='\n' ) - buf[len -1 ] ='\0' ; - -error (buf); -} -#endif arch/x86/boot/compressed/error.h +0
-1@@ -6,6 +6,5 @@void warn (const char *m);void error (char *m)__noreturn ; -void panic (const char *fmt, ...)__noreturn __cold ;#endif /* BOOT_COMPRESSED_ERROR_H */ arch/x86/boot/compressed/mem.c +1
-1@@ -37,7 +37,7 @@ void arch_accept_memory(phys_addr_t start, phys_addr_t end)/* Platform-specific memory-acceptance call goes here */ if (early_is_tdx_guest ()) {if (!tdx_accept_memory (start, end)) -panic ("TDX: Failed to accept memory\n" ); +error ("TDX: Failed to accept memory\n" ); }else if (early_is_sevsnp_guest ()) {snp_accept_memory (start, end); }else {Signature · 1 line
2.55.0.1003.g10538fe699-goog
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteArd Biesheuvel
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 53 lines · Borislav Petkov
Quoted 27 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The decompressor has its own implementation of
panic(), which is based on thevsnprintf()routine provided by the EFI stub.Relying on the EFI stub from code that does not execute in the context of the EFI boot services is a bad idea. It is also completely pointless in this case, given that the only user of this version of
panic()only passes a compile time constant string, without any printf conversions.So use
error()instead ofpanic()in that case, and drop thepanic()implementation entirely. This is needed so that the EFI stub'svsnprintf()can be modified in a manner that is incompatible with the expectations of this caller.Signed-off-by Ard Biesheuvel <[email protected]>--- arch/x86/boot/compressed/error.c | 19 ------------------- arch/x86/boot/compressed/error.h | 1 - arch/x86/boot/compressed/mem.c | 2 +- 3 files changed, 1 insertion(+), 21 deletions(-)arch/x86/boot/compressed/error.c +0
-0arch/x86/boot/compressed/error.c void error(char *m) · line 28
Quoted 7 lines
arch/x86/boot/compressed/error.c +0
-4@@ -22,22 +22,3 @@ void error(char *m)while (1 ) asm("hlt" ); } - -/* EFI libstub provides vsnprintf() */ -#ifdef CONFIG_EFI_STUB -void panic (const char *fmt, ...)So this thing appeared magically in v8 of the TDX unaccepted memory patches and I don't think we questioned it back then.
v7's
tdx_accept_memory()doeserror():https://lore.kernel.org/all/[email protected]/
and v8 started doing
panic():(quoted diff) +5
-0+void tdx_accept_memory(phys_addr_t start, phys_addr_t end) +{ + if (!tdx_enc_status_changed_phys(start, end, true)) + panic("Accepting memory failed: %#llx-%#llx\n", start, end); +}
Quoted 8 lines
and it switched to it being a
vsnprintf()wrapper because it wanted to dump start and end perhaps.But then it ended up dropping the params in v13 and landed upstream with a single string as an argument.
Anyway, adding Kiryl/Kirill for comment and leaving in the rest for reference.
Looking at that v13, it seems the panic() call was added to arch/x86/coco/tdx/tdx-shared.c, which was shared between the kernel proper and the decompressorat the time, and so a panic() implementation was needed in the decompressor too. But that is no longer the case.
Kiryl Shutsemau
Acked-by Kiryl Shutsemau (Meta)Re: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 61 lines · Ard Biesheuvel
Quoted 59 lines · Borislav Petkov
Quoted 27 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The decompressor has its own implementation of
panic(), which is based on thevsnprintf()routine provided by the EFI stub.Relying on the EFI stub from code that does not execute in the context of the EFI boot services is a bad idea. It is also completely pointless in this case, given that the only user of this version of
panic()only passes a compile time constant string, without any printf conversions.So use
error()instead ofpanic()in that case, and drop thepanic()implementation entirely. This is needed so that the EFI stub'svsnprintf()can be modified in a manner that is incompatible with the expectations of this caller.Signed-off-by Ard Biesheuvel <[email protected]>--- arch/x86/boot/compressed/error.c | 19 ------------------- arch/x86/boot/compressed/error.h | 1 - arch/x86/boot/compressed/mem.c | 2 +- 3 files changed, 1 insertion(+), 21 deletions(-)arch/x86/boot/compressed/error.c +0
-0arch/x86/boot/compressed/error.c void error(char *m) · line 28
Quoted 7 lines
arch/x86/boot/compressed/error.c +0
-4@@ -22,22 +22,3 @@ void error(char *m)while (1 ) asm("hlt" ); } - -/* EFI libstub provides vsnprintf() */ -#ifdef CONFIG_EFI_STUB -void panic (const char *fmt, ...)So this thing appeared magically in v8 of the TDX unaccepted memory patches and I don't think we questioned it back then.
v7's
tdx_accept_memory()doeserror():https://lore.kernel.org/all/[email protected]/
and v8 started doing
panic():(quoted diff) +5
-0+void tdx_accept_memory(phys_addr_t start, phys_addr_t end) +{ + if (!tdx_enc_status_changed_phys(start, end, true)) + panic("Accepting memory failed: %#llx-%#llx\n", start, end); +}https://lore.kernel.org/all/[email protected]/
and it switched to it being a
vsnprintf()wrapper because it wanted to dump start and end perhaps.But then it ended up dropping the params in v13 and landed upstream with a single string as an argument.
Quoted 7 lines
Quoted 2 lines
Anyway, adding Kiryl/Kirill for comment and leaving in the rest for reference.
Looking at that v13, it seems the
panic()call was added toarch/x86/coco/tdx/tdx-shared.c, which was shared between the kernel proper and the decompressorat the time, and so apanic()implementation was needed in the decompressor too. But that is no longer the case.
Right. I don't mind dropping this panic() from the decompressor. The range was already gone since v13 and I never saw the error trigger anyway.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Ard Biesheuvel
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 70 lines · Kiryl Shutsemau
Quoted 61 lines · Ard Biesheuvel
Quoted 59 lines · Borislav Petkov
Quoted 27 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The decompressor has its own implementation of
panic(), which is based on thevsnprintf()routine provided by the EFI stub.Relying on the EFI stub from code that does not execute in the context of the EFI boot services is a bad idea. It is also completely pointless in this case, given that the only user of this version of
panic()only passes a compile time constant string, without any printf conversions.So use
error()instead ofpanic()in that case, and drop thepanic()implementation entirely. This is needed so that the EFI stub'svsnprintf()can be modified in a manner that is incompatible with the expectations of this caller.Signed-off-by Ard Biesheuvel <[email protected]>--- arch/x86/boot/compressed/error.c | 19 ------------------- arch/x86/boot/compressed/error.h | 1 - arch/x86/boot/compressed/mem.c | 2 +- 3 files changed, 1 insertion(+), 21 deletions(-)arch/x86/boot/compressed/error.c +0
-0arch/x86/boot/compressed/error.c void error(char *m) · line 28
Quoted 7 lines
arch/x86/boot/compressed/error.c +0
-4@@ -22,22 +22,3 @@ void error(char *m)while (1 ) asm("hlt" ); } - -/* EFI libstub provides vsnprintf() */ -#ifdef CONFIG_EFI_STUB -void panic (const char *fmt, ...)So this thing appeared magically in v8 of the TDX unaccepted memory patches and I don't think we questioned it back then.
v7's
tdx_accept_memory()doeserror():https://lore.kernel.org/all/[email protected]/
and v8 started doing
panic():(quoted diff) +5
-0+void tdx_accept_memory(phys_addr_t start, phys_addr_t end) +{ + if (!tdx_enc_status_changed_phys(start, end, true)) + panic("Accepting memory failed: %#llx-%#llx\n", start, end); +}https://lore.kernel.org/all/[email protected]/
and it switched to it being a
vsnprintf()wrapper because it wanted to dump start and end perhaps.But then it ended up dropping the params in v13 and landed upstream with a single string as an argument.
Quoted 7 lines
Quoted 2 lines
Anyway, adding Kiryl/Kirill for comment and leaving in the rest for reference.
Looking at that v13, it seems the
panic()call was added toarch/x86/coco/tdx/tdx-shared.c, which was shared between the kernel proper and the decompressorat the time, and so apanic()implementation was needed in the decompressor too. But that is no longer the case.
Quoted 6 lines
Right. I don't mind dropping this
panic()from the decompressor. The range was already gone since v13 and I never saw the error trigger anyway.
Thanks. Can this be applied as a fix please? I need it to base efi/next on it for the next cycle.
Borislav Petkov
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 2 lines · Ard Biesheuvel
Thanks. Can this be applied as a fix please? I need it to base efi/next on it for the next cycle.
You mean I stick it into tip and give you an immutable tag to merge?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteArd Biesheuvel
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Ard Biesheuvel
Thanks. Can this be applied as a fix please? I need it to base efi/next on it for the next cycle.
You mean I stick it into tip and give you an immutable tag to merge?
If you're not taking it as a fix, then yes, please.
Borislav Petkov
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 1 line · Ard Biesheuvel
If you're not taking it as a fix, then yes, please.
Help me understand the exact problem pls:
That panic() function gets called by arch_accept_memory() and *that* gets called only by code in drivers/firmware/efi/libstub/unaccepted_memory.c or drivers/firmware/efi/unaccepted_memory.c (but that uses the kernel proper panic() one).
So we *are* in the context of EFI boot services.
What am I missing?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteArd Biesheuvel
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 5 lines · Borislav Petkov
Quoted 1 line · Ard Biesheuvel
If you're not taking it as a fix, then yes, please.
Help me understand the exact problem pls:
Quoted 9 lines
That
panic()function gets called byarch_accept_memory()and *that* gets called only by code indrivers/firmware/efi/libstub/unaccepted_memory.cordrivers/firmware/efi/unaccepted_memory.c(but that uses the kernel properpanic()one).So we *are* in the context of EFI boot services.
What am I missing?
Right, I'd missed that.
But when booting via the EFI stub, the decompressor is omitted entirely, except for some TDX and SEV-SNP code that is shared between them.
This means calling error() is a bad idea, because the TDX initialization code that overrides the port I/O based __putstr() handling never executes in that case.
The reason I want to get rid of panic() is because snprintf() is going to be removed from the EFI stub.
So I think the current patch is still correct. I will follow up with another one that moves arch_accept_memory() into the EFI stub, and drops the dependency on error().
Borislav Petkov
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 2 lines · Ard Biesheuvel
The reason I want to get rid of
panic()is becausesnprintf()is going to be removed from the EFI stub.
Ok, I'll queue it into x86/boot and give you an immutable tag it doesn't look like something urgent to me.
Quoted 3 lines
So I think the current patch is still correct. I will follow up with another one that moves
arch_accept_memory()into the EFI stub, and drops the dependency onerror().
Ack.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteVincent Mailhol
Reviewed-byRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Hi Borislav,
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Ard Biesheuvel
The reason I want to get rid of
panic()is becausesnprintf()is going to be removed from the EFI stub.Ok, I'll queue it into x86/boot and give you an immutable tag it doesn't look like something urgent to me.
I had a look at tip/x86/boot but could not find this patch yet, nor an immutable tag containing it.
Just checking whether it is still queued on your side, thanks!
And, by the way:
Yours sincerely,
Vincent Mailhol
Ard Biesheuvel
maintainerRe: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 13 lines · Vincent Mailhol
Hi Borislav,
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Ard Biesheuvel
The reason I want to get rid of
panic()is becausesnprintf()is going to be removed from the EFI stub.Ok, I'll queue it into x86/boot and give you an immutable tag it doesn't look like something urgent to me.
I had a look at tip/x86/boot but could not find this patch yet, nor an immutable tag containing it.
Quoted 7 lines
Just checking whether it is still queued on your side, thanks!
And, by the way:
Reviewed-by Vincent Mailhol <[email protected]>
Thanks.
Latest is here:
https://lore.kernel.org/linux-efi/[email protected]/
Re: [PATCH v2 01/10] x86/boot: Drop pointless re-implementation of panic()
Quoted 19 lines · Ard Biesheuvel
Quoted 18 lines · Vincent Mailhol
Hi Borislav,
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Ard Biesheuvel
The reason I want to get rid of
panic()is becausesnprintf()is going to be removed from the EFI stub.Ok, I'll queue it into x86/boot and give you an immutable tag it doesn't look like something urgent to me.
I had a look at tip/x86/boot but could not find this patch yet, nor an immutable tag containing it.
Just checking whether it is still queued on your side, thanks!
And, by the way:
Quoted 7 lines
Quoted 2 lines
Reviewed-by Vincent Mailhol <[email protected]>Thanks.
Latest is here:
https://lore.kernel.org/linux-efi/[email protected]/
Ha! I missed that new version. Thanks for the link. I will test it.
Yours sincerely,
Vincent Mailhol
PATCH v2 2/10 lib/ucs2_string: Drop arbitrary input size limit and associated WARN()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
It's not really the job of library code to WARN and potentially bring down the system (with panic_on_warn=1) on a condition that is fairly arbitrary to begin with.
So drop the WARN_ON_ONCE() as well as the condition from ucs2_strscpy().
--- lib/ucs2_string.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ucs2_string.c +1 -1
@@ -57,7 +57,7 @@ ssize_t ucs2_strscpy(ucs2_char_t *dst, const ucs2_char_t *src, size_t count)* Ensure that we have a valid amount of space. We need to store at * least one NUL-character. */ -if (count ==0 ||WARN_ON_ONCE (count >INT_MAX /sizeof (*dst))) +if (count ==0 )return -E2BIG ;/*
Signature · 1 line
2.55.0.1003.g10538fe699-goog
PATCH v2 3/10 lib/ucs2_string: Suppress modinfo when __DISABLE_EXPORTS is set
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Allow the UCS-2 string library to be reused in the EFI stub, by suppressing the modinfo data that is usually emitted so that the library can be built as a module.
--- lib/ucs2_string.c | 2 ++ 1 file changed, 2 insertions(+)
lib/ucs2_string.c +2 -0
@@ -165,5 +165,7 @@ ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength) }EXPORT_SYMBOL (ucs2_as_utf8); +#ifndef __DISABLE_EXPORTSMODULE_DESCRIPTION ("UCS2 string handling" );MODULE_LICENSE ("GPL v2" ); +#endif
Signature · 1 line
2.55.0.1003.g10538fe699-goog
PATCH v2 4/10 lib/ucs2_string: Split out ucs2_as_utf8_l() taking a separate limit
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
ucs2_as_utf() takes a maxlength argument, which specifies how many bytes the function is permitted to store into the destination buffer.
The same value is used as an upper bound for the ucs2_strnlen() invocation, which is reasonable in the general case, as each UCS-2 character produces at least one byte of UTF-8 output, and so there is never a need to process more than 'maxlength' UCS-2 characters.
However, if the UCS-2 string is not NUL terminated, ucs2_strnlen() may read past the end of the buffer if 'maxlength' is set to a high value.
Current callers pass UCS-2 strings that are expected to be NUL terminated, but for processing the load options in the EFI stub, a version is needed that takes a separate limit argument. So split that off from the current implementation.
--- include/linux/ucs2_string.h | 11 ++++++++++- lib/ucs2_string.c | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-)
include/linux/ucs2_string.h +10 -1
@@ -14,7 +14,16 @@ ssize_t ucs2_strscpy(ucs2_char_t *dst, const ucs2_char_t *src, size_t count);int ucs2_strncmp (const ucs2_char_t *a,const ucs2_char_t *b,size_t len);unsigned long ucs2_utf8size (const ucs2_char_t *src); +unsigned long +ucs2_as_utf8_l (u8 *dest,const ucs2_char_t *src,unsigned long limit, +unsigned long maxlength); + +static inline unsigned long ucs2_as_utf8 (u8 *dest,const ucs2_char_t *src, -unsigned long maxlength); +unsigned long maxlength) +{ +return ucs2_as_utf8_l (dest, src,ucs2_strnlen (src, maxlength), + maxlength); +}#endif /* _LINUX_UCS2_STRING_H_ */
lib/ucs2_string.c +3 -3
@@ -132,11 +132,11 @@ EXPORT_SYMBOL(ucs2_utf8size);* final NUL character. */ unsigned long -ucs2_as_utf8 (u8 *dest,const ucs2_char_t *src,unsigned long maxlength) +ucs2_as_utf8_l (u8 *dest,const ucs2_char_t *src,unsigned long limit, +unsigned long maxlength) {unsigned int i;unsigned long j =0 ; -unsigned long limit =ucs2_strnlen (src, maxlength);for (i =0 ; maxlength && i < limit; i++) {u16 c = src[i];
@@ -163,7 +163,7 @@ ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength) dest[j] ='\0' ;return j; } -EXPORT_SYMBOL (ucs2_as_utf8); +EXPORT_SYMBOL (ucs2_as_utf8_l);#ifndef __DISABLE_EXPORTSMODULE_DESCRIPTION ("UCS2 string handling" );
Signature · 1 line
2.55.0.1003.g10538fe699-goog
Re: [PATCH v2 04/10] lib/ucs2_string: Split out ucs2_as_utf8_l() taking a separate limit
Quoted 3 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
ucs2_as_utf()takes a maxlength argument, which specifies how many bytes
^^^^^^^^^^^^^
Typo: ucs2_as_utf8() (missing '8').
Quoted 48 lines
the function is permitted to store into the destination buffer.
The same value is used as an upper bound for the
ucs2_strnlen()invocation, which is reasonable in the general case, as each UCS-2 character produces at least one byte of UTF-8 output, and so there is never a need to process more than 'maxlength' UCS-2 characters.However, if the UCS-2 string is not NUL terminated,
ucs2_strnlen()may read past the end of the buffer if 'maxlength' is set to a high value.Current callers pass UCS-2 strings that are expected to be NUL terminated, but for processing the load options in the EFI stub, a version is needed that takes a separate limit argument. So split that off from the current implementation.
Signed-off-by Ard Biesheuvel <[email protected]>--- include/linux/ucs2_string.h | 11 ++++++++++- lib/ucs2_string.c | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-)include/linux/ucs2_string.h +10
-1@@ -14,7 +14,16 @@ ssize_t ucs2_strscpy(ucs2_char_t *dst, const ucs2_char_t *src, size_t count);int ucs2_strncmp (const ucs2_char_t *a,const ucs2_char_t *b,size_t len);unsigned long ucs2_utf8size (const ucs2_char_t *src); +unsigned long +ucs2_as_utf8_l (u8 *dest,const ucs2_char_t *src,unsigned long limit, +unsigned long maxlength); + +static inline unsigned long ucs2_as_utf8 (u8 *dest,const ucs2_char_t *src, -unsigned long maxlength); +unsigned long maxlength) +{ +return ucs2_as_utf8_l (dest, src,ucs2_strnlen (src, maxlength), + maxlength); +}#endif /* _LINUX_UCS2_STRING_H_ */ lib/ucs2_string.c +0
-0
lib/ucs2_string.c EXPORT_SYMBOL(ucs2_utf8size); · line 133
Quoted 2 lines
lib/ucs2_string.c +0
-0@@ -132,11 +132,11 @@ EXPORT_SYMBOL(ucs2_utf8size);* final NUL character. */
ucs2_as_utf8_l() still uses the old documentation of ucs2_as_utf8(). It currently reads:
/*
* copy at most maxlength bytes of whole utf8 characters to dest from the
* ucs2 string src.
*
* The return value is the number of characters copied, not including the
* final NUL character.
*/
That documentation should probably be moved to linux/ucs2_string.h so that ucs2_as_utf8() remains documented after being turned into a static inline wrapper.
As for ucs2_as_utf8_l(), it would be worth adding a new comment block to highlight its specific behaviour: the new limit argument, the fact that the string is only NUL-terminated if there is enough space and that the return value is not the number of wide characters copied but the number of bytes copied.
Quoted 12 lines
lib/ucs2_string.c +2
-2unsigned long -ucs2_as_utf8 (u8 *dest,const ucs2_char_t *src,unsigned long maxlength) +ucs2_as_utf8_l (u8 *dest,const ucs2_char_t *src,unsigned long limit, +unsigned long maxlength) {unsigned int i;unsigned long j =0 ; -unsigned long limit =ucs2_strnlen (src, maxlength);for (i =0 ; maxlength && i < limit; i++) {u16 c = src[i];
lib/ucs2_string.c ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength) · line 169
Quoted 8 lines
lib/ucs2_string.c +1
-1@@ -163,7 +163,7 @@ ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength) dest[j] ='\0' ;return j; } -EXPORT_SYMBOL (ucs2_as_utf8); +EXPORT_SYMBOL (ucs2_as_utf8_l);#ifndef __DISABLE_EXPORTSMODULE_DESCRIPTION ("UCS2 string handling" );
Yours sincerely,
Vincent Mailhol
PATCH v2 5/10 efi/libstub: Use ucs2_string library for UTF-16 to UTF-8 conversion
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Don't rely on sprintf() with a wide string conversion modifier to convert the command line from UTF-16 to UTF-8. Instead, use the existing ucs2 string library routine that does the same. Note that while UEFI claims support for UTF-16, in practice it ignores surrogate pairs entirely, and so the simplified UCS-2 character set (where each character takes up exactly 2 bytes) is sufficient here.
This removes the only user of sprintf() in the EFI stub, so drop that function as well.
Since boot memory is plentiful on UEFI systems, just establish a worst case upper bound for the size of the buffer (which can never exceed COMMAND_LINE_SIZE), and allocate that first. Then, perform the conversion, and only fall back to processing the command line character by character if that resulted in truncation. This makes the common execution path much simpler.
--- drivers/firmware/efi/libstub/Makefile | 3 +- drivers/firmware/efi/libstub/efi-stub-helper.c | 100 ++++++++------------ drivers/firmware/efi/libstub/vsprintf.c | 11 --- 3 files changed, 41 insertions(+), 73 deletions(-)
drivers/firmware/efi/libstub/Makefile +2 -1
@@ -66,7 +66,8 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ lib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ file.o mem.o random.o randomalloc.o pci.o \ skip_spaces.o lib-cmdline.o lib-ctype.o \ - alignedmem.o printk.o vsprintf.o + alignedmem.o printk.o vsprintf.o \ + lib-ucs2_string.o # include the stub's libfdt dependencies from lib/ when needed libfdt-deps := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \
drivers/firmware/efi/libstub/efi-stub-helper.c +39 -61
126 lines not on this page · raw message
drivers/firmware/efi/libstub/vsprintf.c +0 -11
@@ -551,14 +551,3 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)return pos; } - -int snprintf (char *buf,size_t size,const char *fmt, ...) -{ -va_list args; -int i; - -va_start (args, fmt); - i =vsnprintf (buf, size, fmt, args); -va_end (args); -return i; -}
Signature · 1 line
2.55.0.1003.g10538fe699-goog
Re: [PATCH v2 05/10] efi/libstub: Use ucs2_string library for UTF-16 to UTF-8 conversion
Quoted 70 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
Don't rely on
sprintf()with a wide string conversion modifier to convert the command line from UTF-16 to UTF-8. Instead, use the existing ucs2 string library routine that does the same. Note that while UEFI claims support for UTF-16, in practice it ignores surrogate pairs entirely, and so the simplified UCS-2 character set (where each character takes up exactly 2 bytes) is sufficient here.This removes the only user of
sprintf()in the EFI stub, so drop that function as well.Since boot memory is plentiful on UEFI systems, just establish a worst case upper bound for the size of the buffer (which can never exceed
COMMAND_LINE_SIZE), and allocate that first. Then, perform the conversion, and only fall back to processing the command line character by character if that resulted in truncation. This makes the common execution path much simpler.Signed-off-by Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/libstub/Makefile | 3 +- drivers/firmware/efi/libstub/efi-stub-helper.c | 100 ++++++++------------ drivers/firmware/efi/libstub/vsprintf.c | 11 --- 3 files changed, 41 insertions(+), 73 deletions(-)drivers/firmware/efi/libstub/Makefile +2
-1@@ -66,7 +66,8 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ lib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ file.o mem.o random.o randomalloc.o pci.o \ skip_spaces.o lib-cmdline.o lib-ctype.o \ - alignedmem.o printk.o vsprintf.o + alignedmem.o printk.o vsprintf.o \ + lib-ucs2_string.o # include the stub's libfdt dependencies from lib/ when needed libfdt-deps := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \drivers/firmware/efi/libstub/efi-stub-helper.c +4
-5@@ -12,6 +12,7 @@#include <linux/efi.h> #include <linux/kernel.h> #include <linux/overflow.h> +#include <linux/ucs2_string.h> #include <asm/efi.h> #include <asm/setup.h> @@ -334,81 +335,58 @@ char *efi_convert_cmdline(efi_loaded_image_t *image) {const efi_char16_t *options =efi_table_attr (image, load_options);u32 options_size =efi_table_attr (image, load_options_size); -int options_bytes =0 , safe_options_bytes =0 ;/* UTF-8 bytes */ -unsigned long cmdline_addr =0 ; -const efi_char16_t *s2; -bool in_quote = false; +unsigned long options_chars =0 ; +unsigned long cmdline_bytes;efi_status_t status; -u32 options_chars; +char *cmdline_addr;if (options_size >0 )efi_measure_tagged_event ((unsigned long )options, options_size,EFISTUB_EVT_LOAD_OPTIONS );
drivers/firmware/efi/libstub/efi-stub-helper.c char *efi_convert_cmdline(efi_loaded_image_t *image) · line 357
Quoted 10 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +0
-9efi_apply_loadoptions_quirk ((const void **)&options, &options_size); - options_chars = options_size /sizeof (efi_char16_t); - -if (options) { - s2 = options; -while (options_bytes <COMMAND_LINE_SIZE && options_chars--) { -efi_char16_t c = *s2++; - -if (c <0x80 ) { -if (c ==L'\0' || c ==L'\n' )
^^^^^^^^^^
This newline check is not present in the new version. The new code only checks for NUL termination through ucs2_strnlen(). The patch description says nothing about this, so maybe this is an omission?
Quoted 54 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +6
-44-break ; -if (c ==L'"' ) - in_quote = !in_quote; -else if (!in_quote &&isspace ((char )c)) - safe_options_bytes = options_bytes; - - options_bytes++; -continue ; - } - -/* -* Get the number of UTF-8 bytes corresponding to a -* UTF-16 character. -* The first part handles everything in the BMP. -*/ - options_bytes +=2 + (c >=0x800 ); -/* -* Add one more byte for valid surrogate pairs. Invalid -* surrogates will be replaced with 0xfffd and take up -* only 3 bytes. -*/ -if ((c &0xfc00 ) ==0xd800 ) { -/* -* If the very last word is a high surrogate, -* we must ignore it since we can't access the -* low surrogate. -*/ -if (!options_chars) { - options_bytes -=3 ; - }else if ((*s2 &0xfc00 ) ==0xdc00 ) { - options_bytes++; - options_chars--; - s2++; - } - } - } -if (options_bytes >=COMMAND_LINE_SIZE ) { - options_bytes = safe_options_bytes; -efi_err ("Command line is too long: truncated to %d bytes\n" , - options_bytes); - } - } +if (options) + options_chars =ucs2_strnlen (options, + options_size /sizeof (efi_char16_t)); - options_bytes++;/* NUL termination */ +/* Each UCS-2 char takes up at most 3 UTF-8 bytes */ + cmdline_bytes =min (3 * options_chars,COMMAND_LINE_SIZE -1 ) +1 ; - status =efi_bs_call (allocate_pool,EFI_LOADER_DATA , options_bytes, + status =efi_bs_call (allocate_pool,EFI_LOADER_DATA , cmdline_bytes, (void **)&cmdline_addr);if (status !=EFI_SUCCESS )
drivers/firmware/efi/libstub/efi-stub-helper.c char *efi_convert_cmdline(efi_loaded_image_t *image) · line 365
Quoted 10 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +6
-2return NULL ; -snprintf ((char *)cmdline_addr, options_bytes,"%.*ls" , - options_bytes -1 , options); +if (ucs2_as_utf8_l (cmdline_addr, options, options_chars, + cmdline_bytes) >=COMMAND_LINE_SIZE ) { +/* +* The output fills up the entire buffer, and may have been +* truncated. This can only happen when options_bytes equals +* COMMAND_LINE_SIZE.
If truncation occurs because there is not enough space left for a multi-byte character, ucs2_as_utf8_l() returns a value below COMMAND_LINE_SIZE.
Should the test instead check whether all the options_chars were consumed?
Quoted 35 lines
drivers/firmware/efi/libstub/efi-stub-helper.c +22
-1+* Work backwards through the buffer to find a safe truncation +* point (i.e., a blank character not inside a quoted string). +*/ +int safe_pos[2 ] = {}; +int in_quote =0 ; + +for (int i =COMMAND_LINE_SIZE -1 ; i >=0 ; i--) { +char c = cmdline_addr[i]; + +if (!c) +return cmdline_addr; +else if (c =='"' ) + in_quote ^=1 ; +else if (!safe_pos[in_quote] &&isspace (c)) + safe_pos[in_quote] = i; + } + +efi_err ("Command line is too long: truncated to %d bytes\n" , + safe_pos[in_quote]); + cmdline_addr[safe_pos[in_quote]] ='\0' ; + } -return (char *)cmdline_addr; +return cmdline_addr; }/** drivers/firmware/efi/libstub/vsprintf.c +0
-0@@ -551,14 +551,3 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)return pos; }
drivers/firmware/efi/libstub/vsprintf.c int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) · line 563
Quoted 10 lines
drivers/firmware/efi/libstub/vsprintf.c +0
-10- -int snprintf (char *buf,size_t size,const char *fmt, ...) -{ -va_list args; -int i; - -va_start (args, fmt); - i =vsnprintf (buf, size, fmt, args); -va_end (args); - return i;> -}
Yours sincerely,
Vincent Mailhol
PATCH v2 6/10 efi/libstub: Avoid efi_puts() for compile time constant strings
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
efi_puts() performs a UTF-8 to UTF-16 conversion on its input, as the EFI console's native character set is UTF-16.
This is pointless for compile time constant strings, since we can simply define those as UTF-16 to begin with.
Note that efi_puts() also performs LF to CR-LF conversion, so this needs to be taken into account as well.
--- drivers/firmware/efi/libstub/gop.c | 6 +++--- drivers/firmware/efi/libstub/printk.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/firmware/efi/libstub/gop.c +3 -3
@@ -309,12 +309,12 @@ static u32 choose_mode_list(efi_graphics_output_protocol_t *gop)efi_status_t status;efi_printk ("Available graphics modes are 0-%u\n" , max_mode-1 ); -efi_puts (" * = current mode\n" -" - = unusable mode\n" ); +efi_char16_puts (L" * = current mode\r\n" +" - = unusable mode\r\n" );choose_mode (gop, match_list, (void *)cur_mode); -efi_puts ("\nPress any key to continue (or wait 10 seconds)\n" ); +efi_char16_puts (L"\r\nPress any key to continue (or wait 10 seconds)\r\n" ); status =efi_wait_for_key (10 *EFI_USEC_PER_SEC , &key);if (status !=EFI_SUCCESS && status !=EFI_TIMEOUT ) {efi_err ("Unable to read key, continuing in 10 seconds\n" );
drivers/firmware/efi/libstub/printk.c +2 -2
@@ -136,7 +136,7 @@ int efi_printk(const char *fmt, ...)return 0 ;if (loglevel >=0 ) -efi_puts ("EFI stub: " ); +efi_char16_puts (L"EFI stub: " ); fmt =printk_skip_level (fmt);
@@ -146,7 +146,7 @@ int efi_printk(const char *fmt, ...)efi_puts (printf_buf);if (printed >=sizeof (printf_buf)) { -efi_puts ("[Message truncated]\n" ); +efi_char16_puts (L"[Message truncated]\r\n" );return -1 ; }
Signature · 1 line
2.55.0.1003.g10538fe699-goog
Re: [PATCH v2 06/10] efi/libstub: Avoid efi_puts() for compile time constant strings
Quoted 7 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
efi_puts()performs a UTF-8 to UTF-16 conversion on its input, as the EFI console's native character set is UTF-16.This is pointless for compile time constant strings, since we can simply define those as UTF-16 to begin with.
I would nuance this a bit. This is a trade-off between space and time. ASCII text takes half of the space but needs a runtime conversion.
Note this is not an objection to the patch itself, but I think the commit message could be slightly more nuanced.
Quoted 4 lines
Note that
efi_puts()also performs LF to CR-LF conversion, so this needs to be taken into account as well.Signed-off-by Ard Biesheuvel <[email protected]>
(...)
Yours sincerely,
Vincent Mailhol
PATCH v2 7/10 efi/libstub: Output UTF-16 directly from vsnprintf()
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The only remaining users of vsnprintf() in the EFI stub are the diagnostic printk()'s, which are emitted to the console and not recorded for posterity.
The EFI console uses UTF-16 (or actually, UCS-2) natively, and so all non-UTF16 strings that are emitted need to be converted. Given the stub's vsnprintf() support for wide strings (using the %ls conversion modifier), which uses UTF-16 to UTF-8 conversion internally, the final conversion to UTF-16 needs to support not just plain ASCII but UTF-8 as well.
This is all pointless, of course, and it makes more sense to use UTF-16 internally. This removes the need for UTF-16 to UTF-8 conversion in vsnprintf(), and given that all non-wide string inputs to vsnprintf() that exist in the stub today are compile time constant ASCII strings, the need to convert UTF-8 to UTF-16 disappears as well.
So implement efi_vsnprintf() taking a const char *fmt as before, but outputting a efi_char16_t[] that can be passed to the EFI console directly, rather than via efi_puts(), leaving the latter unused and therefore removed.
Note that efi_puts() performs LF to CR-LF conversion internally, so add this capability to efi_vsnprintf() as well.
--- drivers/firmware/efi/libstub/efistub.h | 5 +- drivers/firmware/efi/libstub/printk.c | 91 ++----------------- drivers/firmware/efi/libstub/vsprintf.c | 96 +++----------------- 3 files changed, 22 insertions(+), 170 deletions(-)
drivers/firmware/efi/libstub/efistub.h +3 -2
@@ -1078,9 +1078,10 @@ efi_status_t check_platform_features(void);void *get_efi_config_table (efi_guid_t guid); -/* NOTE: These functions do not print a trailing newline after the string */ void efi_char16_puts (efi_char16_t *); -void efi_puts (const char *str); + +int efi_vsnprintf (efi_char16_t *buf,size_t size,const char *fmt,va_list ap, +bool crlf); __printf(1, 2) int efi_printk(char const *fmt, ...);
drivers/firmware/efi/libstub/printk.c +7 -84
116 lines not on this page · raw message
drivers/firmware/efi/libstub/vsprintf.c +12 -84
136 lines not on this page · raw message
Signature · 1 line
2.55.0.1003.g10538fe699-goog
PATCH v2 8/10 efi/libstub: Add support for printing human readable GUIDs
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Add support for the %pUl printk conversion specifier, which takes a pointer to a GUID and prints it in the usual format:
aaaaaaaa-bbbb-cccc-dddd-dddddddddddd
--- drivers/firmware/efi/libstub/vsprintf.c | 40 +++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-)
drivers/firmware/efi/libstub/vsprintf.c +35 -5
@@ -113,6 +113,9 @@ char *put_dec(char *end, unsigned long long n)return p; } +/* we are called with base 8, 10 or 16, only, thus don't need "G..." */ +static const char digits[16 ] ="0123456789ABCDEF" ;/* "GHIJKLMNOPQRSTUVWXYZ"; */ +static char *number (char *end,unsigned long long num,int base,char locase) {
@@ -121,9 +124,6 @@ char *number(char *end, unsigned long long num, int base, char locase)* produces same digits or (maybe lowercased) letters */ -/* we are called with base 8, 10 or 16, only, thus don't need "G..." */ -static const char digits[16 ] ="0123456789ABCDEF" ;/* "GHIJKLMNOPQRSTUVWXYZ"; */ -switch (base) {case 10 :if (num !=0 )
@@ -144,6 +144,29 @@ char *number(char *end, unsigned long long num, int base, char locase)return end; } +static char *guid_to_str (const efi_guid_t *guid,char *out,char locase) +{ +static const u8 guid_index[UUID_SIZE ] = { +3 ,2 ,1 ,0 ,5 ,4 ,7 ,6 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 , + }; + +for (int i =0 , p =0 ; i <ARRAY_SIZE (guid_index); i++) { +u8 byte = guid->b[guid_index[i]]; + + out[p++] = locase | digits[byte >>4 ]; + out[p++] = locase | digits[byte &0xf ]; + +switch (i) { +case 3 : +case 5 : +case 7 : +case 9 : + out[p++] ='-' ; + } + } +return out; +} +#define ZEROPAD 1/* pad with zero */ #define SIGN 2/* unsigned/signed long */ #define PLUS 4/* show plus */
@@ -253,8 +276,7 @@ do { \int efi_vsnprintf (efi_char16_t *buf,size_t size,const char *fmt,va_list ap,bool crlf) { -/* The maximum space required is to print a 64-bit number in octal */ -char tmp[(sizeof (unsigned long long ) *8 +2 ) /3 ]; +char tmp[UUID_STRING_LEN ];char *tmp_end = &tmp[ARRAY_SIZE (tmp)];long long num;int base;
@@ -367,6 +389,14 @@ int efi_vsnprintf(efi_char16_t *buf, size_t size, const char *fmt, va_list ap,break ;case 'p' : +if (fmt[1 ] =='U' && (fmt[2 ] |0x20 ) =='l' ) { + flags &=LEFT ; + s = guid_to_str(va_arg(args, efi_guid_t *), tmp, fmt[2] & 0x20); + precision = len =UUID_STRING_LEN ; + fmt +=2 ; + goto output; + } +if (precision <0 ) precision =2 *sizeof (void *); fallthrough;
Signature · 1 line
2.55.0.1003.g10538fe699-goog
PATCH v2 9/10 efi/libstub: Add efi_snprintf() to construct wide strings
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The native EFI character set is UTF-16 (or in practice, UCS-2). Implement efi_snprintf() to construct UTF-16 strings using printf style templates. This will be used in a subsequent patch to set the LoaderDevicePartUUID EFI variable.
--- drivers/firmware/efi/libstub/efistub.h | 1 + drivers/firmware/efi/libstub/vsprintf.c | 11 +++++++++++ 2 files changed, 12 insertions(+)
drivers/firmware/efi/libstub/efistub.h +1 -0
@@ -1084,6 +1084,7 @@ int efi_vsnprintf(efi_char16_t *buf, size_t size, const char *fmt, va_list ap, bool crlf); __printf(1, 2) int efi_printk(char const *fmt, ...); +__printf(3, 4) int efi_snprintf(efi_char16_t *buf, size_t size, const char *fmt, ...);void efi_free (unsigned long size,unsigned long addr); DEFINE_FREE(efi_pool, void *, if (_T) efi_bs_call(free_pool, _T));
drivers/firmware/efi/libstub/vsprintf.c +11 -0
@@ -509,3 +509,14 @@ int efi_vsnprintf(efi_char16_t *buf, size_t size, const char *fmt, va_list ap,return pos; } + +int efi_snprintf (efi_char16_t *buf,size_t size,const char *fmt, ...) +{ +va_list args; +int i; + +va_start (args, fmt); + i =efi_vsnprintf (buf, size, fmt, args, false); +va_end (args); +return i; +}
Signature · 1 line
2.55.0.1003.g10538fe699-goog
Re: [PATCH v2 09/10] efi/libstub: Add efi_snprintf() to construct wide strings
Quoted 34 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The native EFI character set is UTF-16 (or in practice, UCS-2). Implement
efi_snprintf()to construct UTF-16 strings using printf style templates. This will be used in a subsequent patch to set the LoaderDevicePartUUID EFI variable.Signed-off-by Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/libstub/efistub.h | 1 + drivers/firmware/efi/libstub/vsprintf.c | 11 +++++++++++ 2 files changed, 12 insertions(+)drivers/firmware/efi/libstub/efistub.h +1
-0@@ -1084,6 +1084,7 @@ int efi_vsnprintf(efi_char16_t *buf, size_t size, const char *fmt, va_list ap, bool crlf); __printf(1, 2) int efi_printk(char const *fmt, ...); +__printf(3, 4) int efi_snprintf(efi_char16_t *buf, size_t size, const char *fmt, ...);void efi_free (unsigned long size,unsigned long addr); DEFINE_FREE(efi_pool, void *, if (_T) efi_bs_call(free_pool, _T));drivers/firmware/efi/libstub/vsprintf.c +0
-0@@ -509,3 +509,14 @@ int efi_vsnprintf(efi_char16_t *buf, size_t size, const char *fmt, va_list ap,return pos; }
drivers/firmware/efi/libstub/vsprintf.c int efi_vsnprintf(efi_char16_t *buf, size_t size, const char *fmt, va_list ap, · line 521
Quoted 10 lines
drivers/firmware/efi/libstub/vsprintf.c +10
-0+ +int efi_snprintf (efi_char16_t *buf,size_t size,const char *fmt, ...) +{ +va_list args; +int i; + +va_start (args, fmt); + i =efi_vsnprintf (buf, size, fmt, args, false); +va_end (args); +return i;
checkpatch.pl is complaining about the use of spaces instead of tabulations:
WARNING: please, no spaces at the start of a line #41: FILE: drivers/firmware/efi/libstub/vsprintf.c:515:
+ va_list args;$
WARNING: please, no spaces at the start of a line #42: FILE: drivers/firmware/efi/libstub/vsprintf.c:516:
+ int i;$
WARNING: please, no spaces at the start of a line #44: FILE: drivers/firmware/efi/libstub/vsprintf.c:518:
+ va_start(args, fmt);$
WARNING: please, no spaces at the start of a line #45: FILE: drivers/firmware/efi/libstub/vsprintf.c:519:
+ i = efi_vsnprintf(buf, size, fmt, args, false);$
WARNING: please, no spaces at the start of a line #46: FILE: drivers/firmware/efi/libstub/vsprintf.c:520:
+ va_end(args);$
WARNING: please, no spaces at the start of a line #47: FILE: drivers/firmware/efi/libstub/vsprintf.c:521:
+ return i;$
drivers/firmware/efi/libstub/vsprintf.c int efi_vsnprintf(efi_char16_t *buf, size_t size, const char *fmt, va_list ap, · line 522
Quoted 1 line
drivers/firmware/efi/libstub/vsprintf.c +1
-0+}
Yours sincerely,
Vincent Mailhol
PATCH v2 10/10 efi/libstub: add initial Boot Loader Interface support
In reply to Ard BiesheuvelFrom: Vincent Mailhol <[email protected]>
The Boot Loader Interface (BLI) [1] defines EFI variables that expose boot loader state to the running OS. LoaderInfo identifies the boot loader, while LoaderDevicePartUUID records the GPT partition UUID of the partition containing it.
LoaderDevicePartUUID is used, for example, by systemd-gpt-auto-generator
[2] to identify the disk the boot loader was launched from and automatically detect and mount partitions on it.
GRUB [3] and systemd-boot [4] populate these variables, but when the kernel is started directly by EFI firmware, there is no conventional external boot loader to provide them. In that case, because the EFI stub performs the boot loader role, it should provide the variables itself.
Use LoaderInfo as a sentinel: if it is already set by an earlier boot stage or cannot be set, bail out. Otherwise, populate the other BLI variables.
Parse the loaded image device path, extract the GUID signature from its GPT HD() node and publish it under the Linux loader entry vendor GUID as the volatile LoaderDevicePartUUID EFI variable.
Install the efi_bli_set_variables() hook in both the generic efi-stub.c path and the x86-specific x86-stub.c path.
[1] The Boot Loader Interface
[2] systemd-gpt-auto-generator
[3] GRUB -- §16.2 bli
[4] systemd -- systemd-boot UEFI Boot Manager
[ardb: - constify 'image' pointer parameter
- pass efi_guid_t* to efi_snprintf()]
--- drivers/firmware/efi/libstub/Makefile | 2 +- drivers/firmware/efi/libstub/bli.c | 87 ++++++++++++++++++++ drivers/firmware/efi/libstub/efi-stub.c | 1 + drivers/firmware/efi/libstub/efistub.h | 2 + drivers/firmware/efi/libstub/x86-stub.c | 1 + include/linux/efi.h | 22 +++++ 6 files changed, 114 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/Makefile +1 -1
@@ -66,7 +66,7 @@ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ lib-y := efi-stub-helper.o gop.o secureboot.o tpm.o \ file.o mem.o random.o randomalloc.o pci.o \ skip_spaces.o lib-cmdline.o lib-ctype.o \ - alignedmem.o printk.o vsprintf.o \ + alignedmem.o printk.o vsprintf.o bli.o \ lib-ucs2_string.o # include the stub's libfdt dependencies from lib/ when needed
drivers/firmware/efi/libstub/bli.c new file +87 -0
87 lines not on this page · raw message
drivers/firmware/efi/libstub/efi-stub.c +1 -0
@@ -165,6 +165,7 @@ efi_status_t efi_stub_common(efi_handle_t handle, dpy =setup_primary_display ();efi_retrieve_eventlog (); +efi_bli_set_variables (image);/* Ask the firmware to clear memory on unclean shutdown */ efi_enable_reset_attack_mitigation ();
drivers/firmware/efi/libstub/efistub.h +2 -0
@@ -1072,6 +1072,8 @@ efi_status_t efi_random_alloc(unsigned long size, unsigned long align, int memory_type, unsigned long alloc_min, unsigned long alloc_max); +void efi_bli_set_variables (const efi_loaded_image_t *image); +efi_status_t efi_random_get_seed (void );efi_status_t check_platform_features (void );
drivers/firmware/efi/libstub/x86-stub.c +1 -0
@@ -1014,6 +1014,7 @@ void __noreturn efi_stub_entry(efi_handle_t handle,efi_random_get_seed ();efi_retrieve_eventlog (); +efi_bli_set_variables (image);setup_graphics (boot_params);
include/linux/efi.h +22 -0
@@ -957,6 +957,17 @@ extern int efi_status_to_err(efi_status_t status);#define EFI_DEV_END_INSTANCE 0x01#define EFI_DEV_END_ENTIRE 0xFF +enum efi_hd_partition_format { +EFI_HD_PARTITION_FORMAT_MBR =1 , +EFI_HD_PARTITION_FORMAT_GPT , +}; + +enum efi_hd_signature_type { +EFI_HD_SIGNATURE_TYPE_NONE , +EFI_HD_SIGNATURE_TYPE_MBR , +EFI_HD_SIGNATURE_TYPE_GUID , +}; +struct efi_generic_dev_path {u8 type;u8 sub_type;
@@ -988,6 +999,16 @@ struct efi_rel_offset_dev_path {u64 ending_offset; } __packed; +struct efi_hd_dev_path { +struct efi_generic_dev_path header; +u32 partition_number; +u64 partition_start; +u64 partition_size; +efi_guid_t signature; +u8 partition_format; +u8 signature_type; +} __packed; +struct efi_mem_mapped_dev_path {struct efi_generic_dev_path header;u32 memory_type;
@@ -1007,6 +1028,7 @@ struct efi_dev_path {struct efi_pci_dev_path pci;struct efi_vendor_dev_path vendor;struct efi_rel_offset_dev_path rel_offset; +struct efi_hd_dev_path hd; }; } __packed;
Signature · 1 line
2.55.0.1003.g10538fe699-goog
Re: [PATCH v2 10/10] efi/libstub: add initial Boot Loader Interface support
Quoted 44 lines · Ard Biesheuvel
From: Vincent Mailhol <[email protected]>
The Boot Loader Interface (BLI) [1] defines EFI variables that expose boot loader state to the running OS. LoaderInfo identifies the boot loader, while LoaderDevicePartUUID records the GPT partition UUID of the partition containing it.
LoaderDevicePartUUID is used, for example, by systemd-gpt-auto-generator
[2] to identify the disk the boot loader was launched from and automatically detect and mount partitions on it.GRUB [3] and systemd-boot [4] populate these variables, but when the kernel is started directly by EFI firmware, there is no conventional external boot loader to provide them. In that case, because the EFI stub performs the boot loader role, it should provide the variables itself.
Use LoaderInfo as a sentinel: if it is already set by an earlier boot stage or cannot be set, bail out. Otherwise, populate the other BLI variables.
Parse the loaded image device path, extract the GUID signature from its GPT
HD()node and publish it under the Linux loader entry vendor GUID as the volatile LoaderDevicePartUUID EFI variable.Install the
efi_bli_set_variables()hook in both the genericefi-stub.cpath and the x86-specificx86-stub.cpath.[1] The Boot Loader Interface
[2] systemd-gpt-auto-generator
[3] GRUB -- §16.2 bli
[4] systemd -- systemd-boot UEFI Boot Manager
Signed-off-by Vincent Mailhol <[email protected]>[ardb: - constify 'image' pointer parameter
- pass efi_guid_t* toefi_snprintf()]Signed-off-by Ard Biesheuvel <[email protected]>---
(...)
drivers/firmware/efi/libstub/bli.c line 6
Quoted 12 lines
drivers/firmware/efi/libstub/bli.c new file +6
-0@@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <generated/utsrelease.h> + +#include <linux/efi.h> +#include <linux/errno.h>
<linux/errno.h> is not needed anymore following the changes in v3.
(Sorry, this is my mistake)
drivers/firmware/efi/libstub/bli.c line 9
Quoted 3 lines
drivers/firmware/efi/libstub/bli.c new file +3
-0+#include <linux/unaligned.h> + +#include "efistub.h"
(...)
Yours sincerely,
Vincent Mailhol
Quoted 26 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
The EFI libstub performs some redundant conversions between UTF-16 and UTF-8 and back again, which includes dealing with surrogate pairs, which UEFI implementations themselves simply ignore.
So drop all the homegrown code, and use the existing UCS-2 (== UTF-16 without surrogate pairs) library code where conversion to UTF-8 is actually needed (the kernel command line).
The remaining handling involves the EFI console, which supports wide characters natively, so just use those directly.
Changes since v1 [0]:
- drop size limit fromucs2_strscpy()instead of just theWARN()
- suppress modinfo sections fromucs2_stringwhen__DISABLE_EXPORTSis defined
- allow the input limit and max output size to be passed separately toucs2_to_utf8()
- reimplementefi_convert_cmdline()to optimize the common case, and only process the input character by character if its size exceedsCOMMAND_LINE_SIZE
- usememcpy()to avoidstrscpy()semantics in handling of %ls
- incorporate Vincent's patch (which inspired this work) for completeness
Quoted 1 line
Cc Vincent Mailhol <[email protected]>
Sorry for the late feedback. I just finished my review. Aside from the few comments I left, the runtime tests were all OK. My BLI patch works fine on top of your changes.
Thanks again for the time spent to accommodate for my patch!
Yours sincerely,
Vincent Mailhol