blob: 1dc1a4737aa967fbdde9e5b6a7017e4ced8a8259 [file] [log] [blame]
Bob Mooree8707b32008-09-28 15:26:17 +08001/******************************************************************************
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>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "acnamesp.h"
48#include "acpredef.h"
Bob Mooree8707b32008-09-28 15:26:17 +080049
50#define _COMPONENT ACPI_NAMESPACE
51ACPI_MODULE_NAME("nspredef")
52
53/*******************************************************************************
54 *
55 * This module validates predefined ACPI objects that appear in the namespace,
56 * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
57 * validation is to detect problems with BIOS-exposed predefined ACPI objects
58 * before the results are returned to the ACPI-related drivers.
59 *
60 * There are several areas that are validated:
61 *
62 * 1) The number of input arguments as defined by the method/object in the
63 * ASL is validated against the ACPI specification.
64 * 2) The type of the return object (if any) is validated against the ACPI
65 * specification.
66 * 3) For returned package objects, the count of package elements is
67 * validated, as well as the type of each package element. Nested
68 * packages are supported.
69 *
70 * For any problems found, a warning message is issued.
71 *
72 ******************************************************************************/
73/* Local prototypes */
74static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080075acpi_ns_check_package(struct acpi_predefined_data *data,
76 union acpi_operand_object **return_object_ptr);
Bob Mooree8707b32008-09-28 15:26:17 +080077
78static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080079acpi_ns_check_package_elements(struct acpi_predefined_data *data,
Bob Mooree8707b32008-09-28 15:26:17 +080080 union acpi_operand_object **elements,
Bob Moore03ef1322009-03-19 10:14:45 +080081 u8 type1,
82 u32 count1,
83 u8 type2, u32 count2, u32 start_index);
Bob Mooree8707b32008-09-28 15:26:17 +080084
85static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080086acpi_ns_check_object_type(struct acpi_predefined_data *data,
Bob Moorea647b5c2008-11-14 08:44:39 +080087 union acpi_operand_object **return_object_ptr,
Bob Mooree8707b32008-09-28 15:26:17 +080088 u32 expected_btypes, u32 package_index);
89
90static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080091acpi_ns_check_reference(struct acpi_predefined_data *data,
Bob Mooree8707b32008-09-28 15:26:17 +080092 union acpi_operand_object *return_object);
93
Bob Moorea647b5c2008-11-14 08:44:39 +080094static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080095acpi_ns_repair_object(struct acpi_predefined_data *data,
96 u32 expected_btypes,
Bob Moorea647b5c2008-11-14 08:44:39 +080097 u32 package_index,
98 union acpi_operand_object **return_object_ptr);
99
Bob Moore0444e8f2009-06-24 13:38:02 +0800100static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes);
101
Bob Mooree8707b32008-09-28 15:26:17 +0800102/*
103 * Names for the types that can be returned by the predefined objects.
104 * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
105 */
106static const char *acpi_rtype_names[] = {
107 "/Integer",
108 "/String",
109 "/Buffer",
110 "/Package",
111 "/Reference",
112};
113
Bob Moore0444e8f2009-06-24 13:38:02 +0800114/* Object is not a package element */
115
116#define ACPI_NOT_PACKAGE_ELEMENT ACPI_UINT32_MAX
117
118/* Always emit warning message, not dependent on node flags */
119
120#define ACPI_WARN_ALWAYS 0
Bob Mooree8707b32008-09-28 15:26:17 +0800121
122/*******************************************************************************
123 *
124 * FUNCTION: acpi_ns_check_predefined_names
125 *
126 * PARAMETERS: Node - Namespace node for the method/object
Bob Moore0444e8f2009-06-24 13:38:02 +0800127 * user_param_count - Number of parameters actually passed
128 * return_status - Status from the object evaluation
Bob Moorea647b5c2008-11-14 08:44:39 +0800129 * return_object_ptr - Pointer to the object returned from the
130 * evaluation of a method or object
Bob Mooree8707b32008-09-28 15:26:17 +0800131 *
132 * RETURN: Status
133 *
134 * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
135 *
136 ******************************************************************************/
137
138acpi_status
139acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
Bob Mooreeeb44372008-11-13 11:19:24 +0800140 u32 user_param_count,
141 acpi_status return_status,
Bob Moorea647b5c2008-11-14 08:44:39 +0800142 union acpi_operand_object **return_object_ptr)
Bob Mooree8707b32008-09-28 15:26:17 +0800143{
Bob Moorea647b5c2008-11-14 08:44:39 +0800144 union acpi_operand_object *return_object = *return_object_ptr;
Bob Mooree8707b32008-09-28 15:26:17 +0800145 acpi_status status = AE_OK;
146 const union acpi_predefined_info *predefined;
147 char *pathname;
Bob Moore0444e8f2009-06-24 13:38:02 +0800148 struct acpi_predefined_data *data;
Bob Mooree8707b32008-09-28 15:26:17 +0800149
150 /* Match the name for this method/object against the predefined list */
151
152 predefined = acpi_ns_check_for_predefined_name(node);
Bob Mooree8707b32008-09-28 15:26:17 +0800153
Bob Moore0444e8f2009-06-24 13:38:02 +0800154 /* Get the full pathname to the object, for use in warning messages */
Bob Mooree8707b32008-09-28 15:26:17 +0800155
156 pathname = acpi_ns_get_external_pathname(node);
157 if (!pathname) {
Bob Moore65259092009-04-22 12:57:40 +0800158 return AE_OK; /* Could not get pathname, ignore */
Bob Mooree8707b32008-09-28 15:26:17 +0800159 }
160
161 /*
Bob Mooreeeb44372008-11-13 11:19:24 +0800162 * Check that the parameter count for this method matches the ASL
163 * definition. For predefined names, ensure that both the caller and
164 * the method itself are in accordance with the ACPI specification.
Bob Mooree8707b32008-09-28 15:26:17 +0800165 */
Bob Mooreeeb44372008-11-13 11:19:24 +0800166 acpi_ns_check_parameter_count(pathname, node, user_param_count,
167 predefined);
168
169 /* If not a predefined name, we cannot validate the return object */
170
171 if (!predefined) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800172 goto cleanup;
Bob Mooreeeb44372008-11-13 11:19:24 +0800173 }
174
175 /*
Bob Moore0444e8f2009-06-24 13:38:02 +0800176 * If the method failed or did not actually return an object, we cannot
177 * validate the return object
Bob Mooreeeb44372008-11-13 11:19:24 +0800178 */
Bob Moore0444e8f2009-06-24 13:38:02 +0800179 if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
180 goto cleanup;
Bob Mooreeeb44372008-11-13 11:19:24 +0800181 }
182
Bob Mooree8707b32008-09-28 15:26:17 +0800183 /*
184 * If there is no return value, check if we require a return value for
185 * this predefined name. Either one return value is expected, or none,
186 * for both methods and other objects.
187 *
188 * Exit now if there is no return object. Warning if one was expected.
189 */
190 if (!return_object) {
191 if ((predefined->info.expected_btypes) &&
192 (!(predefined->info.expected_btypes & ACPI_RTYPE_NONE))) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800193 ACPI_WARN_PREDEFINED((AE_INFO, pathname,
194 ACPI_WARN_ALWAYS,
195 "Missing expected return value"));
Bob Mooree8707b32008-09-28 15:26:17 +0800196
197 status = AE_AML_NO_RETURN_VALUE;
198 }
Bob Moore0444e8f2009-06-24 13:38:02 +0800199 goto cleanup;
Bob Mooree8707b32008-09-28 15:26:17 +0800200 }
201
202 /*
203 * We have a return value, but if one wasn't expected, just exit, this is
Bob Moore0444e8f2009-06-24 13:38:02 +0800204 * not a problem. For example, if the "Implicit Return" feature is
205 * enabled, methods will always return a value.
Bob Mooree8707b32008-09-28 15:26:17 +0800206 */
207 if (!predefined->info.expected_btypes) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800208 goto cleanup;
Bob Mooree8707b32008-09-28 15:26:17 +0800209 }
210
Bob Moore0444e8f2009-06-24 13:38:02 +0800211 /* Create the parameter data block for object validation */
212
213 data = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_predefined_data));
214 if (!data) {
215 goto cleanup;
216 }
217 data->predefined = predefined;
218 data->node_flags = node->flags;
219 data->pathname = pathname;
220
Bob Mooree8707b32008-09-28 15:26:17 +0800221 /*
222 * Check that the type of the return object is what is expected for
223 * this predefined name
224 */
Bob Moore0444e8f2009-06-24 13:38:02 +0800225 status = acpi_ns_check_object_type(data, return_object_ptr,
Bob Mooree8707b32008-09-28 15:26:17 +0800226 predefined->info.expected_btypes,
Bob Moore0444e8f2009-06-24 13:38:02 +0800227 ACPI_NOT_PACKAGE_ELEMENT);
Bob Mooree8707b32008-09-28 15:26:17 +0800228 if (ACPI_FAILURE(status)) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800229 goto check_validation_status;
Bob Mooree8707b32008-09-28 15:26:17 +0800230 }
231
232 /* For returned Package objects, check the type of all sub-objects */
233
Bob Moore3371c192009-02-18 14:44:03 +0800234 if (return_object->common.type == ACPI_TYPE_PACKAGE) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800235 status = acpi_ns_check_package(data, return_object_ptr);
Bob Mooree8707b32008-09-28 15:26:17 +0800236 }
237
Bob Moore0444e8f2009-06-24 13:38:02 +0800238check_validation_status:
239 /*
240 * If the object validation failed or if we successfully repaired one
241 * or more objects, mark the parent node to suppress further warning
242 * messages during the next evaluation of the same method/object.
243 */
244 if (ACPI_FAILURE(status) || (data->flags & ACPI_OBJECT_REPAIRED)) {
245 node->flags |= ANOBJ_EVALUATED;
246 }
247 ACPI_FREE(data);
248
249cleanup:
Bob Moore65259092009-04-22 12:57:40 +0800250 ACPI_FREE(pathname);
Bob Mooree8707b32008-09-28 15:26:17 +0800251 return (status);
252}
253
254/*******************************************************************************
255 *
256 * FUNCTION: acpi_ns_check_parameter_count
257 *
258 * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
259 * Node - Namespace node for the method/object
Bob Mooreeeb44372008-11-13 11:19:24 +0800260 * user_param_count - Number of args passed in by the caller
Bob Mooree8707b32008-09-28 15:26:17 +0800261 * Predefined - Pointer to entry in predefined name table
262 *
263 * RETURN: None
264 *
265 * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
266 * predefined name is what is expected (i.e., what is defined in
267 * the ACPI specification for this predefined name.)
268 *
269 ******************************************************************************/
270
271void
272acpi_ns_check_parameter_count(char *pathname,
273 struct acpi_namespace_node *node,
Bob Mooreeeb44372008-11-13 11:19:24 +0800274 u32 user_param_count,
Bob Mooree8707b32008-09-28 15:26:17 +0800275 const union acpi_predefined_info *predefined)
276{
277 u32 param_count;
278 u32 required_params_current;
279 u32 required_params_old;
280
Bob Mooreeeb44372008-11-13 11:19:24 +0800281 /* Methods have 0-7 parameters. All other types have zero. */
282
Bob Mooree8707b32008-09-28 15:26:17 +0800283 param_count = 0;
284 if (node->type == ACPI_TYPE_METHOD) {
285 param_count = node->object->method.param_count;
286 }
287
Bob Mooreeeb44372008-11-13 11:19:24 +0800288 if (!predefined) {
289 /*
Bob Moore0444e8f2009-06-24 13:38:02 +0800290 * Check the parameter count for non-predefined methods/objects.
291 *
Bob Mooreeeb44372008-11-13 11:19:24 +0800292 * Warning if too few or too many arguments have been passed by the
293 * caller. An incorrect number of arguments may not cause the method
294 * to fail. However, the method will fail if there are too few
295 * arguments and the method attempts to use one of the missing ones.
296 */
297 if (user_param_count < param_count) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800298 ACPI_WARN_PREDEFINED((AE_INFO, pathname,
299 ACPI_WARN_ALWAYS,
300 "Insufficient arguments - needs %u, found %u",
301 param_count, user_param_count));
Bob Mooreeeb44372008-11-13 11:19:24 +0800302 } else if (user_param_count > param_count) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800303 ACPI_WARN_PREDEFINED((AE_INFO, pathname,
304 ACPI_WARN_ALWAYS,
305 "Excess arguments - needs %u, found %u",
306 param_count, user_param_count));
Bob Mooreeeb44372008-11-13 11:19:24 +0800307 }
308 return;
309 }
310
Bob Moore0444e8f2009-06-24 13:38:02 +0800311 /*
312 * Validate the user-supplied parameter count.
313 * Allow two different legal argument counts (_SCP, etc.)
314 */
Bob Mooree8707b32008-09-28 15:26:17 +0800315 required_params_current = predefined->info.param_count & 0x0F;
316 required_params_old = predefined->info.param_count >> 4;
317
Bob Mooreeeb44372008-11-13 11:19:24 +0800318 if (user_param_count != ACPI_UINT32_MAX) {
Bob Mooreeeb44372008-11-13 11:19:24 +0800319 if ((user_param_count != required_params_current) &&
320 (user_param_count != required_params_old)) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800321 ACPI_WARN_PREDEFINED((AE_INFO, pathname,
322 ACPI_WARN_ALWAYS,
323 "Parameter count mismatch - "
324 "caller passed %u, ACPI requires %u",
325 user_param_count,
326 required_params_current));
Bob Mooreeeb44372008-11-13 11:19:24 +0800327 }
328 }
329
330 /*
Bob Mooreeeb44372008-11-13 11:19:24 +0800331 * Check that the ASL-defined parameter count is what is expected for
Bob Moore0444e8f2009-06-24 13:38:02 +0800332 * this predefined name (parameter count as defined by the ACPI
333 * specification)
Bob Mooreeeb44372008-11-13 11:19:24 +0800334 */
Bob Mooree8707b32008-09-28 15:26:17 +0800335 if ((param_count != required_params_current) &&
336 (param_count != required_params_old)) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800337 ACPI_WARN_PREDEFINED((AE_INFO, pathname, node->flags,
338 "Parameter count mismatch - ASL declared %u, ACPI requires %u",
339 param_count, required_params_current));
Bob Mooree8707b32008-09-28 15:26:17 +0800340 }
341}
342
343/*******************************************************************************
344 *
345 * FUNCTION: acpi_ns_check_for_predefined_name
346 *
347 * PARAMETERS: Node - Namespace node for the method/object
348 *
349 * RETURN: Pointer to entry in predefined table. NULL indicates not found.
350 *
351 * DESCRIPTION: Check an object name against the predefined object list.
352 *
353 ******************************************************************************/
354
355const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct
356 acpi_namespace_node
357 *node)
358{
359 const union acpi_predefined_info *this_name;
360
361 /* Quick check for a predefined name, first character must be underscore */
362
363 if (node->name.ascii[0] != '_') {
364 return (NULL);
365 }
366
367 /* Search info table for a predefined method/object name */
368
369 this_name = predefined_names;
370 while (this_name->info.name[0]) {
371 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) {
Bob Mooree8707b32008-09-28 15:26:17 +0800372 return (this_name);
373 }
374
375 /*
376 * Skip next entry in the table if this name returns a Package
377 * (next entry contains the package info)
378 */
379 if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) {
380 this_name++;
381 }
382
383 this_name++;
384 }
385
Bob Moore0444e8f2009-06-24 13:38:02 +0800386 return (NULL); /* Not found */
Bob Mooree8707b32008-09-28 15:26:17 +0800387}
388
389/*******************************************************************************
390 *
391 * FUNCTION: acpi_ns_check_package
392 *
Bob Moore0444e8f2009-06-24 13:38:02 +0800393 * PARAMETERS: Data - Pointer to validation data structure
Bob Moorea647b5c2008-11-14 08:44:39 +0800394 * return_object_ptr - Pointer to the object returned from the
395 * evaluation of a method or object
Bob Mooree8707b32008-09-28 15:26:17 +0800396 *
397 * RETURN: Status
398 *
399 * DESCRIPTION: Check a returned package object for the correct count and
400 * correct type of all sub-objects.
401 *
402 ******************************************************************************/
403
404static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +0800405acpi_ns_check_package(struct acpi_predefined_data *data,
406 union acpi_operand_object **return_object_ptr)
Bob Mooree8707b32008-09-28 15:26:17 +0800407{
Bob Moorea647b5c2008-11-14 08:44:39 +0800408 union acpi_operand_object *return_object = *return_object_ptr;
Bob Mooree8707b32008-09-28 15:26:17 +0800409 const union acpi_predefined_info *package;
410 union acpi_operand_object *sub_package;
411 union acpi_operand_object **elements;
412 union acpi_operand_object **sub_elements;
413 acpi_status status;
414 u32 expected_count;
415 u32 count;
416 u32 i;
417 u32 j;
418
419 ACPI_FUNCTION_NAME(ns_check_package);
420
421 /* The package info for this name is in the next table entry */
422
Bob Moore0444e8f2009-06-24 13:38:02 +0800423 package = data->predefined + 1;
Bob Mooree8707b32008-09-28 15:26:17 +0800424
425 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
426 "%s Validating return Package of Type %X, Count %X\n",
Bob Moore0444e8f2009-06-24 13:38:02 +0800427 data->pathname, package->ret_info.type,
Bob Mooree8707b32008-09-28 15:26:17 +0800428 return_object->package.count));
429
430 /* Extract package count and elements array */
431
432 elements = return_object->package.elements;
433 count = return_object->package.count;
434
435 /* The package must have at least one element, else invalid */
436
437 if (!count) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800438 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
439 "Return Package has no elements (empty)"));
Bob Mooree8707b32008-09-28 15:26:17 +0800440
441 return (AE_AML_OPERAND_VALUE);
442 }
443
444 /*
445 * Decode the type of the expected package contents
446 *
447 * PTYPE1 packages contain no subpackages
448 * PTYPE2 packages contain sub-packages
449 */
450 switch (package->ret_info.type) {
451 case ACPI_PTYPE1_FIXED:
452
453 /*
454 * The package count is fixed and there are no sub-packages
455 *
456 * If package is too small, exit.
457 * If package is larger than expected, issue warning but continue
458 */
459 expected_count =
460 package->ret_info.count1 + package->ret_info.count2;
461 if (count < expected_count) {
462 goto package_too_small;
463 } else if (count > expected_count) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800464 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
465 data->node_flags,
466 "Return Package is larger than needed - "
467 "found %u, expected %u", count,
468 expected_count));
Bob Mooree8707b32008-09-28 15:26:17 +0800469 }
470
471 /* Validate all elements of the returned package */
472
Bob Moore0444e8f2009-06-24 13:38:02 +0800473 status = acpi_ns_check_package_elements(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800474 package->ret_info.
475 object_type1,
476 package->ret_info.
477 count1,
478 package->ret_info.
479 object_type2,
480 package->ret_info.
Bob Moore03ef1322009-03-19 10:14:45 +0800481 count2, 0);
Bob Mooree8707b32008-09-28 15:26:17 +0800482 if (ACPI_FAILURE(status)) {
483 return (status);
484 }
485 break;
486
487 case ACPI_PTYPE1_VAR:
488
489 /*
490 * The package count is variable, there are no sub-packages, and all
491 * elements must be of the same type
492 */
493 for (i = 0; i < count; i++) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800494 status = acpi_ns_check_object_type(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800495 package->ret_info.
496 object_type1, i);
497 if (ACPI_FAILURE(status)) {
498 return (status);
499 }
500 elements++;
501 }
502 break;
503
504 case ACPI_PTYPE1_OPTION:
505
506 /*
507 * The package count is variable, there are no sub-packages. There are
508 * a fixed number of required elements, and a variable number of
509 * optional elements.
510 *
511 * Check if package is at least as large as the minimum required
512 */
513 expected_count = package->ret_info3.count;
514 if (count < expected_count) {
515 goto package_too_small;
516 }
517
518 /* Variable number of sub-objects */
519
520 for (i = 0; i < count; i++) {
521 if (i < package->ret_info3.count) {
522
523 /* These are the required package elements (0, 1, or 2) */
524
525 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800526 acpi_ns_check_object_type(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800527 package->
528 ret_info3.
529 object_type[i],
530 i);
531 if (ACPI_FAILURE(status)) {
532 return (status);
533 }
534 } else {
535 /* These are the optional package elements */
536
537 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800538 acpi_ns_check_object_type(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800539 package->
540 ret_info3.
541 tail_object_type,
542 i);
543 if (ACPI_FAILURE(status)) {
544 return (status);
545 }
546 }
547 elements++;
548 }
549 break;
550
551 case ACPI_PTYPE2_PKG_COUNT:
552
553 /* First element is the (Integer) count of sub-packages to follow */
554
Bob Moore0444e8f2009-06-24 13:38:02 +0800555 status = acpi_ns_check_object_type(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800556 ACPI_RTYPE_INTEGER, 0);
557 if (ACPI_FAILURE(status)) {
558 return (status);
559 }
560
561 /*
562 * Count cannot be larger than the parent package length, but allow it
563 * to be smaller. The >= accounts for the Integer above.
564 */
565 expected_count = (u32) (*elements)->integer.value;
566 if (expected_count >= count) {
567 goto package_too_small;
568 }
569
570 count = expected_count;
571 elements++;
572
573 /* Now we can walk the sub-packages */
574
575 /*lint -fallthrough */
576
577 case ACPI_PTYPE2:
578 case ACPI_PTYPE2_FIXED:
579 case ACPI_PTYPE2_MIN:
580 case ACPI_PTYPE2_COUNT:
581
582 /*
583 * These types all return a single package that consists of a variable
584 * number of sub-packages
585 */
586 for (i = 0; i < count; i++) {
587 sub_package = *elements;
588 sub_elements = sub_package->package.elements;
589
590 /* Each sub-object must be of type Package */
591
Bob Moore0444e8f2009-06-24 13:38:02 +0800592 status = acpi_ns_check_object_type(data, &sub_package,
593 ACPI_RTYPE_PACKAGE,
594 i);
Bob Mooree8707b32008-09-28 15:26:17 +0800595 if (ACPI_FAILURE(status)) {
596 return (status);
597 }
598
599 /* Examine the different types of sub-packages */
600
601 switch (package->ret_info.type) {
602 case ACPI_PTYPE2:
603 case ACPI_PTYPE2_PKG_COUNT:
604
605 /* Each subpackage has a fixed number of elements */
606
607 expected_count =
608 package->ret_info.count1 +
609 package->ret_info.count2;
610 if (sub_package->package.count !=
611 expected_count) {
612 count = sub_package->package.count;
613 goto package_too_small;
614 }
615
616 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800617 acpi_ns_check_package_elements(data,
Bob Mooree8707b32008-09-28 15:26:17 +0800618 sub_elements,
619 package->
620 ret_info.
621 object_type1,
622 package->
623 ret_info.
624 count1,
625 package->
626 ret_info.
627 object_type2,
628 package->
629 ret_info.
Bob Moore03ef1322009-03-19 10:14:45 +0800630 count2, 0);
Bob Mooree8707b32008-09-28 15:26:17 +0800631 if (ACPI_FAILURE(status)) {
632 return (status);
633 }
634 break;
635
636 case ACPI_PTYPE2_FIXED:
637
638 /* Each sub-package has a fixed length */
639
640 expected_count = package->ret_info2.count;
641 if (sub_package->package.count < expected_count) {
642 count = sub_package->package.count;
643 goto package_too_small;
644 }
645
646 /* Check the type of each sub-package element */
647
648 for (j = 0; j < expected_count; j++) {
649 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800650 acpi_ns_check_object_type(data,
Bob Moorea647b5c2008-11-14 08:44:39 +0800651 &sub_elements[j],
652 package->ret_info2.object_type[j], j);
Bob Mooree8707b32008-09-28 15:26:17 +0800653 if (ACPI_FAILURE(status)) {
654 return (status);
655 }
656 }
657 break;
658
659 case ACPI_PTYPE2_MIN:
660
661 /* Each sub-package has a variable but minimum length */
662
663 expected_count = package->ret_info.count1;
664 if (sub_package->package.count < expected_count) {
665 count = sub_package->package.count;
666 goto package_too_small;
667 }
668
669 /* Check the type of each sub-package element */
670
671 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800672 acpi_ns_check_package_elements(data,
Bob Mooree8707b32008-09-28 15:26:17 +0800673 sub_elements,
674 package->
675 ret_info.
676 object_type1,
677 sub_package->
678 package.
Bob Moore03ef1322009-03-19 10:14:45 +0800679 count, 0, 0,
680 0);
Bob Mooree8707b32008-09-28 15:26:17 +0800681 if (ACPI_FAILURE(status)) {
682 return (status);
683 }
684 break;
685
686 case ACPI_PTYPE2_COUNT:
687
688 /* First element is the (Integer) count of elements to follow */
689
690 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800691 acpi_ns_check_object_type(data,
Bob Moorea647b5c2008-11-14 08:44:39 +0800692 sub_elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800693 ACPI_RTYPE_INTEGER,
694 0);
695 if (ACPI_FAILURE(status)) {
696 return (status);
697 }
698
699 /* Make sure package is large enough for the Count */
700
701 expected_count =
702 (u32) (*sub_elements)->integer.value;
703 if (sub_package->package.count < expected_count) {
704 count = sub_package->package.count;
705 goto package_too_small;
706 }
707
708 /* Check the type of each sub-package element */
709
710 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800711 acpi_ns_check_package_elements(data,
Bob Mooree8707b32008-09-28 15:26:17 +0800712 (sub_elements
713 + 1),
714 package->
715 ret_info.
716 object_type1,
717 (expected_count
Bob Moore03ef1322009-03-19 10:14:45 +0800718 - 1), 0, 0,
719 1);
Bob Mooree8707b32008-09-28 15:26:17 +0800720 if (ACPI_FAILURE(status)) {
721 return (status);
722 }
723 break;
724
725 default:
726 break;
727 }
728
729 elements++;
730 }
731 break;
732
733 default:
734
735 /* Should not get here if predefined info table is correct */
736
Bob Moore0444e8f2009-06-24 13:38:02 +0800737 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
738 "Invalid internal return type in table entry: %X",
739 package->ret_info.type));
Bob Mooree8707b32008-09-28 15:26:17 +0800740
741 return (AE_AML_INTERNAL);
742 }
743
744 return (AE_OK);
745
746 package_too_small:
747
748 /* Error exit for the case with an incorrect package count */
749
Bob Moore0444e8f2009-06-24 13:38:02 +0800750 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
751 "Return Package is too small - found %u, expected %u",
752 count, expected_count));
Bob Mooree8707b32008-09-28 15:26:17 +0800753
754 return (AE_AML_OPERAND_VALUE);
755}
756
757/*******************************************************************************
758 *
759 * FUNCTION: acpi_ns_check_package_elements
760 *
Bob Moore0444e8f2009-06-24 13:38:02 +0800761 * PARAMETERS: Data - Pointer to validation data structure
Bob Mooree8707b32008-09-28 15:26:17 +0800762 * Elements - Pointer to the package elements array
763 * Type1 - Object type for first group
764 * Count1 - Count for first group
765 * Type2 - Object type for second group
766 * Count2 - Count for second group
Bob Moore03ef1322009-03-19 10:14:45 +0800767 * start_index - Start of the first group of elements
Bob Mooree8707b32008-09-28 15:26:17 +0800768 *
769 * RETURN: Status
770 *
771 * DESCRIPTION: Check that all elements of a package are of the correct object
772 * type. Supports up to two groups of different object types.
773 *
774 ******************************************************************************/
775
776static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +0800777acpi_ns_check_package_elements(struct acpi_predefined_data *data,
Bob Mooree8707b32008-09-28 15:26:17 +0800778 union acpi_operand_object **elements,
Bob Moore03ef1322009-03-19 10:14:45 +0800779 u8 type1,
780 u32 count1,
781 u8 type2, u32 count2, u32 start_index)
Bob Mooree8707b32008-09-28 15:26:17 +0800782{
783 union acpi_operand_object **this_element = elements;
784 acpi_status status;
785 u32 i;
786
787 /*
788 * Up to two groups of package elements are supported by the data
789 * structure. All elements in each group must be of the same type.
790 * The second group can have a count of zero.
791 */
792 for (i = 0; i < count1; i++) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800793 status = acpi_ns_check_object_type(data, this_element,
Bob Moore03ef1322009-03-19 10:14:45 +0800794 type1, i + start_index);
Bob Mooree8707b32008-09-28 15:26:17 +0800795 if (ACPI_FAILURE(status)) {
796 return (status);
797 }
798 this_element++;
799 }
800
801 for (i = 0; i < count2; i++) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800802 status = acpi_ns_check_object_type(data, this_element,
Bob Moore03ef1322009-03-19 10:14:45 +0800803 type2,
804 (i + count1 + start_index));
Bob Mooree8707b32008-09-28 15:26:17 +0800805 if (ACPI_FAILURE(status)) {
806 return (status);
807 }
808 this_element++;
809 }
810
811 return (AE_OK);
812}
813
814/*******************************************************************************
815 *
816 * FUNCTION: acpi_ns_check_object_type
817 *
Bob Moore0444e8f2009-06-24 13:38:02 +0800818 * PARAMETERS: Data - Pointer to validation data structure
Bob Moorea647b5c2008-11-14 08:44:39 +0800819 * return_object_ptr - Pointer to the object returned from the
820 * evaluation of a method or object
Bob Mooree8707b32008-09-28 15:26:17 +0800821 * expected_btypes - Bitmap of expected return type(s)
822 * package_index - Index of object within parent package (if
Bob Moore0444e8f2009-06-24 13:38:02 +0800823 * applicable - ACPI_NOT_PACKAGE_ELEMENT
824 * otherwise)
Bob Mooree8707b32008-09-28 15:26:17 +0800825 *
826 * RETURN: Status
827 *
828 * DESCRIPTION: Check the type of the return object against the expected object
829 * type(s). Use of Btype allows multiple expected object types.
830 *
831 ******************************************************************************/
832
833static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +0800834acpi_ns_check_object_type(struct acpi_predefined_data *data,
Bob Moorea647b5c2008-11-14 08:44:39 +0800835 union acpi_operand_object **return_object_ptr,
Bob Mooree8707b32008-09-28 15:26:17 +0800836 u32 expected_btypes, u32 package_index)
837{
Bob Moorea647b5c2008-11-14 08:44:39 +0800838 union acpi_operand_object *return_object = *return_object_ptr;
Bob Mooree8707b32008-09-28 15:26:17 +0800839 acpi_status status = AE_OK;
840 u32 return_btype;
841 char type_buffer[48]; /* Room for 5 types */
Bob Mooree8707b32008-09-28 15:26:17 +0800842
843 /*
844 * If we get a NULL return_object here, it is a NULL package element,
845 * and this is always an error.
846 */
847 if (!return_object) {
848 goto type_error_exit;
849 }
850
851 /* A Namespace node should not get here, but make sure */
852
853 if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800854 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
855 "Invalid return type - Found a Namespace node [%4.4s] type %s",
856 return_object->node.name.ascii,
857 acpi_ut_get_type_name(return_object->node.
858 type)));
Bob Mooree8707b32008-09-28 15:26:17 +0800859 return (AE_AML_OPERAND_TYPE);
860 }
861
862 /*
863 * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
864 * The bitmapped type allows multiple possible return types.
865 *
866 * Note, the cases below must handle all of the possible types returned
867 * from all of the predefined names (including elements of returned
868 * packages)
869 */
Bob Moore3371c192009-02-18 14:44:03 +0800870 switch (return_object->common.type) {
Bob Mooree8707b32008-09-28 15:26:17 +0800871 case ACPI_TYPE_INTEGER:
872 return_btype = ACPI_RTYPE_INTEGER;
873 break;
874
875 case ACPI_TYPE_BUFFER:
876 return_btype = ACPI_RTYPE_BUFFER;
877 break;
878
879 case ACPI_TYPE_STRING:
880 return_btype = ACPI_RTYPE_STRING;
881 break;
882
883 case ACPI_TYPE_PACKAGE:
884 return_btype = ACPI_RTYPE_PACKAGE;
885 break;
886
887 case ACPI_TYPE_LOCAL_REFERENCE:
888 return_btype = ACPI_RTYPE_REFERENCE;
889 break;
890
891 default:
892 /* Not one of the supported objects, must be incorrect */
893
894 goto type_error_exit;
895 }
896
897 /* Is the object one of the expected types? */
898
899 if (!(return_btype & expected_btypes)) {
Bob Moorea647b5c2008-11-14 08:44:39 +0800900
901 /* Type mismatch -- attempt repair of the returned object */
902
Bob Moore0444e8f2009-06-24 13:38:02 +0800903 status = acpi_ns_repair_object(data, expected_btypes,
904 package_index,
Bob Moorea647b5c2008-11-14 08:44:39 +0800905 return_object_ptr);
906 if (ACPI_SUCCESS(status)) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800907 return (AE_OK); /* Repair was successful */
Bob Moorea647b5c2008-11-14 08:44:39 +0800908 }
Bob Mooree8707b32008-09-28 15:26:17 +0800909 goto type_error_exit;
910 }
911
912 /* For reference objects, check that the reference type is correct */
913
Bob Moore3371c192009-02-18 14:44:03 +0800914 if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800915 status = acpi_ns_check_reference(data, return_object);
Bob Mooree8707b32008-09-28 15:26:17 +0800916 }
917
918 return (status);
919
920 type_error_exit:
921
922 /* Create a string with all expected types for this predefined object */
923
Bob Moore0444e8f2009-06-24 13:38:02 +0800924 acpi_ns_get_expected_types(type_buffer, expected_btypes);
Bob Mooree8707b32008-09-28 15:26:17 +0800925
Bob Moore0444e8f2009-06-24 13:38:02 +0800926 if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
927 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
928 "Return type mismatch - found %s, expected %s",
929 acpi_ut_get_object_type_name
930 (return_object), type_buffer));
Bob Mooree8707b32008-09-28 15:26:17 +0800931 } else {
Bob Moore0444e8f2009-06-24 13:38:02 +0800932 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
933 "Return Package type mismatch at index %u - "
934 "found %s, expected %s", package_index,
935 acpi_ut_get_object_type_name
936 (return_object), type_buffer));
Bob Mooree8707b32008-09-28 15:26:17 +0800937 }
938
939 return (AE_AML_OPERAND_TYPE);
940}
941
942/*******************************************************************************
943 *
944 * FUNCTION: acpi_ns_check_reference
945 *
Bob Moore0444e8f2009-06-24 13:38:02 +0800946 * PARAMETERS: Data - Pointer to validation data structure
Bob Mooree8707b32008-09-28 15:26:17 +0800947 * return_object - Object returned from the evaluation of a
948 * method or object
949 *
950 * RETURN: Status
951 *
952 * DESCRIPTION: Check a returned reference object for the correct reference
953 * type. The only reference type that can be returned from a
954 * predefined method is a named reference. All others are invalid.
955 *
956 ******************************************************************************/
957
958static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +0800959acpi_ns_check_reference(struct acpi_predefined_data *data,
Bob Mooree8707b32008-09-28 15:26:17 +0800960 union acpi_operand_object *return_object)
961{
962
963 /*
964 * Check the reference object for the correct reference type (opcode).
965 * The only type of reference that can be converted to an union acpi_object is
966 * a reference to a named object (reference class: NAME)
967 */
968 if (return_object->reference.class == ACPI_REFCLASS_NAME) {
969 return (AE_OK);
970 }
971
Bob Moore0444e8f2009-06-24 13:38:02 +0800972 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
973 "Return type mismatch - unexpected reference object type [%s] %2.2X",
974 acpi_ut_get_reference_name(return_object),
975 return_object->reference.class));
Bob Mooree8707b32008-09-28 15:26:17 +0800976
977 return (AE_AML_OPERAND_TYPE);
978}
Bob Moorea647b5c2008-11-14 08:44:39 +0800979
980/*******************************************************************************
981 *
982 * FUNCTION: acpi_ns_repair_object
983 *
Bob Moore0444e8f2009-06-24 13:38:02 +0800984 * PARAMETERS: Data - Pointer to validation data structure
985 * expected_btypes - Object types expected
986 * package_index - Index of object within parent package (if
987 * applicable - ACPI_NOT_PACKAGE_ELEMENT
988 * otherwise)
Bob Moorea647b5c2008-11-14 08:44:39 +0800989 * return_object_ptr - Pointer to the object returned from the
990 * evaluation of a method or object
991 *
992 * RETURN: Status. AE_OK if repair was successful.
993 *
994 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
995 * not expected.
996 *
997 ******************************************************************************/
998
999static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +08001000acpi_ns_repair_object(struct acpi_predefined_data *data,
1001 u32 expected_btypes,
Bob Moorea647b5c2008-11-14 08:44:39 +08001002 u32 package_index,
1003 union acpi_operand_object **return_object_ptr)
1004{
1005 union acpi_operand_object *return_object = *return_object_ptr;
1006 union acpi_operand_object *new_object;
1007 acpi_size length;
1008
Bob Moore3371c192009-02-18 14:44:03 +08001009 switch (return_object->common.type) {
Bob Moorea647b5c2008-11-14 08:44:39 +08001010 case ACPI_TYPE_BUFFER:
1011
Bob Moore0444e8f2009-06-24 13:38:02 +08001012 /* Does the method/object legally return a string? */
1013
Bob Moorea647b5c2008-11-14 08:44:39 +08001014 if (!(expected_btypes & ACPI_RTYPE_STRING)) {
1015 return (AE_AML_OPERAND_TYPE);
1016 }
1017
1018 /*
1019 * Have a Buffer, expected a String, convert. Use a to_string
1020 * conversion, no transform performed on the buffer data. The best
1021 * example of this is the _BIF method, where the string data from
1022 * the battery is often (incorrectly) returned as buffer object(s).
1023 */
1024 length = 0;
1025 while ((length < return_object->buffer.length) &&
1026 (return_object->buffer.pointer[length])) {
1027 length++;
1028 }
1029
1030 /* Allocate a new string object */
1031
1032 new_object = acpi_ut_create_string_object(length);
1033 if (!new_object) {
1034 return (AE_NO_MEMORY);
1035 }
1036
1037 /*
1038 * Copy the raw buffer data with no transform. String is already NULL
1039 * terminated at Length+1.
1040 */
1041 ACPI_MEMCPY(new_object->string.pointer,
1042 return_object->buffer.pointer, length);
1043
Bob Mooredbdc8f02009-06-24 11:22:22 +08001044 /*
1045 * If the original object is a package element, we need to:
1046 * 1. Set the reference count of the new object to match the
1047 * reference count of the old object.
1048 * 2. Decrement the reference count of the original object.
1049 */
Bob Moore0444e8f2009-06-24 13:38:02 +08001050 if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
Bob Mooredbdc8f02009-06-24 11:22:22 +08001051 new_object->common.reference_count =
1052 return_object->common.reference_count;
1053
1054 if (return_object->common.reference_count > 1) {
1055 return_object->common.reference_count--;
1056 }
Bob Moore0444e8f2009-06-24 13:38:02 +08001057
1058 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
1059 data->node_flags,
1060 "Converted Buffer to expected String at index %u",
1061 package_index));
1062 } else {
1063 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
1064 data->node_flags,
1065 "Converted Buffer to expected String"));
Bob Mooredbdc8f02009-06-24 11:22:22 +08001066 }
1067
1068 /* Delete old object, install the new return object */
Bob Moorea647b5c2008-11-14 08:44:39 +08001069
1070 acpi_ut_remove_reference(return_object);
1071 *return_object_ptr = new_object;
Bob Moore0444e8f2009-06-24 13:38:02 +08001072 data->flags |= ACPI_OBJECT_REPAIRED;
Bob Moorea647b5c2008-11-14 08:44:39 +08001073 return (AE_OK);
1074
1075 default:
1076 break;
1077 }
1078
1079 return (AE_AML_OPERAND_TYPE);
1080}
Bob Moore0444e8f2009-06-24 13:38:02 +08001081
1082/*******************************************************************************
1083 *
1084 * FUNCTION: acpi_ns_get_expected_types
1085 *
1086 * PARAMETERS: Buffer - Pointer to where the string is returned
1087 * expected_btypes - Bitmap of expected return type(s)
1088 *
1089 * RETURN: Buffer is populated with type names.
1090 *
1091 * DESCRIPTION: Translate the expected types bitmap into a string of ascii
1092 * names of expected types, for use in warning messages.
1093 *
1094 ******************************************************************************/
1095
1096static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes)
1097{
1098 u32 this_rtype;
1099 u32 i;
1100 u32 j;
1101
1102 j = 1;
1103 buffer[0] = 0;
1104 this_rtype = ACPI_RTYPE_INTEGER;
1105
1106 for (i = 0; i < ACPI_NUM_RTYPES; i++) {
1107
1108 /* If one of the expected types, concatenate the name of this type */
1109
1110 if (expected_btypes & this_rtype) {
1111 ACPI_STRCAT(buffer, &acpi_rtype_names[i][j]);
1112 j = 0; /* Use name separator from now on */
1113 }
1114 this_rtype <<= 1; /* Next Rtype */
1115 }
1116}