dev-path-parser.c 5.7 KB

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