tbutils.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_tb_handle_to_object
  49. *
  50. * PARAMETERS: table_id - Id for which the function is searching
  51. * table_desc - Pointer to return the matching table
  52. * descriptor.
  53. *
  54. * RETURN: Search the tables to find one with a matching table_id and
  55. * return a pointer to that table descriptor.
  56. *
  57. ******************************************************************************/
  58. #ifdef ACPI_FUTURE_USAGE
  59. acpi_status
  60. acpi_tb_handle_to_object (
  61. u16 table_id,
  62. struct acpi_table_desc **return_table_desc)
  63. {
  64. u32 i;
  65. struct acpi_table_desc *table_desc;
  66. ACPI_FUNCTION_NAME ("tb_handle_to_object");
  67. for (i = 0; i < ACPI_TABLE_MAX; i++) {
  68. table_desc = acpi_gbl_table_lists[i].next;
  69. while (table_desc) {
  70. if (table_desc->table_id == table_id) {
  71. *return_table_desc = table_desc;
  72. return (AE_OK);
  73. }
  74. table_desc = table_desc->next;
  75. }
  76. }
  77. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "table_id=%X does not exist\n", table_id));
  78. return (AE_BAD_PARAMETER);
  79. }
  80. #endif /* ACPI_FUTURE_USAGE */
  81. /*******************************************************************************
  82. *
  83. * FUNCTION: acpi_tb_validate_table_header
  84. *
  85. * PARAMETERS: table_header - Logical pointer to the table
  86. *
  87. * RETURN: Status
  88. *
  89. * DESCRIPTION: Check an ACPI table header for validity
  90. *
  91. * NOTE: Table pointers are validated as follows:
  92. * 1) Table pointer must point to valid physical memory
  93. * 2) Signature must be 4 ASCII chars, even if we don't recognize the
  94. * name
  95. * 3) Table must be readable for length specified in the header
  96. * 4) Table checksum must be valid (with the exception of the FACS
  97. * which has no checksum because it contains variable fields)
  98. *
  99. ******************************************************************************/
  100. acpi_status
  101. acpi_tb_validate_table_header (
  102. struct acpi_table_header *table_header)
  103. {
  104. acpi_name signature;
  105. ACPI_FUNCTION_NAME ("tb_validate_table_header");
  106. /* Verify that this is a valid address */
  107. if (!acpi_os_readable (table_header, sizeof (struct acpi_table_header))) {
  108. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  109. "Cannot read table header at %p\n", table_header));
  110. return (AE_BAD_ADDRESS);
  111. }
  112. /* Ensure that the signature is 4 ASCII characters */
  113. ACPI_MOVE_32_TO_32 (&signature, table_header->signature);
  114. if (!acpi_ut_valid_acpi_name (signature)) {
  115. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  116. "Table signature at %p [%p] has invalid characters\n",
  117. table_header, &signature));
  118. ACPI_REPORT_WARNING (("Invalid table signature found: [%4.4s]\n",
  119. (char *) &signature));
  120. ACPI_DUMP_BUFFER (table_header, sizeof (struct acpi_table_header));
  121. return (AE_BAD_SIGNATURE);
  122. }
  123. /* Validate the table length */
  124. if (table_header->length < sizeof (struct acpi_table_header)) {
  125. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  126. "Invalid length in table header %p name %4.4s\n",
  127. table_header, (char *) &signature));
  128. ACPI_REPORT_WARNING (("Invalid table header length (0x%X) found\n",
  129. (u32) table_header->length));
  130. ACPI_DUMP_BUFFER (table_header, sizeof (struct acpi_table_header));
  131. return (AE_BAD_HEADER);
  132. }
  133. return (AE_OK);
  134. }
  135. /*******************************************************************************
  136. *
  137. * FUNCTION: acpi_tb_verify_table_checksum
  138. *
  139. * PARAMETERS: *table_header - ACPI table to verify
  140. *
  141. * RETURN: 8 bit checksum of table
  142. *
  143. * DESCRIPTION: Does an 8 bit checksum of table and returns status. A correct
  144. * table should have a checksum of 0.
  145. *
  146. ******************************************************************************/
  147. acpi_status
  148. acpi_tb_verify_table_checksum (
  149. struct acpi_table_header *table_header)
  150. {
  151. u8 checksum;
  152. acpi_status status = AE_OK;
  153. ACPI_FUNCTION_TRACE ("tb_verify_table_checksum");
  154. /* Compute the checksum on the table */
  155. checksum = acpi_tb_checksum (table_header, table_header->length);
  156. /* Return the appropriate exception */
  157. if (checksum) {
  158. ACPI_REPORT_WARNING (("Invalid checksum in table [%4.4s] (%02X, sum %02X is not zero)\n",
  159. table_header->signature, (u32) table_header->checksum, (u32) checksum));
  160. status = AE_BAD_CHECKSUM;
  161. }
  162. return_ACPI_STATUS (status);
  163. }
  164. /*******************************************************************************
  165. *
  166. * FUNCTION: acpi_tb_checksum
  167. *
  168. * PARAMETERS: Buffer - Buffer to checksum
  169. * Length - Size of the buffer
  170. *
  171. * RETURNS 8 bit checksum of buffer
  172. *
  173. * DESCRIPTION: Computes an 8 bit checksum of the buffer(length) and returns it.
  174. *
  175. ******************************************************************************/
  176. u8
  177. acpi_tb_checksum (
  178. void *buffer,
  179. u32 length)
  180. {
  181. const u8 *limit;
  182. const u8 *rover;
  183. u8 sum = 0;
  184. if (buffer && length) {
  185. /* Buffer and Length are valid */
  186. limit = (u8 *) buffer + length;
  187. for (rover = buffer; rover < limit; rover++) {
  188. sum = (u8) (sum + *rover);
  189. }
  190. }
  191. return (sum);
  192. }