From 246394d25b7ef9fabe47f6892908ac235565b743 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 24 Nov 2012 11:09:46 +0100 Subject: local: Use disk_dump_raw for dumping mmc image, use linux sysfs for finding MyDocs device --- src/local.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 104 insertions(+), 12 deletions(-) (limited to 'src/local.c') diff --git a/src/local.c b/src/local.c index 1cd9c1d..35b5411 100644 --- a/src/local.c +++ b/src/local.c @@ -27,12 +27,14 @@ #include #include #include +#include #include "local.h" #include "global.h" #include "device.h" #include "image.h" #include "cal.h" +#include "disk.h" static int failed; @@ -247,34 +249,124 @@ int local_dump_image(enum image_type image, const char * file) { int fd = -1; unsigned char * addr = NULL; off_t nlen, len; + int align; + DIR * dir; + DIR * dir2; + struct dirent * dirent; + struct dirent * dirent2; + char * ptr; + int i; + char buf[1024]; + char blk[1024]; - if ( image == IMAGE_ROOTFS ) - return -1; + printf("Dump %s image to file %s...\n", image_type_to_string(image), file); - printf("Dumping %s image to file %s...\n", image_type_to_string(image), file); + if ( image == IMAGE_MMC ) { - if ( device == DEVICE_RX_51 ) { + /* Find block device in /dev/ for MyDocs (mmc device, partition 1) */ - if ( image >= sizeof(nanddump_n900)/sizeof(nanddump_n900[0]) || ! nanddump_n900[image].valid ) { - ERROR("Unsuported image type: %s", image_type_to_string(image)); + dir = opendir("/sys/class/mmc_host/"); + if ( ! dir ) { + ERROR("Cannot find MyDocs mmc device"); goto clean; } - ret = local_nanddump(file, nanddump_n900[image].mtd, nanddump_n900[image].offset, nanddump_n900[image].length); + blk[0] = 0; - } else { + while ( ( dirent = readdir(dir) ) ) { + + snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name); + + fd = open(buf, O_RDONLY); + if ( fd < 0 ) + continue; + + buf[0] = 0; + read(fd, buf, sizeof(buf)); + close(fd); + + if ( strncmp(buf, "internal", strlen("internal")) != 0 ) + continue; + + snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001", dirent->d_name, dirent->d_name); + + dir2 = opendir(buf); + if ( ! dir2 ) + continue; + + while ( ( dirent2 = readdir(dir2) ) ) { + + if ( strncmp(dirent2->d_name, "block:mmcblk", strlen("block:mmcblk")) != 0 ) + continue; + + snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/%s", dirent->d_name, dirent->d_name, dirent2->d_name); + + blk[0] = 0; + if ( readlink(buf, blk, sizeof(blk)) < 0 ) + continue; + + ptr = strrchr(blk, '/'); + if ( ! ptr ) { + blk[0] = 0; + continue; + } + + strcpy(blk, "/dev/"); + i = strlen("/dev/"); + ++ptr; + + while ( *ptr ) + blk[i++] = *(ptr++); - if ( image >= sizeof(nanddump)/sizeof(nanddump[0]) || ! nanddump[image].valid ) { - ERROR("Unsuported image type: %s", image_type_to_string(image)); + strcpy(blk+i, "p1"); + break; + + } + + closedir(dir2); + + if ( blk[0] ) + break; + + } + + closedir(dir); + + if ( ! blk[0] ) { + ERROR("Cannot find MyDocs mmc device"); goto clean; } - ret = local_nanddump(file, nanddump[image].mtd, nanddump[image].offset, nanddump[image].length); + ret = disk_dump_raw(blk, image_type_to_string(image)); + + } else { + + if ( device == DEVICE_RX_51 ) { + + if ( image >= sizeof(nanddump_n900)/sizeof(nanddump_n900[0]) || ! nanddump_n900[image].valid ) { + ERROR("Unsuported image type: %s", image_type_to_string(image)); + goto clean; + } + + ret = local_nanddump(file, nanddump_n900[image].mtd, nanddump_n900[image].offset, nanddump_n900[image].length); + + } else { + + if ( image >= sizeof(nanddump)/sizeof(nanddump[0]) || ! nanddump[image].valid ) { + ERROR("Unsuported image type: %s", image_type_to_string(image)); + goto clean; + } + + ret = local_nanddump(file, nanddump[image].mtd, nanddump[image].offset, nanddump[image].length); + + } } - if ( ret != 0 ) + if ( ret != 0 ) { + ret = -1; goto clean; + } fd = open(file, O_RDWR); if ( fd < 0 ) -- cgit v1.2.3