dev-path-parser.c 5.3 KB

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