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 
parse_acpi_path(const struct efi_dev_path * node,struct device * parent,struct device ** child)15db8952e7SArd Biesheuvel static long __init parse_acpi_path(const struct efi_dev_path *node,
1646cd4b75SLukas Wunner 				   struct device *parent, struct device **child)
1746cd4b75SLukas Wunner {
18edbd1bc4SAndy Shevchenko 	struct acpi_device *adev;
1946cd4b75SLukas Wunner 	struct device *phys_dev;
20*7fc90e86SAndy Shevchenko 	char hid[ACPI_ID_LEN];
21*7fc90e86SAndy Shevchenko 	u64 uid;
22*7fc90e86SAndy Shevchenko 	int ret;
2346cd4b75SLukas Wunner 
24db8952e7SArd Biesheuvel 	if (node->header.length != 12)
2546cd4b75SLukas Wunner 		return -EINVAL;
2646cd4b75SLukas Wunner 
27edbd1bc4SAndy Shevchenko 	sprintf(hid, "%c%c%c%04X",
2846cd4b75SLukas Wunner 		'A' + ((node->acpi.hid >> 10) & 0x1f) - 1,
2946cd4b75SLukas Wunner 		'A' + ((node->acpi.hid >>  5) & 0x1f) - 1,
3046cd4b75SLukas Wunner 		'A' + ((node->acpi.hid >>  0) & 0x1f) - 1,
3146cd4b75SLukas Wunner 			node->acpi.hid >> 16);
3246cd4b75SLukas Wunner 
33edbd1bc4SAndy Shevchenko 	for_each_acpi_dev_match(adev, hid, NULL, -1) {
34*7fc90e86SAndy Shevchenko 		ret = acpi_dev_uid_to_integer(adev, &uid);
35*7fc90e86SAndy Shevchenko 		if (ret == 0 && node->acpi.uid == uid)
36edbd1bc4SAndy Shevchenko 			break;
37*7fc90e86SAndy Shevchenko 		if (ret == -ENODATA && node->acpi.uid == 0)
38edbd1bc4SAndy Shevchenko 			break;
39edbd1bc4SAndy Shevchenko 	}
40edbd1bc4SAndy Shevchenko 	if (!adev)
4146cd4b75SLukas Wunner 		return -ENODEV;
4246cd4b75SLukas Wunner 
43edbd1bc4SAndy Shevchenko 	phys_dev = acpi_get_first_physical_node(adev);
4446cd4b75SLukas Wunner 	if (phys_dev) {
45edbd1bc4SAndy Shevchenko 		*child = get_device(phys_dev);
46edbd1bc4SAndy Shevchenko 		acpi_dev_put(adev);
47edbd1bc4SAndy Shevchenko 	} else
48edbd1bc4SAndy Shevchenko 		*child = &adev->dev;
4946cd4b75SLukas Wunner 
5046cd4b75SLukas Wunner 	return 0;
5146cd4b75SLukas Wunner }
5246cd4b75SLukas Wunner 
match_pci_dev(struct device * dev,void * data)5346cd4b75SLukas Wunner static int __init match_pci_dev(struct device *dev, void *data)
5446cd4b75SLukas Wunner {
5546cd4b75SLukas Wunner 	unsigned int devfn = *(unsigned int *)data;
5646cd4b75SLukas Wunner 
5746cd4b75SLukas Wunner 	return dev_is_pci(dev) && to_pci_dev(dev)->devfn == devfn;
5846cd4b75SLukas Wunner }
5946cd4b75SLukas Wunner 
parse_pci_path(const struct efi_dev_path * node,struct device * parent,struct device ** child)60db8952e7SArd Biesheuvel static long __init parse_pci_path(const struct efi_dev_path *node,
6146cd4b75SLukas Wunner 				  struct device *parent, struct device **child)
6246cd4b75SLukas Wunner {
6346cd4b75SLukas Wunner 	unsigned int devfn;
6446cd4b75SLukas Wunner 
65db8952e7SArd Biesheuvel 	if (node->header.length != 6)
6646cd4b75SLukas Wunner 		return -EINVAL;
6746cd4b75SLukas Wunner 	if (!parent)
6846cd4b75SLukas Wunner 		return -EINVAL;
6946cd4b75SLukas Wunner 
7046cd4b75SLukas Wunner 	devfn = PCI_DEVFN(node->pci.dev, node->pci.fn);
7146cd4b75SLukas Wunner 
7246cd4b75SLukas Wunner 	*child = device_find_child(parent, &devfn, match_pci_dev);
7346cd4b75SLukas Wunner 	if (!*child)
7446cd4b75SLukas Wunner 		return -ENODEV;
7546cd4b75SLukas Wunner 
7646cd4b75SLukas Wunner 	return 0;
7746cd4b75SLukas Wunner }
7846cd4b75SLukas Wunner 
7946cd4b75SLukas Wunner /*
8046cd4b75SLukas Wunner  * Insert parsers for further node types here.
8146cd4b75SLukas Wunner  *
8246cd4b75SLukas Wunner  * Each parser takes a pointer to the @node and to the @parent (will be NULL
8346cd4b75SLukas Wunner  * for the first device path node). If a device corresponding to @node was
8446cd4b75SLukas Wunner  * found below @parent, its reference count should be incremented and the
8546cd4b75SLukas Wunner  * device returned in @child.
8646cd4b75SLukas Wunner  *
8746cd4b75SLukas Wunner  * The return value should be 0 on success or a negative int on failure.
8846cd4b75SLukas Wunner  * The special return values 0x01 (EFI_DEV_END_INSTANCE) and 0xFF
8946cd4b75SLukas Wunner  * (EFI_DEV_END_ENTIRE) signal the end of the device path, only
9046cd4b75SLukas Wunner  * parse_end_path() is supposed to return this.
9146cd4b75SLukas Wunner  *
9246cd4b75SLukas Wunner  * Be sure to validate the node length and contents before commencing the
9346cd4b75SLukas Wunner  * search for a device.
9446cd4b75SLukas Wunner  */
9546cd4b75SLukas Wunner 
parse_end_path(const struct efi_dev_path * node,struct device * parent,struct device ** child)96db8952e7SArd Biesheuvel static long __init parse_end_path(const struct efi_dev_path *node,
9746cd4b75SLukas Wunner 				  struct device *parent, struct device **child)
9846cd4b75SLukas Wunner {
99db8952e7SArd Biesheuvel 	if (node->header.length != 4)
10046cd4b75SLukas Wunner 		return -EINVAL;
101db8952e7SArd Biesheuvel 	if (node->header.sub_type != EFI_DEV_END_INSTANCE &&
102db8952e7SArd Biesheuvel 	    node->header.sub_type != EFI_DEV_END_ENTIRE)
10346cd4b75SLukas Wunner 		return -EINVAL;
10446cd4b75SLukas Wunner 	if (!parent)
10546cd4b75SLukas Wunner 		return -ENODEV;
10646cd4b75SLukas Wunner 
10746cd4b75SLukas Wunner 	*child = get_device(parent);
108db8952e7SArd Biesheuvel 	return node->header.sub_type;
10946cd4b75SLukas Wunner }
11046cd4b75SLukas Wunner 
11146cd4b75SLukas Wunner /**
11246cd4b75SLukas Wunner  * efi_get_device_by_path - find device by EFI Device Path
11346cd4b75SLukas Wunner  * @node: EFI Device Path
11446cd4b75SLukas Wunner  * @len: maximum length of EFI Device Path in bytes
11546cd4b75SLukas Wunner  *
11646cd4b75SLukas Wunner  * Parse a series of EFI Device Path nodes at @node and find the corresponding
11746cd4b75SLukas Wunner  * device.  If the device was found, its reference count is incremented and a
11846cd4b75SLukas Wunner  * pointer to it is returned.  The caller needs to drop the reference with
11946cd4b75SLukas Wunner  * put_device() after use.  The @node pointer is updated to point to the
12046cd4b75SLukas Wunner  * location immediately after the "End of Hardware Device Path" node.
12146cd4b75SLukas Wunner  *
12246cd4b75SLukas Wunner  * If another Device Path instance follows, @len is decremented by the number
12346cd4b75SLukas Wunner  * of bytes consumed.  Otherwise @len is set to %0.
12446cd4b75SLukas Wunner  *
12546cd4b75SLukas Wunner  * If a Device Path node is malformed or its corresponding device is not found,
12646cd4b75SLukas Wunner  * @node is updated to point to this offending node and an ERR_PTR is returned.
12746cd4b75SLukas Wunner  *
12846cd4b75SLukas Wunner  * If @len is initially %0, the function returns %NULL.  Thus, to iterate over
12946cd4b75SLukas Wunner  * all instances in a path, the following idiom may be used:
13046cd4b75SLukas Wunner  *
13146cd4b75SLukas Wunner  *	while (!IS_ERR_OR_NULL(dev = efi_get_device_by_path(&node, &len))) {
13246cd4b75SLukas Wunner  *		// do something with dev
13346cd4b75SLukas Wunner  *		put_device(dev);
13446cd4b75SLukas Wunner  *	}
13546cd4b75SLukas Wunner  *	if (IS_ERR(dev))
13646cd4b75SLukas Wunner  *		// report error
13746cd4b75SLukas Wunner  *
13846cd4b75SLukas Wunner  * Devices can only be found if they're already instantiated. Most buses
13946cd4b75SLukas Wunner  * instantiate devices in the "subsys" initcall level, hence the earliest
14046cd4b75SLukas Wunner  * initcall level in which this function should be called is "fs".
14146cd4b75SLukas Wunner  *
14246cd4b75SLukas Wunner  * Returns the device on success or
14346cd4b75SLukas Wunner  *	%ERR_PTR(-ENODEV) if no device was found,
14446cd4b75SLukas Wunner  *	%ERR_PTR(-EINVAL) if a node is malformed or exceeds @len,
14546cd4b75SLukas Wunner  *	%ERR_PTR(-ENOTSUPP) if support for a node type is not yet implemented.
14646cd4b75SLukas Wunner  */
efi_get_device_by_path(const struct efi_dev_path ** node,size_t * len)147db8952e7SArd Biesheuvel struct device * __init efi_get_device_by_path(const struct efi_dev_path **node,
14846cd4b75SLukas Wunner 					      size_t *len)
14946cd4b75SLukas Wunner {
15046cd4b75SLukas Wunner 	struct device *parent = NULL, *child;
15146cd4b75SLukas Wunner 	long ret = 0;
15246cd4b75SLukas Wunner 
15346cd4b75SLukas Wunner 	if (!*len)
15446cd4b75SLukas Wunner 		return NULL;
15546cd4b75SLukas Wunner 
15646cd4b75SLukas Wunner 	while (!ret) {
157db8952e7SArd Biesheuvel 		if (*len < 4 || *len < (*node)->header.length)
15846cd4b75SLukas Wunner 			ret = -EINVAL;
159db8952e7SArd Biesheuvel 		else if ((*node)->header.type		== EFI_DEV_ACPI &&
160db8952e7SArd Biesheuvel 			 (*node)->header.sub_type	== EFI_DEV_BASIC_ACPI)
16146cd4b75SLukas Wunner 			ret = parse_acpi_path(*node, parent, &child);
162db8952e7SArd Biesheuvel 		else if ((*node)->header.type		== EFI_DEV_HW &&
163db8952e7SArd Biesheuvel 			 (*node)->header.sub_type	== EFI_DEV_PCI)
16446cd4b75SLukas Wunner 			ret = parse_pci_path(*node, parent, &child);
165db8952e7SArd Biesheuvel 		else if (((*node)->header.type		== EFI_DEV_END_PATH ||
166db8952e7SArd Biesheuvel 			  (*node)->header.type		== EFI_DEV_END_PATH2))
16746cd4b75SLukas Wunner 			ret = parse_end_path(*node, parent, &child);
16846cd4b75SLukas Wunner 		else
16946cd4b75SLukas Wunner 			ret = -ENOTSUPP;
17046cd4b75SLukas Wunner 
17146cd4b75SLukas Wunner 		put_device(parent);
17246cd4b75SLukas Wunner 		if (ret < 0)
17346cd4b75SLukas Wunner 			return ERR_PTR(ret);
17446cd4b75SLukas Wunner 
17546cd4b75SLukas Wunner 		parent = child;
176db8952e7SArd Biesheuvel 		*node  = (void *)*node + (*node)->header.length;
177db8952e7SArd Biesheuvel 		*len  -= (*node)->header.length;
17846cd4b75SLukas Wunner 	}
17946cd4b75SLukas Wunner 
18046cd4b75SLukas Wunner 	if (ret == EFI_DEV_END_ENTIRE)
18146cd4b75SLukas Wunner 		*len = 0;
18246cd4b75SLukas Wunner 
18346cd4b75SLukas Wunner 	return child;
18446cd4b75SLukas Wunner }
185