#ident "$Id: disk_format.h,v 1.26 2007/02/22 02:16:49 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.
 *
 */

#ifndef _DISK_FORMAT_H_
#define _DISK_FORMAT_H_

#include "types.h"
#include "estypes.h"            // From NC/GBA tree
#include "es_int.h"             // From NC/GBA tree
#include "iosccert.h"           // From NC/GBA tree


#define REVOLUTION_MAGIC                0x5d1c9ea3
#define GAMECUBE_MAGIC                  0x23339f3d

typedef struct DVDDiskId {
    u8             gameName[4];
    u8             company[2];
    u8             diskNumber;
    u8             gameVersion;
    u8             streaming;
    u8             streamingBufSize;  // 0 = default
    u8             padding1[14];      // 0's are stored
    u32            rvlMagic;
    u32            gcMagic;           // GC magic number is here
} DVDDiskId;

// DISK_INFO_OFFSET is the length of DVDDiskId
#define DISK_INFO_OFFSET        0x20

#define GLOBAL_TOC_OFFSET       0x40000

/* Offset to the start of the real disk image data in an RVM image */
#define RVM_HEADER_LENGTH       0x8000

typedef struct DVDDiskInfo {
    u8             gameNameLong[64];
    u8             noHash;
    u8             noEncryption;
} DVDDiskInfo;

#define DVD_UNENCRYPTED_START           0x0
#define DVD_UNENCRYPTED_LENGTH          0x50000
#define SECURITY_OFFSET                 0x40000
#define GAME_PARTITION_ALIGN            (64 * 1024)
#define PARTITION_ALIGN                 (64 * 1024)
#define UPDATE_AREA_ALIGN               (32 * 1024)
#define ITEM_ALIGN                      32
#define H3_ALIGN                        (32 * 1024)
#define H3_SIZE                         (96 * 1024)
#define ENCRYPT_ALIGN                   (32 * 1024)
#define DISK_ADDR(x)                    (htonl((u32)((x) >> 2)))
/* The inverse of DISK_ADDR */
#define UNDO_DISK_ADDR(x)               ((off_t) ntohl(x) << 2ULL)

#ifndef SHA1_DIGESTSIZE
#define SHA1_DIGESTSIZE                 20
#endif

#define H0_HASH_LENGTH                  (31 * SHA1_DIGESTSIZE)
#define H1_HASH_OFFSET                  640
#define H1_HASH_LENGTH                  (8 * SHA1_DIGESTSIZE)
#define H2_HASH_OFFSET                  832
#define H2_HASH_LENGTH                  (8 * SHA1_DIGESTSIZE)
#define H2_PAD_LENGTH                   32
#define BYTES_PER_BLOCK                 (32 * 1024)
#define WORDS_PER_BLOCK                 (BYTES_PER_BLOCK / 4)
#define DATA_SUBBLOCKS_PER_BLOCK        31

#define BYTES_PER_SUBBLOCK              1024
#define DATA_BYTES_PER_BLOCK            (DATA_SUBBLOCKS_PER_BLOCK * BYTES_PER_SUBBLOCK)
#define DATA_WORDS_PER_BLOCK            (DATA_BYTES_PER_BLOCK / BYTES_PER_WORD)

#ifndef AES_IV_SIZE
#define AES_IV_SIZE                     16
#endif

#define TMD_ACR_ACCESS_BIT      0x1
#define TMD_DVD_ACCESS_BIT      0x2

/*
 * Note: all offsets are relative to the start of the partition
 *       header and are word offsets
 */
typedef struct partition {
    ESTicket       ticket;
    u32            tmdSize;
    u32            tmdOffset;
    u32            certBlobSize;
    u32            certBlobOffset;
    u32            h3HashesOffset;
    u32            encryptedAreaOffset;
    u32            partDataLengthWords;
} partition_t;

#endif
