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_android.cc b/image_properties_android.cc
new file mode 100644
index 0000000..dea6166
--- /dev/null
+++ b/image_properties_android.cc
@@ -0,0 +1,111 @@
+//
+// 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 <base/logging.h>
+#include <brillo/osrelease_reader.h>
+
+#include "update_engine/boot_control_interface.h"
+#include "update_engine/constants.h"
+#include "update_engine/platform_constants.h"
+#include "update_engine/prefs_interface.h"
+#include "update_engine/system_state.h"
+
+namespace chromeos_update_engine {
+
+namespace {
+
+// Build time properties name used in Brillo.
+const char kProductId[] = "product_id";
+const char kProductVersion[] = "product_version";
+
+// Prefs used to store the target channel and powerwash settings.
+const char kPrefsImgPropChannelName[] = "img-prop-channel-name";
+const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed";
+
+std::string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
+ const std::string& key,
+ const std::string& default_value) {
+ std::string result;
+ if (osrelease.GetString(key, &result))
+ return result;
+ LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
+ << default_value;
+ return default_value;
+}
+
+} // namespace
+
+namespace test {
+void SetImagePropertiesRootPrefix(const char* /* test_root_prefix */) {}
+} // namespace test
+
+ImageProperties LoadImageProperties(SystemState* system_state) {
+ ImageProperties result;
+
+ brillo::OsReleaseReader osrelease;
+ osrelease.Load();
+ result.product_id = GetStringWithDefault(
+ osrelease, kProductId, "developer-boards:brillo-starter-board");
+ result.canary_product_id = result.product_id;
+ result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
+
+ result.board = "brillo";
+
+ // Brillo images don't have a channel assigned. We stored the name of the
+ // channel where we got the image from in prefs at the time of the update, so
+ // we use that as the current channel if available. During provisioning, there
+ // is no value assigned, so we default to the "stable-channel".
+ std::string current_channel_key =
+ kPrefsChannelOnSlotPrefix +
+ std::to_string(system_state->boot_control()->GetCurrentSlot());
+ std::string current_channel;
+ if (!system_state->prefs()->Exists(current_channel_key) ||
+ !system_state->prefs()->GetString(current_channel_key, ¤t_channel))
+ current_channel = "stable-channel";
+ result.current_channel = current_channel;
+
+ // Brillo only supports the official omaha URL.
+ result.omaha_url = constants::kOmahaDefaultProductionURL;
+
+ return result;
+}
+
+MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
+ MutableImageProperties result;
+ PrefsInterface* const prefs = system_state->prefs();
+ if (!prefs->GetString(kPrefsImgPropChannelName, &result.target_channel))
+ result.target_channel.clear();
+ if (!prefs->GetBoolean(kPrefsImgPropPowerwashAllowed,
+ &result.is_powerwash_allowed)) {
+ result.is_powerwash_allowed = false;
+ }
+ return result;
+}
+
+bool StoreMutableImageProperties(SystemState* system_state,
+ const MutableImageProperties& properties) {
+ PrefsInterface* const prefs = system_state->prefs();
+ return (
+ prefs->SetString(kPrefsImgPropChannelName, properties.target_channel) &&
+ prefs->SetBoolean(kPrefsImgPropPowerwashAllowed,
+ properties.is_powerwash_allowed));
+}
+
+} // namespace chromeos_update_engine