blob: 48945224e1bb565a594a5fc420d4f1816eab5a2d [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>
Yifan Hongfd6640f2020-08-27 19:13:17 -070020#include <sys/utsname.h>
Alex Deymofb905d92016-06-03 19:26:58 -070021
Tom Cherryfadd03c2017-10-10 14:45:09 -070022#include <memory>
Kelvin Zhangd7191032020-08-11 10:48:16 -040023#include <string>
24#include <string_view>
Alex Deymofb905d92016-06-03 19:26:58 -070025
Kelvin Zhangd7191032020-08-11 10:48:16 -040026#include <android-base/parseint.h>
Tom Cherryfadd03c2017-10-10 14:45:09 -070027#include <android-base/properties.h>
Alex Deymodd132f32015-09-14 19:12:07 -070028#include <base/files/file_util.h>
Tao Bao304680c2018-03-31 10:36:52 -070029#include <bootloader_message/bootloader_message.h>
Yifan Hongfd6640f2020-08-27 19:13:17 -070030#include <kver/kernel_release.h>
31#include <kver/utils.h>
Alex Deymo40d86b22015-09-03 22:27:10 -070032
Yifan Hong87029332020-09-01 17:20:08 -070033#include "update_engine/common/error_code_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/hardware.h"
Sen Jiang9c123462015-11-19 13:16:23 -080035#include "update_engine/common/platform_constants.h"
Kelvin Zhangd7191032020-08-11 10:48:16 -040036#include "update_engine/common/utils.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070037
Tom Cherryfadd03c2017-10-10 14:45:09 -070038using android::base::GetBoolProperty;
39using android::base::GetIntProperty;
40using android::base::GetProperty;
Yifan Hongfd6640f2020-08-27 19:13:17 -070041using android::kver::IsKernelUpdateValid;
42using android::kver::KernelRelease;
Alex Deymo40d86b22015-09-03 22:27:10 -070043using std::string;
44
45namespace chromeos_update_engine {
46
Alex Deymofb905d92016-06-03 19:26:58 -070047namespace {
48
Alex Deymoebf6e122017-03-10 16:12:01 -080049// Android properties that identify the hardware and potentially non-updatable
50// parts of the bootloader (such as the bootloader version and the baseband
51// version).
52const char kPropBootBootloader[] = "ro.boot.bootloader";
53const char kPropBootBaseband[] = "ro.boot.baseband";
54const char kPropProductManufacturer[] = "ro.product.manufacturer";
55const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku";
56const char kPropBootRevision[] = "ro.boot.revision";
Sen Jiang5011df62017-06-28 17:13:19 -070057const char kPropBuildDateUTC[] = "ro.build.date.utc";
Alex Deymoebf6e122017-03-10 16:12:01 -080058
Yifan Hongfd6640f2020-08-27 19:13:17 -070059string GetPartitionBuildDate(const string& partition_name) {
60 return android::base::GetProperty("ro." + partition_name + ".build.date.utc",
61 "");
62}
63
Alex Deymofb905d92016-06-03 19:26:58 -070064} // namespace
65
Alex Deymo40d86b22015-09-03 22:27:10 -070066namespace hardware {
67
68// Factory defined in hardware.h.
69std::unique_ptr<HardwareInterface> CreateHardware() {
Ben Chanab5a0af2017-10-12 14:57:50 -070070 return std::make_unique<HardwareAndroid>();
Alex Deymo40d86b22015-09-03 22:27:10 -070071}
72
73} // namespace hardware
74
Alex Deymo1c4e84a2015-09-22 16:58:10 -070075// In Android there are normally three kinds of builds: eng, userdebug and user.
76// These builds target respectively a developer build, a debuggable version of
77// the final product and the pristine final product the end user will run.
78// Apart from the ro.build.type property name, they differ in the following
79// properties that characterize the builds:
80// * eng builds: ro.secure=0 and ro.debuggable=1
81// * userdebug builds: ro.secure=1 and ro.debuggable=1
82// * user builds: ro.secure=1 and ro.debuggable=0
83//
84// See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
85// Android.
86
Alex Deymo40d86b22015-09-03 22:27:10 -070087bool HardwareAndroid::IsOfficialBuild() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -070088 // We run an official build iff ro.secure == 1, because we expect the build to
89 // behave like the end user product and check for updates. Note that while
90 // developers are able to build "official builds" by just running "make user",
91 // that will only result in a more restrictive environment. The important part
92 // is that we don't produce and push "non-official" builds to the end user.
93 //
94 // In case of a non-bool value, we take the most restrictive option and
95 // assume we are in an official-build.
Tom Cherryfadd03c2017-10-10 14:45:09 -070096 return GetBoolProperty("ro.secure", true);
Alex Deymo40d86b22015-09-03 22:27:10 -070097}
98
99bool HardwareAndroid::IsNormalBootMode() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700100 // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
101 // update_engine will allow extra developers options, such as providing a
102 // different update URL. In case of error, we assume the build is in
103 // normal-mode.
Tom Cherryfadd03c2017-10-10 14:45:09 -0700104 return !GetBoolProperty("ro.debuggable", false);
Alex Deymo40d86b22015-09-03 22:27:10 -0700105}
106
Sen Jiange67bb5b2016-06-20 15:53:56 -0700107bool HardwareAndroid::AreDevFeaturesEnabled() const {
108 return !IsNormalBootMode();
109}
110
Alex Deymo46a9aae2016-05-04 20:20:11 -0700111bool HardwareAndroid::IsOOBEEnabled() const {
112 // No OOBE flow blocking updates for Android-based boards.
113 return false;
114}
115
Alex Deymo40d86b22015-09-03 22:27:10 -0700116bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700117 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
Alex Deymo4d2990d2015-09-15 12:11:26 -0700118 if (out_time_of_oobe)
119 *out_time_of_oobe = base::Time();
Alex Deymo40d86b22015-09-03 22:27:10 -0700120 return true;
121}
122
123string HardwareAndroid::GetHardwareClass() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700124 auto manufacturer = GetProperty(kPropProductManufacturer, "");
125 auto sku = GetProperty(kPropBootHardwareSKU, "");
126 auto revision = GetProperty(kPropBootRevision, "");
Alex Deymoebf6e122017-03-10 16:12:01 -0800127
Tom Cherryfadd03c2017-10-10 14:45:09 -0700128 return manufacturer + ":" + sku + ":" + revision;
Alex Deymo40d86b22015-09-03 22:27:10 -0700129}
130
131string HardwareAndroid::GetFirmwareVersion() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700132 return GetProperty(kPropBootBootloader, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700133}
134
135string HardwareAndroid::GetECVersion() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700136 return GetProperty(kPropBootBaseband, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700137}
138
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -0400139string HardwareAndroid::GetDeviceRequisition() const {
140 LOG(WARNING) << "STUB: Getting requisition is not supported.";
141 return "";
142}
143
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800144int HardwareAndroid::GetMinKernelKeyVersion() const {
145 LOG(WARNING) << "STUB: No Kernel key version is available.";
146 return -1;
147}
148
Marton Hunyady99ced782018-05-08 12:59:50 +0200149int HardwareAndroid::GetMinFirmwareKeyVersion() const {
150 LOG(WARNING) << "STUB: No Firmware key version is available.";
151 return -1;
152}
153
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -0700154int HardwareAndroid::GetMaxFirmwareKeyRollforward() const {
155 LOG(WARNING) << "STUB: Getting firmware_max_rollforward is not supported.";
156 return -1;
157}
158
159bool HardwareAndroid::SetMaxFirmwareKeyRollforward(
160 int firmware_max_rollforward) {
161 LOG(WARNING) << "STUB: Setting firmware_max_rollforward is not supported.";
162 return false;
163}
164
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700165bool HardwareAndroid::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
166 LOG(WARNING) << "STUB: Setting kernel_max_rollforward is not supported.";
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800167 return false;
168}
169
Alex Deymo40d86b22015-09-03 22:27:10 -0700170int HardwareAndroid::GetPowerwashCount() const {
171 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
172 return 0;
173}
174
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800175bool HardwareAndroid::SchedulePowerwash(bool save_rollback_data) {
Alex Deymofb905d92016-06-03 19:26:58 -0700176 LOG(INFO) << "Scheduling a powerwash to BCB.";
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800177 LOG_IF(WARNING, save_rollback_data) << "save_rollback_data was true but "
178 << "isn't supported.";
Sen Jiangd944faa2018-08-22 18:46:39 -0700179 string err;
180 if (!update_bootloader_message({"--wipe_data", "--reason=wipe_data_from_ota"},
181 &err)) {
182 LOG(ERROR) << "Failed to update bootloader message: " << err;
183 return false;
184 }
185 return true;
Alex Deymofb905d92016-06-03 19:26:58 -0700186}
187
188bool HardwareAndroid::CancelPowerwash() {
Sen Jiangd944faa2018-08-22 18:46:39 -0700189 string err;
190 if (!clear_bootloader_message(&err)) {
191 LOG(ERROR) << "Failed to clear bootloader message: " << err;
192 return false;
193 }
194 return true;
Alex Deymofb905d92016-06-03 19:26:58 -0700195}
196
Alex Deymodd132f32015-09-14 19:12:07 -0700197bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800198 base::FilePath local_path(constants::kNonVolatileDirectory);
Kelvin Zhangd2822522020-07-07 17:20:58 -0400199 if (!base::DirectoryExists(local_path)) {
Alex Deymodd132f32015-09-14 19:12:07 -0700200 LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
201 return false;
202 }
203 *path = local_path;
204 return true;
205}
206
207bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
208 // On Android, we don't have a directory persisted across powerwash.
209 return false;
210}
211
Sen Jiang5011df62017-06-28 17:13:19 -0700212int64_t HardwareAndroid::GetBuildTimestamp() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700213 return GetIntProperty<int64_t>(kPropBuildDateUTC, 0);
Sen Jiang5011df62017-06-28 17:13:19 -0700214}
215
Tianjie Xu4ad3af62019-10-30 11:59:45 -0700216// Returns true if the device runs an userdebug build, and explicitly allows OTA
217// downgrade.
218bool HardwareAndroid::AllowDowngrade() const {
219 return GetBoolProperty("ro.ota.allow_downgrade", false) &&
220 GetBoolProperty("ro.debuggable", false);
221}
222
Amin Hassani1677e812017-06-21 13:36:36 -0700223bool HardwareAndroid::GetFirstActiveOmahaPingSent() const {
224 LOG(WARNING) << "STUB: Assuming first active omaha was never set.";
225 return false;
226}
227
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700228bool HardwareAndroid::SetFirstActiveOmahaPingSent() {
229 LOG(WARNING) << "STUB: Assuming first active omaha is set.";
230 // We will set it true, so its failure doesn't cause escalation.
231 return true;
Amin Hassani1677e812017-06-21 13:36:36 -0700232}
233
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800234void HardwareAndroid::SetWarmReset(bool warm_reset) {
235 constexpr char warm_reset_prop[] = "ota.warm_reset";
236 if (!android::base::SetProperty(warm_reset_prop, warm_reset ? "1" : "0")) {
237 LOG(WARNING) << "Failed to set prop " << warm_reset_prop;
238 }
239}
240
Yifan Hongfd6640f2020-08-27 19:13:17 -0700241string HardwareAndroid::GetVersionForLogging(
242 const string& partition_name) const {
243 if (partition_name == "boot") {
244 struct utsname buf;
245 if (uname(&buf) != 0) {
246 PLOG(ERROR) << "Unable to call uname()";
247 return "";
248 }
249 auto kernel_release =
250 KernelRelease::Parse(buf.release, true /* allow_suffix */);
251 return kernel_release.has_value() ? kernel_release->string() : "";
252 }
253 return GetPartitionBuildDate(partition_name);
Kelvin Zhangd7191032020-08-11 10:48:16 -0400254}
255
Yifan Hong87029332020-09-01 17:20:08 -0700256ErrorCode HardwareAndroid::IsPartitionUpdateValid(
Yifan Hongfd6640f2020-08-27 19:13:17 -0700257 const string& partition_name, const string& new_version) const {
258 if (partition_name == "boot") {
259 struct utsname buf;
260 if (uname(&buf) != 0) {
261 PLOG(ERROR) << "Unable to call uname()";
262 return ErrorCode::kError;
263 }
264 return IsKernelUpdateValid(buf.release, new_version);
265 }
266
267 const auto old_version = GetPartitionBuildDate(partition_name);
Kelvin Zhangd7191032020-08-11 10:48:16 -0400268 // TODO(zhangkelvin) for some partitions, missing a current timestamp should
269 // be an error, e.g. system, vendor, product etc.
Yifan Hong87029332020-09-01 17:20:08 -0700270 auto error_code = utils::IsTimestampNewer(old_version, new_version);
271 if (error_code != ErrorCode::kSuccess) {
272 LOG(ERROR) << "Timestamp check failed with "
273 << utils::ErrorCodeToString(error_code)
274 << " Partition timestamp: " << old_version
Kelvin Zhangd7191032020-08-11 10:48:16 -0400275 << " Update timestamp: " << new_version;
276 }
Yifan Hong87029332020-09-01 17:20:08 -0700277 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -0400278}
279
Yifan Hongfd6640f2020-08-27 19:13:17 -0700280ErrorCode HardwareAndroid::IsKernelUpdateValid(const string& old_release,
281 const string& new_release) {
282 // Check that the package either contain an empty version (indicating that the
283 // new build does not use GKI), or a valid GKI kernel release.
284 std::optional<KernelRelease> new_kernel_release;
285 if (new_release.empty()) {
286 LOG(INFO) << "New build does not contain GKI.";
287 } else {
288 new_kernel_release =
289 KernelRelease::Parse(new_release, true /* allow_suffix */);
290 if (!new_kernel_release.has_value()) {
291 LOG(ERROR) << "New kernel release is not valid GKI kernel release: "
292 << new_release;
293 return ErrorCode::kDownloadManifestParseError;
294 }
295 }
296
297 auto old_kernel_release =
298 KernelRelease::Parse(old_release, true /* allow_suffix */);
299 return android::kver::IsKernelUpdateValid(old_kernel_release,
300 new_kernel_release)
301 ? ErrorCode::kSuccess
302 : ErrorCode::kPayloadTimestampError;
303}
304
Alex Deymo40d86b22015-09-03 22:27:10 -0700305} // namespace chromeos_update_engine