local: Fix searching for MyDocs mmc device

Device name in /sys/block/ could not be same as in /dev/ if udev renaming it.
And this is real problem on Maemo without special kernel patch when device
booting with inserted SD card. So read major and minor numbers from /sys/
and find coresponding device in /dev/. These two numbers will be same after
renaming device too.
This commit is contained in:
Pali Rohár committed 2013-09-20 18:12:28 +02:00
1 parent 89bc4a10a3
commit 17dd99f368
1 file changed
+61 -23
+61 -23
View File
@@ -22,6 +22,7 @@
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <libgen.h> #include <libgen.h>
@@ -290,10 +291,11 @@ int local_dump_image(enum image_type image, const char * file) {
int align; int align;
DIR * dir; DIR * dir;
DIR * dir2; DIR * dir2;
FILE * f;
struct dirent * dirent; struct dirent * dirent;
struct dirent * dirent2; struct dirent * dirent2;
char * ptr; struct stat st;
int i; int maj, min;
char buf[1024]; char buf[1024];
char blk[1024]; char blk[1024];
@@ -301,16 +303,17 @@ int local_dump_image(enum image_type image, const char * file) {
if ( image == IMAGE_MMC ) { if ( image == IMAGE_MMC ) {
maj = -1;
min = -1;
/* Find block device in /dev/ for MyDocs (mmc device, partition 1) */ /* Find block device in /dev/ for MyDocs (mmc device, partition 1) */
dir = opendir("/sys/class/mmc_host/"); dir = opendir("/sys/class/mmc_host/");
if ( ! dir ) { if ( ! dir ) {
ERROR("Cannot find MyDocs mmc device"); ERROR("Cannot find MyDocs mmc device: Opening '/sys/class/mmc_host/' failed");
goto clean; goto clean;
} }
blk[0] = 0;
while ( ( dirent = readdir(dir) ) ) { while ( ( dirent = readdir(dir) ) ) {
snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name); snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name);
@@ -327,7 +330,7 @@ int local_dump_image(enum image_type image, const char * file) {
if ( strncmp(buf, "internal", strlen("internal")) != 0 ) if ( strncmp(buf, "internal", strlen("internal")) != 0 )
continue; continue;
snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001", dirent->d_name, dirent->d_name); snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/", dirent->d_name, dirent->d_name);
dir2 = opendir(buf); dir2 = opendir(buf);
if ( ! dir2 ) if ( ! dir2 )
@@ -338,44 +341,79 @@ int local_dump_image(enum image_type image, const char * file) {
if ( strncmp(dirent2->d_name, "block:mmcblk", strlen("block:mmcblk")) != 0 ) if ( strncmp(dirent2->d_name, "block:mmcblk", strlen("block:mmcblk")) != 0 )
continue; continue;
snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/%s", dirent->d_name, dirent->d_name, dirent2->d_name); snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/%s/dev", dirent->d_name, dirent->d_name, dirent2->d_name);
blk[0] = 0; f = fopen(buf, "r");
if ( readlink(buf, blk, sizeof(blk)) < 0 ) if ( ! f )
continue; continue;
ptr = strrchr(blk, '/'); if ( fscanf(f, "%d:%d", &maj, &min) != 2 ) {
if ( ! ptr ) { maj = -1;
blk[0] = 0; min = -1;
fclose(f);
continue; continue;
} }
strcpy(blk, "/dev/"); fclose(f);
i = strlen("/dev/");
++ptr;
while ( *ptr )
blk[i++] = *(ptr++);
strcpy(blk+i, "p1");
break; break;
} }
closedir(dir2); closedir(dir2);
if ( blk[0] ) if ( maj != -1 && min != -1 )
break; break;
} }
closedir(dir); closedir(dir);
if ( ! blk[0] ) { if ( maj == -1 || min == -1 ) {
ERROR("Cannot find MyDocs mmc device"); ERROR("Cannot find MyDocs mmc device: Slot 'internal' was not found");
goto clean; goto clean;
} }
VERBOSE("Detected internal mmc device: major=%d minor=%d\n", maj, min);
blk[0] = 0;
dir = opendir("/dev/");
if ( ! dir ) {
ERROR("Cannot find MyDocs mmc device: Opening '/dev/' failed");
goto clean;
}
while ( ( dirent = readdir(dir) ) ) {
snprintf(buf, sizeof(buf), "/dev/%s", dirent->d_name);
if ( stat(buf, &st) != 0 )
continue;
if ( ! S_ISBLK(st.st_mode) )
continue;
if ( makedev(maj, min) != st.st_rdev )
continue;
strcpy(blk, buf);
break;
}
closedir(dir);
if ( ! blk[0] ) {
ERROR("Cannot find MyDocs mmc device: Block device in /dev/ was not found");
goto clean;
}
VERBOSE("Detected internal mmc device: '%s'\n", blk);
strncat(blk, "p1", sizeof(blk));
printf("Using MyDocs mmc device: '%s'\n", blk);
ret = disk_dump_raw(blk, file); ret = disk_dump_raw(blk, file);
} else { } else {