ia64/xen-unstable

view xen/drivers/acpi/tables/tbutils.c @ 18896:f7f8f44b9292

tools: Fix a few error-path memory leaks.

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Dec 09 12:53:19 2008 +0000 (2008-12-09)
parents ae29cd95ba7d
children
line source
1 /******************************************************************************
2 *
3 * Module Name: tbutils - table utilities
4 *
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2008, Intel Corp.
9 * 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 */
44 #include <xen/config.h>
45 #include <xen/init.h>
46 #include <acpi/acpi.h>
47 #include <acpi/actables.h>
49 #define _COMPONENT ACPI_TABLES
50 ACPI_MODULE_NAME("tbutils")
52 /* Local prototypes */
53 static acpi_physical_address
54 acpi_tb_get_root_table_entry(u8 * table_entry,
55 acpi_native_uint table_entry_size);
56 /*******************************************************************************
57 *
58 * FUNCTION: acpi_tb_check_xsdt
59 *
60 * PARAMETERS: address - Pointer to the XSDT
61 *
62 * RETURN: status
63 * AE_OK - XSDT is okay
64 * AE_NO_MEMORY - can't map XSDT
65 * AE_INVALID_TABLE_LENGTH - invalid table length
66 * AE_NULL_ENTRY - XSDT has NULL entry
67 *
68 * DESCRIPTION: validate XSDT
69 ******************************************************************************/
71 static acpi_status
72 acpi_tb_check_xsdt(acpi_physical_address address)
73 {
74 struct acpi_table_header *table;
75 u32 length;
76 u64 xsdt_entry_address;
77 u8 *table_entry;
78 u32 table_count;
79 int i;
81 table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
82 if (!table)
83 return AE_NO_MEMORY;
85 length = table->length;
86 acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
87 if (length < sizeof(struct acpi_table_header))
88 return AE_INVALID_TABLE_LENGTH;
90 table = acpi_os_map_memory(address, length);
91 if (!table)
92 return AE_NO_MEMORY;
94 /* Calculate the number of tables described in XSDT */
95 table_count =
96 (u32) ((table->length -
97 sizeof(struct acpi_table_header)) / sizeof(u64));
98 table_entry =
99 ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
100 for (i = 0; i < table_count; i++) {
101 ACPI_MOVE_64_TO_64(&xsdt_entry_address, table_entry);
102 if (!xsdt_entry_address) {
103 /* XSDT has NULL entry */
104 break;
105 }
106 table_entry += sizeof(u64);
107 }
108 acpi_os_unmap_memory(table, length);
110 if (i < table_count)
111 return AE_NULL_ENTRY;
112 else
113 return AE_OK;
114 }
116 /*******************************************************************************
117 *
118 * FUNCTION: acpi_tb_tables_loaded
119 *
120 * PARAMETERS: None
121 *
122 * RETURN: TRUE if required ACPI tables are loaded
123 *
124 * DESCRIPTION: Determine if the minimum required ACPI tables are present
125 * (FADT, FACS, DSDT)
126 *
127 ******************************************************************************/
129 u8 acpi_tb_tables_loaded(void)
130 {
132 if (acpi_gbl_root_table_list.count >= 3) {
133 return (TRUE);
134 }
136 return (FALSE);
137 }
139 /*******************************************************************************
140 *
141 * FUNCTION: acpi_tb_print_table_header
142 *
143 * PARAMETERS: Address - Table physical address
144 * Header - Table header
145 *
146 * RETURN: None
147 *
148 * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
149 *
150 ******************************************************************************/
152 void __init
153 acpi_tb_print_table_header(acpi_physical_address address,
154 struct acpi_table_header *header)
155 {
157 if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_FACS)) {
159 /* FACS only has signature and length fields of common table header */
161 ACPI_INFO((AE_INFO, "%4.4s %08lX, %04X",
162 header->signature, (unsigned long)address,
163 header->length));
164 } else if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_RSDP)) {
166 /* RSDP has no common fields */
168 ACPI_INFO((AE_INFO, "RSDP %08lX, %04X (r%d %6.6s)",
169 (unsigned long)address,
170 (ACPI_CAST_PTR(struct acpi_table_rsdp, header)->
171 revision >
172 0) ? ACPI_CAST_PTR(struct acpi_table_rsdp,
173 header)->length : 20,
174 ACPI_CAST_PTR(struct acpi_table_rsdp,
175 header)->revision,
176 ACPI_CAST_PTR(struct acpi_table_rsdp,
177 header)->oem_id));
178 } else {
179 /* Standard ACPI table with full common header */
181 ACPI_INFO((AE_INFO,
182 "%4.4s %08lX, %04X (r%d %6.6s %8.8s %8X %4.4s %8X)",
183 header->signature, (unsigned long)address,
184 header->length, header->revision, header->oem_id,
185 header->oem_table_id, header->oem_revision,
186 header->asl_compiler_id,
187 header->asl_compiler_revision));
188 }
189 }
191 /*******************************************************************************
192 *
193 * FUNCTION: acpi_tb_validate_checksum
194 *
195 * PARAMETERS: Table - ACPI table to verify
196 * Length - Length of entire table
197 *
198 * RETURN: Status
199 *
200 * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
201 * exception on bad checksum.
202 *
203 ******************************************************************************/
205 acpi_status acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
206 {
207 u8 checksum;
209 /* Compute the checksum on the table */
211 checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
213 /* Checksum ok? (should be zero) */
215 if (checksum) {
216 ACPI_WARNING((AE_INFO,
217 "Incorrect checksum in table [%4.4s] - %2.2X, should be %2.2X",
218 table->signature, table->checksum,
219 (u8) (table->checksum - checksum)));
221 #if (ACPI_CHECKSUM_ABORT)
223 return (AE_BAD_CHECKSUM);
224 #endif
225 }
227 return (AE_OK);
228 }
230 /*******************************************************************************
231 *
232 * FUNCTION: acpi_tb_checksum
233 *
234 * PARAMETERS: Buffer - Pointer to memory region to be checked
235 * Length - Length of this memory region
236 *
237 * RETURN: Checksum (u8)
238 *
239 * DESCRIPTION: Calculates circular checksum of memory region.
240 *
241 ******************************************************************************/
243 u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length)
244 {
245 u8 sum = 0;
246 u8 *end = buffer + length;
248 while (buffer < end) {
249 sum = (u8) (sum + *(buffer++));
250 }
252 return sum;
253 }
255 /*******************************************************************************
256 *
257 * FUNCTION: acpi_tb_install_table
258 *
259 * PARAMETERS: Address - Physical address of DSDT or FACS
260 * Flags - Flags
261 * Signature - Table signature, NULL if no need to
262 * match
263 * table_index - Index into root table array
264 *
265 * RETURN: None
266 *
267 * DESCRIPTION: Install an ACPI table into the global data structure.
268 *
269 ******************************************************************************/
271 void __init
272 acpi_tb_install_table(acpi_physical_address address,
273 u8 flags, char *signature, acpi_native_uint table_index)
274 {
275 struct acpi_table_header *table;
277 if (!address) {
278 ACPI_ERROR((AE_INFO,
279 "Null physical address for ACPI table [%s]",
280 signature));
281 return;
282 }
284 /* Map just the table header */
286 table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
287 if (!table) {
288 return;
289 }
291 /* If a particular signature is expected, signature must match */
293 if (signature && !ACPI_COMPARE_NAME(table->signature, signature)) {
294 ACPI_ERROR((AE_INFO,
295 "Invalid signature 0x%X for ACPI table [%s]",
296 *ACPI_CAST_PTR(u32, table->signature), signature));
297 goto unmap_and_exit;
298 }
300 /* Initialize the table entry */
302 acpi_gbl_root_table_list.tables[table_index].address = address;
303 acpi_gbl_root_table_list.tables[table_index].length = table->length;
304 acpi_gbl_root_table_list.tables[table_index].flags = flags;
306 ACPI_MOVE_32_TO_32(&
307 (acpi_gbl_root_table_list.tables[table_index].
308 signature), table->signature);
310 acpi_tb_print_table_header(address, table);
312 if (table_index == ACPI_TABLE_INDEX_DSDT) {
314 /* Global integer width is based upon revision of the DSDT */
316 acpi_ut_set_integer_width(table->revision);
317 }
319 unmap_and_exit:
320 acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
321 }
323 /*******************************************************************************
324 *
325 * FUNCTION: acpi_tb_get_root_table_entry
326 *
327 * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
328 * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
329 *
330 * RETURN: Physical address extracted from the root table
331 *
332 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
333 * both 32-bit and 64-bit platforms
334 *
335 * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
336 * 64-bit platforms.
337 *
338 ******************************************************************************/
340 static acpi_physical_address
341 acpi_tb_get_root_table_entry(u8 * table_entry,
342 acpi_native_uint table_entry_size)
343 {
344 u64 address64;
346 /*
347 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
348 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
349 */
350 if (table_entry_size == sizeof(u32)) {
351 /*
352 * 32-bit platform, RSDT: Return 32-bit table entry
353 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
354 */
355 return ((acpi_physical_address)
356 (*ACPI_CAST_PTR(u32, table_entry)));
357 } else {
358 /*
359 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
360 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local, return 64-bit
361 */
362 ACPI_MOVE_64_TO_64(&address64, table_entry);
364 #if ACPI_MACHINE_WIDTH == 32
365 if (address64 > ACPI_UINT32_MAX) {
367 /* Will truncate 64-bit address to 32 bits, issue warning */
369 ACPI_WARNING((AE_INFO,
370 "64-bit Physical Address in XSDT is too large (%8.8X%8.8X), truncating",
371 ACPI_FORMAT_UINT64(address64)));
372 }
373 #endif
374 return ((acpi_physical_address) (address64));
375 }
376 }
378 /*******************************************************************************
379 *
380 * FUNCTION: acpi_tb_parse_root_table
381 *
382 * PARAMETERS: Rsdp - Pointer to the RSDP
383 * Flags - Flags
384 *
385 * RETURN: Status
386 *
387 * DESCRIPTION: This function is called to parse the Root System Description
388 * Table (RSDT or XSDT)
389 *
390 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
391 * be mapped and cannot be copied because it contains the actual
392 * memory location of the ACPI Global Lock.
393 *
394 ******************************************************************************/
396 acpi_status __init
397 acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags)
398 {
399 struct acpi_table_rsdp *rsdp;
400 acpi_native_uint table_entry_size;
401 acpi_native_uint i;
402 u32 table_count;
403 struct acpi_table_header *table;
404 acpi_physical_address address;
405 acpi_physical_address uninitialized_var(rsdt_address);
406 u32 length;
407 u8 *table_entry;
408 acpi_status status;
410 ACPI_FUNCTION_TRACE(tb_parse_root_table);
412 /*
413 * Map the entire RSDP and extract the address of the RSDT or XSDT
414 */
415 rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
416 if (!rsdp) {
417 return_ACPI_STATUS(AE_NO_MEMORY);
418 }
420 acpi_tb_print_table_header(rsdp_address,
421 ACPI_CAST_PTR(struct acpi_table_header,
422 rsdp));
424 /* Differentiate between RSDT and XSDT root tables */
426 if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
427 /*
428 * Root table is an XSDT (64-bit physical addresses). We must use the
429 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
430 * the ACPI specification.
431 */
432 address = (acpi_physical_address) rsdp->xsdt_physical_address;
433 table_entry_size = sizeof(u64);
434 rsdt_address = (acpi_physical_address)
435 rsdp->rsdt_physical_address;
436 } else {
437 /* Root table is an RSDT (32-bit physical addresses) */
439 address = (acpi_physical_address) rsdp->rsdt_physical_address;
440 table_entry_size = sizeof(u32);
441 }
443 /*
444 * It is not possible to map more than one entry in some environments,
445 * so unmap the RSDP here before mapping other tables
446 */
447 acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
449 if (table_entry_size == sizeof(u64)) {
450 if (acpi_tb_check_xsdt(address) == AE_NULL_ENTRY) {
451 /* XSDT has NULL entry, RSDT is used */
452 address = rsdt_address;
453 table_entry_size = sizeof(u32);
454 ACPI_WARNING((AE_INFO, "BIOS XSDT has NULL entry, "
455 "using RSDT"));
456 }
457 }
458 /* Map the RSDT/XSDT table header to get the full table length */
460 table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
461 if (!table) {
462 return_ACPI_STATUS(AE_NO_MEMORY);
463 }
465 acpi_tb_print_table_header(address, table);
467 /* Get the length of the full table, verify length and map entire table */
469 length = table->length;
470 acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
472 if (length < sizeof(struct acpi_table_header)) {
473 ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
474 length));
475 return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
476 }
478 table = acpi_os_map_memory(address, length);
479 if (!table) {
480 return_ACPI_STATUS(AE_NO_MEMORY);
481 }
483 /* Validate the root table checksum */
485 status = acpi_tb_verify_checksum(table, length);
486 if (ACPI_FAILURE(status)) {
487 acpi_os_unmap_memory(table, length);
488 return_ACPI_STATUS(status);
489 }
491 /* Calculate the number of tables described in the root table */
493 table_count =
494 (u32) ((table->length -
495 sizeof(struct acpi_table_header)) / table_entry_size);
497 /*
498 * First two entries in the table array are reserved for the DSDT and FACS,
499 * which are not actually present in the RSDT/XSDT - they come from the FADT
500 */
501 table_entry =
502 ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
503 acpi_gbl_root_table_list.count = 2;
505 /*
506 * Initialize the root table array from the RSDT/XSDT
507 */
508 for (i = 0; i < table_count; i++) {
509 if (acpi_gbl_root_table_list.count >=
510 acpi_gbl_root_table_list.size) {
512 /* There is no more room in the root table array, attempt resize */
514 status = acpi_tb_resize_root_table_list();
515 if (ACPI_FAILURE(status)) {
516 ACPI_WARNING((AE_INFO,
517 "Truncating %u table entries!",
518 (unsigned)
519 (acpi_gbl_root_table_list.size -
520 acpi_gbl_root_table_list.
521 count)));
522 break;
523 }
524 }
526 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
528 acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
529 address =
530 acpi_tb_get_root_table_entry(table_entry, table_entry_size);
532 table_entry += table_entry_size;
533 acpi_gbl_root_table_list.count++;
534 }
536 /*
537 * It is not possible to map more than one entry in some environments,
538 * so unmap the root table here before mapping other tables
539 */
540 acpi_os_unmap_memory(table, length);
542 /*
543 * Complete the initialization of the root table array by examining
544 * the header of each table
545 */
546 for (i = 2; i < acpi_gbl_root_table_list.count; i++) {
547 acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
548 address, flags, NULL, i);
550 /* Special case for FADT - get the DSDT and FACS */
552 if (ACPI_COMPARE_NAME
553 (&acpi_gbl_root_table_list.tables[i].signature,
554 ACPI_SIG_FADT)) {
555 acpi_tb_parse_fadt(i, flags);
556 }
557 }
559 return_ACPI_STATUS(AE_OK);
560 }