dev-path-parser.c 5.9 KB

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