blob: 0e5abaadc18a1bb23e19f19cb794b8c5fcfabce8 [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;
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";
59
Alex Deymofb905d92016-06-03 19:26:58 -070060// Write a recovery command line |message| to the BCB. The arguments to recovery
61// must be separated by '\n'. An empty string will erase the BCB.
62bool WriteBootloaderRecoveryMessage(const string& message) {
63 base::FilePath misc_device;
64 if (!utils::DeviceForMountPoint("/misc", &misc_device))
65 return false;
66
67 // Setup a bootloader_message with just the command and recovery fields set.
68 bootloader_message boot = {};
69 if (!message.empty()) {
70 strncpy(boot.command, "boot-recovery", sizeof(boot.command) - 1);
71 memcpy(boot.recovery,
72 message.data(),
73 std::min(message.size(), sizeof(boot.recovery) - 1));
74 }
75
George Burgess IV5a46a182017-07-27 23:26:03 -070076 int fd = HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC));
Alex Deymofb905d92016-06-03 19:26:58 -070077 if (fd < 0) {
78 PLOG(ERROR) << "Opening misc";
79 return false;
80 }
81 ScopedFdCloser fd_closer(&fd);
82 // We only re-write the first part of the bootloader_message, up to and
83 // including the recovery message.
84 size_t boot_size =
85 offsetof(bootloader_message, recovery) + sizeof(boot.recovery);
86 if (!utils::WriteAll(fd, &boot, boot_size)) {
87 PLOG(ERROR) << "Writing recovery command to misc";
88 return false;
89 }
90 return true;
91}
92
93} // namespace
94
Alex Deymo40d86b22015-09-03 22:27:10 -070095namespace hardware {
96
97// Factory defined in hardware.h.
98std::unique_ptr<HardwareInterface> CreateHardware() {
Ben Chanab5a0af2017-10-12 14:57:50 -070099 return std::make_unique<HardwareAndroid>();
Alex Deymo40d86b22015-09-03 22:27:10 -0700100}
101
102} // namespace hardware
103
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700104// In Android there are normally three kinds of builds: eng, userdebug and user.
105// These builds target respectively a developer build, a debuggable version of
106// the final product and the pristine final product the end user will run.
107// Apart from the ro.build.type property name, they differ in the following
108// properties that characterize the builds:
109// * eng builds: ro.secure=0 and ro.debuggable=1
110// * userdebug builds: ro.secure=1 and ro.debuggable=1
111// * user builds: ro.secure=1 and ro.debuggable=0
112//
113// See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
114// Android.
115
Alex Deymo40d86b22015-09-03 22:27:10 -0700116bool HardwareAndroid::IsOfficialBuild() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700117 // We run an official build iff ro.secure == 1, because we expect the build to
118 // behave like the end user product and check for updates. Note that while
119 // developers are able to build "official builds" by just running "make user",
120 // that will only result in a more restrictive environment. The important part
121 // is that we don't produce and push "non-official" builds to the end user.
122 //
123 // In case of a non-bool value, we take the most restrictive option and
124 // assume we are in an official-build.
Tom Cherry012aa5b2017-10-10 14:45:09 -0700125 return GetBoolProperty("ro.secure", true);
Alex Deymo40d86b22015-09-03 22:27:10 -0700126}
127
128bool HardwareAndroid::IsNormalBootMode() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700129 // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
130 // update_engine will allow extra developers options, such as providing a
131 // different update URL. In case of error, we assume the build is in
132 // normal-mode.
Tom Cherry012aa5b2017-10-10 14:45:09 -0700133 return !GetBoolProperty("ro.debuggable", false);
Alex Deymo40d86b22015-09-03 22:27:10 -0700134}
135
Sen Jiange67bb5b2016-06-20 15:53:56 -0700136bool HardwareAndroid::AreDevFeaturesEnabled() const {
137 return !IsNormalBootMode();
138}
139
Alex Deymo46a9aae2016-05-04 20:20:11 -0700140bool HardwareAndroid::IsOOBEEnabled() const {
141 // No OOBE flow blocking updates for Android-based boards.
142 return false;
143}
144
Alex Deymo40d86b22015-09-03 22:27:10 -0700145bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700146 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
Alex Deymo4d2990d2015-09-15 12:11:26 -0700147 if (out_time_of_oobe)
148 *out_time_of_oobe = base::Time();
Alex Deymo40d86b22015-09-03 22:27:10 -0700149 return true;
150}
151
152string HardwareAndroid::GetHardwareClass() const {
Tom Cherry012aa5b2017-10-10 14:45:09 -0700153 auto manufacturer = GetProperty(kPropProductManufacturer, "");
154 auto sku = GetProperty(kPropBootHardwareSKU, "");
155 auto revision = GetProperty(kPropBootRevision, "");
Alex Deymoebf6e122017-03-10 16:12:01 -0800156
Tom Cherry012aa5b2017-10-10 14:45:09 -0700157 return manufacturer + ":" + sku + ":" + revision;
Alex Deymo40d86b22015-09-03 22:27:10 -0700158}
159
160string HardwareAndroid::GetFirmwareVersion() const {
Tom Cherry012aa5b2017-10-10 14:45:09 -0700161 return GetProperty(kPropBootBootloader, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700162}
163
164string HardwareAndroid::GetECVersion() const {
Tom Cherry012aa5b2017-10-10 14:45:09 -0700165 return GetProperty(kPropBootBaseband, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700166}
167
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800168int HardwareAndroid::GetMinKernelKeyVersion() const {
169 LOG(WARNING) << "STUB: No Kernel key version is available.";
170 return -1;
171}
172
Marton Hunyady99ced782018-05-08 12:59:50 +0200173int HardwareAndroid::GetMinFirmwareKeyVersion() const {
174 LOG(WARNING) << "STUB: No Firmware key version is available.";
175 return -1;
176}
177
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700178bool HardwareAndroid::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
179 LOG(WARNING) << "STUB: Setting kernel_max_rollforward is not supported.";
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800180 return false;
181}
182
Alex Deymo40d86b22015-09-03 22:27:10 -0700183int HardwareAndroid::GetPowerwashCount() const {
184 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
185 return 0;
186}
187
Alex Deymofb905d92016-06-03 19:26:58 -0700188bool HardwareAndroid::SchedulePowerwash() {
189 LOG(INFO) << "Scheduling a powerwash to BCB.";
190 return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand);
191}
192
193bool HardwareAndroid::CancelPowerwash() {
194 return WriteBootloaderRecoveryMessage("");
195}
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);
Alex Deymodd132f32015-09-14 19:12:07 -0700199 if (!base::PathExists(local_path)) {
200 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
Amin Hassani1677e812017-06-21 13:36:36 -0700212bool HardwareAndroid::GetFirstActiveOmahaPingSent() const {
213 LOG(WARNING) << "STUB: Assuming first active omaha was never set.";
214 return false;
215}
216
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700217bool HardwareAndroid::SetFirstActiveOmahaPingSent() {
218 LOG(WARNING) << "STUB: Assuming first active omaha is set.";
219 // We will set it true, so its failure doesn't cause escalation.
220 return true;
Amin Hassani1677e812017-06-21 13:36:36 -0700221}
222
Alex Deymo40d86b22015-09-03 22:27:10 -0700223} // namespace chromeos_update_engine