tbutils.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /******************************************************************************
  2. *
  3. * Module Name: tbutils - Table manipulation utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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 <acpi/actables.h>
  44. #define _COMPONENT ACPI_TABLES
  45. ACPI_MODULE_NAME("tbutils")
  46. /* Local prototypes */
  47. #ifdef ACPI_OBSOLETE_FUNCTIONS
  48. acpi_status
  49. acpi_tb_handle_to_object(u16 table_id, struct acpi_table_desc **table_desc);
  50. #endif
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_tb_is_table_installed
  54. *
  55. * PARAMETERS: new_table_desc - Descriptor for new table being installed
  56. *
  57. * RETURN: Status - AE_ALREADY_EXISTS if the table is already installed
  58. *
  59. * DESCRIPTION: Determine if an ACPI table is already installed
  60. *
  61. * MUTEX: Table data structures should be locked
  62. *
  63. ******************************************************************************/
  64. acpi_status acpi_tb_is_table_installed(struct acpi_table_desc *new_table_desc)
  65. {
  66. struct acpi_table_desc *table_desc;
  67. ACPI_FUNCTION_TRACE("tb_is_table_installed");
  68. /* Get the list descriptor and first table descriptor */
  69. table_desc = acpi_gbl_table_lists[new_table_desc->type].next;
  70. /* Examine all installed tables of this type */
  71. while (table_desc) {
  72. /*
  73. * If the table lengths match, perform a full bytewise compare. This
  74. * means that we will allow tables with duplicate oem_table_id(s), as
  75. * long as the tables are different in some way.
  76. *
  77. * Checking if the table has been loaded into the namespace means that
  78. * we don't check for duplicate tables during the initial installation
  79. * of tables within the RSDT/XSDT.
  80. */
  81. if ((table_desc->loaded_into_namespace) &&
  82. (table_desc->pointer->length ==
  83. new_table_desc->pointer->length)
  84. &&
  85. (!ACPI_MEMCMP
  86. ((const char *)table_desc->pointer,
  87. (const char *)new_table_desc->pointer,
  88. (acpi_size) new_table_desc->pointer->length))) {
  89. /* Match: this table is already installed */
  90. ACPI_DEBUG_PRINT((ACPI_DB_TABLES,
  91. "Table [%4.4s] already installed: Rev %X oem_table_id [%8.8s]\n",
  92. new_table_desc->pointer->signature,
  93. new_table_desc->pointer->revision,
  94. new_table_desc->pointer->
  95. oem_table_id));
  96. new_table_desc->owner_id = table_desc->owner_id;
  97. new_table_desc->installed_desc = table_desc;
  98. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  99. }
  100. /* Get next table on the list */
  101. table_desc = table_desc->next;
  102. }
  103. return_ACPI_STATUS(AE_OK);
  104. }
  105. /*******************************************************************************
  106. *
  107. * FUNCTION: acpi_tb_validate_table_header
  108. *
  109. * PARAMETERS: table_header - Logical pointer to the table
  110. *
  111. * RETURN: Status
  112. *
  113. * DESCRIPTION: Check an ACPI table header for validity
  114. *
  115. * NOTE: Table pointers are validated as follows:
  116. * 1) Table pointer must point to valid physical memory
  117. * 2) Signature must be 4 ASCII chars, even if we don't recognize the
  118. * name
  119. * 3) Table must be readable for length specified in the header
  120. * 4) Table checksum must be valid (with the exception of the FACS
  121. * which has no checksum because it contains variable fields)
  122. *
  123. ******************************************************************************/
  124. acpi_status
  125. acpi_tb_validate_table_header(struct acpi_table_header *table_header)
  126. {
  127. acpi_name signature;
  128. ACPI_FUNCTION_NAME("tb_validate_table_header");
  129. /* Verify that this is a valid address */
  130. if (!acpi_os_readable(table_header, sizeof(struct acpi_table_header))) {
  131. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  132. "Cannot read table header at %p\n",
  133. table_header));
  134. return (AE_BAD_ADDRESS);
  135. }
  136. /* Ensure that the signature is 4 ASCII characters */
  137. ACPI_MOVE_32_TO_32(&signature, table_header->signature);
  138. if (!acpi_ut_valid_acpi_name(signature)) {
  139. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  140. "Table signature at %p [%p] has invalid characters\n",
  141. table_header, &signature));
  142. ACPI_REPORT_WARNING(("Invalid table signature found: [%4.4s]\n",
  143. (char *)&signature));
  144. ACPI_DUMP_BUFFER(table_header,
  145. sizeof(struct acpi_table_header));
  146. return (AE_BAD_SIGNATURE);
  147. }
  148. /* Validate the table length */
  149. if (table_header->length < sizeof(struct acpi_table_header)) {
  150. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  151. "Invalid length in table header %p name %4.4s\n",
  152. table_header, (char *)&signature));
  153. ACPI_REPORT_WARNING(("Invalid table header length (0x%X) found\n", (u32) table_header->length));
  154. ACPI_DUMP_BUFFER(table_header,
  155. sizeof(struct acpi_table_header));
  156. return (AE_BAD_HEADER);
  157. }
  158. return (AE_OK);
  159. }
  160. /*******************************************************************************
  161. *
  162. * FUNCTION: acpi_tb_verify_table_checksum
  163. *
  164. * PARAMETERS: *table_header - ACPI table to verify
  165. *
  166. * RETURN: 8 bit checksum of table
  167. *
  168. * DESCRIPTION: Does an 8 bit checksum of table and returns status. A correct
  169. * table should have a checksum of 0.
  170. *
  171. ******************************************************************************/
  172. acpi_status
  173. acpi_tb_verify_table_checksum(struct acpi_table_header * table_header)
  174. {
  175. u8 checksum;
  176. acpi_status status = AE_OK;
  177. ACPI_FUNCTION_TRACE("tb_verify_table_checksum");
  178. /* Compute the checksum on the table */
  179. checksum =
  180. acpi_tb_generate_checksum(table_header, table_header->length);
  181. /* Return the appropriate exception */
  182. if (checksum) {
  183. ACPI_REPORT_WARNING(("Invalid checksum in table [%4.4s] (%02X, sum %02X is not zero)\n", table_header->signature, (u32) table_header->checksum, (u32) checksum));
  184. status = AE_BAD_CHECKSUM;
  185. }
  186. return_ACPI_STATUS(status);
  187. }
  188. /*******************************************************************************
  189. *
  190. * FUNCTION: acpi_tb_generate_checksum
  191. *
  192. * PARAMETERS: Buffer - Buffer to checksum
  193. * Length - Size of the buffer
  194. *
  195. * RETURN: 8 bit checksum of buffer
  196. *
  197. * DESCRIPTION: Computes an 8 bit checksum of the buffer(length) and returns it.
  198. *
  199. ******************************************************************************/
  200. u8 acpi_tb_generate_checksum(void *buffer, u32 length)
  201. {
  202. const u8 *limit;
  203. const u8 *rover;
  204. u8 sum = 0;
  205. if (buffer && length) {
  206. /* Buffer and Length are valid */
  207. limit = (u8 *) buffer + length;
  208. for (rover = buffer; rover < limit; rover++) {
  209. sum = (u8) (sum + *rover);
  210. }
  211. }
  212. return (sum);
  213. }
  214. #ifdef ACPI_OBSOLETE_FUNCTIONS
  215. /*******************************************************************************
  216. *
  217. * FUNCTION: acpi_tb_handle_to_object
  218. *
  219. * PARAMETERS: table_id - Id for which the function is searching
  220. * table_desc - Pointer to return the matching table
  221. * descriptor.
  222. *
  223. * RETURN: Search the tables to find one with a matching table_id and
  224. * return a pointer to that table descriptor.
  225. *
  226. ******************************************************************************/
  227. acpi_status
  228. acpi_tb_handle_to_object(u16 table_id,
  229. struct acpi_table_desc ** return_table_desc)
  230. {
  231. u32 i;
  232. struct acpi_table_desc *table_desc;
  233. ACPI_FUNCTION_NAME("tb_handle_to_object");
  234. for (i = 0; i < ACPI_TABLE_MAX; i++) {
  235. table_desc = acpi_gbl_table_lists[i].next;
  236. while (table_desc) {
  237. if (table_desc->table_id == table_id) {
  238. *return_table_desc = table_desc;
  239. return (AE_OK);
  240. }
  241. table_desc = table_desc->next;
  242. }
  243. }
  244. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "table_id=%X does not exist\n",
  245. table_id));
  246. return (AE_BAD_PARAMETER);
  247. }
  248. #endif