From patchwork Mon Nov 30 09:12:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 334699 Delivered-To: patch@linaro.org Received: by 2002:a92:5e16:0:0:0:0:0 with SMTP id s22csp4468498ilb; Mon, 30 Nov 2020 01:14:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJwzatMNWk/PdiOjomHqip5PTxBOLSJMuQkwVwzCEt9f6N7MAv1CIQRGxXlS0K+9QXQsKi7M X-Received: by 2002:a05:6402:3089:: with SMTP id de9mr20531541edb.100.1606727678377; Mon, 30 Nov 2020 01:14:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606727678; cv=none; d=google.com; s=arc-20160816; b=haWkklS/4/M+a1kTUvNcCtVK8ovlQ2glhYJlC/43c2GZCLlw9KGwTtTVW9fKnYLN+j RUJu/pQkIGRrsz6Sxueg0jHXO+lQUxVPOFKrI/qOHY7qP27wKNyR6z6EpEBFLv1xplw/ kKWyur7qmoTy1FP+3twm/bNrRMIq8l7pdQknObQq4fuW5vPYcNBa87AXkY/qwXGJxbNz SWXVkHLPQIeFEIhoJnEP5XXV47Vw04DEjPITEhdWCyLUsPhkWgs9YJJ/zCF155alXhg+ xbbv7hKtsDB82+7HR9FxZRgbR3LlkQm2m+RRLfgN1IeEgnWoO8hpRgQ+F4mBrgNJ6mv2 XTEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=vaCBC+HkYpmOU/6kwQCDudH7VmAHzjQuBwCrYHrv5bg=; b=O/zn+gd58zBd/8uNimsagzT8CErDvcKf6Yz34uMFQn0UTlks46OY1JUD75VEQ4P2X7 Q+rKsHPP7BmQFA0wk70CwGQSb3IPJr9azD7qr5eW1VYDW3D0WiWQEpn5Co2pNFCuq7Fn yJ74HsJoc9u8SLR4ApV9yvfHXNcY4nqNV2qpcT9NrhvatYn4mOC9zHIGY0kprRFBZOfz N2RbFYPAZsswwMnywyOJqEFqN69U757WNcZ0ODmVv73mlz3N0xyVNoBy3iMdSjIKB8B/ bjjKwr1YGt/1IDSrx6IcZUVNXfNuKfcgAo4TsMxIv/G1bYUKT0TC09bwc6ctCE4MVl62 p/0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="ARRdG/gn"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id g3si5684062ejw.12.2020.11.30.01.14.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2020 01:14:38 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="ARRdG/gn"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E71F8827E5; Mon, 30 Nov 2020 10:13:49 +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="ARRdG/gn"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 72E83827D0; Mon, 30 Nov 2020 10:13:15 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) (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 4D73682641 for ; Mon, 30 Nov 2020 10:13:04 +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-pf1-x42b.google.com with SMTP id b63so9978051pfg.12 for ; Mon, 30 Nov 2020 01:13:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vaCBC+HkYpmOU/6kwQCDudH7VmAHzjQuBwCrYHrv5bg=; b=ARRdG/gnbpFaygLH2ZgoqufTpS7XfJTbTaViUUewrzWCLkOh3h/xpRlWeYP7/+ja9K X6YykNczoUxlDL6T1aM2vOHiXSBCRoig6IKrmrGZcSRzqqCe2by5g61ahQWRGlw9Hw72 PSU7M6488q6KPWXWo2moMMYFV/NGIgKK7FGk7ZRUD3L3vCZjmxL8FrHQgIfcBYuw1a6i LNcACz6/SM3nErRsbwJvuzd3c36kz76VpYKwtViEHl8SBQLE8l9oezqSL2d1yBi9d7nh inK8uPO83eSAeGoTLPmDgPbABHz27FuXCxIR+AEiENyhJ0OlafXhW9Z2eR8ULcEJISDU 6K7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vaCBC+HkYpmOU/6kwQCDudH7VmAHzjQuBwCrYHrv5bg=; b=fAdTL1Fqg0BTrpdsTnHXXzWpPcwK0TvtiXxA6QIORFTU4fwkVRgMjLXtB9GbHOdmeL YzQkhB6GZWGdwbkfZ1HHovrtnUWY8OmZw3lwTGRKaac3Coar9YX2USDCVGS8cPzrX88M lM+8S3uLObfoYM1a9yKRyT9YjlcTtjUiyfULLScVKDfodWPMUnonkJKOMR1YRS2IDYV6 PHYbESyjqe+m9io/2UcTjfzi9qg9xlhJP1wSjUpbbhsaR8WmVeodiJxrIoC1OKJrYYOV pwCUf1GI+Zwd66LQ/vcbgjqLCAmbZzsW5EHMtSwmQaTp7P5I72ca4QzAGfkrWpY+3tW3 kMsw== X-Gm-Message-State: AOAM533xf3Er0V0TPuDynAH/d5MZ+kcU+u1vViAl1UoKPnEpS8eOf6+J NvWWv3Na+M1Iozxa0AR8wMelYPuRH5PIOg== X-Received: by 2002:a63:8c55:: with SMTP id q21mr16617545pgn.362.1606727582703; Mon, 30 Nov 2020 01:13:02 -0800 (PST) Received: from localhost.localdomain (p784a5642.tkyea130.ap.so-net.ne.jp. [120.74.86.66]) by smtp.gmail.com with ESMTPSA id y19sm16015792pfn.147.2020.11.30.01.13.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2020 01:13:02 -0800 (PST) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH v10 08/11] tools: add mkeficapsule command for UEFI capsule update Date: Mon, 30 Nov 2020 18:12:15 +0900 Message-Id: <20201130091218.66413-9-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201130091218.66413-1-takahiro.akashi@linaro.org> References: <20201130091218.66413-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean 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 --- tools/Makefile | 2 + tools/mkeficapsule.c | 236 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 tools/mkeficapsule.c -- 2.28.0 diff --git a/tools/Makefile b/tools/Makefile index 51123fd92983..66d9376803e3 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -218,6 +218,8 @@ hostprogs-$(CONFIG_MIPS) += mips-relocs hostprogs-$(CONFIG_ASN1_COMPILER) += asn1_compiler HOSTCFLAGS_asn1_compiler.o = -idirafter $(srctree)/include +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..426038ea27ba --- /dev/null +++ b/tools/mkeficapsule.c @@ -0,0 +1,236 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2018 Linaro Limited + * Author: AKASHI Takahiro + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef __u8 u8; +typedef __u16 u16; +typedef __u32 u32; +typedef __u64 u64; +typedef __s16 s16; +typedef __s32 s32; + +#define aligned_u64 __aligned_u64 + +#ifndef __packed +#define __packed __attribute__((packed)) +#endif + +#include +#include + +static const char *tool_name = "mkeficapsule"; + +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; +efi_guid_t efi_guid_image_type_uboot_raw = + EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID; + +static struct option options[] = { + {"fit", required_argument, NULL, 'f'}, + {"raw", required_argument, NULL, 'r'}, + {"index", required_argument, NULL, 'i'}, + {"instance", required_argument, NULL, 'I'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0}, +}; + +static void print_usage(void) +{ + printf("Usage: %s [options] \n" + "Options:\n" + "\t--fit new FIT image file\n" + "\t--raw new raw image file\n" + "\t--index update image index\n" + "\t--instance update hardware instance\n" + "\t--help print a help message\n", + tool_name); +} + +static int create_fwbin(char *path, char *bin, efi_guid_t *guid, + unsigned long index, unsigned long instance) +{ + 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; + u64 offset; + +#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); +#endif + + 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; + size = fwrite(&capsule, 1, sizeof(capsule), f); + if (size < (sizeof(capsule))) { + printf("write failed (%lx)\n", size); + goto err_3; + } + offset = sizeof(capsule) + sizeof(u64); + size = fwrite(&offset, 1, sizeof(offset), f); + if (size < sizeof(offset)) { + printf("write failed (%lx)\n", size); + goto err_3; + } + + image.version = 0x00000003; + memcpy(&image.update_image_type_id, guid, sizeof(*guid)); + image.update_image_index = index; + image.update_image_size = bin_stat.st_size; + image.update_vendor_code_size = 0; /* none */ + image.update_hardware_instance = instance; + image.image_capsule_support = 0; + + 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; +} + +/* + * Usage: + * $ mkeficapsule -f + */ +int main(int argc, char **argv) +{ + char *file; + efi_guid_t *guid; + unsigned long index, instance; + int c, idx; + + file = NULL; + guid = NULL; + index = 0; + instance = 0; + for (;;) { + c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx); + if (c == -1) + break; + + switch (c) { + case 'f': + if (file) { + printf("Image already specified\n"); + return -1; + } + file = optarg; + guid = &efi_guid_image_type_uboot_fit; + break; + case 'r': + if (file) { + printf("Image already specified\n"); + return -1; + } + file = optarg; + guid = &efi_guid_image_type_uboot_raw; + break; + case 'i': + index = strtoul(optarg, NULL, 0); + break; + case 'I': + instance = strtoul(optarg, NULL, 0); + break; + case 'h': + print_usage(); + return 0; + } + } + + /* need a output file */ + if (argc != optind + 1) { + print_usage(); + return -1; + } + + /* need a fit image file or raw image file */ + if (!file) { + print_usage(); + return -1; + } + + if (create_fwbin(argv[optind], file, guid, index, instance) + < 0) { + printf("Creating firmware capsule failed\n"); + return -1; + } + + return 0; +}