| 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> | 
|  | 24 |  | 
|  | 25 | #include <bootloader.h> | 
|  | 26 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 27 | #include <base/files/file_util.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 28 | #include <brillo/make_unique_ptr.h> | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 29 | #include <cutils/properties.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 |  | 
|  | 36 | using std::string; | 
|  | 37 |  | 
|  | 38 | namespace chromeos_update_engine { | 
|  | 39 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 40 | namespace { | 
|  | 41 |  | 
|  | 42 | // The powerwash arguments passed to recovery. Arguments are separated by \n. | 
|  | 43 | const char kAndroidRecoveryPowerwashCommand[] = | 
|  | 44 | "recovery\n" | 
|  | 45 | "--wipe_data\n" | 
|  | 46 | "--reason=wipe_data_from_ota\n"; | 
|  | 47 |  | 
|  | 48 | // Write a recovery command line |message| to the BCB. The arguments to recovery | 
|  | 49 | // must be separated by '\n'. An empty string will erase the BCB. | 
|  | 50 | bool WriteBootloaderRecoveryMessage(const string& message) { | 
|  | 51 | base::FilePath misc_device; | 
|  | 52 | if (!utils::DeviceForMountPoint("/misc", &misc_device)) | 
|  | 53 | return false; | 
|  | 54 |  | 
|  | 55 | // Setup a bootloader_message with just the command and recovery fields set. | 
|  | 56 | bootloader_message boot = {}; | 
|  | 57 | if (!message.empty()) { | 
|  | 58 | strncpy(boot.command, "boot-recovery", sizeof(boot.command) - 1); | 
|  | 59 | memcpy(boot.recovery, | 
|  | 60 | message.data(), | 
|  | 61 | std::min(message.size(), sizeof(boot.recovery) - 1)); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | int fd = | 
|  | 65 | HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC, 0600)); | 
|  | 66 | if (fd < 0) { | 
|  | 67 | PLOG(ERROR) << "Opening misc"; | 
|  | 68 | return false; | 
|  | 69 | } | 
|  | 70 | ScopedFdCloser fd_closer(&fd); | 
|  | 71 | // We only re-write the first part of the bootloader_message, up to and | 
|  | 72 | // including the recovery message. | 
|  | 73 | size_t boot_size = | 
|  | 74 | offsetof(bootloader_message, recovery) + sizeof(boot.recovery); | 
|  | 75 | if (!utils::WriteAll(fd, &boot, boot_size)) { | 
|  | 76 | PLOG(ERROR) << "Writing recovery command to misc"; | 
|  | 77 | return false; | 
|  | 78 | } | 
|  | 79 | return true; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | }  // namespace | 
|  | 83 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 84 | namespace hardware { | 
|  | 85 |  | 
|  | 86 | // Factory defined in hardware.h. | 
|  | 87 | std::unique_ptr<HardwareInterface> CreateHardware() { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 88 | return brillo::make_unique_ptr(new HardwareAndroid()); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
|  | 91 | }  // namespace hardware | 
|  | 92 |  | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 93 | // In Android there are normally three kinds of builds: eng, userdebug and user. | 
|  | 94 | // These builds target respectively a developer build, a debuggable version of | 
|  | 95 | // the final product and the pristine final product the end user will run. | 
|  | 96 | // Apart from the ro.build.type property name, they differ in the following | 
|  | 97 | // properties that characterize the builds: | 
|  | 98 | // * eng builds: ro.secure=0 and ro.debuggable=1 | 
|  | 99 | // * userdebug builds: ro.secure=1 and ro.debuggable=1 | 
|  | 100 | // * user builds: ro.secure=1 and ro.debuggable=0 | 
|  | 101 | // | 
|  | 102 | // See IsOfficialBuild() and IsNormalMode() for the meaning of these options in | 
|  | 103 | // Android. | 
|  | 104 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 105 | bool HardwareAndroid::IsOfficialBuild() const { | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 106 | // We run an official build iff ro.secure == 1, because we expect the build to | 
|  | 107 | // behave like the end user product and check for updates. Note that while | 
|  | 108 | // developers are able to build "official builds" by just running "make user", | 
|  | 109 | // that will only result in a more restrictive environment. The important part | 
|  | 110 | // is that we don't produce and push "non-official" builds to the end user. | 
|  | 111 | // | 
|  | 112 | // In case of a non-bool value, we take the most restrictive option and | 
|  | 113 | // assume we are in an official-build. | 
|  | 114 | return property_get_bool("ro.secure", 1) != 0; | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
|  | 117 | bool HardwareAndroid::IsNormalBootMode() const { | 
| Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 118 | // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the | 
|  | 119 | // update_engine will allow extra developers options, such as providing a | 
|  | 120 | // different update URL. In case of error, we assume the build is in | 
|  | 121 | // normal-mode. | 
|  | 122 | return property_get_bool("ro.debuggable", 0) != 1; | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
| Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 125 | bool HardwareAndroid::AreDevFeaturesEnabled() const { | 
|  | 126 | return !IsNormalBootMode(); | 
|  | 127 | } | 
|  | 128 |  | 
| Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 129 | bool HardwareAndroid::IsOOBEEnabled() const { | 
|  | 130 | // No OOBE flow blocking updates for Android-based boards. | 
|  | 131 | return false; | 
|  | 132 | } | 
|  | 133 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 134 | bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const { | 
| Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 135 | LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called."; | 
| Alex Deymo | 4d2990d | 2015-09-15 12:11:26 -0700 | [diff] [blame] | 136 | if (out_time_of_oobe) | 
|  | 137 | *out_time_of_oobe = base::Time(); | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 138 | return true; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | string HardwareAndroid::GetHardwareClass() const { | 
|  | 142 | LOG(WARNING) << "STUB: GetHardwareClass()."; | 
|  | 143 | return "ANDROID"; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | string HardwareAndroid::GetFirmwareVersion() const { | 
|  | 147 | LOG(WARNING) << "STUB: GetFirmwareVersion()."; | 
|  | 148 | return "0"; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | string HardwareAndroid::GetECVersion() const { | 
|  | 152 | LOG(WARNING) << "STUB: GetECVersion()."; | 
|  | 153 | return "0"; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | int HardwareAndroid::GetPowerwashCount() const { | 
|  | 157 | LOG(WARNING) << "STUB: Assuming no factory reset was performed."; | 
|  | 158 | return 0; | 
|  | 159 | } | 
|  | 160 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 161 | bool HardwareAndroid::SchedulePowerwash() { | 
|  | 162 | LOG(INFO) << "Scheduling a powerwash to BCB."; | 
|  | 163 | return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand); | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | bool HardwareAndroid::CancelPowerwash() { | 
|  | 167 | return WriteBootloaderRecoveryMessage(""); | 
|  | 168 | } | 
|  | 169 |  | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 170 | bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const { | 
| Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 171 | base::FilePath local_path(constants::kNonVolatileDirectory); | 
| Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 172 | if (!base::PathExists(local_path)) { | 
|  | 173 | LOG(ERROR) << "Non-volatile directory not found: " << local_path.value(); | 
|  | 174 | return false; | 
|  | 175 | } | 
|  | 176 | *path = local_path; | 
|  | 177 | return true; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const { | 
|  | 181 | // On Android, we don't have a directory persisted across powerwash. | 
|  | 182 | return false; | 
|  | 183 | } | 
|  | 184 |  | 
| Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 185 | }  // namespace chromeos_update_engine |