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/strings/string_util.h>
29 #include <base/strings/stringprintf.h>
30 #include <brillo/key_value_store.h>
31 #include <brillo/strings/string_utils.h>
32 #include <policy/device_policy.h>
33
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"
38 #include "update_engine/system_state.h"
39
40 #define CALL_MEMBER_FN(object, member) ((object).*(member))
41
42 using std::map;
43 using std::string;
44 using std::vector;
45
46 namespace chromeos_update_engine {
47
48 const char OmahaRequestParams::kOsVersion[] = "Indy";
49
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 };
57
~OmahaRequestParams()58 OmahaRequestParams::~OmahaRequestParams() {
59 if (!root_.empty())
60 test::SetImagePropertiesRootPrefix(nullptr);
61 }
62
Init(const string & in_app_version,const string & in_update_url,bool in_interactive)63 bool OmahaRequestParams::Init(const string& in_app_version,
64 const string& in_update_url,
65 bool in_interactive) {
66 LOG(INFO) << "Initializing parameters for this update attempt";
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
79 os_platform_ = constants::kOmahaPlatformName;
80 if (!image_props_.system_version.empty())
81 os_version_ = image_props_.system_version;
82 else
83 os_version_ = OmahaRequestParams::kOsVersion;
84 if (!in_app_version.empty())
85 image_props_.version = in_app_version;
86
87 os_sp_ = image_props_.version + "_" + GetMachineType();
88 app_lang_ = "en-US";
89 hwid_ = system_state_->hardware()->GetHardwareClass();
90 if (CollectECFWVersions()) {
91 fw_version_ = system_state_->hardware()->GetFirmwareVersion();
92 ec_version_ = system_state_->hardware()->GetECVersion();
93 }
94
95 if (image_props_.current_channel == mutable_image_props_.target_channel) {
96 // deltas are only okay if the /.nodelta file does not exist. if we don't
97 // know (i.e. stat() returns some unexpected error), then err on the side of
98 // caution and say deltas are not okay.
99 struct stat stbuf;
100 delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
101 (errno == ENOENT);
102 } else {
103 LOG(INFO) << "Disabling deltas as a channel change to "
104 << mutable_image_props_.target_channel
105 << " is pending, with is_powerwash_allowed="
106 << utils::ToString(mutable_image_props_.is_powerwash_allowed);
107 // For now, disable delta updates if the current channel is different from
108 // the channel that we're sending to the update server because such updates
109 // are destined to fail -- the current rootfs hash will be different than
110 // the expected hash due to the different channel in /etc/lsb-release.
111 delta_okay_ = false;
112 }
113
114 if (in_update_url.empty())
115 update_url_ = image_props_.omaha_url;
116 else
117 update_url_ = in_update_url;
118
119 // Set the interactive flag accordingly.
120 interactive_ = in_interactive;
121 return true;
122 }
123
IsUpdateUrlOfficial() const124 bool OmahaRequestParams::IsUpdateUrlOfficial() const {
125 return (update_url_ == constants::kOmahaDefaultAUTestURL ||
126 update_url_ == image_props_.omaha_url);
127 }
128
CollectECFWVersions() const129 bool OmahaRequestParams::CollectECFWVersions() const {
130 return base::StartsWith(hwid_, string("SAMS ALEX"),
131 base::CompareCase::SENSITIVE) ||
132 base::StartsWith(hwid_, string("BUTTERFLY"),
133 base::CompareCase::SENSITIVE) ||
134 base::StartsWith(hwid_, string("LUMPY"),
135 base::CompareCase::SENSITIVE) ||
136 base::StartsWith(hwid_, string("PARROT"),
137 base::CompareCase::SENSITIVE) ||
138 base::StartsWith(hwid_, string("SPRING"),
139 base::CompareCase::SENSITIVE) ||
140 base::StartsWith(hwid_, string("SNOW"), base::CompareCase::SENSITIVE);
141 }
142
SetTargetChannel(const string & new_target_channel,bool is_powerwash_allowed,string * error_message)143 bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel,
144 bool is_powerwash_allowed,
145 string* error_message) {
146 LOG(INFO) << "SetTargetChannel called with " << new_target_channel
147 << ", Is Powerwash Allowed = "
148 << utils::ToString(is_powerwash_allowed)
149 << ". Current channel = " << image_props_.current_channel
150 << ", existing target channel = "
151 << mutable_image_props_.target_channel
152 << ", download channel = " << download_channel_;
153 if (!IsValidChannel(new_target_channel)) {
154 string valid_channels = brillo::string_utils::JoinRange(
155 ", ",
156 std::begin(kChannelsByStability),
157 std::end(kChannelsByStability));
158 if (error_message) {
159 *error_message = base::StringPrintf(
160 "Invalid channel name \"%s\", valid names are: %s",
161 new_target_channel.c_str(), valid_channels.c_str());
162 }
163 return false;
164 }
165
166 MutableImageProperties new_props;
167 new_props.target_channel = new_target_channel;
168 new_props.is_powerwash_allowed = is_powerwash_allowed;
169
170 if (!StoreMutableImageProperties(system_state_, new_props)) {
171 if (error_message)
172 *error_message = "Error storing the new channel value.";
173 return false;
174 }
175 mutable_image_props_ = new_props;
176 return true;
177 }
178
UpdateDownloadChannel()179 void OmahaRequestParams::UpdateDownloadChannel() {
180 if (download_channel_ != mutable_image_props_.target_channel) {
181 download_channel_ = mutable_image_props_.target_channel;
182 LOG(INFO) << "Download channel for this attempt = " << download_channel_;
183 }
184 }
185
GetMachineType() const186 string OmahaRequestParams::GetMachineType() const {
187 struct utsname buf;
188 string ret;
189 if (uname(&buf) == 0)
190 ret = buf.machine;
191 return ret;
192 }
193
IsValidChannel(const string & channel) const194 bool OmahaRequestParams::IsValidChannel(const string& channel) const {
195 return GetChannelIndex(channel) >= 0;
196 }
197
set_root(const string & root)198 void OmahaRequestParams::set_root(const string& root) {
199 root_ = root;
200 test::SetImagePropertiesRootPrefix(root_.c_str());
201 }
202
GetChannelIndex(const string & channel) const203 int OmahaRequestParams::GetChannelIndex(const string& channel) const {
204 for (size_t t = 0; t < arraysize(kChannelsByStability); ++t)
205 if (channel == kChannelsByStability[t])
206 return t;
207
208 return -1;
209 }
210
to_more_stable_channel() const211 bool OmahaRequestParams::to_more_stable_channel() const {
212 int current_channel_index = GetChannelIndex(image_props_.current_channel);
213 int download_channel_index = GetChannelIndex(download_channel_);
214
215 return download_channel_index > current_channel_index;
216 }
217
GetAppId() const218 string OmahaRequestParams::GetAppId() const {
219 return download_channel_ == "canary-channel" ? image_props_.canary_product_id
220 : image_props_.product_id;
221 }
222
223 } // namespace chromeos_update_engine
224