On 9/26/19 6:00 AM, Philippe Mathieu-Daudé wrote:
> On 9/25/19 1:06 PM, Sam Eiderman wrote:
>> From: Sam Eiderman <shmuel.eider...@oracle.com>
>>
>> Add QTest tests to check the logical geometry override option.
>>
>> The tests in hd-geo-test are out of date - they only test IDE and do not
>> test interesting MBRs.
>>
>> I added a few helper functions which will make adding more tests easier.
>>
>> QTest's fw_cfg helper functions support only legacy fw_cfg, so I had to
>> read the new fw_cfg layout on my own.
>>
>> Creating qcow2 disks with specific size and MBR layout is currently
>> unused - we only use a default empty MBR.
>>
>> Reviewed-by: Karl Heubaum <karl.heub...@oracle.com>
>> Reviewed-by: Arbel Moshe <arbel.mo...@oracle.com>
>> Signed-off-by: Sam Eiderman <shmuel.eider...@oracle.com>
>> ---
>> tests/Makefile.include | 2 +-
>> tests/hd-geo-test.c | 589 +++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 590 insertions(+), 1 deletion(-)
>>
>> diff --git a/tests/Makefile.include b/tests/Makefile.include
>> index 479664f899..a5b92fea6a 100644
>> --- a/tests/Makefile.include
>> +++ b/tests/Makefile.include
>> @@ -780,7 +780,7 @@ tests/ide-test$(EXESUF): tests/ide-test.o
>> $(libqos-pc-obj-y)
>> tests/ahci-test$(EXESUF): tests/ahci-test.o $(libqos-pc-obj-y)
>> qemu-img$(EXESUF)
>> tests/ipmi-kcs-test$(EXESUF): tests/ipmi-kcs-test.o
>> tests/ipmi-bt-test$(EXESUF): tests/ipmi-bt-test.o
>> -tests/hd-geo-test$(EXESUF): tests/hd-geo-test.o
>> +tests/hd-geo-test$(EXESUF): tests/hd-geo-test.o $(libqos-obj-y)
>> tests/boot-order-test$(EXESUF): tests/boot-order-test.o $(libqos-obj-y)
>> tests/boot-serial-test$(EXESUF): tests/boot-serial-test.o $(libqos-obj-y)
>> tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \
>> diff --git a/tests/hd-geo-test.c b/tests/hd-geo-test.c
>> index 62eb624726..458de99c31 100644
>> --- a/tests/hd-geo-test.c
>> +++ b/tests/hd-geo-test.c
>> @@ -17,7 +17,12 @@
>>
>> #include "qemu/osdep.h"
>> #include "qemu-common.h"
>> +#include "qemu/bswap.h"
>> +#include "qapi/qmp/qlist.h"
>> #include "libqtest.h"
>> +#include "libqos/fw_cfg.h"
>> +#include "libqos/libqos.h"
>> +#include "standard-headers/linux/qemu_fw_cfg.h"
>>
>> #define ARGV_SIZE 256
>>
>> @@ -388,6 +393,575 @@ static void test_ide_drive_cd_0(void)
>> qtest_quit(qts);
>> }
>>
>> +typedef struct {
>> + bool active;
>> + uint32_t head;
>> + uint32_t sector;
>> + uint32_t cyl;
>> + uint32_t end_head;
>> + uint32_t end_sector;
>> + uint32_t end_cyl;
>> + uint32_t start_sect;
>> + uint32_t nr_sects;
>> +} MBRpartitions[4];
>> +
>> +static MBRpartitions empty_mbr = { {false, 0, 0, 0, 0, 0, 0, 0, 0},
>> + {false, 0, 0, 0, 0, 0, 0, 0, 0},
>> + {false, 0, 0, 0, 0, 0, 0, 0, 0},
>> + {false, 0, 0, 0, 0, 0, 0, 0, 0} };
>> +
>> +static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
>> +{
>> + const char *template = "/tmp/qtest.XXXXXX";
>> + char *raw_path = strdup(template);
>> + char *qcow2_path = strdup(template);
>> + char cmd[100 + 2 * PATH_MAX];
>> + uint8_t buf[512];
>> + int i, ret, fd, offset;
>> + uint64_t qcow2_size = sectors * 512;
>> + uint8_t status, parttype, head, sector, cyl;
>> + char *qemu_img_path;
>> + char *qemu_img_abs_path;
>> +
>> + offset = 0xbe;
>> +
>> + for (i = 0; i < 4; i++) {
>> + status = mbr[i].active ? 0x80 : 0x00;
>> + g_assert(mbr[i].head < 256);
>> + g_assert(mbr[i].sector < 64);
>> + g_assert(mbr[i].cyl < 1024);
>> + head = mbr[i].head;
>> + sector = mbr[i].sector + ((mbr[i].cyl & 0x300) >> 2);
>> + cyl = mbr[i].cyl & 0xff;
>> +
>> + buf[offset + 0x0] = status;
>> + buf[offset + 0x1] = head;
>> + buf[offset + 0x2] = sector;
>> + buf[offset + 0x3] = cyl;
>> +
>> + parttype = 0;
>> + g_assert(mbr[i].end_head < 256);
>> + g_assert(mbr[i].end_sector < 64);
>> + g_assert(mbr[i].end_cyl < 1024);
>> + head = mbr[i].end_head;
>> + sector = mbr[i].end_sector + ((mbr[i].end_cyl & 0x300) >> 2);
>> + cyl = mbr[i].end_cyl & 0xff;
>> +
>> + buf[offset + 0x4] = parttype;
>> + buf[offset + 0x5] = head;
>> + buf[offset + 0x6] = sector;
>> + buf[offset + 0x7] = cyl;
>> +
>> + (*(uint32_t *)&buf[offset + 0x8]) = cpu_to_le32(mbr[i].start_sect);
>> + (*(uint32_t *)&buf[offset + 0xc]) = cpu_to_le32(mbr[i].nr_sects);
>> +
>> + offset += 0x10;
>> + }
>> +
>> + fd = mkstemp(raw_path);
>> + g_assert(fd);
>> + close(fd);
>> +
>> + fd = open(raw_path, O_WRONLY);
>> + g_assert(fd >= 0);
>> + ret = write(fd, buf, sizeof(buf));
>> + g_assert(ret == sizeof(buf));
>> + close(fd);
>> +
>> + fd = mkstemp(qcow2_path);
>> + g_assert(fd);
>> + close(fd);
>> +
>> + qemu_img_path = getenv("QTEST_QEMU_IMG");
>> + g_assert(qemu_img_path);
>> + qemu_img_abs_path = realpath(qemu_img_path, NULL);
>> + g_assert(qemu_img_abs_path);
>> +
>> + ret = snprintf(cmd, sizeof(cmd),
>> + "%s convert -f raw -O qcow2 %s %s > /dev/null",
>> + qemu_img_abs_path,
>> + raw_path, qcow2_path);
>> + g_assert((0 < ret) && (ret <= sizeof(cmd)));
>> + ret = system(cmd);
>> + g_assert(ret == 0);
>> +
>> + ret = snprintf(cmd, sizeof(cmd),
>> + "%s resize %s %" PRIu64 " > /dev/null",
>> + qemu_img_abs_path,
>> + qcow2_path, qcow2_size);
>> + g_assert((0 < ret) && (ret <= sizeof(cmd)));
>> + ret = system(cmd);
>> + g_assert(ret == 0);
>> +
>> + free(qemu_img_abs_path);
>> +
>> + unlink(raw_path);
>> + free(raw_path);
>> +
>> + return qcow2_path;
>> +}
>> +
>> +struct QemuCfgFile {
>> + uint32_t size; /* file size */
>> + uint16_t select; /* write this to 0x510 to read it */
>> + uint16_t reserved;
>> + char name[56];
>> +};
>> +
>> +static uint16_t find_fw_cfg_file(QFWCFG *fw_cfg,
>> + const char *filename)
>> +{
>> + struct QemuCfgFile qfile;
>> + uint32_t count, e;
>> + uint16_t select;
>> +
>> + count = qfw_cfg_get_u32(fw_cfg, FW_CFG_FILE_DIR);
>> + count = be32_to_cpu(count);
>> + for (select = 0, e = 0; e < count; e++) {
>> + qfw_cfg_read_data(fw_cfg, &qfile, sizeof(qfile));
>> + if (!strcmp(filename, qfile.name)) {
>> + select = be16_to_cpu(qfile.select);
>> + }
>> + }
>> +
>> + return select;
>> +}
>> +
>> +static void read_fw_cfg_file(QFWCFG *fw_cfg,
>> + const char *filename,
>> + void *data,
>> + size_t len)
>> +{
>> + uint16_t select = find_fw_cfg_file(fw_cfg, filename);
>> +
>> + g_assert(select);
>> +
>> + qfw_cfg_get(fw_cfg, select, data, len);
>> +}
>> +
>> +#define BIOS_GEOMETRY_MAX_SIZE 10000
>> +
>> +typedef struct {
>> + uint32_t c;
>> + uint32_t h;
>> + uint32_t s;
>> +} CHS;
>> +
>> +typedef struct {
>> + const char *dev_path;
>> + CHS chs;
>> +} CHSResult;
>> +
>> +static void read_bootdevices(QFWCFG *fw_cfg, CHSResult expected[])
>> +{
>> + char *buf = g_malloc0(BIOS_GEOMETRY_MAX_SIZE);
>> + char *cur;
>> + GList *results = NULL, *cur_result;
>> + CHSResult *r;
>> + int i;
>> + int res;
>> + bool found;
>> +
>> + read_fw_cfg_file(fw_cfg, "bios-geometry", buf, BIOS_GEOMETRY_MAX_SIZE);
>
> Oh I'm glad to see the test I requested while reviewing the previous
> patch! I'll have a look at it, but John 589 LoC I doubt I can do it for
> Friday.
>
It's just a test, even so -- we can amend it. There's no real hurry.
--js