blob: 79d19e8f6ba990222ecffec126fa972e1fc7a01c [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>
hscham00b6aa22020-02-20 12:32:06 +090028#include <base/stl_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070029#include <base/strings/string_util.h>
Alex Deymod942f9d2015-11-06 16:11:50 -080030#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/key_value_store.h>
Alex Deymod942f9d2015-11-06 16:11:50 -080032#include <brillo/strings/string_utils.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020033#include <policy/device_policy.h>
Darin Petkov49d91322010-10-25 16:34:58 -070034
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/constants.h"
36#include "update_engine/common/hardware_interface.h"
37#include "update_engine/common/platform_constants.h"
38#include "update_engine/common/utils.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070039#include "update_engine/system_state.h"
Amin Hassanie8153632020-10-27 15:11:28 -070040#include "update_engine/update_manager/policy.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070041
Darin Petkov49d91322010-10-25 16:34:58 -070042#define CALL_MEMBER_FN(object, member) ((object).*(member))
43
Amin Hassanie8153632020-10-27 15:11:28 -070044using chromeos_update_manager::UpdateCheckParams;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070045using std::string;
46
Darin Petkova4a8a8c2010-07-15 22:21:12 -070047namespace chromeos_update_engine {
48
David Pursell02c18642014-11-06 11:26:11 -080049const char OmahaRequestParams::kOsVersion[] = "Indy";
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020050
Jay Srinivasanae4697c2013-03-18 17:08:08 -070051const char* kChannelsByStability[] = {
52 // This list has to be sorted from least stable to most stable channel.
53 "canary-channel",
54 "dev-channel",
55 "beta-channel",
56 "stable-channel",
57};
Darin Petkov49d91322010-10-25 16:34:58 -070058
Alex Deymo3be05c82015-10-23 11:29:11 -070059OmahaRequestParams::~OmahaRequestParams() {
60 if (!root_.empty())
61 test::SetImagePropertiesRootPrefix(nullptr);
62}
63
Amin Hassanie8153632020-10-27 15:11:28 -070064bool OmahaRequestParams::Init(const string& app_version,
65 const string& update_url,
66 const UpdateCheckParams& params) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070067 LOG(INFO) << "Initializing parameters for this update attempt";
Alex Deymo85616652015-10-15 18:48:31 -070068 image_props_ = LoadImageProperties(system_state_);
69 mutable_image_props_ = LoadMutableImageProperties(system_state_);
70
Saint Chouaff51c22020-07-29 14:14:21 +000071 // Validation check the channel names.
Alex Deymo85616652015-10-15 18:48:31 -070072 if (!IsValidChannel(image_props_.current_channel))
73 image_props_.current_channel = "stable-channel";
74 if (!IsValidChannel(mutable_image_props_.target_channel))
75 mutable_image_props_.target_channel = image_props_.current_channel;
76 UpdateDownloadChannel();
77
78 LOG(INFO) << "Running from channel " << image_props_.current_channel;
79
Alex Deymoac41a822015-09-15 20:52:53 -070080 os_platform_ = constants::kOmahaPlatformName;
Sen Jiangf87e5cd2018-03-06 18:33:39 -080081 if (!image_props_.system_version.empty()) {
Amin Hassanie8153632020-10-27 15:11:28 -070082 if (app_version == "ForcedUpdate") {
83 image_props_.system_version = app_version;
Sen Jiangf87e5cd2018-03-06 18:33:39 -080084 }
Sen Jiang3faf4e72017-06-08 11:34:28 -070085 os_version_ = image_props_.system_version;
Sen Jiangf87e5cd2018-03-06 18:33:39 -080086 } else {
Sen Jiang3faf4e72017-06-08 11:34:28 -070087 os_version_ = OmahaRequestParams::kOsVersion;
Sen Jiangf87e5cd2018-03-06 18:33:39 -080088 }
Amin Hassanie8153632020-10-27 15:11:28 -070089 if (!app_version.empty())
90 image_props_.version = app_version;
Alex Deymo85616652015-10-15 18:48:31 -070091
92 os_sp_ = image_props_.version + "_" + GetMachineType();
Jay Srinivasanae4697c2013-03-18 17:08:08 -070093 app_lang_ = "en-US";
J. Richard Barnette522d36f2013-10-28 17:22:12 -070094 hwid_ = system_state_->hardware()->GetHardwareClass();
Chris Sosac1972482013-04-30 22:31:10 -070095 if (CollectECFWVersions()) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -070096 fw_version_ = system_state_->hardware()->GetFirmwareVersion();
97 ec_version_ = system_state_->hardware()->GetECVersion();
Chris Sosac1972482013-04-30 22:31:10 -070098 }
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -040099 device_requisition_ = system_state_->hardware()->GetDeviceRequisition();
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200100
Alex Deymo85616652015-10-15 18:48:31 -0700101 if (image_props_.current_channel == mutable_image_props_.target_channel) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700102 // deltas are only okay if the /.nodelta file does not exist. if we don't
103 // know (i.e. stat() returns some unexpected error), then err on the side of
104 // caution and say deltas are not okay.
105 struct stat stbuf;
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800106 delta_okay_ =
107 (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && (errno == ENOENT);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200108 } else {
Alex Deymo85616652015-10-15 18:48:31 -0700109 LOG(INFO) << "Disabling deltas as a channel change to "
110 << mutable_image_props_.target_channel
111 << " is pending, with is_powerwash_allowed="
112 << utils::ToString(mutable_image_props_.is_powerwash_allowed);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113 // For now, disable delta updates if the current channel is different from
114 // the channel that we're sending to the update server because such updates
115 // are destined to fail -- the current rootfs hash will be different than
116 // the expected hash due to the different channel in /etc/lsb-release.
117 delta_okay_ = false;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200118 }
119
Amin Hassanie8153632020-10-27 15:11:28 -0700120 if (update_url.empty())
Alex Deymo85616652015-10-15 18:48:31 -0700121 update_url_ = image_props_.omaha_url;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700122 else
Amin Hassanie8153632020-10-27 15:11:28 -0700123 update_url_ = update_url;
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800124
125 // Set the interactive flag accordingly.
Amin Hassanie8153632020-10-27 15:11:28 -0700126 interactive_ = params.interactive;
Xiaochu Liu88d90382018-08-29 16:09:11 -0700127
Andrewe045aef2020-01-08 16:29:22 -0800128 dlc_apps_params_.clear();
Xiaochu Liu88d90382018-08-29 16:09:11 -0700129 // Set false so it will do update by default.
130 is_install_ = false;
Amin Hassanie8153632020-10-27 15:11:28 -0700131
132 target_version_prefix_ = params.target_version_prefix;
133
134 lts_tag_ = params.lts_tag;
135
136 rollback_allowed_ = params.rollback_allowed;
137
138 // Set whether saving data over rollback is requested.
139 rollback_data_save_requested_ = params.rollback_data_save_requested;
140
141 // Set how many milestones of rollback are allowed.
142 rollback_allowed_milestones_ = params.rollback_allowed_milestones;
143
144 // Set the target channel, if one was provided.
145 if (params.target_channel.empty()) {
146 LOG(INFO) << "No target channel mandated by policy.";
147 } else {
148 LOG(INFO) << "Setting target channel as mandated: "
149 << params.target_channel;
150 string error_message;
151 if (!SetTargetChannel(params.target_channel,
152 params.rollback_on_channel_downgrade,
153 &error_message)) {
154 LOG(ERROR) << "Setting the channel failed: " << error_message;
155 }
156
157 // Since this is the beginning of a new attempt, update the download
158 // channel. The download channel won't be updated until the next attempt,
159 // even if target channel changes meanwhile, so that how we'll know if we
160 // should cancel the current download attempt if there's such a change in
161 // target channel.
162 UpdateDownloadChannel();
163 }
164
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700165 return true;
166}
167
David Pursell02c18642014-11-06 11:26:11 -0800168bool OmahaRequestParams::IsUpdateUrlOfficial() const {
Alex Deymoac41a822015-09-15 20:52:53 -0700169 return (update_url_ == constants::kOmahaDefaultAUTestURL ||
Alex Deymo85616652015-10-15 18:48:31 -0700170 update_url_ == image_props_.omaha_url);
David Pursell02c18642014-11-06 11:26:11 -0800171}
172
Chris Sosac1972482013-04-30 22:31:10 -0700173bool OmahaRequestParams::CollectECFWVersions() const {
Thiemo Nagela42496e2018-06-12 15:59:09 +0200174 return base::StartsWith(
175 hwid_, string("PARROT"), base::CompareCase::SENSITIVE) ||
176 base::StartsWith(
177 hwid_, string("SPRING"), base::CompareCase::SENSITIVE) ||
Alex Vakulenkoa3cf75a2016-01-20 07:56:15 -0800178 base::StartsWith(hwid_, string("SNOW"), base::CompareCase::SENSITIVE);
Chris Sosac1972482013-04-30 22:31:10 -0700179}
180
Alex Deymof329b932014-10-30 01:37:48 -0700181bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel,
Alex Deymod942f9d2015-11-06 16:11:50 -0800182 bool is_powerwash_allowed,
183 string* error_message) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700184 LOG(INFO) << "SetTargetChannel called with " << new_target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700185 << ", Is Powerwash Allowed = "
186 << utils::ToString(is_powerwash_allowed)
Alex Deymo85616652015-10-15 18:48:31 -0700187 << ". Current channel = " << image_props_.current_channel
188 << ", existing target channel = "
189 << mutable_image_props_.target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700190 << ", download channel = " << download_channel_;
Sen Jiangce3f7cf2018-01-25 14:07:45 -0800191 if (!IsValidChannel(new_target_channel, error_message)) {
Alex Deymod942f9d2015-11-06 16:11:50 -0800192 return false;
193 }
Alex Deymoa7658442014-04-17 15:49:37 -0700194
Alex Deymo85616652015-10-15 18:48:31 -0700195 MutableImageProperties new_props;
196 new_props.target_channel = new_target_channel;
197 new_props.is_powerwash_allowed = is_powerwash_allowed;
Alex Deymoa7658442014-04-17 15:49:37 -0700198
Alex Deymod942f9d2015-11-06 16:11:50 -0800199 if (!StoreMutableImageProperties(system_state_, new_props)) {
200 if (error_message)
201 *error_message = "Error storing the new channel value.";
202 return false;
203 }
Alex Deymo85616652015-10-15 18:48:31 -0700204 mutable_image_props_ = new_props;
Darin Petkov49d91322010-10-25 16:34:58 -0700205 return true;
206}
207
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700208void OmahaRequestParams::UpdateDownloadChannel() {
Alex Deymo85616652015-10-15 18:48:31 -0700209 if (download_channel_ != mutable_image_props_.target_channel) {
210 download_channel_ = mutable_image_props_.target_channel;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700211 LOG(INFO) << "Download channel for this attempt = " << download_channel_;
212 }
213}
214
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700215string OmahaRequestParams::GetMachineType() const {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700216 struct utsname buf;
217 string ret;
218 if (uname(&buf) == 0)
219 ret = buf.machine;
220 return ret;
221}
222
Sen Jiangce3f7cf2018-01-25 14:07:45 -0800223bool OmahaRequestParams::IsValidChannel(const string& channel,
224 string* error_message) const {
225 if (image_props_.allow_arbitrary_channels) {
226 if (!base::EndsWith(channel, "-channel", base::CompareCase::SENSITIVE)) {
227 if (error_message) {
228 *error_message = base::StringPrintf(
229 "Invalid channel name \"%s\", must ends with -channel.",
230 channel.c_str());
231 }
232 return false;
233 }
234 return true;
235 }
236 if (GetChannelIndex(channel) < 0) {
237 string valid_channels = brillo::string_utils::JoinRange(
238 ", ", std::begin(kChannelsByStability), std::end(kChannelsByStability));
239 if (error_message) {
240 *error_message =
241 base::StringPrintf("Invalid channel name \"%s\", valid names are: %s",
242 channel.c_str(),
243 valid_channels.c_str());
244 }
245 return false;
246 }
247 return true;
Darin Petkov49d91322010-10-25 16:34:58 -0700248}
249
Alex Deymof329b932014-10-30 01:37:48 -0700250void OmahaRequestParams::set_root(const string& root) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700251 root_ = root;
Alex Deymo85616652015-10-15 18:48:31 -0700252 test::SetImagePropertiesRootPrefix(root_.c_str());
Darin Petkov49d91322010-10-25 16:34:58 -0700253}
254
Alex Deymof329b932014-10-30 01:37:48 -0700255int OmahaRequestParams::GetChannelIndex(const string& channel) const {
hscham00b6aa22020-02-20 12:32:06 +0900256 for (size_t t = 0; t < base::size(kChannelsByStability); ++t)
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700257 if (channel == kChannelsByStability[t])
258 return t;
259
260 return -1;
261}
262
Sen Jiang8500d3a2018-02-08 12:04:05 -0800263bool OmahaRequestParams::ToMoreStableChannel() const {
Alex Deymo85616652015-10-15 18:48:31 -0700264 int current_channel_index = GetChannelIndex(image_props_.current_channel);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700265 int download_channel_index = GetChannelIndex(download_channel_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700266
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700267 return download_channel_index > current_channel_index;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700268}
269
Sen Jiang8500d3a2018-02-08 12:04:05 -0800270bool OmahaRequestParams::ShouldPowerwash() const {
271 if (!mutable_image_props_.is_powerwash_allowed)
272 return false;
273 // If arbitrary channels are allowed, always powerwash on channel change.
274 if (image_props_.allow_arbitrary_channels)
275 return image_props_.current_channel != download_channel_;
276 // Otherwise only powerwash if we are moving from less stable (higher version)
277 // to more stable channel (lower version).
278 return ToMoreStableChannel();
279}
280
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700281string OmahaRequestParams::GetAppId() const {
Alex Deymo85616652015-10-15 18:48:31 -0700282 return download_channel_ == "canary-channel" ? image_props_.canary_product_id
283 : image_props_.product_id;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700284}
285
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800286string OmahaRequestParams::GetDlcAppId(const std::string& dlc_id) const {
Andrewe045aef2020-01-08 16:29:22 -0800287 // Create APP ID according to |dlc_id| (sticking the current AppID to the
288 // DLC module ID with an underscode).
289 return GetAppId() + "_" + dlc_id;
290}
291
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800292bool OmahaRequestParams::IsDlcAppId(const std::string& app_id) const {
293 return dlc_apps_params().find(app_id) != dlc_apps_params().end();
294}
295
Jae Hoon Kim7fdfbf12020-04-10 18:15:50 -0700296void OmahaRequestParams::SetDlcNoUpdate(const string& app_id) {
297 auto itr = dlc_apps_params_.find(app_id);
298 if (itr == dlc_apps_params_.end())
299 return;
300 itr->second.updated = false;
301}
302
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700303} // namespace chromeos_update_engine