qemu/tests/qtest/acpi-utils.c
<<
>>
Prefs
   1/*
   2 * ACPI Utility Functions
   3 *
   4 * Copyright (c) 2013 Red Hat Inc.
   5 * Copyright (c) 2017 Skyport Systems
   6 *
   7 * Authors:
   8 *  Michael S. Tsirkin <mst@redhat.com>,
   9 *  Ben Warren <ben@skyportsystems.com>
  10 *
  11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12 * See the COPYING file in the top-level directory.
  13 */
  14
  15#include "qemu/osdep.h"
  16#include <glib/gstdio.h>
  17#include "qemu/bitmap.h"
  18#include "acpi-utils.h"
  19#include "boot-sector.h"
  20
  21uint8_t acpi_calc_checksum(const uint8_t *data, int len)
  22{
  23    int i;
  24    uint8_t sum = 0;
  25
  26    for (i = 0; i < len; i++) {
  27        sum += data[i];
  28    }
  29
  30    return sum;
  31}
  32
  33uint32_t acpi_find_rsdp_address(QTestState *qts)
  34{
  35    uint32_t off;
  36
  37    /* RSDP location can vary across a narrow range */
  38    for (off = 0xf0000; off < 0x100000; off += 0x10) {
  39        uint8_t sig[] = "RSD PTR ";
  40        int i;
  41
  42        for (i = 0; i < sizeof sig - 1; ++i) {
  43            sig[i] = qtest_readb(qts, off + i);
  44        }
  45
  46        if (!memcmp(sig, "RSD PTR ", sizeof sig)) {
  47            break;
  48        }
  49    }
  50    return off;
  51}
  52
  53void acpi_fetch_rsdp_table(QTestState *qts, uint64_t addr, uint8_t *rsdp_table)
  54{
  55    uint8_t revision;
  56
  57    /* Read mandatory revision 0 table data (20 bytes) first */
  58    qtest_memread(qts, addr, rsdp_table, 20);
  59    revision = rsdp_table[15 /* Revision offset */];
  60
  61    switch (revision) {
  62    case 0: /* ACPI 1.0 RSDP */
  63        break;
  64    case 2: /* ACPI 2.0+ RSDP */
  65        /* Read the rest of the RSDP table */
  66        qtest_memread(qts, addr + 20, rsdp_table + 20, 16);
  67        break;
  68    default:
  69        g_assert_not_reached();
  70    }
  71
  72    ACPI_ASSERT_CMP64(*((uint64_t *)(rsdp_table)), "RSD PTR ");
  73}
  74
  75/** acpi_fetch_table
  76 *  load ACPI table at @addr_ptr offset pointer into buffer and return it in
  77 *  @aml, its length in @aml_len and check that signature/checksum matches
  78 *  actual one.
  79 */
  80void acpi_fetch_table(QTestState *qts, uint8_t **aml, uint32_t *aml_len,
  81                      const uint8_t *addr_ptr, int addr_size, const char *sig,
  82                      bool verify_checksum)
  83{
  84    uint32_t len;
  85    uint64_t addr = 0;
  86
  87    g_assert(addr_size == 4 || addr_size == 8);
  88    memcpy(&addr, addr_ptr , addr_size);
  89    addr = le64_to_cpu(addr);
  90    qtest_memread(qts, addr + 4, &len, 4); /* Length of ACPI table */
  91    *aml_len = le32_to_cpu(len);
  92    *aml = g_malloc0(*aml_len);
  93    /* get whole table */
  94    qtest_memread(qts, addr, *aml, *aml_len);
  95
  96    if (sig) {
  97        ACPI_ASSERT_CMP(**aml, sig);
  98    }
  99    if (verify_checksum) {
 100        if (acpi_calc_checksum(*aml, *aml_len)) {
 101            gint fd, ret;
 102            char *fname = NULL;
 103            GError *error = NULL;
 104
 105            fprintf(stderr, "Invalid '%.4s'(%d)\n", *aml, *aml_len);
 106            fd = g_file_open_tmp("malformed-XXXXXX.dat", &fname, &error);
 107            g_assert_no_error(error);
 108            fprintf(stderr, "Dumping invalid table into '%s'\n", fname);
 109            ret = qemu_write_full(fd, *aml, *aml_len);
 110            g_assert(ret == *aml_len);
 111            close(fd);
 112            g_free(fname);
 113        }
 114        g_assert(!acpi_calc_checksum(*aml, *aml_len));
 115    }
 116}
 117
 118#define GUID_SIZE 16
 119static const uint8_t AcpiTestSupportGuid[GUID_SIZE] = {
 120       0xb1, 0xa6, 0x87, 0xab,
 121       0x34, 0x20,
 122       0xa0, 0xbd,
 123       0x71, 0xbd, 0x37, 0x50, 0x07, 0x75, 0x77, 0x85 };
 124
 125typedef struct {
 126    uint8_t signature_guid[GUID_SIZE];
 127    uint64_t rsdp10;
 128    uint64_t rsdp20;
 129} __attribute__((packed)) UefiTestSupport;
 130
 131/* Wait at most 600 seconds (test is slow with TCG and --enable-debug) */
 132#define TEST_DELAY (1 * G_USEC_PER_SEC / 10)
 133#define TEST_CYCLES MAX((600 * G_USEC_PER_SEC / TEST_DELAY), 1)
 134#define MB 0x100000ULL
 135uint64_t acpi_find_rsdp_address_uefi(QTestState *qts, uint64_t start,
 136                                     uint64_t size)
 137{
 138    int i, j;
 139    uint8_t data[GUID_SIZE];
 140
 141    for (i = 0; i < TEST_CYCLES; ++i) {
 142        for (j = 0; j < size / MB; j++) {
 143            /* look for GUID at every 1Mb block */
 144            uint64_t addr = start + j * MB;
 145
 146            qtest_memread(qts, addr, data, sizeof(data));
 147            if (!memcmp(AcpiTestSupportGuid, data, sizeof(data))) {
 148                UefiTestSupport ret;
 149
 150                qtest_memread(qts, addr, &ret, sizeof(ret));
 151                ret.rsdp10 = le64_to_cpu(ret.rsdp10);
 152                ret.rsdp20 = le64_to_cpu(ret.rsdp20);
 153                return ret.rsdp20 ? ret.rsdp20 : ret.rsdp10;
 154            }
 155        }
 156        g_usleep(TEST_DELAY);
 157    }
 158    g_assert_not_reached();
 159    return 0;
 160}
 161