mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 14/30] efi: centralize efivarfs_parse_filename
Date: Mon, 22 Nov 2021 09:47:16 +0100	[thread overview]
Message-ID: <20211122084732.2597109-15-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20211122084732.2597109-1-a.fatoum@pengutronix.de>

Turning an EFI varfs filename into its components will be useful for the
EFI loader as well, so factor that out.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/efi/Makefile          |   2 +-
 common/efi/efivar-filename.c | 116 +++++++++++++++++++++++++++++++++++
 fs/efivarfs.c                |  76 -----------------------
 include/efi/efi-util.h       |   4 ++
 4 files changed, 121 insertions(+), 77 deletions(-)
 create mode 100644 common/efi/efivar-filename.c

diff --git a/common/efi/Makefile b/common/efi/Makefile
index ed110f5a6899..a7cebde4f1b3 100644
--- a/common/efi/Makefile
+++ b/common/efi/Makefile
@@ -3,4 +3,4 @@
 obj-$(CONFIG_EFI_BOOTUP)	+= payload/
 obj-$(CONFIG_EFI_GUID)		+= guid.o
 obj-$(CONFIG_EFI_DEVICEPATH)	+= devicepath.o
-obj-y				+= errno.o
+obj-y				+= errno.o efivar-filename.o
diff --git a/common/efi/efivar-filename.c b/common/efi/efivar-filename.c
new file mode 100644
index 000000000000..51d0130fa774
--- /dev/null
+++ b/common/efi/efivar-filename.c
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/ctype.h>
+#include <string.h>
+#include <efi/efi-util.h>
+
+static int char_to_nibble(char c)
+{
+	int ret = tolower(c);
+
+	return ret <= '9' ? ret - '0' : ret - 'a' + 10;
+}
+
+static int read_byte_str(const char *str, u8 *out)
+{
+	if (!isxdigit(*str) || !isxdigit(*(str + 1)))
+		return -EINVAL;
+
+	*out = (char_to_nibble(*str) << 4) | char_to_nibble(*(str + 1));
+
+	return 0;
+}
+
+static int efi_guid_parse(const char *str, efi_guid_t *guid)
+{
+	int i, ret;
+	u8 idx[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
+
+	for (i = 0; i < 16; i++) {
+		ret = read_byte_str(str, &guid->b[idx[i]]);
+		if (ret)
+			return ret;
+		str += 2;
+
+		switch (i) {
+		case 3:
+		case 5:
+		case 7:
+		case 9:
+			if (*str != '-')
+				return -EINVAL;
+			str++;
+			break;
+		}
+	}
+
+	return 0;
+}
+
+
+int __efivarfs_parse_filename(const char *filename, efi_guid_t *vendor,
+			      s16 *name, size_t *namelen)
+{
+	int len, ret;
+	const char *guidstr;
+	int i;
+
+	len = strlen(filename);
+
+	if (len < sizeof("-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"))
+		return -EINVAL;
+
+	guidstr = filename + len - sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
+	if (*guidstr != '-' || guidstr == filename)
+		return -EINVAL;
+
+	guidstr++;
+
+	ret = efi_guid_parse(guidstr, vendor);
+	if (ret)
+		return ret;
+
+	if (guidstr - filename > *namelen)
+		ret = -EFBIG;
+
+	*namelen = guidstr - filename;
+
+	if (ret)
+		return ret;
+
+	for (i = 0; i < *namelen - 1; i++)
+		name[i] = filename[i];
+
+	name[i] = L'\0';
+
+	return 0;
+}
+
+int efivarfs_parse_filename(const char *filename, efi_guid_t *vendor,
+			    s16 **name)
+{
+	int ret;
+	s16 *varname;
+	size_t namelen = 0;
+	int i;
+
+	if (*filename == '/')
+		filename++;
+
+	ret = __efivarfs_parse_filename(filename, vendor, NULL, &namelen);
+	if (ret != -EFBIG)
+		return ret;
+
+	varname = xzalloc(namelen * sizeof(s16));
+
+	for (i = 0; i < namelen - 1; i++)
+		varname[i] = filename[i];
+
+	varname[i] = L'\0';
+
+	*name = varname;
+
+	return 0;
+}
+
+
diff --git a/fs/efivarfs.c b/fs/efivarfs.c
index 01947439ff6c..2e72bb885265 100644
--- a/fs/efivarfs.c
+++ b/fs/efivarfs.c
@@ -47,82 +47,6 @@ struct efivarfs_priv {
 	struct list_head inodes;
 };
 
-static int char_to_nibble(char c)
-{
-	int ret = tolower(c);
-
-	return ret <= '9' ? ret - '0' : ret - 'a' + 10;
-}
-
-static int read_byte_str(const char *str, u8 *out)
-{
-	if (!isxdigit(*str) || !isxdigit(*(str + 1)))
-		return -EINVAL;
-
-	*out = (char_to_nibble(*str) << 4) | char_to_nibble(*(str + 1));
-
-	return 0;
-}
-
-static int efi_guid_parse(const char *str, efi_guid_t *guid)
-{
-	int i, ret;
-	u8 idx[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
-
-	for (i = 0; i < 16; i++) {
-		ret = read_byte_str(str, &guid->b[idx[i]]);
-		if (ret)
-			return ret;
-		str += 2;
-
-		switch (i) {
-		case 3:
-		case 5:
-		case 7:
-		case 9:
-			if (*str != '-')
-				return -EINVAL;
-			str++;
-			break;
-		}
-	}
-
-	return 0;
-}
-
-static int efivarfs_parse_filename(const char *filename, efi_guid_t *vendor, s16 **name)
-{
-	int len, ret;
-	const char *guidstr;
-	s16 *varname;
-	int i;
-
-	if (*filename == '/')
-		filename++;
-
-	len = strlen(filename);
-
-	if (len < sizeof("-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"))
-		return -EINVAL;
-
-	guidstr = filename + len - sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
-	if (*guidstr != '-')
-		return -EINVAL;
-
-	guidstr++;
-
-	ret = efi_guid_parse(guidstr, vendor);
-
-	varname = xzalloc((guidstr - filename) * sizeof(s16));
-
-	for (i = 0; i < guidstr - filename - 1; i++)
-		varname[i] = filename[i];
-
-	*name = varname;
-
-	return 0;
-}
-
 static int efivars_create(struct device_d *dev, const char *pathname, mode_t mode)
 {
 	struct efivarfs_priv *priv = dev->priv;
diff --git a/include/efi/efi-util.h b/include/efi/efi-util.h
index 0645af270769..78e352456ad1 100644
--- a/include/efi/efi-util.h
+++ b/include/efi/efi-util.h
@@ -7,4 +7,8 @@
 const char *efi_strerror(efi_status_t err);
 int efi_errno(efi_status_t err);
 
+int __efivarfs_parse_filename(const char *filename, efi_guid_t *vendor,
+			      s16 *name, size_t *namelen);
+int efivarfs_parse_filename(const char *filename, efi_guid_t *vendor, s16 **name);
+
 #endif
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2021-11-22  8:53 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-22  8:47 [PATCH 00/30] efi: refactor for upcoming loader support Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 01/30] fs: remove useless AT_FDCWD references Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 02/30] fs: remove unused struct node_d in struct dir Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 03/30] block : efi: rename driver variable from efi_fs_driver to efi_bio_driver Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 04/30] include: <linux/types.h>: wrap in #ifndef __ASSEMBLY__ Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 05/30] hw_random: stm32: propagate error codes from rng read Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 06/30] efi: align LOAD_FILE_PROTOCOL_GUID's name with other PROTOCOL_GUIDs Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 07/30] asm-generic: move sync_caches_for_execution declaration to <asm/cache.h> Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 08/30] common: move EFI code into new efi/ top level directory Ahmad Fatoum
2021-11-23  8:55   ` Jules Maselbas
2021-11-22  8:47 ` [PATCH 09/30] serial: efi-stdio: move efi-stdio.h header to central location Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 10/30] efi: use SPDX-License-Identifier where appropriate Ahmad Fatoum
2021-11-23  8:52   ` Jules Maselbas
2021-11-22  8:47 ` [PATCH 11/30] drivers: efi: move Kconfig options to new menu Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 12/30] efi: factor out errno translation Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 13/30] efi: rename <efi/efi.h> to <efi/efi-payload.h> Ahmad Fatoum
2021-11-22  8:47 ` Ahmad Fatoum [this message]
2021-11-22  8:47 ` [PATCH 15/30] kbuild: force 16-bit wchar_t treewide Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 16/30] include: <linux/nls.h>: remove duplicate wchar_t typedef Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 17/30] lib: wchar: add wctomb and mbtowc Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 18/30] lib: implement wcsnlen Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 19/30] vsprintf: add optional support for %ls format modifier Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 20/30] libfile: null-terminate read_file of wchar_t buffer Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 21/30] commands: echo: add wide file output via wecho alias Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 22/30] efi: make efi_main __noreturn Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 23/30] efi: define and use new EFI_ERROR_MASK macro Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 24/30] common: move CONFIG_ELF into General Settings Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 25/30] efi: don't zero executable buffer before freeing Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 26/30] partitions: efi: move header to central location Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 27/30] efi: print early efi_main string on CONFIG_DEBUG_LL=y Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 28/30] ARM64: board-dt-2nd: remove no longer needed noinline function split Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 29/30] bus: acpi: register bus even if without ACPI EFI table Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 30/30] efi: guid: fix typos Ahmad Fatoum
2021-11-25  7:44 ` [PATCH 00/30] efi: refactor for upcoming loader support Sascha Hauer

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=20211122084732.2597109-15-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --subject='Re: [PATCH 14/30] efi: centralize efivarfs_parse_filename' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox