blob: 361b9f18757009348a1f791e41846ac17a906bfb [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
Alex Deymo1b03f9f2015-12-09 00:38:36 -080017#include "update_engine/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
Kelvin Zhangd7191032020-08-11 10:48:16 -040025#include <android-base/parseint.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>
Tao Bao304680c2018-03-31 10:36:52 -070028#include <bootloader_message/bootloader_message.h>
Alex Deymo40d86b22015-09-03 22:27:10 -070029
Yifan Hong87029332020-09-01 17:20:08 -070030#include "update_engine/common/error_code_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/common/hardware.h"
Sen Jiang9c123462015-11-19 13:16:23 -080032#include "update_engine/common/platform_constants.h"
Kelvin Zhangd7191032020-08-11 10:48:16 -040033#include "update_engine/common/utils.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070034
Tom Cherryfadd03c2017-10-10 14:45:09 -070035using android::base::GetBoolProperty;
36using android::base::GetIntProperty;
37using android::base::GetProperty;
Alex Deymo40d86b22015-09-03 22:27:10 -070038using std::string;
39
40namespace chromeos_update_engine {
41
Alex Deymofb905d92016-06-03 19:26:58 -070042namespace {
43
Alex Deymoebf6e122017-03-10 16:12:01 -080044// Android properties that identify the hardware and potentially non-updatable
45// parts of the bootloader (such as the bootloader version and the baseband
46// version).
47const char kPropBootBootloader[] = "ro.boot.bootloader";
48const char kPropBootBaseband[] = "ro.boot.baseband";
49const char kPropProductManufacturer[] = "ro.product.manufacturer";
50const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku";
51const char kPropBootRevision[] = "ro.boot.revision";
Sen Jiang5011df62017-06-28 17:13:19 -070052const char kPropBuildDateUTC[] = "ro.build.date.utc";
Alex Deymoebf6e122017-03-10 16:12:01 -080053
Alex Deymofb905d92016-06-03 19:26:58 -070054} // namespace
55
Alex Deymo40d86b22015-09-03 22:27:10 -070056namespace hardware {
57
58// Factory defined in hardware.h.
59std::unique_ptr<HardwareInterface> CreateHardware() {
Ben Chanab5a0af2017-10-12 14:57:50 -070060 return std::make_unique<HardwareAndroid>();
Alex Deymo40d86b22015-09-03 22:27:10 -070061}
62
63} // namespace hardware
64
Alex Deymo1c4e84a2015-09-22 16:58:10 -070065// In Android there are normally three kinds of builds: eng, userdebug and user.
66// These builds target respectively a developer build, a debuggable version of
67// the final product and the pristine final product the end user will run.
68// Apart from the ro.build.type property name, they differ in the following
69// properties that characterize the builds:
70// * eng builds: ro.secure=0 and ro.debuggable=1
71// * userdebug builds: ro.secure=1 and ro.debuggable=1
72// * user builds: ro.secure=1 and ro.debuggable=0
73//
74// See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
75// Android.
76
Alex Deymo40d86b22015-09-03 22:27:10 -070077bool HardwareAndroid::IsOfficialBuild() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -070078 // We run an official build iff ro.secure == 1, because we expect the build to
79 // behave like the end user product and check for updates. Note that while
80 // developers are able to build "official builds" by just running "make user",
81 // that will only result in a more restrictive environment. The important part
82 // is that we don't produce and push "non-official" builds to the end user.
83 //
84 // In case of a non-bool value, we take the most restrictive option and
85 // assume we are in an official-build.
Tom Cherryfadd03c2017-10-10 14:45:09 -070086 return GetBoolProperty("ro.secure", true);
Alex Deymo40d86b22015-09-03 22:27:10 -070087}
88
89bool HardwareAndroid::IsNormalBootMode() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -070090 // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
91 // update_engine will allow extra developers options, such as providing a
92 // different update URL. In case of error, we assume the build is in
93 // normal-mode.
Tom Cherryfadd03c2017-10-10 14:45:09 -070094 return !GetBoolProperty("ro.debuggable", false);
Alex Deymo40d86b22015-09-03 22:27:10 -070095}
96
Sen Jiange67bb5b2016-06-20 15:53:56 -070097bool HardwareAndroid::AreDevFeaturesEnabled() const {
98 return !IsNormalBootMode();
99}
100
Alex Deymo46a9aae2016-05-04 20:20:11 -0700101bool HardwareAndroid::IsOOBEEnabled() const {
102 // No OOBE flow blocking updates for Android-based boards.
103 return false;
104}
105
Alex Deymo40d86b22015-09-03 22:27:10 -0700106bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700107 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
Alex Deymo4d2990d2015-09-15 12:11:26 -0700108 if (out_time_of_oobe)
109 *out_time_of_oobe = base::Time();
Alex Deymo40d86b22015-09-03 22:27:10 -0700110 return true;
111}
112
113string HardwareAndroid::GetHardwareClass() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700114 auto manufacturer = GetProperty(kPropProductManufacturer, "");
115 auto sku = GetProperty(kPropBootHardwareSKU, "");
116 auto revision = GetProperty(kPropBootRevision, "");
Alex Deymoebf6e122017-03-10 16:12:01 -0800117
Tom Cherryfadd03c2017-10-10 14:45:09 -0700118 return manufacturer + ":" + sku + ":" + revision;
Alex Deymo40d86b22015-09-03 22:27:10 -0700119}
120
121string HardwareAndroid::GetFirmwareVersion() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700122 return GetProperty(kPropBootBootloader, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700123}
124
125string HardwareAndroid::GetECVersion() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700126 return GetProperty(kPropBootBaseband, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700127}
128
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400129string HardwareAndroid::GetDeviceRequisition() const {
130 LOG(WARNING) << "STUB: Getting requisition is not supported.";
131 return "";
132}
133
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800134int HardwareAndroid::GetMinKernelKeyVersion() const {
135 LOG(WARNING) << "STUB: No Kernel key version is available.";
136 return -1;
137}
138
Marton Hunyady99ced782018-05-08 12:59:50 +0200139int HardwareAndroid::GetMinFirmwareKeyVersion() const {
140 LOG(WARNING) << "STUB: No Firmware key version is available.";
141 return -1;
142}
143
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -0700144int HardwareAndroid::GetMaxFirmwareKeyRollforward() const {
145 LOG(WARNING) << "STUB: Getting firmware_max_rollforward is not supported.";
146 return -1;
147}
148
149bool HardwareAndroid::SetMaxFirmwareKeyRollforward(
150 int firmware_max_rollforward) {
151 LOG(WARNING) << "STUB: Setting firmware_max_rollforward is not supported.";
152 return false;
153}
154
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700155bool HardwareAndroid::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
156 LOG(WARNING) << "STUB: Setting kernel_max_rollforward is not supported.";
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800157 return false;
158}
159
Alex Deymo40d86b22015-09-03 22:27:10 -0700160int HardwareAndroid::GetPowerwashCount() const {
161 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
162 return 0;
163}
164
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800165bool HardwareAndroid::SchedulePowerwash(bool save_rollback_data) {
Alex Deymofb905d92016-06-03 19:26:58 -0700166 LOG(INFO) << "Scheduling a powerwash to BCB.";
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800167 LOG_IF(WARNING, save_rollback_data) << "save_rollback_data was true but "
168 << "isn't supported.";
Sen Jiangd944faa2018-08-22 18:46:39 -0700169 string err;
170 if (!update_bootloader_message({"--wipe_data", "--reason=wipe_data_from_ota"},
171 &err)) {
172 LOG(ERROR) << "Failed to update bootloader message: " << err;
173 return false;
174 }
175 return true;
Alex Deymofb905d92016-06-03 19:26:58 -0700176}
177
178bool HardwareAndroid::CancelPowerwash() {
Sen Jiangd944faa2018-08-22 18:46:39 -0700179 string err;
180 if (!clear_bootloader_message(&err)) {
181 LOG(ERROR) << "Failed to clear bootloader message: " << err;
182 return false;
183 }
184 return true;
Alex Deymofb905d92016-06-03 19:26:58 -0700185}
186
Alex Deymodd132f32015-09-14 19:12:07 -0700187bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800188 base::FilePath local_path(constants::kNonVolatileDirectory);
Kelvin Zhangd2822522020-07-07 17:20:58 -0400189 if (!base::DirectoryExists(local_path)) {
Alex Deymodd132f32015-09-14 19:12:07 -0700190 LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
191 return false;
192 }
193 *path = local_path;
194 return true;
195}
196
197bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
198 // On Android, we don't have a directory persisted across powerwash.
199 return false;
200}
201
Sen Jiang5011df62017-06-28 17:13:19 -0700202int64_t HardwareAndroid::GetBuildTimestamp() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700203 return GetIntProperty<int64_t>(kPropBuildDateUTC, 0);
Sen Jiang5011df62017-06-28 17:13:19 -0700204}
205
Tianjie Xu4ad3af62019-10-30 11:59:45 -0700206// Returns true if the device runs an userdebug build, and explicitly allows OTA
207// downgrade.
208bool HardwareAndroid::AllowDowngrade() const {
209 return GetBoolProperty("ro.ota.allow_downgrade", false) &&
210 GetBoolProperty("ro.debuggable", false);
211}
212
Amin Hassani1677e812017-06-21 13:36:36 -0700213bool HardwareAndroid::GetFirstActiveOmahaPingSent() const {
214 LOG(WARNING) << "STUB: Assuming first active omaha was never set.";
215 return false;
216}
217
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700218bool HardwareAndroid::SetFirstActiveOmahaPingSent() {
219 LOG(WARNING) << "STUB: Assuming first active omaha is set.";
220 // We will set it true, so its failure doesn't cause escalation.
221 return true;
Amin Hassani1677e812017-06-21 13:36:36 -0700222}
223
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800224void HardwareAndroid::SetWarmReset(bool warm_reset) {
225 constexpr char warm_reset_prop[] = "ota.warm_reset";
226 if (!android::base::SetProperty(warm_reset_prop, warm_reset ? "1" : "0")) {
227 LOG(WARNING) << "Failed to set prop " << warm_reset_prop;
228 }
229}
230
Kelvin Zhangd7191032020-08-11 10:48:16 -0400231std::string HardwareAndroid::GetVersionForLogging(
232 const std::string& partition_name) const {
233 return android::base::GetProperty("ro." + partition_name + ".build.date.utc",
234 "");
235}
236
Yifan Hong87029332020-09-01 17:20:08 -0700237ErrorCode HardwareAndroid::IsPartitionUpdateValid(
Kelvin Zhangd7191032020-08-11 10:48:16 -0400238 const std::string& partition_name, const std::string& new_version) const {
239 const auto old_version = GetVersionForLogging(partition_name);
240 // TODO(zhangkelvin) for some partitions, missing a current timestamp should
241 // be an error, e.g. system, vendor, product etc.
Yifan Hong87029332020-09-01 17:20:08 -0700242 auto error_code = utils::IsTimestampNewer(old_version, new_version);
243 if (error_code != ErrorCode::kSuccess) {
244 LOG(ERROR) << "Timestamp check failed with "
245 << utils::ErrorCodeToString(error_code)
246 << " Partition timestamp: " << old_version
Kelvin Zhangd7191032020-08-11 10:48:16 -0400247 << " Update timestamp: " << new_version;
248 }
Yifan Hong87029332020-09-01 17:20:08 -0700249 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -0400250}
251
Alex Deymo40d86b22015-09-03 22:27:10 -0700252} // namespace chromeos_update_engine