/*  
 *  Utility file for Kilopass backdoor
 *
 *  :set tabstop=4
 *  :set expandtab
 *  :set shiftwidth=4
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include "dv.h"

int 
DV_BdKpMemPut(int wh, int addr, int sz, unsigned char *data)
{
    unsigned char *sdata;
    int *p, wsz, len;
    unsigned int id;

    wsz = sz +  sizeof(int) *3;
    sdata = (unsigned char *) malloc((wsz+3) & (~3));
    if (sdata == NULL) {
        printf("ERROR: alloc space(sz=%d) Failed\n", sz); 
        return IPC_ERROR;
    }

    p = (int *) sdata;
    p[0] = wh;
    p[1] = addr;
    p[2] = sz;
    memcpy(sdata+sizeof(int)*3, data, sz);

    id = DV_GetSocketNextCmdId(wh);
    if (DV_IpcIssueReq(__func__, wh,
                sizeof(ipc_pkth) + wsz,
                IPC_CMD_BD_KILO_MEM_PUT,
                id | IPC_REQ_ID_COMMIT,
                sdata) != IPC_OK) {
        return IPC_ERROR;
    }
    
    len =  DV_IpcWaitReplyId(__func__, wh, id,
                 sizeof(ipc_pkth),  sizeof(ipc_pkth));
    
    return (len !=  sizeof(ipc_pkth)) ? IPC_ERROR : IPC_OK;
}

int 
DV_Cpu0BdKpMemPut(int addr, int sz, unsigned char *data)
{
    return DV_BdKpMemPut(IPC_SOCK_CPU0, addr, sz, data);
}

int 
DV_Cpu1BdKpMemPut(int addr, int sz, unsigned char *data)
{
    return DV_BdKpMemPut(IPC_SOCK_CPU1, addr, sz, data);
}

int 
DV_CpuBdKpMemPut(int addr, int sz, unsigned char *data)
{
    int sock;

    if ((sock = DV_GetCurrentCpu(__func__)) >= 0) {
        return DV_BdKpMemPut(sock, addr, sz, data);
    }

    return -1; 
}

int 
DV_BdKpMemGet(int wh, int addr, int sz, unsigned char *data)
{
    int pkt[3], len;
    unsigned int id;

    pkt[0] = wh;
    pkt[1] = addr;
    pkt[2] = sz;

    id = DV_GetSocketNextCmdId(wh);
    if (DV_IpcIssueReq(__func__, wh,
                sizeof(ipc_pkth) + sizeof(int) * 3,
                IPC_CMD_BD_KILO_MEM_GET,
                id | IPC_REQ_ID_COMMIT,
                (unsigned char *) pkt) != IPC_OK) {
        return IPC_ERROR;
    }
    
    len =  DV_IpcWaitReplyId(__func__, wh, id,
                 sizeof(ipc_pkth)+sz,  sizeof(ipc_pkth)+sz);
   
    if (len != (sizeof(ipc_pkth)+sz)) {
        DV_Exception(DV_EXP_ERROR, "Kilopass read data sz is wrong");
        return IPC_ERROR;
    } else {
        DV_IpcRecvPktData(wh, (unsigned char*) data, len - sizeof(ipc_pkth));
    }

    return IPC_OK;
}

int 
DV_Cpu0BdKpMemGet(int addr, int sz, unsigned char *data)
{
    return DV_BdKpMemGet(IPC_SOCK_CPU0, addr, sz, data);
}

int 
DV_Cpu1BdKpMemGet(int addr, int sz, unsigned char *data)
{
    return DV_BdKpMemGet(IPC_SOCK_CPU1, addr, sz, data);
}

int 
DV_CpuBdKpMemGet(int addr, int sz, unsigned char *data)
{
    int sock;

    if ((sock = DV_GetCurrentCpu(__func__)) >= 0) {
        return DV_BdKpMemGet(sock, addr, sz, data);
    }

    return IPC_ERROR; 
}

int 
DV_BdKpReset(int wh)
{
    unsigned int id;
	int len;

    id = DV_GetSocketNextCmdId(wh);
    if (DV_IpcIssueReq(__func__, wh,
                sizeof(ipc_pkth),
                IPC_CMD_BD_KILO_RESET,
                id | IPC_REQ_ID_COMMIT,
                NULL) != IPC_OK) {
        return IPC_ERROR;
    }
    
    len =  DV_IpcWaitReplyId(__func__, wh, id,
                 sizeof(ipc_pkth),  sizeof(ipc_pkth));
   
    if (len != (sizeof(ipc_pkth))) {
        DV_Exception(DV_EXP_ERROR, "Kilopass read data sz is wrong");
        return IPC_ERROR;
    } 

    return IPC_OK;
}

int 
DV_Cpu0BdKpReset()
{
    return DV_BdKpReset(IPC_SOCK_CPU0);
}

int 
DV_Cpu1BdKpReset()
{
    return DV_BdKpReset(IPC_SOCK_CPU1);
}

int 
DV_CpuBdKpReset()
{
    int sock;

    if ((sock = DV_GetCurrentCpu(__func__)) >= 0) {
        return DV_BdKpReset(sock);
    }

    return IPC_ERROR; 
}

int
DV_BdGetKpprot(int which)
{
    int kp_prot;

    if (DV_IpcBdRequest(__func__, which,
            sizeof(ipc_pkth),
            sizeof(ipc_pkth) + sizeof(int),
            IPC_CMD_BD_GET_KP_PROT,
            DV_GetSocketNextCmdId(which),
            NULL, (unsigned char *) &kp_prot) != IPC_OK) {
        return -1;
    }
    
    return kp_prot;	
}

int
DV_Cpu0BdGetKpprot()
{
    return DV_BdGetKpprot(IPC_SOCK_CPU0);
}

int
DV_Cpu1BdGetKpprot()
{
    return DV_BdGetKpprot(IPC_SOCK_CPU1);
}

int
DV_CpuBdGetKpprot()
{
    int sock;

    if ((sock = DV_GetCurrentCpu(__func__)) >= 0) {
        return DV_BdGetKpprot(sock);
    }

    return -1;
}

