From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
To: Daniel Henrique Barboza <danielhb413@gmail.com>
Cc: qemu-devel@nongnu.org,
Alistair Francis <alistair.francis@wdc.com>,
David Gibson <david@gibson.dropbear.id.au>
Subject: Re: [PATCH for-7.2 10/10] hmp, device_tree.c: add 'info fdt <property>' support
Date: Mon, 25 Jul 2022 13:28:00 +0100 [thread overview]
Message-ID: <Yt6MULSaLESDs0Qt@work-vm> (raw)
In-Reply-To: <20220722200007.1602174-11-danielhb413@gmail.com>
* Daniel Henrique Barboza (danielhb413@gmail.com) wrote:
> 'info fdt' is only able to print full nodes so far. It would be good to
> be able to also print single properties, since ometimes we just want
> to verify a single value from the FDT.
>
> libfdt does not have support to find a property given its full path, but
> it does have a way to return a fdt_property given a prop name and its
> subnode.
>
> This is how we're going to support it:
>
> - given the same fullpath parameter, assume it's a node. If we have a
> match with an existing node, print it. If not, assume it's a property;
>
> - in fdt_find_property() we're going to split 'fullpath' into node and
> property. Unfortunately we can't use g_path_get_basename() to helps us
> because, although the device tree path format is similar to Linux, it'll
> not work when trying to run QEMU under Windows where the path format is
> different;
>
> - after spliiting into node + property, try to find the node in the FDT.
> If we have a match, use fdt_get_property() to retrieve fdt_property.
> Return it if found;
>
> - using the fdt_print_property() created previously, print the property.
Would it be easier to make 'info fdt' have an optional 2nd parameter
(hmp can do optionals) which if present is the property name, then these
would become:
> After this change, if an user wants to print just the value of 'cpu' inside
> /cpu/cpu-map(...) from an ARM FDT, we can do it:
>
> (qemu) info fdt /cpus/cpu-map/socket0/cluster0/core0/cpu
info fdt /cpus/cpu-map/socket0/cluster0/core0 cpu
> /cpus/cpu-map/socket0/cluster0/core0/cpu = <0x8001>
> (qemu)
>
> Or the 'ibm,my-dma-window' from the v-scsi device inside the pSeries
> FDT:
>
> (qemu) info fdt /vdevice/v-scsi@71000003/ibm,my-dma-window
info fdt /vdevice/v-scsi@71000003 ibm,my-dma-window
it seems more explicit, and seems easier to implement.
Dave
> /vdevice/v-scsi@71000003/ibm,my-dma-window = <0x71000003 0x0 0x0 0x0 0x10000000>
> (qemu)
>
> Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
> Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
> ---
> hmp-commands-info.hx | 2 +-
> softmmu/device_tree.c | 79 ++++++++++++++++++++++++++++++++++++++++---
> 2 files changed, 76 insertions(+), 5 deletions(-)
>
> diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
> index abf277be7d..8891c2918a 100644
> --- a/hmp-commands-info.hx
> +++ b/hmp-commands-info.hx
> @@ -913,7 +913,7 @@ ERST
> .name = "fdt",
> .args_type = "fullpath:s",
> .params = "fullpath",
> - .help = "show firmware device tree node given its full path",
> + .help = "show firmware device tree node or property given its full path",
> .cmd = hmp_info_fdt,
> },
>
> diff --git a/softmmu/device_tree.c b/softmmu/device_tree.c
> index e41894fbef..f6eb060acc 100644
> --- a/softmmu/device_tree.c
> +++ b/softmmu/device_tree.c
> @@ -774,9 +774,74 @@ static void fdt_print_node(int node, int depth, const char *fullpath)
> qemu_printf("%*s}\n", padding, "");
> }
>
> +static const struct fdt_property *fdt_find_property(const char *fullpath,
> + int *prop_size,
> + Error **errp)
> +{
> + const struct fdt_property *prop = NULL;
> + void *fdt = current_machine->fdt;
> + g_autoptr(GString) nodename = NULL;
> + const char *propname = NULL;
> + int path_len = strlen(fullpath);
> + int node = 0; /* default to root node '/' */
> + int i, idx = -1;
> +
> + /*
> + * We'll assume that we're dealing with a property. libfdt
> + * does not have an API to find a property given the full
> + * path, but it does have an API to find a property inside
> + * a node.
> + */
> + nodename = g_string_new("");
> +
> + for (i = path_len - 1; i >= 0; i--) {
> + if (fullpath[i] == '/') {
> + idx = i;
> + break;
> + }
> + }
> +
> + if (idx == -1) {
> + error_setg(errp, "FDT paths must contain at least one '/' character");
> + return NULL;
> + }
> +
> + if (idx == path_len - 1) {
> + error_setg(errp, "FDT paths can't end with a '/' character");
> + return NULL;
> + }
> +
> + propname = &fullpath[idx + 1];
> +
> + if (idx != 0) {
> + g_string_append_len(nodename, fullpath, idx);
> +
> + node = fdt_path_offset(fdt, nodename->str);
> + if (node < 0) {
> + error_setg(errp, "node '%s' of property '%s' not found in FDT",
> + nodename->str, propname);
> + return NULL;
> + }
> + } else {
> + /* idx = 0 means that it's a property of the root node */
> + g_string_append(nodename, "/");
> + }
> +
> + prop = fdt_get_property(fdt, node, propname, prop_size);
> + if (!prop) {
> + error_setg(errp, "property '%s' not found in node '%s' in FDT",
> + propname, nodename->str);
> + return NULL;
> + }
> +
> + return prop;
> +}
> +
> void fdt_info(const char *fullpath, Error **errp)
> {
> - int node;
> + const struct fdt_property *prop = NULL;
> + Error *local_err = NULL;
> + int node, prop_size;
>
> if (!current_machine->fdt) {
> error_setg(errp, "Unable to find the machine FDT");
> @@ -784,10 +849,16 @@ void fdt_info(const char *fullpath, Error **errp)
> }
>
> node = fdt_path_offset(current_machine->fdt, fullpath);
> - if (node < 0) {
> - error_setg(errp, "node '%s' not found in FDT", fullpath);
> + if (node >= 0) {
> + fdt_print_node(node, 0, fullpath);
> + return;
> + }
> +
> + prop = fdt_find_property(fullpath, &prop_size, &local_err);
> + if (local_err) {
> + error_propagate(errp, local_err);
> return;
> }
>
> - fdt_print_node(node, 0, fullpath);
> + fdt_print_property(fullpath, prop->data, prop_size, 0);
> }
> --
> 2.36.1
>
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
next prev parent reply other threads:[~2022-07-25 12:48 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-22 19:59 [PATCH for-7.2 00/10] add hmp 'save-fdt' and 'info fdt' commands Daniel Henrique Barboza
2022-07-22 19:59 ` [PATCH for-7.2 01/10] hw/arm/boot.c: do not free machine->fdt in arm_load_dtb() Daniel Henrique Barboza
2022-07-22 23:09 ` BALATON Zoltan
2022-07-22 19:59 ` [PATCH for-7.2 02/10] hw/ppc/pegasos2.c: set machine->fdt in machine_reset() Daniel Henrique Barboza
2022-07-22 23:11 ` BALATON Zoltan
2022-07-22 20:00 ` [PATCH for-7.2 03/10] hw/ppc: set machine->fdt in spapr machine Daniel Henrique Barboza
2022-07-22 20:00 ` [PATCH for-7.2 04/10] hmp, device_tree.c: introduce fdt-save Daniel Henrique Barboza
2022-07-22 23:13 ` BALATON Zoltan
2022-07-25 13:17 ` Daniel Henrique Barboza
2022-07-25 12:12 ` Dr. David Alan Gilbert
2022-07-22 20:00 ` [PATCH for-7.2 05/10] hmp, device_tree.c: introduce 'info fdt' command Daniel Henrique Barboza
2022-07-22 20:00 ` [PATCH for-7.2 06/10] device_tree.c: support printing of strings props Daniel Henrique Barboza
2022-07-22 20:00 ` [PATCH for-7.2 07/10] device_tree.c: support remaining FDT prop types Daniel Henrique Barboza
2022-07-22 20:00 ` [PATCH for-7.2 08/10] device_node.c: enable 'info fdt' to print subnodes Daniel Henrique Barboza
2022-07-22 20:00 ` [PATCH for-7.2 09/10] device_tree.c: add fdt_print_property() helper Daniel Henrique Barboza
2022-07-22 20:00 ` [PATCH for-7.2 10/10] hmp, device_tree.c: add 'info fdt <property>' support Daniel Henrique Barboza
2022-07-25 12:28 ` Dr. David Alan Gilbert [this message]
2022-07-22 23:16 ` [PATCH for-7.2 00/10] add hmp 'save-fdt' and 'info fdt' commands BALATON Zoltan
2022-07-25 9:11 ` Daniel P. Berrangé
2022-07-25 13:16 ` Daniel Henrique Barboza
2022-07-25 14:05 ` Daniel P. Berrangé
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=Yt6MULSaLESDs0Qt@work-vm \
--to=dgilbert@redhat.com \
--cc=alistair.francis@wdc.com \
--cc=danielhb413@gmail.com \
--cc=david@gibson.dropbear.id.au \
--cc=qemu-devel@nongnu.org \
/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).