From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C972DC433F5 for ; Mon, 25 Oct 2021 05:40:36 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id CD29360F6F for ; Mon, 25 Oct 2021 05:40:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org CD29360F6F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B3DA7815CE; Mon, 25 Oct 2021 07:40:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="kMwiYwiW"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2866482BA5; Mon, 25 Oct 2021 07:40:30 +0200 (CEST) Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7F3F180F5F for ; Mon, 25 Oct 2021 07:40:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masami.hiramatsu@linaro.org Received: by mail-il1-x129.google.com with SMTP id h2so11530523ili.11 for ; Sun, 24 Oct 2021 22:40:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=ySETGV5TgdaAXuBfa7cLiDQ9A9jcrNZSOXQFqq7Xpo4=; b=kMwiYwiWp9iLurCoSEpLbIqo59jLAQkFxl2JWAbWPCphTYH51HJMmWvq3rjT/H0Was Yuksp+HVz08y8bn2W785CyFu/trdIWtcFpXU/lJi8biwZBsutybeI/VlFt7XQt/NUa6H b/UBIfSA+S+EQS39/wHAuooGO+NlfpKRklOkLrTrhlLeewmpTR/D8FQ2G97KKHX669Al W/rmFfHKWNbQ7XvNW8T3XMgneQMetAbOTYbQOqE1x4ghnp97zlgiJqIf7U56NCkaTdSw rN1qPD52wsqEdoeS7fJHt0RwfGfakkK7jMwaiqtk1HhjDjecFePwxW5TrqS16hYM7nAh wvhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=ySETGV5TgdaAXuBfa7cLiDQ9A9jcrNZSOXQFqq7Xpo4=; b=RxQ6KfTAUZsjT3xD16zum9k2FmiVLz2alIIbuc4qy7mMhbhFRDMj1Qa0SKOiouVyIk G2bZP9NQqR54oYQy9NqMiWAYA2Ye2v1xcp+ShqrJzMEMHPSi2a33U7L+7osvO6D89g/J expF9AqS8sOUcriYHFtUtVoQXgkJz6DJgWq+mhrk/cBsyqRHoZXaIsXrRvAiQfvL//59 n2y2OehnWaeRArNkxKj9VlY6Ttm2WaeZ+m2Oi7dyLfu62zeYt6XVLwA4cXBvZwik7hyY FscWuWegNAfhYLTvkl+5oM+7L0tBmlvlSQYdsGNQBrUmh+ua5uW/NS6yQ8ZoXQ3WDZA2 wVuA== X-Gm-Message-State: AOAM530wMX3NXVHdnR9NGe/ejepFFd9Usx4MJE8BajD+Jh5t22YHvODi UX5K5znl/SrhApJIAiQnjjRmR6GDKPB84x65yUb27g== X-Google-Smtp-Source: ABdhPJyilL+i2jpH3gq5LSRtX1tELyJDXFDoT2E0nltF4N59e89TnXEYO3CReuKbeh6xcq1vVrzFiACrYJ1GSOg2H0E= X-Received: by 2002:a05:6e02:144e:: with SMTP id p14mr8720561ilo.286.1635140422916; Sun, 24 Oct 2021 22:40:22 -0700 (PDT) MIME-Version: 1.0 References: <20211007062340.72207-1-takahiro.akashi@linaro.org> <20211007062340.72207-6-takahiro.akashi@linaro.org> <20211025031245.GB44989@laputa> In-Reply-To: <20211025031245.GB44989@laputa> From: Masami Hiramatsu Date: Mon, 25 Oct 2021 14:40:11 +0900 Message-ID: Subject: Re: [PATCH v4 05/11] tools: mkeficapsule: add firmwware image signing To: AKASHI Takahiro , Masami Hiramatsu , Heinrich Schuchardt , Alexander Graf , Ilias Apalodimas , Sughosh Ganu , U-Boot Mailing List Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Hi Takahiro, 2021=E5=B9=B410=E6=9C=8825=E6=97=A5(=E6=9C=88) 12:12 AKASHI Takahiro : > > Hi, Masami, > > On Wed, Oct 20, 2021 at 05:17:12PM +0900, Masami Hiramatsu wrote: > > Hello Akashi-san, > > > > Can you split this patch out from this series? > > It seems that the series is doing several different things. This > > capsule signing has no alternatives, but others are modifying or > > replacing the current existing feature. In other words, this capsule > > signing feature has no alternatives, but others we can continue to > > use. > > What do you specifically mean by "existing feature" or "others we can > continue to use"? > > All the commits, I think, in this series are closely related with > each other. I meant I can use the devicetree to embed the authentication key in the U-Boot itself (currently U-Boot expects that.) Or is there any issue with putting the key in the devicetree? Without this patch, I can not sign the capsule file for U-Boot, since the GenerateCapsule in EDK2 supports old format (is that updated?) Thank you, > > -Takahiro Akashi > > > Thank you, > > > > 2021=E5=B9=B410=E6=9C=887=E6=97=A5(=E6=9C=A8) 15:25 AKASHI Takahiro : > > > > > > With this enhancement, mkeficapsule will be able to sign a capsule > > > file when it is created. A signature added will be used later > > > in the verification at FMP's SetImage() call. > > > > > > To do that, We need specify additional command parameters: > > > -monotonic-cout : monotonic count > > > -private-key : private key file > > > -certificate : certificate file > > > Only when all of those parameters are given, a signature will be adde= d > > > to a capsule file. > > > > > > Users are expected to maintain and increment the monotonic count at > > > every time of the update for each firmware image. > > > > > > Signed-off-by: AKASHI Takahiro > > > --- > > > tools/Kconfig | 7 + > > > tools/Makefile | 8 +- > > > tools/mkeficapsule.c | 435 +++++++++++++++++++++++++++++++++++++++--= -- > > > 3 files changed, 416 insertions(+), 34 deletions(-) > > > > > > diff --git a/tools/Kconfig b/tools/Kconfig > > > index d6f82cd949b5..9a37ed035311 100644 > > > --- a/tools/Kconfig > > > +++ b/tools/Kconfig > > > @@ -20,4 +20,11 @@ config TOOLS_LIBCRYPTO > > > This selection does not affect target features, such as run= time FIT > > > signature verification. > > > > > > +config TOOLS_MKEFICAPSULE > > > + bool "Build efimkcapsule command" > > > + default y if EFI_CAPSULE_ON_DISK > > > + help > > > + This command allows users to create a UEFI capsule file and= , > > > + optionally sign that file. If you want to enable UEFI capsu= le > > > + update feature on your target, you certainly need this. > > > endmenu > > > diff --git a/tools/Makefile b/tools/Makefile > > > index 4a86321f6467..6ea3033dbfb8 100644 > > > --- a/tools/Makefile > > > +++ b/tools/Makefile > > > @@ -237,8 +237,12 @@ hostprogs-$(CONFIG_MIPS) +=3D mips-relocs > > > hostprogs-$(CONFIG_ASN1_COMPILER) +=3D asn1_compiler > > > HOSTCFLAGS_asn1_compiler.o =3D -idirafter $(srctree)/include > > > > > > -mkeficapsule-objs :=3D mkeficapsule.o $(LIBFDT_OBJS) > > > -hostprogs-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) +=3D mkeficapsule > > > +HOSTLDLIBS_mkeficapsule +=3D -luuid > > > +ifeq ($(CONFIG_TOOLS_LIBCRYPTO),y) > > > +HOSTLDLIBS_mkeficapsule +=3D \ > > > + $(shell pkg-config --libs libssl libcrypto 2> /dev/null || ec= ho "-lssl -lcrypto") > > > +endif > > > +hostprogs-$(CONFIG_TOOLS_MKEFICAPSULE) +=3D mkeficapsule > > > > > > # We build some files with extra pedantic flags to try to minimize t= hings > > > # that won't build on some weird host compiler -- though there are l= ots of > > > diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c > > > index 4995ba4e0c2a..5541e4bda894 100644 > > > --- a/tools/mkeficapsule.c > > > +++ b/tools/mkeficapsule.c > > > @@ -15,6 +15,16 @@ > > > #include > > > #include > > > > > > +#include > > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#endif > > > + > > > typedef __u8 u8; > > > typedef __u16 u16; > > > typedef __u32 u32; > > > @@ -38,12 +48,25 @@ efi_guid_t efi_guid_image_type_uboot_fit =3D > > > EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID; > > > efi_guid_t efi_guid_image_type_uboot_raw =3D > > > EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID; > > > +efi_guid_t efi_guid_cert_type_pkcs7 =3D EFI_CERT_TYPE_PKCS7_GUID; > > > + > > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > > +static const char *opts_short =3D "f:r:i:I:v:p:c:m:dh"; > > > +#else > > > +static const char *opts_short =3D "f:r:i:I:v:h"; > > > +#endif > > > > > > static struct option options[] =3D { > > > {"fit", required_argument, NULL, 'f'}, > > > {"raw", required_argument, NULL, 'r'}, > > > {"index", required_argument, NULL, 'i'}, > > > {"instance", required_argument, NULL, 'I'}, > > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > > + {"private-key", required_argument, NULL, 'p'}, > > > + {"certificate", required_argument, NULL, 'c'}, > > > + {"monotonic-count", required_argument, NULL, 'm'}, > > > + {"dump-sig", no_argument, NULL, 'd'}, > > > +#endif > > > {"help", no_argument, NULL, 'h'}, > > > {NULL, 0, NULL, 0}, > > > }; > > > @@ -57,16 +80,280 @@ static void print_usage(void) > > > "\t-r, --raw new raw image file\n" > > > "\t-i, --index update image index\n" > > > "\t-I, --instance update hardware instanc= e\n" > > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > > + "\t-p, --private-key private key file\= n" > > > + "\t-c, --certificate signer's certific= ate file\n" > > > + "\t-m, --monotonic-count monotonic count\n= " > > > + "\t-d, --dump_sig dump signature (*.p7)\n= " > > > +#endif > > > "\t-h, --help print a help message\n"= , > > > tool_name); > > > } > > > > > > +/** > > > + * auth_context - authentication context > > > + * @key_file: Path to a private key file > > > + * @cert_file: Path to a certificate file > > > + * @image_data: Pointer to firmware data > > > + * @image_size: Size of firmware data > > > + * @auth: Authentication header > > > + * @sig_data: Signature data > > > + * @sig_size: Size of signature data > > > + * > > > + * Data structure used in create_auth_data(). @key_file through > > > + * @image_size are input parameters. @auth, @sig_data and @sig_size > > > + * are filled in by create_auth_data(). > > > + */ > > > +struct auth_context { > > > + char *key_file; > > > + char *cert_file; > > > + u8 *image_data; > > > + size_t image_size; > > > + struct efi_firmware_image_authentication auth; > > > + u8 *sig_data; > > > + size_t sig_size; > > > +}; > > > + > > > +static int dump_sig; > > > + > > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > > +/** > > > + * fileio-read_pkey - read out a private key > > > + * @filename: Path to a private key file > > > + * > > > + * Read out a private key file and parse it into "EVP_PKEY" structur= e. > > > + * > > > + * Return: > > > + * * Pointer to private key structure - on success > > > + * * NULL - on failure > > > + */ > > > +static EVP_PKEY *fileio_read_pkey(const char *filename) > > > +{ > > > + EVP_PKEY *key =3D NULL; > > > + BIO *bio; > > > + > > > + bio =3D BIO_new_file(filename, "r"); > > > + if (!bio) > > > + goto out; > > > + > > > + key =3D PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); > > > + > > > +out: > > > + BIO_free_all(bio); > > > + if (!key) { > > > + printf("Can't load key from file '%s'\n", filename); > > > + ERR_print_errors_fp(stderr); > > > + } > > > + > > > + return key; > > > +} > > > + > > > +/** > > > + * fileio-read_cert - read out a certificate > > > + * @filename: Path to a certificate file > > > + * > > > + * Read out a certificate file and parse it into "X509" structure. > > > + * > > > + * Return: > > > + * * Pointer to certificate structure - on success > > > + * * NULL - on failure > > > + */ > > > +static X509 *fileio_read_cert(const char *filename) > > > +{ > > > + X509 *cert =3D NULL; > > > + BIO *bio; > > > + > > > + bio =3D BIO_new_file(filename, "r"); > > > + if (!bio) > > > + goto out; > > > + > > > + cert =3D PEM_read_bio_X509(bio, NULL, NULL, NULL); > > > + > > > +out: > > > + BIO_free_all(bio); > > > + if (!cert) { > > > + printf("Can't load certificate from file '%s'\n", fil= ename); > > > + ERR_print_errors_fp(stderr); > > > + } > > > + > > > + return cert; > > > +} > > > + > > > +/** > > > + * create_auth_data - compose authentication data in capsule > > > + * @auth_context: Pointer to authentication context > > > + * > > > + * Fill up an authentication header (.auth) and signature data (.sig= _data) > > > + * in @auth_context, using library functions from openssl. > > > + * All the parameters in @auth_context must be filled in by a caller= . > > > + * > > > + * Return: > > > + * * 0 - on success > > > + * * -1 - on failure > > > + */ > > > +static int create_auth_data(struct auth_context *ctx) > > > +{ > > > + EVP_PKEY *key =3D NULL; > > > + X509 *cert =3D NULL; > > > + BIO *data_bio =3D NULL; > > > + const EVP_MD *md; > > > + PKCS7 *p7; > > > + int flags, ret =3D -1; > > > + > > > + OpenSSL_add_all_digests(); > > > + OpenSSL_add_all_ciphers(); > > > + ERR_load_crypto_strings(); > > > + > > > + key =3D fileio_read_pkey(ctx->key_file); > > > + if (!key) > > > + goto err; > > > + cert =3D fileio_read_cert(ctx->cert_file); > > > + if (!cert) > > > + goto err; > > > + > > > + /* > > > + * create a BIO, containing: > > > + * * firmware image > > > + * * monotonic count > > > + * in this order! > > > + * See EDK2's FmpAuthenticatedHandlerRsa2048Sha256() > > > + */ > > > + data_bio =3D BIO_new(BIO_s_mem()); > > > + BIO_write(data_bio, ctx->image_data, ctx->image_size); > > > + BIO_write(data_bio, &ctx->auth.monotonic_count, > > > + sizeof(ctx->auth.monotonic_count)); > > > + > > > + md =3D EVP_get_digestbyname("SHA256"); > > > + if (!md) > > > + goto err; > > > + > > > + /* create signature */ > > > + /* TODO: maybe add PKCS7_NOATTR and PKCS7_NOSMIMECAP */ > > > + flags =3D PKCS7_BINARY | PKCS7_DETACHED; > > > + p7 =3D PKCS7_sign(NULL, NULL, NULL, data_bio, flags | PKCS7_P= ARTIAL); > > > + if (!p7) > > > + goto err; > > > + if (!PKCS7_sign_add_signer(p7, cert, key, md, flags)) > > > + goto err; > > > + if (!PKCS7_final(p7, data_bio, flags)) > > > + goto err; > > > + > > > + /* convert pkcs7 into DER */ > > > + ctx->sig_data =3D NULL; > > > + ctx->sig_size =3D ASN1_item_i2d((ASN1_VALUE *)p7, &ctx->sig_d= ata, > > > + ASN1_ITEM_rptr(PKCS7)); > > > + if (!ctx->sig_size) > > > + goto err; > > > + > > > + /* fill auth_info */ > > > + ctx->auth.auth_info.hdr.dwLength =3D sizeof(ctx->auth.auth_in= fo) > > > + + ctx->sig_size; > > > + ctx->auth.auth_info.hdr.wRevision =3D WIN_CERT_REVISION_2_0; > > > + ctx->auth.auth_info.hdr.wCertificateType =3D WIN_CERT_TYPE_EF= I_GUID; > > > + memcpy(&ctx->auth.auth_info.cert_type, &efi_guid_cert_type_pk= cs7, > > > + sizeof(efi_guid_cert_type_pkcs7)); > > > + > > > + ret =3D 0; > > > +err: > > > + BIO_free_all(data_bio); > > > + EVP_PKEY_free(key); > > > + X509_free(cert); > > > + > > > + return ret; > > > +} > > > + > > > +/** > > > + * dump_signature - dump out a signature > > > + * @path: Path to a capsule file > > > + * @signature: Signature data > > > + * @sig_size: Size of signature data > > > + * > > > + * Signature data pointed to by @signature will be saved into > > > + * a file whose file name is @path with ".p7" suffix. > > > + * > > > + * Return: > > > + * * 0 - on success > > > + * * -1 - on failure > > > + */ > > > +static int dump_signature(const char *path, u8 *signature, size_t si= g_size) > > > +{ > > > + char *sig_path; > > > + FILE *f; > > > + size_t size; > > > + int ret =3D -1; > > > + > > > + sig_path =3D malloc(strlen(path) + 3 + 1); > > > + if (!sig_path) > > > + return ret; > > > + > > > + sprintf(sig_path, "%s.p7", path); > > > + f =3D fopen(sig_path, "w"); > > > + if (!f) > > > + goto err; > > > + > > > + size =3D fwrite(signature, 1, sig_size, f); > > > + if (size =3D=3D sig_size) > > > + ret =3D 0; > > > + > > > + fclose(f); > > > +err: > > > + free(sig_path); > > > + return ret; > > > +} > > > + > > > +/** > > > + * free_sig_data - free out signature data > > > + * @ctx: Pointer to authentication context > > > + * > > > + * Free signature data allocated in create_auth_data(). > > > + */ > > > +static void free_sig_data(struct auth_context *ctx) > > > +{ > > > + if (ctx->sig_size) > > > + OPENSSL_free(ctx->sig_data); > > > +} > > > +#else > > > +static int create_auth_data(struct auth_context *ctx) > > > +{ > > > + return 0; > > > +} > > > + > > > +static int dump_signature(const char *path, u8 *signature, size_t si= g_size) > > > +{ > > > + return 0; > > > +} > > > + > > > +static void free_sig_data(struct auth_context *ctx) {} > > > +#endif > > > + > > > +/** > > > + * create_fwbin - create an uefi capsule file > > > + * @path: Path to a created capsule file > > > + * @bin: Path to a firmware binary to encapsulate > > > + * @guid: GUID of related FMP driver > > > + * @index: Index number in capsule > > > + * @instance: Instance number in capsule > > > + * @mcount: Monotonic count in authentication information > > > + * @private_file: Path to a private key file > > > + * @cert_file: Path to a certificate file > > > + * > > > + * This function actually does the job of creating an uefi capsule f= ile. > > > + * All the arguments must be supplied. > > > + * If either @private_file ror @cert_file is NULL, the capsule file > > > + * won't be signed. > > > + * > > > + * Return: > > > + * * 0 - on success > > > + * * -1 - on failure > > > + */ > > > static int create_fwbin(char *path, char *bin, efi_guid_t *guid, > > > - unsigned long index, unsigned long instance) > > > + unsigned long index, unsigned long instance, > > > + uint64_t mcount, char *privkey_file, char *ce= rt_file) > > > { > > > struct efi_capsule_header header; > > > struct efi_firmware_management_capsule_header capsule; > > > struct efi_firmware_management_capsule_image_header image; > > > + struct auth_context auth_context; > > > FILE *f, *g; > > > struct stat bin_stat; > > > u8 *data; > > > @@ -76,8 +363,9 @@ static int create_fwbin(char *path, char *bin, efi= _guid_t *guid, > > > #ifdef DEBUG > > > printf("For output: %s\n", path); > > > printf("\tbin: %s\n\ttype: %pUl\n", bin, guid); > > > - printf("\tindex: %ld\n\tinstance: %ld\n", index, instance); > > > + printf("\tindex: %lu\n\tinstance: %lu\n", index, instance); > > > #endif > > > + auth_context.sig_size =3D 0; > > > > > > g =3D fopen(bin, "r"); > > > if (!g) { > > > @@ -93,11 +381,34 @@ static int create_fwbin(char *path, char *bin, e= fi_guid_t *guid, > > > printf("cannot allocate memory: %zx\n", (size_t)bin_s= tat.st_size); > > > goto err_1; > > > } > > > - f =3D fopen(path, "w"); > > > - if (!f) { > > > - printf("cannot open %s\n", path); > > > + > > > + size =3D fread(data, 1, bin_stat.st_size, g); > > > + if (size < bin_stat.st_size) { > > > + printf("read failed (%zx)\n", size); > > > goto err_2; > > > } > > > + > > > + /* first, calculate signature to determine its size */ > > > + if (privkey_file && cert_file) { > > > + auth_context.key_file =3D privkey_file; > > > + auth_context.cert_file =3D cert_file; > > > + auth_context.auth.monotonic_count =3D mcount; > > > + auth_context.image_data =3D data; > > > + auth_context.image_size =3D bin_stat.st_size; > > > + > > > + if (create_auth_data(&auth_context)) { > > > + printf("Signing firmware image failed\n"); > > > + goto err_3; > > > + } > > > + > > > + if (dump_sig && > > > + dump_signature(path, auth_context.sig_data, > > > + auth_context.sig_size)) { > > > + printf("Creating signature file failed\n"); > > > + goto err_3; > > > + } > > > + } > > > + > > > header.capsule_guid =3D efi_guid_fm_capsule; > > > header.header_size =3D sizeof(header); > > > /* TODO: The current implementation ignores flags */ > > > @@ -106,11 +417,20 @@ static int create_fwbin(char *path, char *bin, = efi_guid_t *guid, > > > + sizeof(capsule) + sizeof(u6= 4) > > > + sizeof(image) > > > + bin_stat.st_size; > > > + if (auth_context.sig_size) > > > + header.capsule_image_size +=3D sizeof(auth_context.au= th) > > > + + auth_context.sig_size; > > > + > > > + f =3D fopen(path, "w"); > > > + if (!f) { > > > + printf("cannot open %s\n", path); > > > + goto err_3; > > > + } > > > > > > size =3D fwrite(&header, 1, sizeof(header), f); > > > if (size < sizeof(header)) { > > > printf("write failed (%zx)\n", size); > > > - goto err_3; > > > + goto err_4; > > > } > > > > > > capsule.version =3D 0x00000001; > > > @@ -119,13 +439,13 @@ static int create_fwbin(char *path, char *bin, = efi_guid_t *guid, > > > size =3D fwrite(&capsule, 1, sizeof(capsule), f); > > > if (size < (sizeof(capsule))) { > > > printf("write failed (%zx)\n", size); > > > - goto err_3; > > > + goto err_4; > > > } > > > offset =3D sizeof(capsule) + sizeof(u64); > > > size =3D fwrite(&offset, 1, sizeof(offset), f); > > > if (size < sizeof(offset)) { > > > printf("write failed (%zx)\n", size); > > > - goto err_3; > > > + goto err_4; > > > } > > > > > > image.version =3D 0x00000003; > > > @@ -135,34 +455,53 @@ static int create_fwbin(char *path, char *bin, = efi_guid_t *guid, > > > image.reserved[1] =3D 0; > > > image.reserved[2] =3D 0; > > > image.update_image_size =3D bin_stat.st_size; > > > + if (auth_context.sig_size) > > > + image.update_image_size +=3D sizeof(auth_context.auth= ) > > > + + auth_context.sig_size; > > > image.update_vendor_code_size =3D 0; /* none */ > > > image.update_hardware_instance =3D instance; > > > image.image_capsule_support =3D 0; > > > + if (auth_context.sig_size) > > > + image.image_capsule_support |=3D CAPSULE_SUPPORT_AUTH= ENTICATION; > > > > > > size =3D fwrite(&image, 1, sizeof(image), f); > > > if (size < sizeof(image)) { > > > printf("write failed (%zx)\n", size); > > > - goto err_3; > > > + goto err_4; > > > } > > > - size =3D fread(data, 1, bin_stat.st_size, g); > > > - if (size < bin_stat.st_size) { > > > - printf("read failed (%zx)\n", size); > > > - goto err_3; > > > + > > > + if (auth_context.sig_size) { > > > + size =3D fwrite(&auth_context.auth, 1, > > > + sizeof(auth_context.auth), f); > > > + if (size < sizeof(auth_context.auth)) { > > > + printf("write failed (%zx)\n", size); > > > + goto err_4; > > > + } > > > + size =3D fwrite(auth_context.sig_data, 1, > > > + auth_context.sig_size, f); > > > + if (size < auth_context.sig_size) { > > > + printf("write failed (%zx)\n", size); > > > + goto err_4; > > > + } > > > } > > > + > > > size =3D fwrite(data, 1, bin_stat.st_size, f); > > > if (size < bin_stat.st_size) { > > > printf("write failed (%zx)\n", size); > > > - goto err_3; > > > + goto err_4; > > > } > > > > > > fclose(f); > > > fclose(g); > > > free(data); > > > + free_sig_data(&auth_context); > > > > > > return 0; > > > > > > -err_3: > > > +err_4: > > > fclose(f); > > > +err_3: > > > + free_sig_data(&auth_context); > > > err_2: > > > free(data); > > > err_1: > > > @@ -171,23 +510,37 @@ err_1: > > > return -1; > > > } > > > > > > -/* > > > - * Usage: > > > - * $ mkeficapsule -f > > > +/** > > > + * main - main entry function of mkeficapsule > > > + * @argc: Number of arguments > > > + * @argv: Array of pointers to arguments > > > + * > > > + * Create an uefi capsule file, optionally signing it. > > > + * Parse all the arguments and pass them on to create_fwbin(). > > > + * > > > + * Return: > > > + * * 0 - on success > > > + * * -1 - on failure > > > */ > > > int main(int argc, char **argv) > > > { > > > char *file; > > > efi_guid_t *guid; > > > unsigned long index, instance; > > > + uint64_t mcount; > > > + char *privkey_file, *cert_file; > > > int c, idx; > > > > > > file =3D NULL; > > > guid =3D NULL; > > > index =3D 0; > > > instance =3D 0; > > > + mcount =3D 0; > > > + privkey_file =3D NULL; > > > + cert_file =3D NULL; > > > + dump_sig =3D 0; > > > for (;;) { > > > - c =3D getopt_long(argc, argv, "f:r:i:I:v:h", options,= &idx); > > > + c =3D getopt_long(argc, argv, opts_short, options, &i= dx); > > > if (c =3D=3D -1) > > > break; > > > > > > @@ -214,29 +567,47 @@ int main(int argc, char **argv) > > > case 'I': > > > instance =3D strtoul(optarg, NULL, 0); > > > break; > > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > > + case 'p': > > > + if (privkey_file) { > > > + printf("Private Key already specified= \n"); > > > + return -1; > > > + } > > > + privkey_file =3D optarg; > > > + break; > > > + case 'c': > > > + if (cert_file) { > > > + printf("Certificate file already spec= ified\n"); > > > + return -1; > > > + } > > > + cert_file =3D optarg; > > > + break; > > > + case 'm': > > > + mcount =3D strtoul(optarg, NULL, 0); > > > + break; > > > + case 'd': > > > + dump_sig =3D 1; > > > + break; > > > +#endif /* CONFIG_TOOLS_LIBCRYPTO */ > > > case 'h': > > > print_usage(); > > > return 0; > > > } > > > } > > > > > > - /* need an output file */ > > > - if (argc !=3D optind + 1) { > > > - print_usage(); > > > - exit(EXIT_FAILURE); > > > - } > > > - > > > - /* need a fit image file or raw image file */ > > > - if (!file) { > > > + /* check necessary parameters */ > > > + if ((argc !=3D optind + 1) || !file || > > > + ((privkey_file && !cert_file) || > > > + (!privkey_file && cert_file))) { > > > print_usage(); > > > - exit(EXIT_SUCCESS); > > > + return -1; > > > } > > > > > > - if (create_fwbin(argv[optind], file, guid, index, instance) > > > - < 0) { > > > + if (create_fwbin(argv[optind], file, guid, index, instance, > > > + mcount, privkey_file, cert_file) < 0) { > > > printf("Creating firmware capsule failed\n"); > > > - exit(EXIT_FAILURE); > > > + return -1; > > > } > > > > > > - exit(EXIT_SUCCESS); > > > + return 0; > > > } > > > -- > > > 2.33.0 > > > > > > > > > -- > > Masami Hiramatsu --=20 Masami Hiramatsu