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/image_properties_chromeos.cc b/image_properties_chromeos.cc
new file mode 100644
index 0000000..7f38848
--- /dev/null
+++ b/image_properties_chromeos.cc
@@ -0,0 +1,148 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/image_properties.h"
+
+#include <string>
+#include <vector>
+
+#include <base/files/file_util.h>
+#include <base/logging.h>
+#include <brillo/key_value_store.h>
+
+#include "update_engine/constants.h"
+#include "update_engine/hardware_interface.h"
+#include "update_engine/platform_constants.h"
+#include "update_engine/system_state.h"
+
+namespace {
+
+const char kLsbRelease[] = "/etc/lsb-release";
+
+const char kLsbReleaseAppIdKey[] = "CHROMEOS_RELEASE_APPID";
+const char kLsbReleaseAutoUpdateServerKey[] = "CHROMEOS_AUSERVER";
+const char kLsbReleaseBoardAppIdKey[] = "CHROMEOS_BOARD_APPID";
+const char kLsbReleaseBoardKey[] = "CHROMEOS_RELEASE_BOARD";
+const char kLsbReleaseCanaryAppIdKey[] = "CHROMEOS_CANARY_APPID";
+const char kLsbReleaseIsPowerwashAllowedKey[] = "CHROMEOS_IS_POWERWASH_ALLOWED";
+const char kLsbReleaseUpdateChannelKey[] = "CHROMEOS_RELEASE_TRACK";
+const char kLsbReleaseVersionKey[] = "CHROMEOS_RELEASE_VERSION";
+
+const char kDefaultAppId[] = "{87efface-864d-49a5-9bb3-4b050a7c227a}";
+
+// A prefix added to the path, used for testing.
+const char* root_prefix = "";
+
+std::string GetStringWithDefault(const brillo::KeyValueStore& store,
+ const std::string& key,
+ const std::string& default_value) {
+ std::string result;
+ if (store.GetString(key, &result))
+ return result;
+ LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
+ << default_value;
+ return default_value;
+}
+
+enum class LsbReleaseSource {
+ kSystem,
+ kStateful,
+};
+
+// Loads the lsb-release properties into the key-value |store| reading the file
+// from either the system image or the stateful partition as specified by
+// |source|. The loaded values are added to the store, possibly overriding
+// existing values.
+void LoadLsbRelease(LsbReleaseSource source, brillo::KeyValueStore* store) {
+ std::string path = root_prefix;
+ if (source == LsbReleaseSource::kStateful)
+ path += chromeos_update_engine::kStatefulPartition;
+ store->Load(base::FilePath(path + kLsbRelease));
+}
+
+} // namespace
+
+namespace chromeos_update_engine {
+
+namespace test {
+void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
+ root_prefix = test_root_prefix;
+}
+} // namespace test
+
+ImageProperties LoadImageProperties(SystemState* system_state) {
+ ImageProperties result;
+
+ brillo::KeyValueStore lsb_release;
+ LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release);
+ result.current_channel = GetStringWithDefault(
+ lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel");
+
+ // In dev-mode and unofficial build we can override the image properties set
+ // in the system image with the ones from the stateful partition, except the
+ // channel of the current image.
+ HardwareInterface* const hardware = system_state->hardware();
+ if (!hardware->IsOfficialBuild() || !hardware->IsNormalBootMode())
+ LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
+
+ // The release_app_id is used as the default appid, but can be override by
+ // the board appid in the general case or the canary appid for the canary
+ // channel only.
+ std::string release_app_id =
+ GetStringWithDefault(lsb_release, kLsbReleaseAppIdKey, kDefaultAppId);
+
+ result.product_id = GetStringWithDefault(
+ lsb_release, kLsbReleaseBoardAppIdKey, release_app_id);
+ result.canary_product_id = GetStringWithDefault(
+ lsb_release, kLsbReleaseCanaryAppIdKey, release_app_id);
+ result.board = GetStringWithDefault(lsb_release, kLsbReleaseBoardKey, "");
+ result.version = GetStringWithDefault(lsb_release, kLsbReleaseVersionKey, "");
+ result.omaha_url =
+ GetStringWithDefault(lsb_release, kLsbReleaseAutoUpdateServerKey,
+ constants::kOmahaDefaultProductionURL);
+
+ return result;
+}
+
+MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
+ MutableImageProperties result;
+ brillo::KeyValueStore lsb_release;
+ LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release);
+ LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
+ result.target_channel = GetStringWithDefault(
+ lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel");
+ if (!lsb_release.GetBoolean(kLsbReleaseIsPowerwashAllowedKey,
+ &result.is_powerwash_allowed))
+ result.is_powerwash_allowed = false;
+ return result;
+}
+
+bool StoreMutableImageProperties(SystemState* system_state,
+ const MutableImageProperties& properties) {
+ brillo::KeyValueStore lsb_release;
+ LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
+ lsb_release.SetString(kLsbReleaseUpdateChannelKey, properties.target_channel);
+ lsb_release.SetBoolean(kLsbReleaseIsPowerwashAllowedKey,
+ properties.is_powerwash_allowed);
+
+ base::FilePath path(std::string(root_prefix) + kStatefulPartition +
+ kLsbRelease);
+ if (!base::DirectoryExists(path.DirName()))
+ base::CreateDirectory(path.DirName());
+ return lsb_release.Save(path);
+}
+
+} // namespace chromeos_update_engine