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"
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"
19 namespace version_loader
{
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";
38 std::string
GetVersion(VersionFormat format
) {
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
;
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",
59 std::string
GetFirmware() {
62 const base::FilePath
file_path(kPathFirmware
);
63 if (base::ReadFileToString(file_path
, &contents
)) {
64 firmware
= ParseFirmware(contents
);
69 std::string
ParseFirmware(const std::string
& contents
) {
70 // The file contains lines such as:
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 std::vector
<std::string
> lines
;
79 base::SplitString(contents
, '\n', &lines
);
80 for (size_t i
= 0; i
< lines
.size(); ++i
) {
81 if (base::StartsWithASCII(lines
[i
], kFirmwarePrefix
, false)) {
82 std::string str
= lines
[i
].substr(std::string(kFirmwarePrefix
).size());
83 size_t found
= str
.find_first_not_of("| ");
84 if (found
!= std::string::npos
)
85 return str
.substr(found
);
91 } // namespace version_loader
92 } // namespace chromeos