int cmd_mmc_erase_partition(const char *partition, const char *filesystem) { mmc_scan_partitions(); const MmcPartition *p; p = mmc_find_partition_by_name(partition); if (p == NULL) return -1; return mmc_format_ext3 (p); }
// format(type, location) // // type="MTD" location=partition Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { char* result = NULL; if (argc != 2) { return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); } char* type; char* location; if (ReadArgs(state, argv, 2, &type, &location) < 0) { return NULL; } if (strlen(type) == 0) { ErrorAbort(state, "type argument to %s() can't be empty", name); goto done; } if (strlen(location) == 0) { ErrorAbort(state, "location argument to %s() can't be empty", name); goto done; } if (strcmp(type, "MTD") == 0) { mtd_scan_partitions(); const MtdPartition* mtd = mtd_find_partition_by_name(location); if (mtd == NULL) { fprintf(stderr, "%s: no mtd partition named \"%s\"", name, location); result = strdup(""); goto done; } MtdWriteContext* ctx = mtd_write_partition(mtd); if (ctx == NULL) { fprintf(stderr, "%s: can't write \"%s\"", name, location); result = strdup(""); goto done; } if (mtd_erase_blocks(ctx, -1) == -1) { mtd_write_close(ctx); fprintf(stderr, "%s: failed to erase \"%s\"", name, location); result = strdup(""); goto done; } if (mtd_write_close(ctx) != 0) { fprintf(stderr, "%s: failed to close \"%s\"", name, location); result = strdup(""); goto done; } } else if (strcmp(type, "MMC") == 0) { mmc_scan_partitions(); const MmcPartition* mmc = mmc_find_partition_by_name(location); if (mmc == NULL) { fprintf(stderr, "%s: no mmc partition named \"%s\"", name, location); result = strdup(""); goto done; } if (mmc_format_ext3(mmc)) { result = strdup(""); goto done; } } else { fprintf(stderr, "%s: unsupported type \"%s\"", name, type); } result = location; done: free(type); if (result != location) free(location); return StringValue(result); }
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); }