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 <[email protected]>
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;
207fc90e86SAndy Shevchenko char hid[ACPI_ID_LEN];
2146cd4b75SLukas Wunner
22db8952e7SArd Biesheuvel if (node->header.length != 12)
2346cd4b75SLukas Wunner return -EINVAL;
2446cd4b75SLukas Wunner
25edbd1bc4SAndy 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);
3046cd4b75SLukas Wunner
31edbd1bc4SAndy Shevchenko for_each_acpi_dev_match(adev, hid, NULL, -1) {
329e93507dSRaag Jadav if (acpi_dev_uid_match(adev, node->acpi.uid))
33edbd1bc4SAndy Shevchenko break;
349e93507dSRaag Jadav if (!acpi_device_uid(adev) && node->acpi.uid == 0)
35edbd1bc4SAndy Shevchenko break;
36edbd1bc4SAndy Shevchenko }
37edbd1bc4SAndy Shevchenko if (!adev)
3846cd4b75SLukas Wunner return -ENODEV;
3946cd4b75SLukas Wunner
40edbd1bc4SAndy Shevchenko phys_dev = acpi_get_first_physical_node(adev);
4146cd4b75SLukas Wunner if (phys_dev) {
42edbd1bc4SAndy Shevchenko *child = get_device(phys_dev);
43edbd1bc4SAndy Shevchenko acpi_dev_put(adev);
44edbd1bc4SAndy Shevchenko } else
45edbd1bc4SAndy Shevchenko *child = &adev->dev;
4646cd4b75SLukas Wunner
4746cd4b75SLukas Wunner return 0;
4846cd4b75SLukas Wunner }
4946cd4b75SLukas Wunner
match_pci_dev(struct device * dev,const void * data)50*f1e8bf56SZijun Hu static int __init match_pci_dev(struct device *dev, const void *data)
5146cd4b75SLukas Wunner {
52*f1e8bf56SZijun Hu unsigned int devfn = *(const unsigned int *)data;
5346cd4b75SLukas Wunner
5446cd4b75SLukas Wunner return dev_is_pci(dev) && to_pci_dev(dev)->devfn == devfn;
5546cd4b75SLukas Wunner }
5646cd4b75SLukas Wunner
parse_pci_path(const struct efi_dev_path * node,struct device * parent,struct device ** child)57db8952e7SArd Biesheuvel static long __init parse_pci_path(const struct efi_dev_path *node,
5846cd4b75SLukas Wunner struct device *parent, struct device **child)
5946cd4b75SLukas Wunner {
6046cd4b75SLukas Wunner unsigned int devfn;
6146cd4b75SLukas Wunner
62db8952e7SArd Biesheuvel if (node->header.length != 6)
6346cd4b75SLukas Wunner return -EINVAL;
6446cd4b75SLukas Wunner if (!parent)
6546cd4b75SLukas Wunner return -EINVAL;
6646cd4b75SLukas Wunner
6746cd4b75SLukas Wunner devfn = PCI_DEVFN(node->pci.dev, node->pci.fn);
6846cd4b75SLukas Wunner
6946cd4b75SLukas Wunner *child = device_find_child(parent, &devfn, match_pci_dev);
7046cd4b75SLukas Wunner if (!*child)
7146cd4b75SLukas Wunner return -ENODEV;
7246cd4b75SLukas Wunner
7346cd4b75SLukas Wunner return 0;
7446cd4b75SLukas Wunner }
7546cd4b75SLukas Wunner
7646cd4b75SLukas Wunner /*
7746cd4b75SLukas Wunner * Insert parsers for further node types here.
7846cd4b75SLukas Wunner *
7946cd4b75SLukas Wunner * Each parser takes a pointer to the @node and to the @parent (will be NULL
8046cd4b75SLukas Wunner * for the first device path node). If a device corresponding to @node was
8146cd4b75SLukas Wunner * found below @parent, its reference count should be incremented and the
8246cd4b75SLukas Wunner * device returned in @child.
8346cd4b75SLukas Wunner *
8446cd4b75SLukas Wunner * The return value should be 0 on success or a negative int on failure.
8546cd4b75SLukas Wunner * The special return values 0x01 (EFI_DEV_END_INSTANCE) and 0xFF
8646cd4b75SLukas Wunner * (EFI_DEV_END_ENTIRE) signal the end of the device path, only
8746cd4b75SLukas Wunner * parse_end_path() is supposed to return this.
8846cd4b75SLukas Wunner *
8946cd4b75SLukas Wunner * Be sure to validate the node length and contents before commencing the
9046cd4b75SLukas Wunner * search for a device.
9146cd4b75SLukas Wunner */
9246cd4b75SLukas Wunner
parse_end_path(const struct efi_dev_path * node,struct device * parent,struct device ** child)93db8952e7SArd Biesheuvel static long __init parse_end_path(const struct efi_dev_path *node,
9446cd4b75SLukas Wunner struct device *parent, struct device **child)
9546cd4b75SLukas Wunner {
96db8952e7SArd Biesheuvel if (node->header.length != 4)
9746cd4b75SLukas Wunner return -EINVAL;
98db8952e7SArd Biesheuvel if (node->header.sub_type != EFI_DEV_END_INSTANCE &&
99db8952e7SArd Biesheuvel node->header.sub_type != EFI_DEV_END_ENTIRE)
10046cd4b75SLukas Wunner return -EINVAL;
10146cd4b75SLukas Wunner if (!parent)
10246cd4b75SLukas Wunner return -ENODEV;
10346cd4b75SLukas Wunner
10446cd4b75SLukas Wunner *child = get_device(parent);
105db8952e7SArd Biesheuvel return node->header.sub_type;
10646cd4b75SLukas Wunner }
10746cd4b75SLukas Wunner
10846cd4b75SLukas Wunner /**
10946cd4b75SLukas Wunner * efi_get_device_by_path - find device by EFI Device Path
11046cd4b75SLukas Wunner * @node: EFI Device Path
11146cd4b75SLukas Wunner * @len: maximum length of EFI Device Path in bytes
11246cd4b75SLukas Wunner *
11346cd4b75SLukas Wunner * Parse a series of EFI Device Path nodes at @node and find the corresponding
11446cd4b75SLukas Wunner * device. If the device was found, its reference count is incremented and a
11546cd4b75SLukas Wunner * pointer to it is returned. The caller needs to drop the reference with
11646cd4b75SLukas Wunner * put_device() after use. The @node pointer is updated to point to the
11746cd4b75SLukas Wunner * location immediately after the "End of Hardware Device Path" node.
11846cd4b75SLukas Wunner *
11946cd4b75SLukas Wunner * If another Device Path instance follows, @len is decremented by the number
12046cd4b75SLukas Wunner * of bytes consumed. Otherwise @len is set to %0.
12146cd4b75SLukas Wunner *
12246cd4b75SLukas Wunner * If a Device Path node is malformed or its corresponding device is not found,
12346cd4b75SLukas Wunner * @node is updated to point to this offending node and an ERR_PTR is returned.
12446cd4b75SLukas Wunner *
12546cd4b75SLukas Wunner * If @len is initially %0, the function returns %NULL. Thus, to iterate over
12646cd4b75SLukas Wunner * all instances in a path, the following idiom may be used:
12746cd4b75SLukas Wunner *
12846cd4b75SLukas Wunner * while (!IS_ERR_OR_NULL(dev = efi_get_device_by_path(&node, &len))) {
12946cd4b75SLukas Wunner * // do something with dev
13046cd4b75SLukas Wunner * put_device(dev);
13146cd4b75SLukas Wunner * }
13246cd4b75SLukas Wunner * if (IS_ERR(dev))
13346cd4b75SLukas Wunner * // report error
13446cd4b75SLukas Wunner *
13546cd4b75SLukas Wunner * Devices can only be found if they're already instantiated. Most buses
13646cd4b75SLukas Wunner * instantiate devices in the "subsys" initcall level, hence the earliest
13746cd4b75SLukas Wunner * initcall level in which this function should be called is "fs".
13846cd4b75SLukas Wunner *
13946cd4b75SLukas Wunner * Returns the device on success or
14046cd4b75SLukas Wunner * %ERR_PTR(-ENODEV) if no device was found,
14146cd4b75SLukas Wunner * %ERR_PTR(-EINVAL) if a node is malformed or exceeds @len,
14246cd4b75SLukas Wunner * %ERR_PTR(-ENOTSUPP) if support for a node type is not yet implemented.
14346cd4b75SLukas Wunner */
efi_get_device_by_path(const struct efi_dev_path ** node,size_t * len)144db8952e7SArd Biesheuvel struct device * __init efi_get_device_by_path(const struct efi_dev_path **node,
14546cd4b75SLukas Wunner size_t *len)
14646cd4b75SLukas Wunner {
14746cd4b75SLukas Wunner struct device *parent = NULL, *child;
14846cd4b75SLukas Wunner long ret = 0;
14946cd4b75SLukas Wunner
15046cd4b75SLukas Wunner if (!*len)
15146cd4b75SLukas Wunner return NULL;
15246cd4b75SLukas Wunner
15346cd4b75SLukas Wunner while (!ret) {
154db8952e7SArd Biesheuvel if (*len < 4 || *len < (*node)->header.length)
15546cd4b75SLukas Wunner ret = -EINVAL;
156db8952e7SArd Biesheuvel else if ((*node)->header.type == EFI_DEV_ACPI &&
157db8952e7SArd Biesheuvel (*node)->header.sub_type == EFI_DEV_BASIC_ACPI)
15846cd4b75SLukas Wunner ret = parse_acpi_path(*node, parent, &child);
159db8952e7SArd Biesheuvel else if ((*node)->header.type == EFI_DEV_HW &&
160db8952e7SArd Biesheuvel (*node)->header.sub_type == EFI_DEV_PCI)
16146cd4b75SLukas Wunner ret = parse_pci_path(*node, parent, &child);
162db8952e7SArd Biesheuvel else if (((*node)->header.type == EFI_DEV_END_PATH ||
163db8952e7SArd Biesheuvel (*node)->header.type == EFI_DEV_END_PATH2))
16446cd4b75SLukas Wunner ret = parse_end_path(*node, parent, &child);
16546cd4b75SLukas Wunner else
16646cd4b75SLukas Wunner ret = -ENOTSUPP;
16746cd4b75SLukas Wunner
16846cd4b75SLukas Wunner put_device(parent);
16946cd4b75SLukas Wunner if (ret < 0)
17046cd4b75SLukas Wunner return ERR_PTR(ret);
17146cd4b75SLukas Wunner
17246cd4b75SLukas Wunner parent = child;
173db8952e7SArd Biesheuvel *node = (void *)*node + (*node)->header.length;
174db8952e7SArd Biesheuvel *len -= (*node)->header.length;
17546cd4b75SLukas Wunner }
17646cd4b75SLukas Wunner
17746cd4b75SLukas Wunner if (ret == EFI_DEV_END_ENTIRE)
17846cd4b75SLukas Wunner *len = 0;
17946cd4b75SLukas Wunner
18046cd4b75SLukas Wunner return child;
18146cd4b75SLukas Wunner }
182