blob: c6297f57fea8db79434df3ddafdf7d985bcdfa57 [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
Bob Moore999e08f2009-08-13 14:30:16 +080045#define ACPI_CREATE_PREDEFINED_TABLE
46
Bob Mooree8707b32008-09-28 15:26:17 +080047#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050048#include "accommon.h"
49#include "acnamesp.h"
50#include "acpredef.h"
Bob Mooree8707b32008-09-28 15:26:17 +080051
52#define _COMPONENT ACPI_NAMESPACE
53ACPI_MODULE_NAME("nspredef")
54
55/*******************************************************************************
56 *
57 * This module validates predefined ACPI objects that appear in the namespace,
58 * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
59 * validation is to detect problems with BIOS-exposed predefined ACPI objects
60 * before the results are returned to the ACPI-related drivers.
61 *
62 * There are several areas that are validated:
63 *
64 * 1) The number of input arguments as defined by the method/object in the
65 * ASL is validated against the ACPI specification.
66 * 2) The type of the return object (if any) is validated against the ACPI
67 * specification.
68 * 3) For returned package objects, the count of package elements is
69 * validated, as well as the type of each package element. Nested
70 * packages are supported.
71 *
72 * For any problems found, a warning message is issued.
73 *
74 ******************************************************************************/
75/* Local prototypes */
76static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080077acpi_ns_check_package(struct acpi_predefined_data *data,
78 union acpi_operand_object **return_object_ptr);
Bob Mooree8707b32008-09-28 15:26:17 +080079
80static acpi_status
Bob Moore53e93872009-07-24 11:22:11 +080081acpi_ns_check_package_list(struct acpi_predefined_data *data,
82 const union acpi_predefined_info *package,
83 union acpi_operand_object **elements, u32 count);
84
85static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080086acpi_ns_check_package_elements(struct acpi_predefined_data *data,
Bob Mooree8707b32008-09-28 15:26:17 +080087 union acpi_operand_object **elements,
Bob Moore03ef1322009-03-19 10:14:45 +080088 u8 type1,
89 u32 count1,
90 u8 type2, u32 count2, u32 start_index);
Bob Mooree8707b32008-09-28 15:26:17 +080091
92static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080093acpi_ns_check_object_type(struct acpi_predefined_data *data,
Bob Moorea647b5c2008-11-14 08:44:39 +080094 union acpi_operand_object **return_object_ptr,
Bob Mooree8707b32008-09-28 15:26:17 +080095 u32 expected_btypes, u32 package_index);
96
97static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +080098acpi_ns_check_reference(struct acpi_predefined_data *data,
Bob Mooree8707b32008-09-28 15:26:17 +080099 union acpi_operand_object *return_object);
100
Bob Moore0444e8f2009-06-24 13:38:02 +0800101static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes);
102
Bob Mooree8707b32008-09-28 15:26:17 +0800103/*
104 * Names for the types that can be returned by the predefined objects.
105 * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
106 */
107static const char *acpi_rtype_names[] = {
108 "/Integer",
109 "/String",
110 "/Buffer",
111 "/Package",
112 "/Reference",
113};
114
Bob Mooree8707b32008-09-28 15:26:17 +0800115/*******************************************************************************
116 *
117 * FUNCTION: acpi_ns_check_predefined_names
118 *
119 * PARAMETERS: Node - Namespace node for the method/object
Bob Moore0444e8f2009-06-24 13:38:02 +0800120 * user_param_count - Number of parameters actually passed
121 * return_status - Status from the object evaluation
Bob Moorea647b5c2008-11-14 08:44:39 +0800122 * return_object_ptr - Pointer to the object returned from the
123 * evaluation of a method or object
Bob Mooree8707b32008-09-28 15:26:17 +0800124 *
125 * RETURN: Status
126 *
127 * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
128 *
129 ******************************************************************************/
130
131acpi_status
132acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
Bob Mooreeeb44372008-11-13 11:19:24 +0800133 u32 user_param_count,
134 acpi_status return_status,
Bob Moorea647b5c2008-11-14 08:44:39 +0800135 union acpi_operand_object **return_object_ptr)
Bob Mooree8707b32008-09-28 15:26:17 +0800136{
Bob Moorea647b5c2008-11-14 08:44:39 +0800137 union acpi_operand_object *return_object = *return_object_ptr;
Bob Mooree8707b32008-09-28 15:26:17 +0800138 acpi_status status = AE_OK;
139 const union acpi_predefined_info *predefined;
140 char *pathname;
Bob Moore0444e8f2009-06-24 13:38:02 +0800141 struct acpi_predefined_data *data;
Bob Mooree8707b32008-09-28 15:26:17 +0800142
143 /* Match the name for this method/object against the predefined list */
144
145 predefined = acpi_ns_check_for_predefined_name(node);
Bob Mooree8707b32008-09-28 15:26:17 +0800146
Bob Moore0444e8f2009-06-24 13:38:02 +0800147 /* Get the full pathname to the object, for use in warning messages */
Bob Mooree8707b32008-09-28 15:26:17 +0800148
149 pathname = acpi_ns_get_external_pathname(node);
150 if (!pathname) {
Bob Moore65259092009-04-22 12:57:40 +0800151 return AE_OK; /* Could not get pathname, ignore */
Bob Mooree8707b32008-09-28 15:26:17 +0800152 }
153
154 /*
Bob Mooreeeb44372008-11-13 11:19:24 +0800155 * Check that the parameter count for this method matches the ASL
156 * definition. For predefined names, ensure that both the caller and
157 * the method itself are in accordance with the ACPI specification.
Bob Mooree8707b32008-09-28 15:26:17 +0800158 */
Bob Mooreeeb44372008-11-13 11:19:24 +0800159 acpi_ns_check_parameter_count(pathname, node, user_param_count,
160 predefined);
161
162 /* If not a predefined name, we cannot validate the return object */
163
164 if (!predefined) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800165 goto cleanup;
Bob Mooreeeb44372008-11-13 11:19:24 +0800166 }
167
168 /*
Bob Moore0444e8f2009-06-24 13:38:02 +0800169 * If the method failed or did not actually return an object, we cannot
170 * validate the return object
Bob Mooreeeb44372008-11-13 11:19:24 +0800171 */
Bob Moore0444e8f2009-06-24 13:38:02 +0800172 if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
173 goto cleanup;
Bob Mooreeeb44372008-11-13 11:19:24 +0800174 }
175
Bob Mooree8707b32008-09-28 15:26:17 +0800176 /*
177 * If there is no return value, check if we require a return value for
178 * this predefined name. Either one return value is expected, or none,
179 * for both methods and other objects.
180 *
181 * Exit now if there is no return object. Warning if one was expected.
182 */
183 if (!return_object) {
184 if ((predefined->info.expected_btypes) &&
185 (!(predefined->info.expected_btypes & ACPI_RTYPE_NONE))) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800186 ACPI_WARN_PREDEFINED((AE_INFO, pathname,
187 ACPI_WARN_ALWAYS,
188 "Missing expected return value"));
Bob Mooree8707b32008-09-28 15:26:17 +0800189
190 status = AE_AML_NO_RETURN_VALUE;
191 }
Bob Moore0444e8f2009-06-24 13:38:02 +0800192 goto cleanup;
Bob Mooree8707b32008-09-28 15:26:17 +0800193 }
194
195 /*
Bob Moore307a0422009-09-03 09:55:40 +0800196 * 1) We have a return value, but if one wasn't expected, just exit, this is
Bob Moore0444e8f2009-06-24 13:38:02 +0800197 * not a problem. For example, if the "Implicit Return" feature is
198 * enabled, methods will always return a value.
Bob Moore307a0422009-09-03 09:55:40 +0800199 *
200 * 2) If the return value can be of any type, then we cannot perform any
201 * validation, exit.
Bob Mooree8707b32008-09-28 15:26:17 +0800202 */
Bob Moore307a0422009-09-03 09:55:40 +0800203 if ((!predefined->info.expected_btypes) ||
204 (predefined->info.expected_btypes == ACPI_RTYPE_ALL)) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800205 goto cleanup;
Bob Mooree8707b32008-09-28 15:26:17 +0800206 }
207
Bob Moore0444e8f2009-06-24 13:38:02 +0800208 /* Create the parameter data block for object validation */
209
210 data = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_predefined_data));
211 if (!data) {
212 goto cleanup;
213 }
214 data->predefined = predefined;
215 data->node_flags = node->flags;
216 data->pathname = pathname;
217
Bob Mooreea7c5ec2009-12-11 15:18:52 +0800218 /* TBD: For variable-length Packages, remove NULL elements here */
219
Bob Mooree8707b32008-09-28 15:26:17 +0800220 /*
221 * Check that the type of the return object is what is expected for
222 * this predefined name
223 */
Bob Moore0444e8f2009-06-24 13:38:02 +0800224 status = acpi_ns_check_object_type(data, return_object_ptr,
Bob Mooree8707b32008-09-28 15:26:17 +0800225 predefined->info.expected_btypes,
Bob Moore0444e8f2009-06-24 13:38:02 +0800226 ACPI_NOT_PACKAGE_ELEMENT);
Bob Moore34c39c72009-12-11 14:53:11 +0800227 if (ACPI_SUCCESS(status)) {
Bob Mooreea7c5ec2009-12-11 15:18:52 +0800228 /*
229 * For returned Package objects, check the type of all sub-objects.
230 * Note: Package may have been created by call above.
231 */
232 if ((*return_object_ptr)->common.type == ACPI_TYPE_PACKAGE) {
Bob Moore34c39c72009-12-11 14:53:11 +0800233 status = acpi_ns_check_package(data, return_object_ptr);
234 }
Bob Mooree8707b32008-09-28 15:26:17 +0800235 }
236
Bob Mooread5babe2009-11-12 09:44:06 +0800237 /*
238 * Perform additional, more complicated repairs on a per-name
Bob Moore34c39c72009-12-11 14:53:11 +0800239 * basis. Do this regardless of the status from above.
Bob Mooread5babe2009-11-12 09:44:06 +0800240 */
241 status = acpi_ns_complex_repairs(data, node, status, return_object_ptr);
242
Bob Moore0444e8f2009-06-24 13:38:02 +0800243 /*
244 * If the object validation failed or if we successfully repaired one
245 * or more objects, mark the parent node to suppress further warning
246 * messages during the next evaluation of the same method/object.
247 */
248 if (ACPI_FAILURE(status) || (data->flags & ACPI_OBJECT_REPAIRED)) {
249 node->flags |= ANOBJ_EVALUATED;
250 }
251 ACPI_FREE(data);
252
253cleanup:
Bob Moore65259092009-04-22 12:57:40 +0800254 ACPI_FREE(pathname);
Bob Mooree8707b32008-09-28 15:26:17 +0800255 return (status);
256}
257
258/*******************************************************************************
259 *
260 * FUNCTION: acpi_ns_check_parameter_count
261 *
262 * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
263 * Node - Namespace node for the method/object
Bob Mooreeeb44372008-11-13 11:19:24 +0800264 * user_param_count - Number of args passed in by the caller
Bob Mooree8707b32008-09-28 15:26:17 +0800265 * Predefined - Pointer to entry in predefined name table
266 *
267 * RETURN: None
268 *
269 * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
270 * predefined name is what is expected (i.e., what is defined in
271 * the ACPI specification for this predefined name.)
272 *
273 ******************************************************************************/
274
275void
276acpi_ns_check_parameter_count(char *pathname,
277 struct acpi_namespace_node *node,
Bob Mooreeeb44372008-11-13 11:19:24 +0800278 u32 user_param_count,
Bob Mooree8707b32008-09-28 15:26:17 +0800279 const union acpi_predefined_info *predefined)
280{
281 u32 param_count;
282 u32 required_params_current;
283 u32 required_params_old;
284
Bob Mooreeeb44372008-11-13 11:19:24 +0800285 /* Methods have 0-7 parameters. All other types have zero. */
286
Bob Mooree8707b32008-09-28 15:26:17 +0800287 param_count = 0;
288 if (node->type == ACPI_TYPE_METHOD) {
289 param_count = node->object->method.param_count;
290 }
291
Bob Mooreeeb44372008-11-13 11:19:24 +0800292 if (!predefined) {
293 /*
Bob Moore0444e8f2009-06-24 13:38:02 +0800294 * Check the parameter count for non-predefined methods/objects.
295 *
Bob Mooreeeb44372008-11-13 11:19:24 +0800296 * Warning if too few or too many arguments have been passed by the
297 * caller. An incorrect number of arguments may not cause the method
298 * to fail. However, the method will fail if there are too few
299 * arguments and the method attempts to use one of the missing ones.
300 */
301 if (user_param_count < param_count) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800302 ACPI_WARN_PREDEFINED((AE_INFO, pathname,
303 ACPI_WARN_ALWAYS,
304 "Insufficient arguments - needs %u, found %u",
305 param_count, user_param_count));
Bob Mooreeeb44372008-11-13 11:19:24 +0800306 } else if (user_param_count > param_count) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800307 ACPI_WARN_PREDEFINED((AE_INFO, pathname,
308 ACPI_WARN_ALWAYS,
309 "Excess arguments - needs %u, found %u",
310 param_count, user_param_count));
Bob Mooreeeb44372008-11-13 11:19:24 +0800311 }
312 return;
313 }
314
Bob Moore0444e8f2009-06-24 13:38:02 +0800315 /*
316 * Validate the user-supplied parameter count.
317 * Allow two different legal argument counts (_SCP, etc.)
318 */
Bob Mooree8707b32008-09-28 15:26:17 +0800319 required_params_current = predefined->info.param_count & 0x0F;
320 required_params_old = predefined->info.param_count >> 4;
321
Bob Mooreeeb44372008-11-13 11:19:24 +0800322 if (user_param_count != ACPI_UINT32_MAX) {
Bob Mooreeeb44372008-11-13 11:19:24 +0800323 if ((user_param_count != required_params_current) &&
324 (user_param_count != required_params_old)) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800325 ACPI_WARN_PREDEFINED((AE_INFO, pathname,
326 ACPI_WARN_ALWAYS,
327 "Parameter count mismatch - "
328 "caller passed %u, ACPI requires %u",
329 user_param_count,
330 required_params_current));
Bob Mooreeeb44372008-11-13 11:19:24 +0800331 }
332 }
333
334 /*
Bob Mooreeeb44372008-11-13 11:19:24 +0800335 * Check that the ASL-defined parameter count is what is expected for
Bob Moore0444e8f2009-06-24 13:38:02 +0800336 * this predefined name (parameter count as defined by the ACPI
337 * specification)
Bob Mooreeeb44372008-11-13 11:19:24 +0800338 */
Bob Mooree8707b32008-09-28 15:26:17 +0800339 if ((param_count != required_params_current) &&
340 (param_count != required_params_old)) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800341 ACPI_WARN_PREDEFINED((AE_INFO, pathname, node->flags,
342 "Parameter count mismatch - ASL declared %u, ACPI requires %u",
343 param_count, required_params_current));
Bob Mooree8707b32008-09-28 15:26:17 +0800344 }
345}
346
347/*******************************************************************************
348 *
349 * FUNCTION: acpi_ns_check_for_predefined_name
350 *
351 * PARAMETERS: Node - Namespace node for the method/object
352 *
353 * RETURN: Pointer to entry in predefined table. NULL indicates not found.
354 *
355 * DESCRIPTION: Check an object name against the predefined object list.
356 *
357 ******************************************************************************/
358
359const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct
360 acpi_namespace_node
361 *node)
362{
363 const union acpi_predefined_info *this_name;
364
365 /* Quick check for a predefined name, first character must be underscore */
366
367 if (node->name.ascii[0] != '_') {
368 return (NULL);
369 }
370
371 /* Search info table for a predefined method/object name */
372
373 this_name = predefined_names;
374 while (this_name->info.name[0]) {
375 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) {
Bob Mooree8707b32008-09-28 15:26:17 +0800376 return (this_name);
377 }
378
379 /*
380 * Skip next entry in the table if this name returns a Package
381 * (next entry contains the package info)
382 */
383 if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) {
384 this_name++;
385 }
386
387 this_name++;
388 }
389
Bob Moore0444e8f2009-06-24 13:38:02 +0800390 return (NULL); /* Not found */
Bob Mooree8707b32008-09-28 15:26:17 +0800391}
392
393/*******************************************************************************
394 *
395 * FUNCTION: acpi_ns_check_package
396 *
Bob Moore0444e8f2009-06-24 13:38:02 +0800397 * PARAMETERS: Data - Pointer to validation data structure
Bob Moorea647b5c2008-11-14 08:44:39 +0800398 * return_object_ptr - Pointer to the object returned from the
399 * evaluation of a method or object
Bob Mooree8707b32008-09-28 15:26:17 +0800400 *
401 * RETURN: Status
402 *
403 * DESCRIPTION: Check a returned package object for the correct count and
404 * correct type of all sub-objects.
405 *
406 ******************************************************************************/
407
408static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +0800409acpi_ns_check_package(struct acpi_predefined_data *data,
410 union acpi_operand_object **return_object_ptr)
Bob Mooree8707b32008-09-28 15:26:17 +0800411{
Bob Moorea647b5c2008-11-14 08:44:39 +0800412 union acpi_operand_object *return_object = *return_object_ptr;
Bob Mooree8707b32008-09-28 15:26:17 +0800413 const union acpi_predefined_info *package;
Bob Mooree8707b32008-09-28 15:26:17 +0800414 union acpi_operand_object **elements;
Bob Moore53e93872009-07-24 11:22:11 +0800415 acpi_status status = AE_OK;
Bob Mooree8707b32008-09-28 15:26:17 +0800416 u32 expected_count;
417 u32 count;
418 u32 i;
Bob Mooree8707b32008-09-28 15:26:17 +0800419
420 ACPI_FUNCTION_NAME(ns_check_package);
421
422 /* The package info for this name is in the next table entry */
423
Bob Moore0444e8f2009-06-24 13:38:02 +0800424 package = data->predefined + 1;
Bob Mooree8707b32008-09-28 15:26:17 +0800425
426 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
427 "%s Validating return Package of Type %X, Count %X\n",
Bob Moore0444e8f2009-06-24 13:38:02 +0800428 data->pathname, package->ret_info.type,
Bob Mooree8707b32008-09-28 15:26:17 +0800429 return_object->package.count));
430
431 /* Extract package count and elements array */
432
433 elements = return_object->package.elements;
434 count = return_object->package.count;
435
436 /* The package must have at least one element, else invalid */
437
438 if (!count) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800439 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
440 "Return Package has no elements (empty)"));
Bob Mooree8707b32008-09-28 15:26:17 +0800441
442 return (AE_AML_OPERAND_VALUE);
443 }
444
445 /*
446 * Decode the type of the expected package contents
447 *
448 * PTYPE1 packages contain no subpackages
449 * PTYPE2 packages contain sub-packages
450 */
451 switch (package->ret_info.type) {
452 case ACPI_PTYPE1_FIXED:
453
454 /*
455 * The package count is fixed and there are no sub-packages
456 *
457 * If package is too small, exit.
458 * If package is larger than expected, issue warning but continue
459 */
460 expected_count =
461 package->ret_info.count1 + package->ret_info.count2;
462 if (count < expected_count) {
463 goto package_too_small;
464 } else if (count > expected_count) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800465 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
466 data->node_flags,
467 "Return Package is larger than needed - "
468 "found %u, expected %u", count,
469 expected_count));
Bob Mooree8707b32008-09-28 15:26:17 +0800470 }
471
472 /* Validate all elements of the returned package */
473
Bob Moore0444e8f2009-06-24 13:38:02 +0800474 status = acpi_ns_check_package_elements(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800475 package->ret_info.
476 object_type1,
477 package->ret_info.
478 count1,
479 package->ret_info.
480 object_type2,
481 package->ret_info.
Bob Moore03ef1322009-03-19 10:14:45 +0800482 count2, 0);
Bob Mooree8707b32008-09-28 15:26:17 +0800483 break;
484
485 case ACPI_PTYPE1_VAR:
486
487 /*
488 * The package count is variable, there are no sub-packages, and all
489 * elements must be of the same type
490 */
491 for (i = 0; i < count; i++) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800492 status = acpi_ns_check_object_type(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800493 package->ret_info.
494 object_type1, i);
495 if (ACPI_FAILURE(status)) {
496 return (status);
497 }
498 elements++;
499 }
500 break;
501
502 case ACPI_PTYPE1_OPTION:
503
504 /*
505 * The package count is variable, there are no sub-packages. There are
506 * a fixed number of required elements, and a variable number of
507 * optional elements.
508 *
509 * Check if package is at least as large as the minimum required
510 */
511 expected_count = package->ret_info3.count;
512 if (count < expected_count) {
513 goto package_too_small;
514 }
515
516 /* Variable number of sub-objects */
517
518 for (i = 0; i < count; i++) {
519 if (i < package->ret_info3.count) {
520
521 /* These are the required package elements (0, 1, or 2) */
522
523 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800524 acpi_ns_check_object_type(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800525 package->
526 ret_info3.
527 object_type[i],
528 i);
529 if (ACPI_FAILURE(status)) {
530 return (status);
531 }
532 } else {
533 /* These are the optional package elements */
534
535 status =
Bob Moore0444e8f2009-06-24 13:38:02 +0800536 acpi_ns_check_object_type(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800537 package->
538 ret_info3.
539 tail_object_type,
540 i);
541 if (ACPI_FAILURE(status)) {
542 return (status);
543 }
544 }
545 elements++;
546 }
547 break;
548
Bob Moore53e93872009-07-24 11:22:11 +0800549 case ACPI_PTYPE2_REV_FIXED:
550
551 /* First element is the (Integer) revision */
552
553 status = acpi_ns_check_object_type(data, elements,
554 ACPI_RTYPE_INTEGER, 0);
555 if (ACPI_FAILURE(status)) {
556 return (status);
557 }
558
559 elements++;
560 count--;
561
562 /* Examine the sub-packages */
563
564 status =
565 acpi_ns_check_package_list(data, package, elements, count);
566 break;
567
Bob Mooree8707b32008-09-28 15:26:17 +0800568 case ACPI_PTYPE2_PKG_COUNT:
569
570 /* First element is the (Integer) count of sub-packages to follow */
571
Bob Moore0444e8f2009-06-24 13:38:02 +0800572 status = acpi_ns_check_object_type(data, elements,
Bob Mooree8707b32008-09-28 15:26:17 +0800573 ACPI_RTYPE_INTEGER, 0);
574 if (ACPI_FAILURE(status)) {
575 return (status);
576 }
577
578 /*
579 * Count cannot be larger than the parent package length, but allow it
580 * to be smaller. The >= accounts for the Integer above.
581 */
582 expected_count = (u32) (*elements)->integer.value;
583 if (expected_count >= count) {
584 goto package_too_small;
585 }
586
587 count = expected_count;
588 elements++;
589
Bob Moore53e93872009-07-24 11:22:11 +0800590 /* Examine the sub-packages */
Bob Mooree8707b32008-09-28 15:26:17 +0800591
Bob Moore53e93872009-07-24 11:22:11 +0800592 status =
593 acpi_ns_check_package_list(data, package, elements, count);
594 break;
Bob Mooree8707b32008-09-28 15:26:17 +0800595
596 case ACPI_PTYPE2:
597 case ACPI_PTYPE2_FIXED:
598 case ACPI_PTYPE2_MIN:
599 case ACPI_PTYPE2_COUNT:
600
601 /*
Bob Mooree5f69d62009-07-24 11:03:09 +0800602 * These types all return a single Package that consists of a
603 * variable number of sub-Packages.
604 *
605 * First, ensure that the first element is a sub-Package. If not,
606 * the BIOS may have incorrectly returned the object as a single
607 * package instead of a Package of Packages (a common error if
608 * there is only one entry). We may be able to repair this by
609 * wrapping the returned Package with a new outer Package.
Bob Mooree8707b32008-09-28 15:26:17 +0800610 */
Bob Moore419a9092009-11-12 09:36:00 +0800611 if (*elements
612 && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
Bob Mooree5f69d62009-07-24 11:03:09 +0800613
614 /* Create the new outer package and populate it */
615
616 status =
617 acpi_ns_repair_package_list(data,
618 return_object_ptr);
619 if (ACPI_FAILURE(status)) {
620 return (status);
621 }
622
623 /* Update locals to point to the new package (of 1 element) */
624
625 return_object = *return_object_ptr;
626 elements = return_object->package.elements;
627 count = 1;
628 }
629
Bob Moore53e93872009-07-24 11:22:11 +0800630 /* Examine the sub-packages */
Bob Mooree5f69d62009-07-24 11:03:09 +0800631
Bob Moore53e93872009-07-24 11:22:11 +0800632 status =
633 acpi_ns_check_package_list(data, package, elements, count);
Bob Mooree8707b32008-09-28 15:26:17 +0800634 break;
635
636 default:
637
638 /* Should not get here if predefined info table is correct */
639
Bob Moore0444e8f2009-06-24 13:38:02 +0800640 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
641 "Invalid internal return type in table entry: %X",
642 package->ret_info.type));
Bob Mooree8707b32008-09-28 15:26:17 +0800643
644 return (AE_AML_INTERNAL);
645 }
646
Bob Moore53e93872009-07-24 11:22:11 +0800647 return (status);
Bob Mooree8707b32008-09-28 15:26:17 +0800648
Bob Moore53e93872009-07-24 11:22:11 +0800649package_too_small:
Bob Mooree8707b32008-09-28 15:26:17 +0800650
651 /* Error exit for the case with an incorrect package count */
652
Bob Moore0444e8f2009-06-24 13:38:02 +0800653 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
Bob Moore53e93872009-07-24 11:22:11 +0800654 "Return Package is too small - found %u elements, expected %u",
Bob Moore0444e8f2009-06-24 13:38:02 +0800655 count, expected_count));
Bob Mooree8707b32008-09-28 15:26:17 +0800656
657 return (AE_AML_OPERAND_VALUE);
658}
659
660/*******************************************************************************
661 *
Bob Moore53e93872009-07-24 11:22:11 +0800662 * FUNCTION: acpi_ns_check_package_list
663 *
664 * PARAMETERS: Data - Pointer to validation data structure
665 * Package - Pointer to package-specific info for method
666 * Elements - Element list of parent package. All elements
667 * of this list should be of type Package.
668 * Count - Count of subpackages
669 *
670 * RETURN: Status
671 *
672 * DESCRIPTION: Examine a list of subpackages
673 *
674 ******************************************************************************/
675
676static acpi_status
677acpi_ns_check_package_list(struct acpi_predefined_data *data,
678 const union acpi_predefined_info *package,
679 union acpi_operand_object **elements, u32 count)
680{
681 union acpi_operand_object *sub_package;
682 union acpi_operand_object **sub_elements;
683 acpi_status status;
Bob Moore419a9092009-11-12 09:36:00 +0800684 u8 non_trailing_null = FALSE;
Bob Moore53e93872009-07-24 11:22:11 +0800685 u32 expected_count;
686 u32 i;
687 u32 j;
688
689 /* Validate each sub-Package in the parent Package */
690
691 for (i = 0; i < count; i++) {
Bob Moore419a9092009-11-12 09:36:00 +0800692 /*
693 * Handling for NULL package elements. For now, we will simply allow
694 * a parent package with trailing NULL elements. This can happen if
695 * the package was defined to be longer than the initializer list.
696 * This is legal as per the ACPI specification. It is often used
697 * to allow for dynamic initialization of a Package.
698 *
699 * A future enhancement may be to simply truncate the package to
700 * remove the trailing NULL elements.
701 */
702 if (!(*elements)) {
703 if (!non_trailing_null) {
704
705 /* Ensure the remaining elements are all NULL */
706
707 for (j = 1; j < (count - i + 1); j++) {
708 if (elements[j]) {
709 non_trailing_null = TRUE;
710 }
711 }
712
713 if (!non_trailing_null) {
714
715 /* Ignore the trailing NULL elements */
716
717 return (AE_OK);
718 }
719 }
720
721 /* There are trailing non-null elements, issue warning */
722
723 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
724 data->node_flags,
725 "Found NULL element at package index %u",
726 i));
727 elements++;
728 continue;
729 }
730
Bob Moore53e93872009-07-24 11:22:11 +0800731 sub_package = *elements;
732 sub_elements = sub_package->package.elements;
733
734 /* Each sub-object must be of type Package */
735
736 status = acpi_ns_check_object_type(data, &sub_package,
737 ACPI_RTYPE_PACKAGE, i);
738 if (ACPI_FAILURE(status)) {
739 return (status);
740 }
741
742 /* Examine the different types of expected sub-packages */
743
744 switch (package->ret_info.type) {
745 case ACPI_PTYPE2:
746 case ACPI_PTYPE2_PKG_COUNT:
747 case ACPI_PTYPE2_REV_FIXED:
748
749 /* Each subpackage has a fixed number of elements */
750
751 expected_count =
752 package->ret_info.count1 + package->ret_info.count2;
753 if (sub_package->package.count < expected_count) {
754 goto package_too_small;
755 }
756
757 status =
758 acpi_ns_check_package_elements(data, sub_elements,
759 package->ret_info.
760 object_type1,
761 package->ret_info.
762 count1,
763 package->ret_info.
764 object_type2,
765 package->ret_info.
766 count2, 0);
767 if (ACPI_FAILURE(status)) {
768 return (status);
769 }
770 break;
771
772 case ACPI_PTYPE2_FIXED:
773
774 /* Each sub-package has a fixed length */
775
776 expected_count = package->ret_info2.count;
777 if (sub_package->package.count < expected_count) {
778 goto package_too_small;
779 }
780
781 /* Check the type of each sub-package element */
782
783 for (j = 0; j < expected_count; j++) {
784 status =
785 acpi_ns_check_object_type(data,
786 &sub_elements[j],
787 package->
788 ret_info2.
789 object_type[j],
790 j);
791 if (ACPI_FAILURE(status)) {
792 return (status);
793 }
794 }
795 break;
796
797 case ACPI_PTYPE2_MIN:
798
799 /* Each sub-package has a variable but minimum length */
800
801 expected_count = package->ret_info.count1;
802 if (sub_package->package.count < expected_count) {
803 goto package_too_small;
804 }
805
806 /* Check the type of each sub-package element */
807
808 status =
809 acpi_ns_check_package_elements(data, sub_elements,
810 package->ret_info.
811 object_type1,
812 sub_package->package.
813 count, 0, 0, 0);
814 if (ACPI_FAILURE(status)) {
815 return (status);
816 }
817 break;
818
819 case ACPI_PTYPE2_COUNT:
820
821 /*
822 * First element is the (Integer) count of elements, including
823 * the count field.
824 */
825 status = acpi_ns_check_object_type(data, sub_elements,
826 ACPI_RTYPE_INTEGER,
827 0);
828 if (ACPI_FAILURE(status)) {
829 return (status);
830 }
831
832 /*
833 * Make sure package is large enough for the Count and is
834 * is as large as the minimum size
835 */
836 expected_count = (u32)(*sub_elements)->integer.value;
837 if (sub_package->package.count < expected_count) {
838 goto package_too_small;
839 }
840 if (sub_package->package.count <
841 package->ret_info.count1) {
842 expected_count = package->ret_info.count1;
843 goto package_too_small;
844 }
845
846 /* Check the type of each sub-package element */
847
848 status =
849 acpi_ns_check_package_elements(data,
850 (sub_elements + 1),
851 package->ret_info.
852 object_type1,
853 (expected_count - 1),
854 0, 0, 1);
855 if (ACPI_FAILURE(status)) {
856 return (status);
857 }
858 break;
859
860 default: /* Should not get here, type was validated by caller */
861
862 return (AE_AML_INTERNAL);
863 }
864
865 elements++;
866 }
867
868 return (AE_OK);
869
870package_too_small:
871
872 /* The sub-package count was smaller than required */
873
874 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
875 "Return Sub-Package[%u] is too small - found %u elements, expected %u",
876 i, sub_package->package.count, expected_count));
877
878 return (AE_AML_OPERAND_VALUE);
879}
880
881/*******************************************************************************
882 *
Bob Mooree8707b32008-09-28 15:26:17 +0800883 * FUNCTION: acpi_ns_check_package_elements
884 *
Bob Moore0444e8f2009-06-24 13:38:02 +0800885 * PARAMETERS: Data - Pointer to validation data structure
Bob Mooree8707b32008-09-28 15:26:17 +0800886 * Elements - Pointer to the package elements array
887 * Type1 - Object type for first group
888 * Count1 - Count for first group
889 * Type2 - Object type for second group
890 * Count2 - Count for second group
Bob Moore03ef1322009-03-19 10:14:45 +0800891 * start_index - Start of the first group of elements
Bob Mooree8707b32008-09-28 15:26:17 +0800892 *
893 * RETURN: Status
894 *
895 * DESCRIPTION: Check that all elements of a package are of the correct object
896 * type. Supports up to two groups of different object types.
897 *
898 ******************************************************************************/
899
900static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +0800901acpi_ns_check_package_elements(struct acpi_predefined_data *data,
Bob Mooree8707b32008-09-28 15:26:17 +0800902 union acpi_operand_object **elements,
Bob Moore03ef1322009-03-19 10:14:45 +0800903 u8 type1,
904 u32 count1,
905 u8 type2, u32 count2, u32 start_index)
Bob Mooree8707b32008-09-28 15:26:17 +0800906{
907 union acpi_operand_object **this_element = elements;
908 acpi_status status;
909 u32 i;
910
911 /*
912 * Up to two groups of package elements are supported by the data
913 * structure. All elements in each group must be of the same type.
914 * The second group can have a count of zero.
915 */
916 for (i = 0; i < count1; i++) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800917 status = acpi_ns_check_object_type(data, this_element,
Bob Moore03ef1322009-03-19 10:14:45 +0800918 type1, i + start_index);
Bob Mooree8707b32008-09-28 15:26:17 +0800919 if (ACPI_FAILURE(status)) {
920 return (status);
921 }
922 this_element++;
923 }
924
925 for (i = 0; i < count2; i++) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800926 status = acpi_ns_check_object_type(data, this_element,
Bob Moore03ef1322009-03-19 10:14:45 +0800927 type2,
928 (i + count1 + start_index));
Bob Mooree8707b32008-09-28 15:26:17 +0800929 if (ACPI_FAILURE(status)) {
930 return (status);
931 }
932 this_element++;
933 }
934
935 return (AE_OK);
936}
937
938/*******************************************************************************
939 *
940 * FUNCTION: acpi_ns_check_object_type
941 *
Bob Moore0444e8f2009-06-24 13:38:02 +0800942 * PARAMETERS: Data - Pointer to validation data structure
Bob Moorea647b5c2008-11-14 08:44:39 +0800943 * return_object_ptr - Pointer to the object returned from the
944 * evaluation of a method or object
Bob Mooree8707b32008-09-28 15:26:17 +0800945 * expected_btypes - Bitmap of expected return type(s)
946 * package_index - Index of object within parent package (if
Bob Moore0444e8f2009-06-24 13:38:02 +0800947 * applicable - ACPI_NOT_PACKAGE_ELEMENT
948 * otherwise)
Bob Mooree8707b32008-09-28 15:26:17 +0800949 *
950 * RETURN: Status
951 *
952 * DESCRIPTION: Check the type of the return object against the expected object
953 * type(s). Use of Btype allows multiple expected object types.
954 *
955 ******************************************************************************/
956
957static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +0800958acpi_ns_check_object_type(struct acpi_predefined_data *data,
Bob Moorea647b5c2008-11-14 08:44:39 +0800959 union acpi_operand_object **return_object_ptr,
Bob Mooree8707b32008-09-28 15:26:17 +0800960 u32 expected_btypes, u32 package_index)
961{
Bob Moorea647b5c2008-11-14 08:44:39 +0800962 union acpi_operand_object *return_object = *return_object_ptr;
Bob Mooree8707b32008-09-28 15:26:17 +0800963 acpi_status status = AE_OK;
964 u32 return_btype;
965 char type_buffer[48]; /* Room for 5 types */
Bob Mooree8707b32008-09-28 15:26:17 +0800966
967 /*
968 * If we get a NULL return_object here, it is a NULL package element,
969 * and this is always an error.
970 */
971 if (!return_object) {
972 goto type_error_exit;
973 }
974
975 /* A Namespace node should not get here, but make sure */
976
977 if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
Bob Moore0444e8f2009-06-24 13:38:02 +0800978 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
979 "Invalid return type - Found a Namespace node [%4.4s] type %s",
980 return_object->node.name.ascii,
981 acpi_ut_get_type_name(return_object->node.
982 type)));
Bob Mooree8707b32008-09-28 15:26:17 +0800983 return (AE_AML_OPERAND_TYPE);
984 }
985
986 /*
987 * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
988 * The bitmapped type allows multiple possible return types.
989 *
990 * Note, the cases below must handle all of the possible types returned
991 * from all of the predefined names (including elements of returned
992 * packages)
993 */
Bob Moore3371c192009-02-18 14:44:03 +0800994 switch (return_object->common.type) {
Bob Mooree8707b32008-09-28 15:26:17 +0800995 case ACPI_TYPE_INTEGER:
996 return_btype = ACPI_RTYPE_INTEGER;
997 break;
998
999 case ACPI_TYPE_BUFFER:
1000 return_btype = ACPI_RTYPE_BUFFER;
1001 break;
1002
1003 case ACPI_TYPE_STRING:
1004 return_btype = ACPI_RTYPE_STRING;
1005 break;
1006
1007 case ACPI_TYPE_PACKAGE:
1008 return_btype = ACPI_RTYPE_PACKAGE;
1009 break;
1010
1011 case ACPI_TYPE_LOCAL_REFERENCE:
1012 return_btype = ACPI_RTYPE_REFERENCE;
1013 break;
1014
1015 default:
1016 /* Not one of the supported objects, must be incorrect */
1017
1018 goto type_error_exit;
1019 }
1020
1021 /* Is the object one of the expected types? */
1022
1023 if (!(return_btype & expected_btypes)) {
Bob Moorea647b5c2008-11-14 08:44:39 +08001024
1025 /* Type mismatch -- attempt repair of the returned object */
1026
Bob Moore0444e8f2009-06-24 13:38:02 +08001027 status = acpi_ns_repair_object(data, expected_btypes,
1028 package_index,
Bob Moorea647b5c2008-11-14 08:44:39 +08001029 return_object_ptr);
1030 if (ACPI_SUCCESS(status)) {
Bob Moore0444e8f2009-06-24 13:38:02 +08001031 return (AE_OK); /* Repair was successful */
Bob Moorea647b5c2008-11-14 08:44:39 +08001032 }
Bob Mooree8707b32008-09-28 15:26:17 +08001033 goto type_error_exit;
1034 }
1035
1036 /* For reference objects, check that the reference type is correct */
1037
Bob Moore3371c192009-02-18 14:44:03 +08001038 if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
Bob Moore0444e8f2009-06-24 13:38:02 +08001039 status = acpi_ns_check_reference(data, return_object);
Bob Mooree8707b32008-09-28 15:26:17 +08001040 }
1041
1042 return (status);
1043
1044 type_error_exit:
1045
1046 /* Create a string with all expected types for this predefined object */
1047
Bob Moore0444e8f2009-06-24 13:38:02 +08001048 acpi_ns_get_expected_types(type_buffer, expected_btypes);
Bob Mooree8707b32008-09-28 15:26:17 +08001049
Bob Moore0444e8f2009-06-24 13:38:02 +08001050 if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
1051 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
1052 "Return type mismatch - found %s, expected %s",
1053 acpi_ut_get_object_type_name
1054 (return_object), type_buffer));
Bob Mooree8707b32008-09-28 15:26:17 +08001055 } else {
Bob Moore0444e8f2009-06-24 13:38:02 +08001056 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
1057 "Return Package type mismatch at index %u - "
1058 "found %s, expected %s", package_index,
1059 acpi_ut_get_object_type_name
1060 (return_object), type_buffer));
Bob Mooree8707b32008-09-28 15:26:17 +08001061 }
1062
1063 return (AE_AML_OPERAND_TYPE);
1064}
1065
1066/*******************************************************************************
1067 *
1068 * FUNCTION: acpi_ns_check_reference
1069 *
Bob Moore0444e8f2009-06-24 13:38:02 +08001070 * PARAMETERS: Data - Pointer to validation data structure
Bob Mooree8707b32008-09-28 15:26:17 +08001071 * return_object - Object returned from the evaluation of a
1072 * method or object
1073 *
1074 * RETURN: Status
1075 *
1076 * DESCRIPTION: Check a returned reference object for the correct reference
1077 * type. The only reference type that can be returned from a
1078 * predefined method is a named reference. All others are invalid.
1079 *
1080 ******************************************************************************/
1081
1082static acpi_status
Bob Moore0444e8f2009-06-24 13:38:02 +08001083acpi_ns_check_reference(struct acpi_predefined_data *data,
Bob Mooree8707b32008-09-28 15:26:17 +08001084 union acpi_operand_object *return_object)
1085{
1086
1087 /*
1088 * Check the reference object for the correct reference type (opcode).
1089 * The only type of reference that can be converted to an union acpi_object is
1090 * a reference to a named object (reference class: NAME)
1091 */
1092 if (return_object->reference.class == ACPI_REFCLASS_NAME) {
1093 return (AE_OK);
1094 }
1095
Bob Moore0444e8f2009-06-24 13:38:02 +08001096 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
1097 "Return type mismatch - unexpected reference object type [%s] %2.2X",
1098 acpi_ut_get_reference_name(return_object),
1099 return_object->reference.class));
Bob Mooree8707b32008-09-28 15:26:17 +08001100
1101 return (AE_AML_OPERAND_TYPE);
1102}
Bob Moorea647b5c2008-11-14 08:44:39 +08001103
1104/*******************************************************************************
1105 *
Bob Moore0444e8f2009-06-24 13:38:02 +08001106 * FUNCTION: acpi_ns_get_expected_types
1107 *
1108 * PARAMETERS: Buffer - Pointer to where the string is returned
1109 * expected_btypes - Bitmap of expected return type(s)
1110 *
1111 * RETURN: Buffer is populated with type names.
1112 *
1113 * DESCRIPTION: Translate the expected types bitmap into a string of ascii
1114 * names of expected types, for use in warning messages.
1115 *
1116 ******************************************************************************/
1117
1118static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes)
1119{
1120 u32 this_rtype;
1121 u32 i;
1122 u32 j;
1123
1124 j = 1;
1125 buffer[0] = 0;
1126 this_rtype = ACPI_RTYPE_INTEGER;
1127
1128 for (i = 0; i < ACPI_NUM_RTYPES; i++) {
1129
1130 /* If one of the expected types, concatenate the name of this type */
1131
1132 if (expected_btypes & this_rtype) {
1133 ACPI_STRCAT(buffer, &acpi_rtype_names[i][j]);
1134 j = 0; /* Use name separator from now on */
1135 }
1136 this_rtype <<= 1; /* Next Rtype */
1137 }
1138}