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/aosp/hardware_android.h"
18
19 #include <sys/types.h>
20
21 #include <memory>
22 #include <string>
23 #include <string_view>
24
25 #include <android/sysprop/GkiProperties.sysprop.h>
26 #include <android-base/properties.h>
27 #include <base/files/file_util.h>
28 #include <base/strings/string_number_conversions.h>
29 #include <base/strings/string_util.h>
30 #include <bootloader_message/bootloader_message.h>
31 #include <fstab/fstab.h>
32 #include <libavb/libavb.h>
33 #include <libavb_user/avb_ops_user.h>
34
35 #include "update_engine/common/error_code_utils.h"
36 #include "update_engine/common/hardware.h"
37 #include "update_engine/common/platform_constants.h"
38 #include "update_engine/common/utils.h"
39
40 #ifndef __ANDROID_RECOVERY__
41 #include <android/sysprop/OtaProperties.sysprop.h>
42 #endif
43
44 using android::base::GetBoolProperty;
45 using android::base::GetIntProperty;
46 using android::base::GetProperty;
47 using std::string;
48
49 namespace chromeos_update_engine {
50
51 namespace {
52
53 // Android properties that identify the hardware and potentially non-updatable
54 // parts of the bootloader (such as the bootloader version and the baseband
55 // version).
56 const char kPropProductManufacturer[] = "ro.product.manufacturer";
57 const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku";
58 const char kPropBootRevision[] = "ro.boot.revision";
59 const char kPropBuildDateUTC[] = "ro.build.date.utc";
60
GetPartitionBuildDate(const string & partition_name)61 string GetPartitionBuildDate(const string& partition_name) {
62 return android::base::GetProperty("ro." + partition_name + ".build.date.utc",
63 "");
64 }
65
IsTimestampNewerLogged(const std::string & partition_name,const std::string & old_version,const std::string & new_version)66 ErrorCode IsTimestampNewerLogged(const std::string& partition_name,
67 const std::string& old_version,
68 const std::string& new_version) {
69 auto error_code = utils::IsTimestampNewer(old_version, new_version);
70 if (error_code != ErrorCode::kSuccess) {
71 LOG(WARNING) << "Timestamp check failed with "
72 << utils::ErrorCodeToString(error_code) << ": "
73 << partition_name << " Partition timestamp: " << old_version
74 << " Update timestamp: " << new_version;
75 }
76 return error_code;
77 }
78
SetVbmetaDigestProp(const std::string & value)79 void SetVbmetaDigestProp(const std::string& value) {
80 #ifndef __ANDROID_RECOVERY__
81 if (!android::sysprop::OtaProperties::other_vbmeta_digest(value)) {
82 LOG(WARNING) << "Failed to set other vbmeta digest to " << value;
83 }
84 #endif
85 }
86
CalculateVbmetaDigestForInactiveSlot()87 std::string CalculateVbmetaDigestForInactiveSlot() {
88 AvbSlotVerifyData* avb_slot_data;
89
90 auto suffix = fs_mgr_get_other_slot_suffix();
91 const char* requested_partitions[] = {nullptr};
92 auto avb_ops = avb_ops_user_new();
93 auto verify_result = avb_slot_verify(avb_ops,
94 requested_partitions,
95 suffix.c_str(),
96 AVB_SLOT_VERIFY_FLAGS_NONE,
97 AVB_HASHTREE_ERROR_MODE_EIO,
98 &avb_slot_data);
99 if (verify_result != AVB_SLOT_VERIFY_RESULT_OK) {
100 LOG(WARNING) << "Failed to verify avb slot data: " << verify_result;
101 return "";
102 }
103
104 uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
105 avb_slot_verify_data_calculate_vbmeta_digest(
106 avb_slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
107
108 std::string encoded_digest =
109 base::HexEncode(vbmeta_digest, AVB_SHA256_DIGEST_SIZE);
110 return base::ToLowerASCII(encoded_digest);
111 }
112
113 } // namespace
114
115 namespace hardware {
116
117 // Factory defined in hardware.h.
CreateHardware()118 std::unique_ptr<HardwareInterface> CreateHardware() {
119 return std::make_unique<HardwareAndroid>();
120 }
121
122 } // namespace hardware
123
124 // In Android there are normally three kinds of builds: eng, userdebug and user.
125 // These builds target respectively a developer build, a debuggable version of
126 // the final product and the pristine final product the end user will run.
127 // Apart from the ro.build.type property name, they differ in the following
128 // properties that characterize the builds:
129 // * eng builds: ro.secure=0 and ro.debuggable=1
130 // * userdebug builds: ro.secure=1 and ro.debuggable=1
131 // * user builds: ro.secure=1 and ro.debuggable=0
132 //
133 // See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
134 // Android.
135
IsOfficialBuild() const136 bool HardwareAndroid::IsOfficialBuild() const {
137 // We run an official build iff ro.secure == 1, because we expect the build to
138 // behave like the end user product and check for updates. Note that while
139 // developers are able to build "official builds" by just running "make user",
140 // that will only result in a more restrictive environment. The important part
141 // is that we don't produce and push "non-official" builds to the end user.
142 //
143 // In case of a non-bool value, we take the most restrictive option and
144 // assume we are in an official-build.
145 return GetBoolProperty("ro.secure", true);
146 }
147
IsNormalBootMode() const148 bool HardwareAndroid::IsNormalBootMode() const {
149 // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
150 // update_engine will allow extra developers options, such as providing a
151 // different update URL. In case of error, we assume the build is in
152 // normal-mode.
153 return !GetBoolProperty("ro.debuggable", false);
154 }
155
AreDevFeaturesEnabled() const156 bool HardwareAndroid::AreDevFeaturesEnabled() const {
157 return !IsNormalBootMode();
158 }
159
IsOOBEEnabled() const160 bool HardwareAndroid::IsOOBEEnabled() const {
161 // No OOBE flow blocking updates for Android-based boards.
162 return false;
163 }
164
IsOOBEComplete(base::Time * out_time_of_oobe) const165 bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
166 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
167 if (out_time_of_oobe)
168 *out_time_of_oobe = base::Time();
169 return true;
170 }
171
GetHardwareClass() const172 string HardwareAndroid::GetHardwareClass() const {
173 auto manufacturer = GetProperty(kPropProductManufacturer, "");
174 auto sku = GetProperty(kPropBootHardwareSKU, "");
175 auto revision = GetProperty(kPropBootRevision, "");
176
177 return manufacturer + ":" + sku + ":" + revision;
178 }
179
GetDeviceRequisition() const180 string HardwareAndroid::GetDeviceRequisition() const {
181 LOG(WARNING) << "STUB: Getting requisition is not supported.";
182 return "";
183 }
184
GetMinKernelKeyVersion() const185 int HardwareAndroid::GetMinKernelKeyVersion() const {
186 LOG(WARNING) << "STUB: No Kernel key version is available.";
187 return -1;
188 }
189
GetMinFirmwareKeyVersion() const190 int HardwareAndroid::GetMinFirmwareKeyVersion() const {
191 LOG(WARNING) << "STUB: No Firmware key version is available.";
192 return -1;
193 }
194
GetMaxFirmwareKeyRollforward() const195 int HardwareAndroid::GetMaxFirmwareKeyRollforward() const {
196 LOG(WARNING) << "STUB: Getting firmware_max_rollforward is not supported.";
197 return -1;
198 }
199
SetMaxFirmwareKeyRollforward(int firmware_max_rollforward)200 bool HardwareAndroid::SetMaxFirmwareKeyRollforward(
201 int firmware_max_rollforward) {
202 LOG(WARNING) << "STUB: Setting firmware_max_rollforward is not supported.";
203 return false;
204 }
205
SetMaxKernelKeyRollforward(int kernel_max_rollforward)206 bool HardwareAndroid::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
207 LOG(WARNING) << "STUB: Setting kernel_max_rollforward is not supported.";
208 return false;
209 }
210
GetPowerwashCount() const211 int HardwareAndroid::GetPowerwashCount() const {
212 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
213 return 0;
214 }
215
SchedulePowerwash(bool save_rollback_data)216 bool HardwareAndroid::SchedulePowerwash(bool save_rollback_data) {
217 LOG(INFO) << "Scheduling a powerwash to BCB.";
218 LOG_IF(WARNING, save_rollback_data) << "save_rollback_data was true but "
219 << "isn't supported.";
220 string err;
221 if (!update_bootloader_message({"--wipe_data", "--reason=wipe_data_from_ota"},
222 &err)) {
223 LOG(ERROR) << "Failed to update bootloader message: " << err;
224 return false;
225 }
226 return true;
227 }
228
CancelPowerwash()229 bool HardwareAndroid::CancelPowerwash() {
230 string err;
231 if (!clear_bootloader_message(&err)) {
232 LOG(ERROR) << "Failed to clear bootloader message: " << err;
233 return false;
234 }
235 return true;
236 }
237
GetNonVolatileDirectory(base::FilePath * path) const238 bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
239 base::FilePath local_path(constants::kNonVolatileDirectory);
240 if (!base::DirectoryExists(local_path)) {
241 LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
242 return false;
243 }
244 *path = local_path;
245 return true;
246 }
247
GetPowerwashSafeDirectory(base::FilePath * path) const248 bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
249 // On Android, we don't have a directory persisted across powerwash.
250 return false;
251 }
252
GetBuildTimestamp() const253 int64_t HardwareAndroid::GetBuildTimestamp() const {
254 return GetIntProperty<int64_t>(kPropBuildDateUTC, 0);
255 }
256
257 // Returns true if the device runs an userdebug build, and explicitly allows OTA
258 // downgrade.
AllowDowngrade() const259 bool HardwareAndroid::AllowDowngrade() const {
260 return GetBoolProperty("ro.ota.allow_downgrade", false) &&
261 GetBoolProperty("ro.debuggable", false);
262 }
263
GetFirstActiveOmahaPingSent() const264 bool HardwareAndroid::GetFirstActiveOmahaPingSent() const {
265 LOG(WARNING) << "STUB: Assuming first active omaha was never set.";
266 return false;
267 }
268
SetFirstActiveOmahaPingSent()269 bool HardwareAndroid::SetFirstActiveOmahaPingSent() {
270 LOG(WARNING) << "STUB: Assuming first active omaha is set.";
271 // We will set it true, so its failure doesn't cause escalation.
272 return true;
273 }
274
SetWarmReset(bool warm_reset)275 void HardwareAndroid::SetWarmReset(bool warm_reset) {
276 if constexpr (!constants::kIsRecovery) {
277 constexpr char warm_reset_prop[] = "ota.warm_reset";
278 if (!android::base::SetProperty(warm_reset_prop, warm_reset ? "1" : "0")) {
279 LOG(WARNING) << "Failed to set prop " << warm_reset_prop;
280 }
281 }
282 }
283
SetVbmetaDigestForInactiveSlot(bool reset)284 void HardwareAndroid::SetVbmetaDigestForInactiveSlot(bool reset) {
285 if constexpr (constants::kIsRecovery) {
286 return;
287 }
288
289 if (android::base::GetProperty("ro.boot.avb_version", "").empty() &&
290 android::base::GetProperty("ro.boot.vbmeta.avb_version", "").empty()) {
291 LOG(INFO) << "Device doesn't use avb, skipping setting vbmeta digest";
292 return;
293 }
294
295 if (reset) {
296 SetVbmetaDigestProp("");
297 return;
298 }
299
300 std::string digest = CalculateVbmetaDigestForInactiveSlot();
301 if (digest.empty()) {
302 LOG(WARNING) << "Failed to calculate the vbmeta digest for the other slot";
303 return;
304 }
305 SetVbmetaDigestProp(digest);
306 }
307
GetVersionForLogging(const string & partition_name) const308 string HardwareAndroid::GetVersionForLogging(
309 const string& partition_name) const {
310 if (partition_name == "boot") {
311 // ro.bootimage.build.date.utc
312 return GetPartitionBuildDate("bootimage");
313 }
314 return GetPartitionBuildDate(partition_name);
315 }
316
IsPartitionUpdateValid(const string & partition_name,const string & new_version) const317 ErrorCode HardwareAndroid::IsPartitionUpdateValid(
318 const string& partition_name, const string& new_version) const {
319 if (partition_name == "boot") {
320 const auto old_version = GetPartitionBuildDate("bootimage");
321 auto error_code =
322 IsTimestampNewerLogged(partition_name, old_version, new_version);
323 if (error_code == ErrorCode::kPayloadTimestampError) {
324 bool prevent_downgrade =
325 android::sysprop::GkiProperties::prevent_downgrade_version().value_or(
326 false);
327 if (!prevent_downgrade) {
328 LOG(WARNING) << "Downgrade of boot image is detected, but permitting "
329 "update because device does not prevent boot image "
330 "downgrade";
331 // If prevent_downgrade_version sysprop is not explicitly set, permit
332 // downgrade in boot image version.
333 // Even though error_code is overridden here, always call
334 // IsTimestampNewerLogged to produce log messages.
335 error_code = ErrorCode::kSuccess;
336 }
337 }
338 return error_code;
339 }
340
341 const auto old_version = GetPartitionBuildDate(partition_name);
342 // TODO(zhangkelvin) for some partitions, missing a current timestamp should
343 // be an error, e.g. system, vendor, product etc.
344 auto error_code =
345 IsTimestampNewerLogged(partition_name, old_version, new_version);
346 return error_code;
347 }
348
349 // Mount options for non-system partitions. This option causes selinux treat
350 // every file in the mounted filesystem as having the 'postinstall_file'
351 // context, regardless of what the filesystem itself records. See "SELinux
352 // User's and Administrator's Guide" for more information on this option.
353 constexpr const char* kDefaultPostinstallMountOptions =
354 "context=u:object_r:postinstall_file:s0";
355
356 // Mount options for system partitions. This option causes selinux to use the
357 // 'postinstall_file' context as a fallback if there are no other selinux
358 // contexts associated with the file in the mounted partition. See "SELinux
359 // User's and Administrator's Guide" for more information on this option.
360 constexpr const char* kSystemPostinstallMountOptions =
361 "defcontext=u:object_r:postinstall_file:s0";
362
363 // Name of the system-partition
364 constexpr std::string_view kSystemPartitionName = "system";
365
GetPartitionMountOptions(const std::string & partition_name) const366 const char* HardwareAndroid::GetPartitionMountOptions(
367 const std::string& partition_name) const {
368 if (partition_name == kSystemPartitionName) {
369 return kSystemPostinstallMountOptions;
370 } else {
371 return kDefaultPostinstallMountOptions;
372 }
373 }
374
375 } // namespace chromeos_update_engine
376