exconcat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /******************************************************************************
  2. *
  3. * Module Name: exconcat - Concatenate-type AML operators
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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 "acinterp.h"
  45. #include "amlresrc.h"
  46. #define _COMPONENT ACPI_EXECUTER
  47. ACPI_MODULE_NAME("exconcat")
  48. /* Local Prototypes */
  49. static acpi_status
  50. acpi_ex_convert_to_object_type_string(union acpi_operand_object *obj_desc,
  51. union acpi_operand_object **result_desc);
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ex_do_concatenate
  55. *
  56. * PARAMETERS: operand0 - First source object
  57. * operand1 - Second source object
  58. * actual_return_desc - Where to place the return object
  59. * walk_state - Current walk state
  60. *
  61. * RETURN: Status
  62. *
  63. * DESCRIPTION: Concatenate two objects with the ACPI-defined conversion
  64. * rules as necessary.
  65. * NOTE:
  66. * Per the ACPI spec (up to 6.1), Concatenate only supports Integer,
  67. * String, and Buffer objects. However, we support all objects here
  68. * as an extension. This improves the usefulness of both Concatenate
  69. * and the Printf/Fprintf macros. The extension returns a string
  70. * describing the object type for the other objects.
  71. * 02/2016.
  72. *
  73. ******************************************************************************/
  74. acpi_status
  75. acpi_ex_do_concatenate(union acpi_operand_object *operand0,
  76. union acpi_operand_object *operand1,
  77. union acpi_operand_object **actual_return_desc,
  78. struct acpi_walk_state *walk_state)
  79. {
  80. union acpi_operand_object *local_operand0 = operand0;
  81. union acpi_operand_object *local_operand1 = operand1;
  82. union acpi_operand_object *temp_operand1 = NULL;
  83. union acpi_operand_object *return_desc;
  84. char *buffer;
  85. acpi_object_type operand0_type;
  86. acpi_object_type operand1_type;
  87. acpi_status status;
  88. ACPI_FUNCTION_TRACE(ex_do_concatenate);
  89. /* Operand 0 preprocessing */
  90. switch (operand0->common.type) {
  91. case ACPI_TYPE_INTEGER:
  92. case ACPI_TYPE_STRING:
  93. case ACPI_TYPE_BUFFER:
  94. operand0_type = operand0->common.type;
  95. break;
  96. default:
  97. /* For all other types, get the "object type" string */
  98. status =
  99. acpi_ex_convert_to_object_type_string(operand0,
  100. &local_operand0);
  101. if (ACPI_FAILURE(status)) {
  102. goto cleanup;
  103. }
  104. operand0_type = ACPI_TYPE_STRING;
  105. break;
  106. }
  107. /* Operand 1 preprocessing */
  108. switch (operand1->common.type) {
  109. case ACPI_TYPE_INTEGER:
  110. case ACPI_TYPE_STRING:
  111. case ACPI_TYPE_BUFFER:
  112. operand1_type = operand1->common.type;
  113. break;
  114. default:
  115. /* For all other types, get the "object type" string */
  116. status =
  117. acpi_ex_convert_to_object_type_string(operand1,
  118. &local_operand1);
  119. if (ACPI_FAILURE(status)) {
  120. goto cleanup;
  121. }
  122. operand1_type = ACPI_TYPE_STRING;
  123. break;
  124. }
  125. /*
  126. * Convert the second operand if necessary. The first operand (0)
  127. * determines the type of the second operand (1) (See the Data Types
  128. * section of the ACPI specification). Both object types are
  129. * guaranteed to be either Integer/String/Buffer by the operand
  130. * resolution mechanism.
  131. */
  132. switch (operand0_type) {
  133. case ACPI_TYPE_INTEGER:
  134. status =
  135. acpi_ex_convert_to_integer(local_operand1, &temp_operand1,
  136. ACPI_STRTOUL_BASE16);
  137. break;
  138. case ACPI_TYPE_BUFFER:
  139. status =
  140. acpi_ex_convert_to_buffer(local_operand1, &temp_operand1);
  141. break;
  142. case ACPI_TYPE_STRING:
  143. switch (operand1_type) {
  144. case ACPI_TYPE_INTEGER:
  145. case ACPI_TYPE_STRING:
  146. case ACPI_TYPE_BUFFER:
  147. /* Other types have already been converted to string */
  148. status =
  149. acpi_ex_convert_to_string(local_operand1,
  150. &temp_operand1,
  151. ACPI_IMPLICIT_CONVERT_HEX);
  152. break;
  153. default:
  154. status = AE_OK;
  155. break;
  156. }
  157. break;
  158. default:
  159. ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X",
  160. operand0->common.type));
  161. status = AE_AML_INTERNAL;
  162. }
  163. if (ACPI_FAILURE(status)) {
  164. goto cleanup;
  165. }
  166. /* Take care with any newly created operand objects */
  167. if ((local_operand1 != operand1) && (local_operand1 != temp_operand1)) {
  168. acpi_ut_remove_reference(local_operand1);
  169. }
  170. local_operand1 = temp_operand1;
  171. /*
  172. * Both operands are now known to be the same object type
  173. * (Both are Integer, String, or Buffer), and we can now perform
  174. * the concatenation.
  175. *
  176. * There are three cases to handle, as per the ACPI spec:
  177. *
  178. * 1) Two Integers concatenated to produce a new Buffer
  179. * 2) Two Strings concatenated to produce a new String
  180. * 3) Two Buffers concatenated to produce a new Buffer
  181. */
  182. switch (operand0_type) {
  183. case ACPI_TYPE_INTEGER:
  184. /* Result of two Integers is a Buffer */
  185. /* Need enough buffer space for two integers */
  186. return_desc = acpi_ut_create_buffer_object((acpi_size)
  187. ACPI_MUL_2
  188. (acpi_gbl_integer_byte_width));
  189. if (!return_desc) {
  190. status = AE_NO_MEMORY;
  191. goto cleanup;
  192. }
  193. buffer = (char *)return_desc->buffer.pointer;
  194. /* Copy the first integer, LSB first */
  195. memcpy(buffer, &operand0->integer.value,
  196. acpi_gbl_integer_byte_width);
  197. /* Copy the second integer (LSB first) after the first */
  198. memcpy(buffer + acpi_gbl_integer_byte_width,
  199. &local_operand1->integer.value,
  200. acpi_gbl_integer_byte_width);
  201. break;
  202. case ACPI_TYPE_STRING:
  203. /* Result of two Strings is a String */
  204. return_desc = acpi_ut_create_string_object(((acpi_size)
  205. local_operand0->
  206. string.length +
  207. local_operand1->
  208. string.length));
  209. if (!return_desc) {
  210. status = AE_NO_MEMORY;
  211. goto cleanup;
  212. }
  213. buffer = return_desc->string.pointer;
  214. /* Concatenate the strings */
  215. strcpy(buffer, local_operand0->string.pointer);
  216. strcat(buffer, local_operand1->string.pointer);
  217. break;
  218. case ACPI_TYPE_BUFFER:
  219. /* Result of two Buffers is a Buffer */
  220. return_desc = acpi_ut_create_buffer_object(((acpi_size)
  221. operand0->buffer.
  222. length +
  223. local_operand1->
  224. buffer.length));
  225. if (!return_desc) {
  226. status = AE_NO_MEMORY;
  227. goto cleanup;
  228. }
  229. buffer = (char *)return_desc->buffer.pointer;
  230. /* Concatenate the buffers */
  231. memcpy(buffer, operand0->buffer.pointer,
  232. operand0->buffer.length);
  233. memcpy(buffer + operand0->buffer.length,
  234. local_operand1->buffer.pointer,
  235. local_operand1->buffer.length);
  236. break;
  237. default:
  238. /* Invalid object type, should not happen here */
  239. ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X",
  240. operand0->common.type));
  241. status = AE_AML_INTERNAL;
  242. goto cleanup;
  243. }
  244. *actual_return_desc = return_desc;
  245. cleanup:
  246. if (local_operand0 != operand0) {
  247. acpi_ut_remove_reference(local_operand0);
  248. }
  249. if (local_operand1 != operand1) {
  250. acpi_ut_remove_reference(local_operand1);
  251. }
  252. return_ACPI_STATUS(status);
  253. }
  254. /*******************************************************************************
  255. *
  256. * FUNCTION: acpi_ex_convert_to_object_type_string
  257. *
  258. * PARAMETERS: obj_desc - Object to be converted
  259. * return_desc - Where to place the return object
  260. *
  261. * RETURN: Status
  262. *
  263. * DESCRIPTION: Convert an object of arbitrary type to a string object that
  264. * contains the namestring for the object. Used for the
  265. * concatenate operator.
  266. *
  267. ******************************************************************************/
  268. static acpi_status
  269. acpi_ex_convert_to_object_type_string(union acpi_operand_object *obj_desc,
  270. union acpi_operand_object **result_desc)
  271. {
  272. union acpi_operand_object *return_desc;
  273. const char *type_string;
  274. type_string = acpi_ut_get_type_name(obj_desc->common.type);
  275. return_desc = acpi_ut_create_string_object(((acpi_size)strlen(type_string) + 9)); /* 9 For "[ Object]" */
  276. if (!return_desc) {
  277. return (AE_NO_MEMORY);
  278. }
  279. strcpy(return_desc->string.pointer, "[");
  280. strcat(return_desc->string.pointer, type_string);
  281. strcat(return_desc->string.pointer, " Object]");
  282. *result_desc = return_desc;
  283. return (AE_OK);
  284. }
  285. /*******************************************************************************
  286. *
  287. * FUNCTION: acpi_ex_concat_template
  288. *
  289. * PARAMETERS: operand0 - First source object
  290. * operand1 - Second source object
  291. * actual_return_desc - Where to place the return object
  292. * walk_state - Current walk state
  293. *
  294. * RETURN: Status
  295. *
  296. * DESCRIPTION: Concatenate two resource templates
  297. *
  298. ******************************************************************************/
  299. acpi_status
  300. acpi_ex_concat_template(union acpi_operand_object *operand0,
  301. union acpi_operand_object *operand1,
  302. union acpi_operand_object **actual_return_desc,
  303. struct acpi_walk_state *walk_state)
  304. {
  305. acpi_status status;
  306. union acpi_operand_object *return_desc;
  307. u8 *new_buf;
  308. u8 *end_tag;
  309. acpi_size length0;
  310. acpi_size length1;
  311. acpi_size new_length;
  312. ACPI_FUNCTION_TRACE(ex_concat_template);
  313. /*
  314. * Find the end_tag descriptor in each resource template.
  315. * Note1: returned pointers point TO the end_tag, not past it.
  316. * Note2: zero-length buffers are allowed; treated like one end_tag
  317. */
  318. /* Get the length of the first resource template */
  319. status = acpi_ut_get_resource_end_tag(operand0, &end_tag);
  320. if (ACPI_FAILURE(status)) {
  321. return_ACPI_STATUS(status);
  322. }
  323. length0 = ACPI_PTR_DIFF(end_tag, operand0->buffer.pointer);
  324. /* Get the length of the second resource template */
  325. status = acpi_ut_get_resource_end_tag(operand1, &end_tag);
  326. if (ACPI_FAILURE(status)) {
  327. return_ACPI_STATUS(status);
  328. }
  329. length1 = ACPI_PTR_DIFF(end_tag, operand1->buffer.pointer);
  330. /* Combine both lengths, minimum size will be 2 for end_tag */
  331. new_length = length0 + length1 + sizeof(struct aml_resource_end_tag);
  332. /* Create a new buffer object for the result (with one end_tag) */
  333. return_desc = acpi_ut_create_buffer_object(new_length);
  334. if (!return_desc) {
  335. return_ACPI_STATUS(AE_NO_MEMORY);
  336. }
  337. /*
  338. * Copy the templates to the new buffer, 0 first, then 1 follows. One
  339. * end_tag descriptor is copied from Operand1.
  340. */
  341. new_buf = return_desc->buffer.pointer;
  342. memcpy(new_buf, operand0->buffer.pointer, length0);
  343. memcpy(new_buf + length0, operand1->buffer.pointer, length1);
  344. /* Insert end_tag and set the checksum to zero, means "ignore checksum" */
  345. new_buf[new_length - 1] = 0;
  346. new_buf[new_length - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
  347. /* Return the completed resource template */
  348. *actual_return_desc = return_desc;
  349. return_ACPI_STATUS(AE_OK);
  350. }