blob: b6c18a6c2bc14554aaff6ad736bc38adf0bf8269 [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::string;
43
Darin Petkova4a8a8c2010-07-15 22:21:12 -070044namespace chromeos_update_engine {
45
David Pursell02c18642014-11-06 11:26:11 -080046const char OmahaRequestParams::kOsVersion[] = "Indy";
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020047
Jay Srinivasanae4697c2013-03-18 17:08:08 -070048const char* kChannelsByStability[] = {
49 // This list has to be sorted from least stable to most stable channel.
50 "canary-channel",
51 "dev-channel",
52 "beta-channel",
53 "stable-channel",
54};
Darin Petkov49d91322010-10-25 16:34:58 -070055
Alex Deymo3be05c82015-10-23 11:29:11 -070056OmahaRequestParams::~OmahaRequestParams() {
57 if (!root_.empty())
58 test::SetImagePropertiesRootPrefix(nullptr);
59}
60
Alex Deymof329b932014-10-30 01:37:48 -070061bool OmahaRequestParams::Init(const string& in_app_version,
62 const string& in_update_url,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070063 bool in_interactive) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070064 LOG(INFO) << "Initializing parameters for this update attempt";
Alex Deymo85616652015-10-15 18:48:31 -070065 image_props_ = LoadImageProperties(system_state_);
66 mutable_image_props_ = LoadMutableImageProperties(system_state_);
67
68 // Sanity check the channel names.
69 if (!IsValidChannel(image_props_.current_channel))
70 image_props_.current_channel = "stable-channel";
71 if (!IsValidChannel(mutable_image_props_.target_channel))
72 mutable_image_props_.target_channel = image_props_.current_channel;
73 UpdateDownloadChannel();
74
75 LOG(INFO) << "Running from channel " << image_props_.current_channel;
76
Alex Deymoac41a822015-09-15 20:52:53 -070077 os_platform_ = constants::kOmahaPlatformName;
Sen Jiangf87e5cd2018-03-06 18:33:39 -080078 if (!image_props_.system_version.empty()) {
79 if (in_app_version == "ForcedUpdate") {
80 image_props_.system_version = in_app_version;
81 }
Sen Jiang3faf4e72017-06-08 11:34:28 -070082 os_version_ = image_props_.system_version;
Sen Jiangf87e5cd2018-03-06 18:33:39 -080083 } else {
Sen Jiang3faf4e72017-06-08 11:34:28 -070084 os_version_ = OmahaRequestParams::kOsVersion;
Sen Jiangf87e5cd2018-03-06 18:33:39 -080085 }
Alex Deymo85616652015-10-15 18:48:31 -070086 if (!in_app_version.empty())
87 image_props_.version = in_app_version;
88
89 os_sp_ = image_props_.version + "_" + GetMachineType();
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 }
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -040096 device_requisition_ = system_state_->hardware()->GetDeviceRequisition();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020097
Alex Deymo85616652015-10-15 18:48:31 -070098 if (image_props_.current_channel == mutable_image_props_.target_channel) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070099 // deltas are only okay if the /.nodelta file does not exist. if we don't
100 // know (i.e. stat() returns some unexpected error), then err on the side of
101 // caution and say deltas are not okay.
102 struct stat stbuf;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800103 delta_okay_ =
104 (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && (errno == ENOENT);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200105 } else {
Alex Deymo85616652015-10-15 18:48:31 -0700106 LOG(INFO) << "Disabling deltas as a channel change to "
107 << mutable_image_props_.target_channel
108 << " is pending, with is_powerwash_allowed="
109 << utils::ToString(mutable_image_props_.is_powerwash_allowed);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700110 // For now, disable delta updates if the current channel is different from
111 // the channel that we're sending to the update server because such updates
112 // are destined to fail -- the current rootfs hash will be different than
113 // the expected hash due to the different channel in /etc/lsb-release.
114 delta_okay_ = false;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200115 }
116
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700117 if (in_update_url.empty())
Alex Deymo85616652015-10-15 18:48:31 -0700118 update_url_ = image_props_.omaha_url;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700119 else
120 update_url_ = in_update_url;
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800121
122 // Set the interactive flag accordingly.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700123 interactive_ = in_interactive;
Xiaochu Liu88d90382018-08-29 16:09:11 -0700124
Andrewe045aef2020-01-08 16:29:22 -0800125 dlc_apps_params_.clear();
Xiaochu Liu88d90382018-08-29 16:09:11 -0700126 // Set false so it will do update by default.
127 is_install_ = false;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700128 return true;
129}
130
David Pursell02c18642014-11-06 11:26:11 -0800131bool OmahaRequestParams::IsUpdateUrlOfficial() const {
Alex Deymoac41a822015-09-15 20:52:53 -0700132 return (update_url_ == constants::kOmahaDefaultAUTestURL ||
Alex Deymo85616652015-10-15 18:48:31 -0700133 update_url_ == image_props_.omaha_url);
David Pursell02c18642014-11-06 11:26:11 -0800134}
135
Chris Sosac1972482013-04-30 22:31:10 -0700136bool OmahaRequestParams::CollectECFWVersions() const {
Thiemo Nagela42496e2018-06-12 15:59:09 +0200137 return base::StartsWith(
138 hwid_, string("PARROT"), base::CompareCase::SENSITIVE) ||
139 base::StartsWith(
140 hwid_, string("SPRING"), base::CompareCase::SENSITIVE) ||
Alex Vakulenkoa3cf75a2016-01-20 07:56:15 -0800141 base::StartsWith(hwid_, string("SNOW"), base::CompareCase::SENSITIVE);
Chris Sosac1972482013-04-30 22:31:10 -0700142}
143
Alex Deymof329b932014-10-30 01:37:48 -0700144bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel,
Alex Deymod942f9d2015-11-06 16:11:50 -0800145 bool is_powerwash_allowed,
146 string* error_message) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700147 LOG(INFO) << "SetTargetChannel called with " << new_target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700148 << ", Is Powerwash Allowed = "
149 << utils::ToString(is_powerwash_allowed)
Alex Deymo85616652015-10-15 18:48:31 -0700150 << ". Current channel = " << image_props_.current_channel
151 << ", existing target channel = "
152 << mutable_image_props_.target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700153 << ", download channel = " << download_channel_;
Sen Jiangce3f7cf2018-01-25 14:07:45 -0800154 if (!IsValidChannel(new_target_channel, error_message)) {
Alex Deymod942f9d2015-11-06 16:11:50 -0800155 return false;
156 }
Alex Deymoa7658442014-04-17 15:49:37 -0700157
Alex Deymo85616652015-10-15 18:48:31 -0700158 MutableImageProperties new_props;
159 new_props.target_channel = new_target_channel;
160 new_props.is_powerwash_allowed = is_powerwash_allowed;
Alex Deymoa7658442014-04-17 15:49:37 -0700161
Alex Deymod942f9d2015-11-06 16:11:50 -0800162 if (!StoreMutableImageProperties(system_state_, new_props)) {
163 if (error_message)
164 *error_message = "Error storing the new channel value.";
165 return false;
166 }
Alex Deymo85616652015-10-15 18:48:31 -0700167 mutable_image_props_ = new_props;
Darin Petkov49d91322010-10-25 16:34:58 -0700168 return true;
169}
170
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700171void OmahaRequestParams::UpdateDownloadChannel() {
Alex Deymo85616652015-10-15 18:48:31 -0700172 if (download_channel_ != mutable_image_props_.target_channel) {
173 download_channel_ = mutable_image_props_.target_channel;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700174 LOG(INFO) << "Download channel for this attempt = " << download_channel_;
175 }
176}
177
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700178string OmahaRequestParams::GetMachineType() const {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700179 struct utsname buf;
180 string ret;
181 if (uname(&buf) == 0)
182 ret = buf.machine;
183 return ret;
184}
185
Sen Jiangce3f7cf2018-01-25 14:07:45 -0800186bool OmahaRequestParams::IsValidChannel(const string& channel,
187 string* error_message) const {
188 if (image_props_.allow_arbitrary_channels) {
189 if (!base::EndsWith(channel, "-channel", base::CompareCase::SENSITIVE)) {
190 if (error_message) {
191 *error_message = base::StringPrintf(
192 "Invalid channel name \"%s\", must ends with -channel.",
193 channel.c_str());
194 }
195 return false;
196 }
197 return true;
198 }
199 if (GetChannelIndex(channel) < 0) {
200 string valid_channels = brillo::string_utils::JoinRange(
201 ", ", std::begin(kChannelsByStability), std::end(kChannelsByStability));
202 if (error_message) {
203 *error_message =
204 base::StringPrintf("Invalid channel name \"%s\", valid names are: %s",
205 channel.c_str(),
206 valid_channels.c_str());
207 }
208 return false;
209 }
210 return true;
Darin Petkov49d91322010-10-25 16:34:58 -0700211}
212
Alex Deymof329b932014-10-30 01:37:48 -0700213void OmahaRequestParams::set_root(const string& root) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700214 root_ = root;
Alex Deymo85616652015-10-15 18:48:31 -0700215 test::SetImagePropertiesRootPrefix(root_.c_str());
Andrewe045aef2020-01-08 16:29:22 -0800216 dlc_prefs_root_ = root + kDlcMetadataRootpath;
Darin Petkov49d91322010-10-25 16:34:58 -0700217}
218
Alex Deymof329b932014-10-30 01:37:48 -0700219int OmahaRequestParams::GetChannelIndex(const string& channel) const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700220 for (size_t t = 0; t < arraysize(kChannelsByStability); ++t)
221 if (channel == kChannelsByStability[t])
222 return t;
223
224 return -1;
225}
226
Sen Jiang8500d3a2018-02-08 12:04:05 -0800227bool OmahaRequestParams::ToMoreStableChannel() const {
Alex Deymo85616652015-10-15 18:48:31 -0700228 int current_channel_index = GetChannelIndex(image_props_.current_channel);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700229 int download_channel_index = GetChannelIndex(download_channel_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700230
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700231 return download_channel_index > current_channel_index;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700232}
233
Sen Jiang8500d3a2018-02-08 12:04:05 -0800234bool OmahaRequestParams::ShouldPowerwash() const {
235 if (!mutable_image_props_.is_powerwash_allowed)
236 return false;
237 // If arbitrary channels are allowed, always powerwash on channel change.
238 if (image_props_.allow_arbitrary_channels)
239 return image_props_.current_channel != download_channel_;
240 // Otherwise only powerwash if we are moving from less stable (higher version)
241 // to more stable channel (lower version).
242 return ToMoreStableChannel();
243}
244
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700245string OmahaRequestParams::GetAppId() const {
Alex Deymo85616652015-10-15 18:48:31 -0700246 return download_channel_ == "canary-channel" ? image_props_.canary_product_id
247 : image_props_.product_id;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700248}
249
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800250string OmahaRequestParams::GetDlcAppId(const std::string& dlc_id) const {
Andrewe045aef2020-01-08 16:29:22 -0800251 // Create APP ID according to |dlc_id| (sticking the current AppID to the
252 // DLC module ID with an underscode).
253 return GetAppId() + "_" + dlc_id;
254}
255
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800256bool OmahaRequestParams::IsDlcAppId(const std::string& app_id) const {
257 return dlc_apps_params().find(app_id) != dlc_apps_params().end();
258}
259
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700260} // namespace chromeos_update_engine