omaha_request_params.cc revision 63137e5ded659c02eb70d59ef38f99aecac4d076
1// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/omaha_request_params.h"
6
7#include <errno.h>
8#include <fcntl.h>
9#include <sys/utsname.h>
10
11#include <map>
12#include <string>
13#include <vector>
14
15#include <base/file_util.h>
16#include <base/string_util.h>
17#include <policy/device_policy.h>
18
19#include "update_engine/constants.h"
20#include "update_engine/simple_key_value_store.h"
21#include "update_engine/system_state.h"
22#include "update_engine/utils.h"
23
24#define CALL_MEMBER_FN(object, member) ((object).*(member))
25
26using std::map;
27using std::string;
28using std::vector;
29
30namespace chromeos_update_engine {
31
32const char* const OmahaRequestParams::kAppId(
33    "{87efface-864d-49a5-9bb3-4b050a7c227a}");
34const char* const OmahaRequestParams::kOsPlatform("Chrome OS");
35const char* const OmahaRequestParams::kOsVersion("Indy");
36const char* const kProductionOmahaUrl(
37    "https://tools.google.com/service/update2");
38
39const char* const OmahaRequestParams::kUpdateChannelKey(
40    "CHROMEOS_RELEASE_TRACK");
41const char* const OmahaRequestParams::kIsPowerwashAllowedKey(
42    "CHROMEOS_IS_POWERWASH_ALLOWED");
43
44const char* kChannelsByStability[] = {
45    // This list has to be sorted from least stable to most stable channel.
46    "canary-channel",
47    "dev-channel",
48    "beta-channel",
49    "stable-channel",
50};
51
52bool OmahaRequestParams::Init(const std::string& in_app_version,
53                              const std::string& in_update_url,
54                              bool in_interactive) {
55  LOG(INFO) << "Initializing parameters for this update attempt";
56  InitFromLsbValue();
57  bool stateful_override = !ShouldLockDown();
58  os_platform_ = OmahaRequestParams::kOsPlatform;
59  os_version_ = OmahaRequestParams::kOsVersion;
60  app_version_ = in_app_version.empty() ?
61      GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL, stateful_override) :
62      in_app_version;
63  os_sp_ = app_version_ + "_" + GetMachineType();
64  os_board_ = GetLsbValue("CHROMEOS_RELEASE_BOARD",
65                          "",
66                          NULL,
67                          stateful_override);
68  string release_app_id = GetLsbValue("CHROMEOS_RELEASE_APPID",
69                                      OmahaRequestParams::kAppId,
70                                      NULL,
71                                      stateful_override);
72  board_app_id_ = GetLsbValue("CHROMEOS_BOARD_APPID",
73                              release_app_id,
74                              NULL,
75                              stateful_override);
76  canary_app_id_ = GetLsbValue("CHROMEOS_CANARY_APPID",
77                               release_app_id,
78                               NULL,
79                               stateful_override);
80  app_lang_ = "en-US";
81  hwid_ = utils::GetHardwareClass();
82  if (CollectECFWVersions()) {
83    fw_version_ = utils::GetFirmwareVersion();
84    ec_version_ = utils::GetECVersion();
85  }
86
87  if (current_channel_ == target_channel_) {
88    // deltas are only okay if the /.nodelta file does not exist.  if we don't
89    // know (i.e. stat() returns some unexpected error), then err on the side of
90    // caution and say deltas are not okay.
91    struct stat stbuf;
92    delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
93                  (errno == ENOENT);
94
95  } else {
96    LOG(INFO) << "Disabling deltas as a channel change is pending";
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;
102  }
103
104  if (in_update_url.empty())
105    update_url_ = GetLsbValue("CHROMEOS_AUSERVER", kProductionOmahaUrl, NULL,
106                              stateful_override);
107  else
108    update_url_ = in_update_url;
109
110  // Set the interactive flag accordingly.
111  interactive_ = in_interactive;
112  return true;
113}
114
115bool OmahaRequestParams::CollectECFWVersions() const {
116  return (
117      StartsWithASCII(hwid_, string("SAMS ALEX"), true) ||
118      StartsWithASCII(hwid_, string("BUTTERFLY"), true) ||
119      StartsWithASCII(hwid_, string("LUMPY"), true) ||
120      StartsWithASCII(hwid_, string("PARROT"), true) ||
121      StartsWithASCII(hwid_, string("SPRING"), true) ||
122      StartsWithASCII(hwid_, string("SNOW"), true)
123  );
124}
125
126bool OmahaRequestParams::SetTargetChannel(const std::string& new_target_channel,
127                                          bool is_powerwash_allowed) {
128  LOG(INFO) << "SetTargetChannel called with " << new_target_channel
129            << ", Is Powerwash Allowed = "
130            << utils::ToString(is_powerwash_allowed)
131            << ". Current channel = " << current_channel_
132            << ", existing target channel = " << target_channel_
133            << ", download channel = " << download_channel_;
134  TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel));
135  FilePath kFile(root_ + kStatefulPartition + "/etc/lsb-release");
136  string file_data;
137  map<string, string> data;
138  if (file_util::ReadFileToString(kFile, &file_data)) {
139    data = simple_key_value_store::ParseString(file_data);
140  }
141  data[kUpdateChannelKey] = new_target_channel;
142  data[kIsPowerwashAllowedKey] = is_powerwash_allowed ? "true" : "false";
143  file_data = simple_key_value_store::AssembleString(data);
144  TEST_AND_RETURN_FALSE(file_util::CreateDirectory(kFile.DirName()));
145  TEST_AND_RETURN_FALSE(
146      file_util::WriteFile(kFile, file_data.data(), file_data.size()) ==
147      static_cast<int>(file_data.size()));
148  target_channel_ = new_target_channel;
149  is_powerwash_allowed_ = is_powerwash_allowed;
150  return true;
151}
152
153void OmahaRequestParams::SetTargetChannelFromLsbValue() {
154  string target_channel_new_value = GetLsbValue(
155      kUpdateChannelKey,
156      current_channel_,
157      &chromeos_update_engine::OmahaRequestParams::IsValidChannel,
158      true);  // stateful_override
159
160  if (target_channel_ != target_channel_new_value) {
161    target_channel_ = target_channel_new_value;
162    LOG(INFO) << "Target Channel set to " << target_channel_
163              << " from LSB file";
164  }
165}
166
167void OmahaRequestParams::SetCurrentChannelFromLsbValue() {
168  string current_channel_new_value = GetLsbValue(
169      kUpdateChannelKey,
170      current_channel_,
171      NULL,  // No need to validate the read-only rootfs channel.
172      false);  // stateful_override is false so we get the current channel.
173
174  if (current_channel_ != current_channel_new_value) {
175    current_channel_ = current_channel_new_value;
176    LOG(INFO) << "Current Channel set to " << current_channel_
177              << " from LSB file in rootfs";
178  }
179}
180
181void OmahaRequestParams::SetIsPowerwashAllowedFromLsbValue() {
182  string is_powerwash_allowed_str = GetLsbValue(
183      kIsPowerwashAllowedKey,
184      "false",
185      NULL, // no need to validate
186      true); // always get it from stateful, as that's the only place it'll be
187  bool is_powerwash_allowed_new_value = (is_powerwash_allowed_str == "true");
188  if (is_powerwash_allowed_ != is_powerwash_allowed_new_value) {
189    is_powerwash_allowed_ = is_powerwash_allowed_new_value;
190    LOG(INFO) << "Powerwash Allowed set to "
191              << utils::ToString(is_powerwash_allowed_)
192              << " from LSB file in stateful";
193  }
194}
195
196void OmahaRequestParams::UpdateDownloadChannel() {
197  if (download_channel_ != target_channel_) {
198    download_channel_ = target_channel_;
199    LOG(INFO) << "Download channel for this attempt = " << download_channel_;
200  }
201}
202
203void OmahaRequestParams::InitFromLsbValue() {
204  SetCurrentChannelFromLsbValue();
205  SetTargetChannelFromLsbValue();
206  SetIsPowerwashAllowedFromLsbValue();
207  UpdateDownloadChannel();
208}
209
210string OmahaRequestParams::GetLsbValue(const string& key,
211                                       const string& default_value,
212                                       ValueValidator validator,
213                                       bool stateful_override) const {
214  vector<string> files;
215  if (stateful_override) {
216    files.push_back(string(kStatefulPartition) + "/etc/lsb-release");
217  }
218  files.push_back("/etc/lsb-release");
219  for (vector<string>::const_iterator it = files.begin();
220       it != files.end(); ++it) {
221    // TODO(adlr): make sure files checked are owned as root (and all their
222    // parents are recursively, too).
223    string file_data;
224    if (!utils::ReadFile(root_ + *it, &file_data))
225      continue;
226
227    map<string, string> data = simple_key_value_store::ParseString(file_data);
228    if (utils::MapContainsKey(data, key)) {
229      const string& value = data[key];
230      if (validator && !CALL_MEMBER_FN(*this, validator)(value)) {
231        continue;
232      }
233      return value;
234    }
235  }
236  // not found
237  return default_value;
238}
239
240string OmahaRequestParams::GetMachineType() const {
241  struct utsname buf;
242  string ret;
243  if (uname(&buf) == 0)
244    ret = buf.machine;
245  return ret;
246}
247
248bool OmahaRequestParams::ShouldLockDown() const {
249  if (force_lock_down_) {
250    return forced_lock_down_;
251  }
252  return utils::IsOfficialBuild() && utils::IsNormalBootMode();
253}
254
255bool OmahaRequestParams::IsValidChannel(const std::string& channel) const {
256  return GetChannelIndex(channel) >= 0;
257}
258
259void OmahaRequestParams::set_root(const std::string& root) {
260  root_ = root;
261  InitFromLsbValue();
262}
263
264void OmahaRequestParams::SetLockDown(bool lock) {
265  force_lock_down_ = true;
266  forced_lock_down_ = lock;
267}
268
269int OmahaRequestParams::GetChannelIndex(const std::string& channel) const {
270  for (size_t t = 0; t < arraysize(kChannelsByStability); ++t)
271    if (channel == kChannelsByStability[t])
272      return t;
273
274  return -1;
275}
276
277bool OmahaRequestParams::to_more_stable_channel() const {
278  int current_channel_index = GetChannelIndex(current_channel_);
279  int download_channel_index = GetChannelIndex(download_channel_);
280
281  return download_channel_index > current_channel_index;
282}
283
284string OmahaRequestParams::GetAppId() const {
285  return download_channel_ == "canary-channel" ? canary_app_id_ : board_app_id_;
286}
287
288}  // namespace chromeos_update_engine
289