diff mbox series

[v2,1/3] efi: Add a generic helper to convert strings to unicode

Message ID 20220729194532.228403-2-gpiccoli@igalia.com
State New
Headers show
Series The UEFI panic notification mechanism, 2nd round | expand

Commit Message

Guilherme G. Piccoli July 29, 2022, 7:45 p.m. UTC
Currently both efi-pstore and efibc rely in simple for-loops to convert
from regular char pointers to u16/unicode strings; so let's export this
functionality as a helper to common EFI code to prevent code duplication.
This helper will also be used in a subsequent patch (adding a new module).

Notice that efi-pstore didn't write the end NULL char in the unicode
string before this patch, but this should not change anything in a
relevant way for this module - other than that, no functional change is
expected.

Signed-off-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
---
 drivers/firmware/efi/efi-pstore.c |  4 +---
 drivers/firmware/efi/efibc.c      |  9 ++++-----
 include/linux/efi.h               | 17 +++++++++++++++++
 3 files changed, 22 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/drivers/firmware/efi/efi-pstore.c b/drivers/firmware/efi/efi-pstore.c
index 3bddc152fcd4..965a4c0d6e60 100644
--- a/drivers/firmware/efi/efi-pstore.c
+++ b/drivers/firmware/efi/efi-pstore.c
@@ -167,7 +167,6 @@  static int efi_pstore_write(struct pstore_record *record)
 	char name[DUMP_NAME_LEN];
 	efi_char16_t efi_name[DUMP_NAME_LEN];
 	efi_status_t status;
-	int i;
 
 	record->id = generic_id(record->time.tv_sec, record->part,
 				record->count);
@@ -180,8 +179,7 @@  static int efi_pstore_write(struct pstore_record *record)
 		 (long long)record->time.tv_sec,
 		 record->compressed ? 'C' : 'D');
 
-	for (i = 0; i < DUMP_NAME_LEN; i++)
-		efi_name[i] = name[i];
+	efi_str8_to_str16(efi_name, name, DUMP_NAME_LEN - 1);
 
 	if (efivar_trylock())
 		return -EBUSY;
diff --git a/drivers/firmware/efi/efibc.c b/drivers/firmware/efi/efibc.c
index 8ced7af8e56d..7e3bf60d24e0 100644
--- a/drivers/firmware/efi/efibc.c
+++ b/drivers/firmware/efi/efibc.c
@@ -39,7 +39,7 @@  static int efibc_reboot_notifier_call(struct notifier_block *notifier,
 						    : L"shutdown";
 	const u8 *str = data;
 	efi_char16_t *wdata;
-	unsigned long l;
+	size_t len;
 	int ret;
 
 	ret = efibc_set_variable(L"LoaderEntryRebootReason", reason,
@@ -48,11 +48,10 @@  static int efibc_reboot_notifier_call(struct notifier_block *notifier,
 		return NOTIFY_DONE;
 
 	wdata = kmalloc(MAX_DATA_LEN * sizeof(efi_char16_t), GFP_KERNEL);
-	for (l = 0; l < MAX_DATA_LEN - 1 && str[l] != '\0'; l++)
-		wdata[l] = str[l];
-	wdata[l] = L'\0';
 
-	efibc_set_variable(L"LoaderEntryOneShot", wdata, l);
+	len = efi_str8_to_str16(wdata, str, MAX_DATA_LEN - 1);
+
+	efibc_set_variable(L"LoaderEntryOneShot", wdata, len);
 
 	kfree(wdata);
 	return NOTIFY_DONE;
diff --git a/include/linux/efi.h b/include/linux/efi.h
index d2b84c2fec39..c0ea01be3772 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -1030,6 +1030,23 @@  struct efivars {
 
 #define EFI_VAR_NAME_LEN	1024
 
+/*
+ * Helper that converts regular char buffer to u16 unicode-like strings;
+ * notice that the unicode buffer requires to be at least len+1 in size.
+ */
+static inline size_t
+efi_str8_to_str16(efi_char16_t *str16, const char *str8, size_t len)
+{
+	size_t i;
+
+	for (i = 0; i < len && str8[i] != '\0'; i++)
+		str16[i] = str8[i];
+
+	str16[i] = L'\0';
+
+	return i;
+}
+
 int efivars_register(struct efivars *efivars,
 		     const struct efivar_operations *ops,
 		     struct kobject *kobject);