rslist.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: rslist - Linked list utilities
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acresrc.h"
  10. #define _COMPONENT ACPI_RESOURCES
  11. ACPI_MODULE_NAME("rslist")
  12. /*******************************************************************************
  13. *
  14. * FUNCTION: acpi_rs_convert_aml_to_resources
  15. *
  16. * PARAMETERS: acpi_walk_aml_callback
  17. * resource_ptr - Pointer to the buffer that will
  18. * contain the output structures
  19. *
  20. * RETURN: Status
  21. *
  22. * DESCRIPTION: Convert an AML resource to an internal representation of the
  23. * resource that is aligned and easier to access.
  24. *
  25. ******************************************************************************/
  26. acpi_status
  27. acpi_rs_convert_aml_to_resources(u8 * aml,
  28. u32 length,
  29. u32 offset, u8 resource_index, void **context)
  30. {
  31. struct acpi_resource **resource_ptr =
  32. ACPI_CAST_INDIRECT_PTR(struct acpi_resource, context);
  33. struct acpi_resource *resource;
  34. union aml_resource *aml_resource;
  35. struct acpi_rsconvert_info *conversion_table;
  36. acpi_status status;
  37. ACPI_FUNCTION_TRACE(rs_convert_aml_to_resources);
  38. /*
  39. * Check that the input buffer and all subsequent pointers into it
  40. * are aligned on a native word boundary. Most important on IA64
  41. */
  42. resource = *resource_ptr;
  43. if (ACPI_IS_MISALIGNED(resource)) {
  44. ACPI_WARNING((AE_INFO,
  45. "Misaligned resource pointer %p", resource));
  46. }
  47. /* Get the appropriate conversion info table */
  48. aml_resource = ACPI_CAST_PTR(union aml_resource, aml);
  49. if (acpi_ut_get_resource_type(aml) == ACPI_RESOURCE_NAME_SERIAL_BUS) {
  50. if (aml_resource->common_serial_bus.type >
  51. AML_RESOURCE_MAX_SERIALBUSTYPE) {
  52. conversion_table = NULL;
  53. } else {
  54. /* This is an I2C, SPI, or UART serial_bus descriptor */
  55. conversion_table =
  56. acpi_gbl_convert_resource_serial_bus_dispatch
  57. [aml_resource->common_serial_bus.type];
  58. }
  59. } else {
  60. conversion_table =
  61. acpi_gbl_get_resource_dispatch[resource_index];
  62. }
  63. if (!conversion_table) {
  64. ACPI_ERROR((AE_INFO,
  65. "Invalid/unsupported resource descriptor: Type 0x%2.2X",
  66. resource_index));
  67. return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
  68. }
  69. /* Convert the AML byte stream resource to a local resource struct */
  70. status =
  71. acpi_rs_convert_aml_to_resource(resource, aml_resource,
  72. conversion_table);
  73. if (ACPI_FAILURE(status)) {
  74. ACPI_EXCEPTION((AE_INFO, status,
  75. "Could not convert AML resource (Type 0x%X)",
  76. *aml));
  77. return_ACPI_STATUS(status);
  78. }
  79. ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
  80. "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
  81. acpi_ut_get_resource_type(aml), length,
  82. resource->length));
  83. /* Point to the next structure in the output buffer */
  84. *resource_ptr = ACPI_NEXT_RESOURCE(resource);
  85. return_ACPI_STATUS(AE_OK);
  86. }
  87. /*******************************************************************************
  88. *
  89. * FUNCTION: acpi_rs_convert_resources_to_aml
  90. *
  91. * PARAMETERS: resource - Pointer to the resource linked list
  92. * aml_size_needed - Calculated size of the byte stream
  93. * needed from calling acpi_rs_get_aml_length()
  94. * The size of the output_buffer is
  95. * guaranteed to be >= aml_size_needed
  96. * output_buffer - Pointer to the buffer that will
  97. * contain the byte stream
  98. *
  99. * RETURN: Status
  100. *
  101. * DESCRIPTION: Takes the resource linked list and parses it, creating a
  102. * byte stream of resources in the caller's output buffer
  103. *
  104. ******************************************************************************/
  105. acpi_status
  106. acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
  107. acpi_size aml_size_needed, u8 * output_buffer)
  108. {
  109. u8 *aml = output_buffer;
  110. u8 *end_aml = output_buffer + aml_size_needed;
  111. struct acpi_rsconvert_info *conversion_table;
  112. acpi_status status;
  113. ACPI_FUNCTION_TRACE(rs_convert_resources_to_aml);
  114. /* Walk the resource descriptor list, convert each descriptor */
  115. while (aml < end_aml) {
  116. /* Validate the (internal) Resource Type */
  117. if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
  118. ACPI_ERROR((AE_INFO,
  119. "Invalid descriptor type (0x%X) in resource list",
  120. resource->type));
  121. return_ACPI_STATUS(AE_BAD_DATA);
  122. }
  123. /* Sanity check the length. It must not be zero, or we loop forever */
  124. if (!resource->length) {
  125. ACPI_ERROR((AE_INFO,
  126. "Invalid zero length descriptor in resource list\n"));
  127. return_ACPI_STATUS(AE_AML_BAD_RESOURCE_LENGTH);
  128. }
  129. /* Perform the conversion */
  130. if (resource->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  131. if (resource->data.common_serial_bus.type >
  132. AML_RESOURCE_MAX_SERIALBUSTYPE) {
  133. conversion_table = NULL;
  134. } else {
  135. /* This is an I2C, SPI, or UART serial_bus descriptor */
  136. conversion_table =
  137. acpi_gbl_convert_resource_serial_bus_dispatch
  138. [resource->data.common_serial_bus.type];
  139. }
  140. } else {
  141. conversion_table =
  142. acpi_gbl_set_resource_dispatch[resource->type];
  143. }
  144. if (!conversion_table) {
  145. ACPI_ERROR((AE_INFO,
  146. "Invalid/unsupported resource descriptor: Type 0x%2.2X",
  147. resource->type));
  148. return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
  149. }
  150. status = acpi_rs_convert_resource_to_aml(resource,
  151. ACPI_CAST_PTR(union
  152. aml_resource,
  153. aml),
  154. conversion_table);
  155. if (ACPI_FAILURE(status)) {
  156. ACPI_EXCEPTION((AE_INFO, status,
  157. "Could not convert resource (type 0x%X) to AML",
  158. resource->type));
  159. return_ACPI_STATUS(status);
  160. }
  161. /* Perform final sanity check on the new AML resource descriptor */
  162. status =
  163. acpi_ut_validate_resource(NULL,
  164. ACPI_CAST_PTR(union aml_resource,
  165. aml), NULL);
  166. if (ACPI_FAILURE(status)) {
  167. return_ACPI_STATUS(status);
  168. }
  169. /* Check for end-of-list, normal exit */
  170. if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
  171. /* An End Tag indicates the end of the input Resource Template */
  172. return_ACPI_STATUS(AE_OK);
  173. }
  174. /*
  175. * Extract the total length of the new descriptor and set the
  176. * Aml to point to the next (output) resource descriptor
  177. */
  178. aml += acpi_ut_get_descriptor_length(aml);
  179. /* Point to the next input resource descriptor */
  180. resource = ACPI_NEXT_RESOURCE(resource);
  181. }
  182. /* Completed buffer, but did not find an end_tag resource descriptor */
  183. return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
  184. }