Re: [PATCH 5/7] efi/libstub: Output UTF-16 directly from vsnprintf()
From: David Laight
Date: Wed Sep 09 2026 - 09:12:59 EST
On Sun, 6 Sep 2026 15:08:23 +0200
Ard Biesheuvel <ardb@xxxxxxxxxx> wrote:
> 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.
>
> Signed-off-by: Ard Biesheuvel <ardb@xxxxxxxxxx>
...
> /**
> * efi_printk() - Print a kernel message
> * @fmt: format string
> *
> * The first letter of the format string is used to determine the logging level
> * of the message. If the level is less then the current EFI logging level, the
> - * message is suppressed. The message will be truncated to 255 bytes.
> + * message is suppressed. The message will be truncated to 255 characters
> + * (ignoring surrogates).
> *
> * Return: number of printed characters
> */
> int efi_printk(const char *fmt, ...)
> {
> - char printf_buf[256];
> + efi_char16_t printf_buf[256];
> va_list args;
> int printed;
> int loglevel = printk_get_level(fmt);
> @@ -141,10 +63,11 @@ int efi_printk(const char *fmt, ...)
> fmt = printk_skip_level(fmt);
>
> va_start(args, fmt);
> - printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args);
> + printed = efi_vsnprintf(printf_buf, ARRAY_SIZE(printf_buf), fmt, args,
> + true);
> va_end(args);
>
> - efi_puts(printf_buf);
> + efi_char16_puts(printf_buf);
> if (printed >= sizeof(printf_buf)) {
You missed that sizeof().
Be nice to have a note about the size not being in bytes.
David