exstorob.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /******************************************************************************
  2. *
  3. * Module Name: exstorob - AML Interpreter object store support, store to object
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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 <acpi/acinterp.h>
  44. #define _COMPONENT ACPI_EXECUTER
  45. ACPI_MODULE_NAME("exstorob")
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_ex_store_buffer_to_buffer
  49. *
  50. * PARAMETERS: source_desc - Source object to copy
  51. * target_desc - Destination object of the copy
  52. *
  53. * RETURN: Status
  54. *
  55. * DESCRIPTION: Copy a buffer object to another buffer object.
  56. *
  57. ******************************************************************************/
  58. acpi_status
  59. acpi_ex_store_buffer_to_buffer(union acpi_operand_object *source_desc,
  60. union acpi_operand_object *target_desc)
  61. {
  62. u32 length;
  63. u8 *buffer;
  64. ACPI_FUNCTION_TRACE_PTR("ex_store_buffer_to_buffer", source_desc);
  65. /* We know that source_desc is a buffer by now */
  66. buffer = (u8 *) source_desc->buffer.pointer;
  67. length = source_desc->buffer.length;
  68. /*
  69. * If target is a buffer of length zero or is a static buffer,
  70. * allocate a new buffer of the proper length
  71. */
  72. if ((target_desc->buffer.length == 0) ||
  73. (target_desc->common.flags & AOPOBJ_STATIC_POINTER)) {
  74. target_desc->buffer.pointer = ACPI_MEM_ALLOCATE(length);
  75. if (!target_desc->buffer.pointer) {
  76. return_ACPI_STATUS(AE_NO_MEMORY);
  77. }
  78. target_desc->buffer.length = length;
  79. }
  80. /* Copy source buffer to target buffer */
  81. if (length <= target_desc->buffer.length) {
  82. /* Clear existing buffer and copy in the new one */
  83. ACPI_MEMSET(target_desc->buffer.pointer, 0,
  84. target_desc->buffer.length);
  85. ACPI_MEMCPY(target_desc->buffer.pointer, buffer, length);
  86. #ifdef ACPI_OBSOLETE_BEHAVIOR
  87. /*
  88. * NOTE: ACPI versions up to 3.0 specified that the buffer must be
  89. * truncated if the string is smaller than the buffer. However, "other"
  90. * implementations of ACPI never did this and thus became the defacto
  91. * standard. ACPi 3.0_a changes this behavior such that the buffer
  92. * is no longer truncated.
  93. */
  94. /*
  95. * OBSOLETE BEHAVIOR:
  96. * If the original source was a string, we must truncate the buffer,
  97. * according to the ACPI spec. Integer-to-Buffer and Buffer-to-Buffer
  98. * copy must not truncate the original buffer.
  99. */
  100. if (original_src_type == ACPI_TYPE_STRING) {
  101. /* Set the new length of the target */
  102. target_desc->buffer.length = length;
  103. }
  104. #endif
  105. } else {
  106. /* Truncate the source, copy only what will fit */
  107. ACPI_MEMCPY(target_desc->buffer.pointer, buffer,
  108. target_desc->buffer.length);
  109. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  110. "Truncating source buffer from %X to %X\n",
  111. length, target_desc->buffer.length));
  112. }
  113. /* Copy flags */
  114. target_desc->buffer.flags = source_desc->buffer.flags;
  115. target_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
  116. return_ACPI_STATUS(AE_OK);
  117. }
  118. /*******************************************************************************
  119. *
  120. * FUNCTION: acpi_ex_store_string_to_string
  121. *
  122. * PARAMETERS: source_desc - Source object to copy
  123. * target_desc - Destination object of the copy
  124. *
  125. * RETURN: Status
  126. *
  127. * DESCRIPTION: Copy a String object to another String object
  128. *
  129. ******************************************************************************/
  130. acpi_status
  131. acpi_ex_store_string_to_string(union acpi_operand_object *source_desc,
  132. union acpi_operand_object *target_desc)
  133. {
  134. u32 length;
  135. u8 *buffer;
  136. ACPI_FUNCTION_TRACE_PTR("ex_store_string_to_string", source_desc);
  137. /* We know that source_desc is a string by now */
  138. buffer = (u8 *) source_desc->string.pointer;
  139. length = source_desc->string.length;
  140. /*
  141. * Replace existing string value if it will fit and the string
  142. * pointer is not a static pointer (part of an ACPI table)
  143. */
  144. if ((length < target_desc->string.length) &&
  145. (!(target_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
  146. /*
  147. * String will fit in existing non-static buffer.
  148. * Clear old string and copy in the new one
  149. */
  150. ACPI_MEMSET(target_desc->string.pointer, 0,
  151. (acpi_size) target_desc->string.length + 1);
  152. ACPI_MEMCPY(target_desc->string.pointer, buffer, length);
  153. } else {
  154. /*
  155. * Free the current buffer, then allocate a new buffer
  156. * large enough to hold the value
  157. */
  158. if (target_desc->string.pointer &&
  159. (!(target_desc->common.flags & AOPOBJ_STATIC_POINTER))) {
  160. /* Only free if not a pointer into the DSDT */
  161. ACPI_MEM_FREE(target_desc->string.pointer);
  162. }
  163. target_desc->string.pointer = ACPI_MEM_CALLOCATE((acpi_size)
  164. length + 1);
  165. if (!target_desc->string.pointer) {
  166. return_ACPI_STATUS(AE_NO_MEMORY);
  167. }
  168. target_desc->common.flags &= ~AOPOBJ_STATIC_POINTER;
  169. ACPI_MEMCPY(target_desc->string.pointer, buffer, length);
  170. }
  171. /* Set the new target length */
  172. target_desc->string.length = length;
  173. return_ACPI_STATUS(AE_OK);
  174. }