14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
258c5475aSLukas Wunner /*
358c5475aSLukas Wunner  * apple-properties.c - EFI device properties on Macs
458c5475aSLukas Wunner  * Copyright (C) 2016 Lukas Wunner <lukas@wunner.de>
558c5475aSLukas Wunner  *
6355845b7SLukas Wunner  * Properties are stored either as:
7355845b7SLukas Wunner  * u8 arrays which can be retrieved with device_property_read_u8_array() or
8355845b7SLukas Wunner  * booleans which can be queried with device_property_present().
958c5475aSLukas Wunner  */
1058c5475aSLukas Wunner 
1158c5475aSLukas Wunner #define pr_fmt(fmt) "apple-properties: " fmt
1258c5475aSLukas Wunner 
1357c8a661SMike Rapoport #include <linux/memblock.h>
1458c5475aSLukas Wunner #include <linux/efi.h>
1544612d7eSAndy Shevchenko #include <linux/io.h>
16630b3affSLukas Wunner #include <linux/platform_data/x86/apple.h>
1758c5475aSLukas Wunner #include <linux/property.h>
1858c5475aSLukas Wunner #include <linux/slab.h>
1958c5475aSLukas Wunner #include <linux/ucs2_string.h>
2058c5475aSLukas Wunner #include <asm/setup.h>
2158c5475aSLukas Wunner 
2258c5475aSLukas Wunner static bool dump_properties __initdata;
2358c5475aSLukas Wunner 
dump_properties_enable(char * arg)2458c5475aSLukas Wunner static int __init dump_properties_enable(char *arg)
2558c5475aSLukas Wunner {
2658c5475aSLukas Wunner 	dump_properties = true;
27*4b49ba22SRandy Dunlap 	return 1;
2858c5475aSLukas Wunner }
2958c5475aSLukas Wunner 
3058c5475aSLukas Wunner __setup("dump_apple_properties", dump_properties_enable);
3158c5475aSLukas Wunner 
3258c5475aSLukas Wunner struct dev_header {
3358c5475aSLukas Wunner 	u32 len;
3458c5475aSLukas Wunner 	u32 prop_count;
353b9274eaSGustavo A. R. Silva 	struct efi_dev_path path[];
3658c5475aSLukas Wunner 	/*
3758c5475aSLukas Wunner 	 * followed by key/value pairs, each key and value preceded by u32 len,
3858c5475aSLukas Wunner 	 * len includes itself, value may be empty (in which case its len is 4)
3958c5475aSLukas Wunner 	 */
4058c5475aSLukas Wunner };
4158c5475aSLukas Wunner 
4258c5475aSLukas Wunner struct properties_header {
4358c5475aSLukas Wunner 	u32 len;
4458c5475aSLukas Wunner 	u32 version;
4558c5475aSLukas Wunner 	u32 dev_count;
463b9274eaSGustavo A. R. Silva 	struct dev_header dev_header[];
4758c5475aSLukas Wunner };
4858c5475aSLukas Wunner 
unmarshal_key_value_pairs(struct dev_header * dev_header,struct device * dev,const void * ptr,struct property_entry entry[])4958c5475aSLukas Wunner static void __init unmarshal_key_value_pairs(struct dev_header *dev_header,
50db8952e7SArd Biesheuvel 					     struct device *dev, const void *ptr,
5158c5475aSLukas Wunner 					     struct property_entry entry[])
5258c5475aSLukas Wunner {
5358c5475aSLukas Wunner 	int i;
5458c5475aSLukas Wunner 
5558c5475aSLukas Wunner 	for (i = 0; i < dev_header->prop_count; i++) {
5658c5475aSLukas Wunner 		int remaining = dev_header->len - (ptr - (void *)dev_header);
574466bf82SDmitry Torokhov 		u32 key_len, val_len, entry_len;
584466bf82SDmitry Torokhov 		const u8 *entry_data;
5958c5475aSLukas Wunner 		char *key;
6058c5475aSLukas Wunner 
6158c5475aSLukas Wunner 		if (sizeof(key_len) > remaining)
6258c5475aSLukas Wunner 			break;
6358c5475aSLukas Wunner 
6458c5475aSLukas Wunner 		key_len = *(typeof(key_len) *)ptr;
6558c5475aSLukas Wunner 		if (key_len + sizeof(val_len) > remaining ||
6658c5475aSLukas Wunner 		    key_len < sizeof(key_len) + sizeof(efi_char16_t) ||
6758c5475aSLukas Wunner 		    *(efi_char16_t *)(ptr + sizeof(key_len)) == 0) {
6858c5475aSLukas Wunner 			dev_err(dev, "invalid property name len at %#zx\n",
6958c5475aSLukas Wunner 				ptr - (void *)dev_header);
7058c5475aSLukas Wunner 			break;
7158c5475aSLukas Wunner 		}
7258c5475aSLukas Wunner 
7358c5475aSLukas Wunner 		val_len = *(typeof(val_len) *)(ptr + key_len);
7458c5475aSLukas Wunner 		if (key_len + val_len > remaining ||
7558c5475aSLukas Wunner 		    val_len < sizeof(val_len)) {
7658c5475aSLukas Wunner 			dev_err(dev, "invalid property val len at %#zx\n",
7758c5475aSLukas Wunner 				ptr - (void *)dev_header + key_len);
7858c5475aSLukas Wunner 			break;
7958c5475aSLukas Wunner 		}
8058c5475aSLukas Wunner 
8158c5475aSLukas Wunner 		/* 4 bytes to accommodate UTF-8 code points + null byte */
8258c5475aSLukas Wunner 		key = kzalloc((key_len - sizeof(key_len)) * 4 + 1, GFP_KERNEL);
8358c5475aSLukas Wunner 		if (!key) {
8458c5475aSLukas Wunner 			dev_err(dev, "cannot allocate property name\n");
8558c5475aSLukas Wunner 			break;
8658c5475aSLukas Wunner 		}
8758c5475aSLukas Wunner 		ucs2_as_utf8(key, ptr + sizeof(key_len),
8858c5475aSLukas Wunner 			     key_len - sizeof(key_len));
8958c5475aSLukas Wunner 
904466bf82SDmitry Torokhov 		entry_data = ptr + key_len + sizeof(val_len);
914466bf82SDmitry Torokhov 		entry_len = val_len - sizeof(val_len);
92355845b7SLukas Wunner 		if (entry_len)
934466bf82SDmitry Torokhov 			entry[i] = PROPERTY_ENTRY_U8_ARRAY_LEN(key, entry_data,
944466bf82SDmitry Torokhov 							       entry_len);
95355845b7SLukas Wunner 		else
96355845b7SLukas Wunner 			entry[i] = PROPERTY_ENTRY_BOOL(key);
97355845b7SLukas Wunner 
9858c5475aSLukas Wunner 		if (dump_properties) {
994466bf82SDmitry Torokhov 			dev_info(dev, "property: %s\n", key);
10058c5475aSLukas Wunner 			print_hex_dump(KERN_INFO, pr_fmt(), DUMP_PREFIX_OFFSET,
1014466bf82SDmitry Torokhov 				16, 1, entry_data, entry_len, true);
10258c5475aSLukas Wunner 		}
10358c5475aSLukas Wunner 
10458c5475aSLukas Wunner 		ptr += key_len + val_len;
10558c5475aSLukas Wunner 	}
10658c5475aSLukas Wunner 
10758c5475aSLukas Wunner 	if (i != dev_header->prop_count) {
10858c5475aSLukas Wunner 		dev_err(dev, "got %d device properties, expected %u\n", i,
10958c5475aSLukas Wunner 			dev_header->prop_count);
11058c5475aSLukas Wunner 		print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET,
11158c5475aSLukas Wunner 			16, 1, dev_header, dev_header->len, true);
11258c5475aSLukas Wunner 		return;
11358c5475aSLukas Wunner 	}
11458c5475aSLukas Wunner 
11558c5475aSLukas Wunner 	dev_info(dev, "assigning %d device properties\n", i);
11658c5475aSLukas Wunner }
11758c5475aSLukas Wunner 
unmarshal_devices(struct properties_header * properties)11858c5475aSLukas Wunner static int __init unmarshal_devices(struct properties_header *properties)
11958c5475aSLukas Wunner {
12058c5475aSLukas Wunner 	size_t offset = offsetof(struct properties_header, dev_header[0]);
12158c5475aSLukas Wunner 
12258c5475aSLukas Wunner 	while (offset + sizeof(struct dev_header) < properties->len) {
12358c5475aSLukas Wunner 		struct dev_header *dev_header = (void *)properties + offset;
12458c5475aSLukas Wunner 		struct property_entry *entry = NULL;
125db8952e7SArd Biesheuvel 		const struct efi_dev_path *ptr;
12658c5475aSLukas Wunner 		struct device *dev;
12758c5475aSLukas Wunner 		size_t len;
12858c5475aSLukas Wunner 		int ret, i;
12958c5475aSLukas Wunner 
13058c5475aSLukas Wunner 		if (offset + dev_header->len > properties->len ||
13158c5475aSLukas Wunner 		    dev_header->len <= sizeof(*dev_header)) {
13258c5475aSLukas Wunner 			pr_err("invalid len in dev_header at %#zx\n", offset);
13358c5475aSLukas Wunner 			return -EINVAL;
13458c5475aSLukas Wunner 		}
13558c5475aSLukas Wunner 
13658c5475aSLukas Wunner 		ptr = dev_header->path;
13758c5475aSLukas Wunner 		len = dev_header->len - sizeof(*dev_header);
13858c5475aSLukas Wunner 
139db8952e7SArd Biesheuvel 		dev = efi_get_device_by_path(&ptr, &len);
14058c5475aSLukas Wunner 		if (IS_ERR(dev)) {
14158c5475aSLukas Wunner 			pr_err("device path parse error %ld at %#zx:\n",
142db8952e7SArd Biesheuvel 			       PTR_ERR(dev), (void *)ptr - (void *)dev_header);
14358c5475aSLukas Wunner 			print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET,
14458c5475aSLukas Wunner 			       16, 1, dev_header, dev_header->len, true);
14558c5475aSLukas Wunner 			dev = NULL;
14658c5475aSLukas Wunner 			goto skip_device;
14758c5475aSLukas Wunner 		}
14858c5475aSLukas Wunner 
14958c5475aSLukas Wunner 		entry = kcalloc(dev_header->prop_count + 1, sizeof(*entry),
15058c5475aSLukas Wunner 				GFP_KERNEL);
15158c5475aSLukas Wunner 		if (!entry) {
15258c5475aSLukas Wunner 			dev_err(dev, "cannot allocate properties\n");
15358c5475aSLukas Wunner 			goto skip_device;
15458c5475aSLukas Wunner 		}
15558c5475aSLukas Wunner 
15658c5475aSLukas Wunner 		unmarshal_key_value_pairs(dev_header, dev, ptr, entry);
15758c5475aSLukas Wunner 		if (!entry[0].name)
15858c5475aSLukas Wunner 			goto skip_device;
15958c5475aSLukas Wunner 
16055fc610cSHeikki Krogerus 		ret = device_create_managed_software_node(dev, entry, NULL);
16158c5475aSLukas Wunner 		if (ret)
16258c5475aSLukas Wunner 			dev_err(dev, "error %d assigning properties\n", ret);
16358c5475aSLukas Wunner 
16458c5475aSLukas Wunner 		for (i = 0; entry[i].name; i++)
16558c5475aSLukas Wunner 			kfree(entry[i].name);
16658c5475aSLukas Wunner 
16758c5475aSLukas Wunner skip_device:
16858c5475aSLukas Wunner 		kfree(entry);
16958c5475aSLukas Wunner 		put_device(dev);
17058c5475aSLukas Wunner 		offset += dev_header->len;
17158c5475aSLukas Wunner 	}
17258c5475aSLukas Wunner 
17358c5475aSLukas Wunner 	return 0;
17458c5475aSLukas Wunner }
17558c5475aSLukas Wunner 
map_properties(void)17658c5475aSLukas Wunner static int __init map_properties(void)
17758c5475aSLukas Wunner {
17858c5475aSLukas Wunner 	struct properties_header *properties;
17958c5475aSLukas Wunner 	struct setup_data *data;
18058c5475aSLukas Wunner 	u32 data_len;
18158c5475aSLukas Wunner 	u64 pa_data;
18258c5475aSLukas Wunner 	int ret;
18358c5475aSLukas Wunner 
184630b3affSLukas Wunner 	if (!x86_apple_machine)
18558c5475aSLukas Wunner 		return 0;
18658c5475aSLukas Wunner 
18758c5475aSLukas Wunner 	pa_data = boot_params.hdr.setup_data;
18858c5475aSLukas Wunner 	while (pa_data) {
18944612d7eSAndy Shevchenko 		data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
19058c5475aSLukas Wunner 		if (!data) {
19158c5475aSLukas Wunner 			pr_err("cannot map setup_data header\n");
19258c5475aSLukas Wunner 			return -ENOMEM;
19358c5475aSLukas Wunner 		}
19458c5475aSLukas Wunner 
19558c5475aSLukas Wunner 		if (data->type != SETUP_APPLE_PROPERTIES) {
19658c5475aSLukas Wunner 			pa_data = data->next;
19744612d7eSAndy Shevchenko 			memunmap(data);
19858c5475aSLukas Wunner 			continue;
19958c5475aSLukas Wunner 		}
20058c5475aSLukas Wunner 
20158c5475aSLukas Wunner 		data_len = data->len;
20244612d7eSAndy Shevchenko 		memunmap(data);
20358c5475aSLukas Wunner 
20444612d7eSAndy Shevchenko 		data = memremap(pa_data, sizeof(*data) + data_len, MEMREMAP_WB);
20558c5475aSLukas Wunner 		if (!data) {
20658c5475aSLukas Wunner 			pr_err("cannot map setup_data payload\n");
20758c5475aSLukas Wunner 			return -ENOMEM;
20858c5475aSLukas Wunner 		}
20958c5475aSLukas Wunner 
21058c5475aSLukas Wunner 		properties = (struct properties_header *)data->data;
21158c5475aSLukas Wunner 		if (properties->version != 1) {
21258c5475aSLukas Wunner 			pr_err("unsupported version:\n");
21358c5475aSLukas Wunner 			print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET,
21458c5475aSLukas Wunner 			       16, 1, properties, data_len, true);
21558c5475aSLukas Wunner 			ret = -ENOTSUPP;
21658c5475aSLukas Wunner 		} else if (properties->len != data_len) {
21758c5475aSLukas Wunner 			pr_err("length mismatch, expected %u\n", data_len);
21858c5475aSLukas Wunner 			print_hex_dump(KERN_ERR, pr_fmt(), DUMP_PREFIX_OFFSET,
21958c5475aSLukas Wunner 			       16, 1, properties, data_len, true);
22058c5475aSLukas Wunner 			ret = -EINVAL;
22158c5475aSLukas Wunner 		} else
22258c5475aSLukas Wunner 			ret = unmarshal_devices(properties);
22358c5475aSLukas Wunner 
22458c5475aSLukas Wunner 		/*
22558c5475aSLukas Wunner 		 * Can only free the setup_data payload but not its header
22658c5475aSLukas Wunner 		 * to avoid breaking the chain of ->next pointers.
22758c5475aSLukas Wunner 		 */
22858c5475aSLukas Wunner 		data->len = 0;
22944612d7eSAndy Shevchenko 		memunmap(data);
23053ab85ebSMike Rapoport 		memblock_free_late(pa_data + sizeof(*data), data_len);
23158c5475aSLukas Wunner 
23258c5475aSLukas Wunner 		return ret;
23358c5475aSLukas Wunner 	}
23458c5475aSLukas Wunner 	return 0;
23558c5475aSLukas Wunner }
23658c5475aSLukas Wunner 
23758c5475aSLukas Wunner fs_initcall(map_properties);
238