add scratchmap and change scratching algorithm
parent
917b90b78e
commit
ef80204e3e
130
main.c
130
main.c
|
@ -7,14 +7,22 @@
|
||||||
|
|
||||||
char devpath[FILENAME_MAX];
|
char devpath[FILENAME_MAX];
|
||||||
char targetpath[FILENAME_MAX];
|
char targetpath[FILENAME_MAX];
|
||||||
|
char mapfile[FILENAME_MAX];
|
||||||
|
|
||||||
int firstSector = -1;
|
int firstSector = -1;
|
||||||
long offset = -1;
|
long offset = -1;
|
||||||
int cntSectors = -1;
|
int cntSectors = -1;
|
||||||
int sectSize = 4096;
|
int sectSize = 4096;
|
||||||
|
long devSectorCount = -1;
|
||||||
|
|
||||||
|
char *map;
|
||||||
|
|
||||||
void printUsage();
|
void printUsage();
|
||||||
int scratch(int dev, int image,int sector,int size);
|
|
||||||
|
int loadScratchMap(char *filename, int size, char *map);
|
||||||
|
int saveScratchMap(char *filename, int size, char *map);
|
||||||
|
|
||||||
|
int scratch(int dev, int image,int firstSector, int cntSectors, int sectSize);
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int ch;
|
int ch;
|
||||||
|
@ -24,13 +32,17 @@ int main(int argc, char **argv) {
|
||||||
offset = strtol(optarg, NULL, 0);
|
offset = strtol(optarg, NULL, 0);
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
offset = strtol(optarg, NULL, 0);
|
firstSector = strtol(optarg, NULL, 0);
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
cntSectors = strtol(optarg, NULL, 0);
|
cntSectors = strtol(optarg, NULL, 0);
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
sectSize = strtol(optarg, NULL, 0);
|
sectSize = strtol(optarg, NULL, 0);
|
||||||
|
break;
|
||||||
|
case 'm':
|
||||||
|
strncpy(mapfile, optarg, sizeof(mapfile));
|
||||||
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
printUsage();
|
printUsage();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -39,12 +51,16 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
if (optind != (argc-2)){
|
if (optind != (argc-2)){
|
||||||
printUsage();
|
printUsage();
|
||||||
|
printf("need device and target!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy(devpath, argv[optind], sizeof(devpath));
|
strncpy(devpath, argv[optind], sizeof(devpath));
|
||||||
strncpy(targetpath, argv[optind+1], sizeof(targetpath));
|
strncpy(targetpath, argv[optind+1], sizeof(targetpath));
|
||||||
|
|
||||||
|
if (!mapfile[0])
|
||||||
|
snprintf(mapfile, sizeof(mapfile), "%s.scratchmap", targetpath);
|
||||||
|
|
||||||
if ((offset != -1) && (firstSector != -1)){
|
if ((offset != -1) && (firstSector != -1)){
|
||||||
printUsage();
|
printUsage();
|
||||||
printf("only first sector OR offset may be given!\n");
|
printf("only first sector OR offset may be given!\n");
|
||||||
|
@ -55,64 +71,87 @@ int main(int argc, char **argv) {
|
||||||
if (firstSector == -1)
|
if (firstSector == -1)
|
||||||
firstSector = 0;
|
firstSector = 0;
|
||||||
|
|
||||||
int dev = open(devpath, O_RDONLY);
|
int dev = open(devpath, O_RDONLY | O_SYNC);
|
||||||
if (dev == -1){
|
if (dev == -1){
|
||||||
printf("could not open device %s\n", devpath);
|
printf("could not open device %s\n", devpath);
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
long devsize = lseek(dev, 0, SEEK_END);
|
long devsize = lseek(dev, 0, SEEK_END);
|
||||||
|
devSectorCount = (devsize / sectSize);
|
||||||
|
|
||||||
if (cntSectors == -1)
|
if (cntSectors == -1)
|
||||||
cntSectors = (int)(devsize / sectSize);
|
cntSectors = (int)(devsize / sectSize);
|
||||||
|
|
||||||
int target = open(devpath, O_RDONLY);
|
int target = open(targetpath, O_RDWR | O_CREAT);
|
||||||
if (target == -1){
|
if (target == -1){
|
||||||
printf("could not open target image %s\n", targetpath);
|
printf("could not open target image %s\n", targetpath);
|
||||||
close(dev);
|
close(dev);
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("target (image file): %s\n", targetpath);
|
map = malloc(devSectorCount);
|
||||||
|
if (!map){
|
||||||
|
printf("failed to allocate in memory map\n");
|
||||||
|
close(target);
|
||||||
|
close(dev);
|
||||||
|
return -4;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("target (image file): %s\n", targetpath);
|
||||||
printf("device: %s\n", devpath);
|
printf("device: %s\n", devpath);
|
||||||
printf("sector size: %i\n", sectSize);
|
printf("sector size: %i\n", sectSize);
|
||||||
printf("offset: 0x%08lx\n", ((long)firstSector * sectSize));
|
printf("offset: 0x%08lx\n", ((long)firstSector * sectSize));
|
||||||
printf("first sector: %i\n", firstSector);
|
printf("first sector: %i\n", firstSector);
|
||||||
printf("sectors: %i\n", cntSectors);
|
printf("sectors: %i\n", cntSectors);
|
||||||
|
|
||||||
for (int n=0;n<cntSectors;n++){
|
memset( map, 0x00, devSectorCount );
|
||||||
scratch(dev, target, firstSector + n, sectSize);
|
loadScratchMap( mapfile, devSectorCount, map);
|
||||||
}
|
|
||||||
|
scratch(dev, target, firstSector, cntSectors, sectSize );
|
||||||
|
|
||||||
|
saveScratchMap( mapfile, devSectorCount, map);
|
||||||
|
|
||||||
close(target);
|
close(target);
|
||||||
close(dev);
|
close(dev);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int scratch(int dev, int image,int sector,int size){
|
int scratch(int dev, int image,int firstSector, int cntSectors, int sectSize){
|
||||||
char p[size];
|
char p[sectSize];
|
||||||
long offset = (long)sector * size;
|
|
||||||
for (int n = 0; n < 16; n++){
|
|
||||||
printf("\r#%08i 0x%08lX %i", sector, offset, n);
|
|
||||||
if (lseek(dev, offset, SEEK_SET) != offset){
|
|
||||||
printf("\ncould not seek (dev).\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (lseek(image, offset, SEEK_SET) != offset){
|
|
||||||
printf("\ncould not seek (image).\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int nread = read(dev, p, size);
|
for (int try = 0; try < 16; try ++){
|
||||||
if (nread == -1){
|
for (int sect = firstSector; sect < (firstSector + cntSectors); sect++){
|
||||||
continue;
|
long offset = (long)sect * sectSize;
|
||||||
} else {
|
printf("\r#%08i 0x%08lX %i", sect, offset, try);
|
||||||
if (nread < size) {
|
fflush(stdout);
|
||||||
printf("\nshort read!\n");
|
|
||||||
}
|
if (map[sect])
|
||||||
write(image, p, nread);
|
continue;
|
||||||
return size;
|
|
||||||
}
|
if (lseek(dev, offset, SEEK_SET) != offset){
|
||||||
}
|
printf("\ncould not seek (dev).\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (lseek(image, offset, SEEK_SET) != offset){
|
||||||
|
printf("\ncould not seek (image).\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nread = read(dev, p, sectSize);
|
||||||
|
if (nread == -1){
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
if (nread < sectSize) {
|
||||||
|
printf("\nshort read!\n");
|
||||||
|
}
|
||||||
|
write(image, p, nread);
|
||||||
|
map[sect] = 0x01;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -135,7 +174,24 @@ int scratch(int dev, int image,int sector,int size){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int loadScratchMap(char *filename, int size, char *map){
|
||||||
|
int scratchmap = open(filename, O_RDONLY);
|
||||||
|
if (scratchmap != -1){
|
||||||
|
int nread = read(scratchmap, map, size);
|
||||||
|
}
|
||||||
|
close(scratchmap);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int saveScratchMap(char *filename, int size, char *map){
|
||||||
|
int nwritten = -1;
|
||||||
|
int scratchmap = open(filename, O_WRONLY | O_TRUNC);
|
||||||
|
if (scratchmap != -1){
|
||||||
|
nwritten = write(scratchmap, map, size);
|
||||||
|
}
|
||||||
|
close(scratchmap);
|
||||||
|
return nwritten;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue