blob: dbfa2f426344acd37513128e58872ec29b21380b [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
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 Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/updater.h"
Tao Bao59dcb9c2016-10-03 18:06:46 -070018
Elliott Hughescd3c55a2015-01-29 20:50:08 -080019#include <string.h>
Tao Bao641fa972018-04-25 18:59:40 -070020#include <unistd.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070022#include <string>
23
Tao Bao039f2da2016-11-22 16:29:50 -080024#include <android-base/logging.h>
Tao Bao59dcb9c2016-10-03 18:06:46 -070025#include <android-base/strings.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070026
Tianjie Xu1536db82019-05-14 10:54:43 -070027#include "updater/updater_runtime.h"
28
29Updater::Updater() : Updater(std::make_unique<UpdaterRuntime>(nullptr)) {}
30
Tianjie Xu58d59122019-05-03 01:05:04 -070031Updater::~Updater() {
32 if (package_handle_) {
33 CloseArchive(package_handle_);
34 }
Tao Bao039f2da2016-11-22 16:29:50 -080035}
36
Tianjie Xu1536db82019-05-14 10:54:43 -070037bool Updater::Init(int fd, const std::string_view package_filename, bool is_retry) {
Tao Bao039f2da2016-11-22 16:29:50 -080038 // Set up the pipe for sending commands back to the parent process.
Tianjie Xu58d59122019-05-03 01:05:04 -070039 cmd_pipe_.reset(fdopen(fd, "wb"));
40 if (!cmd_pipe_) {
41 LOG(ERROR) << "Failed to open the command pipe";
42 return false;
Tao Bao039f2da2016-11-22 16:29:50 -080043 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070044
Tianjie Xu58d59122019-05-03 01:05:04 -070045 setlinebuf(cmd_pipe_.get());
46
Tianjie Xu1536db82019-05-14 10:54:43 -070047 if (!mapped_package_.MapFile(std::string(package_filename))) {
Tianjie Xu58d59122019-05-03 01:05:04 -070048 LOG(ERROR) << "failed to map package " << package_filename;
49 return false;
50 }
51 if (int open_err = OpenArchiveFromMemory(mapped_package_.addr, mapped_package_.length,
Tianjie Xu1536db82019-05-14 10:54:43 -070052 std::string(package_filename).c_str(), &package_handle_);
Tianjie Xu58d59122019-05-03 01:05:04 -070053 open_err != 0) {
54 LOG(ERROR) << "failed to open package " << package_filename << ": "
55 << ErrorCodeString(open_err);
56 return false;
57 }
58 if (!ReadEntryToString(package_handle_, SCRIPT_NAME, &updater_script_)) {
59 return false;
Tao Bao039f2da2016-11-22 16:29:50 -080060 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070061
Tianjie Xu58d59122019-05-03 01:05:04 -070062 is_retry_ = is_retry;
63
Tianjie Xu58d59122019-05-03 01:05:04 -070064 return true;
65}
Doug Zongker9931f7f2009-06-10 14:11:53 -070066
Tianjie Xu58d59122019-05-03 01:05:04 -070067bool Updater::RunUpdate() {
Tianjie Xu1536db82019-05-14 10:54:43 -070068 CHECK(runtime_);
69
Tao Bao039f2da2016-11-22 16:29:50 -080070 // Parse the script.
Tianjie Xuc4447322017-03-06 14:44:59 -080071 std::unique_ptr<Expr> root;
Tao Bao039f2da2016-11-22 16:29:50 -080072 int error_count = 0;
Tianjie Xu58d59122019-05-03 01:05:04 -070073 int error = ParseString(updater_script_, &root, &error_count);
Tao Bao039f2da2016-11-22 16:29:50 -080074 if (error != 0 || error_count > 0) {
75 LOG(ERROR) << error_count << " parse errors";
Tianjie Xu58d59122019-05-03 01:05:04 -070076 return false;
Tao Bao039f2da2016-11-22 16:29:50 -080077 }
Doug Zongker9931f7f2009-06-10 14:11:53 -070078
Tao Bao039f2da2016-11-22 16:29:50 -080079 // Evaluate the parsed script.
Tianjie Xu58d59122019-05-03 01:05:04 -070080 State state(updater_script_, this);
81 state.is_retry = is_retry_;
Doug Zongker9931f7f2009-06-10 14:11:53 -070082
Tianjie Xu58d59122019-05-03 01:05:04 -070083 bool status = Evaluate(&state, root, &result_);
84 if (status) {
85 fprintf(cmd_pipe_.get(), "ui_print script succeeded: result was [%s]\n", result_.c_str());
86 // Even though the script doesn't abort, still log the cause code if result is empty.
87 if (result_.empty() && state.cause_code != kNoCause) {
88 fprintf(cmd_pipe_.get(), "log cause: %d\n", state.cause_code);
89 }
Tianjie Xu1536db82019-05-14 10:54:43 -070090 for (const auto& func : skipped_functions_) {
91 LOG(WARNING) << "Skipped executing function " << func;
92 }
Tianjie Xu58d59122019-05-03 01:05:04 -070093 return true;
94 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -070095
Tianjie Xu58d59122019-05-03 01:05:04 -070096 ParseAndReportErrorCode(&state);
97 return false;
98}
Tianjie Xu7ce287d2016-05-31 09:29:49 -070099
Tianjie Xu1536db82019-05-14 10:54:43 -0700100void Updater::WriteToCommandPipe(const std::string_view message, bool flush) const {
101 fprintf(cmd_pipe_.get(), "%s\n", std::string(message).c_str());
Tianjie Xu58d59122019-05-03 01:05:04 -0700102 if (flush) {
103 fflush(cmd_pipe_.get());
104 }
105}
106
Tianjie Xu1536db82019-05-14 10:54:43 -0700107void Updater::UiPrint(const std::string_view message) const {
Tianjie Xu58d59122019-05-03 01:05:04 -0700108 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
109 // so skip sending empty strings to ui.
Tianjie Xu1536db82019-05-14 10:54:43 -0700110 std::vector<std::string> lines = android::base::Split(std::string(message), "\n");
Tianjie Xu58d59122019-05-03 01:05:04 -0700111 for (const auto& line : lines) {
112 if (!line.empty()) {
113 fprintf(cmd_pipe_.get(), "ui_print %s\n", line.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800114 }
115 }
116
Tianjie Xu58d59122019-05-03 01:05:04 -0700117 // on the updater side, we need to dump the contents to stderr (which has
118 // been redirected to the log file). because the recovery will only print
119 // the contents to screen when processing pipe command ui_print.
120 LOG(INFO) << message;
121}
Tao Bao039f2da2016-11-22 16:29:50 -0800122
Tianjie Xu1536db82019-05-14 10:54:43 -0700123std::string Updater::FindBlockDeviceName(const std::string_view name) const {
124 return runtime_->FindBlockDeviceName(name);
125}
126
Tianjie Xu58d59122019-05-03 01:05:04 -0700127void Updater::ParseAndReportErrorCode(State* state) {
128 CHECK(state);
129 if (state->errmsg.empty()) {
130 LOG(ERROR) << "script aborted (no error message)";
131 fprintf(cmd_pipe_.get(), "ui_print script aborted (no error message)\n");
Tao Bao039f2da2016-11-22 16:29:50 -0800132 } else {
Tianjie Xu58d59122019-05-03 01:05:04 -0700133 LOG(ERROR) << "script aborted: " << state->errmsg;
134 const std::vector<std::string> lines = android::base::Split(state->errmsg, "\n");
135 for (const std::string& line : lines) {
136 // Parse the error code in abort message.
137 // Example: "E30: This package is for bullhead devices."
138 if (!line.empty() && line[0] == 'E') {
139 if (sscanf(line.c_str(), "E%d: ", &state->error_code) != 1) {
140 LOG(ERROR) << "Failed to parse error code: [" << line << "]";
141 }
142 }
143 fprintf(cmd_pipe_.get(), "ui_print %s\n", line.c_str());
144 }
Tao Bao039f2da2016-11-22 16:29:50 -0800145 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700146
Tianjie Xu58d59122019-05-03 01:05:04 -0700147 // Installation has been aborted. Set the error code to kScriptExecutionFailure unless
148 // a more specific code has been set in errmsg.
149 if (state->error_code == kNoError) {
150 state->error_code = kScriptExecutionFailure;
151 }
152 fprintf(cmd_pipe_.get(), "log error: %d\n", state->error_code);
153 // Cause code should provide additional information about the abort.
154 if (state->cause_code != kNoCause) {
155 fprintf(cmd_pipe_.get(), "log cause: %d\n", state->cause_code);
156 if (state->cause_code == kPatchApplicationFailure) {
157 LOG(INFO) << "Patch application failed, retry update.";
158 fprintf(cmd_pipe_.get(), "retry_update\n");
159 } else if (state->cause_code == kEioFailure) {
160 LOG(INFO) << "Update failed due to EIO, retry update.";
161 fprintf(cmd_pipe_.get(), "retry_update\n");
162 }
163 }
164}
165
166bool Updater::ReadEntryToString(ZipArchiveHandle za, const std::string& entry_name,
167 std::string* content) {
168 ZipEntry entry;
169 int find_err = FindEntry(za, entry_name, &entry);
170 if (find_err != 0) {
171 LOG(ERROR) << "failed to find " << entry_name
172 << " in the package: " << ErrorCodeString(find_err);
173 return false;
Tao Bao039f2da2016-11-22 16:29:50 -0800174 }
Tao Bao039f2da2016-11-22 16:29:50 -0800175
Tianjie Xu58d59122019-05-03 01:05:04 -0700176 content->resize(entry.uncompressed_length);
177 int extract_err = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&content->at(0)),
178 entry.uncompressed_length);
179 if (extract_err != 0) {
180 LOG(ERROR) << "failed to read script from package: " << ErrorCodeString(extract_err);
181 return false;
182 }
183
184 return true;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700185}