You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.6 KiB

5 years ago
  1. # Copyright (c) 2014-2018 Adafruit Industries
  2. # Author: Tony DiCola, Limor Fried, Brennen Bearnes
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. # The above copyright notice and this permission notice shall be included in all
  10. # copies or substantial portions of the Software.
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. # SOFTWARE.
  18. """
  19. Attempt to detect the current platform.
  20. """
  21. import re
  22. from adafruit_platformdetect.board import Board
  23. from adafruit_platformdetect.chip import Chip
  24. # Various methods here may retain state in future, so tell pylint not to worry
  25. # that they don't use self right now:
  26. # pylint: disable=no-self-use
  27. class Detector:
  28. """Wrap various platform detection functions."""
  29. def __init__(self):
  30. self.board = Board(self)
  31. self.chip = Chip(self)
  32. def get_cpuinfo_field(self, field):
  33. """
  34. Search /proc/cpuinfo for a field and return its value, if found,
  35. otherwise None.
  36. """
  37. # Match a line like 'Hardware : BCM2709':
  38. pattern = r"^" + field + r"\s+:\s+(.*)$"
  39. with open("/proc/cpuinfo", "r") as infile:
  40. cpuinfo = infile.read().split("\n")
  41. for line in cpuinfo:
  42. match = re.search(pattern, line, flags=re.IGNORECASE)
  43. if match:
  44. return match.group(1)
  45. return None
  46. def check_dt_compatible_value(self, value):
  47. """
  48. Search /proc/device-tree/compatible for a value and return True, if found,
  49. otherwise False.
  50. """
  51. # Match a value like 'qcom,apq8016-sbc':
  52. dt_compatible = self.get_device_compatible()
  53. if dt_compatible and value in dt_compatible:
  54. return True
  55. return False
  56. def get_armbian_release_field(self, field):
  57. """
  58. Search /etc/armbian-release, if it exists, for a field and return its
  59. value, if found, otherwise None.
  60. """
  61. field_value = None
  62. pattern = r"^" + field + r"=(.*)"
  63. try:
  64. with open("/etc/armbian-release", "r") as release_file:
  65. armbian = release_file.read().split("\n")
  66. for line in armbian:
  67. match = re.search(pattern, line)
  68. if match:
  69. field_value = match.group(1)
  70. except FileNotFoundError:
  71. pass
  72. return field_value
  73. def get_device_model(self):
  74. """
  75. Search /proc/device-tree/model for the device model and return its value, if found,
  76. otherwise None.
  77. """
  78. try:
  79. with open("/proc/device-tree/model", "r") as model_file:
  80. return model_file.read()
  81. except FileNotFoundError:
  82. pass
  83. return None
  84. def get_device_compatible(self):
  85. """
  86. Search /proc/device-tree/compatible for the compatible chip name.
  87. """
  88. try:
  89. with open("/proc/device-tree/compatible", "r") as model_file:
  90. return model_file.read()
  91. except FileNotFoundError:
  92. pass
  93. return None
  94. def check_board_asset_tag_value(self):
  95. """
  96. Search /sys/devices/virtual/dmi/id for the device model and return its value, if found,
  97. otherwise None.
  98. """
  99. try:
  100. with open("/sys/devices/virtual/dmi/id/board_asset_tag", "r") as tag_file:
  101. return tag_file.read().strip()
  102. except FileNotFoundError:
  103. pass
  104. return None
  105. def check_board_name_value(self):
  106. """
  107. Search /sys/devices/virtual/dmi/id for the board name and return its value, if found,
  108. otherwise None. Debian/ubuntu based
  109. """
  110. try:
  111. with open("/sys/devices/virtual/dmi/id/board_name", "r") as board_name_file:
  112. return board_name_file.read().strip()
  113. except FileNotFoundError:
  114. pass
  115. return None