mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Guilherme G. Piccoli" <gpiccoli@igalia.com>
To: ardb@kernel.org, linux-efi@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, kernel-dev@igalia.com,
	kernel@gpiccoli.net, anton@enomsg.org, ccross@android.com,
	keescook@chromium.org, matt@codeblueprint.co.uk,
	matthew.garrett@nebula.com, tony.luck@intel.com,
	"Guilherme G. Piccoli" <gpiccoli@igalia.com>
Subject: [PATCH 1/2] efi: Add a generic helper to convert strings to unicode
Date: Fri, 20 May 2022 16:50:27 -0300	[thread overview]
Message-ID: <20220520195028.1347426-2-gpiccoli@igalia.com> (raw)
In-Reply-To: <20220520195028.1347426-1-gpiccoli@igalia.com>

Currently both efi-pstore and efibc rely in simple for-loops
to convert from regular char pointers to u16/unicode strings;
efibc has even a nice helper to perform such work.

So, let's export this helper to common EFI code to prevent
code duplication (like in efi-pstore); 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. No functional change is expected here.

Signed-off-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
---
 drivers/firmware/efi/efi-pstore.c |  5 ++---
 drivers/firmware/efi/efibc.c      | 16 ++++------------
 include/linux/efi.h               | 15 +++++++++++++++
 3 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/drivers/firmware/efi/efi-pstore.c b/drivers/firmware/efi/efi-pstore.c
index 7e771c56c13c..299116ecfb4e 100644
--- a/drivers/firmware/efi/efi-pstore.c
+++ b/drivers/firmware/efi/efi-pstore.c
@@ -249,7 +249,7 @@ static int efi_pstore_write(struct pstore_record *record)
 	char name[DUMP_NAME_LEN];
 	efi_char16_t efi_name[DUMP_NAME_LEN];
 	efi_guid_t vendor = LINUX_EFI_CRASH_GUID;
-	int i, ret = 0;
+	int ret = 0;
 
 	record->id = generic_id(record->time.tv_sec, record->part,
 				record->count);
@@ -262,8 +262,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);
 
 	ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
 			      false, record->size, record->psi->buf);
diff --git a/drivers/firmware/efi/efibc.c b/drivers/firmware/efi/efibc.c
index 15a47539dc56..63fe2bf753cb 100644
--- a/drivers/firmware/efi/efibc.c
+++ b/drivers/firmware/efi/efibc.c
@@ -11,16 +11,6 @@
 #include <linux/reboot.h>
 #include <linux/slab.h>
 
-static void efibc_str_to_str16(const char *str, efi_char16_t *str16)
-{
-	size_t i;
-
-	for (i = 0; i < strlen(str); i++)
-		str16[i] = str[i];
-
-	str16[i] = '\0';
-}
-
 static int efibc_set_variable(const char *name, const char *value)
 {
 	int ret;
@@ -39,8 +29,10 @@ static int efibc_set_variable(const char *name, const char *value)
 		return -ENOMEM;
 	}
 
-	efibc_str_to_str16(name, entry->var.VariableName);
-	efibc_str_to_str16(value, (efi_char16_t *)entry->var.Data);
+	efi_str8_to_str16(entry->var.VariableName, name, strlen(name));
+	efi_str8_to_str16((efi_char16_t *)entry->var.Data, value,
+			  strlen(value));
+
 	memcpy(&entry->var.VendorGuid, &guid, sizeof(guid));
 
 	ret = efivar_entry_set_safe(entry->var.VariableName,
diff --git a/include/linux/efi.h b/include/linux/efi.h
index ccd4d3f91c98..066ebc5bcb2a 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -1030,6 +1030,21 @@ efivar_unregister(struct efivar_entry *var)
 	kobject_put(&var->kobj);
 }
 
+/*
+ * 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 void
+efi_str8_to_str16(efi_char16_t *str16, const char *str8, size_t len)
+{
+	size_t i;
+
+	for (i = 0; i < len; i++)
+		str16[i] = str8[i];
+
+	str16[i] = '\0';
+}
+
 int efivars_register(struct efivars *efivars,
 		     const struct efivar_operations *ops,
 		     struct kobject *kobject);
-- 
2.36.0


  reply	other threads:[~2022-05-20 19:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-20 19:50 [PATCH 0/2] UEFI panic notification mechanism Guilherme G. Piccoli
2022-05-20 19:50 ` Guilherme G. Piccoli [this message]
2022-05-20 19:50 ` [PATCH 2/2] efi-panic: Introduce the UEFI panic notification module Guilherme G. Piccoli
2022-06-02 17:39 ` [PATCH 0/2] UEFI panic notification mechanism Guilherme G. Piccoli
2022-06-28  7:49   ` Ard Biesheuvel
2022-06-28 18:10     ` Guilherme G. Piccoli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220520195028.1347426-2-gpiccoli@igalia.com \
    --to=gpiccoli@igalia.com \
    --cc=anton@enomsg.org \
    --cc=ardb@kernel.org \
    --cc=ccross@android.com \
    --cc=keescook@chromium.org \
    --cc=kernel-dev@igalia.com \
    --cc=kernel@gpiccoli.net \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matt@codeblueprint.co.uk \
    --cc=matthew.garrett@nebula.com \
    --cc=tony.luck@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®