From patchwork Mon Dec 20 05:02:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 526040 Delivered-To: patch@linaro.org Received: by 2002:a05:6e04:2287:0:0:0:0 with SMTP id bl7csp3767870imb; Sun, 19 Dec 2021 21:03:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJyNWjbl5bjjrZiyUzdZqT5rSt6kZqLuFr6t5P9UNBNgWZVzm/XgBTySVXfxvX3oHAP5odyo X-Received: by 2002:a05:6402:27d3:: with SMTP id c19mr14326015ede.390.1639976637944; Sun, 19 Dec 2021 21:03:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639976637; cv=none; d=google.com; s=arc-20160816; b=y154kNclBAo1ZVqGayv8oAsOjEXNMhtgeSztPP2OC5X49EZpAqgLoMCM5HSakiY20I BgJ0f3t9x31EzDvakK4HLn1ErjRwyGf+EQyz9Yfo+/0l8fiFllLf1Nk8pVxy+yudCbRe bcqKjuJtERHd/rmBBubEKiWde1ZAI+a+kZlcOY0aILlLcPlMVi3+jf/oX6wX5FGUjUhX yYDzl2uk+K2md/3+7RFin6Hne3vlMhiBf+ENEnXlw5d3nNC1470QdBTAS5yhw65WQ8RZ kpvsCWG6hlXa3Ua+WIQTCF+xkE9X5mjwJhRF0wgkCwk1HcLyTL6g+r6mnfJTKuJ3dX0V 1pcQ== 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=iFw8sEaVsjbOh3WtyITOeJPHF+YFN0n1B1UBG80ctW4=; b=JcFwoAmz9YMZHHxkD2Z2MToxnrb3Obwo0BFo3vgKuWDU8KzHvz5f3bnFLQTvCzSjHT RGLHc/IBWRV9zGWE8OrwsveHXUNiN+4+E9E3IDGcbETOBY81P2VKjsu1rJSPXIg/NENP VmEwWz1pLFTV4+CsM9m8wYrCuAbFvFAdMYmgGpft6fkLu3/FLUDuD1Fa7oSGLVzVtPww nMz7DeZTlJTrWge0dtc80I6IiG8WRZC/S7rMA1KxdEBFvXkqzcnuGLegQgLVcizigG7Z wEmS/uED7bLBlAKZKwigqBp+BTFf7kD4qkL11SBcTm5Y5JMNxL3myAvnN+b05DmsHOuL s6BQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HvYvTned; 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 j16si11146607edw.472.2021.12.19.21.03.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 21:03:57 -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=HvYvTned; 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 02EE4830CE; Mon, 20 Dec 2021 06:03:52 +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="HvYvTned"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0BC74830BE; Mon, 20 Dec 2021 06:03:49 +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-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) (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 7AD2E8309A for ; Mon, 20 Dec 2021 06:03:43 +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-x636.google.com with SMTP id n16so6382310plc.2 for ; Sun, 19 Dec 2021 21:03:43 -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=iFw8sEaVsjbOh3WtyITOeJPHF+YFN0n1B1UBG80ctW4=; b=HvYvTnedYimmbokrFCKNuRGhs/99AZZilD4SSFZvW8nMF4Hnu75NO2XHoL6n7NKtBG LJ8ZszHi1hJIt6f6M4GMNE8Q8YcRISQ+Exs++v14PLQ5ftjJu3gqMpmQvZAP4LWB1fzW kGE7BuhNIt4XgH+vZo8VZ2A6/qJbJoVfEY5Z2mlrGo3o+lkj4gNZSzKIOtE/4p7CmbrD 0Pj2//PRuyMkgievqEhiZbHTYLekxGkfO4gf/LiR+4RnCGij7QsXMUzgF1TRZynX/Agp +XuzTuI3vNodoefjWKiATY1WLR+as81yzFsbfDv1kiv6P+wY7VTZTwjz7g7BjjbJS8Wn 74sA== 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=iFw8sEaVsjbOh3WtyITOeJPHF+YFN0n1B1UBG80ctW4=; b=OnQYr/O+2tNOnFM6xhOOvCt/eP0VUzZHomeAsUdnv+aRkthuoKlGwxrz8S61YU1dIy Uhx02kskfAM2H6pBvSGruJrqa0hpv6LxpRqI4oW6X9mhcvMKrjgQyH4fMtU3/8MtY0/C X52Of5L2RQi2N8xZsnaSY0N19gQlLeRdRYjbB0BAT6P7IUNTlD9ZnvqstM5ZmJzFXheW 2ezrCY2GPDeUZkmLLwfbebBVON/mEuWrRUkPSIP/HkYT9k6sXvsYDwsLCw6wPYxOAHjf rPtNv4wdFTtzVzWthTqnOJ38zJxYxTzGfhU2fqXukaRb/MgEM/lhkElOkam7WKNivLsi OGdg== X-Gm-Message-State: AOAM533HjF4Y269koC7PDxxybJ6eY6WGTKoVZ8TqkmlUSMCUWsE2IUtQ XBBNPAktU7RM1Q6f24Lhw4m7nQ== X-Received: by 2002:a17:903:1c6:b0:148:a2e7:fb28 with SMTP id e6-20020a17090301c600b00148a2e7fb28mr14822048plh.105.1639976621789; Sun, 19 Dec 2021 21:03:41 -0800 (PST) Received: from localhost.localdomain ([2400:4050:c3e1:100:89bf:be4:468d:12c2]) by smtp.gmail.com with ESMTPSA id mu2sm4581163pjb.43.2021.12.19.21.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 21:03:41 -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 v8 01/12] tools: mkeficapsule: rework the code a little bit Date: Mon, 20 Dec 2021 14:02:42 +0900 Message-Id: <20211220050253.31163-2-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211220050253.31163-1-takahiro.akashi@linaro.org> References: <20211220050253.31163-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.38 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 | 223 ++++++++++++++++++++++++++++++------------- 1 file changed, 159 insertions(+), 64 deletions(-) diff --git a/tools/mkeficapsule.c b/tools/mkeficapsule.c index 4995ba4e0c2a..afdcaf7e7933 100644 --- a/tools/mkeficapsule.c +++ b/tools/mkeficapsule.c @@ -61,17 +61,122 @@ 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; + } + if (bin_stat.st_size > (u32)~0U) { + printf("file size is too large: %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 +184,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 +213,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)) { - 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(data); -err_1: - fclose(g); - return -1; + return ret; } /*