blob: 5f14403765140a4affad0235162eacbc337c6be5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: tbutils - Table manipulation utilities
4 *
5 *****************************************************************************/
6
7/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05008 * Copyright (C) 2000 - 2006, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
45#include <acpi/actables.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define _COMPONENT ACPI_TABLES
Len Brown4be44fc2005-08-05 00:44:28 -040048ACPI_MODULE_NAME("tbutils")
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Robert Moore44f6c012005-04-18 22:49:35 -040050/* Local prototypes */
Robert Moore44f6c012005-04-18 22:49:35 -040051#ifdef ACPI_OBSOLETE_FUNCTIONS
Linus Torvalds1da177e2005-04-16 15:20:36 -070052acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040053acpi_tb_handle_to_object(u16 table_id, struct acpi_table_desc **table_desc);
Robert Moore44f6c012005-04-18 22:49:35 -040054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*******************************************************************************
57 *
Robert Moore0c9938c2005-07-29 15:15:00 -070058 * FUNCTION: acpi_tb_is_table_installed
59 *
60 * PARAMETERS: new_table_desc - Descriptor for new table being installed
61 *
62 * RETURN: Status - AE_ALREADY_EXISTS if the table is already installed
63 *
64 * DESCRIPTION: Determine if an ACPI table is already installed
65 *
66 * MUTEX: Table data structures should be locked
67 *
68 ******************************************************************************/
69
Len Brown4be44fc2005-08-05 00:44:28 -040070acpi_status acpi_tb_is_table_installed(struct acpi_table_desc *new_table_desc)
Robert Moore0c9938c2005-07-29 15:15:00 -070071{
Len Brown4be44fc2005-08-05 00:44:28 -040072 struct acpi_table_desc *table_desc;
Robert Moore0c9938c2005-07-29 15:15:00 -070073
Bob Mooreb229cf92006-04-21 17:15:00 -040074 ACPI_FUNCTION_TRACE(tb_is_table_installed);
Robert Moore0c9938c2005-07-29 15:15:00 -070075
76 /* Get the list descriptor and first table descriptor */
77
78 table_desc = acpi_gbl_table_lists[new_table_desc->type].next;
79
80 /* Examine all installed tables of this type */
81
82 while (table_desc) {
Bob Moorea18ecf42005-08-15 03:42:00 -080083 /*
84 * If the table lengths match, perform a full bytewise compare. This
85 * means that we will allow tables with duplicate oem_table_id(s), as
86 * long as the tables are different in some way.
87 *
88 * Checking if the table has been loaded into the namespace means that
89 * we don't check for duplicate tables during the initial installation
90 * of tables within the RSDT/XSDT.
91 */
Robert Moore0c9938c2005-07-29 15:15:00 -070092 if ((table_desc->loaded_into_namespace) &&
Bob Moorea18ecf42005-08-15 03:42:00 -080093 (table_desc->pointer->length ==
94 new_table_desc->pointer->length)
95 &&
96 (!ACPI_MEMCMP
Bob Moore08978312005-10-21 00:00:00 -040097 (table_desc->pointer, new_table_desc->pointer,
98 new_table_desc->pointer->length))) {
Bob Moore52fc0b02006-10-02 00:00:00 -040099
Bob Moorea18ecf42005-08-15 03:42:00 -0800100 /* Match: this table is already installed */
Robert Moore0c9938c2005-07-29 15:15:00 -0700101
Len Brown4be44fc2005-08-05 00:44:28 -0400102 ACPI_DEBUG_PRINT((ACPI_DB_TABLES,
Bob Mooreb229cf92006-04-21 17:15:00 -0400103 "Table [%4.4s] already installed: Rev %X OemTableId [%8.8s]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400104 new_table_desc->pointer->signature,
105 new_table_desc->pointer->revision,
106 new_table_desc->pointer->
107 oem_table_id));
Robert Moore0c9938c2005-07-29 15:15:00 -0700108
Len Brown4be44fc2005-08-05 00:44:28 -0400109 new_table_desc->owner_id = table_desc->owner_id;
Robert Moore0c9938c2005-07-29 15:15:00 -0700110 new_table_desc->installed_desc = table_desc;
111
Len Brown4be44fc2005-08-05 00:44:28 -0400112 return_ACPI_STATUS(AE_ALREADY_EXISTS);
Robert Moore0c9938c2005-07-29 15:15:00 -0700113 }
114
115 /* Get next table on the list */
116
117 table_desc = table_desc->next;
118 }
119
Len Brown4be44fc2005-08-05 00:44:28 -0400120 return_ACPI_STATUS(AE_OK);
Robert Moore0c9938c2005-07-29 15:15:00 -0700121}
122
Robert Moore0c9938c2005-07-29 15:15:00 -0700123/*******************************************************************************
124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * FUNCTION: acpi_tb_validate_table_header
126 *
127 * PARAMETERS: table_header - Logical pointer to the table
128 *
129 * RETURN: Status
130 *
131 * DESCRIPTION: Check an ACPI table header for validity
132 *
133 * NOTE: Table pointers are validated as follows:
134 * 1) Table pointer must point to valid physical memory
135 * 2) Signature must be 4 ASCII chars, even if we don't recognize the
136 * name
137 * 3) Table must be readable for length specified in the header
138 * 4) Table checksum must be valid (with the exception of the FACS
139 * which has no checksum because it contains variable fields)
140 *
141 ******************************************************************************/
142
143acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400144acpi_tb_validate_table_header(struct acpi_table_header *table_header)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Len Brown4be44fc2005-08-05 00:44:28 -0400146 acpi_name signature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Bob Moore4a90c7e2006-01-13 16:22:00 -0500148 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* Verify that this is a valid address */
151
Len Brown4be44fc2005-08-05 00:44:28 -0400152 if (!acpi_os_readable(table_header, sizeof(struct acpi_table_header))) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500153 ACPI_ERROR((AE_INFO,
154 "Cannot read table header at %p", table_header));
Robert Moore44f6c012005-04-18 22:49:35 -0400155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return (AE_BAD_ADDRESS);
157 }
158
159 /* Ensure that the signature is 4 ASCII characters */
160
Len Brown4be44fc2005-08-05 00:44:28 -0400161 ACPI_MOVE_32_TO_32(&signature, table_header->signature);
162 if (!acpi_ut_valid_acpi_name(signature)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500163 ACPI_ERROR((AE_INFO,
164 "Table signature at %p [%p] has invalid characters",
165 table_header, &signature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Bob Mooreb8e4d892006-01-27 16:43:00 -0500167 ACPI_WARNING((AE_INFO, "Invalid table signature found: [%4.4s]",
168 ACPI_CAST_PTR(char, &signature)));
Robert Moore44f6c012005-04-18 22:49:35 -0400169
Len Brown4be44fc2005-08-05 00:44:28 -0400170 ACPI_DUMP_BUFFER(table_header,
171 sizeof(struct acpi_table_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return (AE_BAD_SIGNATURE);
173 }
174
175 /* Validate the table length */
176
Len Brown4be44fc2005-08-05 00:44:28 -0400177 if (table_header->length < sizeof(struct acpi_table_header)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500178 ACPI_ERROR((AE_INFO,
179 "Invalid length in table header %p name %4.4s",
180 table_header, (char *)&signature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Bob Mooreb8e4d892006-01-27 16:43:00 -0500182 ACPI_WARNING((AE_INFO,
183 "Invalid table header length (0x%X) found",
184 (u32) table_header->length));
Robert Moore44f6c012005-04-18 22:49:35 -0400185
Len Brown4be44fc2005-08-05 00:44:28 -0400186 ACPI_DUMP_BUFFER(table_header,
187 sizeof(struct acpi_table_header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return (AE_BAD_HEADER);
189 }
190
191 return (AE_OK);
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*******************************************************************************
195 *
Bob Moore793c2382006-03-31 00:00:00 -0500196 * FUNCTION: acpi_tb_sum_table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
Bob Moore793c2382006-03-31 00:00:00 -0500198 * PARAMETERS: Buffer - Buffer to sum
199 * Length - Size of the buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
Bob Moore793c2382006-03-31 00:00:00 -0500201 * RETURN: 8 bit sum of buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
Bob Moore793c2382006-03-31 00:00:00 -0500203 * DESCRIPTION: Computes an 8 bit sum of the buffer(length) and returns it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 *
205 ******************************************************************************/
206
Bob Moore793c2382006-03-31 00:00:00 -0500207u8 acpi_tb_sum_table(void *buffer, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Bob Moore793c2382006-03-31 00:00:00 -0500209 acpi_native_uint i;
210 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Bob Moore793c2382006-03-31 00:00:00 -0500212 if (!buffer || !length) {
213 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Bob Moore793c2382006-03-31 00:00:00 -0500215
216 for (i = 0; i < length; i++) {
217 sum = (u8) (sum + ((u8 *) buffer)[i]);
218 }
219 return (sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*******************************************************************************
223 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700224 * FUNCTION: acpi_tb_generate_checksum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *
Bob Moore793c2382006-03-31 00:00:00 -0500226 * PARAMETERS: Table - Pointer to a valid ACPI table (with a
227 * standard ACPI header)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *
Robert Moore44f6c012005-04-18 22:49:35 -0400229 * RETURN: 8 bit checksum of buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *
Bob Moore793c2382006-03-31 00:00:00 -0500231 * DESCRIPTION: Computes an 8 bit checksum of the table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *
233 ******************************************************************************/
234
Bob Moore793c2382006-03-31 00:00:00 -0500235u8 acpi_tb_generate_checksum(struct acpi_table_header * table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Bob Moore793c2382006-03-31 00:00:00 -0500237 u8 checksum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Bob Moore793c2382006-03-31 00:00:00 -0500239 /* Sum the entire table as-is */
Bob Moore52fc0b02006-10-02 00:00:00 -0400240
Bob Moore793c2382006-03-31 00:00:00 -0500241 checksum = acpi_tb_sum_table(table, table->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Bob Moore793c2382006-03-31 00:00:00 -0500243 /* Subtract off the existing checksum value in the table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Bob Moore793c2382006-03-31 00:00:00 -0500245 checksum = (u8) (checksum - table->checksum);
246
247 /* Compute the final checksum */
248
249 checksum = (u8) (0 - checksum);
250 return (checksum);
251}
252
253/*******************************************************************************
254 *
255 * FUNCTION: acpi_tb_set_checksum
256 *
257 * PARAMETERS: Table - Pointer to a valid ACPI table (with a
258 * standard ACPI header)
259 *
260 * RETURN: None. Sets the table checksum field
261 *
262 * DESCRIPTION: Computes an 8 bit checksum of the table and inserts the
263 * checksum into the table header.
264 *
265 ******************************************************************************/
266
267void acpi_tb_set_checksum(struct acpi_table_header *table)
268{
269
270 table->checksum = acpi_tb_generate_checksum(table);
271}
272
273/*******************************************************************************
274 *
275 * FUNCTION: acpi_tb_verify_table_checksum
276 *
277 * PARAMETERS: *table_header - ACPI table to verify
278 *
279 * RETURN: 8 bit checksum of table
280 *
281 * DESCRIPTION: Generates an 8 bit checksum of table and returns and compares
282 * it to the existing checksum value.
283 *
284 ******************************************************************************/
285
286acpi_status
287acpi_tb_verify_table_checksum(struct acpi_table_header *table_header)
288{
289 u8 checksum;
290
Bob Mooreb229cf92006-04-21 17:15:00 -0400291 ACPI_FUNCTION_TRACE(tb_verify_table_checksum);
Bob Moore793c2382006-03-31 00:00:00 -0500292
293 /* Compute the checksum on the table */
294
295 checksum = acpi_tb_generate_checksum(table_header);
296
297 /* Checksum ok? */
298
299 if (checksum == table_header->checksum) {
300 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Bob Moore793c2382006-03-31 00:00:00 -0500302
303 ACPI_WARNING((AE_INFO,
304 "Incorrect checksum in table [%4.4s] - is %2.2X, should be %2.2X",
305 table_header->signature, table_header->checksum,
306 checksum));
307
308 return_ACPI_STATUS(AE_BAD_CHECKSUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Robert Moore44f6c012005-04-18 22:49:35 -0400311#ifdef ACPI_OBSOLETE_FUNCTIONS
312/*******************************************************************************
313 *
314 * FUNCTION: acpi_tb_handle_to_object
315 *
316 * PARAMETERS: table_id - Id for which the function is searching
317 * table_desc - Pointer to return the matching table
318 * descriptor.
319 *
320 * RETURN: Search the tables to find one with a matching table_id and
321 * return a pointer to that table descriptor.
322 *
323 ******************************************************************************/
324
325acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400326acpi_tb_handle_to_object(u16 table_id,
Bob Moore793c2382006-03-31 00:00:00 -0500327 struct acpi_table_desc **return_table_desc)
Robert Moore44f6c012005-04-18 22:49:35 -0400328{
Len Brown4be44fc2005-08-05 00:44:28 -0400329 u32 i;
330 struct acpi_table_desc *table_desc;
Robert Moore44f6c012005-04-18 22:49:35 -0400331
Bob Mooreb229cf92006-04-21 17:15:00 -0400332 ACPI_FUNCTION_NAME(tb_handle_to_object);
Robert Moore44f6c012005-04-18 22:49:35 -0400333
334 for (i = 0; i < ACPI_TABLE_MAX; i++) {
335 table_desc = acpi_gbl_table_lists[i].next;
336 while (table_desc) {
337 if (table_desc->table_id == table_id) {
338 *return_table_desc = table_desc;
339 return (AE_OK);
340 }
341
342 table_desc = table_desc->next;
343 }
344 }
345
Bob Mooreb229cf92006-04-21 17:15:00 -0400346 ACPI_ERROR((AE_INFO, "TableId=%X does not exist", table_id));
Robert Moore44f6c012005-04-18 22:49:35 -0400347 return (AE_BAD_PARAMETER);
348}
349#endif