158c5475aSLukas Wunner /* 258c5475aSLukas Wunner * apple-properties.c - EFI device properties on Macs 358c5475aSLukas Wunner * Copyright (C) 2016 Lukas Wunner <lukas@wunner.de> 458c5475aSLukas Wunner * 558c5475aSLukas Wunner * This program is free software; you can redistribute it and/or modify 658c5475aSLukas Wunner * it under the terms of the GNU General Public License (version 2) as 758c5475aSLukas Wunner * published by the Free Software Foundation. 858c5475aSLukas Wunner * 958c5475aSLukas Wunner * This program is distributed in the hope that it will be useful, 1058c5475aSLukas Wunner * but WITHOUT ANY WARRANTY; without even the implied warranty of 1158c5475aSLukas Wunner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1258c5475aSLukas Wunner * GNU General Public License for more details. 1358c5475aSLukas Wunner * 1458c5475aSLukas Wunner * You should have received a copy of the GNU General Public License 1558c5475aSLukas Wunner * along with this program; if not, see <http://www.gnu.org/licenses/>. 1658c5475aSLukas Wunner */ 1758c5475aSLukas Wunner 1858c5475aSLukas Wunner #define pr_fmt(fmt) "apple-properties: " fmt 1958c5475aSLukas Wunner 2058c5475aSLukas Wunner #include <linux/bootmem.h> 2158c5475aSLukas Wunner #include <linux/efi.h> 22*630b3affSLukas Wunner #include <linux/platform_data/x86/apple.h> 2358c5475aSLukas Wunner #include <linux/property.h> 2458c5475aSLukas Wunner #include <linux/slab.h> 2558c5475aSLukas Wunner #include <linux/ucs2_string.h> 2658c5475aSLukas Wunner #include <asm/setup.h> 2758c5475aSLukas Wunner 2858c5475aSLukas Wunner static bool dump_properties __initdata; 2958c5475aSLukas Wunner 3058c5475aSLukas Wunner static int __init dump_properties_enable(char *arg) 3158c5475aSLukas Wunner { 3258c5475aSLukas Wunner dump_properties = true; 3358c5475aSLukas Wunner return 0; 3458c5475aSLukas Wunner } 3558c5475aSLukas Wunner 3658c5475aSLukas Wunner __setup("dump_apple_properties", dump_properties_enable); 3758c5475aSLukas Wunner 3858c5475aSLukas Wunner struct dev_header { 3958c5475aSLukas Wunner u32 len; 4058c5475aSLukas Wunner u32 prop_count; 4158c5475aSLukas Wunner struct efi_dev_path path[0]; 4258c5475aSLukas Wunner /* 4358c5475aSLukas Wunner * followed by key/value pairs, each key and value preceded by u32 len, 4458c5475aSLukas Wunner * len includes itself, value may be empty (in which case its len is 4) 4558c5475aSLukas Wunner */ 4658c5475aSLukas Wunner }; 4758c5475aSLukas Wunner 4858c5475aSLukas Wunner struct properties_header { 4958c5475aSLukas Wunner u32 len; 5058c5475aSLukas Wunner u32 version; 5158c5475aSLukas Wunner u32 dev_count; 5258c5475aSLukas Wunner struct dev_header dev_header[0]; 5358c5475aSLukas Wunner }; 5458c5475aSLukas Wunner 5558c5475aSLukas Wunner static u8 one __initdata = 1; 5658c5475aSLukas Wunner 5758c5475aSLukas Wunner static void __init unmarshal_key_value_pairs(struct dev_header *dev_header, 5858c5475aSLukas Wunner struct device *dev, void *ptr, 5958c5475aSLukas Wunner struct property_entry entry[]) 6058c5475aSLukas Wunner { 6158c5475aSLukas Wunner int i; 6258c5475aSLukas Wunner 6358c5475aSLukas Wunner for (i = 0; i < dev_header->prop_count; i++) { 6458c5475aSLukas Wunner int remaining = dev_header->len - (ptr - (void *)dev_header); 6558c5475aSLukas Wunner u32 key_len, val_len; 6658c5475aSLukas Wunner char *key; 6758c5475aSLukas Wunner 6858c5475aSLukas Wunner if (sizeof(key_len) > remaining) 6958c5475aSLukas Wunner break; 7058c5475aSLukas Wunner 7158c5475aSLukas Wunner key_len = *(typeof(key_len) *)ptr; 7258c5475aSLukas Wunner if (key_len + sizeof(val_len) > remaining || 7358c5475aSLukas Wunner key_len < sizeof(key_len) + sizeof(efi_char16_t) || 7458c5475aSLukas Wunner *(efi_char16_t *)(ptr + sizeof(key_len)) == 0) { 7558c5475aSLukas Wunner dev_err(dev, "invalid property name len at %#zx\n", 7658c5475aSLukas Wunner ptr - (void *)dev_header); 7758c5475aSLukas Wunner break; 7858c5475aSLukas Wunner } 7958c5475aSLukas Wunner 8058c5475aSLukas Wunner val_len = *(typeof(val_len) *)(ptr + key_len); 8158c5475aSLukas Wunner if (key_len + val_len > remaining || 8258c5475aSLukas Wunner val_len < sizeof(val_len)) { 8358c5475aSLukas Wunner dev_err(dev, "invalid property val len at %#zx\n", 8458c5475aSLukas Wunner ptr - (void *)dev_header + key_len); 8558c5475aSLukas Wunner break; 8658c5475aSLukas Wunner } 8758c5475aSLukas Wunner 8858c5475aSLukas Wunner /* 4 bytes to accommodate UTF-8 code points + null byte */ 8958c5475aSLukas Wunner key = kzalloc((key_len - sizeof(key_len)) * 4 + 1, GFP_KERNEL); 9058c5475aSLukas Wunner if (!key) { 9158c5475aSLukas Wunner dev_err(dev, "cannot allocate property name\n"); 9258c5475aSLukas Wunner break; 9358c5475aSLukas Wunner } 9458c5475aSLukas Wunner ucs2_as_utf8(key, ptr + sizeof(key_len), 9558c5475aSLukas Wunner key_len - sizeof(key_len)); 9658c5475aSLukas Wunner 9758c5475aSLukas Wunner entry[i].name = key; 9858c5475aSLukas Wunner entry[i].is_array = true; 9958c5475aSLukas Wunner entry[i].length = val_len - sizeof(val_len); 10058c5475aSLukas Wunner entry[i].pointer.raw_data = ptr + key_len + sizeof(val_len); 10158c5475aSLukas Wunner if (!entry[i].length) { 10258c5475aSLukas Wunner /* driver core doesn't accept empty properties */ 10358c5475aSLukas Wunner entry[i].length = 1; 10458c5475aSLukas Wunner entry[i].pointer.raw_data = &one; 10558c5475aSLukas Wunner } 10658c5475aSLukas Wunner 10758c5475aSLukas Wunner if (dump_properties) { 10858c5475aSLukas Wunner dev_info(dev, "property: %s\n", entry[i].name); 10958c5475aSLukas Wunner print_hex_dump(KERN_INFO, pr_fmt(), DUMP_PREFIX_OFFSET, 11058c5475aSLukas Wunner 16, 1, entry[i].pointer.raw_data, 11158c5475aSLukas Wunner entry[i].length, true); 11258c5475aSLukas Wunner } 11358c5475aSLukas Wunner 11458c5475aSLukas Wunner ptr += key_len + val_len; 11558c5475aSLukas Wunner } 11658c5475aSLukas Wunner 11758c5475aSLukas Wunner if (i != dev_header->prop_count) { 11858c5475aSLukas Wunner dev_err(dev, "got %d device properties, expected %u\n", i, 11958c5475aSLukas Wunner dev_header->prop_count); 12058c5475aSLukas Wunner print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET, 12158c5475aSLukas Wunner 16, 1, dev_header, dev_header->len, true); 12258c5475aSLukas Wunner return; 12358c5475aSLukas Wunner } 12458c5475aSLukas Wunner 12558c5475aSLukas Wunner dev_info(dev, "assigning %d device properties\n", i); 12658c5475aSLukas Wunner } 12758c5475aSLukas Wunner 12858c5475aSLukas Wunner static int __init unmarshal_devices(struct properties_header *properties) 12958c5475aSLukas Wunner { 13058c5475aSLukas Wunner size_t offset = offsetof(struct properties_header, dev_header[0]); 13158c5475aSLukas Wunner 13258c5475aSLukas Wunner while (offset + sizeof(struct dev_header) < properties->len) { 13358c5475aSLukas Wunner struct dev_header *dev_header = (void *)properties + offset; 13458c5475aSLukas Wunner struct property_entry *entry = NULL; 13558c5475aSLukas Wunner struct device *dev; 13658c5475aSLukas Wunner size_t len; 13758c5475aSLukas Wunner int ret, i; 13858c5475aSLukas Wunner void *ptr; 13958c5475aSLukas Wunner 14058c5475aSLukas Wunner if (offset + dev_header->len > properties->len || 14158c5475aSLukas Wunner dev_header->len <= sizeof(*dev_header)) { 14258c5475aSLukas Wunner pr_err("invalid len in dev_header at %#zx\n", offset); 14358c5475aSLukas Wunner return -EINVAL; 14458c5475aSLukas Wunner } 14558c5475aSLukas Wunner 14658c5475aSLukas Wunner ptr = dev_header->path; 14758c5475aSLukas Wunner len = dev_header->len - sizeof(*dev_header); 14858c5475aSLukas Wunner 14958c5475aSLukas Wunner dev = efi_get_device_by_path((struct efi_dev_path **)&ptr, &len); 15058c5475aSLukas Wunner if (IS_ERR(dev)) { 15158c5475aSLukas Wunner pr_err("device path parse error %ld at %#zx:\n", 15258c5475aSLukas Wunner PTR_ERR(dev), ptr - (void *)dev_header); 15358c5475aSLukas Wunner print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET, 15458c5475aSLukas Wunner 16, 1, dev_header, dev_header->len, true); 15558c5475aSLukas Wunner dev = NULL; 15658c5475aSLukas Wunner goto skip_device; 15758c5475aSLukas Wunner } 15858c5475aSLukas Wunner 15958c5475aSLukas Wunner entry = kcalloc(dev_header->prop_count + 1, sizeof(*entry), 16058c5475aSLukas Wunner GFP_KERNEL); 16158c5475aSLukas Wunner if (!entry) { 16258c5475aSLukas Wunner dev_err(dev, "cannot allocate properties\n"); 16358c5475aSLukas Wunner goto skip_device; 16458c5475aSLukas Wunner } 16558c5475aSLukas Wunner 16658c5475aSLukas Wunner unmarshal_key_value_pairs(dev_header, dev, ptr, entry); 16758c5475aSLukas Wunner if (!entry[0].name) 16858c5475aSLukas Wunner goto skip_device; 16958c5475aSLukas Wunner 17058c5475aSLukas Wunner ret = device_add_properties(dev, entry); /* makes deep copy */ 17158c5475aSLukas Wunner if (ret) 17258c5475aSLukas Wunner dev_err(dev, "error %d assigning properties\n", ret); 17358c5475aSLukas Wunner 17458c5475aSLukas Wunner for (i = 0; entry[i].name; i++) 17558c5475aSLukas Wunner kfree(entry[i].name); 17658c5475aSLukas Wunner 17758c5475aSLukas Wunner skip_device: 17858c5475aSLukas Wunner kfree(entry); 17958c5475aSLukas Wunner put_device(dev); 18058c5475aSLukas Wunner offset += dev_header->len; 18158c5475aSLukas Wunner } 18258c5475aSLukas Wunner 18358c5475aSLukas Wunner return 0; 18458c5475aSLukas Wunner } 18558c5475aSLukas Wunner 18658c5475aSLukas Wunner static int __init map_properties(void) 18758c5475aSLukas Wunner { 18858c5475aSLukas Wunner struct properties_header *properties; 18958c5475aSLukas Wunner struct setup_data *data; 19058c5475aSLukas Wunner u32 data_len; 19158c5475aSLukas Wunner u64 pa_data; 19258c5475aSLukas Wunner int ret; 19358c5475aSLukas Wunner 194*630b3affSLukas Wunner if (!x86_apple_machine) 19558c5475aSLukas Wunner return 0; 19658c5475aSLukas Wunner 19758c5475aSLukas Wunner pa_data = boot_params.hdr.setup_data; 19858c5475aSLukas Wunner while (pa_data) { 19958c5475aSLukas Wunner data = ioremap(pa_data, sizeof(*data)); 20058c5475aSLukas Wunner if (!data) { 20158c5475aSLukas Wunner pr_err("cannot map setup_data header\n"); 20258c5475aSLukas Wunner return -ENOMEM; 20358c5475aSLukas Wunner } 20458c5475aSLukas Wunner 20558c5475aSLukas Wunner if (data->type != SETUP_APPLE_PROPERTIES) { 20658c5475aSLukas Wunner pa_data = data->next; 20758c5475aSLukas Wunner iounmap(data); 20858c5475aSLukas Wunner continue; 20958c5475aSLukas Wunner } 21058c5475aSLukas Wunner 21158c5475aSLukas Wunner data_len = data->len; 21258c5475aSLukas Wunner iounmap(data); 21358c5475aSLukas Wunner 21458c5475aSLukas Wunner data = ioremap(pa_data, sizeof(*data) + data_len); 21558c5475aSLukas Wunner if (!data) { 21658c5475aSLukas Wunner pr_err("cannot map setup_data payload\n"); 21758c5475aSLukas Wunner return -ENOMEM; 21858c5475aSLukas Wunner } 21958c5475aSLukas Wunner 22058c5475aSLukas Wunner properties = (struct properties_header *)data->data; 22158c5475aSLukas Wunner if (properties->version != 1) { 22258c5475aSLukas Wunner pr_err("unsupported version:\n"); 22358c5475aSLukas Wunner print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET, 22458c5475aSLukas Wunner 16, 1, properties, data_len, true); 22558c5475aSLukas Wunner ret = -ENOTSUPP; 22658c5475aSLukas Wunner } else if (properties->len != data_len) { 22758c5475aSLukas Wunner pr_err("length mismatch, expected %u\n", data_len); 22858c5475aSLukas Wunner print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET, 22958c5475aSLukas Wunner 16, 1, properties, data_len, true); 23058c5475aSLukas Wunner ret = -EINVAL; 23158c5475aSLukas Wunner } else 23258c5475aSLukas Wunner ret = unmarshal_devices(properties); 23358c5475aSLukas Wunner 23458c5475aSLukas Wunner /* 23558c5475aSLukas Wunner * Can only free the setup_data payload but not its header 23658c5475aSLukas Wunner * to avoid breaking the chain of ->next pointers. 23758c5475aSLukas Wunner */ 23858c5475aSLukas Wunner data->len = 0; 23958c5475aSLukas Wunner iounmap(data); 24058c5475aSLukas Wunner free_bootmem_late(pa_data + sizeof(*data), data_len); 24158c5475aSLukas Wunner 24258c5475aSLukas Wunner return ret; 24358c5475aSLukas Wunner } 24458c5475aSLukas Wunner return 0; 24558c5475aSLukas Wunner } 24658c5475aSLukas Wunner 24758c5475aSLukas Wunner fs_initcall(map_properties); 248