#ident "$Id: ndev_common.c,v 1.2 2007/02/02 23:23:29 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.
 *
 */

/*
 * Program comments here
 * 
 */

#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#else
#define fseeko(file, where, whence)     fseeko64(file, where, whence)
#define ftello(file)                    ftello64(file)
#endif


/*
 * Need to define this, or else on Cygwin the ticket and tmd
 * structures are laid out differently than on Linux
 */
#define BROADWAY_REV

#include <netinet/in.h>         // For htonl()
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>          // For off_t

#include "disk_format.h"
#include "es_int.h"
#include "tool_common.h"

extern char* ProgName;

/* ------------------------------------------------------------------------ */

/*
 * INPUTS:
 *         partitionStart: byte offset to the start of partition in the 
 *                         output file outFile
 */

bool
generateNdevPartition(FILE                      *outFile,
                      const off_t               partitionStart,
                      const ESTicket* const     ticket,
                      const ESTitleMeta* const  tmd,
                      const u32                 tmdSize,
                      const void* const         certBlob,
                      const u32                 certBlobSize
                      )
{
    bool                returnVal = false;
    partition_t         partition, partitionOnDisk;
    off_t               tmdStart, certBlobStart, cryptAreaStart;
    off_t               currentPosition;
    u8                  paddingByte;
    int                 paddingBytes;


    memcpy(&(partition.ticket), ticket, sizeof(ESTicket));
    partition.tmdSize      = tmdSize;
    partition.certBlobSize = certBlobSize;
    tmdStart = partitionStart + sizeof(partition_t);
    tmdStart = alignAddress(tmdStart, ITEM_ALIGN);
    certBlobStart = alignAddress(tmdStart + partition.tmdSize, ITEM_ALIGN);
    cryptAreaStart = alignAddress(certBlobStart + partition.certBlobSize,
                                 ENCRYPT_ALIGN);

    partition.tmdOffset           = DISK_ADDR(tmdStart - partitionStart);
    partition.certBlobOffset      = DISK_ADDR(certBlobStart - partitionStart);
    partition.h3HashesOffset      = 0;
    partition.partDataLengthWords = 0;
    partition.encryptedAreaOffset = DISK_ADDR(cryptAreaStart - partitionStart);

    partitionOnDisk              = partition;
    partitionOnDisk.tmdSize      = htonl(partition.tmdSize);
    partitionOnDisk.certBlobSize = htonl(partition.certBlobSize);
    /*
     * Write out the game partition structure, which includes
     * the eTicket
     */
    if (fseeko(outFile, partitionStart, SEEK_SET) == -1) {
        perror("fseeko");
        goto end;
    }
    if (fwrite(&partitionOnDisk, sizeof(partition_t), 1, outFile) != 1) {
        fprintf(stderr, "Partition struct write failed\n");
        goto end;
    }

    /* Write out TMD structure */
    if (fseeko(outFile, tmdStart, SEEK_SET) == -1) {
        perror("fseeko");
        goto end;
    }
    if (fwrite(tmd, 1, partition.tmdSize, outFile) != partition.tmdSize) {
        fprintf(stderr, "Partition TMD write failed\n");
        goto end;
    }

    /* Write out certificates */
    if (fseeko(outFile, certBlobStart, SEEK_SET) == -1) {
        perror("fseeko");
        returnVal = false;
        goto end;
    }
    if (fwrite(certBlob, partition.certBlobSize, 1, outFile) != 1) {
        fprintf(stderr, "Partition certificates write failed\n");
        returnVal = false;
        goto end;
    }

    /*
     * Pad the length of the generated file out to the start
     * of the "encrypted" area.
     */
    currentPosition = ftell(outFile);
    paddingBytes = cryptAreaStart - currentPosition;

    paddingByte = 0;
    for (int i = 0; i < paddingBytes; i++) {
        if (fwrite (&paddingByte, 1, 1, outFile) != 1) {
            fprintf (stderr, "Writing padding byte faild\n");
            returnVal = false;
            goto end;
        }
    }
    returnVal = true;
end:
    return(returnVal);
}

/* ------------------------------------------------------------------------ */
