Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / system / version_loader.cc
blobab9505b6274f27040faa0a60f6764ffd5bedb42f
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/system/version_loader.h"
7 #include <vector>
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/location.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/sys_info.h"
16 #include "base/time/time.h"
18 namespace chromeos {
19 namespace version_loader {
21 namespace {
23 // Beginning of line we look for that gives full version number.
24 // Format: x.x.xx.x (Developer|Official build extra info) board info
25 const char kFullVersionKey[] = "CHROMEOS_RELEASE_DESCRIPTION";
27 // Same but for short version (x.x.xx.x).
28 const char kVersionKey[] = "CHROMEOS_RELEASE_VERSION";
30 // Beginning of line we look for that gives the firmware version.
31 const char kFirmwarePrefix[] = "version";
33 // File to look for firmware number in.
34 const char kPathFirmware[] = "/var/log/bios_info.txt";
36 } // namespace
38 std::string GetVersion(VersionFormat format) {
39 std::string version;
40 std::string key = (format == VERSION_FULL ?
41 kFullVersionKey : kVersionKey);
42 if (!base::SysInfo::GetLsbReleaseValue(key, &version)) {
43 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS())
44 << "No LSB version key: " << key;
45 version = "0.0.0.0";
47 if (format == VERSION_SHORT_WITH_DATE) {
48 base::Time::Exploded ctime;
49 base::SysInfo::GetLsbReleaseTime().UTCExplode(&ctime);
50 version += base::StringPrintf("-%02u.%02u.%02u",
51 ctime.year % 100,
52 ctime.month,
53 ctime.day_of_month);
56 return version;
59 std::string GetFirmware() {
60 std::string firmware;
61 std::string contents;
62 const base::FilePath file_path(kPathFirmware);
63 if (base::ReadFileToString(file_path, &contents)) {
64 firmware = ParseFirmware(contents);
66 return firmware;
69 std::string ParseFirmware(const std::string& contents) {
70 // The file contains lines such as:
71 // vendor | ...
72 // version | ...
73 // release_date | ...
74 // We don't make any assumption that the spaces between "version" and "|" is
75 // fixed. So we just match kFirmwarePrefix at the start of the line and find
76 // the first character that is not "|" or space
78 base::StringPiece firmware_prefix(kFirmwarePrefix);
79 for (const std::string& line : base::SplitString(
80 contents, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
81 if (base::StartsWith(line, firmware_prefix,
82 base::CompareCase::INSENSITIVE_ASCII)) {
83 std::string str = line.substr(firmware_prefix.size());
84 size_t found = str.find_first_not_of("| ");
85 if (found != std::string::npos)
86 return str.substr(found);
89 return std::string();
92 } // namespace version_loader
93 } // namespace chromeos