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; } } } else { return format_non_mtd_device(root); } //TODO: handle other device types (sdcard, etc.) LOGW("format_root_device: can't handle non-mtd device \"%s\"\n", root); return -1; }
void show_partition_menu() { static char* headers[] = { "Mounts and Storage Menu", "", NULL }; typedef char* string; string mounts[MOUNTABLE_COUNT][3] = { { "mount /system", "unmount /system", "SYSTEM:" }, { "mount /data", "unmount /data", "DATA:" }, { "mount /dbdata", "unmount /dbdata", "DATADATA:" }, { "mount /cache", "unmount /cache", "CACHE:" }, { "mount /sdcard", "unmount /sdcard", "SDCARD:" }, { "mount /sd-ext", "unmount /sd-ext", "SDEXT:" }, { "", "", "" }, }; string mtds[MTD_COUNT][2] = { { "format system", "SYSTEM:" }, { "format data", "DATA:" }, { "format dbdata", "DATADATA:" }, { "format cache", "CACHE:" }, { "", "" }, }; string conv_mtds[MTD_COUNT][3] = { { "convert system", "SYSTEM:", "**" }, { "convert data", "DATA:", "**" }, { "convert dbdata", "DATADATA:", "**" }, { "convert cache", "CACHE:", "**" }, { "", "", "" }, }; string mmcs[MMC_COUNT][3] = { { "format sdcard", "SDCARD:" }, { "format sd-ext", "SDEXT:" }, { "", "" }, }; static char* confirm_format = "Confirm format?"; static char* confirm = "Yes - Format"; for (;;) { int ismounted[MOUNTABLE_COUNT]; int i; static string options[MOUNTABLE_COUNT + MTD_COUNT + MTD_COUNT + MMC_COUNT + 1 + 1]; // mountables, format mtds, convet mtds, format mmcs, usb storage, null for (i = 0; i < MOUNTABLE_COUNT; i++) { 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] = mtds[i][0]; } for (i = 0; i < MTD_COUNT; i++) { options[MOUNTABLE_COUNT + MTD_COUNT + i] = conv_mtds[i][0]; } for (i = 0; i < MMC_COUNT; i++) { options[MOUNTABLE_COUNT + MTD_COUNT*2 + i] = mmcs[i][0]; } options[MOUNTABLE_COUNT + MTD_COUNT*2 + MMC_COUNT] = "mount USB storage"; options[MOUNTABLE_COUNT + MTD_COUNT*2 + MMC_COUNT + 1] = NULL; int chosen_item = get_menu_selection(headers, options, 0); if (chosen_item == GO_BACK) break; if (chosen_item == MOUNTABLE_COUNT + MTD_COUNT*2 + MMC_COUNT) { show_mount_usb_storage_menu(); } else if (chosen_item < MOUNTABLE_COUNT) { if (ismounted[chosen_item]) { 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][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_selection(confirm_format, confirm)) continue; ui_print("Formatting %s...\n", mtds[chosen_item][1]); if (0 != format_root_device(mtds[chosen_item][1])) ui_print("Error formatting %s!\n", mtds[chosen_item][1]); else ui_print("Done.\n"); } else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT*2) { chosen_item = chosen_item - MOUNTABLE_COUNT - MTD_COUNT; if (0 == convert_mtd_device(conv_mtds[chosen_item][1], conv_mtds[chosen_item][2])) { ui_print("Done.\n"); detect_root_fs(); } ui_clear_backgroud(); ui_reset_progress(); } else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT*2 + MMC_COUNT) { chosen_item = chosen_item - MOUNTABLE_COUNT - MTD_COUNT*2; if (!confirm_selection(confirm_format, confirm)) continue; ui_print("Formatting %s...\n", mmcs[chosen_item][1]); if (0 != format_non_mtd_device(mmcs[chosen_item][1])) ui_print("Error formatting %s!\n", mmcs[chosen_item][1]); else ui_print("Done.\n"); } } }
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; } } } //Handle MMC device types if (info->device == g_mmc_device) { mmc_scan_partitions(); const MmcPartition *partition; partition = mmc_find_partition_by_name(info->partition_name); if (partition == NULL) { LOGE("format_root_device: can't find mmc partition \"%s\"\n", info->partition_name); return -1; } if (!strcmp(info->filesystem, "ext3")) { if(mmc_format_ext3(partition)) LOGE("\n\"%s\" wipe failed!\n", info->partition_name); } } /* Format rfs filesystem */ if (!strcmp(info->filesystem, "rfs")) { LOGW("format_root_device: %s as rfs\n", info->device); char stl_format[32] = "stl.format "; strcat(stl_format, info->device); if (__system(stl_format) != 0) { LOGE("format_root_device: Can't run STL format [%s]\n", strerror(errno)); return -1; } return 0; } /* Format ext file system */ if (!strncmp(info->filesystem, "ext", 3)) { LOGW("format_root_device: %s as %s\n", info->device, info->filesystem); char ext_format[96]; sprintf(ext_format, "/sbin/mke2fs -T %s -F -j -q -m 0 -b 4096 %s %s", info->filesystem, (info->filesystem[3]=='2')?"":"-O ^huge_file,extent ", info->device); if (__system(ext_format) != 0) { LOGE("format_root_device: Can't run mke2fs [%s]\n", strerror(errno)); return -1; } return 0; } return format_non_mtd_device(root); }