blob: 4c5473f139ec8b5f74c25181f2eab98064098dd9 [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>
24
25#include <bootloader.h>
26
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>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029#include <brillo/make_unique_ptr.h>
Alex Deymo1c4e84a2015-09-22 16:58:10 -070030#include <cutils/properties.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
37using std::string;
38
39namespace chromeos_update_engine {
40
Alex Deymofb905d92016-06-03 19:26:58 -070041namespace {
42
43// The powerwash arguments passed to recovery. Arguments are separated by \n.
44const char kAndroidRecoveryPowerwashCommand[] =
45 "recovery\n"
46 "--wipe_data\n"
47 "--reason=wipe_data_from_ota\n";
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";
57
Alex Deymofb905d92016-06-03 19:26:58 -070058// Write a recovery command line |message| to the BCB. The arguments to recovery
59// must be separated by '\n'. An empty string will erase the BCB.
60bool WriteBootloaderRecoveryMessage(const string& message) {
61 base::FilePath misc_device;
62 if (!utils::DeviceForMountPoint("/misc", &misc_device))
63 return false;
64
65 // Setup a bootloader_message with just the command and recovery fields set.
66 bootloader_message boot = {};
67 if (!message.empty()) {
68 strncpy(boot.command, "boot-recovery", sizeof(boot.command) - 1);
69 memcpy(boot.recovery,
70 message.data(),
71 std::min(message.size(), sizeof(boot.recovery) - 1));
72 }
73
George Burgess IV5a46a182017-07-27 23:26:03 -070074 int fd = HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC));
Alex Deymofb905d92016-06-03 19:26:58 -070075 if (fd < 0) {
76 PLOG(ERROR) << "Opening misc";
77 return false;
78 }
79 ScopedFdCloser fd_closer(&fd);
80 // We only re-write the first part of the bootloader_message, up to and
81 // including the recovery message.
82 size_t boot_size =
83 offsetof(bootloader_message, recovery) + sizeof(boot.recovery);
84 if (!utils::WriteAll(fd, &boot, boot_size)) {
85 PLOG(ERROR) << "Writing recovery command to misc";
86 return false;
87 }
88 return true;
89}
90
91} // namespace
92
Alex Deymo40d86b22015-09-03 22:27:10 -070093namespace hardware {
94
95// Factory defined in hardware.h.
96std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070097 return brillo::make_unique_ptr(new HardwareAndroid());
Alex Deymo40d86b22015-09-03 22:27:10 -070098}
99
100} // namespace hardware
101
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700102// In Android there are normally three kinds of builds: eng, userdebug and user.
103// These builds target respectively a developer build, a debuggable version of
104// the final product and the pristine final product the end user will run.
105// Apart from the ro.build.type property name, they differ in the following
106// properties that characterize the builds:
107// * eng builds: ro.secure=0 and ro.debuggable=1
108// * userdebug builds: ro.secure=1 and ro.debuggable=1
109// * user builds: ro.secure=1 and ro.debuggable=0
110//
111// See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
112// Android.
113
Alex Deymo40d86b22015-09-03 22:27:10 -0700114bool HardwareAndroid::IsOfficialBuild() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700115 // We run an official build iff ro.secure == 1, because we expect the build to
116 // behave like the end user product and check for updates. Note that while
117 // developers are able to build "official builds" by just running "make user",
118 // that will only result in a more restrictive environment. The important part
119 // is that we don't produce and push "non-official" builds to the end user.
120 //
121 // In case of a non-bool value, we take the most restrictive option and
122 // assume we are in an official-build.
123 return property_get_bool("ro.secure", 1) != 0;
Alex Deymo40d86b22015-09-03 22:27:10 -0700124}
125
126bool HardwareAndroid::IsNormalBootMode() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700127 // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
128 // update_engine will allow extra developers options, such as providing a
129 // different update URL. In case of error, we assume the build is in
130 // normal-mode.
131 return property_get_bool("ro.debuggable", 0) != 1;
Alex Deymo40d86b22015-09-03 22:27:10 -0700132}
133
Sen Jiange67bb5b2016-06-20 15:53:56 -0700134bool HardwareAndroid::AreDevFeaturesEnabled() const {
135 return !IsNormalBootMode();
136}
137
Alex Deymo46a9aae2016-05-04 20:20:11 -0700138bool HardwareAndroid::IsOOBEEnabled() const {
139 // No OOBE flow blocking updates for Android-based boards.
140 return false;
141}
142
Alex Deymo40d86b22015-09-03 22:27:10 -0700143bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700144 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
Alex Deymo4d2990d2015-09-15 12:11:26 -0700145 if (out_time_of_oobe)
146 *out_time_of_oobe = base::Time();
Alex Deymo40d86b22015-09-03 22:27:10 -0700147 return true;
148}
149
150string HardwareAndroid::GetHardwareClass() const {
Alex Deymoebf6e122017-03-10 16:12:01 -0800151 char manufacturer[PROPERTY_VALUE_MAX];
152 char sku[PROPERTY_VALUE_MAX];
153 char revision[PROPERTY_VALUE_MAX];
154 property_get(kPropBootHardwareSKU, sku, "");
155 property_get(kPropProductManufacturer, manufacturer, "");
156 property_get(kPropBootRevision, revision, "");
157
158 return base::StringPrintf("%s:%s:%s", manufacturer, sku, revision);
Alex Deymo40d86b22015-09-03 22:27:10 -0700159}
160
161string HardwareAndroid::GetFirmwareVersion() const {
Alex Deymoebf6e122017-03-10 16:12:01 -0800162 char bootloader[PROPERTY_VALUE_MAX];
163 property_get(kPropBootBootloader, bootloader, "");
164 return bootloader;
Alex Deymo40d86b22015-09-03 22:27:10 -0700165}
166
167string HardwareAndroid::GetECVersion() const {
Alex Deymoebf6e122017-03-10 16:12:01 -0800168 char baseband[PROPERTY_VALUE_MAX];
169 property_get(kPropBootBaseband, baseband, "");
170 return baseband;
Alex Deymo40d86b22015-09-03 22:27:10 -0700171}
172
173int HardwareAndroid::GetPowerwashCount() const {
174 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
175 return 0;
176}
177
Alex Deymofb905d92016-06-03 19:26:58 -0700178bool HardwareAndroid::SchedulePowerwash() {
179 LOG(INFO) << "Scheduling a powerwash to BCB.";
180 return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand);
181}
182
183bool HardwareAndroid::CancelPowerwash() {
184 return WriteBootloaderRecoveryMessage("");
185}
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);
Alex Deymodd132f32015-09-14 19:12:07 -0700189 if (!base::PathExists(local_path)) {
190 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
Alex Deymo40d86b22015-09-03 22:27:10 -0700202} // namespace chromeos_update_engine