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