blob: 3402451343128ecf710261e688f7d57ab4166837 [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 Deymod942f9d2015-11-06 16:11:50 -080029#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/key_value_store.h>
Alex Deymod942f9d2015-11-06 16:11:50 -080031#include <brillo/strings/string_utils.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020032#include <policy/device_policy.h>
Darin Petkov49d91322010-10-25 16:34:58 -070033
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/constants.h"
35#include "update_engine/common/hardware_interface.h"
36#include "update_engine/common/platform_constants.h"
37#include "update_engine/common/utils.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070038#include "update_engine/system_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070039
Darin Petkov49d91322010-10-25 16:34:58 -070040#define CALL_MEMBER_FN(object, member) ((object).*(member))
41
Darin Petkova4a8a8c2010-07-15 22:21:12 -070042using std::map;
43using std::string;
Darin Petkova3df55b2010-11-15 13:33:55 -080044using std::vector;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070045
Darin Petkova4a8a8c2010-07-15 22:21:12 -070046namespace chromeos_update_engine {
47
David Pursell02c18642014-11-06 11:26:11 -080048const char OmahaRequestParams::kOsVersion[] = "Indy";
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020049
Jay Srinivasanae4697c2013-03-18 17:08:08 -070050const char* kChannelsByStability[] = {
51 // This list has to be sorted from least stable to most stable channel.
52 "canary-channel",
53 "dev-channel",
54 "beta-channel",
55 "stable-channel",
56};
Darin Petkov49d91322010-10-25 16:34:58 -070057
Alex Deymo3be05c82015-10-23 11:29:11 -070058OmahaRequestParams::~OmahaRequestParams() {
59 if (!root_.empty())
60 test::SetImagePropertiesRootPrefix(nullptr);
61}
62
Alex Deymof329b932014-10-30 01:37:48 -070063bool OmahaRequestParams::Init(const string& in_app_version,
64 const string& in_update_url,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070065 bool in_interactive) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070066 LOG(INFO) << "Initializing parameters for this update attempt";
Alex Deymo85616652015-10-15 18:48:31 -070067 image_props_ = LoadImageProperties(system_state_);
68 mutable_image_props_ = LoadMutableImageProperties(system_state_);
69
70 // Sanity check the channel names.
71 if (!IsValidChannel(image_props_.current_channel))
72 image_props_.current_channel = "stable-channel";
73 if (!IsValidChannel(mutable_image_props_.target_channel))
74 mutable_image_props_.target_channel = image_props_.current_channel;
75 UpdateDownloadChannel();
76
77 LOG(INFO) << "Running from channel " << image_props_.current_channel;
78
Alex Deymoac41a822015-09-15 20:52:53 -070079 os_platform_ = constants::kOmahaPlatformName;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070080 os_version_ = OmahaRequestParams::kOsVersion;
Alex Deymo85616652015-10-15 18:48:31 -070081 if (!in_app_version.empty())
82 image_props_.version = in_app_version;
83
84 os_sp_ = image_props_.version + "_" + GetMachineType();
Jay Srinivasanae4697c2013-03-18 17:08:08 -070085 app_lang_ = "en-US";
J. Richard Barnette522d36f2013-10-28 17:22:12 -070086 hwid_ = system_state_->hardware()->GetHardwareClass();
Chris Sosac1972482013-04-30 22:31:10 -070087 if (CollectECFWVersions()) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -070088 fw_version_ = system_state_->hardware()->GetFirmwareVersion();
89 ec_version_ = system_state_->hardware()->GetECVersion();
Chris Sosac1972482013-04-30 22:31:10 -070090 }
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020091
Alex Deymo85616652015-10-15 18:48:31 -070092 if (image_props_.current_channel == mutable_image_props_.target_channel) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070093 // deltas are only okay if the /.nodelta file does not exist. if we don't
94 // know (i.e. stat() returns some unexpected error), then err on the side of
95 // caution and say deltas are not okay.
96 struct stat stbuf;
97 delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
98 (errno == ENOENT);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020099 } else {
Alex Deymo85616652015-10-15 18:48:31 -0700100 LOG(INFO) << "Disabling deltas as a channel change to "
101 << mutable_image_props_.target_channel
102 << " is pending, with is_powerwash_allowed="
103 << utils::ToString(mutable_image_props_.is_powerwash_allowed);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700104 // For now, disable delta updates if the current channel is different from
105 // the channel that we're sending to the update server because such updates
106 // are destined to fail -- the current rootfs hash will be different than
107 // the expected hash due to the different channel in /etc/lsb-release.
108 delta_okay_ = false;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200109 }
110
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700111 if (in_update_url.empty())
Alex Deymo85616652015-10-15 18:48:31 -0700112 update_url_ = image_props_.omaha_url;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113 else
114 update_url_ = in_update_url;
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800115
116 // Set the interactive flag accordingly.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700117 interactive_ = in_interactive;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700118 return true;
119}
120
David Pursell02c18642014-11-06 11:26:11 -0800121bool OmahaRequestParams::IsUpdateUrlOfficial() const {
Alex Deymoac41a822015-09-15 20:52:53 -0700122 return (update_url_ == constants::kOmahaDefaultAUTestURL ||
Alex Deymo85616652015-10-15 18:48:31 -0700123 update_url_ == image_props_.omaha_url);
David Pursell02c18642014-11-06 11:26:11 -0800124}
125
Chris Sosac1972482013-04-30 22:31:10 -0700126bool OmahaRequestParams::CollectECFWVersions() const {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800127 return base::StartsWith(hwid_, string("SAMS ALEX"),
128 base::CompareCase::SENSITIVE) ||
129 base::StartsWith(hwid_, string("BUTTERFLY"),
130 base::CompareCase::SENSITIVE) ||
131 base::StartsWith(hwid_, string("LUMPY"),
132 base::CompareCase::SENSITIVE) ||
133 base::StartsWith(hwid_, string("PARROT"),
134 base::CompareCase::SENSITIVE) ||
135 base::StartsWith(hwid_, string("SPRING"),
136 base::CompareCase::SENSITIVE) ||
137 base::StartsWith(hwid_, string("SNOW"), base::CompareCase::SENSITIVE);
Chris Sosac1972482013-04-30 22:31:10 -0700138}
139
Alex Deymof329b932014-10-30 01:37:48 -0700140bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel,
Alex Deymod942f9d2015-11-06 16:11:50 -0800141 bool is_powerwash_allowed,
142 string* error_message) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700143 LOG(INFO) << "SetTargetChannel called with " << new_target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700144 << ", Is Powerwash Allowed = "
145 << utils::ToString(is_powerwash_allowed)
Alex Deymo85616652015-10-15 18:48:31 -0700146 << ". Current channel = " << image_props_.current_channel
147 << ", existing target channel = "
148 << mutable_image_props_.target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700149 << ", download channel = " << download_channel_;
Alex Deymod942f9d2015-11-06 16:11:50 -0800150 if (!IsValidChannel(new_target_channel)) {
151 string valid_channels = brillo::string_utils::JoinRange(
152 ", ",
153 std::begin(kChannelsByStability),
154 std::end(kChannelsByStability));
155 if (error_message) {
156 *error_message = base::StringPrintf(
157 "Invalid channel name \"%s\", valid names are: %s",
158 new_target_channel.c_str(), valid_channels.c_str());
159 }
160 return false;
161 }
Alex Deymoa7658442014-04-17 15:49:37 -0700162
Alex Deymo85616652015-10-15 18:48:31 -0700163 MutableImageProperties new_props;
164 new_props.target_channel = new_target_channel;
165 new_props.is_powerwash_allowed = is_powerwash_allowed;
Alex Deymoa7658442014-04-17 15:49:37 -0700166
Alex Deymod942f9d2015-11-06 16:11:50 -0800167 if (!StoreMutableImageProperties(system_state_, new_props)) {
168 if (error_message)
169 *error_message = "Error storing the new channel value.";
170 return false;
171 }
Alex Deymo85616652015-10-15 18:48:31 -0700172 mutable_image_props_ = new_props;
Darin Petkov49d91322010-10-25 16:34:58 -0700173 return true;
174}
175
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700176void OmahaRequestParams::UpdateDownloadChannel() {
Alex Deymo85616652015-10-15 18:48:31 -0700177 if (download_channel_ != mutable_image_props_.target_channel) {
178 download_channel_ = mutable_image_props_.target_channel;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700179 LOG(INFO) << "Download channel for this attempt = " << download_channel_;
180 }
181}
182
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700183string OmahaRequestParams::GetMachineType() const {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700184 struct utsname buf;
185 string ret;
186 if (uname(&buf) == 0)
187 ret = buf.machine;
188 return ret;
189}
190
Alex Deymof329b932014-10-30 01:37:48 -0700191bool OmahaRequestParams::IsValidChannel(const string& channel) const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700192 return GetChannelIndex(channel) >= 0;
Darin Petkov49d91322010-10-25 16:34:58 -0700193}
194
Alex Deymof329b932014-10-30 01:37:48 -0700195void OmahaRequestParams::set_root(const string& root) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700196 root_ = root;
Alex Deymo85616652015-10-15 18:48:31 -0700197 test::SetImagePropertiesRootPrefix(root_.c_str());
Darin Petkov49d91322010-10-25 16:34:58 -0700198}
199
Alex Deymof329b932014-10-30 01:37:48 -0700200int OmahaRequestParams::GetChannelIndex(const string& channel) const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700201 for (size_t t = 0; t < arraysize(kChannelsByStability); ++t)
202 if (channel == kChannelsByStability[t])
203 return t;
204
205 return -1;
206}
207
208bool OmahaRequestParams::to_more_stable_channel() const {
Alex Deymo85616652015-10-15 18:48:31 -0700209 int current_channel_index = GetChannelIndex(image_props_.current_channel);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700210 int download_channel_index = GetChannelIndex(download_channel_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700211
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700212 return download_channel_index > current_channel_index;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700213}
214
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700215string OmahaRequestParams::GetAppId() const {
Alex Deymo85616652015-10-15 18:48:31 -0700216 return download_channel_ == "canary-channel" ? image_props_.canary_product_id
217 : image_props_.product_id;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700218}
219
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700220} // namespace chromeos_update_engine