diff mbox series

[1/2] tests: fw_cfg: add a function to get the fw_cfg file entry

Message ID 20181028124004.30494-2-liq3ea@163.com (mailing list archive)
State New, archived
Headers show
Series test: fw_cfg: add reboot-timeout test case | expand

Commit Message

Li Qiang Oct. 28, 2018, 12:40 p.m. UTC
This is useful to write qtest abount fw_cfg file entry.

Signed-off-by: Li Qiang <liq3ea@163.com>
---
 tests/libqos/fw_cfg.c | 30 ++++++++++++++++++++++++++++++
 tests/libqos/fw_cfg.h |  2 ++
 2 files changed, 32 insertions(+)

Comments

Philippe Mathieu-Daudé Oct. 29, 2018, 11:46 p.m. UTC | #1
Hi Li,

On 28/10/18 13:40, Li Qiang wrote:
> This is useful to write qtest abount fw_cfg file entry.
> 
> Signed-off-by: Li Qiang <liq3ea@163.com>
> ---
>   tests/libqos/fw_cfg.c | 30 ++++++++++++++++++++++++++++++
>   tests/libqos/fw_cfg.h |  2 ++
>   2 files changed, 32 insertions(+)
> 
> diff --git a/tests/libqos/fw_cfg.c b/tests/libqos/fw_cfg.c
> index d0889d1e22..2dd7a498ab 100644
> --- a/tests/libqos/fw_cfg.c
> +++ b/tests/libqos/fw_cfg.c
> @@ -16,6 +16,7 @@
>   #include "libqos/fw_cfg.h"
>   #include "libqtest.h"
>   #include "qemu/bswap.h"
> +#include "standard-headers/linux/qemu_fw_cfg.h"
>   
>   void qfw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
>   {
> @@ -54,6 +55,35 @@ uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key)
>       return le64_to_cpu(value);
>   }
>   

Can this return size_t the size of the file?
So qfw_cfg_get_file(..., buflen=0) returns the file size.

> +void 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;
> +    uint32_t dsize;
> +    struct fw_cfg_file *p;

        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);
> +    p = (struct fw_cfg_file *)(filesbuf + 4);
> +    for (i = 0; i < count; ++i, ++p) {
> +        if (!strcmp(p->name, filename)) {
> +            uint32_t len = be32_to_cpu(p->size);


> +            uint16_t sel = be16_to_cpu(p->select);
> +            void *buf = g_malloc0(len);

Why call malloc() here?

> +            qfw_cfg_get(fw_cfg, sel, buf, len);

And not copy directly to 'data':

                filesize = len;
                if (len > buflen) {
                    len = buflen;
                }
                qfw_cfg_get(fw_cfg, sel, data, len);

> +            memcpy(data, buf, buflen);
> +            g_free(buf);

Dropping 2 previous lines.

> +            break;
> +        }
> +    }
> +    g_free(filesbuf);

        return filesize;

> +}
> +
>   static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
>   {
>       qtest_writew(fw_cfg->qts, fw_cfg->base, key);
> diff --git a/tests/libqos/fw_cfg.h b/tests/libqos/fw_cfg.h
> index 0353416af0..50e4227638 100644
> --- a/tests/libqos/fw_cfg.h
> +++ b/tests/libqos/fw_cfg.h
> @@ -31,6 +31,8 @@ void qfw_cfg_get(QFWCFG *fw_cfg, uint16_t key, void *data, size_t len);
>   uint16_t qfw_cfg_get_u16(QFWCFG *fw_cfg, uint16_t key);
>   uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key);
>   uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key);
> +void qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename,
> +                      void *data, size_t buflen);
>   
>   QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base);
>   QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base);
>
li qiang Oct. 30, 2018, 1:45 a.m. UTC | #2
Hello Philippe,


Thanks for your review,

I will send v2 later.


Thanks.

Li Qiang

在 2018/10/30 7:46, Philippe Mathieu-Daudé 写道:
> Hi Li,
>
> On 28/10/18 13:40, Li Qiang wrote:
>> This is useful to write qtest abount fw_cfg file entry.
>>
>> Signed-off-by: Li Qiang <liq3ea@163.com>
>> ---
>>   tests/libqos/fw_cfg.c | 30 ++++++++++++++++++++++++++++++
>>   tests/libqos/fw_cfg.h |  2 ++
>>   2 files changed, 32 insertions(+)
>>
>> diff --git a/tests/libqos/fw_cfg.c b/tests/libqos/fw_cfg.c
>> index d0889d1e22..2dd7a498ab 100644
>> --- a/tests/libqos/fw_cfg.c
>> +++ b/tests/libqos/fw_cfg.c
>> @@ -16,6 +16,7 @@
>>   #include "libqos/fw_cfg.h"
>>   #include "libqtest.h"
>>   #include "qemu/bswap.h"
>> +#include "standard-headers/linux/qemu_fw_cfg.h"
>>     void qfw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
>>   {
>> @@ -54,6 +55,35 @@ uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t 
>> key)
>>       return le64_to_cpu(value);
>>   }
>
> Can this return size_t the size of the file?
> So qfw_cfg_get_file(..., buflen=0) returns the file size.
>
>> +void 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;
>> +    uint32_t dsize;
>> +    struct fw_cfg_file *p;
>
>        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);
>> +    p = (struct fw_cfg_file *)(filesbuf + 4);
>> +    for (i = 0; i < count; ++i, ++p) {
>> +        if (!strcmp(p->name, filename)) {
>> +            uint32_t len = be32_to_cpu(p->size);
>
>
>> +            uint16_t sel = be16_to_cpu(p->select);
>> +            void *buf = g_malloc0(len);
>
> Why call malloc() here?
>
>> +            qfw_cfg_get(fw_cfg, sel, buf, len);
>
> And not copy directly to 'data':
>
>                filesize = len;
>                if (len > buflen) {
>                    len = buflen;
>                }
>                qfw_cfg_get(fw_cfg, sel, data, len);
>
>> +            memcpy(data, buf, buflen);
>> +            g_free(buf);
>
> Dropping 2 previous lines.
>
>> +            break;
>> +        }
>> +    }
>> +    g_free(filesbuf);
>
>        return filesize;
>
>> +}
>> +
>>   static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
>>   {
>>       qtest_writew(fw_cfg->qts, fw_cfg->base, key);
>> diff --git a/tests/libqos/fw_cfg.h b/tests/libqos/fw_cfg.h
>> index 0353416af0..50e4227638 100644
>> --- a/tests/libqos/fw_cfg.h
>> +++ b/tests/libqos/fw_cfg.h
>> @@ -31,6 +31,8 @@ void qfw_cfg_get(QFWCFG *fw_cfg, uint16_t key, void 
>> *data, size_t len);
>>   uint16_t qfw_cfg_get_u16(QFWCFG *fw_cfg, uint16_t key);
>>   uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key);
>>   uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key);
>> +void qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename,
>> +                      void *data, size_t buflen);
>>     QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base);
>>   QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base);
>>
>
diff mbox series

Patch

diff --git a/tests/libqos/fw_cfg.c b/tests/libqos/fw_cfg.c
index d0889d1e22..2dd7a498ab 100644
--- a/tests/libqos/fw_cfg.c
+++ b/tests/libqos/fw_cfg.c
@@ -16,6 +16,7 @@ 
 #include "libqos/fw_cfg.h"
 #include "libqtest.h"
 #include "qemu/bswap.h"
+#include "standard-headers/linux/qemu_fw_cfg.h"
 
 void qfw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
 {
@@ -54,6 +55,35 @@  uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key)
     return le64_to_cpu(value);
 }
 
+void 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;
+    uint32_t dsize;
+    struct fw_cfg_file *p;
+
+    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);
+    p = (struct fw_cfg_file *)(filesbuf + 4);
+    for (i = 0; i < count; ++i, ++p) {
+        if (!strcmp(p->name, filename)) {
+            uint32_t len = be32_to_cpu(p->size);
+            uint16_t sel = be16_to_cpu(p->select);
+            void *buf = g_malloc0(len);
+            qfw_cfg_get(fw_cfg, sel, buf, len);
+            memcpy(data, buf, buflen);
+            g_free(buf);
+            break;
+        }
+    }
+    g_free(filesbuf);
+}
+
 static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key)
 {
     qtest_writew(fw_cfg->qts, fw_cfg->base, key);
diff --git a/tests/libqos/fw_cfg.h b/tests/libqos/fw_cfg.h
index 0353416af0..50e4227638 100644
--- a/tests/libqos/fw_cfg.h
+++ b/tests/libqos/fw_cfg.h
@@ -31,6 +31,8 @@  void qfw_cfg_get(QFWCFG *fw_cfg, uint16_t key, void *data, size_t len);
 uint16_t qfw_cfg_get_u16(QFWCFG *fw_cfg, uint16_t key);
 uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key);
 uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key);
+void qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename,
+                      void *data, size_t buflen);
 
 QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base);
 QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base);