summaryrefslogtreecommitdiffstats
path: root/src/local.c
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2014-11-21 15:02:10 +0100
committerPali Rohár <pali.rohar@gmail.com>2014-11-21 15:02:10 +0100
commite30e08136a7ce083bbaff194be77bbe380d7c9f3 (patch)
tree7606a340fc39d00a887ef7d528757b7b5812289f /src/local.c
parente00331fb42561669c198cbe3715b1dafdb53bee9 (diff)
download0xFFFF-e30e08136a7ce083bbaff194be77bbe380d7c9f3.tar.bz2
local: Use new disk functions
Diffstat (limited to 'src/local.c')
-rw-r--r--src/local.c157
1 files changed, 66 insertions, 91 deletions
diff --git a/src/local.c b/src/local.c
index c7f3ad1..08c6b8b 100644
--- a/src/local.c
+++ b/src/local.c
@@ -282,143 +282,118 @@ static struct nanddump_device nanddump[] = {
#undef NANDDUMP
-int local_dump_image(enum image_type image, const char * file) {
+static void local_find_internal_mydocs(int * maj, int * min) {
- int ret = -1;
- int fd = -1;
- unsigned char * addr = NULL;
- off_t nlen, len;
- int align;
+ int fd;
DIR * dir;
DIR * dir2;
FILE * f;
struct dirent * dirent;
struct dirent * dirent2;
- struct stat st;
- int maj, min;
char buf[1024];
- char blk[1024];
- printf("Dump %s image to file %s...\n", image_type_to_string(image), file);
+ /* Find min & maj id for block device MyDocs (mmc device, partition 1) */
- if ( image == IMAGE_MMC ) {
+ dir = opendir("/sys/class/mmc_host/");
+ if ( ! dir ) {
+ ERROR("Cannot find MyDocs mmc device: Opening '/sys/class/mmc_host/' failed");
+ return;
+ }
- maj = -1;
- min = -1;
+ while ( ( dirent = readdir(dir) ) ) {
- /* Find block device in /dev/ for MyDocs (mmc device, partition 1) */
+ if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name) <= 0 )
+ continue;
- dir = opendir("/sys/class/mmc_host/");
- if ( ! dir ) {
- ERROR("Cannot find MyDocs mmc device: Opening '/sys/class/mmc_host/' failed");
- goto clean;
- }
+ fd = open(buf, O_RDONLY);
+ if ( fd < 0 )
+ continue;
- while ( ( dirent = readdir(dir) ) ) {
+ memset(buf, 0, sizeof(buf));
+ if ( read(fd, buf, sizeof(buf)-1) < 0 )
+ buf[0] = 0;
+ close(fd);
+ fd = -1;
- if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name) <= 0 )
- continue;
+ if ( strncmp(buf, "internal", sizeof("internal")-1) != 0 )
+ continue;
- fd = open(buf, O_RDONLY);
- if ( fd < 0 )
- continue;
+ if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/", dirent->d_name, dirent->d_name) <= 0 )
+ continue;
- memset(buf, 0, sizeof(buf));
- if ( read(fd, buf, sizeof(buf)-1) < 0 )
- buf[0] = 0;
- close(fd);
+ dir2 = opendir(buf);
+ if ( ! dir2 )
+ continue;
- if ( strncmp(buf, "internal", sizeof("internal")-1) != 0 )
- continue;
+ while ( ( dirent2 = readdir(dir2) ) ) {
- if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/", dirent->d_name, dirent->d_name) <= 0 )
+ if ( strncmp(dirent2->d_name, "block:mmcblk", sizeof("block:mmcblk")-1) != 0 )
continue;
- dir2 = opendir(buf);
- if ( ! dir2 )
+ if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/%s/dev", dirent->d_name, dirent->d_name, dirent2->d_name) <= 0 )
continue;
- while ( ( dirent2 = readdir(dir2) ) ) {
-
- if ( strncmp(dirent2->d_name, "block:mmcblk", sizeof("block:mmcblk")-1) != 0 )
- continue;
-
- if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/%s/dev", dirent->d_name, dirent->d_name, dirent2->d_name) <= 0 )
- continue;
-
- f = fopen(buf, "r");
- if ( ! f )
- continue;
-
- if ( fscanf(f, "%d:%d", &maj, &min) != 2 ) {
- maj = -1;
- min = -1;
- fclose(f);
- continue;
- }
+ f = fopen(buf, "r");
+ if ( ! f )
+ continue;
+ if ( fscanf(f, "%d:%d", maj, min) != 2 ) {
+ *maj = -1;
+ *min = -1;
fclose(f);
- break;
-
+ continue;
}
- closedir(dir2);
-
- if ( maj != -1 && min != -1 )
- break;
+ fclose(f);
+ break;
}
- closedir(dir);
+ closedir(dir2);
- if ( maj == -1 || min == -1 ) {
- ERROR("Cannot find MyDocs mmc device: Slot 'internal' was not found");
- goto clean;
- }
-
- VERBOSE("Detected internal mmc device: major=%d minor=%d\n", maj, min);
+ if ( *maj != -1 && *min != -1 )
+ break;
- blk[0] = 0;
+ }
- dir = opendir("/dev/");
- if ( ! dir ) {
- ERROR("Cannot find MyDocs mmc device: Opening '/dev/' failed");
- goto clean;
- }
+ closedir(dir);
- while ( ( dirent = readdir(dir) ) ) {
+}
- if ( snprintf(buf, sizeof(buf), "/dev/%s", dirent->d_name) <= 0 )
- continue;
+int local_dump_image(enum image_type image, const char * file) {
- if ( stat(buf, &st) != 0 )
- continue;
+ int ret = -1;
+ int fd = -1;
+ unsigned char * addr = NULL;
+ off_t nlen, len;
+ int align;
+ int maj, min;
- if ( ! S_ISBLK(st.st_mode) )
- continue;
+ printf("Dump %s image to file %s...\n", image_type_to_string(image), file);
- if ( makedev(maj, min) != st.st_rdev )
- continue;
+ if ( image == IMAGE_MMC ) {
- strcpy(blk, buf);
- break;
+ maj = -1;
+ min = -1;
+ local_find_internal_mydocs(&maj, &min);
+ if ( maj == -1 || min == -1 ) {
+ ERROR("Cannot find MyDocs mmc device: Slot 'internal' was not found");
+ goto clean;
}
- closedir(dir);
+ VERBOSE("Detected internal MyDocs mmc device: major=%d minor=%d\n", maj, min);
- if ( ! blk[0] ) {
- ERROR("Cannot find MyDocs mmc device: Block device in /dev/ was not found");
+ fd = disk_open_dev(maj, min, 1, 1);
+ if ( fd < 0 ) {
+ ERROR("Cannot open MyDocs mmc device in /dev/");
goto clean;
}
- VERBOSE("Detected internal mmc device: '%s'\n", blk);
+ ret = disk_dump_dev(fd, file);
- strncat(blk, "p1", sizeof(blk)-strlen(blk)-1);
-
- printf("Using MyDocs mmc device: '%s'\n", blk);
-
- ret = disk_dump_raw(blk, file);
+ close(fd);
+ fd = -1;
} else {