View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.linux;
6   
7   import static oshi.util.Memoizer.memoize;
8   
9   import java.time.format.DateTimeFormatter;
10  import java.time.format.DateTimeParseException;
11  import java.util.List;
12  import java.util.Locale;
13  import java.util.function.Supplier;
14  
15  import oshi.annotation.concurrent.Immutable;
16  import oshi.driver.linux.Dmidecode;
17  import oshi.driver.linux.Sysfs;
18  import oshi.hardware.common.AbstractFirmware;
19  import oshi.util.Constants;
20  import oshi.util.ExecutingCommand;
21  import oshi.util.ParseUtil;
22  import oshi.util.tuples.Pair;
23  
24  /**
25   * Firmware data obtained by sysfs.
26   */
27  @Immutable
28  final class LinuxFirmware extends AbstractFirmware {
29      // Jan 13 2013 16:24:29
30      private static final DateTimeFormatter VCGEN_FORMATTER = DateTimeFormatter.ofPattern("MMM d uuuu HH:mm:ss",
31              Locale.ENGLISH);
32  
33      private final Supplier<String> manufacturer = memoize(this::queryManufacturer);
34  
35      private final Supplier<String> description = memoize(this::queryDescription);
36  
37      private final Supplier<String> version = memoize(this::queryVersion);
38  
39      private final Supplier<String> releaseDate = memoize(this::queryReleaseDate);
40  
41      private final Supplier<String> name = memoize(this::queryName);
42  
43      private final Supplier<VcGenCmdStrings> vcGenCmd = memoize(LinuxFirmware::queryVcGenCmd);
44  
45      private final Supplier<Pair<String, String>> biosNameRev = memoize(Dmidecode::queryBiosNameRev);
46  
47      @Override
48      public String getManufacturer() {
49          return manufacturer.get();
50      }
51  
52      @Override
53      public String getDescription() {
54          return description.get();
55      }
56  
57      @Override
58      public String getVersion() {
59          return version.get();
60      }
61  
62      @Override
63      public String getReleaseDate() {
64          return releaseDate.get();
65      }
66  
67      @Override
68      public String getName() {
69          return name.get();
70      }
71  
72      private String queryManufacturer() {
73          String result = null;
74          if ((result = Sysfs.queryBiosVendor()) == null && (result = vcGenCmd.get().manufacturer) == null) {
75              return Constants.UNKNOWN;
76          }
77          return result;
78      }
79  
80      private String queryDescription() {
81          String result = null;
82          if ((result = Sysfs.queryBiosDescription()) == null && (result = vcGenCmd.get().description) == null) {
83              return Constants.UNKNOWN;
84          }
85          return result;
86      }
87  
88      private String queryVersion() {
89          String result = null;
90          if ((result = Sysfs.queryBiosVersion(this.biosNameRev.get().getB())) == null
91                  && (result = vcGenCmd.get().version) == null) {
92              return Constants.UNKNOWN;
93          }
94          return result;
95      }
96  
97      private String queryReleaseDate() {
98          String result = null;
99          if ((result = Sysfs.queryBiosReleaseDate()) == null && (result = vcGenCmd.get().releaseDate) == null) {
100             return Constants.UNKNOWN;
101         }
102         return result;
103     }
104 
105     private String queryName() {
106         String result = null;
107         if ((result = biosNameRev.get().getA()) == null && (result = vcGenCmd.get().name) == null) {
108             return Constants.UNKNOWN;
109         }
110         return result;
111     }
112 
113     private static VcGenCmdStrings queryVcGenCmd() {
114         String vcReleaseDate = null;
115         String vcManufacturer = null;
116         String vcVersion = null;
117 
118         List<String> vcgencmd = ExecutingCommand.runNative("vcgencmd version");
119         if (vcgencmd.size() >= 3) {
120             // First line is date
121             try {
122                 vcReleaseDate = DateTimeFormatter.ISO_LOCAL_DATE.format(VCGEN_FORMATTER.parse(vcgencmd.get(0)));
123             } catch (DateTimeParseException e) {
124                 vcReleaseDate = Constants.UNKNOWN;
125             }
126             // Second line is copyright
127             String[] copyright = ParseUtil.whitespaces.split(vcgencmd.get(1));
128             vcManufacturer = copyright[copyright.length - 1];
129             // Third line is version
130             vcVersion = vcgencmd.get(2).replace("version ", "");
131             return new VcGenCmdStrings(vcReleaseDate, vcManufacturer, vcVersion, "RPi", "Bootloader");
132         }
133         return new VcGenCmdStrings(null, null, null, null, null);
134     }
135 
136     private static final class VcGenCmdStrings {
137         private final String releaseDate;
138         private final String manufacturer;
139         private final String version;
140         private final String name;
141         private final String description;
142 
143         private VcGenCmdStrings(String releaseDate, String manufacturer, String version, String name,
144                 String description) {
145             this.releaseDate = releaseDate;
146             this.manufacturer = manufacturer;
147             this.version = version;
148             this.name = name;
149             this.description = description;
150         }
151     }
152 }