blob: 9dd8bb61fe7222a92c5d15f813ebec2237860e19 [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 <fcntl.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <algorithm>
Tom Cherryfadd03c2017-10-10 14:45:09 -070024#include <memory>
Alex Deymofb905d92016-06-03 19:26:58 -070025
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>
Alex Deymoebf6e122017-03-10 16:12:01 -080028#include <base/strings/stringprintf.h>
Tao Bao304680c2018-03-31 10:36:52 -070029#include <bootloader_message/bootloader_message.h>
Alex Deymo40d86b22015-09-03 22:27:10 -070030
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"
Alex Deymofb905d92016-06-03 19:26:58 -070033#include "update_engine/common/utils.h"
34#include "update_engine/utils_android.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070035
Tom Cherryfadd03c2017-10-10 14:45:09 -070036using android::base::GetBoolProperty;
37using android::base::GetIntProperty;
38using android::base::GetProperty;
Alex Deymo40d86b22015-09-03 22:27:10 -070039using std::string;
40
41namespace chromeos_update_engine {
42
Alex Deymofb905d92016-06-03 19:26:58 -070043namespace {
44
45// The powerwash arguments passed to recovery. Arguments are separated by \n.
46const char kAndroidRecoveryPowerwashCommand[] =
47 "recovery\n"
48 "--wipe_data\n"
49 "--reason=wipe_data_from_ota\n";
50
Alex Deymoebf6e122017-03-10 16:12:01 -080051// Android properties that identify the hardware and potentially non-updatable
52// parts of the bootloader (such as the bootloader version and the baseband
53// version).
54const char kPropBootBootloader[] = "ro.boot.bootloader";
55const char kPropBootBaseband[] = "ro.boot.baseband";
56const char kPropProductManufacturer[] = "ro.product.manufacturer";
57const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku";
58const char kPropBootRevision[] = "ro.boot.revision";
Sen Jiang5011df62017-06-28 17:13:19 -070059const char kPropBuildDateUTC[] = "ro.build.date.utc";
Alex Deymoebf6e122017-03-10 16:12:01 -080060
Alex Deymofb905d92016-06-03 19:26:58 -070061// Write a recovery command line |message| to the BCB. The arguments to recovery
62// must be separated by '\n'. An empty string will erase the BCB.
63bool WriteBootloaderRecoveryMessage(const string& message) {
64 base::FilePath misc_device;
65 if (!utils::DeviceForMountPoint("/misc", &misc_device))
66 return false;
67
68 // Setup a bootloader_message with just the command and recovery fields set.
69 bootloader_message boot = {};
70 if (!message.empty()) {
71 strncpy(boot.command, "boot-recovery", sizeof(boot.command) - 1);
72 memcpy(boot.recovery,
73 message.data(),
74 std::min(message.size(), sizeof(boot.recovery) - 1));
75 }
76
George Burgess IV5a46a182017-07-27 23:26:03 -070077 int fd = HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC));
Alex Deymofb905d92016-06-03 19:26:58 -070078 if (fd < 0) {
79 PLOG(ERROR) << "Opening misc";
80 return false;
81 }
82 ScopedFdCloser fd_closer(&fd);
83 // We only re-write the first part of the bootloader_message, up to and
84 // including the recovery message.
85 size_t boot_size =
86 offsetof(bootloader_message, recovery) + sizeof(boot.recovery);
87 if (!utils::WriteAll(fd, &boot, boot_size)) {
88 PLOG(ERROR) << "Writing recovery command to misc";
89 return false;
90 }
91 return true;
92}
93
94} // namespace
95
Alex Deymo40d86b22015-09-03 22:27:10 -070096namespace hardware {
97
98// Factory defined in hardware.h.
99std::unique_ptr<HardwareInterface> CreateHardware() {
Ben Chanab5a0af2017-10-12 14:57:50 -0700100 return std::make_unique<HardwareAndroid>();
Alex Deymo40d86b22015-09-03 22:27:10 -0700101}
102
103} // namespace hardware
104
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700105// In Android there are normally three kinds of builds: eng, userdebug and user.
106// These builds target respectively a developer build, a debuggable version of
107// the final product and the pristine final product the end user will run.
108// Apart from the ro.build.type property name, they differ in the following
109// properties that characterize the builds:
110// * eng builds: ro.secure=0 and ro.debuggable=1
111// * userdebug builds: ro.secure=1 and ro.debuggable=1
112// * user builds: ro.secure=1 and ro.debuggable=0
113//
114// See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
115// Android.
116
Alex Deymo40d86b22015-09-03 22:27:10 -0700117bool HardwareAndroid::IsOfficialBuild() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700118 // We run an official build iff ro.secure == 1, because we expect the build to
119 // behave like the end user product and check for updates. Note that while
120 // developers are able to build "official builds" by just running "make user",
121 // that will only result in a more restrictive environment. The important part
122 // is that we don't produce and push "non-official" builds to the end user.
123 //
124 // In case of a non-bool value, we take the most restrictive option and
125 // assume we are in an official-build.
Tom Cherryfadd03c2017-10-10 14:45:09 -0700126 return GetBoolProperty("ro.secure", true);
Alex Deymo40d86b22015-09-03 22:27:10 -0700127}
128
129bool HardwareAndroid::IsNormalBootMode() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700130 // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
131 // update_engine will allow extra developers options, such as providing a
132 // different update URL. In case of error, we assume the build is in
133 // normal-mode.
Tom Cherryfadd03c2017-10-10 14:45:09 -0700134 return !GetBoolProperty("ro.debuggable", false);
Alex Deymo40d86b22015-09-03 22:27:10 -0700135}
136
Sen Jiange67bb5b2016-06-20 15:53:56 -0700137bool HardwareAndroid::AreDevFeaturesEnabled() const {
138 return !IsNormalBootMode();
139}
140
Alex Deymo46a9aae2016-05-04 20:20:11 -0700141bool HardwareAndroid::IsOOBEEnabled() const {
142 // No OOBE flow blocking updates for Android-based boards.
143 return false;
144}
145
Alex Deymo40d86b22015-09-03 22:27:10 -0700146bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700147 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
Alex Deymo4d2990d2015-09-15 12:11:26 -0700148 if (out_time_of_oobe)
149 *out_time_of_oobe = base::Time();
Alex Deymo40d86b22015-09-03 22:27:10 -0700150 return true;
151}
152
153string HardwareAndroid::GetHardwareClass() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700154 auto manufacturer = GetProperty(kPropProductManufacturer, "");
155 auto sku = GetProperty(kPropBootHardwareSKU, "");
156 auto revision = GetProperty(kPropBootRevision, "");
Alex Deymoebf6e122017-03-10 16:12:01 -0800157
Tom Cherryfadd03c2017-10-10 14:45:09 -0700158 return manufacturer + ":" + sku + ":" + revision;
Alex Deymo40d86b22015-09-03 22:27:10 -0700159}
160
161string HardwareAndroid::GetFirmwareVersion() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700162 return GetProperty(kPropBootBootloader, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700163}
164
165string HardwareAndroid::GetECVersion() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700166 return GetProperty(kPropBootBaseband, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700167}
168
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800169int HardwareAndroid::GetMinKernelKeyVersion() const {
170 LOG(WARNING) << "STUB: No Kernel key version is available.";
171 return -1;
172}
173
Marton Hunyady99ced782018-05-08 12:59:50 +0200174int HardwareAndroid::GetMinFirmwareKeyVersion() const {
175 LOG(WARNING) << "STUB: No Firmware key version is available.";
176 return -1;
177}
178
Zentaro Kavanagh8f6f2432018-05-16 13:48:12 -0700179int HardwareAndroid::GetMaxFirmwareKeyRollforward() const {
180 LOG(WARNING) << "STUB: Getting firmware_max_rollforward is not supported.";
181 return -1;
182}
183
184bool HardwareAndroid::SetMaxFirmwareKeyRollforward(
185 int firmware_max_rollforward) {
186 LOG(WARNING) << "STUB: Setting firmware_max_rollforward is not supported.";
187 return false;
188}
189
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700190bool HardwareAndroid::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
191 LOG(WARNING) << "STUB: Setting kernel_max_rollforward is not supported.";
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800192 return false;
193}
194
Alex Deymo40d86b22015-09-03 22:27:10 -0700195int HardwareAndroid::GetPowerwashCount() const {
196 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
197 return 0;
198}
199
Alex Deymofb905d92016-06-03 19:26:58 -0700200bool HardwareAndroid::SchedulePowerwash() {
201 LOG(INFO) << "Scheduling a powerwash to BCB.";
202 return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand);
203}
204
205bool HardwareAndroid::CancelPowerwash() {
206 return WriteBootloaderRecoveryMessage("");
207}
208
Alex Deymodd132f32015-09-14 19:12:07 -0700209bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800210 base::FilePath local_path(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700211 if (!base::PathExists(local_path)) {
212 LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
213 return false;
214 }
215 *path = local_path;
216 return true;
217}
218
219bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
220 // On Android, we don't have a directory persisted across powerwash.
221 return false;
222}
223
Sen Jiang5011df62017-06-28 17:13:19 -0700224int64_t HardwareAndroid::GetBuildTimestamp() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700225 return GetIntProperty<int64_t>(kPropBuildDateUTC, 0);
Sen Jiang5011df62017-06-28 17:13:19 -0700226}
227
Amin Hassani1677e812017-06-21 13:36:36 -0700228bool HardwareAndroid::GetFirstActiveOmahaPingSent() const {
229 LOG(WARNING) << "STUB: Assuming first active omaha was never set.";
230 return false;
231}
232
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700233bool HardwareAndroid::SetFirstActiveOmahaPingSent() {
234 LOG(WARNING) << "STUB: Assuming first active omaha is set.";
235 // We will set it true, so its failure doesn't cause escalation.
236 return true;
Amin Hassani1677e812017-06-21 13:36:36 -0700237}
238
Alex Deymo40d86b22015-09-03 22:27:10 -0700239} // namespace chromeos_update_engine