blob: ed80060b5f0d944e344923bf60a8e23f64418dc6 [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// Copyright (c) 2010 The Chromium OS 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.
4
5#include "update_engine/omaha_request_params.h"
6
7#include <errno.h>
8#include <fcntl.h>
9#include <sys/utsname.h>
10
11#include <map>
12#include <string>
Darin Petkova3df55b2010-11-15 13:33:55 -080013#include <vector>
Darin Petkova4a8a8c2010-07-15 22:21:12 -070014
Darin Petkov49d91322010-10-25 16:34:58 -070015#include <base/file_util.h>
16#include <base/string_util.h>
17
Darin Petkova4a8a8c2010-07-15 22:21:12 -070018#include "update_engine/simple_key_value_store.h"
19#include "update_engine/utils.h"
20
Darin Petkov49d91322010-10-25 16:34:58 -070021#define CALL_MEMBER_FN(object, member) ((object).*(member))
22
Darin Petkova4a8a8c2010-07-15 22:21:12 -070023using std::map;
24using std::string;
Darin Petkova3df55b2010-11-15 13:33:55 -080025using std::vector;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070026
Darin Petkova4a8a8c2010-07-15 22:21:12 -070027namespace chromeos_update_engine {
28
Darin Petkov49d91322010-10-25 16:34:58 -070029const char OmahaRequestParams::kUpdateTrackKey[] = "CHROMEOS_RELEASE_TRACK";
30
Darin Petkov5a7f5652010-07-22 21:40:09 -070031const char* const OmahaRequestParams::kAppId(
32 "{87efface-864d-49a5-9bb3-4b050a7c227a}");
33const char* const OmahaRequestParams::kOsPlatform("Chrome OS");
34const char* const OmahaRequestParams::kOsVersion("Indy");
35const char* const OmahaRequestParams::kUpdateUrl(
36 "https://tools.google.com/service/update2");
37
Darin Petkovfbb40092010-07-29 17:05:50 -070038static const char kHWIDPath[] = "/sys/devices/platform/chromeos_acpi/HWID";
39
Darin Petkov49d91322010-10-25 16:34:58 -070040OmahaRequestDeviceParams::OmahaRequestDeviceParams() :
Darin Petkov10d02dd2011-01-10 14:57:39 -080041 force_lock_down_(false),
42 forced_lock_down_(false) {}
Darin Petkov49d91322010-10-25 16:34:58 -070043
Darin Petkov5a7f5652010-07-22 21:40:09 -070044bool OmahaRequestDeviceParams::Init(const std::string& in_app_version,
45 const std::string& in_update_url) {
Darin Petkov10d02dd2011-01-10 14:57:39 -080046 bool stateful_override = !ShouldLockDown();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070047 os_platform = OmahaRequestParams::kOsPlatform;
48 os_version = OmahaRequestParams::kOsVersion;
Darin Petkov5a7f5652010-07-22 21:40:09 -070049 app_version = in_app_version.empty() ?
Darin Petkov10d02dd2011-01-10 14:57:39 -080050 GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL, stateful_override) :
51 in_app_version;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070052 os_sp = app_version + "_" + GetMachineType();
Darin Petkov10d02dd2011-01-10 14:57:39 -080053 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "", NULL, stateful_override);
Darin Petkovd315dc72010-11-15 09:52:24 -080054 app_id = GetLsbValue("CHROMEOS_RELEASE_APPID",
55 OmahaRequestParams::kAppId,
Darin Petkova3df55b2010-11-15 13:33:55 -080056 NULL,
Darin Petkov10d02dd2011-01-10 14:57:39 -080057 stateful_override);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070058 app_lang = "en-US";
Darin Petkov49d91322010-10-25 16:34:58 -070059 app_track = GetLsbValue(
60 kUpdateTrackKey,
61 "",
Darin Petkova3df55b2010-11-15 13:33:55 -080062 &chromeos_update_engine::OmahaRequestDeviceParams::IsValidTrack,
Darin Petkov10d02dd2011-01-10 14:57:39 -080063 true); // stateful_override
Darin Petkovfbb40092010-07-29 17:05:50 -070064 hardware_class = GetHardwareClass();
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070065 struct stat stbuf;
Darin Petkov5a7f5652010-07-22 21:40:09 -070066
Darin Petkova3df55b2010-11-15 13:33:55 -080067 // Deltas are only okay if the /.nodelta file does not exist. If we don't
68 // know (i.e. stat() returns some unexpected error), then err on the side of
69 // caution and say deltas are not okay.
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070070 delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
71 (errno == ENOENT);
72
Darin Petkova3df55b2010-11-15 13:33:55 -080073 // For now, disable delta updates if the rootfs track is different than the
74 // track that we're sending to the update server because such updates are
75 // destined to fail -- the source rootfs hash will be different than the
76 // expected hash due to the different track in /etc/lsb-release.
77 //
78 // Longer term we should consider an alternative: (a) clients can send
79 // (current_version, current_channel, new_channel) information, or (b) the
80 // build process can make sure releases on separate tracks are identical (i.e,
81 // by not stamping the release with the channel), or (c) the release process
82 // can ensure that different channels get different version numbers.
83 const string rootfs_track = GetLsbValue(
84 kUpdateTrackKey,
85 "",
Darin Petkov10d02dd2011-01-10 14:57:39 -080086 NULL, // No need to validate the read-only rootfs track.
87 false); // stateful_override
Darin Petkova3df55b2010-11-15 13:33:55 -080088 delta_okay = delta_okay && rootfs_track == app_track;
89
Darin Petkov5a7f5652010-07-22 21:40:09 -070090 update_url = in_update_url.empty() ?
Darin Petkova3df55b2010-11-15 13:33:55 -080091 GetLsbValue("CHROMEOS_AUSERVER",
92 OmahaRequestParams::kUpdateUrl,
93 NULL,
Darin Petkov10d02dd2011-01-10 14:57:39 -080094 stateful_override) :
Darin Petkov5a7f5652010-07-22 21:40:09 -070095 in_update_url;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070096 return true;
97}
98
Darin Petkov49d91322010-10-25 16:34:58 -070099bool OmahaRequestDeviceParams::SetTrack(const std::string& track) {
100 TEST_AND_RETURN_FALSE(IsValidTrack(track));
101 FilePath kFile(root_ + utils::kStatefulPartition + "/etc/lsb-release");
102 string file_data;
103 map<string, string> data;
104 if (file_util::ReadFileToString(kFile, &file_data)) {
105 data = simple_key_value_store::ParseString(file_data);
106 }
107 data[kUpdateTrackKey] = track;
108 file_data = simple_key_value_store::AssembleString(data);
109 TEST_AND_RETURN_FALSE(file_util::CreateDirectory(kFile.DirName()));
110 TEST_AND_RETURN_FALSE(
111 file_util::WriteFile(kFile, file_data.data(), file_data.size()) ==
112 static_cast<int>(file_data.size()));
113 app_track = track;
114 return true;
115}
116
117bool OmahaRequestDeviceParams::SetDeviceTrack(const std::string& track) {
118 OmahaRequestDeviceParams params;
119 TEST_AND_RETURN_FALSE(params.Init("", ""));
120 return params.SetTrack(track);
121}
122
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900123string OmahaRequestDeviceParams::GetDeviceTrack() {
124 OmahaRequestDeviceParams params;
125 // Note that params.app_track is an empty string if the value in
126 // lsb-release file is invalid. See Init() for details.
127 return params.Init("", "") ? params.app_track : "";
128}
129
Darin Petkov49d91322010-10-25 16:34:58 -0700130string OmahaRequestDeviceParams::GetLsbValue(const string& key,
131 const string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800132 ValueValidator validator,
133 bool stateful_override) const {
134 vector<string> files;
135 if (stateful_override) {
136 files.push_back(string(utils::kStatefulPartition) + "/etc/lsb-release");
137 }
138 files.push_back("/etc/lsb-release");
139 for (vector<string>::const_iterator it = files.begin();
140 it != files.end(); ++it) {
141 // TODO(adlr): make sure files checked are owned as root (and all their
142 // parents are recursively, too).
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700143 string file_data;
Darin Petkova3df55b2010-11-15 13:33:55 -0800144 if (!utils::ReadFileToString(root_ + *it, &file_data))
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700145 continue;
146
147 map<string, string> data = simple_key_value_store::ParseString(file_data);
Darin Petkov49d91322010-10-25 16:34:58 -0700148 if (utils::MapContainsKey(data, key)) {
149 const string& value = data[key];
150 if (validator && !CALL_MEMBER_FN(*this, validator)(value)) {
151 continue;
152 }
153 return value;
154 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700155 }
156 // not found
157 return default_value;
158}
159
160string OmahaRequestDeviceParams::GetMachineType() const {
161 struct utsname buf;
162 string ret;
163 if (uname(&buf) == 0)
164 ret = buf.machine;
165 return ret;
166}
167
Darin Petkovfbb40092010-07-29 17:05:50 -0700168string OmahaRequestDeviceParams::GetHardwareClass() const {
169 string hwid;
170 if (!file_util::ReadFileToString(FilePath(root_ + kHWIDPath), &hwid)) {
171 LOG(ERROR) << "Unable to determine the system hardware qualification ID.";
172 return "";
173 }
174 TrimWhitespaceASCII(hwid, TRIM_ALL, &hwid);
175 return hwid;
176}
177
Darin Petkov10d02dd2011-01-10 14:57:39 -0800178bool OmahaRequestDeviceParams::ShouldLockDown() const {
179 if (force_lock_down_) {
180 return forced_lock_down_;
181 }
182 return utils::IsOfficialBuild() && utils::IsNormalBootMode();
Darin Petkov49d91322010-10-25 16:34:58 -0700183}
184
185bool OmahaRequestDeviceParams::IsValidTrack(const std::string& track) const {
Darin Petkov3f375c72010-11-05 09:42:26 -0700186 static const char* kValidTracks[] = {
187 "canary-channel",
188 "beta-channel",
189 "dev-channel",
190 };
Darin Petkov10d02dd2011-01-10 14:57:39 -0800191 if (!ShouldLockDown()) {
Darin Petkov3f375c72010-11-05 09:42:26 -0700192 return true;
193 }
194 for (size_t t = 0; t < arraysize(kValidTracks); ++t) {
195 if (track == kValidTracks[t]) {
196 return true;
197 }
198 }
199 return false;
Darin Petkov49d91322010-10-25 16:34:58 -0700200}
201
Darin Petkov10d02dd2011-01-10 14:57:39 -0800202void OmahaRequestDeviceParams::SetLockDown(bool lock) {
203 force_lock_down_ = true;
204 forced_lock_down_ = lock;
Darin Petkov49d91322010-10-25 16:34:58 -0700205}
206
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700207} // namespace chromeos_update_engine