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 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame^] | 17 | #include "update_engine/aosp/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 <sys/types.h> |
Yifan Hong | fd6640f | 2020-08-27 19:13:17 -0700 | [diff] [blame] | 20 | #include <sys/utsname.h> |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 21 | |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 22 | #include <memory> |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <string_view> |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 25 | |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 26 | #include <android-base/parseint.h> |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 27 | #include <android-base/properties.h> |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 28 | #include <base/files/file_util.h> |
Tao Bao | 304680c | 2018-03-31 10:36:52 -0700 | [diff] [blame] | 29 | #include <bootloader_message/bootloader_message.h> |
Yifan Hong | fd6640f | 2020-08-27 19:13:17 -0700 | [diff] [blame] | 30 | #include <kver/kernel_release.h> |
| 31 | #include <kver/utils.h> |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 32 | |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 33 | #include "update_engine/common/error_code_utils.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 34 | #include "update_engine/common/hardware.h" |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 35 | #include "update_engine/common/platform_constants.h" |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 36 | #include "update_engine/common/utils.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; |
Yifan Hong | fd6640f | 2020-08-27 19:13:17 -0700 | [diff] [blame] | 41 | using android::kver::IsKernelUpdateValid; |
| 42 | using android::kver::KernelRelease; |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 43 | using std::string; |
| 44 | |
| 45 | namespace chromeos_update_engine { |
| 46 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 47 | namespace { |
| 48 | |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 49 | // Android properties that identify the hardware and potentially non-updatable |
| 50 | // parts of the bootloader (such as the bootloader version and the baseband |
| 51 | // version). |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 52 | const char kPropProductManufacturer[] = "ro.product.manufacturer"; |
| 53 | const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku"; |
| 54 | const char kPropBootRevision[] = "ro.boot.revision"; |
Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 55 | const char kPropBuildDateUTC[] = "ro.build.date.utc"; |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 56 | |
Yifan Hong | fd6640f | 2020-08-27 19:13:17 -0700 | [diff] [blame] | 57 | string GetPartitionBuildDate(const string& partition_name) { |
| 58 | return android::base::GetProperty("ro." + partition_name + ".build.date.utc", |
| 59 | ""); |
| 60 | } |
| 61 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 62 | } // namespace |
| 63 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 64 | namespace hardware { |
| 65 | |
| 66 | // Factory defined in hardware.h. |
| 67 | std::unique_ptr<HardwareInterface> CreateHardware() { |
Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 68 | return std::make_unique<HardwareAndroid>(); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } // namespace hardware |
| 72 | |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 73 | // In Android there are normally three kinds of builds: eng, userdebug and user. |
| 74 | // These builds target respectively a developer build, a debuggable version of |
| 75 | // the final product and the pristine final product the end user will run. |
| 76 | // Apart from the ro.build.type property name, they differ in the following |
| 77 | // properties that characterize the builds: |
| 78 | // * eng builds: ro.secure=0 and ro.debuggable=1 |
| 79 | // * userdebug builds: ro.secure=1 and ro.debuggable=1 |
| 80 | // * user builds: ro.secure=1 and ro.debuggable=0 |
| 81 | // |
| 82 | // See IsOfficialBuild() and IsNormalMode() for the meaning of these options in |
| 83 | // Android. |
| 84 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 85 | bool HardwareAndroid::IsOfficialBuild() const { |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 86 | // We run an official build iff ro.secure == 1, because we expect the build to |
| 87 | // behave like the end user product and check for updates. Note that while |
| 88 | // developers are able to build "official builds" by just running "make user", |
| 89 | // that will only result in a more restrictive environment. The important part |
| 90 | // is that we don't produce and push "non-official" builds to the end user. |
| 91 | // |
| 92 | // In case of a non-bool value, we take the most restrictive option and |
| 93 | // assume we are in an official-build. |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 94 | return GetBoolProperty("ro.secure", true); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool HardwareAndroid::IsNormalBootMode() const { |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 98 | // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the |
| 99 | // update_engine will allow extra developers options, such as providing a |
| 100 | // different update URL. In case of error, we assume the build is in |
| 101 | // normal-mode. |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 102 | return !GetBoolProperty("ro.debuggable", false); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 105 | bool HardwareAndroid::AreDevFeaturesEnabled() const { |
| 106 | return !IsNormalBootMode(); |
| 107 | } |
| 108 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 109 | bool HardwareAndroid::IsOOBEEnabled() const { |
| 110 | // No OOBE flow blocking updates for Android-based boards. |
| 111 | return false; |
| 112 | } |
| 113 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 114 | bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const { |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 115 | LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called."; |
Alex Deymo | 4d2990d | 2015-09-15 12:11:26 -0700 | [diff] [blame] | 116 | if (out_time_of_oobe) |
| 117 | *out_time_of_oobe = base::Time(); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | string HardwareAndroid::GetHardwareClass() const { |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 122 | auto manufacturer = GetProperty(kPropProductManufacturer, ""); |
| 123 | auto sku = GetProperty(kPropBootHardwareSKU, ""); |
| 124 | auto revision = GetProperty(kPropBootRevision, ""); |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 125 | |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 126 | return manufacturer + ":" + sku + ":" + revision; |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Matt Ziegelbaum | aa8e1a4 | 2019-05-09 21:41:58 -0400 | [diff] [blame] | 129 | string HardwareAndroid::GetDeviceRequisition() const { |
| 130 | LOG(WARNING) << "STUB: Getting requisition is not supported."; |
| 131 | return ""; |
| 132 | } |
| 133 | |
Zentaro Kavanagh | baacb98 | 2018-02-20 17:48:39 -0800 | [diff] [blame] | 134 | int HardwareAndroid::GetMinKernelKeyVersion() const { |
| 135 | LOG(WARNING) << "STUB: No Kernel key version is available."; |
| 136 | return -1; |
| 137 | } |
| 138 | |
Marton Hunyady | 99ced78 | 2018-05-08 12:59:50 +0200 | [diff] [blame] | 139 | int HardwareAndroid::GetMinFirmwareKeyVersion() const { |
| 140 | LOG(WARNING) << "STUB: No Firmware key version is available."; |
| 141 | return -1; |
| 142 | } |
| 143 | |
Zentaro Kavanagh | 8f6f243 | 2018-05-16 13:48:12 -0700 | [diff] [blame] | 144 | int HardwareAndroid::GetMaxFirmwareKeyRollforward() const { |
| 145 | LOG(WARNING) << "STUB: Getting firmware_max_rollforward is not supported."; |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | bool HardwareAndroid::SetMaxFirmwareKeyRollforward( |
| 150 | int firmware_max_rollforward) { |
| 151 | LOG(WARNING) << "STUB: Setting firmware_max_rollforward is not supported."; |
| 152 | return false; |
| 153 | } |
| 154 | |
Zentaro Kavanagh | 5d95615 | 2018-05-15 09:40:33 -0700 | [diff] [blame] | 155 | bool HardwareAndroid::SetMaxKernelKeyRollforward(int kernel_max_rollforward) { |
| 156 | LOG(WARNING) << "STUB: Setting kernel_max_rollforward is not supported."; |
Zentaro Kavanagh | baacb98 | 2018-02-20 17:48:39 -0800 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 160 | int HardwareAndroid::GetPowerwashCount() const { |
| 161 | LOG(WARNING) << "STUB: Assuming no factory reset was performed."; |
| 162 | return 0; |
| 163 | } |
| 164 | |
Zentaro Kavanagh | 28def4f | 2019-01-15 17:15:01 -0800 | [diff] [blame] | 165 | bool HardwareAndroid::SchedulePowerwash(bool save_rollback_data) { |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 166 | LOG(INFO) << "Scheduling a powerwash to BCB."; |
Zentaro Kavanagh | 28def4f | 2019-01-15 17:15:01 -0800 | [diff] [blame] | 167 | LOG_IF(WARNING, save_rollback_data) << "save_rollback_data was true but " |
| 168 | << "isn't supported."; |
Sen Jiang | d944faa | 2018-08-22 18:46:39 -0700 | [diff] [blame] | 169 | string err; |
| 170 | if (!update_bootloader_message({"--wipe_data", "--reason=wipe_data_from_ota"}, |
| 171 | &err)) { |
| 172 | LOG(ERROR) << "Failed to update bootloader message: " << err; |
| 173 | return false; |
| 174 | } |
| 175 | return true; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | bool HardwareAndroid::CancelPowerwash() { |
Sen Jiang | d944faa | 2018-08-22 18:46:39 -0700 | [diff] [blame] | 179 | string err; |
| 180 | if (!clear_bootloader_message(&err)) { |
| 181 | LOG(ERROR) << "Failed to clear bootloader message: " << err; |
| 182 | return false; |
| 183 | } |
| 184 | return true; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 187 | bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const { |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 188 | base::FilePath local_path(constants::kNonVolatileDirectory); |
Kelvin Zhang | d282252 | 2020-07-07 17:20:58 -0400 | [diff] [blame] | 189 | if (!base::DirectoryExists(local_path)) { |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 190 | LOG(ERROR) << "Non-volatile directory not found: " << local_path.value(); |
| 191 | return false; |
| 192 | } |
| 193 | *path = local_path; |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const { |
| 198 | // On Android, we don't have a directory persisted across powerwash. |
| 199 | return false; |
| 200 | } |
| 201 | |
Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 202 | int64_t HardwareAndroid::GetBuildTimestamp() const { |
Tom Cherry | fadd03c | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 203 | return GetIntProperty<int64_t>(kPropBuildDateUTC, 0); |
Sen Jiang | 5011df6 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Tianjie Xu | 4ad3af6 | 2019-10-30 11:59:45 -0700 | [diff] [blame] | 206 | // Returns true if the device runs an userdebug build, and explicitly allows OTA |
| 207 | // downgrade. |
| 208 | bool HardwareAndroid::AllowDowngrade() const { |
| 209 | return GetBoolProperty("ro.ota.allow_downgrade", false) && |
| 210 | GetBoolProperty("ro.debuggable", false); |
| 211 | } |
| 212 | |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 213 | bool HardwareAndroid::GetFirstActiveOmahaPingSent() const { |
| 214 | LOG(WARNING) << "STUB: Assuming first active omaha was never set."; |
| 215 | return false; |
| 216 | } |
| 217 | |
Amin Hassani | 80f4d4c | 2018-05-16 13:34:00 -0700 | [diff] [blame] | 218 | bool HardwareAndroid::SetFirstActiveOmahaPingSent() { |
| 219 | LOG(WARNING) << "STUB: Assuming first active omaha is set."; |
| 220 | // We will set it true, so its failure doesn't cause escalation. |
| 221 | return true; |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Tianjie Xu | d6aa91f | 2019-11-14 11:55:10 -0800 | [diff] [blame] | 224 | void HardwareAndroid::SetWarmReset(bool warm_reset) { |
| 225 | constexpr char warm_reset_prop[] = "ota.warm_reset"; |
| 226 | if (!android::base::SetProperty(warm_reset_prop, warm_reset ? "1" : "0")) { |
| 227 | LOG(WARNING) << "Failed to set prop " << warm_reset_prop; |
| 228 | } |
| 229 | } |
| 230 | |
Yifan Hong | fd6640f | 2020-08-27 19:13:17 -0700 | [diff] [blame] | 231 | string HardwareAndroid::GetVersionForLogging( |
| 232 | const string& partition_name) const { |
| 233 | if (partition_name == "boot") { |
| 234 | struct utsname buf; |
| 235 | if (uname(&buf) != 0) { |
| 236 | PLOG(ERROR) << "Unable to call uname()"; |
| 237 | return ""; |
| 238 | } |
| 239 | auto kernel_release = |
| 240 | KernelRelease::Parse(buf.release, true /* allow_suffix */); |
| 241 | return kernel_release.has_value() ? kernel_release->string() : ""; |
| 242 | } |
| 243 | return GetPartitionBuildDate(partition_name); |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 246 | ErrorCode HardwareAndroid::IsPartitionUpdateValid( |
Yifan Hong | fd6640f | 2020-08-27 19:13:17 -0700 | [diff] [blame] | 247 | const string& partition_name, const string& new_version) const { |
| 248 | if (partition_name == "boot") { |
| 249 | struct utsname buf; |
| 250 | if (uname(&buf) != 0) { |
| 251 | PLOG(ERROR) << "Unable to call uname()"; |
| 252 | return ErrorCode::kError; |
| 253 | } |
| 254 | return IsKernelUpdateValid(buf.release, new_version); |
| 255 | } |
| 256 | |
| 257 | const auto old_version = GetPartitionBuildDate(partition_name); |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 258 | // TODO(zhangkelvin) for some partitions, missing a current timestamp should |
| 259 | // be an error, e.g. system, vendor, product etc. |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 260 | auto error_code = utils::IsTimestampNewer(old_version, new_version); |
| 261 | if (error_code != ErrorCode::kSuccess) { |
| 262 | LOG(ERROR) << "Timestamp check failed with " |
| 263 | << utils::ErrorCodeToString(error_code) |
| 264 | << " Partition timestamp: " << old_version |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 265 | << " Update timestamp: " << new_version; |
| 266 | } |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 267 | return error_code; |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Yifan Hong | fd6640f | 2020-08-27 19:13:17 -0700 | [diff] [blame] | 270 | ErrorCode HardwareAndroid::IsKernelUpdateValid(const string& old_release, |
| 271 | const string& new_release) { |
| 272 | // Check that the package either contain an empty version (indicating that the |
| 273 | // new build does not use GKI), or a valid GKI kernel release. |
| 274 | std::optional<KernelRelease> new_kernel_release; |
| 275 | if (new_release.empty()) { |
| 276 | LOG(INFO) << "New build does not contain GKI."; |
| 277 | } else { |
| 278 | new_kernel_release = |
| 279 | KernelRelease::Parse(new_release, true /* allow_suffix */); |
| 280 | if (!new_kernel_release.has_value()) { |
| 281 | LOG(ERROR) << "New kernel release is not valid GKI kernel release: " |
| 282 | << new_release; |
| 283 | return ErrorCode::kDownloadManifestParseError; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | auto old_kernel_release = |
| 288 | KernelRelease::Parse(old_release, true /* allow_suffix */); |
| 289 | return android::kver::IsKernelUpdateValid(old_kernel_release, |
| 290 | new_kernel_release) |
| 291 | ? ErrorCode::kSuccess |
| 292 | : ErrorCode::kPayloadTimestampError; |
| 293 | } |
| 294 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 295 | } // namespace chromeos_update_engine |