Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * dev-path-parser.c - EFI Device Path parser
4 * Copyright (C) 2016 Lukas Wunner <lukas@wunner.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2) as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/acpi.h>
12#include <linux/efi.h>
13#include <linux/pci.h>
14
15static long __init parse_acpi_path(const struct efi_dev_path *node,
16 struct device *parent, struct device **child)
17{
18 char hid[ACPI_ID_LEN], uid[11]; /* UINT_MAX + null byte */
19 struct acpi_device *adev;
20 struct device *phys_dev;
21
22 if (node->header.length != 12)
23 return -EINVAL;
24
25 sprintf(hid, "%c%c%c%04X",
26 'A' + ((node->acpi.hid >> 10) & 0x1f) - 1,
27 'A' + ((node->acpi.hid >> 5) & 0x1f) - 1,
28 'A' + ((node->acpi.hid >> 0) & 0x1f) - 1,
29 node->acpi.hid >> 16);
30 sprintf(uid, "%u", node->acpi.uid);
31
32 for_each_acpi_dev_match(adev, hid, NULL, -1) {
33 if (adev->pnp.unique_id && !strcmp(adev->pnp.unique_id, uid))
34 break;
35 if (!adev->pnp.unique_id && node->acpi.uid == 0)
36 break;
37 acpi_dev_put(adev);
38 }
39 if (!adev)
40 return -ENODEV;
41
42 phys_dev = acpi_get_first_physical_node(adev);
43 if (phys_dev) {
44 *child = get_device(phys_dev);
45 acpi_dev_put(adev);
46 } else
47 *child = &adev->dev;
48
49 return 0;
50}
51
52static int __init match_pci_dev(struct device *dev, void *data)
53{
54 unsigned int devfn = *(unsigned int *)data;
55
56 return dev_is_pci(dev) && to_pci_dev(dev)->devfn == devfn;
57}
58
59static long __init parse_pci_path(const struct efi_dev_path *node,
60 struct device *parent, struct device **child)
61{
62 unsigned int devfn;
63
64 if (node->header.length != 6)
65 return -EINVAL;
66 if (!parent)
67 return -EINVAL;
68
69 devfn = PCI_DEVFN(node->pci.dev, node->pci.fn);
70
71 *child = device_find_child(parent, &devfn, match_pci_dev);
72 if (!*child)
73 return -ENODEV;
74
75 return 0;
76}
77
78/*
79 * Insert parsers for further node types here.
80 *
81 * Each parser takes a pointer to the @node and to the @parent (will be NULL
82 * for the first device path node). If a device corresponding to @node was
83 * found below @parent, its reference count should be incremented and the
84 * device returned in @child.
85 *
86 * The return value should be 0 on success or a negative int on failure.
87 * The special return values 0x01 (EFI_DEV_END_INSTANCE) and 0xFF
88 * (EFI_DEV_END_ENTIRE) signal the end of the device path, only
89 * parse_end_path() is supposed to return this.
90 *
91 * Be sure to validate the node length and contents before commencing the
92 * search for a device.
93 */
94
95static long __init parse_end_path(const struct efi_dev_path *node,
96 struct device *parent, struct device **child)
97{
98 if (node->header.length != 4)
99 return -EINVAL;
100 if (node->header.sub_type != EFI_DEV_END_INSTANCE &&
101 node->header.sub_type != EFI_DEV_END_ENTIRE)
102 return -EINVAL;
103 if (!parent)
104 return -ENODEV;
105
106 *child = get_device(parent);
107 return node->header.sub_type;
108}
109
110/**
111 * efi_get_device_by_path - find device by EFI Device Path
112 * @node: EFI Device Path
113 * @len: maximum length of EFI Device Path in bytes
114 *
115 * Parse a series of EFI Device Path nodes at @node and find the corresponding
116 * device. If the device was found, its reference count is incremented and a
117 * pointer to it is returned. The caller needs to drop the reference with
118 * put_device() after use. The @node pointer is updated to point to the
119 * location immediately after the "End of Hardware Device Path" node.
120 *
121 * If another Device Path instance follows, @len is decremented by the number
122 * of bytes consumed. Otherwise @len is set to %0.
123 *
124 * If a Device Path node is malformed or its corresponding device is not found,
125 * @node is updated to point to this offending node and an ERR_PTR is returned.
126 *
127 * If @len is initially %0, the function returns %NULL. Thus, to iterate over
128 * all instances in a path, the following idiom may be used:
129 *
130 * while (!IS_ERR_OR_NULL(dev = efi_get_device_by_path(&node, &len))) {
131 * // do something with dev
132 * put_device(dev);
133 * }
134 * if (IS_ERR(dev))
135 * // report error
136 *
137 * Devices can only be found if they're already instantiated. Most buses
138 * instantiate devices in the "subsys" initcall level, hence the earliest
139 * initcall level in which this function should be called is "fs".
140 *
141 * Returns the device on success or
142 * %ERR_PTR(-ENODEV) if no device was found,
143 * %ERR_PTR(-EINVAL) if a node is malformed or exceeds @len,
144 * %ERR_PTR(-ENOTSUPP) if support for a node type is not yet implemented.
145 */
146struct device * __init efi_get_device_by_path(const struct efi_dev_path **node,
147 size_t *len)
148{
149 struct device *parent = NULL, *child;
150 long ret = 0;
151
152 if (!*len)
153 return NULL;
154
155 while (!ret) {
156 if (*len < 4 || *len < (*node)->header.length)
157 ret = -EINVAL;
158 else if ((*node)->header.type == EFI_DEV_ACPI &&
159 (*node)->header.sub_type == EFI_DEV_BASIC_ACPI)
160 ret = parse_acpi_path(*node, parent, &child);
161 else if ((*node)->header.type == EFI_DEV_HW &&
162 (*node)->header.sub_type == EFI_DEV_PCI)
163 ret = parse_pci_path(*node, parent, &child);
164 else if (((*node)->header.type == EFI_DEV_END_PATH ||
165 (*node)->header.type == EFI_DEV_END_PATH2))
166 ret = parse_end_path(*node, parent, &child);
167 else
168 ret = -ENOTSUPP;
169
170 put_device(parent);
171 if (ret < 0)
172 return ERR_PTR(ret);
173
174 parent = child;
175 *node = (void *)*node + (*node)->header.length;
176 *len -= (*node)->header.length;
177 }
178
179 if (ret == EFI_DEV_END_ENTIRE)
180 *len = 0;
181
182 return child;
183}