diff options
Diffstat (limited to 'src/local.c')
-rw-r--r-- | src/local.c | 187 |
1 files changed, 90 insertions, 97 deletions
diff --git a/src/local.c b/src/local.c index c7f3ad1..3813910 100644 --- a/src/local.c +++ b/src/local.c @@ -53,7 +53,9 @@ static int root_device = -1; #define min(a, b) (a < b ? a : b) #define local_cal_copy(dest, from, len) strncpy(dest, from, min(len, sizeof(dest)-1)) #define local_cal_read(cal, str, ptr, len) ( cal_read_block(cal, str, &ptr, &len, 0) == 0 && ptr ) -#define local_cal_readcopy(cal, str, dest) do { void * ptr; unsigned long int len; if ( local_cal_read(cal, str, ptr, len) ) local_cal_copy(dest, ptr, len); } while ( 0 ) +#define local_cal_readcopy(cal, str, dest) do { void * ptr; unsigned long int len; if ( local_cal_read(cal, str, ptr, len) ) { local_cal_copy(dest, ptr, len); free(ptr); } } while ( 0 ) + +#if defined(__linux__) && defined(__arm__) static void local_cal_parse(void) { @@ -109,16 +111,22 @@ static void local_cal_parse(void) { } +#endif + int local_init(void) { +#if defined(__linux__) && defined(__arm__) char buf[1024]; char * ptr; char * ptr2; FILE * file; +#endif if ( failed ) return -1; +#if defined(__linux__) && defined(__arm__) + file = fopen("/proc/cpuinfo", "r"); if ( ! file ) { failed = 1; @@ -174,9 +182,12 @@ int local_init(void) { } + fclose(file); + +#endif + failed = 1; printf("Not a local device\n"); - fclose(file); return -1; } @@ -212,17 +223,17 @@ static int local_nanddump(const char * file, int mtd, int offset, int length) { free(path); if ( ret == 0 && buf.f_bsize * buf.f_bfree < (long unsigned int)length ) { - ERROR("Not enough free space (have: %lu, need: %d)", buf.f_bsize * buf.f_bfree, length); + ERROR("Not enough free space (have: %ju, need: %d)", (intmax_t)(buf.f_bsize * buf.f_bfree), length); return 1; } - size = snprintf(NULL, 0, "nanddump -i -o -b -s %d -l %d -f %s /dev/mtd%dro", offset, length, file, mtd); + size = snprintf(NULL, 0, "nanddump -o -b -s %d -l %d -f %s /dev/mtd%dro", offset, length, file, mtd); command = malloc(size+1); if ( ! command ) return 1; - snprintf(command, size+1, "nanddump -i -o -b -s %d -l %d -f %s /dev/mtd%dro", offset, length, file, mtd); + snprintf(command, size+1, "nanddump -o -b -s %d -l %d -f %s /dev/mtd%dro", offset, length, file, mtd); ret = system(command); @@ -282,143 +293,125 @@ 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; +#ifdef __linux__ + + 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 ( strncmp(dirent->d_name, ".", sizeof(".")) == 0 || strncmp(dirent->d_name, "..", sizeof("..")) == 0 ) + continue; - dir = opendir("/sys/class/mmc_host/"); - if ( ! dir ) { - ERROR("Cannot find MyDocs mmc device: Opening '/sys/class/mmc_host/' failed"); - goto clean; - } + if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name) <= 0 ) + continue; - while ( ( dirent = readdir(dir) ) ) { + fd = open(buf, O_RDONLY); + if ( fd < 0 ) + continue; - if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name) <= 0 ) - continue; + memset(buf, 0, sizeof(buf)); + if ( read(fd, buf, sizeof(buf)-1) < 0 ) + buf[0] = 0; + close(fd); + fd = -1; - fd = open(buf, O_RDONLY); - if ( fd < 0 ) - continue; + if ( strncmp(buf, "internal", sizeof("internal")-1) != 0 ) + continue; - memset(buf, 0, sizeof(buf)); - if ( read(fd, buf, sizeof(buf)-1) < 0 ) - buf[0] = 0; - close(fd); + if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/", dirent->d_name, dirent->d_name) <= 0 ) + continue; - if ( strncmp(buf, "internal", sizeof("internal")-1) != 0 ) - continue; + dir2 = opendir(buf); + if ( ! dir2 ) + continue; - if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/", dirent->d_name, dirent->d_name) <= 0 ) - continue; + while ( ( dirent2 = readdir(dir2) ) ) { - dir2 = opendir(buf); - if ( ! dir2 ) + if ( strncmp(dirent2->d_name, "block:mmcblk", sizeof("block:mmcblk")-1) != 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 ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/%s/dev", dirent->d_name, dirent->d_name, dirent2->d_name) <= 0 ) + 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; - } + if ( *maj != -1 && *min != -1 ) + break; - VERBOSE("Detected internal mmc device: major=%d minor=%d\n", maj, min); + } - blk[0] = 0; + closedir(dir); - dir = opendir("/dev/"); - if ( ! dir ) { - ERROR("Cannot find MyDocs mmc device: Opening '/dev/' failed"); - goto clean; - } +#endif - 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); - - strncat(blk, "p1", sizeof(blk)-strlen(blk)-1); + ret = disk_dump_dev(fd, file); - printf("Using MyDocs mmc device: '%s'\n", blk); - - ret = disk_dump_raw(blk, file); + close(fd); + fd = -1; } else { @@ -449,7 +442,7 @@ int local_dump_image(enum image_type image, const char * file) { if ( len == (off_t)-1 || len == 0 ) goto clean; - addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); + addr = (unsigned char *)mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); if ( addr == MAP_FAILED ) addr = NULL; @@ -484,7 +477,7 @@ int local_dump_image(enum image_type image, const char * file) { clean: if ( addr ) - munmap(addr, len); + munmap((void *)addr, len); if ( fd >= 0 ) close(fd); |