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 8BF82C433EF for ; Mon, 8 Nov 2021 04:38:34 +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 A86F861279 for ; Mon, 8 Nov 2021 04:38:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org A86F861279 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 55DBE83819; Mon, 8 Nov 2021 05:38:31 +0100 (CET) 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="yfSSKWLn"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1CB418381D; Mon, 8 Nov 2021 05:38:29 +0100 (CET) Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) (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 13D2F83817 for ; Mon, 8 Nov 2021 05:38:23 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pj1-x1033.google.com with SMTP id gn3so7185914pjb.0 for ; Sun, 07 Nov 2021 20:38:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to; bh=Ht3c/qTxkoHZu21T2ZILb14aTd9koah7/LS7fy597KA=; b=yfSSKWLnGQIz0MGWqmXaicgvV4qINwMilcKg2HsItdILefNzt2bfvdsg8ikSmcpuaH 0xBMpjrFK7CDyQLSemHxYX2/8FaIvvmBg4/cXsinbMxnOuq7j9hscSAW/MZUwlrtwXdj SPTfnshnQv8CuL/jvkPSUR6mMAun9AK38YdL4lOuuHhnWlW2Hx5DMpYQ35hYZ9kXnGiW LqrM/2UAgaXFE+P68Dp7h5UjeuApzsMsoxmm9A5xRWwDMhJCdv6Ejnc7/CLcLRHSsSf0 vBn89Z4MljcRtK5BWuXbkWnjOJeCpkmKaXkWnXliSw6MaszglH1sAC9ocBcV7Gi0fQRm 1lNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to; bh=Ht3c/qTxkoHZu21T2ZILb14aTd9koah7/LS7fy597KA=; b=Uo0X/lvv4AvZULBxBcI27HX8JVV5RBeRMf/di9bjJCwf2siSjClPiN5bkwwWYWkqy/ W36wUke1CUCnRRj8j4OLaOTxbkypCqg6Wj1o+l+sAnJ+ZFTJ2UdPvFNmFyFfVK9pBOmB 41DJ+USXUP47HTSur8e52ZtuoFCyQqtC8f3V1cAbUfpxBLVssRFB0TIJLPSB6/QPu19b cczpfBokLlPfpGBCVWBPrt1OZpUivb1pArQE1QtaFYUZuxs2FjZyjv/RNHmTppp7Ttz9 Kki7+oUyLv5hUzSTz9FJFdwXJfVqmsLM4QWEyWOkyIFHSbQXgEGGcAFQIYmGBSuJyoeU jvgw== X-Gm-Message-State: AOAM530HxoZ+lBTElg7yU2z7WnADMCcbr3uBgnN4oIMbbupAldDizNbk LtObRnvPAlu2CAOhcDxUtbSQGw== X-Google-Smtp-Source: ABdhPJxHSZqP7+WjkX0x6Zf1osYUR/9dQcltiGv+nCp/QB/3K/BwTzEtXrk2KflE8+2WRqo0zi9M0A== X-Received: by 2002:a17:902:ecc7:b0:141:e920:3b4c with SMTP id a7-20020a170902ecc700b00141e9203b4cmr49230689plh.64.1636346301276; Sun, 07 Nov 2021 20:38:21 -0800 (PST) Received: from laputa ([2400:4050:c3e1:100:98bf:5be1:75ff:1c8a]) by smtp.gmail.com with ESMTPSA id h15sm14485380pfv.212.2021.11.07.20.38.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Nov 2021 20:38:20 -0800 (PST) Date: Mon, 8 Nov 2021 13:38:16 +0900 From: AKASHI Takahiro To: Heinrich Schuchardt Cc: ilias.apalodimas@linaro.org, sughosh.ganu@linaro.org, masami.hiramatsu@linaro.org, u-boot@lists.denx.de, agraf@csgraf.de, sjg@chromium.org Subject: Re: [PATCH v6 03/12] tools: mkeficapsule: add firmwware image signing Message-ID: <20211108043816.GC16401@laputa> Mail-Followup-To: AKASHI Takahiro , Heinrich Schuchardt , ilias.apalodimas@linaro.org, sughosh.ganu@linaro.org, masami.hiramatsu@linaro.org, u-boot@lists.denx.de, agraf@csgraf.de, sjg@chromium.org References: <20211102005512.96019-1-takahiro.akashi@linaro.org> <20211102005512.96019-4-takahiro.akashi@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 On Sun, Nov 07, 2021 at 07:53:24AM +0100, Heinrich Schuchardt wrote: > On 11/2/21 01:55, AKASHI Takahiro wrote: > > 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 added > > 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 > > Reviewed-by: Simon Glass > > You licensed tools/mkeficapsule.c under GPL-2.0 which is imcompatible > with OpenSSL (https://www.gnu.org/licenses/license-list.html#OpenSSL). In fact, this is not a this-tool-specific issue, but other tools, including mkimage, have it and that is why CONFIG_TOOLS_LIBCRYPTO was introduced. # I think that I have mentioned the issue in my past comment. -Takahiro Akashi > Either agree with all contributors of the tool to use a more liberal > license or use another library. > > > --- > > tools/Kconfig | 8 + > > tools/Makefile | 8 +- > > tools/mkeficapsule.c | 382 ++++++++++++++++++++++++++++++++++++++++--- > > 3 files changed, 376 insertions(+), 22 deletions(-) > > > > diff --git a/tools/Kconfig b/tools/Kconfig > > index 91ce8ae3e516..117c921da3fe 100644 > > --- a/tools/Kconfig > > +++ b/tools/Kconfig > > @@ -90,4 +90,12 @@ config TOOLS_SHA512 > > help > > Enable SHA512 support in the tools builds > > > > +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 capsule > > + update feature on your target, you certainly need this. > > + > > endmenu > > diff --git a/tools/Makefile b/tools/Makefile > > index b45219e2c30c..5a73cc4b363d 100644 > > --- a/tools/Makefile > > +++ b/tools/Makefile > > @@ -238,8 +238,12 @@ hostprogs-$(CONFIG_MIPS) += mips-relocs > > hostprogs-$(CONFIG_ASN1_COMPILER) += asn1_compiler > > HOSTCFLAGS_asn1_compiler.o = -idirafter $(srctree)/include > > > > -mkeficapsule-objs := mkeficapsule.o $(LIBFDT_OBJS) > > -hostprogs-$(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) += mkeficapsule > > +HOSTLDLIBS_mkeficapsule += -luuid > > +ifeq ($(CONFIG_TOOLS_LIBCRYPTO),y) > > +HOSTLDLIBS_mkeficapsule += \ > > + $(shell pkg-config --libs libssl libcrypto 2> /dev/null || echo "-lssl -lcrypto") > > +endif > > +hostprogs-$(CONFIG_TOOLS_MKEFICAPSULE) += 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 > > diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c > > index 8427fedd941c..086757ee8ad7 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 = > > EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID; > > efi_guid_t efi_guid_image_type_uboot_raw = > > EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID; > > +efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID; > > + > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > +static const char *opts_short = "f:r:i:I:v:p:c:m:dh"; > > +#else > > +static const char *opts_short = "f:r:i:I:v:h"; > > +#endif > > > > static struct option options[] = { > > {"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,10 +80,252 @@ 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 instance\n" > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > + "\t-p, --private-key private key file\n" > > + "\t-c, --certificate signer's certificate 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" structure. > > + * > > + * Return: > > + * * Pointer to private key structure - on success > > + * * NULL - on failure > > + */ > > +static EVP_PKEY *fileio_read_pkey(const char *filename) > > +{ > > + EVP_PKEY *key = NULL; > > + BIO *bio; > > + > > + bio = BIO_new_file(filename, "r"); > > + if (!bio) > > + goto out; > > + > > + key = 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 = NULL; > > + BIO *bio; > > + > > + bio = BIO_new_file(filename, "r"); > > + if (!bio) > > + goto out; > > + > > + cert = PEM_read_bio_X509(bio, NULL, NULL, NULL); > > + > > +out: > > + BIO_free_all(bio); > > + if (!cert) { > > + printf("Can't load certificate from file '%s'\n", filename); > > + 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 = NULL; > > + X509 *cert = NULL; > > + BIO *data_bio = NULL; > > + const EVP_MD *md; > > + PKCS7 *p7; > > + int flags, ret = -1; > > + > > + OpenSSL_add_all_digests(); > > + OpenSSL_add_all_ciphers(); > > + ERR_load_crypto_strings(); > > + > > + key = fileio_read_pkey(ctx->key_file); > > + if (!key) > > + goto err; > > + cert = 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 = 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 = EVP_get_digestbyname("SHA256"); > > + if (!md) > > + goto err; > > + > > + /* create signature */ > > + /* TODO: maybe add PKCS7_NOATTR and PKCS7_NOSMIMECAP */ > > + flags = PKCS7_BINARY | PKCS7_DETACHED; > > + p7 = PKCS7_sign(NULL, NULL, NULL, data_bio, flags | PKCS7_PARTIAL); > > + 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 = NULL; > > + ctx->sig_size = ASN1_item_i2d((ASN1_VALUE *)p7, &ctx->sig_data, > > + ASN1_ITEM_rptr(PKCS7)); > > + if (!ctx->sig_size) > > + goto err; > > + > > + /* fill auth_info */ > > + ctx->auth.auth_info.hdr.dwLength = sizeof(ctx->auth.auth_info) > > + + ctx->sig_size; > > + ctx->auth.auth_info.hdr.wRevision = WIN_CERT_REVISION_2_0; > > + ctx->auth.auth_info.hdr.wCertificateType = WIN_CERT_TYPE_EFI_GUID; > > + memcpy(&ctx->auth.auth_info.cert_type, &efi_guid_cert_type_pkcs7, > > + sizeof(efi_guid_cert_type_pkcs7)); > > + > > + ret = 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 sig_size) > > +{ > > + char *sig_path; > > + FILE *f; > > + size_t size; > > + int ret = -1; > > + > > + sig_path = malloc(strlen(path) + 3 + 1); > > + if (!sig_path) > > + return ret; > > + > > + sprintf(sig_path, "%s.p7", path); > > + f = fopen(sig_path, "w"); > > + if (!f) > > + goto err; > > + > > + size = fwrite(signature, 1, sig_size, f); > > + if (size == sig_size) > > + ret = 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 sig_size) > > +{ > > + return 0; > > +} > > + > > +static void free_sig_data(struct auth_context *ctx) {} > > +#endif > > + > > /** > > * read_bin_file - read a firmware binary file > > * @bin: Path to a firmware binary file > > @@ -162,11 +427,13 @@ static int write_capsule_file(FILE *f, void *data, size_t size, const char *msg) > > * * -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 *cert_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; > > void *data; > > off_t bin_size; > > @@ -176,9 +443,9 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, > > #ifdef DEBUG > > scripts/checkpatch.pl gives the following warning for this file: > > Use 'if (IS_ENABLED(CONFIG...))' instead of '#if or #ifdef' where possible. > > Best regards > > Heinrich > > > 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 = 0; > > f = NULL; > > data = NULL; > > ret = -1; > > @@ -189,6 +456,27 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, > > if (read_bin_file(bin, &data, &bin_size)) > > goto err; > > > > + /* first, calculate signature to determine its size */ > > + if (privkey_file && cert_file) { > > + auth_context.key_file = privkey_file; > > + auth_context.cert_file = cert_file; > > + auth_context.auth.monotonic_count = mcount; > > + auth_context.image_data = data; > > + auth_context.image_size = bin_size; > > + > > + if (create_auth_data(&auth_context)) { > > + printf("Signing firmware image failed\n"); > > + goto err; > > + } > > + > > + if (dump_sig && > > + dump_signature(path, auth_context.sig_data, > > + auth_context.sig_size)) { > > + printf("Creating signature file failed\n"); > > + goto err; > > + } > > + } > > + > > /* > > * write a capsule file > > */ > > @@ -209,6 +497,9 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, > > + sizeof(capsule) + sizeof(u64) > > + sizeof(image) > > + bin_size; > > + if (auth_context.sig_size) > > + header.capsule_image_size += sizeof(auth_context.auth) > > + + auth_context.sig_size; > > if (write_capsule_file(f, &header, sizeof(header), > > "Capsule header")) > > goto err; > > @@ -239,13 +530,32 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, > > image.reserved[1] = 0; > > image.reserved[2] = 0; > > image.update_image_size = bin_size; > > + if (auth_context.sig_size) > > + image.update_image_size += sizeof(auth_context.auth) > > + + auth_context.sig_size; > > image.update_vendor_code_size = 0; /* none */ > > image.update_hardware_instance = instance; > > image.image_capsule_support = 0; > > + if (auth_context.sig_size) > > + image.image_capsule_support |= CAPSULE_SUPPORT_AUTHENTICATION; > > if (write_capsule_file(f, &image, sizeof(image), > > "Firmware capsule image header")) > > goto err; > > > > + /* > > + * signature > > + */ > > + if (auth_context.sig_size) { > > + if (write_capsule_file(f, &auth_context.auth, > > + sizeof(auth_context.auth), > > + "Authentication header")) > > + goto err; > > + > > + if (write_capsule_file(f, auth_context.sig_data, > > + auth_context.sig_size, "Signature")) > > + goto err; > > + } > > + > > /* > > * firmware binary > > */ > > @@ -262,23 +572,37 @@ err: > > return ret; > > } > > > > -/* > > - * 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 = NULL; > > guid = NULL; > > index = 0; > > instance = 0; > > + mcount = 0; > > + privkey_file = NULL; > > + cert_file = NULL; > > + dump_sig = 0; > > for (;;) { > > - c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx); > > + c = getopt_long(argc, argv, opts_short, options, &idx); > > if (c == -1) > > break; > > > > @@ -286,7 +610,7 @@ int main(int argc, char **argv) > > case 'f': > > if (file) { > > printf("Image already specified\n"); > > - return -1; > > + exit(EXIT_FAILURE); > > } > > file = optarg; > > guid = &efi_guid_image_type_uboot_fit; > > @@ -294,7 +618,7 @@ int main(int argc, char **argv) > > case 'r': > > if (file) { > > printf("Image already specified\n"); > > - return -1; > > + exit(EXIT_FAILURE); > > } > > file = optarg; > > guid = &efi_guid_image_type_uboot_raw; > > @@ -305,26 +629,44 @@ int main(int argc, char **argv) > > case 'I': > > instance = strtoul(optarg, NULL, 0); > > break; > > +#ifdef CONFIG_TOOLS_LIBCRYPTO > > + case 'p': > > + if (privkey_file) { > > + printf("Private Key already specified\n"); > > + exit(EXIT_FAILURE); > > + } > > + privkey_file = optarg; > > + break; > > + case 'c': > > + if (cert_file) { > > + printf("Certificate file already specified\n"); > > + exit(EXIT_FAILURE); > > + } > > + cert_file = optarg; > > + break; > > + case 'm': > > + mcount = strtoul(optarg, NULL, 0); > > + break; > > + case 'd': > > + dump_sig = 1; > > + break; > > +#endif /* CONFIG_TOOLS_LIBCRYPTO */ > > case 'h': > > print_usage(); > > - return 0; > > + exit(EXIT_SUCCESS); > > } > > } > > > > - /* need an output file */ > > - if (argc != optind + 1) { > > + /* check necessary parameters */ > > + if ((argc != optind + 1) || !file || > > + ((privkey_file && !cert_file) || > > + (!privkey_file && cert_file))) { > > print_usage(); > > exit(EXIT_FAILURE); > > } > > > > - /* need a fit image file or raw image file */ > > - if (!file) { > > - print_usage(); > > - exit(EXIT_SUCCESS); > > - } > > - > > - 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); > > } > >