add scratchmap and change scratching algorithm

master
Harald Wolff 2024-01-12 00:40:48 +01:00
parent 917b90b78e
commit ef80204e3e
1 changed files with 93 additions and 37 deletions

130
main.c
View File

@ -7,14 +7,22 @@
char devpath[FILENAME_MAX];
char targetpath[FILENAME_MAX];
char mapfile[FILENAME_MAX];
int firstSector = -1;
long offset = -1;
int cntSectors = -1;
int sectSize = 4096;
long devSectorCount = -1;
char *map;
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 ch;
@ -24,13 +32,17 @@ int main(int argc, char **argv) {
offset = strtol(optarg, NULL, 0);
break;
case 'b':
offset = strtol(optarg, NULL, 0);
firstSector = strtol(optarg, NULL, 0);
break;
case 'c':
cntSectors = strtol(optarg, NULL, 0);
break;
case 's':
sectSize = strtol(optarg, NULL, 0);
break;
case 'm':
strncpy(mapfile, optarg, sizeof(mapfile));
break;
case 'h':
printUsage();
return 0;
@ -39,12 +51,16 @@ int main(int argc, char **argv) {
if (optind != (argc-2)){
printUsage();
printf("need device and target!\n");
return -1;
}
strncpy(devpath, argv[optind], sizeof(devpath));
strncpy(targetpath, argv[optind+1], sizeof(targetpath));
if (!mapfile[0])
snprintf(mapfile, sizeof(mapfile), "%s.scratchmap", targetpath);
if ((offset != -1) && (firstSector != -1)){
printUsage();
printf("only first sector OR offset may be given!\n");
@ -55,64 +71,87 @@ int main(int argc, char **argv) {
if (firstSector == -1)
firstSector = 0;
int dev = open(devpath, O_RDONLY);
int dev = open(devpath, O_RDONLY | O_SYNC);
if (dev == -1){
printf("could not open device %s\n", devpath);
return -2;
}
long devsize = lseek(dev, 0, SEEK_END);
devSectorCount = (devsize / sectSize);
if (cntSectors == -1)
cntSectors = (int)(devsize / sectSize);
int target = open(devpath, O_RDONLY);
if (target == -1){
printf("could not open target image %s\n", targetpath);
close(dev);
return -3;
}
int target = open(targetpath, O_RDWR | O_CREAT);
if (target == -1){
printf("could not open target image %s\n", targetpath);
close(dev);
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("sector size: %i\n", sectSize);
printf("offset: 0x%08lx\n", ((long)firstSector * sectSize));
printf("first sector: %i\n", firstSector);
printf("sectors: %i\n", cntSectors);
for (int n=0;n<cntSectors;n++){
scratch(dev, target, firstSector + n, sectSize);
}
memset( map, 0x00, devSectorCount );
loadScratchMap( mapfile, devSectorCount, map);
scratch(dev, target, firstSector, cntSectors, sectSize );
saveScratchMap( mapfile, devSectorCount, map);
close(target);
close(dev);
return 0;
}
int scratch(int dev, int image,int sector,int size){
char p[size];
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 scratch(int dev, int image,int firstSector, int cntSectors, int sectSize){
char p[sectSize];
int nread = read(dev, p, size);
if (nread == -1){
continue;
} else {
if (nread < size) {
printf("\nshort read!\n");
}
write(image, p, nread);
return size;
}
}
for (int try = 0; try < 16; try ++){
for (int sect = firstSector; sect < (firstSector + cntSectors); sect++){
long offset = (long)sect * sectSize;
printf("\r#%08i 0x%08lX %i", sect, offset, try);
fflush(stdout);
if (map[sect])
continue;
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;
}