tbutils.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 OemTableId [%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, "Invalid table signature 0x%8.8X",
  138. signature));
  139. ACPI_DUMP_BUFFER(table_header,
  140. sizeof(struct acpi_table_header));
  141. return (AE_BAD_SIGNATURE);
  142. }
  143. /* Validate the table length */
  144. if (table_header->length < sizeof(struct acpi_table_header)) {
  145. ACPI_ERROR((AE_INFO,
  146. "Invalid length 0x%X in table with signature %4.4s",
  147. (u32) table_header->length,
  148. ACPI_CAST_PTR(char, &signature)));
  149. ACPI_DUMP_BUFFER(table_header,
  150. sizeof(struct acpi_table_header));
  151. return (AE_BAD_HEADER);
  152. }
  153. return (AE_OK);
  154. }
  155. /*******************************************************************************
  156. *
  157. * FUNCTION: acpi_tb_sum_table
  158. *
  159. * PARAMETERS: Buffer - Buffer to sum
  160. * Length - Size of the buffer
  161. *
  162. * RETURN: 8 bit sum of buffer
  163. *
  164. * DESCRIPTION: Computes an 8 bit sum of the buffer(length) and returns it.
  165. *
  166. ******************************************************************************/
  167. u8 acpi_tb_sum_table(void *buffer, u32 length)
  168. {
  169. acpi_native_uint i;
  170. u8 sum = 0;
  171. if (!buffer || !length) {
  172. return (0);
  173. }
  174. for (i = 0; i < length; i++) {
  175. sum = (u8) (sum + ((u8 *) buffer)[i]);
  176. }
  177. return (sum);
  178. }
  179. /*******************************************************************************
  180. *
  181. * FUNCTION: acpi_tb_generate_checksum
  182. *
  183. * PARAMETERS: Table - Pointer to a valid ACPI table (with a
  184. * standard ACPI header)
  185. *
  186. * RETURN: 8 bit checksum of buffer
  187. *
  188. * DESCRIPTION: Computes an 8 bit checksum of the table.
  189. *
  190. ******************************************************************************/
  191. u8 acpi_tb_generate_checksum(struct acpi_table_header * table)
  192. {
  193. u8 checksum;
  194. /* Sum the entire table as-is */
  195. checksum = acpi_tb_sum_table(table, table->length);
  196. /* Subtract off the existing checksum value in the table */
  197. checksum = (u8) (checksum - table->checksum);
  198. /* Compute the final checksum */
  199. checksum = (u8) (0 - checksum);
  200. return (checksum);
  201. }
  202. /*******************************************************************************
  203. *
  204. * FUNCTION: acpi_tb_set_checksum
  205. *
  206. * PARAMETERS: Table - Pointer to a valid ACPI table (with a
  207. * standard ACPI header)
  208. *
  209. * RETURN: None. Sets the table checksum field
  210. *
  211. * DESCRIPTION: Computes an 8 bit checksum of the table and inserts the
  212. * checksum into the table header.
  213. *
  214. ******************************************************************************/
  215. void acpi_tb_set_checksum(struct acpi_table_header *table)
  216. {
  217. table->checksum = acpi_tb_generate_checksum(table);
  218. }
  219. /*******************************************************************************
  220. *
  221. * FUNCTION: acpi_tb_verify_table_checksum
  222. *
  223. * PARAMETERS: *table_header - ACPI table to verify
  224. *
  225. * RETURN: 8 bit checksum of table
  226. *
  227. * DESCRIPTION: Generates an 8 bit checksum of table and returns and compares
  228. * it to the existing checksum value.
  229. *
  230. ******************************************************************************/
  231. acpi_status
  232. acpi_tb_verify_table_checksum(struct acpi_table_header *table_header)
  233. {
  234. u8 checksum;
  235. ACPI_FUNCTION_TRACE(tb_verify_table_checksum);
  236. /* Compute the checksum on the table */
  237. checksum = acpi_tb_generate_checksum(table_header);
  238. /* Checksum ok? */
  239. if (checksum == table_header->checksum) {
  240. return_ACPI_STATUS(AE_OK);
  241. }
  242. ACPI_WARNING((AE_INFO,
  243. "Incorrect checksum in table [%4.4s] - is %2.2X, should be %2.2X",
  244. table_header->signature, table_header->checksum,
  245. checksum));
  246. return_ACPI_STATUS(AE_BAD_CHECKSUM);
  247. }
  248. #ifdef ACPI_OBSOLETE_FUNCTIONS
  249. /*******************************************************************************
  250. *
  251. * FUNCTION: acpi_tb_handle_to_object
  252. *
  253. * PARAMETERS: table_id - Id for which the function is searching
  254. * table_desc - Pointer to return the matching table
  255. * descriptor.
  256. *
  257. * RETURN: Search the tables to find one with a matching table_id and
  258. * return a pointer to that table descriptor.
  259. *
  260. ******************************************************************************/
  261. acpi_status
  262. acpi_tb_handle_to_object(u16 table_id,
  263. struct acpi_table_desc **return_table_desc)
  264. {
  265. u32 i;
  266. struct acpi_table_desc *table_desc;
  267. ACPI_FUNCTION_NAME(tb_handle_to_object);
  268. for (i = 0; i < ACPI_TABLE_MAX; i++) {
  269. table_desc = acpi_gbl_table_lists[i].next;
  270. while (table_desc) {
  271. if (table_desc->table_id == table_id) {
  272. *return_table_desc = table_desc;
  273. return (AE_OK);
  274. }
  275. table_desc = table_desc->next;
  276. }
  277. }
  278. ACPI_ERROR((AE_INFO, "TableId=%X does not exist", table_id));
  279. return (AE_BAD_PARAMETER);
  280. }
  281. #endif