/*
 *               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.
 *
 */

 /*
  * NOTES:
  *   - These tests are used to verify the new system call IOSC_CheckDiHashes()
  *
  */


#include <hw.h>
#include <ios.h>
#include <iostypes.h>
#include <iosc.h>
#include <ioslibc.h>
#include <iop.h>
#include <disk_format.h>
#include <addr_map.h>

#define STACK_SIZE 1024
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;


//#define RVL_64              1             // To use RVL_64 address space
//#define IOS_MEM_12          1             // To use the old 12 MB IOP space

/*
 * For IOS 37 and later (based on latest top-of-trunk release), the
 * GDDR3 area (0x17600000 - 0x17620000 for 128MB) is no longer accessible
 * by PPC. Caller will get the IOS_ERROR_ACCESS error.
 * Defining this flag "ALLOW_DI_DMA_SP" should be done when running these
 * tests against older IOS versions where PPC can access this 128KB region
 * of GDDR3.
 */
//#define ALLOW_DI_DMA_SP     1             // Allow access to 128KB of DI DMA
                                            // space in GDDR3
#define NAPA_START          0x80000000      // Virtual address from PPC: 24MB
#define NAPA_END            0x81800000

#define NAPA_PA_START       0x00000000      // Physical address: 24MB 
#define NAPA_PA_END         0x01800000

#if defined(BROADWAY_REV)
#if defined(IOS_MEM_12)
#if defined(RVL_64)
#define IOP_GDDR3_START     0x13400000      // 12MB
#define IOP_GDDR3_END       0x14000000
#else
#define IOP_GDDR3_START     0x17400000      // 12MB
#define IOP_GDDR3_END       0x18000000
#endif
#else  /* !IOS_MEM_12 */
#if defined(RVL_64)
#define IOP_GDDR3_START     0x13600000      // 10MB
#define IOP_GDDR3_END       0x14000000
#else
#define IOP_GDDR3_START     0x17600000      // 10MB
#define IOP_GDDR3_END       0x18000000
#endif
#endif

/*
 * For IOS 37 and later (based on latest top-of-trunk release), the
 * GDDR3 area (0x17600000 - 0x17620000) is no longer accessible by PPC.
 * Caller will get the IOS_ERROR_ACCESS error.
 */
#if defined(ALLOW_DI_DMA_SP)
#define DI_DMA_SP_SIZE      0x20000         // 128KB
#else
#define DI_DMA_SP_SIZE      0x0             // 128KB
#endif
#endif  /* BROADWAY_REV */

#define VADDR(a)            (((a)|0x80000000))

#define GDDR3_START         0x90000000      // 24MB
#define GDDR3_END           (VADDR(IOP_GDDR3_START+DI_DMA_SP_SIZE))
#define GDDR3_PA_START      0x10000000      // 24MB
#define GDDR3_PA_END        (IOP_GDDR3_START+DI_DMA_SP_SIZE)
#define PAGE_SIZE           (64*1024)

#define VALID_DSTADDR       (u8 *)(0x10000000)   // Start of GDDR3
#define VALID_DISKRDBUF     (u8 *)(IOP_GDDR3_START)
#define VALID_H1INDEX       0
#define VALID_H2INDEX       0
#define VALID_H3PTR         (u8 *)(0x11000000)
//#define VALID_H3PTR         (u8 *)(0x20214000)
#define INVALID_H1INDEX     (64*1024)
#define INVALID_H2INDEX     (64*1024)


u32 InvalidBufAddrs[] = {
#if 0
    KERNEL_DATA_START,
    KERNEL_DATA_END-(PAGE_SIZE),
    KERNEL_DATA_END,
#endif
    CRYPTO_SP_START,
    CRYPTO_SP_END-(PAGE_SIZE),
    CRYPTO_SP_END,
    ES_SP_START,
    ES_SP_END-(PAGE_SIZE),
    ES_SP_END,
    FS_SP_START,
    FS_SP_END-(PAGE_SIZE),
    FS_SP_END,
    SD0_SP_START,
    SD0_SP_END-(PAGE_SIZE),
    SD0_SP_END,
    SYSCALL_STACK_START,
    SYSCALL_STACK_END-(PAGE_SIZE),
    SYSCALL_STACK_END,
    SYS_RSVD_SP_START,
    SYS_RSVD_SP_END-(PAGE_SIZE),
    SYS_RSVD_SP_END,
    STM_SP_START,
    STM_SP_END-(PAGE_SIZE),
    STM_SP_END,

    SSL_SP_START,
    SSL_SP_END-(PAGE_SIZE),
    SSL_SP_END,
    KD_SP_START,
    KD_SP_END-(PAGE_SIZE),
    KD_SP_END,
    NCD_SP_START,
    NCD_SP_END-(PAGE_SIZE),
    NCD_SP_END,
#if 0
    NAPA_PA_END,
    NAPA_PA_END-(DATA_BYTES_PER_BLOCK),
    NAPA_PA_END-(DATA_BYTES_PER_BLOCK+8),
    NAPA_PA_END-(BYTES_PER_BLOCK),
    NAPA_PA_END-(BYTES_PER_BLOCK+8),
    GDDR3_PA_END,
    GDDR3_PA_END-(DATA_BYTES_PER_BLOCK),
    IOP_GDDR3_START+(8),
    IOP_GDDR3_START+(8*PAGE_SIZE),
    IOP_GDDR3_START+(16*PAGE_SIZE),
    IOP_GDDR3_START+(32*PAGE_SIZE),
    IOP_GDDR3_START+(63*PAGE_SIZE),
    IOP_GDDR3_START+(64*PAGE_SIZE),
    IOP_GDDR3_START+(65*PAGE_SIZE),
    IOP_GDDR3_START+(66*PAGE_SIZE),
    IOP_GDDR3_START+(67*PAGE_SIZE),
    IOP_GDDR3_START+(68*PAGE_SIZE),
    IOP_GDDR3_START+(69*PAGE_SIZE),
    IOP_GDDR3_START+(70*PAGE_SIZE),
    IOP_GDDR3_START+(71*PAGE_SIZE),
    IOP_GDDR3_START+(72*PAGE_SIZE),
    IOP_GDDR3_START+(73*PAGE_SIZE),
#endif
};

static u32 isDI = 0;
static u32 testCnt = 0;


static IOSError
testDIProcess(void)
{
    IOSError rv;

    // IOSError IOSC_CheckDiHashes(u8 *dstAddr, u8 *diskRdbuf, u32 h1Index,
    //                            u32 h2Index, u8 *h3Ptr);

    printf("\n*** Test %2d: Using Invalid Process ID\n", testCnt);

    rv = IOSC_CheckDiHashes(VALID_DSTADDR+8, VALID_DISKRDBUF, VALID_H1INDEX, 
             VALID_H2INDEX, VALID_H3PTR);
    if (isDI) {
        /*  Test should fail 64-byte aligned check for destAddr */
        if (rv == IOS_ERROR_INVALID) {  
            printf("  -> CheckDiHashes failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
            goto out;
        }
        else {
            printf("  -> CheckDiHashes failed: %d -> BAD\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    } else {
        if (rv == IOS_ERROR_ACCESS) {
            printf("  -> CheckDiHashes failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
            goto out;
        } else {
            printf("  -> CheckDiHashes failed: %d -> BAD\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }

out:
    return rv;
}

static IOSError
testInvalidBufAddrs()
{
    IOSError rv = IOS_ERROR_OK;
    u32 i, j;

    // IOSError IOSC_CheckDiHashes(u8 *dstAddr, u8 *diskRdbuf, u32 h1Index,
    //                            u32 h2Index, u8 *h3Ptr);

    printf("\n*** Test %2d: Using Invalid Buffer Address\n", testCnt);

    j = 0;
    for (i=0; i < (sizeof(InvalidBufAddrs)/sizeof(u32)); i++) {
        u8 *p = (u8 *)InvalidBufAddrs[i];
        
        if (j==0) {
           printf("  -> Testing destAddr with test addr: 0x%x\n", p);
           rv = IOSC_CheckDiHashes(p,VALID_DISKRDBUF,0,0,VALID_H3PTR);
           j++;
        } 
        else if (j==1) {
           printf("  -> Testing diskReadBuf with test addr: 0x%x\n", p);
           rv = IOSC_CheckDiHashes(VALID_DSTADDR,p,0,0,VALID_H3PTR);
           j++;
        } 
        else if (j==2) {
           printf("  -> Testing h3Ptr with test addr: 0x%x\n", p);
           rv = IOSC_CheckDiHashes(VALID_DSTADDR,VALID_DISKRDBUF,0,0,p);
           j = 0;
        } 

        if (rv < 0) {
            if (rv == IOS_ERROR_ACCESS) {
                printf("  -> CheckDiHashes failed: %d -> OK\n", rv);
                rv = IOS_ERROR_OK;
            } else {
                printf("  -> CheckDiHashes failed: %d -> BAD\n", rv);
                rv = IOS_ERROR_INVALID;
                goto out;
            }
        }
        else {
            printf("  -> CheckDiHashes succeeded -> BAD\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }

out:
    return rv;
}

static IOSError
testInvalidArgs()
{
    IOSError rv = IOS_ERROR_OK;

    // IOSError IOSC_CheckDiHashes(u8 *dstAddr, u8 *diskRdBuf, u32 h1Index,
    //                            u32 h2Index, u8 *h3Ptr);

    printf("\n*** Test %2d: Using Invalid Arguments\n", testCnt);

    /* Test for non-64-byte aligned buffer address */
    printf(" ** Testing using unaligned buffer for dstAddr...\n");
    rv = IOSC_CheckDiHashes(VALID_DSTADDR+8, VALID_DISKRDBUF, VALID_H1INDEX, 
             VALID_H2INDEX, VALID_H3PTR);
    if (rv != IOS_ERROR_OK) {
        if (rv == IOS_ERROR_INVALID) {
            printf("  -> CheckDiHashes failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
        } else {
            printf("  -> CheckDiHashes failed: %d -> FAIL\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }
    else {
        printf("  -> CheckDiHashes succeeded -> FAIL\n", rv);
        rv = IOS_ERROR_INVALID;
        goto out;
    }

    printf(" ** Testing using unaligned buffer for diskRdBuf...\n");
    rv = IOSC_CheckDiHashes(VALID_DSTADDR, VALID_DISKRDBUF+8, VALID_H1INDEX, 
             VALID_H2INDEX, VALID_H3PTR);
    if (rv != IOS_ERROR_OK) {
        if (rv == IOS_ERROR_INVALID) {
            printf("  -> CheckDiHashes failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
        } else {
            printf("  -> CheckDiHashes failed: %d -> FAIL\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }
    else {
        printf("  -> CheckDiHashes succeeded -> FAIL\n", rv);
        rv = IOS_ERROR_INVALID;
        goto out;
    }

    /* Test for invalid buffer range */
    printf(" ** Testing using invalid buffer range for dstAddr (0x%08x)...\n",
           SHARED_SP_END-(DATA_BYTES_PER_BLOCK-64));
    rv = IOSC_CheckDiHashes((u8 *)(SHARED_SP_END-(DATA_BYTES_PER_BLOCK-64)), 
             VALID_DISKRDBUF, VALID_H1INDEX, VALID_H2INDEX, VALID_H3PTR);
    if (rv != IOS_ERROR_OK) {
        if (rv == IOS_ERROR_ACCESS) {
            printf("  -> CheckDiHashes failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
        } else {
            printf("  -> CheckDiHashes failed: %d -> FAIL\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }
    else {
        printf("  -> CheckDiHashes succeeded -> FAIL\n", rv);
        rv = IOS_ERROR_INVALID;
        goto out;
    }

    printf(" ** Testing using invalid buffer range for diskRdBuf (0x%08x)...\n",
           (SHARED_SP_END-(BYTES_PER_BLOCK-64)));
    rv = IOSC_CheckDiHashes(VALID_DSTADDR,
             (u8 *)(SHARED_SP_END-(BYTES_PER_BLOCK-64)), 
             VALID_H1INDEX, VALID_H2INDEX, VALID_H3PTR);
    if (rv != IOS_ERROR_OK) {
        if (rv == IOS_ERROR_ACCESS) {
            printf("  -> CheckDiHashes failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
        } else {
            printf("  -> CheckDiHashes failed: %d -> FAIL\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }
    else {
        printf("  -> CheckDiHashes succeeded -> FAIL\n", rv);
        rv = IOS_ERROR_INVALID;
        goto out;
    }

    printf(" ** Testing using invalid buffer range for h3Ptr (0x%08x)...\n",
           (SHARED_SP_END-(SHA1_DIGESTSIZE-4)));
    rv = IOSC_CheckDiHashes(VALID_DSTADDR, VALID_DISKRDBUF,
             VALID_H1INDEX, VALID_H2INDEX, 
             (u8 *)(SHARED_SP_END-(SHA1_DIGESTSIZE-4)));
    if (rv != IOS_ERROR_OK) {
        if (rv == IOS_ERROR_ACCESS) {
            printf("  -> CheckDiHashes failed: %d -> OK\n", rv);
            rv = IOS_ERROR_OK;
        } else {
            printf("  -> CheckDiHashes failed: %d -> FAIL\n", rv);
            rv = IOS_ERROR_INVALID;
            goto out;
        }
    }
    else {
        printf("  -> CheckDiHashes succeeded -> FAIL\n", rv);
        rv = IOS_ERROR_INVALID;
        goto out;
    }

out:
    return rv;
}


void
main(u32 arg)
{
    IOSError rv = IOS_ERROR_OK;
    u32     errCnt = 0;

    errCnt = testCnt = 0;

    printf("\n*** Testing IOS_DiCheckHashes ***\n");

    isDI = (IOS_DI_PROCESS == IOS_GetProcessId())? 1 : 0;
    printf("Current Process ID = DI\n");

    testCnt++;
    if ((rv = testDIProcess()) < 0) {
        printf("*** Test %2d: Using Invalid Process ID -> FAIL\n", testCnt);
        errCnt++;
    }
    else {
        printf("*** Test %2d: Using Invalid Process ID -> PASS\n", testCnt);
    }

    testCnt++;
    if ((rv = testInvalidArgs()) < 0) {
        printf("*** Test %2d: Using Invalid Arguments -> FAIL\n", testCnt);
        errCnt++;
    }
    else {
        printf("*** Test %2d: Using Invalid Arguments -> PASS\n", testCnt);
    }

    testCnt++;
    if ((rv = testInvalidBufAddrs()) < 0) {
        printf("*** Test %2d: Using Invalid Buffer Addrs -> FAIL\n", testCnt);
        errCnt++;
    }
    else {
        printf("*** Test %2d: Using Invalid Buffer Addrs -> PASS\n", testCnt);
    }

    printf("\n>>>> Total Tests = %d, Errors = %d\n\n", testCnt, errCnt);

    return;
}

