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