1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Module Name: nspredef - Validation of ACPI predefined methods and objects
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun *****************************************************************************/
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #define ACPI_CREATE_PREDEFINED_TABLE
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <acpi/acpi.h>
13*4882a593Smuzhiyun #include "accommon.h"
14*4882a593Smuzhiyun #include "acnamesp.h"
15*4882a593Smuzhiyun #include "acpredef.h"
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define _COMPONENT ACPI_NAMESPACE
18*4882a593Smuzhiyun ACPI_MODULE_NAME("nspredef")
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /*******************************************************************************
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * This module validates predefined ACPI objects that appear in the namespace,
23*4882a593Smuzhiyun * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
24*4882a593Smuzhiyun * validation is to detect problems with BIOS-exposed predefined ACPI objects
25*4882a593Smuzhiyun * before the results are returned to the ACPI-related drivers.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * There are several areas that are validated:
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * 1) The number of input arguments as defined by the method/object in the
30*4882a593Smuzhiyun * ASL is validated against the ACPI specification.
31*4882a593Smuzhiyun * 2) The type of the return object (if any) is validated against the ACPI
32*4882a593Smuzhiyun * specification.
33*4882a593Smuzhiyun * 3) For returned package objects, the count of package elements is
34*4882a593Smuzhiyun * validated, as well as the type of each package element. Nested
35*4882a593Smuzhiyun * packages are supported.
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * For any problems found, a warning message is issued.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun ******************************************************************************/
40*4882a593Smuzhiyun /* Local prototypes */
41*4882a593Smuzhiyun static acpi_status
42*4882a593Smuzhiyun acpi_ns_check_reference(struct acpi_evaluate_info *info,
43*4882a593Smuzhiyun union acpi_operand_object *return_object);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*******************************************************************************
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * FUNCTION: acpi_ns_check_return_value
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * PARAMETERS: node - Namespace node for the method/object
52*4882a593Smuzhiyun * info - Method execution information block
53*4882a593Smuzhiyun * user_param_count - Number of parameters actually passed
54*4882a593Smuzhiyun * return_status - Status from the object evaluation
55*4882a593Smuzhiyun * return_object_ptr - Pointer to the object returned from the
56*4882a593Smuzhiyun * evaluation of a method or object
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * RETURN: Status
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * DESCRIPTION: Check the value returned from a predefined name.
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun ******************************************************************************/
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun acpi_status
acpi_ns_check_return_value(struct acpi_namespace_node * node,struct acpi_evaluate_info * info,u32 user_param_count,acpi_status return_status,union acpi_operand_object ** return_object_ptr)65*4882a593Smuzhiyun acpi_ns_check_return_value(struct acpi_namespace_node *node,
66*4882a593Smuzhiyun struct acpi_evaluate_info *info,
67*4882a593Smuzhiyun u32 user_param_count,
68*4882a593Smuzhiyun acpi_status return_status,
69*4882a593Smuzhiyun union acpi_operand_object **return_object_ptr)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun acpi_status status;
72*4882a593Smuzhiyun const union acpi_predefined_info *predefined;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* If not a predefined name, we cannot validate the return object */
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun predefined = info->predefined;
77*4882a593Smuzhiyun if (!predefined) {
78*4882a593Smuzhiyun return (AE_OK);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun * If the method failed or did not actually return an object, we cannot
83*4882a593Smuzhiyun * validate the return object
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
86*4882a593Smuzhiyun return (AE_OK);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /*
90*4882a593Smuzhiyun * Return value validation and possible repair.
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * 1) Don't perform return value validation/repair if this feature
93*4882a593Smuzhiyun * has been disabled via a global option.
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * 2) We have a return value, but if one wasn't expected, just exit,
96*4882a593Smuzhiyun * this is not a problem. For example, if the "Implicit Return"
97*4882a593Smuzhiyun * feature is enabled, methods will always return a value.
98*4882a593Smuzhiyun *
99*4882a593Smuzhiyun * 3) If the return value can be of any type, then we cannot perform
100*4882a593Smuzhiyun * any validation, just exit.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun if (acpi_gbl_disable_auto_repair ||
103*4882a593Smuzhiyun (!predefined->info.expected_btypes) ||
104*4882a593Smuzhiyun (predefined->info.expected_btypes == ACPI_RTYPE_ALL)) {
105*4882a593Smuzhiyun return (AE_OK);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * Check that the type of the main return object is what is expected
110*4882a593Smuzhiyun * for this predefined name
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun status = acpi_ns_check_object_type(info, return_object_ptr,
113*4882a593Smuzhiyun predefined->info.expected_btypes,
114*4882a593Smuzhiyun ACPI_NOT_PACKAGE_ELEMENT);
115*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
116*4882a593Smuzhiyun goto exit;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * 4) If there is no return value and it is optional, just return
122*4882a593Smuzhiyun * AE_OK (_WAK).
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun if (!(*return_object_ptr)) {
125*4882a593Smuzhiyun goto exit;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * For returned Package objects, check the type of all sub-objects.
130*4882a593Smuzhiyun * Note: Package may have been newly created by call above.
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun if ((*return_object_ptr)->common.type == ACPI_TYPE_PACKAGE) {
133*4882a593Smuzhiyun info->parent_package = *return_object_ptr;
134*4882a593Smuzhiyun status = acpi_ns_check_package(info, return_object_ptr);
135*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /* We might be able to fix some errors */
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun if ((status != AE_AML_OPERAND_TYPE) &&
140*4882a593Smuzhiyun (status != AE_AML_OPERAND_VALUE)) {
141*4882a593Smuzhiyun goto exit;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /*
147*4882a593Smuzhiyun * The return object was OK, or it was successfully repaired above.
148*4882a593Smuzhiyun * Now make some additional checks such as verifying that package
149*4882a593Smuzhiyun * objects are sorted correctly (if required) or buffer objects have
150*4882a593Smuzhiyun * the correct data width (bytes vs. dwords). These repairs are
151*4882a593Smuzhiyun * performed on a per-name basis, i.e., the code is specific to
152*4882a593Smuzhiyun * particular predefined names.
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun status = acpi_ns_complex_repairs(info, node, status, return_object_ptr);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun exit:
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun * If the object validation failed or if we successfully repaired one
159*4882a593Smuzhiyun * or more objects, mark the parent node to suppress further warning
160*4882a593Smuzhiyun * messages during the next evaluation of the same method/object.
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun if (ACPI_FAILURE(status) || (info->return_flags & ACPI_OBJECT_REPAIRED)) {
163*4882a593Smuzhiyun node->flags |= ANOBJ_EVALUATED;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun return (status);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /*******************************************************************************
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * FUNCTION: acpi_ns_check_object_type
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * PARAMETERS: info - Method execution information block
174*4882a593Smuzhiyun * return_object_ptr - Pointer to the object returned from the
175*4882a593Smuzhiyun * evaluation of a method or object
176*4882a593Smuzhiyun * expected_btypes - Bitmap of expected return type(s)
177*4882a593Smuzhiyun * package_index - Index of object within parent package (if
178*4882a593Smuzhiyun * applicable - ACPI_NOT_PACKAGE_ELEMENT
179*4882a593Smuzhiyun * otherwise)
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun * RETURN: Status
182*4882a593Smuzhiyun *
183*4882a593Smuzhiyun * DESCRIPTION: Check the type of the return object against the expected object
184*4882a593Smuzhiyun * type(s). Use of Btype allows multiple expected object types.
185*4882a593Smuzhiyun *
186*4882a593Smuzhiyun ******************************************************************************/
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun acpi_status
acpi_ns_check_object_type(struct acpi_evaluate_info * info,union acpi_operand_object ** return_object_ptr,u32 expected_btypes,u32 package_index)189*4882a593Smuzhiyun acpi_ns_check_object_type(struct acpi_evaluate_info *info,
190*4882a593Smuzhiyun union acpi_operand_object **return_object_ptr,
191*4882a593Smuzhiyun u32 expected_btypes, u32 package_index)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun union acpi_operand_object *return_object = *return_object_ptr;
194*4882a593Smuzhiyun acpi_status status = AE_OK;
195*4882a593Smuzhiyun char type_buffer[96]; /* Room for 10 types */
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /* A Namespace node should not get here, but make sure */
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun if (return_object &&
200*4882a593Smuzhiyun ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
201*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
202*4882a593Smuzhiyun info->node_flags,
203*4882a593Smuzhiyun "Invalid return type - Found a Namespace node [%4.4s] type %s",
204*4882a593Smuzhiyun return_object->node.name.ascii,
205*4882a593Smuzhiyun acpi_ut_get_type_name(return_object->node.
206*4882a593Smuzhiyun type)));
207*4882a593Smuzhiyun return (AE_AML_OPERAND_TYPE);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
212*4882a593Smuzhiyun * The bitmapped type allows multiple possible return types.
213*4882a593Smuzhiyun *
214*4882a593Smuzhiyun * Note, the cases below must handle all of the possible types returned
215*4882a593Smuzhiyun * from all of the predefined names (including elements of returned
216*4882a593Smuzhiyun * packages)
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun info->return_btype = acpi_ns_get_bitmapped_type(return_object);
219*4882a593Smuzhiyun if (info->return_btype == ACPI_RTYPE_ANY) {
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* Not one of the supported objects, must be incorrect */
222*4882a593Smuzhiyun goto type_error_exit;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /* For reference objects, check that the reference type is correct */
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun if ((info->return_btype & expected_btypes) == ACPI_RTYPE_REFERENCE) {
228*4882a593Smuzhiyun status = acpi_ns_check_reference(info, return_object);
229*4882a593Smuzhiyun return (status);
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* Attempt simple repair of the returned object if necessary */
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun status = acpi_ns_simple_repair(info, expected_btypes,
235*4882a593Smuzhiyun package_index, return_object_ptr);
236*4882a593Smuzhiyun if (ACPI_SUCCESS(status)) {
237*4882a593Smuzhiyun return (AE_OK); /* Successful repair */
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun type_error_exit:
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /* Create a string with all expected types for this predefined object */
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun acpi_ut_get_expected_return_types(type_buffer, expected_btypes);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun if (!return_object) {
247*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
248*4882a593Smuzhiyun info->node_flags,
249*4882a593Smuzhiyun "Expected return object of type %s",
250*4882a593Smuzhiyun type_buffer));
251*4882a593Smuzhiyun } else if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
252*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
253*4882a593Smuzhiyun info->node_flags,
254*4882a593Smuzhiyun "Return type mismatch - found %s, expected %s",
255*4882a593Smuzhiyun acpi_ut_get_object_type_name
256*4882a593Smuzhiyun (return_object), type_buffer));
257*4882a593Smuzhiyun } else {
258*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
259*4882a593Smuzhiyun info->node_flags,
260*4882a593Smuzhiyun "Return Package type mismatch at index %u - "
261*4882a593Smuzhiyun "found %s, expected %s", package_index,
262*4882a593Smuzhiyun acpi_ut_get_object_type_name
263*4882a593Smuzhiyun (return_object), type_buffer));
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun return (AE_AML_OPERAND_TYPE);
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /*******************************************************************************
270*4882a593Smuzhiyun *
271*4882a593Smuzhiyun * FUNCTION: acpi_ns_check_reference
272*4882a593Smuzhiyun *
273*4882a593Smuzhiyun * PARAMETERS: info - Method execution information block
274*4882a593Smuzhiyun * return_object - Object returned from the evaluation of a
275*4882a593Smuzhiyun * method or object
276*4882a593Smuzhiyun *
277*4882a593Smuzhiyun * RETURN: Status
278*4882a593Smuzhiyun *
279*4882a593Smuzhiyun * DESCRIPTION: Check a returned reference object for the correct reference
280*4882a593Smuzhiyun * type. The only reference type that can be returned from a
281*4882a593Smuzhiyun * predefined method is a named reference. All others are invalid.
282*4882a593Smuzhiyun *
283*4882a593Smuzhiyun ******************************************************************************/
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun static acpi_status
acpi_ns_check_reference(struct acpi_evaluate_info * info,union acpi_operand_object * return_object)286*4882a593Smuzhiyun acpi_ns_check_reference(struct acpi_evaluate_info *info,
287*4882a593Smuzhiyun union acpi_operand_object *return_object)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /*
291*4882a593Smuzhiyun * Check the reference object for the correct reference type (opcode).
292*4882a593Smuzhiyun * The only type of reference that can be converted to a union acpi_object is
293*4882a593Smuzhiyun * a reference to a named object (reference class: NAME)
294*4882a593Smuzhiyun */
295*4882a593Smuzhiyun if (return_object->reference.class == ACPI_REFCLASS_NAME) {
296*4882a593Smuzhiyun return (AE_OK);
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
300*4882a593Smuzhiyun "Return type mismatch - unexpected reference object type [%s] %2.2X",
301*4882a593Smuzhiyun acpi_ut_get_reference_name(return_object),
302*4882a593Smuzhiyun return_object->reference.class));
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun return (AE_AML_OPERAND_TYPE);
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun /*******************************************************************************
308*4882a593Smuzhiyun *
309*4882a593Smuzhiyun * FUNCTION: acpi_ns_get_bitmapped_type
310*4882a593Smuzhiyun *
311*4882a593Smuzhiyun * PARAMETERS: return_object - Object returned from method/obj evaluation
312*4882a593Smuzhiyun *
313*4882a593Smuzhiyun * RETURN: Object return type. ACPI_RTYPE_ANY indicates that the object
314*4882a593Smuzhiyun * type is not supported. ACPI_RTYPE_NONE indicates that no
315*4882a593Smuzhiyun * object was returned (return_object is NULL).
316*4882a593Smuzhiyun *
317*4882a593Smuzhiyun * DESCRIPTION: Convert object type into a bitmapped object return type.
318*4882a593Smuzhiyun *
319*4882a593Smuzhiyun ******************************************************************************/
320*4882a593Smuzhiyun
acpi_ns_get_bitmapped_type(union acpi_operand_object * return_object)321*4882a593Smuzhiyun static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun u32 return_btype;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (!return_object) {
326*4882a593Smuzhiyun return (ACPI_RTYPE_NONE);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /* Map acpi_object_type to internal bitmapped type */
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun switch (return_object->common.type) {
332*4882a593Smuzhiyun case ACPI_TYPE_INTEGER:
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun return_btype = ACPI_RTYPE_INTEGER;
335*4882a593Smuzhiyun break;
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun case ACPI_TYPE_BUFFER:
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun return_btype = ACPI_RTYPE_BUFFER;
340*4882a593Smuzhiyun break;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun case ACPI_TYPE_STRING:
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun return_btype = ACPI_RTYPE_STRING;
345*4882a593Smuzhiyun break;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun case ACPI_TYPE_PACKAGE:
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun return_btype = ACPI_RTYPE_PACKAGE;
350*4882a593Smuzhiyun break;
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun case ACPI_TYPE_LOCAL_REFERENCE:
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun return_btype = ACPI_RTYPE_REFERENCE;
355*4882a593Smuzhiyun break;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun default:
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /* Not one of the supported objects, must be incorrect */
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun return_btype = ACPI_RTYPE_ANY;
362*4882a593Smuzhiyun break;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun return (return_btype);
366*4882a593Smuzhiyun }
367