Eric Auger <eric.au...@linaro.org> writes:

> This function returns the host device tree blob from sysfs
> (/sys/firmware/devicetree/base).
>
> This has a runtime dependency on the dtc binary. This functionality
> is useful for platform device passthrough where the host device tree
> needs to be parsed to feed information into the guest device tree.
>
> Signed-off-by: Eric Auger <eric.au...@linaro.org>
> ---
>  device_tree.c                | 40 ++++++++++++++++++++++++++++++++++++++++
>  include/sysemu/device_tree.h |  1 +
>  2 files changed, 41 insertions(+)
>
> diff --git a/device_tree.c b/device_tree.c
> index a9f5f8e..58a5329 100644
> --- a/device_tree.c
> +++ b/device_tree.c
> @@ -117,6 +117,46 @@ fail:
>      return NULL;
>  }
>
> +/**
> + * load_device_tree_from_sysfs
> + *
> + * extract the dt blob from host sysfs
> + * this has a runtime dependency on the dtc binary
> + */
> +void *load_device_tree_from_sysfs(void)
> +{
> +    char cmd[] = "dtc -I fs -O dtb /sys/firmware/devicetree/base";
> +    FILE *pipe;
> +    void *fdt;
> +    int ret, actual_dt_size;
> +
> +    pipe = popen(cmd, "r");
> +    if (!pipe) {
> +        error_report("%s: Error when executing dtc", __func__);
> +        return NULL;
> +    }
> +    fdt = g_malloc0(FDT_MAX_SIZE);
> +    actual_dt_size = fread(fdt, 1, FDT_MAX_SIZE, pipe);
> +    pclose(pipe);

I think this looks OK but I'm wary of anything that calls out to a
external script. However it seems the other popen() case is for
migration and that looks as though it will get removed by the IO Channel
framework stuff.

There may be millage in using g_spawn_sync() as it will return you an
automatically sized buffer.

> +
> +    if (actual_dt_size == 0) {
> +        error_report("%s: could not copy host device tree in memory: %m",
> +                     __func__);
> +        goto fail;
> +    }
> +    ret = fdt_check_header(fdt);
> +    if (ret) {
> +        error_report("%s: Host dt file loaded into memory is invalid: %s",
> +                     __func__, fdt_strerror(ret));
> +        goto fail;
> +    }
> +    return fdt;
> +
> +fail:
> +    g_free(fdt);
> +    return NULL;
> +}
> +
>  static int findnode_nofail(void *fdt, const char *node_path)
>  {
>      int offset;
> diff --git a/include/sysemu/device_tree.h b/include/sysemu/device_tree.h
> index 359e143..307e53d 100644
> --- a/include/sysemu/device_tree.h
> +++ b/include/sysemu/device_tree.h
> @@ -16,6 +16,7 @@
>
>  void *create_device_tree(int *sizep);
>  void *load_device_tree(const char *filename_path, int *sizep);
> +void *load_device_tree_from_sysfs(void);
>
>  int qemu_fdt_setprop(void *fdt, const char *node_path,
>                       const char *property, const void *val, int size);


--
Alex Bennée

Reply via email to