acpi-utils.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "qemu/osdep.h"
  15. #include <glib/gstdio.h>
  16. #include "qemu-common.h"
  17. #include "qemu/bitmap.h"
  18. #include "acpi-utils.h"
  19. #include "boot-sector.h"
  20. uint8_t acpi_calc_checksum(const uint8_t *data, int len)
  21. {
  22. int i;
  23. uint8_t sum = 0;
  24. for (i = 0; i < len; i++) {
  25. sum += data[i];
  26. }
  27. return sum;
  28. }
  29. uint32_t acpi_find_rsdp_address(QTestState *qts)
  30. {
  31. uint32_t off;
  32. /* RSDP location can vary across a narrow range */
  33. for (off = 0xf0000; off < 0x100000; off += 0x10) {
  34. uint8_t sig[] = "RSD PTR ";
  35. int i;
  36. for (i = 0; i < sizeof sig - 1; ++i) {
  37. sig[i] = qtest_readb(qts, off + i);
  38. }
  39. if (!memcmp(sig, "RSD PTR ", sizeof sig)) {
  40. break;
  41. }
  42. }
  43. return off;
  44. }
  45. void acpi_fetch_rsdp_table(QTestState *qts, uint64_t addr, uint8_t *rsdp_table)
  46. {
  47. uint8_t revision;
  48. /* Read mandatory revision 0 table data (20 bytes) first */
  49. qtest_memread(qts, addr, rsdp_table, 20);
  50. revision = rsdp_table[15 /* Revision offset */];
  51. switch (revision) {
  52. case 0: /* ACPI 1.0 RSDP */
  53. break;
  54. case 2: /* ACPI 2.0+ RSDP */
  55. /* Read the rest of the RSDP table */
  56. qtest_memread(qts, addr + 20, rsdp_table + 20, 16);
  57. break;
  58. default:
  59. g_assert_not_reached();
  60. }
  61. ACPI_ASSERT_CMP64(*((uint64_t *)(rsdp_table)), "RSD PTR ");
  62. }
  63. /** acpi_fetch_table
  64. * load ACPI table at @addr_ptr offset pointer into buffer and return it in
  65. * @aml, its length in @aml_len and check that signature/checksum matches
  66. * actual one.
  67. */
  68. void acpi_fetch_table(QTestState *qts, uint8_t **aml, uint32_t *aml_len,
  69. const uint8_t *addr_ptr, int addr_size, const char *sig,
  70. bool verify_checksum)
  71. {
  72. uint32_t len;
  73. uint64_t addr = 0;
  74. g_assert(addr_size == 4 || addr_size == 8);
  75. memcpy(&addr, addr_ptr , addr_size);
  76. addr = le64_to_cpu(addr);
  77. qtest_memread(qts, addr + 4, &len, 4); /* Length of ACPI table */
  78. *aml_len = le32_to_cpu(len);
  79. *aml = g_malloc0(*aml_len);
  80. /* get whole table */
  81. qtest_memread(qts, addr, *aml, *aml_len);
  82. if (sig) {
  83. ACPI_ASSERT_CMP(**aml, sig);
  84. }
  85. if (verify_checksum) {
  86. g_assert(!acpi_calc_checksum(*aml, *aml_len));
  87. }
  88. }
  89. #define GUID_SIZE 16
  90. static const uint8_t AcpiTestSupportGuid[GUID_SIZE] = {
  91. 0xb1, 0xa6, 0x87, 0xab,
  92. 0x34, 0x20,
  93. 0xa0, 0xbd,
  94. 0x71, 0xbd, 0x37, 0x50, 0x07, 0x75, 0x77, 0x85 };
  95. typedef struct {
  96. uint8_t signature_guid[GUID_SIZE];
  97. uint64_t rsdp10;
  98. uint64_t rsdp20;
  99. } __attribute__((packed)) UefiTestSupport;
  100. /* Wait at most 600 seconds (test is slow with TCG and --enable-debug) */
  101. #define TEST_DELAY (1 * G_USEC_PER_SEC / 10)
  102. #define TEST_CYCLES MAX((600 * G_USEC_PER_SEC / TEST_DELAY), 1)
  103. #define MB 0x100000ULL
  104. uint64_t acpi_find_rsdp_address_uefi(QTestState *qts, uint64_t start,
  105. uint64_t size)
  106. {
  107. int i, j;
  108. uint8_t data[GUID_SIZE];
  109. for (i = 0; i < TEST_CYCLES; ++i) {
  110. for (j = 0; j < size / MB; j++) {
  111. /* look for GUID at every 1Mb block */
  112. uint64_t addr = start + j * MB;
  113. qtest_memread(qts, addr, data, sizeof(data));
  114. if (!memcmp(AcpiTestSupportGuid, data, sizeof(data))) {
  115. UefiTestSupport ret;
  116. qtest_memread(qts, addr, &ret, sizeof(ret));
  117. ret.rsdp10 = le64_to_cpu(ret.rsdp10);
  118. ret.rsdp20 = le64_to_cpu(ret.rsdp20);
  119. return ret.rsdp20 ? ret.rsdp20 : ret.rsdp10;
  120. }
  121. }
  122. g_usleep(TEST_DELAY);
  123. }
  124. g_assert_not_reached();
  125. return 0;
  126. }