View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.freebsd;
6   
7   import static oshi.util.Memoizer.memoize;
8   
9   import java.util.function.Supplier;
10  
11  import oshi.annotation.concurrent.Immutable;
12  import oshi.hardware.common.AbstractFirmware;
13  import oshi.util.Constants;
14  import oshi.util.ExecutingCommand;
15  import oshi.util.ParseUtil;
16  import oshi.util.Util;
17  import oshi.util.tuples.Triplet;
18  
19  /**
20   * Firmware information from dmidecode
21   */
22  @Immutable
23  final class FreeBsdFirmware extends AbstractFirmware {
24  
25      private final Supplier<Triplet<String, String, String>> manufVersRelease = memoize(FreeBsdFirmware::readDmiDecode);
26  
27      @Override
28      public String getManufacturer() {
29          return manufVersRelease.get().getA();
30      }
31  
32      @Override
33      public String getVersion() {
34          return manufVersRelease.get().getB();
35      }
36  
37      @Override
38      public String getReleaseDate() {
39          return manufVersRelease.get().getC();
40      }
41  
42      /*
43       * Name and Description not set
44       */
45  
46      private static Triplet<String, String, String> readDmiDecode() {
47          String manufacturer = null;
48          String version = null;
49          String releaseDate = "";
50  
51          // $ sudo dmidecode -t bios
52          // # dmidecode 3.0
53          // Scanning /dev/mem for entry point.
54          // SMBIOS 2.7 present.
55          //
56          // Handle 0x0000, DMI type 0, 24 bytes
57          // BIOS Information
58          // Vendor: Parallels Software International Inc.
59          // Version: 11.2.1 (32626)
60          // Release Date: 07/15/2016
61          // ... <snip> ...
62          // BIOS Revision: 11.2
63          // Firmware Revision: 11.2
64  
65          final String manufacturerMarker = "Vendor:";
66          final String versionMarker = "Version:";
67          final String releaseDateMarker = "Release Date:";
68  
69          // Only works with root permissions but it's all we've got
70          for (final String checkLine : ExecutingCommand.runNative("dmidecode -t bios")) {
71              if (checkLine.contains(manufacturerMarker)) {
72                  manufacturer = checkLine.split(manufacturerMarker)[1].trim();
73              } else if (checkLine.contains(versionMarker)) {
74                  version = checkLine.split(versionMarker)[1].trim();
75              } else if (checkLine.contains(releaseDateMarker)) {
76                  releaseDate = checkLine.split(releaseDateMarker)[1].trim();
77              }
78          }
79          releaseDate = ParseUtil.parseMmDdYyyyToYyyyMmDD(releaseDate);
80          return new Triplet<>(Util.isBlank(manufacturer) ? Constants.UNKNOWN : manufacturer,
81                  Util.isBlank(version) ? Constants.UNKNOWN : version,
82                  Util.isBlank(releaseDate) ? Constants.UNKNOWN : releaseDate);
83      }
84  }