tbutils.c 8.34 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/******************************************************************************
 *
 * Module Name: tbutils - Table manipulation utilities
Andy Grover's avatar
Andy Grover committed
4
 *              $Revision: 49 $
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8
 *
 *****************************************************************************/

/*
Andy Grover's avatar
Andy Grover committed
9
 *  Copyright (C) 2000 - 2002, R. Byron Moore
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "acpi.h"
#include "actables.h"
#include "acinterp.h"


Linus Torvalds's avatar
Linus Torvalds committed
32
#define _COMPONENT          ACPI_TABLES
Andy Grover's avatar
Andy Grover committed
33
	 ACPI_MODULE_NAME    ("tbutils")
Linus Torvalds's avatar
Linus Torvalds committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48


/*******************************************************************************
 *
 * FUNCTION:    Acpi_tb_handle_to_object
 *
 * PARAMETERS:  Table_id            - Id for which the function is searching
 *              Table_desc          - Pointer to return the matching table
 *                                      descriptor.
 *
 * RETURN:      Search the tables to find one with a matching Table_id and
 *              return a pointer to that table descriptor.
 *
 ******************************************************************************/

Linus Torvalds's avatar
Linus Torvalds committed
49
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
50 51
acpi_tb_handle_to_object (
	u16                     table_id,
Linus Torvalds's avatar
Linus Torvalds committed
52
	acpi_table_desc         **table_desc)
Linus Torvalds's avatar
Linus Torvalds committed
53 54
{
	u32                     i;
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57
	acpi_table_desc         *list_head;


Andy Grover's avatar
Andy Grover committed
58
	ACPI_FUNCTION_NAME ("Tb_handle_to_object");
Linus Torvalds's avatar
Linus Torvalds committed
59 60 61 62


	for (i = 0; i < ACPI_TABLE_MAX; i++) {
		list_head = &acpi_gbl_acpi_tables[i];
Linus Torvalds's avatar
Linus Torvalds committed
63
		do {
Linus Torvalds's avatar
Linus Torvalds committed
64 65 66 67 68 69 70 71 72 73 74
			if (list_head->table_id == table_id) {
				*table_desc = list_head;
				return (AE_OK);
			}

			list_head = list_head->next;

		} while (list_head != &acpi_gbl_acpi_tables[i]);
	}


Linus Torvalds's avatar
Linus Torvalds committed
75
	ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Table_id=%X does not exist\n", table_id));
Linus Torvalds's avatar
Linus Torvalds committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	return (AE_BAD_PARAMETER);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_tb_validate_table_header
 *
 * PARAMETERS:  Table_header        - Logical pointer to the table
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Check an ACPI table header for validity
 *
 * NOTE:  Table pointers are validated as follows:
 *          1) Table pointer must point to valid physical memory
 *          2) Signature must be 4 ASCII chars, even if we don't recognize the
 *             name
 *          3) Table must be readable for length specified in the header
 *          4) Table checksum must be valid (with the exception of the FACS
 *              which has no checksum for some odd reason)
 *
 ******************************************************************************/

Linus Torvalds's avatar
Linus Torvalds committed
100
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
101
acpi_tb_validate_table_header (
Linus Torvalds's avatar
Linus Torvalds committed
102
	acpi_table_header       *table_header)
Linus Torvalds's avatar
Linus Torvalds committed
103
{
Linus Torvalds's avatar
Linus Torvalds committed
104
	acpi_name               signature;
Linus Torvalds's avatar
Linus Torvalds committed
105 106


Andy Grover's avatar
Andy Grover committed
107
	ACPI_FUNCTION_NAME ("Tb_validate_table_header");
Linus Torvalds's avatar
Linus Torvalds committed
108 109


Linus Torvalds's avatar
Linus Torvalds committed
110 111
	/* Verify that this is a valid address */

Linus Torvalds's avatar
Linus Torvalds committed
112 113 114
	if (!acpi_os_readable (table_header, sizeof (acpi_table_header))) {
		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
			"Cannot read table header at %p\n", table_header));
Linus Torvalds's avatar
Linus Torvalds committed
115 116 117 118 119 120
		return (AE_BAD_ADDRESS);
	}


	/* Ensure that the signature is 4 ASCII characters */

Andy Grover's avatar
Andy Grover committed
121
	ACPI_MOVE_UNALIGNED32_TO_32 (&signature, &table_header->signature);
Linus Torvalds's avatar
Linus Torvalds committed
122
	if (!acpi_ut_valid_acpi_name (signature)) {
Linus Torvalds's avatar
Linus Torvalds committed
123
		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
Linus Torvalds's avatar
Linus Torvalds committed
124
			"Table signature at %p [%p] has invalid characters\n",
Linus Torvalds's avatar
Linus Torvalds committed
125 126
			table_header, &signature));

Andy Grover's avatar
Andy Grover committed
127 128
		ACPI_REPORT_WARNING (("Invalid table signature %4.4s found\n", (char *) &signature));
		ACPI_DUMP_BUFFER (table_header, sizeof (acpi_table_header));
Linus Torvalds's avatar
Linus Torvalds committed
129 130 131 132 133 134
		return (AE_BAD_SIGNATURE);
	}


	/* Validate the table length */

Linus Torvalds's avatar
Linus Torvalds committed
135 136 137
	if (table_header->length < sizeof (acpi_table_header)) {
		ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
			"Invalid length in table header %p name %4.4s\n",
Andy Grover's avatar
Andy Grover committed
138
			table_header, (char *) &signature));
Linus Torvalds's avatar
Linus Torvalds committed
139

Andy Grover's avatar
Andy Grover committed
140 141
		ACPI_REPORT_WARNING (("Invalid table header length (0x%X) found\n", table_header->length));
		ACPI_DUMP_BUFFER (table_header, sizeof (acpi_table_header));
Linus Torvalds's avatar
Linus Torvalds committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		return (AE_BAD_HEADER);
	}

	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_tb_map_acpi_table
 *
 * PARAMETERS:  Physical_address        - Physical address of table to map
 *              *Size                   - Size of the table.  If zero, the size
 *                                        from the table header is used.
 *                                        Actual size is returned here.
 *              **Logical_address       - Logical address of mapped table
 *
 * RETURN:      Logical address of the mapped table.
 *
 * DESCRIPTION: Maps the physical address of table into a logical address
 *
 ******************************************************************************/

Linus Torvalds's avatar
Linus Torvalds committed
165
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
166 167 168
acpi_tb_map_acpi_table (
	ACPI_PHYSICAL_ADDRESS   physical_address,
	u32                     *size,
Linus Torvalds's avatar
Linus Torvalds committed
169
	acpi_table_header       **logical_address)
Linus Torvalds's avatar
Linus Torvalds committed
170
{
Linus Torvalds's avatar
Linus Torvalds committed
171
	acpi_table_header       *table;
Linus Torvalds's avatar
Linus Torvalds committed
172
	u32                     table_size = *size;
Linus Torvalds's avatar
Linus Torvalds committed
173 174 175
	acpi_status             status = AE_OK;


Andy Grover's avatar
Andy Grover committed
176
	ACPI_FUNCTION_NAME ("Tb_map_acpi_table");
Linus Torvalds's avatar
Linus Torvalds committed
177 178 179 180 181 182 183


	/* If size is zero, look at the table header to get the actual size */

	if ((*size) == 0) {
		/* Get the table header so we can extract the table length */

Linus Torvalds's avatar
Linus Torvalds committed
184
		status = acpi_os_map_memory (physical_address, sizeof (acpi_table_header),
Linus Torvalds's avatar
Linus Torvalds committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
				  (void **) &table);
		if (ACPI_FAILURE (status)) {
			return (status);
		}

		/* Extract the full table length before we delete the mapping */

		table_size = table->length;

		/*
		 * Validate the header and delete the mapping.
		 * We will create a mapping for the full table below.
		 */
		status = acpi_tb_validate_table_header (table);

		/* Always unmap the memory for the header */

Linus Torvalds's avatar
Linus Torvalds committed
202
		acpi_os_unmap_memory (table, sizeof (acpi_table_header));
Linus Torvalds's avatar
Linus Torvalds committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

		/* Exit if header invalid */

		if (ACPI_FAILURE (status)) {
			return (status);
		}
	}


	/* Map the physical memory for the correct length */

	status = acpi_os_map_memory (physical_address, table_size, (void **) &table);
	if (ACPI_FAILURE (status)) {
		return (status);
	}

Linus Torvalds's avatar
Linus Torvalds committed
219 220 221 222
	ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
		"Mapped memory for ACPI table, length=%d(%X) at %p\n",
		table_size, table_size, table));

Linus Torvalds's avatar
Linus Torvalds committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	*size = table_size;
	*logical_address = table;

	return (status);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_tb_verify_table_checksum
 *
 * PARAMETERS:  *Table_header           - ACPI table to verify
 *
 * RETURN:      8 bit checksum of table
 *
 * DESCRIPTION: Does an 8 bit checksum of table and returns status.  A correct
 *              table should have a checksum of 0.
 *
 ******************************************************************************/

Linus Torvalds's avatar
Linus Torvalds committed
243
acpi_status
Linus Torvalds's avatar
Linus Torvalds committed
244
acpi_tb_verify_table_checksum (
Linus Torvalds's avatar
Linus Torvalds committed
245
	acpi_table_header       *table_header)
Linus Torvalds's avatar
Linus Torvalds committed
246 247
{
	u8                      checksum;
Linus Torvalds's avatar
Linus Torvalds committed
248 249 250
	acpi_status             status = AE_OK;


Andy Grover's avatar
Andy Grover committed
251
	ACPI_FUNCTION_TRACE ("Tb_verify_table_checksum");
Linus Torvalds's avatar
Linus Torvalds committed
252 253 254 255 256 257 258 259 260


	/* Compute the checksum on the table */

	checksum = acpi_tb_checksum (table_header, table_header->length);

	/* Return the appropriate exception */

	if (checksum) {
Andy Grover's avatar
Andy Grover committed
261 262
		ACPI_REPORT_WARNING (("Invalid checksum (%X) in table %4.4s\n",
			checksum, (char *) &table_header->signature));
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265 266 267

		status = AE_BAD_CHECKSUM;
	}


Linus Torvalds's avatar
Linus Torvalds committed
268
	return_ACPI_STATUS (status);
Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_tb_checksum
 *
 * PARAMETERS:  Buffer              - Buffer to checksum
 *              Length              - Size of the buffer
 *
 * RETURNS      8 bit checksum of buffer
 *
 * DESCRIPTION: Computes an 8 bit checksum of the buffer(length) and returns it.
 *
 ******************************************************************************/

u8
acpi_tb_checksum (
	void                    *buffer,
	u32                     length)
{
Andy Grover's avatar
Andy Grover committed
290 291
	const u8                *limit;
	const u8                *rover;
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	u8                      sum = 0;


	if (buffer && length) {
		/*  Buffer and Length are valid   */

		limit = (u8 *) buffer + length;

		for (rover = buffer; rover < limit; rover++) {
			sum = (u8) (sum + *rover);
		}
	}

	return (sum);
}