tbutils.c 7.6 KB

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