acpi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2012 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/types.h>
  19. #include <grub/time.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/acpi.h>
  23. /* Simple checksum by summing all bytes. Used by ACPI and SMBIOS. */
  24. grub_uint8_t
  25. grub_byte_checksum (void *base, grub_size_t size)
  26. {
  27. grub_uint8_t *ptr;
  28. grub_uint8_t ret = 0;
  29. for (ptr = (grub_uint8_t *) base; ptr < ((grub_uint8_t *) base) + size;
  30. ptr++)
  31. ret += *ptr;
  32. return ret;
  33. }
  34. static void *
  35. grub_acpi_rsdt_find_table (struct grub_acpi_table_header *rsdt, const char *sig)
  36. {
  37. grub_size_t s;
  38. grub_unaligned_uint32_t *ptr;
  39. if (!rsdt)
  40. return 0;
  41. if (grub_memcmp (rsdt->signature, "RSDT", 4) != 0)
  42. return 0;
  43. ptr = (grub_unaligned_uint32_t *) (rsdt + 1);
  44. s = (rsdt->length - sizeof (*rsdt)) / sizeof (grub_uint32_t);
  45. for (; s; s--, ptr++)
  46. {
  47. struct grub_acpi_table_header *tbl;
  48. tbl = (struct grub_acpi_table_header *) (grub_addr_t) ptr->val;
  49. if (grub_memcmp (tbl->signature, sig, 4) == 0)
  50. return tbl;
  51. }
  52. return 0;
  53. }
  54. static void *
  55. grub_acpi_xsdt_find_table (struct grub_acpi_table_header *xsdt, const char *sig)
  56. {
  57. grub_size_t s;
  58. grub_unaligned_uint64_t *ptr;
  59. if (!xsdt)
  60. return 0;
  61. if (grub_memcmp (xsdt->signature, "XSDT", 4) != 0)
  62. return 0;
  63. ptr = (grub_unaligned_uint64_t *) (xsdt + 1);
  64. s = (xsdt->length - sizeof (*xsdt)) / sizeof (grub_uint32_t);
  65. for (; s; s--, ptr++)
  66. {
  67. struct grub_acpi_table_header *tbl;
  68. #if GRUB_CPU_SIZEOF_VOID_P != 8
  69. if (ptr->val >> 32)
  70. continue;
  71. #endif
  72. tbl = (struct grub_acpi_table_header *) (grub_addr_t) ptr->val;
  73. if (grub_memcmp (tbl->signature, sig, 4) == 0)
  74. return tbl;
  75. }
  76. return 0;
  77. }
  78. void *
  79. grub_acpi_find_table (const char *sig)
  80. {
  81. struct grub_acpi_fadt *r = NULL;
  82. struct grub_acpi_rsdp_v10 *rsdpv1;
  83. struct grub_acpi_rsdp_v20 *rsdpv2;
  84. rsdpv1 = grub_machine_acpi_get_rsdpv1 ();
  85. if (rsdpv1)
  86. r = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
  87. (grub_addr_t) rsdpv1->rsdt_addr,
  88. sig);
  89. if (r)
  90. return r;
  91. rsdpv2 = grub_machine_acpi_get_rsdpv2 ();
  92. if (rsdpv2
  93. #if GRUB_CPU_SIZEOF_VOID_P != 8
  94. && !(rsdpv2->xsdt_addr >> 32)
  95. #endif
  96. )
  97. r = grub_acpi_xsdt_find_table ((struct grub_acpi_table_header *)
  98. (grub_addr_t) rsdpv2->xsdt_addr,
  99. sig);
  100. if (r)
  101. return r;
  102. if (rsdpv2)
  103. r = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
  104. (grub_addr_t) rsdpv2->rsdpv1.rsdt_addr,
  105. sig);
  106. if (r)
  107. return r;
  108. return 0;
  109. }
  110. struct grub_acpi_fadt *
  111. grub_acpi_find_fadt (void)
  112. {
  113. return grub_acpi_find_table (GRUB_ACPI_FADT_SIGNATURE);
  114. }