#ident "$Id: debug.c,v 1.5 2008/01/23 20:59:05 cnguyen 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.
 *
 */

/*
 * Functions used only for debugging
 * 
 */

#include "iostypes.h"
#include "hw.h"
#include "ioslibc.h"
#include "ios.h"
#include "stdbool.h"

#include "fs.h"
#include "debug.h"

#include "dvd.h"
#include "dvd_hw.h"
#include "dvd_command.h"

extern  IOSMessageQueueId      dvdDriverSleepQueue;

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

void
printMD5sum(const void* const ptr, const u32 numBytes)
{
    md5_state_t state;
    md5_byte_t digest[16];

    md5_init(&state);
    md5_append(&state, ptr, numBytes);
    md5_finish(&state, digest);

    for (int idx = 0; idx < 16; idx++) {
        printf("%02x", digest[idx]);
    }
}


/* This section is for routines to log to NAND */
#ifdef LOG_REGS

#define DATA_BUF_SIZE   192     /* size of log buffer */
static u8 *dataBuf = NULL;

#define DI_LOG_DIR      "/shared2/test2"
#define DI_LOG_FILE     "/shared2/test2/dilog.txt"

ISFSError
diWriteLogFile(const u8* data, u32 size)
{
    ISFSError rc = ISFS_ERROR_OK;
    IOSFd fd = -1;
    int bytes;
    u32 num;

    /* Create log directory if does not exist */
    rc = ISFS_ReadDir(DI_LOG_DIR, NULL, &num);
    if (rc == ISFS_ERROR_NOEXISTS) {
        printf("DI: creating log directory (%s)...\n", DI_LOG_DIR);
        rc = ISFS_CreateDir(DI_LOG_DIR, 0, ISFS_RW_ACCESS,
                         ISFS_RW_ACCESS, ISFS_RW_ACCESS);
        if (rc != ISFS_ERROR_OK) {
            printf("DI: create log directory (%s) failed, rc=%d\n", 
                DI_LOG_DIR, rc);
            goto out;
        }
    }

    /* Create log file */
    rc = ISFS_CreateFile((const u8*)DI_LOG_FILE, 0, ISFS_RW_ACCESS,
                         ISFS_RW_ACCESS, ISFS_RW_ACCESS);
    if ((rc != ISFS_ERROR_OK) && (rc != ISFS_ERROR_EXISTS)) {
        printf("DI: create log file (%s) failed, rc=%d\n", DI_LOG_FILE, rc);
        goto out;
    }

    /* Open log file */
    fd = ISFS_Open((const u8*)DI_LOG_FILE, ISFS_WRITE_ACCESS);
    if (fd < 0) {
        printf("DI: open file (%s) failed, fd=%d\n", DI_LOG_FILE, fd);
        rc = fd;
        goto out;
    }

    /* Seek to end of file */
    rc = ISFS_Seek(fd, 0, IOS_SEEK_END);
    if (rc < 0) {
        printf("DI: seek to end of file (%s) failed, rc=%d\n", DI_LOG_FILE, rc);
        goto out;
    }

    /* Write or append to file */
    bytes = ISFS_Write(fd, data, size);
    if (bytes != size) {
        printf("DI: write file (%s) failed, bytes=%d size=%d\n", DI_LOG_FILE, 
               bytes, size);
        rc = ISFS_ERROR_UNKNOWN;
        goto out;
    }

    /* Close */
    rc = ISFS_Close(fd);
    fd = -1;
    if (rc != ISFS_ERROR_OK) {
        printf("DI: close file (%s) failed, rc=%d\n", DI_LOG_FILE, rc);
        goto out;
    }

    // printf("DI: written file (%s) size=%d\n", DI_LOG_FILE, bytes);

out:
    return(rc);

}


#include "dvd_hw_reg.i"

void
diLogRegs(u8 *msg, const diCommand_t *diCommand, u8 noRegs)
{

    u8 *fsBuf = NULL;
    static u8 initialized = 0;
    static u8 loggedOnce = 0;

    ISFSError rc = ISFS_ERROR_OK;
    u32 buf[9];

    if (!initialized) {
        /* Need to allocate buffers from shared heap to communicate with
           ISFS due to memory protection scheme */
        extern void *__isfsBuf;

        fsBuf = IOS_Alloc(0, ISFS_INITLIB_BUFSIZE);
        if (fsBuf == NULL) {
            printf("DI: alloc ISFS buffer failed!\n");
            goto out;
        }
        
        dataBuf = IOS_Alloc(0, DATA_BUF_SIZE);
        if (dataBuf == NULL) {
            printf("DI: alloc data buffer failed!\n");
            goto out;
        }

        __isfsBuf = fsBuf;  /* Overload ISFS internal buffer in library */
        initialized = 1;

    }

    buf[0] = readDIRegister(DI_STATUS_ADDR);
    buf[1] = readDIRegister(DI_COVER_ADDR);
    buf[2] = readDIRegister(DI_CMD_BUF_0_ADDR);
    buf[3] = readDIRegister(DI_CMD_BUF_1_ADDR);
    buf[4] = readDIRegister(DI_CMD_BUF_2_ADDR);
    buf[5] = readDIRegister(DI_DMA_DEST_ADDR);
    buf[6] = readDIRegister(DI_LENGTH_ADDR);
    buf[7] = readDIRegister(DI_CONTROL_ADDR);
    buf[8] = readDIRegister(DI_IMM_DATA_ADDR);

    memset(dataBuf, 0, DATA_BUF_SIZE);
    if (strncmp(msg, "IOS_OPEN", 8) == 0) {
        snprintf(dataBuf, DATA_BUF_SIZE, 
            "\x0D\x0A%s (%s)\x0D\x0A", msg, BUILD_TIME);
    }
    else if (noRegs) {
        snprintf(dataBuf, DATA_BUF_SIZE, 
            "\x0D\x0A%s: %04x: %08x %08x\x0D\x0A",
            msg, (diCommand == NULL) ? 0 : diCommand->theCommand, 
            (diCommand == NULL) ? 0 : diCommand->arg[0], 
            (diCommand == NULL) ? 0 : diCommand->arg[1]);
    }
    else {
        /* xxx: Need to make sure that msg is within range (< 70 chars) */
        snprintf(dataBuf, DATA_BUF_SIZE, 
            "\x0D\x0A%s: %04x: %08x %08x: %08x, %08x, %08x, %08x, %08x, %08x, "
            "%08x, %08x, %08x\x0D\x0A",
            msg, (diCommand == NULL) ? 0 : diCommand->theCommand, 
            (diCommand == NULL) ? 0 : diCommand->arg[0], 
            (diCommand == NULL) ? 0 : diCommand->arg[1],
            buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
            buf[6], buf[7], buf[8]);
    }

    if (!loggedOnce) {
        printf("DI: logging to NAND file (%s)\n", DI_LOG_FILE);
        loggedOnce = 1;
    }

    /* If want to print out log message to JTAG */
    DPRINT("DI: %s", dataBuf);

    rc = diWriteLogFile((const u8*)dataBuf, DATA_BUF_SIZE);
    if (rc != ISFS_ERROR_OK) {
        printf("DI: write to log file failed, rc=%d\n", rc);
        goto out;
    }

out:
    return;

}

#endif

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