apple-properties.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * apple-properties.c - EFI device properties on Macs
  3. * Copyright (C) 2016 Lukas Wunner <lukas@wunner.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License (version 2) as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Note, all properties are considered as u8 arrays.
  18. * To get a value of any of them the caller must use device_property_read_u8_array().
  19. */
  20. #define pr_fmt(fmt) "apple-properties: " fmt
  21. #include <linux/bootmem.h>
  22. #include <linux/efi.h>
  23. #include <linux/io.h>
  24. #include <linux/platform_data/x86/apple.h>
  25. #include <linux/property.h>
  26. #include <linux/slab.h>
  27. #include <linux/ucs2_string.h>
  28. #include <asm/setup.h>
  29. static bool dump_properties __initdata;
  30. static int __init dump_properties_enable(char *arg)
  31. {
  32. dump_properties = true;
  33. return 0;
  34. }
  35. __setup("dump_apple_properties", dump_properties_enable);
  36. struct dev_header {
  37. u32 len;
  38. u32 prop_count;
  39. struct efi_dev_path path[0];
  40. /*
  41. * followed by key/value pairs, each key and value preceded by u32 len,
  42. * len includes itself, value may be empty (in which case its len is 4)
  43. */
  44. };
  45. struct properties_header {
  46. u32 len;
  47. u32 version;
  48. u32 dev_count;
  49. struct dev_header dev_header[0];
  50. };
  51. static void __init unmarshal_key_value_pairs(struct dev_header *dev_header,
  52. struct device *dev, void *ptr,
  53. struct property_entry entry[])
  54. {
  55. int i;
  56. for (i = 0; i < dev_header->prop_count; i++) {
  57. int remaining = dev_header->len - (ptr - (void *)dev_header);
  58. u32 key_len, val_len;
  59. char *key;
  60. if (sizeof(key_len) > remaining)
  61. break;
  62. key_len = *(typeof(key_len) *)ptr;
  63. if (key_len + sizeof(val_len) > remaining ||
  64. key_len < sizeof(key_len) + sizeof(efi_char16_t) ||
  65. *(efi_char16_t *)(ptr + sizeof(key_len)) == 0) {
  66. dev_err(dev, "invalid property name len at %#zx\n",
  67. ptr - (void *)dev_header);
  68. break;
  69. }
  70. val_len = *(typeof(val_len) *)(ptr + key_len);
  71. if (key_len + val_len > remaining ||
  72. val_len < sizeof(val_len)) {
  73. dev_err(dev, "invalid property val len at %#zx\n",
  74. ptr - (void *)dev_header + key_len);
  75. break;
  76. }
  77. /* 4 bytes to accommodate UTF-8 code points + null byte */
  78. key = kzalloc((key_len - sizeof(key_len)) * 4 + 1, GFP_KERNEL);
  79. if (!key) {
  80. dev_err(dev, "cannot allocate property name\n");
  81. break;
  82. }
  83. ucs2_as_utf8(key, ptr + sizeof(key_len),
  84. key_len - sizeof(key_len));
  85. entry[i].name = key;
  86. entry[i].length = val_len - sizeof(val_len);
  87. entry[i].is_array = !!entry[i].length;
  88. entry[i].type = DEV_PROP_U8;
  89. entry[i].pointer.u8_data = ptr + key_len + sizeof(val_len);
  90. if (dump_properties) {
  91. dev_info(dev, "property: %s\n", entry[i].name);
  92. print_hex_dump(KERN_INFO, pr_fmt(), DUMP_PREFIX_OFFSET,
  93. 16, 1, entry[i].pointer.u8_data,
  94. entry[i].length, true);
  95. }
  96. ptr += key_len + val_len;
  97. }
  98. if (i != dev_header->prop_count) {
  99. dev_err(dev, "got %d device properties, expected %u\n", i,
  100. dev_header->prop_count);
  101. print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET,
  102. 16, 1, dev_header, dev_header->len, true);
  103. return;
  104. }
  105. dev_info(dev, "assigning %d device properties\n", i);
  106. }
  107. static int __init unmarshal_devices(struct properties_header *properties)
  108. {
  109. size_t offset = offsetof(struct properties_header, dev_header[0]);
  110. while (offset + sizeof(struct dev_header) < properties->len) {
  111. struct dev_header *dev_header = (void *)properties + offset;
  112. struct property_entry *entry = NULL;
  113. struct device *dev;
  114. size_t len;
  115. int ret, i;
  116. void *ptr;
  117. if (offset + dev_header->len > properties->len ||
  118. dev_header->len <= sizeof(*dev_header)) {
  119. pr_err("invalid len in dev_header at %#zx\n", offset);
  120. return -EINVAL;
  121. }
  122. ptr = dev_header->path;
  123. len = dev_header->len - sizeof(*dev_header);
  124. dev = efi_get_device_by_path((struct efi_dev_path **)&ptr, &len);
  125. if (IS_ERR(dev)) {
  126. pr_err("device path parse error %ld at %#zx:\n",
  127. PTR_ERR(dev), ptr - (void *)dev_header);
  128. print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET,
  129. 16, 1, dev_header, dev_header->len, true);
  130. dev = NULL;
  131. goto skip_device;
  132. }
  133. entry = kcalloc(dev_header->prop_count + 1, sizeof(*entry),
  134. GFP_KERNEL);
  135. if (!entry) {
  136. dev_err(dev, "cannot allocate properties\n");
  137. goto skip_device;
  138. }
  139. unmarshal_key_value_pairs(dev_header, dev, ptr, entry);
  140. if (!entry[0].name)
  141. goto skip_device;
  142. ret = device_add_properties(dev, entry); /* makes deep copy */
  143. if (ret)
  144. dev_err(dev, "error %d assigning properties\n", ret);
  145. for (i = 0; entry[i].name; i++)
  146. kfree(entry[i].name);
  147. skip_device:
  148. kfree(entry);
  149. put_device(dev);
  150. offset += dev_header->len;
  151. }
  152. return 0;
  153. }
  154. static int __init map_properties(void)
  155. {
  156. struct properties_header *properties;
  157. struct setup_data *data;
  158. u32 data_len;
  159. u64 pa_data;
  160. int ret;
  161. if (!x86_apple_machine)
  162. return 0;
  163. pa_data = boot_params.hdr.setup_data;
  164. while (pa_data) {
  165. data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
  166. if (!data) {
  167. pr_err("cannot map setup_data header\n");
  168. return -ENOMEM;
  169. }
  170. if (data->type != SETUP_APPLE_PROPERTIES) {
  171. pa_data = data->next;
  172. memunmap(data);
  173. continue;
  174. }
  175. data_len = data->len;
  176. memunmap(data);
  177. data = memremap(pa_data, sizeof(*data) + data_len, MEMREMAP_WB);
  178. if (!data) {
  179. pr_err("cannot map setup_data payload\n");
  180. return -ENOMEM;
  181. }
  182. properties = (struct properties_header *)data->data;
  183. if (properties->version != 1) {
  184. pr_err("unsupported version:\n");
  185. print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET,
  186. 16, 1, properties, data_len, true);
  187. ret = -ENOTSUPP;
  188. } else if (properties->len != data_len) {
  189. pr_err("length mismatch, expected %u\n", data_len);
  190. print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET,
  191. 16, 1, properties, data_len, true);
  192. ret = -EINVAL;
  193. } else
  194. ret = unmarshal_devices(properties);
  195. /*
  196. * Can only free the setup_data payload but not its header
  197. * to avoid breaking the chain of ->next pointers.
  198. */
  199. data->len = 0;
  200. memunmap(data);
  201. free_bootmem_late(pa_data + sizeof(*data), data_len);
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. fs_initcall(map_properties);