Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Module Name: nspredef - Validation of ACPI predefined methods and objects |
| 4 | * $Revision: 1.1 $ |
| 5 | * |
| 6 | *****************************************************************************/ |
| 7 | |
| 8 | /* |
| 9 | * Copyright (C) 2000 - 2008, Intel Corp. |
| 10 | * All rights reserved. |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions, and the following disclaimer, |
| 17 | * without modification. |
| 18 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer |
| 19 | * substantially similar to the "NO WARRANTY" disclaimer below |
| 20 | * ("Disclaimer") and any redistribution must be conditioned upon |
| 21 | * including a substantially similar Disclaimer requirement for further |
| 22 | * binary redistribution. |
| 23 | * 3. Neither the names of the above-listed copyright holders nor the names |
| 24 | * of any contributors may be used to endorse or promote products derived |
| 25 | * from this software without specific prior written permission. |
| 26 | * |
| 27 | * Alternatively, this software may be distributed under the terms of the |
| 28 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 29 | * Software Foundation. |
| 30 | * |
| 31 | * NO WARRANTY |
| 32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 33 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 34 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR |
| 35 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 36 | * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 38 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 39 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 40 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 41 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 42 | * POSSIBILITY OF SUCH DAMAGES. |
| 43 | */ |
| 44 | |
| 45 | #include <acpi/acpi.h> |
| 46 | #include <acpi/acnamesp.h> |
| 47 | #include <acpi/acpredef.h> |
| 48 | |
| 49 | #define _COMPONENT ACPI_NAMESPACE |
| 50 | ACPI_MODULE_NAME("nspredef") |
| 51 | |
| 52 | /******************************************************************************* |
| 53 | * |
| 54 | * This module validates predefined ACPI objects that appear in the namespace, |
| 55 | * at the time they are evaluated (via acpi_evaluate_object). The purpose of this |
| 56 | * validation is to detect problems with BIOS-exposed predefined ACPI objects |
| 57 | * before the results are returned to the ACPI-related drivers. |
| 58 | * |
| 59 | * There are several areas that are validated: |
| 60 | * |
| 61 | * 1) The number of input arguments as defined by the method/object in the |
| 62 | * ASL is validated against the ACPI specification. |
| 63 | * 2) The type of the return object (if any) is validated against the ACPI |
| 64 | * specification. |
| 65 | * 3) For returned package objects, the count of package elements is |
| 66 | * validated, as well as the type of each package element. Nested |
| 67 | * packages are supported. |
| 68 | * |
| 69 | * For any problems found, a warning message is issued. |
| 70 | * |
| 71 | ******************************************************************************/ |
| 72 | /* Local prototypes */ |
| 73 | static acpi_status |
| 74 | acpi_ns_check_package(char *pathname, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 75 | union acpi_operand_object **return_object_ptr, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 76 | const union acpi_predefined_info *predefined); |
| 77 | |
| 78 | static acpi_status |
| 79 | acpi_ns_check_package_elements(char *pathname, |
| 80 | union acpi_operand_object **elements, |
| 81 | u8 type1, u32 count1, u8 type2, u32 count2); |
| 82 | |
| 83 | static acpi_status |
| 84 | acpi_ns_check_object_type(char *pathname, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 85 | union acpi_operand_object **return_object_ptr, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 86 | u32 expected_btypes, u32 package_index); |
| 87 | |
| 88 | static acpi_status |
| 89 | acpi_ns_check_reference(char *pathname, |
| 90 | union acpi_operand_object *return_object); |
| 91 | |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 92 | static acpi_status |
| 93 | acpi_ns_repair_object(u32 expected_btypes, |
| 94 | u32 package_index, |
| 95 | union acpi_operand_object **return_object_ptr); |
| 96 | |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 97 | /* |
| 98 | * Names for the types that can be returned by the predefined objects. |
| 99 | * Used for warning messages. Must be in the same order as the ACPI_RTYPEs |
| 100 | */ |
| 101 | static const char *acpi_rtype_names[] = { |
| 102 | "/Integer", |
| 103 | "/String", |
| 104 | "/Buffer", |
| 105 | "/Package", |
| 106 | "/Reference", |
| 107 | }; |
| 108 | |
| 109 | #define ACPI_NOT_PACKAGE ACPI_UINT32_MAX |
| 110 | |
| 111 | /******************************************************************************* |
| 112 | * |
| 113 | * FUNCTION: acpi_ns_check_predefined_names |
| 114 | * |
| 115 | * PARAMETERS: Node - Namespace node for the method/object |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 116 | * return_object_ptr - Pointer to the object returned from the |
| 117 | * evaluation of a method or object |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 118 | * |
| 119 | * RETURN: Status |
| 120 | * |
| 121 | * DESCRIPTION: Check an ACPI name for a match in the predefined name list. |
| 122 | * |
| 123 | ******************************************************************************/ |
| 124 | |
| 125 | acpi_status |
| 126 | acpi_ns_check_predefined_names(struct acpi_namespace_node *node, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 127 | union acpi_operand_object **return_object_ptr) |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 128 | { |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 129 | union acpi_operand_object *return_object = *return_object_ptr; |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 130 | acpi_status status = AE_OK; |
| 131 | const union acpi_predefined_info *predefined; |
| 132 | char *pathname; |
| 133 | |
| 134 | /* Match the name for this method/object against the predefined list */ |
| 135 | |
| 136 | predefined = acpi_ns_check_for_predefined_name(node); |
| 137 | if (!predefined) { |
| 138 | |
| 139 | /* Name was not one of the predefined names */ |
| 140 | |
| 141 | return (AE_OK); |
| 142 | } |
| 143 | |
| 144 | /* Get the full pathname to the object, for use in error messages */ |
| 145 | |
| 146 | pathname = acpi_ns_get_external_pathname(node); |
| 147 | if (!pathname) { |
| 148 | pathname = ACPI_CAST_PTR(char, predefined->info.name); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Check that the parameter count for this method is in accordance |
| 153 | * with the ACPI specification. |
| 154 | */ |
| 155 | acpi_ns_check_parameter_count(pathname, node, predefined); |
| 156 | |
| 157 | /* |
| 158 | * If there is no return value, check if we require a return value for |
| 159 | * this predefined name. Either one return value is expected, or none, |
| 160 | * for both methods and other objects. |
| 161 | * |
| 162 | * Exit now if there is no return object. Warning if one was expected. |
| 163 | */ |
| 164 | if (!return_object) { |
| 165 | if ((predefined->info.expected_btypes) && |
| 166 | (!(predefined->info.expected_btypes & ACPI_RTYPE_NONE))) { |
| 167 | ACPI_ERROR((AE_INFO, |
| 168 | "%s: Missing expected return value", |
| 169 | pathname)); |
| 170 | |
| 171 | status = AE_AML_NO_RETURN_VALUE; |
| 172 | } |
| 173 | goto exit; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * We have a return value, but if one wasn't expected, just exit, this is |
| 178 | * not a problem |
| 179 | * |
| 180 | * For example, if "Implicit return value" is enabled, methods will |
| 181 | * always return a value |
| 182 | */ |
| 183 | if (!predefined->info.expected_btypes) { |
| 184 | goto exit; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Check that the type of the return object is what is expected for |
| 189 | * this predefined name |
| 190 | */ |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 191 | status = acpi_ns_check_object_type(pathname, return_object_ptr, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 192 | predefined->info.expected_btypes, |
| 193 | ACPI_NOT_PACKAGE); |
| 194 | if (ACPI_FAILURE(status)) { |
| 195 | goto exit; |
| 196 | } |
| 197 | |
| 198 | /* For returned Package objects, check the type of all sub-objects */ |
| 199 | |
| 200 | if (ACPI_GET_OBJECT_TYPE(return_object) == ACPI_TYPE_PACKAGE) { |
| 201 | status = |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 202 | acpi_ns_check_package(pathname, return_object_ptr, |
| 203 | predefined); |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | exit: |
| 207 | if (pathname) { |
| 208 | ACPI_FREE(pathname); |
| 209 | } |
| 210 | |
| 211 | return (status); |
| 212 | } |
| 213 | |
| 214 | /******************************************************************************* |
| 215 | * |
| 216 | * FUNCTION: acpi_ns_check_parameter_count |
| 217 | * |
| 218 | * PARAMETERS: Pathname - Full pathname to the node (for error msgs) |
| 219 | * Node - Namespace node for the method/object |
| 220 | * Predefined - Pointer to entry in predefined name table |
| 221 | * |
| 222 | * RETURN: None |
| 223 | * |
| 224 | * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a |
| 225 | * predefined name is what is expected (i.e., what is defined in |
| 226 | * the ACPI specification for this predefined name.) |
| 227 | * |
| 228 | ******************************************************************************/ |
| 229 | |
| 230 | void |
| 231 | acpi_ns_check_parameter_count(char *pathname, |
| 232 | struct acpi_namespace_node *node, |
| 233 | const union acpi_predefined_info *predefined) |
| 234 | { |
| 235 | u32 param_count; |
| 236 | u32 required_params_current; |
| 237 | u32 required_params_old; |
| 238 | |
| 239 | /* |
| 240 | * Check that the ASL-defined parameter count is what is expected for |
| 241 | * this predefined name. |
| 242 | * |
| 243 | * Methods have 0-7 parameters. All other types have zero. |
| 244 | */ |
| 245 | param_count = 0; |
| 246 | if (node->type == ACPI_TYPE_METHOD) { |
| 247 | param_count = node->object->method.param_count; |
| 248 | } |
| 249 | |
| 250 | /* Validate parameter count - allow two different legal counts (_SCP) */ |
| 251 | |
| 252 | required_params_current = predefined->info.param_count & 0x0F; |
| 253 | required_params_old = predefined->info.param_count >> 4; |
| 254 | |
| 255 | if ((param_count != required_params_current) && |
| 256 | (param_count != required_params_old)) { |
| 257 | ACPI_WARNING((AE_INFO, |
| 258 | "%s: Parameter count mismatch - ASL declared %d, expected %d", |
| 259 | pathname, param_count, required_params_current)); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /******************************************************************************* |
| 264 | * |
| 265 | * FUNCTION: acpi_ns_check_for_predefined_name |
| 266 | * |
| 267 | * PARAMETERS: Node - Namespace node for the method/object |
| 268 | * |
| 269 | * RETURN: Pointer to entry in predefined table. NULL indicates not found. |
| 270 | * |
| 271 | * DESCRIPTION: Check an object name against the predefined object list. |
| 272 | * |
| 273 | ******************************************************************************/ |
| 274 | |
| 275 | const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct |
| 276 | acpi_namespace_node |
| 277 | *node) |
| 278 | { |
| 279 | const union acpi_predefined_info *this_name; |
| 280 | |
| 281 | /* Quick check for a predefined name, first character must be underscore */ |
| 282 | |
| 283 | if (node->name.ascii[0] != '_') { |
| 284 | return (NULL); |
| 285 | } |
| 286 | |
| 287 | /* Search info table for a predefined method/object name */ |
| 288 | |
| 289 | this_name = predefined_names; |
| 290 | while (this_name->info.name[0]) { |
| 291 | if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) { |
| 292 | |
| 293 | /* Return pointer to this table entry */ |
| 294 | |
| 295 | return (this_name); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Skip next entry in the table if this name returns a Package |
| 300 | * (next entry contains the package info) |
| 301 | */ |
| 302 | if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) { |
| 303 | this_name++; |
| 304 | } |
| 305 | |
| 306 | this_name++; |
| 307 | } |
| 308 | |
| 309 | return (NULL); |
| 310 | } |
| 311 | |
| 312 | /******************************************************************************* |
| 313 | * |
| 314 | * FUNCTION: acpi_ns_check_package |
| 315 | * |
| 316 | * PARAMETERS: Pathname - Full pathname to the node (for error msgs) |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 317 | * return_object_ptr - Pointer to the object returned from the |
| 318 | * evaluation of a method or object |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 319 | * Predefined - Pointer to entry in predefined name table |
| 320 | * |
| 321 | * RETURN: Status |
| 322 | * |
| 323 | * DESCRIPTION: Check a returned package object for the correct count and |
| 324 | * correct type of all sub-objects. |
| 325 | * |
| 326 | ******************************************************************************/ |
| 327 | |
| 328 | static acpi_status |
| 329 | acpi_ns_check_package(char *pathname, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 330 | union acpi_operand_object **return_object_ptr, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 331 | const union acpi_predefined_info *predefined) |
| 332 | { |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 333 | union acpi_operand_object *return_object = *return_object_ptr; |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 334 | const union acpi_predefined_info *package; |
| 335 | union acpi_operand_object *sub_package; |
| 336 | union acpi_operand_object **elements; |
| 337 | union acpi_operand_object **sub_elements; |
| 338 | acpi_status status; |
| 339 | u32 expected_count; |
| 340 | u32 count; |
| 341 | u32 i; |
| 342 | u32 j; |
| 343 | |
| 344 | ACPI_FUNCTION_NAME(ns_check_package); |
| 345 | |
| 346 | /* The package info for this name is in the next table entry */ |
| 347 | |
| 348 | package = predefined + 1; |
| 349 | |
| 350 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, |
| 351 | "%s Validating return Package of Type %X, Count %X\n", |
| 352 | pathname, package->ret_info.type, |
| 353 | return_object->package.count)); |
| 354 | |
| 355 | /* Extract package count and elements array */ |
| 356 | |
| 357 | elements = return_object->package.elements; |
| 358 | count = return_object->package.count; |
| 359 | |
| 360 | /* The package must have at least one element, else invalid */ |
| 361 | |
| 362 | if (!count) { |
| 363 | ACPI_WARNING((AE_INFO, |
| 364 | "%s: Return Package has no elements (empty)", |
| 365 | pathname)); |
| 366 | |
| 367 | return (AE_AML_OPERAND_VALUE); |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Decode the type of the expected package contents |
| 372 | * |
| 373 | * PTYPE1 packages contain no subpackages |
| 374 | * PTYPE2 packages contain sub-packages |
| 375 | */ |
| 376 | switch (package->ret_info.type) { |
| 377 | case ACPI_PTYPE1_FIXED: |
| 378 | |
| 379 | /* |
| 380 | * The package count is fixed and there are no sub-packages |
| 381 | * |
| 382 | * If package is too small, exit. |
| 383 | * If package is larger than expected, issue warning but continue |
| 384 | */ |
| 385 | expected_count = |
| 386 | package->ret_info.count1 + package->ret_info.count2; |
| 387 | if (count < expected_count) { |
| 388 | goto package_too_small; |
| 389 | } else if (count > expected_count) { |
| 390 | ACPI_WARNING((AE_INFO, |
| 391 | "%s: Return Package is larger than needed - " |
| 392 | "found %u, expected %u", pathname, count, |
| 393 | expected_count)); |
| 394 | } |
| 395 | |
| 396 | /* Validate all elements of the returned package */ |
| 397 | |
| 398 | status = acpi_ns_check_package_elements(pathname, elements, |
| 399 | package->ret_info. |
| 400 | object_type1, |
| 401 | package->ret_info. |
| 402 | count1, |
| 403 | package->ret_info. |
| 404 | object_type2, |
| 405 | package->ret_info. |
| 406 | count2); |
| 407 | if (ACPI_FAILURE(status)) { |
| 408 | return (status); |
| 409 | } |
| 410 | break; |
| 411 | |
| 412 | case ACPI_PTYPE1_VAR: |
| 413 | |
| 414 | /* |
| 415 | * The package count is variable, there are no sub-packages, and all |
| 416 | * elements must be of the same type |
| 417 | */ |
| 418 | for (i = 0; i < count; i++) { |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 419 | status = acpi_ns_check_object_type(pathname, elements, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 420 | package->ret_info. |
| 421 | object_type1, i); |
| 422 | if (ACPI_FAILURE(status)) { |
| 423 | return (status); |
| 424 | } |
| 425 | elements++; |
| 426 | } |
| 427 | break; |
| 428 | |
| 429 | case ACPI_PTYPE1_OPTION: |
| 430 | |
| 431 | /* |
| 432 | * The package count is variable, there are no sub-packages. There are |
| 433 | * a fixed number of required elements, and a variable number of |
| 434 | * optional elements. |
| 435 | * |
| 436 | * Check if package is at least as large as the minimum required |
| 437 | */ |
| 438 | expected_count = package->ret_info3.count; |
| 439 | if (count < expected_count) { |
| 440 | goto package_too_small; |
| 441 | } |
| 442 | |
| 443 | /* Variable number of sub-objects */ |
| 444 | |
| 445 | for (i = 0; i < count; i++) { |
| 446 | if (i < package->ret_info3.count) { |
| 447 | |
| 448 | /* These are the required package elements (0, 1, or 2) */ |
| 449 | |
| 450 | status = |
| 451 | acpi_ns_check_object_type(pathname, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 452 | elements, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 453 | package-> |
| 454 | ret_info3. |
| 455 | object_type[i], |
| 456 | i); |
| 457 | if (ACPI_FAILURE(status)) { |
| 458 | return (status); |
| 459 | } |
| 460 | } else { |
| 461 | /* These are the optional package elements */ |
| 462 | |
| 463 | status = |
| 464 | acpi_ns_check_object_type(pathname, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 465 | elements, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 466 | package-> |
| 467 | ret_info3. |
| 468 | tail_object_type, |
| 469 | i); |
| 470 | if (ACPI_FAILURE(status)) { |
| 471 | return (status); |
| 472 | } |
| 473 | } |
| 474 | elements++; |
| 475 | } |
| 476 | break; |
| 477 | |
| 478 | case ACPI_PTYPE2_PKG_COUNT: |
| 479 | |
| 480 | /* First element is the (Integer) count of sub-packages to follow */ |
| 481 | |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 482 | status = acpi_ns_check_object_type(pathname, elements, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 483 | ACPI_RTYPE_INTEGER, 0); |
| 484 | if (ACPI_FAILURE(status)) { |
| 485 | return (status); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Count cannot be larger than the parent package length, but allow it |
| 490 | * to be smaller. The >= accounts for the Integer above. |
| 491 | */ |
| 492 | expected_count = (u32) (*elements)->integer.value; |
| 493 | if (expected_count >= count) { |
| 494 | goto package_too_small; |
| 495 | } |
| 496 | |
| 497 | count = expected_count; |
| 498 | elements++; |
| 499 | |
| 500 | /* Now we can walk the sub-packages */ |
| 501 | |
| 502 | /*lint -fallthrough */ |
| 503 | |
| 504 | case ACPI_PTYPE2: |
| 505 | case ACPI_PTYPE2_FIXED: |
| 506 | case ACPI_PTYPE2_MIN: |
| 507 | case ACPI_PTYPE2_COUNT: |
| 508 | |
| 509 | /* |
| 510 | * These types all return a single package that consists of a variable |
| 511 | * number of sub-packages |
| 512 | */ |
| 513 | for (i = 0; i < count; i++) { |
| 514 | sub_package = *elements; |
| 515 | sub_elements = sub_package->package.elements; |
| 516 | |
| 517 | /* Each sub-object must be of type Package */ |
| 518 | |
| 519 | status = |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 520 | acpi_ns_check_object_type(pathname, &sub_package, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 521 | ACPI_RTYPE_PACKAGE, i); |
| 522 | if (ACPI_FAILURE(status)) { |
| 523 | return (status); |
| 524 | } |
| 525 | |
| 526 | /* Examine the different types of sub-packages */ |
| 527 | |
| 528 | switch (package->ret_info.type) { |
| 529 | case ACPI_PTYPE2: |
| 530 | case ACPI_PTYPE2_PKG_COUNT: |
| 531 | |
| 532 | /* Each subpackage has a fixed number of elements */ |
| 533 | |
| 534 | expected_count = |
| 535 | package->ret_info.count1 + |
| 536 | package->ret_info.count2; |
| 537 | if (sub_package->package.count != |
| 538 | expected_count) { |
| 539 | count = sub_package->package.count; |
| 540 | goto package_too_small; |
| 541 | } |
| 542 | |
| 543 | status = |
| 544 | acpi_ns_check_package_elements(pathname, |
| 545 | sub_elements, |
| 546 | package-> |
| 547 | ret_info. |
| 548 | object_type1, |
| 549 | package-> |
| 550 | ret_info. |
| 551 | count1, |
| 552 | package-> |
| 553 | ret_info. |
| 554 | object_type2, |
| 555 | package-> |
| 556 | ret_info. |
| 557 | count2); |
| 558 | if (ACPI_FAILURE(status)) { |
| 559 | return (status); |
| 560 | } |
| 561 | break; |
| 562 | |
| 563 | case ACPI_PTYPE2_FIXED: |
| 564 | |
| 565 | /* Each sub-package has a fixed length */ |
| 566 | |
| 567 | expected_count = package->ret_info2.count; |
| 568 | if (sub_package->package.count < expected_count) { |
| 569 | count = sub_package->package.count; |
| 570 | goto package_too_small; |
| 571 | } |
| 572 | |
| 573 | /* Check the type of each sub-package element */ |
| 574 | |
| 575 | for (j = 0; j < expected_count; j++) { |
| 576 | status = |
| 577 | acpi_ns_check_object_type(pathname, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 578 | &sub_elements[j], |
| 579 | package->ret_info2.object_type[j], j); |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 580 | if (ACPI_FAILURE(status)) { |
| 581 | return (status); |
| 582 | } |
| 583 | } |
| 584 | break; |
| 585 | |
| 586 | case ACPI_PTYPE2_MIN: |
| 587 | |
| 588 | /* Each sub-package has a variable but minimum length */ |
| 589 | |
| 590 | expected_count = package->ret_info.count1; |
| 591 | if (sub_package->package.count < expected_count) { |
| 592 | count = sub_package->package.count; |
| 593 | goto package_too_small; |
| 594 | } |
| 595 | |
| 596 | /* Check the type of each sub-package element */ |
| 597 | |
| 598 | status = |
| 599 | acpi_ns_check_package_elements(pathname, |
| 600 | sub_elements, |
| 601 | package-> |
| 602 | ret_info. |
| 603 | object_type1, |
| 604 | sub_package-> |
| 605 | package. |
| 606 | count, 0, 0); |
| 607 | if (ACPI_FAILURE(status)) { |
| 608 | return (status); |
| 609 | } |
| 610 | break; |
| 611 | |
| 612 | case ACPI_PTYPE2_COUNT: |
| 613 | |
| 614 | /* First element is the (Integer) count of elements to follow */ |
| 615 | |
| 616 | status = |
| 617 | acpi_ns_check_object_type(pathname, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 618 | sub_elements, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 619 | ACPI_RTYPE_INTEGER, |
| 620 | 0); |
| 621 | if (ACPI_FAILURE(status)) { |
| 622 | return (status); |
| 623 | } |
| 624 | |
| 625 | /* Make sure package is large enough for the Count */ |
| 626 | |
| 627 | expected_count = |
| 628 | (u32) (*sub_elements)->integer.value; |
| 629 | if (sub_package->package.count < expected_count) { |
| 630 | count = sub_package->package.count; |
| 631 | goto package_too_small; |
| 632 | } |
| 633 | |
| 634 | /* Check the type of each sub-package element */ |
| 635 | |
| 636 | status = |
| 637 | acpi_ns_check_package_elements(pathname, |
| 638 | (sub_elements |
| 639 | + 1), |
| 640 | package-> |
| 641 | ret_info. |
| 642 | object_type1, |
| 643 | (expected_count |
| 644 | - 1), 0, 0); |
| 645 | if (ACPI_FAILURE(status)) { |
| 646 | return (status); |
| 647 | } |
| 648 | break; |
| 649 | |
| 650 | default: |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | elements++; |
| 655 | } |
| 656 | break; |
| 657 | |
| 658 | default: |
| 659 | |
| 660 | /* Should not get here if predefined info table is correct */ |
| 661 | |
| 662 | ACPI_WARNING((AE_INFO, |
| 663 | "%s: Invalid internal return type in table entry: %X", |
| 664 | pathname, package->ret_info.type)); |
| 665 | |
| 666 | return (AE_AML_INTERNAL); |
| 667 | } |
| 668 | |
| 669 | return (AE_OK); |
| 670 | |
| 671 | package_too_small: |
| 672 | |
| 673 | /* Error exit for the case with an incorrect package count */ |
| 674 | |
| 675 | ACPI_WARNING((AE_INFO, "%s: Return Package is too small - " |
| 676 | "found %u, expected %u", pathname, count, |
| 677 | expected_count)); |
| 678 | |
| 679 | return (AE_AML_OPERAND_VALUE); |
| 680 | } |
| 681 | |
| 682 | /******************************************************************************* |
| 683 | * |
| 684 | * FUNCTION: acpi_ns_check_package_elements |
| 685 | * |
| 686 | * PARAMETERS: Pathname - Full pathname to the node (for error msgs) |
| 687 | * Elements - Pointer to the package elements array |
| 688 | * Type1 - Object type for first group |
| 689 | * Count1 - Count for first group |
| 690 | * Type2 - Object type for second group |
| 691 | * Count2 - Count for second group |
| 692 | * |
| 693 | * RETURN: Status |
| 694 | * |
| 695 | * DESCRIPTION: Check that all elements of a package are of the correct object |
| 696 | * type. Supports up to two groups of different object types. |
| 697 | * |
| 698 | ******************************************************************************/ |
| 699 | |
| 700 | static acpi_status |
| 701 | acpi_ns_check_package_elements(char *pathname, |
| 702 | union acpi_operand_object **elements, |
| 703 | u8 type1, u32 count1, u8 type2, u32 count2) |
| 704 | { |
| 705 | union acpi_operand_object **this_element = elements; |
| 706 | acpi_status status; |
| 707 | u32 i; |
| 708 | |
| 709 | /* |
| 710 | * Up to two groups of package elements are supported by the data |
| 711 | * structure. All elements in each group must be of the same type. |
| 712 | * The second group can have a count of zero. |
| 713 | */ |
| 714 | for (i = 0; i < count1; i++) { |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 715 | status = acpi_ns_check_object_type(pathname, this_element, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 716 | type1, i); |
| 717 | if (ACPI_FAILURE(status)) { |
| 718 | return (status); |
| 719 | } |
| 720 | this_element++; |
| 721 | } |
| 722 | |
| 723 | for (i = 0; i < count2; i++) { |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 724 | status = acpi_ns_check_object_type(pathname, this_element, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 725 | type2, (i + count1)); |
| 726 | if (ACPI_FAILURE(status)) { |
| 727 | return (status); |
| 728 | } |
| 729 | this_element++; |
| 730 | } |
| 731 | |
| 732 | return (AE_OK); |
| 733 | } |
| 734 | |
| 735 | /******************************************************************************* |
| 736 | * |
| 737 | * FUNCTION: acpi_ns_check_object_type |
| 738 | * |
| 739 | * PARAMETERS: Pathname - Full pathname to the node (for error msgs) |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 740 | * return_object_ptr - Pointer to the object returned from the |
| 741 | * evaluation of a method or object |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 742 | * expected_btypes - Bitmap of expected return type(s) |
| 743 | * package_index - Index of object within parent package (if |
| 744 | * applicable - ACPI_NOT_PACKAGE otherwise) |
| 745 | * |
| 746 | * RETURN: Status |
| 747 | * |
| 748 | * DESCRIPTION: Check the type of the return object against the expected object |
| 749 | * type(s). Use of Btype allows multiple expected object types. |
| 750 | * |
| 751 | ******************************************************************************/ |
| 752 | |
| 753 | static acpi_status |
| 754 | acpi_ns_check_object_type(char *pathname, |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 755 | union acpi_operand_object **return_object_ptr, |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 756 | u32 expected_btypes, u32 package_index) |
| 757 | { |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 758 | union acpi_operand_object *return_object = *return_object_ptr; |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 759 | acpi_status status = AE_OK; |
| 760 | u32 return_btype; |
| 761 | char type_buffer[48]; /* Room for 5 types */ |
| 762 | u32 this_rtype; |
| 763 | u32 i; |
| 764 | u32 j; |
| 765 | |
| 766 | /* |
| 767 | * If we get a NULL return_object here, it is a NULL package element, |
| 768 | * and this is always an error. |
| 769 | */ |
| 770 | if (!return_object) { |
| 771 | goto type_error_exit; |
| 772 | } |
| 773 | |
| 774 | /* A Namespace node should not get here, but make sure */ |
| 775 | |
| 776 | if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) { |
| 777 | ACPI_WARNING((AE_INFO, |
| 778 | "%s: Invalid return type - Found a Namespace node [%4.4s] type %s", |
| 779 | pathname, return_object->node.name.ascii, |
| 780 | acpi_ut_get_type_name(return_object->node.type))); |
| 781 | return (AE_AML_OPERAND_TYPE); |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type. |
| 786 | * The bitmapped type allows multiple possible return types. |
| 787 | * |
| 788 | * Note, the cases below must handle all of the possible types returned |
| 789 | * from all of the predefined names (including elements of returned |
| 790 | * packages) |
| 791 | */ |
| 792 | switch (ACPI_GET_OBJECT_TYPE(return_object)) { |
| 793 | case ACPI_TYPE_INTEGER: |
| 794 | return_btype = ACPI_RTYPE_INTEGER; |
| 795 | break; |
| 796 | |
| 797 | case ACPI_TYPE_BUFFER: |
| 798 | return_btype = ACPI_RTYPE_BUFFER; |
| 799 | break; |
| 800 | |
| 801 | case ACPI_TYPE_STRING: |
| 802 | return_btype = ACPI_RTYPE_STRING; |
| 803 | break; |
| 804 | |
| 805 | case ACPI_TYPE_PACKAGE: |
| 806 | return_btype = ACPI_RTYPE_PACKAGE; |
| 807 | break; |
| 808 | |
| 809 | case ACPI_TYPE_LOCAL_REFERENCE: |
| 810 | return_btype = ACPI_RTYPE_REFERENCE; |
| 811 | break; |
| 812 | |
| 813 | default: |
| 814 | /* Not one of the supported objects, must be incorrect */ |
| 815 | |
| 816 | goto type_error_exit; |
| 817 | } |
| 818 | |
| 819 | /* Is the object one of the expected types? */ |
| 820 | |
| 821 | if (!(return_btype & expected_btypes)) { |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 822 | |
| 823 | /* Type mismatch -- attempt repair of the returned object */ |
| 824 | |
| 825 | status = acpi_ns_repair_object(expected_btypes, package_index, |
| 826 | return_object_ptr); |
| 827 | if (ACPI_SUCCESS(status)) { |
| 828 | return (status); |
| 829 | } |
Bob Moore | e8707b3 | 2008-09-28 15:26:17 +0800 | [diff] [blame] | 830 | goto type_error_exit; |
| 831 | } |
| 832 | |
| 833 | /* For reference objects, check that the reference type is correct */ |
| 834 | |
| 835 | if (ACPI_GET_OBJECT_TYPE(return_object) == ACPI_TYPE_LOCAL_REFERENCE) { |
| 836 | status = acpi_ns_check_reference(pathname, return_object); |
| 837 | } |
| 838 | |
| 839 | return (status); |
| 840 | |
| 841 | type_error_exit: |
| 842 | |
| 843 | /* Create a string with all expected types for this predefined object */ |
| 844 | |
| 845 | j = 1; |
| 846 | type_buffer[0] = 0; |
| 847 | this_rtype = ACPI_RTYPE_INTEGER; |
| 848 | |
| 849 | for (i = 0; i < ACPI_NUM_RTYPES; i++) { |
| 850 | |
| 851 | /* If one of the expected types, concatenate the name of this type */ |
| 852 | |
| 853 | if (expected_btypes & this_rtype) { |
| 854 | ACPI_STRCAT(type_buffer, &acpi_rtype_names[i][j]); |
| 855 | j = 0; /* Use name separator from now on */ |
| 856 | } |
| 857 | this_rtype <<= 1; /* Next Rtype */ |
| 858 | } |
| 859 | |
| 860 | if (package_index == ACPI_NOT_PACKAGE) { |
| 861 | ACPI_WARNING((AE_INFO, |
| 862 | "%s: Return type mismatch - found %s, expected %s", |
| 863 | pathname, |
| 864 | acpi_ut_get_object_type_name(return_object), |
| 865 | type_buffer)); |
| 866 | } else { |
| 867 | ACPI_WARNING((AE_INFO, |
| 868 | "%s: Return Package type mismatch at index %u - " |
| 869 | "found %s, expected %s", pathname, package_index, |
| 870 | acpi_ut_get_object_type_name(return_object), |
| 871 | type_buffer)); |
| 872 | } |
| 873 | |
| 874 | return (AE_AML_OPERAND_TYPE); |
| 875 | } |
| 876 | |
| 877 | /******************************************************************************* |
| 878 | * |
| 879 | * FUNCTION: acpi_ns_check_reference |
| 880 | * |
| 881 | * PARAMETERS: Pathname - Full pathname to the node (for error msgs) |
| 882 | * return_object - Object returned from the evaluation of a |
| 883 | * method or object |
| 884 | * |
| 885 | * RETURN: Status |
| 886 | * |
| 887 | * DESCRIPTION: Check a returned reference object for the correct reference |
| 888 | * type. The only reference type that can be returned from a |
| 889 | * predefined method is a named reference. All others are invalid. |
| 890 | * |
| 891 | ******************************************************************************/ |
| 892 | |
| 893 | static acpi_status |
| 894 | acpi_ns_check_reference(char *pathname, |
| 895 | union acpi_operand_object *return_object) |
| 896 | { |
| 897 | |
| 898 | /* |
| 899 | * Check the reference object for the correct reference type (opcode). |
| 900 | * The only type of reference that can be converted to an union acpi_object is |
| 901 | * a reference to a named object (reference class: NAME) |
| 902 | */ |
| 903 | if (return_object->reference.class == ACPI_REFCLASS_NAME) { |
| 904 | return (AE_OK); |
| 905 | } |
| 906 | |
| 907 | ACPI_WARNING((AE_INFO, |
| 908 | "%s: Return type mismatch - unexpected reference object type [%s] %2.2X", |
| 909 | pathname, acpi_ut_get_reference_name(return_object), |
| 910 | return_object->reference.class)); |
| 911 | |
| 912 | return (AE_AML_OPERAND_TYPE); |
| 913 | } |
Bob Moore | a647b5c | 2008-11-14 08:44:39 +0800 | [diff] [blame^] | 914 | |
| 915 | /******************************************************************************* |
| 916 | * |
| 917 | * FUNCTION: acpi_ns_repair_object |
| 918 | * |
| 919 | * PARAMETERS: Pathname - Full pathname to the node (for error msgs) |
| 920 | * package_index - Used to determine if target is in a package |
| 921 | * return_object_ptr - Pointer to the object returned from the |
| 922 | * evaluation of a method or object |
| 923 | * |
| 924 | * RETURN: Status. AE_OK if repair was successful. |
| 925 | * |
| 926 | * DESCRIPTION: Attempt to repair/convert a return object of a type that was |
| 927 | * not expected. |
| 928 | * |
| 929 | ******************************************************************************/ |
| 930 | |
| 931 | static acpi_status |
| 932 | acpi_ns_repair_object(u32 expected_btypes, |
| 933 | u32 package_index, |
| 934 | union acpi_operand_object **return_object_ptr) |
| 935 | { |
| 936 | union acpi_operand_object *return_object = *return_object_ptr; |
| 937 | union acpi_operand_object *new_object; |
| 938 | acpi_size length; |
| 939 | |
| 940 | switch (ACPI_GET_OBJECT_TYPE(return_object)) { |
| 941 | case ACPI_TYPE_BUFFER: |
| 942 | |
| 943 | if (!(expected_btypes & ACPI_RTYPE_STRING)) { |
| 944 | return (AE_AML_OPERAND_TYPE); |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * Have a Buffer, expected a String, convert. Use a to_string |
| 949 | * conversion, no transform performed on the buffer data. The best |
| 950 | * example of this is the _BIF method, where the string data from |
| 951 | * the battery is often (incorrectly) returned as buffer object(s). |
| 952 | */ |
| 953 | length = 0; |
| 954 | while ((length < return_object->buffer.length) && |
| 955 | (return_object->buffer.pointer[length])) { |
| 956 | length++; |
| 957 | } |
| 958 | |
| 959 | /* Allocate a new string object */ |
| 960 | |
| 961 | new_object = acpi_ut_create_string_object(length); |
| 962 | if (!new_object) { |
| 963 | return (AE_NO_MEMORY); |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Copy the raw buffer data with no transform. String is already NULL |
| 968 | * terminated at Length+1. |
| 969 | */ |
| 970 | ACPI_MEMCPY(new_object->string.pointer, |
| 971 | return_object->buffer.pointer, length); |
| 972 | |
| 973 | /* Install the new return object */ |
| 974 | |
| 975 | acpi_ut_remove_reference(return_object); |
| 976 | *return_object_ptr = new_object; |
| 977 | |
| 978 | /* |
| 979 | * If the object is a package element, we need to: |
| 980 | * 1. Decrement the reference count of the orignal object, it was |
| 981 | * incremented when building the package |
| 982 | * 2. Increment the reference count of the new object, it will be |
| 983 | * decremented when releasing the package |
| 984 | */ |
| 985 | if (package_index != ACPI_NOT_PACKAGE) { |
| 986 | acpi_ut_remove_reference(return_object); |
| 987 | acpi_ut_add_reference(new_object); |
| 988 | } |
| 989 | return (AE_OK); |
| 990 | |
| 991 | default: |
| 992 | break; |
| 993 | } |
| 994 | |
| 995 | return (AE_AML_OPERAND_TYPE); |
| 996 | } |