The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 17 | #include "install/install.h" |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 18 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
Alex Deymo | 4e29ce0 | 2016-08-12 13:43:04 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <limits.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 24 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <sys/stat.h> |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 26 | #include <sys/wait.h> |
| 27 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 29 | #include <algorithm> |
Tianjie Xu | 3795710 | 2017-06-07 17:59:55 -0700 | [diff] [blame] | 30 | #include <atomic> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 31 | #include <chrono> |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 32 | #include <condition_variable> |
Tianjie Xu | cd8faf7 | 2019-08-06 12:32:05 -0700 | [diff] [blame] | 33 | #include <filesystem> |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 34 | #include <functional> |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 35 | #include <limits> |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 36 | #include <mutex> |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 37 | #include <thread> |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
Tianjie Xu | e16e799 | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 40 | #include <android-base/file.h> |
| 41 | #include <android-base/logging.h> |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 42 | #include <android-base/parsedouble.h> |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 43 | #include <android-base/parseint.h> |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 44 | #include <android-base/properties.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 45 | #include <android-base/stringprintf.h> |
| 46 | #include <android-base/strings.h> |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 47 | #include <android-base/unique_fd.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 48 | |
LuK1337 | 2a70656 | 2023-08-04 22:09:53 +0200 | [diff] [blame^] | 49 | #include "bootloader_message/bootloader_message.h" |
aleasto | 41d0154 | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 50 | #include "install/snapshot_utils.h" |
Kelvin Zhang | 33c62fc | 2021-05-14 17:15:50 -0400 | [diff] [blame] | 51 | #include "install/spl_check.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 52 | #include "install/wipe_data.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 53 | #include "otautil/error_code.h" |
Jacky Liu | 068329e | 2021-12-21 00:32:50 +0800 | [diff] [blame] | 54 | #include "otautil/package.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 55 | #include "otautil/paths.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 56 | #include "otautil/sysutil.h" |
Jacky Liu | 068329e | 2021-12-21 00:32:50 +0800 | [diff] [blame] | 57 | #include "otautil/verifier.h" |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 58 | #include "private/setup_commands.h" |
Tom Marshall | 3c709de | 2018-06-21 00:57:24 +0200 | [diff] [blame] | 59 | #include "recovery_ui/device.h" |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 60 | #include "recovery_ui/ui.h" |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 61 | #include "recovery_utils/roots.h" |
| 62 | #include "recovery_utils/thermalutil.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 64 | using namespace std::chrono_literals; |
| 65 | |
LuK1337 | 107dfac | 2023-07-29 11:16:41 +0200 | [diff] [blame] | 66 | bool ask_to_ab_reboot(Device* device); |
Tom Marshall | 3c709de | 2018-06-21 00:57:24 +0200 | [diff] [blame] | 67 | bool ask_to_continue_unverified(Device* device); |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 68 | bool ask_to_continue_downgrade(Device* device); |
Tom Marshall | 3c709de | 2018-06-21 00:57:24 +0200 | [diff] [blame] | 69 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 70 | static constexpr int kRecoveryApiVersion = 3; |
Tianjie Xu | 164c60a | 2019-05-15 13:59:39 -0700 | [diff] [blame] | 71 | // We define RECOVERY_API_VERSION in Android.mk, which will be picked up by build system and packed |
| 72 | // into target_files.zip. Assert the version defined in code and in Android.mk are consistent. |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 73 | static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions."); |
| 74 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 75 | // Default allocation of progress bar segments to operations |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 76 | static constexpr int VERIFICATION_PROGRESS_TIME = 60; |
| 77 | static constexpr float VERIFICATION_PROGRESS_FRACTION = 0.25; |
Kelvin Zhang | e1ae78c | 2020-06-26 11:06:00 -0400 | [diff] [blame] | 78 | // The charater used to separate dynamic fingerprints. e.x. sargo|aosp-sargo |
Alessandro Astone | 7aa4c68 | 2021-11-01 17:47:07 +0100 | [diff] [blame] | 79 | #define FINGERPRING_SEPARATOR "|" |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 80 | static std::condition_variable finish_log_temperature; |
Kelvin Zhang | e1ae78c | 2020-06-26 11:06:00 -0400 | [diff] [blame] | 81 | static bool isInStringList(const std::string& target_token, const std::string& str_list, |
| 82 | const std::string& deliminator); |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 83 | |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 84 | bool ReadMetadataFromPackage(ZipArchiveHandle zip, std::map<std::string, std::string>* metadata) { |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 85 | CHECK(metadata != nullptr); |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 86 | |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 87 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
Kelvin Zhang | 4f81130 | 2020-09-16 14:06:12 -0400 | [diff] [blame] | 88 | ZipEntry64 entry; |
Elliott Hughes | a86dddb | 2019-05-03 22:52:37 -0700 | [diff] [blame] | 89 | if (FindEntry(zip, METADATA_PATH, &entry) != 0) { |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
| 93 | uint32_t length = entry.uncompressed_length; |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 94 | std::string metadata_string(length, '\0'); |
| 95 | int32_t err = |
| 96 | ExtractToMemory(zip, &entry, reinterpret_cast<uint8_t*>(&metadata_string[0]), length); |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 97 | if (err != 0) { |
| 98 | LOG(ERROR) << "Failed to extract " << METADATA_PATH << ": " << ErrorCodeString(err); |
| 99 | return false; |
| 100 | } |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 101 | |
| 102 | for (const std::string& line : android::base::Split(metadata_string, "\n")) { |
| 103 | size_t eq = line.find('='); |
| 104 | if (eq != std::string::npos) { |
| 105 | metadata->emplace(android::base::Trim(line.substr(0, eq)), |
| 106 | android::base::Trim(line.substr(eq + 1))); |
| 107 | } |
| 108 | } |
| 109 | |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 110 | return true; |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 113 | // Gets the value for the given key in |metadata|. Returns an emtpy string if the key isn't |
| 114 | // present. |
| 115 | static std::string get_value(const std::map<std::string, std::string>& metadata, |
| 116 | const std::string& key) { |
| 117 | const auto& it = metadata.find(key); |
| 118 | return (it == metadata.end()) ? "" : it->second; |
| 119 | } |
| 120 | |
| 121 | static std::string OtaTypeToString(OtaType type) { |
| 122 | switch (type) { |
| 123 | case OtaType::AB: |
| 124 | return "AB"; |
| 125 | case OtaType::BLOCK: |
| 126 | return "BLOCK"; |
| 127 | case OtaType::BRICK: |
| 128 | return "BRICK"; |
Tao Bao | 8a7afcc | 2017-04-18 22:05:50 -0700 | [diff] [blame] | 129 | } |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 132 | // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. |
| 133 | static void ReadSourceTargetBuild(const std::map<std::string, std::string>& metadata, |
| 134 | std::vector<std::string>* log_buffer) { |
| 135 | // Examples of the pre-build and post-build strings in metadata: |
| 136 | // pre-build-incremental=2943039 |
| 137 | // post-build-incremental=2951741 |
| 138 | auto source_build = get_value(metadata, "pre-build-incremental"); |
| 139 | if (!source_build.empty()) { |
| 140 | log_buffer->push_back("source_build: " + source_build); |
| 141 | } |
| 142 | |
| 143 | auto target_build = get_value(metadata, "post-build-incremental"); |
| 144 | if (!target_build.empty()) { |
| 145 | log_buffer->push_back("target_build: " + target_build); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Checks the build version, fingerprint and timestamp in the metadata of the A/B package. |
| 150 | // Downgrading is not allowed unless explicitly enabled in the package and only for |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 151 | // incremental packages. |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 152 | static bool CheckAbSpecificMetadata(const std::map<std::string, std::string>& metadata, |
| 153 | RecoveryUI* ui) { |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 154 | // Incremental updates should match the current build. |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 155 | auto device_pre_build = android::base::GetProperty("ro.build.version.incremental", ""); |
| 156 | auto pkg_pre_build = get_value(metadata, "pre-build-incremental"); |
| 157 | if (!pkg_pre_build.empty() && pkg_pre_build != device_pre_build) { |
| 158 | LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " |
| 159 | << device_pre_build; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 160 | return false; |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 161 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 162 | |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 163 | auto device_fingerprint = android::base::GetProperty("ro.build.fingerprint", ""); |
| 164 | auto pkg_pre_build_fingerprint = get_value(metadata, "pre-build"); |
Kelvin Zhang | e1ae78c | 2020-06-26 11:06:00 -0400 | [diff] [blame] | 165 | if (!pkg_pre_build_fingerprint.empty() && |
| 166 | !isInStringList(device_fingerprint, pkg_pre_build_fingerprint, FINGERPRING_SEPARATOR)) { |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 167 | LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint << " but expected " |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 168 | << device_fingerprint; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 169 | return false; |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 170 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 171 | |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 172 | // Check for downgrade version. |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 173 | bool undeclared_downgrade = false; |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 174 | int64_t build_timestamp = |
| 175 | android::base::GetIntProperty("ro.build.date.utc", std::numeric_limits<int64_t>::max()); |
| 176 | int64_t pkg_post_timestamp = 0; |
| 177 | // We allow to full update to the same version we are running, in case there |
| 178 | // is a problem with the current copy of that version. |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 179 | auto pkg_post_timestamp_string = get_value(metadata, "post-timestamp"); |
| 180 | if (pkg_post_timestamp_string.empty() || |
| 181 | !android::base::ParseInt(pkg_post_timestamp_string, &pkg_post_timestamp) || |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 182 | pkg_post_timestamp < build_timestamp) { |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 183 | if (get_value(metadata, "ota-downgrade") != "yes") { |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 184 | LOG(ERROR) << "Update package is older than the current build, expected a build " |
| 185 | "newer than timestamp " |
| 186 | << build_timestamp << " but package has timestamp " << pkg_post_timestamp |
| 187 | << " and downgrade not allowed."; |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 188 | undeclared_downgrade = true; |
| 189 | } else if (pkg_pre_build_fingerprint.empty()) { |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 190 | LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed."; |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 191 | undeclared_downgrade = true; |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 195 | if (undeclared_downgrade && |
| 196 | !(ui->IsTextVisible() && ask_to_continue_downgrade(ui->GetDevice()))) { |
| 197 | return false; |
| 198 | } |
| 199 | |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 200 | return true; |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 203 | bool CheckPackageMetadata(const std::map<std::string, std::string>& metadata, OtaType ota_type, |
| 204 | RecoveryUI* ui) { |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 205 | auto package_ota_type = get_value(metadata, "ota-type"); |
| 206 | auto expected_ota_type = OtaTypeToString(ota_type); |
| 207 | if (ota_type != OtaType::AB && ota_type != OtaType::BRICK) { |
| 208 | LOG(INFO) << "Skip package metadata check for ota type " << expected_ota_type; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 209 | return true; |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (package_ota_type != expected_ota_type) { |
| 213 | LOG(ERROR) << "Unexpected ota package type, expects " << expected_ota_type << ", actual " |
| 214 | << package_ota_type; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 215 | return false; |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | auto device = android::base::GetProperty("ro.product.device", ""); |
| 219 | auto pkg_device = get_value(metadata, "pre-device"); |
Kelvin Zhang | e1ae78c | 2020-06-26 11:06:00 -0400 | [diff] [blame] | 220 | // device name can be a | separated list, so need to check |
Erfan Abdi | d62d939 | 2022-03-15 21:34:22 -0400 | [diff] [blame] | 221 | if (pkg_device.empty() || !isInStringList(device, pkg_device, FINGERPRING_SEPARATOR ":" ",")) { |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 222 | LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << device; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 223 | return false; |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // We allow the package to not have any serialno; and we also allow it to carry multiple serial |
| 227 | // numbers split by "|"; e.g. serialno=serialno1|serialno2|serialno3 ... We will fail the |
| 228 | // verification if the device's serialno doesn't match any of these carried numbers. |
| 229 | auto pkg_serial_no = get_value(metadata, "serialno"); |
| 230 | if (!pkg_serial_no.empty()) { |
| 231 | auto device_serial_no = android::base::GetProperty("ro.serialno", ""); |
| 232 | bool serial_number_match = false; |
| 233 | for (const auto& number : android::base::Split(pkg_serial_no, "|")) { |
| 234 | if (device_serial_no == android::base::Trim(number)) { |
| 235 | serial_number_match = true; |
| 236 | } |
| 237 | } |
| 238 | if (!serial_number_match) { |
| 239 | LOG(ERROR) << "Package is for serial " << pkg_serial_no; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 240 | return false; |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | if (ota_type == OtaType::AB) { |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 245 | return CheckAbSpecificMetadata(metadata, ui); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 248 | return true; |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 251 | static std::string ExtractPayloadProperties(ZipArchiveHandle zip) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 252 | // For A/B updates we extract the payload properties to a buffer and obtain the RAW payload offset |
| 253 | // in the zip file. |
| 254 | static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; |
Kelvin Zhang | 4f81130 | 2020-09-16 14:06:12 -0400 | [diff] [blame] | 255 | ZipEntry64 properties_entry; |
Elliott Hughes | a86dddb | 2019-05-03 22:52:37 -0700 | [diff] [blame] | 256 | if (FindEntry(zip, AB_OTA_PAYLOAD_PROPERTIES, &properties_entry) != 0) { |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 257 | return {}; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 258 | } |
Kelvin Zhang | d1ba38f | 2020-09-17 11:32:29 -0400 | [diff] [blame] | 259 | auto properties_entry_length = properties_entry.uncompressed_length; |
| 260 | if (properties_entry_length > std::numeric_limits<size_t>::max()) { |
| 261 | LOG(ERROR) << "Failed to extract " << AB_OTA_PAYLOAD_PROPERTIES |
| 262 | << " because's uncompressed size exceeds size of address space. " |
| 263 | << properties_entry_length; |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 264 | return {}; |
Kelvin Zhang | d1ba38f | 2020-09-17 11:32:29 -0400 | [diff] [blame] | 265 | } |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 266 | std::string payload_properties(properties_entry_length, '\0'); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 267 | int32_t err = |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 268 | ExtractToMemory(zip, &properties_entry, reinterpret_cast<uint8_t*>(payload_properties.data()), |
| 269 | properties_entry_length); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 270 | if (err != 0) { |
| 271 | LOG(ERROR) << "Failed to extract " << AB_OTA_PAYLOAD_PROPERTIES << ": " << ErrorCodeString(err); |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 272 | return {}; |
| 273 | } |
| 274 | return payload_properties; |
| 275 | } |
| 276 | |
| 277 | bool SetUpAbUpdateCommands(const std::string& package, ZipArchiveHandle zip, int status_fd, |
| 278 | std::vector<std::string>* cmd) { |
| 279 | CHECK(cmd != nullptr); |
| 280 | |
| 281 | // For A/B updates we extract the payload properties to a buffer and obtain the RAW payload offset |
| 282 | // in the zip file. |
| 283 | const auto payload_properties = ExtractPayloadProperties(zip); |
| 284 | if (payload_properties.empty()) { |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 285 | return false; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 286 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 287 | |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 288 | static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; |
Kelvin Zhang | 4f81130 | 2020-09-16 14:06:12 -0400 | [diff] [blame] | 289 | ZipEntry64 payload_entry; |
Elliott Hughes | a86dddb | 2019-05-03 22:52:37 -0700 | [diff] [blame] | 290 | if (FindEntry(zip, AB_OTA_PAYLOAD, &payload_entry) != 0) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 291 | LOG(ERROR) << "Failed to find " << AB_OTA_PAYLOAD; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 292 | return false; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 293 | } |
| 294 | long payload_offset = payload_entry.offset; |
| 295 | *cmd = { |
Tao Bao | 2cc9bbb | 2018-08-14 12:34:46 -0700 | [diff] [blame] | 296 | "/system/bin/update_engine_sideload", |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 297 | "--payload=file://" + package, |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 298 | android::base::StringPrintf("--offset=%ld", payload_offset), |
| 299 | "--headers=" + std::string(payload_properties.begin(), payload_properties.end()), |
| 300 | android::base::StringPrintf("--status_fd=%d", status_fd), |
| 301 | }; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 302 | return true; |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 305 | bool SetUpNonAbUpdateCommands(const std::string& package, ZipArchiveHandle zip, int retry_count, |
| 306 | int status_fd, std::vector<std::string>* cmd) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 307 | CHECK(cmd != nullptr); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 308 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 309 | // In non-A/B updates we extract the update binary from the package. |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 310 | static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary"; |
Kelvin Zhang | 4f81130 | 2020-09-16 14:06:12 -0400 | [diff] [blame] | 311 | ZipEntry64 binary_entry; |
Elliott Hughes | a86dddb | 2019-05-03 22:52:37 -0700 | [diff] [blame] | 312 | if (FindEntry(zip, UPDATE_BINARY_NAME, &binary_entry) != 0) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 313 | LOG(ERROR) << "Failed to find update binary " << UPDATE_BINARY_NAME; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 314 | return false; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 315 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 316 | |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 317 | const std::string binary_path = Paths::Get().temporary_update_binary(); |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 318 | unlink(binary_path.c_str()); |
Tao Bao | cf60a44 | 2018-06-18 14:56:20 -0700 | [diff] [blame] | 319 | android::base::unique_fd fd( |
| 320 | open(binary_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0755)); |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 321 | if (fd == -1) { |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 322 | PLOG(ERROR) << "Failed to create " << binary_path; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 323 | return false; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 324 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 325 | |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 326 | if (auto error = ExtractEntryToFile(zip, &binary_entry, fd); error != 0) { |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 327 | LOG(ERROR) << "Failed to extract " << UPDATE_BINARY_NAME << ": " << ErrorCodeString(error); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 328 | return false; |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 331 | // When executing the update binary contained in the package, the arguments passed are: |
| 332 | // - the version number for this interface |
| 333 | // - an FD to which the program can write in order to update the progress bar. |
| 334 | // - the name of the package zip file. |
| 335 | // - an optional argument "retry" if this update is a retry of a failed update attempt. |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 336 | *cmd = { |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 337 | binary_path, |
Tao Bao | bd0ddcd | 2017-05-04 13:03:18 -0700 | [diff] [blame] | 338 | std::to_string(kRecoveryApiVersion), |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 339 | std::to_string(status_fd), |
Tao Bao | 00d5757 | 2017-05-02 15:48:54 -0700 | [diff] [blame] | 340 | package, |
Tao Bao | bc4b1fe | 2017-04-17 16:46:05 -0700 | [diff] [blame] | 341 | }; |
| 342 | if (retry_count > 0) { |
| 343 | cmd->push_back("retry"); |
| 344 | } |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 345 | return true; |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 346 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 347 | |
Tianjie Xu | 3795710 | 2017-06-07 17:59:55 -0700 | [diff] [blame] | 348 | static void log_max_temperature(int* max_temperature, const std::atomic<bool>& logger_finished) { |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 349 | CHECK(max_temperature != nullptr); |
| 350 | std::mutex mtx; |
| 351 | std::unique_lock<std::mutex> lck(mtx); |
Tianjie Xu | 3795710 | 2017-06-07 17:59:55 -0700 | [diff] [blame] | 352 | while (!logger_finished.load() && |
| 353 | finish_log_temperature.wait_for(lck, 20s) == std::cv_status::timeout) { |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 354 | *max_temperature = std::max(*max_temperature, GetMaxValueFromThermalZone()); |
| 355 | } |
| 356 | } |
| 357 | |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 358 | static bool PerformPowerwashIfRequired(ZipArchiveHandle zip, Device *device) { |
| 359 | const auto payload_properties = ExtractPayloadProperties(zip); |
| 360 | if (payload_properties.find("POWERWASH=1") != std::string::npos) { |
| 361 | LOG(INFO) << "Payload properties has POWERWASH=1, wiping userdata..."; |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 362 | return WipeData(device); |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 363 | } |
| 364 | return true; |
| 365 | } |
| 366 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 367 | // If the package contains an update binary, extract it and run it. |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 368 | static InstallResult TryUpdateBinary(Package* package, bool* wipe_cache, |
| 369 | std::vector<std::string>* log_buffer, int retry_count, |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 370 | int* max_temperature, Device* device) { |
| 371 | auto ui = device->GetUI(); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 372 | std::map<std::string, std::string> metadata; |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 373 | auto zip = package->GetZipArchiveHandle(); |
Christopher N. Hesse | 66f9510 | 2018-04-03 21:57:30 +0200 | [diff] [blame] | 374 | bool has_metadata = ReadMetadataFromPackage(zip, &metadata); |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 375 | |
Richard Hansen | b6a02ea | 2020-05-04 20:46:09 -0400 | [diff] [blame] | 376 | bool package_is_ab = has_metadata && get_value(metadata, "ota-type") == OtaTypeToString(OtaType::AB); |
Yifan Hong | f2af562 | 2020-04-27 20:16:18 -0700 | [diff] [blame] | 377 | bool device_supports_ab = android::base::GetBoolProperty("ro.build.ab_update", false); |
Christopher N. Hesse | 66f9510 | 2018-04-03 21:57:30 +0200 | [diff] [blame] | 378 | bool ab_device_supports_nonab = true; |
Yifan Hong | f2af562 | 2020-04-27 20:16:18 -0700 | [diff] [blame] | 379 | bool device_only_supports_ab = device_supports_ab && !ab_device_supports_nonab; |
aleasto | 41d0154 | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 380 | bool device_supports_virtual_ab = android::base::GetBoolProperty("ro.virtual_ab.enabled", false); |
Yifan Hong | f2af562 | 2020-04-27 20:16:18 -0700 | [diff] [blame] | 381 | |
Kelvin Zhang | 33c62fc | 2021-05-14 17:15:50 -0400 | [diff] [blame] | 382 | const auto current_spl = android::base::GetProperty("ro.build.version.security_patch", ""); |
| 383 | if (ViolatesSPLDowngrade(zip, current_spl)) { |
| 384 | LOG(ERROR) << "Denying OTA because it's SPL downgrade"; |
| 385 | return INSTALL_ERROR; |
| 386 | } |
| 387 | |
LuK1337 | 2a70656 | 2023-08-04 22:09:53 +0200 | [diff] [blame^] | 388 | const auto reboot_to_recovery = [] { |
| 389 | if (std::string err; !clear_bootloader_message(&err)) { |
| 390 | LOG(ERROR) << "Failed to clear BCB message: " << err; |
| 391 | } |
| 392 | Reboot("userrequested,recovery,ui"); |
| 393 | }; |
| 394 | |
LuK1337 | 107dfac | 2023-07-29 11:16:41 +0200 | [diff] [blame] | 395 | static bool ab_package_installed = false; |
| 396 | if (ab_package_installed) { |
| 397 | if (ask_to_ab_reboot(device)) { |
LuK1337 | 2a70656 | 2023-08-04 22:09:53 +0200 | [diff] [blame^] | 398 | reboot_to_recovery(); |
LuK1337 | 107dfac | 2023-07-29 11:16:41 +0200 | [diff] [blame] | 399 | } |
| 400 | return INSTALL_ERROR; |
| 401 | } |
| 402 | |
Yifan Hong | f2af562 | 2020-04-27 20:16:18 -0700 | [diff] [blame] | 403 | if (package_is_ab) { |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 404 | CHECK(package->GetType() == PackageType::kFile); |
| 405 | } |
| 406 | |
Yifan Hong | f2af562 | 2020-04-27 20:16:18 -0700 | [diff] [blame] | 407 | // Verify against the metadata in the package first. Expects A/B metadata if: |
| 408 | // Package declares itself as an A/B package |
| 409 | // Package does not declare itself as an A/B package, but device only supports A/B; |
| 410 | // still calls CheckPackageMetadata to get a meaningful error message. |
| 411 | if (package_is_ab || device_only_supports_ab) { |
Alessandro Astone | 5f24cb1 | 2020-03-17 23:06:52 +0100 | [diff] [blame] | 412 | if (!CheckPackageMetadata(metadata, OtaType::AB, ui)) { |
Yifan Hong | f2af562 | 2020-04-27 20:16:18 -0700 | [diff] [blame] | 413 | log_buffer->push_back(android::base::StringPrintf("error: %d", kUpdateBinaryCommandFailure)); |
| 414 | return INSTALL_ERROR; |
| 415 | } |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 416 | } |
| 417 | |
aleasto | 41d0154 | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 418 | if (!package_is_ab && !logical_partitions_mapped()) { |
| 419 | CreateSnapshotPartitions(); |
| 420 | map_logical_partitions(); |
| 421 | } else if (package_is_ab && device_supports_virtual_ab && logical_partitions_mapped()) { |
| 422 | LOG(ERROR) << "Logical partitions are mapped. " |
| 423 | << "Please reboot recovery before installing an OTA update."; |
| 424 | return INSTALL_ERROR; |
| 425 | } |
| 426 | |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 427 | ReadSourceTargetBuild(metadata, log_buffer); |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 428 | |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 429 | // The updater in child process writes to the pipe to communicate with recovery. |
| 430 | android::base::unique_fd pipe_read, pipe_write; |
Bill Peckham | 08ba1ad | 2019-03-28 18:42:13 -0700 | [diff] [blame] | 431 | // Explicitly disable O_CLOEXEC using 0 as the flags (last) parameter to Pipe |
| 432 | // so that the child updater process will recieve a non-closed fd. |
| 433 | if (!android::base::Pipe(&pipe_read, &pipe_write, 0)) { |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 434 | PLOG(ERROR) << "Failed to create pipe for updater-recovery communication"; |
| 435 | return INSTALL_CORRUPT; |
| 436 | } |
| 437 | |
| 438 | // The updater-recovery communication protocol. |
| 439 | // |
| 440 | // progress <frac> <secs> |
| 441 | // fill up the next <frac> part of of the progress bar over <secs> seconds. If <secs> is |
| 442 | // zero, use `set_progress` commands to manually control the progress of this segment of the |
| 443 | // bar. |
| 444 | // |
| 445 | // set_progress <frac> |
| 446 | // <frac> should be between 0.0 and 1.0; sets the progress bar within the segment defined by |
| 447 | // the most recent progress command. |
| 448 | // |
| 449 | // ui_print <string> |
| 450 | // display <string> on the screen. |
| 451 | // |
| 452 | // wipe_cache |
| 453 | // a wipe of cache will be performed following a successful installation. |
| 454 | // |
| 455 | // clear_display |
| 456 | // turn off the text display. |
| 457 | // |
| 458 | // enable_reboot |
| 459 | // packages can explicitly request that they want the user to be able to reboot during |
| 460 | // installation (useful for debugging packages that don't exit). |
| 461 | // |
| 462 | // retry_update |
| 463 | // updater encounters some issue during the update. It requests a reboot to retry the same |
| 464 | // package automatically. |
| 465 | // |
| 466 | // log <string> |
| 467 | // updater requests logging the string (e.g. cause of the failure). |
| 468 | // |
| 469 | |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 470 | std::string package_path = package->GetPath(); |
| 471 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 472 | std::vector<std::string> args; |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 473 | if (auto setup_result = |
Yifan Hong | f2af562 | 2020-04-27 20:16:18 -0700 | [diff] [blame] | 474 | package_is_ab |
| 475 | ? SetUpAbUpdateCommands(package_path, zip, pipe_write.get(), &args) |
| 476 | : SetUpNonAbUpdateCommands(package_path, zip, retry_count, pipe_write.get(), &args); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 477 | !setup_result) { |
Tianjie Xu | 3fec8c6 | 2017-08-23 00:18:07 -0700 | [diff] [blame] | 478 | log_buffer->push_back(android::base::StringPrintf("error: %d", kUpdateBinaryCommandFailure)); |
Tao Bao | 36c7276 | 2019-04-30 00:25:41 -0700 | [diff] [blame] | 479 | return INSTALL_CORRUPT; |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 480 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 481 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 482 | pid_t pid = fork(); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 483 | if (pid == -1) { |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 484 | PLOG(ERROR) << "Failed to fork update binary"; |
Tianjie Xu | 3fec8c6 | 2017-08-23 00:18:07 -0700 | [diff] [blame] | 485 | log_buffer->push_back(android::base::StringPrintf("error: %d", kForkUpdateBinaryFailure)); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 486 | return INSTALL_ERROR; |
| 487 | } |
| 488 | |
| 489 | if (pid == 0) { |
| 490 | umask(022); |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 491 | pipe_read.reset(); |
| 492 | |
| 493 | // Convert the std::string vector to a NULL-terminated char* vector suitable for execv. |
| 494 | auto chr_args = StringVectorToNullTerminatedArray(args); |
Tao Bao | 3d69f0d | 2018-12-20 09:44:06 -0800 | [diff] [blame] | 495 | execv(chr_args[0], chr_args.data()); |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 496 | // We shouldn't use LOG/PLOG in the forked process, since they may cause the child process to |
| 497 | // hang. This deadlock results from an improperly copied mutex in the ui functions. |
| 498 | // (Bug: 34769056) |
Tianjie Xu | ab1abae | 2017-01-30 16:48:52 -0800 | [diff] [blame] | 499 | fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno)); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 500 | _exit(EXIT_FAILURE); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 501 | } |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 502 | pipe_write.reset(); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 503 | |
Tianjie Xu | 3795710 | 2017-06-07 17:59:55 -0700 | [diff] [blame] | 504 | std::atomic<bool> logger_finished(false); |
| 505 | std::thread temperature_logger(log_max_temperature, max_temperature, std::ref(logger_finished)); |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 506 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 507 | *wipe_cache = false; |
| 508 | bool retry_update = false; |
| 509 | |
| 510 | char buffer[1024]; |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 511 | FILE* from_child = android::base::Fdopen(std::move(pipe_read), "r"); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 512 | while (fgets(buffer, sizeof(buffer), from_child) != nullptr) { |
| 513 | std::string line(buffer); |
| 514 | size_t space = line.find_first_of(" \n"); |
| 515 | std::string command(line.substr(0, space)); |
| 516 | if (command.empty()) continue; |
| 517 | |
| 518 | // Get rid of the leading and trailing space and/or newline. |
| 519 | std::string args = space == std::string::npos ? "" : android::base::Trim(line.substr(space)); |
| 520 | |
| 521 | if (command == "progress") { |
| 522 | std::vector<std::string> tokens = android::base::Split(args, " "); |
| 523 | double fraction; |
| 524 | int seconds; |
| 525 | if (tokens.size() == 2 && android::base::ParseDouble(tokens[0].c_str(), &fraction) && |
| 526 | android::base::ParseInt(tokens[1], &seconds)) { |
| 527 | ui->ShowProgress(fraction * (1 - VERIFICATION_PROGRESS_FRACTION), seconds); |
| 528 | } else { |
| 529 | LOG(ERROR) << "invalid \"progress\" parameters: " << line; |
| 530 | } |
| 531 | } else if (command == "set_progress") { |
| 532 | std::vector<std::string> tokens = android::base::Split(args, " "); |
| 533 | double fraction; |
| 534 | if (tokens.size() == 1 && android::base::ParseDouble(tokens[0].c_str(), &fraction)) { |
| 535 | ui->SetProgress(fraction); |
| 536 | } else { |
| 537 | LOG(ERROR) << "invalid \"set_progress\" parameters: " << line; |
| 538 | } |
| 539 | } else if (command == "ui_print") { |
Tao Bao | f013642 | 2017-01-21 13:03:25 -0800 | [diff] [blame] | 540 | ui->PrintOnScreenOnly("%s\n", args.c_str()); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 541 | fflush(stdout); |
| 542 | } else if (command == "wipe_cache") { |
| 543 | *wipe_cache = true; |
| 544 | } else if (command == "clear_display") { |
| 545 | ui->SetBackground(RecoveryUI::NONE); |
| 546 | } else if (command == "enable_reboot") { |
| 547 | // packages can explicitly request that they want the user |
| 548 | // to be able to reboot during installation (useful for |
| 549 | // debugging packages that don't exit). |
| 550 | ui->SetEnableReboot(true); |
| 551 | } else if (command == "retry_update") { |
| 552 | retry_update = true; |
| 553 | } else if (command == "log") { |
| 554 | if (!args.empty()) { |
| 555 | // Save the logging request from updater and write to last_install later. |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 556 | log_buffer->push_back(args); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 557 | } else { |
| 558 | LOG(ERROR) << "invalid \"log\" parameters: " << line; |
| 559 | } |
| 560 | } else { |
| 561 | LOG(ERROR) << "unknown command [" << command << "]"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 562 | } |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 563 | } |
| 564 | fclose(from_child); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 565 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 566 | int status; |
| 567 | waitpid(pid, &status, 0); |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 568 | |
Tianjie Xu | 3795710 | 2017-06-07 17:59:55 -0700 | [diff] [blame] | 569 | logger_finished.store(true); |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 570 | finish_log_temperature.notify_one(); |
| 571 | temperature_logger.join(); |
| 572 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 573 | if (retry_update) { |
| 574 | return INSTALL_RETRY; |
| 575 | } |
Tao Bao | e7b775c | 2018-11-07 15:13:30 -0800 | [diff] [blame] | 576 | if (WIFEXITED(status)) { |
| 577 | if (WEXITSTATUS(status) != EXIT_SUCCESS) { |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 578 | LOG(ERROR) << "Error in " << package_path << " (status " << WEXITSTATUS(status) << ")"; |
Tao Bao | e7b775c | 2018-11-07 15:13:30 -0800 | [diff] [blame] | 579 | return INSTALL_ERROR; |
| 580 | } |
| 581 | } else if (WIFSIGNALED(status)) { |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 582 | LOG(ERROR) << "Error in " << package_path << " (killed by signal " << WTERMSIG(status) << ")"; |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 583 | return INSTALL_ERROR; |
Tao Bao | e7b775c | 2018-11-07 15:13:30 -0800 | [diff] [blame] | 584 | } else { |
| 585 | LOG(FATAL) << "Invalid status code " << status; |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 586 | } |
Tim Zimmermann | a3b26e9 | 2022-10-14 19:43:01 +0200 | [diff] [blame] | 587 | if (package_is_ab) { |
LuK1337 | 107dfac | 2023-07-29 11:16:41 +0200 | [diff] [blame] | 588 | ab_package_installed = true; |
Tim Zimmermann | a3b26e9 | 2022-10-14 19:43:01 +0200 | [diff] [blame] | 589 | PerformPowerwashIfRequired(zip, device); |
LuK1337 | 107dfac | 2023-07-29 11:16:41 +0200 | [diff] [blame] | 590 | if (ask_to_ab_reboot(device)) { |
LuK1337 | 2a70656 | 2023-08-04 22:09:53 +0200 | [diff] [blame^] | 591 | reboot_to_recovery(); |
LuK1337 | 107dfac | 2023-07-29 11:16:41 +0200 | [diff] [blame] | 592 | } |
Tim Zimmermann | a3b26e9 | 2022-10-14 19:43:01 +0200 | [diff] [blame] | 593 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 594 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 595 | return INSTALL_SUCCESS; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 598 | static InstallResult VerifyAndInstallPackage(Package* package, bool* wipe_cache, |
| 599 | std::vector<std::string>* log_buffer, int retry_count, |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 600 | int* max_temperature, Device* device) { |
| 601 | auto ui = device->GetUI(); |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 602 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 603 | // Give verification half the progress bar... |
| 604 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 605 | ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 606 | |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 607 | // Verify package. |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 608 | if (!verify_package(package, ui)) { |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 609 | log_buffer->push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure)); |
Tom Marshall | 3c709de | 2018-06-21 00:57:24 +0200 | [diff] [blame] | 610 | if (!ui->IsTextVisible() || !ask_to_continue_unverified(ui->GetDevice())) { |
| 611 | return INSTALL_CORRUPT; |
| 612 | } |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 613 | } |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 614 | |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 615 | // Verify and install the contents of the package. |
| 616 | ui->Print("Installing update...\n"); |
| 617 | if (retry_count > 0) { |
| 618 | ui->Print("Retry attempt: %d\n", retry_count); |
| 619 | } |
| 620 | ui->SetEnableReboot(false); |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 621 | auto result = |
| 622 | TryUpdateBinary(package, wipe_cache, log_buffer, retry_count, max_temperature, device); |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 623 | ui->SetEnableReboot(true); |
| 624 | ui->Print("\n"); |
| 625 | |
Tao Bao | 29ee69b | 2017-05-01 12:23:17 -0700 | [diff] [blame] | 626 | return result; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 627 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 628 | |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 629 | InstallResult InstallPackage(Package* package, const std::string_view package_id, |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 630 | bool should_wipe_cache, int retry_count, Device* device) { |
| 631 | auto ui = device->GetUI(); |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 632 | auto start = std::chrono::system_clock::now(); |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 633 | |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 634 | int start_temperature = GetMaxValueFromThermalZone(); |
| 635 | int max_temperature = start_temperature; |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 636 | |
Tao Bao | adc99ef | 2019-04-29 23:48:02 -0700 | [diff] [blame] | 637 | InstallResult result; |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 638 | std::vector<std::string> log_buffer; |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 639 | |
Tianjie Xu | 164c60a | 2019-05-15 13:59:39 -0700 | [diff] [blame] | 640 | ui->Print("Supported API: %d\n", kRecoveryApiVersion); |
| 641 | |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 642 | ui->Print("Finding update package...\n"); |
| 643 | LOG(INFO) << "Update package id: " << package_id; |
| 644 | if (!package) { |
| 645 | log_buffer.push_back(android::base::StringPrintf("error: %d", kMapFileFailure)); |
| 646 | result = INSTALL_CORRUPT; |
| 647 | } else if (setup_install_mounts() != 0) { |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 648 | LOG(ERROR) << "failed to set up expected mounts for install; aborting"; |
| 649 | result = INSTALL_ERROR; |
| 650 | } else { |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 651 | bool updater_wipe_cache = false; |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 652 | result = VerifyAndInstallPackage(package, &updater_wipe_cache, &log_buffer, retry_count, |
Kelvin Zhang | a4208b5 | 2022-02-14 18:38:06 -0800 | [diff] [blame] | 653 | &max_temperature, device); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 654 | should_wipe_cache = should_wipe_cache || updater_wipe_cache; |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // Measure the time spent to apply OTA update in seconds. |
| 658 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - start; |
| 659 | int time_total = static_cast<int>(duration.count()); |
| 660 | |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 661 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 662 | // Skip logging the uncrypt_status on devices without /cache. |
| 663 | if (has_cache) { |
| 664 | static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status"; |
| 665 | if (ensure_path_mounted(UNCRYPT_STATUS) != 0) { |
| 666 | LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 667 | } else { |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 668 | std::string uncrypt_status; |
| 669 | if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) { |
| 670 | PLOG(WARNING) << "failed to read uncrypt status"; |
| 671 | } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) { |
| 672 | LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status; |
Tianjie Xu | a286778 | 2017-03-24 14:13:56 -0700 | [diff] [blame] | 673 | } else { |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 674 | log_buffer.push_back(android::base::Trim(uncrypt_status)); |
Tianjie Xu | a286778 | 2017-03-24 14:13:56 -0700 | [diff] [blame] | 675 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 676 | } |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 677 | } |
Tao Bao | badaac4 | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 678 | |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 679 | // The first two lines need to be the package name and install result. |
| 680 | std::vector<std::string> log_header = { |
Tianjie Xu | 980f92e | 2019-06-11 15:43:43 -0700 | [diff] [blame] | 681 | std::string(package_id), |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 682 | result == INSTALL_SUCCESS ? "1" : "0", |
| 683 | "time_total: " + std::to_string(time_total), |
| 684 | "retry: " + std::to_string(retry_count), |
| 685 | }; |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 686 | |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 687 | int end_temperature = GetMaxValueFromThermalZone(); |
| 688 | max_temperature = std::max(end_temperature, max_temperature); |
| 689 | if (start_temperature > 0) { |
| 690 | log_buffer.push_back("temperature_start: " + std::to_string(start_temperature)); |
| 691 | } |
| 692 | if (end_temperature > 0) { |
| 693 | log_buffer.push_back("temperature_end: " + std::to_string(end_temperature)); |
| 694 | } |
| 695 | if (max_temperature > 0) { |
| 696 | log_buffer.push_back("temperature_max: " + std::to_string(max_temperature)); |
| 697 | } |
Tianjie Xu | 3ee2b9d | 2017-03-27 14:12:26 -0700 | [diff] [blame] | 698 | |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 699 | std::string log_content = |
| 700 | android::base::Join(log_header, "\n") + "\n" + android::base::Join(log_buffer, "\n") + "\n"; |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 701 | const std::string& install_file = Paths::Get().temporary_install_file(); |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 702 | if (!android::base::WriteStringToFile(log_content, install_file)) { |
| 703 | PLOG(ERROR) << "failed to write " << install_file; |
| 704 | } |
Tao Bao | badaac4 | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 705 | |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 706 | // Write a copy into last_log. |
| 707 | LOG(INFO) << log_content; |
Tao Bao | badaac4 | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 708 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 709 | if (result == INSTALL_SUCCESS && should_wipe_cache) { |
| 710 | if (!WipeCache(ui, nullptr)) { |
| 711 | result = INSTALL_ERROR; |
| 712 | } |
| 713 | } |
| 714 | |
Tao Bao | f8119fb | 2017-04-18 21:35:12 -0700 | [diff] [blame] | 715 | return result; |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 716 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 717 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 718 | bool verify_package(Package* package, RecoveryUI* ui) { |
Tianjie Xu | e1d02fb | 2018-10-19 15:52:17 -0700 | [diff] [blame] | 719 | static constexpr const char* CERTIFICATE_ZIP_FILE = "/system/etc/security/otacerts.zip"; |
| 720 | std::vector<Certificate> loaded_keys = LoadKeysFromZipfile(CERTIFICATE_ZIP_FILE); |
| 721 | if (loaded_keys.empty()) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 722 | LOG(ERROR) << "Failed to load keys"; |
| 723 | return false; |
| 724 | } |
Tianjie Xu | e1d02fb | 2018-10-19 15:52:17 -0700 | [diff] [blame] | 725 | LOG(INFO) << loaded_keys.size() << " key(s) loaded from " << CERTIFICATE_ZIP_FILE; |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 726 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 727 | // Verify package. |
| 728 | ui->Print("Verifying update package...\n"); |
| 729 | auto t0 = std::chrono::system_clock::now(); |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 730 | int err = verify_file(package, loaded_keys); |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 731 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0; |
| 732 | ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err); |
| 733 | if (err != VERIFY_SUCCESS) { |
| 734 | LOG(ERROR) << "Signature verification failed"; |
| 735 | LOG(ERROR) << "error: " << kZipVerificationFailure; |
| 736 | return false; |
| 737 | } |
| 738 | return true; |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 739 | } |
Tianjie Xu | cd8faf7 | 2019-08-06 12:32:05 -0700 | [diff] [blame] | 740 | |
| 741 | bool SetupPackageMount(const std::string& package_path, bool* should_use_fuse) { |
| 742 | CHECK(should_use_fuse != nullptr); |
| 743 | |
| 744 | if (package_path.empty()) { |
| 745 | return false; |
| 746 | } |
| 747 | |
| 748 | *should_use_fuse = true; |
| 749 | if (package_path[0] == '@') { |
| 750 | auto block_map_path = package_path.substr(1); |
| 751 | if (ensure_path_mounted(block_map_path) != 0) { |
| 752 | LOG(ERROR) << "Failed to mount " << block_map_path; |
| 753 | return false; |
| 754 | } |
| 755 | // uncrypt only produces block map only if the package stays on /data. |
| 756 | *should_use_fuse = false; |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | // Package is not a block map file. |
| 761 | if (ensure_path_mounted(package_path) != 0) { |
| 762 | LOG(ERROR) << "Failed to mount " << package_path; |
| 763 | return false; |
| 764 | } |
| 765 | |
| 766 | // Reject the package if the input path doesn't equal the canonicalized path. |
| 767 | // e.g. /cache/../sdcard/update_package. |
| 768 | std::error_code ec; |
| 769 | auto canonical_path = std::filesystem::canonical(package_path, ec); |
| 770 | if (ec) { |
| 771 | LOG(ERROR) << "Failed to get canonical of " << package_path << ", " << ec.message(); |
| 772 | return false; |
| 773 | } |
| 774 | if (canonical_path.string() != package_path) { |
| 775 | LOG(ERROR) << "Installation aborts. The canonical path " << canonical_path.string() |
| 776 | << " doesn't equal the original path " << package_path; |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | constexpr const char* CACHE_ROOT = "/cache"; |
| 781 | if (android::base::StartsWith(package_path, CACHE_ROOT)) { |
| 782 | *should_use_fuse = false; |
| 783 | } |
| 784 | return true; |
| 785 | } |
Kelvin Zhang | e1ae78c | 2020-06-26 11:06:00 -0400 | [diff] [blame] | 786 | |
| 787 | // Check if `target_token` is in string `str_list`, where `str_list` is expected to be a |
| 788 | // list delimited by `deliminator` |
| 789 | // E.X. isInStringList("a", "a|b|c|d", "|") => true |
| 790 | // E.X. isInStringList("abc", "abc", "|") => true |
| 791 | static bool isInStringList(const std::string& target_token, const std::string& str_list, |
| 792 | const std::string& deliminator) { |
| 793 | if (target_token.length() > str_list.length()) { |
| 794 | return false; |
| 795 | } else if (target_token.length() == str_list.length() || deliminator.length() == 0) { |
| 796 | return target_token == str_list; |
| 797 | } |
| 798 | auto&& list = android::base::Split(str_list, deliminator); |
| 799 | return std::find(list.begin(), list.end(), target_token) != list.end(); |
| 800 | } |