diff mbox series

[V4,1/3] hw/arm, loongarch: Move load_image_to_fw_cfg() to common location

Message ID 20220906090219.412517-2-sunilvl@ventanamicro.com (mailing list archive)
State New, archived
Headers show
Series hw/riscv: virt: Enable booting S-mode firmware from pflash | expand

Commit Message

Sunil V L Sept. 6, 2022, 9:02 a.m. UTC
load_image_to_fw_cfg() is duplicated by both arm and loongarch. The same
function will be required by riscv too. So, it's time to refactor and
move this function to a common path.

Signed-off-by: Sunil V L <sunilvl@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
---
 hw/arm/boot.c             | 49 ---------------------------------------
 hw/loongarch/virt.c       | 33 --------------------------
 hw/nvram/fw_cfg.c         | 32 +++++++++++++++++++++++++
 include/hw/nvram/fw_cfg.h | 21 +++++++++++++++++
 4 files changed, 53 insertions(+), 82 deletions(-)

Comments

Philippe Mathieu-Daudé Sept. 6, 2022, 1:32 p.m. UTC | #1
On 6/9/22 11:02, Sunil V L wrote:
> load_image_to_fw_cfg() is duplicated by both arm and loongarch. The same
> function will be required by riscv too. So, it's time to refactor and
> move this function to a common path.
> 
> Signed-off-by: Sunil V L <sunilvl@ventanamicro.com>
> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
> ---
>   hw/arm/boot.c             | 49 ---------------------------------------
>   hw/loongarch/virt.c       | 33 --------------------------
>   hw/nvram/fw_cfg.c         | 32 +++++++++++++++++++++++++
>   include/hw/nvram/fw_cfg.h | 21 +++++++++++++++++
>   4 files changed, 53 insertions(+), 82 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Alistair Francis Sept. 8, 2022, 9:17 a.m. UTC | #2
On Tue, Sep 6, 2022 at 11:38 AM Sunil V L <sunilvl@ventanamicro.com> wrote:
>
> load_image_to_fw_cfg() is duplicated by both arm and loongarch. The same
> function will be required by riscv too. So, it's time to refactor and
> move this function to a common path.
>
> Signed-off-by: Sunil V L <sunilvl@ventanamicro.com>
> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
>  hw/arm/boot.c             | 49 ---------------------------------------
>  hw/loongarch/virt.c       | 33 --------------------------
>  hw/nvram/fw_cfg.c         | 32 +++++++++++++++++++++++++
>  include/hw/nvram/fw_cfg.h | 21 +++++++++++++++++
>  4 files changed, 53 insertions(+), 82 deletions(-)
>
> diff --git a/hw/arm/boot.c b/hw/arm/boot.c
> index ada2717f76..704f368d9c 100644
> --- a/hw/arm/boot.c
> +++ b/hw/arm/boot.c
> @@ -818,55 +818,6 @@ static void do_cpu_reset(void *opaque)
>      }
>  }
>
> -/**
> - * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
> - *                          by key.
> - * @fw_cfg:         The firmware config instance to store the data in.
> - * @size_key:       The firmware config key to store the size of the loaded
> - *                  data under, with fw_cfg_add_i32().
> - * @data_key:       The firmware config key to store the loaded data under,
> - *                  with fw_cfg_add_bytes().
> - * @image_name:     The name of the image file to load. If it is NULL, the
> - *                  function returns without doing anything.
> - * @try_decompress: Whether the image should be decompressed (gunzipped) before
> - *                  adding it to fw_cfg. If decompression fails, the image is
> - *                  loaded as-is.
> - *
> - * In case of failure, the function prints an error message to stderr and the
> - * process exits with status 1.
> - */
> -static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
> -                                 uint16_t data_key, const char *image_name,
> -                                 bool try_decompress)
> -{
> -    size_t size = -1;
> -    uint8_t *data;
> -
> -    if (image_name == NULL) {
> -        return;
> -    }
> -
> -    if (try_decompress) {
> -        size = load_image_gzipped_buffer(image_name,
> -                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
> -    }
> -
> -    if (size == (size_t)-1) {
> -        gchar *contents;
> -        gsize length;
> -
> -        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
> -            error_report("failed to load \"%s\"", image_name);
> -            exit(1);
> -        }
> -        size = length;
> -        data = (uint8_t *)contents;
> -    }
> -
> -    fw_cfg_add_i32(fw_cfg, size_key, size);
> -    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
> -}
> -
>  static int do_arm_linux_init(Object *obj, void *opaque)
>  {
>      if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
> diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
> index 5cc0b05538..eee2c193c0 100644
> --- a/hw/loongarch/virt.c
> +++ b/hw/loongarch/virt.c
> @@ -542,39 +542,6 @@ static void reset_load_elf(void *opaque)
>      }
>  }
>
> -/* Load an image file into an fw_cfg entry identified by key. */
> -static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
> -                                 uint16_t data_key, const char *image_name,
> -                                 bool try_decompress)
> -{
> -    size_t size = -1;
> -    uint8_t *data;
> -
> -    if (image_name == NULL) {
> -        return;
> -    }
> -
> -    if (try_decompress) {
> -        size = load_image_gzipped_buffer(image_name,
> -                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
> -    }
> -
> -    if (size == (size_t)-1) {
> -        gchar *contents;
> -        gsize length;
> -
> -        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
> -            error_report("failed to load \"%s\"", image_name);
> -            exit(1);
> -        }
> -        size = length;
> -        data = (uint8_t *)contents;
> -    }
> -
> -    fw_cfg_add_i32(fw_cfg, size_key, size);
> -    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
> -}
> -
>  static void fw_cfg_add_kernel_info(FWCfgState *fw_cfg)
>  {
>      /*
> diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c
> index d605f3f45a..371a45dfe2 100644
> --- a/hw/nvram/fw_cfg.c
> +++ b/hw/nvram/fw_cfg.c
> @@ -41,6 +41,7 @@
>  #include "qapi/error.h"
>  #include "hw/acpi/aml-build.h"
>  #include "hw/pci/pci_bus.h"
> +#include "hw/loader.h"
>
>  #define FW_CFG_FILE_SLOTS_DFLT 0x20
>
> @@ -1221,6 +1222,37 @@ FWCfgState *fw_cfg_find(void)
>      return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL));
>  }
>
> +void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
> +                          uint16_t data_key, const char *image_name,
> +                          bool try_decompress)
> +{
> +    size_t size = -1;
> +    uint8_t *data;
> +
> +    if (image_name == NULL) {
> +        return;
> +    }
> +
> +    if (try_decompress) {
> +        size = load_image_gzipped_buffer(image_name,
> +                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
> +    }
> +
> +    if (size == (size_t)-1) {
> +        gchar *contents;
> +        gsize length;
> +
> +        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
> +            error_report("failed to load \"%s\"", image_name);
> +            exit(1);
> +        }
> +        size = length;
> +        data = (uint8_t *)contents;
> +    }
> +
> +    fw_cfg_add_i32(fw_cfg, size_key, size);
> +    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
> +}
>
>  static void fw_cfg_class_init(ObjectClass *klass, void *data)
>  {
> diff --git a/include/hw/nvram/fw_cfg.h b/include/hw/nvram/fw_cfg.h
> index 0e7a8bc7af..c1f81a5f13 100644
> --- a/include/hw/nvram/fw_cfg.h
> +++ b/include/hw/nvram/fw_cfg.h
> @@ -342,4 +342,25 @@ bool fw_cfg_dma_enabled(void *opaque);
>   */
>  const char *fw_cfg_arch_key_name(uint16_t key);
>
> +/**
> + * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
> + *                          by key.
> + * @fw_cfg:         The firmware config instance to store the data in.
> + * @size_key:       The firmware config key to store the size of the loaded
> + *                  data under, with fw_cfg_add_i32().
> + * @data_key:       The firmware config key to store the loaded data under,
> + *                  with fw_cfg_add_bytes().
> + * @image_name:     The name of the image file to load. If it is NULL, the
> + *                  function returns without doing anything.
> + * @try_decompress: Whether the image should be decompressed (gunzipped) before
> + *                  adding it to fw_cfg. If decompression fails, the image is
> + *                  loaded as-is.
> + *
> + * In case of failure, the function prints an error message to stderr and the
> + * process exits with status 1.
> + */
> +void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
> +                          uint16_t data_key, const char *image_name,
> +                          bool try_decompress);
> +
>  #endif
> --
> 2.25.1
>
>
Sunil V L Sept. 15, 2022, 11:59 a.m. UTC | #3
Hi,

Could maintainers of hw/arm and hw/loongarch to take a look at this? I
have addressed Peter's earlier comment.

Thanks
Sunil

On Thu, Sep 08, 2022 at 11:17:52AM +0200, Alistair Francis wrote:
> On Tue, Sep 6, 2022 at 11:38 AM Sunil V L <sunilvl@ventanamicro.com> wrote:
> >
> > load_image_to_fw_cfg() is duplicated by both arm and loongarch. The same
> > function will be required by riscv too. So, it's time to refactor and
> > move this function to a common path.
> >
> > Signed-off-by: Sunil V L <sunilvl@ventanamicro.com>
> > Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
> 
> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
> 
> Alistair
> 
> > ---
> >  hw/arm/boot.c             | 49 ---------------------------------------
> >  hw/loongarch/virt.c       | 33 --------------------------
> >  hw/nvram/fw_cfg.c         | 32 +++++++++++++++++++++++++
> >  include/hw/nvram/fw_cfg.h | 21 +++++++++++++++++
> >  4 files changed, 53 insertions(+), 82 deletions(-)
> >
> > diff --git a/hw/arm/boot.c b/hw/arm/boot.c
> > index ada2717f76..704f368d9c 100644
> > --- a/hw/arm/boot.c
> > +++ b/hw/arm/boot.c
> > @@ -818,55 +818,6 @@ static void do_cpu_reset(void *opaque)
> >      }
> >  }
> >
> > -/**
> > - * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
> > - *                          by key.
> > - * @fw_cfg:         The firmware config instance to store the data in.
> > - * @size_key:       The firmware config key to store the size of the loaded
> > - *                  data under, with fw_cfg_add_i32().
> > - * @data_key:       The firmware config key to store the loaded data under,
> > - *                  with fw_cfg_add_bytes().
> > - * @image_name:     The name of the image file to load. If it is NULL, the
> > - *                  function returns without doing anything.
> > - * @try_decompress: Whether the image should be decompressed (gunzipped) before
> > - *                  adding it to fw_cfg. If decompression fails, the image is
> > - *                  loaded as-is.
> > - *
> > - * In case of failure, the function prints an error message to stderr and the
> > - * process exits with status 1.
> > - */
> > -static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
> > -                                 uint16_t data_key, const char *image_name,
> > -                                 bool try_decompress)
> > -{
> > -    size_t size = -1;
> > -    uint8_t *data;
> > -
> > -    if (image_name == NULL) {
> > -        return;
> > -    }
> > -
> > -    if (try_decompress) {
> > -        size = load_image_gzipped_buffer(image_name,
> > -                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
> > -    }
> > -
> > -    if (size == (size_t)-1) {
> > -        gchar *contents;
> > -        gsize length;
> > -
> > -        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
> > -            error_report("failed to load \"%s\"", image_name);
> > -            exit(1);
> > -        }
> > -        size = length;
> > -        data = (uint8_t *)contents;
> > -    }
> > -
> > -    fw_cfg_add_i32(fw_cfg, size_key, size);
> > -    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
> > -}
> > -
> >  static int do_arm_linux_init(Object *obj, void *opaque)
> >  {
> >      if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
> > diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
> > index 5cc0b05538..eee2c193c0 100644
> > --- a/hw/loongarch/virt.c
> > +++ b/hw/loongarch/virt.c
> > @@ -542,39 +542,6 @@ static void reset_load_elf(void *opaque)
> >      }
> >  }
> >
> > -/* Load an image file into an fw_cfg entry identified by key. */
> > -static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
> > -                                 uint16_t data_key, const char *image_name,
> > -                                 bool try_decompress)
> > -{
> > -    size_t size = -1;
> > -    uint8_t *data;
> > -
> > -    if (image_name == NULL) {
> > -        return;
> > -    }
> > -
> > -    if (try_decompress) {
> > -        size = load_image_gzipped_buffer(image_name,
> > -                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
> > -    }
> > -
> > -    if (size == (size_t)-1) {
> > -        gchar *contents;
> > -        gsize length;
> > -
> > -        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
> > -            error_report("failed to load \"%s\"", image_name);
> > -            exit(1);
> > -        }
> > -        size = length;
> > -        data = (uint8_t *)contents;
> > -    }
> > -
> > -    fw_cfg_add_i32(fw_cfg, size_key, size);
> > -    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
> > -}
> > -
> >  static void fw_cfg_add_kernel_info(FWCfgState *fw_cfg)
> >  {
> >      /*
> > diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c
> > index d605f3f45a..371a45dfe2 100644
> > --- a/hw/nvram/fw_cfg.c
> > +++ b/hw/nvram/fw_cfg.c
> > @@ -41,6 +41,7 @@
> >  #include "qapi/error.h"
> >  #include "hw/acpi/aml-build.h"
> >  #include "hw/pci/pci_bus.h"
> > +#include "hw/loader.h"
> >
> >  #define FW_CFG_FILE_SLOTS_DFLT 0x20
> >
> > @@ -1221,6 +1222,37 @@ FWCfgState *fw_cfg_find(void)
> >      return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL));
> >  }
> >
> > +void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
> > +                          uint16_t data_key, const char *image_name,
> > +                          bool try_decompress)
> > +{
> > +    size_t size = -1;
> > +    uint8_t *data;
> > +
> > +    if (image_name == NULL) {
> > +        return;
> > +    }
> > +
> > +    if (try_decompress) {
> > +        size = load_image_gzipped_buffer(image_name,
> > +                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
> > +    }
> > +
> > +    if (size == (size_t)-1) {
> > +        gchar *contents;
> > +        gsize length;
> > +
> > +        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
> > +            error_report("failed to load \"%s\"", image_name);
> > +            exit(1);
> > +        }
> > +        size = length;
> > +        data = (uint8_t *)contents;
> > +    }
> > +
> > +    fw_cfg_add_i32(fw_cfg, size_key, size);
> > +    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
> > +}
> >
> >  static void fw_cfg_class_init(ObjectClass *klass, void *data)
> >  {
> > diff --git a/include/hw/nvram/fw_cfg.h b/include/hw/nvram/fw_cfg.h
> > index 0e7a8bc7af..c1f81a5f13 100644
> > --- a/include/hw/nvram/fw_cfg.h
> > +++ b/include/hw/nvram/fw_cfg.h
> > @@ -342,4 +342,25 @@ bool fw_cfg_dma_enabled(void *opaque);
> >   */
> >  const char *fw_cfg_arch_key_name(uint16_t key);
> >
> > +/**
> > + * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
> > + *                          by key.
> > + * @fw_cfg:         The firmware config instance to store the data in.
> > + * @size_key:       The firmware config key to store the size of the loaded
> > + *                  data under, with fw_cfg_add_i32().
> > + * @data_key:       The firmware config key to store the loaded data under,
> > + *                  with fw_cfg_add_bytes().
> > + * @image_name:     The name of the image file to load. If it is NULL, the
> > + *                  function returns without doing anything.
> > + * @try_decompress: Whether the image should be decompressed (gunzipped) before
> > + *                  adding it to fw_cfg. If decompression fails, the image is
> > + *                  loaded as-is.
> > + *
> > + * In case of failure, the function prints an error message to stderr and the
> > + * process exits with status 1.
> > + */
> > +void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
> > +                          uint16_t data_key, const char *image_name,
> > +                          bool try_decompress);
> > +
> >  #endif
> > --
> > 2.25.1
> >
> >
gaosong Sept. 15, 2022, 12:56 p.m. UTC | #4
在 2022/9/15 下午7:59, Sunil V L 写道:
> Hi,
>
> Could maintainers of hw/arm and hw/loongarch to take a look at this? I
> have addressed Peter's earlier comment.
>
> Thanks
> Sunil
>
> On Thu, Sep 08, 2022 at 11:17:52AM +0200, Alistair Francis wrote:
>> On Tue, Sep 6, 2022 at 11:38 AM Sunil V L <sunilvl@ventanamicro.com> wrote:
>>> load_image_to_fw_cfg() is duplicated by both arm and loongarch. The same
>>> function will be required by riscv too. So, it's time to refactor and
>>> move this function to a common path.
>>>
>>> Signed-off-by: Sunil V L <sunilvl@ventanamicro.com>
>>> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
>> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
>>
>> Alistair
Reviewed-by: Song Gao <gaosong@loongson.cn>

Thanks
Song Gao
>>> ---
>>>   hw/arm/boot.c             | 49 ---------------------------------------
>>>   hw/loongarch/virt.c       | 33 --------------------------
>>>   hw/nvram/fw_cfg.c         | 32 +++++++++++++++++++++++++
>>>   include/hw/nvram/fw_cfg.h | 21 +++++++++++++++++
>>>   4 files changed, 53 insertions(+), 82 deletions(-)
>>>
>>> diff --git a/hw/arm/boot.c b/hw/arm/boot.c
>>> index ada2717f76..704f368d9c 100644
>>> --- a/hw/arm/boot.c
>>> +++ b/hw/arm/boot.c
>>> @@ -818,55 +818,6 @@ static void do_cpu_reset(void *opaque)
>>>       }
>>>   }
>>>
>>> -/**
>>> - * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
>>> - *                          by key.
>>> - * @fw_cfg:         The firmware config instance to store the data in.
>>> - * @size_key:       The firmware config key to store the size of the loaded
>>> - *                  data under, with fw_cfg_add_i32().
>>> - * @data_key:       The firmware config key to store the loaded data under,
>>> - *                  with fw_cfg_add_bytes().
>>> - * @image_name:     The name of the image file to load. If it is NULL, the
>>> - *                  function returns without doing anything.
>>> - * @try_decompress: Whether the image should be decompressed (gunzipped) before
>>> - *                  adding it to fw_cfg. If decompression fails, the image is
>>> - *                  loaded as-is.
>>> - *
>>> - * In case of failure, the function prints an error message to stderr and the
>>> - * process exits with status 1.
>>> - */
>>> -static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
>>> -                                 uint16_t data_key, const char *image_name,
>>> -                                 bool try_decompress)
>>> -{
>>> -    size_t size = -1;
>>> -    uint8_t *data;
>>> -
>>> -    if (image_name == NULL) {
>>> -        return;
>>> -    }
>>> -
>>> -    if (try_decompress) {
>>> -        size = load_image_gzipped_buffer(image_name,
>>> -                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
>>> -    }
>>> -
>>> -    if (size == (size_t)-1) {
>>> -        gchar *contents;
>>> -        gsize length;
>>> -
>>> -        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
>>> -            error_report("failed to load \"%s\"", image_name);
>>> -            exit(1);
>>> -        }
>>> -        size = length;
>>> -        data = (uint8_t *)contents;
>>> -    }
>>> -
>>> -    fw_cfg_add_i32(fw_cfg, size_key, size);
>>> -    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
>>> -}
>>> -
>>>   static int do_arm_linux_init(Object *obj, void *opaque)
>>>   {
>>>       if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
>>> diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
>>> index 5cc0b05538..eee2c193c0 100644
>>> --- a/hw/loongarch/virt.c
>>> +++ b/hw/loongarch/virt.c
>>> @@ -542,39 +542,6 @@ static void reset_load_elf(void *opaque)
>>>       }
>>>   }
>>>
>>> -/* Load an image file into an fw_cfg entry identified by key. */
>>> -static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
>>> -                                 uint16_t data_key, const char *image_name,
>>> -                                 bool try_decompress)
>>> -{
>>> -    size_t size = -1;
>>> -    uint8_t *data;
>>> -
>>> -    if (image_name == NULL) {
>>> -        return;
>>> -    }
>>> -
>>> -    if (try_decompress) {
>>> -        size = load_image_gzipped_buffer(image_name,
>>> -                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
>>> -    }
>>> -
>>> -    if (size == (size_t)-1) {
>>> -        gchar *contents;
>>> -        gsize length;
>>> -
>>> -        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
>>> -            error_report("failed to load \"%s\"", image_name);
>>> -            exit(1);
>>> -        }
>>> -        size = length;
>>> -        data = (uint8_t *)contents;
>>> -    }
>>> -
>>> -    fw_cfg_add_i32(fw_cfg, size_key, size);
>>> -    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
>>> -}
>>> -
>>>   static void fw_cfg_add_kernel_info(FWCfgState *fw_cfg)
>>>   {
>>>       /*
>>> diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c
>>> index d605f3f45a..371a45dfe2 100644
>>> --- a/hw/nvram/fw_cfg.c
>>> +++ b/hw/nvram/fw_cfg.c
>>> @@ -41,6 +41,7 @@
>>>   #include "qapi/error.h"
>>>   #include "hw/acpi/aml-build.h"
>>>   #include "hw/pci/pci_bus.h"
>>> +#include "hw/loader.h"
>>>
>>>   #define FW_CFG_FILE_SLOTS_DFLT 0x20
>>>
>>> @@ -1221,6 +1222,37 @@ FWCfgState *fw_cfg_find(void)
>>>       return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL));
>>>   }
>>>
>>> +void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
>>> +                          uint16_t data_key, const char *image_name,
>>> +                          bool try_decompress)
>>> +{
>>> +    size_t size = -1;
>>> +    uint8_t *data;
>>> +
>>> +    if (image_name == NULL) {
>>> +        return;
>>> +    }
>>> +
>>> +    if (try_decompress) {
>>> +        size = load_image_gzipped_buffer(image_name,
>>> +                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
>>> +    }
>>> +
>>> +    if (size == (size_t)-1) {
>>> +        gchar *contents;
>>> +        gsize length;
>>> +
>>> +        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
>>> +            error_report("failed to load \"%s\"", image_name);
>>> +            exit(1);
>>> +        }
>>> +        size = length;
>>> +        data = (uint8_t *)contents;
>>> +    }
>>> +
>>> +    fw_cfg_add_i32(fw_cfg, size_key, size);
>>> +    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
>>> +}
>>>
>>>   static void fw_cfg_class_init(ObjectClass *klass, void *data)
>>>   {
>>> diff --git a/include/hw/nvram/fw_cfg.h b/include/hw/nvram/fw_cfg.h
>>> index 0e7a8bc7af..c1f81a5f13 100644
>>> --- a/include/hw/nvram/fw_cfg.h
>>> +++ b/include/hw/nvram/fw_cfg.h
>>> @@ -342,4 +342,25 @@ bool fw_cfg_dma_enabled(void *opaque);
>>>    */
>>>   const char *fw_cfg_arch_key_name(uint16_t key);
>>>
>>> +/**
>>> + * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
>>> + *                          by key.
>>> + * @fw_cfg:         The firmware config instance to store the data in.
>>> + * @size_key:       The firmware config key to store the size of the loaded
>>> + *                  data under, with fw_cfg_add_i32().
>>> + * @data_key:       The firmware config key to store the loaded data under,
>>> + *                  with fw_cfg_add_bytes().
>>> + * @image_name:     The name of the image file to load. If it is NULL, the
>>> + *                  function returns without doing anything.
>>> + * @try_decompress: Whether the image should be decompressed (gunzipped) before
>>> + *                  adding it to fw_cfg. If decompression fails, the image is
>>> + *                  loaded as-is.
>>> + *
>>> + * In case of failure, the function prints an error message to stderr and the
>>> + * process exits with status 1.
>>> + */
>>> +void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
>>> +                          uint16_t data_key, const char *image_name,
>>> +                          bool try_decompress);
>>> +
>>>   #endif
>>> --
>>> 2.25.1
>>>
>>>
Sunil V L Sept. 22, 2022, 11:28 a.m. UTC | #5
On Thu, Sep 15, 2022 at 08:56:01PM +0800, gaosong wrote:
> 
> 在 2022/9/15 下午7:59, Sunil V L 写道:
> > Hi,
> > 
> > Could maintainers of hw/arm and hw/loongarch to take a look at this? I
> > have addressed Peter's earlier comment.
> > 
> > Thanks
> > Sunil
> > 
> > On Thu, Sep 08, 2022 at 11:17:52AM +0200, Alistair Francis wrote:
> > > On Tue, Sep 6, 2022 at 11:38 AM Sunil V L <sunilvl@ventanamicro.com> wrote:
> > > > load_image_to_fw_cfg() is duplicated by both arm and loongarch. The same
> > > > function will be required by riscv too. So, it's time to refactor and
> > > > move this function to a common path.
> > > > 
> > > > Signed-off-by: Sunil V L <sunilvl@ventanamicro.com>
> > > > Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
> > > Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
> > > 
> > > Alistair
> Reviewed-by: Song Gao <gaosong@loongson.cn>
> 
> Thanks
> Song Gao
Thanks Song Gao.

Hi Peter,

Could you please provide RB or ACK if things are fine with this patch?

Thanks
Sunil
diff mbox series

Patch

diff --git a/hw/arm/boot.c b/hw/arm/boot.c
index ada2717f76..704f368d9c 100644
--- a/hw/arm/boot.c
+++ b/hw/arm/boot.c
@@ -818,55 +818,6 @@  static void do_cpu_reset(void *opaque)
     }
 }
 
-/**
- * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
- *                          by key.
- * @fw_cfg:         The firmware config instance to store the data in.
- * @size_key:       The firmware config key to store the size of the loaded
- *                  data under, with fw_cfg_add_i32().
- * @data_key:       The firmware config key to store the loaded data under,
- *                  with fw_cfg_add_bytes().
- * @image_name:     The name of the image file to load. If it is NULL, the
- *                  function returns without doing anything.
- * @try_decompress: Whether the image should be decompressed (gunzipped) before
- *                  adding it to fw_cfg. If decompression fails, the image is
- *                  loaded as-is.
- *
- * In case of failure, the function prints an error message to stderr and the
- * process exits with status 1.
- */
-static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
-                                 uint16_t data_key, const char *image_name,
-                                 bool try_decompress)
-{
-    size_t size = -1;
-    uint8_t *data;
-
-    if (image_name == NULL) {
-        return;
-    }
-
-    if (try_decompress) {
-        size = load_image_gzipped_buffer(image_name,
-                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
-    }
-
-    if (size == (size_t)-1) {
-        gchar *contents;
-        gsize length;
-
-        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
-            error_report("failed to load \"%s\"", image_name);
-            exit(1);
-        }
-        size = length;
-        data = (uint8_t *)contents;
-    }
-
-    fw_cfg_add_i32(fw_cfg, size_key, size);
-    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
-}
-
 static int do_arm_linux_init(Object *obj, void *opaque)
 {
     if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
index 5cc0b05538..eee2c193c0 100644
--- a/hw/loongarch/virt.c
+++ b/hw/loongarch/virt.c
@@ -542,39 +542,6 @@  static void reset_load_elf(void *opaque)
     }
 }
 
-/* Load an image file into an fw_cfg entry identified by key. */
-static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
-                                 uint16_t data_key, const char *image_name,
-                                 bool try_decompress)
-{
-    size_t size = -1;
-    uint8_t *data;
-
-    if (image_name == NULL) {
-        return;
-    }
-
-    if (try_decompress) {
-        size = load_image_gzipped_buffer(image_name,
-                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
-    }
-
-    if (size == (size_t)-1) {
-        gchar *contents;
-        gsize length;
-
-        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
-            error_report("failed to load \"%s\"", image_name);
-            exit(1);
-        }
-        size = length;
-        data = (uint8_t *)contents;
-    }
-
-    fw_cfg_add_i32(fw_cfg, size_key, size);
-    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
-}
-
 static void fw_cfg_add_kernel_info(FWCfgState *fw_cfg)
 {
     /*
diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c
index d605f3f45a..371a45dfe2 100644
--- a/hw/nvram/fw_cfg.c
+++ b/hw/nvram/fw_cfg.c
@@ -41,6 +41,7 @@ 
 #include "qapi/error.h"
 #include "hw/acpi/aml-build.h"
 #include "hw/pci/pci_bus.h"
+#include "hw/loader.h"
 
 #define FW_CFG_FILE_SLOTS_DFLT 0x20
 
@@ -1221,6 +1222,37 @@  FWCfgState *fw_cfg_find(void)
     return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL));
 }
 
+void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
+                          uint16_t data_key, const char *image_name,
+                          bool try_decompress)
+{
+    size_t size = -1;
+    uint8_t *data;
+
+    if (image_name == NULL) {
+        return;
+    }
+
+    if (try_decompress) {
+        size = load_image_gzipped_buffer(image_name,
+                                         LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
+    }
+
+    if (size == (size_t)-1) {
+        gchar *contents;
+        gsize length;
+
+        if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
+            error_report("failed to load \"%s\"", image_name);
+            exit(1);
+        }
+        size = length;
+        data = (uint8_t *)contents;
+    }
+
+    fw_cfg_add_i32(fw_cfg, size_key, size);
+    fw_cfg_add_bytes(fw_cfg, data_key, data, size);
+}
 
 static void fw_cfg_class_init(ObjectClass *klass, void *data)
 {
diff --git a/include/hw/nvram/fw_cfg.h b/include/hw/nvram/fw_cfg.h
index 0e7a8bc7af..c1f81a5f13 100644
--- a/include/hw/nvram/fw_cfg.h
+++ b/include/hw/nvram/fw_cfg.h
@@ -342,4 +342,25 @@  bool fw_cfg_dma_enabled(void *opaque);
  */
 const char *fw_cfg_arch_key_name(uint16_t key);
 
+/**
+ * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
+ *                          by key.
+ * @fw_cfg:         The firmware config instance to store the data in.
+ * @size_key:       The firmware config key to store the size of the loaded
+ *                  data under, with fw_cfg_add_i32().
+ * @data_key:       The firmware config key to store the loaded data under,
+ *                  with fw_cfg_add_bytes().
+ * @image_name:     The name of the image file to load. If it is NULL, the
+ *                  function returns without doing anything.
+ * @try_decompress: Whether the image should be decompressed (gunzipped) before
+ *                  adding it to fw_cfg. If decompression fails, the image is
+ *                  loaded as-is.
+ *
+ * In case of failure, the function prints an error message to stderr and the
+ * process exits with status 1.
+ */
+void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
+                          uint16_t data_key, const char *image_name,
+                          bool try_decompress);
+
 #endif