2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2013-05-14 20:03:02 +02:00
|
|
|
#include <dirent.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
2016-06-02 20:16:50 +02:00
|
|
|
#include <inttypes.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <limits.h>
|
2016-06-02 20:16:50 +02:00
|
|
|
#include <linux/fs.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <linux/input.h>
|
2013-05-14 20:03:02 +02:00
|
|
|
#include <stdarg.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-12-09 20:26:40 +01:00
|
|
|
#include <sys/klog.h>
|
2010-07-09 02:27:55 +02:00
|
|
|
#include <sys/stat.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <sys/types.h>
|
2016-01-14 00:05:20 +01:00
|
|
|
#include <sys/wait.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-09-11 00:32:24 +02:00
|
|
|
#include <chrono>
|
2016-06-02 20:16:50 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-09-11 00:32:24 +02:00
|
|
|
|
2015-05-27 23:46:17 +02:00
|
|
|
#include <adb.h>
|
2015-12-05 00:30:20 +01:00
|
|
|
#include <android-base/file.h>
|
2016-08-06 03:00:04 +02:00
|
|
|
#include <android-base/logging.h>
|
2016-02-06 03:25:58 +01:00
|
|
|
#include <android-base/parseint.h>
|
2016-09-24 00:30:55 +02:00
|
|
|
#include <android-base/properties.h>
|
2015-12-05 00:30:20 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2016-06-02 20:16:50 +02:00
|
|
|
#include <android-base/strings.h>
|
|
|
|
#include <android-base/unique_fd.h>
|
2016-06-25 03:22:02 +02:00
|
|
|
#include <bootloader_message/bootloader_message.h>
|
2015-05-27 23:46:17 +02:00
|
|
|
#include <cutils/android_reboot.h>
|
2016-09-24 00:30:55 +02:00
|
|
|
#include <cutils/properties.h> /* for property_list */
|
2016-04-02 03:24:39 +02:00
|
|
|
#include <healthd/BatteryMonitor.h>
|
2016-03-09 23:58:16 +01:00
|
|
|
#include <private/android_logger.h> /* private pmsg functions */
|
2016-04-02 03:24:39 +02:00
|
|
|
#include <selinux/label.h>
|
|
|
|
#include <selinux/selinux.h>
|
2016-09-09 05:10:11 +02:00
|
|
|
#include <ziparchive/zip_archive.h>
|
2016-02-17 21:21:52 +01:00
|
|
|
|
2015-05-27 23:46:17 +02:00
|
|
|
#include "adb_install.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
#include "common.h"
|
2015-05-27 23:46:17 +02:00
|
|
|
#include "device.h"
|
2016-04-30 20:49:59 +02:00
|
|
|
#include "error_code.h"
|
2015-05-27 23:46:17 +02:00
|
|
|
#include "fuse_sdcard_provider.h"
|
|
|
|
#include "fuse_sideload.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
#include "install.h"
|
2016-08-27 03:24:34 +02:00
|
|
|
#include "minadbd/minadbd.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
#include "minui/minui.h"
|
2016-09-09 05:10:11 +02:00
|
|
|
#include "otautil/DirUtil.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
#include "roots.h"
|
2016-10-22 02:46:13 +02:00
|
|
|
#include "rotate_logs.h"
|
2011-10-29 00:13:10 +02:00
|
|
|
#include "screen_ui.h"
|
2016-12-10 01:20:49 +01:00
|
|
|
#include "stub_ui.h"
|
2016-10-22 02:46:13 +02:00
|
|
|
#include "ui.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
static const struct option OPTIONS[] = {
|
|
|
|
{ "update_package", required_argument, NULL, 'u' },
|
2016-02-06 03:25:58 +01:00
|
|
|
{ "retry_count", required_argument, NULL, 'n' },
|
2009-03-04 04:28:42 +01:00
|
|
|
{ "wipe_data", no_argument, NULL, 'w' },
|
|
|
|
{ "wipe_cache", no_argument, NULL, 'c' },
|
2010-09-03 20:00:13 +02:00
|
|
|
{ "show_text", no_argument, NULL, 't' },
|
2015-03-30 18:43:49 +02:00
|
|
|
{ "sideload", no_argument, NULL, 's' },
|
|
|
|
{ "sideload_auto_reboot", no_argument, NULL, 'a' },
|
2012-04-12 20:01:22 +02:00
|
|
|
{ "just_exit", no_argument, NULL, 'x' },
|
2012-08-23 02:26:40 +02:00
|
|
|
{ "locale", required_argument, NULL, 'l' },
|
2013-11-25 22:53:25 +01:00
|
|
|
{ "stages", required_argument, NULL, 'g' },
|
2014-03-18 18:32:12 +01:00
|
|
|
{ "shutdown_after", no_argument, NULL, 'p' },
|
2014-09-24 20:46:17 +02:00
|
|
|
{ "reason", required_argument, NULL, 'r' },
|
2016-04-29 03:06:26 +02:00
|
|
|
{ "security", no_argument, NULL, 'e'},
|
2016-06-25 03:22:02 +02:00
|
|
|
{ "wipe_ab", no_argument, NULL, 0 },
|
2016-06-09 23:09:39 +02:00
|
|
|
{ "wipe_package_size", required_argument, NULL, 0 },
|
2016-12-19 22:04:23 +01:00
|
|
|
{ "prompt_and_wipe_data", no_argument, NULL, 0 },
|
2009-10-06 23:41:38 +02:00
|
|
|
{ NULL, 0, NULL, 0 },
|
2009-03-04 04:28:42 +01:00
|
|
|
};
|
|
|
|
|
2016-07-11 23:04:08 +02:00
|
|
|
// More bootreasons can be found in "system/core/bootstat/bootstat.cpp".
|
|
|
|
static const std::vector<std::string> bootreason_blacklist {
|
|
|
|
"kernel_panic",
|
|
|
|
"Panic",
|
|
|
|
};
|
|
|
|
|
2013-07-09 22:34:55 +02:00
|
|
|
static const char *CACHE_LOG_DIR = "/cache/recovery";
|
2010-09-20 21:16:13 +02:00
|
|
|
static const char *COMMAND_FILE = "/cache/recovery/command";
|
|
|
|
static const char *LOG_FILE = "/cache/recovery/log";
|
2011-10-19 19:51:12 +02:00
|
|
|
static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
|
2012-08-30 00:19:29 +02:00
|
|
|
static const char *LOCALE_FILE = "/cache/recovery/last_locale";
|
2016-02-25 21:42:19 +01:00
|
|
|
static const char *CONVERT_FBE_DIR = "/tmp/convert_fbe";
|
|
|
|
static const char *CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe";
|
2011-06-24 07:14:24 +02:00
|
|
|
static const char *CACHE_ROOT = "/cache";
|
2015-11-05 22:38:40 +01:00
|
|
|
static const char *DATA_ROOT = "/data";
|
2010-09-20 21:16:13 +02:00
|
|
|
static const char *SDCARD_ROOT = "/sdcard";
|
2009-03-04 04:28:42 +01:00
|
|
|
static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
|
2011-10-19 19:51:12 +02:00
|
|
|
static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
|
2015-04-11 04:04:11 +02:00
|
|
|
static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
|
2015-05-05 03:50:27 +02:00
|
|
|
static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
|
2016-06-24 01:52:17 +02:00
|
|
|
// We will try to apply the update package 5 times at most in case of an I/O error.
|
|
|
|
static const int EIO_RETRY_COUNT = 4;
|
2016-02-17 21:21:52 +01:00
|
|
|
static const int BATTERY_READ_TIMEOUT_IN_SEC = 10;
|
|
|
|
// GmsCore enters recovery mode to install package when having enough battery
|
|
|
|
// percentage. Normally, the threshold is 40% without charger and 20% with charger.
|
|
|
|
// So we should check battery with a slightly lower limitation.
|
|
|
|
static const int BATTERY_OK_PERCENTAGE = 20;
|
|
|
|
static const int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
|
2016-11-03 19:37:15 +01:00
|
|
|
static constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe";
|
|
|
|
static constexpr const char* DEFAULT_LOCALE = "en_US";
|
2011-10-29 00:13:10 +02:00
|
|
|
|
2016-11-03 19:37:15 +01:00
|
|
|
static std::string locale;
|
2016-10-27 05:08:08 +02:00
|
|
|
static bool has_cache = false;
|
2016-10-14 00:17:04 +02:00
|
|
|
|
2016-11-03 19:37:15 +01:00
|
|
|
RecoveryUI* ui = nullptr;
|
|
|
|
bool modified_flash = false;
|
|
|
|
const char* stage = nullptr;
|
|
|
|
const char* reason = nullptr;
|
|
|
|
struct selabel_handle* sehandle;
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* The recovery tool communicates with the main system through /cache files.
|
|
|
|
* /cache/recovery/command - INPUT - command line for tool, one arg per line
|
|
|
|
* /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
|
|
|
|
*
|
|
|
|
* The arguments which may be supplied in the recovery.command file:
|
2010-09-20 21:16:13 +02:00
|
|
|
* --update_package=path - verify install an OTA package file
|
2009-03-04 04:28:42 +01:00
|
|
|
* --wipe_data - erase user data (and cache), then reboot
|
2016-12-19 22:04:23 +01:00
|
|
|
* --prompt_and_wipe_data - prompt the user that data is corrupt,
|
|
|
|
* with their consent erase user data (and cache), then reboot
|
2009-03-04 04:28:42 +01:00
|
|
|
* --wipe_cache - wipe cache (but not user data), then reboot
|
2009-11-30 17:40:57 +01:00
|
|
|
* --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
|
2012-04-12 20:01:22 +02:00
|
|
|
* --just_exit - do nothing; exit and reboot
|
2009-03-04 04:28:42 +01:00
|
|
|
*
|
|
|
|
* After completing, we remove /cache/recovery/command and reboot.
|
|
|
|
* Arguments may also be supplied in the bootloader control block (BCB).
|
|
|
|
* These important scenarios must be safely restartable at any point:
|
|
|
|
*
|
|
|
|
* FACTORY RESET
|
|
|
|
* 1. user selects "factory reset"
|
|
|
|
* 2. main system writes "--wipe_data" to /cache/recovery/command
|
|
|
|
* 3. main system reboots into recovery
|
|
|
|
* 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
|
|
|
|
* -- after this, rebooting will restart the erase --
|
2010-09-20 21:16:13 +02:00
|
|
|
* 5. erase_volume() reformats /data
|
|
|
|
* 6. erase_volume() reformats /cache
|
2009-03-04 04:28:42 +01:00
|
|
|
* 7. finish_recovery() erases BCB
|
|
|
|
* -- after this, rebooting will restart the main system --
|
|
|
|
* 8. main() calls reboot() to boot main system
|
|
|
|
*
|
|
|
|
* OTA INSTALL
|
|
|
|
* 1. main system downloads OTA package to /cache/some-filename.zip
|
2010-09-22 21:01:37 +02:00
|
|
|
* 2. main system writes "--update_package=/cache/some-filename.zip"
|
2009-03-04 04:28:42 +01:00
|
|
|
* 3. main system reboots into recovery
|
|
|
|
* 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
|
|
|
|
* -- after this, rebooting will attempt to reinstall the update --
|
|
|
|
* 5. install_package() attempts to install the update
|
|
|
|
* NOTE: the package install must itself be restartable from any point
|
|
|
|
* 6. finish_recovery() erases BCB
|
|
|
|
* -- after this, rebooting will (try to) restart the main system --
|
|
|
|
* 7. ** if install failed **
|
|
|
|
* 7a. prompt_and_wait() shows an error icon and waits for the user
|
2016-12-14 07:29:49 +01:00
|
|
|
* 7b. the user reboots (pulling the battery, etc) into the main system
|
2009-03-04 04:28:42 +01:00
|
|
|
*/
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
// open a given path, mounting partitions as necessary
|
2015-09-11 00:32:24 +02:00
|
|
|
FILE* fopen_path(const char *path, const char *mode) {
|
2010-09-20 21:16:13 +02:00
|
|
|
if (ensure_path_mounted(path) != 0) {
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(ERROR) << "Can't mount " << path;
|
2009-03-04 04:28:42 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When writing, try to create the containing directory, if necessary.
|
|
|
|
// Use generous permissions, the system (init.rc) will reset them.
|
2012-02-09 20:13:23 +01:00
|
|
|
if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
FILE *fp = fopen(path, mode);
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2015-09-11 00:32:24 +02:00
|
|
|
// close a file, log an error if the error indicator is set
|
|
|
|
static void check_and_fclose(FILE *fp, const char *name) {
|
|
|
|
fflush(fp);
|
2016-11-03 19:37:15 +01:00
|
|
|
if (fsync(fileno(fp)) == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to fsync " << name;
|
|
|
|
}
|
2016-08-06 03:00:04 +02:00
|
|
|
if (ferror(fp)) {
|
|
|
|
PLOG(ERROR) << "Error in " << name;
|
|
|
|
}
|
2015-09-11 00:32:24 +02:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2015-02-10 23:46:14 +01:00
|
|
|
bool is_ro_debuggable() {
|
2016-09-24 00:30:55 +02:00
|
|
|
return android::base::GetBoolProperty("ro.debuggable", false);
|
2015-02-10 23:46:14 +01:00
|
|
|
}
|
|
|
|
|
2014-10-23 03:38:48 +02:00
|
|
|
static void redirect_stdio(const char* filename) {
|
2015-09-11 00:32:24 +02:00
|
|
|
int pipefd[2];
|
|
|
|
if (pipe(pipefd) == -1) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "pipe failed";
|
2014-10-23 03:38:48 +02:00
|
|
|
|
2015-09-11 00:32:24 +02:00
|
|
|
// Fall back to traditional logging mode without timestamps.
|
|
|
|
// If these fail, there's not really anywhere to complain...
|
|
|
|
freopen(filename, "a", stdout); setbuf(stdout, NULL);
|
|
|
|
freopen(filename, "a", stderr); setbuf(stderr, NULL);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "fork failed";
|
2015-09-11 00:32:24 +02:00
|
|
|
|
|
|
|
// Fall back to traditional logging mode without timestamps.
|
|
|
|
// If these fail, there's not really anywhere to complain...
|
|
|
|
freopen(filename, "a", stdout); setbuf(stdout, NULL);
|
|
|
|
freopen(filename, "a", stderr); setbuf(stderr, NULL);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
/// Close the unused write end.
|
|
|
|
close(pipefd[1]);
|
|
|
|
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
// Child logger to actually write to the log file.
|
|
|
|
FILE* log_fp = fopen(filename, "a");
|
|
|
|
if (log_fp == nullptr) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "fopen \"" << filename << "\" failed";
|
2015-09-11 00:32:24 +02:00
|
|
|
close(pipefd[0]);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* pipe_fp = fdopen(pipefd[0], "r");
|
|
|
|
if (pipe_fp == nullptr) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "fdopen failed";
|
2015-09-11 00:32:24 +02:00
|
|
|
check_and_fclose(log_fp, filename);
|
|
|
|
close(pipefd[0]);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* line = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
while (getline(&line, &len, pipe_fp) != -1) {
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
double duration = std::chrono::duration_cast<std::chrono::duration<double>>(
|
|
|
|
now - start).count();
|
|
|
|
if (line[0] == '\n') {
|
|
|
|
fprintf(log_fp, "[%12.6lf]\n", duration);
|
|
|
|
} else {
|
|
|
|
fprintf(log_fp, "[%12.6lf] %s", duration, line);
|
|
|
|
}
|
|
|
|
fflush(log_fp);
|
|
|
|
}
|
|
|
|
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "getline failed";
|
2015-09-11 00:32:24 +02:00
|
|
|
|
|
|
|
free(line);
|
|
|
|
check_and_fclose(log_fp, filename);
|
|
|
|
close(pipefd[0]);
|
|
|
|
_exit(1);
|
|
|
|
} else {
|
|
|
|
// Redirect stdout/stderr to the logger process.
|
|
|
|
// Close the unused read end.
|
|
|
|
close(pipefd[0]);
|
|
|
|
|
|
|
|
setbuf(stdout, nullptr);
|
|
|
|
setbuf(stderr, nullptr);
|
|
|
|
|
|
|
|
if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "dup2 stdout failed";
|
2015-09-11 00:32:24 +02:00
|
|
|
}
|
|
|
|
if (dup2(pipefd[1], STDERR_FILENO) == -1) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "dup2 stderr failed";
|
2015-09-11 00:32:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(pipefd[1]);
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// command line args come from, in decreasing precedence:
|
|
|
|
// - the actual command line
|
|
|
|
// - the bootloader control block (one per line, after "recovery")
|
|
|
|
// - the contents of COMMAND_FILE (one per line)
|
2016-12-02 20:32:19 +01:00
|
|
|
static std::vector<std::string> get_args(const int argc, char** const argv) {
|
|
|
|
CHECK_GT(argc, 0);
|
|
|
|
|
|
|
|
bootloader_message boot = {};
|
|
|
|
std::string err;
|
|
|
|
if (!read_bootloader_message(&boot, &err)) {
|
|
|
|
LOG(ERROR) << err;
|
|
|
|
// If fails, leave a zeroed bootloader_message.
|
|
|
|
boot = {};
|
|
|
|
}
|
|
|
|
stage = strndup(boot.stage, sizeof(boot.stage));
|
|
|
|
|
|
|
|
if (boot.command[0] != 0) {
|
|
|
|
std::string boot_command = std::string(boot.command, sizeof(boot.command));
|
|
|
|
LOG(INFO) << "Boot command: " << boot_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boot.status[0] != 0) {
|
|
|
|
std::string boot_status = std::string(boot.status, sizeof(boot.status));
|
|
|
|
LOG(INFO) << "Boot status: " << boot_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> args(argv, argv + argc);
|
|
|
|
|
|
|
|
// --- if arguments weren't supplied, look in the bootloader control block
|
2017-01-09 19:29:59 +01:00
|
|
|
if (args.size() == 1) {
|
2016-12-02 20:32:19 +01:00
|
|
|
boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
|
|
|
|
std::string boot_recovery(boot.recovery);
|
|
|
|
std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n");
|
|
|
|
if (!tokens.empty() && tokens[0] == "recovery") {
|
|
|
|
for (auto it = tokens.begin() + 1; it != tokens.end(); it++) {
|
|
|
|
// Skip empty and '\0'-filled tokens.
|
|
|
|
if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Got " << args.size() << " arguments from boot message";
|
|
|
|
} else if (boot.recovery[0] != 0) {
|
|
|
|
LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- if that doesn't work, try the command file (if we have /cache).
|
2017-01-09 19:29:59 +01:00
|
|
|
if (args.size() == 1 && has_cache) {
|
2016-12-02 20:32:19 +01:00
|
|
|
std::string content;
|
2016-12-09 03:10:48 +01:00
|
|
|
if (ensure_path_mounted(COMMAND_FILE) == 0 &&
|
|
|
|
android::base::ReadFileToString(COMMAND_FILE, &content)) {
|
2016-12-02 20:32:19 +01:00
|
|
|
std::vector<std::string> tokens = android::base::Split(content, "\n");
|
2016-12-09 03:10:48 +01:00
|
|
|
// All the arguments in COMMAND_FILE are needed (unlike the BCB message,
|
|
|
|
// COMMAND_FILE doesn't use filename as the first argument).
|
|
|
|
for (auto it = tokens.begin(); it != tokens.end(); it++) {
|
2016-12-02 20:32:19 +01:00
|
|
|
// Skip empty and '\0'-filled tokens.
|
|
|
|
if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the arguments (excluding the filename in args[0]) back into the
|
|
|
|
// bootloader control block. So the device will always boot into recovery to
|
|
|
|
// finish the pending work, until finish_recovery() is called.
|
|
|
|
std::vector<std::string> options(args.cbegin() + 1, args.cend());
|
2016-12-14 06:53:31 +01:00
|
|
|
if (!update_bootloader_message(options, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to set BCB message: " << err;
|
2016-12-02 20:32:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 06:53:31 +01:00
|
|
|
// Set the BCB to reboot back into recovery (it won't resume the install from
|
|
|
|
// sdcard though).
|
|
|
|
static void set_sdcard_update_bootloader_message() {
|
|
|
|
std::vector<std::string> options;
|
|
|
|
std::string err;
|
|
|
|
if (!update_bootloader_message(options, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to set BCB message: " << err;
|
|
|
|
}
|
2009-08-18 21:05:45 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2015-05-05 03:50:27 +02:00
|
|
|
// Read from kernel log into buffer and write out to file.
|
|
|
|
static void save_kernel_log(const char* destination) {
|
|
|
|
int klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
|
2014-12-09 20:26:40 +01:00
|
|
|
if (klog_buf_len <= 0) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "Error getting klog size";
|
2014-12-09 20:26:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:50:27 +02:00
|
|
|
std::string buffer(klog_buf_len, 0);
|
|
|
|
int n = klogctl(KLOG_READ_ALL, &buffer[0], klog_buf_len);
|
|
|
|
if (n == -1) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "Error in reading klog";
|
2014-12-09 20:26:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-05-05 03:50:27 +02:00
|
|
|
buffer.resize(n);
|
|
|
|
android::base::WriteStringToFile(buffer, destination);
|
2014-12-09 20:26:40 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:58:16 +01:00
|
|
|
// write content to the current pmsg session.
|
|
|
|
static ssize_t __pmsg_write(const char *filename, const char *buf, size_t len) {
|
|
|
|
return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
|
|
|
|
filename, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_log_file_to_pmsg(const char* source, const char* destination) {
|
|
|
|
std::string content;
|
|
|
|
android::base::ReadFileToString(source, &content);
|
|
|
|
__pmsg_write(destination, content.c_str(), content.length());
|
|
|
|
}
|
|
|
|
|
2015-04-11 04:04:11 +02:00
|
|
|
// How much of the temp log we have copied to the copy in cache.
|
2016-04-18 20:30:55 +02:00
|
|
|
static off_t tmplog_offset = 0;
|
2015-04-11 04:04:11 +02:00
|
|
|
|
2015-05-05 03:50:27 +02:00
|
|
|
static void copy_log_file(const char* source, const char* destination, bool append) {
|
|
|
|
FILE* dest_fp = fopen_path(destination, append ? "a" : "w");
|
|
|
|
if (dest_fp == nullptr) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "Can't open " << destination;
|
2010-09-29 22:21:30 +02:00
|
|
|
} else {
|
2015-05-05 03:50:27 +02:00
|
|
|
FILE* source_fp = fopen(source, "r");
|
|
|
|
if (source_fp != nullptr) {
|
2015-04-11 04:04:11 +02:00
|
|
|
if (append) {
|
2016-04-18 20:30:55 +02:00
|
|
|
fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write
|
2010-09-29 22:21:30 +02:00
|
|
|
}
|
|
|
|
char buf[4096];
|
2015-05-05 03:50:27 +02:00
|
|
|
size_t bytes;
|
|
|
|
while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
|
|
|
|
fwrite(buf, 1, bytes, dest_fp);
|
|
|
|
}
|
2015-04-11 04:04:11 +02:00
|
|
|
if (append) {
|
2016-04-18 20:30:55 +02:00
|
|
|
tmplog_offset = ftello(source_fp);
|
2010-09-29 22:21:30 +02:00
|
|
|
}
|
2015-05-05 03:50:27 +02:00
|
|
|
check_and_fclose(source_fp, source);
|
2010-09-29 22:21:30 +02:00
|
|
|
}
|
2015-05-05 03:50:27 +02:00
|
|
|
check_and_fclose(dest_fp, destination);
|
2010-09-29 22:21:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:16:35 +02:00
|
|
|
static void copy_logs() {
|
|
|
|
// We only rotate and record the log of the current session if there are
|
|
|
|
// actual attempts to modify the flash, such as wipes, installs from BCB
|
|
|
|
// or menu selections. This is to avoid unnecessary rotation (and
|
|
|
|
// possible deletion) of log files, if it does not do anything loggable.
|
|
|
|
if (!modified_flash) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:58:16 +01:00
|
|
|
// Always write to pmsg, this allows the OTA logs to be caught in logcat -L
|
|
|
|
copy_log_file_to_pmsg(TEMPORARY_LOG_FILE, LAST_LOG_FILE);
|
|
|
|
copy_log_file_to_pmsg(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE);
|
|
|
|
|
|
|
|
// We can do nothing for now if there's no /cache partition.
|
|
|
|
if (!has_cache) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-22 02:46:13 +02:00
|
|
|
ensure_path_mounted(LAST_LOG_FILE);
|
|
|
|
ensure_path_mounted(LAST_KMSG_FILE);
|
|
|
|
rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
|
2015-04-08 02:16:35 +02:00
|
|
|
|
2013-07-02 20:43:25 +02:00
|
|
|
// Copy logs to cache so the system can find out what happened.
|
2015-04-11 04:04:11 +02:00
|
|
|
copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
|
|
|
|
copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
|
|
|
|
copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
|
|
|
|
save_kernel_log(LAST_KMSG_FILE);
|
2013-07-02 20:43:25 +02:00
|
|
|
chmod(LOG_FILE, 0600);
|
|
|
|
chown(LOG_FILE, 1000, 1000); // system user
|
2015-04-11 04:04:11 +02:00
|
|
|
chmod(LAST_KMSG_FILE, 0600);
|
|
|
|
chown(LAST_KMSG_FILE, 1000, 1000); // system user
|
2013-07-02 20:43:25 +02:00
|
|
|
chmod(LAST_LOG_FILE, 0640);
|
|
|
|
chmod(LAST_INSTALL_FILE, 0644);
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
// clear the recovery command and prepare to boot a (hopefully working) system,
|
2016-03-24 19:50:34 +01:00
|
|
|
// copy our log file to cache as well (for the system to read). This function is
|
|
|
|
// idempotent: call it as many times as you like.
|
2016-11-03 19:37:15 +01:00
|
|
|
static void finish_recovery() {
|
2012-08-23 22:16:12 +02:00
|
|
|
// Save the locale to cache, so if recovery is next started up
|
|
|
|
// without a --locale argument (eg, directly from the bootloader)
|
|
|
|
// it will use the last-known locale.
|
2016-11-03 19:37:15 +01:00
|
|
|
if (!locale.empty() && has_cache) {
|
|
|
|
LOG(INFO) << "Saving locale \"" << locale << "\"";
|
|
|
|
|
|
|
|
FILE* fp = fopen_path(LOCALE_FILE, "w");
|
|
|
|
if (!android::base::WriteStringToFd(locale, fileno(fp))) {
|
|
|
|
PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE;
|
2016-03-09 23:58:16 +01:00
|
|
|
}
|
2016-11-03 19:37:15 +01:00
|
|
|
check_and_fclose(fp, LOCALE_FILE);
|
2012-08-23 22:16:12 +02:00
|
|
|
}
|
|
|
|
|
2013-07-02 20:43:25 +02:00
|
|
|
copy_logs();
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2012-04-12 20:01:22 +02:00
|
|
|
// Reset to normal system boot so recovery won't cycle indefinitely.
|
2016-06-25 03:22:02 +02:00
|
|
|
std::string err;
|
2016-09-29 01:32:10 +02:00
|
|
|
if (!clear_bootloader_message(&err)) {
|
2016-12-14 06:53:31 +01:00
|
|
|
LOG(ERROR) << "Failed to clear BCB message: " << err;
|
2016-06-25 03:22:02 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
// Remove the command file, so recovery won't repeat indefinitely.
|
2016-02-25 21:29:40 +01:00
|
|
|
if (has_cache) {
|
|
|
|
if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) {
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(WARNING) << "Can't unlink " << COMMAND_FILE;
|
2016-02-25 21:29:40 +01:00
|
|
|
}
|
|
|
|
ensure_path_unmounted(CACHE_ROOT);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sync(); // For good measure.
|
|
|
|
}
|
|
|
|
|
2016-12-13 20:14:37 +01:00
|
|
|
struct saved_log_file {
|
|
|
|
std::string name;
|
|
|
|
struct stat sb;
|
|
|
|
std::string data;
|
|
|
|
};
|
2013-07-09 22:34:55 +02:00
|
|
|
|
2015-06-06 02:59:56 +02:00
|
|
|
static bool erase_volume(const char* volume) {
|
2016-12-13 20:14:37 +01:00
|
|
|
bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
|
|
|
|
bool is_data = (strcmp(volume, DATA_ROOT) == 0);
|
|
|
|
|
|
|
|
ui->SetBackground(RecoveryUI::ERASING);
|
|
|
|
ui->SetProgressType(RecoveryUI::INDETERMINATE);
|
|
|
|
|
|
|
|
std::vector<saved_log_file> log_files;
|
|
|
|
|
|
|
|
if (is_cache) {
|
|
|
|
// If we're reformatting /cache, we load any past logs
|
|
|
|
// (i.e. "/cache/recovery/last_*") and the current log
|
|
|
|
// ("/cache/recovery/log") into memory, so we can restore them after
|
|
|
|
// the reformat.
|
|
|
|
|
|
|
|
ensure_path_mounted(volume);
|
|
|
|
|
|
|
|
struct dirent* de;
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> d(opendir(CACHE_LOG_DIR), closedir);
|
|
|
|
if (d) {
|
|
|
|
while ((de = readdir(d.get())) != nullptr) {
|
|
|
|
if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) {
|
|
|
|
std::string path = android::base::StringPrintf("%s/%s", CACHE_LOG_DIR, de->d_name);
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(path.c_str(), &sb) == 0) {
|
|
|
|
// truncate files to 512kb
|
|
|
|
if (sb.st_size > (1 << 19)) {
|
|
|
|
sb.st_size = 1 << 19;
|
2013-07-09 22:34:55 +02:00
|
|
|
}
|
2016-12-13 20:14:37 +01:00
|
|
|
|
|
|
|
std::string data(sb.st_size, '\0');
|
|
|
|
FILE* f = fopen(path.c_str(), "rb");
|
|
|
|
fread(&data[0], 1, data.size(), f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
log_files.emplace_back(saved_log_file{ path, sb, data });
|
|
|
|
}
|
2013-07-09 22:34:55 +02:00
|
|
|
}
|
2016-12-13 20:14:37 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR;
|
|
|
|
}
|
2013-07-09 22:34:55 +02:00
|
|
|
}
|
2016-12-13 20:14:37 +01:00
|
|
|
}
|
2013-07-09 22:34:55 +02:00
|
|
|
|
2016-12-13 20:14:37 +01:00
|
|
|
ui->Print("Formatting %s...\n", volume);
|
2010-09-29 22:21:30 +02:00
|
|
|
|
2016-12-13 20:14:37 +01:00
|
|
|
ensure_path_unmounted(volume);
|
2015-11-05 22:38:40 +01:00
|
|
|
|
2016-12-13 20:14:37 +01:00
|
|
|
int result;
|
2015-11-05 22:38:40 +01:00
|
|
|
|
2016-12-13 20:14:37 +01:00
|
|
|
if (is_data && reason && strcmp(reason, "convert_fbe") == 0) {
|
|
|
|
// Create convert_fbe breadcrumb file to signal to init
|
|
|
|
// to convert to file based encryption, not full disk encryption
|
|
|
|
if (mkdir(CONVERT_FBE_DIR, 0700) != 0) {
|
|
|
|
ui->Print("Failed to make convert_fbe dir %s\n", strerror(errno));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
FILE* f = fopen(CONVERT_FBE_FILE, "wb");
|
|
|
|
if (!f) {
|
|
|
|
ui->Print("Failed to convert to file encryption %s\n", strerror(errno));
|
|
|
|
return true;
|
2015-11-05 22:38:40 +01:00
|
|
|
}
|
2016-12-13 20:14:37 +01:00
|
|
|
fclose(f);
|
|
|
|
result = format_volume(volume, CONVERT_FBE_DIR);
|
|
|
|
remove(CONVERT_FBE_FILE);
|
|
|
|
rmdir(CONVERT_FBE_DIR);
|
|
|
|
} else {
|
|
|
|
result = format_volume(volume);
|
|
|
|
}
|
2013-07-09 22:34:55 +02:00
|
|
|
|
2016-12-13 20:14:37 +01:00
|
|
|
if (is_cache) {
|
|
|
|
// Re-create the log dir and write back the log entries.
|
|
|
|
if (ensure_path_mounted(CACHE_LOG_DIR) == 0 &&
|
|
|
|
dirCreateHierarchy(CACHE_LOG_DIR, 0777, nullptr, false, sehandle) == 0) {
|
|
|
|
for (const auto& log : log_files) {
|
|
|
|
if (!android::base::WriteStringToFile(log.data, log.name, log.sb.st_mode, log.sb.st_uid,
|
|
|
|
log.sb.st_gid)) {
|
|
|
|
PLOG(ERROR) << "Failed to write to " << log.name;
|
2013-07-09 22:34:55 +02:00
|
|
|
}
|
2016-12-13 20:14:37 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PLOG(ERROR) << "Failed to mount / create " << CACHE_LOG_DIR;
|
2010-09-29 22:21:30 +02:00
|
|
|
}
|
|
|
|
|
2016-12-13 20:14:37 +01:00
|
|
|
// Any part of the log we'd copied to cache is now gone.
|
|
|
|
// Reset the pointer so we copy from the beginning of the temp
|
|
|
|
// log.
|
|
|
|
tmplog_offset = 0;
|
|
|
|
copy_logs();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result == 0);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2009-09-23 00:16:02 +02:00
|
|
|
static int
|
2011-10-28 19:33:05 +02:00
|
|
|
get_menu_selection(const char* const * headers, const char* const * items,
|
2011-10-31 17:34:15 +01:00
|
|
|
int menu_only, int initial_selection, Device* device) {
|
2009-09-23 00:16:02 +02:00
|
|
|
// throw away keys pressed previously, so user doesn't
|
|
|
|
// accidentally trigger menu items.
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->FlushKeys();
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->StartMenu(headers, items, initial_selection);
|
2010-09-15 20:08:23 +02:00
|
|
|
int selected = initial_selection;
|
2009-03-04 04:28:42 +01:00
|
|
|
int chosen_item = -1;
|
|
|
|
|
2009-09-23 00:16:02 +02:00
|
|
|
while (chosen_item < 0) {
|
2011-10-29 00:13:10 +02:00
|
|
|
int key = ui->WaitKey();
|
|
|
|
int visible = ui->IsTextVisible();
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2011-01-25 22:15:30 +01:00
|
|
|
if (key == -1) { // ui_wait_key() timed out
|
2011-10-29 00:13:10 +02:00
|
|
|
if (ui->WasTextEverVisible()) {
|
2011-01-25 22:15:30 +01:00
|
|
|
continue;
|
|
|
|
} else {
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(INFO) << "timed out waiting for key input; rebooting.";
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->EndMenu();
|
2011-10-31 17:34:15 +01:00
|
|
|
return 0; // XXX fixme
|
2011-01-25 22:15:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
int action = device->HandleMenuKey(key, visible);
|
2009-06-09 21:22:33 +02:00
|
|
|
|
|
|
|
if (action < 0) {
|
|
|
|
switch (action) {
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::kHighlightUp:
|
2015-04-10 21:47:46 +02:00
|
|
|
selected = ui->SelectMenu(--selected);
|
2009-06-09 21:22:33 +02:00
|
|
|
break;
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::kHighlightDown:
|
2015-04-10 21:47:46 +02:00
|
|
|
selected = ui->SelectMenu(++selected);
|
2009-06-09 21:22:33 +02:00
|
|
|
break;
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::kInvokeItem:
|
2009-06-09 21:22:33 +02:00
|
|
|
chosen_item = selected;
|
|
|
|
break;
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::kNoAction:
|
2009-06-09 21:22:33 +02:00
|
|
|
break;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2009-09-23 00:16:02 +02:00
|
|
|
} else if (!menu_only) {
|
2009-06-09 21:22:33 +02:00
|
|
|
chosen_item = action;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2009-09-23 00:16:02 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->EndMenu();
|
2009-09-23 00:16:02 +02:00
|
|
|
return chosen_item;
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2010-09-15 20:08:23 +02:00
|
|
|
static int compare_string(const void* a, const void* b) {
|
|
|
|
return strcmp(*(const char**)a, *(const char**)b);
|
|
|
|
}
|
|
|
|
|
2014-07-08 23:10:23 +02:00
|
|
|
// Returns a malloc'd path, or NULL.
|
2015-03-25 23:16:51 +01:00
|
|
|
static char* browse_directory(const char* path, Device* device) {
|
2011-06-24 07:14:24 +02:00
|
|
|
ensure_path_mounted(path);
|
2010-09-22 01:49:26 +02:00
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
DIR* d = opendir(path);
|
2010-09-15 20:08:23 +02:00
|
|
|
if (d == NULL) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "error opening " << path;
|
2014-07-08 23:10:23 +02:00
|
|
|
return NULL;
|
2010-09-15 20:08:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int d_size = 0;
|
|
|
|
int d_alloc = 10;
|
2011-10-28 19:33:05 +02:00
|
|
|
char** dirs = (char**)malloc(d_alloc * sizeof(char*));
|
2010-09-15 20:08:23 +02:00
|
|
|
int z_size = 1;
|
|
|
|
int z_alloc = 10;
|
2011-10-28 19:33:05 +02:00
|
|
|
char** zips = (char**)malloc(z_alloc * sizeof(char*));
|
2010-09-15 20:08:23 +02:00
|
|
|
zips[0] = strdup("../");
|
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
struct dirent* de;
|
2010-09-15 20:08:23 +02:00
|
|
|
while ((de = readdir(d)) != NULL) {
|
|
|
|
int name_len = strlen(de->d_name);
|
|
|
|
|
|
|
|
if (de->d_type == DT_DIR) {
|
|
|
|
// skip "." and ".." entries
|
|
|
|
if (name_len == 1 && de->d_name[0] == '.') continue;
|
|
|
|
if (name_len == 2 && de->d_name[0] == '.' &&
|
|
|
|
de->d_name[1] == '.') continue;
|
|
|
|
|
|
|
|
if (d_size >= d_alloc) {
|
|
|
|
d_alloc *= 2;
|
2011-10-28 19:33:05 +02:00
|
|
|
dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
|
2010-09-15 20:08:23 +02:00
|
|
|
}
|
2011-10-28 19:33:05 +02:00
|
|
|
dirs[d_size] = (char*)malloc(name_len + 2);
|
2010-09-15 20:08:23 +02:00
|
|
|
strcpy(dirs[d_size], de->d_name);
|
|
|
|
dirs[d_size][name_len] = '/';
|
|
|
|
dirs[d_size][name_len+1] = '\0';
|
|
|
|
++d_size;
|
|
|
|
} else if (de->d_type == DT_REG &&
|
|
|
|
name_len >= 4 &&
|
|
|
|
strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
|
|
|
|
if (z_size >= z_alloc) {
|
|
|
|
z_alloc *= 2;
|
2011-10-28 19:33:05 +02:00
|
|
|
zips = (char**)realloc(zips, z_alloc * sizeof(char*));
|
2010-09-15 20:08:23 +02:00
|
|
|
}
|
|
|
|
zips[z_size++] = strdup(de->d_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
qsort(dirs, d_size, sizeof(char*), compare_string);
|
|
|
|
qsort(zips, z_size, sizeof(char*), compare_string);
|
|
|
|
|
|
|
|
// append dirs to the zips list
|
|
|
|
if (d_size + z_size + 1 > z_alloc) {
|
|
|
|
z_alloc = d_size + z_size + 1;
|
2011-10-28 19:33:05 +02:00
|
|
|
zips = (char**)realloc(zips, z_alloc * sizeof(char*));
|
2010-09-15 20:08:23 +02:00
|
|
|
}
|
|
|
|
memcpy(zips + z_size, dirs, d_size * sizeof(char*));
|
|
|
|
free(dirs);
|
|
|
|
z_size += d_size;
|
|
|
|
zips[z_size] = NULL;
|
|
|
|
|
2015-04-13 23:36:02 +02:00
|
|
|
const char* headers[] = { "Choose a package to install:", path, NULL };
|
2015-03-25 23:16:51 +01:00
|
|
|
|
2014-07-08 23:10:23 +02:00
|
|
|
char* result;
|
2010-09-15 20:08:23 +02:00
|
|
|
int chosen_item = 0;
|
2014-07-08 23:10:23 +02:00
|
|
|
while (true) {
|
2011-10-31 17:34:15 +01:00
|
|
|
chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
|
2010-09-15 20:08:23 +02:00
|
|
|
|
|
|
|
char* item = zips[chosen_item];
|
|
|
|
int item_len = strlen(item);
|
|
|
|
if (chosen_item == 0) { // item 0 is always "../"
|
2011-06-24 07:14:24 +02:00
|
|
|
// go up but continue browsing (if the caller is update_directory)
|
2014-07-08 23:10:23 +02:00
|
|
|
result = NULL;
|
2010-09-15 20:08:23 +02:00
|
|
|
break;
|
2014-07-08 23:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char new_path[PATH_MAX];
|
|
|
|
strlcpy(new_path, path, PATH_MAX);
|
|
|
|
strlcat(new_path, "/", PATH_MAX);
|
|
|
|
strlcat(new_path, item, PATH_MAX);
|
|
|
|
|
|
|
|
if (item[item_len-1] == '/') {
|
2010-09-15 20:08:23 +02:00
|
|
|
// recurse down into a subdirectory
|
2010-09-20 21:16:13 +02:00
|
|
|
new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
|
2014-07-08 23:10:23 +02:00
|
|
|
result = browse_directory(new_path, device);
|
|
|
|
if (result) break;
|
2010-09-15 20:08:23 +02:00
|
|
|
} else {
|
2014-07-08 23:10:23 +02:00
|
|
|
// selected a zip file: return the malloc'd path to the caller.
|
|
|
|
result = strdup(new_path);
|
2010-09-15 20:08:23 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-07-08 23:10:23 +02:00
|
|
|
}
|
2010-09-15 20:08:23 +02:00
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
for (int i = 0; i < z_size; ++i) free(zips[i]);
|
2010-09-15 20:08:23 +02:00
|
|
|
free(zips);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
static bool yes_no(Device* device, const char* question1, const char* question2) {
|
2015-04-13 23:36:02 +02:00
|
|
|
const char* headers[] = { question1, question2, NULL };
|
2015-03-25 23:16:51 +01:00
|
|
|
const char* items[] = { " No", " Yes", NULL };
|
2009-04-01 22:57:40 +02:00
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
int chosen_item = get_menu_selection(headers, items, 1, 0, device);
|
|
|
|
return (chosen_item == 1);
|
|
|
|
}
|
|
|
|
|
2016-12-19 22:04:23 +01:00
|
|
|
static bool ask_to_wipe_data(Device* device) {
|
|
|
|
return yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!");
|
|
|
|
}
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2016-12-19 22:04:23 +01:00
|
|
|
// Return true on success.
|
|
|
|
static bool wipe_data(Device* device) {
|
2015-04-08 02:16:35 +02:00
|
|
|
modified_flash = true;
|
|
|
|
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("\n-- Wiping data...\n");
|
2015-06-06 02:59:56 +02:00
|
|
|
bool success =
|
|
|
|
device->PreWipeData() &&
|
|
|
|
erase_volume("/data") &&
|
2016-02-25 21:29:40 +01:00
|
|
|
(has_cache ? erase_volume("/cache") : true) &&
|
2015-06-06 02:59:56 +02:00
|
|
|
device->PostWipeData();
|
|
|
|
ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
|
|
|
|
return success;
|
2009-09-23 00:16:02 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 22:04:23 +01:00
|
|
|
static bool prompt_and_wipe_data(Device* device) {
|
|
|
|
const char* const headers[] = {
|
|
|
|
"Boot halted, user data is corrupt",
|
|
|
|
"Wipe all user data to recover",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
const char* const items[] = {
|
|
|
|
"Retry boot",
|
|
|
|
"Wipe user data",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
for (;;) {
|
|
|
|
int chosen_item = get_menu_selection(headers, items, 1, 0, device);
|
|
|
|
if (chosen_item != 1) {
|
|
|
|
return true; // Just reboot, no wipe; not a failure, user asked for it
|
|
|
|
}
|
|
|
|
if (ask_to_wipe_data(device)) {
|
|
|
|
return wipe_data(device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:19:05 +02:00
|
|
|
// Return true on success.
|
|
|
|
static bool wipe_cache(bool should_confirm, Device* device) {
|
2016-02-25 21:29:40 +01:00
|
|
|
if (!has_cache) {
|
|
|
|
ui->Print("No /cache partition found.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) {
|
2015-03-31 21:19:05 +02:00
|
|
|
return false;
|
2015-03-25 23:16:51 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 02:16:35 +02:00
|
|
|
modified_flash = true;
|
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
ui->Print("\n-- Wiping cache...\n");
|
2015-06-06 02:59:56 +02:00
|
|
|
bool success = erase_volume("/cache");
|
|
|
|
ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
|
|
|
|
return success;
|
2015-03-25 23:16:51 +01:00
|
|
|
}
|
|
|
|
|
2016-06-02 20:16:50 +02:00
|
|
|
// Secure-wipe a given partition. It uses BLKSECDISCARD, if supported.
|
|
|
|
// Otherwise, it goes with BLKDISCARD (if device supports BLKDISCARDZEROES) or
|
|
|
|
// BLKZEROOUT.
|
|
|
|
static bool secure_wipe_partition(const std::string& partition) {
|
|
|
|
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY)));
|
|
|
|
if (fd == -1) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "failed to open \"" << partition << "\"";
|
2016-06-02 20:16:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t range[2] = {0, 0};
|
|
|
|
if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) {
|
2016-08-06 03:00:04 +02:00
|
|
|
PLOG(ERROR) << "failed to get partition size";
|
2016-06-02 20:16:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
printf("Secure-wiping \"%s\" from %" PRIu64 " to %" PRIu64 ".\n",
|
|
|
|
partition.c_str(), range[0], range[1]);
|
|
|
|
|
|
|
|
printf("Trying BLKSECDISCARD...\t");
|
|
|
|
if (ioctl(fd, BLKSECDISCARD, &range) == -1) {
|
|
|
|
printf("failed: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
// Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT.
|
|
|
|
unsigned int zeroes;
|
|
|
|
if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) {
|
|
|
|
printf("Trying BLKDISCARD...\t");
|
|
|
|
if (ioctl(fd, BLKDISCARD, &range) == -1) {
|
|
|
|
printf("failed: %s\n", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("Trying BLKZEROOUT...\t");
|
|
|
|
if (ioctl(fd, BLKZEROOUT, &range) == -1) {
|
|
|
|
printf("failed: %s\n", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("done\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-09 23:09:39 +02:00
|
|
|
// Check if the wipe package matches expectation:
|
|
|
|
// 1. verify the package.
|
|
|
|
// 2. check metadata (ota-type, pre-device and serial number if having one).
|
|
|
|
static bool check_wipe_package(size_t wipe_package_size) {
|
|
|
|
if (wipe_package_size == 0) {
|
|
|
|
LOG(ERROR) << "wipe_package_size is zero";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string wipe_package;
|
|
|
|
std::string err_str;
|
|
|
|
if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) {
|
|
|
|
PLOG(ERROR) << "Failed to read wipe package";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()),
|
|
|
|
wipe_package.size())) {
|
|
|
|
LOG(ERROR) << "Failed to verify package";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract metadata
|
|
|
|
ZipArchiveHandle zip;
|
|
|
|
int err = OpenArchiveFromMemory(reinterpret_cast<void*>(&wipe_package[0]),
|
|
|
|
wipe_package.size(), "wipe_package", &zip);
|
|
|
|
if (err != 0) {
|
|
|
|
LOG(ERROR) << "Can't open wipe package : " << ErrorCodeString(err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string metadata;
|
|
|
|
if (!read_metadata_from_package(&zip, &metadata)) {
|
|
|
|
CloseArchive(zip);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CloseArchive(zip);
|
|
|
|
|
|
|
|
// Check metadata
|
|
|
|
std::vector<std::string> lines = android::base::Split(metadata, "\n");
|
|
|
|
bool ota_type_matched = false;
|
|
|
|
bool device_type_matched = false;
|
|
|
|
bool has_serial_number = false;
|
|
|
|
bool serial_number_matched = false;
|
|
|
|
for (const auto& line : lines) {
|
|
|
|
if (line == "ota-type=BRICK") {
|
|
|
|
ota_type_matched = true;
|
|
|
|
} else if (android::base::StartsWith(line, "pre-device=")) {
|
|
|
|
std::string device_type = line.substr(strlen("pre-device="));
|
|
|
|
char real_device_type[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("ro.build.product", real_device_type, "");
|
|
|
|
device_type_matched = (device_type == real_device_type);
|
|
|
|
} else if (android::base::StartsWith(line, "serialno=")) {
|
|
|
|
std::string serial_no = line.substr(strlen("serialno="));
|
|
|
|
char real_serial_no[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("ro.serialno", real_serial_no, "");
|
|
|
|
has_serial_number = true;
|
|
|
|
serial_number_matched = (serial_no == real_serial_no);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ota_type_matched && device_type_matched && (!has_serial_number || serial_number_matched);
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:22:02 +02:00
|
|
|
// Wipe the current A/B device, with a secure wipe of all the partitions in
|
|
|
|
// RECOVERY_WIPE.
|
2016-06-09 23:09:39 +02:00
|
|
|
static bool wipe_ab_device(size_t wipe_package_size) {
|
2016-06-02 20:16:50 +02:00
|
|
|
ui->SetBackground(RecoveryUI::ERASING);
|
|
|
|
ui->SetProgressType(RecoveryUI::INDETERMINATE);
|
|
|
|
|
2016-06-09 23:09:39 +02:00
|
|
|
if (!check_wipe_package(wipe_package_size)) {
|
|
|
|
LOG(ERROR) << "Failed to verify wipe package";
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-02 20:16:50 +02:00
|
|
|
std::string partition_list;
|
2016-06-25 03:22:02 +02:00
|
|
|
if (!android::base::ReadFileToString(RECOVERY_WIPE, &partition_list)) {
|
|
|
|
LOG(ERROR) << "failed to read \"" << RECOVERY_WIPE << "\"";
|
2016-06-02 20:16:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> lines = android::base::Split(partition_list, "\n");
|
|
|
|
for (const std::string& line : lines) {
|
|
|
|
std::string partition = android::base::Trim(line);
|
|
|
|
// Ignore '#' comment or empty lines.
|
|
|
|
if (android::base::StartsWith(partition, "#") || partition.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proceed anyway even if it fails to wipe some partition.
|
|
|
|
secure_wipe_partition(partition);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-23 03:38:48 +02:00
|
|
|
static void choose_recovery_file(Device* device) {
|
2015-05-06 21:40:05 +02:00
|
|
|
// "Back" + KEEP_LOG_COUNT * 2 + terminating nullptr entry
|
|
|
|
char* entries[1 + KEEP_LOG_COUNT * 2 + 1];
|
2014-10-23 03:38:48 +02:00
|
|
|
memset(entries, 0, sizeof(entries));
|
|
|
|
|
2015-05-05 03:50:27 +02:00
|
|
|
unsigned int n = 0;
|
2014-12-09 20:26:40 +01:00
|
|
|
|
2016-08-17 21:02:46 +02:00
|
|
|
if (has_cache) {
|
|
|
|
// Add LAST_LOG_FILE + LAST_LOG_FILE.x
|
|
|
|
// Add LAST_KMSG_FILE + LAST_KMSG_FILE.x
|
|
|
|
for (int i = 0; i < KEEP_LOG_COUNT; i++) {
|
|
|
|
char* log_file;
|
|
|
|
int ret;
|
|
|
|
ret = (i == 0) ? asprintf(&log_file, "%s", LAST_LOG_FILE) :
|
|
|
|
asprintf(&log_file, "%s.%d", LAST_LOG_FILE, i);
|
|
|
|
if (ret == -1) {
|
|
|
|
// memory allocation failure - return early. Should never happen.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((ensure_path_mounted(log_file) != 0) || (access(log_file, R_OK) == -1)) {
|
|
|
|
free(log_file);
|
|
|
|
} else {
|
|
|
|
entries[n++] = log_file;
|
|
|
|
}
|
2015-05-05 03:50:27 +02:00
|
|
|
|
2016-08-17 21:02:46 +02:00
|
|
|
char* kmsg_file;
|
|
|
|
ret = (i == 0) ? asprintf(&kmsg_file, "%s", LAST_KMSG_FILE) :
|
|
|
|
asprintf(&kmsg_file, "%s.%d", LAST_KMSG_FILE, i);
|
|
|
|
if (ret == -1) {
|
|
|
|
// memory allocation failure - return early. Should never happen.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((ensure_path_mounted(kmsg_file) != 0) || (access(kmsg_file, R_OK) == -1)) {
|
|
|
|
free(kmsg_file);
|
|
|
|
} else {
|
|
|
|
entries[n++] = kmsg_file;
|
|
|
|
}
|
2015-05-05 03:50:27 +02:00
|
|
|
}
|
2016-08-17 21:02:46 +02:00
|
|
|
} else {
|
|
|
|
// If cache partition is not found, view /tmp/recovery.log instead.
|
|
|
|
ui->Print("No /cache partition found.\n");
|
|
|
|
if (access(TEMPORARY_LOG_FILE, R_OK) == -1) {
|
|
|
|
return;
|
|
|
|
} else{
|
|
|
|
entries[n++] = strdup(TEMPORARY_LOG_FILE);
|
2014-10-23 03:38:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 21:40:05 +02:00
|
|
|
entries[n++] = strdup("Back");
|
|
|
|
|
2015-05-05 03:50:27 +02:00
|
|
|
const char* headers[] = { "Select file to view", nullptr };
|
2014-10-23 03:38:48 +02:00
|
|
|
|
2015-04-09 05:06:50 +02:00
|
|
|
while (true) {
|
2015-03-25 23:16:51 +01:00
|
|
|
int chosen_item = get_menu_selection(headers, entries, 1, 0, device);
|
2015-05-06 21:40:05 +02:00
|
|
|
if (strcmp(entries[chosen_item], "Back") == 0) break;
|
2015-04-09 05:06:50 +02:00
|
|
|
|
|
|
|
ui->ShowFile(entries[chosen_item]);
|
2014-10-23 03:38:48 +02:00
|
|
|
}
|
|
|
|
|
2015-05-05 03:50:27 +02:00
|
|
|
for (size_t i = 0; i < (sizeof(entries) / sizeof(*entries)); i++) {
|
2014-10-23 03:38:48 +02:00
|
|
|
free(entries[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 01:49:04 +02:00
|
|
|
static void run_graphics_test(Device* device) {
|
|
|
|
// Switch to graphics screen.
|
|
|
|
ui->ShowText(false);
|
|
|
|
|
|
|
|
ui->SetProgressType(RecoveryUI::INDETERMINATE);
|
|
|
|
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
ui->SetBackground(RecoveryUI::NO_COMMAND);
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
ui->SetBackground(RecoveryUI::ERASING);
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
|
|
|
|
|
|
|
|
ui->SetProgressType(RecoveryUI::DETERMINATE);
|
|
|
|
ui->ShowProgress(1.0, 10.0);
|
|
|
|
float fraction = 0.0;
|
|
|
|
for (size_t i = 0; i < 100; ++i) {
|
|
|
|
fraction += .01;
|
|
|
|
ui->SetProgress(fraction);
|
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->ShowText(true);
|
|
|
|
}
|
|
|
|
|
2016-01-14 00:05:20 +01:00
|
|
|
// How long (in seconds) we wait for the fuse-provided package file to
|
|
|
|
// appear, before timing out.
|
|
|
|
#define SDCARD_INSTALL_TIMEOUT 10
|
|
|
|
|
2015-03-25 23:51:15 +01:00
|
|
|
static int apply_from_sdcard(Device* device, bool* wipe_cache) {
|
2015-04-08 02:16:35 +02:00
|
|
|
modified_flash = true;
|
|
|
|
|
2015-02-19 11:42:39 +01:00
|
|
|
if (ensure_path_mounted(SDCARD_ROOT) != 0) {
|
|
|
|
ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT);
|
|
|
|
return INSTALL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* path = browse_directory(SDCARD_ROOT, device);
|
|
|
|
if (path == NULL) {
|
2015-04-09 01:51:36 +02:00
|
|
|
ui->Print("\n-- No package file selected.\n");
|
2015-06-10 10:46:38 +02:00
|
|
|
ensure_path_unmounted(SDCARD_ROOT);
|
2015-02-19 11:42:39 +01:00
|
|
|
return INSTALL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->Print("\n-- Install %s ...\n", path);
|
|
|
|
set_sdcard_update_bootloader_message();
|
|
|
|
|
2016-01-14 00:05:20 +01:00
|
|
|
// We used to use fuse in a thread as opposed to a process. Since accessing
|
|
|
|
// through fuse involves going from kernel to userspace to kernel, it leads
|
|
|
|
// to deadlock when a page fault occurs. (Bug: 26313124)
|
|
|
|
pid_t child;
|
|
|
|
if ((child = fork()) == 0) {
|
|
|
|
bool status = start_sdcard_fuse(path);
|
|
|
|
|
|
|
|
_exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child
|
|
|
|
// process is ready.
|
|
|
|
int result = INSTALL_ERROR;
|
|
|
|
int status;
|
|
|
|
bool waited = false;
|
|
|
|
for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
|
|
|
|
if (waitpid(child, &status, WNOHANG) == -1) {
|
|
|
|
result = INSTALL_ERROR;
|
|
|
|
waited = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
|
|
|
|
if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
} else {
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
|
2016-01-14 00:05:20 +01:00
|
|
|
result = INSTALL_ERROR;
|
|
|
|
kill(child, SIGKILL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache,
|
2016-04-30 20:49:59 +02:00
|
|
|
TEMPORARY_INSTALL_FILE, false, 0/*retry_count*/);
|
2016-01-14 00:05:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!waited) {
|
|
|
|
// Calling stat() on this magic filename signals the fuse
|
|
|
|
// filesystem to shut down.
|
|
|
|
struct stat sb;
|
|
|
|
stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
|
|
|
|
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
|
2016-01-14 00:05:20 +01:00
|
|
|
}
|
2015-02-19 11:42:39 +01:00
|
|
|
|
|
|
|
ensure_path_unmounted(SDCARD_ROOT);
|
2016-01-14 00:05:20 +01:00
|
|
|
return result;
|
2015-02-19 11:42:39 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 22:20:23 +02:00
|
|
|
// Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION
|
|
|
|
// means to take the default, which is to reboot or shutdown depending
|
|
|
|
// on if the --shutdown_after flag was passed to recovery.
|
|
|
|
static Device::BuiltinAction
|
2012-09-24 19:40:47 +02:00
|
|
|
prompt_and_wait(Device* device, int status) {
|
2009-09-23 00:16:02 +02:00
|
|
|
for (;;) {
|
2016-03-24 19:50:34 +01:00
|
|
|
finish_recovery();
|
2012-09-24 19:40:47 +02:00
|
|
|
switch (status) {
|
|
|
|
case INSTALL_SUCCESS:
|
|
|
|
case INSTALL_NONE:
|
|
|
|
ui->SetBackground(RecoveryUI::NO_COMMAND);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSTALL_ERROR:
|
|
|
|
case INSTALL_CORRUPT:
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
break;
|
|
|
|
}
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->SetProgressType(RecoveryUI::EMPTY);
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2015-04-13 23:36:02 +02:00
|
|
|
int chosen_item = get_menu_selection(nullptr, device->GetMenuItems(), 0, 0, device);
|
2009-09-23 00:16:02 +02:00
|
|
|
|
|
|
|
// device-specific code may take some action here. It may
|
|
|
|
// return one of the core actions handled in the switch
|
|
|
|
// statement below.
|
2014-04-01 22:20:23 +02:00
|
|
|
Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
bool should_wipe_cache = false;
|
2014-04-01 22:20:23 +02:00
|
|
|
switch (chosen_action) {
|
|
|
|
case Device::NO_ACTION:
|
|
|
|
break;
|
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::REBOOT:
|
2014-04-01 22:20:23 +02:00
|
|
|
case Device::SHUTDOWN:
|
|
|
|
case Device::REBOOT_BOOTLOADER:
|
|
|
|
return chosen_action;
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::WIPE_DATA:
|
2016-12-19 22:04:23 +01:00
|
|
|
if (ui->IsTextVisible()) {
|
|
|
|
if (ask_to_wipe_data(device)) {
|
|
|
|
wipe_data(device);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wipe_data(device);
|
|
|
|
return Device::NO_ACTION;
|
|
|
|
}
|
2009-09-23 00:16:02 +02:00
|
|
|
break;
|
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::WIPE_CACHE:
|
2015-03-25 23:16:51 +01:00
|
|
|
wipe_cache(ui->IsTextVisible(), device);
|
2014-04-01 22:20:23 +02:00
|
|
|
if (!ui->IsTextVisible()) return Device::NO_ACTION;
|
2009-09-23 00:16:02 +02:00
|
|
|
break;
|
|
|
|
|
2015-02-19 11:42:39 +01:00
|
|
|
case Device::APPLY_ADB_SIDELOAD:
|
2015-04-10 19:01:53 +02:00
|
|
|
case Device::APPLY_SDCARD:
|
2015-02-19 11:42:39 +01:00
|
|
|
{
|
|
|
|
bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
|
|
|
|
if (adb) {
|
2015-03-25 23:16:51 +01:00
|
|
|
status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE);
|
2011-10-19 19:51:12 +02:00
|
|
|
} else {
|
2015-03-25 23:16:51 +01:00
|
|
|
status = apply_from_sdcard(device, &should_wipe_cache);
|
2011-10-19 19:51:12 +02:00
|
|
|
}
|
2014-07-10 19:50:39 +02:00
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
if (status == INSTALL_SUCCESS && should_wipe_cache) {
|
2015-03-31 21:19:05 +02:00
|
|
|
if (!wipe_cache(false, device)) {
|
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
2015-02-19 11:42:39 +01:00
|
|
|
}
|
|
|
|
|
2015-03-30 18:43:49 +02:00
|
|
|
if (status != INSTALL_SUCCESS) {
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
ui->Print("Installation aborted.\n");
|
|
|
|
copy_logs();
|
|
|
|
} else if (!ui->IsTextVisible()) {
|
|
|
|
return Device::NO_ACTION; // reboot if logs aren't visible
|
|
|
|
} else {
|
|
|
|
ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card");
|
2010-09-15 20:08:23 +02:00
|
|
|
}
|
2009-09-23 00:16:02 +02:00
|
|
|
}
|
|
|
|
break;
|
2011-10-31 17:34:15 +01:00
|
|
|
|
2015-04-10 19:01:53 +02:00
|
|
|
case Device::VIEW_RECOVERY_LOGS:
|
|
|
|
choose_recovery_file(device);
|
2011-06-24 07:14:24 +02:00
|
|
|
break;
|
2012-01-10 00:16:13 +01:00
|
|
|
|
2016-04-15 01:49:04 +02:00
|
|
|
case Device::RUN_GRAPHICS_TEST:
|
|
|
|
run_graphics_test(device);
|
|
|
|
break;
|
|
|
|
|
2015-04-10 19:01:53 +02:00
|
|
|
case Device::MOUNT_SYSTEM:
|
2015-07-30 23:43:27 +02:00
|
|
|
// For a system image built with the root directory (i.e.
|
|
|
|
// system_root_image == "true"), we mount it to /system_root, and symlink /system
|
|
|
|
// to /system_root/system to make adb shell work (the symlink is created through
|
|
|
|
// the build system).
|
|
|
|
// Bug: 22855115
|
2016-09-24 00:30:55 +02:00
|
|
|
if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
|
2015-07-30 23:43:27 +02:00
|
|
|
if (ensure_path_mounted_at("/", "/system_root") != -1) {
|
|
|
|
ui->Print("Mounted /system.\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ensure_path_mounted("/system") != -1) {
|
|
|
|
ui->Print("Mounted /system.\n");
|
|
|
|
}
|
2015-04-10 19:01:53 +02:00
|
|
|
}
|
2015-07-30 23:43:27 +02:00
|
|
|
|
2014-10-23 03:38:48 +02:00
|
|
|
break;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-30 17:40:57 +01:00
|
|
|
print_property(const char *key, const char *name, void *cookie) {
|
2010-07-01 18:18:44 +02:00
|
|
|
printf("%s=%s\n", key, name);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2016-11-03 19:37:15 +01:00
|
|
|
static std::string load_locale_from_cache() {
|
|
|
|
if (ensure_path_mounted(LOCALE_FILE) != 0) {
|
|
|
|
LOG(ERROR) << "Can't mount " << LOCALE_FILE;
|
|
|
|
return "";
|
2012-08-23 02:26:40 +02:00
|
|
|
}
|
|
|
|
|
2016-11-03 19:37:15 +01:00
|
|
|
std::string content;
|
|
|
|
if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
|
|
|
|
PLOG(ERROR) << "Can't read " << LOCALE_FILE;
|
|
|
|
return "";
|
|
|
|
}
|
2013-05-14 20:03:02 +02:00
|
|
|
|
2016-11-03 19:37:15 +01:00
|
|
|
return android::base::Trim(content);
|
|
|
|
}
|
2013-05-14 20:03:02 +02:00
|
|
|
|
2016-11-03 19:37:15 +01:00
|
|
|
void ui_print(const char* format, ...) {
|
|
|
|
std::string buffer;
|
2013-05-14 20:03:02 +02:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2016-11-03 19:37:15 +01:00
|
|
|
android::base::StringAppendV(&buffer, format, ap);
|
2013-05-14 20:03:02 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
2016-11-03 19:37:15 +01:00
|
|
|
if (ui != nullptr) {
|
|
|
|
ui->Print("%s", buffer.c_str());
|
2013-05-14 20:03:02 +02:00
|
|
|
} else {
|
2016-11-03 19:37:15 +01:00
|
|
|
fputs(buffer.c_str(), stdout);
|
2013-05-14 20:03:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 03:00:04 +02:00
|
|
|
static constexpr char log_characters[] = "VDIWEF";
|
|
|
|
|
|
|
|
void UiLogger(android::base::LogId id, android::base::LogSeverity severity,
|
|
|
|
const char* tag, const char* file, unsigned int line,
|
|
|
|
const char* message) {
|
2016-11-03 19:37:15 +01:00
|
|
|
if (severity >= android::base::ERROR && ui != nullptr) {
|
|
|
|
ui->Print("E:%s\n", message);
|
2016-08-06 03:00:04 +02:00
|
|
|
} else {
|
|
|
|
fprintf(stdout, "%c:%s\n", log_characters[severity], message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 21:21:52 +01:00
|
|
|
static bool is_battery_ok() {
|
|
|
|
struct healthd_config healthd_config = {
|
|
|
|
.batteryStatusPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryHealthPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryPresentPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryCapacityPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryVoltagePath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryTemperaturePath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryTechnologyPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryCurrentNowPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryCurrentAvgPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryChargeCounterPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryFullChargePath = android::String8(android::String8::kEmptyString),
|
|
|
|
.batteryCycleCountPath = android::String8(android::String8::kEmptyString),
|
|
|
|
.energyCounter = NULL,
|
|
|
|
.boot_min_cap = 0,
|
|
|
|
.screen_on = NULL
|
|
|
|
};
|
|
|
|
healthd_board_init(&healthd_config);
|
|
|
|
|
|
|
|
android::BatteryMonitor monitor;
|
|
|
|
monitor.init(&healthd_config);
|
|
|
|
|
|
|
|
int wait_second = 0;
|
|
|
|
while (true) {
|
|
|
|
int charge_status = monitor.getChargeStatus();
|
|
|
|
// Treat unknown status as charged.
|
|
|
|
bool charged = (charge_status != android::BATTERY_STATUS_DISCHARGING &&
|
|
|
|
charge_status != android::BATTERY_STATUS_NOT_CHARGING);
|
|
|
|
android::BatteryProperty capacity;
|
|
|
|
android::status_t status = monitor.getProperty(android::BATTERY_PROP_CAPACITY, &capacity);
|
|
|
|
ui_print("charge_status %d, charged %d, status %d, capacity %lld\n", charge_status,
|
|
|
|
charged, status, capacity.valueInt64);
|
|
|
|
// At startup, the battery drivers in devices like N5X/N6P take some time to load
|
|
|
|
// the battery profile. Before the load finishes, it reports value 50 as a fake
|
|
|
|
// capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected
|
|
|
|
// to finish loading the battery profile earlier than 10 seconds after kernel startup.
|
|
|
|
if (status == 0 && capacity.valueInt64 == 50) {
|
|
|
|
if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) {
|
|
|
|
sleep(1);
|
|
|
|
wait_second++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we can't read battery percentage, it may be a device without battery. In this
|
|
|
|
// situation, use 100 as a fake battery percentage.
|
|
|
|
if (status != 0) {
|
|
|
|
capacity.valueInt64 = 100;
|
|
|
|
}
|
|
|
|
return (charged && capacity.valueInt64 >= BATTERY_WITH_CHARGER_OK_PERCENTAGE) ||
|
|
|
|
(!charged && capacity.valueInt64 >= BATTERY_OK_PERCENTAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 03:25:58 +01:00
|
|
|
static void set_retry_bootloader_message(int retry_count, int argc, char** argv) {
|
2016-09-29 01:32:10 +02:00
|
|
|
std::vector<std::string> options;
|
2016-02-06 03:25:58 +01:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (strstr(argv[i], "retry_count") == nullptr) {
|
2016-09-29 01:32:10 +02:00
|
|
|
options.push_back(argv[i]);
|
2016-02-06 03:25:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 01:32:10 +02:00
|
|
|
// Increment the retry counter by 1.
|
|
|
|
options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count+1));
|
2016-06-25 03:22:02 +02:00
|
|
|
std::string err;
|
2016-12-14 06:53:31 +01:00
|
|
|
if (!update_bootloader_message(options, &err)) {
|
2016-06-25 03:22:02 +02:00
|
|
|
LOG(ERROR) << err;
|
|
|
|
}
|
2016-02-06 03:25:58 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 23:04:08 +02:00
|
|
|
static bool bootreason_in_blacklist() {
|
|
|
|
char bootreason[PROPERTY_VALUE_MAX];
|
|
|
|
if (property_get("ro.boot.bootreason", bootreason, nullptr) > 0) {
|
|
|
|
for (const auto& str : bootreason_blacklist) {
|
|
|
|
if (strcasecmp(str.c_str(), bootreason) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void log_failure_code(ErrorCode code, const char *update_package) {
|
|
|
|
std::vector<std::string> log_buffer = {
|
|
|
|
update_package,
|
|
|
|
"0", // install result
|
|
|
|
"error: " + std::to_string(code),
|
|
|
|
};
|
|
|
|
std::string log_content = android::base::Join(log_buffer, "\n");
|
|
|
|
if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) {
|
2016-11-03 19:37:15 +01:00
|
|
|
PLOG(ERROR) << "failed to write " << TEMPORARY_INSTALL_FILE;
|
2016-07-11 23:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also write the info into last_log.
|
|
|
|
LOG(INFO) << log_content;
|
|
|
|
}
|
|
|
|
|
2016-02-17 21:21:52 +01:00
|
|
|
int main(int argc, char **argv) {
|
2016-08-06 03:00:04 +02:00
|
|
|
// We don't have logcat yet under recovery; so we'll print error on screen and
|
|
|
|
// log to stdout (which is redirected to recovery.log) as we used to do.
|
|
|
|
android::base::InitLogging(argv, &UiLogger);
|
|
|
|
|
2016-03-09 23:58:16 +01:00
|
|
|
// Take last pmsg contents and rewrite it to the current pmsg session.
|
|
|
|
static const char filter[] = "recovery/";
|
|
|
|
// Do we need to rotate?
|
|
|
|
bool doRotate = false;
|
2016-10-22 02:46:13 +02:00
|
|
|
|
2016-03-09 23:58:16 +01:00
|
|
|
__android_log_pmsg_file_read(
|
|
|
|
LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
|
|
|
|
logbasename, &doRotate);
|
|
|
|
// Take action to refresh pmsg contents
|
|
|
|
__android_log_pmsg_file_read(
|
|
|
|
LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
|
|
|
|
logrotate, &doRotate);
|
|
|
|
|
2012-01-10 00:16:13 +01:00
|
|
|
// If this binary is started with the single argument "--adbd",
|
|
|
|
// instead of being the normal recovery binary, it turns into kind
|
|
|
|
// of a stripped-down version of adbd that only supports the
|
|
|
|
// 'sideload' command. Note this must be a real argument, not
|
|
|
|
// anything in the command file or bootloader control block; the
|
|
|
|
// only way recovery should be run with this argument is when it
|
|
|
|
// starts a copy of itself from the apply_from_adb() function.
|
|
|
|
if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
|
2016-08-27 03:24:34 +02:00
|
|
|
minadbd_main();
|
2012-01-10 00:16:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-11 00:32:24 +02:00
|
|
|
time_t start = time(NULL);
|
|
|
|
|
|
|
|
// redirect_stdio should be called only in non-sideload mode. Otherwise
|
|
|
|
// we may have two logger instances with different timestamps.
|
|
|
|
redirect_stdio(TEMPORARY_LOG_FILE);
|
|
|
|
|
2014-01-21 18:25:41 +01:00
|
|
|
printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
load_volume_table();
|
2016-02-25 21:29:40 +01:00
|
|
|
has_cache = volume_for_path(CACHE_ROOT) != nullptr;
|
|
|
|
|
2016-12-02 20:32:19 +01:00
|
|
|
std::vector<std::string> args = get_args(argc, argv);
|
|
|
|
std::vector<char*> args_to_parse(args.size());
|
|
|
|
std::transform(args.cbegin(), args.cend(), args_to_parse.begin(),
|
|
|
|
[](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
const char *update_package = NULL;
|
2015-03-31 21:19:05 +02:00
|
|
|
bool should_wipe_data = false;
|
2016-12-19 22:04:23 +01:00
|
|
|
bool should_prompt_and_wipe_data = false;
|
2015-03-30 18:43:49 +02:00
|
|
|
bool should_wipe_cache = false;
|
2016-06-25 03:22:02 +02:00
|
|
|
bool should_wipe_ab = false;
|
2016-06-09 23:09:39 +02:00
|
|
|
size_t wipe_package_size = 0;
|
2015-03-25 23:51:15 +01:00
|
|
|
bool show_text = false;
|
2015-03-30 18:43:49 +02:00
|
|
|
bool sideload = false;
|
|
|
|
bool sideload_auto_reboot = false;
|
2012-04-12 20:01:22 +02:00
|
|
|
bool just_exit = false;
|
2014-03-18 18:32:12 +01:00
|
|
|
bool shutdown_after = false;
|
2016-02-06 03:25:58 +01:00
|
|
|
int retry_count = 0;
|
2016-04-29 03:06:26 +02:00
|
|
|
bool security_update = false;
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
int arg;
|
2016-06-02 20:16:50 +02:00
|
|
|
int option_index;
|
2016-12-02 20:32:19 +01:00
|
|
|
while ((arg = getopt_long(args_to_parse.size(), args_to_parse.data(), "", OPTIONS,
|
|
|
|
&option_index)) != -1) {
|
2009-03-04 04:28:42 +01:00
|
|
|
switch (arg) {
|
2016-02-06 03:25:58 +01:00
|
|
|
case 'n': android::base::ParseInt(optarg, &retry_count, 0); break;
|
2009-03-04 04:28:42 +01:00
|
|
|
case 'u': update_package = optarg; break;
|
2015-03-31 21:19:05 +02:00
|
|
|
case 'w': should_wipe_data = true; break;
|
2015-03-30 18:43:49 +02:00
|
|
|
case 'c': should_wipe_cache = true; break;
|
2015-03-25 23:51:15 +01:00
|
|
|
case 't': show_text = true; break;
|
2015-03-30 18:43:49 +02:00
|
|
|
case 's': sideload = true; break;
|
|
|
|
case 'a': sideload = true; sideload_auto_reboot = true; break;
|
2012-04-12 20:01:22 +02:00
|
|
|
case 'x': just_exit = true; break;
|
2012-08-23 02:26:40 +02:00
|
|
|
case 'l': locale = optarg; break;
|
2013-11-25 22:53:25 +01:00
|
|
|
case 'g': {
|
|
|
|
if (stage == NULL || *stage == '\0') {
|
|
|
|
char buffer[20] = "1/";
|
|
|
|
strncat(buffer, optarg, sizeof(buffer)-3);
|
|
|
|
stage = strdup(buffer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-03-18 18:32:12 +01:00
|
|
|
case 'p': shutdown_after = true; break;
|
2014-09-24 20:46:17 +02:00
|
|
|
case 'r': reason = optarg; break;
|
2016-04-29 03:06:26 +02:00
|
|
|
case 'e': security_update = true; break;
|
2016-06-02 20:16:50 +02:00
|
|
|
case 0: {
|
2016-12-19 22:04:23 +01:00
|
|
|
std::string option = OPTIONS[option_index].name;
|
|
|
|
if (option == "wipe_ab") {
|
2016-06-25 03:22:02 +02:00
|
|
|
should_wipe_ab = true;
|
2016-12-19 22:04:23 +01:00
|
|
|
} else if (option == "wipe_package_size") {
|
2016-06-09 23:09:39 +02:00
|
|
|
android::base::ParseUint(optarg, &wipe_package_size);
|
2016-12-19 22:04:23 +01:00
|
|
|
} else if (option == "prompt_and_wipe_data") {
|
|
|
|
should_prompt_and_wipe_data = true;
|
2016-06-02 20:16:50 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
case '?':
|
2016-08-06 03:00:04 +02:00
|
|
|
LOG(ERROR) << "Invalid command argument";
|
2009-03-04 04:28:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 19:37:15 +01:00
|
|
|
if (locale.empty()) {
|
|
|
|
if (has_cache) {
|
|
|
|
locale = load_locale_from_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (locale.empty()) {
|
|
|
|
locale = DEFAULT_LOCALE;
|
|
|
|
}
|
2012-08-23 02:26:40 +02:00
|
|
|
}
|
2016-11-03 19:37:15 +01:00
|
|
|
|
|
|
|
printf("locale is [%s]\n", locale.c_str());
|
2014-02-14 00:07:56 +01:00
|
|
|
printf("stage is [%s]\n", stage);
|
2014-09-24 20:46:17 +02:00
|
|
|
printf("reason is [%s]\n", reason);
|
2012-08-23 02:26:40 +02:00
|
|
|
|
|
|
|
Device* device = make_device();
|
|
|
|
ui = device->GetUI();
|
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
if (!ui->Init(locale)) {
|
2016-12-10 01:20:49 +01:00
|
|
|
printf("Failed to initialize UI, use stub UI instead.");
|
|
|
|
ui = new StubRecoveryUI();
|
|
|
|
}
|
2016-04-29 03:06:26 +02:00
|
|
|
// Set background string to "installing security update" for security update,
|
|
|
|
// otherwise set it to "installing system update".
|
|
|
|
ui->SetSystemUpdateText(security_update);
|
2013-11-25 22:53:25 +01:00
|
|
|
|
|
|
|
int st_cur, st_max;
|
|
|
|
if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {
|
|
|
|
ui->SetStage(st_cur, st_max);
|
|
|
|
}
|
|
|
|
|
2012-08-23 02:26:40 +02:00
|
|
|
ui->SetBackground(RecoveryUI::NONE);
|
|
|
|
if (show_text) ui->ShowText(true);
|
|
|
|
|
2012-02-09 20:13:23 +01:00
|
|
|
struct selinux_opt seopts[] = {
|
2016-11-03 19:37:15 +01:00
|
|
|
{ SELABEL_OPT_PATH, "/file_contexts" }
|
2012-02-09 20:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
|
|
|
|
|
|
|
|
if (!sehandle) {
|
2013-07-09 21:29:45 +02:00
|
|
|
ui->Print("Warning: No file_contexts\n");
|
2012-02-09 20:13:23 +01:00
|
|
|
}
|
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
device->StartRecovery();
|
2010-02-02 00:59:12 +01:00
|
|
|
|
2010-07-01 18:18:44 +02:00
|
|
|
printf("Command:");
|
2016-12-02 20:32:19 +01:00
|
|
|
for (const auto& arg : args) {
|
|
|
|
printf(" \"%s\"", arg.c_str());
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2016-12-10 18:24:50 +01:00
|
|
|
printf("\n\n");
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
property_list(print_property, NULL);
|
2010-07-01 18:18:44 +02:00
|
|
|
printf("\n");
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2015-04-10 05:51:08 +02:00
|
|
|
ui->Print("Supported API: %d\n", RECOVERY_API_VERSION);
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
int status = INSTALL_SUCCESS;
|
|
|
|
|
2011-01-18 22:36:58 +01:00
|
|
|
if (update_package != NULL) {
|
2016-04-30 00:37:05 +02:00
|
|
|
// It's not entirely true that we will modify the flash. But we want
|
|
|
|
// to log the update attempt since update_package is non-NULL.
|
|
|
|
modified_flash = true;
|
|
|
|
|
2016-02-17 21:21:52 +01:00
|
|
|
if (!is_battery_ok()) {
|
|
|
|
ui->Print("battery capacity is not enough for installing package, needed is %d%%\n",
|
|
|
|
BATTERY_OK_PERCENTAGE);
|
2016-04-30 20:49:59 +02:00
|
|
|
// Log the error code to last_install when installation skips due to
|
|
|
|
// low battery.
|
2016-07-11 23:04:08 +02:00
|
|
|
log_failure_code(kLowBattery, update_package);
|
|
|
|
status = INSTALL_SKIPPED;
|
|
|
|
} else if (bootreason_in_blacklist()) {
|
|
|
|
// Skip update-on-reboot when bootreason is kernel_panic or similar
|
|
|
|
ui->Print("bootreason is in the blacklist; skip OTA installation\n");
|
|
|
|
log_failure_code(kBootreasonInBlacklist, update_package);
|
2016-02-17 21:21:52 +01:00
|
|
|
status = INSTALL_SKIPPED;
|
|
|
|
} else {
|
|
|
|
status = install_package(update_package, &should_wipe_cache,
|
2016-04-30 20:49:59 +02:00
|
|
|
TEMPORARY_INSTALL_FILE, true, retry_count);
|
2016-02-17 21:21:52 +01:00
|
|
|
if (status == INSTALL_SUCCESS && should_wipe_cache) {
|
|
|
|
wipe_cache(false, device);
|
|
|
|
}
|
|
|
|
if (status != INSTALL_SUCCESS) {
|
|
|
|
ui->Print("Installation aborted.\n");
|
2016-02-06 03:25:58 +01:00
|
|
|
// When I/O error happens, reboot and retry installation EIO_RETRY_COUNT
|
|
|
|
// times before we abandon this OTA update.
|
|
|
|
if (status == INSTALL_RETRY && retry_count < EIO_RETRY_COUNT) {
|
|
|
|
copy_logs();
|
|
|
|
set_retry_bootloader_message(retry_count, argc, argv);
|
|
|
|
// Print retry count on screen.
|
|
|
|
ui->Print("Retry attempt %d\n", retry_count);
|
|
|
|
|
|
|
|
// Reboot and retry the update
|
2016-09-24 00:30:55 +02:00
|
|
|
if (!android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,recovery")) {
|
2016-02-06 03:25:58 +01:00
|
|
|
ui->Print("Reboot failed\n");
|
|
|
|
} else {
|
|
|
|
while (true) {
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 21:21:52 +01:00
|
|
|
// If this is an eng or userdebug build, then automatically
|
|
|
|
// turn the text display on if the script fails so the error
|
|
|
|
// message is visible.
|
|
|
|
if (is_ro_debuggable()) {
|
|
|
|
ui->ShowText(true);
|
|
|
|
}
|
2013-05-14 20:03:02 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-31 21:19:05 +02:00
|
|
|
} else if (should_wipe_data) {
|
2016-12-19 22:04:23 +01:00
|
|
|
if (!wipe_data(device)) {
|
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
|
|
|
} else if (should_prompt_and_wipe_data) {
|
|
|
|
ui->ShowText(true);
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
if (!prompt_and_wipe_data(device)) {
|
2015-03-31 21:19:05 +02:00
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
2016-12-19 22:04:23 +01:00
|
|
|
ui->ShowText(false);
|
2015-03-30 18:43:49 +02:00
|
|
|
} else if (should_wipe_cache) {
|
2015-03-31 21:19:05 +02:00
|
|
|
if (!wipe_cache(false, device)) {
|
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
2016-06-25 03:22:02 +02:00
|
|
|
} else if (should_wipe_ab) {
|
2016-06-09 23:09:39 +02:00
|
|
|
if (!wipe_ab_device(wipe_package_size)) {
|
2016-06-02 20:16:50 +02:00
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
2015-03-30 18:43:49 +02:00
|
|
|
} else if (sideload) {
|
|
|
|
// 'adb reboot sideload' acts the same as user presses key combinations
|
|
|
|
// to enter the sideload mode. When 'sideload-auto-reboot' is used, text
|
|
|
|
// display will NOT be turned on by default. And it will reboot after
|
|
|
|
// sideload finishes even if there are errors. Unless one turns on the
|
|
|
|
// text display during the installation. This is to enable automated
|
|
|
|
// testing.
|
|
|
|
if (!sideload_auto_reboot) {
|
|
|
|
ui->ShowText(true);
|
|
|
|
}
|
|
|
|
status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE);
|
|
|
|
if (status == INSTALL_SUCCESS && should_wipe_cache) {
|
2015-03-31 21:19:05 +02:00
|
|
|
if (!wipe_cache(false, device)) {
|
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
2015-03-30 18:43:49 +02:00
|
|
|
}
|
|
|
|
ui->Print("\nInstall from ADB complete (status: %d).\n", status);
|
|
|
|
if (sideload_auto_reboot) {
|
|
|
|
ui->Print("Rebooting automatically.\n");
|
|
|
|
}
|
2012-04-12 20:01:22 +02:00
|
|
|
} else if (!just_exit) {
|
2012-08-23 02:26:40 +02:00
|
|
|
status = INSTALL_NONE; // No command specified
|
|
|
|
ui->SetBackground(RecoveryUI::NO_COMMAND);
|
2015-05-04 18:34:29 +02:00
|
|
|
|
|
|
|
// http://b/17489952
|
|
|
|
// If this is an eng or userdebug build, automatically turn on the
|
|
|
|
// text display if no command is specified.
|
|
|
|
if (is_ro_debuggable()) {
|
|
|
|
ui->ShowText(true);
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2015-03-30 18:43:49 +02:00
|
|
|
if (!sideload_auto_reboot && (status == INSTALL_ERROR || status == INSTALL_CORRUPT)) {
|
2013-07-02 20:43:25 +02:00
|
|
|
copy_logs();
|
2012-08-23 02:26:40 +02:00
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
}
|
2015-03-30 18:43:49 +02:00
|
|
|
|
2014-04-01 22:20:23 +02:00
|
|
|
Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
|
2016-02-17 21:21:52 +01:00
|
|
|
if ((status != INSTALL_SUCCESS && status != INSTALL_SKIPPED && !sideload_auto_reboot) ||
|
|
|
|
ui->IsTextVisible()) {
|
2014-04-01 22:20:23 +02:00
|
|
|
Device::BuiltinAction temp = prompt_and_wait(device, status);
|
2015-03-30 18:43:49 +02:00
|
|
|
if (temp != Device::NO_ACTION) {
|
|
|
|
after = temp;
|
|
|
|
}
|
2010-09-15 20:08:23 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2014-04-01 22:20:23 +02:00
|
|
|
// Save logs and clean up before rebooting or shutting down.
|
2016-03-24 19:50:34 +01:00
|
|
|
finish_recovery();
|
2014-04-01 22:20:23 +02:00
|
|
|
|
|
|
|
switch (after) {
|
|
|
|
case Device::SHUTDOWN:
|
|
|
|
ui->Print("Shutting down...\n");
|
2016-09-24 00:30:55 +02:00
|
|
|
android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,");
|
2014-04-01 22:20:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Device::REBOOT_BOOTLOADER:
|
|
|
|
ui->Print("Rebooting to bootloader...\n");
|
2016-09-24 00:30:55 +02:00
|
|
|
android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
|
2014-04-01 22:20:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ui->Print("Rebooting...\n");
|
2016-09-24 00:30:55 +02:00
|
|
|
android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,");
|
2014-04-01 22:20:23 +02:00
|
|
|
break;
|
2014-03-18 18:32:12 +01:00
|
|
|
}
|
2015-05-27 23:46:17 +02:00
|
|
|
while (true) {
|
2016-11-03 19:37:15 +01:00
|
|
|
pause();
|
2015-05-27 23:46:17 +02:00
|
|
|
}
|
|
|
|
// Should be unreachable.
|
2009-03-04 04:28:42 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|