blob: 4dc2c027438f3ca839dc8dba088d2defd2e9deb3 [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
Sen Jiang740c2fc2018-01-22 17:10:41 -080019#include <fcntl.h>
20
Alex Deymo85616652015-10-15 18:48:31 -070021#include <string>
22
Tom Cherry012aa5b2017-10-10 14:45:09 -070023#include <android-base/properties.h>
Alex Deymo85616652015-10-15 18:48:31 -070024#include <base/logging.h>
Sen Jiangce3f7cf2018-01-25 14:07:45 -080025#include <base/strings/string_util.h>
Sen Jiang740c2fc2018-01-22 17:10:41 -080026#include <bootloader.h>
Alex Deymo85616652015-10-15 18:48:31 -070027#include <brillo/osrelease_reader.h>
Sen Jiangb56fe9f2017-06-16 15:19:35 -070028#include <brillo/strings/string_utils.h>
Alex Deymo85616652015-10-15 18:48:31 -070029
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/boot_control_interface.h"
31#include "update_engine/common/constants.h"
32#include "update_engine/common/platform_constants.h"
33#include "update_engine/common/prefs_interface.h"
Sen Jiang684c9cd2017-10-17 16:26:45 -070034#include "update_engine/common/utils.h"
Alex Deymo85616652015-10-15 18:48:31 -070035#include "update_engine/system_state.h"
Sen Jiang740c2fc2018-01-22 17:10:41 -080036#include "update_engine/utils_android.h"
Alex Deymo85616652015-10-15 18:48:31 -070037
Tom Cherry012aa5b2017-10-10 14:45:09 -070038using android::base::GetProperty;
Sen Jiangb56fe9f2017-06-16 15:19:35 -070039using std::string;
40
Alex Deymo85616652015-10-15 18:48:31 -070041namespace chromeos_update_engine {
42
43namespace {
44
Sen Jiang94a4dec2017-03-28 18:23:35 -070045// Build time properties name used in Android Things.
Alex Deymo85616652015-10-15 18:48:31 -070046const char kProductId[] = "product_id";
47const char kProductVersion[] = "product_version";
Sen Jiang94a4dec2017-03-28 18:23:35 -070048const char kSystemId[] = "system_id";
Sen Jiang983f5782017-02-21 15:46:02 -080049const char kSystemVersion[] = "system_version";
Alex Deymo85616652015-10-15 18:48:31 -070050
Sen Jiang684c9cd2017-10-17 16:26:45 -070051// The path to the product_components file which stores the version of each
52// components in OEM partition.
53const char kProductComponentsPath[] = "/oem/os-release.d/product_components";
54
Sen Jiang740c2fc2018-01-22 17:10:41 -080055// Prefs used to store the powerwash settings.
Alex Deymo85616652015-10-15 18:48:31 -070056const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed";
57
Alex Deymoebf6e122017-03-10 16:12:01 -080058// System properties that identifies the "board".
59const char kPropProductName[] = "ro.product.name";
60const char kPropBuildFingerprint[] = "ro.build.fingerprint";
Sen Jiang1d5d95f2017-05-19 11:33:10 -070061const char kPropBuildType[] = "ro.build.type";
Alex Deymoebf6e122017-03-10 16:12:01 -080062
Sen Jiang740c2fc2018-01-22 17:10:41 -080063// Default channel from factory.prop
64const char kPropDefaultChannel[] = "ro.update.default_channel";
65
Sen Jiangb56fe9f2017-06-16 15:19:35 -070066// A prefix added to the path, used for testing.
67const char* root_prefix = nullptr;
68
69string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
70 const string& key,
71 const string& default_value) {
72 string result;
Alex Deymo85616652015-10-15 18:48:31 -070073 if (osrelease.GetString(key, &result))
74 return result;
75 LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
76 << default_value;
77 return default_value;
78}
79
Sen Jiang740c2fc2018-01-22 17:10:41 -080080// Open misc partition for read or write and output the fd in |out_fd|.
81bool OpenMisc(bool write, int* out_fd) {
82 base::FilePath misc_device;
83 int flags = write ? O_WRONLY | O_SYNC : O_RDONLY;
84 if (root_prefix) {
85 // Use a file for unittest and create one if doesn't exist.
86 misc_device = base::FilePath(root_prefix).Append("misc");
87 if (write)
88 flags |= O_CREAT;
89 } else if (!utils::DeviceForMountPoint("/misc", &misc_device)) {
90 return false;
91 }
92
93 int fd = HANDLE_EINTR(open(misc_device.value().c_str(), flags, 0600));
94 if (fd < 0) {
95 PLOG(ERROR) << "Opening misc failed";
96 return false;
97 }
98 *out_fd = fd;
99 return true;
100}
101
102// The offset and size of the channel field in misc partition.
103constexpr size_t kChannelOffset =
104 BOOTLOADER_MESSAGE_OFFSET_IN_MISC +
105 offsetof(bootloader_message_ab, update_channel);
106constexpr size_t kChannelSize = sizeof(bootloader_message_ab::update_channel);
107
108// Read channel from misc partition to |out_channel|, return false if unable to
109// read misc or no channel is set in misc.
110bool ReadChannelFromMisc(string* out_channel) {
111 int fd;
112 TEST_AND_RETURN_FALSE(OpenMisc(false, &fd));
113 ScopedFdCloser fd_closer(&fd);
114 char channel[kChannelSize] = {0};
115 ssize_t bytes_read = 0;
116 if (!utils::PReadAll(
117 fd, channel, kChannelSize - 1, kChannelOffset, &bytes_read) ||
118 bytes_read != kChannelSize - 1) {
119 PLOG(ERROR) << "Reading update channel from misc failed";
120 return false;
121 }
122 if (channel[0] == '\0') {
123 LOG(INFO) << "No channel set in misc.";
124 return false;
125 }
Sen Jiangce3f7cf2018-01-25 14:07:45 -0800126 if (!base::EndsWith(channel, "-channel", base::CompareCase::SENSITIVE)) {
127 LOG(ERROR) << "Channel " << channel << " doesn't end with -channel.";
128 return false;
129 }
Sen Jiang740c2fc2018-01-22 17:10:41 -0800130 out_channel->assign(channel);
131 return true;
132}
133
134// Write |in_channel| to misc partition, return false if failed to write.
135bool WriteChannelToMisc(const string& in_channel) {
136 int fd;
137 TEST_AND_RETURN_FALSE(OpenMisc(true, &fd));
138 ScopedFdCloser fd_closer(&fd);
139 if (in_channel.size() >= kChannelSize) {
140 LOG(ERROR) << "Channel name is too long: " << in_channel
141 << ", the maximum length is " << kChannelSize - 1;
142 return false;
143 }
144 char channel[kChannelSize] = {0};
145 memcpy(channel, in_channel.data(), in_channel.size());
146 if (!utils::PWriteAll(fd, channel, kChannelSize, kChannelOffset)) {
147 PLOG(ERROR) << "Writing update channel to misc failed";
148 return false;
149 }
150 return true;
151}
152
153string GetTargetChannel() {
154 string channel;
155 if (!ReadChannelFromMisc(&channel))
156 channel = GetProperty(kPropDefaultChannel, "stable-channel");
157 return channel;
158}
Alex Deymo85616652015-10-15 18:48:31 -0700159} // namespace
160
161namespace test {
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700162void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
163 root_prefix = test_root_prefix;
164}
Alex Deymo85616652015-10-15 18:48:31 -0700165} // namespace test
166
167ImageProperties LoadImageProperties(SystemState* system_state) {
168 ImageProperties result;
169
170 brillo::OsReleaseReader osrelease;
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700171 if (root_prefix)
172 osrelease.LoadTestingOnly(base::FilePath(root_prefix));
173 else
174 osrelease.Load();
Sen Jiang94a4dec2017-03-28 18:23:35 -0700175 result.product_id =
176 GetStringWithDefault(osrelease, kProductId, "invalid-product");
177 result.system_id = GetStringWithDefault(
178 osrelease, kSystemId, "developer-boards:brillo-starter-board");
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700179 // Update the system id to match the prefix of product id for testing.
180 string prefix, not_used, system_id;
181 if (brillo::string_utils::SplitAtFirst(
182 result.product_id, ":", &prefix, &not_used, false) &&
183 brillo::string_utils::SplitAtFirst(
184 result.system_id, ":", &not_used, &system_id, false)) {
185 result.system_id = prefix + ":" + system_id;
186 }
Alex Deymo85616652015-10-15 18:48:31 -0700187 result.canary_product_id = result.product_id;
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700188 result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
189 result.system_version =
Sen Jiang94a4dec2017-03-28 18:23:35 -0700190 GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0");
Sen Jiang684c9cd2017-10-17 16:26:45 -0700191 // Can't read it with OsReleaseReader because it has multiple lines.
192 utils::ReadFile(kProductComponentsPath, &result.product_components);
Alex Deymo85616652015-10-15 18:48:31 -0700193
Tom Cherry012aa5b2017-10-10 14:45:09 -0700194 result.board = GetProperty(kPropProductName, "brillo");
195 result.build_fingerprint = GetProperty(kPropBuildFingerprint, "none");
196 result.build_type = GetProperty(kPropBuildType, "");
Sen Jiang1d5d95f2017-05-19 11:33:10 -0700197
Sen Jiang740c2fc2018-01-22 17:10:41 -0800198 // Android doesn't have channel information in system image, we try to read
199 // the channel of current slot from prefs and then fallback to use the
200 // persisted target channel as current channel.
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700201 string current_channel_key =
Alex Deymo85616652015-10-15 18:48:31 -0700202 kPrefsChannelOnSlotPrefix +
203 std::to_string(system_state->boot_control()->GetCurrentSlot());
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700204 string current_channel;
Alex Deymo85616652015-10-15 18:48:31 -0700205 if (!system_state->prefs()->Exists(current_channel_key) ||
206 !system_state->prefs()->GetString(current_channel_key, &current_channel))
Sen Jiang740c2fc2018-01-22 17:10:41 -0800207 current_channel = GetTargetChannel();
Alex Deymo85616652015-10-15 18:48:31 -0700208 result.current_channel = current_channel;
Sen Jiang7f785f52018-01-24 13:31:56 -0800209 result.allow_arbitrary_channels = true;
Alex Deymo85616652015-10-15 18:48:31 -0700210
211 // Brillo only supports the official omaha URL.
212 result.omaha_url = constants::kOmahaDefaultProductionURL;
213
214 return result;
215}
216
217MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
218 MutableImageProperties result;
Sen Jiang740c2fc2018-01-22 17:10:41 -0800219 result.target_channel = GetTargetChannel();
220 if (!system_state->prefs()->GetBoolean(kPrefsImgPropPowerwashAllowed,
221 &result.is_powerwash_allowed)) {
Alex Deymo85616652015-10-15 18:48:31 -0700222 result.is_powerwash_allowed = false;
223 }
224 return result;
225}
226
227bool StoreMutableImageProperties(SystemState* system_state,
228 const MutableImageProperties& properties) {
Sen Jiang740c2fc2018-01-22 17:10:41 -0800229 bool ret = true;
230 if (!WriteChannelToMisc(properties.target_channel))
231 ret = false;
232 if (!system_state->prefs()->SetBoolean(kPrefsImgPropPowerwashAllowed,
233 properties.is_powerwash_allowed))
234 ret = false;
235 return ret;
Alex Deymo85616652015-10-15 18:48:31 -0700236}
237
Amin Hassaniafd8cea2017-12-04 14:20:00 -0800238void LogImageProperties() {
239 // TODO(*): Implement this.
240}
241
Alex Deymo85616652015-10-15 18:48:31 -0700242} // namespace chromeos_update_engine