blob: 6e743f9f389dc2d6eda5490b4629ba77ac118895 [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 Jiang740c2fc2018-01-22 17:10:41 -080025#include <bootloader.h>
Alex Deymo85616652015-10-15 18:48:31 -070026#include <brillo/osrelease_reader.h>
Sen Jiangb56fe9f2017-06-16 15:19:35 -070027#include <brillo/strings/string_utils.h>
Alex Deymo85616652015-10-15 18:48:31 -070028
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/boot_control_interface.h"
30#include "update_engine/common/constants.h"
31#include "update_engine/common/platform_constants.h"
32#include "update_engine/common/prefs_interface.h"
Sen Jiang684c9cd2017-10-17 16:26:45 -070033#include "update_engine/common/utils.h"
Alex Deymo85616652015-10-15 18:48:31 -070034#include "update_engine/system_state.h"
Sen Jiang740c2fc2018-01-22 17:10:41 -080035#include "update_engine/utils_android.h"
Alex Deymo85616652015-10-15 18:48:31 -070036
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) {
81 base::FilePath misc_device;
82 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.
85 misc_device = base::FilePath(root_prefix).Append("misc");
86 if (write)
87 flags |= O_CREAT;
88 } else if (!utils::DeviceForMountPoint("/misc", &misc_device)) {
89 return false;
90 }
91
92 int fd = HANDLE_EINTR(open(misc_device.value().c_str(), flags, 0600));
93 if (fd < 0) {
94 PLOG(ERROR) << "Opening misc failed";
95 return false;
96 }
97 *out_fd = fd;
98 return true;
99}
100
101// The offset and size of the channel field in misc partition.
102constexpr size_t kChannelOffset =
103 BOOTLOADER_MESSAGE_OFFSET_IN_MISC +
104 offsetof(bootloader_message_ab, update_channel);
105constexpr size_t kChannelSize = sizeof(bootloader_message_ab::update_channel);
106
107// Read channel from misc partition to |out_channel|, return false if unable to
108// read misc or no channel is set in misc.
109bool ReadChannelFromMisc(string* out_channel) {
110 int fd;
111 TEST_AND_RETURN_FALSE(OpenMisc(false, &fd));
112 ScopedFdCloser fd_closer(&fd);
113 char channel[kChannelSize] = {0};
114 ssize_t bytes_read = 0;
115 if (!utils::PReadAll(
116 fd, channel, kChannelSize - 1, kChannelOffset, &bytes_read) ||
117 bytes_read != kChannelSize - 1) {
118 PLOG(ERROR) << "Reading update channel from misc failed";
119 return false;
120 }
121 if (channel[0] == '\0') {
122 LOG(INFO) << "No channel set in misc.";
123 return false;
124 }
125 out_channel->assign(channel);
126 return true;
127}
128
129// Write |in_channel| to misc partition, return false if failed to write.
130bool WriteChannelToMisc(const string& in_channel) {
131 int fd;
132 TEST_AND_RETURN_FALSE(OpenMisc(true, &fd));
133 ScopedFdCloser fd_closer(&fd);
134 if (in_channel.size() >= kChannelSize) {
135 LOG(ERROR) << "Channel name is too long: " << in_channel
136 << ", the maximum length is " << kChannelSize - 1;
137 return false;
138 }
139 char channel[kChannelSize] = {0};
140 memcpy(channel, in_channel.data(), in_channel.size());
141 if (!utils::PWriteAll(fd, channel, kChannelSize, kChannelOffset)) {
142 PLOG(ERROR) << "Writing update channel to misc failed";
143 return false;
144 }
145 return true;
146}
147
148string GetTargetChannel() {
149 string channel;
150 if (!ReadChannelFromMisc(&channel))
151 channel = GetProperty(kPropDefaultChannel, "stable-channel");
152 return channel;
153}
Alex Deymo85616652015-10-15 18:48:31 -0700154} // namespace
155
156namespace test {
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700157void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
158 root_prefix = test_root_prefix;
159}
Alex Deymo85616652015-10-15 18:48:31 -0700160} // namespace test
161
162ImageProperties LoadImageProperties(SystemState* system_state) {
163 ImageProperties result;
164
165 brillo::OsReleaseReader osrelease;
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700166 if (root_prefix)
167 osrelease.LoadTestingOnly(base::FilePath(root_prefix));
168 else
169 osrelease.Load();
Sen Jiang94a4dec2017-03-28 18:23:35 -0700170 result.product_id =
171 GetStringWithDefault(osrelease, kProductId, "invalid-product");
172 result.system_id = GetStringWithDefault(
173 osrelease, kSystemId, "developer-boards:brillo-starter-board");
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700174 // Update the system id to match the prefix of product id for testing.
175 string prefix, not_used, system_id;
176 if (brillo::string_utils::SplitAtFirst(
177 result.product_id, ":", &prefix, &not_used, false) &&
178 brillo::string_utils::SplitAtFirst(
179 result.system_id, ":", &not_used, &system_id, false)) {
180 result.system_id = prefix + ":" + system_id;
181 }
Alex Deymo85616652015-10-15 18:48:31 -0700182 result.canary_product_id = result.product_id;
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700183 result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
184 result.system_version =
Sen Jiang94a4dec2017-03-28 18:23:35 -0700185 GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0");
Sen Jiang684c9cd2017-10-17 16:26:45 -0700186 // Can't read it with OsReleaseReader because it has multiple lines.
187 utils::ReadFile(kProductComponentsPath, &result.product_components);
Alex Deymo85616652015-10-15 18:48:31 -0700188
Tom Cherry012aa5b2017-10-10 14:45:09 -0700189 result.board = GetProperty(kPropProductName, "brillo");
190 result.build_fingerprint = GetProperty(kPropBuildFingerprint, "none");
191 result.build_type = GetProperty(kPropBuildType, "");
Sen Jiang1d5d95f2017-05-19 11:33:10 -0700192
Sen Jiang740c2fc2018-01-22 17:10:41 -0800193 // Android doesn't have channel information in system image, we try to read
194 // the channel of current slot from prefs and then fallback to use the
195 // persisted target channel as current channel.
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700196 string current_channel_key =
Alex Deymo85616652015-10-15 18:48:31 -0700197 kPrefsChannelOnSlotPrefix +
198 std::to_string(system_state->boot_control()->GetCurrentSlot());
Sen Jiangb56fe9f2017-06-16 15:19:35 -0700199 string current_channel;
Alex Deymo85616652015-10-15 18:48:31 -0700200 if (!system_state->prefs()->Exists(current_channel_key) ||
201 !system_state->prefs()->GetString(current_channel_key, &current_channel))
Sen Jiang740c2fc2018-01-22 17:10:41 -0800202 current_channel = GetTargetChannel();
Alex Deymo85616652015-10-15 18:48:31 -0700203 result.current_channel = current_channel;
204
205 // Brillo only supports the official omaha URL.
206 result.omaha_url = constants::kOmahaDefaultProductionURL;
207
208 return result;
209}
210
211MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
212 MutableImageProperties result;
Sen Jiang740c2fc2018-01-22 17:10:41 -0800213 result.target_channel = GetTargetChannel();
214 if (!system_state->prefs()->GetBoolean(kPrefsImgPropPowerwashAllowed,
215 &result.is_powerwash_allowed)) {
Alex Deymo85616652015-10-15 18:48:31 -0700216 result.is_powerwash_allowed = false;
217 }
218 return result;
219}
220
221bool StoreMutableImageProperties(SystemState* system_state,
222 const MutableImageProperties& properties) {
Sen Jiang740c2fc2018-01-22 17:10:41 -0800223 bool ret = true;
224 if (!WriteChannelToMisc(properties.target_channel))
225 ret = false;
226 if (!system_state->prefs()->SetBoolean(kPrefsImgPropPowerwashAllowed,
227 properties.is_powerwash_allowed))
228 ret = false;
229 return ret;
Alex Deymo85616652015-10-15 18:48:31 -0700230}
231
Amin Hassaniafd8cea2017-12-04 14:20:00 -0800232void LogImageProperties() {
233 // TODO(*): Implement this.
234}
235
Alex Deymo85616652015-10-15 18:48:31 -0700236} // namespace chromeos_update_engine