Initial commit

master
Harald Wolff 2024-01-11 23:45:26 +01:00
commit 24e7b32238
8 changed files with 182 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
cmake-build-debug

8
.idea/.gitignore vendored 100644
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

4
.idea/misc.xml 100644
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ddscratch.iml" filepath="$PROJECT_DIR$/.idea/ddscratch.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml 100644
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

6
CMakeLists.txt 100644
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.26)
project(ddscratch C)
set(CMAKE_C_STANDARD 11)
add_executable(ddscratch main.c)

147
main.c 100644
View File

@ -0,0 +1,147 @@
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
char devpath[FILENAME_MAX];
char targetpath[FILENAME_MAX];
int firstSector = -1;
long offset = -1;
int cntSectors = -1;
int sectSize = 4096;
void printUsage();
int scratch(int dev, int image,int sector,int size);
int main(int argc, char **argv) {
int ch;
while ((ch = getopt(argc, argv, "o:c:s:h")) != -1){
switch (ch){
case 'o':
offset = strtol(optarg, NULL, 0);
break;
case 'b':
offset = strtol(optarg, NULL, 0);
break;
case 'c':
cntSectors = strtol(optarg, NULL, 0);
break;
case 's':
sectSize = strtol(optarg, NULL, 0);
case 'h':
printUsage();
return 0;
}
}
if (optind != (argc-2)){
printUsage();
return -1;
}
strncpy(devpath, argv[optind], sizeof(devpath));
strncpy(targetpath, argv[optind+1], sizeof(targetpath));
if ((offset != -1) && (firstSector != -1)){
printUsage();
printf("only first sector OR offset may be given!\n");
}
if (offset != -1)
firstSector = (int)(offset / sectSize);
if (firstSector == -1)
firstSector = 0;
int dev = open(devpath, O_RDONLY);
if (dev == -1){
printf("could not open device %s\n", devpath);
return -2;
}
long devsize = lseek(dev, 0, SEEK_END);
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;
}
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);
}
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 nread = read(dev, p, size);
if (nread == -1){
continue;
} else {
if (nread < size) {
printf("\nshort read!\n");
}
write(image, p, nread);
return size;
}
}
}
void printUsage(){
printf("usage: ddscratch ( [-b <firstsector> ] | [-o <offset>] ) [-c <sectorcount>] [-s <sectorsize>] <path> <target>\n");
}