blob: 065d5bb690eff171505ea93691aa0c9e10f7e7eb [file] [log] [blame]
Darin Petkovf2065b42011-05-17 16:36:27 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Darin Petkova4a8a8c2010-07-15 22:21:12 -07002// 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>
Chris Sosac1972482013-04-30 22:31:10 -070016#include <base/string_util.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020017#include <policy/device_policy.h>
Darin Petkov49d91322010-10-25 16:34:58 -070018
Chris Sosabe45bef2013-04-09 18:25:12 -070019#include "update_engine/constants.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070020#include "update_engine/hardware_interface.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070021#include "update_engine/simple_key_value_store.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070022#include "update_engine/system_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070023#include "update_engine/utils.h"
24
Darin Petkov49d91322010-10-25 16:34:58 -070025#define CALL_MEMBER_FN(object, member) ((object).*(member))
26
Darin Petkova4a8a8c2010-07-15 22:21:12 -070027using std::map;
28using std::string;
Darin Petkova3df55b2010-11-15 13:33:55 -080029using std::vector;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070030
Darin Petkova4a8a8c2010-07-15 22:21:12 -070031namespace chromeos_update_engine {
32
Darin Petkov5a7f5652010-07-22 21:40:09 -070033const char* const OmahaRequestParams::kAppId(
34 "{87efface-864d-49a5-9bb3-4b050a7c227a}");
35const char* const OmahaRequestParams::kOsPlatform("Chrome OS");
36const char* const OmahaRequestParams::kOsVersion("Indy");
Jay Srinivasan55f50c22013-01-10 19:24:35 -080037const char* const kProductionOmahaUrl(
Darin Petkov5a7f5652010-07-22 21:40:09 -070038 "https://tools.google.com/service/update2");
39
Jay Srinivasanae4697c2013-03-18 17:08:08 -070040const char* const OmahaRequestParams::kUpdateChannelKey(
41 "CHROMEOS_RELEASE_TRACK");
42const char* const OmahaRequestParams::kIsPowerwashAllowedKey(
43 "CHROMEOS_IS_POWERWASH_ALLOWED");
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020044
Jay Srinivasanae4697c2013-03-18 17:08:08 -070045const char* kChannelsByStability[] = {
46 // This list has to be sorted from least stable to most stable channel.
47 "canary-channel",
48 "dev-channel",
49 "beta-channel",
50 "stable-channel",
51};
Darin Petkov49d91322010-10-25 16:34:58 -070052
Jay Srinivasanae4697c2013-03-18 17:08:08 -070053bool OmahaRequestParams::Init(const std::string& in_app_version,
54 const std::string& in_update_url,
55 bool in_interactive) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070056 LOG(INFO) << "Initializing parameters for this update attempt";
Jay Srinivasanae4697c2013-03-18 17:08:08 -070057 InitFromLsbValue();
Darin Petkov10d02dd2011-01-10 14:57:39 -080058 bool stateful_override = !ShouldLockDown();
Jay Srinivasanae4697c2013-03-18 17:08:08 -070059 os_platform_ = OmahaRequestParams::kOsPlatform;
60 os_version_ = OmahaRequestParams::kOsVersion;
61 app_version_ = in_app_version.empty() ?
Darin Petkov10d02dd2011-01-10 14:57:39 -080062 GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL, stateful_override) :
63 in_app_version;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070064 os_sp_ = app_version_ + "_" + GetMachineType();
65 os_board_ = GetLsbValue("CHROMEOS_RELEASE_BOARD",
66 "",
67 NULL,
68 stateful_override);
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070069 string release_app_id = GetLsbValue("CHROMEOS_RELEASE_APPID",
70 OmahaRequestParams::kAppId,
71 NULL,
72 stateful_override);
Jay Srinivasanae4697c2013-03-18 17:08:08 -070073 board_app_id_ = GetLsbValue("CHROMEOS_BOARD_APPID",
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070074 release_app_id,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070075 NULL,
76 stateful_override);
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070077 canary_app_id_ = GetLsbValue("CHROMEOS_CANARY_APPID",
78 release_app_id,
79 NULL,
80 stateful_override);
Jay Srinivasanae4697c2013-03-18 17:08:08 -070081 app_lang_ = "en-US";
J. Richard Barnette522d36f2013-10-28 17:22:12 -070082 hwid_ = system_state_->hardware()->GetHardwareClass();
Chris Sosac1972482013-04-30 22:31:10 -070083 if (CollectECFWVersions()) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -070084 fw_version_ = system_state_->hardware()->GetFirmwareVersion();
85 ec_version_ = system_state_->hardware()->GetECVersion();
Chris Sosac1972482013-04-30 22:31:10 -070086 }
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020087
Jay Srinivasanae4697c2013-03-18 17:08:08 -070088 if (current_channel_ == target_channel_) {
89 // deltas are only okay if the /.nodelta file does not exist. if we don't
90 // know (i.e. stat() returns some unexpected error), then err on the side of
91 // caution and say deltas are not okay.
92 struct stat stbuf;
93 delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
94 (errno == ENOENT);
95
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020096 } else {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070097 LOG(INFO) << "Disabling deltas as a channel change is pending";
98 // For now, disable delta updates if the current channel is different from
99 // the channel that we're sending to the update server because such updates
100 // are destined to fail -- the current rootfs hash will be different than
101 // the expected hash due to the different channel in /etc/lsb-release.
102 delta_okay_ = false;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200103 }
104
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700105 if (in_update_url.empty())
106 update_url_ = GetLsbValue("CHROMEOS_AUSERVER", kProductionOmahaUrl, NULL,
107 stateful_override);
108 else
109 update_url_ = in_update_url;
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800110
111 // Set the interactive flag accordingly.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700112 interactive_ = in_interactive;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700113 return true;
114}
115
Chris Sosac1972482013-04-30 22:31:10 -0700116bool OmahaRequestParams::CollectECFWVersions() const {
117 return (
118 StartsWithASCII(hwid_, string("SAMS ALEX"), true) ||
119 StartsWithASCII(hwid_, string("BUTTERFLY"), true) ||
120 StartsWithASCII(hwid_, string("LUMPY"), true) ||
121 StartsWithASCII(hwid_, string("PARROT"), true) ||
122 StartsWithASCII(hwid_, string("SPRING"), true) ||
123 StartsWithASCII(hwid_, string("SNOW"), true)
124 );
125}
126
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700127bool OmahaRequestParams::SetTargetChannel(const std::string& new_target_channel,
128 bool is_powerwash_allowed) {
129 LOG(INFO) << "SetTargetChannel called with " << new_target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700130 << ", Is Powerwash Allowed = "
131 << utils::ToString(is_powerwash_allowed)
132 << ". Current channel = " << current_channel_
133 << ", existing target channel = " << target_channel_
134 << ", download channel = " << download_channel_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700135 TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel));
Chris Sosabe45bef2013-04-09 18:25:12 -0700136 FilePath kFile(root_ + kStatefulPartition + "/etc/lsb-release");
Darin Petkov49d91322010-10-25 16:34:58 -0700137 string file_data;
138 map<string, string> data;
139 if (file_util::ReadFileToString(kFile, &file_data)) {
140 data = simple_key_value_store::ParseString(file_data);
141 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700142 data[kUpdateChannelKey] = new_target_channel;
143 data[kIsPowerwashAllowedKey] = is_powerwash_allowed ? "true" : "false";
Darin Petkov49d91322010-10-25 16:34:58 -0700144 file_data = simple_key_value_store::AssembleString(data);
145 TEST_AND_RETURN_FALSE(file_util::CreateDirectory(kFile.DirName()));
146 TEST_AND_RETURN_FALSE(
147 file_util::WriteFile(kFile, file_data.data(), file_data.size()) ==
148 static_cast<int>(file_data.size()));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700149 target_channel_ = new_target_channel;
150 is_powerwash_allowed_ = is_powerwash_allowed;
Darin Petkov49d91322010-10-25 16:34:58 -0700151 return true;
152}
153
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700154void OmahaRequestParams::SetTargetChannelFromLsbValue() {
155 string target_channel_new_value = GetLsbValue(
156 kUpdateChannelKey,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700157 current_channel_,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700158 &chromeos_update_engine::OmahaRequestParams::IsValidChannel,
159 true); // stateful_override
160
161 if (target_channel_ != target_channel_new_value) {
162 target_channel_ = target_channel_new_value;
163 LOG(INFO) << "Target Channel set to " << target_channel_
164 << " from LSB file";
165 }
Darin Petkov49d91322010-10-25 16:34:58 -0700166}
167
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700168void OmahaRequestParams::SetCurrentChannelFromLsbValue() {
169 string current_channel_new_value = GetLsbValue(
170 kUpdateChannelKey,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700171 current_channel_,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700172 NULL, // No need to validate the read-only rootfs channel.
173 false); // stateful_override is false so we get the current channel.
174
175 if (current_channel_ != current_channel_new_value) {
176 current_channel_ = current_channel_new_value;
177 LOG(INFO) << "Current Channel set to " << current_channel_
178 << " from LSB file in rootfs";
179 }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900180}
181
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700182void OmahaRequestParams::SetIsPowerwashAllowedFromLsbValue() {
183 string is_powerwash_allowed_str = GetLsbValue(
184 kIsPowerwashAllowedKey,
185 "false",
186 NULL, // no need to validate
187 true); // always get it from stateful, as that's the only place it'll be
188 bool is_powerwash_allowed_new_value = (is_powerwash_allowed_str == "true");
189 if (is_powerwash_allowed_ != is_powerwash_allowed_new_value) {
190 is_powerwash_allowed_ = is_powerwash_allowed_new_value;
191 LOG(INFO) << "Powerwash Allowed set to "
192 << utils::ToString(is_powerwash_allowed_)
193 << " from LSB file in stateful";
194 }
195}
196
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700197void OmahaRequestParams::UpdateDownloadChannel() {
198 if (download_channel_ != target_channel_) {
199 download_channel_ = target_channel_;
200 LOG(INFO) << "Download channel for this attempt = " << download_channel_;
201 }
202}
203
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700204void OmahaRequestParams::InitFromLsbValue() {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700205 SetCurrentChannelFromLsbValue();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700206 SetTargetChannelFromLsbValue();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700207 SetIsPowerwashAllowedFromLsbValue();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700208 UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700209}
210
211string OmahaRequestParams::GetLsbValue(const string& key,
212 const string& default_value,
213 ValueValidator validator,
214 bool stateful_override) const {
Darin Petkova3df55b2010-11-15 13:33:55 -0800215 vector<string> files;
216 if (stateful_override) {
Chris Sosabe45bef2013-04-09 18:25:12 -0700217 files.push_back(string(kStatefulPartition) + "/etc/lsb-release");
Darin Petkova3df55b2010-11-15 13:33:55 -0800218 }
219 files.push_back("/etc/lsb-release");
220 for (vector<string>::const_iterator it = files.begin();
221 it != files.end(); ++it) {
222 // TODO(adlr): make sure files checked are owned as root (and all their
223 // parents are recursively, too).
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700224 string file_data;
Gilad Arnold19a45f02012-07-19 12:36:10 -0700225 if (!utils::ReadFile(root_ + *it, &file_data))
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700226 continue;
227
228 map<string, string> data = simple_key_value_store::ParseString(file_data);
Darin Petkov49d91322010-10-25 16:34:58 -0700229 if (utils::MapContainsKey(data, key)) {
230 const string& value = data[key];
231 if (validator && !CALL_MEMBER_FN(*this, validator)(value)) {
232 continue;
233 }
234 return value;
235 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700236 }
237 // not found
238 return default_value;
239}
240
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700241string OmahaRequestParams::GetMachineType() const {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700242 struct utsname buf;
243 string ret;
244 if (uname(&buf) == 0)
245 ret = buf.machine;
246 return ret;
247}
248
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700249bool OmahaRequestParams::ShouldLockDown() const {
Darin Petkov10d02dd2011-01-10 14:57:39 -0800250 if (force_lock_down_) {
251 return forced_lock_down_;
252 }
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700253 return system_state_->hardware()->IsOfficialBuild() &&
254 system_state_->hardware()->IsNormalBootMode();
Darin Petkov49d91322010-10-25 16:34:58 -0700255}
256
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700257bool OmahaRequestParams::IsValidChannel(const std::string& channel) const {
258 return GetChannelIndex(channel) >= 0;
Darin Petkov49d91322010-10-25 16:34:58 -0700259}
260
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700261void OmahaRequestParams::set_root(const std::string& root) {
262 root_ = root;
263 InitFromLsbValue();
264}
265
266void OmahaRequestParams::SetLockDown(bool lock) {
Darin Petkov10d02dd2011-01-10 14:57:39 -0800267 force_lock_down_ = true;
268 forced_lock_down_ = lock;
Darin Petkov49d91322010-10-25 16:34:58 -0700269}
270
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700271int OmahaRequestParams::GetChannelIndex(const std::string& channel) const {
272 for (size_t t = 0; t < arraysize(kChannelsByStability); ++t)
273 if (channel == kChannelsByStability[t])
274 return t;
275
276 return -1;
277}
278
279bool OmahaRequestParams::to_more_stable_channel() const {
280 int current_channel_index = GetChannelIndex(current_channel_);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700281 int download_channel_index = GetChannelIndex(download_channel_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700282
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700283 return download_channel_index > current_channel_index;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700284}
285
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700286string OmahaRequestParams::GetAppId() const {
287 return download_channel_ == "canary-channel" ? canary_app_id_ : board_app_id_;
288}
289
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700290} // namespace chromeos_update_engine