View Javadoc
1   /*
2    * Copyright 2020-2024 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.linux;
6   
7   import oshi.annotation.concurrent.ThreadSafe;
8   import oshi.util.FileUtil;
9   import oshi.util.ParseUtil;
10  import oshi.util.Util;
11  import oshi.util.platform.linux.SysPath;
12  
13  /**
14   * Utility to read info from {@code sysfs}
15   */
16  @ThreadSafe
17  public final class Sysfs {
18  
19      private Sysfs() {
20      }
21  
22      /**
23       * Query the vendor from sysfs
24       *
25       * @return The vendor if available, null otherwise
26       */
27      public static String querySystemVendor() {
28          final String sysVendor = FileUtil.getStringFromFile(SysPath.DMI_ID + "sys_vendor").trim();
29          if (!sysVendor.isEmpty()) {
30              return sysVendor;
31          }
32          return null;
33      }
34  
35      /**
36       * Query the model from sysfs
37       *
38       * @return The model if available, null otherwise
39       */
40      public static String queryProductModel() {
41          final String productName = FileUtil.getStringFromFile(SysPath.DMI_ID + "product_name").trim();
42          final String productVersion = FileUtil.getStringFromFile(SysPath.DMI_ID + "product_version").trim();
43          if (productName.isEmpty()) {
44              if (!productVersion.isEmpty()) {
45                  return productVersion;
46              }
47          } else {
48              if (!productVersion.isEmpty() && !"None".equals(productVersion)) {
49                  return productName + " (version: " + productVersion + ")";
50              }
51              return productName;
52          }
53          return null;
54      }
55  
56      /**
57       * Query the product serial number from sysfs
58       *
59       * @return The serial number if available, null otherwise
60       */
61      public static String queryProductSerial() {
62          // These sysfs files accessible by root, or can be chmod'd at boot time
63          // to enable access without root
64          String serial = FileUtil.getStringFromFile(SysPath.DMI_ID + "product_serial");
65          if (!serial.isEmpty() && !"None".equals(serial)) {
66              return serial;
67          }
68          return queryBoardSerial();
69      }
70  
71      /**
72       * Query the UUID from sysfs
73       *
74       * @return The UUID if available, null otherwise
75       */
76      public static String queryUUID() {
77          // These sysfs files accessible by root, or can be chmod'd at boot time
78          // to enable access without root
79          String uuid = FileUtil.getStringFromFile(SysPath.DMI_ID + "product_uuid");
80          if (!uuid.isEmpty() && !"None".equals(uuid)) {
81              return uuid;
82          }
83          return null;
84      }
85  
86      /**
87       * Query the board vendor from sysfs
88       *
89       * @return The board vendor if available, null otherwise
90       */
91      public static String queryBoardVendor() {
92          final String boardVendor = FileUtil.getStringFromFile(SysPath.DMI_ID + "board_vendor").trim();
93          if (!boardVendor.isEmpty()) {
94              return boardVendor;
95          }
96          return null;
97      }
98  
99      /**
100      * Query the board model from sysfs
101      *
102      * @return The board model if available, null otherwise
103      */
104     public static String queryBoardModel() {
105         final String boardName = FileUtil.getStringFromFile(SysPath.DMI_ID + "board_name").trim();
106         if (!boardName.isEmpty()) {
107             return boardName;
108         }
109         return null;
110     }
111 
112     /**
113      * Query the board version from sysfs
114      *
115      * @return The board version if available, null otherwise
116      */
117     public static String queryBoardVersion() {
118         final String boardVersion = FileUtil.getStringFromFile(SysPath.DMI_ID + "board_version").trim();
119         if (!boardVersion.isEmpty()) {
120             return boardVersion;
121         }
122         return null;
123     }
124 
125     /**
126      * Query the board serial number from sysfs
127      *
128      * @return The board serial number if available, null otherwise
129      */
130     public static String queryBoardSerial() {
131         final String boardSerial = FileUtil.getStringFromFile(SysPath.DMI_ID + "board_serial").trim();
132         if (!boardSerial.isEmpty()) {
133             return boardSerial;
134         }
135         return null;
136     }
137 
138     /**
139      * Query the bios vendor from sysfs
140      *
141      * @return The bios vendor if available, null otherwise
142      */
143     public static String queryBiosVendor() {
144         final String biosVendor = FileUtil.getStringFromFile(SysPath.DMI_ID + "bios_vendor").trim();
145         if (biosVendor.isEmpty()) {
146             return biosVendor;
147         }
148         return null;
149     }
150 
151     /**
152      * Query the bios description from sysfs
153      *
154      * @return The bios description if available, null otherwise
155      */
156     public static String queryBiosDescription() {
157         final String modalias = FileUtil.getStringFromFile(SysPath.DMI_ID + "modalias").trim();
158         if (!modalias.isEmpty()) {
159             return modalias;
160         }
161         return null;
162     }
163 
164     /**
165      * Query the bios version from sysfs
166      *
167      * @param biosRevision A revision string to append
168      * @return The bios version if available, null otherwise
169      */
170     public static String queryBiosVersion(String biosRevision) {
171         final String biosVersion = FileUtil.getStringFromFile(SysPath.DMI_ID + "bios_version").trim();
172         if (!biosVersion.isEmpty()) {
173             return biosVersion + (Util.isBlank(biosRevision) ? "" : " (revision " + biosRevision + ")");
174         }
175         return null;
176     }
177 
178     /**
179      * Query the bios release date from sysfs
180      *
181      * @return The bios release date if available, null otherwise
182      */
183     public static String queryBiosReleaseDate() {
184         final String biosDate = FileUtil.getStringFromFile(SysPath.DMI_ID + "bios_date").trim();
185         if (!biosDate.isEmpty()) {
186             return ParseUtil.parseMmDdYyyyToYyyyMmDD(biosDate);
187         }
188         return null;
189     }
190 }