#ident "$Id: dvd_raw.c,v 1.10 2006/04/27 23:45:51 craig Exp $"
/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/*
 * This program generates a disk file consisting of hexadecimal words
 * starting with 0 and increasing by 1.  Word N in the file contains
 * value N
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <netinet/in.h>

// #define FILE_SIZE_BYTES    (1024ULL * 1024ULL * 4)
// Number of bytes in 4.7 Gbyte disk
#define FILE_SIZE_BYTES    4669979776ULL
// #define FILE_SIZE_BYTES    (1024 * 1024 * 25)
// #define FILE_SIZE_BYTES       (128 * 256)
// #define FILE_SIZE_BYTES       (2 * 1024)

#ifdef __CYGWIN__
#define fopen64(name, mode)     _fopen64(name,mode)
#define ftello64(stream)        _ftello64(stream)
#endif


int
main(int argc, char *argv[])
{
    FILE        *raw_dvd;
    char        *raw_name;
    uint64_t    i;
    uint64_t    file_size_bytes, file_size_words;

    if (argc == 2) {
        file_size_bytes = atoll(argv[1]);
    }
    else if (argc == 1) {
        file_size_bytes = FILE_SIZE_BYTES;
    }
    else {
        printf ("Usage: %s file_size_in_bytes\n", argv[0]);
        exit(1);
    }
    if ((file_size_bytes & 0x3) != 0) {
        fprintf (stderr, "File size must be a multiple of 4 - exit\n");
        exit(1);
    }

    file_size_words = file_size_bytes >> 2;

    raw_name = "image.raw";
    if ( (raw_dvd = fopen (raw_name, "w")) == NULL) {
        printf ("Error: can't open %s for writing - exit\n", raw_name);
        exit(1);
    }

    for (i = 0; i < file_size_words; i++) {
        uint32_t        data;

        data = htonl((uint32_t) i);
        fwrite (&data, sizeof(uint32_t), 1, raw_dvd);
    }

    fclose(raw_dvd);
    exit(0);
}
