From patchwork Sun Nov 6 15:41:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Bj=C3=B8rn_Mork?= X-Patchwork-Id: 1700298 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=mork.no header.i=@mork.no header.a=rsa-sha256 header.s=b header.b=Q30saXb9; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N52jj2cCJz23ls for ; Mon, 7 Nov 2022 05:20:57 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0E7A984BD2; Sun, 6 Nov 2022 19:20:36 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=mork.no Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=mork.no header.i=@mork.no header.b="Q30saXb9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C766883F5C; Sun, 6 Nov 2022 16:41:22 +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_PASS,SPF_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from louie.mork.no (louie.mork.no [IPv6:2001:41c8:51:8a:feff:ff:fe00:e5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6A98184198 for ; Sun, 6 Nov 2022 16:41:20 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=mork.no Authentication-Results: phobos.denx.de; spf=none smtp.mailfrom=bjorn@miraculix.mork.no Received: from canardo.dyn.mork.no ([IPv6:2a01:799:c9c:2c00:0:0:0:1]) (authenticated bits=0) by louie.mork.no (8.15.2/8.15.2) with ESMTPSA id 2A6FfICA766553 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=OK) for ; Sun, 6 Nov 2022 15:41:19 GMT Received: from miraculix.mork.no ([IPv6:2a01:799:c9c:2c02:34cc:c78d:869d:3d9d]) (authenticated bits=0) by canardo.dyn.mork.no (8.15.2/8.15.2) with ESMTPSA id 2A6FfDpw3699860 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=OK); Sun, 6 Nov 2022 16:41:13 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mork.no; s=b; t=1667749273; bh=5yXUgJNrEOMdIvXl6SRbsMZGvcS8SEuIDFNPeAWTP90=; h=From:To:Cc:Subject:Date:Message-Id:References:From; b=Q30saXb9kxk4abcdoEWv4mEZ8NyisPX3cuTggBKbxw7XrXk1F5MGXRlGcVcbTRiVv mazVEJL/0zamtbAggYSEX/TYFF91j3jCK2PAYXeh48Y8oAqFSY9tpjH8ScgqJTy5NR bSVe7m6EuVtBjGHsa5nzBEzNX0TGJxGi3QeS9Pcg= Received: (nullmailer pid 872262 invoked by uid 1000); Sun, 06 Nov 2022 15:41:12 -0000 From: =?utf-8?q?Bj=C3=B8rn_Mork?= To: u-boot@lists.denx.de Cc: =?utf-8?q?Bj=C3=B8rn_Mork?= Subject: [RFC v2 1/2] libfdt: add fdt_alignprop Date: Sun, 6 Nov 2022 16:41:10 +0100 Message-Id: <20221106154111.872209-2-bjorn@mork.no> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221106154111.872209-1-bjorn@mork.no> References: <20221106154111.872209-1-bjorn@mork.no> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 06 Nov 2022 19:20:30 +0100 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 Properties are sometimes used to store data with stricter alignment requirements than the 4-byte fdt tag alignment. Aligning the offset of the property data will alloe a client to directly address it, without reloaction, provided the fdt is loaded on a similarily aligned boundary. One known use-case for this is the U-Boot FIT images, which may embed nested fdt blobs inside properties of the outer FIT fdt. Although strongly discouraged, U-Boot can be configured to use these embedded blobs "in place". This is discouraged because if only will work if the offset of the property value, i.e. the embedded fdt, happens to match the 8-byte boundary required by the devicetree spec. Adding the ability to align property data allows U-Boot tools to reliably create FIT images for such use. Other use cases are currently not known, but anticipated. Signed-off-by: Bjørn Mork --- scripts/dtc/libfdt/fdt_rw.c | 26 ++++++++++++++++++++++++ scripts/dtc/libfdt/fdt_wip.c | 2 +- scripts/dtc/libfdt/libfdt.h | 30 ++++++++++++++++++++++++++++ scripts/dtc/libfdt/libfdt_internal.h | 1 + 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/scripts/dtc/libfdt/fdt_rw.c b/scripts/dtc/libfdt/fdt_rw.c index 2eb2b38703be..9e0e8dfeec8c 100644 --- a/scripts/dtc/libfdt/fdt_rw.c +++ b/scripts/dtc/libfdt/fdt_rw.c @@ -331,6 +331,32 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name) return fdt_splice_struct_(fdt, prop, proplen, 0); } +int fdt_alignprop(void *fdt, int nodeoffset, const char *name, size_t align) +{ + struct fdt_property *prop; + int err, len, diff; + + FDT_RW_PROBE(fdt); + + if (align != FDT_TAGALIGN(align)) + return -FDT_ERR_BADOFFSET; + + prop = fdt_get_property_w(fdt, nodeoffset, name, &len); + if (!prop) + return len; + + diff = FDT_ALIGN((uintptr_t)prop->data, align) - (uintptr_t)prop->data; + if (!diff) + return 0; + + err = fdt_splice_struct_(fdt, prop, 0, diff); + if (err) + return err; + + fdt_nop_region_(prop, diff); + return 0; +} + int fdt_add_subnode_namelen(void *fdt, int parentoffset, const char *name, int namelen) { diff --git a/scripts/dtc/libfdt/fdt_wip.c b/scripts/dtc/libfdt/fdt_wip.c index c2d7566a67dc..10a75058f0f0 100644 --- a/scripts/dtc/libfdt/fdt_wip.c +++ b/scripts/dtc/libfdt/fdt_wip.c @@ -48,7 +48,7 @@ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, val, len); } -static void fdt_nop_region_(void *start, int len) +void fdt_nop_region_(void *start, int len) { fdt32_t *p; diff --git a/scripts/dtc/libfdt/libfdt.h b/scripts/dtc/libfdt/libfdt.h index d706f85962bd..4490f9feb7ce 100644 --- a/scripts/dtc/libfdt/libfdt.h +++ b/scripts/dtc/libfdt/libfdt.h @@ -1928,6 +1928,36 @@ int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset, */ int fdt_delprop(void *fdt, int nodeoffset, const char *name); +/** + * fdt_alignprop - align property data to a given boundary + * @fdt: pointer to the device tree blob + * @nodeoffset: offset of the node whose property to nop + * @name: name of the property to nop + * @align: requested alignment boundary in bytes (must be multiple of 4) + * + * fdt_alignprop() will align the property data to a chosen boundary + * by injecting nop tags in front of the given property. The + * alignment must be a multiple of 4. + * + * This function may insert nop tags in the blob, and will therefore + * change the offsets of some existing nodes. + * + * returns: + * 0, on success + * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to + * move the property to the new boundary + * -FDT_ERR_NOTFOUND, node does not have the named property + * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag + * or the requested alignment was not a multiple of 4 + * -FDT_ERR_BADLAYOUT, + * -FDT_ERR_BADMAGIC, + * -FDT_ERR_BADVERSION, + * -FDT_ERR_BADSTATE, + * -FDT_ERR_BADSTRUCTURE, + * -FDT_ERR_TRUNCATED, standard meanings + */ +int fdt_alignprop(void *fdt, int nodeoffset, const char *name, size_t align); + /** * fdt_add_subnode_namelen - creates a new node based on substring * @fdt: pointer to the device tree blob diff --git a/scripts/dtc/libfdt/libfdt_internal.h b/scripts/dtc/libfdt/libfdt_internal.h index 5436e2ceeacf..7abee9c21057 100644 --- a/scripts/dtc/libfdt/libfdt_internal.h +++ b/scripts/dtc/libfdt/libfdt_internal.h @@ -25,6 +25,7 @@ int fdt_check_node_offset_(const void *fdt, int offset); int fdt_check_prop_offset_(const void *fdt, int offset); const char *fdt_find_string_(const char *strtab, int tabsize, const char *s); int fdt_node_end_offset_(void *fdt, int nodeoffset); +void fdt_nop_region_(void *start, int len); static inline const void *fdt_offset_ptr_(const void *fdt, int offset) { From patchwork Sun Nov 6 15:41:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Bj=C3=B8rn_Mork?= X-Patchwork-Id: 1700299 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=mork.no header.i=@mork.no header.a=rsa-sha256 header.s=b header.b=eCEYohVm; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N52jn0mRKz23ls for ; Mon, 7 Nov 2022 05:21:01 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EF36884CB2; Sun, 6 Nov 2022 19:20:38 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=mork.no Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=mork.no header.i=@mork.no header.b="eCEYohVm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 98ADD841D1; Sun, 6 Nov 2022 16:41:24 +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_PASS,SPF_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from louie.mork.no (louie.mork.no [IPv6:2001:41c8:51:8a:feff:ff:fe00:e5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 938B384374 for ; Sun, 6 Nov 2022 16:41:20 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=mork.no Authentication-Results: phobos.denx.de; spf=none smtp.mailfrom=bjorn@miraculix.mork.no Received: from canardo.dyn.mork.no ([IPv6:2a01:799:c9c:2c00:0:0:0:1]) (authenticated bits=0) by louie.mork.no (8.15.2/8.15.2) with ESMTPSA id 2A6FfIkG766554 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=OK) for ; Sun, 6 Nov 2022 15:41:19 GMT Received: from miraculix.mork.no ([IPv6:2a01:799:c9c:2c02:34cc:c78d:869d:3d9d]) (authenticated bits=0) by canardo.dyn.mork.no (8.15.2/8.15.2) with ESMTPSA id 2A6FfDid3699868 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=OK); Sun, 6 Nov 2022 16:41:13 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mork.no; s=b; t=1667749273; bh=QMca5yVtBsjAMPET1tSj3H+G4UZbPwKyLiUJMsqDgnQ=; h=From:To:Cc:Subject:Date:Message-Id:References:From; b=eCEYohVm6sqv2Kf/WtyiXmtVXh7C95sCAMwVYrtgKAGN2fEX9T2HLdWBoxZyoloE4 n+Gp5hui5YcA5ROxJayipX4/noJxDNQKEwcNXsTcaGgDauZP7CHkW/0lq+Rv1CCWFf zghSO9qwBdtcI9d22afMYhnVa2p5ckj1f9Z0e+E0= Received: (nullmailer pid 872264 invoked by uid 1000); Sun, 06 Nov 2022 15:41:12 -0000 From: =?utf-8?q?Bj=C3=B8rn_Mork?= To: u-boot@lists.denx.de Cc: =?utf-8?q?Bj=C3=B8rn_Mork?= Subject: [RFC v2 2/2] mkimage: Align fdt images in FIT to 8 bytes Date: Sun, 6 Nov 2022 16:41:11 +0100 Message-Id: <20221106154111.872209-3-bjorn@mork.no> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221106154111.872209-1-bjorn@mork.no> References: <20221106154111.872209-1-bjorn@mork.no> MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 06 Nov 2022 19:20:30 +0100 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 "In place" configurations are strongly discouraged, but known to be used. This can cause hard to debug alignment problems since the value of the "data" property only is guaranteed to be 4-byte aligned. Unconditionally aligning fdt images to 8 byte boundaries will prevent these problems, at the maximum price of 4 bytes per fdt. Signed-off-by: Bjørn Mork --- tools/fit_image.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tools/fit_image.c b/tools/fit_image.c index 923a9755b709..1e060784895d 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -24,6 +24,41 @@ static struct legacy_img_hdr header; +/** + * fit_align_fdt_images() - Align all fdt images in the FIT to 8 bytes + * + * An fdt blob must be placed at an 8 byte aligned offset if it is to + * be used "in place". This will not be verified by U-Boot. Simply align + * all IH_TYPE_FLATDT images to prevent problems. + */ +static int fit_align_fdt_images(void *fit) +{ + int images_noffset, noffset, ret; + uint8_t type; + size_t size; + const void *ptr; + + images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); + if (images_noffset < 0) { + printf("Can't find images parent node '%s' (%s)\n", + FIT_IMAGES_PATH, fdt_strerror(images_noffset)); + return images_noffset; + } + + for (noffset = fdt_first_subnode(fit, images_noffset); + noffset >= 0; + noffset = fdt_next_subnode(fit, noffset)) { + fit_image_get_type(fit, noffset, &type); + if (type != IH_TYPE_FLATDT) + continue; + + ret = fdt_alignprop(fit, noffset, FIT_DATA_PROP, 8); + if (ret < 0) + return ret; + } + return 0; +} + static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, const char *tmpfile) { @@ -81,6 +116,10 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, ¶ms->summary); } + if (!ret) { + ret = fit_align_fdt_images(ptr); + } + if (dest_blob) { munmap(dest_blob, destfd_size); close(destfd);