devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: Yves-Alexis Perez <corsac@debian.org>,
	 devicetree-compiler@vger.kernel.org, entwicklung@pengutronix.de
Subject: Re: [PATCH v2] libfdt: overlay: ensure that existing phandles are not overwritten
Date: Thu, 22 Feb 2024 10:38:54 +0100	[thread overview]
Message-ID: <oitc63fe3agrg2vwgml6x652uo4dqst3m6kbaiu4rs3sbqnco4@rylqo655o4m5> (raw)
In-Reply-To: <ZdbqaoTUfBmSlC98@zatzit>

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

On Thu, Feb 22, 2024 at 05:32:10PM +1100, David Gibson wrote:
> On Fri, Feb 16, 2024 at 07:19:20PM +0100, Uwe Kleine-König wrote:
> > +static int overlay_update_node_conflicting_references(void *fdto, int tree_node,
> > +						      int fixup_node,
> > +						      uint32_t fdt_phandle,
> > +						      uint32_t fdto_phandle)
> > +{
> > +	int fixup_prop;
> > +	int fixup_child;
> > +	int ret;
> > +
> > +	fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
> > +
> > +		const fdt32_t *fixup_val;
> > +                const char *tree_val;
> > +                const char *name;
> > +                int fixup_len;
> > +                int tree_len;
> > +                int i;
> > +
> > +                fixup_val = fdt_getprop_by_offset(fdto, fixup_prop,
> > +                                                  &name, &fixup_len);
> > +                if (!fixup_val)
> > +                        return fixup_len;
> > +
> > +                if (fixup_len % sizeof(uint32_t))
> > +                        return -FDT_ERR_BADOVERLAY;
> > +                fixup_len /= sizeof(uint32_t);
> > +
> > +                tree_val = fdt_getprop(fdto, tree_node, name, &tree_len);
> > +                if (!tree_val) {
> > +                        if (tree_len == -FDT_ERR_NOTFOUND)
> > +                                return -FDT_ERR_BADOVERLAY;
> > +
> > +                        return tree_len;
> > +                }
> > +
> > +		for (i = 0; i < fixup_len; i++) {
> > +			fdt32_t adj_val;
> > +			uint32_t poffset;
> > +
> > +			poffset = fdt32_to_cpu(fixup_val[i]);
> 
> You can use fdt32_ld_() here to slightly simplify this

I don't see what you suggest here. fixup_val is assigned to in
fdt_getprop_by_offset() which I need to get fixup_len and then fixup_val
is already around. If you still think this could be improved, please be
more explicit.

> > +
> > +			/*
> > +			 * phandles to fixup can be unaligned.
> > +			 *
> > +			 * Use a memcpy for the architectures that do
> > +			 * not support unaligned accesses.
> > +			 */
> > +			memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
> 
> And here you can use fdt32_ld() which has unaligned access handling
> built in.
> 
> > +
> > +			if (fdt32_to_cpu(adj_val) == fdto_phandle) {
> > +
> > +				adj_val = cpu_to_fdt32(fdt_phandle);
> > +
> > +				ret = fdt_setprop_inplace_namelen_partial(fdto,
> > +									  tree_node,
> > +									  name,
> > +									  strlen(name),
> > +									  poffset,
> > +									  &adj_val,
> > +									  sizeof(adj_val));
> > +				if (ret == -FDT_ERR_NOSPACE)
> > +					return -FDT_ERR_BADOVERLAY;
> > +
> > +				if (ret)
> > +					return ret;
> 
> And here you can use fdt32_st() on (tree_val + poffset), avoiding
> quite some complexity.

There is some complexity because tree_val is a const char * (as this is
what fdt_getprop() returns) and fdt32_st() obviously doens't take a
const pointer.

If you want to play around with that, the code is mostly a copy of
overlay_update_local_node_references() which could benefit from your
suggestions in the same way.

Can I lure you in improving overlay_update_local_node_references()? Then
I could copy from the improved function for my patch. (Or maybe refactor
the function to take a function as parameter which is

	uint32_t delta;
	uint32_t increase_by_delta(uint32_t phandle)
	{
		return phandle + delta;
	}

for the overlay_update_local_node_references() case and

	uint32_t fdt_phandle;
	uint32_t fdto_phandle;
	uint32_t resolve_fdt_conflict(uint32_t phandle)
	{
		if (phandle == fdto_phandle)
			return fdt_phandle;
		else
			return phandle;
	}

for my function (maybe passing the data stored in global vars here in a
context parameter).

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

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

  parent reply	other threads:[~2024-02-22  9:39 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-16 18:19 [PATCH v2] libfdt: overlay: ensure that existing phandles are not overwritten Uwe Kleine-König
2024-02-16 21:06 ` Uwe Kleine-König
2024-02-22  6:33   ` David Gibson
2024-02-22  6:32 ` David Gibson
2024-02-22  7:22   ` Uwe Kleine-König
2024-02-22  9:02     ` David Gibson
2024-02-22  9:38   ` Uwe Kleine-König [this message]
2024-02-23  4:41     ` David Gibson
2024-02-23  7:53       ` Uwe Kleine-König
2024-02-23  8:15         ` David Gibson

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=oitc63fe3agrg2vwgml6x652uo4dqst3m6kbaiu4rs3sbqnco4@rylqo655o4m5 \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=corsac@debian.org \
    --cc=david@gibson.dropbear.id.au \
    --cc=devicetree-compiler@vger.kernel.org \
    --cc=entwicklung@pengutronix.de \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).