John Morgan
John Morgan

Reputation: 121

How to erase a Hard Disk Drive

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?

  1. From what I understand, DBAN deletes all hard drives it detects. I only want to clean my external.

  2. 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.

  3. 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.

Upvotes: 5

Views: 9280

Answers (3)

Nikos C.
Nikos C.

Reputation: 51832

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.

Upvotes: 6

Luca Stein
Luca Stein

Reputation: 93

As a note. Your question seems to be about erasing data, aka shredding etc.

Flashing a HDD would be to update the internal software. As in: an HDD has its own firmware. Under Linux you can do i.e.:

$ sudo hdparm -I /dev/sda

You will then get information such as:

ATA device, with non-removable media
    Model Number:       ST31000524AS                            
    Serial Number:      XXXXXXX
    Firmware Revision:  JC4B    
    Transport:          Serial, SATA Rev 3.0

...

Tho this is the other way around, you can look at / search forensics. I.e. FAU, Open Source Digital Forensics etc. to get more information.

A bit more on wiping.

Upvotes: 1

Brian Cain
Brian Cain

Reputation: 14619

From what I understand, DBAN deletes all hard drives it detects. I only want to clean my external.

It doesn't.

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.

But when you wake up in the morning, it's done, right? Also, it's apparently configurable.

To be honest, I actually am kind of curious as to how firmware works.

IMO, this isn't the best place to start.

Upvotes: 2

Related Questions