From nobody Sun Sep 27 03:13:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 056C73CCFD2; Sun, 6 Sep 2026 13:08:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700121; cv=none; b=lsZXGZ2HZmrgQdEO/zIf7zV7UucWlEk885aq8Dry2MwhxB9NwxqTytmyxMMr7Yp1iv5puoY8/0QHeSPKSNBYV3CxRDvqUhMqBTrts5dZ60iE2bwYVg/M5/7VwZQ3/qj4PvQddi9OzNyt6EaKh/n3a5QYpVxfJGcKyjCU6wxlhwI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700121; c=relaxed/simple; bh=u/lZldW/Cm68+dTChkN5vlteNqFXMe+bpIYz6xSd9LU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=daZdjlTNUeaZKElR7sGQbbH+zAA7BnOYqujPRdi7h4g51TOurP1shADWixGg4t57owyaPK6jZSKPaZofJm52/kM8Ytl6XysUgXqVGYHqelWOWeOMH2RaFQWr1VOXR+eXUjFUwNOFEDKtOvXGC7iB3zNh1QIyMZ7F+PpTWdz6118= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a8qUPKqz; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="a8qUPKqz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D9921F00A3A; Sun, 6 Sep 2026 13:08:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788700119; bh=0AGX0kN1kzsoE2xguvYOGm/uBS4C2JYRFcF18NAWHsY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=a8qUPKqzxPDptup6MRdxoRq0iardEBfVqF3GpKfoguYn7N2kmoGN8Ymq6CNlsmTj1 QzN9gwKM4U1qutbx3b9tue8RIYvh9uqZuZ6aWhHgivLXgZSvn+cI047dMk68GrCnBO QMyACk9hV6Mc1a5qieLrl6cNcJp521s1okoU2jY7WGcoba4tmYTwz2lxGQ2duQyP34 09BcXYTQJG6tAGjiGKDcnddrkfGAXW0LfGSaImiShiOMEG1p9BZGd9+i36yBaq/bu/ JozHWVXy6ryhsNSpsWAmtKHXAYt/MfGT6RcKQYjDtFtmmLuWKCFNljaiEGA0AdQU2W KGmLNLqUU+55g== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Vincent Mailhol , x86@kernel.org Subject: [PATCH 1/7] x86/boot: Drop pointless re-implementation of panic() Date: Sun, 6 Sep 2026 15:08:19 +0200 Message-ID: <20260906130817.1151961-10-ardb@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260906130817.1151961-9-ardb@kernel.org> References: <20260906130817.1151961-9-ardb@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2515; i=ardb@kernel.org; h=from:subject; bh=u/lZldW/Cm68+dTChkN5vlteNqFXMe+bpIYz6xSd9LU=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIWtu6pG1+6oumTF5z7nybepbmWDOmQF/OperLX96+W/AB +Z3a8IPdpSyMIhxMciKKbIIzP77bufpiVK1zrNkYeawMoEMYeDiFICJcNcxMhysO5Fe9euvzPL/ U9mOvzHfaVijInxH5ubq+hnpRxe/nPaL4Z9+2+qW4usasZVuC7fxvtwicCdqaWXWxG8VPM5KLzr TTnECAA== X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Ard Biesheuvel --- 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(-) diff --git a/arch/x86/boot/compressed/error.c b/arch/x86/boot/compressed/er= ror.c index 19a8251de506..ce5ed7d8265e 100644 --- a/arch/x86/boot/compressed/error.c +++ b/arch/x86/boot/compressed/error.c @@ -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 =3D vsnprintf(buf, sizeof(buf), fmt, args); - va_end(args); - - if (len && buf[len - 1] =3D=3D '\n') - buf[len - 1] =3D '\0'; - - error(buf); -} -#endif diff --git a/arch/x86/boot/compressed/error.h b/arch/x86/boot/compressed/er= ror.h index 31f9e080d61a..87062dea9a20 100644 --- a/arch/x86/boot/compressed/error.h +++ b/arch/x86/boot/compressed/error.h @@ -6,6 +6,5 @@ =20 void warn(const char *m); void error(char *m) __noreturn; -void panic(const char *fmt, ...) __noreturn __cold; =20 #endif /* BOOT_COMPRESSED_ERROR_H */ diff --git a/arch/x86/boot/compressed/mem.c b/arch/x86/boot/compressed/mem.c index 0e9f84ab4bdc..e1c017b55184 100644 --- a/arch/x86/boot/compressed/mem.c +++ b/arch/x86/boot/compressed/mem.c @@ -37,7 +37,7 @@ void arch_accept_memory(phys_addr_t start, phys_addr_t en= d) /* 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 { --=20 2.47.3 From nobody Sun Sep 27 03:13:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 891BA3CF673; Sun, 6 Sep 2026 13:08:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700122; cv=none; b=edx384d9VZ4PGFV2ikzNQAWYPU3jvK11hpJF/bP/d1pNHNNdrBGhUILWyAUOR6ak6Yntq0PDdA2rFyyvQ0txFWIDzm+3ubVUo/nqVLD7C/9C8sfdaHco6kGhjoJmSgL6q9+vITEGZ9pZ74pO3qPIiH+96fN0oJpUhDofjZNLW9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700122; c=relaxed/simple; bh=l4TB+2TeCuTlY0OJCVLsunoaHu72Xx1EIrdqOvisbI4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ktpQow6tc5xF6EteMWn8TdnCsou2Pcxf63uebOssONmrG04iJnZf0vYYjm+BgG9shmtmHRCGMjVuWBMEBjthHm9rtcIOtVglGnylFFqC+eeQj4gP7xpwuCabZGt8qMS7eitiUPSVl55kD6YX4dn2O2r060pBeQBPCjcTAuSlcLA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hPjAsSUO; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hPjAsSUO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 163881F00A3D; Sun, 6 Sep 2026 13:08:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788700121; bh=fyc8LQ/Z+NjkucxXu1s7ry/SopNvj9pgZ/5QwdnIHAw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hPjAsSUOu4gQwC6Zov7asFzpOt9/5sayUJxodHyx3h5yN9iwS/WwEvhCZhMqwt8cV snndP4OiabbTq5AUZHC8dINOBg7jHmr196GpDgnee9m8KWEH9ti90szAJY/pjU6JpY KohLFxzg+x7C/a+pSiYwpFpim5ZGUovw9AEqg0yP7IvJ3Jl3UAgdNcvgQ4MbVhhnrn yPD2dayoGd2ZCiDDKgJNrDTNsK9wnq9z69nim7sPrPuSzKtSQ04K3suwAPQtVBuYlq BVSHPg0D2OnW5g3vbJEPphPedyYQI8u5wwQHcnFcysq19amS5Y+/LY27grayCQe66L 6pd8CyOsPGDIA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Vincent Mailhol , x86@kernel.org Subject: [PATCH 2/7] lib/ucs2_string: Avoid WARN in library code Date: Sun, 6 Sep 2026 15:08:20 +0200 Message-ID: <20260906130817.1151961-11-ardb@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260906130817.1151961-9-ardb@kernel.org> References: <20260906130817.1151961-9-ardb@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=865; i=ardb@kernel.org; h=from:subject; bh=l4TB+2TeCuTlY0OJCVLsunoaHu72Xx1EIrdqOvisbI4=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIWtu6vFPC3xfFvst4Q2Wj/krGGwV6348aO6Jc91T3f/+v tBQ8/BwRykLgxgXg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjIoakM/6zrPr2aI52ZseLU kWXfWEX/nuP9PkvGWonxuTvzgiUTZ/1j+Ct6Nc/lrq1U0hvJ5q3cf2xcW5f47kpkMzAIneq/anv FAw4A X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It's not really the job of library code to WARN and potentially bring down the system (with panic_on_warn=3D1) on a condition that is really not something to obsess over. So drop the WARN_ON_ONCE() from ucs2_strscpy(). Signed-off-by: Ard Biesheuvel --- lib/ucs2_string.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ucs2_string.c b/lib/ucs2_string.c index 1f7dd4eb640a..d391c9d8eba5 100644 --- a/lib/ucs2_string.c +++ b/lib/ucs2_string.c @@ -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 =3D=3D 0 || WARN_ON_ONCE(count > INT_MAX / sizeof(*dst))) + if (count =3D=3D 0 || count > INT_MAX / sizeof(*dst)) return -E2BIG; =20 /* --=20 2.47.3 From nobody Sun Sep 27 03:13:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1EA43CF209; Sun, 6 Sep 2026 13:08:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700124; cv=none; b=d2XiyllTAy2aQaVD8g3xFu+uuNNAiioK8u+v/a+oeDh52k7PMGJz2T4+BUR/OVAjzoluuz6vA1WLAjDWQIqEywtSPZRxUUhzE8A/U/hPHAmTUYFnYb/2EhirovH1HxEM84KbbYQByYnrHKKYYTD6KMHTG68bJVtlOp0WtWP0yjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700124; c=relaxed/simple; bh=45M/Hsc6CXuQzJCiLuWB++v23YCdS474I4sUrpQUDiY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SafPdfgr+j04OS42fL2uF3QBFfzXyPiTakBaKeYmN149Vvlq6gUMh+tJjBZD8MZak5tKCt9tCe4LlSUhOlehBuW2JmECT14CP3tOpIacG5DKIo3+urkT9GOqHz1tk9Ju/6V13H1YQWgnTiYOtbckausd25O1FCROHS0WWeaXrSY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=msVQ5AH1; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="msVQ5AH1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92BA91F00A3A; Sun, 6 Sep 2026 13:08:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788700122; bh=yo2CnbwEpgDJetynsarnjAiwnB2/EA1CTVqiPjFuZs0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=msVQ5AH1vD4ykGh7fQO0VohKo8n8GMPzHp7RFJYe858kC8IJPo/IyseEODyu3wI1w 46w0e43npcW1J8Zfv6W8zp0pIlRaz1NXNT/t4aZrDSYFk7Dbeu//euMhrjZSEBaDwe bCOd7UOxXc/YcWoPwb/s35d+OxIbIUrOsTsT794lLfeWjLO+yc0tagMZFTwtKJ2S8X wA1C/kraLdCewr9McvFQtMKQ3OJpw4d8PUkUEHmuTrHwSCM0+u0gzuXLj7L+Hevc6y 3jGxuQiiwhSrX0uWQeeOEiLUp+i3RwSIKNcKqQZzAUHZks39uldGf6XTOfKyoLgr+I o5RCieT/E3gQA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Vincent Mailhol , x86@kernel.org Subject: [PATCH 3/7] efi/libstub: Use ucs2_string library for UTF-16 to UTF-8 conversion Date: Sun, 6 Sep 2026 15:08:21 +0200 Message-ID: <20260906130817.1151961-12-ardb@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260906130817.1151961-9-ardb@kernel.org> References: <20260906130817.1151961-9-ardb@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4985; i=ardb@kernel.org; h=from:subject; bh=45M/Hsc6CXuQzJCiLuWB++v23YCdS474I4sUrpQUDiY=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIWtu6omG/H23Bf9nBu6Q4kvjOWQp8XDxd+6qBUxp56ysD nq856rrKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABMR4GFk+H33/wKtnus3A77P 0tW9qnpii4sI682Jt/fNMV79zEi5bCXD/yh7pf+ZHDah+QIvg9Z9PNFotNf3Cl/l69a2SVzvTBR 4WQA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. While at it, make cmdline_addr a char* and remove the pointless casts. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/Makefile | 3 +- drivers/firmware/efi/libstub/efi-stub-helper.c | 59 ++++++-------------- drivers/firmware/efi/libstub/vsprintf.c | 11 ---- 3 files changed, 18 insertions(+), 55 deletions(-) diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/l= ibstub/Makefile index 77a2b2d74f3f..12c0c7deb5cb 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -66,7 +66,8 @@ KBUILD_AFLAGS :=3D $(KBUILD_CFLAGS) -D__ASSEMBLY__ lib-y :=3D 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 =20 # include the stub's libfdt dependencies from lib/ when needed libfdt-deps :=3D fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \ diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmw= are/efi/libstub/efi-stub-helper.c index f27f2e1f0019..c221d67bfff6 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include =20 @@ -335,10 +336,10 @@ char *efi_convert_cmdline(efi_loaded_image_t *image) const efi_char16_t *options =3D efi_table_attr(image, load_options); u32 options_size =3D efi_table_attr(image, load_options_size); int options_bytes =3D 0, safe_options_bytes =3D 0; /* UTF-8 bytes */ - unsigned long cmdline_addr =3D 0; const efi_char16_t *s2; bool in_quote =3D false; efi_status_t status; + char *cmdline_addr; u32 options_chars; =20 if (options_size > 0) @@ -351,45 +352,18 @@ char *efi_convert_cmdline(efi_loaded_image_t *image) if (options) { s2 =3D options; while (options_bytes < COMMAND_LINE_SIZE && options_chars--) { - efi_char16_t c =3D *s2++; - - if (c < 0x80) { - if (c =3D=3D L'\0' || c =3D=3D L'\n') - break; - if (c =3D=3D L'"') - in_quote =3D !in_quote; - else if (!in_quote && isspace((char)c)) - safe_options_bytes =3D 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 +=3D 2 + (c >=3D 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) =3D=3D 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 -=3D 3; - } else if ((*s2 & 0xfc00) =3D=3D 0xdc00) { - options_bytes++; - options_chars--; - s2++; - } - } + efi_char16_t c[2] =3D { *s2++, L'\0' }; + + if (c[0] =3D=3D L'\0' || c[0] =3D=3D L'\n') + break; + + // Check whether the current position is a safe + // truncation point + in_quote ^=3D (c[0] =3D=3D L'"'); + if (!in_quote && isspace((char)c[0])) + safe_options_bytes =3D options_bytes; + + options_bytes +=3D ucs2_utf8size(c); } if (options_bytes >=3D COMMAND_LINE_SIZE) { options_bytes =3D safe_options_bytes; @@ -405,10 +379,9 @@ char *efi_convert_cmdline(efi_loaded_image_t *image) if (status !=3D EFI_SUCCESS) return NULL; =20 - snprintf((char *)cmdline_addr, options_bytes, "%.*ls", - options_bytes - 1, options); + ucs2_as_utf8(cmdline_addr, options, options_bytes); =20 - return (char *)cmdline_addr; + return cmdline_addr; } =20 /** diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi= /libstub/vsprintf.c index 71c71c222346..dba136679172 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -551,14 +551,3 @@ int vsnprintf(char *buf, size_t size, const char *fmt,= va_list ap) =20 return pos; } - -int snprintf(char *buf, size_t size, const char *fmt, ...) -{ - va_list args; - int i; - - va_start(args, fmt); - i =3D vsnprintf(buf, size, fmt, args); - va_end(args); - return i; -} --=20 2.47.3 From nobody Sun Sep 27 03:13:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B7603CDBC3; Sun, 6 Sep 2026 13:08:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700125; cv=none; b=Z0pmwX8wylOKnt7vK08kefKHw9YSWH+olPD5x4sYw20xDoIxPPQtlTa1iexTLaFFhg9a+fgBL/jt3B70+OzW6oAL5stjHArANEPTbqdReJvO/q88yRn+o5SDR1QD711wR65KO+I21SaN9mCIQy2qhnva31iHWOTaYEk9zOg75ig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700125; c=relaxed/simple; bh=6Jy+fsLEkRLfCSUAx6Uz1rko/nvUdEtvDWVFGbNxBuY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rbjc6MhGGp/+WQoPX3ArQKU5GIlAl1EBoFEK67LXIozUZjhlI643NcptvKD7i/Nw0AJnmk3M/1ko1enR0I2NbTtz/1admPK591MW4r6dGLEY/DtbRk/uhcbTVhGpfGNdfTLvbjwiHwFpUlEEMRax+GJq7dLnaqZexchjRrvdpx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I+pIYcN0; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="I+pIYcN0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1ADB21F00A3D; Sun, 6 Sep 2026 13:08:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788700124; bh=GioO1TjVhWaD4hx3LEmglO1WHROdxiVW7/aupMqAvvk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=I+pIYcN01GJPuSSy1m6gWE+609HW68oj3ZqUrMz8qZyuOdvIP6UTwXm4q/mjfJ7fO Abn2U3IfFOm0DX4P19lMeXS9ldPhtDRhn130iVLi56AWnLR8VzRKsoCAJ5h2b57XXp O6Y2N2LucMoxK7SdTH0rDqYVBz0itOO4fNURqJIM1zD1AcIlCS2E2obP2GveV3t63h Jwg+SZ4Gx3fCCVOOOcFbfWTi+v958D9Iq7tYe8NnuteYhFIHDmIyGaMtS+TNKesaat +1r7FSPDbYXbRCO11lCgQJLYWPpHmFyoyXAvcM2MJ2NcYsT0q91bGrhIPlwCTl3YxL 3vMJaFR4ncE7A== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Vincent Mailhol , x86@kernel.org Subject: [PATCH 4/7] efi/libstub: Avoid efi_puts() for compile time constant strings Date: Sun, 6 Sep 2026 15:08:22 +0200 Message-ID: <20260906130817.1151961-13-ardb@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260906130817.1151961-9-ardb@kernel.org> References: <20260906130817.1151961-9-ardb@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2145; i=ardb@kernel.org; h=from:subject; bh=6Jy+fsLEkRLfCSUAx6Uz1rko/nvUdEtvDWVFGbNxBuY=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIWtu6qnYlGz2vUFxgrdEGlSWWHL+e/9Fpuip6YUe3ZsFP dkVe/51lLIwiHExyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIkEtDMyHL3K8+LDll1bvdmF 58bXHm5JdT9attzKqybs4UzRTxmrNjMyNCS2Jq64uE3sndyXyBOXy+cev7ji67cXRvvk9KUFX8w y5QUA X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/gop.c | 6 +++--- drivers/firmware/efi/libstub/printk.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/firmware/efi/libstub/gop.c b/drivers/firmware/efi/libs= tub/gop.c index 80dc8cfeb33e..6919e28ba92b 100644 --- a/drivers/firmware/efi/libstub/gop.c +++ b/drivers/firmware/efi/libstub/gop.c @@ -309,12 +309,12 @@ static u32 choose_mode_list(efi_graphics_output_proto= col_t *gop) efi_status_t status; =20 efi_printk("Available graphics modes are 0-%u\n", max_mode-1); - efi_puts(" * =3D current mode\n" - " - =3D unusable mode\n"); + efi_char16_puts(L" * =3D current mode\r\n" + " - =3D unusable mode\r\n"); =20 choose_mode(gop, match_list, (void *)cur_mode); =20 - 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 =3D efi_wait_for_key(10 * EFI_USEC_PER_SEC, &key); if (status !=3D EFI_SUCCESS && status !=3D EFI_TIMEOUT) { efi_err("Unable to read key, continuing in 10 seconds\n"); diff --git a/drivers/firmware/efi/libstub/printk.c b/drivers/firmware/efi/l= ibstub/printk.c index bc599212c05d..f36639886d00 100644 --- a/drivers/firmware/efi/libstub/printk.c +++ b/drivers/firmware/efi/libstub/printk.c @@ -136,7 +136,7 @@ int efi_printk(const char *fmt, ...) return 0; =20 if (loglevel >=3D 0) - efi_puts("EFI stub: "); + efi_char16_puts(L"EFI stub: "); =20 fmt =3D printk_skip_level(fmt); =20 @@ -146,7 +146,7 @@ int efi_printk(const char *fmt, ...) =20 efi_puts(printf_buf); if (printed >=3D sizeof(printf_buf)) { - efi_puts("[Message truncated]\n"); + efi_char16_puts(L"[Message truncated]\r\n"); return -1; } =20 --=20 2.47.3 From nobody Sun Sep 27 03:13:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9AD43CCFD2; Sun, 6 Sep 2026 13:08:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700128; cv=none; b=hqXcMchQNPb12/SiUHO/ASCtkNOSKOMaD5aE3/8eHaOfPhaMIH2G24UJTOWage2ZuAMERoc5/qzXy+sV1Jg3v3KcFbqWRzoBDYcBAPs/yX5OWBirTyMP204jRmGCKrgj5VpSwFfQNaIluzksa0uTowIaa/IGqLOqUEvertYCEcI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700128; c=relaxed/simple; bh=uaC1WgWvXsjlrDW1h2SQSTAdv5SZYBB92Q2bmEIYDMs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fNqANonn0Pr9oaR13i6Vll0PiAmNhEISFxvyVLtaFc5AKcQY5E38G+/Ykm7vxbe+snU/4ZnAIlXbSJrzY2RFuRhy/+908/Dqlqgye8F6BuzQDf/n7upahpllYlB88pTI+6DTLAp+IfIQYd4WdhlGOCTCas8Jbz5dntPJipo4pqM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CNKYN93K; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CNKYN93K" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 976701F00A3A; Sun, 6 Sep 2026 13:08:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788700125; bh=BG7Q/C2K5vK3m3pC+ZGYD64g5zvWsaUSPh5Sz0kyUbw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CNKYN93KOTPggdtdSIQktE9piOJLT8+RrfqU6gBpOEmMZHqiD2dvsUHqVazug+aTj hE6+B9g8D+u1VkZN9vwo1LG6qCoKcT6s9iXTziui7/elgmuO7KPeVJRttzNxvSaTfc p4RkE/a8ggyrV1LGmL+9tUKh8xjKZqhEaYlb639lZ7N5nU/x/iEggcT4AT4n5AWZRV CUOp+Cqu1AyhtdVIPatj4qcv8QOdousQel6aNMEjSRbFNE7cCCE4trW3H+vcaey6Qv 6OP2oZKQeZEqelu5ADJ6wkrfGdL6aBRz7dysCvgHsM05h0C01LJp4PZOTXTMrydC1k GI8P0c3wQX3/A== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Vincent Mailhol , x86@kernel.org Subject: [PATCH 5/7] efi/libstub: Output UTF-16 directly from vsnprintf() Date: Sun, 6 Sep 2026 15:08:23 +0200 Message-ID: <20260906130817.1151961-14-ardb@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260906130817.1151961-9-ardb@kernel.org> References: <20260906130817.1151961-9-ardb@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=9424; i=ardb@kernel.org; h=from:subject; bh=uaC1WgWvXsjlrDW1h2SQSTAdv5SZYBB92Q2bmEIYDMs=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIWtu6plVcaH23FE3/v4Jm7LtPe9DIaGtgpVcb7U5a1xu8 2q/9BPrKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABO5fZuRYcbjtA1/WvjPMXpr pLzxPeTX7TH5wPudyaIZPCoxew81RjL84a1RFVOU1GCc5qrs/uuFxp5V4mLnO652qyz2FLo+M0a SHQA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- drivers/firmware/efi/libstub/efistub.h | 5 +- drivers/firmware/efi/libstub/printk.c | 89 ++---------------- drivers/firmware/efi/libstub/vsprintf.c | 94 +++----------------- 3 files changed, 19 insertions(+), 169 deletions(-) diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/= libstub/efistub.h index fd91fc15ec81..36056c624782 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -1078,9 +1078,10 @@ efi_status_t check_platform_features(void); =20 void *get_efi_config_table(efi_guid_t guid); =20 -/* 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); =20 __printf(1, 2) int efi_printk(char const *fmt, ...); =20 diff --git a/drivers/firmware/efi/libstub/printk.c b/drivers/firmware/efi/l= ibstub/printk.c index f36639886d00..09476a6d564e 100644 --- a/drivers/firmware/efi/libstub/printk.c +++ b/drivers/firmware/efi/libstub/printk.c @@ -23,98 +23,20 @@ void efi_char16_puts(efi_char16_t *str) output_string, str); } =20 -static -u32 utf8_to_utf32(const u8 **s8) -{ - u32 c32; - u8 c0, cx; - size_t clen, i; - - c0 =3D cx =3D *(*s8)++; - /* - * The position of the most-significant 0 bit gives us the length of - * a multi-octet encoding. - */ - for (clen =3D 0; cx & 0x80; ++clen) - cx <<=3D 1; - /* - * If the 0 bit is in position 8, this is a valid single-octet - * encoding. If the 0 bit is in position 7 or positions 1-3, the - * encoding is invalid. - * In either case, we just return the first octet. - */ - if (clen < 2 || clen > 4) - return c0; - /* Get the bits from the first octet. */ - c32 =3D cx >> clen--; - for (i =3D 0; i < clen; ++i) { - /* Trailing octets must have 10 in most significant bits. */ - cx =3D (*s8)[i] ^ 0x80; - if (cx & 0xc0) - return c0; - c32 =3D (c32 << 6) | cx; - } - /* - * Check for validity: - * - The character must be in the Unicode range. - * - It must not be a surrogate. - * - It must be encoded using the correct number of octets. - */ - if (c32 > 0x10ffff || - (c32 & 0xf800) =3D=3D 0xd800 || - clen !=3D (c32 >=3D 0x80) + (c32 >=3D 0x800) + (c32 >=3D 0x10000)) - return c0; - *s8 +=3D clen; - return c32; -} - -/** - * efi_puts() - Write a UTF-8 encoded string to the console - * @str: UTF-8 encoded string - */ -void efi_puts(const char *str) -{ - efi_char16_t buf[128]; - size_t pos =3D 0, lim =3D ARRAY_SIZE(buf); - const u8 *s8 =3D (const u8 *)str; - u32 c32; - - while (*s8) { - if (*s8 =3D=3D '\n') - buf[pos++] =3D L'\r'; - c32 =3D utf8_to_utf32(&s8); - if (c32 < 0x10000) { - /* Characters in plane 0 use a single word. */ - buf[pos++] =3D c32; - } else { - /* - * Characters in other planes encode into a surrogate - * pair. - */ - buf[pos++] =3D (0xd800 - (0x10000 >> 10)) + (c32 >> 10); - buf[pos++] =3D 0xdc00 + (c32 & 0x3ff); - } - if (*s8 =3D=3D '\0' || pos >=3D lim - 2) { - buf[pos] =3D L'\0'; - efi_char16_puts(buf); - pos =3D 0; - } - } -} - /** * 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 =3D printk_get_level(fmt); @@ -141,10 +63,11 @@ int efi_printk(const char *fmt, ...) fmt =3D printk_skip_level(fmt); =20 va_start(args, fmt); - printed =3D vsnprintf(printf_buf, sizeof(printf_buf), fmt, args); + printed =3D efi_vsnprintf(printf_buf, ARRAY_SIZE(printf_buf), fmt, args, + true); va_end(args); =20 - efi_puts(printf_buf); + efi_char16_puts(printf_buf); if (printed >=3D sizeof(printf_buf)) { efi_char16_puts(L"[Message truncated]\r\n"); return -1; diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi= /libstub/vsprintf.c index dba136679172..9ac6df268105 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -14,10 +14,12 @@ =20 #include #include +#include #include #include #include #include +#include =20 static int skip_atoi(const char **s) @@ -239,58 +241,6 @@ char get_sign(long long *num, int flags) return 0; } =20 -static -size_t utf16s_utf8nlen(const u16 *s16, size_t maxlen) -{ - size_t len, clen; - - for (len =3D 0; len < maxlen && *s16; len +=3D clen) { - u16 c0 =3D *s16++; - - /* First, get the length for a BMP character */ - clen =3D 1 + (c0 >=3D 0x80) + (c0 >=3D 0x800); - if (len + clen > maxlen) - break; - /* - * If this is a high surrogate, and we're already at maxlen, we - * can't include the character if it's a valid surrogate pair. - * Avoid accessing one extra word just to check if it's valid - * or not. - */ - if ((c0 & 0xfc00) =3D=3D 0xd800) { - if (len + clen =3D=3D maxlen) - break; - if ((*s16 & 0xfc00) =3D=3D 0xdc00) { - ++s16; - ++clen; - } - } - } - - return len; -} - -static -u32 utf16_to_utf32(const u16 **s16) -{ - u16 c0, c1; - - c0 =3D *(*s16)++; - /* not a surrogate */ - if ((c0 & 0xf800) !=3D 0xd800) - return c0; - /* invalid: low surrogate instead of high */ - if (c0 & 0x0400) - return 0xfffd; - c1 =3D **s16; - /* invalid: missing low surrogate */ - if ((c1 & 0xfc00) !=3D 0xdc00) - return 0xfffd; - /* valid surrogate pair */ - ++(*s16); - return (0x10000 - (0xd800 << 10) - 0xdc00) + (c0 << 10) + c1; -} - #define PUTC(c) \ do { \ if (pos < size) \ @@ -298,7 +248,8 @@ do { \ ++pos; \ } while (0); =20 -int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) +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]; @@ -336,6 +287,8 @@ int vsnprintf(char *buf, size_t size, const char *fmt, = va_list ap) =20 for (pos =3D 0; *fmt; ++fmt) { if (*fmt !=3D '%' || *++fmt =3D=3D '%') { + if (crlf && *fmt =3D=3D '\n') + PUTC('\r'); PUTC(*fmt); continue; } @@ -400,7 +353,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, = va_list ap) else if (qualifier =3D=3D 'l') { wstring: flags |=3D WIDE; - precision =3D len =3D utf16s_utf8nlen((const u16 *)s, precision); + precision =3D len =3D ucs2_strnlen((const u16 *)s, precision); goto output; } precision =3D len =3D strnlen(s, precision); @@ -505,36 +458,9 @@ int vsnprintf(char *buf, size_t size, const char *fmt,= va_list ap) if (flags & WIDE) { const u16 *ws =3D (const u16 *)s; =20 - while (len-- > 0) { - u32 c32 =3D utf16_to_utf32(&ws); - u8 *s8; - size_t clen; - - if (c32 < 0x80) { - PUTC(c32); - continue; - } - - /* Number of trailing octets */ - clen =3D 1 + (c32 >=3D 0x800) + (c32 >=3D 0x10000); - - len -=3D clen; - s8 =3D (u8 *)&buf[pos]; - - /* Avoid writing partial character */ - PUTC('\0'); - pos +=3D clen; - if (pos >=3D size) - continue; - - /* Set high bits of leading octet */ - *s8 =3D (0xf00 >> 1) >> clen; - /* Write trailing octets in reverse order */ - for (s8 +=3D clen; clen; --clen, c32 >>=3D 6) - *s8-- =3D 0x80 | (c32 & 0x3f); - /* Set low bits of leading octet */ - *s8 |=3D c32; - } + if (pos < size) + ucs2_strscpy(&buf[pos], ws, min(len, size - pos)); + pos +=3D len; } else { while (len-- > 0) PUTC(*s++); --=20 2.47.3 From nobody Sun Sep 27 03:13:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5030B3CF20C; Sun, 6 Sep 2026 13:08:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700129; cv=none; b=sdxw6d1Cztxezjm2Q3iIFUTarvvoYSVLAwQHyxgFpA907Yqrw72uBV2lUbTPmWmOasuVNHbxVXUDjBQ7HKMw+wx4OJKw+7yiWTlrtq4JKYM6EwQ8hIeFX6mg8a1L/b/0eOStXvwrlJz0eoly1V69FObrpkWb6Z2YY5fFhoSPduo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700129; c=relaxed/simple; bh=iboO10g3Ee6nqq95gXfzyoo/IppyePzufMy4zEa754I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=No2nLnllnXmfteyuZGJIJ2YpZhPH5jvbGpgr/1H8mCDLYVW+EtlkRnDbeHmIBolV+h7Ka03FiMf5AmJJ7mTZ42qj5qL1JNiOiVUwji43FVHnZheE/50L2oLDF+GkIuk2AXK7a7OVySbBwhg+PyTrCv8lpOHS9QGBxPVBFzF1ukI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OhAav7vB; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OhAav7vB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1FB641F00A3D; Sun, 6 Sep 2026 13:08:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788700127; bh=ypS4GaCWPY2eWch613jft148kE4UGIa+o3+eY6E2KWk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OhAav7vB+QHZXfe2G/+S6Bc84xoSJbFkVup32E0mCjXZwCtRpSCviXTdTKVFauyQQ Kmu8R3PrrItiyzP7hqL7wNEqywC1l8x4JMN4mdzJ/EIUC7ITBbgughNFEPibEr36jH DeX3oKAJLalxNSJnXLHBRKoiKzb6McT6s1X1DIEbxX5vZkGMmizdU/Y0rs/Dz/ZLmp mmXlo6ransHcTamfyfFd8shHkiOiuolt5o5nUDmV/CmikaFZxbMybanhSzcAnlCbAs rcZOjXMZDhYaeVHceFXHrPfsmAi6+HNxH+KJij6Be9uNGJ/ncok6PpxDcYWImD1Oti 3x3dUaQBptlYA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Vincent Mailhol , x86@kernel.org Subject: [PATCH 6/7] efi/libstub: Add support for printing human readable GUIDs Date: Sun, 6 Sep 2026 15:08:24 +0200 Message-ID: <20260906130817.1151961-15-ardb@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260906130817.1151961-9-ardb@kernel.org> References: <20260906130817.1151961-9-ardb@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2941; i=ardb@kernel.org; h=from:subject; bh=iboO10g3Ee6nqq95gXfzyoo/IppyePzufMy4zEa754I=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIWtu6rnL7+Z1NZhIzdYsSOqSjYz41nlAQa3nmBfTn/3iC xlXic3oKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABNpN2NkuF0rO2V1NMvf1GDz 8j7nf0Us+ny75e5JZ5d98VN/pbl+DsNfySi13fu/C39Yp27hIbFMdek2kwrfZ6yTVhW7zDRaeuY zEwA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Co-developed-by: Vincent Mailhol Signed-off-by: Vincent Mailhol Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/vsprintf.c | 40 +++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi= /libstub/vsprintf.c index 9ac6df268105..4c11cee6c722 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -111,6 +111,9 @@ char *put_dec(char *end, unsigned long long n) return p; } =20 +/* we are called with base 8, 10 or 16, only, thus don't need "G..." */ +static const char digits[16] =3D "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVW= XYZ"; */ + static char *number(char *end, unsigned long long num, int base, char locase) { @@ -119,9 +122,6 @@ char *number(char *end, unsigned long long num, int bas= e, char locase) * produces same digits or (maybe lowercased) letters */ =20 - /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ - static const char digits[16] =3D "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUV= WXYZ"; */ - switch (base) { case 10: if (num !=3D 0) @@ -142,6 +142,29 @@ char *number(char *end, unsigned long long num, int ba= se, char locase) return end; } =20 +static char *guid_to_str(const efi_guid_t *guid, char *out, char locase) +{ + static const u8 guid_index[UUID_SIZE] =3D { + 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15, + }; + + for (int i =3D 0, p =3D 0; i < ARRAY_SIZE(guid_index); i++) { + u8 byte =3D guid->b[guid_index[i]]; + + out[p++] =3D locase | digits[byte >> 4]; + out[p++] =3D locase | digits[byte & 0xf]; + + switch (i) { + case 3: + case 5: + case 7: + case 9: + out[p++] =3D '-'; + } + } + return out; +} + #define ZEROPAD 1 /* pad with zero */ #define SIGN 2 /* unsigned/signed long */ #define PLUS 4 /* show plus */ @@ -251,8 +274,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 =3D &tmp[ARRAY_SIZE(tmp)]; long long num; int base; @@ -365,6 +387,14 @@ int efi_vsnprintf(efi_char16_t *buf, size_t size, cons= t char *fmt, va_list ap, break; =20 case 'p': + if (fmt[1] =3D=3D 'U' && (fmt[2] | 0x20) =3D=3D 'l') { + flags &=3D LEFT; + s =3D guid_to_str(va_arg(args, efi_guid_t *), tmp, fmt[2] & 0x20); + precision =3D len =3D UUID_STRING_LEN; + fmt +=3D 2; + goto output; + } + if (precision < 0) precision =3D 2 * sizeof(void *); fallthrough; --=20 2.47.3 From nobody Sun Sep 27 03:13:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 088383CE49E; Sun, 6 Sep 2026 13:08:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700130; cv=none; b=LbgQaFC1C3Ad7fzuJbBCnV4JYC3J5aVmbMnft1yH0IKpLspGKR67vvQ2PKFjWrqggaTM6NxINIa2f3ny4i8jknS0mdbBnCB0h+iLfBsoQ6paHWdxy8O3BEEPUQNzX1xNF6apVy6zg9HHkQRENejdTZAZZdmJavUy/xi9k3fldYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788700130; c=relaxed/simple; bh=h1rkn8iLXywePMTn/eVvCM8jJqNXN/zvpLjGK71QOc8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=akhG0oBID54jOj8LoM0NGtd3qdMddjInZTUPUyYjA+hGyjrJOP4S9a/jhih2xfBH7YaD5i+YPAgB2P03/0SQR+V6GRW/kL3LStfg8iIKNqVJbdltttuDji2pnrQT6BrYDP209tXncPQv6sSm06pDOIOK8XibGcMZ4wTLn5pt0UY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=G0Qz3FWc; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="G0Qz3FWc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BC241F00A3E; Sun, 6 Sep 2026 13:08:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788700128; bh=rq+Ezv3ECindkaMlHExJ+Xf+msAjcLNpA2hBCU14dJw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=G0Qz3FWcjLgmTHp5OzHVXggaFaCPBF1KkIai60D5XNxIumDDGZhZz0ceK1rw0P5Dy EWBzHaKIu72bl5S98rtKEn/ww+/ApZRkkE2NfRSZ0QpEdB/Q8jScGpWTjXnjiWarAK s0xkuPx7Iv9Z/+++2DH8Fbi3oolevLo4kJjtBMaRTAWmpEXCeJW4/FbcTSYhTsm2UN mENYE8aB7pjI2caxWPhE68s+5Y/o2WcY48DxMJbDeepBggKEGlhRt5stbmB5bjTYHa GyPjs6vKCXxhTOW3XVgGp4UTQU9IGnngbCwP92kIumeK3Yjund1QpK/HuDUQQgt7d6 z76Tp5H9Wr3nA== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Vincent Mailhol , x86@kernel.org Subject: [PATCH 7/7] efi/libstub: Add efi_snprintf() to construct wide strings Date: Sun, 6 Sep 2026 15:08:25 +0200 Message-ID: <20260906130817.1151961-16-ardb@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260906130817.1151961-9-ardb@kernel.org> References: <20260906130817.1151961-9-ardb@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1771; i=ardb@kernel.org; h=from:subject; bh=h1rkn8iLXywePMTn/eVvCM8jJqNXN/zvpLjGK71QOc8=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIWtu6nmnh3tz1jQ9VzEy6+ObxqXJ0BV9oZND0Nq8z86P5 e7O1yc7SlkYxLgYZMUUWQRm/3238/REqVrnWbIwc1iZQIYwcHEKwEScDzIyNCwsdS13Yl+zzqJ+ mqxotaXPBT8JhebGSyvnsuhtnDBPm5Gh/Y+DpwFvY3vGta0RMc8PPFzFNEn/xNRbKQmrffZ4b/v IAgA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Link: https://lore.kernel.org/all/20260903-efi_stub_bli-v2-1-dbf7ba915117@k= ernel.org/ Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/efistub.h | 1 + drivers/firmware/efi/libstub/vsprintf.c | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/= libstub/efistub.h index 36056c624782..880c1d0c464b 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -1084,6 +1084,7 @@ int efi_vsnprintf(efi_char16_t *buf, size_t size, con= st char *fmt, va_list ap, bool crlf); =20 __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, ...); =20 void efi_free(unsigned long size, unsigned long addr); DEFINE_FREE(efi_pool, void *, if (_T) efi_bs_call(free_pool, _T)); diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi= /libstub/vsprintf.c index 4c11cee6c722..0c1000ccfe71 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -507,3 +507,14 @@ int efi_vsnprintf(efi_char16_t *buf, size_t size, cons= t char *fmt, va_list ap, =20 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 =3D efi_vsnprintf(buf, size, fmt, args, false); + va_end(args); + return i; +} --=20 2.47.3 From nobody Sun Sep 27 03:13:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E939D4A4989; Tue, 8 Sep 2026 17:26:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788888376; cv=none; b=Y5Qb+lLC9U2KwSRS07zZbs4zfmyk4dmi6HUZEqQZPOFLMIZ1MvdXQrXK/csYqT8kWbT5CJwLeu2+53XfNUYqfXJMlJqD7tu8Fa3Y0tbF8DCH5gORrKTYdM0z4XBnh4gyrEuPx+5pgeMyibPMCN+dz3zpCaEmPFA0PQeTLxm3U84= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788888376; c=relaxed/simple; bh=OS6GckkaKtUv/HWuqL0G/2QoFTSf6JP2z/baJIhCLf0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t3KUWWayjLKYxYQ/0vAJa0VMqlO36hvNsO7nK4dLlGp5Loir+BbBfqVtMWGyyrf/2M2gfOh6ZG1K0C+2YtonMCUeh7LKwULbhKhlV5sK1d0XbuABChCzvcjmKMBfgvzXRmD8AgKsyRnHacRsVMQwMO+VNBBZ+1Dv945logHJ7qM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bb/wUR2w; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bb/wUR2w" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C9DE81F00A3A; Tue, 8 Sep 2026 17:26:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788888374; bh=jM3P1SONxa42AJzxhJ25bnkVtmobAHaArz1/6Ajlj0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bb/wUR2w8sqwWsO38QEbnn1fVtayV+qDnb5wC7JB5qlgFJ6pu8srUgZGwCDisg3LC /KMqH+jSg4BKfEqV4CiMc99xasVRnbfYVhFKKO/QECoQZfd7IOndcB7xCCNnJipUAs qmzdw/rsCx7MwZEkRccucn3xpYBMmobfY6eX6mPQTAS4QM/uXFvtaGEeOE2MvNsO4A oXCCGPD4Nh8jCAvTxP9IVwsOA1OL+stqVlV4QtFhWyYMpyyOzS9qs8syNZDcdLPpuy B5iqPw/OYSdc1UwDXbNqK3Lapp86DKRmA+ZJTTPDeA9Askkg2JlQskUHKMnmL9KrTQ POJnOsuEZsm0A== From: Vincent Mailhol To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, Vincent Mailhol Subject: [PATCH] efi: pass NUL-inclusive sizes to ucs2_as_utf8() Date: Tue, 8 Sep 2026 19:24:34 +0200 Message-ID: <20260908172555.3356-1-mailhol@kernel.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260906130817.1151961-9-ardb@kernel.org> References: <20260906130817.1151961-9-ardb@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" An upcoming change will update ucs2_as_utf8() to expose a strscpy() style API where the size argument is the destination buffer size, including space for the final NUL terminator. Some EFI callers currently pass the exact number of UTF-8 payload bytes that they expect to copy and add the terminator themselves afterwards. Extend those sizes to include the final NUL terminator so the upcoming contract change does not truncate the converted output by one byte. Signed-off-by: Vincent Mailhol --- Hi Ard, I saw that you pushed on efi-libstub-native-utf16 WIP branch [1] and did some testing, despite those changes not yet submitted for review. There is an off-by-one error following your ucs2_as_utf8() code refactor. This patch prevents the issue. It should be cherry-picked just before your "efi/libstub: Use ucs2_string library for UTF-16 to UTF-8 conversion" commit. There is one final off-by-one in "efi/libstub: Use ucs2_string library for UTF-16 to UTF-8 conversion" itself. This is the fix: ---8<--- diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firm= ware/efi/libstub/efi-stub-helper.c index e9b714ca811db..db0bde514f7fd 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -379,8 +379,7 @@ char *efi_convert_cmdline(efi_loaded_image_t *image) if (status !=3D EFI_SUCCESS) return NULL; - ucs2_as_utf8(cmdline_addr, options, options_bytes - 1); - cmdline_addr[options_bytes - 1] =3D '\0'; + ucs2_as_utf8(cmdline_addr, options, options_bytes); return cmdline_addr; } ---8<--- [1] https://git.kernel.org/pub/scm/linux/kernel/git/ardb/linux.git/log/?h= =3Defi-libstub-native-utf16 --- drivers/firmware/efi/efi.c | 2 +- fs/efivarfs/vars.c | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index 6d987d7f97781..221804e7d5390 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -304,7 +304,7 @@ static __init int efivar_ssdt_load(void) } =20 limit =3D min(EFIVAR_SSDT_NAME_MAX, name_size); - ucs2_as_utf8(utf8_name, name, limit - 1); + ucs2_as_utf8(utf8_name, name, limit); if (strncmp(utf8_name, efivar_ssdt, limit) !=3D 0) continue; =20 diff --git a/fs/efivarfs/vars.c b/fs/efivarfs/vars.c index 6833c3d24b541..1ddc89e13518f 100644 --- a/fs/efivarfs/vars.c +++ b/fs/efivarfs/vars.c @@ -237,7 +237,7 @@ efivar_get_utf8name(const efi_char16_t *name16, efi_gui= d_t *vendor) if (!name) return NULL; =20 - ucs2_as_utf8(name, name16, len); + ucs2_as_utf8(name, name16, len + 1); =20 name[len] =3D '-'; =20 @@ -264,8 +264,7 @@ efivar_validate(efi_guid_t vendor, efi_char16_t *var_na= me, u8 *data, if (!utf8_name) return false; =20 - ucs2_as_utf8(utf8_name, var_name, utf8_size); - utf8_name[utf8_size] =3D '\0'; + ucs2_as_utf8(utf8_name, var_name, utf8_size + 1); =20 for (i =3D 0; variable_validate[i].name[0] !=3D '\0'; i++) { const char *name =3D variable_validate[i].name; --=20 2.43.0