[Concept,v2,08/18] efi: Move a few helper functions into the common efi/ dir

Message ID 20250820112340.147082-9-sjg@u-boot.org
State New
Headers
Series efi: Move towards the EFI app booting EFI applications |

Commit Message

Simon Glass Aug. 20, 2025, 11:23 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

Checking if a variable is a load option is useful for the app, so move
efi_varname_is_load_option() and u16_tohex() into a new helper.c file.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v1)

 lib/efi/Makefile            |  1 +
 lib/efi/helper.c            | 42 +++++++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_helper.c | 33 -----------------------------
 3 files changed, 43 insertions(+), 33 deletions(-)
 create mode 100644 lib/efi/helper.c
  

Patch

diff --git a/lib/efi/Makefile b/lib/efi/Makefile
index 213c9910b39..a31caf1fce9 100644
--- a/lib/efi/Makefile
+++ b/lib/efi/Makefile
@@ -5,5 +5,6 @@ 
 
 obj-y += basename.o
 obj-y += device_path.o
+obj-y += helper.o
 obj-y += memory.o
 obj-y += run.o
diff --git a/lib/efi/helper.c b/lib/efi/helper.c
new file mode 100644
index 00000000000..34cf3f49f95
--- /dev/null
+++ b/lib/efi/helper.c
@@ -0,0 +1,42 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2020, Linaro Limited
+ */
+
+#define LOG_CATEGORY LOGC_EFI
+
+#include <string.h>
+#include <linux/types.h>
+
+static int u16_tohex(u16 c)
+{
+	if (c >= '0' && c <= '9')
+		return c - '0';
+	if (c >= 'A' && c <= 'F')
+		return c - 'A' + 10;
+
+	/* not hexadecimal */
+	return -1;
+}
+
+bool efi_varname_is_load_option(u16 *var_name16, int *index)
+{
+	int id, i, digit;
+
+	if (memcmp(var_name16, u"Boot", 8))
+		return false;
+
+	for (id = 0, i = 0; i < 4; i++) {
+		digit = u16_tohex(var_name16[4 + i]);
+		if (digit < 0)
+			break;
+		id = (id << 4) + digit;
+	}
+	if (i == 4 && !var_name16[8]) {
+		if (index)
+			*index = id;
+		return true;
+	}
+
+	return false;
+}
diff --git a/lib/efi_loader/efi_helper.c b/lib/efi_loader/efi_helper.c
index 82e0fd7b069..56ea7d1c7d9 100644
--- a/lib/efi_loader/efi_helper.c
+++ b/lib/efi_loader/efi_helper.c
@@ -241,39 +241,6 @@  int efi_unlink_dev(efi_handle_t handle)
 	return 0;
 }
 
-static int u16_tohex(u16 c)
-{
-	if (c >= '0' && c <= '9')
-		return c - '0';
-	if (c >= 'A' && c <= 'F')
-		return c - 'A' + 10;
-
-	/* not hexadecimal */
-	return -1;
-}
-
-bool efi_varname_is_load_option(u16 *var_name16, int *index)
-{
-	int id, i, digit;
-
-	if (memcmp(var_name16, u"Boot", 8))
-		return false;
-
-	for (id = 0, i = 0; i < 4; i++) {
-		digit = u16_tohex(var_name16[4 + i]);
-		if (digit < 0)
-			break;
-		id = (id << 4) + digit;
-	}
-	if (i == 4 && !var_name16[8]) {
-		if (index)
-			*index = id;
-		return true;
-	}
-
-	return false;
-}
-
 /**
  * efi_next_variable_name() - get next variable name
  *