dev-path-parser.c 5.3 KB

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