From: Mike Frysinger Date: Sun, 5 Apr 2015 02:02:05 +0000 (-0400) Subject: ddnuke: add random data options X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=d8ceb2d61d353617ea72a576566bb969b60bf0fb;p=home.git ddnuke: add random data options --- diff --git a/ddnuke.c b/ddnuke.c index 94691b3..03ac789 100644 --- a/ddnuke.c +++ b/ddnuke.c @@ -15,17 +15,17 @@ #include #include #include +#include #include #include #include +#include #include #include #include #include #include -static const char zero[1024 * 1024]; - #define errp(msg, args...) \ do { \ printf("%s: " msg ": %m\n", program_invocation_short_name, ## args); \ @@ -66,32 +66,12 @@ static off_t get_size(int fd) errp("unknown type of file"); } -int main(int argc, char *argv[]) +static void nuke(int fd, off_t offset, off_t max_size, unsigned char pattern) { - off_t offset = 0; - const char *file; - - setlocale(LC_NUMERIC, "en_US"); - - switch (argc) { - case 3: - offset = atoll(argv[2]); - case 2: - file = argv[1]; - break; - default: - puts("Usage: ddnuke [offset in bytes]"); - return 1; - } - - int fd = open(file, O_WRONLY); - if (fd == -1) - errp("open(%s) failed", file); - - off_t max_size = get_size(fd); - - if (lseek(fd, offset, SEEK_SET) != offset) - errp("lseek(%"PRIu64"u) failed", offset); + static char mem[1024 * 1024]; + memset(mem, pattern, sizeof(mem)); + if (pattern) + printf("Writing 0x%X to the output\n", pattern); unsigned long long speed = 0; struct timespec stime, etime, itime; @@ -102,12 +82,13 @@ int main(int argc, char *argv[]) ssize_t ret; int fsync_pos = 0; while (pos < max_size) { - ret = write(fd, zero, sizeof(zero)); + /* This will round up to sizeof(mem) ... */ + ret = write(fd, mem, sizeof(mem)); pos += ret; - if (ret != sizeof(zero)) { + if (ret != sizeof(mem)) { printf("\nread() returned %zi (wanted %zu)\n%'llu MB/s over entire run\n", - ret, sizeof(zero), mbps(&itime, &etime, pos)); - return 0; + ret, sizeof(mem), mbps(&itime, &etime, pos)); + return; } printf("%'llu bytes %u%% (%'llu MB/s)%20s\r", (unsigned long long)pos, @@ -122,5 +103,60 @@ int main(int argc, char *argv[]) } printf("\n"); - return 0; +} + +static void usage(int status) +{ + fprintf( + status ? stderr : stdout, + "Usage: ddnuke [options] \n" + "\n" + "Options:" + " -o Position to start writing\n" + " -r Write random garbage to the device\n" + ); + exit(status); +} + +int main(int argc, char *argv[]) +{ + off_t offset = 0; + const char *file; + bool random = false; + int o; + + setlocale(LC_NUMERIC, "en_US"); + + while ((o = getopt(argc, argv, "ho:r")) != -1) { + switch (o) { + case 'o': + offset = atoll(optarg); + break; + case 'r': + random = true; + break; + case 'h': usage(EX_OK); + default: usage(EX_USAGE); + } + } + if (argc != optind + 1) + usage(EX_USAGE); + file = argv[optind]; + + int fd = open(file, O_WRONLY|O_CLOEXEC); + if (fd == -1) + errp("open(%s) failed", file); + + off_t max_size = get_size(fd); + + if (lseek(fd, offset, SEEK_SET) != offset) + errp("lseek(%"PRIu64"u) failed", offset); + + nuke(fd, offset, max_size, 0x00); + if (random) { + nuke(fd, offset, max_size, 0xaa); + nuke(fd, offset, max_size, 0x55); + } + + return EX_OK; }