blob: 5872c54dc1803348750bf08eaf8451a12ef4a4d4 [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 Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/make_unique_ptr.h>
Alex Deymo40d86b22015-09-03 22:27:10 -070032
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/hardware.h"
Sen Jiang9c123462015-11-19 13:16:23 -080034#include "update_engine/common/platform_constants.h"
Alex Deymofb905d92016-06-03 19:26:58 -070035#include "update_engine/common/utils.h"
36#include "update_engine/utils_android.h"
Alex Deymo40d86b22015-09-03 22:27:10 -070037
Tom Cherry012aa5b2017-10-10 14:45:09 -070038using android::base::GetBoolProperty;
39using 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";
60
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() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700100 return brillo::make_unique_ptr(new 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 Cherry012aa5b2017-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 Cherry012aa5b2017-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 Cherry012aa5b2017-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 Cherry012aa5b2017-10-10 14:45:09 -0700158 return manufacturer + ":" + sku + ":" + revision;
Alex Deymo40d86b22015-09-03 22:27:10 -0700159}
160
161string HardwareAndroid::GetFirmwareVersion() const {
Tom Cherry012aa5b2017-10-10 14:45:09 -0700162 return GetProperty(kPropBootBootloader, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700163}
164
165string HardwareAndroid::GetECVersion() const {
Tom Cherry012aa5b2017-10-10 14:45:09 -0700166 return GetProperty(kPropBootBaseband, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700167}
168
169int HardwareAndroid::GetPowerwashCount() const {
170 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
171 return 0;
172}
173
Alex Deymofb905d92016-06-03 19:26:58 -0700174bool HardwareAndroid::SchedulePowerwash() {
175 LOG(INFO) << "Scheduling a powerwash to BCB.";
176 return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand);
177}
178
179bool HardwareAndroid::CancelPowerwash() {
180 return WriteBootloaderRecoveryMessage("");
181}
182
Alex Deymodd132f32015-09-14 19:12:07 -0700183bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800184 base::FilePath local_path(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700185 if (!base::PathExists(local_path)) {
186 LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
187 return false;
188 }
189 *path = local_path;
190 return true;
191}
192
193bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
194 // On Android, we don't have a directory persisted across powerwash.
195 return false;
196}
197
Alex Deymo40d86b22015-09-03 22:27:10 -0700198} // namespace chromeos_update_engine