devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: "Uwe Kleine-König" <u.kleine-koenig@baylibre.com>
Cc: devicetree-compiler@vger.kernel.org
Subject: Re: [PATCH 6/6] Restore phandle references from __fixups__ node
Date: Thu, 21 Aug 2025 14:31:37 +1000	[thread overview]
Message-ID: <aKahKWCQhFt_o0Jp@zatzit> (raw)
In-Reply-To: <f3fa240e296b08e68b990d995da12978058c97f9.1755692822.git.u.kleine-koenig@baylibre.com>

[-- Attachment #1: Type: text/plain, Size: 5526 bytes --]

On Wed, Aug 20, 2025 at 03:11:32PM +0200, Uwe Kleine-König wrote:
> The __fixups__ node contains information about labels. Parse its
> properties to create phandle markers which improve the resulting dts
> when decompiling a device tree blob.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  dtc.c        |  1 +
>  dtc.h        |  3 ++
>  livetree.c   | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  treesource.c |  6 ++++
>  4 files changed, 106 insertions(+)
> 
> diff --git a/dtc.c b/dtc.c
> index 9f90b373967d..6dae60de0ea5 100644
> --- a/dtc.c
> +++ b/dtc.c
> @@ -343,6 +343,7 @@ int main(int argc, char *argv[])
>  	if (generate_symbols)
>  		generate_label_tree(dti, "__symbols__", true);
>  
> +	fixup_phandles(dti, "__fixups__");
>  	local_fixup_phandles(dti, "__local_fixups__");
>  
>  	if (generate_fixups) {
> diff --git a/dtc.h b/dtc.h
> index d07a583441f6..7231200e5d02 100644
> --- a/dtc.h
> +++ b/dtc.h
> @@ -342,6 +342,7 @@ void sort_tree(struct dt_info *dti);
>  void generate_labels_from_tree(struct dt_info *dti, const char *name);
>  void generate_label_tree(struct dt_info *dti, const char *name, bool allocph);
>  void generate_fixups_tree(struct dt_info *dti, const char *name);
> +void fixup_phandles(struct dt_info *dti, const char *name);
>  void generate_local_fixups_tree(struct dt_info *dti, const char *name);
>  void local_fixup_phandles(struct dt_info *dti, const char *name);
>  
> @@ -359,6 +360,8 @@ struct dt_info *dt_from_blob(const char *fname);
>  
>  /* Tree source */
>  
> +void property_add_marker(struct property *prop,
> +			 enum markertype type, unsigned int offset, char *ref);
>  void add_phandle_marker(struct dt_info *dti, struct property *prop, unsigned int offset);
>  void dt_to_source(FILE *f, struct dt_info *dti);
>  struct dt_info *dt_from_source(const char *f);
> diff --git a/livetree.c b/livetree.c
> index 3dbef9071017..5d72abceb526 100644
> --- a/livetree.c
> +++ b/livetree.c
> @@ -1151,6 +1151,102 @@ void generate_fixups_tree(struct dt_info *dti, const char *name)
>  			name);
>  }
>  
> +void fixup_phandles(struct dt_info *dti, const char *name)
> +{
> +	struct node *an;
> +	struct property *fp;
> +
> +	an = get_subnode(dti->dt, name);
> +	if (!an)
> +		return;
> +
> +	for_each_property(an, fp) {
> +		char *fnext = fp->val.val;
> +		char *fv;
> +		unsigned int fl;
> +
> +		while ((fl = fp->val.len - (fnext - fp->val.val))) {
> +			char *propname, *soffset;
> +			struct node *n;
> +			struct property *p;
> +			long offset;
> +
> +			fv = fnext;
> +			fnext = memchr(fv, 0, fl);
> +
> +			if (!fnext) {
> +				if (quiet < 1)
> +					fprintf(stderr, "Warning: Malformed fixup entry for label %s\n",
> +						fp->name);
> +				break;
> +			}
> +			fnext += 1;
> +
> +			propname = memchr(fv, ':', fnext - 1 - fv);
> +			if (!propname) {
> +				if (quiet < 1)
> +					fprintf(stderr, "Warning: Malformed fixup entry for label %s\n",
> +						fp->name);
> +				continue;
> +			}
> +			propname++;
> +
> +			soffset = memchr(propname, ':', fnext - 1 - propname);
> +			if (!soffset) {
> +				if (quiet < 1)
> +					fprintf(stderr, "Warning: Malformed fixup entry for label %s\n",
> +						fp->name);
> +				continue;
> +			}
> +			soffset++;
> +
> +			/*
> +			 * temporarily modify the property to not have to create
> +			 * a copy for the node path.
> +			 */
> +			*(propname - 1) = '\0';
> +
> +			n = get_node_by_path(dti->dt, fv);
> +			if (!n && quiet < 1)
> +				fprintf(stderr, "Warning: Label %s references non-existing node %s\n",
> +					fp->name, fv);
> +
> +			*(propname - 1) = ':';
> +
> +			if (!n)
> +				continue;
> +
> +			/*
> +			 * temporarily modify the property to not have to create
> +			 * a copy for the property name.
> +			 */
> +			*(soffset - 1) = '\0';
> +
> +			p = get_property(n, propname);
> +
> +			if (!p && quiet < 1)
> +				fprintf(stderr, "Warning: Label %s references non-existing property %s in node %s\n",
> +					fp->name, n->fullpath, propname);
> +
> +			*(soffset - 1) = ':';
> +
> +			if (!p)
> +				continue;
> +
> +			offset = strtol(soffset, NULL, 0);
> +			if (offset < 0 || offset + 4 > p->val.len) {
> +				if (quiet < 1)
> +					fprintf(stderr,
> +						"Warning: Label %s contains invalid offset for property %s in node %s\n",
> +						fp->name, p->name, n->fullpath);
> +				continue;
> +			}
> +
> +			property_add_marker(p, REF_PHANDLE, offset, fp->name);
> +		}
> +	}
> +}
> +
>  void generate_local_fixups_tree(struct dt_info *dti, const char *name)
>  {
>  	if (!any_local_fixup_tree(dti, dti->dt))
> diff --git a/treesource.c b/treesource.c
> index 5b8d7a679519..6cd996f0d17b 100644
> --- a/treesource.c
> +++ b/treesource.c
> @@ -173,6 +173,12 @@ static struct marker **add_marker(struct marker **mi,
>  	return &nm->next;
>  }
>  
> +void property_add_marker(struct property *prop,
> +			 enum markertype type, unsigned int offset, char *ref)
> +{
> +	add_marker(&prop->val.markers, type, offset, ref);
> +}
> +
>  static void add_string_markers(struct property *prop, unsigned int offset, int len)
>  {
>  	int l;

-- 
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2025-08-21  4:31 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-20 13:11 [PATCH 0/6] Restore phandles from binary representations Uwe Kleine-König
2025-08-20 13:11 ` [PATCH 1/6] Emit /plugin/ when compiling to .dts with DTSF_PLUGIN set Uwe Kleine-König
2025-08-21  4:24   ` David Gibson
2025-08-20 13:11 ` [PATCH 2/6] Set DTSF_PLUGIN if needed when compiling from dtb Uwe Kleine-König
2025-08-21  4:24   ` David Gibson
2025-08-20 13:11 ` [PATCH 3/6] Improve type guessing when compiling to dts format Uwe Kleine-König
2025-08-21  4:24   ` David Gibson
2025-08-20 13:11 ` [PATCH 4/6] Restore labels from __symbols__ node Uwe Kleine-König
2025-08-21  4:25   ` David Gibson
2025-08-20 13:11 ` [PATCH 5/6] Restore phandle references from __local_fixups__ node Uwe Kleine-König
2025-08-21  4:23   ` David Gibson
2025-08-20 13:11 ` [PATCH 6/6] Restore phandle references from __fixups__ node Uwe Kleine-König
2025-08-21  4:31   ` David Gibson [this message]

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=aKahKWCQhFt_o0Jp@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=devicetree-compiler@vger.kernel.org \
    --cc=u.kleine-koenig@baylibre.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;
as well as URLs for NNTP newsgroup(s).