On 19/03/2019 03.30, Li Qiang wrote: > This is useful to write qtest about fw_cfg file entry. > > Signed-off-by: Li Qiang <liq...@163.com> > --- > tests/libqos/fw_cfg.c | 45 +++++++++++++++++++++++++++++++++++++++++++ > tests/libqos/fw_cfg.h | 2 ++ > 2 files changed, 47 insertions(+) > > diff --git a/tests/libqos/fw_cfg.c b/tests/libqos/fw_cfg.c > index d0889d1e22..2df33df859 100644 > --- a/tests/libqos/fw_cfg.c > +++ b/tests/libqos/fw_cfg.c > @@ -16,12 +16,57 @@ > #include "libqos/fw_cfg.h" > #include "libqtest.h" > #include "qemu/bswap.h" > +#include "hw/nvram/fw_cfg.h" > > void qfw_cfg_select(QFWCFG *fw_cfg, uint16_t key) > { > fw_cfg->select(fw_cfg, 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) > +{ > + uint32_t count; > + uint32_t i; > + unsigned char *filesbuf = NULL; > + size_t dsize; > + FWCfgFile *pdir_entry; > + size_t filesize = 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_malloc0(dsize); > + qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, filesbuf, dsize);
If I get the code right, qfw_cfg_get() fills the whole buffer here... in that case, g_malloc() should be sufficient, so you don't need g_malloc0() here. Thomas