acpi.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/acpi.h>
  22. /* Simple checksum by summing all bytes. Used by ACPI and SMBIOS. */
  23. grub_uint8_t
  24. grub_byte_checksum (void *base, grub_size_t size)
  25. {
  26. grub_uint8_t *ptr;
  27. grub_uint8_t ret = 0;
  28. for (ptr = (grub_uint8_t *) base; ptr < ((grub_uint8_t *) base) + size;
  29. ptr++)
  30. ret += *ptr;
  31. return ret;
  32. }
  33. static void *
  34. grub_acpi_rsdt_find_table (struct grub_acpi_table_header *rsdt, const char *sig)
  35. {
  36. grub_size_t s;
  37. grub_unaligned_uint32_t *ptr;
  38. if (!rsdt)
  39. return 0;
  40. if (grub_memcmp (rsdt->signature, "RSDT", 4) != 0)
  41. return 0;
  42. ptr = (grub_unaligned_uint32_t *) (rsdt + 1);
  43. s = (rsdt->length - sizeof (*rsdt)) / sizeof (grub_uint32_t);
  44. for (; s; s--, ptr++)
  45. {
  46. struct grub_acpi_table_header *tbl;
  47. tbl = (struct grub_acpi_table_header *) (grub_addr_t) ptr->val;
  48. if (grub_memcmp (tbl->signature, sig, 4) == 0)
  49. return tbl;
  50. }
  51. return 0;
  52. }
  53. static void *
  54. grub_acpi_xsdt_find_table (struct grub_acpi_table_header *xsdt, const char *sig)
  55. {
  56. grub_size_t s;
  57. grub_unaligned_uint64_t *ptr;
  58. if (!xsdt)
  59. return 0;
  60. if (grub_memcmp (xsdt->signature, "XSDT", 4) != 0)
  61. return 0;
  62. ptr = (grub_unaligned_uint64_t *) (xsdt + 1);
  63. s = (xsdt->length - sizeof (*xsdt)) / sizeof (grub_uint32_t);
  64. for (; s; s--, ptr++)
  65. {
  66. struct grub_acpi_table_header *tbl;
  67. #if GRUB_CPU_SIZEOF_VOID_P != 8
  68. if (ptr->val >> 32)
  69. continue;
  70. #endif
  71. tbl = (struct grub_acpi_table_header *) (grub_addr_t) ptr->val;
  72. if (grub_memcmp (tbl->signature, sig, 4) == 0)
  73. return tbl;
  74. }
  75. return 0;
  76. }
  77. struct grub_acpi_fadt *
  78. grub_acpi_find_fadt (void)
  79. {
  80. struct grub_acpi_fadt *fadt = 0;
  81. struct grub_acpi_rsdp_v10 *rsdpv1;
  82. struct grub_acpi_rsdp_v20 *rsdpv2;
  83. rsdpv1 = grub_machine_acpi_get_rsdpv1 ();
  84. if (rsdpv1)
  85. fadt = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
  86. (grub_addr_t) rsdpv1->rsdt_addr,
  87. GRUB_ACPI_FADT_SIGNATURE);
  88. if (fadt)
  89. return fadt;
  90. rsdpv2 = grub_machine_acpi_get_rsdpv2 ();
  91. if (rsdpv2)
  92. fadt = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
  93. (grub_addr_t) rsdpv2->rsdpv1.rsdt_addr,
  94. GRUB_ACPI_FADT_SIGNATURE);
  95. if (fadt)
  96. return fadt;
  97. if (rsdpv2
  98. #if GRUB_CPU_SIZEOF_VOID_P != 8
  99. && !(rsdpv2->xsdt_addr >> 32)
  100. #endif
  101. )
  102. fadt = grub_acpi_xsdt_find_table ((struct grub_acpi_table_header *)
  103. (grub_addr_t) rsdpv2->xsdt_addr,
  104. GRUB_ACPI_FADT_SIGNATURE);
  105. if (fadt)
  106. return fadt;
  107. return 0;
  108. }