efi: Move a few helper functions into the common efi/ dir

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>
This commit is contained in:
Simon Glass
2025-08-15 17:21:43 -06:00
parent 7cc4e9b062
commit eeaec57199
3 changed files with 43 additions and 33 deletions

View File

@@ -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

42
lib/efi/helper.c Normal file
View File

@@ -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;
}