From patchwork Tue Nov 2 00:55:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 516599 Delivered-To: patch@linaro.org Received: by 2002:ad5:5208:0:0:0:0:0 with SMTP id p8csp3916001iml; Mon, 1 Nov 2021 17:56:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwqFrVO6UsodRR86eyQWZsawfoSSrBCZtMgpH6H3qhdoC/+W9b1rliHC8YCwIJAVhaRgBx X-Received: by 2002:a05:6402:128a:: with SMTP id w10mr37652598edv.272.1635814578351; Mon, 01 Nov 2021 17:56:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635814578; cv=none; d=google.com; s=arc-20160816; b=XFXM3tPO3k8IFRAOYshUQlOd7eK1Y5zM37qBNHpmSf+3Qeuf8K3/6iKPrQIv2TzclY ztKoLu1v5bPCyBARNCGKb/F6mqaLVsqFgufeezyvOF4xppNp4YlmUKTjkhxKbQ8OMt24 oWFbfGGVq5GVCIgb9y1rju7Ffc0iaA6TFo8PYwnSf7VGhWFMRbLtzKQtsR/HMSMBPX0U zsCEWUIaCRfCIqZYj35qX7rCBmDGZlypSdRqS1HqVPdi9obCaSl68cit8jUnLhaiK9fJ yyiY68s2YopPPnsCam4N8USKyRZchrDakkXlThjPWHCDYMGSvi3DOQOiwB7qdOOB0yDD 1E9w== 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=lLny3Rk5NjLcv3moqx0kt8GiEcNg84mKBUDy5LzFZBA=; b=jnonrs0Urfip4o48HhBY1wcmQCVDDA6dPdt7QlCNBEM3jrhApec3oWZVPfhTY4FbYL kch/UqU12yfqYYBXXVyckvH4cRkywhl+gyFFC1MQa9flvx8veXWj5i9Nx2jbnM973URO GgBScfbf7tBRfxd75YDnQ2fwDSWwAmUBFJaJPZ8DEuBoeMwLMPqQwfq4NPx6AcVUzL+S kFxPihFjevwrienVjVCcfhBD9RyXfWPVbxGT7ua/Dzd8wsoRtJWFiLwmXjNpnuFcA33d I3iM1ntoNBKi7gp1kJ6mcaiVzNcEj2H1b5PRuh9/r5YPnvAnV5LDQTaeS1tZ7aUA+WrT xGSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=teglUdDE; 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 l11si2501584ejo.694.2021.11.01.17.56.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Nov 2021 17:56:18 -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=teglUdDE; 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 C051B83602; Tue, 2 Nov 2021 01:56: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=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="teglUdDE"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D7AF283383; Tue, 2 Nov 2021 01:56:04 +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-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) (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 52FD68354D for ; Tue, 2 Nov 2021 01:55:53 +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-x432.google.com with SMTP id y4so3578717pfa.5 for ; Mon, 01 Nov 2021 17:55:53 -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=lLny3Rk5NjLcv3moqx0kt8GiEcNg84mKBUDy5LzFZBA=; b=teglUdDElmg6O7ZdWaA50XQf5/VVYIgwbsi9Fsd1klnwkP/OkodfUdahCRchq/LzTd TuDUHM1WWN+JBGL5FeZUglZ+r2Tmam6sVBjy9l3GQP1Dsx13HIcoQnG5cHM34XCMoEiJ mNOloVMRj5MoPJ3ooP4SzOhzpeWpyp29ijFuTeMwW/Ofk7PuakY7wUI5OZYCCH+8w7kV oMy2RL3d6GHv95cht3OKFFbkGKiI4gl04vs0wYLrgvNaRNkaNcOgdUfCQJ+OM4HkPg0Q bRu0vPY9+/tonULQCEeeQIcVBWxZDPn04phM+yhZwFNABDvWT4xSM33sgfN08eUzkciQ fv5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lLny3Rk5NjLcv3moqx0kt8GiEcNg84mKBUDy5LzFZBA=; b=utFn+M3rD/+uRnNQLnA6wPIkOoN8aiiSVQk3fnJ6syRO7dt445QCGEdiCWSl92uhPv GpL6hIsTHQo5yZEVG1Rc5e6obS1/NTH7f2uiuHLW4EoCpW/lOMz1rFUWu+wmzQLLAmXd EKNUHhfJdQX/wc9cLQkyNw/qMVHEXgiv0n/Tq33e4kD6JMGpLCWFv2ALCTFiiFxo+udP 76mhnqh0a62FoHAm0mLyUCkibWH92rKgrlyf1BtMMa3TDx5/Ew3+1RpNOjYg2CTeUHYi b8K98Gb8m4HipGTwthhJIZb6zrS5RtUkbdg0HPZpCJy+7qFsQZrqlufGw3PBmOTSYzbN 2l5Q== X-Gm-Message-State: AOAM533hx/F60L9F43FxJ0CVQfV6z3/pgbJ1ggIRjmxpLdT1CXV2uH/v NBzm6GRWIE6ZXY+iSKZCxelP3A== X-Received: by 2002:a62:1a17:0:b0:480:f9fc:6a8c with SMTP id a23-20020a621a17000000b00480f9fc6a8cmr15605633pfa.22.1635814551497; Mon, 01 Nov 2021 17:55:51 -0700 (PDT) Received: from localhost.localdomain ([2400:4050:c3e1:100:a475:65cc:d4b7:aaf5]) by smtp.gmail.com with ESMTPSA id n29sm12305596pfv.29.2021.11.01.17.55.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Nov 2021 17:55:51 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de, sjg@chromium.org Cc: ilias.apalodimas@linaro.org, sughosh.ganu@linaro.org, masami.hiramatsu@linaro.org, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH v6 02/12] tools: mkeficapsule: rework the code a little bit Date: Tue, 2 Nov 2021 09:55:02 +0900 Message-Id: <20211102005512.96019-3-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211102005512.96019-1-takahiro.akashi@linaro.org> References: <20211102005512.96019-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.103.2 at phobos.denx.de X-Virus-Status: Clean Abstract common routines to make the code easily understandable. No functional change. Signed-off-by: AKASHI Takahiro Reviewed-by: Simon Glass --- tools/mkeficapsule.c | 219 ++++++++++++++++++++++++++++++------------- 1 file changed, 155 insertions(+), 64 deletions(-) -- 2.33.0 diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c index 4995ba4e0c2a..8427fedd941c 100644 --- a/tools/mkeficapsule.c +++ b/tools/mkeficapsule.c @@ -61,17 +61,117 @@ static void print_usage(void) tool_name); } +/** + * read_bin_file - read a firmware binary file + * @bin: Path to a firmware binary file + * @data: Pointer to pointer of allocated buffer + * @bin_size: Size of allocated buffer + * + * Read out a content of binary, @bin, into @data. + * A caller should free @data. + * + * Return: + * * 0 - on success + * * -1 - on failure + */ +static int read_bin_file(char *bin, void **data, off_t *bin_size) +{ + FILE *g; + struct stat bin_stat; + void *buf; + size_t size; + int ret = 0; + + 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); + ret = -1; + goto err; + } + buf = malloc(bin_stat.st_size); + if (!buf) { + printf("cannot allocate memory: %zx\n", + (size_t)bin_stat.st_size); + ret = -1; + goto err; + } + + size = fread(buf, 1, bin_stat.st_size, g); + if (size < bin_stat.st_size) { + printf("read failed (%zx)\n", size); + ret = -1; + goto err; + } + + *data = buf; + *bin_size = bin_stat.st_size; +err: + fclose(g); + + return ret; +} + +/** + * write_capsule_file - write a capsule file + * @bin: FILE stream + * @data: Pointer to data + * @bin_size: Size of data + * + * Write out data, @data, with the size @bin_size. + * + * Return: + * * 0 - on success + * * -1 - on failure + */ +static int write_capsule_file(FILE *f, void *data, size_t size, const char *msg) +{ + size_t size_written; + + size_written = fwrite(data, 1, size, f); + if (size_written < size) { + printf("%s: write failed (%zx != %zx)\n", msg, + size_written, size); + return -1; + } + + return 0; +} + +/** + * 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 file. + * 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) { 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; + FILE *f; + void *data; + off_t bin_size; u64 offset; + int ret; #ifdef DEBUG printf("For output: %s\n", path); @@ -79,25 +179,28 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *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: %zx\n", (size_t)bin_stat.st_size); - goto err_1; - } + f = NULL; + data = NULL; + ret = -1; + + /* + * read a firmware binary + */ + if (read_bin_file(bin, &data, &bin_size)) + goto err; + + /* + * write a capsule file + */ f = fopen(path, "w"); if (!f) { printf("cannot open %s\n", path); - goto err_2; + goto err; } + + /* + * capsule file header + */ header.capsule_guid = efi_guid_fm_capsule; header.header_size = sizeof(header); /* TODO: The current implementation ignores flags */ @@ -105,70 +208,58 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, 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 (%zx)\n", size); - goto err_3; - } + + bin_size; + if (write_capsule_file(f, &header, sizeof(header), + "Capsule header")) + goto err; + /* + * firmware capsule header + * This capsule has only one firmware capsule image. + */ 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 (%zx)\n", size); - goto err_3; - } + if (write_capsule_file(f, &capsule, sizeof(capsule), + "Firmware capsule header")) + goto err; + offset = sizeof(capsule) + sizeof(u64); - size = fwrite(&offset, 1, sizeof(offset), f); - if (size < sizeof(offset)) { - printf("write failed (%zx)\n", size); - goto err_3; - } + if (write_capsule_file(f, &offset, sizeof(offset), + "Offset to capsule image")) + goto err; + /* + * firmware capsule image header + */ image.version = 0x00000003; memcpy(&image.update_image_type_id, guid, sizeof(*guid)); image.update_image_index = index; image.reserved[0] = 0; image.reserved[1] = 0; image.reserved[2] = 0; - image.update_image_size = bin_stat.st_size; + image.update_image_size = bin_size; image.update_vendor_code_size = 0; /* none */ image.update_hardware_instance = instance; image.image_capsule_support = 0; + if (write_capsule_file(f, &image, sizeof(image), + "Firmware capsule image header")) + goto err; - size = fwrite(&image, 1, sizeof(image), f); - if (size < sizeof(image)) { - printf("write failed (%zx)\n", size); - goto err_3; - } - size = fread(data, 1, bin_stat.st_size, g); - if (size < bin_stat.st_size) { - printf("read failed (%zx)\n", size); - goto err_3; - } - size = fwrite(data, 1, bin_stat.st_size, f); - if (size < bin_stat.st_size) { - printf("write failed (%zx)\n", size); - goto err_3; - } - - fclose(f); - fclose(g); - free(data); - - return 0; + /* + * firmware binary + */ + if (write_capsule_file(f, data, bin_size, "Firmware binary")) + goto err; -err_3: - fclose(f); -err_2: + ret = 0; +err: + if (f) + fclose(f); + free_sig_data(&auth_context); free(data); -err_1: - fclose(g); - return -1; + return ret; } /*