blob: 947b13aa6c479cf95684a6c7512821e8b066d68a [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 Cherry012aa5b2017-10-10 14:45:09 -070024#include <memory>
Alex Deymofb905d92016-06-03 19:26:58 -070025
26#include <bootloader.h>
27
Tom Cherry012aa5b2017-10-10 14:45:09 -070028#include <android-base/properties.h>
Alex Deymodd132f32015-09-14 19:12:07 -070029#include <base/files/file_util.h>
Alex Deymoebf6e122017-03-10 16:12:01 -080030#include <base/strings/stringprintf.h>
Alex Deymo40d86b22015-09-03 22:27:10 -070031
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/common/hardware.h"
Sen Jiang9c123462015-11-19 13:16:23 -080033#include "update_engine/common/platform_constants.h"
Alex Deymofb905d92016-06-03 19:26:58 -070034#include "update_engine/common/utils.h"
35#include "update_engine/utils_android.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070036
Tom Cherry012aa5b2017-10-10 14:45:09 -070037using android::base::GetBoolProperty;
Sen Jiang8e768e92017-06-28 17:13:19 -070038using android::base::GetIntProperty;
Tom Cherry012aa5b2017-10-10 14:45:09 -070039using android::base::GetProperty;
Alex Deymo40d86b22015-09-03 22:27:10 -070040using std::string;
41
42namespace chromeos_update_engine {
43
Alex Deymofb905d92016-06-03 19:26:58 -070044namespace {
45
46// The powerwash arguments passed to recovery. Arguments are separated by \n.
47const char kAndroidRecoveryPowerwashCommand[] =
48 "recovery\n"
49 "--wipe_data\n"
50 "--reason=wipe_data_from_ota\n";
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).
55const char kPropBootBootloader[] = "ro.boot.bootloader";
56const char kPropBootBaseband[] = "ro.boot.baseband";
57const char kPropProductManufacturer[] = "ro.product.manufacturer";
58const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku";
59const char kPropBootRevision[] = "ro.boot.revision";
Sen Jiang8e768e92017-06-28 17:13:19 -070060const char kPropBuildDateUTC[] = "ro.build.date.utc";
Alex Deymoebf6e122017-03-10 16:12:01 -080061
Alex Deymofb905d92016-06-03 19:26:58 -070062// Write a recovery command line |message| to the BCB. The arguments to recovery
63// must be separated by '\n'. An empty string will erase the BCB.
64bool WriteBootloaderRecoveryMessage(const string& message) {
65 base::FilePath misc_device;
66 if (!utils::DeviceForMountPoint("/misc", &misc_device))
67 return false;
68
69 // Setup a bootloader_message with just the command and recovery fields set.
70 bootloader_message boot = {};
71 if (!message.empty()) {
72 strncpy(boot.command, "boot-recovery", sizeof(boot.command) - 1);
73 memcpy(boot.recovery,
74 message.data(),
75 std::min(message.size(), sizeof(boot.recovery) - 1));
76 }
77
George Burgess IV5a46a182017-07-27 23:26:03 -070078 int fd = HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC));
Alex Deymofb905d92016-06-03 19:26:58 -070079 if (fd < 0) {
80 PLOG(ERROR) << "Opening misc";
81 return false;
82 }
83 ScopedFdCloser fd_closer(&fd);
84 // We only re-write the first part of the bootloader_message, up to and
85 // including the recovery message.
86 size_t boot_size =
87 offsetof(bootloader_message, recovery) + sizeof(boot.recovery);
88 if (!utils::WriteAll(fd, &boot, boot_size)) {
89 PLOG(ERROR) << "Writing recovery command to misc";
90 return false;
91 }
92 return true;
93}
94
95} // namespace
96
Alex Deymo40d86b22015-09-03 22:27:10 -070097namespace hardware {
98
99// Factory defined in hardware.h.
100std::unique_ptr<HardwareInterface> CreateHardware() {
Ben Chanab5a0af2017-10-12 14:57:50 -0700101 return std::make_unique<HardwareAndroid>();
Alex Deymo40d86b22015-09-03 22:27:10 -0700102}
103
104} // namespace hardware
105
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700106// In Android there are normally three kinds of builds: eng, userdebug and user.
107// These builds target respectively a developer build, a debuggable version of
108// the final product and the pristine final product the end user will run.
109// Apart from the ro.build.type property name, they differ in the following
110// properties that characterize the builds:
111// * eng builds: ro.secure=0 and ro.debuggable=1
112// * userdebug builds: ro.secure=1 and ro.debuggable=1
113// * user builds: ro.secure=1 and ro.debuggable=0
114//
115// See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
116// Android.
117
Alex Deymo40d86b22015-09-03 22:27:10 -0700118bool HardwareAndroid::IsOfficialBuild() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700119 // We run an official build iff ro.secure == 1, because we expect the build to
120 // behave like the end user product and check for updates. Note that while
121 // developers are able to build "official builds" by just running "make user",
122 // that will only result in a more restrictive environment. The important part
123 // is that we don't produce and push "non-official" builds to the end user.
124 //
125 // In case of a non-bool value, we take the most restrictive option and
126 // assume we are in an official-build.
Tom Cherry012aa5b2017-10-10 14:45:09 -0700127 return GetBoolProperty("ro.secure", true);
Alex Deymo40d86b22015-09-03 22:27:10 -0700128}
129
130bool HardwareAndroid::IsNormalBootMode() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700131 // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
132 // update_engine will allow extra developers options, such as providing a
133 // different update URL. In case of error, we assume the build is in
134 // normal-mode.
Tom Cherry012aa5b2017-10-10 14:45:09 -0700135 return !GetBoolProperty("ro.debuggable", false);
Alex Deymo40d86b22015-09-03 22:27:10 -0700136}
137
Sen Jiange67bb5b2016-06-20 15:53:56 -0700138bool HardwareAndroid::AreDevFeaturesEnabled() const {
139 return !IsNormalBootMode();
140}
141
Alex Deymo46a9aae2016-05-04 20:20:11 -0700142bool HardwareAndroid::IsOOBEEnabled() const {
143 // No OOBE flow blocking updates for Android-based boards.
144 return false;
145}
146
Alex Deymo40d86b22015-09-03 22:27:10 -0700147bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700148 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
Alex Deymo4d2990d2015-09-15 12:11:26 -0700149 if (out_time_of_oobe)
150 *out_time_of_oobe = base::Time();
Alex Deymo40d86b22015-09-03 22:27:10 -0700151 return true;
152}
153
154string HardwareAndroid::GetHardwareClass() const {
Tom Cherry012aa5b2017-10-10 14:45:09 -0700155 auto manufacturer = GetProperty(kPropProductManufacturer, "");
156 auto sku = GetProperty(kPropBootHardwareSKU, "");
157 auto revision = GetProperty(kPropBootRevision, "");
Alex Deymoebf6e122017-03-10 16:12:01 -0800158
Tom Cherry012aa5b2017-10-10 14:45:09 -0700159 return manufacturer + ":" + sku + ":" + revision;
Alex Deymo40d86b22015-09-03 22:27:10 -0700160}
161
162string HardwareAndroid::GetFirmwareVersion() const {
Tom Cherry012aa5b2017-10-10 14:45:09 -0700163 return GetProperty(kPropBootBootloader, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700164}
165
166string HardwareAndroid::GetECVersion() const {
Tom Cherry012aa5b2017-10-10 14:45:09 -0700167 return GetProperty(kPropBootBaseband, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700168}
169
170int HardwareAndroid::GetPowerwashCount() const {
171 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
172 return 0;
173}
174
Alex Deymofb905d92016-06-03 19:26:58 -0700175bool HardwareAndroid::SchedulePowerwash() {
176 LOG(INFO) << "Scheduling a powerwash to BCB.";
177 return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand);
178}
179
180bool HardwareAndroid::CancelPowerwash() {
181 return WriteBootloaderRecoveryMessage("");
182}
183
Alex Deymodd132f32015-09-14 19:12:07 -0700184bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800185 base::FilePath local_path(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700186 if (!base::PathExists(local_path)) {
187 LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
188 return false;
189 }
190 *path = local_path;
191 return true;
192}
193
194bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
195 // On Android, we don't have a directory persisted across powerwash.
196 return false;
197}
198
Sen Jiang8e768e92017-06-28 17:13:19 -0700199int64_t HardwareAndroid::GetBuildTimestamp() const {
200 return GetIntProperty<int64_t>(kPropBuildDateUTC, 0);
201}
202
Amin Hassani1677e812017-06-21 13:36:36 -0700203bool HardwareAndroid::GetFirstActiveOmahaPingSent() const {
204 LOG(WARNING) << "STUB: Assuming first active omaha was never set.";
205 return false;
206}
207
Sen Jiang18414082018-01-11 14:50:36 -0800208void HardwareAndroid::SetFirstActiveOmahaPingSent() {
Amin Hassani1677e812017-06-21 13:36:36 -0700209 LOG(WARNING) << "STUB: Assuming first active omaha is never set.";
210 return;
211}
212
Alex Deymo40d86b22015-09-03 22:27:10 -0700213} // namespace chromeos_update_engine