blob: dd39fdd8a8c110eb483a299bf9a4cfdfed0a892b [file] [log] [blame]
Alex Deymo40d86b22015-09-03 22:27:10 -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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/aosp/hardware_android.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070018
Alex Deymofb905d92016-06-03 19:26:58 -070019#include <sys/types.h>
20
Tom Cherryfadd03c2017-10-10 14:45:09 -070021#include <memory>
Kelvin Zhangd7191032020-08-11 10:48:16 -040022#include <string>
23#include <string_view>
Alex Deymofb905d92016-06-03 19:26:58 -070024
Yifan Hong126d13e2020-09-21 19:50:06 -070025#include <android/sysprop/GkiProperties.sysprop.h>
Tom Cherryfadd03c2017-10-10 14:45:09 -070026#include <android-base/properties.h>
Alex Deymodd132f32015-09-14 19:12:07 -070027#include <base/files/file_util.h>
Tianjie838793d2021-01-14 22:05:13 -080028#include <base/strings/string_number_conversions.h>
Tao Bao304680c2018-03-31 10:36:52 -070029#include <bootloader_message/bootloader_message.h>
Tianjie838793d2021-01-14 22:05:13 -080030#include <fstab/fstab.h>
31#include <libavb/libavb.h>
32#include <libavb_user/avb_ops_user.h>
Alex Deymo40d86b22015-09-03 22:27:10 -070033
Yifan Hong87029332020-09-01 17:20:08 -070034#include "update_engine/common/error_code_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/hardware.h"
Sen Jiang9c123462015-11-19 13:16:23 -080036#include "update_engine/common/platform_constants.h"
Kelvin Zhangd7191032020-08-11 10:48:16 -040037#include "update_engine/common/utils.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070038
Tianjie838793d2021-01-14 22:05:13 -080039#ifndef __ANDROID_RECOVERY__
40#include <android/sysprop/OtaProperties.sysprop.h>
41#endif
42
Tom Cherryfadd03c2017-10-10 14:45:09 -070043using android::base::GetBoolProperty;
44using android::base::GetIntProperty;
45using android::base::GetProperty;
Alex Deymo40d86b22015-09-03 22:27:10 -070046using std::string;
47
48namespace chromeos_update_engine {
49
Alex Deymofb905d92016-06-03 19:26:58 -070050namespace {
51
Alex Deymoebf6e122017-03-10 16:12:01 -080052// Android properties that identify the hardware and potentially non-updatable
53// parts of the bootloader (such as the bootloader version and the baseband
54// version).
Alex Deymoebf6e122017-03-10 16:12:01 -080055const char kPropProductManufacturer[] = "ro.product.manufacturer";
56const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku";
57const char kPropBootRevision[] = "ro.boot.revision";
Sen Jiang5011df62017-06-28 17:13:19 -070058const char kPropBuildDateUTC[] = "ro.build.date.utc";
Alex Deymoebf6e122017-03-10 16:12:01 -080059
Yifan Hongfd6640f2020-08-27 19:13:17 -070060string GetPartitionBuildDate(const string& partition_name) {
61 return android::base::GetProperty("ro." + partition_name + ".build.date.utc",
62 "");
63}
64
Yifan Hong70ba92e2020-09-22 23:56:00 +000065ErrorCode IsTimestampNewerLogged(const std::string& partition_name,
66 const std::string& old_version,
67 const std::string& new_version) {
68 auto error_code = utils::IsTimestampNewer(old_version, new_version);
69 if (error_code != ErrorCode::kSuccess) {
70 LOG(WARNING) << "Timestamp check failed with "
71 << utils::ErrorCodeToString(error_code) << ": "
72 << partition_name << " Partition timestamp: " << old_version
73 << " Update timestamp: " << new_version;
74 }
75 return error_code;
76}
77
Tianjie838793d2021-01-14 22:05:13 -080078void SetVbmetaDigestProp(const std::string& value) {
79#ifndef __ANDROID_RECOVERY__
80 if (!android::sysprop::OtaProperties::other_vbmeta_digest(value)) {
81 LOG(WARNING) << "Failed to set other vbmeta digest to " << value;
82 }
83#endif
84}
85
86std::string CalculateVbmetaDigestForInactiveSlot() {
Kelvin Zhang5b00dc52023-05-23 12:25:56 -070087 AvbSlotVerifyData* avb_slot_data{};
Tianjie838793d2021-01-14 22:05:13 -080088
89 auto suffix = fs_mgr_get_other_slot_suffix();
90 const char* requested_partitions[] = {nullptr};
91 auto avb_ops = avb_ops_user_new();
92 auto verify_result = avb_slot_verify(avb_ops,
93 requested_partitions,
94 suffix.c_str(),
95 AVB_SLOT_VERIFY_FLAGS_NONE,
96 AVB_HASHTREE_ERROR_MODE_EIO,
97 &avb_slot_data);
98 if (verify_result != AVB_SLOT_VERIFY_RESULT_OK) {
99 LOG(WARNING) << "Failed to verify avb slot data: " << verify_result;
100 return "";
101 }
102
103 uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
104 avb_slot_verify_data_calculate_vbmeta_digest(
105 avb_slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
106
Kelvin Zhang5b00dc52023-05-23 12:25:56 -0700107 const std::string encoded_digest =
Tianjie838793d2021-01-14 22:05:13 -0800108 base::HexEncode(vbmeta_digest, AVB_SHA256_DIGEST_SIZE);
Kelvin Zhang5b00dc52023-05-23 12:25:56 -0700109 LOG(INFO) << "vbmeta digest for target slot: " << encoded_digest;
Kelvin Zhang0c184242024-10-25 11:19:27 -0700110 return ToLower(encoded_digest);
Tianjie838793d2021-01-14 22:05:13 -0800111}
112
Alex Deymofb905d92016-06-03 19:26:58 -0700113} // namespace
114
Alex Deymo40d86b22015-09-03 22:27:10 -0700115namespace hardware {
116
117// Factory defined in hardware.h.
118std::unique_ptr<HardwareInterface> CreateHardware() {
Ben Chanab5a0af2017-10-12 14:57:50 -0700119 return std::make_unique<HardwareAndroid>();
Alex Deymo40d86b22015-09-03 22:27:10 -0700120}
121
122} // namespace hardware
123
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700124// 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
Alex Deymo40d86b22015-09-03 22:27:10 -0700136bool HardwareAndroid::IsOfficialBuild() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700137 // 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.
Tom Cherryfadd03c2017-10-10 14:45:09 -0700145 return GetBoolProperty("ro.secure", true);
Alex Deymo40d86b22015-09-03 22:27:10 -0700146}
147
148bool HardwareAndroid::IsNormalBootMode() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700149 // 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.
Tom Cherryfadd03c2017-10-10 14:45:09 -0700153 return !GetBoolProperty("ro.debuggable", false);
Alex Deymo40d86b22015-09-03 22:27:10 -0700154}
155
Sen Jiange67bb5b2016-06-20 15:53:56 -0700156bool HardwareAndroid::AreDevFeaturesEnabled() const {
157 return !IsNormalBootMode();
158}
159
Alex Deymo46a9aae2016-05-04 20:20:11 -0700160bool HardwareAndroid::IsOOBEEnabled() const {
161 // No OOBE flow blocking updates for Android-based boards.
162 return false;
163}
164
Alex Deymo40d86b22015-09-03 22:27:10 -0700165bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700166 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
Alex Deymo4d2990d2015-09-15 12:11:26 -0700167 if (out_time_of_oobe)
168 *out_time_of_oobe = base::Time();
Alex Deymo40d86b22015-09-03 22:27:10 -0700169 return true;
170}
171
172string HardwareAndroid::GetHardwareClass() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700173 auto manufacturer = GetProperty(kPropProductManufacturer, "");
174 auto sku = GetProperty(kPropBootHardwareSKU, "");
175 auto revision = GetProperty(kPropBootRevision, "");
Alex Deymoebf6e122017-03-10 16:12:01 -0800176
Tom Cherryfadd03c2017-10-10 14:45:09 -0700177 return manufacturer + ":" + sku + ":" + revision;
Alex Deymo40d86b22015-09-03 22:27:10 -0700178}
179
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400180string HardwareAndroid::GetDeviceRequisition() const {
181 LOG(WARNING) << "STUB: Getting requisition is not supported.";
182 return "";
183}
184
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800185int HardwareAndroid::GetMinKernelKeyVersion() const {
186 LOG(WARNING) << "STUB: No Kernel key version is available.";
187 return -1;
188}
189
Marton Hunyady99ced782018-05-08 12:59:50 +0200190int HardwareAndroid::GetMinFirmwareKeyVersion() const {
191 LOG(WARNING) << "STUB: No Firmware key version is available.";
192 return -1;
193}
194
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -0700195int HardwareAndroid::GetMaxFirmwareKeyRollforward() const {
196 LOG(WARNING) << "STUB: Getting firmware_max_rollforward is not supported.";
197 return -1;
198}
199
200bool HardwareAndroid::SetMaxFirmwareKeyRollforward(
201 int firmware_max_rollforward) {
202 LOG(WARNING) << "STUB: Setting firmware_max_rollforward is not supported.";
203 return false;
204}
205
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700206bool HardwareAndroid::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
207 LOG(WARNING) << "STUB: Setting kernel_max_rollforward is not supported.";
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800208 return false;
209}
210
Alex Deymo40d86b22015-09-03 22:27:10 -0700211int HardwareAndroid::GetPowerwashCount() const {
212 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
213 return 0;
214}
215
Kelvin Zhang399bd4d2024-12-03 11:08:30 -0800216bool HardwareAndroid::SchedulePowerwash() {
Alex Deymofb905d92016-06-03 19:26:58 -0700217 LOG(INFO) << "Scheduling a powerwash to BCB.";
Sen Jiangd944faa2018-08-22 18:46:39 -0700218 string err;
219 if (!update_bootloader_message({"--wipe_data", "--reason=wipe_data_from_ota"},
220 &err)) {
221 LOG(ERROR) << "Failed to update bootloader message: " << err;
222 return false;
223 }
224 return true;
Alex Deymofb905d92016-06-03 19:26:58 -0700225}
226
227bool HardwareAndroid::CancelPowerwash() {
Sen Jiangd944faa2018-08-22 18:46:39 -0700228 string err;
229 if (!clear_bootloader_message(&err)) {
230 LOG(ERROR) << "Failed to clear bootloader message: " << err;
231 return false;
232 }
233 return true;
Alex Deymofb905d92016-06-03 19:26:58 -0700234}
235
Alex Deymodd132f32015-09-14 19:12:07 -0700236bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800237 base::FilePath local_path(constants::kNonVolatileDirectory);
Kelvin Zhangd2822522020-07-07 17:20:58 -0400238 if (!base::DirectoryExists(local_path)) {
Alex Deymodd132f32015-09-14 19:12:07 -0700239 LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
240 return false;
241 }
242 *path = local_path;
243 return true;
244}
245
246bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
247 // On Android, we don't have a directory persisted across powerwash.
248 return false;
249}
250
Sen Jiang5011df62017-06-28 17:13:19 -0700251int64_t HardwareAndroid::GetBuildTimestamp() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700252 return GetIntProperty<int64_t>(kPropBuildDateUTC, 0);
Sen Jiang5011df62017-06-28 17:13:19 -0700253}
254
Tianjie Xu4ad3af62019-10-30 11:59:45 -0700255// Returns true if the device runs an userdebug build, and explicitly allows OTA
256// downgrade.
257bool HardwareAndroid::AllowDowngrade() const {
258 return GetBoolProperty("ro.ota.allow_downgrade", false) &&
259 GetBoolProperty("ro.debuggable", false);
260}
261
Amin Hassani1677e812017-06-21 13:36:36 -0700262bool HardwareAndroid::GetFirstActiveOmahaPingSent() const {
263 LOG(WARNING) << "STUB: Assuming first active omaha was never set.";
264 return false;
265}
266
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700267bool HardwareAndroid::SetFirstActiveOmahaPingSent() {
268 LOG(WARNING) << "STUB: Assuming first active omaha is set.";
269 // We will set it true, so its failure doesn't cause escalation.
270 return true;
Amin Hassani1677e812017-06-21 13:36:36 -0700271}
272
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800273void HardwareAndroid::SetWarmReset(bool warm_reset) {
Kelvin Zhanga22ef552020-10-12 19:03:52 -0400274 if constexpr (!constants::kIsRecovery) {
275 constexpr char warm_reset_prop[] = "ota.warm_reset";
276 if (!android::base::SetProperty(warm_reset_prop, warm_reset ? "1" : "0")) {
277 LOG(WARNING) << "Failed to set prop " << warm_reset_prop;
278 }
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800279 }
280}
281
Tianjie838793d2021-01-14 22:05:13 -0800282void HardwareAndroid::SetVbmetaDigestForInactiveSlot(bool reset) {
283 if constexpr (constants::kIsRecovery) {
284 return;
285 }
286
287 if (android::base::GetProperty("ro.boot.avb_version", "").empty() &&
288 android::base::GetProperty("ro.boot.vbmeta.avb_version", "").empty()) {
289 LOG(INFO) << "Device doesn't use avb, skipping setting vbmeta digest";
290 return;
291 }
292
293 if (reset) {
294 SetVbmetaDigestProp("");
295 return;
296 }
297
298 std::string digest = CalculateVbmetaDigestForInactiveSlot();
299 if (digest.empty()) {
300 LOG(WARNING) << "Failed to calculate the vbmeta digest for the other slot";
301 return;
302 }
303 SetVbmetaDigestProp(digest);
304}
305
Yifan Hongfd6640f2020-08-27 19:13:17 -0700306string HardwareAndroid::GetVersionForLogging(
307 const string& partition_name) const {
308 if (partition_name == "boot") {
Yifan Hong70ba92e2020-09-22 23:56:00 +0000309 // ro.bootimage.build.date.utc
310 return GetPartitionBuildDate("bootimage");
Yifan Hongfd6640f2020-08-27 19:13:17 -0700311 }
312 return GetPartitionBuildDate(partition_name);
Kelvin Zhangd7191032020-08-11 10:48:16 -0400313}
314
Yifan Hong87029332020-09-01 17:20:08 -0700315ErrorCode HardwareAndroid::IsPartitionUpdateValid(
Yifan Hongfd6640f2020-08-27 19:13:17 -0700316 const string& partition_name, const string& new_version) const {
317 if (partition_name == "boot") {
Yifan Hong70ba92e2020-09-22 23:56:00 +0000318 const auto old_version = GetPartitionBuildDate("bootimage");
319 auto error_code =
320 IsTimestampNewerLogged(partition_name, old_version, new_version);
321 if (error_code == ErrorCode::kPayloadTimestampError) {
322 bool prevent_downgrade =
323 android::sysprop::GkiProperties::prevent_downgrade_version().value_or(
324 false);
325 if (!prevent_downgrade) {
326 LOG(WARNING) << "Downgrade of boot image is detected, but permitting "
327 "update because device does not prevent boot image "
328 "downgrade";
329 // If prevent_downgrade_version sysprop is not explicitly set, permit
330 // downgrade in boot image version.
331 // Even though error_code is overridden here, always call
332 // IsTimestampNewerLogged to produce log messages.
333 error_code = ErrorCode::kSuccess;
334 }
Yifan Hongfd6640f2020-08-27 19:13:17 -0700335 }
Yifan Hong70ba92e2020-09-22 23:56:00 +0000336 return error_code;
Yifan Hongfd6640f2020-08-27 19:13:17 -0700337 }
338
339 const auto old_version = GetPartitionBuildDate(partition_name);
Kelvin Zhangd7191032020-08-11 10:48:16 -0400340 // TODO(zhangkelvin) for some partitions, missing a current timestamp should
341 // be an error, e.g. system, vendor, product etc.
Yifan Hong70ba92e2020-09-22 23:56:00 +0000342 auto error_code =
343 IsTimestampNewerLogged(partition_name, old_version, new_version);
Yifan Hong87029332020-09-01 17:20:08 -0700344 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -0400345}
346
Alex Light7361d272021-03-12 09:05:55 -0800347// Mount options for non-system partitions. This option causes selinux treat
348// every file in the mounted filesystem as having the 'postinstall_file'
349// context, regardless of what the filesystem itself records. See "SELinux
350// User's and Administrator's Guide" for more information on this option.
351constexpr const char* kDefaultPostinstallMountOptions =
352 "context=u:object_r:postinstall_file:s0";
353
354// Mount options for system partitions. This option causes selinux to use the
355// 'postinstall_file' context as a fallback if there are no other selinux
356// contexts associated with the file in the mounted partition. See "SELinux
357// User's and Administrator's Guide" for more information on this option.
358constexpr const char* kSystemPostinstallMountOptions =
359 "defcontext=u:object_r:postinstall_file:s0";
360
361// Name of the system-partition
362constexpr std::string_view kSystemPartitionName = "system";
363
364const char* HardwareAndroid::GetPartitionMountOptions(
365 const std::string& partition_name) const {
366 if (partition_name == kSystemPartitionName) {
367 return kSystemPostinstallMountOptions;
368 } else {
369 return kDefaultPostinstallMountOptions;
370 }
371}
372
Alex Deymo40d86b22015-09-03 22:27:10 -0700373} // namespace chromeos_update_engine