| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Deymo | 1b03f9f | 2015-12-09 00:38:36 -0800 | [diff] [blame] | 17 | #include "update_engine/hardware_android.h" | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 18 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 19 | #include <fcntl.h> | 
|  | 20 | #include <sys/stat.h> | 
|  | 21 | #include <sys/types.h> | 
|  | 22 |  | 
|  | 23 | #include <algorithm> | 
| Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 24 | #include <memory> | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 25 |  | 
| Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 26 | #include <android-base/properties.h> | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 27 | #include <base/files/file_util.h> | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 28 | #include <base/strings/stringprintf.h> | 
| Tao Bao | 304680c | 2018-03-31 10:36:52 -0700 | [diff] [blame] | 29 | #include <bootloader_message/bootloader_message.h> | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 30 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/common/hardware.h" | 
| Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 32 | #include "update_engine/common/platform_constants.h" | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 33 | #include "update_engine/common/utils.h" | 
|  | 34 | #include "update_engine/utils_android.h" | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 35 |  | 
| Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 36 | using android::base::GetBoolProperty; | 
|  | 37 | using android::base::GetIntProperty; | 
|  | 38 | using android::base::GetProperty; | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 39 | using std::string; | 
|  | 40 |  | 
|  | 41 | namespace chromeos_update_engine { | 
|  | 42 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 43 | namespace { | 
|  | 44 |  | 
|  | 45 | // The powerwash arguments passed to recovery. Arguments are separated by \n. | 
|  | 46 | const char kAndroidRecoveryPowerwashCommand[] = | 
|  | 47 | "recovery\n" | 
|  | 48 | "--wipe_data\n" | 
|  | 49 | "--reason=wipe_data_from_ota\n"; | 
|  | 50 |  | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 51 | // 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). | 
|  | 54 | const char kPropBootBootloader[] = "ro.boot.bootloader"; | 
|  | 55 | const char kPropBootBaseband[] = "ro.boot.baseband"; | 
|  | 56 | const char kPropProductManufacturer[] = "ro.product.manufacturer"; | 
|  | 57 | const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku"; | 
|  | 58 | const char kPropBootRevision[] = "ro.boot.revision"; | 
| Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 59 | const char kPropBuildDateUTC[] = "ro.build.date.utc"; | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 60 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 61 | // 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. | 
|  | 63 | bool 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 IV | 5a46a18 | 2017-07-27 23:26:03 -0700 | [diff] [blame] | 77 | int fd = HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC)); | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 78 | 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 Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 96 | namespace hardware { | 
|  | 97 |  | 
|  | 98 | // Factory defined in hardware.h. | 
|  | 99 | std::unique_ptr<HardwareInterface> CreateHardware() { | 
| Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 100 | return std::make_unique<HardwareAndroid>(); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
|  | 103 | }  // namespace hardware | 
|  | 104 |  | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 105 | // 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 Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 117 | bool HardwareAndroid::IsOfficialBuild() const { | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 118 | // 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 Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 126 | return GetBoolProperty("ro.secure", true); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
|  | 129 | bool HardwareAndroid::IsNormalBootMode() const { | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 130 | // 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 Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 134 | return !GetBoolProperty("ro.debuggable", false); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 137 | bool HardwareAndroid::AreDevFeaturesEnabled() const { | 
|  | 138 | return !IsNormalBootMode(); | 
|  | 139 | } | 
|  | 140 |  | 
| Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 141 | bool HardwareAndroid::IsOOBEEnabled() const { | 
|  | 142 | // No OOBE flow blocking updates for Android-based boards. | 
|  | 143 | return false; | 
|  | 144 | } | 
|  | 145 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 146 | bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const { | 
| Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 147 | LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called."; | 
| Alex Deymo | 4d2990d | 2015-09-15 12:11:26 -0700 | [diff] [blame] | 148 | if (out_time_of_oobe) | 
|  | 149 | *out_time_of_oobe = base::Time(); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 150 | return true; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | string HardwareAndroid::GetHardwareClass() const { | 
| Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 154 | auto manufacturer = GetProperty(kPropProductManufacturer, ""); | 
|  | 155 | auto sku = GetProperty(kPropBootHardwareSKU, ""); | 
|  | 156 | auto revision = GetProperty(kPropBootRevision, ""); | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 157 |  | 
| Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 158 | return manufacturer + ":" + sku + ":" + revision; | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
|  | 161 | string HardwareAndroid::GetFirmwareVersion() const { | 
| Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 162 | return GetProperty(kPropBootBootloader, ""); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
|  | 165 | string HardwareAndroid::GetECVersion() const { | 
| Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 166 | return GetProperty(kPropBootBaseband, ""); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
|  | 169 | int HardwareAndroid::GetPowerwashCount() const { | 
|  | 170 | LOG(WARNING) << "STUB: Assuming no factory reset was performed."; | 
|  | 171 | return 0; | 
|  | 172 | } | 
|  | 173 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 174 | bool HardwareAndroid::SchedulePowerwash() { | 
|  | 175 | LOG(INFO) << "Scheduling a powerwash to BCB."; | 
|  | 176 | return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | bool HardwareAndroid::CancelPowerwash() { | 
|  | 180 | return WriteBootloaderRecoveryMessage(""); | 
|  | 181 | } | 
|  | 182 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 183 | bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const { | 
| Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 184 | base::FilePath local_path(constants::kNonVolatileDirectory); | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 185 | 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 |  | 
|  | 193 | bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const { | 
|  | 194 | // On Android, we don't have a directory persisted across powerwash. | 
|  | 195 | return false; | 
|  | 196 | } | 
|  | 197 |  | 
| Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 198 | int64_t HardwareAndroid::GetBuildTimestamp() const { | 
| Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 199 | return GetIntProperty<int64_t>(kPropBuildDateUTC, 0); | 
| Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
| Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 202 | bool HardwareAndroid::GetFirstActiveOmahaPingSent() const { | 
|  | 203 | LOG(WARNING) << "STUB: Assuming first active omaha was never set."; | 
|  | 204 | return false; | 
|  | 205 | } | 
|  | 206 |  | 
| Sen Jiang | 4490696 | 2018-01-08 17:39:04 -0800 | [diff] [blame] | 207 | void HardwareAndroid::SetFirstActiveOmahaPingSent() { | 
| Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 208 | LOG(WARNING) << "STUB: Assuming first active omaha is never set."; | 
|  | 209 | return; | 
|  | 210 | } | 
|  | 211 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 212 | }  // namespace chromeos_update_engine |