From 16f0b49bea798c38366c6037934c51d864493e5e Mon Sep 17 00:00:00 2001 From: "Koushik K. Dutta" Date: Fri, 19 Mar 2010 14:37:11 -0700 Subject: [PATCH] working erase_image and options in recovery now --- Android.mk | 4 +- extendedcommands.c | 62 ++++++++++++++++++----- mtdutils/Android.mk | 23 +++------ mtdutils/{erase_flash.c => erase_image.c} | 34 ++++++++----- recovery.c | 2 + 5 files changed, 82 insertions(+), 43 deletions(-) rename mtdutils/{erase_flash.c => erase_image.c} (78%) diff --git a/Android.mk b/Android.mk index 675f47e..f0f0e81 100644 --- a/Android.mk +++ b/Android.mk @@ -42,7 +42,7 @@ ifeq ($(TARGET_RECOVERY_UI_LIB),) else LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) endif -LOCAL_STATIC_LIBRARIES += libbusybox libclearsilverregex libmkyaffs2image libunyaffs libdump_image libflash_image libmtdutils +LOCAL_STATIC_LIBRARIES += libbusybox libclearsilverregex libmkyaffs2image libunyaffs liberase_image libdump_image libflash_image libmtdutils LOCAL_STATIC_LIBRARIES += libamend LOCAL_STATIC_LIBRARIES += libminzip libunz libmtdutils libmincrypt LOCAL_STATIC_LIBRARIES += libminui libpixelflinger_static libpng libcutils @@ -50,7 +50,7 @@ LOCAL_STATIC_LIBRARIES += libstdc++ libc include $(BUILD_EXECUTABLE) -RECOVERY_LINKS := amend busybox flash_image dump_image mkyaffs2image unyaffs +RECOVERY_LINKS := amend busybox flash_image dump_image mkyaffs2image unyaffs erase_image # nc is provided by external/netcat SYMLINKS := $(addprefix $(TARGET_RECOVERY_ROOT_OUT)/sbin/,$(RECOVERY_LINKS)) $(SYMLINKS): RECOVERY_BINARY := $(LOCAL_MODULE) diff --git a/extendedcommands.c b/extendedcommands.c index 50f4306..2acc0d6 100644 --- a/extendedcommands.c +++ b/extendedcommands.c @@ -373,8 +373,33 @@ void show_mount_usb_storage_menu() __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable"); } +int confirm_format() +{ + static char* title_headers[] = { "Confirm format?", + " THIS CAN NOT BE UNDONE.", + "", + NULL, + }; + + char* items[] = { " No", + " No", + " No", + " No", + " No", + " No", + " No", + " Yes -- wipe partition", // [7] + " No", + " No", + " No", + NULL }; + + int chosen_item = get_menu_selection(title_headers, items, 0); + return chosen_item == 7; +} + #define MOUNTABLE_COUNT 4 -#define MTD_COUNT 3 +#define MTD_COUNT 4 void show_partition_menu() { @@ -384,13 +409,20 @@ void show_partition_menu() }; typedef char* string; - string mounts[MOUNTABLE_COUNT][4] = { - { "mount /system", "unmount /system", "format SYSTEM:", "SYSTEM:" }, - { "mount /data", "unmount /data", "format DATA:", "DATA:" }, - { "mount /cache", "unmount /cache", "format CACHE:", "CACHE:" }, - { "mount /sdcard", "unmount /sdcard", NULL, "SDCARD:" } + string mounts[MOUNTABLE_COUNT][3] = { + { "mount /system", "unmount /system", "SYSTEM:" }, + { "mount /data", "unmount /data", "DATA:" }, + { "mount /cache", "unmount /cache", "CACHE:" }, + { "mount /sdcard", "unmount /sdcard", "SDCARD:" } }; + string mtds[MTD_COUNT][2] = { + { "format system", "system" }, + { "format data", "data" }, + { "format cache", "cache" }, + { "format boot", "boot" } + }; + for (;;) { int ismounted[MOUNTABLE_COUNT]; @@ -398,13 +430,13 @@ void show_partition_menu() static string options[MOUNTABLE_COUNT + MTD_COUNT + 1 + 1]; // mountables, format mtds, usb storage, null for (i = 0; i < MOUNTABLE_COUNT; i++) { - ismounted[i] = is_root_path_mounted(mounts[i][3]); + ismounted[i] = is_root_path_mounted(mounts[i][2]); options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0]; } for (i = 0; i < MTD_COUNT; i++) { - options[MOUNTABLE_COUNT + i] = mounts[i][2]; + options[MOUNTABLE_COUNT + i] = mtds[i][0]; } options[MOUNTABLE_COUNT + MTD_COUNT] = "mount USB storage"; @@ -421,18 +453,24 @@ void show_partition_menu() { if (ismounted[chosen_item]) { - if (0 != ensure_root_path_unmounted(mounts[chosen_item][3])) - ui_print("Error unmounting %s!\n", mounts[chosen_item][3]); + if (0 != ensure_root_path_unmounted(mounts[chosen_item][2])) + ui_print("Error unmounting %s!\n", mounts[chosen_item][2]); } else { - if (0 != ensure_root_path_mounted(mounts[chosen_item][3])) - ui_print("Error mounting %s!\n", mounts[chosen_item][3]); + if (0 != ensure_root_path_mounted(mounts[chosen_item][2])) + ui_print("Error mounting %s!\n", mounts[chosen_item][2]); } } else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT) { chosen_item = chosen_item - MOUNTABLE_COUNT; + if (!confirm_format()) + continue; + ui_print("Formatting %s...\n", mtds[chosen_item][1]); + if (0 != erase_image(mtds[chosen_item][1])) + ui_print("Error erasing %s!\n", mtds[chosen_item][1]); + ui_print("Done.\n"); } } } diff --git a/mtdutils/Android.mk b/mtdutils/Android.mk index e83cbf3..04a579c 100644 --- a/mtdutils/Android.mk +++ b/mtdutils/Android.mk @@ -29,8 +29,8 @@ LOCAL_SHARED_LIBRARIES := libcutils libc include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) -LOCAL_SRC_FILES := erase_flash.c -LOCAL_MODULE := erase_flash +LOCAL_SRC_FILES := erase_image.c +LOCAL_MODULE := erase_image LOCAL_MODULE_TAGS := eng LOCAL_STATIC_LIBRARIES := libmtdutils LOCAL_SHARED_LIBRARIES := libcutils libc @@ -42,24 +42,17 @@ LOCAL_MODULE := libflash_image LOCAL_CFLAGS += -Dmain=flash_image_main include $(BUILD_STATIC_LIBRARY) -include $(CLEAR_VARS) -LOCAL_SRC_FILES := flash_image.c -LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES -LOCAL_MODULE := recovery_flash_image -LOCAL_MODULE_TAGS := eng -LOCAL_MODULE_STEM := flash_image -LOCAL_FORCE_STATIC_EXECUTABLE := true -LOCAL_UNSTRIPPED_PATH := $(TARGET_OUT_EXECUTABLES_UNSTRIPPED) -LOCAL_MODULE_PATH := $(TARGET_OUT)/utilities -LOCAL_STATIC_LIBRARIES := libmtdutils libcutils libc -include $(BUILD_EXECUTABLE) - - include $(CLEAR_VARS) LOCAL_SRC_FILES := dump_image.c LOCAL_MODULE := libdump_image LOCAL_CFLAGS += -Dmain=dump_image_main include $(BUILD_STATIC_LIBRARY) +include $(CLEAR_VARS) +LOCAL_SRC_FILES := erase_image.c +LOCAL_MODULE := liberase_image +LOCAL_CFLAGS += -Dmain=erase_image_main +include $(BUILD_STATIC_LIBRARY) + endif # TARGET_ARCH == arm endif # !TARGET_SIMULATOR diff --git a/mtdutils/erase_flash.c b/mtdutils/erase_image.c similarity index 78% rename from mtdutils/erase_flash.c rename to mtdutils/erase_image.c index 111e637..37623b8 100644 --- a/mtdutils/erase_flash.c +++ b/mtdutils/erase_image.c @@ -32,9 +32,9 @@ #undef LOG_TAG #endif -#define LOG_TAG "erase_flash" +#define LOG_TAG "erase_image" -void die(const char *msg, ...) { +static int die(const char *msg, ...) { int err = errno; va_list args; va_start(args, msg); @@ -49,34 +49,40 @@ void die(const char *msg, ...) { fprintf(stderr, "%s\n", buf); LOGE("%s\n", buf); - exit(1); + return 3; } -/* Erase a mtd partition */ -int main(int argc, char **argv) { +int erase_image(char* partition_name) { MtdWriteContext *out; size_t erased; size_t total_size; size_t erase_size; - if (argc != 2) { - fprintf(stderr, "usage: %s \n", argv[0]); - return 2; - } - if (mtd_scan_partitions() <= 0) die("error scanning partitions"); - const MtdPartition *partition = mtd_find_partition_by_name(argv[1]); - if (partition == NULL) die("can't find %s partition", argv[1]); + const MtdPartition *partition = mtd_find_partition_by_name(partition_name); + if (partition == NULL) return die("can't find %s partition", partition_name); out = mtd_write_partition(partition); - if (out == NULL) die("could not estabilish write context for %s", argv[1]); + if (out == NULL) return die("could not estabilish write context for %s", partition_name); // do the actual erase, -1 = full partition erase erased = mtd_erase_blocks(out, -1); // erased = bytes erased, if zero, something borked - if (!erased) die("error erasing %s", argv[1]); + if (!erased) return die("error erasing %s", partition_name); return 0; } + + +/* Erase a mtd partition */ + +int main(int argc, char **argv) { + if (argc != 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + return 2; + } + + return erase_image(argv[1]); +} diff --git a/recovery.c b/recovery.c index 85c4dc1..d1e96f5 100644 --- a/recovery.c +++ b/recovery.c @@ -480,6 +480,8 @@ main(int argc, char **argv) return flash_image_main(argc, argv); if (strstr(argv[0], "dump_image") != NULL) return dump_image_main(argc, argv); + if (strstr(argv[0], "erase_image") != NULL) + return erase_image_main(argc, argv); if (strstr(argv[0], "mkyaffs2image") != NULL) return mkyaffs2image_main(argc, argv); if (strstr(argv[0], "unyaffs") != NULL)