blob: 7f3884895d2a486b9a0b1951a5710cb15dd7a594 [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
26#include "update_engine/constants.h"
27#include "update_engine/hardware_interface.h"
28#include "update_engine/platform_constants.h"
29#include "update_engine/system_state.h"
30
31namespace {
32
33const char kLsbRelease[] = "/etc/lsb-release";
34
35const char kLsbReleaseAppIdKey[] = "CHROMEOS_RELEASE_APPID";
36const char kLsbReleaseAutoUpdateServerKey[] = "CHROMEOS_AUSERVER";
37const char kLsbReleaseBoardAppIdKey[] = "CHROMEOS_BOARD_APPID";
38const char kLsbReleaseBoardKey[] = "CHROMEOS_RELEASE_BOARD";
39const char kLsbReleaseCanaryAppIdKey[] = "CHROMEOS_CANARY_APPID";
40const char kLsbReleaseIsPowerwashAllowedKey[] = "CHROMEOS_IS_POWERWASH_ALLOWED";
41const char kLsbReleaseUpdateChannelKey[] = "CHROMEOS_RELEASE_TRACK";
42const char kLsbReleaseVersionKey[] = "CHROMEOS_RELEASE_VERSION";
43
44const char kDefaultAppId[] = "{87efface-864d-49a5-9bb3-4b050a7c227a}";
45
46// A prefix added to the path, used for testing.
47const char* root_prefix = "";
48
49std::string GetStringWithDefault(const brillo::KeyValueStore& store,
50 const std::string& key,
51 const std::string& default_value) {
52 std::string result;
53 if (store.GetString(key, &result))
54 return result;
55 LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
56 << default_value;
57 return default_value;
58}
59
60enum class LsbReleaseSource {
61 kSystem,
62 kStateful,
63};
64
65// Loads the lsb-release properties into the key-value |store| reading the file
66// from either the system image or the stateful partition as specified by
67// |source|. The loaded values are added to the store, possibly overriding
68// existing values.
69void LoadLsbRelease(LsbReleaseSource source, brillo::KeyValueStore* store) {
70 std::string path = root_prefix;
71 if (source == LsbReleaseSource::kStateful)
72 path += chromeos_update_engine::kStatefulPartition;
73 store->Load(base::FilePath(path + kLsbRelease));
74}
75
76} // namespace
77
78namespace chromeos_update_engine {
79
80namespace test {
81void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
82 root_prefix = test_root_prefix;
83}
84} // namespace test
85
86ImageProperties LoadImageProperties(SystemState* system_state) {
87 ImageProperties result;
88
89 brillo::KeyValueStore lsb_release;
90 LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release);
91 result.current_channel = GetStringWithDefault(
92 lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel");
93
94 // In dev-mode and unofficial build we can override the image properties set
95 // in the system image with the ones from the stateful partition, except the
96 // channel of the current image.
97 HardwareInterface* const hardware = system_state->hardware();
98 if (!hardware->IsOfficialBuild() || !hardware->IsNormalBootMode())
99 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
100
101 // The release_app_id is used as the default appid, but can be override by
102 // the board appid in the general case or the canary appid for the canary
103 // channel only.
104 std::string release_app_id =
105 GetStringWithDefault(lsb_release, kLsbReleaseAppIdKey, kDefaultAppId);
106
107 result.product_id = GetStringWithDefault(
108 lsb_release, kLsbReleaseBoardAppIdKey, release_app_id);
109 result.canary_product_id = GetStringWithDefault(
110 lsb_release, kLsbReleaseCanaryAppIdKey, release_app_id);
111 result.board = GetStringWithDefault(lsb_release, kLsbReleaseBoardKey, "");
112 result.version = GetStringWithDefault(lsb_release, kLsbReleaseVersionKey, "");
113 result.omaha_url =
114 GetStringWithDefault(lsb_release, kLsbReleaseAutoUpdateServerKey,
115 constants::kOmahaDefaultProductionURL);
116
117 return result;
118}
119
120MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
121 MutableImageProperties result;
122 brillo::KeyValueStore lsb_release;
123 LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release);
124 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
125 result.target_channel = GetStringWithDefault(
126 lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel");
127 if (!lsb_release.GetBoolean(kLsbReleaseIsPowerwashAllowedKey,
128 &result.is_powerwash_allowed))
129 result.is_powerwash_allowed = false;
130 return result;
131}
132
133bool StoreMutableImageProperties(SystemState* system_state,
134 const MutableImageProperties& properties) {
135 brillo::KeyValueStore lsb_release;
136 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
137 lsb_release.SetString(kLsbReleaseUpdateChannelKey, properties.target_channel);
138 lsb_release.SetBoolean(kLsbReleaseIsPowerwashAllowedKey,
139 properties.is_powerwash_allowed);
140
141 base::FilePath path(std::string(root_prefix) + kStatefulPartition +
142 kLsbRelease);
143 if (!base::DirectoryExists(path.DirName()))
144 base::CreateDirectory(path.DirName());
145 return lsb_release.Save(path);
146}
147
148} // namespace chromeos_update_engine