blob: e815dbfc066beb51f3ec48212be63d934117995a [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
21#include <base/logging.h>
22#include <brillo/osrelease_reader.h>
Alex Deymoebf6e122017-03-10 16:12:01 -080023#include <cutils/properties.h>
Alex Deymo85616652015-10-15 18:48:31 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/boot_control_interface.h"
26#include "update_engine/common/constants.h"
27#include "update_engine/common/platform_constants.h"
28#include "update_engine/common/prefs_interface.h"
Alex Deymo85616652015-10-15 18:48:31 -070029#include "update_engine/system_state.h"
30
31namespace chromeos_update_engine {
32
33namespace {
34
Sen Jiang94a4dec2017-03-28 18:23:35 -070035// Build time properties name used in Android Things.
Alex Deymo85616652015-10-15 18:48:31 -070036const char kProductId[] = "product_id";
37const char kProductVersion[] = "product_version";
Sen Jiang94a4dec2017-03-28 18:23:35 -070038const char kSystemId[] = "system_id";
Sen Jiang983f5782017-02-21 15:46:02 -080039const char kSystemVersion[] = "system_version";
Alex Deymo85616652015-10-15 18:48:31 -070040
41// Prefs used to store the target channel and powerwash settings.
42const char kPrefsImgPropChannelName[] = "img-prop-channel-name";
43const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed";
44
Alex Deymoebf6e122017-03-10 16:12:01 -080045// System properties that identifies the "board".
46const char kPropProductName[] = "ro.product.name";
47const char kPropBuildFingerprint[] = "ro.build.fingerprint";
Sen Jiang1d5d95f2017-05-19 11:33:10 -070048const char kPropBuildType[] = "ro.build.type";
Alex Deymoebf6e122017-03-10 16:12:01 -080049
Alex Deymo85616652015-10-15 18:48:31 -070050std::string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
51 const std::string& key,
52 const std::string& default_value) {
53 std::string result;
54 if (osrelease.GetString(key, &result))
55 return result;
56 LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
57 << default_value;
58 return default_value;
59}
60
61} // namespace
62
63namespace test {
64void SetImagePropertiesRootPrefix(const char* /* test_root_prefix */) {}
65} // namespace test
66
67ImageProperties LoadImageProperties(SystemState* system_state) {
68 ImageProperties result;
69
70 brillo::OsReleaseReader osrelease;
71 osrelease.Load();
Sen Jiang94a4dec2017-03-28 18:23:35 -070072 result.product_id =
73 GetStringWithDefault(osrelease, kProductId, "invalid-product");
74 result.system_id = GetStringWithDefault(
75 osrelease, kSystemId, "developer-boards:brillo-starter-board");
Alex Deymo85616652015-10-15 18:48:31 -070076 result.canary_product_id = result.product_id;
Sen Jiang983f5782017-02-21 15:46:02 -080077 std::string system_version =
Sen Jiang94a4dec2017-03-28 18:23:35 -070078 GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0");
Sen Jiang983f5782017-02-21 15:46:02 -080079 std::string product_version =
Sen Jiang94a4dec2017-03-28 18:23:35 -070080 GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
81 result.version = product_version;
82 result.system_version = system_version;
Alex Deymo85616652015-10-15 18:48:31 -070083
Alex Deymoebf6e122017-03-10 16:12:01 -080084 char prop[PROPERTY_VALUE_MAX];
85 property_get(kPropProductName, prop, "brillo");
86 result.board = prop;
87
88 property_get(kPropBuildFingerprint, prop, "none");
89 result.build_fingerprint = prop;
Alex Deymo85616652015-10-15 18:48:31 -070090
Sen Jiang1d5d95f2017-05-19 11:33:10 -070091 property_get(kPropBuildType, prop, "");
92 result.build_type = prop;
93
Alex Deymo85616652015-10-15 18:48:31 -070094 // Brillo images don't have a channel assigned. We stored the name of the
95 // channel where we got the image from in prefs at the time of the update, so
96 // we use that as the current channel if available. During provisioning, there
97 // is no value assigned, so we default to the "stable-channel".
98 std::string current_channel_key =
99 kPrefsChannelOnSlotPrefix +
100 std::to_string(system_state->boot_control()->GetCurrentSlot());
101 std::string current_channel;
102 if (!system_state->prefs()->Exists(current_channel_key) ||
103 !system_state->prefs()->GetString(current_channel_key, &current_channel))
104 current_channel = "stable-channel";
105 result.current_channel = current_channel;
106
107 // Brillo only supports the official omaha URL.
108 result.omaha_url = constants::kOmahaDefaultProductionURL;
109
110 return result;
111}
112
113MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
114 MutableImageProperties result;
115 PrefsInterface* const prefs = system_state->prefs();
116 if (!prefs->GetString(kPrefsImgPropChannelName, &result.target_channel))
117 result.target_channel.clear();
118 if (!prefs->GetBoolean(kPrefsImgPropPowerwashAllowed,
119 &result.is_powerwash_allowed)) {
120 result.is_powerwash_allowed = false;
121 }
122 return result;
123}
124
125bool StoreMutableImageProperties(SystemState* system_state,
126 const MutableImageProperties& properties) {
127 PrefsInterface* const prefs = system_state->prefs();
128 return (
129 prefs->SetString(kPrefsImgPropChannelName, properties.target_channel) &&
130 prefs->SetBoolean(kPrefsImgPropPowerwashAllowed,
131 properties.is_powerwash_allowed));
132}
133
134} // namespace chromeos_update_engine