[Skiboot] [PATCH 2/4] core/device.c: Sort nodes with name at unit names by unit

Jeremy Kerr jk at ozlabs.org
Mon Jan 18 12:16:29 AEDT 2016


Hi Oliver,

> When unflattening (or building from hdat) a device tree child nodes are added in the order in which they are encountered. For nodes that have a <basename>@<unit> style name with a common basename it is useful to have them in the tree sorted by the unit in ascending order. Currently this requires the source data to present them in sorted order, but this isn't always the case.
> 
> This patch modifies the node insertion process to insert new nodes in the correct location so the list of child nodes is sorted.

Can you wrap these?

> +static int cmp_subnodes(const struct dt_node *a, const struct dt_node *b)
> +{
> +	const char *a_unit = get_unitname(a);
> +	const char *b_unit = get_unitname(b);
> +
> +	ptrdiff_t basenamelen = a_unit - a->name;
> +
> +	/* sort hex unit addresses by number */
> +	if (a_unit && b_unit && !strncmp(a->name, b->name, basenamelen)) {
> +		unsigned long long a_num, b_num;
> +		char *a_end, *b_end;
> +
> +		a_num = strtoul(a_unit, &a_end, 16);
> +		b_num = strtoul(b_unit, &b_end, 16);
> +
> +		/* only compare if the unit addr parsed correctly */
> +		if (*a_end == 0 && *b_end == 0)
> +			return (a_num > b_num) - (a_num < b_num);
> +	}
> +
> +	return strcmp(a->name, b->name);
> +}

A note to maintainers: here we're comparing unit address *before* the
node name; this is what Anton's proposed patch to dtc does, so we keep
compatibility with that.

Acked-by: Jeremy Kerr <jk at ozlabs.org>

Cheers,


Jeremy


More information about the Skiboot mailing list