omaha_request_params.cc revision 3be05c82442b2fbab693b6399f64610e8542462b
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 <brillo/key_value_store.h> 30#include <policy/device_policy.h> 31 32#include "update_engine/constants.h" 33#include "update_engine/hardware_interface.h" 34#include "update_engine/platform_constants.h" 35#include "update_engine/system_state.h" 36#include "update_engine/utils.h" 37 38#define CALL_MEMBER_FN(object, member) ((object).*(member)) 39 40using std::map; 41using std::string; 42using std::vector; 43 44namespace chromeos_update_engine { 45 46const char OmahaRequestParams::kOsVersion[] = "Indy"; 47 48const 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}; 55 56OmahaRequestParams::~OmahaRequestParams() { 57 if (!root_.empty()) 58 test::SetImagePropertiesRootPrefix(nullptr); 59} 60 61bool OmahaRequestParams::Init(const string& in_app_version, 62 const string& in_update_url, 63 bool in_interactive) { 64 LOG(INFO) << "Initializing parameters for this update attempt"; 65 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 77 os_platform_ = constants::kOmahaPlatformName; 78 os_version_ = OmahaRequestParams::kOsVersion; 79 if (!in_app_version.empty()) 80 image_props_.version = in_app_version; 81 82 os_sp_ = image_props_.version + "_" + GetMachineType(); 83 app_lang_ = "en-US"; 84 hwid_ = system_state_->hardware()->GetHardwareClass(); 85 if (CollectECFWVersions()) { 86 fw_version_ = system_state_->hardware()->GetFirmwareVersion(); 87 ec_version_ = system_state_->hardware()->GetECVersion(); 88 } 89 90 if (image_props_.current_channel == mutable_image_props_.target_channel) { 91 // 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); 97 } else { 98 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); 102 // 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; 107 } 108 109 if (in_update_url.empty()) 110 update_url_ = image_props_.omaha_url; 111 else 112 update_url_ = in_update_url; 113 114 // Set the interactive flag accordingly. 115 interactive_ = in_interactive; 116 return true; 117} 118 119bool OmahaRequestParams::IsUpdateUrlOfficial() const { 120 return (update_url_ == constants::kOmahaDefaultAUTestURL || 121 update_url_ == image_props_.omaha_url); 122} 123 124bool OmahaRequestParams::CollectECFWVersions() const { 125 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); 131} 132 133bool OmahaRequestParams::SetTargetChannel(const string& new_target_channel, 134 bool is_powerwash_allowed) { 135 LOG(INFO) << "SetTargetChannel called with " << new_target_channel 136 << ", Is Powerwash Allowed = " 137 << utils::ToString(is_powerwash_allowed) 138 << ". Current channel = " << image_props_.current_channel 139 << ", existing target channel = " 140 << mutable_image_props_.target_channel 141 << ", download channel = " << download_channel_; 142 TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel)); 143 144 MutableImageProperties new_props; 145 new_props.target_channel = new_target_channel; 146 new_props.is_powerwash_allowed = is_powerwash_allowed; 147 148 TEST_AND_RETURN_FALSE(StoreMutableImageProperties(system_state_, new_props)); 149 mutable_image_props_ = new_props; 150 return true; 151} 152 153void OmahaRequestParams::UpdateDownloadChannel() { 154 if (download_channel_ != mutable_image_props_.target_channel) { 155 download_channel_ = mutable_image_props_.target_channel; 156 LOG(INFO) << "Download channel for this attempt = " << download_channel_; 157 } 158} 159 160string OmahaRequestParams::GetMachineType() const { 161 struct utsname buf; 162 string ret; 163 if (uname(&buf) == 0) 164 ret = buf.machine; 165 return ret; 166} 167 168bool OmahaRequestParams::IsValidChannel(const string& channel) const { 169 return GetChannelIndex(channel) >= 0; 170} 171 172void OmahaRequestParams::set_root(const string& root) { 173 root_ = root; 174 test::SetImagePropertiesRootPrefix(root_.c_str()); 175} 176 177int OmahaRequestParams::GetChannelIndex(const string& channel) const { 178 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 { 186 int current_channel_index = GetChannelIndex(image_props_.current_channel); 187 int download_channel_index = GetChannelIndex(download_channel_); 188 189 return download_channel_index > current_channel_index; 190} 191 192string OmahaRequestParams::GetAppId() const { 193 return download_channel_ == "canary-channel" ? image_props_.canary_product_id 194 : image_props_.product_id; 195} 196 197} // namespace chromeos_update_engine 198