summaryrefslogtreecommitdiffstats
path: root/recovery.c
diff options
context:
space:
mode:
Diffstat (limited to 'recovery.c')
-rw-r--r--recovery.c162
1 files changed, 157 insertions, 5 deletions
diff --git a/recovery.c b/recovery.c
index 087258fe8..04bf657d5 100644
--- a/recovery.c
+++ b/recovery.c
@@ -24,6 +24,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/reboot.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
@@ -36,12 +37,14 @@
#include "minzip/DirUtil.h"
#include "roots.h"
#include "recovery_ui.h"
+#include "encryptedfs_provisioning.h"
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' },
+ { "set_encrypted_filesystems", required_argument, NULL, 'e' },
{ NULL, 0, NULL, 0 },
};
@@ -50,6 +53,7 @@ static const char *INTENT_FILE = "CACHE:recovery/intent";
static const char *LOG_FILE = "CACHE:recovery/log";
static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
+static const char *SIDELOAD_TEMP_DIR = "TMP:sideload";
/*
* The recovery tool communicates with the main system through /cache files.
@@ -108,9 +112,9 @@ static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
* -- after this, rebooting will (try to) restart the main system --
* 9. main() calls reboot() to boot main system
*
- * ENCRYPTED FILE SYSTEMS ENABLE/DISABLE
+ * SECURE FILE SYSTEMS ENABLE/DISABLE
* 1. user selects "enable encrypted file systems"
- * 2. main system writes "--set_encrypted_filesystem=on|off" to
+ * 2. main system writes "--set_encrypted_filesystems=on|off" to
* /cache/recovery/command
* 3. main system reboots into recovery
* 4. get_args() writes BCB with "boot-recovery" and
@@ -297,6 +301,109 @@ erase_root(const char *root) {
return format_root_device(root);
}
+static char*
+copy_sideloaded_package(const char* original_root_path) {
+ if (ensure_root_path_mounted(original_root_path) != 0) {
+ LOGE("Can't mount %s\n", original_root_path);
+ return NULL;
+ }
+
+ char original_path[PATH_MAX] = "";
+ if (translate_root_path(original_root_path, original_path,
+ sizeof(original_path)) == NULL) {
+ LOGE("Bad path %s\n", original_root_path);
+ return NULL;
+ }
+
+ if (ensure_root_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
+ LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
+ return NULL;
+ }
+
+ char copy_path[PATH_MAX] = "";
+ if (translate_root_path(SIDELOAD_TEMP_DIR, copy_path,
+ sizeof(copy_path)) == NULL) {
+ LOGE("Bad path %s\n", SIDELOAD_TEMP_DIR);
+ return NULL;
+ }
+
+ if (mkdir(copy_path, 0700) != 0) {
+ if (errno != EEXIST) {
+ LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
+ return NULL;
+ }
+ }
+
+ struct stat st;
+ if (stat(copy_path, &st) != 0) {
+ LOGE("failed to stat %s (%s)\n", copy_path, strerror(errno));
+ return NULL;
+ }
+ if (!S_ISDIR(st.st_mode)) {
+ LOGE("%s isn't a directory\n", copy_path);
+ return NULL;
+ }
+ if ((st.st_mode & 0777) != 0700) {
+ LOGE("%s has perms %o\n", copy_path, st.st_mode);
+ return NULL;
+ }
+ if (st.st_uid != 0) {
+ LOGE("%s owned by %lu; not root\n", copy_path, st.st_uid);
+ return NULL;
+ }
+
+ strcat(copy_path, "/package.zip");
+
+ char* buffer = malloc(BUFSIZ);
+ 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;
+ }
+
+ char* copy_root_path = malloc(strlen(SIDELOAD_TEMP_DIR) + 20);
+ strcpy(copy_root_path, SIDELOAD_TEMP_DIR);
+ strcat(copy_root_path, "/package.zip");
+ return copy_root_path;
+}
+
static char**
prepend_title(char** headers) {
char* title[] = { "Android system recovery <"
@@ -433,8 +540,13 @@ prompt_and_wait() {
case ITEM_APPLY_SDCARD:
ui_print("\n-- Install from sdcard...\n");
- set_sdcard_update_bootloader_message();
- int status = install_package(SDCARD_PACKAGE_FILE);
+ int status = INSTALL_CORRUPT;
+ char* copy = copy_sideloaded_package(SDCARD_PACKAGE_FILE);
+ if (copy != NULL) {
+ set_sdcard_update_bootloader_message();
+ status = install_package(copy);
+ free(copy);
+ }
if (status != INSTALL_SUCCESS) {
ui_set_background(BACKGROUND_ICON_ERROR);
ui_print("Installation aborted.\n");
@@ -468,7 +580,10 @@ main(int argc, char **argv) {
int previous_runs = 0;
const char *send_intent = NULL;
const char *update_package = NULL;
+ const char *encrypted_fs_mode = NULL;
int wipe_data = 0, wipe_cache = 0;
+ int toggle_secure_fs = 0;
+ encrypted_fs_info encrypted_fs_data;
int arg;
while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
@@ -478,6 +593,7 @@ main(int argc, char **argv) {
case 'u': update_package = optarg; break;
case 'w': wipe_data = wipe_cache = 1; break;
case 'c': wipe_cache = 1; break;
+ case 'e': encrypted_fs_mode = optarg; toggle_secure_fs = 1; break;
case '?':
LOGE("Invalid command argument\n");
continue;
@@ -497,7 +613,43 @@ main(int argc, char **argv) {
int status = INSTALL_SUCCESS;
- if (update_package != NULL) {
+ if (toggle_secure_fs) {
+ if (strcmp(encrypted_fs_mode,"on") == 0) {
+ encrypted_fs_data.mode = MODE_ENCRYPTED_FS_ENABLED;
+ ui_print("Enabling Encrypted FS.\n");
+ } else if (strcmp(encrypted_fs_mode,"off") == 0) {
+ encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
+ ui_print("Disabling Encrypted FS.\n");
+ } else {
+ ui_print("Error: invalid Encrypted FS setting.\n");
+ status = INSTALL_ERROR;
+ }
+
+ // Recovery strategy: if the data partition is damaged, disable encrypted file systems.
+ // This preventsthe device recycling endlessly in recovery mode.
+ if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
+ (read_encrypted_fs_info(&encrypted_fs_data))) {
+ ui_print("Encrypted FS change aborted, resetting to disabled state.\n");
+ encrypted_fs_data.mode = MODE_ENCRYPTED_FS_DISABLED;
+ }
+
+ if (status != INSTALL_ERROR) {
+ if (erase_root("DATA:")) {
+ ui_print("Data wipe failed.\n");
+ status = INSTALL_ERROR;
+ } else if (erase_root("CACHE:")) {
+ ui_print("Cache wipe failed.\n");
+ status = INSTALL_ERROR;
+ } else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
+ (restore_encrypted_fs_info(&encrypted_fs_data))) {
+ ui_print("Encrypted FS change aborted.\n");
+ status = INSTALL_ERROR;
+ } else {
+ ui_print("Successfully updated Encrypted FS.\n");
+ status = INSTALL_SUCCESS;
+ }
+ }
+ } else if (update_package != NULL) {
status = install_package(update_package);
if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
} else if (wipe_data) {