Exemplo n.º 1
0
int cmd_mmc_restore_raw_partition(const char *partition, const char *filename)
{
    if (partition[0] != '/') {
        mmc_scan_partitions();
        const MmcPartition *p;
        p = mmc_find_partition_by_name(partition);
        if (p == NULL)
            return -1;
        return mmc_raw_copy(p, filename);
    }
    else {
        return mmc_raw_dump_internal(filename, partition);
    }
}
int cmd_mmc_backup_raw_partition(const char *partition, const char *filename)
{
    if (partition[0] != '/') {
        mmc_scan_partitions();
        const MmcPartition *p;
        p = mmc_find_partition_by_name(partition);
        if (p == NULL)
            return -1;
        return mmc_raw_dump(p, filename);
    }
    else 
    {
        unsigned sz = 0;

#if defined(MTK_BOOT_DEVICE_NAME) && defined(MTK_BOOT_DEVICE_SIZE)
        if (strcmp(partition, STR(MTK_BOOT_DEVICE_NAME)) == 0) {
            sz = MTK_BOOT_DEVICE_SIZE;
            printf("MTK_BOOT_DEVICE: %s; Size: 0x%x\n", partition, sz);
        }
#endif

#if defined(MTK_RECOVERY_DEVICE_NAME) && defined(MTK_RECOVERY_DEVICE_SIZE)
        if (strcmp(partition, STR(MTK_RECOVERY_DEVICE_NAME)) == 0) {
            sz = MTK_RECOVERY_DEVICE_SIZE;
            printf("MTK_RECOVERY_DEVICE: %s; Size: 0x%x\n", partition, sz);
        }
#endif

#if defined(MTK_UBOOT_DEVICE_NAME) && defined(MTK_UBOOT_DEVICE_SIZE)
        if (strcmp(partition, STR(MTK_UBOOT_DEVICE_NAME)) == 0) {
            sz = MTK_UBOOT_DEVICE_SIZE;
            printf("MTK_UBOOT_DEVICE: %s; Size: 0x%x\n", partition, sz);
        }
#endif
        
        return mmc_raw_dump_internal(partition, filename, sz);
    }
}
Exemplo n.º 3
0
// write_raw_image(file, partition)
Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
    char* result = NULL;

    char* partition;
    char* filename;
    if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
        return NULL;
    }

    if (strlen(partition) == 0) {
        ErrorAbort(state, "partition argument to %s can't be empty", name);
        goto done;
    }
    if (strlen(filename) == 0) {
        ErrorAbort(state, "file argument to %s can't be empty", name);
        goto done;
    }

#ifdef BOARD_USES_BMLUTILS
    if (0 == write_raw_image(name, filename)) {
        result = partition;
    }
    result = strdup("Failure");
#else
    mtd_scan_partitions();
    const MtdPartition* mtd = mtd_find_partition_by_name(partition);
    if (mtd == NULL) {
        fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
        result = strdup("");
        goto MMC;
    }

    MtdWriteContext* ctx = mtd_write_partition(mtd);
    if (ctx == NULL) {
        fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
                name, partition);
        result = strdup("");
        goto done;
    }

    bool success;

    FILE* f = fopen(filename, "rb");
    if (f == NULL) {
        fprintf(stderr, "%s: can't open %s: %s\n",
                name, filename, strerror(errno));
        result = strdup("");
        goto done;
    }

    success = true;
    char* buffer = malloc(BUFSIZ);
    int read;
    while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
        int wrote = mtd_write_data(ctx, buffer, read);
        success = success && (wrote == read);
        if (!success) {
            fprintf(stderr, "mtd_write_data to %s failed: %s\n",
                    partition, strerror(errno));
        }
    }
    free(buffer);
    fclose(f);

    if (mtd_erase_blocks(ctx, -1) == -1) {
        fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
    }
    if (mtd_write_close(ctx) != 0) {
        fprintf(stderr, "%s: error closing write of %s\n", name, partition);
    }

    printf("%s %s partition from %s\n",
           success ? "wrote" : "failed to write", partition, filename);

    result = success ? partition : strdup("");
    goto done;

MMC:
    mmc_scan_partitions();
    const MmcPartition* mmc = mmc_find_partition_by_name(partition);
    if (mmc == NULL) {
        fprintf(stderr, "%s: no mmc partition named \"%s\"\n", name, partition);
        result = strdup("");
        goto done;
    }
    if (mmc_raw_copy(mmc, filename)) {
        fprintf(stderr, "%s: error erasing mmc partition named \"%s\"\n", name, partition);
        result = strdup("");
        goto done;
    }
    result = partition;
#endif

done:
    if (result != partition) free(partition);
    free(filename);
    return StringValue(result);
}
Exemplo n.º 4
0
// mount(type, location, mount_point)
//
//   what:  type="MTD"   location="<partition>"            to mount a yaffs2 filesystem
//          type="vfat"  location="/dev/block/<whatever>"  to mount a device
Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
    char* result = NULL;
    if (argc != 3) {
        return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc);
    }
    char* type;
    char* location;
    char* mount_point;
    if (ReadArgs(state, argv, 3, &type, &location, &mount_point) < 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 (strlen(mount_point) == 0) {
        ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
        goto done;
    }

    mkdir(mount_point, 0755);

    if (strcmp(type, "MTD") == 0) {
        mtd_scan_partitions();
        const MtdPartition* mtd;
        mtd = mtd_find_partition_by_name(location);
        if (mtd == NULL) {
            fprintf(stderr, "%s: no mtd partition named \"%s\"",
                    name, location);
            result = strdup("");
            goto done;
        }
        if (mtd_mount_partition(mtd, mount_point, "yaffs2", 0 /* rw */) != 0) {
            fprintf(stderr, "mtd mount of %s failed: %s\n",
                    location, strerror(errno));
            result = strdup("");
            goto done;
        }
        result = mount_point;
    } else if (strcmp(type, "MMC") == 0) {
        mmc_scan_partitions();
        const MmcPartition* mmc;
        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_mount_partition(mmc, mount_point, 0 /* rw */) != 0) {
            fprintf(stderr, "mmc mount of %s failed: %s\n",
                    location, strerror(errno));
            result = strdup("");
            goto done;
        }
        result = mount_point;
    } else {
        if (mount(location, mount_point, type,
                  MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
            fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
                    name, location, mount_point, strerror(errno));
            result = strdup("");
        } else {
            result = mount_point;
        }
    }

done:
    free(type);
    free(location);
    if (result != mount_point) free(mount_point);
    return StringValue(result);
}
Exemplo n.º 5
0
// 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);
}
Exemplo n.º 6
0
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);
}