This is an odd question, but here it goes. I would like to write a program to flash my external hard drive with 1s and then 0s to clean it completely so I can sell it. Now, why do I want to write my own software instead of just using DBAN?
From what I understand, DBAN deletes all hard drives it detects. I only want to clean my external.
DBAN flashes seven times. I feel this is a little excessive for my purposes, since my external contains no illegal material nor credit card information.
To be honest, I actually am kind of curious as to how firmware works.
Google didn't turn up too much (stupid Adobe Flash). I would prefer doing this in C/C++, but other languages work to, including assembly.
Well, doing it in C is rather easy. First, you open the appropriate device file in write mode:
int fd = open("/dev/sdc", O_WRONLY);
and you simply write()
512 byte chunks to it until you can write no more. Newer disks use 4096 byte sectors instead, but the OS usually treats them as if they have 512 byte sectors, so 512 is the safest value. Here's a C program that does exactly this:
(Note: Be very careful to choose the correct /dev device file, or else you're going to wipe out the wrong disk!)
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
int fd = open("/dev/sdd", O_WRONLY);
if (fd < 0) {
fprintf(stderr, "Error opening device file.\n");
return EXIT_FAILURE;
}
// Write 0's all over the disk, in chunks of 512 bytes.
char* zeros = calloc(1, 512);
ssize_t written, total = 0;
do {
total += written = write(fd, zeros, 512);
printf("\rBytes written: %ld", total);
} while (written == 512);
printf("\nDone!\n");
close(fd);
free(zeros);
return 0;
}
You might get a speed-up if you remove the printf()
, though it's kind of cool to see the progress as it happens. You should probably also do additional error checking at the end (if written
is -1, an error occurred, and you should check errno
.)
Note that due to caching, the program might appear to hang at the end for a little while after it prints "Done". It's not really hanging, it's only that the caching of the write operations is blocking it until they're all finished.