162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * dev-path-parser.c - EFI Device Path parser
462306a36Sopenharmony_ci * Copyright (C) 2016 Lukas Wunner <lukas@wunner.de>
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
762306a36Sopenharmony_ci * it under the terms of the GNU General Public License (version 2) as
862306a36Sopenharmony_ci * published by the Free Software Foundation.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/acpi.h>
1262306a36Sopenharmony_ci#include <linux/efi.h>
1362306a36Sopenharmony_ci#include <linux/pci.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistatic long __init parse_acpi_path(const struct efi_dev_path *node,
1662306a36Sopenharmony_ci				   struct device *parent, struct device **child)
1762306a36Sopenharmony_ci{
1862306a36Sopenharmony_ci	struct acpi_device *adev;
1962306a36Sopenharmony_ci	struct device *phys_dev;
2062306a36Sopenharmony_ci	char hid[ACPI_ID_LEN];
2162306a36Sopenharmony_ci	u64 uid;
2262306a36Sopenharmony_ci	int ret;
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci	if (node->header.length != 12)
2562306a36Sopenharmony_ci		return -EINVAL;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	sprintf(hid, "%c%c%c%04X",
2862306a36Sopenharmony_ci		'A' + ((node->acpi.hid >> 10) & 0x1f) - 1,
2962306a36Sopenharmony_ci		'A' + ((node->acpi.hid >>  5) & 0x1f) - 1,
3062306a36Sopenharmony_ci		'A' + ((node->acpi.hid >>  0) & 0x1f) - 1,
3162306a36Sopenharmony_ci			node->acpi.hid >> 16);
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci	for_each_acpi_dev_match(adev, hid, NULL, -1) {
3462306a36Sopenharmony_ci		ret = acpi_dev_uid_to_integer(adev, &uid);
3562306a36Sopenharmony_ci		if (ret == 0 && node->acpi.uid == uid)
3662306a36Sopenharmony_ci			break;
3762306a36Sopenharmony_ci		if (ret == -ENODATA && node->acpi.uid == 0)
3862306a36Sopenharmony_ci			break;
3962306a36Sopenharmony_ci	}
4062306a36Sopenharmony_ci	if (!adev)
4162306a36Sopenharmony_ci		return -ENODEV;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	phys_dev = acpi_get_first_physical_node(adev);
4462306a36Sopenharmony_ci	if (phys_dev) {
4562306a36Sopenharmony_ci		*child = get_device(phys_dev);
4662306a36Sopenharmony_ci		acpi_dev_put(adev);
4762306a36Sopenharmony_ci	} else
4862306a36Sopenharmony_ci		*child = &adev->dev;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	return 0;
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic int __init match_pci_dev(struct device *dev, void *data)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	unsigned int devfn = *(unsigned int *)data;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	return dev_is_pci(dev) && to_pci_dev(dev)->devfn == devfn;
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic long __init parse_pci_path(const struct efi_dev_path *node,
6162306a36Sopenharmony_ci				  struct device *parent, struct device **child)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	unsigned int devfn;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	if (node->header.length != 6)
6662306a36Sopenharmony_ci		return -EINVAL;
6762306a36Sopenharmony_ci	if (!parent)
6862306a36Sopenharmony_ci		return -EINVAL;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	devfn = PCI_DEVFN(node->pci.dev, node->pci.fn);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	*child = device_find_child(parent, &devfn, match_pci_dev);
7362306a36Sopenharmony_ci	if (!*child)
7462306a36Sopenharmony_ci		return -ENODEV;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	return 0;
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/*
8062306a36Sopenharmony_ci * Insert parsers for further node types here.
8162306a36Sopenharmony_ci *
8262306a36Sopenharmony_ci * Each parser takes a pointer to the @node and to the @parent (will be NULL
8362306a36Sopenharmony_ci * for the first device path node). If a device corresponding to @node was
8462306a36Sopenharmony_ci * found below @parent, its reference count should be incremented and the
8562306a36Sopenharmony_ci * device returned in @child.
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * The return value should be 0 on success or a negative int on failure.
8862306a36Sopenharmony_ci * The special return values 0x01 (EFI_DEV_END_INSTANCE) and 0xFF
8962306a36Sopenharmony_ci * (EFI_DEV_END_ENTIRE) signal the end of the device path, only
9062306a36Sopenharmony_ci * parse_end_path() is supposed to return this.
9162306a36Sopenharmony_ci *
9262306a36Sopenharmony_ci * Be sure to validate the node length and contents before commencing the
9362306a36Sopenharmony_ci * search for a device.
9462306a36Sopenharmony_ci */
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_cistatic long __init parse_end_path(const struct efi_dev_path *node,
9762306a36Sopenharmony_ci				  struct device *parent, struct device **child)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	if (node->header.length != 4)
10062306a36Sopenharmony_ci		return -EINVAL;
10162306a36Sopenharmony_ci	if (node->header.sub_type != EFI_DEV_END_INSTANCE &&
10262306a36Sopenharmony_ci	    node->header.sub_type != EFI_DEV_END_ENTIRE)
10362306a36Sopenharmony_ci		return -EINVAL;
10462306a36Sopenharmony_ci	if (!parent)
10562306a36Sopenharmony_ci		return -ENODEV;
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	*child = get_device(parent);
10862306a36Sopenharmony_ci	return node->header.sub_type;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/**
11262306a36Sopenharmony_ci * efi_get_device_by_path - find device by EFI Device Path
11362306a36Sopenharmony_ci * @node: EFI Device Path
11462306a36Sopenharmony_ci * @len: maximum length of EFI Device Path in bytes
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * Parse a series of EFI Device Path nodes at @node and find the corresponding
11762306a36Sopenharmony_ci * device.  If the device was found, its reference count is incremented and a
11862306a36Sopenharmony_ci * pointer to it is returned.  The caller needs to drop the reference with
11962306a36Sopenharmony_ci * put_device() after use.  The @node pointer is updated to point to the
12062306a36Sopenharmony_ci * location immediately after the "End of Hardware Device Path" node.
12162306a36Sopenharmony_ci *
12262306a36Sopenharmony_ci * If another Device Path instance follows, @len is decremented by the number
12362306a36Sopenharmony_ci * of bytes consumed.  Otherwise @len is set to %0.
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * If a Device Path node is malformed or its corresponding device is not found,
12662306a36Sopenharmony_ci * @node is updated to point to this offending node and an ERR_PTR is returned.
12762306a36Sopenharmony_ci *
12862306a36Sopenharmony_ci * If @len is initially %0, the function returns %NULL.  Thus, to iterate over
12962306a36Sopenharmony_ci * all instances in a path, the following idiom may be used:
13062306a36Sopenharmony_ci *
13162306a36Sopenharmony_ci *	while (!IS_ERR_OR_NULL(dev = efi_get_device_by_path(&node, &len))) {
13262306a36Sopenharmony_ci *		// do something with dev
13362306a36Sopenharmony_ci *		put_device(dev);
13462306a36Sopenharmony_ci *	}
13562306a36Sopenharmony_ci *	if (IS_ERR(dev))
13662306a36Sopenharmony_ci *		// report error
13762306a36Sopenharmony_ci *
13862306a36Sopenharmony_ci * Devices can only be found if they're already instantiated. Most buses
13962306a36Sopenharmony_ci * instantiate devices in the "subsys" initcall level, hence the earliest
14062306a36Sopenharmony_ci * initcall level in which this function should be called is "fs".
14162306a36Sopenharmony_ci *
14262306a36Sopenharmony_ci * Returns the device on success or
14362306a36Sopenharmony_ci *	%ERR_PTR(-ENODEV) if no device was found,
14462306a36Sopenharmony_ci *	%ERR_PTR(-EINVAL) if a node is malformed or exceeds @len,
14562306a36Sopenharmony_ci *	%ERR_PTR(-ENOTSUPP) if support for a node type is not yet implemented.
14662306a36Sopenharmony_ci */
14762306a36Sopenharmony_cistruct device * __init efi_get_device_by_path(const struct efi_dev_path **node,
14862306a36Sopenharmony_ci					      size_t *len)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	struct device *parent = NULL, *child;
15162306a36Sopenharmony_ci	long ret = 0;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	if (!*len)
15462306a36Sopenharmony_ci		return NULL;
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	while (!ret) {
15762306a36Sopenharmony_ci		if (*len < 4 || *len < (*node)->header.length)
15862306a36Sopenharmony_ci			ret = -EINVAL;
15962306a36Sopenharmony_ci		else if ((*node)->header.type		== EFI_DEV_ACPI &&
16062306a36Sopenharmony_ci			 (*node)->header.sub_type	== EFI_DEV_BASIC_ACPI)
16162306a36Sopenharmony_ci			ret = parse_acpi_path(*node, parent, &child);
16262306a36Sopenharmony_ci		else if ((*node)->header.type		== EFI_DEV_HW &&
16362306a36Sopenharmony_ci			 (*node)->header.sub_type	== EFI_DEV_PCI)
16462306a36Sopenharmony_ci			ret = parse_pci_path(*node, parent, &child);
16562306a36Sopenharmony_ci		else if (((*node)->header.type		== EFI_DEV_END_PATH ||
16662306a36Sopenharmony_ci			  (*node)->header.type		== EFI_DEV_END_PATH2))
16762306a36Sopenharmony_ci			ret = parse_end_path(*node, parent, &child);
16862306a36Sopenharmony_ci		else
16962306a36Sopenharmony_ci			ret = -ENOTSUPP;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci		put_device(parent);
17262306a36Sopenharmony_ci		if (ret < 0)
17362306a36Sopenharmony_ci			return ERR_PTR(ret);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci		parent = child;
17662306a36Sopenharmony_ci		*node  = (void *)*node + (*node)->header.length;
17762306a36Sopenharmony_ci		*len  -= (*node)->header.length;
17862306a36Sopenharmony_ci	}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	if (ret == EFI_DEV_END_ENTIRE)
18162306a36Sopenharmony_ci		*len = 0;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	return child;
18462306a36Sopenharmony_ci}
185