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 //
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>
25 #include <vector>
26
27 #include <base/files/file_util.h>
28 #include <base/stl_util.h>
29 #include <base/strings/string_util.h>
30 #include <base/strings/stringprintf.h>
31 #include <brillo/key_value_store.h>
32 #include <brillo/strings/string_utils.h>
33 #include <policy/device_policy.h>
34
35 #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"
39 #include "update_engine/system_state.h"
40
41 #define CALL_MEMBER_FN(object, member) ((object).*(member))
42
43 using std::string;
44
45 namespace chromeos_update_engine {
46
47 const char OmahaRequestParams::kOsVersion[] = "Indy";
48
49 const char* kChannelsByStability[] = {
50 // This list has to be sorted from least stable to most stable channel.
51 "canary-channel",
52 "dev-channel",
53 "beta-channel",
54 "stable-channel",
55 };
56
~OmahaRequestParams()57 OmahaRequestParams::~OmahaRequestParams() {
58 if (!root_.empty())
59 test::SetImagePropertiesRootPrefix(nullptr);
60 }
61
Init(const string & in_app_version,const string & in_update_url,bool in_interactive)62 bool OmahaRequestParams::Init(const string& in_app_version,
63 const string& in_update_url,
64 bool in_interactive) {
65 LOG(INFO) << "Initializing parameters for this update attempt";
66 image_props_ = LoadImageProperties(system_state_);
67 mutable_image_props_ = LoadMutableImageProperties(system_state_);
68
69 // Validation check the channel names.
70 if (!IsValidChannel(image_props_.current_channel))
71 image_props_.current_channel = "stable-channel";
72 if (!IsValidChannel(mutable_image_props_.target_channel))
73 mutable_image_props_.target_channel = image_props_.current_channel;
74 UpdateDownloadChannel();
75
76 LOG(INFO) << "Running from channel " << image_props_.current_channel;
77
78 os_platform_ = constants::kOmahaPlatformName;
79 if (!image_props_.system_version.empty()) {
80 if (in_app_version == "ForcedUpdate") {
81 image_props_.system_version = in_app_version;
82 }
83 os_version_ = image_props_.system_version;
84 } else {
85 os_version_ = OmahaRequestParams::kOsVersion;
86 }
87 if (!in_app_version.empty())
88 image_props_.version = in_app_version;
89
90 os_sp_ = image_props_.version + "_" + GetMachineType();
91 app_lang_ = "en-US";
92 hwid_ = system_state_->hardware()->GetHardwareClass();
93 if (CollectECFWVersions()) {
94 fw_version_ = system_state_->hardware()->GetFirmwareVersion();
95 ec_version_ = system_state_->hardware()->GetECVersion();
96 }
97 device_requisition_ = system_state_->hardware()->GetDeviceRequisition();
98
99 if (image_props_.current_channel == mutable_image_props_.target_channel) {
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_ =
105 (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && (errno == ENOENT);
106 } else {
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);
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;
116 }
117
118 if (in_update_url.empty())
119 update_url_ = image_props_.omaha_url;
120 else
121 update_url_ = in_update_url;
122
123 // Set the interactive flag accordingly.
124 interactive_ = in_interactive;
125
126 dlc_apps_params_.clear();
127 // Set false so it will do update by default.
128 is_install_ = false;
129 return true;
130 }
131
IsUpdateUrlOfficial() const132 bool OmahaRequestParams::IsUpdateUrlOfficial() const {
133 return (update_url_ == constants::kOmahaDefaultAUTestURL ||
134 update_url_ == image_props_.omaha_url);
135 }
136
CollectECFWVersions() const137 bool OmahaRequestParams::CollectECFWVersions() const {
138 return base::StartsWith(
139 hwid_, string("PARROT"), base::CompareCase::SENSITIVE) ||
140 base::StartsWith(
141 hwid_, string("SPRING"), base::CompareCase::SENSITIVE) ||
142 base::StartsWith(hwid_, string("SNOW"), base::CompareCase::SENSITIVE);
143 }
144
SetTargetChannel(const string & new_target_channel,bool is_powerwash_allowed,string * error_message)145 bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel,
146 bool is_powerwash_allowed,
147 string* error_message) {
148 LOG(INFO) << "SetTargetChannel called with " << new_target_channel
149 << ", Is Powerwash Allowed = "
150 << utils::ToString(is_powerwash_allowed)
151 << ". Current channel = " << image_props_.current_channel
152 << ", existing target channel = "
153 << mutable_image_props_.target_channel
154 << ", download channel = " << download_channel_;
155 if (!IsValidChannel(new_target_channel, error_message)) {
156 return false;
157 }
158
159 MutableImageProperties new_props;
160 new_props.target_channel = new_target_channel;
161 new_props.is_powerwash_allowed = is_powerwash_allowed;
162
163 if (!StoreMutableImageProperties(system_state_, new_props)) {
164 if (error_message)
165 *error_message = "Error storing the new channel value.";
166 return false;
167 }
168 mutable_image_props_ = new_props;
169 return true;
170 }
171
UpdateDownloadChannel()172 void OmahaRequestParams::UpdateDownloadChannel() {
173 if (download_channel_ != mutable_image_props_.target_channel) {
174 download_channel_ = mutable_image_props_.target_channel;
175 LOG(INFO) << "Download channel for this attempt = " << download_channel_;
176 }
177 }
178
GetMachineType() const179 string OmahaRequestParams::GetMachineType() const {
180 struct utsname buf;
181 string ret;
182 if (uname(&buf) == 0)
183 ret = buf.machine;
184 return ret;
185 }
186
IsValidChannel(const string & channel,string * error_message) const187 bool OmahaRequestParams::IsValidChannel(const string& channel,
188 string* error_message) const {
189 if (image_props_.allow_arbitrary_channels) {
190 if (!base::EndsWith(channel, "-channel", base::CompareCase::SENSITIVE)) {
191 if (error_message) {
192 *error_message = base::StringPrintf(
193 "Invalid channel name \"%s\", must ends with -channel.",
194 channel.c_str());
195 }
196 return false;
197 }
198 return true;
199 }
200 if (GetChannelIndex(channel) < 0) {
201 string valid_channels = brillo::string_utils::JoinRange(
202 ", ", std::begin(kChannelsByStability), std::end(kChannelsByStability));
203 if (error_message) {
204 *error_message =
205 base::StringPrintf("Invalid channel name \"%s\", valid names are: %s",
206 channel.c_str(),
207 valid_channels.c_str());
208 }
209 return false;
210 }
211 return true;
212 }
213
set_root(const string & root)214 void OmahaRequestParams::set_root(const string& root) {
215 root_ = root;
216 test::SetImagePropertiesRootPrefix(root_.c_str());
217 }
218
GetChannelIndex(const string & channel) const219 int OmahaRequestParams::GetChannelIndex(const string& channel) const {
220 for (size_t t = 0; t < base::size(kChannelsByStability); ++t)
221 if (channel == kChannelsByStability[t])
222 return t;
223
224 return -1;
225 }
226
ToMoreStableChannel() const227 bool OmahaRequestParams::ToMoreStableChannel() const {
228 int current_channel_index = GetChannelIndex(image_props_.current_channel);
229 int download_channel_index = GetChannelIndex(download_channel_);
230
231 return download_channel_index > current_channel_index;
232 }
233
ShouldPowerwash() const234 bool 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
GetAppId() const245 string OmahaRequestParams::GetAppId() const {
246 return download_channel_ == "canary-channel" ? image_props_.canary_product_id
247 : image_props_.product_id;
248 }
249
GetDlcAppId(const std::string & dlc_id) const250 string OmahaRequestParams::GetDlcAppId(const std::string& dlc_id) const {
251 // 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
IsDlcAppId(const std::string & app_id) const256 bool OmahaRequestParams::IsDlcAppId(const std::string& app_id) const {
257 return dlc_apps_params().find(app_id) != dlc_apps_params().end();
258 }
259
SetDlcNoUpdate(const string & app_id)260 void OmahaRequestParams::SetDlcNoUpdate(const string& app_id) {
261 auto itr = dlc_apps_params_.find(app_id);
262 if (itr == dlc_apps_params_.end())
263 return;
264 itr->second.updated = false;
265 }
266
267 } // namespace chromeos_update_engine
268