blob: 474f192613e39d6c4d4bd15945874f0c9f6d9e5c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkova4a8a8c2010-07-15 22:21:12 -070016
17#include "update_engine/omaha_request_params.h"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/utsname.h>
22
23#include <map>
24#include <string>
Darin Petkova3df55b2010-11-15 13:33:55 -080025#include <vector>
Darin Petkova4a8a8c2010-07-15 22:21:12 -070026
Ben Chan06c76a42014-09-05 08:21:06 -070027#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/strings/string_util.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029#include <brillo/key_value_store.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020030#include <policy/device_policy.h>
Darin Petkov49d91322010-10-25 16:34:58 -070031
Chris Sosabe45bef2013-04-09 18:25:12 -070032#include "update_engine/constants.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070033#include "update_engine/hardware_interface.h"
Alex Deymoac41a822015-09-15 20:52:53 -070034#include "update_engine/platform_constants.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070035#include "update_engine/system_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070036#include "update_engine/utils.h"
37
Darin Petkov49d91322010-10-25 16:34:58 -070038#define CALL_MEMBER_FN(object, member) ((object).*(member))
39
Darin Petkova4a8a8c2010-07-15 22:21:12 -070040using std::map;
41using std::string;
Darin Petkova3df55b2010-11-15 13:33:55 -080042using std::vector;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070043
Darin Petkova4a8a8c2010-07-15 22:21:12 -070044namespace chromeos_update_engine {
45
David Pursell02c18642014-11-06 11:26:11 -080046const char OmahaRequestParams::kAppId[] =
47 "{87efface-864d-49a5-9bb3-4b050a7c227a}";
David Pursell02c18642014-11-06 11:26:11 -080048const char OmahaRequestParams::kOsVersion[] = "Indy";
49const char OmahaRequestParams::kUpdateChannelKey[] = "CHROMEOS_RELEASE_TRACK";
50const char OmahaRequestParams::kIsPowerwashAllowedKey[] =
51 "CHROMEOS_IS_POWERWASH_ALLOWED";
52const char OmahaRequestParams::kAutoUpdateServerKey[] = "CHROMEOS_AUSERVER";
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020053
Jay Srinivasanae4697c2013-03-18 17:08:08 -070054const char* kChannelsByStability[] = {
55 // This list has to be sorted from least stable to most stable channel.
56 "canary-channel",
57 "dev-channel",
58 "beta-channel",
59 "stable-channel",
60};
Darin Petkov49d91322010-10-25 16:34:58 -070061
Alex Deymof329b932014-10-30 01:37:48 -070062bool OmahaRequestParams::Init(const string& in_app_version,
63 const string& in_update_url,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070064 bool in_interactive) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070065 LOG(INFO) << "Initializing parameters for this update attempt";
Jay Srinivasanae4697c2013-03-18 17:08:08 -070066 InitFromLsbValue();
Darin Petkov10d02dd2011-01-10 14:57:39 -080067 bool stateful_override = !ShouldLockDown();
Alex Deymoac41a822015-09-15 20:52:53 -070068 os_platform_ = constants::kOmahaPlatformName;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070069 os_version_ = OmahaRequestParams::kOsVersion;
70 app_version_ = in_app_version.empty() ?
Alex Vakulenko88b591f2014-08-28 16:48:57 -070071 GetLsbValue("CHROMEOS_RELEASE_VERSION", "", nullptr, stateful_override) :
Darin Petkov10d02dd2011-01-10 14:57:39 -080072 in_app_version;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070073 os_sp_ = app_version_ + "_" + GetMachineType();
74 os_board_ = GetLsbValue("CHROMEOS_RELEASE_BOARD",
75 "",
Alex Vakulenko88b591f2014-08-28 16:48:57 -070076 nullptr,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070077 stateful_override);
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070078 string release_app_id = GetLsbValue("CHROMEOS_RELEASE_APPID",
79 OmahaRequestParams::kAppId,
Alex Vakulenko88b591f2014-08-28 16:48:57 -070080 nullptr,
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070081 stateful_override);
Jay Srinivasanae4697c2013-03-18 17:08:08 -070082 board_app_id_ = GetLsbValue("CHROMEOS_BOARD_APPID",
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070083 release_app_id,
Alex Vakulenko88b591f2014-08-28 16:48:57 -070084 nullptr,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070085 stateful_override);
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070086 canary_app_id_ = GetLsbValue("CHROMEOS_CANARY_APPID",
87 release_app_id,
Alex Vakulenko88b591f2014-08-28 16:48:57 -070088 nullptr,
Jay Srinivasandb0acdf2013-04-02 14:47:45 -070089 stateful_override);
Jay Srinivasanae4697c2013-03-18 17:08:08 -070090 app_lang_ = "en-US";
J. Richard Barnette522d36f2013-10-28 17:22:12 -070091 hwid_ = system_state_->hardware()->GetHardwareClass();
Chris Sosac1972482013-04-30 22:31:10 -070092 if (CollectECFWVersions()) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -070093 fw_version_ = system_state_->hardware()->GetFirmwareVersion();
94 ec_version_ = system_state_->hardware()->GetECVersion();
Chris Sosac1972482013-04-30 22:31:10 -070095 }
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020096
Jay Srinivasanae4697c2013-03-18 17:08:08 -070097 if (current_channel_ == target_channel_) {
98 // deltas are only okay if the /.nodelta file does not exist. if we don't
99 // know (i.e. stat() returns some unexpected error), then err on the side of
100 // caution and say deltas are not okay.
101 struct stat stbuf;
102 delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
103 (errno == ENOENT);
104
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200105 } else {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700106 LOG(INFO) << "Disabling deltas as a channel change is pending";
107 // For now, disable delta updates if the current channel is different from
108 // the channel that we're sending to the update server because such updates
109 // are destined to fail -- the current rootfs hash will be different than
110 // the expected hash due to the different channel in /etc/lsb-release.
111 delta_okay_ = false;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200112 }
113
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700114 if (in_update_url.empty())
Alex Deymoac41a822015-09-15 20:52:53 -0700115 update_url_ = GetLsbValue(kAutoUpdateServerKey,
116 constants::kOmahaDefaultProductionURL,
David Pursell02c18642014-11-06 11:26:11 -0800117 nullptr, stateful_override);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700118 else
119 update_url_ = in_update_url;
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800120
121 // Set the interactive flag accordingly.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700122 interactive_ = in_interactive;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700123 return true;
124}
125
David Pursell02c18642014-11-06 11:26:11 -0800126bool OmahaRequestParams::IsUpdateUrlOfficial() const {
Alex Deymoac41a822015-09-15 20:52:53 -0700127 return (update_url_ == constants::kOmahaDefaultAUTestURL ||
128 update_url_ == GetLsbValue(kAutoUpdateServerKey,
129 constants::kOmahaDefaultProductionURL,
David Pursell02c18642014-11-06 11:26:11 -0800130 nullptr, !ShouldLockDown()));
131}
132
Chris Sosac1972482013-04-30 22:31:10 -0700133bool OmahaRequestParams::CollectECFWVersions() const {
Alex Vakulenko6a9d3492015-06-15 12:53:22 -0700134 return base::StartsWithASCII(hwid_, string("SAMS ALEX"), true) ||
135 base::StartsWithASCII(hwid_, string("BUTTERFLY"), true) ||
136 base::StartsWithASCII(hwid_, string("LUMPY"), true) ||
137 base::StartsWithASCII(hwid_, string("PARROT"), true) ||
138 base::StartsWithASCII(hwid_, string("SPRING"), true) ||
139 base::StartsWithASCII(hwid_, string("SNOW"), true);
Chris Sosac1972482013-04-30 22:31:10 -0700140}
141
Alex Deymof329b932014-10-30 01:37:48 -0700142bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700143 bool is_powerwash_allowed) {
144 LOG(INFO) << "SetTargetChannel called with " << new_target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700145 << ", Is Powerwash Allowed = "
146 << utils::ToString(is_powerwash_allowed)
147 << ". Current channel = " << current_channel_
148 << ", existing target channel = " << target_channel_
149 << ", download channel = " << download_channel_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700150 TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700151 brillo::KeyValueStore lsb_release;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700152 base::FilePath kFile(root_ + kStatefulPartition + "/etc/lsb-release");
Alex Deymoa7658442014-04-17 15:49:37 -0700153
Alex Deymo2b4e2a52014-09-23 12:05:56 -0700154 lsb_release.Load(kFile);
Alex Deymoa7658442014-04-17 15:49:37 -0700155 lsb_release.SetString(kUpdateChannelKey, new_target_channel);
156 lsb_release.SetBoolean(kIsPowerwashAllowedKey, is_powerwash_allowed);
157
Alex Vakulenko75039d72014-03-25 12:36:28 -0700158 TEST_AND_RETURN_FALSE(base::CreateDirectory(kFile.DirName()));
Alex Deymo2b4e2a52014-09-23 12:05:56 -0700159 TEST_AND_RETURN_FALSE(lsb_release.Save(kFile));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700160 target_channel_ = new_target_channel;
161 is_powerwash_allowed_ = is_powerwash_allowed;
Darin Petkov49d91322010-10-25 16:34:58 -0700162 return true;
163}
164
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700165void OmahaRequestParams::SetTargetChannelFromLsbValue() {
166 string target_channel_new_value = GetLsbValue(
167 kUpdateChannelKey,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700168 current_channel_,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700169 &chromeos_update_engine::OmahaRequestParams::IsValidChannel,
170 true); // stateful_override
171
172 if (target_channel_ != target_channel_new_value) {
173 target_channel_ = target_channel_new_value;
174 LOG(INFO) << "Target Channel set to " << target_channel_
175 << " from LSB file";
176 }
Darin Petkov49d91322010-10-25 16:34:58 -0700177}
178
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700179void OmahaRequestParams::SetCurrentChannelFromLsbValue() {
180 string current_channel_new_value = GetLsbValue(
181 kUpdateChannelKey,
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700182 current_channel_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700183 nullptr, // No need to validate the read-only rootfs channel.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700184 false); // stateful_override is false so we get the current channel.
185
186 if (current_channel_ != current_channel_new_value) {
187 current_channel_ = current_channel_new_value;
188 LOG(INFO) << "Current Channel set to " << current_channel_
189 << " from LSB file in rootfs";
190 }
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900191}
192
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700193void OmahaRequestParams::SetIsPowerwashAllowedFromLsbValue() {
194 string is_powerwash_allowed_str = GetLsbValue(
195 kIsPowerwashAllowedKey,
196 "false",
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700197 nullptr, // no need to validate
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700198 true); // always get it from stateful, as that's the only place it'll be
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700199 bool is_powerwash_allowed_new_value = (is_powerwash_allowed_str == "true");
200 if (is_powerwash_allowed_ != is_powerwash_allowed_new_value) {
201 is_powerwash_allowed_ = is_powerwash_allowed_new_value;
202 LOG(INFO) << "Powerwash Allowed set to "
203 << utils::ToString(is_powerwash_allowed_)
204 << " from LSB file in stateful";
205 }
206}
207
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700208void OmahaRequestParams::UpdateDownloadChannel() {
209 if (download_channel_ != target_channel_) {
210 download_channel_ = target_channel_;
211 LOG(INFO) << "Download channel for this attempt = " << download_channel_;
212 }
213}
214
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700215void OmahaRequestParams::InitFromLsbValue() {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700216 SetCurrentChannelFromLsbValue();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700217 SetTargetChannelFromLsbValue();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700218 SetIsPowerwashAllowedFromLsbValue();
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700219 UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700220}
221
222string OmahaRequestParams::GetLsbValue(const string& key,
223 const string& default_value,
224 ValueValidator validator,
225 bool stateful_override) const {
Darin Petkova3df55b2010-11-15 13:33:55 -0800226 vector<string> files;
227 if (stateful_override) {
Chris Sosabe45bef2013-04-09 18:25:12 -0700228 files.push_back(string(kStatefulPartition) + "/etc/lsb-release");
Darin Petkova3df55b2010-11-15 13:33:55 -0800229 }
230 files.push_back("/etc/lsb-release");
231 for (vector<string>::const_iterator it = files.begin();
232 it != files.end(); ++it) {
233 // TODO(adlr): make sure files checked are owned as root (and all their
234 // parents are recursively, too).
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700235 brillo::KeyValueStore data;
Alex Deymo2b4e2a52014-09-23 12:05:56 -0700236 if (!data.Load(base::FilePath(root_ + *it)))
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700237 continue;
238
Alex Deymoa7658442014-04-17 15:49:37 -0700239 string value;
240 if (data.GetString(key, &value)) {
Darin Petkov49d91322010-10-25 16:34:58 -0700241 if (validator && !CALL_MEMBER_FN(*this, validator)(value)) {
242 continue;
243 }
244 return value;
245 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700246 }
247 // not found
248 return default_value;
249}
250
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700251string OmahaRequestParams::GetMachineType() const {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700252 struct utsname buf;
253 string ret;
254 if (uname(&buf) == 0)
255 ret = buf.machine;
256 return ret;
257}
258
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700259bool OmahaRequestParams::ShouldLockDown() const {
Darin Petkov10d02dd2011-01-10 14:57:39 -0800260 if (force_lock_down_) {
261 return forced_lock_down_;
262 }
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700263 return system_state_->hardware()->IsOfficialBuild() &&
264 system_state_->hardware()->IsNormalBootMode();
Darin Petkov49d91322010-10-25 16:34:58 -0700265}
266
Alex Deymof329b932014-10-30 01:37:48 -0700267bool OmahaRequestParams::IsValidChannel(const string& channel) const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700268 return GetChannelIndex(channel) >= 0;
Darin Petkov49d91322010-10-25 16:34:58 -0700269}
270
Alex Deymof329b932014-10-30 01:37:48 -0700271void OmahaRequestParams::set_root(const string& root) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700272 root_ = root;
273 InitFromLsbValue();
274}
275
276void OmahaRequestParams::SetLockDown(bool lock) {
Darin Petkov10d02dd2011-01-10 14:57:39 -0800277 force_lock_down_ = true;
278 forced_lock_down_ = lock;
Darin Petkov49d91322010-10-25 16:34:58 -0700279}
280
Alex Deymof329b932014-10-30 01:37:48 -0700281int OmahaRequestParams::GetChannelIndex(const string& channel) const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700282 for (size_t t = 0; t < arraysize(kChannelsByStability); ++t)
283 if (channel == kChannelsByStability[t])
284 return t;
285
286 return -1;
287}
288
289bool OmahaRequestParams::to_more_stable_channel() const {
290 int current_channel_index = GetChannelIndex(current_channel_);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700291 int download_channel_index = GetChannelIndex(download_channel_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700292
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700293 return download_channel_index > current_channel_index;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700294}
295
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700296string OmahaRequestParams::GetAppId() const {
297 return download_channel_ == "canary-channel" ? canary_app_id_ : board_app_id_;
298}
299
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700300} // namespace chromeos_update_engine