blob: f6fad1cbb6a7e9fc791143c9e8e0ce5a87ad1ade [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>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/strings/string_util.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029#include <brillo/key_value_store.h>
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020030#include <policy/device_policy.h>
Darin Petkov49d91322010-10-25 16:34:58 -070031
Chris Sosabe45bef2013-04-09 18:25:12 -070032#include "update_engine/constants.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070033#include "update_engine/hardware_interface.h"
Alex Deymoac41a822015-09-15 20:52:53 -070034#include "update_engine/platform_constants.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070035#include "update_engine/system_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070036#include "update_engine/utils.h"
37
Darin Petkov49d91322010-10-25 16:34:58 -070038#define CALL_MEMBER_FN(object, member) ((object).*(member))
39
Darin Petkova4a8a8c2010-07-15 22:21:12 -070040using std::map;
41using std::string;
Darin Petkova3df55b2010-11-15 13:33:55 -080042using std::vector;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070043
Darin Petkova4a8a8c2010-07-15 22:21:12 -070044namespace chromeos_update_engine {
45
David Pursell02c18642014-11-06 11:26:11 -080046const char OmahaRequestParams::kOsVersion[] = "Indy";
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020047
Jay Srinivasanae4697c2013-03-18 17:08:08 -070048const 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 Petkov49d91322010-10-25 16:34:58 -070055
Alex Deymo3be05c82015-10-23 11:29:11 -070056OmahaRequestParams::~OmahaRequestParams() {
57 if (!root_.empty())
58 test::SetImagePropertiesRootPrefix(nullptr);
59}
60
Alex Deymof329b932014-10-30 01:37:48 -070061bool OmahaRequestParams::Init(const string& in_app_version,
62 const string& in_update_url,
Jay Srinivasanae4697c2013-03-18 17:08:08 -070063 bool in_interactive) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070064 LOG(INFO) << "Initializing parameters for this update attempt";
Alex Deymo85616652015-10-15 18:48:31 -070065 image_props_ = LoadImageProperties(system_state_);
66 mutable_image_props_ = LoadMutableImageProperties(system_state_);
67
68 // Sanity check the channel names.
69 if (!IsValidChannel(image_props_.current_channel))
70 image_props_.current_channel = "stable-channel";
71 if (!IsValidChannel(mutable_image_props_.target_channel))
72 mutable_image_props_.target_channel = image_props_.current_channel;
73 UpdateDownloadChannel();
74
75 LOG(INFO) << "Running from channel " << image_props_.current_channel;
76
Alex Deymoac41a822015-09-15 20:52:53 -070077 os_platform_ = constants::kOmahaPlatformName;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070078 os_version_ = OmahaRequestParams::kOsVersion;
Alex Deymo85616652015-10-15 18:48:31 -070079 if (!in_app_version.empty())
80 image_props_.version = in_app_version;
81
82 os_sp_ = image_props_.version + "_" + GetMachineType();
Jay Srinivasanae4697c2013-03-18 17:08:08 -070083 app_lang_ = "en-US";
J. Richard Barnette522d36f2013-10-28 17:22:12 -070084 hwid_ = system_state_->hardware()->GetHardwareClass();
Chris Sosac1972482013-04-30 22:31:10 -070085 if (CollectECFWVersions()) {
J. Richard Barnette522d36f2013-10-28 17:22:12 -070086 fw_version_ = system_state_->hardware()->GetFirmwareVersion();
87 ec_version_ = system_state_->hardware()->GetECVersion();
Chris Sosac1972482013-04-30 22:31:10 -070088 }
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020089
Alex Deymo85616652015-10-15 18:48:31 -070090 if (image_props_.current_channel == mutable_image_props_.target_channel) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -070091 // deltas are only okay if the /.nodelta file does not exist. if we don't
92 // know (i.e. stat() returns some unexpected error), then err on the side of
93 // caution and say deltas are not okay.
94 struct stat stbuf;
95 delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
96 (errno == ENOENT);
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +020097 } else {
Alex Deymo85616652015-10-15 18:48:31 -070098 LOG(INFO) << "Disabling deltas as a channel change to "
99 << mutable_image_props_.target_channel
100 << " is pending, with is_powerwash_allowed="
101 << utils::ToString(mutable_image_props_.is_powerwash_allowed);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700102 // For now, disable delta updates if the current channel is different from
103 // the channel that we're sending to the update server because such updates
104 // are destined to fail -- the current rootfs hash will be different than
105 // the expected hash due to the different channel in /etc/lsb-release.
106 delta_okay_ = false;
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200107 }
108
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700109 if (in_update_url.empty())
Alex Deymo85616652015-10-15 18:48:31 -0700110 update_url_ = image_props_.omaha_url;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700111 else
112 update_url_ = in_update_url;
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800113
114 // Set the interactive flag accordingly.
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700115 interactive_ = in_interactive;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700116 return true;
117}
118
David Pursell02c18642014-11-06 11:26:11 -0800119bool OmahaRequestParams::IsUpdateUrlOfficial() const {
Alex Deymoac41a822015-09-15 20:52:53 -0700120 return (update_url_ == constants::kOmahaDefaultAUTestURL ||
Alex Deymo85616652015-10-15 18:48:31 -0700121 update_url_ == image_props_.omaha_url);
David Pursell02c18642014-11-06 11:26:11 -0800122}
123
Chris Sosac1972482013-04-30 22:31:10 -0700124bool OmahaRequestParams::CollectECFWVersions() const {
Alex Vakulenko6a9d3492015-06-15 12:53:22 -0700125 return base::StartsWithASCII(hwid_, string("SAMS ALEX"), true) ||
126 base::StartsWithASCII(hwid_, string("BUTTERFLY"), true) ||
127 base::StartsWithASCII(hwid_, string("LUMPY"), true) ||
128 base::StartsWithASCII(hwid_, string("PARROT"), true) ||
129 base::StartsWithASCII(hwid_, string("SPRING"), true) ||
130 base::StartsWithASCII(hwid_, string("SNOW"), true);
Chris Sosac1972482013-04-30 22:31:10 -0700131}
132
Alex Deymof329b932014-10-30 01:37:48 -0700133bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel,
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700134 bool is_powerwash_allowed) {
135 LOG(INFO) << "SetTargetChannel called with " << new_target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700136 << ", Is Powerwash Allowed = "
137 << utils::ToString(is_powerwash_allowed)
Alex Deymo85616652015-10-15 18:48:31 -0700138 << ". Current channel = " << image_props_.current_channel
139 << ", existing target channel = "
140 << mutable_image_props_.target_channel
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700141 << ", download channel = " << download_channel_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700142 TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel));
Alex Deymoa7658442014-04-17 15:49:37 -0700143
Alex Deymo85616652015-10-15 18:48:31 -0700144 MutableImageProperties new_props;
145 new_props.target_channel = new_target_channel;
146 new_props.is_powerwash_allowed = is_powerwash_allowed;
Alex Deymoa7658442014-04-17 15:49:37 -0700147
Alex Deymo85616652015-10-15 18:48:31 -0700148 TEST_AND_RETURN_FALSE(StoreMutableImageProperties(system_state_, new_props));
149 mutable_image_props_ = new_props;
Darin Petkov49d91322010-10-25 16:34:58 -0700150 return true;
151}
152
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700153void OmahaRequestParams::UpdateDownloadChannel() {
Alex Deymo85616652015-10-15 18:48:31 -0700154 if (download_channel_ != mutable_image_props_.target_channel) {
155 download_channel_ = mutable_image_props_.target_channel;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700156 LOG(INFO) << "Download channel for this attempt = " << download_channel_;
157 }
158}
159
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700160string OmahaRequestParams::GetMachineType() const {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700161 struct utsname buf;
162 string ret;
163 if (uname(&buf) == 0)
164 ret = buf.machine;
165 return ret;
166}
167
Alex Deymof329b932014-10-30 01:37:48 -0700168bool OmahaRequestParams::IsValidChannel(const string& channel) const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700169 return GetChannelIndex(channel) >= 0;
Darin Petkov49d91322010-10-25 16:34:58 -0700170}
171
Alex Deymof329b932014-10-30 01:37:48 -0700172void OmahaRequestParams::set_root(const string& root) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700173 root_ = root;
Alex Deymo85616652015-10-15 18:48:31 -0700174 test::SetImagePropertiesRootPrefix(root_.c_str());
Darin Petkov49d91322010-10-25 16:34:58 -0700175}
176
Alex Deymof329b932014-10-30 01:37:48 -0700177int OmahaRequestParams::GetChannelIndex(const string& channel) const {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700178 for (size_t t = 0; t < arraysize(kChannelsByStability); ++t)
179 if (channel == kChannelsByStability[t])
180 return t;
181
182 return -1;
183}
184
185bool OmahaRequestParams::to_more_stable_channel() const {
Alex Deymo85616652015-10-15 18:48:31 -0700186 int current_channel_index = GetChannelIndex(image_props_.current_channel);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700187 int download_channel_index = GetChannelIndex(download_channel_);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700188
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700189 return download_channel_index > current_channel_index;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700190}
191
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700192string OmahaRequestParams::GetAppId() const {
Alex Deymo85616652015-10-15 18:48:31 -0700193 return download_channel_ == "canary-channel" ? image_props_.canary_product_id
194 : image_props_.product_id;
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700195}
196
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700197} // namespace chromeos_update_engine