blob: f9c9fd45cd1f767e4333a20e804402a626f04718 [file] [log] [blame]
Bob Mooreb2deadd2009-07-24 10:56:43 +08001/******************************************************************************
2 *
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
4 *
5 *****************************************************************************/
6
7/*
Bob Moore25f044e2013-01-25 05:38:56 +00008 * Copyright (C) 2000 - 2013, Intel Corp.
Bob Mooreb2deadd2009-07-24 10:56:43 +08009 * 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"
Lin Ming0240d7b2009-10-13 10:23:20 +080047#include "acinterp.h"
Bob Moore091f4d72010-01-21 09:28:32 +080048#include "acpredef.h"
Bob Moored5a36102013-03-08 09:23:03 +000049#include "amlresrc.h"
Bob Mooreb2deadd2009-07-24 10:56:43 +080050
51#define _COMPONENT ACPI_NAMESPACE
52ACPI_MODULE_NAME("nsrepair")
53
54/*******************************************************************************
55 *
Bob Moore47e11d52009-12-11 15:01:12 +080056 * This module attempts to repair or convert objects returned by the
57 * predefined methods to an object type that is expected, as per the ACPI
58 * specification. The need for this code is dictated by the many machines that
59 * return incorrect types for the standard predefined methods. Performing these
60 * conversions here, in one place, eliminates the need for individual ACPI
61 * device drivers to do the same. Note: Most of these conversions are different
62 * than the internal object conversion routines used for implicit object
63 * conversion.
64 *
65 * The following conversions can be performed as necessary:
66 *
67 * Integer -> String
68 * Integer -> Buffer
69 * String -> Integer
70 * String -> Buffer
71 * Buffer -> Integer
72 * Buffer -> String
73 * Buffer -> Package of Integers
74 * Package -> Package of one Package
Bob Moored5a36102013-03-08 09:23:03 +000075 *
76 * Additional conversions that are available:
77 * Convert a null return or zero return value to an end_tag descriptor
78 * Convert an ASCII string to a Unicode buffer
79 *
Bob Moore6a99b1c2012-03-21 09:51:39 +080080 * An incorrect standalone object is wrapped with required outer package
Bob Moore47e11d52009-12-11 15:01:12 +080081 *
Bob Moore091f4d72010-01-21 09:28:32 +080082 * Additional possible repairs:
Bob Moore091f4d72010-01-21 09:28:32 +080083 * Required package elements that are NULL replaced by Integer/String/Buffer
Bob Moore091f4d72010-01-21 09:28:32 +080084 *
Bob Moore47e11d52009-12-11 15:01:12 +080085 ******************************************************************************/
86/* Local prototypes */
87static acpi_status
88acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
89 union acpi_operand_object **return_object);
90
91static acpi_status
92acpi_ns_convert_to_string(union acpi_operand_object *original_object,
93 union acpi_operand_object **return_object);
94
95static acpi_status
96acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
97 union acpi_operand_object **return_object);
98
Bob Moored5a36102013-03-08 09:23:03 +000099static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
100 acpi_namespace_node
101 *node,
102 u32
103 return_btype,
104 u32
105 package_index);
106
107/*
108 * Special but simple repairs for some names.
109 *
110 * 2nd argument: Unexpected types that can be repaired
111 */
112static const struct acpi_simple_repair_info acpi_object_repair_info[] = {
113 {{0, 0, 0, 0}, 0, 0, NULL} /* Table terminator */
114};
115
Bob Moore47e11d52009-12-11 15:01:12 +0800116/*******************************************************************************
117 *
Bob Moored5a36102013-03-08 09:23:03 +0000118 * FUNCTION: acpi_ns_simple_repair
Bob Mooreb2deadd2009-07-24 10:56:43 +0800119 *
Bob Mooreba494be2012-07-12 09:40:10 +0800120 * PARAMETERS: data - Pointer to validation data structure
Bob Mooreb2deadd2009-07-24 10:56:43 +0800121 * expected_btypes - Object types expected
122 * package_index - Index of object within parent package (if
123 * applicable - ACPI_NOT_PACKAGE_ELEMENT
124 * otherwise)
125 * return_object_ptr - Pointer to the object returned from the
126 * evaluation of a method or object
127 *
128 * RETURN: Status. AE_OK if repair was successful.
129 *
130 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
131 * not expected.
132 *
133 ******************************************************************************/
Bob Moore47e11d52009-12-11 15:01:12 +0800134
Bob Mooreb2deadd2009-07-24 10:56:43 +0800135acpi_status
Bob Moored5a36102013-03-08 09:23:03 +0000136acpi_ns_simple_repair(struct acpi_predefined_data *data,
Bob Mooreb2deadd2009-07-24 10:56:43 +0800137 u32 expected_btypes,
138 u32 package_index,
139 union acpi_operand_object **return_object_ptr)
140{
141 union acpi_operand_object *return_object = *return_object_ptr;
Bob Moored5a36102013-03-08 09:23:03 +0000142 union acpi_operand_object *new_object = NULL;
Lin Ming0240d7b2009-10-13 10:23:20 +0800143 acpi_status status;
Bob Moored5a36102013-03-08 09:23:03 +0000144 const struct acpi_simple_repair_info *predefined;
Bob Mooreb2deadd2009-07-24 10:56:43 +0800145
Bob Moored5a36102013-03-08 09:23:03 +0000146 ACPI_FUNCTION_NAME(ns_simple_repair);
147
148 /*
149 * Special repairs for certain names that are in the repair table.
150 * Check if this name is in the list of repairable names.
151 */
152 predefined = acpi_ns_match_simple_repair(data->node,
153 data->return_btype,
154 package_index);
155 if (predefined) {
156 if (!return_object) {
157 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
158 ACPI_WARN_ALWAYS,
159 "Missing expected return value"));
160 }
161
162 status =
163 predefined->object_converter(return_object, &new_object);
164 if (ACPI_FAILURE(status)) {
165
166 /* A fatal error occurred during a conversion */
167
168 ACPI_EXCEPTION((AE_INFO, status,
169 "During return object analysis"));
170 return (status);
171 }
172 if (new_object) {
173 goto object_repaired;
174 }
175 }
176
177 /*
178 * Do not perform simple object repair unless the return type is not
179 * expected.
180 */
181 if (data->return_btype & expected_btypes) {
182 return (AE_OK);
183 }
Bob Moore3a581762009-12-11 15:23:22 +0800184
Bob Moore27526992009-10-13 10:20:33 +0800185 /*
186 * At this point, we know that the type of the returned object was not
187 * one of the expected types for this predefined name. Attempt to
Bob Moore47e11d52009-12-11 15:01:12 +0800188 * repair the object by converting it to one of the expected object
189 * types for this predefined name.
Bob Moore27526992009-10-13 10:20:33 +0800190 */
Bob Moored5a36102013-03-08 09:23:03 +0000191
192 /*
193 * If there is no return value, check if we require a return value for
194 * this predefined name. Either one return value is expected, or none,
195 * for both methods and other objects.
196 *
197 * Exit now if there is no return object. Warning if one was expected.
198 */
199 if (!return_object) {
200 if (expected_btypes && (!(expected_btypes & ACPI_RTYPE_NONE))) {
201 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
202 ACPI_WARN_ALWAYS,
203 "Missing expected return value"));
204
205 return (AE_AML_NO_RETURN_VALUE);
206 }
207 }
208
Bob Moore47e11d52009-12-11 15:01:12 +0800209 if (expected_btypes & ACPI_RTYPE_INTEGER) {
210 status = acpi_ns_convert_to_integer(return_object, &new_object);
211 if (ACPI_SUCCESS(status)) {
212 goto object_repaired;
Bob Mooreb2deadd2009-07-24 10:56:43 +0800213 }
Bob Mooreb2deadd2009-07-24 10:56:43 +0800214 }
Bob Moore47e11d52009-12-11 15:01:12 +0800215 if (expected_btypes & ACPI_RTYPE_STRING) {
216 status = acpi_ns_convert_to_string(return_object, &new_object);
217 if (ACPI_SUCCESS(status)) {
218 goto object_repaired;
219 }
220 }
221 if (expected_btypes & ACPI_RTYPE_BUFFER) {
222 status = acpi_ns_convert_to_buffer(return_object, &new_object);
223 if (ACPI_SUCCESS(status)) {
224 goto object_repaired;
225 }
226 }
227 if (expected_btypes & ACPI_RTYPE_PACKAGE) {
Bob Moore6a99b1c2012-03-21 09:51:39 +0800228 /*
229 * A package is expected. We will wrap the existing object with a
230 * new package object. It is often the case that if a variable-length
231 * package is required, but there is only a single object needed, the
232 * BIOS will return that object instead of wrapping it with a Package
233 * object. Note: after the wrapping, the package will be validated
234 * for correct contents (expected object type or types).
235 */
236 status =
237 acpi_ns_wrap_with_package(data, return_object, &new_object);
Bob Moore47e11d52009-12-11 15:01:12 +0800238 if (ACPI_SUCCESS(status)) {
Bob Moore6a99b1c2012-03-21 09:51:39 +0800239 /*
240 * The original object just had its reference count
241 * incremented for being inserted into the new package.
242 */
243 *return_object_ptr = new_object; /* New Package object */
244 data->flags |= ACPI_OBJECT_REPAIRED;
245 return (AE_OK);
Bob Moore47e11d52009-12-11 15:01:12 +0800246 }
247 }
248
249 /* We cannot repair this object */
250
251 return (AE_AML_OPERAND_TYPE);
252
253 object_repaired:
Bob Mooreb2deadd2009-07-24 10:56:43 +0800254
Bob Moore27526992009-10-13 10:20:33 +0800255 /* Object was successfully repaired */
256
Bob Moore27526992009-10-13 10:20:33 +0800257 if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
Bob Moore6a99b1c2012-03-21 09:51:39 +0800258 /*
259 * The original object is a package element. We need to
260 * decrement the reference count of the original object,
261 * for removing it from the package.
262 *
263 * However, if the original object was just wrapped with a
264 * package object as part of the repair, we don't need to
265 * change the reference count.
266 */
267 if (!(data->flags & ACPI_OBJECT_WRAPPED)) {
268 new_object->common.reference_count =
269 return_object->common.reference_count;
Bob Moore27526992009-10-13 10:20:33 +0800270
Bob Moore6a99b1c2012-03-21 09:51:39 +0800271 if (return_object->common.reference_count > 1) {
272 return_object->common.reference_count--;
273 }
Bob Moore27526992009-10-13 10:20:33 +0800274 }
275
Bob Moore3a581762009-12-11 15:23:22 +0800276 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
Bob Moore6a99b1c2012-03-21 09:51:39 +0800277 "%s: Converted %s to expected %s at Package index %u\n",
Bob Moore3a581762009-12-11 15:23:22 +0800278 data->pathname,
279 acpi_ut_get_object_type_name(return_object),
280 acpi_ut_get_object_type_name(new_object),
281 package_index));
Bob Moore27526992009-10-13 10:20:33 +0800282 } else {
Bob Moore3a581762009-12-11 15:23:22 +0800283 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
284 "%s: Converted %s to expected %s\n",
285 data->pathname,
286 acpi_ut_get_object_type_name(return_object),
287 acpi_ut_get_object_type_name(new_object)));
Bob Moore27526992009-10-13 10:20:33 +0800288 }
289
290 /* Delete old object, install the new return object */
291
292 acpi_ut_remove_reference(return_object);
293 *return_object_ptr = new_object;
294 data->flags |= ACPI_OBJECT_REPAIRED;
295 return (AE_OK);
Bob Mooreb2deadd2009-07-24 10:56:43 +0800296}
Bob Mooree5f69d62009-07-24 11:03:09 +0800297
Bob Moored5a36102013-03-08 09:23:03 +0000298/******************************************************************************
299 *
300 * FUNCTION: acpi_ns_match_simple_repair
301 *
302 * PARAMETERS: node - Namespace node for the method/object
303 * return_btype - Object type that was returned
304 * package_index - Index of object within parent package (if
305 * applicable - ACPI_NOT_PACKAGE_ELEMENT
306 * otherwise)
307 *
308 * RETURN: Pointer to entry in repair table. NULL indicates not found.
309 *
310 * DESCRIPTION: Check an object name against the repairable object list.
311 *
312 *****************************************************************************/
313
314static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
315 acpi_namespace_node
316 *node,
317 u32
318 return_btype,
319 u32
320 package_index)
321{
322 const struct acpi_simple_repair_info *this_name;
323
324 /* Search info table for a repairable predefined method/object name */
325
326 this_name = acpi_object_repair_info;
327 while (this_name->object_converter) {
328 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
329
330 /* Check if we can actually repair this name/type combination */
331
332 if ((return_btype & this_name->unexpected_btypes) &&
333 (package_index == this_name->package_index)) {
334 return (this_name);
335 }
336
337 return (NULL);
338 }
339 this_name++;
340 }
341
342 return (NULL); /* Name was not found in the repair table */
343}
344
Bob Mooree5f69d62009-07-24 11:03:09 +0800345/*******************************************************************************
346 *
Bob Moore47e11d52009-12-11 15:01:12 +0800347 * FUNCTION: acpi_ns_convert_to_integer
348 *
349 * PARAMETERS: original_object - Object to be converted
350 * return_object - Where the new converted object is returned
351 *
352 * RETURN: Status. AE_OK if conversion was successful.
353 *
354 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
355 *
356 ******************************************************************************/
357
358static acpi_status
359acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
360 union acpi_operand_object **return_object)
361{
362 union acpi_operand_object *new_object;
363 acpi_status status;
364 u64 value = 0;
365 u32 i;
366
367 switch (original_object->common.type) {
368 case ACPI_TYPE_STRING:
369
370 /* String-to-Integer conversion */
371
372 status = acpi_ut_strtoul64(original_object->string.pointer,
373 ACPI_ANY_BASE, &value);
374 if (ACPI_FAILURE(status)) {
375 return (status);
376 }
377 break;
378
379 case ACPI_TYPE_BUFFER:
380
381 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
382
383 if (original_object->buffer.length > 8) {
384 return (AE_AML_OPERAND_TYPE);
385 }
386
387 /* Extract each buffer byte to create the integer */
388
389 for (i = 0; i < original_object->buffer.length; i++) {
390 value |=
391 ((u64) original_object->buffer.
392 pointer[i] << (i * 8));
393 }
394 break;
395
396 default:
397 return (AE_AML_OPERAND_TYPE);
398 }
399
400 new_object = acpi_ut_create_integer_object(value);
401 if (!new_object) {
402 return (AE_NO_MEMORY);
403 }
404
405 *return_object = new_object;
406 return (AE_OK);
407}
408
409/*******************************************************************************
410 *
411 * FUNCTION: acpi_ns_convert_to_string
412 *
413 * PARAMETERS: original_object - Object to be converted
414 * return_object - Where the new converted object is returned
415 *
416 * RETURN: Status. AE_OK if conversion was successful.
417 *
418 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
419 *
420 ******************************************************************************/
421
422static acpi_status
423acpi_ns_convert_to_string(union acpi_operand_object *original_object,
424 union acpi_operand_object **return_object)
425{
426 union acpi_operand_object *new_object;
427 acpi_size length;
428 acpi_status status;
429
430 switch (original_object->common.type) {
431 case ACPI_TYPE_INTEGER:
432 /*
433 * Integer-to-String conversion. Commonly, convert
434 * an integer of value 0 to a NULL string. The last element of
435 * _BIF and _BIX packages occasionally need this fix.
436 */
437 if (original_object->integer.value == 0) {
438
439 /* Allocate a new NULL string object */
440
441 new_object = acpi_ut_create_string_object(0);
442 if (!new_object) {
443 return (AE_NO_MEMORY);
444 }
445 } else {
446 status =
447 acpi_ex_convert_to_string(original_object,
448 &new_object,
449 ACPI_IMPLICIT_CONVERT_HEX);
450 if (ACPI_FAILURE(status)) {
451 return (status);
452 }
453 }
454 break;
455
456 case ACPI_TYPE_BUFFER:
457 /*
458 * Buffer-to-String conversion. Use a to_string
459 * conversion, no transform performed on the buffer data. The best
460 * example of this is the _BIF method, where the string data from
461 * the battery is often (incorrectly) returned as buffer object(s).
462 */
463 length = 0;
464 while ((length < original_object->buffer.length) &&
465 (original_object->buffer.pointer[length])) {
466 length++;
467 }
468
469 /* Allocate a new string object */
470
471 new_object = acpi_ut_create_string_object(length);
472 if (!new_object) {
473 return (AE_NO_MEMORY);
474 }
475
476 /*
477 * Copy the raw buffer data with no transform. String is already NULL
478 * terminated at Length+1.
479 */
480 ACPI_MEMCPY(new_object->string.pointer,
481 original_object->buffer.pointer, length);
482 break;
483
484 default:
485 return (AE_AML_OPERAND_TYPE);
486 }
487
488 *return_object = new_object;
489 return (AE_OK);
490}
491
492/*******************************************************************************
493 *
494 * FUNCTION: acpi_ns_convert_to_buffer
495 *
496 * PARAMETERS: original_object - Object to be converted
497 * return_object - Where the new converted object is returned
498 *
499 * RETURN: Status. AE_OK if conversion was successful.
500 *
Bob Mooreea7c5ec2009-12-11 15:18:52 +0800501 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
Bob Moore47e11d52009-12-11 15:01:12 +0800502 *
503 ******************************************************************************/
504
505static acpi_status
506acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
507 union acpi_operand_object **return_object)
508{
509 union acpi_operand_object *new_object;
510 acpi_status status;
Bob Mooreea7c5ec2009-12-11 15:18:52 +0800511 union acpi_operand_object **elements;
512 u32 *dword_buffer;
513 u32 count;
514 u32 i;
Bob Moore47e11d52009-12-11 15:01:12 +0800515
516 switch (original_object->common.type) {
517 case ACPI_TYPE_INTEGER:
518 /*
519 * Integer-to-Buffer conversion.
520 * Convert the Integer to a packed-byte buffer. _MAT and other
521 * objects need this sometimes, if a read has been performed on a
522 * Field object that is less than or equal to the global integer
523 * size (32 or 64 bits).
524 */
525 status =
526 acpi_ex_convert_to_buffer(original_object, &new_object);
527 if (ACPI_FAILURE(status)) {
528 return (status);
529 }
530 break;
531
532 case ACPI_TYPE_STRING:
533
534 /* String-to-Buffer conversion. Simple data copy */
535
536 new_object =
537 acpi_ut_create_buffer_object(original_object->string.
538 length);
539 if (!new_object) {
540 return (AE_NO_MEMORY);
541 }
542
543 ACPI_MEMCPY(new_object->buffer.pointer,
544 original_object->string.pointer,
545 original_object->string.length);
546 break;
547
Bob Mooreea7c5ec2009-12-11 15:18:52 +0800548 case ACPI_TYPE_PACKAGE:
Bob Moore43420bb2009-12-11 15:24:27 +0800549 /*
550 * This case is often seen for predefined names that must return a
551 * Buffer object with multiple DWORD integers within. For example,
552 * _FDE and _GTM. The Package can be converted to a Buffer.
553 */
Bob Mooreea7c5ec2009-12-11 15:18:52 +0800554
555 /* All elements of the Package must be integers */
556
557 elements = original_object->package.elements;
558 count = original_object->package.count;
559
560 for (i = 0; i < count; i++) {
561 if ((!*elements) ||
562 ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
563 return (AE_AML_OPERAND_TYPE);
564 }
565 elements++;
566 }
567
568 /* Create the new buffer object to replace the Package */
569
570 new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
571 if (!new_object) {
572 return (AE_NO_MEMORY);
573 }
574
575 /* Copy the package elements (integers) to the buffer as DWORDs */
576
577 elements = original_object->package.elements;
578 dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
579
580 for (i = 0; i < count; i++) {
581 *dword_buffer = (u32) (*elements)->integer.value;
582 dword_buffer++;
583 elements++;
584 }
585 break;
586
Bob Moore47e11d52009-12-11 15:01:12 +0800587 default:
588 return (AE_AML_OPERAND_TYPE);
589 }
590
591 *return_object = new_object;
592 return (AE_OK);
593}
594
595/*******************************************************************************
596 *
Bob Moore091f4d72010-01-21 09:28:32 +0800597 * FUNCTION: acpi_ns_repair_null_element
598 *
Bob Mooreba494be2012-07-12 09:40:10 +0800599 * PARAMETERS: data - Pointer to validation data structure
Bob Moore091f4d72010-01-21 09:28:32 +0800600 * expected_btypes - Object types expected
601 * package_index - Index of object within parent package (if
602 * applicable - ACPI_NOT_PACKAGE_ELEMENT
603 * otherwise)
604 * return_object_ptr - Pointer to the object returned from the
605 * evaluation of a method or object
606 *
607 * RETURN: Status. AE_OK if repair was successful.
608 *
609 * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
610 *
611 ******************************************************************************/
612
613acpi_status
614acpi_ns_repair_null_element(struct acpi_predefined_data *data,
615 u32 expected_btypes,
616 u32 package_index,
617 union acpi_operand_object **return_object_ptr)
618{
619 union acpi_operand_object *return_object = *return_object_ptr;
620 union acpi_operand_object *new_object;
621
622 ACPI_FUNCTION_NAME(ns_repair_null_element);
623
624 /* No repair needed if return object is non-NULL */
625
626 if (return_object) {
627 return (AE_OK);
628 }
629
630 /*
631 * Attempt to repair a NULL element of a Package object. This applies to
632 * predefined names that return a fixed-length package and each element
633 * is required. It does not apply to variable-length packages where NULL
634 * elements are allowed, especially at the end of the package.
635 */
636 if (expected_btypes & ACPI_RTYPE_INTEGER) {
637
Bob Mooreba494be2012-07-12 09:40:10 +0800638 /* Need an integer - create a zero-value integer */
Bob Moore091f4d72010-01-21 09:28:32 +0800639
Bob Moore150dba32010-07-06 10:35:55 +0800640 new_object = acpi_ut_create_integer_object((u64)0);
Bob Moore091f4d72010-01-21 09:28:32 +0800641 } else if (expected_btypes & ACPI_RTYPE_STRING) {
642
Bob Mooreba494be2012-07-12 09:40:10 +0800643 /* Need a string - create a NULL string */
Bob Moore091f4d72010-01-21 09:28:32 +0800644
645 new_object = acpi_ut_create_string_object(0);
646 } else if (expected_btypes & ACPI_RTYPE_BUFFER) {
647
Bob Mooreba494be2012-07-12 09:40:10 +0800648 /* Need a buffer - create a zero-length buffer */
Bob Moore091f4d72010-01-21 09:28:32 +0800649
650 new_object = acpi_ut_create_buffer_object(0);
651 } else {
652 /* Error for all other expected types */
653
654 return (AE_AML_OPERAND_TYPE);
655 }
656
657 if (!new_object) {
658 return (AE_NO_MEMORY);
659 }
660
661 /* Set the reference count according to the parent Package object */
662
663 new_object->common.reference_count =
664 data->parent_package->common.reference_count;
665
666 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
667 "%s: Converted NULL package element to expected %s at index %u\n",
668 data->pathname,
669 acpi_ut_get_object_type_name(new_object),
670 package_index));
671
672 *return_object_ptr = new_object;
673 data->flags |= ACPI_OBJECT_REPAIRED;
674 return (AE_OK);
675}
676
677/******************************************************************************
678 *
679 * FUNCTION: acpi_ns_remove_null_elements
680 *
Bob Mooreba494be2012-07-12 09:40:10 +0800681 * PARAMETERS: data - Pointer to validation data structure
Bob Moore091f4d72010-01-21 09:28:32 +0800682 * package_type - An acpi_return_package_types value
683 * obj_desc - A Package object
684 *
685 * RETURN: None.
686 *
687 * DESCRIPTION: Remove all NULL package elements from packages that contain
688 * a variable number of sub-packages. For these types of
689 * packages, NULL elements can be safely removed.
690 *
691 *****************************************************************************/
692
693void
694acpi_ns_remove_null_elements(struct acpi_predefined_data *data,
695 u8 package_type,
696 union acpi_operand_object *obj_desc)
697{
698 union acpi_operand_object **source;
699 union acpi_operand_object **dest;
700 u32 count;
701 u32 new_count;
702 u32 i;
703
704 ACPI_FUNCTION_NAME(ns_remove_null_elements);
705
706 /*
Bob Moore945488b2011-04-13 13:15:58 +0800707 * We can safely remove all NULL elements from these package types:
708 * PTYPE1_VAR packages contain a variable number of simple data types.
709 * PTYPE2 packages contain a variable number of sub-packages.
Bob Moore091f4d72010-01-21 09:28:32 +0800710 */
711 switch (package_type) {
Bob Moore091f4d72010-01-21 09:28:32 +0800712 case ACPI_PTYPE1_VAR:
Bob Moore091f4d72010-01-21 09:28:32 +0800713 case ACPI_PTYPE2:
714 case ACPI_PTYPE2_COUNT:
715 case ACPI_PTYPE2_PKG_COUNT:
716 case ACPI_PTYPE2_FIXED:
717 case ACPI_PTYPE2_MIN:
718 case ACPI_PTYPE2_REV_FIXED:
Bob Moore7fce7a42011-11-16 14:59:17 +0800719 case ACPI_PTYPE2_FIX_VAR:
Bob Moore091f4d72010-01-21 09:28:32 +0800720 break;
721
722 default:
Bob Moore945488b2011-04-13 13:15:58 +0800723 case ACPI_PTYPE1_FIXED:
724 case ACPI_PTYPE1_OPTION:
Bob Moore091f4d72010-01-21 09:28:32 +0800725 return;
726 }
727
728 count = obj_desc->package.count;
729 new_count = count;
730
731 source = obj_desc->package.elements;
732 dest = source;
733
734 /* Examine all elements of the package object, remove nulls */
735
736 for (i = 0; i < count; i++) {
737 if (!*source) {
738 new_count--;
739 } else {
740 *dest = *source;
741 dest++;
742 }
743 source++;
744 }
745
746 /* Update parent package if any null elements were removed */
747
748 if (new_count < count) {
749 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
750 "%s: Found and removed %u NULL elements\n",
751 data->pathname, (count - new_count)));
752
753 /* NULL terminate list and update the package count */
754
755 *dest = NULL;
756 obj_desc->package.count = new_count;
757 }
758}
759
760/*******************************************************************************
761 *
Bob Moore6a99b1c2012-03-21 09:51:39 +0800762 * FUNCTION: acpi_ns_wrap_with_package
Bob Mooree5f69d62009-07-24 11:03:09 +0800763 *
Bob Mooreba494be2012-07-12 09:40:10 +0800764 * PARAMETERS: data - Pointer to validation data structure
Bob Moore6a99b1c2012-03-21 09:51:39 +0800765 * original_object - Pointer to the object to repair.
766 * obj_desc_ptr - The new package object is returned here
Bob Mooree5f69d62009-07-24 11:03:09 +0800767 *
768 * RETURN: Status, new object in *obj_desc_ptr
769 *
Bob Moore6a99b1c2012-03-21 09:51:39 +0800770 * DESCRIPTION: Repair a common problem with objects that are defined to
771 * return a variable-length Package of sub-objects. If there is
772 * only one sub-object, some BIOS code mistakenly simply declares
773 * the single object instead of a Package with one sub-object.
774 * This function attempts to repair this error by wrapping a
775 * Package object around the original object, creating the
776 * correct and expected Package with one sub-object.
Bob Mooree5f69d62009-07-24 11:03:09 +0800777 *
778 * Names that can be repaired in this manner include:
Bob Moore6a99b1c2012-03-21 09:51:39 +0800779 * _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS,
780 * _BCL, _DOD, _FIX, _Sx
Bob Mooree5f69d62009-07-24 11:03:09 +0800781 *
782 ******************************************************************************/
783
784acpi_status
Bob Moore6a99b1c2012-03-21 09:51:39 +0800785acpi_ns_wrap_with_package(struct acpi_predefined_data *data,
786 union acpi_operand_object *original_object,
787 union acpi_operand_object **obj_desc_ptr)
Bob Mooree5f69d62009-07-24 11:03:09 +0800788{
789 union acpi_operand_object *pkg_obj_desc;
790
Bob Moore6a99b1c2012-03-21 09:51:39 +0800791 ACPI_FUNCTION_NAME(ns_wrap_with_package);
Bob Moore3a581762009-12-11 15:23:22 +0800792
Bob Mooree5f69d62009-07-24 11:03:09 +0800793 /*
794 * Create the new outer package and populate it. The new package will
Bob Moore6a99b1c2012-03-21 09:51:39 +0800795 * have a single element, the lone sub-object.
Bob Mooree5f69d62009-07-24 11:03:09 +0800796 */
797 pkg_obj_desc = acpi_ut_create_package_object(1);
798 if (!pkg_obj_desc) {
799 return (AE_NO_MEMORY);
800 }
801
Bob Moore6a99b1c2012-03-21 09:51:39 +0800802 pkg_obj_desc->package.elements[0] = original_object;
803
804 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
805 "%s: Wrapped %s with expected Package object\n",
806 data->pathname,
807 acpi_ut_get_object_type_name(original_object)));
Bob Mooree5f69d62009-07-24 11:03:09 +0800808
809 /* Return the new object in the object pointer */
810
811 *obj_desc_ptr = pkg_obj_desc;
Bob Moore6a99b1c2012-03-21 09:51:39 +0800812 data->flags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED;
Bob Mooree5f69d62009-07-24 11:03:09 +0800813 return (AE_OK);
814}