File: motherboard.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (187 lines) | stat: -rw-r--r-- 6,366 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/motherboard.h"

#include <optional>
#include <string>
#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_WIN)
#include <windows.h>

#include "base/strings/utf_string_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_variant.h"
#include "base/win/wmi.h"
#endif

namespace metrics {
namespace {

struct MotherboardDetails {
  std::optional<std::string> manufacturer;
  std::optional<std::string> model;
  std::optional<std::string> bios_manufacturer;
  std::optional<std::string> bios_version;
  std::optional<Motherboard::BiosType> bios_type;
};

#if BUILDFLAG(IS_LINUX)
using base::FilePath;
using base::PathExists;
using base::ReadFileToString;
using base::TrimWhitespaceASCII;
using base::TRIM_TRAILING;

MotherboardDetails ReadMotherboardDetails() {
  constexpr FilePath::CharType kDmiPath[] = "/sys/devices/virtual/dmi/id";
  constexpr FilePath::CharType kEfiPath[] = "/sys/firmware/efi";
  const FilePath dmi_path(kDmiPath);
  MotherboardDetails details;
  std::string temp;
  if (ReadFileToString(dmi_path.Append("board_vendor"), &temp)) {
    details.manufacturer =
        std::string(TrimWhitespaceASCII(temp, TRIM_TRAILING));
  }
  if (ReadFileToString(dmi_path.Append("board_name"), &temp)) {
    details.model = std::string(TrimWhitespaceASCII(temp, TRIM_TRAILING));
  }
  if (ReadFileToString(dmi_path.Append("bios_vendor"), &temp)) {
    details.bios_manufacturer =
        std::string(TrimWhitespaceASCII(temp, TRIM_TRAILING));
  }
  if (ReadFileToString(dmi_path.Append("bios_version"), &temp)) {
    details.bios_version =
        std::string(TrimWhitespaceASCII(temp, TRIM_TRAILING));
  }
  if (PathExists(FilePath(kEfiPath))) {
    details.bios_type = Motherboard::BiosType::kUefi;
  } else {
    details.bios_type = Motherboard::BiosType::kLegacy;
  }
  return details;
}
#endif

#if BUILDFLAG(IS_WIN)
using Microsoft::WRL::ComPtr;
using base::win::ScopedBstr;
using base::win::ScopedVariant;

std::optional<std::string> ReadStringMember(
    ComPtr<IWbemClassObject> class_object,
    const wchar_t* key) {
  ScopedVariant variant;
  HRESULT hr = class_object->Get(key, 0, variant.Receive(), 0, 0);
  if (SUCCEEDED(hr) && variant.type() == VT_BSTR) {
    const auto len = ::SysStringLen(V_BSTR(variant.ptr()));
    std::wstring wstr(V_BSTR(variant.ptr()), len);
    return base::WideToUTF8(wstr);
  }
  return {};
}

void ReadWin32BaseBoard(const ComPtr<IWbemServices>& services,
                        std::optional<std::string>* manufacturer,
                        std::optional<std::string>* model) {
  static constexpr wchar_t kManufacturer[] = L"Manufacturer";
  static constexpr wchar_t kProduct[] = L"Product";
  static constexpr wchar_t kQueryProcessor[] =
      L"SELECT Manufacturer,Product FROM Win32_BaseBoard";

  ComPtr<IEnumWbemClassObject> enumerator_base_board;
  HRESULT hr = services->ExecQuery(
      ScopedBstr(L"WQL").Get(), ScopedBstr(kQueryProcessor).Get(),
      WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, nullptr,
      &enumerator_base_board);
  if (FAILED(hr) || !enumerator_base_board.Get())
    return;

  ComPtr<IWbemClassObject> class_object;
  ULONG items_returned = 0;
  hr = enumerator_base_board->Next(WBEM_INFINITE, 1, &class_object,
                                   &items_returned);
  if (FAILED(hr) || !items_returned)
    return;
  *manufacturer = ReadStringMember(class_object, kManufacturer);
  *model = ReadStringMember(class_object, kProduct);
}

void ReadWin32Bios(const ComPtr<IWbemServices>& services,
                   std::optional<std::string>* bios_manufacturer,
                   std::optional<std::string>* bios_version) {
  static constexpr wchar_t kManufacturer[] = L"Manufacturer";
  static constexpr wchar_t kVersion[] = L"Version";
  static constexpr wchar_t kQueryProcessor[] =
      L"SELECT Manufacturer,Version FROM Win32_BIOS";

  ComPtr<IEnumWbemClassObject> enumerator_base_board;
  HRESULT hr = services->ExecQuery(
      ScopedBstr(L"WQL").Get(), ScopedBstr(kQueryProcessor).Get(),
      WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, nullptr,
      &enumerator_base_board);
  if (FAILED(hr) || !enumerator_base_board.Get())
    return;

  ComPtr<IWbemClassObject> class_object;
  ULONG items_returned = 0;
  hr = enumerator_base_board->Next(WBEM_INFINITE, 1, &class_object,
                                   &items_returned);
  if (FAILED(hr) || !items_returned)
    return;
  *bios_manufacturer = ReadStringMember(class_object, kManufacturer);
  *bios_version = ReadStringMember(class_object, kVersion);
}

void ReadFirmwareType(std::optional<Motherboard::BiosType>* bios_type) {
  FIRMWARE_TYPE firmware_type = FirmwareTypeUnknown;
  if (::GetFirmwareType(&firmware_type)) {
    if (firmware_type == FirmwareTypeBios) {
      *bios_type = Motherboard::BiosType::kLegacy;
    } else if (firmware_type == FirmwareTypeUefi) {
      *bios_type = Motherboard::BiosType::kUefi;
    } else {
      *bios_type = std::nullopt;
    }
  }
}

MotherboardDetails ReadMotherboardDetails() {
  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
                                                base::BlockingType::MAY_BLOCK);
  ComPtr<IWbemServices> services;
  MotherboardDetails details;
  if (!base::win::CreateLocalWmiConnection(true, &services))
    return details;
  ReadWin32BaseBoard(services, &details.manufacturer, &details.model);
  ReadWin32Bios(services, &details.bios_manufacturer, &details.bios_version);
  ReadFirmwareType(&details.bios_type);
  return details;
}
#endif
}  // namespace

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN)
Motherboard::Motherboard() {
  auto details = ReadMotherboardDetails();
  manufacturer_ = std::move(details.manufacturer),
  model_ = std::move(details.model),
  bios_manufacturer_ = std::move(details.bios_manufacturer),
  bios_version_ = std::move(details.bios_version),
  bios_type_ = std::move(details.bios_type);
}
#else
Motherboard::Motherboard() = default;
#endif

Motherboard::~Motherboard() = default;

}  // namespace metrics