From patchwork Mon Mar 30 03:56:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kever Yang X-Patchwork-Id: 1263718 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rock-chips.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=fIvVT/FJ; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48rJZs1DK7z9sPF for ; Mon, 30 Mar 2020 14:57:56 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2A9C681901; Mon, 30 Mar 2020 05:57:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=rock-chips.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="fIvVT/FJ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E4BAD818FC; Mon, 30 Mar 2020 05:56:58 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) (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 6E8A7818BA for ; Mon, 30 Mar 2020 05:56:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=rock-chips.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kever.yang@gmail.com Received: by mail-pf1-x442.google.com with SMTP id f206so7940881pfa.10 for ; Sun, 29 Mar 2020 20:56:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=afVtwvOWOCI/bT2OmhO7y2WAfKHMlRAcKv1cwhHJmsw=; b=fIvVT/FJbxl6Td4yoVwmvywevLrtlROUhBBnrO+3wmatGEr+cEf955QY1xmJsT3RRO T44iICAzjKhiL4kC8QDOqqv0GQOINhydxqBztkbYbEUEck7/hpNqLJGJp/bc7RrNeww9 dalP6mT5PbGZZJll0/Bs91LsttfMKQ0iQNQ/bx8XKxqvNFH+sAS0rTxxQTX4N/xIIViE pAK2z141u/G+yVl+aXOOggtRJNdz2H79wOlcb06EoAJqofuckkx5uH1weCrYL2LCJFQ0 cjbI63VKOezQO7zIx5nNvlf8PS83TvBZkfpdsScq4MROcgobdTI370qKLdWCO95mOtgw KTvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=afVtwvOWOCI/bT2OmhO7y2WAfKHMlRAcKv1cwhHJmsw=; b=LM0jrse/b+Aq0Tj7UifRJvZ+6qtV3Ovq3OBpD98owoT7cyh9sRNGMOwTYpPPdfHP49 0RyZlDIc9eo4RVvc/0NVgEErncsBZU35n0G+FkztL3L01L9T3H3cHje/lojolUuyzIsa vpBzse+f3+LtszBR7X7ZcWTvEx+E1QqLJemzpsEnIQfFECnmOPkH1gcU0X+/DVh/vzUp NVcfw+0mXaltlMDn/3uWBZYfsMzJQ2/gCEIJB/hESthHI52krWhqii/89y5EZCYtctpP C4KYveS8Y8aoMDvG6Dkava2YPN3DA0UZFZcNH1OoWdz+OqpLl3Ue+KByg6sCHcgl5ces oEiw== X-Gm-Message-State: ANhLgQ0Ke4Tz14THVRp4cBCtxiTe6pXV71q6tzJbIci4mwuAYnKMQCFO ZUg4aNqUQotf9NaKGFBAQJYauUS2 X-Google-Smtp-Source: ADFU+vs7kZKW7LRR7Fd5lc9yw1KNnvCHB5n8qMH/eAyqQli7xPK6syYS/5I9srafy2S08jG5gXc9JA== X-Received: by 2002:a63:d84f:: with SMTP id k15mr10606051pgj.137.1585540612753; Sun, 29 Mar 2020 20:56:52 -0700 (PDT) Received: from localhost.localdomain ([103.29.142.67]) by smtp.gmail.com with ESMTPSA id n100sm8902514pjc.38.2020.03.29.20.56.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2020 20:56:52 -0700 (PDT) From: Kever Yang To: u-boot@lists.denx.de Cc: chenjh@rock-chips.com, rasmus.villemoes@prevas.dk, trini@konsulko.com, xypron.glpk@gmx.de, punit1.agrawal@toshiba.co.jp, Kever Yang , "Andrew F. Davis" , Jeffy Chen , Jordan Hand , Julius Werner , Luca Boccassi , Michal Sojka , Philippe Reynes , Simon Glass , Thomas Hebb , =?utf-8?b?VmVzYSBKw6TDpHNrZWzDpGlu?= =?utf-8?q?en?= Subject: [PATCH v4 8/8] mkimage: fit_image: Add option to make fit header align Date: Mon, 30 Mar 2020 11:56:24 +0800 Message-Id: <20200330035625.25164-8-kever.yang@rock-chips.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200330035625.25164-1-kever.yang@rock-chips.com> References: <20200330035625.25164-1-kever.yang@rock-chips.com> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 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.2 at phobos.denx.de X-Virus-Status: Clean The image is usually stored in block device like emmc, SD card, make the offset of image data aligned to block(512 byte) can avoid data copy during boot process. eg. SPL boot from FIT image with external data: - SPL read the first block of FIT image, and then parse the header; - SPL read image data separately; - The first image offset is the base_offset which is the header size; - The second image offset is just after the first image; - If the offset of imge does not aligned, SPL will do memcpy; The header size is a ramdon number, which is very possible not aligned, so add '-B size'to specify the align size in hex for better performance. example usage: ./tools/mkimage -E -f u-boot.its -B 0x200 u-boot.itb Signed-off-by: Kever Yang Reviewed-by: Punit Agrawal Reviewed-by: Tom Rini --- Changes in v4: - update to clarify the size in hex format Changes in v3: - add common ALIGN() at imagetool.h - migrate to use imagetool.h for all other files under tools/ Changes in v2: - use '-B' to take a argument as a align block lenth; - add new variable to indecate align_size - address commens from Heinrich, Rasmus, Tom, Punit; doc/uImage.FIT/source_file_format.txt | 5 ++++ tools/fit_image.c | 33 ++++++++++++++++----------- tools/imagetool.h | 1 + tools/mkimage.c | 14 ++++++++++-- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/doc/uImage.FIT/source_file_format.txt b/doc/uImage.FIT/source_file_format.txt index 18d2aedcb7..884a58456f 100644 --- a/doc/uImage.FIT/source_file_format.txt +++ b/doc/uImage.FIT/source_file_format.txt @@ -304,6 +304,11 @@ Normal kernel FIT image has data embedded within FIT structure. U-Boot image for SPL boot has external data. Existence of 'data-offset' can be used to identify which format is used. +For FIT image with external data, it would be better to align each blob of data +to block(512 byte) for block device, so that we don't need to do the copy when +read the image data in SPL. Pass '-B 0x200' to mkimage to align the FIT +structure and data to 512 byte, other values available for other align size. + 9) Examples ----------- diff --git a/tools/fit_image.c b/tools/fit_image.c index b7d615f8c8..06c32b3e71 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -422,7 +422,7 @@ err_buf: */ static int fit_extract_data(struct image_tool_params *params, const char *fname) { - void *buf; + void *buf = NULL; int buf_ptr; int fit_size, new_size; int fd; @@ -431,26 +431,33 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) int ret; int images; int node; + int image_number; + int align_size; + align_size = params->bl_len ? params->bl_len : 4; fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false); if (fd < 0) return -EIO; fit_size = fdt_totalsize(fdt); - /* Allocate space to hold the image data we will extract */ - buf = malloc(fit_size); - if (!buf) { - ret = -ENOMEM; - goto err_munmap; - } - buf_ptr = 0; - images = fdt_path_offset(fdt, FIT_IMAGES_PATH); if (images < 0) { debug("%s: Cannot find /images node: %d\n", __func__, images); ret = -EINVAL; goto err_munmap; } + image_number = fdtdec_get_child_count(fdt, images); + + /* + * Allocate space to hold the image data we will extract, + * extral space allocate for image alignment to prevent overflow. + */ + buf = malloc(fit_size + (align_size * image_number)); + if (!buf) { + ret = -ENOMEM; + goto err_munmap; + } + buf_ptr = 0; for (node = fdt_first_subnode(fdt, images); node >= 0; @@ -478,17 +485,17 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) buf_ptr); } fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len); - - buf_ptr += (len + 3) & ~3; + buf_ptr += ALIGN(len, align_size); } /* Pack the FDT and place the data after it */ fdt_pack(fdt); + new_size = fdt_totalsize(fdt); + new_size = ALIGN(new_size, align_size); + fdt_set_totalsize(fdt, new_size); debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt)); debug("External data size %x\n", buf_ptr); - new_size = fdt_totalsize(fdt); - new_size = (new_size + 3) & ~3; munmap(fdt, sbuf.st_size); if (ftruncate(fd, new_size)) { diff --git a/tools/imagetool.h b/tools/imagetool.h index 81e5cd0c5c..f54809cd57 100644 --- a/tools/imagetool.h +++ b/tools/imagetool.h @@ -79,6 +79,7 @@ struct image_tool_params { bool external_data; /* Store data outside the FIT */ bool quiet; /* Don't output text in normal operation */ unsigned int external_offset; /* Add padding to external data */ + int bl_len; /* Block length in byte for external data */ const char *engine_id; /* Engine to use for signing */ }; diff --git a/tools/mkimage.c b/tools/mkimage.c index 0279f6867e..336376f8d0 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -98,8 +98,9 @@ static void usage(const char *msg) " -i => input filename for ramdisk file\n"); #ifdef CONFIG_FIT_SIGNATURE fprintf(stderr, - "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c ] [-p addr] [-r] [-N engine]\n" + "Signing / verified boot options: [-E] [-B size] [-k keydir] [-K dtb] [ -c ] [-p addr] [-r] [-N engine]\n" " -E => place data outside of the FIT structure\n" + " -B => align size in hex for FIT structure and header\n" " -k => set directory containing private keys\n" " -K => write public keys to this .dtb file\n" " -c => add comment in signature node\n" @@ -144,7 +145,7 @@ static void process_args(int argc, char **argv) int opt; while ((opt = getopt(argc, argv, - "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) { + "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) { switch (opt) { case 'a': params.addr = strtoull(optarg, &ptr, 16); @@ -168,6 +169,15 @@ static void process_args(int argc, char **argv) params.cmdname, optarg); exit(EXIT_FAILURE); } + break; + case 'B': + params.bl_len = strtoull(optarg, &ptr, 16); + if (*ptr) { + fprintf(stderr, "%s: invalid block length %s\n", + params.cmdname, optarg); + exit(EXIT_FAILURE); + } + break; case 'c': params.comment = optarg;