remove the notion of "root path"; support mixed flash types

Remove the wacky notion of "roots" and "root paths" (those things that
look like "FOO:some/path" instead of just "/foo/some/path").  Let each
device specify its own table of available partitions and how to mount
them (needed for devices that use both MTD/yaffs2 and EMMC/ext4
partitions).

(Cherrypicked from gingerbread w/slight edits.)

Change-Id: I2479ce76b13e73f1d12035c89386c3a82b3edf51
This commit is contained in:
Doug Zongker 2010-09-20 12:16:13 -07:00
parent 0f1ad110f8
commit cc8cd3f3ca
9 changed files with 346 additions and 558 deletions

View file

@ -15,8 +15,6 @@ LOCAL_SRC_FILES := \
verifier.c \
encryptedfs_provisioning.c
LOCAL_SRC_FILES += test_roots.c
LOCAL_MODULE := recovery
LOCAL_FORCE_STATIC_EXECUTABLE := true
@ -44,10 +42,13 @@ ifeq ($(TARGET_RECOVERY_UI_LIB),)
else
LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB)
endif
LOCAL_STATIC_LIBRARIES += libext4_utils libz
LOCAL_STATIC_LIBRARIES += libminzip libunz libmtdutils libmincrypt
LOCAL_STATIC_LIBRARIES += libminui libpixelflinger_static libpng libcutils
LOCAL_STATIC_LIBRARIES += libstdc++ libc
LOCAL_C_INCLUDES += system/extras/ext4_utils
include $(BUILD_EXECUTABLE)

View file

@ -23,145 +23,103 @@
#include <stdio.h>
#include <string.h>
static const char *CACHE_NAME = "CACHE:";
static const char *MISC_NAME = "MISC:";
static int get_bootloader_message_mtd(struct bootloader_message *out, const Volume* v);
static int set_bootloader_message_mtd(const struct bootloader_message *in, const Volume* v);
static int get_bootloader_message_block(struct bootloader_message *out, const Volume* v);
static int set_bootloader_message_block(const struct bootloader_message *in, const Volume* v);
#ifdef LOG_VERBOSE
static void dump_data(const char *data, int len) {
int pos;
for (pos = 0; pos < len; ) {
printf("%05x: %02x", pos, data[pos]);
for (++pos; pos < len && (pos % 24) != 0; ++pos) {
printf(" %02x", data[pos]);
}
printf("\n");
int get_bootloader_message(struct bootloader_message *out) {
Volume* v = volume_for_path("/misc");
if (strcmp(v->fs_type, "mtd") == 0) {
return get_bootloader_message_mtd(out, v);
} else if (strcmp(v->fs_type, "emmc") == 0) {
return get_bootloader_message_block(out, v);
}
}
#endif
#ifdef USE_EXT4
// Strictly speaking this doesn't have anything to do with ext4; we
// really just mean "misc is an emmc partition". We should have a
// more configurable way have describing partitions, filesystems, etc.
static const char* MISC_PARTITION =
"/dev/block/platform/sdhci-tegra.3/by-name/misc";
int get_bootloader_message(struct bootloader_message* out) {
FILE* f = fopen(MISC_PARTITION, "rb");
if (f == NULL) {
LOGE("Can't open %s\n(%s)\n", MISC_PARTITION, strerror(errno));
return -1;
}
struct bootloader_message temp;
int count = fread(&temp, sizeof(temp), 1, f);
if (count != 1) {
LOGE("Failed reading %s\n(%s)\n", MISC_PARTITION, strerror(errno));
return -1;
}
if (fclose(f) != 0) {
LOGE("Failed closing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
return -1;
}
memcpy(out, &temp, sizeof(temp));
return 0;
LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
return -1;
}
int set_bootloader_message(const struct bootloader_message* in) {
FILE* f = fopen(MISC_PARTITION, "wb");
if (f == NULL) {
LOGE("Can't open %s\n(%s)\n", MISC_PARTITION, strerror(errno));
return -1;
int set_bootloader_message(const struct bootloader_message *in) {
Volume* v = volume_for_path("/misc");
if (strcmp(v->fs_type, "mtd") == 0) {
return set_bootloader_message_mtd(in, v);
} else if (strcmp(v->fs_type, "emmc") == 0) {
return set_bootloader_message_block(in, v);
}
int count = fwrite(in, sizeof(*in), 1, f);
if (count != 1) {
LOGE("Failed writing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
return -1;
}
if (fclose(f) != 0) {
LOGE("Failed closing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
return -1;
}
return 0;
LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
return -1;
}
#else // MTD partitions
// ------------------------------
// for misc partitions on MTD
// ------------------------------
static const int MISC_PAGES = 3; // number of pages to save
static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
int get_bootloader_message(struct bootloader_message *out) {
static int get_bootloader_message_mtd(struct bootloader_message *out,
const Volume* v) {
size_t write_size;
const MtdPartition *part = get_root_mtd_partition(MISC_NAME);
mtd_scan_partitions();
const MtdPartition *part = mtd_find_partition_by_name(v->device);
if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
LOGE("Can't find %s\n", MISC_NAME);
LOGE("Can't find %s\n", v->device);
return -1;
}
MtdReadContext *read = mtd_read_partition(part);
if (read == NULL) {
LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
const ssize_t size = write_size * MISC_PAGES;
char data[size];
ssize_t r = mtd_read_data(read, data, size);
if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno));
if (r != size) LOGE("Can't read %s\n(%s)\n", v->device, strerror(errno));
mtd_read_close(read);
if (r != size) return -1;
#ifdef LOG_VERBOSE
printf("\n--- get_bootloader_message ---\n");
dump_data(data, size);
printf("\n");
#endif
memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
return 0;
}
int set_bootloader_message(const struct bootloader_message *in) {
static int set_bootloader_message_mtd(const struct bootloader_message *in,
const Volume* v) {
size_t write_size;
const MtdPartition *part = get_root_mtd_partition(MISC_NAME);
mtd_scan_partitions();
const MtdPartition *part = mtd_find_partition_by_name(v->device);
if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
LOGE("Can't find %s\n", MISC_NAME);
LOGE("Can't find %s\n", v->device);
return -1;
}
MtdReadContext *read = mtd_read_partition(part);
if (read == NULL) {
LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
ssize_t size = write_size * MISC_PAGES;
char data[size];
ssize_t r = mtd_read_data(read, data, size);
if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno));
if (r != size) LOGE("Can't read %s\n(%s)\n", v->device, strerror(errno));
mtd_read_close(read);
if (r != size) return -1;
memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
#ifdef LOG_VERBOSE
printf("\n--- set_bootloader_message ---\n");
dump_data(data, size);
printf("\n");
#endif
MtdWriteContext *write = mtd_write_partition(part);
if (write == NULL) {
LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
if (mtd_write_data(write, data, size) != size) {
LOGE("Can't write %s\n(%s)\n", MISC_NAME, strerror(errno));
LOGE("Can't write %s\n(%s)\n", v->device, strerror(errno));
mtd_write_close(write);
return -1;
}
if (mtd_write_close(write)) {
LOGE("Can't finish %s\n(%s)\n", MISC_NAME, strerror(errno));
LOGE("Can't finish %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
@ -169,4 +127,47 @@ int set_bootloader_message(const struct bootloader_message *in) {
return 0;
}
#endif
// ------------------------------------
// for misc partitions on block devices
// ------------------------------------
static int get_bootloader_message_block(struct bootloader_message *out,
const Volume* v) {
FILE* f = fopen(v->device, "rb");
if (f == NULL) {
LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
struct bootloader_message temp;
int count = fread(&temp, sizeof(temp), 1, f);
if (count != 1) {
LOGE("Failed reading %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
if (fclose(f) != 0) {
LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
memcpy(out, &temp, sizeof(temp));
return 0;
}
static int set_bootloader_message_block(const struct bootloader_message *in,
const Volume* v) {
FILE* f = fopen(v->device, "wb");
if (f == NULL) {
LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
int count = fwrite(in, sizeof(*in), 1, f);
if (count != 1) {
LOGE("Failed writing %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
if (fclose(f) != 0) {
LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno));
return -1;
}
return 0;
}

View file

@ -87,4 +87,17 @@ void ui_reset_progress();
#define STRINGIFY(x) #x
#define EXPAND(x) STRINGIFY(x)
typedef struct {
const char* mount_point; // eg. "/cache". must live in the root directory.
const char* fs_type; // "yaffs2" or "ext4" or "vfat"
const char* device; // MTD partition name if fs_type == "yaffs"
// block device if fs_type == "ext4" or "vfat"
const char* device2; // alternative device to try if fs_type
// == "ext4" or "vfat" and mounting
// 'device' fails
} Volume;
#endif // RECOVERY_COMMON_H

View file

@ -186,7 +186,7 @@ int write_encrypted_fs_boolean_property(const char *prop_name, int value) {
int read_encrypted_fs_info(encrypted_fs_info *encrypted_fs_data) {
int result;
int value;
result = ensure_root_path_mounted("DATA:");
result = ensure_path_mounted("/data");
if (result != 0) {
LOGE("Secure FS: error mounting userdata partition.");
return ENCRYPTED_FS_ERROR;
@ -221,7 +221,7 @@ int read_encrypted_fs_info(encrypted_fs_info *encrypted_fs_data) {
return ENCRYPTED_FS_ERROR;
}
result = ensure_root_path_unmounted("DATA:");
result = ensure_path_unmounted("/data");
if (result != 0) {
LOGE("Secure FS: error unmounting data partition.");
return ENCRYPTED_FS_ERROR;
@ -232,7 +232,7 @@ int read_encrypted_fs_info(encrypted_fs_info *encrypted_fs_data) {
int restore_encrypted_fs_info(encrypted_fs_info *encrypted_fs_data) {
int result;
result = ensure_root_path_mounted("DATA:");
result = ensure_path_mounted("/data");
if (result != 0) {
LOGE("Secure FS: error mounting userdata partition.");
return ENCRYPTED_FS_ERROR;
@ -273,7 +273,7 @@ int restore_encrypted_fs_info(encrypted_fs_info *encrypted_fs_data) {
return result;
}
result = ensure_root_path_unmounted("DATA:");
result = ensure_path_unmounted("/data");
if (result != 0) {
LOGE("Secure FS: error unmounting data partition.");
return ENCRYPTED_FS_ERROR;
@ -281,4 +281,3 @@ int restore_encrypted_fs_info(encrypted_fs_info *encrypted_fs_data) {
return ENCRYPTED_FS_OK;
}

View file

@ -234,26 +234,19 @@ exit:
}
int
install_package(const char *root_path)
install_package(const char *path)
{
ui_set_background(BACKGROUND_ICON_INSTALLING);
ui_print("Finding update package...\n");
ui_show_indeterminate_progress();
LOGI("Update location: %s\n", root_path);
LOGI("Update location: %s\n", path);
if (ensure_root_path_mounted(root_path) != 0) {
LOGE("Can't mount %s\n", root_path);
return INSTALL_CORRUPT;
}
char path[PATH_MAX] = "";
if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
LOGE("Bad path %s\n", root_path);
if (ensure_path_mounted(path) != 0) {
LOGE("Can't mount %s\n", path);
return INSTALL_CORRUPT;
}
ui_print("Opening update package...\n");
LOGI("Update file path: %s\n", path);
int numKeys;
RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);

View file

@ -50,12 +50,12 @@ static const struct option OPTIONS[] = {
{ NULL, 0, NULL, 0 },
};
static const char *COMMAND_FILE = "CACHE:recovery/command";
static const char *INTENT_FILE = "CACHE:recovery/intent";
static const char *LOG_FILE = "CACHE:recovery/log";
static const char *SDCARD_ROOT = "SDCARD:";
static const char *COMMAND_FILE = "/cache/recovery/command";
static const char *INTENT_FILE = "/cache/recovery/intent";
static const char *LOG_FILE = "/cache/recovery/log";
static const char *SDCARD_ROOT = "/sdcard";
static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
static const char *SIDELOAD_TEMP_DIR = "TMP:sideload";
static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
/*
* The recovery tool communicates with the main system through /cache files.
@ -65,7 +65,7 @@ static const char *SIDELOAD_TEMP_DIR = "TMP:sideload";
*
* The arguments which may be supplied in the recovery.command file:
* --send_intent=anystring - write the text out to recovery.intent
* --update_package=root:path - verify install an OTA package file
* --update_package=path - verify install an OTA package file
* --wipe_data - erase user data (and cache), then reboot
* --wipe_cache - wipe cache (but not user data), then reboot
* --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
@ -80,8 +80,8 @@ static const char *SIDELOAD_TEMP_DIR = "TMP:sideload";
* 3. main system reboots into recovery
* 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
* -- after this, rebooting will restart the erase --
* 5. erase_root() reformats /data
* 6. erase_root() reformats /cache
* 5. erase_volume() reformats /data
* 6. erase_volume() reformats /cache
* 7. finish_recovery() erases BCB
* -- after this, rebooting will restart the main system --
* 8. main() calls reboot() to boot main system
@ -109,7 +109,7 @@ static const char *SIDELOAD_TEMP_DIR = "TMP:sideload";
* 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 --
* 8f. erase_root() reformats /cache
* 8f. erase_volume() reformats /cache
* 8g. finish_recovery() erases BCB
* -- after this, rebooting will (try to) restart the main system --
* 9. main() calls reboot() to boot main system
@ -125,8 +125,8 @@ static const char *SIDELOAD_TEMP_DIR = "TMP:sideload";
* 5. read_encrypted_fs_info() retrieves encrypted file systems settings from /data
* Settings include: property to specify the Encrypted FS istatus and
* FS encryption key if enabled (not yet implemented)
* 6. erase_root() reformats /data
* 7. erase_root() reformats /cache
* 6. erase_volume() reformats /data
* 7. erase_volume() reformats /cache
* 8. restore_encrypted_fs_info() writes required encrypted file systems settings to /data
* Settings include: property to specify the Encrypted FS status and
* FS encryption key if enabled (not yet implemented)
@ -138,17 +138,11 @@ static const char *SIDELOAD_TEMP_DIR = "TMP:sideload";
static const int MAX_ARG_LENGTH = 4096;
static const int MAX_ARGS = 100;
// open a file given in root:path format, mounting partitions as necessary
// open a given path, mounting partitions as necessary
static FILE*
fopen_root_path(const char *root_path, const char *mode) {
if (ensure_root_path_mounted(root_path) != 0) {
LOGE("Can't mount %s\n", root_path);
return NULL;
}
char path[PATH_MAX] = "";
if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
LOGE("Bad path %s\n", root_path);
fopen_path(const char *path, const char *mode) {
if (ensure_path_mounted(path) != 0) {
LOGE("Can't mount %s\n", path);
return NULL;
}
@ -205,7 +199,7 @@ get_args(int *argc, char ***argv) {
// --- if that doesn't work, try the command file
if (*argc <= 1) {
FILE *fp = fopen_root_path(COMMAND_FILE, "r");
FILE *fp = fopen_path(COMMAND_FILE, "r");
if (fp != NULL) {
char *argv0 = (*argv)[0];
*argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
@ -251,7 +245,7 @@ static void
finish_recovery(const char *send_intent) {
// By this point, we're ready to return to the main system...
if (send_intent != NULL) {
FILE *fp = fopen_root_path(INTENT_FILE, "w");
FILE *fp = fopen_path(INTENT_FILE, "w");
if (fp == NULL) {
LOGE("Can't open %s\n", INTENT_FILE);
} else {
@ -261,7 +255,7 @@ finish_recovery(const char *send_intent) {
}
// Copy logs to cache so the system can find out what happened.
FILE *log = fopen_root_path(LOG_FILE, "a");
FILE *log = fopen_path(LOG_FILE, "a");
if (log == NULL) {
LOGE("Can't open %s\n", LOG_FILE);
} else {
@ -285,10 +279,8 @@ finish_recovery(const char *send_intent) {
set_bootloader_message(&boot);
// Remove the command file, so recovery won't repeat indefinitely.
char path[PATH_MAX] = "";
if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
(unlink(path) && errno != ENOENT)) {
if (ensure_path_mounted(COMMAND_FILE) != 0 ||
(unlink(COMMAND_FILE) && errno != ENOENT)) {
LOGW("Can't unlink %s\n", COMMAND_FILE);
}
@ -296,64 +288,54 @@ finish_recovery(const char *send_intent) {
}
static int
erase_root(const char *root) {
erase_volume(const char *volume) {
ui_set_background(BACKGROUND_ICON_INSTALLING);
ui_show_indeterminate_progress();
ui_print("Formatting %s...\n", root);
return format_root_device(root);
ui_print("Formatting %s...\n", volume);
return format_volume(volume);
}
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);
copy_sideloaded_package(const char* original_path) {
if (ensure_path_mounted(original_path) != 0) {
LOGE("Can't mount %s\n", original_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) {
if (ensure_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 (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
if (errno != EEXIST) {
LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
return NULL;
}
}
// verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
// directory, owned by root, readable and writable only by root.
struct stat st;
if (stat(copy_path, &st) != 0) {
LOGE("failed to stat %s (%s)\n", copy_path, strerror(errno));
if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
return NULL;
}
if (!S_ISDIR(st.st_mode)) {
LOGE("%s isn't a directory\n", copy_path);
LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
return NULL;
}
if ((st.st_mode & 0777) != 0700) {
LOGE("%s has perms %o\n", copy_path, st.st_mode);
LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
return NULL;
}
if (st.st_uid != 0) {
LOGE("%s owned by %lu; not root\n", copy_path, st.st_uid);
LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
return NULL;
}
char copy_path[PATH_MAX];
strcpy(copy_path, SIDELOAD_TEMP_DIR);
strcat(copy_path, "/package.zip");
char* buffer = malloc(BUFSIZ);
@ -400,10 +382,7 @@ copy_sideloaded_package(const char* original_root_path) {
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;
return strdup(copy_path);
}
static char**
@ -476,15 +455,14 @@ static int compare_string(const void* a, const void* b) {
}
static int
sdcard_directory(const char* root_path) {
sdcard_directory(const char* path) {
const char* MENU_HEADERS[] = { "Choose a package to install:",
root_path,
path,
"",
NULL };
DIR* d;
struct dirent* de;
char path[PATH_MAX];
d = opendir(translate_root_path(root_path, path, sizeof(path)));
d = opendir(path);
if (d == NULL) {
LOGE("error opening %s: %s\n", path, strerror(errno));
return 0;
@ -557,20 +535,28 @@ sdcard_directory(const char* root_path) {
} else if (item[item_len-1] == '/') {
// recurse down into a subdirectory
char new_path[PATH_MAX];
strlcpy(new_path, root_path, PATH_MAX);
strlcpy(new_path, path, PATH_MAX);
strlcat(new_path, "/", PATH_MAX);
strlcat(new_path, item, PATH_MAX);
new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
result = sdcard_directory(new_path);
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];
strlcpy(new_path, root_path, PATH_MAX);
strlcpy(new_path, path, PATH_MAX);
strlcat(new_path, item, PATH_MAX);
ui_print("\n-- Install %s ...\n", new_path);
ui_print("\n-- Install %s ...\n", path);
set_sdcard_update_bootloader_message();
result = install_package(new_path);
char* copy = copy_sideloaded_package(new_path);
if (copy) {
result = install_package(copy);
free(copy);
} else {
result = INSTALL_ERROR;
}
break;
}
} while (true);
@ -617,8 +603,8 @@ wipe_data(int confirm) {
ui_print("\n-- Wiping data...\n");
device_wipe_data();
erase_root("DATA:");
erase_root("CACHE:");
erase_volume("/data");
erase_volume("/cache");
ui_print("Data wipe complete.\n");
}
@ -648,7 +634,7 @@ prompt_and_wait() {
case ITEM_WIPE_CACHE:
ui_print("\n-- Wiping cache...\n");
erase_root("CACHE:");
erase_volume("/cache");
ui_print("Cache wipe complete.\n");
if (!ui_text_visible()) return;
break;
@ -686,6 +672,7 @@ main(int argc, char **argv) {
printf("Starting recovery on %s", ctime(&start));
ui_init();
load_volume_table();
get_args(&argc, &argv);
int previous_runs = 0;
@ -746,10 +733,10 @@ main(int argc, char **argv) {
}
if (status != INSTALL_ERROR) {
if (erase_root("DATA:")) {
if (erase_volume("/data")) {
ui_print("Data wipe failed.\n");
status = INSTALL_ERROR;
} else if (erase_root("CACHE:")) {
} else if (erase_volume("/cache")) {
ui_print("Cache wipe failed.\n");
status = INSTALL_ERROR;
} else if ((encrypted_fs_data.mode == MODE_ENCRYPTED_FS_ENABLED) &&
@ -766,11 +753,11 @@ main(int argc, char **argv) {
if (status != INSTALL_SUCCESS) ui_print("Installation aborted.\n");
} else if (wipe_data) {
if (device_wipe_data()) status = INSTALL_ERROR;
if (erase_root("DATA:")) status = INSTALL_ERROR;
if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
if (erase_volume("/data")) status = INSTALL_ERROR;
if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
if (status != INSTALL_SUCCESS) ui_print("Data wipe failed.\n");
} else if (wipe_cache) {
if (wipe_cache && erase_root("CACHE:")) status = INSTALL_ERROR;
if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
if (status != INSTALL_SUCCESS) ui_print("Cache wipe failed.\n");
} else {
status = INSTALL_ERROR; // No command specified
@ -780,7 +767,7 @@ main(int argc, char **argv) {
if (status != INSTALL_SUCCESS || ui_text_visible()) {
// Mount the sdcard when the menu is enabled so you can "adb
// push" packages to the sdcard and immediately install them.
ensure_root_path_mounted(SDCARD_ROOT);
ensure_path_mounted(SDCARD_ROOT);
prompt_and_wait();
}

437
roots.c
View file

@ -20,330 +20,213 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include "mtdutils/mtdutils.h"
#include "mtdutils/mounts.h"
#ifdef USE_EXT4
#include "make_ext4fs.h"
#endif
#include "minzip/Zip.h"
#include "roots.h"
#include "common.h"
#include "make_ext4fs.h"
typedef struct {
const char *name;
const char *device;
const char *device2; // If the first one doesn't work (may be NULL)
const char *partition_name;
const char *mount_point;
const char *filesystem;
} RootInfo;
static int num_volumes = 0;
static Volume* device_volumes = NULL;
/* Canonical pointers.
xxx may just want to use enums
*/
static const char g_mtd_device[] = "@\0g_mtd_device";
static const char g_raw[] = "@\0g_raw";
static const char g_package_file[] = "@\0g_package_file";
static const char g_ramdisk[] = "@\0g_ramdisk";
void load_volume_table() {
int alloc = 2;
device_volumes = malloc(alloc * sizeof(Volume));
static RootInfo g_roots[] = {
{ "SDCARD:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" },
{ "TMP:", NULL, NULL, NULL, "/tmp", g_ramdisk },
#ifdef USE_EXT4
{ "CACHE:", "/dev/block/platform/sdhci-tegra.3/by-name/cache", NULL, NULL,
"/cache", "ext4" },
{ "DATA:", "/dev/block/platform/sdhci-tegra.3/by-name/userdata", NULL, NULL,
"/data", "ext4" },
{ "EXT:", "/dev/block/sda1", NULL, NULL, "/sdcard", "vfat" },
#else
{ "CACHE:", g_mtd_device, NULL, "cache", "/cache", "yaffs2" },
{ "DATA:", g_mtd_device, NULL, "userdata", "/data", "yaffs2" },
{ "EXT:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" },
{ "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw },
#endif
};
#define NUM_ROOTS (sizeof(g_roots) / sizeof(g_roots[0]))
static const RootInfo *
get_root_info_for_path(const char *root_path)
{
const char *c;
/* Find the first colon.
*/
c = root_path;
while (*c != '\0' && *c != ':') {
c++;
FILE* fstab = fopen("/etc/recovery.fstab", "r");
if (fstab == NULL) {
LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
return;
}
if (*c == '\0') {
return NULL;
char buffer[1024];
int i;
while (fgets(buffer, sizeof(buffer)-1, fstab)) {
for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
if (buffer[i] == '\0' || buffer[i] == '#') continue;
char* original = strdup(buffer);
char* mount_point = strtok(buffer+i, " \t\n");
char* fs_type = strtok(NULL, " \t\n");
char* device = strtok(NULL, " \t\n");
// lines may optionally have a second device, to use if
// mounting the first one fails.
char* device2 = strtok(NULL, " \t\n");
if (mount_point && fs_type && device) {
while (num_volumes >= alloc) {
alloc *= 2;
device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
}
device_volumes[num_volumes].mount_point = strdup(mount_point);
device_volumes[num_volumes].fs_type = strdup(fs_type);
device_volumes[num_volumes].device = strdup(device);
device_volumes[num_volumes].device2 =
device2 ? strdup(device2) : NULL;
++num_volumes;
} else {
LOGE("skipping malformed recovery.fstab line: %s\n", original);
}
free(original);
}
size_t len = c - root_path + 1;
size_t i;
for (i = 0; i < NUM_ROOTS; i++) {
RootInfo *info = &g_roots[i];
if (strncmp(info->name, root_path, len) == 0) {
return info;
fclose(fstab);
printf("recovery filesystem table\n");
printf("=========================\n");
for (i = 0; i < num_volumes; ++i) {
Volume* v = &device_volumes[i];
printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type,
v->device, v->device2);
}
printf("\n");
}
Volume* volume_for_path(const char* path) {
int i;
for (i = 0; i < num_volumes; ++i) {
Volume* v = device_volumes+i;
int len = strlen(v->mount_point);
if (strncmp(path, v->mount_point, len) == 0 &&
(path[len] == '\0' || path[len] == '/')) {
return v;
}
}
return NULL;
}
/* Takes a string like "SYSTEM:lib" and turns it into a string
* like "/system/lib". The translated path is put in out_buf,
* and out_buf is returned if the translation succeeded.
*/
const char *
translate_root_path(const char *root_path, char *out_buf, size_t out_buf_len)
{
if (out_buf_len < 1) {
return NULL;
}
const RootInfo *info = get_root_info_for_path(root_path);
if (info == NULL || info->mount_point == NULL) {
return NULL;
}
/* Find the relative part of the non-root part of the path.
*/
root_path += strlen(info->name); // strip off the "root:"
while (*root_path != '\0' && *root_path == '/') {
root_path++;
}
size_t mp_len = strlen(info->mount_point);
size_t rp_len = strlen(root_path);
if (mp_len + 1 + rp_len + 1 > out_buf_len) {
return NULL;
}
/* Glue the mount point to the relative part of the path.
*/
memcpy(out_buf, info->mount_point, mp_len);
if (out_buf[mp_len - 1] != '/') out_buf[mp_len++] = '/';
memcpy(out_buf + mp_len, root_path, rp_len);
out_buf[mp_len + rp_len] = '\0';
return out_buf;
}
static int
internal_root_mounted(const RootInfo *info)
{
if (info->mount_point == NULL) {
int ensure_path_mounted(const char* path) {
Volume* v = volume_for_path(path);
if (v == NULL) {
LOGE("unknown volume for path [%s]\n", path);
return -1;
}
if (info->filesystem == g_ramdisk) {
return 0;
int result;
result = scan_mounted_volumes();
if (result < 0) {
LOGE("failed to scan mounted volumes\n");
return -1;
}
/* See if this root is already mounted.
*/
int ret = scan_mounted_volumes();
if (ret < 0) {
return ret;
}
const MountedVolume *volume;
volume = find_mounted_volume_by_mount_point(info->mount_point);
if (volume != NULL) {
/* It's already mounted.
*/
const MountedVolume* mv =
find_mounted_volume_by_mount_point(v->mount_point);
if (mv) {
// volume is already mounted
return 0;
}
mkdir(v->mount_point, 0755); // in case it doesn't already exist
if (strcmp(v->fs_type, "yaffs2") == 0) {
// mount an MTD partition as a YAFFS2 filesystem.
mtd_scan_partitions();
const MtdPartition* partition;
partition = mtd_find_partition_by_name(v->device);
if (partition == NULL) {
LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
v->device, v->mount_point);
return -1;
}
return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
} else if (strcmp(v->fs_type, "ext4") == 0 ||
strcmp(v->fs_type, "vfat") == 0) {
result = mount(v->device, v->mount_point, v->fs_type,
MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
if (result == 0) return 0;
if (v->device2) {
LOGW("failed to mount %s (%s); trying %s\n",
v->device, strerror(errno), v->device2);
result = mount(v->device2, v->mount_point, v->fs_type,
MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
if (result == 0) return 0;
}
LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
return -1;
}
LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
return -1;
}
int
is_root_path_mounted(const char *root_path)
{
const RootInfo *info = get_root_info_for_path(root_path);
if (info == NULL) {
return -1;
}
return internal_root_mounted(info) >= 0;
}
int
ensure_root_path_mounted(const char *root_path)
{
const RootInfo *info = get_root_info_for_path(root_path);
if (info == NULL) {
int ensure_path_unmounted(const char* path) {
Volume* v = volume_for_path(path);
if (v == NULL) {
LOGE("unknown volume for path [%s]\n", path);
return -1;
}
int ret = internal_root_mounted(info);
if (ret >= 0) {
/* It's already mounted.
*/
int result;
result = scan_mounted_volumes();
if (result < 0) {
LOGE("failed to scan mounted volumes\n");
return -1;
}
const MountedVolume* mv =
find_mounted_volume_by_mount_point(v->mount_point);
if (mv == NULL) {
// volume is already unmounted
return 0;
}
/* It's not mounted.
*/
if (info->device == g_mtd_device) {
if (info->partition_name == NULL) {
return -1;
}
//TODO: make the mtd stuff scan once when it needs to
return unmount_mounted_volume(mv);
}
int format_volume(const char* volume) {
Volume* v = volume_for_path(volume);
if (v == NULL) {
LOGE("unknown volume \"%s\"\n", volume);
return -1;
}
if (strcmp(v->mount_point, volume) != 0) {
LOGE("can't give path \"%s\" to format_volume\n", volume);
return -1;
}
if (ensure_path_unmounted(volume) != 0) {
LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
return -1;
}
if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
mtd_scan_partitions();
const MtdPartition *partition;
partition = mtd_find_partition_by_name(info->partition_name);
const MtdPartition* partition = mtd_find_partition_by_name(v->device);
if (partition == NULL) {
LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
return -1;
}
return mtd_mount_partition(partition, info->mount_point,
info->filesystem, 0);
}
if (info->device == NULL || info->mount_point == NULL ||
info->filesystem == NULL ||
info->filesystem == g_raw ||
info->filesystem == g_package_file) {
return -1;
}
mkdir(info->mount_point, 0755); // in case it doesn't already exist
if (mount(info->device, info->mount_point, info->filesystem,
MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
if (info->device2 == NULL) {
LOGE("Can't mount %s\n(%s)\n", info->device, strerror(errno));
MtdWriteContext *write = mtd_write_partition(partition);
if (write == NULL) {
LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
return -1;
} else if (mount(info->device2, info->mount_point, info->filesystem,
MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
LOGE("Can't mount %s (or %s)\n(%s)\n",
info->device, info->device2, strerror(errno));
} else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
mtd_write_close(write);
return -1;
} else if (mtd_write_close(write)) {
LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
return -1;
}
}
return 0;
}
int
ensure_root_path_unmounted(const char *root_path)
{
const RootInfo *info = get_root_info_for_path(root_path);
if (info == NULL) {
return -1;
}
if (info->mount_point == NULL) {
/* This root can't be mounted, so by definition it isn't.
*/
return 0;
}
//xxx if TMP: (or similar) just return error
/* See if this root is already mounted.
*/
int ret = scan_mounted_volumes();
if (ret < 0) {
return ret;
}
const MountedVolume *volume;
volume = find_mounted_volume_by_mount_point(info->mount_point);
if (volume == NULL) {
/* It's not mounted.
*/
return 0;
}
return unmount_mounted_volume(volume);
}
const MtdPartition *
get_root_mtd_partition(const char *root_path)
{
const RootInfo *info = get_root_info_for_path(root_path);
if (info == NULL || info->device != g_mtd_device ||
info->partition_name == NULL)
{
return NULL;
}
mtd_scan_partitions();
return mtd_find_partition_by_name(info->partition_name);
}
int
format_root_device(const char *root)
{
/* Be a little safer here; require that "root" is just
* a device with no relative path after it.
*/
const char *c = root;
while (*c != '\0' && *c != ':') {
c++;
}
if (c[0] != ':' || c[1] != '\0') {
LOGW("format_root_device: bad root name \"%s\"\n", root);
return -1;
}
const RootInfo *info = get_root_info_for_path(root);
if (info == NULL || info->device == NULL) {
LOGW("format_root_device: can't resolve \"%s\"\n", root);
return -1;
}
if (info->mount_point != NULL) {
/* Don't try to format a mounted device.
*/
int ret = ensure_root_path_unmounted(root);
if (ret < 0) {
LOGW("format_root_device: can't unmount \"%s\"\n", root);
return ret;
}
}
/* Format the device.
*/
if (info->device == g_mtd_device) {
mtd_scan_partitions();
const MtdPartition *partition;
partition = mtd_find_partition_by_name(info->partition_name);
if (partition == NULL) {
LOGW("format_root_device: can't find mtd partition \"%s\"\n",
info->partition_name);
return -1;
}
if (info->filesystem == g_raw || !strcmp(info->filesystem, "yaffs2")) {
MtdWriteContext *write = mtd_write_partition(partition);
if (write == NULL) {
LOGW("format_root_device: can't open \"%s\"\n", root);
return -1;
} else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
LOGW("format_root_device: can't erase \"%s\"\n", root);
mtd_write_close(write);
return -1;
} else if (mtd_write_close(write)) {
LOGW("format_root_device: can't close \"%s\"\n", root);
return -1;
} else {
return 0;
}
}
}
#ifdef USE_EXT4
if (strcmp(info->filesystem, "ext4") == 0) {
LOGW("starting to reformat ext4\n");
if (strcmp(v->fs_type, "ext4") == 0) {
reset_ext4fs_info();
int result = make_ext4fs(info->device, NULL, NULL, 0, 0, 0);
LOGW("finished reformat ext4: result = %d\n", result);
int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
if (result != 0) {
LOGW("make_ext4fs failed: %d\n", result);
LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
return -1;
}
return 0;
}
#endif
//TODO: handle other device types (sdcard, etc.)
LOGW("format_root_device: unknown device \"%s\"\n", root);
LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
return -1;
}

38
roots.h
View file

@ -17,33 +17,25 @@
#ifndef RECOVERY_ROOTS_H_
#define RECOVERY_ROOTS_H_
#include "minzip/Zip.h"
#include "mtdutils/mtdutils.h"
#include "common.h"
/* Any of the "root_path" arguments can be paths with relative
* components, like "SYSTEM:a/b/c".
*/
// Load and parse volume data from /etc/recovery.fstab.
void load_volume_table();
/* Takes a string like "SYSTEM:lib" and turns it into a string
* like "/system/lib". The translated path is put in out_buf,
* and out_buf is returned if the translation succeeded.
*/
const char *translate_root_path(const char *root_path,
char *out_buf, size_t out_buf_len);
// Return the Volume* record for this path (or NULL).
Volume* volume_for_path(const char* path);
/* Returns negative on error, positive if it's mounted, zero if it isn't.
*/
int is_root_path_mounted(const char *root_path);
// Make sure that the volume 'path' is on is mounted. Returns 0 on
// success (volume is mounted).
int ensure_path_mounted(const char* path);
int ensure_root_path_mounted(const char *root_path);
// Make sure that the volume 'path' is on is mounted. Returns 0 on
// success (volume is unmounted);
int ensure_path_unmounted(const char* path);
int ensure_root_path_unmounted(const char *root_path);
const MtdPartition *get_root_mtd_partition(const char *root_path);
/* "root" must be the exact name of the root; no relative path is permitted.
* If the named root is mounted, this will attempt to unmount it first.
*/
int format_root_device(const char *root);
// Reformat the given volume (must be the mount point only, eg
// "/cache"), no paths permitted. Attempts to unmount the volume if
// it is mounted.
int format_volume(const char* volume);
#endif // RECOVERY_ROOTS_H_

View file

@ -1,81 +0,0 @@
/*
* 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 <sys/stat.h>
#include "roots.h"
#include "common.h"
#define CANARY_FILE "/system/build.prop"
#define CANARY_FILE_ROOT_PATH "SYSTEM:build.prop"
int
file_exists(const char *path)
{
struct stat st;
int ret;
ret = stat(path, &st);
if (ret == 0) {
return S_ISREG(st.st_mode);
}
return 0;
}
int
test_roots()
{
int ret;
/* Make sure that /system isn't mounted yet.
*/
if (file_exists(CANARY_FILE)) return -__LINE__;
if (is_root_path_mounted(CANARY_FILE_ROOT_PATH)) return -__LINE__;
/* Try to mount the root.
*/
ret = ensure_root_path_mounted(CANARY_FILE_ROOT_PATH);
if (ret < 0) return -__LINE__;
/* Make sure we can see the file now and that we know the root is mounted.
*/
if (!file_exists(CANARY_FILE)) return -__LINE__;
if (!is_root_path_mounted(CANARY_FILE_ROOT_PATH)) return -__LINE__;
/* Make sure that the root path corresponds to the regular path.
*/
struct stat st1, st2;
char buf[128];
const char *path = translate_root_path(CANARY_FILE_ROOT_PATH,
buf, sizeof(buf));
if (path == NULL) return -__LINE__;
ret = stat(CANARY_FILE, &st1);
if (ret != 0) return -__LINE__;
ret = stat(path, &st2);
if (ret != 0) return -__LINE__;
if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) return -__LINE__;
/* Try to unmount the root.
*/
ret = ensure_root_path_unmounted(CANARY_FILE_ROOT_PATH);
if (ret < 0) return -__LINE__;
/* Make sure that we can't see the file anymore and that
* we don't think the root is mounted.
*/
if (file_exists(CANARY_FILE)) return -__LINE__;
if (is_root_path_mounted(CANARY_FILE_ROOT_PATH)) return -__LINE__;
return 0;
}