2009-06-10 23:11:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-10-11 00:48:37 +02:00
|
|
|
#include "updater/updater.h"
|
2016-10-04 03:06:46 +02:00
|
|
|
|
2015-01-30 05:50:08 +01:00
|
|
|
#include <string.h>
|
2018-04-26 03:59:40 +02:00
|
|
|
#include <unistd.h>
|
2009-06-10 23:11:53 +02:00
|
|
|
|
2016-09-09 05:10:11 +02:00
|
|
|
#include <string>
|
|
|
|
|
2016-11-23 01:29:50 +01:00
|
|
|
#include <android-base/logging.h>
|
2016-10-04 03:06:46 +02:00
|
|
|
#include <android-base/strings.h>
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-22 23:48:35 +02:00
|
|
|
#include "edify/updater_runtime_interface.h"
|
2019-05-14 19:54:43 +02:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
Updater::~Updater() {
|
|
|
|
if (package_handle_) {
|
|
|
|
CloseArchive(package_handle_);
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
2019-05-03 10:05:04 +02:00
|
|
|
}
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-14 19:54:43 +02:00
|
|
|
bool Updater::Init(int fd, const std::string_view package_filename, bool is_retry) {
|
2016-11-23 01:29:50 +01:00
|
|
|
// Set up the pipe for sending commands back to the parent process.
|
2019-05-03 10:05:04 +02:00
|
|
|
cmd_pipe_.reset(fdopen(fd, "wb"));
|
|
|
|
if (!cmd_pipe_) {
|
|
|
|
LOG(ERROR) << "Failed to open the command pipe";
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
setlinebuf(cmd_pipe_.get());
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-14 19:54:43 +02:00
|
|
|
if (!mapped_package_.MapFile(std::string(package_filename))) {
|
2019-05-03 10:05:04 +02:00
|
|
|
LOG(ERROR) << "failed to map package " << package_filename;
|
|
|
|
return false;
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
2019-05-03 10:05:04 +02:00
|
|
|
if (int open_err = OpenArchiveFromMemory(mapped_package_.addr, mapped_package_.length,
|
2019-05-14 19:54:43 +02:00
|
|
|
std::string(package_filename).c_str(), &package_handle_);
|
2019-05-03 10:05:04 +02:00
|
|
|
open_err != 0) {
|
|
|
|
LOG(ERROR) << "failed to open package " << package_filename << ": "
|
|
|
|
<< ErrorCodeString(open_err);
|
|
|
|
return false;
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
2019-05-03 10:05:04 +02:00
|
|
|
if (!ReadEntryToString(package_handle_, SCRIPT_NAME, &updater_script_)) {
|
|
|
|
return false;
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
is_retry_ = is_retry;
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
bool Updater::RunUpdate() {
|
2019-05-14 19:54:43 +02:00
|
|
|
CHECK(runtime_);
|
|
|
|
|
2016-11-23 01:29:50 +01:00
|
|
|
// Parse the script.
|
2017-03-06 23:44:59 +01:00
|
|
|
std::unique_ptr<Expr> root;
|
2016-11-23 01:29:50 +01:00
|
|
|
int error_count = 0;
|
2019-05-03 10:05:04 +02:00
|
|
|
int error = ParseString(updater_script_, &root, &error_count);
|
2016-11-23 01:29:50 +01:00
|
|
|
if (error != 0 || error_count > 0) {
|
|
|
|
LOG(ERROR) << error_count << " parse errors";
|
2019-05-03 10:05:04 +02:00
|
|
|
return false;
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate the parsed script.
|
2019-05-03 10:05:04 +02:00
|
|
|
State state(updater_script_, this);
|
|
|
|
state.is_retry = is_retry_;
|
|
|
|
|
|
|
|
bool status = Evaluate(&state, root, &result_);
|
|
|
|
if (status) {
|
|
|
|
fprintf(cmd_pipe_.get(), "ui_print script succeeded: result was [%s]\n", result_.c_str());
|
|
|
|
// Even though the script doesn't abort, still log the cause code if result is empty.
|
|
|
|
if (result_.empty() && state.cause_code != kNoCause) {
|
|
|
|
fprintf(cmd_pipe_.get(), "log cause: %d\n", state.cause_code);
|
|
|
|
}
|
2019-05-14 19:54:43 +02:00
|
|
|
for (const auto& func : skipped_functions_) {
|
|
|
|
LOG(WARNING) << "Skipped executing function " << func;
|
|
|
|
}
|
2019-05-03 10:05:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
ParseAndReportErrorCode(&state);
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-14 19:54:43 +02:00
|
|
|
void Updater::WriteToCommandPipe(const std::string_view message, bool flush) const {
|
|
|
|
fprintf(cmd_pipe_.get(), "%s\n", std::string(message).c_str());
|
2019-05-03 10:05:04 +02:00
|
|
|
if (flush) {
|
|
|
|
fflush(cmd_pipe_.get());
|
|
|
|
}
|
|
|
|
}
|
2016-11-23 01:29:50 +01:00
|
|
|
|
2019-05-14 19:54:43 +02:00
|
|
|
void Updater::UiPrint(const std::string_view message) const {
|
2019-05-03 10:05:04 +02:00
|
|
|
// "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
|
|
|
|
// so skip sending empty strings to ui.
|
2019-05-14 19:54:43 +02:00
|
|
|
std::vector<std::string> lines = android::base::Split(std::string(message), "\n");
|
2019-05-03 10:05:04 +02:00
|
|
|
for (const auto& line : lines) {
|
|
|
|
if (!line.empty()) {
|
|
|
|
fprintf(cmd_pipe_.get(), "ui_print %s\n", line.c_str());
|
2012-02-09 20:13:23 +01:00
|
|
|
}
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
2012-02-09 20:13:23 +01:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
// on the updater side, we need to dump the contents to stderr (which has
|
|
|
|
// been redirected to the log file). because the recovery will only print
|
|
|
|
// the contents to screen when processing pipe command ui_print.
|
|
|
|
LOG(INFO) << message;
|
|
|
|
}
|
|
|
|
|
2019-05-14 19:54:43 +02:00
|
|
|
std::string Updater::FindBlockDeviceName(const std::string_view name) const {
|
|
|
|
return runtime_->FindBlockDeviceName(name);
|
|
|
|
}
|
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
void Updater::ParseAndReportErrorCode(State* state) {
|
|
|
|
CHECK(state);
|
|
|
|
if (state->errmsg.empty()) {
|
|
|
|
LOG(ERROR) << "script aborted (no error message)";
|
|
|
|
fprintf(cmd_pipe_.get(), "ui_print script aborted (no error message)\n");
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "script aborted: " << state->errmsg;
|
|
|
|
const std::vector<std::string> lines = android::base::Split(state->errmsg, "\n");
|
|
|
|
for (const std::string& line : lines) {
|
|
|
|
// Parse the error code in abort message.
|
|
|
|
// Example: "E30: This package is for bullhead devices."
|
|
|
|
if (!line.empty() && line[0] == 'E') {
|
|
|
|
if (sscanf(line.c_str(), "E%d: ", &state->error_code) != 1) {
|
|
|
|
LOG(ERROR) << "Failed to parse error code: [" << line << "]";
|
2016-05-31 18:29:49 +02:00
|
|
|
}
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
2019-05-03 10:05:04 +02:00
|
|
|
fprintf(cmd_pipe_.get(), "ui_print %s\n", line.c_str());
|
2016-05-31 18:29:49 +02:00
|
|
|
}
|
2019-05-03 10:05:04 +02:00
|
|
|
}
|
2016-05-31 18:29:49 +02:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
// Installation has been aborted. Set the error code to kScriptExecutionFailure unless
|
|
|
|
// a more specific code has been set in errmsg.
|
|
|
|
if (state->error_code == kNoError) {
|
|
|
|
state->error_code = kScriptExecutionFailure;
|
|
|
|
}
|
|
|
|
fprintf(cmd_pipe_.get(), "log error: %d\n", state->error_code);
|
|
|
|
// Cause code should provide additional information about the abort.
|
|
|
|
if (state->cause_code != kNoCause) {
|
|
|
|
fprintf(cmd_pipe_.get(), "log cause: %d\n", state->cause_code);
|
|
|
|
if (state->cause_code == kPatchApplicationFailure) {
|
|
|
|
LOG(INFO) << "Patch application failed, retry update.";
|
|
|
|
fprintf(cmd_pipe_.get(), "retry_update\n");
|
|
|
|
} else if (state->cause_code == kEioFailure) {
|
|
|
|
LOG(INFO) << "Update failed due to EIO, retry update.";
|
|
|
|
fprintf(cmd_pipe_.get(), "retry_update\n");
|
2009-06-10 23:11:53 +02:00
|
|
|
}
|
2019-05-03 10:05:04 +02:00
|
|
|
}
|
|
|
|
}
|
2009-06-10 23:11:53 +02:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
bool Updater::ReadEntryToString(ZipArchiveHandle za, const std::string& entry_name,
|
|
|
|
std::string* content) {
|
|
|
|
ZipEntry entry;
|
|
|
|
int find_err = FindEntry(za, entry_name, &entry);
|
|
|
|
if (find_err != 0) {
|
|
|
|
LOG(ERROR) << "failed to find " << entry_name
|
|
|
|
<< " in the package: " << ErrorCodeString(find_err);
|
|
|
|
return false;
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
content->resize(entry.uncompressed_length);
|
|
|
|
int extract_err = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&content->at(0)),
|
|
|
|
entry.uncompressed_length);
|
|
|
|
if (extract_err != 0) {
|
2019-05-22 23:34:12 +02:00
|
|
|
LOG(ERROR) << "failed to read " << entry_name
|
|
|
|
<< " from package: " << ErrorCodeString(extract_err);
|
2019-05-03 10:05:04 +02:00
|
|
|
return false;
|
2016-11-23 01:29:50 +01:00
|
|
|
}
|
2009-06-10 23:11:53 +02:00
|
|
|
|
2019-05-03 10:05:04 +02:00
|
|
|
return true;
|
2009-06-10 23:11:53 +02:00
|
|
|
}
|