From: Ani Sinha <anisi...@redhat.com> 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 <anisi...@redhat.com> Reviewed-by: Fabiano Rosas <faro...@suse.de> Message-ID: <20250120043847.954881-2-anisi...@redhat.com> Signed-off-by: Fabiano Rosas <faro...@suse.de> --- tests/qtest/libqos/fw_cfg.c | 67 +++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/tests/qtest/libqos/fw_cfg.c b/tests/qtest/libqos/fw_cfg.c index 89f053ccac..22435873d9 100644 --- a/tests/qtest/libqos/fw_cfg.c +++ b/tests/qtest/libqos/fw_cfg.c @@ -60,27 +60,18 @@ static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key) qtest_writew(fw_cfg->qts, fw_cfg->base, key); } -/* - * The caller need check the return value. When the return value is - * nonzero, it means that some bytes have been transferred. - * - * If the fw_cfg file in question is smaller than the allocated & passed-in - * buffer, then the buffer has been populated only in part. - * - * If the fw_cfg file in question is larger than the passed-in - * buffer, then the return value explains how much room would have been - * necessary in total. And, while the caller's buffer has been fully - * populated, it has received only a starting slice of the fw_cfg file. - */ -size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename, - void *data, size_t buflen) +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; - uint32_t i; - unsigned char *filesbuf = NULL; size_t dsize; FWCfgFile *pdir_entry; - size_t filesize = 0; + 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); @@ -90,17 +81,43 @@ size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename, 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); + *size = be32_to_cpu(pdir_entry->size); + *sel = be16_to_cpu(pdir_entry->select); + found = true; break; } } - g_free(filesbuf); + + return found; +} + +/* + * The caller need check the return value. When the return value is + * nonzero, it means that some bytes have been transferred. + * + * If the fw_cfg file in question is smaller than the allocated & passed-in + * buffer, then the buffer has been populated only in part. + * + * If the fw_cfg file in question is larger than the passed-in + * buffer, then the return value explains how much room would have been + * necessary in total. And, while the caller's buffer has been fully + * populated, it has received only a starting slice of the fw_cfg file. + */ +size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename, + void *data, size_t buflen) +{ + size_t filesize = 0; + uint32_t len; + uint16_t sel; + + if (find_pdir_entry(fw_cfg, filename, &sel, &len)) { + filesize = len; + if (len > buflen) { + len = buflen; + } + qfw_cfg_get(fw_cfg, sel, data, len); + } + return filesize; } -- 2.35.3