Read ProductId, ProductVersion and Channel setting in Brillo.

The ProductId, ProductVersion and current/target channels are specified
in a very different way in Brillo compared to Chrome OS. This patch
moves the logic to read and parse /etc/lsb-release in Chrome OS to a
new image_properties module and implements the equivalent module in
Brillo.

This new module replaces some of the logic previously in the
OmahaRequestParams class, both for parsing the read-only properties
from the rootfs and parsing and storing the target channel in the
stateful partition. The Chrome OS version of the new module keeps the
same behavior, except that it falls back to "stable-channel" if the
the current channel is missing in the rootfs (unlikely in Chrome OS).

On the other hand, the new Brillo implementation reads these settings
from the /etc/osrelease file and /etc/osrelease.d directory and doesn't
allow to override those setting during development. The persisted
target_channel and powerwash_allowed settings are stored in Prefs
as many other settings. Finally, since Brillo images don't contain
a channel name baked in the image, we store the channel name where we
got the image from at the time of the update. The first boot after
provisioning will default to "stable-channel".

Bug: 25013069
Test: unittest in Chrome OS; `mm` and tested on a Brillo device.

Change-Id: Icc114b8098af3edaaba715c9c2e3ebe9f417c876
diff --git a/omaha_request_params.cc b/omaha_request_params.cc
index 474f192..9759935 100644
--- a/omaha_request_params.cc
+++ b/omaha_request_params.cc
@@ -43,13 +43,7 @@
 
 namespace chromeos_update_engine {
 
-const char OmahaRequestParams::kAppId[] =
-    "{87efface-864d-49a5-9bb3-4b050a7c227a}";
 const char OmahaRequestParams::kOsVersion[] = "Indy";
-const char OmahaRequestParams::kUpdateChannelKey[] = "CHROMEOS_RELEASE_TRACK";
-const char OmahaRequestParams::kIsPowerwashAllowedKey[] =
-    "CHROMEOS_IS_POWERWASH_ALLOWED";
-const char OmahaRequestParams::kAutoUpdateServerKey[] = "CHROMEOS_AUSERVER";
 
 const char* kChannelsByStability[] = {
     // This list has to be sorted from least stable to most stable channel.
@@ -63,30 +57,24 @@
                               const string& in_update_url,
                               bool in_interactive) {
   LOG(INFO) << "Initializing parameters for this update attempt";
-  InitFromLsbValue();
-  bool stateful_override = !ShouldLockDown();
+  image_props_ = LoadImageProperties(system_state_);
+  mutable_image_props_ = LoadMutableImageProperties(system_state_);
+
+  // Sanity check the channel names.
+  if (!IsValidChannel(image_props_.current_channel))
+    image_props_.current_channel = "stable-channel";
+  if (!IsValidChannel(mutable_image_props_.target_channel))
+    mutable_image_props_.target_channel = image_props_.current_channel;
+  UpdateDownloadChannel();
+
+  LOG(INFO) << "Running from channel " << image_props_.current_channel;
+
   os_platform_ = constants::kOmahaPlatformName;
   os_version_ = OmahaRequestParams::kOsVersion;
-  app_version_ = in_app_version.empty() ?
-      GetLsbValue("CHROMEOS_RELEASE_VERSION", "", nullptr, stateful_override) :
-      in_app_version;
-  os_sp_ = app_version_ + "_" + GetMachineType();
-  os_board_ = GetLsbValue("CHROMEOS_RELEASE_BOARD",
-                          "",
-                          nullptr,
-                          stateful_override);
-  string release_app_id = GetLsbValue("CHROMEOS_RELEASE_APPID",
-                                      OmahaRequestParams::kAppId,
-                                      nullptr,
-                                      stateful_override);
-  board_app_id_ = GetLsbValue("CHROMEOS_BOARD_APPID",
-                              release_app_id,
-                              nullptr,
-                              stateful_override);
-  canary_app_id_ = GetLsbValue("CHROMEOS_CANARY_APPID",
-                               release_app_id,
-                               nullptr,
-                               stateful_override);
+  if (!in_app_version.empty())
+    image_props_.version = in_app_version;
+
+  os_sp_ = image_props_.version + "_" + GetMachineType();
   app_lang_ = "en-US";
   hwid_ = system_state_->hardware()->GetHardwareClass();
   if (CollectECFWVersions()) {
@@ -94,16 +82,18 @@
     ec_version_ = system_state_->hardware()->GetECVersion();
   }
 
-  if (current_channel_ == target_channel_) {
+  if (image_props_.current_channel == mutable_image_props_.target_channel) {
     // deltas are only okay if the /.nodelta file does not exist.  if we don't
     // know (i.e. stat() returns some unexpected error), then err on the side of
     // caution and say deltas are not okay.
     struct stat stbuf;
     delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
                   (errno == ENOENT);
-
   } else {
-    LOG(INFO) << "Disabling deltas as a channel change is pending";
+    LOG(INFO) << "Disabling deltas as a channel change to "
+              << mutable_image_props_.target_channel
+              << " is pending, with is_powerwash_allowed="
+              << utils::ToString(mutable_image_props_.is_powerwash_allowed);
     // For now, disable delta updates if the current channel is different from
     // the channel that we're sending to the update server because such updates
     // are destined to fail -- the current rootfs hash will be different than
@@ -112,9 +102,7 @@
   }
 
   if (in_update_url.empty())
-    update_url_ = GetLsbValue(kAutoUpdateServerKey,
-                              constants::kOmahaDefaultProductionURL,
-                              nullptr, stateful_override);
+    update_url_ = image_props_.omaha_url;
   else
     update_url_ = in_update_url;
 
@@ -125,9 +113,7 @@
 
 bool OmahaRequestParams::IsUpdateUrlOfficial() const {
   return (update_url_ == constants::kOmahaDefaultAUTestURL ||
-          update_url_ == GetLsbValue(kAutoUpdateServerKey,
-                                     constants::kOmahaDefaultProductionURL,
-                                     nullptr, !ShouldLockDown()));
+          update_url_ == image_props_.omaha_url);
 }
 
 bool OmahaRequestParams::CollectECFWVersions() const {
@@ -144,110 +130,28 @@
   LOG(INFO) << "SetTargetChannel called with " << new_target_channel
             << ", Is Powerwash Allowed = "
             << utils::ToString(is_powerwash_allowed)
-            << ". Current channel = " << current_channel_
-            << ", existing target channel = " << target_channel_
+            << ". Current channel = " << image_props_.current_channel
+            << ", existing target channel = "
+            << mutable_image_props_.target_channel
             << ", download channel = " << download_channel_;
   TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel));
-  brillo::KeyValueStore lsb_release;
-  base::FilePath kFile(root_ + kStatefulPartition + "/etc/lsb-release");
 
-  lsb_release.Load(kFile);
-  lsb_release.SetString(kUpdateChannelKey, new_target_channel);
-  lsb_release.SetBoolean(kIsPowerwashAllowedKey, is_powerwash_allowed);
+  MutableImageProperties new_props;
+  new_props.target_channel = new_target_channel;
+  new_props.is_powerwash_allowed = is_powerwash_allowed;
 
-  TEST_AND_RETURN_FALSE(base::CreateDirectory(kFile.DirName()));
-  TEST_AND_RETURN_FALSE(lsb_release.Save(kFile));
-  target_channel_ = new_target_channel;
-  is_powerwash_allowed_ = is_powerwash_allowed;
+  TEST_AND_RETURN_FALSE(StoreMutableImageProperties(system_state_, new_props));
+  mutable_image_props_ = new_props;
   return true;
 }
 
-void OmahaRequestParams::SetTargetChannelFromLsbValue() {
-  string target_channel_new_value = GetLsbValue(
-      kUpdateChannelKey,
-      current_channel_,
-      &chromeos_update_engine::OmahaRequestParams::IsValidChannel,
-      true);  // stateful_override
-
-  if (target_channel_ != target_channel_new_value) {
-    target_channel_ = target_channel_new_value;
-    LOG(INFO) << "Target Channel set to " << target_channel_
-              << " from LSB file";
-  }
-}
-
-void OmahaRequestParams::SetCurrentChannelFromLsbValue() {
-  string current_channel_new_value = GetLsbValue(
-      kUpdateChannelKey,
-      current_channel_,
-      nullptr,  // No need to validate the read-only rootfs channel.
-      false);  // stateful_override is false so we get the current channel.
-
-  if (current_channel_ != current_channel_new_value) {
-    current_channel_ = current_channel_new_value;
-    LOG(INFO) << "Current Channel set to " << current_channel_
-              << " from LSB file in rootfs";
-  }
-}
-
-void OmahaRequestParams::SetIsPowerwashAllowedFromLsbValue() {
-  string is_powerwash_allowed_str = GetLsbValue(
-      kIsPowerwashAllowedKey,
-      "false",
-      nullptr,  // no need to validate
-      true);  // always get it from stateful, as that's the only place it'll be
-  bool is_powerwash_allowed_new_value = (is_powerwash_allowed_str == "true");
-  if (is_powerwash_allowed_ != is_powerwash_allowed_new_value) {
-    is_powerwash_allowed_ = is_powerwash_allowed_new_value;
-    LOG(INFO) << "Powerwash Allowed set to "
-              << utils::ToString(is_powerwash_allowed_)
-              << " from LSB file in stateful";
-  }
-}
-
 void OmahaRequestParams::UpdateDownloadChannel() {
-  if (download_channel_ != target_channel_) {
-    download_channel_ = target_channel_;
+  if (download_channel_ != mutable_image_props_.target_channel) {
+    download_channel_ = mutable_image_props_.target_channel;
     LOG(INFO) << "Download channel for this attempt = " << download_channel_;
   }
 }
 
-void OmahaRequestParams::InitFromLsbValue() {
-  SetCurrentChannelFromLsbValue();
-  SetTargetChannelFromLsbValue();
-  SetIsPowerwashAllowedFromLsbValue();
-  UpdateDownloadChannel();
-}
-
-string OmahaRequestParams::GetLsbValue(const string& key,
-                                       const string& default_value,
-                                       ValueValidator validator,
-                                       bool stateful_override) const {
-  vector<string> files;
-  if (stateful_override) {
-    files.push_back(string(kStatefulPartition) + "/etc/lsb-release");
-  }
-  files.push_back("/etc/lsb-release");
-  for (vector<string>::const_iterator it = files.begin();
-       it != files.end(); ++it) {
-    // TODO(adlr): make sure files checked are owned as root (and all their
-    // parents are recursively, too).
-    brillo::KeyValueStore data;
-    if (!data.Load(base::FilePath(root_ + *it)))
-      continue;
-
-    string value;
-    if (data.GetString(key, &value)) {
-      if (validator && !CALL_MEMBER_FN(*this, validator)(value)) {
-        continue;
-      }
-      return value;
-    }
-  }
-  // not found
-  return default_value;
-}
-
 string OmahaRequestParams::GetMachineType() const {
   struct utsname buf;
   string ret;
@@ -256,26 +160,13 @@
   return ret;
 }
 
-bool OmahaRequestParams::ShouldLockDown() const {
-  if (force_lock_down_) {
-    return forced_lock_down_;
-  }
-  return system_state_->hardware()->IsOfficialBuild() &&
-            system_state_->hardware()->IsNormalBootMode();
-}
-
 bool OmahaRequestParams::IsValidChannel(const string& channel) const {
   return GetChannelIndex(channel) >= 0;
 }
 
 void OmahaRequestParams::set_root(const string& root) {
   root_ = root;
-  InitFromLsbValue();
-}
-
-void OmahaRequestParams::SetLockDown(bool lock) {
-  force_lock_down_ = true;
-  forced_lock_down_ = lock;
+  test::SetImagePropertiesRootPrefix(root_.c_str());
 }
 
 int OmahaRequestParams::GetChannelIndex(const string& channel) const {
@@ -287,14 +178,15 @@
 }
 
 bool OmahaRequestParams::to_more_stable_channel() const {
-  int current_channel_index = GetChannelIndex(current_channel_);
+  int current_channel_index = GetChannelIndex(image_props_.current_channel);
   int download_channel_index = GetChannelIndex(download_channel_);
 
   return download_channel_index > current_channel_index;
 }
 
 string OmahaRequestParams::GetAppId() const {
-  return download_channel_ == "canary-channel" ? canary_app_id_ : board_app_id_;
+  return download_channel_ == "canary-channel" ? image_props_.canary_product_id
+                                               : image_props_.product_id;
 }
 
 }  // namespace chromeos_update_engine