[U-Boot] [PATCH 1/1] cmd: add eficfg command

Heinrich Schuchardt xypron.glpk at gmx.de
Thu Jun 28 19:05:06 UTC 2018


This relates to a patch in
https://git.linaro.org/people/takahiro.akashi/u-boot.git
branch efi/for_sct.

On 05/29/2018 08:47 AM, AKASHI Takahiro wrote:

Thanks for providing a possibility to edit EFI variables.

Before submitting, please, split the patch into a patch series.

E.g. moving definitions from cmd/bootefi.c to include/efi_loader.h could
be in a separate patch.

> ---
>  cmd/Makefile                 |   2 +-
>  cmd/bootefi.c                |  52 +++-
>  cmd/eficfg.c                 | 530 +++++++++++++++++++++++++++++++++++
>  include/efi_loader.h         |  24 ++
>  lib/efi_loader/efi_bootmgr.c |  65 +++--
>  5 files changed, 639 insertions(+), 34 deletions(-)
>  create mode 100644 cmd/eficfg.c
> 
> diff --git a/cmd/Makefile b/cmd/Makefile
> index 13cf7bf6c2..6794eb9dcc 100644
> --- a/cmd/Makefile
> +++ b/cmd/Makefile
> @@ -23,7 +23,7 @@ obj-$(CONFIG_CMD_BINOP) += binop.o
>  obj-$(CONFIG_CMD_BLOCK_CACHE) += blkcache.o
>  obj-$(CONFIG_CMD_BMP) += bmp.o
>  obj-$(CONFIG_CMD_BOOTCOUNT) += bootcount.o
> -obj-$(CONFIG_CMD_BOOTEFI) += bootefi.o
> +obj-$(CONFIG_CMD_BOOTEFI) += bootefi.o eficfg.o
>  obj-$(CONFIG_CMD_BOOTMENU) += bootmenu.o
>  obj-$(CONFIG_CMD_BOOTSTAGE) += bootstage.o
>  obj-$(CONFIG_CMD_BOOTZ) += bootz.o
> diff --git a/cmd/bootefi.c b/cmd/bootefi.c
> index 28cc64bd02..9c1f5ffc89 100644
> --- a/cmd/bootefi.c
> +++ b/cmd/bootefi.c
> @@ -544,32 +544,43 @@ U_BOOT_CMD(
>  	bootefi_help_text
>  );
>  
> -void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
> +efi_status_t __efi_set_bootdev(const char *dev, const char *devnr,
> +				const char *path,
> +				struct efi_device_path **device,
> +				struct efi_device_path **file)
>  {
> +	int is_net;
> +	struct blk_desc *desc = NULL;
> +	disk_partition_t fs_partition;
> +	int part = 0;
> +	struct efi_device_path *dp;
>  	char filename[32] = { 0 }; /* dp->str is u16[32] long */
>  	char *s;
>  
> -	if (strcmp(dev, "Net")) {
> -		struct blk_desc *desc;
> -		disk_partition_t fs_partition;
> -		int part;
> -
> +	is_net = !strcmp(dev, "Net");
> +	if (!is_net) {
>  		part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
>  					       1);
>  		if (part < 0)
> -			return;
> +			return CMD_RET_FAILURE;
>  
> -		bootefi_device_path = efi_dp_from_part(desc, part);
> +		dp = efi_dp_from_part(desc, part);
>  	} else {
>  #ifdef CONFIG_NET
> -		bootefi_device_path = efi_dp_from_eth();
> +		dp = efi_dp_from_eth();
>  #endif
>  	}
> +	if (device)
> +		*device = dp;
> +#if 0 /* FIXME */
> +	else
> +		free(dp);
> +#endif
>  
>  	if (!path)
> -		return;
> +		return CMD_RET_FAILURE;
>  
> -	if (strcmp(dev, "Net")) {
> +	if (!is_net) {
>  		/* Add leading / to fs paths, because they're absolute */
>  		snprintf(filename, sizeof(filename), "/%s", path);
>  	} else {
> @@ -579,5 +590,22 @@ void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
>  	s = filename;
>  	while ((s = strchr(s, '/')))
>  		*s++ = '\\';
> -	bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
> +	if (device || is_net)
> +		*file = efi_dp_from_file(NULL, 0, filename);
> +	else
> +		*file = efi_dp_from_file(desc, part, filename);
> +
> +	return EFI_SUCCESS;
> +}
> +
> +void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
> +{
> +	struct efi_device_path *device, *image;
> +	efi_status_t ret;
> +
> +	ret = __efi_set_bootdev(dev, devnr, path, &device, &image);
> +	if (ret == EFI_SUCCESS) {
> +		bootefi_device_path = device;
> +		bootefi_image_path = image;
> +	}
>  }
> diff --git a/cmd/eficfg.c b/cmd/eficfg.c
> new file mode 100644
> index 0000000000..3a0dc3afe0
> --- /dev/null
> +++ b/cmd/eficfg.c
> @@ -0,0 +1,530 @@
> +/*
> + *  EFI Shell-like configuration command
> + *
> + *  Copyright (c) 2018 Takahiro AKASHI, Linaro Limited
> + *
> + *  SPDX-License-Identifier:     GPL-2.0+
> + */
> +
> +#include <charset.h>
> +#include <common.h>
> +#include <command.h>
> +#include <efi_loader.h>
> +#include <environment.h>
> +#include <errno.h>
> +#include <exports.h>
> +#include <search.h>
> +#include <asm/global_data.h>
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +/* TODO: move this to somewhere */
> +efi_status_t __efi_set_bootdev(const char *dev, const char *devnr,
> +				const char *path,
> +				struct efi_device_path **device,
> +				struct efi_device_path **file);
> +
> +/*
> + * From efi_variable.c,
> + *
> + * Mapping between EFI variables and u-boot variables:
> + *
> + *   efi_$guid_$varname = {attributes}(type)value
> + */
> +static efi_status_t do_efi_get_var(int argc, char * const argv[])
> +{
> +	char regex[256];
> +	char *regexlist[] = {regex};
> +	char *res = NULL;
> +	int len;
> +
> +	if (argc > 2)
> +		return CMD_RET_USAGE;
> +
> +	if (argc == 2)
> +		snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_%s", argv[1]);
> +	else
> +		snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
> +	debug("%s:%d grep uefi var %s\n", __func__, __LINE__, regex);
> +
> +	len = hexport_r(&env_htab, '\n', H_MATCH_REGEX | H_MATCH_KEY,
> +                        &res, 0, 1, regexlist);
> +
> +	if (len < 0)
> +		return CMD_RET_FAILURE;
> +
> +	if (len > 0) {
> +		/* TODO: pretty-print */
> +		puts(res);
> +		free(res);
> +		if (len < 2 && argc == 2)
> +			printf("%s: not found\n", argv[1]);
> +	}
> +
> +	return EFI_SUCCESS;
> +}
> +
> +static bool isalnum(char c)
> +{
> +	if (c >= '0' && c <= '9')
> +		return true;
> +	if (c >= 'A' && c <= 'F')
> +		return true;
> +	if (c >= 'a' && c <= 'f')
> +		return true;
> +
> +	return false;
> +}

Please, use isxdigit() defined in include/linux/ctype.h.

> +
> +static efi_status_t do_efi_set_var(int argc, char * const argv[])
> +{
> +	char *value, *data, *ptr, *ptr2 = NULL, num[3] = {'\0', '\0', '\0'};
> +	unsigned long len, size;

efi_uintn_t len, size;

to avoid

	warning: passing argument 4 of ‘efi_set_variable’ from
	incompatible pointer type

on 32bit build.

> +	u16 *var_name;
> +	efi_guid_t guid;
> +	efi_status_t ret;
> +
> +	if (argc == 1)
> +		return EFI_SUCCESS;
> +
> +	if (argc == 2) {
> +		value = NULL;
> +		size = 0;
> +	} else if (argc == 3) {
> +		value = argv[2];
> +		if (value[0] == '=')
> +			value++;
> +		len = strlen(value);
> +		if ((len > 2) && !strncmp(value, "0x", 2)) {
> +			if ((len % 2))
> +				return CMD_RET_USAGE;
> +
> +			data = value + 2;
> +			size = (len - 2) / 2;
> +			ptr2 = ptr = malloc(size);
> +			len = 0;
> +printf("+++ value: %s, data: %s, size:%ld\n", value, data, size);

size:%zu

But this line will anyway be removed from the final patch.

> +			while (len < size * 2) {
> +				num[0] = data[len++];
> +				num[1] = data[len++];
> +				if (!isalnum(num[0]) || !isalnum(num[1])) {
> +					printf("Invalid format: %s\n\n", value);
> +					ret = CMD_RET_USAGE;
> +					goto out;
> +				}
> +printf("+++ num: %s, len:%ld\n", num, len);

size:%zu

> +				*ptr++ = (char)simple_strtoul(num, NULL, 16);
> +			}
> +			value = data;
> +		} else
> +			size = len;
> +	} else
> +		return CMD_RET_USAGE;
> +
> +	var_name = malloc((strlen(argv[1]) + 1) * 2);
> +	utf8_to_utf16(var_name, (u8 *)argv[1], strlen(argv[1]) + 1);
> +
> +	/* FIXME: guid and attributes */
> +	guid = efi_global_variable_guid;
> +	ret = efi_set_variable(var_name, &guid,
> +			EFI_VARIABLE_BOOTSERVICE_ACCESS |
> +			EFI_VARIABLE_RUNTIME_ACCESS, size, value);
> +out:
> +	if (ptr2)
> +		free(ptr2);
> +	return ret;
> +}
> +
> +static int show_efi_device_path(struct efi_device_path *dp0)
> +{
> +	char *type, *sub_type;

Please, reuse efi_convert_device_path_to_text().

> +
> +	while (dp0->type != DEVICE_PATH_TYPE_END) {
> +		switch (dp0->type) {
> +		case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
> +			type = "hardware";
> +			switch (dp0->sub_type) {
> +				struct efi_device_path_memory *dp_mem;
> +				struct efi_device_path_vendor *dp_vendor;
> +
> +			case DEVICE_PATH_SUB_TYPE_MEMORY:
> +				dp_mem = (typeof(dp_mem))dp0;
> +				printf("\t\t%s/memory: %llx-%llx\n",
> +						type,
> +						dp_mem->start_address,
> +						dp_mem->end_address);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_VENDOR:
> +				dp_vendor = (typeof(dp_vendor))dp0;
> +				/* TODO: print guid */
> +				printf("\t\t%s/vendor:\n", type);
> +				break;
> +			default:
> +				printf("\t\tUnknown subtype:%s/%02x\n",
> +							type, dp0->sub_type);
> +				break;
> +			};
> +			break;
> +		case DEVICE_PATH_TYPE_ACPI_DEVICE:
> +			type = "acpi";
> +			switch (dp0->sub_type) {
> +				struct efi_device_path_acpi_path *dp_acpi;
> +
> +			case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE:
> +				dp_acpi = (typeof(dp_acpi))dp0;
> +				printf("\t\t%s/%s: hid=%08x uid=%08x\n",
> +						type, type,
> +						dp_acpi->hid, dp_acpi->uid);
> +				break;
> +			default:
> +				printf("\t\tUnknown subtype:%s/%02x\n",
> +							type, dp0->sub_type);
> +				break;
> +			};
> +			break;
> +		case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
> +			type = "messaging";
> +			/* TODO */
> +			switch (dp0->sub_type) {
> +				struct efi_device_path_sd_mmc_path *dp_sd;
> +
> +			case DEVICE_PATH_SUB_TYPE_MSG_ATAPI:
> +				sub_type = "atapi";
> +				printf("\t\ttype:%s/%s\n", type, sub_type);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_MSG_SCSI:
> +				sub_type = "scsi";
> +				printf("\t\ttype:%s/%s\n", type, sub_type);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_MSG_USB:
> +				sub_type = "usb";
> +				printf("\t\ttype:%s/%s\n", type, sub_type);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR:
> +				sub_type = "mac-addr";
> +				printf("\t\ttype:%s/%s\n", type, sub_type);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS:
> +				sub_type = "usb-class";
> +				printf("\t\ttype:%s/%s\n", type, sub_type);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_MSG_SD:
> +				sub_type = "sd";
> +				dp_sd = (typeof(dp_sd))dp0;
> +				printf("\t\t%s/sd: slot=%d\n",
> +						type, (int)dp_sd->slot_number);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_MSG_MMC:
> +				sub_type = "mmc";
> +				dp_sd = (typeof(dp_sd))dp0;
> +				printf("\t\t%s/sd: slot=%d\n",
> +						type, (int)dp_sd->slot_number);
> +				break;
> +			default:
> +				sub_type = "unknown";
> +				break;
> +			};
> +			break;
> +		case DEVICE_PATH_TYPE_MEDIA_DEVICE:
> +			type = "media";
> +			switch (dp0->sub_type) {
> +				struct efi_device_path_hard_drive_path *dp_hd;
> +				struct efi_device_path_cdrom_path *dp_cdrom;
> +				struct efi_device_path_file_path *dp_file;
> +				char file[256];
> +
> +			case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH:
> +				dp_hd = (typeof(dp_hd))dp0;
> +				printf("\t\t%s/disk: %d(0x%llx-0x%llx)\n",
> +					type,
> +					dp_hd->partition_number,
> +					dp_hd->partition_start,
> +					dp_hd->partition_end);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_CDROM_PATH:
> +				dp_cdrom = (typeof(dp_cdrom))dp0;
> +				printf("\t\t%s/cdrom: %d(0x%llx-0x%llx)\n",
> +					type,
> +					dp_cdrom->boot_entry,
> +					dp_cdrom->partition_start,
> +					dp_cdrom->partition_end);
> +				break;
> +			case DEVICE_PATH_SUB_TYPE_FILE_PATH:
> +				dp_file = (typeof(dp_file))dp0;
> +				utf16_to_utf8((u8 *)file, dp_file->str,
> +						utf16_strlen(dp_file->str) + 1);
> +				printf("\t\t%s/file: %s\n", type, file);
> +				break;
> +			default:
> +				printf("\t\tUnknown subtype:%s/%02x\n",
> +							type, dp0->sub_type);
> +				break;
> +			};
> +			break;
> +		default:
> +			printf("\t\tUnknown type:%02x\n", dp0->type);
> +			break;
> +		}
> +
> +		dp0 = ((void *)dp0 + dp0->length);
> +	}
> +
> +	return EFI_SUCCESS;
> +}
> +
> +void dump_data(void *data, size_t count)
> +{
> +	char *p;
> +	int i, j;
> +
> +	for (i = 0, p = data; i < count; i += 8) {
> +		printf("| %p:", data + i);
> +		for (j = 0; j < 8; j++) {
> +			if (i + j < count) {
> +				if (j == 4)
> +					puts("   ");
> +				printf(" %02x", *p++);
> +			} else {
> +				putc('\n');
> +				goto out;
> +			}
> +		}
> +		putc('\n');
> +	}
> +out:
> +	return;
> +}
> +
> +static efi_status_t show_efi_boot_opt(int id, void *data)
> +{
> +	struct load_option lo;
> +	char *label;
> +
> +	parse_load_option(&lo, data);
> +
> +	label = malloc(utf16_strlen(lo.label) + 1);
> +	if (!label)
> +		return EFI_OUT_OF_RESOURCES;
> +	utf16_to_utf8((u8 *)label, lo.label, utf16_strlen(lo.label) + 1);
> +
> +	printf("Boot%04X:\n", id);
> +	printf("\tattributes: %c%c%c (0x%08x)\n",
> +			/* ACTIVE */
> +			lo.attributes & 0x1 ? 'A' : '-',
> +			/* FORCE RECONNECT */
> +			lo.attributes & 0x2 ? 'R' : '-',
> +			/* HIDDEN */
> +			lo.attributes & 0x8 ? 'H' : '-',
> +			lo.attributes);
> +	printf("\tlabel: %s\n", label);
> +	printf("\tfile_path: ");
> +{ /* TODO */
> +	u16 *dp_str = efi_dp_str(lo.file_path);
> +	printf("%ls\n", dp_str);
> +	efi_free_pool(dp_str);
> +}
> +	show_efi_device_path((struct efi_device_path *)lo.file_path);

Use efi_convert_device_path_to_text().

> +	printf("\tdata: %s\n", lo.optional_data);
> +
> +	free(label);
> +	return EFI_SUCCESS;
> +}
> +
> +static efi_status_t do_efi_boot_opt(int argc, char * const argv[])
> +{
> +	int id;
> +	char *endp;
> +	char var_name[9];
> +	u16 var_name16[9];
> +	efi_guid_t guid;
> +	void *data;
> +	unsigned long size;

efi_uintn_t size;

> +	int ret;
> +	uint32_t attr;
> +	u16 *label;
> +	char *interface, *device, *file, *option;
> +	struct efi_device_path *file_path;
> +
> +	if (argc == 1)
> +		return CMD_RET_USAGE;
> +
> +	id = (int)simple_strtoul(argv[1], &endp, 0);
> +	if (*endp != '\0' || id > 0xffff)
> +		return EFI_INVALID_PARAMETER;
> +
> +	sprintf(var_name, "Boot%04X", id);
> +	utf8_to_utf16(var_name16, (u8 *)var_name, 9);
> +	guid = efi_global_variable_guid;
> +
> +	if (argc == 2) {
> +		size = 0;
> +		ret = efi_get_variable(var_name16, &guid, NULL, &size, NULL);
> +		if (ret == (int)EFI_BUFFER_TOO_SMALL) {
> +			data = malloc(size);
> +			ret = efi_get_variable(var_name16, &guid,
> +							NULL, &size, data);
> +		}
> +		if (ret != EFI_SUCCESS) {
> +			printf("Boot%04X: not found\n", id);
> +			return EFI_SUCCESS;
> +		}
> +
> +dump_data(data, size+2);
> +		ret = show_efi_boot_opt(id, data);
> +		free(data);
> +
> +		return ret;
> +	}
> +
> +	if (argc < 7 || argc > 8)
> +		return CMD_RET_USAGE;
> +
> +	attr = (uint32_t)simple_strtoul(argv[2], NULL, 0);
> +	label = malloc(((strlen(argv[3]) + 1)) * 2);
> +	if (!label)
> +		return EFI_OUT_OF_RESOURCES;
> +	utf8_to_utf16(label, (u8 *)argv[3], strlen(argv[3]) + 1);
> +	interface = argv[4];
> +	device = argv[5];
> +	file = argv[6];
> +	option = (argc == 7 ? "" : argv[7]);
> +
> +	ret = __efi_set_bootdev(interface, device, file, NULL, &file_path);
> +	if (ret != EFI_SUCCESS)
> +		goto out_err;
> +
> +	size = efi_marshal_load_option(attr, label, file_path, option, &data);
> +	if (!size) {
> +		ret = CMD_RET_FAILURE;
> +		goto out_err2;
> +	}
> +
> +dump_data(data, size+2);
> +	ret = efi_set_variable(var_name16, &guid,
> +			EFI_VARIABLE_BOOTSERVICE_ACCESS |
> +			EFI_VARIABLE_RUNTIME_ACCESS, size, data);
> +
> +	free(data);
> +out_err2:
> +#if 0 /* FIXME */
> +	free(file_path);
> +#endif
> +out_err:
> +	free(label);
> +	return ret;
> +}
> +
> +static efi_status_t do_efi_boot_order(int argc, char * const argv[])
> +{
> +	u16 *bootorder = NULL;
> +	unsigned long size, value;

+       efi_uintn_t size;
+       unsigned long value;

> +	efi_guid_t guid;
> +	efi_status_t ret = EFI_SUCCESS;
> +	int num, i;
> +	char *endp;
> +
> +	guid = efi_global_variable_guid;
> +	if (argc == 1) {
> +		size = 0;
> +		ret = efi_get_variable(u"BootOrder",
> +					&guid, NULL, &size, NULL);
> +		if (ret == EFI_BUFFER_TOO_SMALL) {
> +			bootorder = malloc(size);
> +			ret = efi_get_variable(u"BootOrder",
> +					&guid, NULL, &size, bootorder);
> +		}
> +		if (ret != EFI_SUCCESS) {
> +			printf("BootOrder not defined\n");
> +			return EFI_SUCCESS;
> +		}
> +
> +		num = size / sizeof(u16);
> +		for (i = 0; i < num; i++)
> +			/* TODO: more details */
> +			printf("%2d: Boot%04X\n", i + 1, bootorder[i]);
> +
> +		goto out;
> +	}
> +
> +	argc--;
> +	argv++;
> +	size = argc * sizeof(u16);
> +	bootorder = malloc(size);
> +	if (!bootorder)
> +		return EFI_OUT_OF_RESOURCES;
> +
> +	for (i = 0; i < argc; i++) {
> +		value = simple_strtoul(argv[i], &endp, 0);
> +		if (*endp != '\0') {
> +			printf("invalid value: %s\n", argv[i]);
> +			goto out;
> +		}
> +
> +		bootorder[i] = (u16)value;
> +	}
> +
> +	ret = efi_set_variable(u"BootOrder", &guid,
> +			EFI_VARIABLE_BOOTSERVICE_ACCESS |
> +			EFI_VARIABLE_RUNTIME_ACCESS, size, bootorder);
> +out:
> +	free(bootorder);
> +
> +	return ret;
> +}
> +
> +/* Interpreter command to configure EFI environment */
> +static int do_eficfg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> +{
> +	char *command;
> +#if 1 /* FIXME */
> +	efi_status_t r;
> +	extern efi_status_t efi_init_obj_list(void);
> +
> +	/* Initialize EFI drivers */
> +	r = efi_init_obj_list();
> +	if (r != EFI_SUCCESS) {
> +		printf("Error: Cannot set up EFI drivers, r = %lu\n",
> +		       r & ~EFI_ERROR_MASK);
> +		return CMD_RET_FAILURE;
> +	}
> +#endif
> +	if (argc < 2)
> +		return CMD_RET_USAGE;
> +
> +	command = argv[1];
> +	argc--; argv++;
> +
> +	if (!strcmp(command, "getvar"))
> +		return do_efi_get_var(argc, argv);
> +	else if (!strcmp(command, "setvar"))
> +		return do_efi_set_var(argc, argv);
> +	else if (!strcmp(command, "device") || !strcmp(command, "boot"))
> +		return do_efi_boot_opt(argc, argv);
> +	else if (!strcmp(command, "order"))
> +		return do_efi_boot_order(argc, argv);
> +	else
> +		return CMD_RET_USAGE;
> +}
> +
> +#ifdef CONFIG_SYS_LONGHELP
> +static char eficfg_help_text[] =
> +	"  - EFI Shell-like interface to configure EFI envrionment\n"
> +	"\n"
> +	"eficfg getvar [<name>]\n"
> +	"  - get uefi variable's value\n"
> +	"eficfg setvar <name> [<value>]\n"
> +	"  - set/delete uefi variable's value\n"
> +	"    <value> may be \"=\"...\"\", \"=0x...\" (set) or \"=\" (delete)\n"
> +	"\n"
> +	"eficfg device <bootid> <attr> <label> "
> +	"<interface> <device>[:<part>] <file path> <option>\n"
> +	"  - set/delete uefi variable's value\n"
> +	"eficfg order <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
> +	"  - set uefi boot order\n";
> +#endif
> +
> +U_BOOT_CMD(
> +	eficfg, 10, 0, do_eficfg,
> +	"Configure EFI environment",
> +	eficfg_help_text
> +);
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index 664179ba5f..929784002c 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -450,6 +450,30 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name, efi_guid_t *vendor,
>  				     u32 attributes, efi_uintn_t data_size,
>  				     void *data);
>  
> +/*
> + * See section 3.1.3 in the v2.7 UEFI spec for more details on
> + * the layout of EFI_LOAD_OPTION.  In short it is:
> + *
> + *    typedef struct _EFI_LOAD_OPTION {
> + *        UINT32 Attributes;
> + *        UINT16 FilePathListLength;
> + *        // CHAR16 Description[];   <-- variable length, NULL terminated
> + *        // EFI_DEVICE_PATH_PROTOCOL FilePathList[];  <-- FilePathListLength bytes
> + *        // UINT8 OptionalData[];
> + *    } EFI_LOAD_OPTION;
> + */
> +struct load_option {
> +	u32 attributes;
> +	u16 file_path_length;
> +	u16 *label;
> +	struct efi_device_path *file_path;
> +	u8 *optional_data;
> +};
> +
> +void parse_load_option(struct load_option *lo, void *ptr);
> +unsigned long efi_marshal_load_option(uint32_t attr, uint16_t *label,
> +			struct efi_device_path *file_path,
> +			char *option, void **data);
>  void *efi_bootmgr_load(struct efi_device_path **device_path,
>  		       struct efi_device_path **file_path);
>  
> diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> index 853358ab93..3cadccd699 100644
> --- a/lib/efi_loader/efi_bootmgr.c
> +++ b/lib/efi_loader/efi_bootmgr.c
> @@ -4,6 +4,7 @@
>   *
>   *  Copyright (c) 2017 Rob Clark
>   */
> +#define DEBUG

To be removed before submitting.

Best regards

Heinrich

>  
>  #include <common.h>
>  #include <charset.h>
> @@ -30,28 +31,8 @@ static const struct efi_runtime_services *rs;
>   */
>  
>  
> -/*
> - * See section 3.1.3 in the v2.7 UEFI spec for more details on
> - * the layout of EFI_LOAD_OPTION.  In short it is:
> - *
> - *    typedef struct _EFI_LOAD_OPTION {
> - *        UINT32 Attributes;
> - *        UINT16 FilePathListLength;
> - *        // CHAR16 Description[];   <-- variable length, NULL terminated
> - *        // EFI_DEVICE_PATH_PROTOCOL FilePathList[];  <-- FilePathListLength bytes
> - *        // UINT8 OptionalData[];
> - *    } EFI_LOAD_OPTION;
> - */
> -struct load_option {
> -	u32 attributes;
> -	u16 file_path_length;
> -	u16 *label;
> -	struct efi_device_path *file_path;
> -	u8 *optional_data;
> -};
> -
>  /* parse an EFI_LOAD_OPTION, as described above */
> -static void parse_load_option(struct load_option *lo, void *ptr)
> +void parse_load_option(struct load_option *lo, void *ptr)
>  {
>  	lo->attributes = *(u32 *)ptr;
>  	ptr += sizeof(u32);
> @@ -68,6 +49,48 @@ static void parse_load_option(struct load_option *lo, void *ptr)
>  	lo->optional_data = ptr;
>  }
>  
> +unsigned long efi_marshal_load_option(uint32_t attr, uint16_t *label,
> +					struct efi_device_path *file_path,
> +					char *option, void **data)
> +{
> +	unsigned long size;
> +	unsigned long label_len, option_len;
> +	uint16_t file_path_len;
> +	void *p;
> +
> +	label_len = (utf16_strlen(label) + 1) * 2;
> +	file_path_len = efi_dp_size(file_path)
> +			+ sizeof(struct efi_device_path); /* for END */
> +	option_len = strlen(option);
> +
> +	/* total size */
> +	size = sizeof(attr);
> +	size += file_path_len;
> +	size += label_len;
> +	size += option_len + 1;
> +	p = malloc(size);
> +	if (!p)
> +		return 0;
> +
> +	/* copy data */
> +	*data = p;
> +	memcpy(p, &attr, sizeof(attr));
> +	p += sizeof(attr);
> +	memcpy(p, &file_path_len, sizeof(file_path_len));
> +	p += sizeof(file_path_len);
> +	memcpy(p, label, label_len);
> +	p += label_len;
> +
> +	memcpy(p, file_path, file_path_len);
> +	p += file_path_len;
> +
> +	memcpy(p, option, option_len);
> +	p += option_len;
> +	*(char *)p = '\0';
> +
> +	return size;
> +}
> +
>  /* free() the result */
>  static void *get_var(u16 *name, const efi_guid_t *vendor,
>  		     efi_uintn_t *size)
> 



More information about the U-Boot mailing list