nsrepair.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /******************************************************************************
  2. *
  3. * Module Name: nsrepair - Repair for objects returned by predefined methods
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2009, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #include "acpredef.h"
  46. #define _COMPONENT ACPI_NAMESPACE
  47. ACPI_MODULE_NAME("nsrepair")
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ns_repair_object
  51. *
  52. * PARAMETERS: Data - Pointer to validation data structure
  53. * expected_btypes - Object types expected
  54. * package_index - Index of object within parent package (if
  55. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  56. * otherwise)
  57. * return_object_ptr - Pointer to the object returned from the
  58. * evaluation of a method or object
  59. *
  60. * RETURN: Status. AE_OK if repair was successful.
  61. *
  62. * DESCRIPTION: Attempt to repair/convert a return object of a type that was
  63. * not expected.
  64. *
  65. ******************************************************************************/
  66. acpi_status
  67. acpi_ns_repair_object(struct acpi_predefined_data *data,
  68. u32 expected_btypes,
  69. u32 package_index,
  70. union acpi_operand_object **return_object_ptr)
  71. {
  72. union acpi_operand_object *return_object = *return_object_ptr;
  73. union acpi_operand_object *new_object;
  74. acpi_size length;
  75. switch (return_object->common.type) {
  76. case ACPI_TYPE_BUFFER:
  77. /* Does the method/object legally return a string? */
  78. if (!(expected_btypes & ACPI_RTYPE_STRING)) {
  79. return (AE_AML_OPERAND_TYPE);
  80. }
  81. /*
  82. * Have a Buffer, expected a String, convert. Use a to_string
  83. * conversion, no transform performed on the buffer data. The best
  84. * example of this is the _BIF method, where the string data from
  85. * the battery is often (incorrectly) returned as buffer object(s).
  86. */
  87. length = 0;
  88. while ((length < return_object->buffer.length) &&
  89. (return_object->buffer.pointer[length])) {
  90. length++;
  91. }
  92. /* Allocate a new string object */
  93. new_object = acpi_ut_create_string_object(length);
  94. if (!new_object) {
  95. return (AE_NO_MEMORY);
  96. }
  97. /*
  98. * Copy the raw buffer data with no transform. String is already NULL
  99. * terminated at Length+1.
  100. */
  101. ACPI_MEMCPY(new_object->string.pointer,
  102. return_object->buffer.pointer, length);
  103. /*
  104. * If the original object is a package element, we need to:
  105. * 1. Set the reference count of the new object to match the
  106. * reference count of the old object.
  107. * 2. Decrement the reference count of the original object.
  108. */
  109. if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
  110. new_object->common.reference_count =
  111. return_object->common.reference_count;
  112. if (return_object->common.reference_count > 1) {
  113. return_object->common.reference_count--;
  114. }
  115. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
  116. data->node_flags,
  117. "Converted Buffer to expected String at index %u",
  118. package_index));
  119. } else {
  120. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
  121. data->node_flags,
  122. "Converted Buffer to expected String"));
  123. }
  124. /* Delete old object, install the new return object */
  125. acpi_ut_remove_reference(return_object);
  126. *return_object_ptr = new_object;
  127. data->flags |= ACPI_OBJECT_REPAIRED;
  128. return (AE_OK);
  129. default:
  130. break;
  131. }
  132. return (AE_AML_OPERAND_TYPE);
  133. }