All of lore.kernel.org
 help / color / mirror / Atom feed
From: Fabiano Rosas <farosas@suse.de>
To: "Ani Sinha" <anisinha@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Laurent Vivier" <lvivier@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>
Cc: berrange@redhat.com, armbru@redhat.com,
	Ani Sinha <anisinha@redhat.com>,
	qemu-devel@nongnu.org
Subject: Re: [PATCH v4 1/2] libqos/fw_cfg: refactor file directory iteraton to make it more reusable
Date: Thu, 09 Jan 2025 17:01:05 -0300	[thread overview]
Message-ID: <87ldvjeozi.fsf@suse.de> (raw)
In-Reply-To: <20250109074929.252339-2-anisinha@redhat.com>

Ani Sinha <anisinha@redhat.com> writes:

> fw-cfg file directory iteration code can be used by other functions that may
> want to implement fw-cfg file operations. Refactor it into a smaller helper
> so that it can be reused.
>
> No functional change.
>
> Signed-off-by: Ani Sinha <anisinha@redhat.com>
> ---
>  tests/qtest/libqos/fw_cfg.c | 63 ++++++++++++++++++++++++-------------
>  1 file changed, 41 insertions(+), 22 deletions(-)
>
> diff --git a/tests/qtest/libqos/fw_cfg.c b/tests/qtest/libqos/fw_cfg.c
> index 89f053ccac..f1ed4898f7 100644
> --- a/tests/qtest/libqos/fw_cfg.c
> +++ b/tests/qtest/libqos/fw_cfg.c
> @@ -60,6 +60,39 @@ static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
>      qtest_writew(fw_cfg->qts, fw_cfg->base, key);
>  }
>  
> +static bool
> +find_pdir_entry(QFWCFG *fw_cfg, const char *filename,
> +                uint16_t *sel, uint32_t *size)
> +{
> +    g_autofree unsigned char *filesbuf = NULL;
> +    uint32_t count;
> +    size_t dsize;
> +    FWCfgFile *pdir_entry;
> +    uint32_t i;
> +    bool found = false;
> +
> +    *size = 0;
> +    *sel = 0;
> +
> +    qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, &count, sizeof(count));
> +    count = be32_to_cpu(count);
> +    dsize = sizeof(uint32_t) + count * sizeof(struct fw_cfg_file);
> +    filesbuf = g_malloc(dsize);
> +    g_assert(filesbuf);

g_malloc already aborts the program if the allocation fails.

> +    qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, filesbuf, dsize);
> +    pdir_entry = (FWCfgFile *)(filesbuf + sizeof(uint32_t));
> +    for (i = 0; i < count; ++i, ++pdir_entry) {
> +        if (!strcmp(pdir_entry->name, filename)) {
> +            *size = be32_to_cpu(pdir_entry->size);
> +            *sel = be16_to_cpu(pdir_entry->select);
> +            found = true;
> +            break;
> +        }
> +    }
> +
> +    return found;
> +}
> +
>  /*
>   * The caller need check the return value. When the return value is
>   * nonzero, it means that some bytes have been transferred.
> @@ -75,32 +108,18 @@ static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
>  size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename,
>                        void *data, size_t buflen)
>  {
> -    uint32_t count;
> -    uint32_t i;
> -    unsigned char *filesbuf = NULL;
> -    size_t dsize;
> -    FWCfgFile *pdir_entry;
>      size_t filesize = 0;
> +    uint32_t len;
> +    uint16_t sel;
>  
> -    qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, &count, sizeof(count));
> -    count = be32_to_cpu(count);
> -    dsize = sizeof(uint32_t) + count * sizeof(struct fw_cfg_file);
> -    filesbuf = g_malloc(dsize);
> -    qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, filesbuf, dsize);
> -    pdir_entry = (FWCfgFile *)(filesbuf + sizeof(uint32_t));
> -    for (i = 0; i < count; ++i, ++pdir_entry) {
> -        if (!strcmp(pdir_entry->name, filename)) {
> -            uint32_t len = be32_to_cpu(pdir_entry->size);
> -            uint16_t sel = be16_to_cpu(pdir_entry->select);
> -            filesize = len;
> -            if (len > buflen) {
> -                len = buflen;
> -            }
> -            qfw_cfg_get(fw_cfg, sel, data, len);
> -            break;
> +    if (find_pdir_entry(fw_cfg, filename, &sel, &len)) {
> +        filesize = len;
> +        if (len > buflen) {
> +            len = buflen;
>          }
> +        qfw_cfg_get(fw_cfg, sel, data, len);
>      }
> -    g_free(filesbuf);
> +
>      return filesize;
>  }


  reply	other threads:[~2025-01-09 20:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-09  7:49 [PATCH v4 0/2] tests/qtest/libqos: add DMA support for writing and reading fw_cfg files Ani Sinha
2025-01-09  7:49 ` [PATCH v4 1/2] libqos/fw_cfg: refactor file directory iteraton to make it more reusable Ani Sinha
2025-01-09 20:01   ` Fabiano Rosas [this message]
2025-01-10  3:43     ` Ani Sinha
2025-01-09  7:49 ` [PATCH v4 2/2] tests/qtest/libqos: add DMA support for writing and reading fw_cfg files Ani Sinha
2025-01-09 20:30   ` Fabiano Rosas
2025-01-10  5:23     ` Ani Sinha
2025-01-10 10:12       ` Gerd Hoffmann
2025-01-10  6:43     ` Philippe Mathieu-Daudé

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=87ldvjeozi.fsf@suse.de \
    --to=farosas@suse.de \
    --cc=anisinha@redhat.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.