1 //
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>
23
24 #include "update_engine/common/boot_control_interface.h"
25 #include "update_engine/common/constants.h"
26 #include "update_engine/common/platform_constants.h"
27 #include "update_engine/common/prefs_interface.h"
28 #include "update_engine/system_state.h"
29
30 namespace chromeos_update_engine {
31
32 namespace {
33
34 // Build time properties name used in Brillo.
35 const char kProductId[] = "product_id";
36 const char kProductVersion[] = "product_version";
37
38 // Prefs used to store the target channel and powerwash settings.
39 const char kPrefsImgPropChannelName[] = "img-prop-channel-name";
40 const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed";
41
GetStringWithDefault(const brillo::OsReleaseReader & osrelease,const std::string & key,const std::string & default_value)42 std::string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
43 const std::string& key,
44 const std::string& default_value) {
45 std::string result;
46 if (osrelease.GetString(key, &result))
47 return result;
48 LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
49 << default_value;
50 return default_value;
51 }
52
53 } // namespace
54
55 namespace test {
SetImagePropertiesRootPrefix(const char *)56 void SetImagePropertiesRootPrefix(const char* /* test_root_prefix */) {}
57 } // namespace test
58
LoadImageProperties(SystemState * system_state)59 ImageProperties LoadImageProperties(SystemState* system_state) {
60 ImageProperties result;
61
62 brillo::OsReleaseReader osrelease;
63 osrelease.Load();
64 result.product_id = GetStringWithDefault(
65 osrelease, kProductId, "developer-boards:brillo-starter-board");
66 result.canary_product_id = result.product_id;
67 result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
68
69 result.board = "brillo";
70
71 // Brillo images don't have a channel assigned. We stored the name of the
72 // channel where we got the image from in prefs at the time of the update, so
73 // we use that as the current channel if available. During provisioning, there
74 // is no value assigned, so we default to the "stable-channel".
75 std::string current_channel_key =
76 kPrefsChannelOnSlotPrefix +
77 std::to_string(system_state->boot_control()->GetCurrentSlot());
78 std::string current_channel;
79 if (!system_state->prefs()->Exists(current_channel_key) ||
80 !system_state->prefs()->GetString(current_channel_key, ¤t_channel))
81 current_channel = "stable-channel";
82 result.current_channel = current_channel;
83
84 // Brillo only supports the official omaha URL.
85 result.omaha_url = constants::kOmahaDefaultProductionURL;
86
87 return result;
88 }
89
LoadMutableImageProperties(SystemState * system_state)90 MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
91 MutableImageProperties result;
92 PrefsInterface* const prefs = system_state->prefs();
93 if (!prefs->GetString(kPrefsImgPropChannelName, &result.target_channel))
94 result.target_channel.clear();
95 if (!prefs->GetBoolean(kPrefsImgPropPowerwashAllowed,
96 &result.is_powerwash_allowed)) {
97 result.is_powerwash_allowed = false;
98 }
99 return result;
100 }
101
StoreMutableImageProperties(SystemState * system_state,const MutableImageProperties & properties)102 bool StoreMutableImageProperties(SystemState* system_state,
103 const MutableImageProperties& properties) {
104 PrefsInterface* const prefs = system_state->prefs();
105 return (
106 prefs->SetString(kPrefsImgPropChannelName, properties.target_channel) &&
107 prefs->SetBoolean(kPrefsImgPropPowerwashAllowed,
108 properties.is_powerwash_allowed));
109 }
110
111 } // namespace chromeos_update_engine
112