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