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 | |
| 26 | #include <bootloader.h> |
| 27 | |
Tom Cherry | fadd03c | 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 31 | #include <brillo/make_unique_ptr.h> |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 32 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/hardware.h" |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 34 | #include "update_engine/common/platform_constants.h" |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 35 | #include "update_engine/common/utils.h" |
| 36 | #include "update_engine/utils_android.h" |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 37 | |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame^] | 38 | using android::base::GetBoolProperty; |
| 39 | using android::base::GetIntProperty; |
| 40 | using android::base::GetProperty; |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 41 | using std::string; |
| 42 | |
| 43 | namespace chromeos_update_engine { |
| 44 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | // The powerwash arguments passed to recovery. Arguments are separated by \n. |
| 48 | const char kAndroidRecoveryPowerwashCommand[] = |
| 49 | "recovery\n" |
| 50 | "--wipe_data\n" |
| 51 | "--reason=wipe_data_from_ota\n"; |
| 52 | |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 53 | // 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). |
| 56 | const char kPropBootBootloader[] = "ro.boot.bootloader"; |
| 57 | const char kPropBootBaseband[] = "ro.boot.baseband"; |
| 58 | const char kPropProductManufacturer[] = "ro.product.manufacturer"; |
| 59 | const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku"; |
| 60 | const char kPropBootRevision[] = "ro.boot.revision"; |
Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 61 | const char kPropBuildDateUTC[] = "ro.build.date.utc"; |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 62 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 63 | // 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. |
| 65 | bool 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 IV | 5a46a18 | 2017-07-27 23:26:03 -0700 | [diff] [blame] | 79 | 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] | 80 | 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 Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 98 | namespace hardware { |
| 99 | |
| 100 | // Factory defined in hardware.h. |
| 101 | std::unique_ptr<HardwareInterface> CreateHardware() { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 102 | return brillo::make_unique_ptr(new HardwareAndroid()); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace hardware |
| 106 | |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 107 | // 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 Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 119 | bool HardwareAndroid::IsOfficialBuild() const { |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 120 | // 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 Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame^] | 128 | return GetBoolProperty("ro.secure", true); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool HardwareAndroid::IsNormalBootMode() const { |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 132 | // 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 Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame^] | 136 | return !GetBoolProperty("ro.debuggable", false); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 139 | bool HardwareAndroid::AreDevFeaturesEnabled() const { |
| 140 | return !IsNormalBootMode(); |
| 141 | } |
| 142 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 143 | bool HardwareAndroid::IsOOBEEnabled() const { |
| 144 | // No OOBE flow blocking updates for Android-based boards. |
| 145 | return false; |
| 146 | } |
| 147 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 148 | bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const { |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 149 | LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called."; |
Alex Deymo | 4d2990d | 2015-09-15 12:11:26 -0700 | [diff] [blame] | 150 | if (out_time_of_oobe) |
| 151 | *out_time_of_oobe = base::Time(); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 152 | return true; |
| 153 | } |
| 154 | |
| 155 | string HardwareAndroid::GetHardwareClass() const { |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame^] | 156 | auto manufacturer = GetProperty(kPropProductManufacturer, ""); |
| 157 | auto sku = GetProperty(kPropBootHardwareSKU, ""); |
| 158 | auto revision = GetProperty(kPropBootRevision, ""); |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 159 | |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame^] | 160 | return manufacturer + ":" + sku + ":" + revision; |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | string HardwareAndroid::GetFirmwareVersion() const { |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame^] | 164 | return GetProperty(kPropBootBootloader, ""); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | string HardwareAndroid::GetECVersion() const { |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame^] | 168 | return GetProperty(kPropBootBaseband, ""); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | int HardwareAndroid::GetPowerwashCount() const { |
| 172 | LOG(WARNING) << "STUB: Assuming no factory reset was performed."; |
| 173 | return 0; |
| 174 | } |
| 175 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 176 | bool HardwareAndroid::SchedulePowerwash() { |
| 177 | LOG(INFO) << "Scheduling a powerwash to BCB."; |
| 178 | return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand); |
| 179 | } |
| 180 | |
| 181 | bool HardwareAndroid::CancelPowerwash() { |
| 182 | return WriteBootloaderRecoveryMessage(""); |
| 183 | } |
| 184 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 185 | bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const { |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 186 | base::FilePath local_path(constants::kNonVolatileDirectory); |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 187 | 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 | |
| 195 | bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const { |
| 196 | // On Android, we don't have a directory persisted across powerwash. |
| 197 | return false; |
| 198 | } |
| 199 | |
Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 200 | int64_t HardwareAndroid::GetBuildTimestamp() const { |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame^] | 201 | return GetIntProperty<int64_t>(kPropBuildDateUTC, 0); |
Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 204 | } // namespace chromeos_update_engine |