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>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.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>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2010-09-15 20:08:23 +02:00
|
|
|
#include <dirent.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
#include "bootloader.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "cutils/properties.h"
|
2011-03-08 08:37:27 +01:00
|
|
|
#include "cutils/android_reboot.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
#include "install.h"
|
|
|
|
#include "minui/minui.h"
|
|
|
|
#include "minzip/DirUtil.h"
|
|
|
|
#include "roots.h"
|
2011-10-28 19:33:05 +02:00
|
|
|
#include "ui.h"
|
2011-10-29 00:13:10 +02:00
|
|
|
#include "screen_ui.h"
|
2011-10-31 17:34:15 +01:00
|
|
|
#include "device.h"
|
2012-01-10 00:16:13 +01:00
|
|
|
#include "adb_install.h"
|
|
|
|
extern "C" {
|
|
|
|
#include "minadbd/adb.h"
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2012-02-09 20:13:23 +01:00
|
|
|
struct selabel_handle *sehandle;
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
static const struct option OPTIONS[] = {
|
|
|
|
{ "send_intent", required_argument, NULL, 's' },
|
|
|
|
{ "update_package", required_argument, NULL, 'u' },
|
|
|
|
{ "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' },
|
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' },
|
2009-10-06 23:41:38 +02:00
|
|
|
{ NULL, 0, NULL, 0 },
|
2009-03-04 04:28:42 +01:00
|
|
|
};
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
static const char *COMMAND_FILE = "/cache/recovery/command";
|
|
|
|
static const char *INTENT_FILE = "/cache/recovery/intent";
|
|
|
|
static const char *LOG_FILE = "/cache/recovery/log";
|
2010-09-29 22:21:30 +02:00
|
|
|
static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
|
2011-10-19 19:51:12 +02:00
|
|
|
static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
|
2012-08-23 02:26:40 +02:00
|
|
|
static const char *LOCALE_FILE = "/cache/recovery/locale";
|
2011-06-24 07:14:24 +02:00
|
|
|
static const char *CACHE_ROOT = "/cache";
|
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";
|
2010-09-20 21:16:13 +02:00
|
|
|
static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2011-10-29 00:13:10 +02:00
|
|
|
RecoveryUI* ui = NULL;
|
2012-08-23 02:26:40 +02:00
|
|
|
char* locale = NULL;
|
2011-10-29 00:13:10 +02:00
|
|
|
|
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)
|
|
|
|
* /cache/recovery/intent - OUTPUT - intent that was passed in
|
|
|
|
*
|
|
|
|
* The arguments which may be supplied in the recovery.command file:
|
|
|
|
* --send_intent=anystring - write the text out to recovery.intent
|
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
|
|
|
|
* --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
|
|
|
|
* 7b; the user reboots (pulling the battery, etc) into the main system
|
|
|
|
* 8. main() calls maybe_install_firmware_update()
|
|
|
|
* ** if the update contained radio/hboot firmware **:
|
|
|
|
* 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
|
|
|
|
* -- after this, rebooting will reformat cache & restart main system --
|
|
|
|
* 8b. m_i_f_u() writes firmware image into raw cache partition
|
|
|
|
* 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
|
|
|
|
* -- after this, rebooting will attempt to reinstall firmware --
|
|
|
|
* 8d. bootloader tries to flash firmware
|
|
|
|
* 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
|
|
|
|
* -- after this, rebooting will reformat cache & restart main system --
|
2010-09-20 21:16:13 +02:00
|
|
|
* 8f. erase_volume() reformats /cache
|
2009-03-04 04:28:42 +01:00
|
|
|
* 8g. finish_recovery() erases BCB
|
|
|
|
* -- after this, rebooting will (try to) restart the main system --
|
|
|
|
* 9. main() calls reboot() to boot main system
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const int MAX_ARG_LENGTH = 4096;
|
|
|
|
static const int MAX_ARGS = 100;
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
// open a given path, mounting partitions as necessary
|
2011-04-12 18:28:10 +02:00
|
|
|
FILE*
|
2010-09-20 21:16:13 +02:00
|
|
|
fopen_path(const char *path, const char *mode) {
|
|
|
|
if (ensure_path_mounted(path) != 0) {
|
|
|
|
LOGE("Can't mount %s\n", 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close a file, log an error if the error indicator is set
|
|
|
|
static void
|
|
|
|
check_and_fclose(FILE *fp, const char *name) {
|
|
|
|
fflush(fp);
|
|
|
|
if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
static void
|
|
|
|
get_args(int *argc, char ***argv) {
|
|
|
|
struct bootloader_message boot;
|
|
|
|
memset(&boot, 0, sizeof(boot));
|
|
|
|
get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
|
|
|
|
|
|
|
|
if (boot.command[0] != 0 && boot.command[0] != 255) {
|
|
|
|
LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boot.status[0] != 0 && boot.status[0] != 255) {
|
|
|
|
LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- if arguments weren't supplied, look in the bootloader control block
|
|
|
|
if (*argc <= 1) {
|
|
|
|
boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
|
|
|
|
const char *arg = strtok(boot.recovery, "\n");
|
|
|
|
if (arg != NULL && !strcmp(arg, "recovery")) {
|
|
|
|
*argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
|
|
|
|
(*argv)[0] = strdup(arg);
|
|
|
|
for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
|
|
|
|
if ((arg = strtok(NULL, "\n")) == NULL) break;
|
|
|
|
(*argv)[*argc] = strdup(arg);
|
|
|
|
}
|
|
|
|
LOGI("Got arguments from boot message\n");
|
|
|
|
} else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
|
|
|
|
LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- if that doesn't work, try the command file
|
|
|
|
if (*argc <= 1) {
|
2010-09-20 21:16:13 +02:00
|
|
|
FILE *fp = fopen_path(COMMAND_FILE, "r");
|
2009-03-04 04:28:42 +01:00
|
|
|
if (fp != NULL) {
|
|
|
|
char *argv0 = (*argv)[0];
|
|
|
|
*argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
|
|
|
|
(*argv)[0] = argv0; // use the same program name
|
|
|
|
|
|
|
|
char buf[MAX_ARG_LENGTH];
|
|
|
|
for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
|
|
|
|
if (!fgets(buf, sizeof(buf), fp)) break;
|
|
|
|
(*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
|
|
|
|
}
|
|
|
|
|
|
|
|
check_and_fclose(fp, COMMAND_FILE);
|
|
|
|
LOGI("Got arguments from %s\n", COMMAND_FILE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --> write the arguments we have back into the bootloader control block
|
|
|
|
// always boot into recovery after this (until finish_recovery() is called)
|
|
|
|
strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
|
|
|
|
strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < *argc; ++i) {
|
|
|
|
strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
|
|
|
|
strlcat(boot.recovery, "\n", sizeof(boot.recovery));
|
|
|
|
}
|
|
|
|
set_bootloader_message(&boot);
|
|
|
|
}
|
|
|
|
|
2009-08-18 21:05:45 +02:00
|
|
|
static void
|
2009-11-30 17:40:57 +01:00
|
|
|
set_sdcard_update_bootloader_message() {
|
2009-08-18 21:05:45 +02:00
|
|
|
struct bootloader_message boot;
|
|
|
|
memset(&boot, 0, sizeof(boot));
|
|
|
|
strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
|
|
|
|
strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
|
|
|
|
set_bootloader_message(&boot);
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2010-09-29 22:21:30 +02:00
|
|
|
// How much of the temp log we have copied to the copy in cache.
|
|
|
|
static long tmplog_offset = 0;
|
|
|
|
|
|
|
|
static void
|
2011-10-19 19:51:12 +02:00
|
|
|
copy_log_file(const char* source, const char* destination, int append) {
|
2010-09-29 22:21:30 +02:00
|
|
|
FILE *log = fopen_path(destination, append ? "a" : "w");
|
|
|
|
if (log == NULL) {
|
|
|
|
LOGE("Can't open %s\n", destination);
|
|
|
|
} else {
|
2011-10-19 19:51:12 +02:00
|
|
|
FILE *tmplog = fopen(source, "r");
|
|
|
|
if (tmplog != NULL) {
|
2010-09-29 22:21:30 +02:00
|
|
|
if (append) {
|
|
|
|
fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
|
|
|
|
}
|
|
|
|
char buf[4096];
|
|
|
|
while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
|
|
|
|
if (append) {
|
|
|
|
tmplog_offset = ftell(tmplog);
|
|
|
|
}
|
2011-10-19 19:51:12 +02:00
|
|
|
check_and_fclose(tmplog, source);
|
2010-09-29 22:21:30 +02:00
|
|
|
}
|
|
|
|
check_and_fclose(log, destination);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
// clear the recovery command and prepare to boot a (hopefully working) system,
|
|
|
|
// copy our log file to cache as well (for the system to read), and
|
|
|
|
// record any intent we were asked to communicate back to the system.
|
|
|
|
// this function is idempotent: call it as many times as you like.
|
|
|
|
static void
|
2009-11-30 17:40:57 +01:00
|
|
|
finish_recovery(const char *send_intent) {
|
2009-03-04 04:28:42 +01:00
|
|
|
// By this point, we're ready to return to the main system...
|
|
|
|
if (send_intent != NULL) {
|
2010-09-20 21:16:13 +02:00
|
|
|
FILE *fp = fopen_path(INTENT_FILE, "w");
|
2008-11-17 02:56:05 +01:00
|
|
|
if (fp == NULL) {
|
|
|
|
LOGE("Can't open %s\n", INTENT_FILE);
|
|
|
|
} else {
|
2009-03-04 04:28:42 +01:00
|
|
|
fputs(send_intent, fp);
|
|
|
|
check_and_fclose(fp, INTENT_FILE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy logs to cache so the system can find out what happened.
|
2011-10-19 19:51:12 +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);
|
|
|
|
chmod(LOG_FILE, 0600);
|
|
|
|
chown(LOG_FILE, 1000, 1000); // system user
|
2010-09-29 22:21:30 +02:00
|
|
|
chmod(LAST_LOG_FILE, 0640);
|
2011-10-19 19:51:12 +02:00
|
|
|
chmod(LAST_INSTALL_FILE, 0644);
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2012-08-23 02:26:40 +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.
|
|
|
|
if (locale != NULL) {
|
|
|
|
FILE* fp = fopen(LOCALE_FILE, "w");
|
|
|
|
fwrite(locale, 1, strlen(locale), fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2012-04-12 20:01:22 +02:00
|
|
|
// Reset to normal system boot so recovery won't cycle indefinitely.
|
2009-03-04 04:28:42 +01:00
|
|
|
struct bootloader_message boot;
|
|
|
|
memset(&boot, 0, sizeof(boot));
|
|
|
|
set_bootloader_message(&boot);
|
|
|
|
|
|
|
|
// Remove the command file, so recovery won't repeat indefinitely.
|
2010-09-20 21:16:13 +02:00
|
|
|
if (ensure_path_mounted(COMMAND_FILE) != 0 ||
|
|
|
|
(unlink(COMMAND_FILE) && errno != ENOENT)) {
|
2009-03-04 04:28:42 +01:00
|
|
|
LOGW("Can't unlink %s\n", COMMAND_FILE);
|
|
|
|
}
|
|
|
|
|
2011-10-19 19:51:12 +02:00
|
|
|
ensure_path_unmounted(CACHE_ROOT);
|
2009-03-04 04:28:42 +01:00
|
|
|
sync(); // For good measure.
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-09-20 21:16:13 +02:00
|
|
|
erase_volume(const char *volume) {
|
2012-08-23 02:26:40 +02:00
|
|
|
ui->SetBackground(RecoveryUI::ERASING);
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->SetProgressType(RecoveryUI::INDETERMINATE);
|
|
|
|
ui->Print("Formatting %s...\n", volume);
|
2010-09-29 22:21:30 +02:00
|
|
|
|
2011-10-19 19:51:12 +02:00
|
|
|
ensure_path_unmounted(volume);
|
|
|
|
|
2010-09-29 22:21:30 +02:00
|
|
|
if (strcmp(volume, "/cache") == 0) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
return format_volume(volume);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2010-07-09 02:27:55 +02:00
|
|
|
static char*
|
2010-09-20 21:16:13 +02:00
|
|
|
copy_sideloaded_package(const char* original_path) {
|
|
|
|
if (ensure_path_mounted(original_path) != 0) {
|
|
|
|
LOGE("Can't mount %s\n", original_path);
|
2010-07-09 02:27:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
|
2010-07-09 02:27:55 +02:00
|
|
|
LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
|
2010-07-09 02:27:55 +02:00
|
|
|
if (errno != EEXIST) {
|
|
|
|
LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
// verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
|
|
|
|
// directory, owned by root, readable and writable only by root.
|
2010-07-09 02:27:55 +02:00
|
|
|
struct stat st;
|
2010-09-20 21:16:13 +02:00
|
|
|
if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
|
|
|
|
LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
|
2010-07-09 02:27:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2010-09-20 21:16:13 +02:00
|
|
|
LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
|
2010-07-09 02:27:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((st.st_mode & 0777) != 0700) {
|
2010-09-20 21:16:13 +02:00
|
|
|
LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
|
2010-07-09 02:27:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (st.st_uid != 0) {
|
2010-09-20 21:16:13 +02:00
|
|
|
LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
|
2010-07-09 02:27:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
char copy_path[PATH_MAX];
|
|
|
|
strcpy(copy_path, SIDELOAD_TEMP_DIR);
|
2010-07-09 02:27:55 +02:00
|
|
|
strcat(copy_path, "/package.zip");
|
|
|
|
|
2011-10-28 19:33:05 +02:00
|
|
|
char* buffer = (char*)malloc(BUFSIZ);
|
2010-07-09 02:27:55 +02:00
|
|
|
if (buffer == NULL) {
|
|
|
|
LOGE("Failed to allocate buffer\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t read;
|
|
|
|
FILE* fin = fopen(original_path, "rb");
|
|
|
|
if (fin == NULL) {
|
|
|
|
LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
FILE* fout = fopen(copy_path, "wb");
|
|
|
|
if (fout == NULL) {
|
|
|
|
LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
|
|
|
|
if (fwrite(buffer, 1, read, fout) != read) {
|
|
|
|
LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
if (fclose(fout) != 0) {
|
|
|
|
LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose(fin) != 0) {
|
|
|
|
LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "adb push" is happy to overwrite read-only files when it's
|
|
|
|
// running as root, but we'll try anyway.
|
|
|
|
if (chmod(copy_path, 0400) != 0) {
|
|
|
|
LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
return strdup(copy_path);
|
2010-07-09 02:27:55 +02:00
|
|
|
}
|
|
|
|
|
2011-10-28 19:33:05 +02:00
|
|
|
static const char**
|
2011-10-31 17:34:15 +01:00
|
|
|
prepend_title(const char* const* headers) {
|
2011-10-28 19:33:05 +02:00
|
|
|
const char* title[] = { "Android system recovery <"
|
|
|
|
EXPAND(RECOVERY_API_VERSION) "e>",
|
|
|
|
"",
|
|
|
|
NULL };
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2009-06-18 07:07:13 +02:00
|
|
|
// count the number of lines in our title, plus the
|
2009-09-23 00:16:02 +02:00
|
|
|
// caller-provided headers.
|
2009-06-18 07:07:13 +02:00
|
|
|
int count = 0;
|
2011-10-31 17:34:15 +01:00
|
|
|
const char* const* p;
|
2009-06-18 07:07:13 +02:00
|
|
|
for (p = title; *p; ++p, ++count);
|
2009-09-23 00:16:02 +02:00
|
|
|
for (p = headers; *p; ++p, ++count);
|
2009-06-18 07:07:13 +02:00
|
|
|
|
2011-10-28 19:33:05 +02:00
|
|
|
const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
|
|
|
|
const char** h = new_headers;
|
2009-06-18 07:07:13 +02:00
|
|
|
for (p = title; *p; ++p, ++h) *h = *p;
|
2009-09-23 00:16:02 +02:00
|
|
|
for (p = headers; *p; ++p, ++h) *h = *p;
|
2009-06-18 07:07:13 +02:00
|
|
|
*h = NULL;
|
|
|
|
|
2009-09-23 00:16:02 +02:00
|
|
|
return new_headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
LOGI("timed out waiting for key input; rebooting.\n");
|
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:
|
2009-06-09 21:22:33 +02:00
|
|
|
--selected;
|
2011-10-29 00:13:10 +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:
|
2009-06-09 21:22:33 +02:00
|
|
|
++selected;
|
2011-10-29 00:13:10 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-19 19:51:12 +02:00
|
|
|
update_directory(const char* path, const char* unmount_when_done,
|
2011-10-31 17:34:15 +01:00
|
|
|
int* wipe_cache, Device* device) {
|
2011-06-24 07:14:24 +02:00
|
|
|
ensure_path_mounted(path);
|
2010-09-22 01:49:26 +02:00
|
|
|
|
2010-09-15 20:08:23 +02:00
|
|
|
const char* MENU_HEADERS[] = { "Choose a package to install:",
|
2010-09-20 21:16:13 +02:00
|
|
|
path,
|
2010-09-15 20:08:23 +02:00
|
|
|
"",
|
|
|
|
NULL };
|
|
|
|
DIR* d;
|
|
|
|
struct dirent* de;
|
2010-09-20 21:16:13 +02:00
|
|
|
d = opendir(path);
|
2010-09-15 20:08:23 +02:00
|
|
|
if (d == NULL) {
|
|
|
|
LOGE("error opening %s: %s\n", path, strerror(errno));
|
2011-06-24 07:14:24 +02:00
|
|
|
if (unmount_when_done != NULL) {
|
|
|
|
ensure_path_unmounted(unmount_when_done);
|
|
|
|
}
|
2010-09-15 20:08:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-28 19:33:05 +02:00
|
|
|
const char** headers = prepend_title(MENU_HEADERS);
|
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("../");
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
int result;
|
|
|
|
int chosen_item = 0;
|
|
|
|
do {
|
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)
|
2010-09-15 20:08:23 +02:00
|
|
|
result = -1;
|
|
|
|
break;
|
|
|
|
} else if (item[item_len-1] == '/') {
|
|
|
|
// recurse down into a subdirectory
|
|
|
|
char new_path[PATH_MAX];
|
2010-09-20 21:16:13 +02:00
|
|
|
strlcpy(new_path, path, PATH_MAX);
|
|
|
|
strlcat(new_path, "/", PATH_MAX);
|
2010-09-15 20:08:23 +02:00
|
|
|
strlcat(new_path, item, PATH_MAX);
|
2010-09-20 21:16:13 +02:00
|
|
|
new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
|
2011-10-31 17:34:15 +01:00
|
|
|
result = update_directory(new_path, unmount_when_done, wipe_cache, device);
|
2010-09-15 20:08:23 +02:00
|
|
|
if (result >= 0) break;
|
|
|
|
} else {
|
|
|
|
// selected a zip file: attempt to install it, and return
|
|
|
|
// the status to the caller.
|
|
|
|
char new_path[PATH_MAX];
|
2010-09-20 21:16:13 +02:00
|
|
|
strlcpy(new_path, path, PATH_MAX);
|
2010-09-22 01:49:26 +02:00
|
|
|
strlcat(new_path, "/", PATH_MAX);
|
2010-09-15 20:08:23 +02:00
|
|
|
strlcat(new_path, item, PATH_MAX);
|
|
|
|
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("\n-- Install %s ...\n", path);
|
2010-09-15 20:08:23 +02:00
|
|
|
set_sdcard_update_bootloader_message();
|
2010-09-20 21:16:13 +02:00
|
|
|
char* copy = copy_sideloaded_package(new_path);
|
2011-06-24 07:14:24 +02:00
|
|
|
if (unmount_when_done != NULL) {
|
|
|
|
ensure_path_unmounted(unmount_when_done);
|
|
|
|
}
|
2010-09-20 21:16:13 +02:00
|
|
|
if (copy) {
|
2011-10-19 19:51:12 +02:00
|
|
|
result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
|
2010-09-20 21:16:13 +02:00
|
|
|
free(copy);
|
|
|
|
} else {
|
|
|
|
result = INSTALL_ERROR;
|
|
|
|
}
|
2010-09-15 20:08:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < z_size; ++i) free(zips[i]);
|
|
|
|
free(zips);
|
|
|
|
free(headers);
|
|
|
|
|
2011-06-24 07:14:24 +02:00
|
|
|
if (unmount_when_done != NULL) {
|
|
|
|
ensure_path_unmounted(unmount_when_done);
|
|
|
|
}
|
2010-09-15 20:08:23 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-23 00:16:02 +02:00
|
|
|
static void
|
2011-10-31 17:34:15 +01:00
|
|
|
wipe_data(int confirm, Device* device) {
|
2009-09-23 00:16:02 +02:00
|
|
|
if (confirm) {
|
2011-10-28 19:33:05 +02:00
|
|
|
static const char** title_headers = NULL;
|
2009-09-23 00:16:02 +02:00
|
|
|
|
|
|
|
if (title_headers == NULL) {
|
2011-10-28 19:33:05 +02:00
|
|
|
const char* headers[] = { "Confirm wipe of all user data?",
|
|
|
|
" THIS CAN NOT BE UNDONE.",
|
|
|
|
"",
|
|
|
|
NULL };
|
2010-09-15 20:08:23 +02:00
|
|
|
title_headers = prepend_title((const char**)headers);
|
2009-09-23 00:16:02 +02:00
|
|
|
}
|
2009-04-01 22:57:40 +02:00
|
|
|
|
2011-10-28 19:33:05 +02:00
|
|
|
const char* items[] = { " No",
|
|
|
|
" No",
|
|
|
|
" No",
|
|
|
|
" No",
|
|
|
|
" No",
|
|
|
|
" No",
|
|
|
|
" No",
|
|
|
|
" Yes -- delete all user data", // [7]
|
|
|
|
" No",
|
|
|
|
" No",
|
|
|
|
" No",
|
|
|
|
NULL };
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
|
2009-09-23 00:16:02 +02:00
|
|
|
if (chosen_item != 7) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("\n-- Wiping data...\n");
|
2011-10-31 17:34:15 +01:00
|
|
|
device->WipeData();
|
2010-09-20 21:16:13 +02:00
|
|
|
erase_volume("/data");
|
|
|
|
erase_volume("/cache");
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("Data wipe complete.\n");
|
2009-09-23 00:16:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-31 17:34:15 +01:00
|
|
|
prompt_and_wait(Device* device) {
|
|
|
|
const char* const* headers = prepend_title(device->GetMenuHeaders());
|
2009-09-23 00:16:02 +02:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
finish_recovery(NULL);
|
2012-08-23 02:26:40 +02:00
|
|
|
ui->SetBackground(RecoveryUI::NO_COMMAND);
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->SetProgressType(RecoveryUI::EMPTY);
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
int chosen_item = get_menu_selection(headers, 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.
|
2011-10-31 17:34:15 +01:00
|
|
|
chosen_item = device->InvokeMenuItem(chosen_item);
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2011-06-24 07:14:24 +02:00
|
|
|
int status;
|
2011-10-19 19:51:12 +02:00
|
|
|
int wipe_cache;
|
2009-09-23 00:16:02 +02:00
|
|
|
switch (chosen_item) {
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::REBOOT:
|
2009-09-23 00:16:02 +02:00
|
|
|
return;
|
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::WIPE_DATA:
|
|
|
|
wipe_data(ui->IsTextVisible(), device);
|
2011-10-29 00:13:10 +02:00
|
|
|
if (!ui->IsTextVisible()) return;
|
2009-09-23 00:16:02 +02:00
|
|
|
break;
|
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::WIPE_CACHE:
|
2012-08-23 02:26:40 +02:00
|
|
|
ui->ShowText(false);
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("\n-- Wiping cache...\n");
|
2010-09-20 21:16:13 +02:00
|
|
|
erase_volume("/cache");
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("Cache wipe complete.\n");
|
|
|
|
if (!ui->IsTextVisible()) return;
|
2009-09-23 00:16:02 +02:00
|
|
|
break;
|
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
case Device::APPLY_EXT:
|
2011-12-14 01:03:28 +01:00
|
|
|
// Some packages expect /cache to be mounted (eg,
|
|
|
|
// standard incremental packages expect to use /cache
|
|
|
|
// as scratch space).
|
|
|
|
ensure_path_mounted(CACHE_ROOT);
|
2011-10-31 17:34:15 +01:00
|
|
|
status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
|
2011-10-19 19:51:12 +02:00
|
|
|
if (status == INSTALL_SUCCESS && wipe_cache) {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("\n-- Wiping cache (at package request)...\n");
|
2011-10-19 19:51:12 +02:00
|
|
|
if (erase_volume("/cache")) {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("Cache wipe failed.\n");
|
2011-10-19 19:51:12 +02:00
|
|
|
} else {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("Cache wipe complete.\n");
|
2011-10-19 19:51:12 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-15 20:08:23 +02:00
|
|
|
if (status >= 0) {
|
|
|
|
if (status != INSTALL_SUCCESS) {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
ui->Print("Installation aborted.\n");
|
|
|
|
} else if (!ui->IsTextVisible()) {
|
2010-09-15 20:08:23 +02:00
|
|
|
return; // reboot if logs aren't visible
|
|
|
|
} else {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("\nInstall from sdcard complete.\n");
|
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
|
|
|
|
|
|
|
case Device::APPLY_CACHE:
|
2011-06-24 07:14:24 +02:00
|
|
|
// Don't unmount cache at the end of this.
|
2011-10-31 17:34:15 +01:00
|
|
|
status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
|
2011-10-19 19:51:12 +02:00
|
|
|
if (status == INSTALL_SUCCESS && wipe_cache) {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("\n-- Wiping cache (at package request)...\n");
|
2011-10-19 19:51:12 +02:00
|
|
|
if (erase_volume("/cache")) {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("Cache wipe failed.\n");
|
2011-10-19 19:51:12 +02:00
|
|
|
} else {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("Cache wipe complete.\n");
|
2011-10-19 19:51:12 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-24 07:14:24 +02:00
|
|
|
if (status >= 0) {
|
|
|
|
if (status != INSTALL_SUCCESS) {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
ui->Print("Installation aborted.\n");
|
|
|
|
} else if (!ui->IsTextVisible()) {
|
2011-06-24 07:14:24 +02:00
|
|
|
return; // reboot if logs aren't visible
|
|
|
|
} else {
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("\nInstall from cache complete.\n");
|
2011-06-24 07:14:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-01-10 00:16:13 +01:00
|
|
|
|
|
|
|
case Device::APPLY_ADB_SIDELOAD:
|
|
|
|
ensure_path_mounted(CACHE_ROOT);
|
|
|
|
status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
|
|
|
|
if (status >= 0) {
|
|
|
|
if (status != INSTALL_SUCCESS) {
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
ui->Print("Installation aborted.\n");
|
|
|
|
} else if (!ui->IsTextVisible()) {
|
|
|
|
return; // reboot if logs aren't visible
|
|
|
|
} else {
|
|
|
|
ui->Print("\nInstall from ADB complete.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-23 02:26:40 +02:00
|
|
|
static void
|
|
|
|
load_locale_from_cache() {
|
|
|
|
FILE* fp = fopen_path(LOCALE_FILE, "r");
|
|
|
|
char buffer[80];
|
|
|
|
if (fp != NULL) {
|
|
|
|
fgets(buffer, sizeof(buffer), fp);
|
|
|
|
int j = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
|
|
|
|
if (!isspace(buffer[i])) {
|
|
|
|
buffer[j++] = buffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer[j] = 0;
|
|
|
|
locale = strdup(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
int
|
2009-11-30 17:40:57 +01:00
|
|
|
main(int argc, char **argv) {
|
2009-03-04 04:28:42 +01:00
|
|
|
time_t start = time(NULL);
|
|
|
|
|
|
|
|
// If these fail, there's not really anywhere to complain...
|
|
|
|
freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
|
|
|
|
freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
|
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) {
|
|
|
|
adb_main();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-01 18:18:44 +02:00
|
|
|
printf("Starting recovery on %s", ctime(&start));
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2010-09-20 21:16:13 +02:00
|
|
|
load_volume_table();
|
2009-03-04 04:28:42 +01:00
|
|
|
get_args(&argc, &argv);
|
|
|
|
|
|
|
|
int previous_runs = 0;
|
|
|
|
const char *send_intent = NULL;
|
|
|
|
const char *update_package = NULL;
|
2012-08-23 02:26:40 +02:00
|
|
|
int wipe_data = 0, wipe_cache = 0, show_text = 0;
|
2012-04-12 20:01:22 +02:00
|
|
|
bool just_exit = false;
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
int arg;
|
|
|
|
while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
|
|
|
|
switch (arg) {
|
|
|
|
case 'p': previous_runs = atoi(optarg); break;
|
|
|
|
case 's': send_intent = optarg; break;
|
|
|
|
case 'u': update_package = optarg; break;
|
|
|
|
case 'w': wipe_data = wipe_cache = 1; break;
|
|
|
|
case 'c': wipe_cache = 1; break;
|
2012-08-23 02:26:40 +02:00
|
|
|
case 't': show_text = 1; 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;
|
2009-03-04 04:28:42 +01:00
|
|
|
case '?':
|
|
|
|
LOGE("Invalid command argument\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-23 02:26:40 +02:00
|
|
|
if (locale == NULL) {
|
|
|
|
load_locale_from_cache();
|
|
|
|
}
|
|
|
|
printf("locale is [%s]\n", locale);
|
|
|
|
|
|
|
|
Device* device = make_device();
|
|
|
|
ui = device->GetUI();
|
|
|
|
|
|
|
|
ui->Init();
|
|
|
|
ui->SetBackground(RecoveryUI::NONE);
|
|
|
|
if (show_text) ui->ShowText(true);
|
|
|
|
|
2012-02-09 20:13:23 +01:00
|
|
|
#ifdef HAVE_SELINUX
|
|
|
|
struct selinux_opt seopts[] = {
|
|
|
|
{ SELABEL_OPT_PATH, "/file_contexts" }
|
|
|
|
};
|
|
|
|
|
|
|
|
sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
|
|
|
|
|
|
|
|
if (!sehandle) {
|
|
|
|
fprintf(stderr, "Warning: No file_contexts\n");
|
2012-04-08 20:03:01 +02:00
|
|
|
ui->Print("Warning: No file_contexts\n");
|
2012-02-09 20:13:23 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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:");
|
2009-03-04 04:28:42 +01:00
|
|
|
for (arg = 0; arg < argc; arg++) {
|
2010-07-01 18:18:44 +02:00
|
|
|
printf(" \"%s\"", argv[arg]);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2010-09-22 21:01:37 +02:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
if (update_package) {
|
|
|
|
// For backwards compatibility on the cache partition only, if
|
|
|
|
// we're given an old 'root' path "CACHE:foo", change it to
|
|
|
|
// "/cache/foo".
|
|
|
|
if (strncmp(update_package, "CACHE:", 6) == 0) {
|
|
|
|
int len = strlen(update_package) + 10;
|
2011-10-28 19:33:05 +02:00
|
|
|
char* modified_path = (char*)malloc(len);
|
2010-09-22 21:01:37 +02:00
|
|
|
strlcpy(modified_path, "/cache/", len);
|
|
|
|
strlcat(modified_path, update_package+6, len);
|
|
|
|
printf("(replacing path \"%s\" with \"%s\")\n",
|
|
|
|
update_package, modified_path);
|
|
|
|
update_package = modified_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\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
|
|
|
|
|
|
|
int status = INSTALL_SUCCESS;
|
|
|
|
|
2011-01-18 22:36:58 +01:00
|
|
|
if (update_package != NULL) {
|
2011-10-19 19:51:12 +02:00
|
|
|
status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
|
|
|
|
if (status == INSTALL_SUCCESS && wipe_cache) {
|
|
|
|
if (erase_volume("/cache")) {
|
|
|
|
LOGE("Cache wipe (requested by package) failed.");
|
|
|
|
}
|
|
|
|
}
|
2011-10-29 00:13:10 +02:00
|
|
|
if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
|
2009-06-19 00:07:14 +02:00
|
|
|
} else if (wipe_data) {
|
2011-10-31 17:34:15 +01:00
|
|
|
if (device->WipeData()) status = INSTALL_ERROR;
|
2010-09-20 21:16:13 +02:00
|
|
|
if (erase_volume("/data")) status = INSTALL_ERROR;
|
|
|
|
if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
|
2011-10-29 00:13:10 +02:00
|
|
|
if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
|
2009-06-19 00:07:14 +02:00
|
|
|
} else if (wipe_cache) {
|
2010-09-20 21:16:13 +02:00
|
|
|
if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
|
2011-10-29 00:13:10 +02:00
|
|
|
if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\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);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2012-08-23 02:26:40 +02:00
|
|
|
if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
}
|
2011-10-29 00:13:10 +02:00
|
|
|
if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
|
2011-10-31 17:34:15 +01:00
|
|
|
prompt_and_wait(device);
|
2010-09-15 20:08:23 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
// Otherwise, get ready to boot the main system...
|
|
|
|
finish_recovery(send_intent);
|
2011-10-29 00:13:10 +02:00
|
|
|
ui->Print("Rebooting...\n");
|
2011-03-08 08:37:27 +01:00
|
|
|
android_reboot(ANDROID_RB_RESTART, 0, 0);
|
2009-03-04 04:28:42 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|