All of lore.kernel.org
 help / color / mirror / Atom feed
From: AKASHI Takahiro <takahiro.akashi@linaro.org>
To: u-boot@lists.denx.de
Subject: [PATCH 09/10] tools: add mkeficapsule command for UEFI capsule update test
Date: Tue, 28 Apr 2020 10:52:25 +0900	[thread overview]
Message-ID: <20200428015225.GE16947@laputa> (raw)
In-Reply-To: <16b1d847-dd26-5010-2ab8-5fea95123608@gmx.de>

On Mon, Apr 27, 2020 at 10:15:41PM +0200, Heinrich Schuchardt wrote:
> On 4/27/20 11:48 AM, AKASHI Takahiro wrote:
> > This is a utility mainly for test purpose.
> >   mkeficapsule -f: create a test capsule file for FIT image firmware
> >
> > Having said that, you will be able to customize the code to fit
> > your specific requirements for your platform.
> >
> > Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> > ---
> >  tools/Makefile       |   3 +
> >  tools/mkeficapsule.c | 209 +++++++++++++++++++++++++++++++++++++++++++
> >  2 files changed, 212 insertions(+)
> >  create mode 100644 tools/mkeficapsule.c
> >
> > diff --git a/tools/Makefile b/tools/Makefile
> > index c2b26340047a..08e97063d5fa 100644
> > --- a/tools/Makefile
> > +++ b/tools/Makefile
> > @@ -222,6 +222,9 @@ hostprogs-$(CONFIG_MIPS) += mips-relocs
> >  hostprogs-$(CONFIG_ASN1_COMPILER)	+= asn1_compiler
> >  HOSTCFLAGS_asn1_compiler.o = -idirafter $(srctree)/include
> >
> > +# TODO: only build this for capsule pytest
> 
> Do you plan to send a new version of the series to fix this issue?

No.

> We need a documentation page for this tool in doc/uefi/.

Even currently used only in a test?

> Best regards
> 
> Heinrich
> 
> > +hostprogs-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += mkeficapsule
> > +
> >  # We build some files with extra pedantic flags to try to minimize things
> >  # that won't build on some weird host compiler -- though there are lots of
> >  # exceptions for files that aren't complaint.
> > diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c
> > new file mode 100644
> > index 000000000000..b26c88e42020
> > --- /dev/null
> > +++ b/tools/mkeficapsule.c
> > @@ -0,0 +1,209 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Copyright 2018 Linaro Limited
> > + *		Author: AKASHI Takahiro
> > + */
> > +
> > +#include <malloc.h>
> > +#include <stdio.h>
> > +#include <sys/stat.h>
> > +#include <sys/types.h>
> > +/*
> > + * TODO: use libefi/libgnuefi headers
> 
> Can't you use the definitions from include/efi_api.h?
> I would prefer if you could avoid duplicate definitions in this file.

Including efi.h and efi_api.h causes a bunch of compiler errors.
But now I could sort them out.
Will fix in the next version.

Thanks,
-Takahiro Akashi


> Best regards
> 
> Heinrich
> 
> > + */
> > +
> > +typedef u_int8_t u8;
> > +typedef u_int16_t u16;
> > +typedef u_int32_t u32;
> > +typedef u_int64_t u64;
> > +
> > +/* include/efi.h */
> > +typedef struct {
> > +	u8 b[16];
> > +} efi_guid_t;
> > +
> > +#define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
> > +	{{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, \
> > +		((a) >> 24) & 0xff, \
> > +		(b) & 0xff, ((b) >> 8) & 0xff, \
> > +		(c) & 0xff, ((c) >> 8) & 0xff, \
> > +		(d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) } }
> > +
> > +#define EFI_VARIABLE_NON_VOLATILE	0x0000000000000001
> > +#define EFI_VARIABLE_BOOTSERVICE_ACCESS	0x0000000000000002
> > +#define EFI_VARIABLE_RUNTIME_ACCESS	0x0000000000000004
> > +
> > +/* include/efi_api.h */
> > +#define EFI_GLOBAL_VARIABLE_GUID \
> > +	EFI_GUID(0x8be4df61, 0x93ca, 0x11d2, 0xaa, 0x0d, \
> > +		 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c)
> > +
> > +#define CAPSULE_FLAGS_PERSIST_ACROSS_RESET	0x00010000
> > +#define CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE	0x00020000
> > +#define CAPSULE_FLAGS_INITIATE_RESET		0x00040000
> > +
> > +#define EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID \
> > +	EFI_GUID(0xae13ff2d, 0x9ad4, 0x4e25, 0x9a, 0xc8, \
> > +		 0x6d, 0x80, 0xb3, 0xb2, 0x21, 0x47)
> > +
> > +#define EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID \
> > +	EFI_GUID(0x6dcbd5ed, 0xe82d, 0x4c44, 0xbd, 0xa1, \
> > +		 0x71, 0x94, 0x19, 0x9a, 0xd9, 0x2a)
> > +
> > +struct efi_capsule_header {
> > +	efi_guid_t capsule_guid;
> > +	u32 header_size;
> > +	u32 flags;
> > +	u32 capsule_image_size;
> > +} __attribute__((packed));
> > +
> > +struct efi_capsule_block_descriptor {
> > +	u64 length;
> > +	union {
> > +		u64 data_block;
> > +		u64 continuation_ptr;
> > +	};
> > +} __attribute__((packed));
> > +
> > +struct efi_firmware_management_capsule_header {
> > +	u32 version;
> > +	u16 embedded_driver_count;
> > +	u16 payload_item_count;
> > +	u64 item_offset_list[];
> > +} __attribute__((packed));
> > +
> > +struct efi_firmware_management_capsule_image_header {
> > +	u32 version;
> > +	efi_guid_t update_image_type_id;
> > +	u8 update_image_index;
> > +	u8 reserved[3];
> > +	u32 update_image_size;
> > +	u32 update_vendor_code_size;
> > +	u64 update_hardware_instance;
> > +} __attribute__((packed));
> > +
> > +efi_guid_t efi_guid_fm_capsule = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
> > +efi_guid_t efi_guid_image_type_uboot_fit =
> > +		EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID;
> > +
> > +static int create_fwbin(char *bin, char *path)
> > +{
> > +	struct efi_capsule_header header;
> > +	struct efi_firmware_management_capsule_header capsule;
> > +	struct efi_firmware_management_capsule_image_header image;
> > +	FILE *f, *g;
> > +	struct stat bin_stat;
> > +	u8 *data;
> > +	size_t size;
> > +
> > +	g = fopen(bin, "r");
> > +	if (!g) {
> > +		printf("cannot open %s\n", bin);
> > +		return -1;
> > +	}
> > +	if (stat(bin, &bin_stat) < 0) {
> > +		printf("cannot determine the size of %s\n", bin);
> > +		goto err_1;
> > +	}
> > +	data = malloc(bin_stat.st_size);
> > +	if (!data) {
> > +		printf("cannot allocate memory: %lx\n", bin_stat.st_size);
> > +		goto err_1;
> > +	}
> > +	f = fopen(path, "w");
> > +	if (!f) {
> > +		printf("cannot open %s\n", path);
> > +		goto err_2;
> > +	}
> > +	header.capsule_guid = efi_guid_fm_capsule;
> > +	header.header_size = sizeof(header);
> > +	header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET; /* TODO */
> > +	header.capsule_image_size = sizeof(header)
> > +					+ sizeof(capsule) + sizeof(u64)
> > +					+ sizeof(image)
> > +					+ bin_stat.st_size;
> > +
> > +	size = fwrite(&header, 1, sizeof(header), f);
> > +	if (size < sizeof(header)) {
> > +		printf("write failed (%lx)\n", size);
> > +		goto err_3;
> > +	}
> > +
> > +	capsule.version = 0x00000001;
> > +	capsule.embedded_driver_count = 0;
> > +	capsule.payload_item_count = 1;
> > +	capsule.item_offset_list[0] = sizeof(capsule) + sizeof(u64);
> > +	size = fwrite(&capsule, 1, sizeof(capsule) + sizeof(u64), f);
> > +	if (size < (sizeof(capsule) + sizeof(u64))) {
> > +		printf("write failed (%lx)\n", size);
> > +		goto err_3;
> > +	}
> > +
> > +	image.version = 0x00000002;
> > +	image.update_image_type_id = efi_guid_image_type_uboot_fit;
> > +	image.update_image_index = 1;
> > +	image.update_image_size = bin_stat.st_size;
> > +	image.update_vendor_code_size = 0; /* none */
> > +	image.update_hardware_instance = 1;
> > +
> > +	size = fwrite(&image, 1, sizeof(image), f);
> > +	if (size < sizeof(image)) {
> > +		printf("write failed (%lx)\n", size);
> > +		goto err_3;
> > +	}
> > +	size = fread(data, 1, bin_stat.st_size, g);
> > +	if (size < bin_stat.st_size) {
> > +		printf("read failed (%lx)\n", size);
> > +		goto err_3;
> > +	}
> > +	size = fwrite(data, 1, bin_stat.st_size, f);
> > +	if (size < bin_stat.st_size) {
> > +		printf("write failed (%lx)\n", size);
> > +		goto err_3;
> > +	}
> > +
> > +	fclose(f);
> > +	fclose(g);
> > +	free(data);
> > +
> > +	return 0;
> > +
> > +err_3:
> > +	fclose(f);
> > +err_2:
> > +	free(data);
> > +err_1:
> > +	fclose(g);
> > +
> > +	return -1;
> > +}
> > +
> > +void print_usage(void)
> > +{
> > +	printf("mkeficapsule -f <firmware binary> <output file>\n");
> > +}
> > +
> > +/*
> > + * Usage:
> > + *   $ mkeficapsule -f <firmware binary> <output file>
> > + */
> > +int main(int argc, char **argv)
> > +{
> > +	if (argc != 4) {
> > +		print_usage();
> > +		return -1;
> > +	}
> > +
> > +	if (!strcmp(argv[1], "-f")) {
> > +		if (create_fwbin(argv[2], argv[3]) < 0) {
> > +			printf("Creating firmware capsule for %s failed\n",
> > +			       argv[2]);
> > +			return -1;
> > +		}
> > +
> > +		return 0;
> > +	}
> > +
> > +	print_usage();
> > +	return -1;
> > +}
> >
> 

  reply	other threads:[~2020-04-28  1:52 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-27  9:48 [PATCH 00/10] efi_loader: add capsule update support AKASHI Takahiro
2020-04-27  9:48 ` [PATCH 01/10] efi_loader: disk: add efi_disk_is_system_part() AKASHI Takahiro
2020-04-27 19:57   ` Heinrich Schuchardt
2020-04-27 23:54     ` AKASHI Takahiro
2020-05-01  7:06       ` Heinrich Schuchardt
2020-05-05 22:33         ` Heinrich Schuchardt
2020-04-27  9:48 ` [PATCH 02/10] efi_loader: add option to initialise EFI subsystem early AKASHI Takahiro
2020-04-27 20:09   ` Heinrich Schuchardt
2020-04-28  0:16     ` AKASHI Takahiro
2020-05-17  7:29       ` Heinrich Schuchardt
2020-05-18  1:43         ` AKASHI Takahiro
2020-04-27  9:48 ` [PATCH 03/10] efi_loader: define UpdateCapsule api AKASHI Takahiro
2020-05-17  8:02   ` Heinrich Schuchardt
2020-05-18  1:34     ` AKASHI Takahiro
2020-04-27  9:48 ` [PATCH 04/10] efi_loader: capsule: add capsule_on_disk support AKASHI Takahiro
2020-04-27 20:28   ` Heinrich Schuchardt
2020-04-28  0:28     ` AKASHI Takahiro
2020-04-30 12:52       ` Sughosh Ganu
2020-04-30 19:51         ` Heinrich Schuchardt
2020-05-07  2:50           ` AKASHI Takahiro
2020-05-07 12:05           ` Sughosh Ganu
2020-04-27  9:48 ` [PATCH 05/10] efi_loader: capsule: add memory range capsule definitions AKASHI Takahiro
2020-04-27  9:48 ` [PATCH 06/10] efi_loader: capsule: support firmware update AKASHI Takahiro
2020-04-27  9:48 ` [PATCH 07/10] efi_loader: add simple firmware management protocol for FIT image AKASHI Takahiro
2020-04-27  9:48 ` [PATCH 08/10] cmd: add "efidebug capsule" command AKASHI Takahiro
2020-04-30 12:38   ` Sughosh Ganu
2020-05-07  1:58     ` AKASHI Takahiro
2020-04-27  9:48 ` [PATCH 09/10] tools: add mkeficapsule command for UEFI capsule update test AKASHI Takahiro
2020-04-27 20:15   ` Heinrich Schuchardt
2020-04-28  1:52     ` AKASHI Takahiro [this message]
2020-04-27  9:48 ` [PATCH 10/10] test/py: add a test for efi firmware update capsule AKASHI Takahiro
2020-04-27 20:33 ` [PATCH 00/10] efi_loader: add capsule update support Heinrich Schuchardt
2020-04-27 23:45   ` AKASHI Takahiro

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=20200428015225.GE16947@laputa \
    --to=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.