marshalling.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ACPI-WMI buffer marshalling.
  4. *
  5. * Copyright (C) 2025 Armin Wolf <W_Armin@gmx.de>
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/align.h>
  9. #include <linux/math.h>
  10. #include <linux/overflow.h>
  11. #include <linux/slab.h>
  12. #include <linux/unaligned.h>
  13. #include <linux/wmi.h>
  14. #include <kunit/visibility.h>
  15. #include "internal.h"
  16. static int wmi_adjust_buffer_length(size_t *length, const union acpi_object *obj)
  17. {
  18. size_t alignment, size;
  19. switch (obj->type) {
  20. case ACPI_TYPE_INTEGER:
  21. /*
  22. * Integers are threated as 32 bit even if the ACPI DSDT
  23. * declares 64 bit integer width.
  24. */
  25. alignment = 4;
  26. size = sizeof(u32);
  27. break;
  28. case ACPI_TYPE_STRING:
  29. /*
  30. * Strings begin with a single little-endian 16-bit field containing
  31. * the string length in bytes and are encoded as UTF-16LE with a terminating
  32. * nul character.
  33. */
  34. if (obj->string.length + 1 > U16_MAX / 2)
  35. return -EOVERFLOW;
  36. alignment = 2;
  37. size = struct_size_t(struct wmi_string, chars, obj->string.length + 1);
  38. break;
  39. case ACPI_TYPE_BUFFER:
  40. /*
  41. * Buffers are copied as-is.
  42. */
  43. alignment = 1;
  44. size = obj->buffer.length;
  45. break;
  46. default:
  47. return -EPROTO;
  48. }
  49. *length = size_add(ALIGN(*length, alignment), size);
  50. return 0;
  51. }
  52. static int wmi_obj_get_buffer_length(const union acpi_object *obj, size_t *length)
  53. {
  54. size_t total = 0;
  55. int ret;
  56. if (obj->type == ACPI_TYPE_PACKAGE) {
  57. for (int i = 0; i < obj->package.count; i++) {
  58. ret = wmi_adjust_buffer_length(&total, &obj->package.elements[i]);
  59. if (ret < 0)
  60. return ret;
  61. }
  62. } else {
  63. ret = wmi_adjust_buffer_length(&total, obj);
  64. if (ret < 0)
  65. return ret;
  66. }
  67. *length = total;
  68. return 0;
  69. }
  70. static int wmi_obj_transform_simple(const union acpi_object *obj, u8 *buffer, size_t *consumed)
  71. {
  72. struct wmi_string *string;
  73. size_t length;
  74. __le32 value;
  75. u8 *aligned;
  76. switch (obj->type) {
  77. case ACPI_TYPE_INTEGER:
  78. aligned = PTR_ALIGN(buffer, 4);
  79. length = sizeof(value);
  80. value = cpu_to_le32(obj->integer.value);
  81. memcpy(aligned, &value, length);
  82. break;
  83. case ACPI_TYPE_STRING:
  84. aligned = PTR_ALIGN(buffer, 2);
  85. string = (struct wmi_string *)aligned;
  86. length = struct_size(string, chars, obj->string.length + 1);
  87. /* We do not have to worry about unaligned accesses here as the WMI
  88. * string will already be aligned on a two-byte boundary.
  89. */
  90. string->length = cpu_to_le16((obj->string.length + 1) * 2);
  91. for (int i = 0; i < obj->string.length; i++)
  92. string->chars[i] = cpu_to_le16(obj->string.pointer[i]);
  93. /*
  94. * The Windows WMI-ACPI driver always emits a terminating nul character,
  95. * so we emulate this behavior here as well.
  96. */
  97. string->chars[obj->string.length] = '\0';
  98. break;
  99. case ACPI_TYPE_BUFFER:
  100. aligned = buffer;
  101. length = obj->buffer.length;
  102. memcpy(aligned, obj->buffer.pointer, length);
  103. break;
  104. default:
  105. return -EPROTO;
  106. }
  107. *consumed = (aligned - buffer) + length;
  108. return 0;
  109. }
  110. static int wmi_obj_transform(const union acpi_object *obj, u8 *buffer)
  111. {
  112. size_t consumed;
  113. int ret;
  114. if (obj->type == ACPI_TYPE_PACKAGE) {
  115. for (int i = 0; i < obj->package.count; i++) {
  116. ret = wmi_obj_transform_simple(&obj->package.elements[i], buffer,
  117. &consumed);
  118. if (ret < 0)
  119. return ret;
  120. buffer += consumed;
  121. }
  122. } else {
  123. ret = wmi_obj_transform_simple(obj, buffer, &consumed);
  124. if (ret < 0)
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. int wmi_unmarshal_acpi_object(const union acpi_object *obj, struct wmi_buffer *buffer)
  130. {
  131. size_t length, alloc_length;
  132. u8 *data;
  133. int ret;
  134. ret = wmi_obj_get_buffer_length(obj, &length);
  135. if (ret < 0)
  136. return ret;
  137. if (ARCH_KMALLOC_MINALIGN < 8) {
  138. /*
  139. * kmalloc() guarantees that the alignment of the resulting memory allocation is at
  140. * least the largest power-of-two divisor of the allocation size. The WMI buffer
  141. * data needs to be aligned on a 8 byte boundary to properly support 64-bit WMI
  142. * integers, so we have to round the allocation size to the next multiple of 8.
  143. */
  144. alloc_length = round_up(length, 8);
  145. } else {
  146. alloc_length = length;
  147. }
  148. data = kzalloc(alloc_length, GFP_KERNEL);
  149. if (!data)
  150. return -ENOMEM;
  151. ret = wmi_obj_transform(obj, data);
  152. if (ret < 0) {
  153. kfree(data);
  154. return ret;
  155. }
  156. buffer->length = length;
  157. buffer->data = data;
  158. return 0;
  159. }
  160. EXPORT_SYMBOL_IF_KUNIT(wmi_unmarshal_acpi_object);
  161. int wmi_marshal_string(const struct wmi_buffer *buffer, struct acpi_buffer *out)
  162. {
  163. const struct wmi_string *string;
  164. u16 length, value;
  165. size_t chars;
  166. char *str;
  167. if (buffer->length < sizeof(*string))
  168. return -ENODATA;
  169. string = buffer->data;
  170. length = get_unaligned_le16(&string->length);
  171. if (buffer->length < sizeof(*string) + length)
  172. return -ENODATA;
  173. /* Each character needs to be 16 bits long */
  174. if (length % 2)
  175. return -EINVAL;
  176. chars = length / 2;
  177. str = kmalloc(chars + 1, GFP_KERNEL);
  178. if (!str)
  179. return -ENOMEM;
  180. for (int i = 0; i < chars; i++) {
  181. value = get_unaligned_le16(&string->chars[i]);
  182. /* ACPI only accepts ASCII strings */
  183. if (value > 0x7F) {
  184. kfree(str);
  185. return -EINVAL;
  186. }
  187. str[i] = value & 0xFF;
  188. /*
  189. * ACPI strings should only contain a single nul character at the end.
  190. * Because of this we must not copy any padding from the WMI string.
  191. */
  192. if (!value) {
  193. /* ACPICA wants the length of the string without the nul character */
  194. out->length = i;
  195. out->pointer = str;
  196. return 0;
  197. }
  198. }
  199. str[chars] = '\0';
  200. out->length = chars;
  201. out->pointer = str;
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_IF_KUNIT(wmi_marshal_string);