From patchwork Tue Jan 18 04:39:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 532854 Delivered-To: patch@linaro.org Received: by 2002:ad5:544f:0:0:0:0:0 with SMTP id a15csp3352229imp; Mon, 17 Jan 2022 20:40:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJxt3UcUNKNTES+w9m1ZZjoLWDL3EYUjRaTbZvG5QjWutf7hzlVzSsnLGl6aVjqdziAv8dt/ X-Received: by 2002:a17:906:4fc8:: with SMTP id i8mr19286372ejw.427.1642480839985; Mon, 17 Jan 2022 20:40:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642480839; cv=none; d=google.com; s=arc-20160816; b=jGz3tRKM52H4Tbj3RQOLuda3K6IVtngyQttmR4hYkIs1/xN1XcckU3fmCVmOtnILZb EmcrbNTgw039337GZrVcY5FQffGAhLu0OfZBCHMOTxB01/Bc/sbQil3pz010uFK1C0Cr 62TwBb86TenqeFnOtAgY0E6k4wMobgO28clvTKLj547MhEZPFUdBw9/aStmd4JzdGfNR yIIYKilx89I9TqxPxS2v/hNmtSxlw7RlAYcjIZHdyX9qFE13HypXbTxiREqzuAiGefS6 VtIEfPMdeY0x/rbdpb7GOLHzNaE8EHb5PN6J7pbGWJf0r3YVZUP2BVABpqVh4Cy9Op5j 7x3w== 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=V4fAE4zrb9Z9f8o8QiYYqycnxckuGQcEerLEyWlZXD4=; b=Ap/5L7pKyUAeJoGqufCDHhSzNCfo0hiehxIF+8dpxclqQqg5sngH0gnCQMnV/bNTSQ 0J6Vsc8siRqsGE6VKeeG+lf22W6k28NvqtIMzsPbQ6Ji0llx4uvOC9UWNu5l9+9VxFNh 8+BOqJphYhQw+HPoPCMoxz62nzSDq+ZYhWDBAJ0bOuWeF9i5Cuo5x1LBKFUQu4N5bAlR gVJ8aQsO9c0qVOk6VVHRS/y1DUTHHcoH0hVWo0lmYPmzGnGAqCx0D/NNMs0s3rlRCi2m 3o5HyKw2XQtILlQLGfwo9C5t9drs3z1NRCM97qyHW0S93srQO/BRCE3SV6PDLW2SXBUt Tk3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=vRwm+9RX; 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 y13si2364329ejq.763.2022.01.17.20.40.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jan 2022 20:40:39 -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=vRwm+9RX; 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 C2DF383180; Tue, 18 Jan 2022 05:40:32 +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="vRwm+9RX"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E8E6E830C0; Tue, 18 Jan 2022 05:40:29 +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-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) (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 A753583214 for ; Tue, 18 Jan 2022 05:40:24 +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-pl1-x62b.google.com with SMTP id f13so12998058plg.0 for ; Mon, 17 Jan 2022 20:40:24 -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=V4fAE4zrb9Z9f8o8QiYYqycnxckuGQcEerLEyWlZXD4=; b=vRwm+9RX11AnjCt+AOT3+PFKqyiybr7p0bzX+gzFS+chUEHtuz6DpjXh4bWTkzIs7B mKW1KTSDVFLYKJEn9h1Z+8Q1ZBqFOKr/xLtrhlM51Rq4oUNINzq4R2JAFY29q4rryH6y A3va329r7yKyqTo1841KB4Rb+ACZfUGWTZyc2RpMTlAV1ArjryJKb8iIElq9M5hje6BD yrKqoYZC0Y/BqJ8b1s26MwLG99Vk0E/ZyNrSMSm+L2uvU36f0fH6G1ki0eUY+V/yxce9 UY36mqzAFcpKSADSafDupNYe65pTmnXajL02QxpYyJqY4VGRUtGO5b/yam32/I9xyefE LDag== 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=V4fAE4zrb9Z9f8o8QiYYqycnxckuGQcEerLEyWlZXD4=; b=y6tNhaXcfIYf7PvAioxeQXF36FCndDfVQO8Zki2Ypwsb0ktdjrGZLWk2DE2/kNZb4p eZs4kXOSV/03BzLRFYfUMijB8NfcJTc/R5fqgu6dNfh+ObPrH5AfYi2EItyc3yG6WoX1 lxRXs/98Usx+OE9tLCaGUbpcig7aVkMwALZu+Jz7oIVZlEYpl4+oa+TtxGTZeH55wnXI AGw7a/nJdQCit01+b6y0oa5z2lMfLzP+ObYWJJtI+xw0geeFxerllFEDY+HOHonp3qQQ U7yZ8ehr0Hx+6KCFYNjsJ3SIk/ZYVbfrBLkt1RL2ekoWi8QCIsQ3b0Z/cwRwA2I8kmbd oi1A== X-Gm-Message-State: AOAM530iCNhCwwbGOnms2GZQlASA2vcefx3JNiZ6iEioiR62mBxjfSg9 uUzJpRivveAe9KE15Wlsc4ZDZA== X-Received: by 2002:a17:902:b944:b0:14a:b045:4d00 with SMTP id h4-20020a170902b94400b0014ab0454d00mr10581101pls.52.1642480821669; Mon, 17 Jan 2022 20:40:21 -0800 (PST) Received: from localhost.localdomain ([2400:4050:c3e1:100:bc1a:291e:ac91:be98]) by smtp.gmail.com with ESMTPSA id y69sm15670770pfg.171.2022.01.17.20.40.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jan 2022 20:40:21 -0800 (PST) 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, mark.kettenis@xs4all.nl, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH v9 02/11] tools: mkeficapsule: rework the code a little bit Date: Tue, 18 Jan 2022 13:39:45 +0900 Message-Id: <20220118043954.55940-3-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20220118043954.55940-1-takahiro.akashi@linaro.org> References: <20220118043954.55940-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 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.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 | 239 ++++++++++++++++++++++++++++++------------- 1 file changed, 167 insertions(+), 72 deletions(-) diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c index 19d5eea3cb59..ee3e489c0b30 100644 --- a/tools/mkeficapsule.c +++ b/tools/mkeficapsule.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -51,33 +52,36 @@ static struct option options[] = { static void print_usage(void) { fprintf(stderr, "Usage: %s [options] \n" - "Options:\n" - - "\t-f, --fit new FIT image file\n" - "\t-r, --raw new raw image file\n" - "\t-i, --index update image index\n" - "\t-I, --instance update hardware instance\n" - "\t-h, --help print a help message\n", - tool_name); + "Options:\n" + + "\t-f, --fit new FIT image file\n" + "\t-r, --raw new raw image file\n" + "\t-i, --index update image index\n" + "\t-I, --instance update hardware instance\n" + "\t-h, --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) +/** + * 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) { - struct efi_capsule_header header; - struct efi_firmware_management_capsule_header capsule; - struct efi_firmware_management_capsule_image_header image; - FILE *f, *g; + FILE *g; struct stat bin_stat; - u8 *data; + void *buf; size_t size; - u64 offset; - -#ifdef DEBUG - fprintf(stderr, "For output: %s\n", path); - fprintf(stderr, "\tbin: %s\n\ttype: %pUl\n", bin, guid); - fprintf(stderr, "\tindex: %ld\n\tinstance: %ld\n", index, instance); -#endif + int ret = 0; g = fopen(bin, "r"); if (!g) { @@ -86,19 +90,123 @@ static int create_fwbin(char *path, char *bin, efi_guid_t *guid, } if (stat(bin, &bin_stat) < 0) { fprintf(stderr, "cannot determine the size of %s\n", bin); - goto err_1; + ret = -1; + goto err; + } + if (bin_stat.st_size > SIZE_MAX) { + fprintf(stderr, "file size is too large for malloc: %s\n", bin); + ret = -1; + goto err; } - data = malloc(bin_stat.st_size); - if (!data) { + buf = malloc(bin_stat.st_size); + if (!buf) { fprintf(stderr, "cannot allocate memory: %zx\n", (size_t)bin_stat.st_size); - goto err_1; + ret = -1; + goto err; + } + + size = fread(buf, 1, bin_stat.st_size, g); + if (size < bin_stat.st_size) { + fprintf(stderr, "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) { + fprintf(stderr, "%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; + void *data; + off_t bin_size; + u64 offset; + int ret; + +#ifdef DEBUG + fprintf(stderr, "For output: %s\n", path); + fprintf(stderr, "\tbin: %s\n\ttype: %pUl\n", bin, guid); + fprintf(stderr, "\tindex: %ld\n\tinstance: %ld\n", index, instance); +#endif + + 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) { fprintf(stderr, "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 */ @@ -106,70 +214,57 @@ 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)) { - fprintf(stderr, "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))) { - fprintf(stderr, "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)) { - fprintf(stderr, "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)) { - fprintf(stderr, "write failed (%zx)\n", size); - goto err_3; - } - size = fread(data, 1, bin_stat.st_size, g); - if (size < bin_stat.st_size) { - fprintf(stderr, "read failed (%zx)\n", size); - goto err_3; - } - size = fwrite(data, 1, bin_stat.st_size, f); - if (size < bin_stat.st_size) { - fprintf(stderr, "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(data); -err_1: - fclose(g); - return -1; + return ret; } /*