tbutils.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /******************************************************************************
  2. *
  3. * Module Name: tbutils - Table manipulation utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, 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. (table_desc->pointer, new_table_desc->pointer,
  87. new_table_desc->pointer->length))) {
  88. /* Match: this table is already installed */
  89. ACPI_DEBUG_PRINT((ACPI_DB_TABLES,
  90. "Table [%4.4s] already installed: Rev %X oem_table_id [%8.8s]\n",
  91. new_table_desc->pointer->signature,
  92. new_table_desc->pointer->revision,
  93. new_table_desc->pointer->
  94. oem_table_id));
  95. new_table_desc->owner_id = table_desc->owner_id;
  96. new_table_desc->installed_desc = table_desc;
  97. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  98. }
  99. /* Get next table on the list */
  100. table_desc = table_desc->next;
  101. }
  102. return_ACPI_STATUS(AE_OK);
  103. }
  104. /*******************************************************************************
  105. *
  106. * FUNCTION: acpi_tb_validate_table_header
  107. *
  108. * PARAMETERS: table_header - Logical pointer to the table
  109. *
  110. * RETURN: Status
  111. *
  112. * DESCRIPTION: Check an ACPI table header for validity
  113. *
  114. * NOTE: Table pointers are validated as follows:
  115. * 1) Table pointer must point to valid physical memory
  116. * 2) Signature must be 4 ASCII chars, even if we don't recognize the
  117. * name
  118. * 3) Table must be readable for length specified in the header
  119. * 4) Table checksum must be valid (with the exception of the FACS
  120. * which has no checksum because it contains variable fields)
  121. *
  122. ******************************************************************************/
  123. acpi_status
  124. acpi_tb_validate_table_header(struct acpi_table_header *table_header)
  125. {
  126. acpi_name signature;
  127. ACPI_FUNCTION_ENTRY();
  128. /* Verify that this is a valid address */
  129. if (!acpi_os_readable(table_header, sizeof(struct acpi_table_header))) {
  130. ACPI_ERROR((AE_INFO,
  131. "Cannot read table header at %p", table_header));
  132. return (AE_BAD_ADDRESS);
  133. }
  134. /* Ensure that the signature is 4 ASCII characters */
  135. ACPI_MOVE_32_TO_32(&signature, table_header->signature);
  136. if (!acpi_ut_valid_acpi_name(signature)) {
  137. ACPI_ERROR((AE_INFO,
  138. "Table signature at %p [%p] has invalid characters",
  139. table_header, &signature));
  140. ACPI_WARNING((AE_INFO, "Invalid table signature found: [%4.4s]",
  141. ACPI_CAST_PTR(char, &signature)));
  142. ACPI_DUMP_BUFFER(table_header,
  143. sizeof(struct acpi_table_header));
  144. return (AE_BAD_SIGNATURE);
  145. }
  146. /* Validate the table length */
  147. if (table_header->length < sizeof(struct acpi_table_header)) {
  148. ACPI_ERROR((AE_INFO,
  149. "Invalid length in table header %p name %4.4s",
  150. table_header, (char *)&signature));
  151. ACPI_WARNING((AE_INFO,
  152. "Invalid table header length (0x%X) found",
  153. (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_WARNING((AE_INFO,
  184. "Invalid checksum in table [%4.4s] (%02X, sum %02X is not zero)",
  185. table_header->signature,
  186. (u32) table_header->checksum, (u32) checksum));
  187. status = AE_BAD_CHECKSUM;
  188. }
  189. return_ACPI_STATUS(status);
  190. }
  191. /*******************************************************************************
  192. *
  193. * FUNCTION: acpi_tb_generate_checksum
  194. *
  195. * PARAMETERS: Buffer - Buffer to checksum
  196. * Length - Size of the buffer
  197. *
  198. * RETURN: 8 bit checksum of buffer
  199. *
  200. * DESCRIPTION: Computes an 8 bit checksum of the buffer(length) and returns it.
  201. *
  202. ******************************************************************************/
  203. u8 acpi_tb_generate_checksum(void *buffer, u32 length)
  204. {
  205. u8 *end_buffer;
  206. u8 *rover;
  207. u8 sum = 0;
  208. if (buffer && length) {
  209. /* Buffer and Length are valid */
  210. end_buffer = ACPI_ADD_PTR(u8, buffer, length);
  211. for (rover = buffer; rover < end_buffer; rover++) {
  212. sum = (u8) (sum + *rover);
  213. }
  214. }
  215. return (sum);
  216. }
  217. #ifdef ACPI_OBSOLETE_FUNCTIONS
  218. /*******************************************************************************
  219. *
  220. * FUNCTION: acpi_tb_handle_to_object
  221. *
  222. * PARAMETERS: table_id - Id for which the function is searching
  223. * table_desc - Pointer to return the matching table
  224. * descriptor.
  225. *
  226. * RETURN: Search the tables to find one with a matching table_id and
  227. * return a pointer to that table descriptor.
  228. *
  229. ******************************************************************************/
  230. acpi_status
  231. acpi_tb_handle_to_object(u16 table_id,
  232. struct acpi_table_desc ** return_table_desc)
  233. {
  234. u32 i;
  235. struct acpi_table_desc *table_desc;
  236. ACPI_FUNCTION_NAME("tb_handle_to_object");
  237. for (i = 0; i < ACPI_TABLE_MAX; i++) {
  238. table_desc = acpi_gbl_table_lists[i].next;
  239. while (table_desc) {
  240. if (table_desc->table_id == table_id) {
  241. *return_table_desc = table_desc;
  242. return (AE_OK);
  243. }
  244. table_desc = table_desc->next;
  245. }
  246. }
  247. ACPI_ERROR((AE_INFO, "table_id=%X does not exist", table_id));
  248. return (AE_BAD_PARAMETER);
  249. }
  250. #endif