/*-
 * Copyright (c) 2003 Robert N. M. Watson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <sys/disk.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define	DISK_ERROR	-1
#define	SYS_ERROR	-2

#define	VERSION		0x00000100

void
usage(void)
{

	fprintf(stderr, "diskclear device\n");
	exit(-1);
}

int
writesector(int disk_fd, u_int64_t sector, u_int sectorsize, u_char *buffer)
{
	if (lseek(disk_fd, sector * sectorsize, SEEK_SET) < 0)
		return (-1);

	return (write(disk_fd, buffer, sectorsize));
}

int
randomsector(int random_fd, int disk_fd, u_int sector, u_int sectorsize)
{
	char buffer[sectorsize];
	ssize_t len;

	len = read(random_fd, buffer, sectorsize);
	if (len != sectorsize) {
		perror("/dev/random");
		return (SYS_ERROR);
	}

	if (writesector(disk_fd, sector, sectorsize, buffer) != len)
		return (DISK_ERROR);

	return (0);
}

int
main(int argc, char *argv[])
{
	u_int64_t sectorcount, i;
	int disk_fd, random_fd;
	u_int sectorsize;
	off_t mediasize;

	if (argc != 2)
		usage();

	disk_fd = open(argv[1], O_RDWR);
	if (disk_fd == -1) {
		perror(argv[1]);
		exit(-1);
	}

	random_fd = open("/dev/random", O_RDONLY);
	if (random_fd == -1) {
		perror("/dev/random");
		exit(-1);
	}

	if (ioctl(disk_fd, DIOCGMEDIASIZE, &mediasize) != 0) {
		perror("DIOCGMEDIASIZE");
		exit(-1);
	}

	if (ioctl(disk_fd, DIOCGSECTORSIZE, &sectorsize) != 0) {
		perror("DIOCGSECTORSIZE");
		exit(-1);
	}

	printf("%s: %llu media, %u sector\n", argv[1], mediasize, sectorsize);

	if (mediasize < sectorsize || sectorsize == 0) {
		fprintf(stderr, "Inconsistent disk parameters\n");
		exit(-1);
	}

	sectorcount = mediasize / sectorsize;

	printf("%s: %llu sectorcount\n", argv[1], sectorcount);

	for (i = 0; i < sectorcount; i++) {
		printf("%llu\r", i);
		if (randomsector(random_fd, disk_fd, i, sectorsize) != 0) {
			fprintf(stderr, "%s: sector %llu error %s (%d)\n",
			    argv[1], i, strerror(errno), errno);
		}
	}

	return (0);
}
