acpi.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /* Skip NULL entries in RSDT/XSDT. */
  49. if (!ptr->val)
  50. continue;
  51. tbl = (struct grub_acpi_table_header *) (grub_addr_t) ptr->val;
  52. if (grub_memcmp (tbl->signature, sig, 4) == 0)
  53. return tbl;
  54. }
  55. return 0;
  56. }
  57. static void *
  58. grub_acpi_xsdt_find_table (struct grub_acpi_table_header *xsdt, const char *sig)
  59. {
  60. grub_size_t s;
  61. grub_unaligned_uint64_t *ptr;
  62. if (!xsdt)
  63. return 0;
  64. if (grub_memcmp (xsdt->signature, "XSDT", 4) != 0)
  65. return 0;
  66. ptr = (grub_unaligned_uint64_t *) (xsdt + 1);
  67. s = (xsdt->length - sizeof (*xsdt)) / sizeof (grub_uint64_t);
  68. for (; s; s--, ptr++)
  69. {
  70. struct grub_acpi_table_header *tbl;
  71. /* Skip NULL entries in RSDT/XSDT. */
  72. if (!ptr->val)
  73. continue;
  74. #if GRUB_CPU_SIZEOF_VOID_P != 8
  75. if (ptr->val >> 32)
  76. continue;
  77. #endif
  78. tbl = (struct grub_acpi_table_header *) (grub_addr_t) ptr->val;
  79. if (grub_memcmp (tbl->signature, sig, 4) == 0)
  80. return tbl;
  81. }
  82. return 0;
  83. }
  84. void *
  85. grub_acpi_find_table (const char *sig)
  86. {
  87. struct grub_acpi_fadt *r = NULL;
  88. struct grub_acpi_rsdp_v10 *rsdpv1;
  89. struct grub_acpi_rsdp_v20 *rsdpv2;
  90. rsdpv1 = grub_machine_acpi_get_rsdpv1 ();
  91. if (rsdpv1)
  92. r = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
  93. (grub_addr_t) rsdpv1->rsdt_addr,
  94. sig);
  95. if (r)
  96. return r;
  97. rsdpv2 = grub_machine_acpi_get_rsdpv2 ();
  98. if (rsdpv2
  99. #if GRUB_CPU_SIZEOF_VOID_P != 8
  100. && !(rsdpv2->xsdt_addr >> 32)
  101. #endif
  102. )
  103. r = grub_acpi_xsdt_find_table ((struct grub_acpi_table_header *)
  104. (grub_addr_t) rsdpv2->xsdt_addr,
  105. sig);
  106. if (r)
  107. return r;
  108. if (rsdpv2)
  109. r = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
  110. (grub_addr_t) rsdpv2->rsdpv1.rsdt_addr,
  111. sig);
  112. if (r)
  113. return r;
  114. return 0;
  115. }
  116. struct grub_acpi_fadt *
  117. grub_acpi_find_fadt (void)
  118. {
  119. return grub_acpi_find_table (GRUB_ACPI_FADT_SIGNATURE);
  120. }