| 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 | 012aa5b | 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 |  | 
 | 26 | #include <bootloader.h> | 
 | 27 |  | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 28 | #include <android-base/properties.h> | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 29 | #include <base/files/file_util.h> | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 30 | #include <base/strings/stringprintf.h> | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 31 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 32 | #include "update_engine/common/hardware.h" | 
| Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 33 | #include "update_engine/common/platform_constants.h" | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 34 | #include "update_engine/common/utils.h" | 
 | 35 | #include "update_engine/utils_android.h" | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 36 |  | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 37 | using android::base::GetBoolProperty; | 
 | 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"; | 
 | 59 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 60 | // 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. | 
 | 62 | bool 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 IV | 5a46a18 | 2017-07-27 23:26:03 -0700 | [diff] [blame] | 76 |   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] | 77 |   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 Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 95 | namespace hardware { | 
 | 96 |  | 
 | 97 | // Factory defined in hardware.h. | 
 | 98 | std::unique_ptr<HardwareInterface> CreateHardware() { | 
| Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 99 |   return std::make_unique<HardwareAndroid>(); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 100 | } | 
 | 101 |  | 
 | 102 | }  // namespace hardware | 
 | 103 |  | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 104 | // 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 Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 116 | bool HardwareAndroid::IsOfficialBuild() const { | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 117 |   // 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 Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 125 |   return GetBoolProperty("ro.secure", true); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 126 | } | 
 | 127 |  | 
 | 128 | bool HardwareAndroid::IsNormalBootMode() const { | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 129 |   // 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 Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 133 |   return !GetBoolProperty("ro.debuggable", false); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 134 | } | 
 | 135 |  | 
| Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 136 | bool HardwareAndroid::AreDevFeaturesEnabled() const { | 
 | 137 |   return !IsNormalBootMode(); | 
 | 138 | } | 
 | 139 |  | 
| Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 140 | bool HardwareAndroid::IsOOBEEnabled() const { | 
 | 141 |   // No OOBE flow blocking updates for Android-based boards. | 
 | 142 |   return false; | 
 | 143 | } | 
 | 144 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 145 | bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const { | 
| Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 146 |   LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called."; | 
| Alex Deymo | 4d2990d | 2015-09-15 12:11:26 -0700 | [diff] [blame] | 147 |   if (out_time_of_oobe) | 
 | 148 |     *out_time_of_oobe = base::Time(); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 149 |   return true; | 
 | 150 | } | 
 | 151 |  | 
 | 152 | string HardwareAndroid::GetHardwareClass() const { | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 153 |   auto manufacturer = GetProperty(kPropProductManufacturer, ""); | 
 | 154 |   auto sku = GetProperty(kPropBootHardwareSKU, ""); | 
 | 155 |   auto revision = GetProperty(kPropBootRevision, ""); | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 156 |  | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 157 |   return manufacturer + ":" + sku + ":" + revision; | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 158 | } | 
 | 159 |  | 
 | 160 | string HardwareAndroid::GetFirmwareVersion() const { | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 161 |   return GetProperty(kPropBootBootloader, ""); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 162 | } | 
 | 163 |  | 
 | 164 | string HardwareAndroid::GetECVersion() const { | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 165 |   return GetProperty(kPropBootBaseband, ""); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 166 | } | 
 | 167 |  | 
 | 168 | int HardwareAndroid::GetPowerwashCount() const { | 
 | 169 |   LOG(WARNING) << "STUB: Assuming no factory reset was performed."; | 
 | 170 |   return 0; | 
 | 171 | } | 
 | 172 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 173 | bool HardwareAndroid::SchedulePowerwash() { | 
 | 174 |   LOG(INFO) << "Scheduling a powerwash to BCB."; | 
 | 175 |   return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand); | 
 | 176 | } | 
 | 177 |  | 
 | 178 | bool HardwareAndroid::CancelPowerwash() { | 
 | 179 |   return WriteBootloaderRecoveryMessage(""); | 
 | 180 | } | 
 | 181 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 182 | bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const { | 
| Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 183 |   base::FilePath local_path(constants::kNonVolatileDirectory); | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 184 |   if (!base::PathExists(local_path)) { | 
 | 185 |     LOG(ERROR) << "Non-volatile directory not found: " << local_path.value(); | 
 | 186 |     return false; | 
 | 187 |   } | 
 | 188 |   *path = local_path; | 
 | 189 |   return true; | 
 | 190 | } | 
 | 191 |  | 
 | 192 | bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const { | 
 | 193 |   // On Android, we don't have a directory persisted across powerwash. | 
 | 194 |   return false; | 
 | 195 | } | 
 | 196 |  | 
| Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 197 | bool HardwareAndroid::GetFirstActiveOmahaPingSent() const { | 
 | 198 |   LOG(WARNING) << "STUB: Assuming first active omaha was never set."; | 
 | 199 |   return false; | 
 | 200 | } | 
 | 201 |  | 
| Sen Jiang | 1841408 | 2018-01-11 14:50:36 -0800 | [diff] [blame] | 202 | void HardwareAndroid::SetFirstActiveOmahaPingSent() { | 
| Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 203 |   LOG(WARNING) << "STUB: Assuming first active omaha is never set."; | 
 | 204 |   return; | 
 | 205 | } | 
 | 206 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 207 | }  // namespace chromeos_update_engine |