tbutils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /******************************************************************************
  2. *
  3. * Module Name: tbutils - ACPI Table utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "actables.h"
  45. #define _COMPONENT ACPI_TABLES
  46. ACPI_MODULE_NAME("tbutils")
  47. /* Local prototypes */
  48. static acpi_physical_address
  49. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
  50. #if (!ACPI_REDUCED_HARDWARE)
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_tb_initialize_facs
  54. *
  55. * PARAMETERS: None
  56. *
  57. * RETURN: Status
  58. *
  59. * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
  60. * for accessing the Global Lock and Firmware Waking Vector
  61. *
  62. ******************************************************************************/
  63. acpi_status acpi_tb_initialize_facs(void)
  64. {
  65. struct acpi_table_facs *facs;
  66. /* If Hardware Reduced flag is set, there is no FACS */
  67. if (acpi_gbl_reduced_hardware) {
  68. acpi_gbl_FACS = NULL;
  69. return (AE_OK);
  70. } else if (acpi_gbl_FADT.Xfacs &&
  71. (!acpi_gbl_FADT.facs
  72. || !acpi_gbl_use32_bit_facs_addresses)) {
  73. (void)acpi_get_table_by_index(acpi_gbl_xfacs_index,
  74. ACPI_CAST_INDIRECT_PTR(struct
  75. acpi_table_header,
  76. &facs));
  77. acpi_gbl_FACS = facs;
  78. } else if (acpi_gbl_FADT.facs) {
  79. (void)acpi_get_table_by_index(acpi_gbl_facs_index,
  80. ACPI_CAST_INDIRECT_PTR(struct
  81. acpi_table_header,
  82. &facs));
  83. acpi_gbl_FACS = facs;
  84. }
  85. /* If there is no FACS, just continue. There was already an error msg */
  86. return (AE_OK);
  87. }
  88. #endif /* !ACPI_REDUCED_HARDWARE */
  89. /*******************************************************************************
  90. *
  91. * FUNCTION: acpi_tb_check_dsdt_header
  92. *
  93. * PARAMETERS: None
  94. *
  95. * RETURN: None
  96. *
  97. * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
  98. * if the DSDT has been replaced from outside the OS and/or if
  99. * the DSDT header has been corrupted.
  100. *
  101. ******************************************************************************/
  102. void acpi_tb_check_dsdt_header(void)
  103. {
  104. /* Compare original length and checksum to current values */
  105. if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
  106. acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
  107. ACPI_BIOS_ERROR((AE_INFO,
  108. "The DSDT has been corrupted or replaced - "
  109. "old, new headers below"));
  110. acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
  111. acpi_tb_print_table_header(0, acpi_gbl_DSDT);
  112. ACPI_ERROR((AE_INFO,
  113. "Please send DMI info to linux-acpi@vger.kernel.org\n"
  114. "If system does not work as expected, please boot with acpi=copy_dsdt"));
  115. /* Disable further error messages */
  116. acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
  117. acpi_gbl_original_dsdt_header.checksum =
  118. acpi_gbl_DSDT->checksum;
  119. }
  120. }
  121. /*******************************************************************************
  122. *
  123. * FUNCTION: acpi_tb_copy_dsdt
  124. *
  125. * PARAMETERS: table_desc - Installed table to copy
  126. *
  127. * RETURN: None
  128. *
  129. * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
  130. * Some very bad BIOSs are known to either corrupt the DSDT or
  131. * install a new, bad DSDT. This copy works around the problem.
  132. *
  133. ******************************************************************************/
  134. struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
  135. {
  136. struct acpi_table_header *new_table;
  137. struct acpi_table_desc *table_desc;
  138. table_desc = &acpi_gbl_root_table_list.tables[table_index];
  139. new_table = ACPI_ALLOCATE(table_desc->length);
  140. if (!new_table) {
  141. ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
  142. table_desc->length));
  143. return (NULL);
  144. }
  145. memcpy(new_table, table_desc->pointer, table_desc->length);
  146. acpi_tb_uninstall_table(table_desc);
  147. acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
  148. tables[acpi_gbl_dsdt_index],
  149. ACPI_PTR_TO_PHYSADDR(new_table),
  150. ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
  151. new_table);
  152. ACPI_INFO((AE_INFO,
  153. "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
  154. new_table->length));
  155. return (new_table);
  156. }
  157. /*******************************************************************************
  158. *
  159. * FUNCTION: acpi_tb_get_root_table_entry
  160. *
  161. * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
  162. * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
  163. *
  164. * RETURN: Physical address extracted from the root table
  165. *
  166. * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
  167. * both 32-bit and 64-bit platforms
  168. *
  169. * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
  170. * 64-bit platforms.
  171. *
  172. ******************************************************************************/
  173. static acpi_physical_address
  174. acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
  175. {
  176. u64 address64;
  177. /*
  178. * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
  179. * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
  180. */
  181. if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
  182. /*
  183. * 32-bit platform, RSDT: Return 32-bit table entry
  184. * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
  185. */
  186. return ((acpi_physical_address)
  187. (*ACPI_CAST_PTR(u32, table_entry)));
  188. } else {
  189. /*
  190. * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
  191. * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
  192. * return 64-bit
  193. */
  194. ACPI_MOVE_64_TO_64(&address64, table_entry);
  195. #if ACPI_MACHINE_WIDTH == 32
  196. if (address64 > ACPI_UINT32_MAX) {
  197. /* Will truncate 64-bit address to 32 bits, issue warning */
  198. ACPI_BIOS_WARNING((AE_INFO,
  199. "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
  200. " truncating",
  201. ACPI_FORMAT_UINT64(address64)));
  202. }
  203. #endif
  204. return ((acpi_physical_address) (address64));
  205. }
  206. }
  207. /*******************************************************************************
  208. *
  209. * FUNCTION: acpi_tb_parse_root_table
  210. *
  211. * PARAMETERS: rsdp - Pointer to the RSDP
  212. *
  213. * RETURN: Status
  214. *
  215. * DESCRIPTION: This function is called to parse the Root System Description
  216. * Table (RSDT or XSDT)
  217. *
  218. * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
  219. * be mapped and cannot be copied because it contains the actual
  220. * memory location of the ACPI Global Lock.
  221. *
  222. ******************************************************************************/
  223. acpi_status __init acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
  224. {
  225. struct acpi_table_rsdp *rsdp;
  226. u32 table_entry_size;
  227. u32 i;
  228. u32 table_count;
  229. struct acpi_table_header *table;
  230. acpi_physical_address address;
  231. u32 length;
  232. u8 *table_entry;
  233. acpi_status status;
  234. u32 table_index;
  235. ACPI_FUNCTION_TRACE(tb_parse_root_table);
  236. /* Map the entire RSDP and extract the address of the RSDT or XSDT */
  237. rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
  238. if (!rsdp) {
  239. return_ACPI_STATUS(AE_NO_MEMORY);
  240. }
  241. acpi_tb_print_table_header(rsdp_address,
  242. ACPI_CAST_PTR(struct acpi_table_header,
  243. rsdp));
  244. /* Use XSDT if present and not overridden. Otherwise, use RSDT */
  245. if ((rsdp->revision > 1) &&
  246. rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
  247. /*
  248. * RSDP contains an XSDT (64-bit physical addresses). We must use
  249. * the XSDT if the revision is > 1 and the XSDT pointer is present,
  250. * as per the ACPI specification.
  251. */
  252. address = (acpi_physical_address) rsdp->xsdt_physical_address;
  253. table_entry_size = ACPI_XSDT_ENTRY_SIZE;
  254. } else {
  255. /* Root table is an RSDT (32-bit physical addresses) */
  256. address = (acpi_physical_address) rsdp->rsdt_physical_address;
  257. table_entry_size = ACPI_RSDT_ENTRY_SIZE;
  258. }
  259. /*
  260. * It is not possible to map more than one entry in some environments,
  261. * so unmap the RSDP here before mapping other tables
  262. */
  263. acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
  264. /* Map the RSDT/XSDT table header to get the full table length */
  265. table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
  266. if (!table) {
  267. return_ACPI_STATUS(AE_NO_MEMORY);
  268. }
  269. acpi_tb_print_table_header(address, table);
  270. /*
  271. * Validate length of the table, and map entire table.
  272. * Minimum length table must contain at least one entry.
  273. */
  274. length = table->length;
  275. acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
  276. if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
  277. ACPI_BIOS_ERROR((AE_INFO,
  278. "Invalid table length 0x%X in RSDT/XSDT",
  279. length));
  280. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  281. }
  282. table = acpi_os_map_memory(address, length);
  283. if (!table) {
  284. return_ACPI_STATUS(AE_NO_MEMORY);
  285. }
  286. /* Validate the root table checksum */
  287. status = acpi_tb_verify_checksum(table, length);
  288. if (ACPI_FAILURE(status)) {
  289. acpi_os_unmap_memory(table, length);
  290. return_ACPI_STATUS(status);
  291. }
  292. /* Get the number of entries and pointer to first entry */
  293. table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
  294. table_entry_size);
  295. table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
  296. /* Initialize the root table array from the RSDT/XSDT */
  297. for (i = 0; i < table_count; i++) {
  298. /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
  299. address =
  300. acpi_tb_get_root_table_entry(table_entry, table_entry_size);
  301. /* Skip NULL entries in RSDT/XSDT */
  302. if (!address) {
  303. goto next_table;
  304. }
  305. status = acpi_tb_install_standard_table(address,
  306. ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
  307. FALSE, TRUE,
  308. &table_index);
  309. if (ACPI_SUCCESS(status) &&
  310. ACPI_COMPARE_NAME(&acpi_gbl_root_table_list.
  311. tables[table_index].signature,
  312. ACPI_SIG_FADT)) {
  313. acpi_gbl_fadt_index = table_index;
  314. acpi_tb_parse_fadt();
  315. }
  316. next_table:
  317. table_entry += table_entry_size;
  318. }
  319. acpi_os_unmap_memory(table, length);
  320. return_ACPI_STATUS(AE_OK);
  321. }
  322. /*******************************************************************************
  323. *
  324. * FUNCTION: acpi_is_valid_signature
  325. *
  326. * PARAMETERS: signature - Sig string to be validated
  327. *
  328. * RETURN: TRUE if signature is correct length and has valid characters
  329. *
  330. * DESCRIPTION: Validate an ACPI table signature.
  331. *
  332. ******************************************************************************/
  333. u8 acpi_is_valid_signature(char *signature)
  334. {
  335. u32 i;
  336. /* Validate the signature length */
  337. if (strlen(signature) != ACPI_NAME_SIZE) {
  338. return (FALSE);
  339. }
  340. /* Validate each character in the signature */
  341. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  342. if (!acpi_ut_valid_acpi_char(signature[i], i)) {
  343. return (FALSE);
  344. }
  345. }
  346. return (TRUE);
  347. }