blob: 86432a9c39d73829b69d2ceae09d652bee3a0ea2 [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
26#include <bootloader.h>
27
Tom Cherryfadd03c2017-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 Cherryfadd03c2017-10-10 14:45:09 -070038using android::base::GetBoolProperty;
39using android::base::GetIntProperty;
40using android::base::GetProperty;
Alex Deymo40d86b22015-09-03 22:27:10 -070041using std::string;
42
43namespace chromeos_update_engine {
44
Alex Deymofb905d92016-06-03 19:26:58 -070045namespace {
46
47// The powerwash arguments passed to recovery. Arguments are separated by \n.
48const char kAndroidRecoveryPowerwashCommand[] =
49 "recovery\n"
50 "--wipe_data\n"
51 "--reason=wipe_data_from_ota\n";
52
Alex Deymoebf6e122017-03-10 16:12:01 -080053// Android properties that identify the hardware and potentially non-updatable
54// parts of the bootloader (such as the bootloader version and the baseband
55// version).
56const char kPropBootBootloader[] = "ro.boot.bootloader";
57const char kPropBootBaseband[] = "ro.boot.baseband";
58const char kPropProductManufacturer[] = "ro.product.manufacturer";
59const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku";
60const char kPropBootRevision[] = "ro.boot.revision";
Sen Jiang5011df62017-06-28 17:13:19 -070061const char kPropBuildDateUTC[] = "ro.build.date.utc";
Alex Deymoebf6e122017-03-10 16:12:01 -080062
Alex Deymofb905d92016-06-03 19:26:58 -070063// Write a recovery command line |message| to the BCB. The arguments to recovery
64// must be separated by '\n'. An empty string will erase the BCB.
65bool WriteBootloaderRecoveryMessage(const string& message) {
66 base::FilePath misc_device;
67 if (!utils::DeviceForMountPoint("/misc", &misc_device))
68 return false;
69
70 // Setup a bootloader_message with just the command and recovery fields set.
71 bootloader_message boot = {};
72 if (!message.empty()) {
73 strncpy(boot.command, "boot-recovery", sizeof(boot.command) - 1);
74 memcpy(boot.recovery,
75 message.data(),
76 std::min(message.size(), sizeof(boot.recovery) - 1));
77 }
78
George Burgess IV5a46a182017-07-27 23:26:03 -070079 int fd = HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC));
Alex Deymofb905d92016-06-03 19:26:58 -070080 if (fd < 0) {
81 PLOG(ERROR) << "Opening misc";
82 return false;
83 }
84 ScopedFdCloser fd_closer(&fd);
85 // We only re-write the first part of the bootloader_message, up to and
86 // including the recovery message.
87 size_t boot_size =
88 offsetof(bootloader_message, recovery) + sizeof(boot.recovery);
89 if (!utils::WriteAll(fd, &boot, boot_size)) {
90 PLOG(ERROR) << "Writing recovery command to misc";
91 return false;
92 }
93 return true;
94}
95
96} // namespace
97
Alex Deymo40d86b22015-09-03 22:27:10 -070098namespace hardware {
99
100// Factory defined in hardware.h.
101std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700102 return brillo::make_unique_ptr(new HardwareAndroid());
Alex Deymo40d86b22015-09-03 22:27:10 -0700103}
104
105} // namespace hardware
106
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700107// In Android there are normally three kinds of builds: eng, userdebug and user.
108// These builds target respectively a developer build, a debuggable version of
109// the final product and the pristine final product the end user will run.
110// Apart from the ro.build.type property name, they differ in the following
111// properties that characterize the builds:
112// * eng builds: ro.secure=0 and ro.debuggable=1
113// * userdebug builds: ro.secure=1 and ro.debuggable=1
114// * user builds: ro.secure=1 and ro.debuggable=0
115//
116// See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
117// Android.
118
Alex Deymo40d86b22015-09-03 22:27:10 -0700119bool HardwareAndroid::IsOfficialBuild() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700120 // We run an official build iff ro.secure == 1, because we expect the build to
121 // behave like the end user product and check for updates. Note that while
122 // developers are able to build "official builds" by just running "make user",
123 // that will only result in a more restrictive environment. The important part
124 // is that we don't produce and push "non-official" builds to the end user.
125 //
126 // In case of a non-bool value, we take the most restrictive option and
127 // assume we are in an official-build.
Tom Cherryfadd03c2017-10-10 14:45:09 -0700128 return GetBoolProperty("ro.secure", true);
Alex Deymo40d86b22015-09-03 22:27:10 -0700129}
130
131bool HardwareAndroid::IsNormalBootMode() const {
Alex Deymo1c4e84a2015-09-22 16:58:10 -0700132 // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
133 // update_engine will allow extra developers options, such as providing a
134 // different update URL. In case of error, we assume the build is in
135 // normal-mode.
Tom Cherryfadd03c2017-10-10 14:45:09 -0700136 return !GetBoolProperty("ro.debuggable", false);
Alex Deymo40d86b22015-09-03 22:27:10 -0700137}
138
Sen Jiange67bb5b2016-06-20 15:53:56 -0700139bool HardwareAndroid::AreDevFeaturesEnabled() const {
140 return !IsNormalBootMode();
141}
142
Alex Deymo46a9aae2016-05-04 20:20:11 -0700143bool HardwareAndroid::IsOOBEEnabled() const {
144 // No OOBE flow blocking updates for Android-based boards.
145 return false;
146}
147
Alex Deymo40d86b22015-09-03 22:27:10 -0700148bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700149 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
Alex Deymo4d2990d2015-09-15 12:11:26 -0700150 if (out_time_of_oobe)
151 *out_time_of_oobe = base::Time();
Alex Deymo40d86b22015-09-03 22:27:10 -0700152 return true;
153}
154
155string HardwareAndroid::GetHardwareClass() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700156 auto manufacturer = GetProperty(kPropProductManufacturer, "");
157 auto sku = GetProperty(kPropBootHardwareSKU, "");
158 auto revision = GetProperty(kPropBootRevision, "");
Alex Deymoebf6e122017-03-10 16:12:01 -0800159
Tom Cherryfadd03c2017-10-10 14:45:09 -0700160 return manufacturer + ":" + sku + ":" + revision;
Alex Deymo40d86b22015-09-03 22:27:10 -0700161}
162
163string HardwareAndroid::GetFirmwareVersion() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700164 return GetProperty(kPropBootBootloader, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700165}
166
167string HardwareAndroid::GetECVersion() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700168 return GetProperty(kPropBootBaseband, "");
Alex Deymo40d86b22015-09-03 22:27:10 -0700169}
170
171int HardwareAndroid::GetPowerwashCount() const {
172 LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
173 return 0;
174}
175
Alex Deymofb905d92016-06-03 19:26:58 -0700176bool HardwareAndroid::SchedulePowerwash() {
177 LOG(INFO) << "Scheduling a powerwash to BCB.";
178 return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand);
179}
180
181bool HardwareAndroid::CancelPowerwash() {
182 return WriteBootloaderRecoveryMessage("");
183}
184
Alex Deymodd132f32015-09-14 19:12:07 -0700185bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800186 base::FilePath local_path(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700187 if (!base::PathExists(local_path)) {
188 LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
189 return false;
190 }
191 *path = local_path;
192 return true;
193}
194
195bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
196 // On Android, we don't have a directory persisted across powerwash.
197 return false;
198}
199
Sen Jiang5011df62017-06-28 17:13:19 -0700200int64_t HardwareAndroid::GetBuildTimestamp() const {
Tom Cherryfadd03c2017-10-10 14:45:09 -0700201 return GetIntProperty<int64_t>(kPropBuildDateUTC, 0);
Sen Jiang5011df62017-06-28 17:13:19 -0700202}
203
Alex Deymo40d86b22015-09-03 22:27:10 -0700204} // namespace chromeos_update_engine