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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 29 | #include <brillo/key_value_store.h> |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 30 | #include <policy/device_policy.h> |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 31 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 32 | #include "update_engine/constants.h" |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 33 | #include "update_engine/hardware_interface.h" |
Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 34 | #include "update_engine/platform_constants.h" |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 35 | #include "update_engine/system_state.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 36 | #include "update_engine/utils.h" |
| 37 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 38 | #define CALL_MEMBER_FN(object, member) ((object).*(member)) |
| 39 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 40 | using std::map; |
| 41 | using std::string; |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 42 | using std::vector; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 43 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 44 | namespace chromeos_update_engine { |
| 45 | |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 46 | const char OmahaRequestParams::kOsVersion[] = "Indy"; |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 47 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 48 | const 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 Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 55 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 56 | bool OmahaRequestParams::Init(const string& in_app_version, |
| 57 | const string& in_update_url, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 58 | bool in_interactive) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 59 | LOG(INFO) << "Initializing parameters for this update attempt"; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 60 | image_props_ = LoadImageProperties(system_state_); |
| 61 | mutable_image_props_ = LoadMutableImageProperties(system_state_); |
| 62 | |
| 63 | // Sanity check the channel names. |
| 64 | if (!IsValidChannel(image_props_.current_channel)) |
| 65 | image_props_.current_channel = "stable-channel"; |
| 66 | if (!IsValidChannel(mutable_image_props_.target_channel)) |
| 67 | mutable_image_props_.target_channel = image_props_.current_channel; |
| 68 | UpdateDownloadChannel(); |
| 69 | |
| 70 | LOG(INFO) << "Running from channel " << image_props_.current_channel; |
| 71 | |
Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 72 | os_platform_ = constants::kOmahaPlatformName; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 73 | os_version_ = OmahaRequestParams::kOsVersion; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 74 | if (!in_app_version.empty()) |
| 75 | image_props_.version = in_app_version; |
| 76 | |
| 77 | os_sp_ = image_props_.version + "_" + GetMachineType(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 78 | app_lang_ = "en-US"; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 79 | hwid_ = system_state_->hardware()->GetHardwareClass(); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 80 | if (CollectECFWVersions()) { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 81 | fw_version_ = system_state_->hardware()->GetFirmwareVersion(); |
| 82 | ec_version_ = system_state_->hardware()->GetECVersion(); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 83 | } |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 84 | |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 85 | if (image_props_.current_channel == mutable_image_props_.target_channel) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 86 | // deltas are only okay if the /.nodelta file does not exist. if we don't |
| 87 | // know (i.e. stat() returns some unexpected error), then err on the side of |
| 88 | // caution and say deltas are not okay. |
| 89 | struct stat stbuf; |
| 90 | delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && |
| 91 | (errno == ENOENT); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 92 | } else { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 93 | LOG(INFO) << "Disabling deltas as a channel change to " |
| 94 | << mutable_image_props_.target_channel |
| 95 | << " is pending, with is_powerwash_allowed=" |
| 96 | << utils::ToString(mutable_image_props_.is_powerwash_allowed); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 97 | // For now, disable delta updates if the current channel is different from |
| 98 | // the channel that we're sending to the update server because such updates |
| 99 | // are destined to fail -- the current rootfs hash will be different than |
| 100 | // the expected hash due to the different channel in /etc/lsb-release. |
| 101 | delta_okay_ = false; |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 104 | if (in_update_url.empty()) |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 105 | update_url_ = image_props_.omaha_url; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 106 | else |
| 107 | update_url_ = in_update_url; |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 108 | |
| 109 | // Set the interactive flag accordingly. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 110 | interactive_ = in_interactive; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 114 | bool OmahaRequestParams::IsUpdateUrlOfficial() const { |
Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 115 | return (update_url_ == constants::kOmahaDefaultAUTestURL || |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 116 | update_url_ == image_props_.omaha_url); |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 119 | bool OmahaRequestParams::CollectECFWVersions() const { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 120 | return base::StartsWithASCII(hwid_, string("SAMS ALEX"), true) || |
| 121 | base::StartsWithASCII(hwid_, string("BUTTERFLY"), true) || |
| 122 | base::StartsWithASCII(hwid_, string("LUMPY"), true) || |
| 123 | base::StartsWithASCII(hwid_, string("PARROT"), true) || |
| 124 | base::StartsWithASCII(hwid_, string("SPRING"), true) || |
| 125 | base::StartsWithASCII(hwid_, string("SNOW"), true); |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 128 | bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 129 | bool is_powerwash_allowed) { |
| 130 | LOG(INFO) << "SetTargetChannel called with " << new_target_channel |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 131 | << ", Is Powerwash Allowed = " |
| 132 | << utils::ToString(is_powerwash_allowed) |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 133 | << ". Current channel = " << image_props_.current_channel |
| 134 | << ", existing target channel = " |
| 135 | << mutable_image_props_.target_channel |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 136 | << ", download channel = " << download_channel_; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 137 | TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel)); |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 138 | |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 139 | MutableImageProperties new_props; |
| 140 | new_props.target_channel = new_target_channel; |
| 141 | new_props.is_powerwash_allowed = is_powerwash_allowed; |
Alex Deymo | a765844 | 2014-04-17 15:49:37 -0700 | [diff] [blame] | 142 | |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 143 | TEST_AND_RETURN_FALSE(StoreMutableImageProperties(system_state_, new_props)); |
| 144 | mutable_image_props_ = new_props; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 148 | void OmahaRequestParams::UpdateDownloadChannel() { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 149 | if (download_channel_ != mutable_image_props_.target_channel) { |
| 150 | download_channel_ = mutable_image_props_.target_channel; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 151 | LOG(INFO) << "Download channel for this attempt = " << download_channel_; |
| 152 | } |
| 153 | } |
| 154 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 155 | string OmahaRequestParams::GetMachineType() const { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 156 | struct utsname buf; |
| 157 | string ret; |
| 158 | if (uname(&buf) == 0) |
| 159 | ret = buf.machine; |
| 160 | return ret; |
| 161 | } |
| 162 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 163 | bool OmahaRequestParams::IsValidChannel(const string& channel) const { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 164 | return GetChannelIndex(channel) >= 0; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 167 | void OmahaRequestParams::set_root(const string& root) { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 168 | root_ = root; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 169 | test::SetImagePropertiesRootPrefix(root_.c_str()); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 172 | int OmahaRequestParams::GetChannelIndex(const string& channel) const { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 173 | for (size_t t = 0; t < arraysize(kChannelsByStability); ++t) |
| 174 | if (channel == kChannelsByStability[t]) |
| 175 | return t; |
| 176 | |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | bool OmahaRequestParams::to_more_stable_channel() const { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 181 | int current_channel_index = GetChannelIndex(image_props_.current_channel); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 182 | int download_channel_index = GetChannelIndex(download_channel_); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 183 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 184 | return download_channel_index > current_channel_index; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 187 | string OmahaRequestParams::GetAppId() const { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame^] | 188 | return download_channel_ == "canary-channel" ? image_props_.canary_product_id |
| 189 | : image_props_.product_id; |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 192 | } // namespace chromeos_update_engine |