xref: /openbmc/linux/drivers/acpi/acpica/nsrepair.c (revision 6ee73861)
1 /******************************************************************************
2  *
3  * Module Name: nsrepair - Repair for objects returned by predefined methods
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2009, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "acpredef.h"
48 
49 #define _COMPONENT          ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsrepair")
51 
52 /*******************************************************************************
53  *
54  * FUNCTION:    acpi_ns_repair_object
55  *
56  * PARAMETERS:  Data                - Pointer to validation data structure
57  *              expected_btypes     - Object types expected
58  *              package_index       - Index of object within parent package (if
59  *                                    applicable - ACPI_NOT_PACKAGE_ELEMENT
60  *                                    otherwise)
61  *              return_object_ptr   - Pointer to the object returned from the
62  *                                    evaluation of a method or object
63  *
64  * RETURN:      Status. AE_OK if repair was successful.
65  *
66  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
67  *              not expected.
68  *
69  ******************************************************************************/
70 acpi_status
71 acpi_ns_repair_object(struct acpi_predefined_data *data,
72 		      u32 expected_btypes,
73 		      u32 package_index,
74 		      union acpi_operand_object **return_object_ptr)
75 {
76 	union acpi_operand_object *return_object = *return_object_ptr;
77 	union acpi_operand_object *new_object;
78 	acpi_size length;
79 
80 	switch (return_object->common.type) {
81 	case ACPI_TYPE_BUFFER:
82 
83 		/* Does the method/object legally return a string? */
84 
85 		if (!(expected_btypes & ACPI_RTYPE_STRING)) {
86 			return (AE_AML_OPERAND_TYPE);
87 		}
88 
89 		/*
90 		 * Have a Buffer, expected a String, convert. Use a to_string
91 		 * conversion, no transform performed on the buffer data. The best
92 		 * example of this is the _BIF method, where the string data from
93 		 * the battery is often (incorrectly) returned as buffer object(s).
94 		 */
95 		length = 0;
96 		while ((length < return_object->buffer.length) &&
97 		       (return_object->buffer.pointer[length])) {
98 			length++;
99 		}
100 
101 		/* Allocate a new string object */
102 
103 		new_object = acpi_ut_create_string_object(length);
104 		if (!new_object) {
105 			return (AE_NO_MEMORY);
106 		}
107 
108 		/*
109 		 * Copy the raw buffer data with no transform. String is already NULL
110 		 * terminated at Length+1.
111 		 */
112 		ACPI_MEMCPY(new_object->string.pointer,
113 			    return_object->buffer.pointer, length);
114 
115 		/*
116 		 * If the original object is a package element, we need to:
117 		 * 1. Set the reference count of the new object to match the
118 		 *    reference count of the old object.
119 		 * 2. Decrement the reference count of the original object.
120 		 */
121 		if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
122 			new_object->common.reference_count =
123 			    return_object->common.reference_count;
124 
125 			if (return_object->common.reference_count > 1) {
126 				return_object->common.reference_count--;
127 			}
128 
129 			ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
130 					      data->node_flags,
131 					      "Converted Buffer to expected String at index %u",
132 					      package_index));
133 		} else {
134 			ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
135 					      data->node_flags,
136 					      "Converted Buffer to expected String"));
137 		}
138 
139 		/* Delete old object, install the new return object */
140 
141 		acpi_ut_remove_reference(return_object);
142 		*return_object_ptr = new_object;
143 		data->flags |= ACPI_OBJECT_REPAIRED;
144 		return (AE_OK);
145 
146 	default:
147 		break;
148 	}
149 
150 	return (AE_AML_OPERAND_TYPE);
151 }
152 
153 /*******************************************************************************
154  *
155  * FUNCTION:    acpi_ns_repair_package_list
156  *
157  * PARAMETERS:  Data                - Pointer to validation data structure
158  *              obj_desc_ptr        - Pointer to the object to repair. The new
159  *                                    package object is returned here,
160  *                                    overwriting the old object.
161  *
162  * RETURN:      Status, new object in *obj_desc_ptr
163  *
164  * DESCRIPTION: Repair a common problem with objects that are defined to return
165  *              a variable-length Package of Packages. If the variable-length
166  *              is one, some BIOS code mistakenly simply declares a single
167  *              Package instead of a Package with one sub-Package. This
168  *              function attempts to repair this error by wrapping a Package
169  *              object around the original Package, creating the correct
170  *              Package with one sub-Package.
171  *
172  *              Names that can be repaired in this manner include:
173  *              _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
174  *
175  ******************************************************************************/
176 
177 acpi_status
178 acpi_ns_repair_package_list(struct acpi_predefined_data *data,
179 			    union acpi_operand_object **obj_desc_ptr)
180 {
181 	union acpi_operand_object *pkg_obj_desc;
182 
183 	/*
184 	 * Create the new outer package and populate it. The new package will
185 	 * have a single element, the lone subpackage.
186 	 */
187 	pkg_obj_desc = acpi_ut_create_package_object(1);
188 	if (!pkg_obj_desc) {
189 		return (AE_NO_MEMORY);
190 	}
191 
192 	pkg_obj_desc->package.elements[0] = *obj_desc_ptr;
193 
194 	/* Return the new object in the object pointer */
195 
196 	*obj_desc_ptr = pkg_obj_desc;
197 	data->flags |= ACPI_OBJECT_REPAIRED;
198 
199 	ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
200 			      "Incorrectly formed Package, attempting repair"));
201 
202 	return (AE_OK);
203 }
204