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