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