blob: 2d418b3d9b4215bb5c806bdadb81f1a972e1da68 [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>
Tao Bao304680c2018-03-31 10:36:52 -070026#include <bootloader_message/bootloader_message.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"
36
Tom Cherry012aa5b2017-10-10 14:45:09 -070037using android::base::GetProperty;
Sen Jiangb56fe9f2017-06-16 15:19:35 -070038using std::string;
39
Alex Deymo85616652015-10-15 18:48:31 -070040namespace chromeos_update_engine {
41
42namespace {
43
Sen Jiang94a4dec2017-03-28 18:23:35 -070044// Build time properties name used in Android Things.
Alex Deymo85616652015-10-15 18:48:31 -070045const char kProductId[] = "product_id";
46const char kProductVersion[] = "product_version";
Sen Jiang94a4dec2017-03-28 18:23:35 -070047const char kSystemId[] = "system_id";
Sen Jiang983f5782017-02-21 15:46:02 -080048const char kSystemVersion[] = "system_version";
Alex Deymo85616652015-10-15 18:48:31 -070049
Sen Jiang684c9cd2017-10-17 16:26:45 -070050// The path to the product_components file which stores the version of each
51// components in OEM partition.
52const char kProductComponentsPath[] = "/oem/os-release.d/product_components";
53
Sen Jiang740c2fc2018-01-22 17:10:41 -080054// Prefs used to store the powerwash settings.
Alex Deymo85616652015-10-15 18:48:31 -070055const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed";
56
Alex Deymoebf6e122017-03-10 16:12:01 -080057// System properties that identifies the "board".
58const char kPropProductName[] = "ro.product.name";
59const char kPropBuildFingerprint[] = "ro.build.fingerprint";
Sen Jiang1d5d95f2017-05-19 11:33:10 -070060const char kPropBuildType[] = "ro.build.type";
Alex Deymoebf6e122017-03-10 16:12:01 -080061
Sen Jiang740c2fc2018-01-22 17:10:41 -080062// Default channel from factory.prop
63const char kPropDefaultChannel[] = "ro.update.default_channel";
64
Sen Jiangb56fe9f2017-06-16 15:19:35 -070065// A prefix added to the path, used for testing.
66const char* root_prefix = nullptr;
67
68string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
69 const string& key,
70 const string& default_value) {
71 string result;
Alex Deymo85616652015-10-15 18:48:31 -070072 if (osrelease.GetString(key, &result))
73 return result;
74 LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value "
75 << default_value;
76 return default_value;
77}
78
Sen Jiang740c2fc2018-01-22 17:10:41 -080079// Open misc partition for read or write and output the fd in |out_fd|.
80bool OpenMisc(bool write, int* out_fd) {
Sen Jiangd944faa2018-08-22 18:46:39 -070081 string misc_device;
Sen Jiang740c2fc2018-01-22 17:10:41 -080082 int flags = write ? O_WRONLY | O_SYNC : O_RDONLY;
83 if (root_prefix) {
84 // Use a file for unittest and create one if doesn't exist.
Sen Jiangd944faa2018-08-22 18:46:39 -070085 misc_device = base::FilePath(root_prefix).Append("misc").value();
Sen Jiang740c2fc2018-01-22 17:10:41 -080086 if (write)
87 flags |= O_CREAT;
Sen Jiangd944faa2018-08-22 18:46:39 -070088 } else {
89 string err;
90 misc_device = get_bootloader_message_blk_device(&err);
91 if (misc_device.empty()) {
92 LOG(ERROR) << "Unable to get misc block device: " << err;
93 return false;
94 }
Sen Jiang740c2fc2018-01-22 17:10:41 -080095 }
96
Sen Jiangd944faa2018-08-22 18:46:39 -070097 int fd = HANDLE_EINTR(open(misc_device.c_str(), flags, 0600));
Sen Jiang740c2fc2018-01-22 17:10:41 -080098 if (fd < 0) {
99 PLOG(ERROR) << "Opening misc failed";
100 return false;
101 }
102 *out_fd = fd;
103 return true;
104}
105
106// The offset and size of the channel field in misc partition.
107constexpr size_t kChannelOffset =
108 BOOTLOADER_MESSAGE_OFFSET_IN_MISC +
109 offsetof(bootloader_message_ab, update_channel);
110constexpr size_t kChannelSize = sizeof(bootloader_message_ab::update_channel);
111
112// Read channel from misc partition to |out_channel|, return false if unable to
113// read misc or no channel is set in misc.
114bool ReadChannelFromMisc(string* out_channel) {
115 int fd;
116 TEST_AND_RETURN_FALSE(OpenMisc(false, &fd));
117 ScopedFdCloser fd_closer(&fd);
118 char channel[kChannelSize] = {0};
119 ssize_t bytes_read = 0;
120 if (!utils::PReadAll(
121 fd, channel, kChannelSize - 1, kChannelOffset, &bytes_read) ||
122 bytes_read != kChannelSize - 1) {
123 PLOG(ERROR) << "Reading update channel from misc failed";
124 return false;
125 }
126 if (channel[0] == '\0') {
127 LOG(INFO) << "No channel set in misc.";
128 return false;
129 }
Sen Jiangce3f7cf2018-01-25 14:07:45 -0800130 if (!base::EndsWith(channel, "-channel", base::CompareCase::SENSITIVE)) {
131 LOG(ERROR) << "Channel " << channel << " doesn't end with -channel.";
132 return false;
133 }
Sen Jiang740c2fc2018-01-22 17:10:41 -0800134 out_channel->assign(channel);
135 return true;
136}
137
138// Write |in_channel| to misc partition, return false if failed to write.
139bool WriteChannelToMisc(const string& in_channel) {
140 int fd;
141 TEST_AND_RETURN_FALSE(OpenMisc(true, &fd));
142 ScopedFdCloser fd_closer(&fd);
143 if (in_channel.size() >= kChannelSize) {
144 LOG(ERROR) << "Channel name is too long: " << in_channel
145 << ", the maximum length is " << kChannelSize - 1;
146 return false;
147 }
148 char channel[kChannelSize] = {0};
149 memcpy(channel, in_channel.data(), in_channel.size());
150 if (!utils::PWriteAll(fd, channel, kChannelSize, kChannelOffset)) {
151 PLOG(ERROR) << "Writing update channel to misc failed";
152 return false;
153 }
154 return true;
155}
156
157string GetTargetChannel() {
158 string channel;
159 if (!ReadChannelFromMisc(&channel))
160 channel = GetProperty(kPropDefaultChannel, "stable-channel");
161 return channel;
162}
Alex Deymo85616652015-10-15 18:48:31 -0700163} // namespace
164
165namespace test {
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700166void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
167 root_prefix = test_root_prefix;
168}
Alex Deymo85616652015-10-15 18:48:31 -0700169} // namespace test
170
171ImageProperties LoadImageProperties(SystemState* system_state) {
172 ImageProperties result;
173
174 brillo::OsReleaseReader osrelease;
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700175 if (root_prefix)
176 osrelease.LoadTestingOnly(base::FilePath(root_prefix));
177 else
178 osrelease.Load();
Sen Jiang94a4dec2017-03-28 18:23:35 -0700179 result.product_id =
180 GetStringWithDefault(osrelease, kProductId, "invalid-product");
181 result.system_id = GetStringWithDefault(
182 osrelease, kSystemId, "developer-boards:brillo-starter-board");
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700183 // Update the system id to match the prefix of product id for testing.
184 string prefix, not_used, system_id;
185 if (brillo::string_utils::SplitAtFirst(
186 result.product_id, ":", &prefix, &not_used, false) &&
187 brillo::string_utils::SplitAtFirst(
188 result.system_id, ":", &not_used, &system_id, false)) {
189 result.system_id = prefix + ":" + system_id;
190 }
Alex Deymo85616652015-10-15 18:48:31 -0700191 result.canary_product_id = result.product_id;
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700192 result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
193 result.system_version =
Sen Jiang94a4dec2017-03-28 18:23:35 -0700194 GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0");
Sen Jiang684c9cd2017-10-17 16:26:45 -0700195 // Can't read it with OsReleaseReader because it has multiple lines.
196 utils::ReadFile(kProductComponentsPath, &result.product_components);
Alex Deymo85616652015-10-15 18:48:31 -0700197
Tom Cherry012aa5b2017-10-10 14:45:09 -0700198 result.board = GetProperty(kPropProductName, "brillo");
199 result.build_fingerprint = GetProperty(kPropBuildFingerprint, "none");
200 result.build_type = GetProperty(kPropBuildType, "");
Sen Jiang1d5d95f2017-05-19 11:33:10 -0700201
Sen Jiang740c2fc2018-01-22 17:10:41 -0800202 // Android doesn't have channel information in system image, we try to read
203 // the channel of current slot from prefs and then fallback to use the
204 // persisted target channel as current channel.
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700205 string current_channel_key =
Alex Deymo85616652015-10-15 18:48:31 -0700206 kPrefsChannelOnSlotPrefix +
207 std::to_string(system_state->boot_control()->GetCurrentSlot());
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700208 string current_channel;
Alex Deymo85616652015-10-15 18:48:31 -0700209 if (!system_state->prefs()->Exists(current_channel_key) ||
210 !system_state->prefs()->GetString(current_channel_key, &current_channel))
Sen Jiang740c2fc2018-01-22 17:10:41 -0800211 current_channel = GetTargetChannel();
Alex Deymo85616652015-10-15 18:48:31 -0700212 result.current_channel = current_channel;
Sen Jiang7f785f52018-01-24 13:31:56 -0800213 result.allow_arbitrary_channels = true;
Alex Deymo85616652015-10-15 18:48:31 -0700214
215 // Brillo only supports the official omaha URL.
216 result.omaha_url = constants::kOmahaDefaultProductionURL;
217
218 return result;
219}
220
221MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
222 MutableImageProperties result;
Sen Jiang740c2fc2018-01-22 17:10:41 -0800223 result.target_channel = GetTargetChannel();
224 if (!system_state->prefs()->GetBoolean(kPrefsImgPropPowerwashAllowed,
225 &result.is_powerwash_allowed)) {
Alex Deymo85616652015-10-15 18:48:31 -0700226 result.is_powerwash_allowed = false;
227 }
228 return result;
229}
230
231bool StoreMutableImageProperties(SystemState* system_state,
232 const MutableImageProperties& properties) {
Sen Jiang740c2fc2018-01-22 17:10:41 -0800233 bool ret = true;
234 if (!WriteChannelToMisc(properties.target_channel))
235 ret = false;
236 if (!system_state->prefs()->SetBoolean(kPrefsImgPropPowerwashAllowed,
237 properties.is_powerwash_allowed))
238 ret = false;
239 return ret;
Alex Deymo85616652015-10-15 18:48:31 -0700240}
241
Amin Hassaniafd8cea2017-12-04 14:20:00 -0800242void LogImageProperties() {
243 // TODO(*): Implement this.
244}
245
Alex Deymo85616652015-10-15 18:48:31 -0700246} // namespace chromeos_update_engine