Devicetree
 help / color / mirror / Atom feed
From: Herve Codina <herve.codina@bootlin.com>
To: David Gibson <david@gibson.dropbear.id.au>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>
Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>,
	Kieran Bingham <kieran.bingham@ideasonboard.com>,
	Frank Li <Frank.Li@nxp.com>,
	David Lechner <dlechner@baylibre.com>,
	Ayush Singh <ayush@beagleboard.org>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	devicetree-compiler@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, devicetree-spec@vger.kernel.org,
	Hui Pu <hui.pu@gehealthcare.com>,
	Ian Ray <ian.ray@gehealthcare.com>,
	Luca Ceresoli <luca.ceresoli@bootlin.com>,
	Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	Herve Codina <herve.codina@bootlin.com>
Subject: [PATCH v4 19/22] libfdt: Introduce fdt_getprop_by_offset_w()
Date: Thu, 26 Nov 2026 12:22:36 +0100	[thread overview]
Message-ID: <20261126112239.636284-20-herve.codina@bootlin.com> (raw)
In-Reply-To: <20261126112239.636284-1-herve.codina@bootlin.com>

Future operations, such as handling unknown tags on dtb modifications,
need to modify properties retrieved by their offset.

fdt_getprop_by_offset() already exists to get a property by its offset
but the property returned is read-only. A writable returned property is
needed.

Fill the gap and introduce fdt_getprop_by_offset_w(), the write enabled
variant of fdt_getprop_by_offset().

Also add its related test.

Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
 libfdt/libfdt.h           |  6 +++
 tests/.gitignore          |  1 +
 tests/Makefile.tests      |  2 +-
 tests/get_prop_offset_w.c | 78 +++++++++++++++++++++++++++++++++++++++
 tests/meson.build         |  1 +
 tests/run_tests.sh        |  1 +
 6 files changed, 88 insertions(+), 1 deletion(-)
 create mode 100644 tests/get_prop_offset_w.c

diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
index 7a1915a5..82965e32 100644
--- a/libfdt/libfdt.h
+++ b/libfdt/libfdt.h
@@ -856,6 +856,12 @@ static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
 #ifndef SWIG /* This function is not useful in Python */
 const void *fdt_getprop_by_offset(const void *fdt, int offset,
 				  const char **namep, int *lenp);
+static inline void *fdt_getprop_by_offset_w(const void *fdt, int offset,
+					    const char **namep, int *lenp)
+{
+	return (void *)(uintptr_t)fdt_getprop_by_offset(fdt, offset, namep,
+							lenp);
+}
 #endif
 
 /**
diff --git a/tests/.gitignore b/tests/.gitignore
index 914699be..6fd6cb97 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -30,6 +30,7 @@ tmp.*
 /get_phandle
 /getprop
 /get_prop_offset
+/get_prop_offset_w
 /incbin
 /integer-expressions
 /fs_tree1
diff --git a/tests/Makefile.tests b/tests/Makefile.tests
index 30452d05..ddfe317e 100644
--- a/tests/Makefile.tests
+++ b/tests/Makefile.tests
@@ -16,7 +16,7 @@ LIB_TESTS_L = get_mem_rsv \
 	sw_tree1 sw_states \
 	move_and_save mangle-layout nopulate \
 	open_pack rw_tree1 rw_oom set_name setprop del_property del_node \
-	appendprop1 appendprop2 propname_escapes \
+	appendprop1 appendprop2 propname_escapes get_prop_offset_w \
 	string_escapes references path-references phandle_format \
 	boot-cpuid incbin relref_merge \
 	extra-terminating-null \
diff --git a/tests/get_prop_offset_w.c b/tests/get_prop_offset_w.c
new file mode 100644
index 00000000..be31744c
--- /dev/null
+++ b/tests/get_prop_offset_w.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * libfdt - Flat Device Tree manipulation
+ *	Testcase for fdt_getprop_by_offset_w()
+ * Copyright (C) 2026 Bootlin.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include <libfdt.h>
+
+#include "tests.h"
+#include "testdata.h"
+#include "util.h"
+
+int main(int argc, char *argv[])
+{
+	const char *name, *name_w;
+	const void *propval;
+	char *modif_val;
+	void *propval_w;
+	int proplen_w;
+	int exp_len;
+	int poffset;
+	void *fdt;
+
+	test_init(argc, argv);
+	fdt = load_blob_arg(argc, argv);
+
+	/* Retrieve the offset of the 'prop-str' property */
+	fdt_for_each_property_offset(poffset, fdt, 0) {
+		propval = fdt_getprop_by_offset(fdt, poffset, &name, NULL);
+		if (propval) {
+			if (!strcmp(name, "prop-str"))
+				break;
+		}
+	}
+	if (poffset < 0)
+		FAIL("Property 'prop-str' not found");
+
+	/* Get the property value for modification */
+	propval_w = fdt_getprop_by_offset_w(fdt, poffset, &name_w, &proplen_w);
+	if (!propval_w)
+		FAIL("fdt_getprop_by_offset_w(%d): %s", poffset,
+		     fdt_strerror(proplen_w));
+
+	/* Check original values */
+	if (name_w != name)
+		FAIL("Name pointers mismatch : %p instead %p", name_w, name);
+
+	/* Check that returned pointers for read-only and write are the same */
+	if (propval_w != propval)
+		FAIL("Data pointers mismatch : %p instead %p", propval_w, propval);
+
+	/* Check length of the property */
+	exp_len = strlen(TEST_STRING_1) + 1;
+	if (exp_len < 4)
+		CONFIG("Property \"%s\" should be at least 4 bytes", name);
+	if (proplen_w != exp_len)
+		FAIL("Original size mismatch on property \"%s\": %d instead of %d",
+		     name, proplen_w, exp_len);
+
+	/* Modify the property value */
+	modif_val = xmalloc(proplen_w);
+	memcpy(modif_val, propval_w, proplen_w);
+	modif_val[1] = (modif_val[1] != 'A') ? 'A' : 'a';
+	modif_val[3] = (modif_val[3] != 'B') ? 'B' : 'b';
+	memcpy(propval_w, modif_val, proplen_w);
+
+	/* Check that the modified value is taken into account */
+	if (!check_get_prop_offset(fdt, poffset, "prop-str", proplen_w, modif_val))
+		FAIL("Modified property 'prop-str' not found");
+
+	PASS();
+}
diff --git a/tests/meson.build b/tests/meson.build
index 89c6a75a..59264262 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -284,6 +284,7 @@ tests = [
   'get_path',
   'get_phandle',
   'get_prop_offset',
+  'get_prop_offset_w',
   'get_next_tag_invalid_prop_len',
   'getprop',
   'incbin',
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 5334a9b4..06f6188a 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -445,6 +445,7 @@ tree1_tests () {
     run_test setprop_inplace $TREE
     run_test nop_property $TREE
     run_test nop_node $TREE
+    run_test get_prop_offset_w $TREE
 }
 
 tree1_tests_rw () {
-- 
2.56.1


  parent reply	other threads:[~2026-11-26 11:24 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 11:22 [PATCH v4 00/22] Add support for structured tags and v18 dtb version Herve Codina
2026-11-26 11:22 ` [PATCH v4 01/22] libfdt: Don't assume the root node is available at offset 0 Herve Codina
2026-11-26 11:22 ` [PATCH v4 02/22] tests: Add a test for fdt_root_offset() Herve Codina
2026-11-26 11:22 ` [PATCH v4 03/22] tests: Don't assume the root node is available at offset 0 Herve Codina
2026-11-26 11:22 ` [PATCH v4 04/22] tests/nopulate: Add an FDT_NOP before the root node Herve Codina
2026-11-26 11:22 ` [PATCH v4 05/22] tests: dtbs_equal_ordered: Add --noskip-nop option Herve Codina
2026-11-26 11:22 ` [PATCH v4 06/22] dtc: Skip possible FDT_NOP before the root node and before FDT_END tag Herve Codina
2026-11-26 11:22 ` [PATCH v4 07/22] tests: treegen: Introduce emit_fdt_header_vers() Herve Codina
2026-11-26 11:22 ` [PATCH v4 08/22] tests: treegen: Introduce tree harness Herve Codina
2026-11-26 11:22 ` [PATCH v4 09/22] util.h: Add missing <stdio.h> Herve Codina
2026-11-26 11:22 ` [PATCH v4 10/22] tests: treegen: Export tree generation primitives Herve Codina
2026-11-26 11:22 ` [PATCH v4 11/22] tests: treegen: Move base harness trees to treegen_base.c Herve Codina
2026-11-26 11:22 ` [PATCH v4 12/22] Introduce structured tag value definition Herve Codina
2026-11-26 11:22 ` [PATCH v4 13/22] util.h: Add concat() macro Herve Codina
2026-11-26 11:22 ` [PATCH v4 14/22] tests: treegen: Generate dtbs with unknown tags Herve Codina
2026-11-26 11:22 ` [PATCH v4 15/22] fdtdump: Handle " Herve Codina
2026-11-26 11:22 ` [PATCH v4 16/22] libfdt: Handle unknown tags in fdt_next_tag() Herve Codina
2026-11-26 11:22 ` [PATCH v4 17/22] flattree: Handle unknown tags Herve Codina
2026-11-26 11:22 ` [PATCH v4 18/22] libfdt: Introduce fdt_ptr_offset_() Herve Codina
2026-11-26 11:22 ` Herve Codina [this message]
2026-11-26 11:22 ` [PATCH v4 20/22] tests: Add wip_func utility Herve Codina
2026-11-26 11:22 ` [PATCH v4 21/22] libfdt: Handle unknown tags on dtb modifications Herve Codina
2026-11-26 11:22 ` [PATCH v4 22/22] Introduce v18 dtb version Herve Codina

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126112239.636284-20-herve.codina@bootlin.com \
    --to=herve.codina@bootlin.com \
    --cc=Frank.Li@nxp.com \
    --cc=ayush@beagleboard.org \
    --cc=conor+dt@kernel.org \
    --cc=david@gibson.dropbear.id.au \
    --cc=devicetree-compiler@vger.kernel.org \
    --cc=devicetree-spec@vger.kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=geert@linux-m68k.org \
    --cc=hui.pu@gehealthcare.com \
    --cc=ian.ray@gehealthcare.com \
    --cc=kieran.bingham@ideasonboard.com \
    --cc=krzk@kernel.org \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luca.ceresoli@bootlin.com \
    --cc=robh@kernel.org \
    --cc=thomas.petazzoni@bootlin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox