/*
 *               Copyright (C) 2006, 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.
 *
 * $Id: boot1_dev.h,v 1.3 2008/06/04 20:10:31 cnguyen Exp $
 */

#ifndef __BOOT1_DEV__
#define __BOOT1_DEV__


/* Hardcoded addresses for the start of AHB, AHB protected, SRAM, and Napa */
#define AHB_START           0x0d000000
#define AHB_PROT_START      0x0d800000
#define SRAM_START          0x0d400000
#define SRAM_ALT_START      0xfff00000
#define NAPA_START          0x00000000

/*
 * The maximum boot1 size, anything after this can be used for buffering
 * The size is aligned on 16KB boundary so that the TTB can be placed
 * at this location. The first 32KB of the SRAM is used for boot1 code,
 * the next 16KB is used for the TTB, and the last 16KB is used for the
 * stack. The SRAM beyond that is not used because it may not work in
 * some older version of hardware
 */
#define BOOT1_MAX_SIZE      (BOOT1_MAX_CODEDATA_SIZE + BOOT1_MAX_BSS_SIZE)

/* TTB address and size */
#define BOOT1_TTB_START     (SRAM_START + BOOT1_MAX_SIZE)
#define BOOT1_TTB_ENTRIES   4096

/* Define staging area for loading boot2 */
#ifdef LOAD_TO_NAPA
#define BOOT2_LOAD_START    0x00028000
#define BOOT2_LOAD_END      0x00080000
#endif /* LOAD_TO_NAPA */

#ifdef LOAD_TO_DDR
#define BOOT2_LOAD_START    (RSVD_SP_START - (4 * 1024 * 1024))
#define BOOT2_LOAD_END      (BOOT2_LOAD_START + (1024 * 1024))
#endif

#define BOOT2_MAX_SIZE      (BOOT2_LOAD_END - BOOT2_LOAD_START)

/*
 * Because of a hardware bug, we can't do DMA with from/into the
 * SRAM while running in the SRAM. The workaround here is to use
 * the second safe area inside the Napa for buffering. The area
 * from 0x0000a8000 to 0x00100000 is safe. Divide the area into
 * several 16KB sections for use by the boot1 code
 */
#define NAPA_BUFFER_1       0x000a8000
#define NAPA_BUFFER_2       0x000ac000
#define NAPA_BUFFER_3       0x000b0000
#define NAPA_BUFFER_SIZE    (16*1024)

/* Alignment sizes */
#define AES_BLOCK_SZ_SHIFT  4
#define AES_BLOCK_SZ        (1 << AES_BLOCK_SZ_SHIFT)
#define SHA_BLOCK_SZ_SHIFT  6
#define SHA_BLOCK_SZ        (1 << SHA_BLOCK_SZ_SHIFT)
#define ALIGN_SZ_SHIFT      6
#define ALIGN_SZ            (1 << ALIGN_SZ_SHIFT)

#define OVERLAP(addr1, sz1, addr2, sz2)                                     \
    (((addr1 <= addr2) && ((addr1 + sz1) > addr2)) ||                       \
    ((addr2 <= addr1) && ((addr2 + sz2) > addr1)))

/* Direction to read boot2 */
#define DIR_FORWARD         1
#define DIR_BACKWARD        2

/*
 * String limiting for cert names;
 * each piece is a two-char name followed by 8 digits;
 * we build up ROOT plus up to 3 pieces, for a total of 4;
 * sizeof(IOSCName) is 64, divide by 4 is 16 for each piece;
 */
#define SIZE_LIMCNAME       (sizeof(IOSCName) / 4)

/* Constants for flash */
#define FL_BLOCK_SHIFT      17
#define FL_BLOCK_SIZE       (1 << FL_BLOCK_SHIFT)
#define FL_PAGE_SHIFT       11
#define FL_PAGE_SIZE        (1 << FL_PAGE_SHIFT)
#define FL_SPARE_SIZE       64
#define FL_READ_SETUP_CMD   0x00
#define FL_READ_CMD         0x30
#define FL_ADP_1F           0x1f
#define FL_ADP_00           0x00
#define FL_STATUS_BYTE      0

#define BLOCK_WITHIN_SYSAREA(bl)                                            \
    ((bl >= 1) && (bl < (ISFS_SYSAREA_SIZE >> FL_BLOCK_SHIFT)))

/* Hash types */
#define HASH_FIRST          1
#define HASH_MIDDLE         2
#define HASH_LAST           3

/* Type of certs/tmd/ticket to verify */
#define TYPE_CERT4096       1
#define TYPE_CERT2048       2
#define TYPE_TMD            3
#define TYPE_TICKET         4

/* Delay */
#define LED_DELAY           1000000

/* Error codes */
#define REASON_DBE          -1
#define REASON_BADMAP       -2
#define REASON_BADELF       -3
#define REASON_BADBLOCK     -4
#define REASON_BADHEADER    -5
#define REASON_BADCONTENT   -6
#define REASON_HASH         -7
#define REASON_BADSIG       -8
#define REASON_BADNAME      -9
#define REASON_BADVERSION   -10
#define REASON_BADTITLE     -11
#define REASON_EEPROM       -12

/* Byte swap */
#define byte_swap(x)    (((x & 0xff000000) >> 24)                           \
                        | ((x & 0x00ff0000) >> 8)                           \
                        | ((x & 0x0000ff00) << 8)                           \
                        | ((x & 0x000000ff) << 24))

/* Device control */
#define WAIT(dev)       while (busRd32(dev##_CTRL_REG) & dev##_CTRL_EXEC)
#define FLUSH(dev)      ahbMemFlush(IOS_WB_##dev)
#define INVAL(dev)      ahbMemRBInvalidate(IOS_RB_##dev)
#define FLUSH_ALL()                                                         \
    do {                                                                    \
        FLUSH(IOD);                                                         \
        FLUSH(FLA);                                                         \
        FLUSH(AES);                                                         \
        FLUSH(SHA);                                                         \
    } while (0)
#define INVAL_ALL()                                                         \
    do {                                                                    \
        INVAL(IOD);                                                         \
        INVAL(FLA);                                                         \
        INVAL(AES);                                                         \
        INVAL(SHA);                                                         \
    } while (0)


#if defined(ENABLE_DEBUG)
#define DEBUG_LED(x)    __outputLed(x)
#else
#define DEBUG_LED(x)    (void)(0)
#endif


extern void __outputLed(u8 value);
extern void setupAes(CSLOSAesKey *aesKey, CSLOSAesIv *iv, u32 base);
extern int  checkEcc(u8 *buf, u8 *spare, u32 pageSize);
extern void readPageAsync(u32 page, u8 *dataBuf, u8 *spareBuf);
extern int  runHashAsync(u32 *ctx, u32 type, u32 base, u32 size, 
                CSLOSSha1Hash *result);
extern int  verifyBundle(u32 blockOffset, ESWadHeader *wadHdr, u32 *contentSize,
                CSLOSAesKey *aesKey, CSLOSAesIv *iv, CSLOSSha1Hash *hash, 
                u32 base);

#endif /* __BOOT1_DEV__ */
