commit 24e7b322387957e2b6d670a2afd6c886ebf133ca Author: haraldwolff Date: Thu Jan 11 23:45:26 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2dff2f0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cmake-build-debug diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/ddscratch.iml b/.idea/ddscratch.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/ddscratch.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a0c2875 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..109a099 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.26) +project(ddscratch C) + +set(CMAKE_C_STANDARD 11) + +add_executable(ddscratch main.c) diff --git a/main.c b/main.c new file mode 100644 index 0000000..cb4f2fb --- /dev/null +++ b/main.c @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include + +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 ] | [-o ] ) [-c ] [-s ] \n"); +} + +