blob: 87c32f010311678ee3a2dce3fe92451cb01e68a9 [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#include <vector>
21
22#include <base/files/file_util.h>
23#include <base/logging.h>
24#include <brillo/key_value_store.h>
25
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/constants.h"
27#include "update_engine/common/hardware_interface.h"
28#include "update_engine/common/platform_constants.h"
Amin Hassaniafd8cea2017-12-04 14:20:00 -080029#include "update_engine/common/utils.h"
Alex Deymo85616652015-10-15 18:48:31 -070030#include "update_engine/system_state.h"
31
32namespace {
33
34const char kLsbRelease[] = "/etc/lsb-release";
35
36const char kLsbReleaseAppIdKey[] = "CHROMEOS_RELEASE_APPID";
37const char kLsbReleaseAutoUpdateServerKey[] = "CHROMEOS_AUSERVER";
38const char kLsbReleaseBoardAppIdKey[] = "CHROMEOS_BOARD_APPID";
39const char kLsbReleaseBoardKey[] = "CHROMEOS_RELEASE_BOARD";
40const char kLsbReleaseCanaryAppIdKey[] = "CHROMEOS_CANARY_APPID";
41const char kLsbReleaseIsPowerwashAllowedKey[] = "CHROMEOS_IS_POWERWASH_ALLOWED";
42const char kLsbReleaseUpdateChannelKey[] = "CHROMEOS_RELEASE_TRACK";
43const char kLsbReleaseVersionKey[] = "CHROMEOS_RELEASE_VERSION";
44
45const char kDefaultAppId[] = "{87efface-864d-49a5-9bb3-4b050a7c227a}";
46
47// A prefix added to the path, used for testing.
Alex Deymo3be05c82015-10-23 11:29:11 -070048const char* root_prefix = nullptr;
Alex Deymo85616652015-10-15 18:48:31 -070049
50std::string GetStringWithDefault(const brillo::KeyValueStore& store,
51 const std::string& key,
52 const std::string& default_value) {
53 std::string result;
54 if (store.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
61enum class LsbReleaseSource {
62 kSystem,
63 kStateful,
64};
65
66// Loads the lsb-release properties into the key-value |store| reading the file
67// from either the system image or the stateful partition as specified by
68// |source|. The loaded values are added to the store, possibly overriding
69// existing values.
70void LoadLsbRelease(LsbReleaseSource source, brillo::KeyValueStore* store) {
Alex Deymo3be05c82015-10-23 11:29:11 -070071 std::string path;
72 if (root_prefix)
73 path = root_prefix;
Alex Deymo85616652015-10-15 18:48:31 -070074 if (source == LsbReleaseSource::kStateful)
75 path += chromeos_update_engine::kStatefulPartition;
76 store->Load(base::FilePath(path + kLsbRelease));
77}
78
79} // namespace
80
81namespace chromeos_update_engine {
82
83namespace test {
84void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
85 root_prefix = test_root_prefix;
86}
87} // namespace test
88
89ImageProperties LoadImageProperties(SystemState* system_state) {
90 ImageProperties result;
91
92 brillo::KeyValueStore lsb_release;
93 LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release);
94 result.current_channel = GetStringWithDefault(
95 lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel");
96
97 // In dev-mode and unofficial build we can override the image properties set
98 // in the system image with the ones from the stateful partition, except the
99 // channel of the current image.
100 HardwareInterface* const hardware = system_state->hardware();
101 if (!hardware->IsOfficialBuild() || !hardware->IsNormalBootMode())
102 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
103
104 // The release_app_id is used as the default appid, but can be override by
105 // the board appid in the general case or the canary appid for the canary
106 // channel only.
107 std::string release_app_id =
108 GetStringWithDefault(lsb_release, kLsbReleaseAppIdKey, kDefaultAppId);
109
110 result.product_id = GetStringWithDefault(
111 lsb_release, kLsbReleaseBoardAppIdKey, release_app_id);
112 result.canary_product_id = GetStringWithDefault(
113 lsb_release, kLsbReleaseCanaryAppIdKey, release_app_id);
114 result.board = GetStringWithDefault(lsb_release, kLsbReleaseBoardKey, "");
115 result.version = GetStringWithDefault(lsb_release, kLsbReleaseVersionKey, "");
116 result.omaha_url =
117 GetStringWithDefault(lsb_release, kLsbReleaseAutoUpdateServerKey,
118 constants::kOmahaDefaultProductionURL);
Alex Deymoebf6e122017-03-10 16:12:01 -0800119 // Build fingerprint not used in Chrome OS.
Alex Deymo558fe6a2017-05-19 13:16:20 -0700120 result.build_fingerprint = "";
Sen Jiang7f785f52018-01-24 13:31:56 -0800121 result.allow_arbitrary_channels = false;
Alex Deymo85616652015-10-15 18:48:31 -0700122
123 return result;
124}
125
126MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
127 MutableImageProperties result;
128 brillo::KeyValueStore lsb_release;
129 LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release);
130 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
131 result.target_channel = GetStringWithDefault(
132 lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel");
133 if (!lsb_release.GetBoolean(kLsbReleaseIsPowerwashAllowedKey,
134 &result.is_powerwash_allowed))
135 result.is_powerwash_allowed = false;
136 return result;
137}
138
139bool StoreMutableImageProperties(SystemState* system_state,
140 const MutableImageProperties& properties) {
141 brillo::KeyValueStore lsb_release;
142 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
143 lsb_release.SetString(kLsbReleaseUpdateChannelKey, properties.target_channel);
144 lsb_release.SetBoolean(kLsbReleaseIsPowerwashAllowedKey,
145 properties.is_powerwash_allowed);
146
Alex Deymo5378f5e2015-11-10 15:02:50 -0800147 std::string root_prefix_str = root_prefix ? root_prefix : "";
148 base::FilePath path(root_prefix_str + kStatefulPartition + kLsbRelease);
Alex Deymo85616652015-10-15 18:48:31 -0700149 if (!base::DirectoryExists(path.DirName()))
150 base::CreateDirectory(path.DirName());
151 return lsb_release.Save(path);
152}
153
Amin Hassaniafd8cea2017-12-04 14:20:00 -0800154void LogImageProperties() {
155 std::string lsb_release;
156 if (utils::ReadFile(kLsbRelease, &lsb_release)) {
157 LOG(INFO) << "lsb-release inside the old rootfs:\n" << lsb_release;
158 }
159
160 std::string stateful_lsb_release;
161 if (utils::ReadFile(std::string(kStatefulPartition) + kLsbRelease,
162 &stateful_lsb_release)) {
163 LOG(INFO) << "stateful lsb-release:\n" << stateful_lsb_release;
164 }
165}
166
Alex Deymo85616652015-10-15 18:48:31 -0700167} // namespace chromeos_update_engine