14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
246cd4b75SLukas Wunner /*
346cd4b75SLukas Wunner  * dev-path-parser.c - EFI Device Path parser
446cd4b75SLukas Wunner  * Copyright (C) 2016 Lukas Wunner <lukas@wunner.de>
546cd4b75SLukas Wunner  *
646cd4b75SLukas Wunner  * This program is free software; you can redistribute it and/or modify
746cd4b75SLukas Wunner  * it under the terms of the GNU General Public License (version 2) as
846cd4b75SLukas Wunner  * published by the Free Software Foundation.
946cd4b75SLukas Wunner  */
1046cd4b75SLukas Wunner 
1146cd4b75SLukas Wunner #include <linux/acpi.h>
1246cd4b75SLukas Wunner #include <linux/efi.h>
1346cd4b75SLukas Wunner #include <linux/pci.h>
1446cd4b75SLukas Wunner 
15db8952e7SArd Biesheuvel static long __init parse_acpi_path(const struct efi_dev_path *node,
1646cd4b75SLukas Wunner 				   struct device *parent, struct device **child)
1746cd4b75SLukas Wunner {
18*edbd1bc4SAndy Shevchenko 	char hid[ACPI_ID_LEN], uid[11]; /* UINT_MAX + null byte */
19*edbd1bc4SAndy Shevchenko 	struct acpi_device *adev;
2046cd4b75SLukas Wunner 	struct device *phys_dev;
2146cd4b75SLukas Wunner 
22db8952e7SArd Biesheuvel 	if (node->header.length != 12)
2346cd4b75SLukas Wunner 		return -EINVAL;
2446cd4b75SLukas Wunner 
25*edbd1bc4SAndy Shevchenko 	sprintf(hid, "%c%c%c%04X",
2646cd4b75SLukas Wunner 		'A' + ((node->acpi.hid >> 10) & 0x1f) - 1,
2746cd4b75SLukas Wunner 		'A' + ((node->acpi.hid >>  5) & 0x1f) - 1,
2846cd4b75SLukas Wunner 		'A' + ((node->acpi.hid >>  0) & 0x1f) - 1,
2946cd4b75SLukas Wunner 			node->acpi.hid >> 16);
30*edbd1bc4SAndy Shevchenko 	sprintf(uid, "%u", node->acpi.uid);
3146cd4b75SLukas Wunner 
32*edbd1bc4SAndy Shevchenko 	for_each_acpi_dev_match(adev, hid, NULL, -1) {
33*edbd1bc4SAndy Shevchenko 		if (adev->pnp.unique_id && !strcmp(adev->pnp.unique_id, uid))
34*edbd1bc4SAndy Shevchenko 			break;
35*edbd1bc4SAndy Shevchenko 		if (!adev->pnp.unique_id && node->acpi.uid == 0)
36*edbd1bc4SAndy Shevchenko 			break;
37*edbd1bc4SAndy Shevchenko 		acpi_dev_put(adev);
38*edbd1bc4SAndy Shevchenko 	}
39*edbd1bc4SAndy Shevchenko 	if (!adev)
4046cd4b75SLukas Wunner 		return -ENODEV;
4146cd4b75SLukas Wunner 
42*edbd1bc4SAndy Shevchenko 	phys_dev = acpi_get_first_physical_node(adev);
4346cd4b75SLukas Wunner 	if (phys_dev) {
44*edbd1bc4SAndy Shevchenko 		*child = get_device(phys_dev);
45*edbd1bc4SAndy Shevchenko 		acpi_dev_put(adev);
46*edbd1bc4SAndy Shevchenko 	} else
47*edbd1bc4SAndy Shevchenko 		*child = &adev->dev;
4846cd4b75SLukas Wunner 
4946cd4b75SLukas Wunner 	return 0;
5046cd4b75SLukas Wunner }
5146cd4b75SLukas Wunner 
5246cd4b75SLukas Wunner static int __init match_pci_dev(struct device *dev, void *data)
5346cd4b75SLukas Wunner {
5446cd4b75SLukas Wunner 	unsigned int devfn = *(unsigned int *)data;
5546cd4b75SLukas Wunner 
5646cd4b75SLukas Wunner 	return dev_is_pci(dev) && to_pci_dev(dev)->devfn == devfn;
5746cd4b75SLukas Wunner }
5846cd4b75SLukas Wunner 
59db8952e7SArd Biesheuvel static long __init parse_pci_path(const struct efi_dev_path *node,
6046cd4b75SLukas Wunner 				  struct device *parent, struct device **child)
6146cd4b75SLukas Wunner {
6246cd4b75SLukas Wunner 	unsigned int devfn;
6346cd4b75SLukas Wunner 
64db8952e7SArd Biesheuvel 	if (node->header.length != 6)
6546cd4b75SLukas Wunner 		return -EINVAL;
6646cd4b75SLukas Wunner 	if (!parent)
6746cd4b75SLukas Wunner 		return -EINVAL;
6846cd4b75SLukas Wunner 
6946cd4b75SLukas Wunner 	devfn = PCI_DEVFN(node->pci.dev, node->pci.fn);
7046cd4b75SLukas Wunner 
7146cd4b75SLukas Wunner 	*child = device_find_child(parent, &devfn, match_pci_dev);
7246cd4b75SLukas Wunner 	if (!*child)
7346cd4b75SLukas Wunner 		return -ENODEV;
7446cd4b75SLukas Wunner 
7546cd4b75SLukas Wunner 	return 0;
7646cd4b75SLukas Wunner }
7746cd4b75SLukas Wunner 
7846cd4b75SLukas Wunner /*
7946cd4b75SLukas Wunner  * Insert parsers for further node types here.
8046cd4b75SLukas Wunner  *
8146cd4b75SLukas Wunner  * Each parser takes a pointer to the @node and to the @parent (will be NULL
8246cd4b75SLukas Wunner  * for the first device path node). If a device corresponding to @node was
8346cd4b75SLukas Wunner  * found below @parent, its reference count should be incremented and the
8446cd4b75SLukas Wunner  * device returned in @child.
8546cd4b75SLukas Wunner  *
8646cd4b75SLukas Wunner  * The return value should be 0 on success or a negative int on failure.
8746cd4b75SLukas Wunner  * The special return values 0x01 (EFI_DEV_END_INSTANCE) and 0xFF
8846cd4b75SLukas Wunner  * (EFI_DEV_END_ENTIRE) signal the end of the device path, only
8946cd4b75SLukas Wunner  * parse_end_path() is supposed to return this.
9046cd4b75SLukas Wunner  *
9146cd4b75SLukas Wunner  * Be sure to validate the node length and contents before commencing the
9246cd4b75SLukas Wunner  * search for a device.
9346cd4b75SLukas Wunner  */
9446cd4b75SLukas Wunner 
95db8952e7SArd Biesheuvel static long __init parse_end_path(const struct efi_dev_path *node,
9646cd4b75SLukas Wunner 				  struct device *parent, struct device **child)
9746cd4b75SLukas Wunner {
98db8952e7SArd Biesheuvel 	if (node->header.length != 4)
9946cd4b75SLukas Wunner 		return -EINVAL;
100db8952e7SArd Biesheuvel 	if (node->header.sub_type != EFI_DEV_END_INSTANCE &&
101db8952e7SArd Biesheuvel 	    node->header.sub_type != EFI_DEV_END_ENTIRE)
10246cd4b75SLukas Wunner 		return -EINVAL;
10346cd4b75SLukas Wunner 	if (!parent)
10446cd4b75SLukas Wunner 		return -ENODEV;
10546cd4b75SLukas Wunner 
10646cd4b75SLukas Wunner 	*child = get_device(parent);
107db8952e7SArd Biesheuvel 	return node->header.sub_type;
10846cd4b75SLukas Wunner }
10946cd4b75SLukas Wunner 
11046cd4b75SLukas Wunner /**
11146cd4b75SLukas Wunner  * efi_get_device_by_path - find device by EFI Device Path
11246cd4b75SLukas Wunner  * @node: EFI Device Path
11346cd4b75SLukas Wunner  * @len: maximum length of EFI Device Path in bytes
11446cd4b75SLukas Wunner  *
11546cd4b75SLukas Wunner  * Parse a series of EFI Device Path nodes at @node and find the corresponding
11646cd4b75SLukas Wunner  * device.  If the device was found, its reference count is incremented and a
11746cd4b75SLukas Wunner  * pointer to it is returned.  The caller needs to drop the reference with
11846cd4b75SLukas Wunner  * put_device() after use.  The @node pointer is updated to point to the
11946cd4b75SLukas Wunner  * location immediately after the "End of Hardware Device Path" node.
12046cd4b75SLukas Wunner  *
12146cd4b75SLukas Wunner  * If another Device Path instance follows, @len is decremented by the number
12246cd4b75SLukas Wunner  * of bytes consumed.  Otherwise @len is set to %0.
12346cd4b75SLukas Wunner  *
12446cd4b75SLukas Wunner  * If a Device Path node is malformed or its corresponding device is not found,
12546cd4b75SLukas Wunner  * @node is updated to point to this offending node and an ERR_PTR is returned.
12646cd4b75SLukas Wunner  *
12746cd4b75SLukas Wunner  * If @len is initially %0, the function returns %NULL.  Thus, to iterate over
12846cd4b75SLukas Wunner  * all instances in a path, the following idiom may be used:
12946cd4b75SLukas Wunner  *
13046cd4b75SLukas Wunner  *	while (!IS_ERR_OR_NULL(dev = efi_get_device_by_path(&node, &len))) {
13146cd4b75SLukas Wunner  *		// do something with dev
13246cd4b75SLukas Wunner  *		put_device(dev);
13346cd4b75SLukas Wunner  *	}
13446cd4b75SLukas Wunner  *	if (IS_ERR(dev))
13546cd4b75SLukas Wunner  *		// report error
13646cd4b75SLukas Wunner  *
13746cd4b75SLukas Wunner  * Devices can only be found if they're already instantiated. Most buses
13846cd4b75SLukas Wunner  * instantiate devices in the "subsys" initcall level, hence the earliest
13946cd4b75SLukas Wunner  * initcall level in which this function should be called is "fs".
14046cd4b75SLukas Wunner  *
14146cd4b75SLukas Wunner  * Returns the device on success or
14246cd4b75SLukas Wunner  *	%ERR_PTR(-ENODEV) if no device was found,
14346cd4b75SLukas Wunner  *	%ERR_PTR(-EINVAL) if a node is malformed or exceeds @len,
14446cd4b75SLukas Wunner  *	%ERR_PTR(-ENOTSUPP) if support for a node type is not yet implemented.
14546cd4b75SLukas Wunner  */
146db8952e7SArd Biesheuvel struct device * __init efi_get_device_by_path(const struct efi_dev_path **node,
14746cd4b75SLukas Wunner 					      size_t *len)
14846cd4b75SLukas Wunner {
14946cd4b75SLukas Wunner 	struct device *parent = NULL, *child;
15046cd4b75SLukas Wunner 	long ret = 0;
15146cd4b75SLukas Wunner 
15246cd4b75SLukas Wunner 	if (!*len)
15346cd4b75SLukas Wunner 		return NULL;
15446cd4b75SLukas Wunner 
15546cd4b75SLukas Wunner 	while (!ret) {
156db8952e7SArd Biesheuvel 		if (*len < 4 || *len < (*node)->header.length)
15746cd4b75SLukas Wunner 			ret = -EINVAL;
158db8952e7SArd Biesheuvel 		else if ((*node)->header.type		== EFI_DEV_ACPI &&
159db8952e7SArd Biesheuvel 			 (*node)->header.sub_type	== EFI_DEV_BASIC_ACPI)
16046cd4b75SLukas Wunner 			ret = parse_acpi_path(*node, parent, &child);
161db8952e7SArd Biesheuvel 		else if ((*node)->header.type		== EFI_DEV_HW &&
162db8952e7SArd Biesheuvel 			 (*node)->header.sub_type	== EFI_DEV_PCI)
16346cd4b75SLukas Wunner 			ret = parse_pci_path(*node, parent, &child);
164db8952e7SArd Biesheuvel 		else if (((*node)->header.type		== EFI_DEV_END_PATH ||
165db8952e7SArd Biesheuvel 			  (*node)->header.type		== EFI_DEV_END_PATH2))
16646cd4b75SLukas Wunner 			ret = parse_end_path(*node, parent, &child);
16746cd4b75SLukas Wunner 		else
16846cd4b75SLukas Wunner 			ret = -ENOTSUPP;
16946cd4b75SLukas Wunner 
17046cd4b75SLukas Wunner 		put_device(parent);
17146cd4b75SLukas Wunner 		if (ret < 0)
17246cd4b75SLukas Wunner 			return ERR_PTR(ret);
17346cd4b75SLukas Wunner 
17446cd4b75SLukas Wunner 		parent = child;
175db8952e7SArd Biesheuvel 		*node  = (void *)*node + (*node)->header.length;
176db8952e7SArd Biesheuvel 		*len  -= (*node)->header.length;
17746cd4b75SLukas Wunner 	}
17846cd4b75SLukas Wunner 
17946cd4b75SLukas Wunner 	if (ret == EFI_DEV_END_ENTIRE)
18046cd4b75SLukas Wunner 		*len = 0;
18146cd4b75SLukas Wunner 
18246cd4b75SLukas Wunner 	return child;
18346cd4b75SLukas Wunner }
184