From patchwork Wed Mar 1 09:15:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 657799 Delivered-To: patch@linaro.org Received: by 2002:adf:a3c6:0:0:0:0:0 with SMTP id m6csp3386832wrb; Wed, 1 Mar 2023 01:17:22 -0800 (PST) X-Google-Smtp-Source: AK7set+nMW4CJeKmWpb734E+EljSsp+WUJrI2yxDhu5ftUAhrjduYc/4sQGj/XBJRwTJhB5cdqxC X-Received: by 2002:a05:6870:831c:b0:16d:e12c:fa70 with SMTP id p28-20020a056870831c00b0016de12cfa70mr2727210oae.50.1677662241940; Wed, 01 Mar 2023 01:17:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677662241; cv=none; d=google.com; s=arc-20160816; b=saZlAmcqsYPlBdQfC/TvbQ6nRiJsxU+hTN58wkvachRkFfI/1jhrXR/31BowE9hWIw iPOjbigqfwvOr9QDa/PBJBx8e2MIxRVomYU77IG6tQ8mSZDVljRYdvvdp3DsDAkfnCdJ +YGJ63/3NSo443+deBkb3gciHaBsBSgBxdl8qXDpSnDSe2iLZsF7SHILZpcOYbtbGUrb zWhBn+suOCsdtt+lukl7x0WTfRQ9nZSnDNElWt9z2mBC0kPf9kZ8Tm7iH4QX6hMR1ojl xCMoCfYaBjiX5p8leCzySQWmcV2Yd/oahmK1xRfgc5NpMNUffVJ5RPKS3QAHA/upp5Ad G4kA== 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:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=7P/k4EFngYonz1GILTT5eHwxf0T5BpHfQ/KNBg2Thp0=; b=xpQmG7AFkLW8EUkIXoC9m1R2nV4rvoqAJbNUPMSAVAutX9fOGLFpcyU3CV7eaAxjvA 0/cDchSsVO7NzYbUj5nMsgLBHPKRo8+0XQCuo1EWYETONlNmE14qAqsPsLt2sC5JIuql 0W8MPYvCI+0pIOTk8KSYb7c7AI88VFJ0JIF3CuoPIo9iY97uV46LGIXNW3vMTCigFrcR lFpCVU8wTTCezeeD/f3gehG1Hc0odIrQMuVuS/AyXLZxEetQcAOM8zH08gwqET0haH+z Xfam5pdYosGkSHLZSzPRHEMjsRvH6Z/RpFkHp4FO1hYGZgBN+L3Gac1esmojmCBH+fem Xkqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XPMADcIY; 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 ec21-20020a0568708c1500b001729856967fsi11990919oab.176.2023.03.01.01.17.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Mar 2023 01:17:21 -0800 (PST) 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=XPMADcIY; 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 B0745857B3; Wed, 1 Mar 2023 10:16:35 +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="XPMADcIY"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3EA0F85C1A; Wed, 1 Mar 2023 10:16:27 +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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) (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 A80A585C06 for ; Wed, 1 Mar 2023 10:16:13 +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=masahisa.kojima@linaro.org Received: by mail-pl1-x62d.google.com with SMTP id h8so10009138plf.10 for ; Wed, 01 Mar 2023 01:16:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1677662162; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=7P/k4EFngYonz1GILTT5eHwxf0T5BpHfQ/KNBg2Thp0=; b=XPMADcIYDUQ7R5e3FcmrlW4aZ6amzOurgQmw3sFSCAb+HAlwRR0Z10Nyv3gXkn6G1G P7OE9HWKxQQ0RiAI3UWztOBWC4nRKTrX9SQcKMXe9ckMQFJ7O89KOAqjTGL52ULV9y2P PnW+Y2OKUA08XnJpnSLDv+MxeJvWFpDmcBVJ7RqVRQoj/E6WLs4ghZgu5/LDFxWKBJtJ oKubSPgkAM3dgSHuF2OmFxOn/oaq2WUdpSXWyw1iEIl0/kdhEXBu9wABE9PSU/tnzyBT txMu9m6LIUxZkifprDCynatTyurdf22PZdS5w+2afJa6xDaXex2m59JhrnwXYJi/ps9D cztw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677662162; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7P/k4EFngYonz1GILTT5eHwxf0T5BpHfQ/KNBg2Thp0=; b=tC9B3p07kSfrOz4Q2eLNodxIGh0d637hvAlHjr1vB3hojU3Yr8+Y92grDbIzmu7POQ +ir9XFE7BppK8fLnM1fOC71HICLGl98iclckq1QKHLOQZN4etXDtR8+6htjcu6efuckx cb11zixUDn+OyC6Ri+K+LYO+feMnmvzgK4chOkFVEyIzIq23zE+LbQRZCMsode97/fpJ 9yu/8fx4QhDr2Z1ohb3PDUjtnUQadX0qgJh/dOkX7PJe08lHhxmhWqzOYFyHgTq8vB4+ RSb/Wq+vIKUHHiW3Yj88gk5mE02GCuWS9hLCHhWzavYKUhXufms3kKeWtooUUoQoqqNQ 0/yA== X-Gm-Message-State: AO0yUKXo49wslVatcc0IKWTMxssWAYrXiNQqBojuEqukCQjwhn9eQ+SF f5lH7c2MFOBOk6hpNcPmIQa2ImeY8hj3peh7 X-Received: by 2002:a05:6a20:8f1f:b0:cd:f73a:55c with SMTP id b31-20020a056a208f1f00b000cdf73a055cmr4635542pzk.13.1677662162426; Wed, 01 Mar 2023 01:16:02 -0800 (PST) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id 13-20020aa7910d000000b005a8173829d5sm7466637pfh.66.2023.03.01.01.16.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Mar 2023 01:16:01 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Takahiro Akashi , Masahisa Kojima Subject: [PATCH v2 4/4] mkeficapsule: add FMP Payload Header Date: Wed, 1 Mar 2023 18:15:22 +0900 Message-Id: <20230301091523.18384-5-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230301091523.18384-1-masahisa.kojima@linaro.org> References: <20230301091523.18384-1-masahisa.kojima@linaro.org> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.6 at phobos.denx.de X-Virus-Status: Clean Current mkeficapsule tool does not provide firmware version management. EDK2 reference implementation inserts the FMP Payload Header right before the payload. It coutains the fw_version and lowest supported version. This commit adds three new parameters required to generate the FMP Payload Header for mkeficapsule tool. '-f' indicates whether FMP Payload Header is inserted. '-v' indicates the firmware version. '-l' indicates the lowest supported version. When mkeficapsule tool is invoked without '-f' option, FMP Payload Header is not inserted, the behavior is same as current implementation. Signed-off-by: Masahisa Kojima --- Newly created in v2 tools/mkeficapsule.c | 81 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c index b71537beee..e0a6948df8 100644 --- a/tools/mkeficapsule.c +++ b/tools/mkeficapsule.c @@ -29,7 +29,7 @@ static const char *tool_name = "mkeficapsule"; efi_guid_t efi_guid_fm_capsule = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID; efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID; -static const char *opts_short = "g:i:I:v:p:c:m:o:dhAR"; +static const char *opts_short = "g:i:I:v:l:p:c:m:o:dfhAR"; enum { CAPSULE_NORMAL_BLOB = 0, @@ -41,6 +41,9 @@ static struct option options[] = { {"guid", required_argument, NULL, 'g'}, {"index", required_argument, NULL, 'i'}, {"instance", required_argument, NULL, 'I'}, + {"fmp-payload-header", no_argument, NULL, 'f'}, + {"fw-version", required_argument, NULL, 'v'}, + {"lsv", required_argument, NULL, 'l'}, {"private-key", required_argument, NULL, 'p'}, {"certificate", required_argument, NULL, 'c'}, {"monotonic-count", required_argument, NULL, 'm'}, @@ -60,6 +63,9 @@ static void print_usage(void) "\t-g, --guid guid for image blob type\n" "\t-i, --index update image index\n" "\t-I, --instance update hardware instance\n" + "\t-f, --fmp-payload-header insert fmp payload header\n" + "\t-v, --fw-version firmware version\n" + "\t-l, --lsv lowest supported version\n" "\t-p, --private-key private key file\n" "\t-c, --certificate signer's certificate file\n" "\t-m, --monotonic-count monotonic count\n" @@ -71,6 +77,30 @@ static void print_usage(void) tool_name); } +#define SIGNATURE_16(A, B) ((A) | ((B) << 8)) +#define SIGNATURE_32(A, B, C, D) \ + (SIGNATURE_16(A, B) | (SIGNATURE_16(C, D) << 16)) + +#define FMP_PAYLOAD_HDR_SIGNATURE SIGNATURE_32('M', 'S', 'S', '1') + +/** + * struct fmp_payload_header - EDK2 header for the FMP payload + * + * This structure describes the header which is preprended to the + * FMP payload by the edk2 capsule generation scripts. + * + * @signature: Header signature used to identify the header + * @header_size: Size of the structure + * @fw_version: Firmware versions used + * @lowest_supported_version: Lowest supported version + */ +struct fmp_payload_header { + uint32_t signature; + uint32_t header_size; + uint32_t fw_version; + uint32_t lowest_supported_version; +}; + /** * auth_context - authentication context * @key_file: Path to a private key file @@ -95,6 +125,12 @@ struct auth_context { size_t sig_size; }; +struct fmp_payload_header_params { + bool have_header; + uint32_t fw_version; + uint32_t lowest_supported_version; +}; + static int dump_sig; /** @@ -402,6 +438,7 @@ static void free_sig_data(struct auth_context *ctx) */ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, unsigned long index, unsigned long instance, + struct fmp_payload_header_params *fmp_ph_params, uint64_t mcount, char *privkey_file, char *cert_file, uint16_t oemflags) { @@ -410,10 +447,11 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, struct efi_firmware_management_capsule_image_header image; struct auth_context auth_context; FILE *f; - uint8_t *data; + uint8_t *data, *new_data, *buf; off_t bin_size; uint64_t offset; int ret; + struct fmp_payload_header payload_header; #ifdef DEBUG fprintf(stderr, "For output: %s\n", path); @@ -423,6 +461,7 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, auth_context.sig_size = 0; f = NULL; data = NULL; + new_data = NULL; ret = -1; /* @@ -431,12 +470,31 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, if (read_bin_file(bin, &data, &bin_size)) goto err; + buf = data; + + /* insert fmp payload header right before the payload */ + if (fmp_ph_params->have_header) { + new_data = malloc(bin_size + sizeof(payload_header)); + if (!new_data) + goto err; + + payload_header.signature = FMP_PAYLOAD_HDR_SIGNATURE; + payload_header.header_size = sizeof(payload_header); + payload_header.fw_version = fmp_ph_params->fw_version; + payload_header.lowest_supported_version = + fmp_ph_params->lowest_supported_version; + memcpy(new_data, &payload_header, sizeof(payload_header)); + memcpy(new_data + sizeof(payload_header), data, bin_size); + buf = new_data; + bin_size += sizeof(payload_header); + } + /* 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_data = buf; auth_context.image_size = bin_size; if (create_auth_data(&auth_context)) { @@ -536,7 +594,7 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, /* * firmware binary */ - if (write_capsule_file(f, data, bin_size, "Firmware binary")) + if (write_capsule_file(f, buf, bin_size, "Firmware binary")) goto err; ret = 0; @@ -545,6 +603,7 @@ err: fclose(f); free_sig_data(&auth_context); free(data); + free(new_data); return ret; } @@ -644,6 +703,7 @@ int main(int argc, char **argv) unsigned long oemflags; char *privkey_file, *cert_file; int c, idx; + struct fmp_payload_header_params fmp_ph_params = { 0 }; guid = NULL; index = 0; @@ -679,6 +739,15 @@ int main(int argc, char **argv) case 'I': instance = strtoul(optarg, NULL, 0); break; + case 'f': + fmp_ph_params.have_header = true; + break; + case 'v': + fmp_ph_params.fw_version = strtoul(optarg, NULL, 0); + break; + case 'l': + fmp_ph_params.lowest_supported_version = strtoul(optarg, NULL, 0); + break; case 'p': if (privkey_file) { fprintf(stderr, @@ -747,11 +816,11 @@ int main(int argc, char **argv) if (capsule_type != CAPSULE_NORMAL_BLOB) { if (create_empty_capsule(argv[argc - 1], guid, capsule_type == CAPSULE_ACCEPT) < 0) { - fprintf(stderr, "Creating empty capsule failed\n"); + fprintf(stderr, "Creating empty capsulefailed\n"); exit(EXIT_FAILURE); } } else if (create_fwbin(argv[argc - 1], argv[argc - 2], guid, - index, instance, mcount, privkey_file, + index, instance, &fmp_ph_params, mcount, privkey_file, cert_file, (uint16_t)oemflags) < 0) { fprintf(stderr, "Creating firmware capsule failed\n"); exit(EXIT_FAILURE);