Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 16 | |
| 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 Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 25 | #include <vector> |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 26 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 27 | #include <base/files/file_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 28 | #include <base/strings/string_util.h> |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 29 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 30 | #include <brillo/key_value_store.h> |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 31 | #include <brillo/strings/string_utils.h> |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 32 | #include <policy/device_policy.h> |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 33 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 34 | #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 Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 38 | #include "update_engine/system_state.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 39 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 40 | #define CALL_MEMBER_FN(object, member) ((object).*(member)) |
| 41 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 42 | using std::map; |
| 43 | using std::string; |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 44 | using std::vector; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 45 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 46 | namespace chromeos_update_engine { |
| 47 | |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 48 | const char OmahaRequestParams::kOsVersion[] = "Indy"; |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 49 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 50 | const 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 Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 57 | |
Alex Deymo | 3be05c8 | 2015-10-23 11:29:11 -0700 | [diff] [blame] | 58 | OmahaRequestParams::~OmahaRequestParams() { |
| 59 | if (!root_.empty()) |
| 60 | test::SetImagePropertiesRootPrefix(nullptr); |
| 61 | } |
| 62 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 63 | bool OmahaRequestParams::Init(const string& in_app_version, |
| 64 | const string& in_update_url, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 65 | bool in_interactive) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 66 | LOG(INFO) << "Initializing parameters for this update attempt"; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 67 | 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 Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 79 | os_platform_ = constants::kOmahaPlatformName; |
Sen Jiang | f87e5cd | 2018-03-06 18:33:39 -0800 | [diff] [blame] | 80 | if (!image_props_.system_version.empty()) { |
| 81 | if (in_app_version == "ForcedUpdate") { |
| 82 | image_props_.system_version = in_app_version; |
| 83 | } |
Sen Jiang | 3faf4e7 | 2017-06-08 11:34:28 -0700 | [diff] [blame] | 84 | os_version_ = image_props_.system_version; |
Sen Jiang | f87e5cd | 2018-03-06 18:33:39 -0800 | [diff] [blame] | 85 | } else { |
Sen Jiang | 3faf4e7 | 2017-06-08 11:34:28 -0700 | [diff] [blame] | 86 | os_version_ = OmahaRequestParams::kOsVersion; |
Sen Jiang | f87e5cd | 2018-03-06 18:33:39 -0800 | [diff] [blame] | 87 | } |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 88 | if (!in_app_version.empty()) |
| 89 | image_props_.version = in_app_version; |
| 90 | |
| 91 | os_sp_ = image_props_.version + "_" + GetMachineType(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 92 | app_lang_ = "en-US"; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 93 | hwid_ = system_state_->hardware()->GetHardwareClass(); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 94 | if (CollectECFWVersions()) { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 95 | fw_version_ = system_state_->hardware()->GetFirmwareVersion(); |
| 96 | ec_version_ = system_state_->hardware()->GetECVersion(); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 97 | } |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 98 | |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 99 | if (image_props_.current_channel == mutable_image_props_.target_channel) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 100 | // deltas are only okay if the /.nodelta file does not exist. if we don't |
| 101 | // know (i.e. stat() returns some unexpected error), then err on the side of |
| 102 | // caution and say deltas are not okay. |
| 103 | struct stat stbuf; |
| 104 | delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && |
| 105 | (errno == ENOENT); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 106 | } else { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 107 | LOG(INFO) << "Disabling deltas as a channel change to " |
| 108 | << mutable_image_props_.target_channel |
| 109 | << " is pending, with is_powerwash_allowed=" |
| 110 | << utils::ToString(mutable_image_props_.is_powerwash_allowed); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 111 | // For now, disable delta updates if the current channel is different from |
| 112 | // the channel that we're sending to the update server because such updates |
| 113 | // are destined to fail -- the current rootfs hash will be different than |
| 114 | // the expected hash due to the different channel in /etc/lsb-release. |
| 115 | delta_okay_ = false; |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 118 | if (in_update_url.empty()) |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 119 | update_url_ = image_props_.omaha_url; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 120 | else |
| 121 | update_url_ = in_update_url; |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 122 | |
| 123 | // Set the interactive flag accordingly. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 124 | interactive_ = in_interactive; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 128 | bool OmahaRequestParams::IsUpdateUrlOfficial() const { |
Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 129 | return (update_url_ == constants::kOmahaDefaultAUTestURL || |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 130 | update_url_ == image_props_.omaha_url); |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 133 | bool OmahaRequestParams::CollectECFWVersions() const { |
Thiemo Nagel | a42496e | 2018-06-12 15:59:09 +0200 | [diff] [blame] | 134 | return base::StartsWith( |
| 135 | hwid_, string("PARROT"), base::CompareCase::SENSITIVE) || |
| 136 | base::StartsWith( |
| 137 | hwid_, string("SPRING"), base::CompareCase::SENSITIVE) || |
Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 138 | base::StartsWith(hwid_, string("SNOW"), base::CompareCase::SENSITIVE); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 141 | bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel, |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 142 | bool is_powerwash_allowed, |
| 143 | string* error_message) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 144 | LOG(INFO) << "SetTargetChannel called with " << new_target_channel |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 145 | << ", Is Powerwash Allowed = " |
| 146 | << utils::ToString(is_powerwash_allowed) |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 147 | << ". Current channel = " << image_props_.current_channel |
| 148 | << ", existing target channel = " |
| 149 | << mutable_image_props_.target_channel |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 150 | << ", download channel = " << download_channel_; |
Sen Jiang | ce3f7cf | 2018-01-25 14:07:45 -0800 | [diff] [blame] | 151 | if (!IsValidChannel(new_target_channel, error_message)) { |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 152 | return false; |
| 153 | } |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 154 | |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 155 | MutableImageProperties new_props; |
| 156 | new_props.target_channel = new_target_channel; |
| 157 | new_props.is_powerwash_allowed = is_powerwash_allowed; |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 158 | |
Alex Deymo | d942f9d | 2015-11-06 16:11:50 -0800 | [diff] [blame] | 159 | if (!StoreMutableImageProperties(system_state_, new_props)) { |
| 160 | if (error_message) |
| 161 | *error_message = "Error storing the new channel value."; |
| 162 | return false; |
| 163 | } |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 164 | mutable_image_props_ = new_props; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 168 | void OmahaRequestParams::UpdateDownloadChannel() { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 169 | if (download_channel_ != mutable_image_props_.target_channel) { |
| 170 | download_channel_ = mutable_image_props_.target_channel; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 171 | LOG(INFO) << "Download channel for this attempt = " << download_channel_; |
| 172 | } |
| 173 | } |
| 174 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 175 | string OmahaRequestParams::GetMachineType() const { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 176 | struct utsname buf; |
| 177 | string ret; |
| 178 | if (uname(&buf) == 0) |
| 179 | ret = buf.machine; |
| 180 | return ret; |
| 181 | } |
| 182 | |
Sen Jiang | ce3f7cf | 2018-01-25 14:07:45 -0800 | [diff] [blame] | 183 | bool OmahaRequestParams::IsValidChannel(const string& channel, |
| 184 | string* error_message) const { |
| 185 | if (image_props_.allow_arbitrary_channels) { |
| 186 | if (!base::EndsWith(channel, "-channel", base::CompareCase::SENSITIVE)) { |
| 187 | if (error_message) { |
| 188 | *error_message = base::StringPrintf( |
| 189 | "Invalid channel name \"%s\", must ends with -channel.", |
| 190 | channel.c_str()); |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | if (GetChannelIndex(channel) < 0) { |
| 197 | string valid_channels = brillo::string_utils::JoinRange( |
| 198 | ", ", std::begin(kChannelsByStability), std::end(kChannelsByStability)); |
| 199 | if (error_message) { |
| 200 | *error_message = |
| 201 | base::StringPrintf("Invalid channel name \"%s\", valid names are: %s", |
| 202 | channel.c_str(), |
| 203 | valid_channels.c_str()); |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | return true; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 210 | void OmahaRequestParams::set_root(const string& root) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 211 | root_ = root; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 212 | test::SetImagePropertiesRootPrefix(root_.c_str()); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 215 | int OmahaRequestParams::GetChannelIndex(const string& channel) const { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 216 | for (size_t t = 0; t < arraysize(kChannelsByStability); ++t) |
| 217 | if (channel == kChannelsByStability[t]) |
| 218 | return t; |
| 219 | |
| 220 | return -1; |
| 221 | } |
| 222 | |
Sen Jiang | 8500d3a | 2018-02-08 12:04:05 -0800 | [diff] [blame] | 223 | bool OmahaRequestParams::ToMoreStableChannel() const { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 224 | int current_channel_index = GetChannelIndex(image_props_.current_channel); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 225 | int download_channel_index = GetChannelIndex(download_channel_); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 226 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 227 | return download_channel_index > current_channel_index; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Sen Jiang | 8500d3a | 2018-02-08 12:04:05 -0800 | [diff] [blame] | 230 | bool OmahaRequestParams::ShouldPowerwash() const { |
| 231 | if (!mutable_image_props_.is_powerwash_allowed) |
| 232 | return false; |
| 233 | // If arbitrary channels are allowed, always powerwash on channel change. |
| 234 | if (image_props_.allow_arbitrary_channels) |
| 235 | return image_props_.current_channel != download_channel_; |
| 236 | // Otherwise only powerwash if we are moving from less stable (higher version) |
| 237 | // to more stable channel (lower version). |
| 238 | return ToMoreStableChannel(); |
| 239 | } |
| 240 | |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 241 | string OmahaRequestParams::GetAppId() const { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 242 | return download_channel_ == "canary-channel" ? image_props_.canary_product_id |
| 243 | : image_props_.product_id; |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 246 | } // namespace chromeos_update_engine |