1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Module Name: nsarguments - Validation of args for ACPI predefined methods
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun *****************************************************************************/
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <acpi/acpi.h>
11*4882a593Smuzhiyun #include "accommon.h"
12*4882a593Smuzhiyun #include "acnamesp.h"
13*4882a593Smuzhiyun #include "acpredef.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define _COMPONENT ACPI_NAMESPACE
16*4882a593Smuzhiyun ACPI_MODULE_NAME("nsarguments")
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /*******************************************************************************
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * FUNCTION: acpi_ns_check_argument_types
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * PARAMETERS: info - Method execution information block
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * RETURN: None
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * DESCRIPTION: Check the incoming argument count and all argument types
27*4882a593Smuzhiyun * against the argument type list for a predefined name.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun ******************************************************************************/
acpi_ns_check_argument_types(struct acpi_evaluate_info * info)30*4882a593Smuzhiyun void acpi_ns_check_argument_types(struct acpi_evaluate_info *info)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun u16 arg_type_list;
33*4882a593Smuzhiyun u8 arg_count;
34*4882a593Smuzhiyun u8 arg_type;
35*4882a593Smuzhiyun u8 user_arg_type;
36*4882a593Smuzhiyun u32 i;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun * If not a predefined name, cannot typecheck args, because
40*4882a593Smuzhiyun * we have no idea what argument types are expected.
41*4882a593Smuzhiyun * Also, ignore typecheck if warnings/errors if this method
42*4882a593Smuzhiyun * has already been evaluated at least once -- in order
43*4882a593Smuzhiyun * to suppress repetitive messages.
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun if (!info->predefined || (info->node->flags & ANOBJ_EVALUATED)) {
46*4882a593Smuzhiyun return;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun arg_type_list = info->predefined->info.argument_list;
50*4882a593Smuzhiyun arg_count = METHOD_GET_ARG_COUNT(arg_type_list);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* Typecheck all arguments */
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun for (i = 0; ((i < arg_count) && (i < info->param_count)); i++) {
55*4882a593Smuzhiyun arg_type = METHOD_GET_NEXT_TYPE(arg_type_list);
56*4882a593Smuzhiyun user_arg_type = info->parameters[i]->common.type;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /* No typechecking for ACPI_TYPE_ANY */
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun if ((user_arg_type != arg_type) && (arg_type != ACPI_TYPE_ANY)) {
61*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
62*4882a593Smuzhiyun ACPI_WARN_ALWAYS,
63*4882a593Smuzhiyun "Argument #%u type mismatch - "
64*4882a593Smuzhiyun "Found [%s], ACPI requires [%s]",
65*4882a593Smuzhiyun (i + 1),
66*4882a593Smuzhiyun acpi_ut_get_type_name
67*4882a593Smuzhiyun (user_arg_type),
68*4882a593Smuzhiyun acpi_ut_get_type_name(arg_type)));
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Prevent any additional typechecking for this method */
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun info->node->flags |= ANOBJ_EVALUATED;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /*******************************************************************************
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * FUNCTION: acpi_ns_check_acpi_compliance
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun * PARAMETERS: pathname - Full pathname to the node (for error msgs)
82*4882a593Smuzhiyun * node - Namespace node for the method/object
83*4882a593Smuzhiyun * predefined - Pointer to entry in predefined name table
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * RETURN: None
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * DESCRIPTION: Check that the declared parameter count (in ASL/AML) for a
88*4882a593Smuzhiyun * predefined name is what is expected (matches what is defined in
89*4882a593Smuzhiyun * the ACPI specification for this predefined name.)
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun ******************************************************************************/
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun void
acpi_ns_check_acpi_compliance(char * pathname,struct acpi_namespace_node * node,const union acpi_predefined_info * predefined)94*4882a593Smuzhiyun acpi_ns_check_acpi_compliance(char *pathname,
95*4882a593Smuzhiyun struct acpi_namespace_node *node,
96*4882a593Smuzhiyun const union acpi_predefined_info *predefined)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun u32 aml_param_count;
99*4882a593Smuzhiyun u32 required_param_count;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun if (!predefined || (node->flags & ANOBJ_EVALUATED)) {
102*4882a593Smuzhiyun return;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /* Get the ACPI-required arg count from the predefined info table */
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun required_param_count =
108*4882a593Smuzhiyun METHOD_GET_ARG_COUNT(predefined->info.argument_list);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun * If this object is not a control method, we can check if the ACPI
112*4882a593Smuzhiyun * spec requires that it be a method.
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun if (node->type != ACPI_TYPE_METHOD) {
115*4882a593Smuzhiyun if (required_param_count > 0) {
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* Object requires args, must be implemented as a method */
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun ACPI_BIOS_ERROR_PREDEFINED((AE_INFO, pathname,
120*4882a593Smuzhiyun ACPI_WARN_ALWAYS,
121*4882a593Smuzhiyun "Object (%s) must be a control method with %u arguments",
122*4882a593Smuzhiyun acpi_ut_get_type_name(node->
123*4882a593Smuzhiyun type),
124*4882a593Smuzhiyun required_param_count));
125*4882a593Smuzhiyun } else if (!required_param_count
126*4882a593Smuzhiyun && !predefined->info.expected_btypes) {
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /* Object requires no args and no return value, must be a method */
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun ACPI_BIOS_ERROR_PREDEFINED((AE_INFO, pathname,
131*4882a593Smuzhiyun ACPI_WARN_ALWAYS,
132*4882a593Smuzhiyun "Object (%s) must be a control method "
133*4882a593Smuzhiyun "with no arguments and no return value",
134*4882a593Smuzhiyun acpi_ut_get_type_name(node->
135*4882a593Smuzhiyun type)));
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * This is a control method.
143*4882a593Smuzhiyun * Check that the ASL/AML-defined parameter count for this method
144*4882a593Smuzhiyun * matches the ACPI-required parameter count
145*4882a593Smuzhiyun *
146*4882a593Smuzhiyun * Some methods are allowed to have a "minimum" number of args (_SCP)
147*4882a593Smuzhiyun * because their definition in ACPI has changed over time.
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * Note: These are BIOS errors in the declaration of the object
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun aml_param_count = node->object->method.param_count;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (aml_param_count < required_param_count) {
154*4882a593Smuzhiyun ACPI_BIOS_ERROR_PREDEFINED((AE_INFO, pathname, ACPI_WARN_ALWAYS,
155*4882a593Smuzhiyun "Insufficient arguments - "
156*4882a593Smuzhiyun "ASL declared %u, ACPI requires %u",
157*4882a593Smuzhiyun aml_param_count,
158*4882a593Smuzhiyun required_param_count));
159*4882a593Smuzhiyun } else if ((aml_param_count > required_param_count)
160*4882a593Smuzhiyun && !(predefined->info.
161*4882a593Smuzhiyun argument_list & ARG_COUNT_IS_MINIMUM)) {
162*4882a593Smuzhiyun ACPI_BIOS_ERROR_PREDEFINED((AE_INFO, pathname, ACPI_WARN_ALWAYS,
163*4882a593Smuzhiyun "Excess arguments - "
164*4882a593Smuzhiyun "ASL declared %u, ACPI requires %u",
165*4882a593Smuzhiyun aml_param_count,
166*4882a593Smuzhiyun required_param_count));
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /*******************************************************************************
171*4882a593Smuzhiyun *
172*4882a593Smuzhiyun * FUNCTION: acpi_ns_check_argument_count
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * PARAMETERS: pathname - Full pathname to the node (for error msgs)
175*4882a593Smuzhiyun * node - Namespace node for the method/object
176*4882a593Smuzhiyun * user_param_count - Number of args passed in by the caller
177*4882a593Smuzhiyun * predefined - Pointer to entry in predefined name table
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * RETURN: None
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun * DESCRIPTION: Check that incoming argument count matches the declared
182*4882a593Smuzhiyun * parameter count (in the ASL/AML) for an object.
183*4882a593Smuzhiyun *
184*4882a593Smuzhiyun ******************************************************************************/
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun void
acpi_ns_check_argument_count(char * pathname,struct acpi_namespace_node * node,u32 user_param_count,const union acpi_predefined_info * predefined)187*4882a593Smuzhiyun acpi_ns_check_argument_count(char *pathname,
188*4882a593Smuzhiyun struct acpi_namespace_node *node,
189*4882a593Smuzhiyun u32 user_param_count,
190*4882a593Smuzhiyun const union acpi_predefined_info *predefined)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun u32 aml_param_count;
193*4882a593Smuzhiyun u32 required_param_count;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun if (node->flags & ANOBJ_EVALUATED) {
196*4882a593Smuzhiyun return;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun if (!predefined) {
200*4882a593Smuzhiyun /*
201*4882a593Smuzhiyun * Not a predefined name. Check the incoming user argument count
202*4882a593Smuzhiyun * against the count that is specified in the method/object.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun if (node->type != ACPI_TYPE_METHOD) {
205*4882a593Smuzhiyun if (user_param_count) {
206*4882a593Smuzhiyun ACPI_INFO_PREDEFINED((AE_INFO, pathname,
207*4882a593Smuzhiyun ACPI_WARN_ALWAYS,
208*4882a593Smuzhiyun "%u arguments were passed to a non-method ACPI object (%s)",
209*4882a593Smuzhiyun user_param_count,
210*4882a593Smuzhiyun acpi_ut_get_type_name
211*4882a593Smuzhiyun (node->type)));
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun return;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /*
218*4882a593Smuzhiyun * This is a control method. Check the parameter count.
219*4882a593Smuzhiyun * We can only check the incoming argument count against the
220*4882a593Smuzhiyun * argument count declared for the method in the ASL/AML.
221*4882a593Smuzhiyun *
222*4882a593Smuzhiyun * Emit a message if too few or too many arguments have been passed
223*4882a593Smuzhiyun * by the caller.
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun * Note: Too many arguments will not cause the method to
226*4882a593Smuzhiyun * fail. However, the method will fail if there are too few
227*4882a593Smuzhiyun * arguments and the method attempts to use one of the missing ones.
228*4882a593Smuzhiyun */
229*4882a593Smuzhiyun aml_param_count = node->object->method.param_count;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun if (user_param_count < aml_param_count) {
232*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, pathname,
233*4882a593Smuzhiyun ACPI_WARN_ALWAYS,
234*4882a593Smuzhiyun "Insufficient arguments - "
235*4882a593Smuzhiyun "Caller passed %u, method requires %u",
236*4882a593Smuzhiyun user_param_count,
237*4882a593Smuzhiyun aml_param_count));
238*4882a593Smuzhiyun } else if (user_param_count > aml_param_count) {
239*4882a593Smuzhiyun ACPI_INFO_PREDEFINED((AE_INFO, pathname,
240*4882a593Smuzhiyun ACPI_WARN_ALWAYS,
241*4882a593Smuzhiyun "Excess arguments - "
242*4882a593Smuzhiyun "Caller passed %u, method requires %u",
243*4882a593Smuzhiyun user_param_count,
244*4882a593Smuzhiyun aml_param_count));
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun return;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun * This is a predefined name. Validate the user-supplied parameter
252*4882a593Smuzhiyun * count against the ACPI specification. We don't validate against
253*4882a593Smuzhiyun * the method itself because what is important here is that the
254*4882a593Smuzhiyun * caller is in conformance with the spec. (The arg count for the
255*4882a593Smuzhiyun * method was checked against the ACPI spec earlier.)
256*4882a593Smuzhiyun *
257*4882a593Smuzhiyun * Some methods are allowed to have a "minimum" number of args (_SCP)
258*4882a593Smuzhiyun * because their definition in ACPI has changed over time.
259*4882a593Smuzhiyun */
260*4882a593Smuzhiyun required_param_count =
261*4882a593Smuzhiyun METHOD_GET_ARG_COUNT(predefined->info.argument_list);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (user_param_count < required_param_count) {
264*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, pathname, ACPI_WARN_ALWAYS,
265*4882a593Smuzhiyun "Insufficient arguments - "
266*4882a593Smuzhiyun "Caller passed %u, ACPI requires %u",
267*4882a593Smuzhiyun user_param_count, required_param_count));
268*4882a593Smuzhiyun } else if ((user_param_count > required_param_count) &&
269*4882a593Smuzhiyun !(predefined->info.argument_list & ARG_COUNT_IS_MINIMUM)) {
270*4882a593Smuzhiyun ACPI_INFO_PREDEFINED((AE_INFO, pathname, ACPI_WARN_ALWAYS,
271*4882a593Smuzhiyun "Excess arguments - "
272*4882a593Smuzhiyun "Caller passed %u, ACPI requires %u",
273*4882a593Smuzhiyun user_param_count, required_param_count));
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun }
276