Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 17 | #include "updater/updater.h" |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 19 | #include <string.h> |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 20 | #include <unistd.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 21 | |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 25 | #include <android-base/strings.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 26 | |
Tianjie Xu | 27556d0 | 2019-05-22 14:48:35 -0700 | [diff] [blame] | 27 | #include "edify/updater_runtime_interface.h" |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 28 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 29 | Updater::~Updater() { |
| 30 | if (package_handle_) { |
| 31 | CloseArchive(package_handle_); |
| 32 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 35 | bool Updater::Init(int fd, const std::string_view package_filename, bool is_retry) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 36 | // Set up the pipe for sending commands back to the parent process. |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 37 | cmd_pipe_.reset(fdopen(fd, "wb")); |
| 38 | if (!cmd_pipe_) { |
| 39 | LOG(ERROR) << "Failed to open the command pipe"; |
| 40 | return false; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 41 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 42 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 43 | setlinebuf(cmd_pipe_.get()); |
| 44 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 45 | if (!mapped_package_.MapFile(std::string(package_filename))) { |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 46 | LOG(ERROR) << "failed to map package " << package_filename; |
| 47 | return false; |
| 48 | } |
| 49 | if (int open_err = OpenArchiveFromMemory(mapped_package_.addr, mapped_package_.length, |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 50 | std::string(package_filename).c_str(), &package_handle_); |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 51 | open_err != 0) { |
| 52 | LOG(ERROR) << "failed to open package " << package_filename << ": " |
| 53 | << ErrorCodeString(open_err); |
| 54 | return false; |
| 55 | } |
| 56 | if (!ReadEntryToString(package_handle_, SCRIPT_NAME, &updater_script_)) { |
| 57 | return false; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 58 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 59 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 60 | is_retry_ = is_retry; |
| 61 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 62 | return true; |
| 63 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 64 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 65 | bool Updater::RunUpdate() { |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 66 | CHECK(runtime_); |
| 67 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 68 | // Parse the script. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 69 | std::unique_ptr<Expr> root; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 70 | int error_count = 0; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 71 | int error = ParseString(updater_script_, &root, &error_count); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 72 | if (error != 0 || error_count > 0) { |
| 73 | LOG(ERROR) << error_count << " parse errors"; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 74 | return false; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 75 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 76 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 77 | // Evaluate the parsed script. |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 78 | State state(updater_script_, this); |
| 79 | state.is_retry = is_retry_; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 80 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 81 | bool status = Evaluate(&state, root, &result_); |
| 82 | if (status) { |
| 83 | fprintf(cmd_pipe_.get(), "ui_print script succeeded: result was [%s]\n", result_.c_str()); |
| 84 | // Even though the script doesn't abort, still log the cause code if result is empty. |
| 85 | if (result_.empty() && state.cause_code != kNoCause) { |
| 86 | fprintf(cmd_pipe_.get(), "log cause: %d\n", state.cause_code); |
| 87 | } |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 88 | for (const auto& func : skipped_functions_) { |
| 89 | LOG(WARNING) << "Skipped executing function " << func; |
| 90 | } |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 91 | return true; |
| 92 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 93 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 94 | ParseAndReportErrorCode(&state); |
| 95 | return false; |
| 96 | } |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 97 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 98 | void Updater::WriteToCommandPipe(const std::string_view message, bool flush) const { |
| 99 | fprintf(cmd_pipe_.get(), "%s\n", std::string(message).c_str()); |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 100 | if (flush) { |
| 101 | fflush(cmd_pipe_.get()); |
| 102 | } |
| 103 | } |
| 104 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 105 | void Updater::UiPrint(const std::string_view message) const { |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 106 | // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "". |
| 107 | // so skip sending empty strings to ui. |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 108 | std::vector<std::string> lines = android::base::Split(std::string(message), "\n"); |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 109 | for (const auto& line : lines) { |
| 110 | if (!line.empty()) { |
| 111 | fprintf(cmd_pipe_.get(), "ui_print %s\n", line.c_str()); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 115 | // on the updater side, we need to dump the contents to stderr (which has |
| 116 | // been redirected to the log file). because the recovery will only print |
| 117 | // the contents to screen when processing pipe command ui_print. |
| 118 | LOG(INFO) << message; |
| 119 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 120 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 121 | std::string Updater::FindBlockDeviceName(const std::string_view name) const { |
| 122 | return runtime_->FindBlockDeviceName(name); |
| 123 | } |
| 124 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 125 | void Updater::ParseAndReportErrorCode(State* state) { |
| 126 | CHECK(state); |
| 127 | if (state->errmsg.empty()) { |
| 128 | LOG(ERROR) << "script aborted (no error message)"; |
| 129 | fprintf(cmd_pipe_.get(), "ui_print script aborted (no error message)\n"); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 130 | } else { |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 131 | LOG(ERROR) << "script aborted: " << state->errmsg; |
| 132 | const std::vector<std::string> lines = android::base::Split(state->errmsg, "\n"); |
| 133 | for (const std::string& line : lines) { |
| 134 | // Parse the error code in abort message. |
| 135 | // Example: "E30: This package is for bullhead devices." |
| 136 | if (!line.empty() && line[0] == 'E') { |
| 137 | if (sscanf(line.c_str(), "E%d: ", &state->error_code) != 1) { |
| 138 | LOG(ERROR) << "Failed to parse error code: [" << line << "]"; |
| 139 | } |
| 140 | } |
| 141 | fprintf(cmd_pipe_.get(), "ui_print %s\n", line.c_str()); |
| 142 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 143 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 144 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 145 | // Installation has been aborted. Set the error code to kScriptExecutionFailure unless |
| 146 | // a more specific code has been set in errmsg. |
| 147 | if (state->error_code == kNoError) { |
| 148 | state->error_code = kScriptExecutionFailure; |
| 149 | } |
| 150 | fprintf(cmd_pipe_.get(), "log error: %d\n", state->error_code); |
| 151 | // Cause code should provide additional information about the abort. |
| 152 | if (state->cause_code != kNoCause) { |
| 153 | fprintf(cmd_pipe_.get(), "log cause: %d\n", state->cause_code); |
| 154 | if (state->cause_code == kPatchApplicationFailure) { |
| 155 | LOG(INFO) << "Patch application failed, retry update."; |
| 156 | fprintf(cmd_pipe_.get(), "retry_update\n"); |
| 157 | } else if (state->cause_code == kEioFailure) { |
| 158 | LOG(INFO) << "Update failed due to EIO, retry update."; |
| 159 | fprintf(cmd_pipe_.get(), "retry_update\n"); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | bool Updater::ReadEntryToString(ZipArchiveHandle za, const std::string& entry_name, |
| 165 | std::string* content) { |
Kelvin Zhang | 4f81130 | 2020-09-16 14:06:12 -0400 | [diff] [blame] | 166 | ZipEntry64 entry; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 167 | int find_err = FindEntry(za, entry_name, &entry); |
| 168 | if (find_err != 0) { |
| 169 | LOG(ERROR) << "failed to find " << entry_name |
| 170 | << " in the package: " << ErrorCodeString(find_err); |
| 171 | return false; |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 172 | } |
Kelvin Zhang | d1ba38f | 2020-09-17 11:32:29 -0400 | [diff] [blame] | 173 | if (entry.uncompressed_length > std::numeric_limits<size_t>::max()) { |
| 174 | LOG(ERROR) << "Failed to extract " << entry_name |
| 175 | << " because's uncompressed size exceeds size of address space. " |
| 176 | << entry.uncompressed_length; |
| 177 | return false; |
| 178 | } |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 179 | content->resize(entry.uncompressed_length); |
| 180 | int extract_err = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&content->at(0)), |
| 181 | entry.uncompressed_length); |
| 182 | if (extract_err != 0) { |
Tianjie Xu | c1a5e26 | 2019-05-22 14:34:12 -0700 | [diff] [blame] | 183 | LOG(ERROR) << "failed to read " << entry_name |
| 184 | << " from package: " << ErrorCodeString(extract_err); |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
| 188 | return true; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 189 | } |