blob: 886a6b6ac0a79c197feae247dd77142f836802fa [file] [log] [blame]
Alex Deymo85616652015-10-15 18:48:31 -07001//
2// Copyright (C) 2015 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/image_properties.h"
18
19#include <string>
20
Tom Cherry012aa5b2017-10-10 14:45:09 -070021#include <android-base/properties.h>
Alex Deymo85616652015-10-15 18:48:31 -070022#include <base/logging.h>
23#include <brillo/osrelease_reader.h>
Sen Jiangb56fe9f2017-06-16 15:19:35 -070024#include <brillo/strings/string_utils.h>
Alex Deymo85616652015-10-15 18:48:31 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/boot_control_interface.h"
27#include "update_engine/common/constants.h"
28#include "update_engine/common/platform_constants.h"
29#include "update_engine/common/prefs_interface.h"
Alex Deymo85616652015-10-15 18:48:31 -070030#include "update_engine/system_state.h"
31
Tom Cherry012aa5b2017-10-10 14:45:09 -070032using android::base::GetProperty;
Sen Jiangb56fe9f2017-06-16 15:19:35 -070033using std::string;
34
Alex Deymo85616652015-10-15 18:48:31 -070035namespace chromeos_update_engine {
36
37namespace {
38
Sen Jiang94a4dec2017-03-28 18:23:35 -070039// Build time properties name used in Android Things.
Alex Deymo85616652015-10-15 18:48:31 -070040const char kProductId[] = "product_id";
41const char kProductVersion[] = "product_version";
Sen Jiang94a4dec2017-03-28 18:23:35 -070042const char kSystemId[] = "system_id";
Sen Jiang983f5782017-02-21 15:46:02 -080043const char kSystemVersion[] = "system_version";
Alex Deymo85616652015-10-15 18:48:31 -070044
45// Prefs used to store the target channel and powerwash settings.
46const char kPrefsImgPropChannelName[] = "img-prop-channel-name";
47const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed";
48
Alex Deymoebf6e122017-03-10 16:12:01 -080049// System properties that identifies the "board".
50const char kPropProductName[] = "ro.product.name";
51const char kPropBuildFingerprint[] = "ro.build.fingerprint";
Sen Jiang1d5d95f2017-05-19 11:33:10 -070052const char kPropBuildType[] = "ro.build.type";
Alex Deymoebf6e122017-03-10 16:12:01 -080053
Sen Jiangb56fe9f2017-06-16 15:19:35 -070054// A prefix added to the path, used for testing.
55const char* root_prefix = nullptr;
56
57string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
58 const string& key,
59 const string& default_value) {
60 string result;
Alex Deymo85616652015-10-15 18:48:31 -070061 if (osrelease.GetString(key, &result))
62 return result;
63 LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
64 << default_value;
65 return default_value;
66}
67
68} // namespace
69
70namespace test {
Sen Jiangb56fe9f2017-06-16 15:19:35 -070071void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
72 root_prefix = test_root_prefix;
73}
Alex Deymo85616652015-10-15 18:48:31 -070074} // namespace test
75
76ImageProperties LoadImageProperties(SystemState* system_state) {
77 ImageProperties result;
78
79 brillo::OsReleaseReader osrelease;
Sen Jiangb56fe9f2017-06-16 15:19:35 -070080 if (root_prefix)
81 osrelease.LoadTestingOnly(base::FilePath(root_prefix));
82 else
83 osrelease.Load();
Sen Jiang94a4dec2017-03-28 18:23:35 -070084 result.product_id =
85 GetStringWithDefault(osrelease, kProductId, "invalid-product");
86 result.system_id = GetStringWithDefault(
87 osrelease, kSystemId, "developer-boards:brillo-starter-board");
Sen Jiangb56fe9f2017-06-16 15:19:35 -070088 // Update the system id to match the prefix of product id for testing.
89 string prefix, not_used, system_id;
90 if (brillo::string_utils::SplitAtFirst(
91 result.product_id, ":", &prefix, &not_used, false) &&
92 brillo::string_utils::SplitAtFirst(
93 result.system_id, ":", &not_used, &system_id, false)) {
94 result.system_id = prefix + ":" + system_id;
95 }
Alex Deymo85616652015-10-15 18:48:31 -070096 result.canary_product_id = result.product_id;
Sen Jiangb56fe9f2017-06-16 15:19:35 -070097 result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
98 result.system_version =
Sen Jiang94a4dec2017-03-28 18:23:35 -070099 GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0");
Alex Deymo85616652015-10-15 18:48:31 -0700100
Tom Cherry012aa5b2017-10-10 14:45:09 -0700101 result.board = GetProperty(kPropProductName, "brillo");
102 result.build_fingerprint = GetProperty(kPropBuildFingerprint, "none");
103 result.build_type = GetProperty(kPropBuildType, "");
Sen Jiang1d5d95f2017-05-19 11:33:10 -0700104
Alex Deymo85616652015-10-15 18:48:31 -0700105 // Brillo images don't have a channel assigned. We stored the name of the
106 // channel where we got the image from in prefs at the time of the update, so
107 // we use that as the current channel if available. During provisioning, there
108 // is no value assigned, so we default to the "stable-channel".
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700109 string current_channel_key =
Alex Deymo85616652015-10-15 18:48:31 -0700110 kPrefsChannelOnSlotPrefix +
111 std::to_string(system_state->boot_control()->GetCurrentSlot());
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700112 string current_channel;
Alex Deymo85616652015-10-15 18:48:31 -0700113 if (!system_state->prefs()->Exists(current_channel_key) ||
114 !system_state->prefs()->GetString(current_channel_key, &current_channel))
115 current_channel = "stable-channel";
116 result.current_channel = current_channel;
117
118 // Brillo only supports the official omaha URL.
119 result.omaha_url = constants::kOmahaDefaultProductionURL;
120
121 return result;
122}
123
124MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
125 MutableImageProperties result;
126 PrefsInterface* const prefs = system_state->prefs();
127 if (!prefs->GetString(kPrefsImgPropChannelName, &result.target_channel))
128 result.target_channel.clear();
129 if (!prefs->GetBoolean(kPrefsImgPropPowerwashAllowed,
130 &result.is_powerwash_allowed)) {
131 result.is_powerwash_allowed = false;
132 }
133 return result;
134}
135
136bool StoreMutableImageProperties(SystemState* system_state,
137 const MutableImageProperties& properties) {
138 PrefsInterface* const prefs = system_state->prefs();
139 return (
140 prefs->SetString(kPrefsImgPropChannelName, properties.target_channel) &&
141 prefs->SetBoolean(kPrefsImgPropPowerwashAllowed,
142 properties.is_powerwash_allowed));
143}
144
145} // namespace chromeos_update_engine