1 //
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 <fcntl.h>
20
21 #include <string>
22
23 #include <android-base/properties.h>
24 #include <base/logging.h>
25 #include <base/strings/string_util.h>
26 #include <bootloader.h>
27 #include <brillo/osrelease_reader.h>
28 #include <brillo/strings/string_utils.h>
29
30 #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"
34 #include "update_engine/common/utils.h"
35 #include "update_engine/system_state.h"
36 #include "update_engine/utils_android.h"
37
38 using android::base::GetProperty;
39 using std::string;
40
41 namespace chromeos_update_engine {
42
43 namespace {
44
45 // Build time properties name used in Android Things.
46 const char kProductId[] = "product_id";
47 const char kProductVersion[] = "product_version";
48 const char kSystemId[] = "system_id";
49 const char kSystemVersion[] = "system_version";
50
51 // The path to the product_components file which stores the version of each
52 // components in OEM partition.
53 const char kProductComponentsPath[] = "/oem/os-release.d/product_components";
54
55 // Prefs used to store the powerwash settings.
56 const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed";
57
58 // System properties that identifies the "board".
59 const char kPropProductName[] = "ro.product.name";
60 const char kPropBuildFingerprint[] = "ro.build.fingerprint";
61 const char kPropBuildType[] = "ro.build.type";
62
63 // Default channel from factory.prop
64 const char kPropDefaultChannel[] = "ro.update.default_channel";
65
66 // A prefix added to the path, used for testing.
67 const char* root_prefix = nullptr;
68
GetStringWithDefault(const brillo::OsReleaseReader & osrelease,const string & key,const string & default_value)69 string GetStringWithDefault(const brillo::OsReleaseReader& osrelease,
70 const string& key,
71 const string& default_value) {
72 string result;
73 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
80 // Open misc partition for read or write and output the fd in |out_fd|.
OpenMisc(bool write,int * out_fd)81 bool 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.
103 constexpr size_t kChannelOffset =
104 BOOTLOADER_MESSAGE_OFFSET_IN_MISC +
105 offsetof(bootloader_message_ab, update_channel);
106 constexpr 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.
ReadChannelFromMisc(string * out_channel)110 bool 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 }
126 if (!base::EndsWith(channel, "-channel", base::CompareCase::SENSITIVE)) {
127 LOG(ERROR) << "Channel " << channel << " doesn't end with -channel.";
128 return false;
129 }
130 out_channel->assign(channel);
131 return true;
132 }
133
134 // Write |in_channel| to misc partition, return false if failed to write.
WriteChannelToMisc(const string & in_channel)135 bool 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
GetTargetChannel()153 string GetTargetChannel() {
154 string channel;
155 if (!ReadChannelFromMisc(&channel))
156 channel = GetProperty(kPropDefaultChannel, "stable-channel");
157 return channel;
158 }
159 } // namespace
160
161 namespace test {
SetImagePropertiesRootPrefix(const char * test_root_prefix)162 void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
163 root_prefix = test_root_prefix;
164 }
165 } // namespace test
166
LoadImageProperties(SystemState * system_state)167 ImageProperties LoadImageProperties(SystemState* system_state) {
168 ImageProperties result;
169
170 brillo::OsReleaseReader osrelease;
171 if (root_prefix)
172 osrelease.LoadTestingOnly(base::FilePath(root_prefix));
173 else
174 osrelease.Load();
175 result.product_id =
176 GetStringWithDefault(osrelease, kProductId, "invalid-product");
177 result.system_id = GetStringWithDefault(
178 osrelease, kSystemId, "developer-boards:brillo-starter-board");
179 // 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, ¬_used, false) &&
183 brillo::string_utils::SplitAtFirst(
184 result.system_id, ":", ¬_used, &system_id, false)) {
185 result.system_id = prefix + ":" + system_id;
186 }
187 result.canary_product_id = result.product_id;
188 result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0");
189 result.system_version =
190 GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0");
191 // Can't read it with OsReleaseReader because it has multiple lines.
192 utils::ReadFile(kProductComponentsPath, &result.product_components);
193
194 result.board = GetProperty(kPropProductName, "brillo");
195 result.build_fingerprint = GetProperty(kPropBuildFingerprint, "none");
196 result.build_type = GetProperty(kPropBuildType, "");
197
198 // 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.
201 string current_channel_key =
202 kPrefsChannelOnSlotPrefix +
203 std::to_string(system_state->boot_control()->GetCurrentSlot());
204 string current_channel;
205 if (!system_state->prefs()->Exists(current_channel_key) ||
206 !system_state->prefs()->GetString(current_channel_key, ¤t_channel))
207 current_channel = GetTargetChannel();
208 result.current_channel = current_channel;
209 result.allow_arbitrary_channels = true;
210
211 // Brillo only supports the official omaha URL.
212 result.omaha_url = constants::kOmahaDefaultProductionURL;
213
214 return result;
215 }
216
LoadMutableImageProperties(SystemState * system_state)217 MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
218 MutableImageProperties result;
219 result.target_channel = GetTargetChannel();
220 if (!system_state->prefs()->GetBoolean(kPrefsImgPropPowerwashAllowed,
221 &result.is_powerwash_allowed)) {
222 result.is_powerwash_allowed = false;
223 }
224 return result;
225 }
226
StoreMutableImageProperties(SystemState * system_state,const MutableImageProperties & properties)227 bool StoreMutableImageProperties(SystemState* system_state,
228 const MutableImageProperties& properties) {
229 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;
236 }
237
LogImageProperties()238 void LogImageProperties() {
239 // TODO(*): Implement this.
240 }
241
242 } // namespace chromeos_update_engine
243