From patchwork Wed Jul 22 06:05:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 246961 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp254924ilg; Tue, 21 Jul 2020 23:09:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw9Cxzw4Sz6lv3KeZzcGqb0+IWDKHPF2YJ1N8qu9Zq5Jkmzcgo43PUxOxvU+9Tqs8EwoUMi X-Received: by 2002:a17:906:fa9b:: with SMTP id lt27mr26922276ejb.365.1595398142631; Tue, 21 Jul 2020 23:09:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595398142; cv=none; d=google.com; s=arc-20160816; b=lMTfU/us8c03z4dA269pCbmFTmkNHX2kJYHr3obhYcjsdG1AHc7QSjNeTC4aai2usP cIprg6mWzba7gdF/V1YZTWbGZNIwTyfMP/kD7OU9AiD0UH9ZJBeH8XKQRYQsOYNx4/e9 OjwGgaiYlc5+rn30+87fweLlieRC+uc+ZXW2rbYHPXuTKI97s7SpliowhFnDx6+L+p93 Saa/vFqt/X3PoIB5/nkTo0Y8cWxOLfIGklen+Yq0NtqXVLgHFvyKiDQ7lNZY23CKoi26 +TgGXwqdMh69aLjRclftVZlnNMnFOr22gWL2Eqh7kWJmh8ermf4qHquO3BVsHXs4lVJQ 027A== 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=eUbHT1GmvzFEO9oQKllw3m5nisdjAJSVduoe65Ixh3Q=; b=U1Ryexzb1+2rVgbre1H09i5b5vUMXvXYuiKXkJQXkQ5glNJXbIS3NYR+bT0XWgdiKl AfNis3SG74A+GsEPQXPd02aPb9RXoJRXj9+Jfqi9bP529xXlJUM4Qpn/B+dqmwk0W4Nu reTUFYL6YCSBlUV7rSwDRV0OKTLmVdSyiNG2uKHQUOmJK7jMVx+1pqbQQIvqJGi4GLcD L80JqeQ0Z1xLOJUxWSS3WcT8NJk3eETQGDGZnOt6qOO+n9ueGPgyfV7KM9Gwmw+BmsH6 nb9HjzD0BnL8Oktb+x+60zapHjSngPsxVpCyDe8fmo7XhwkZVPjlE74mHXDINdudanWm lRDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=S8K5N0U6; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id 94si14176327eda.384.2020.07.21.23.09.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2020 23:09:02 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=S8K5N0U6; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 6636781EE3; Wed, 22 Jul 2020 08:07:23 +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="S8K5N0U6"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 634BF82170; Wed, 22 Jul 2020 08:07:04 +0200 (CEST) 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,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) (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 B0EF882159 for ; Wed, 22 Jul 2020 08:06:58 +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=takahiro.akashi@linaro.org Received: by mail-pj1-x1032.google.com with SMTP id md7so611345pjb.1 for ; Tue, 21 Jul 2020 23:06:58 -0700 (PDT) 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=eUbHT1GmvzFEO9oQKllw3m5nisdjAJSVduoe65Ixh3Q=; b=S8K5N0U6Idc8E5w4SQ7T/wNl5QP+0F5pAv9N+TIa83OqX98iNOjjbO/OR4EfWw25C8 cWHqCvzY8iUzd2wiaEtGyjGeB+BaQhRIQAfUo+WMJgnBBj7jGtR1FV0CHylwkwWvHyR1 zXiy9YMR56x5+qsvUc1YwlG5AwN44pU5+9kCjrznRhiQKOqT/6oyF0sPV2+uB9s30+WF Ybh/ZkHqHMymgZle3Xl203w9U0vykKmfr79eXj6P/sUiI5gC/Uvz49QNDGYlYlnk277q TDWRxxqPVMaCFx4CkDClZL5w4Hgs3Q7HQ/5qg3PkpMJsdOq/0q+wAwtSm5Jy+KW66qiz iqUg== 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=eUbHT1GmvzFEO9oQKllw3m5nisdjAJSVduoe65Ixh3Q=; b=aXoPrXYmLtiO43KTKh3IAoXsvHQ7jwzofFQ17WvI+KsMup0/2Vsx9Ez9i5TLRlqOdb PaawQGIOMh6WmUoGqJZFs1k34M0jSKCt/NysAPthWwCTL3NbTvkXwjR3ZKPEREGZ8FbV u6TBXKl6Nnit0zaWJcxgfni3h+kAGx/oRVmREfBQ7vtLRsObF+OlLBrlnpCKDymPM4UA yF2Q48FVoqZGhgAr/fiwqbpZtBSdc8V1cW5LIFNyYHUJaHLVoe9owOQIRU8ESOZnneH6 QCJB9ioBUETvn6kziXQ7IfbKButxLKK/8KpNJ8jJQ/ISroTvi+M7CP9DZaORJUE5b/Ru n3DQ== X-Gm-Message-State: AOAM531hA5edYkxAXtN7pjBOTOZb1+fKSCAhKE0iYRt2kB28bt03U8vZ Z5TGqlabXVZZ4B7yjEv2xcx8xQ== X-Received: by 2002:a17:90a:c90c:: with SMTP id v12mr7885683pjt.112.1595398017180; Tue, 21 Jul 2020 23:06:57 -0700 (PDT) Received: from localhost.localdomain (p6e424d9a.tkyea130.ap.so-net.ne.jp. [110.66.77.154]) by smtp.gmail.com with ESMTPSA id p10sm19578759pgn.6.2020.07.21.23.06.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jul 2020 23:06:56 -0700 (PDT) From: AKASHI Takahiro To: lukma@denx.de, trini@konsulko.com, xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH v4 14/16] tools: add mkeficapsule command for UEFI capsule update Date: Wed, 22 Jul 2020 15:05:37 +0900 Message-Id: <20200722060539.15168-15-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200722060539.15168-1-takahiro.akashi@linaro.org> References: <20200722060539.15168-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 | 3 + tools/mkeficapsule.c | 237 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 tools/mkeficapsule.c -- 2.27.0 diff --git a/tools/Makefile b/tools/Makefile index 51123fd92983..153235612997 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -218,6 +218,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 +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..93f22192fdb4 --- /dev/null +++ b/tools/mkeficapsule.c @@ -0,0 +1,237 @@ +// 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'}, + {"version", required_argument, NULL, 'v'}, + {"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--version firmware version\n" + "\t--help print a help message\n", + tool_name); +} + +static int create_fwbin(char *path, char *bin, efi_guid_t *guid, + unsigned long version, 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; + +#ifdef DEBUG + printf("For output: %s\n", path); + printf("\tbin: %s\n\ttype: %pUl\n" bin, guid); + printf("\tversion: %ld\n\tindex: %ld\n\tinstance: %ld\n", + version, 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; + 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 = version; + 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; + + 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, version; + int c, idx; + + file = NULL; + guid = NULL; + index = 0; + instance = 0; + version = 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 'v': + version = 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, version, index, instance) + < 0) { + printf("Creating firmware capsule failed\n"); + return -1; + } + + return 0; +}