[PATCH] libfdt: Replace hardcoded FDT version numbers with macros

Dmytro Prokopchuk1 posted 1 patch 5 days, 13 hours ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/b449ec7262c4e04c7e82eb50df294991dd9fbe50.1763030084.git.dmytro._5Fprokopchuk1@epam.com
xen/common/device-tree/device-tree.c |  2 +-
xen/common/libfdt/fdt.c              |  8 +++++---
xen/common/libfdt/fdt_ro.c           | 13 +++++++------
xen/common/libfdt/fdt_rw.c           | 16 ++++++++--------
4 files changed, 21 insertions(+), 18 deletions(-)
[PATCH] libfdt: Replace hardcoded FDT version numbers with macros
Posted by Dmytro Prokopchuk1 5 days, 13 hours ago
Replace all hardcoded FDT version numbers (such as 16 and 17, or 0x10 and 0x11)
with the macros FDT_LAST_COMPATIBLE_VERSION and FDT_LAST_SUPPORTED_VERSION
throughout the Xen device tree and libfdt code.

This avoids magic numbers and makes the Xen codebase more robust to future FDT
version changes.

Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>
---
 xen/common/device-tree/device-tree.c |  2 +-
 xen/common/libfdt/fdt.c              |  8 +++++---
 xen/common/libfdt/fdt_ro.c           | 13 +++++++------
 xen/common/libfdt/fdt_rw.c           | 16 ++++++++--------
 4 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/xen/common/device-tree/device-tree.c b/xen/common/device-tree/device-tree.c
index 0b5375f151..ee8b32a8b4 100644
--- a/xen/common/device-tree/device-tree.c
+++ b/xen/common/device-tree/device-tree.c
@@ -1949,7 +1949,7 @@ static unsigned long unflatten_dt_node(const void *fdt,
         sz = be32_to_cpu(*(__be32 *)(*p));
         noff = be32_to_cpu(*(__be32 *)((*p) + 4));
         *p += 8;
-        if ( fdt_version(fdt) < 0x10 )
+        if ( fdt_version(fdt) < FDT_LAST_COMPATIBLE_VERSION )
             *p = ROUNDUP(*p, sz >= 8 ? 8 : 4);
 
         pname = fdt_string(fdt, noff);
diff --git a/xen/common/libfdt/fdt.c b/xen/common/libfdt/fdt.c
index 9fe7cf4b74..4b30a17b93 100644
--- a/xen/common/libfdt/fdt.c
+++ b/xen/common/libfdt/fdt.c
@@ -119,7 +119,8 @@ int fdt_check_header(const void *fdt)
 
 	if (!can_assume(VALID_DTB)) {
 		/* Bounds check structure block */
-		if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
+		if (!can_assume(LATEST) &&
+			fdt_version(fdt) < FDT_LAST_SUPPORTED_VERSION) {
 			if (!check_off_(hdrsize, fdt_totalsize(fdt),
 					fdt_off_dt_struct(fdt)))
 				return -FDT_ERR_TRUNCATED;
@@ -154,7 +155,7 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
 		    || (absoffset + len) > fdt_totalsize(fdt))
 			return NULL;
 
-	if (can_assume(LATEST) || fdt_version(fdt) >= 0x11)
+	if (can_assume(LATEST) || fdt_version(fdt) >= FDT_LAST_SUPPORTED_VERSION)
 		if (((uoffset + len) < uoffset)
 		    || ((offset + len) > fdt_size_dt_struct(fdt)))
 			return NULL;
@@ -195,7 +196,8 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
 		offset += sizeof(struct fdt_property) - FDT_TAGSIZE
 			+ fdt32_to_cpu(*lenp);
 		if (!can_assume(LATEST) &&
-		    fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 &&
+		    fdt_version(fdt) < FDT_LAST_COMPATIBLE_VERSION &&
+		    fdt32_to_cpu(*lenp) >= 8 &&
 		    ((offset - fdt32_to_cpu(*lenp)) % 8) != 0)
 			offset += 4;
 		break;
diff --git a/xen/common/libfdt/fdt_ro.c b/xen/common/libfdt/fdt_ro.c
index 17584da257..f719712b71 100644
--- a/xen/common/libfdt/fdt_ro.c
+++ b/xen/common/libfdt/fdt_ro.c
@@ -60,7 +60,8 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
 	if (fdt_magic(fdt) == FDT_MAGIC) {
 		if (stroffset < 0)
 			goto fail;
-		if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
+		if (can_assume(LATEST) ||
+			fdt_version(fdt) >= FDT_LAST_SUPPORTED_VERSION) {
 			if ((unsigned)stroffset >= fdt_size_dt_strings(fdt))
 				goto fail;
 			if ((fdt_size_dt_strings(fdt) - stroffset) < len)
@@ -309,7 +310,7 @@ const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
 
 	nameptr = nh->name;
 
-	if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
+	if (!can_assume(LATEST) && fdt_version(fdt) < FDT_LAST_COMPATIBLE_VERSION) {
 		/*
 		 * For old FDT versions, match the naming conventions of V16:
 		 * give only the leaf name (after all /). The actual tree
@@ -382,7 +383,7 @@ const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
 	/* Prior to version 16, properties may need realignment
 	 * and this API does not work. fdt_getprop_*() will, however. */
 
-	if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
+	if (!can_assume(LATEST) && fdt_version(fdt) < FDT_LAST_COMPATIBLE_VERSION) {
 		if (lenp)
 			*lenp = -FDT_ERR_BADVERSION;
 		return NULL;
@@ -429,7 +430,7 @@ const struct fdt_property *fdt_get_property_namelen(const void *fdt,
 {
 	/* Prior to version 16, properties may need realignment
 	 * and this API does not work. fdt_getprop_*() will, however. */
-	if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
+	if (!can_assume(LATEST) && fdt_version(fdt) < FDT_LAST_COMPATIBLE_VERSION) {
 		if (lenp)
 			*lenp = -FDT_ERR_BADVERSION;
 		return NULL;
@@ -460,7 +461,7 @@ const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
 		return NULL;
 
 	/* Handle realignment */
-	if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 &&
+	if (!can_assume(LATEST) && fdt_version(fdt) < FDT_LAST_COMPATIBLE_VERSION &&
 	    (poffset + sizeof(*prop)) % 8 && fdt32_ld_(&prop->len) >= 8)
 		return prop->data + 4;
 	return prop->data;
@@ -493,7 +494,7 @@ const void *fdt_getprop_by_offset(const void *fdt, int offset,
 	}
 
 	/* Handle realignment */
-	if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 &&
+	if (!can_assume(LATEST) && fdt_version(fdt) < FDT_LAST_COMPATIBLE_VERSION &&
 	    (offset + sizeof(*prop)) % 8 && fdt32_ld_(&prop->len) >= 8)
 		return prop->data + 4;
 	return prop->data;
diff --git a/xen/common/libfdt/fdt_rw.c b/xen/common/libfdt/fdt_rw.c
index 3621d3651d..9e87eabc5d 100644
--- a/xen/common/libfdt/fdt_rw.c
+++ b/xen/common/libfdt/fdt_rw.c
@@ -28,13 +28,13 @@ static int fdt_rw_probe_(void *fdt)
 		return 0;
 	FDT_RO_PROBE(fdt);
 
-	if (!can_assume(LATEST) && fdt_version(fdt) < 17)
+	if (!can_assume(LATEST) && fdt_version(fdt) < FDT_LAST_SUPPORTED_VERSION)
 		return -FDT_ERR_BADVERSION;
 	if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
 				   fdt_size_dt_struct(fdt)))
 		return -FDT_ERR_BADLAYOUT;
-	if (!can_assume(LATEST) && fdt_version(fdt) > 17)
-		fdt_set_version(fdt, 17);
+	if (!can_assume(LATEST) && fdt_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
+		fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
 
 	return 0;
 }
@@ -430,9 +430,9 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
 		* sizeof(struct fdt_reserve_entry);
 
-	if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
+	if (can_assume(LATEST) || fdt_version(fdt) >= FDT_LAST_SUPPORTED_VERSION) {
 		struct_size = fdt_size_dt_struct(fdt);
-	} else if (fdt_version(fdt) == 16) {
+	} else if (fdt_version(fdt) == FDT_LAST_COMPATIBLE_VERSION) {
 		struct_size = 0;
 		while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
 			;
@@ -448,7 +448,7 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 		err = fdt_move(fdt, buf, bufsize);
 		if (err)
 			return err;
-		fdt_set_version(buf, 17);
+		fdt_set_version(buf, FDT_LAST_SUPPORTED_VERSION);
 		fdt_set_size_dt_struct(buf, struct_size);
 		fdt_set_totalsize(buf, bufsize);
 		return 0;
@@ -477,8 +477,8 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize)
 
 	fdt_set_magic(buf, FDT_MAGIC);
 	fdt_set_totalsize(buf, bufsize);
-	fdt_set_version(buf, 17);
-	fdt_set_last_comp_version(buf, 16);
+	fdt_set_version(buf, FDT_LAST_SUPPORTED_VERSION);
+	fdt_set_last_comp_version(buf, FDT_LAST_COMPATIBLE_VERSION);
 	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
 
 	return 0;
-- 
2.43.0
Re: [PATCH] libfdt: Replace hardcoded FDT version numbers with macros
Posted by Julien Grall 2 days, 12 hours ago
Hi,

On 13/11/2025 10:36, Dmytro Prokopchuk1 wrote:
> Replace all hardcoded FDT version numbers (such as 16 and 17, or 0x10 and 0x11)
> with the macros FDT_LAST_COMPATIBLE_VERSION and FDT_LAST_SUPPORTED_VERSION
> throughout the Xen device tree and libfdt code.
> 
> This avoids magic numbers and makes the Xen codebase more robust to future FDT
> version changes.
 > > Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>
> ---
>   xen/common/device-tree/device-tree.c |  2 +-
>   xen/common/libfdt/fdt.c              |  8 +++++---
>   xen/common/libfdt/fdt_ro.c           | 13 +++++++------
>   xen/common/libfdt/fdt_rw.c           | 16 ++++++++--------

While I appreciate the goal, libfdt/ is meant to be a verbatim copy of 
the [1]. I would like to keep it like that (backports would be 
acceptable). So if you want to make any change, then it first needs to 
be merged in the upstream repo.

Cheers,

[1] https://github.com/dgibson/dtc/tree/main/libfdt

-- 
Julien Grall