
/**
 * @file        hv_comm_shell_hdcp_debug.c
 * @brief       shell source code.
 * @details     This file provides the following functions: \n
 *              (1) shell init and handle \n
 * @author      HiView SoC Software Team
 * @version     1.0.1
 * @date        2022-09-05
 * @copyright   Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par         History:
 * <table>
 * <tr><th>Author                        <th>Date         <th>Change Description
 * <tr><td>HiView SoC Software Team      <td>2022-08-25   <td>create
 * </table>
*/

#ifdef HV_SCALER_DEBUG_VERSION
#include <string.h>
#include "hv_comm_Log.h"
#include "hv_comm_shell_hvconfig.h"
#include "hv_comm_DataType.h"
#include "hv_comm_Assert.h"
#include "hv_comm_Define.h"

#include "hv_vos_Comm.h"
#include "hv_vos_Task.h"

#include "hv_mw_Hdmi.h"
#include "hv_mw_Hdcp.h"

#include "hv_mw_SystemManager.h"
#include "hv_mw_InputPortManager.h"
#include "hv_drv_Physical.h"
#include "hv_drv_HdcpRx.h"

#define HV_LOG_BUF_PRINT(...) HV_LOG_BUF(HV_LOG_LEVEL_ERROR, __VA_ARGS__)

HDCPDataDBRegion gstTestHDCPDataDBRegion =
{
    .usCheckCrc = 0x0,
    .usVersion = 0x0,
    .uiMagicNumber = DATABASE_VALID_MAGIC_NUMBER,
    .stKey =
    {
        .stHdcp1xRxKey =
        {
            /*Bksv key input hear*/
            .aucBksv =
            {
                0xb5, 0x93, 0x68, 0xca, 0xb4
            },
            /*Dks key input hear*/
            .aucDks =
            {
                0x99, 0x1b, 0x46, 0x9c, 0x8c, 0xf9, 0xd0, 0xfd,
                0xfe, 0x1b, 0xf3, 0x91, 0x21, 0x7e, 0xd8, 0x18,
                0xb3, 0x87, 0x4b, 0x05, 0xbf, 0x1f, 0x47, 0xd8,
                0xed, 0x6a, 0x14, 0x48, 0xd6, 0x17, 0x00, 0x08,
                0x4b, 0x3f, 0x2e, 0x49, 0x9c, 0x9e, 0x12, 0xa6,
                0xdd, 0xee, 0x32, 0x5a, 0xc6, 0x35, 0xdb, 0xf2,
                0x93, 0x38, 0x53, 0x59, 0x10, 0xaf, 0xcc, 0x90,
                0x0c, 0x9c, 0x16, 0xba, 0xb6, 0x94, 0xc4, 0x8c,
                0xcd, 0xcf, 0x70, 0xa0, 0xa2, 0xae, 0xbb, 0xfc,
                0xac, 0x2d, 0x67, 0x98, 0x92, 0xff, 0x2e, 0xdb,
                0x97, 0x45, 0x87, 0x49, 0x93, 0x49, 0x69, 0x3b,
                0xaf, 0xa2, 0xe5, 0x56, 0x51, 0x71, 0x24, 0xc3,
                0xf3, 0x3f, 0x5f, 0xf0, 0xc3, 0xba, 0x84, 0xbc,
                0x05, 0xa9, 0x70, 0xa4, 0x9a, 0x09, 0x88, 0xe8,
                0xc7, 0x84, 0x55, 0xb2, 0x25, 0x64, 0xf2, 0xcf,
                0x8a, 0x86, 0x69, 0x0b, 0x1c, 0x93, 0xcb, 0x0a,
                0x41, 0x60, 0x12, 0xd9, 0x8a, 0x51, 0xea, 0x39,
                0x32, 0x70, 0xda, 0x53, 0xbf, 0x4f, 0x7e, 0xf6,
                0xc3, 0xe8, 0x9d, 0x90, 0x72, 0x58, 0xfc, 0x0e,
                0x4d, 0xcd, 0xbd, 0x1a, 0x0c, 0x50, 0x96, 0x70,
                0x88, 0x14, 0x60, 0x7b, 0x08, 0x72, 0xbc, 0xe8,
                0xdd, 0x9b, 0x29, 0x10, 0x3b, 0xce, 0x1c, 0xef,
                0x25, 0x9a, 0x9e, 0xcf, 0x11, 0x3f, 0x77, 0x48,
                0xe4, 0xab, 0x8c, 0xc6, 0x39, 0xad, 0x03, 0xbd,
                0x5d, 0x52, 0x06, 0xcf, 0x1c, 0xf7, 0x8a, 0xbe,
                0x24, 0x9b, 0xe5, 0x7d, 0x55, 0x8d, 0x70, 0x3d,
                0x59, 0xe3, 0x93, 0xea, 0xb9, 0x2c, 0x85, 0x6e,
                0x72, 0xfb, 0x41, 0xcb, 0x2d, 0x86, 0xb1, 0x48,
                0xed, 0x0b, 0x2b, 0x58, 0x71, 0xc8, 0xb0, 0x70,
                0x18, 0x90, 0x14, 0xbc, 0xcb, 0x28, 0x9c, 0x79,
                0x79, 0x62, 0xca, 0x1e, 0xf5, 0xc3, 0x47, 0x51,
                0x43, 0x77, 0x56, 0x9e, 0xe7, 0x10, 0x45, 0x9c,
                0x80, 0xf8, 0x98, 0x56, 0x83, 0x69, 0x7f, 0x96,
                0x73, 0x1d, 0x51, 0x6f, 0x8d, 0xec, 0x37, 0x40,
                0xba, 0x93, 0xcc, 0x9d, 0x64, 0x34, 0x9d, 0x87
            }
        },
        .stHdcp2xRxKey =
        {
            .aucHdcp2xRxLc128 =
            {
                0xb5, 0xd8, 0xe9, 0xab, 0x5f, 0x8a, 0xfe, 0xca, 0x38, 0x55, 0xb1, 0xa5, 0x1e, 0xc9, 0xbc, 0x0f
            },
            .aucHdcp2xRxCert =
            {
                0xfc, 0x24, 0x4a, 0x3c, 0x8f, 0xe7, 0x07, 0xd7, 0x19, 0xb0, 0xca, 0x7d, 0x07, 0xce, 0xcc, 0xb7,
                0x3b, 0x5d, 0xfb, 0x0a, 0xc3, 0xfc, 0xbe, 0x36, 0x62, 0x34, 0x1f, 0x66, 0x45, 0x59, 0xfa, 0xe7,
                0x84, 0x4a, 0x11, 0xf2, 0x44, 0xef, 0x85, 0xb3, 0xd8, 0xca, 0x5d, 0x31, 0x18, 0x08, 0xe3, 0xfe,
                0x6b, 0xee, 0x61, 0x73, 0x67, 0x7f, 0xde, 0xa9, 0xd2, 0x16, 0xee, 0x06, 0x17, 0xae, 0x02, 0x0c,
                0x13, 0xcf, 0x8a, 0x6e, 0x52, 0xc9, 0xcc, 0x7d, 0x8d, 0xbe, 0x81, 0x64, 0x4b, 0x0a, 0xe2, 0x49,
                0xc3, 0xc0, 0x86, 0x2a, 0xc3, 0x24, 0x84, 0x7f, 0xa7, 0x45, 0x60, 0x96, 0xe4, 0x59, 0x51, 0x0b,
                0x3c, 0x00, 0x09, 0x5f, 0xb5, 0x8b, 0x6c, 0xc0, 0x06, 0x6b, 0x48, 0xb3, 0xf1, 0x64, 0x32, 0x29,
                0x79, 0xf0, 0xe9, 0xd1, 0xba, 0x89, 0x04, 0x41, 0xb3, 0x81, 0x50, 0x97, 0x54, 0xc7, 0xec, 0x33,
                0xc9, 0x6d, 0xfd, 0x60, 0x3d, 0x01, 0x00, 0x01, 0x10, 0x00, 0x95, 0x8e, 0xc8, 0xca, 0x9f, 0xc2,
                0x94, 0x7f, 0x8b, 0x32, 0x98, 0xd1, 0x4d, 0x3c, 0x8e, 0xa1, 0x42, 0xa5, 0x6d, 0x87, 0x26, 0x6b,
                0xfd, 0x81, 0xbf, 0xbe, 0x51, 0x6e, 0x61, 0x46, 0x88, 0xf5, 0x0e, 0xf8, 0x56, 0x05, 0x95, 0x17,
                0x73, 0x16, 0xda, 0xd1, 0xcf, 0xb0, 0x5e, 0xb0, 0x1c, 0xab, 0x2d, 0xe6, 0x3f, 0x6d, 0xca, 0x8e,
                0xd2, 0x72, 0x4b, 0xd6, 0xf2, 0x29, 0x2a, 0x62, 0x74, 0xfe, 0x5a, 0xf4, 0x0d, 0xdb, 0x08, 0xa2,
                0xa6, 0x41, 0xe3, 0xed, 0xd6, 0xe4, 0xbe, 0xeb, 0x76, 0xd0, 0x07, 0x1c, 0xf4, 0x6e, 0xd2, 0x5b,
                0xc8, 0x89, 0xd7, 0x2d, 0xc5, 0x3a, 0xaa, 0xf9, 0xe6, 0x02, 0x21, 0xc5, 0xee, 0x4e, 0xa9, 0x93,
                0x40, 0xf4, 0xe6, 0xb3, 0x1e, 0xfe, 0x9f, 0x89, 0xf5, 0xc1, 0xfe, 0x36, 0x9a, 0x1c, 0xb8, 0x0f,
                0xd0, 0xa8, 0xbc, 0x00, 0x81, 0x1b, 0xe4, 0xbb, 0xf5, 0x7b, 0x52, 0x5b, 0x52, 0x21, 0xf1, 0xf7,
                0xfe, 0xd9, 0x3b, 0x68, 0xf9, 0xde, 0xa8, 0x73, 0xff, 0x63, 0x9e, 0x38, 0xe2, 0xac, 0xaa, 0x55,
                0x40, 0xbc, 0x7a, 0xc8, 0xbc, 0x8c, 0xe8, 0x4e, 0x5b, 0x7e, 0xc0, 0x06, 0x9e, 0xb3, 0x49, 0x3f,
                0x29, 0xd8, 0x16, 0x5e, 0x99, 0xee, 0xc9, 0x35, 0x86, 0x7b, 0xb8, 0xa5, 0x24, 0xd4, 0x23, 0xbb,
                0x5c, 0x81, 0x66, 0xa7, 0x21, 0x21, 0x64, 0x5c, 0x98, 0x87, 0x72, 0x9b, 0x86, 0x8b, 0xfd, 0xd3,
                0x8e, 0x50, 0x9a, 0x21, 0xae, 0x1e, 0x70, 0x7c, 0x7e, 0xf7, 0x50, 0xc3, 0x79, 0xaa, 0xe7, 0x7a,
                0x14, 0x28, 0x7b, 0x2b, 0x0c, 0xf8, 0xce, 0xe2, 0x46, 0xcf, 0xeb, 0x95, 0x1d, 0xff, 0x72, 0xab,
                0x8d, 0xa9, 0x2a, 0x6a, 0xe0, 0x9b, 0x72, 0x44, 0x70, 0xbe, 0x7f, 0xa5, 0x2a, 0x2c, 0xb4, 0xed,
                0x96, 0x44, 0x48, 0x80, 0x90, 0x60, 0x87, 0x21, 0x1e, 0xb6, 0xfb, 0xa8, 0xfc, 0x92, 0x14, 0xcb,
                0xe8, 0x0e, 0x3a, 0x50, 0x98, 0x7d, 0x43, 0x3d, 0x18, 0xc5, 0xe2, 0xbf, 0x94, 0xb3, 0xcd, 0xee,
                0xe1, 0x4f, 0x58, 0x3e, 0x08, 0xe7, 0x74, 0xae, 0x24, 0xb9, 0x8a, 0x72, 0x0e, 0x6f, 0x17, 0x70,
                0xf8, 0x81, 0x04, 0x5e, 0xbe, 0x5d, 0xed, 0x30, 0x81, 0x9f, 0xb1, 0x06, 0x98, 0x15, 0x97, 0xab,
                0x03, 0x8d, 0x7d, 0x9d, 0xb2, 0x4e, 0xdc, 0x0d, 0xb0, 0x25, 0xcf, 0xa5, 0x48, 0x73, 0x4d, 0x60,
                0xfc, 0x6d, 0x4b, 0x09, 0xae, 0x59, 0x21, 0xcf, 0xd5, 0x6b, 0xe0, 0x24, 0x83, 0x00, 0x1b, 0xfb,
                0x27, 0x09, 0x48, 0xc5, 0x28, 0x95, 0xea, 0x1b, 0xd3, 0x7f, 0xec, 0x45, 0xc4, 0x1a, 0x79, 0x37,
                0xe8, 0xe1, 0xb6, 0x33, 0x3b, 0x4c, 0x54, 0x87, 0x27, 0x9f, 0x32, 0x70, 0xb1, 0x31, 0x84, 0x22,
                0xbe, 0x54, 0x4f, 0xcd, 0xeb, 0x4e, 0xfc, 0x68, 0xc4, 0xe2
            },
            .aucHdcp2xRxPrivKey =
            {
                0xf6, 0x1c, 0x97, 0xce, 0x1f, 0x6b, 0x27, 0xb5, 0x2b, 0xe6, 0xff, 0x25, 0x98, 0xc3, 0x02, 0x79,
                0xa7, 0x90, 0xbb, 0xc6, 0x64, 0xaf, 0x98, 0x44, 0xb0, 0x01, 0xe0, 0xf7, 0x73, 0xe3, 0xd5, 0x2b,
                0x5a, 0x43, 0x3e, 0xc3, 0xe1, 0x52, 0x52, 0x30, 0x56, 0x5a, 0xe3, 0x5e, 0xc2, 0xc0, 0x17, 0xbf,
                0x88, 0x21, 0xe3, 0x91, 0x61, 0x51, 0xd7, 0xdd, 0x82, 0xfb, 0x4d, 0x41, 0x90, 0xd9, 0x2c, 0xd7,
                0xf0, 0x50, 0x21, 0x20, 0x95, 0x8e, 0x87, 0x85, 0xb9, 0x22, 0x6d, 0x0b, 0xfd, 0x70, 0xfc, 0x3f,
                0xec, 0xc2, 0x2d, 0x6e, 0xcf, 0xa1, 0xed, 0xeb, 0x5d, 0xf9, 0x74, 0x96, 0xbf, 0x37, 0x29, 0x41,
                0xe5, 0xf0, 0x2d, 0x7c, 0x3f, 0x17, 0x98, 0xfa, 0xa8, 0x7f, 0xbb, 0x65, 0xb0, 0xc1, 0x66, 0x27,
                0x77, 0x2a, 0xb1, 0x3c, 0xb0, 0xaa, 0xd5, 0x53, 0x78, 0xd2, 0xba, 0x1c, 0x80, 0x31, 0xc5, 0x0b,
                0x32, 0x4d, 0x67, 0xcd, 0xae, 0x68, 0x45, 0x66, 0x67, 0x41, 0xc0, 0x76, 0x9b, 0xc1, 0x87, 0xf8,
                0xfb, 0x81, 0xbe, 0x8f, 0x6f, 0x36, 0xe0, 0xa5, 0x71, 0x60, 0x00, 0xee, 0x32, 0xd6, 0xa7, 0x54,
                0x44, 0x0f, 0xa8, 0x7d, 0x7a, 0xc9, 0x4b, 0x99, 0x2b, 0x9d, 0x4a, 0x09, 0xad, 0x3e, 0x7b, 0xdc,
                0x1e, 0xe3, 0xe6, 0x52, 0x69, 0x94, 0xc3, 0xcd, 0x37, 0x9f, 0x4b, 0x8c, 0x12, 0x22, 0x01, 0x63,
                0xe1, 0xa8, 0x1c, 0xb3, 0x60, 0x3c, 0xaa, 0x7d, 0xa3, 0x4e, 0xf4, 0x5e, 0xe1, 0xea, 0x0a, 0xb4,
                0x6c, 0x3a, 0x1a, 0x37, 0x87, 0xb9, 0x62, 0x94, 0x35, 0x76, 0x5d, 0x48, 0x80, 0xdd, 0xe7, 0x5b,
                0xfd, 0xf2, 0xfe, 0x83, 0x41, 0x2b, 0x83, 0x77, 0x3c, 0x6a, 0x55, 0x2a, 0x34, 0x44, 0x27, 0x0c,
                0xc0, 0x15, 0xd2, 0xc0, 0x4e, 0x94, 0xdc, 0x24, 0xb9, 0xbc, 0x07, 0x0f, 0x64, 0x41, 0x57, 0xe9,
                0x0c, 0x7b, 0x10, 0x3b, 0xe2, 0x1c, 0x6b, 0x0c, 0x2a, 0x5e, 0x02, 0x6e, 0xfd, 0x9e, 0x9f, 0xe0,
                0xb6, 0xcb, 0xc4, 0xe2, 0x75, 0x25, 0x6d, 0xaf, 0x82, 0x7f, 0x7b, 0x16, 0x67, 0x86, 0x3c, 0x70,
                0x04, 0x71, 0xcd, 0xf6, 0xc7, 0xa2, 0xe0, 0x41, 0xec, 0x26, 0x29, 0xce, 0x89, 0xaa, 0x3f, 0xae,
                0xc5, 0x0d, 0xc6, 0x6e, 0xbf, 0xb5, 0xdc, 0x01, 0x5a, 0x56, 0x0e, 0x29, 0x1a, 0xfb, 0xe5, 0x1c
            }
        },
        .ucRsved = 0x00,
        .uiRsved1 = 0x00000000,
        .uiCheckSum = 0x00000000
    },
    .aucHdcp1XKeyId = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0},
    .aucHdcp2XKeyId = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0},
    .bUseDefaultKey = HV_TRUE,
    .bHdcp1XValid = HV_TRUE,
    .bHdcp2XValid = HV_TRUE,
    .bModifyFlag = HV_FALSE
};


void Hv_Shell_HdcpDebug_Usage()
{
    HV_LOG_ERROR(HDCP,
        "    hdcpdebug [OPTION]\n"
        "        -d [dump], Dump the details of the HDCP port status and configrations.\n"
        "        -h print the command hdmidebug help info.\n"
        "           For example to dump all the information of hdcp port 0 : "
        "           hdcpdebug -d 0");
}

void Hv_Shell_HdcpDebug_Dump1XStatus(Hdcp1xRx* pst1XStatus, UCHAR8 ucHdcpPortIndex)
{
    HV_LOG_BUF_PRINT("  An:", pst1XStatus->stParams.An, sizeof(pst1XStatus->stParams.An));
    HV_LOG_BUF_PRINT("Aksv:", pst1XStatus->stParams.AKsv, sizeof(pst1XStatus->stParams.AKsv));
    HV_LOG_BUF_PRINT("  Km:", pst1XStatus->stParams.Km, sizeof(pst1XStatus->stParams.Km));
    HV_LOG_BUF_PRINT("  Ks:", pst1XStatus->stParams.Ks, sizeof(pst1XStatus->stParams.Ks));
    HV_LOG_BUF_PRINT("  M0:", pst1XStatus->stParams.M0, sizeof(pst1XStatus->stParams.M0));

    Hv_Engine_RxBlockCipher(pst1XStatus->stParams.An, pst1XStatus->stParams.Km, 0,
                pst1XStatus->stParams.Ks, pst1XStatus->stParams.M0, &(pst1XStatus->stParams.R0), ucHdcpPortIndex);

    HV_LOG_INFO(HDCP, "After RxBlockCipher the value is:\n");
    HV_LOG_BUF_PRINT("  An:", pst1XStatus->stParams.An, sizeof(pst1XStatus->stParams.An));
    HV_LOG_BUF_PRINT("Aksv:", pst1XStatus->stParams.AKsv, sizeof(pst1XStatus->stParams.AKsv));
    HV_LOG_BUF_PRINT("  Km:", pst1XStatus->stParams.Km, sizeof(pst1XStatus->stParams.Km));
    HV_LOG_BUF_PRINT("  Ks:", pst1XStatus->stParams.Ks, sizeof(pst1XStatus->stParams.Ks));
    HV_LOG_BUF_PRINT("  M0:", pst1XStatus->stParams.M0, sizeof(pst1XStatus->stParams.M0));

    return;
}

void Hv_Shell_HdcpDebug_Dump2XStatus(Hdcp2xRx* pst2XStatus)
{
    /* Rcved MsgId 2[AKE Init] */
    HV_LOG_BUF_PRINT("Rtx:",pst2XStatus->stParams.Rtx, sizeof(pst2XStatus->stParams.Rtx));
    HV_LOG_BUF_PRINT("TxCaps:",pst2XStatus->stParams.TxCaps, sizeof(pst2XStatus->stParams.TxCaps));

    /* Snd MsgId 3[Cert] */
    HV_LOG_BUF_PRINT("Rrx:",pst2XStatus->stParams.Rrx, sizeof(pst2XStatus->stParams.Rrx));
    HV_LOG_BUF_PRINT("RxCaps:",pst2XStatus->stParams.RxCaps, sizeof(pst2XStatus->stParams.RxCaps));
    HV_LOG_BUF_PRINT("pucPubCert:",pst2XStatus->pucPubCert, HV_HDCP_2X_RX_CERT_SIZE);

    /* Rcved MsgId 4(NoStored km) */
    HV_LOG_BUF_PRINT("EKpubKm:", pst2XStatus->stParams.EKpubKm, sizeof(pst2XStatus->stParams.EKpubKm));

    /* Rcved MsgId 5(Stored km) */
    HV_LOG_BUF_PRINT("EKhKm:", pst2XStatus->stParams.EKhKm, sizeof(pst2XStatus->stParams.EKhKm));
    HV_LOG_BUF_PRINT("M:", pst2XStatus->stParams.M, sizeof(pst2XStatus->stParams.M));

    /* EKpubKm [MsgId 4(NoStored km)] + pucPrivKey OAEP==> Km */
    /* Engine with (Rtx || Rrx)m + pucPrivKey + EKhKm[MsgId 5(Stored)] ==> Km */
    HV_LOG_BUF_PRINT("Km:", pst2XStatus->stParams.Km, sizeof(pst2XStatus->stParams.Km));

    /* Snd MsgId 7[H Prime] Engine with RxCaps Rrx Rtx TxCaps Km ==> HPrime */
    HV_LOG_BUF_PRINT("HPrime:", pst2XStatus->stParams.HPrime, sizeof(pst2XStatus->stParams.HPrime));
    /* Snd MsgId 8(EKhKm)[ParingInfo] Engine with (Rtx || Rrx)m + pucPrivKey + Km ==> EKhKm */

    /* Rcved MsgId 9[Lc_init] */
    HV_LOG_BUF_PRINT("    Rn:", pst2XStatus->stParams.Rn, sizeof(pst2XStatus->stParams.Rn));

    /* Snd MsgId 0xA[L Prime] Engine with Rn Km Rrx Rtx ==> LPrime */
    HV_LOG_BUF_PRINT("LPrime:", pst2XStatus->stParams.LPrime, sizeof(pst2XStatus->stParams.LPrime));

    /* Rcved MsgIn 0xB (Eks) */
    HV_LOG_BUF_PRINT("Edkeyks:", pst2XStatus->stParams.EDkeyKs, sizeof(pst2XStatus->stParams.EDkeyKs));
    HV_LOG_BUF_PRINT("   Riv:", pst2XStatus->stParams.Riv, sizeof(pst2XStatus->stParams.Riv));

    /* Engine with Rrx Rtx Km Rn EdkeyKs ==> Ks */
    HV_LOG_BUF_PRINT("Ks:", pst2XStatus->stParams.Ks, sizeof(pst2XStatus->stParams.Ks));

    /* DP Used */
    HV_LOG_BUF_PRINT("EKh:", pst2XStatus->stParams.EKh, sizeof(pst2XStatus->stParams.EKh));

    /* Private Key Checking */
    HV_LOG_BUF_PRINT("pucPrivKey:", pst2XStatus->pucPrivKey, HV_HDCP_2X_RX_PRIVATEKEY_SIZE);

    return;
}

void Hv_Shell_HdcpDebug_DumpHdcpStatus(UINT32 uiPortIndex, UINT32 uiDumpOption)
{
    if (uiPortIndex >= HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM)
    {
        HV_LOG_ERROR(HDCP,"invalid Hdcp port index %d", uiPortIndex);
        return;
    }
    UINT32 uiSize = 0;
    HdcpCtrlData* pstHdcpDataPtr = Hv_Mw_Hdcp_GetHdcpData(uiPortIndex & 0xFF);
    HV_ASSERT_VALID_PTR_VOID(pstHdcpDataPtr);

    HdcpRx* pstHdcpRx = &(pstHdcpDataPtr->stHdcpRx);

    if(HdcpVesion_Hdcp1X == pstHdcpDataPtr->enHdcpVersion)
    {
        HV_LOG_ERROR(HDCP, "HDCP 1X is Working");
        Hv_Shell_HdcpDebug_Dump1XStatus(&(pstHdcpDataPtr->stHdcpRx.stHdcp1xRx), pstHdcpDataPtr->ucHdcpIndex);
    }

    if(HdcpVesion_Hdcp2X == pstHdcpDataPtr->enHdcpVersion)
    {
        HV_LOG_ERROR(HDCP, "HDCP 2X is Working with Status:%d and MsgStatus:%d .", 
                      pstHdcpDataPtr->enCurrentPort2xState, pstHdcpDataPtr->stHdcpRx.enHdcp2xMsgState);

        Hv_Shell_HdcpDebug_Dump2XStatus(&(pstHdcpRx->stHdcp2xRx));

        if(pstHdcpDataPtr->stHdcpRx.enKmStoreState)
        {
            if(HV_SUCCESS == Hv_Engine_RxRsaesOaepDecrypt(&pstHdcpRx->stHdcp2xRx, (HDCP2X_RX_KPriv *)(pstHdcpRx->stHdcp2xRx.pucPrivKey),
                    pstHdcpRx->stHdcp2xRx.stParams.EKpubKm, pstHdcpRx->stHdcp2xRx.stParams.Km, &uiSize))
            {
                Hv_Engine_RxComputeHPrime(pstHdcpRx->stHdcp2xRx.stParams.Rrx, pstHdcpRx->stHdcp2xRx.stParams.RxCaps,
                pstHdcpRx->stHdcp2xRx.stParams.Rtx, pstHdcpRx->stHdcp2xRx.stParams.TxCaps, pstHdcpRx->stHdcp2xRx.stParams.Km,
                pstHdcpRx->stHdcp2xRx.stParams.HPrime);

            }
            else
            {
                HV_LOG_ERROR(HDCP, "RsaesOaepDecrypt Failed!\n");
            }
        }
        else
        {
            Hv_Engine_RxComputeEkh(pstHdcpRx->stHdcp2xRx.pucPrivKey, pstHdcpRx->stHdcp2xRx.stParams.EKhKm,
                pstHdcpRx->stHdcp2xRx.stParams.M, pstHdcpRx->stHdcp2xRx.stParams.Km);

            Hv_Engine_RxComputeHPrime(pstHdcpRx->stHdcp2xRx.stParams.Rrx, pstHdcpRx->stHdcp2xRx.stParams.RxCaps,
            pstHdcpRx->stHdcp2xRx.stParams.Rtx, pstHdcpRx->stHdcp2xRx.stParams.TxCaps, pstHdcpRx->stHdcp2xRx.stParams.Km,
            pstHdcpRx->stHdcp2xRx.stParams.HPrime);
        }
/*
        HV_LOG_BUF_PRINT("EKpubKm:", pstHdcpRx->stHdcp2xRx.stParams.EKpubKm, sizeof(pstHdcpRx->stHdcp2xRx.stParams.EKpubKm));
        HV_LOG_BUF_PRINT("Rrx:",pstHdcpRx->stHdcp2xRx.stParams.Rrx, sizeof(pstHdcpRx->stHdcp2xRx.stParams.Rrx));
        HV_LOG_BUF_PRINT("RxCaps:",pstHdcpRx->stHdcp2xRx.stParams.RxCaps, sizeof(pstHdcpRx->stHdcp2xRx.stParams.RxCaps));
        HV_LOG_BUF_PRINT("Rtx:",pstHdcpRx->stHdcp2xRx.stParams.Rtx, sizeof(pstHdcpRx->stHdcp2xRx.stParams.Rtx));
        HV_LOG_BUF_PRINT("TxCaps:",pstHdcpRx->stHdcp2xRx.stParams.TxCaps, sizeof(pstHdcpRx->stHdcp2xRx.stParams.TxCaps));
        HV_LOG_BUF_PRINT("Km:", pstHdcpRx->stHdcp2xRx.stParams.Km, sizeof(pstHdcpRx->stHdcp2xRx.stParams.Km));
*/
        HV_LOG_INFO(HDCP, "\nAfter Caculate the Status Details is :");
        Hv_Shell_HdcpDebug_Dump2XStatus(&(pstHdcpRx->stHdcp2xRx));
    }
    
    return;
}
void Hv_Shell_HdcpDebug_DumpPortData(UINT32 uiPortIndex, UINT32 uiDumpOption)
{
    if (uiPortIndex >= HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM)
    {
        HV_LOG_ERROR(HDCP,"invalid Hdcp port index %d\n", uiPortIndex);
        return;
    }

    HdcpCtrlData* pstHdcpDataPtr = Hv_Mw_Hdcp_GetHdcpData(uiPortIndex & 0xFF);
    HV_ASSERT_VALID_PTR_VOID(pstHdcpDataPtr);

    HV_LOG_ERROR(HDCP,"\nFollowing is the Hdcp: %d information:", uiPortIndex);
    HV_LOG_ERROR(HDCP,"enCurrentPortState : %d ", pstHdcpDataPtr->enCurrentPort2xState);
    HV_LOG_ERROR(HDCP,"InputPort Info: LID:%d and PID:%d .", pstHdcpDataPtr->enInputPortIndex, pstHdcpDataPtr->ucPortIndex);
    HV_LOG_ERROR(HDCP,"HDCP Version : %d and Type:%d. Status Detail:", pstHdcpDataPtr->enHdcpVersion, pstHdcpDataPtr->enHdcpPortType);

    return;
}

void Hv_Shell_HdcpDebug_DumpKey(HDCPKeyDataDBRegion *pstKey)
{
    HDCP2X_RX_KPriv *pstKpriv = (HDCP2X_RX_KPriv *)(pstKey->stHdcp2xRxKey.aucHdcp2xRxPrivKey);
    UCHAR8 *pucChar = pstKey->stHdcp1xRxKey.aucBksv;

    HV_LOG_INFO(HDCP, "\n HDCP Key is Following: ");
    HV_LOG_INFO(HDCP, "1X: Bksv:[5]= %02x %02x %02x %02x %02x", pucChar[0], pucChar[1], pucChar[2], pucChar[3], pucChar[4]);
    HV_LOG_BUF_PRINT("1X: Bksv:", pstKey->stHdcp1xRxKey.aucBksv, HV_HDCP_1X_SIZE_BKSV);
    HV_LOG_BUF_PRINT("1X: Dks:", pstKey->stHdcp1xRxKey.aucDks, HV_HDCP_1X_SIZE_DKS_BYTE);
    HV_LOG_BUF_PRINT("2X: RxLc128:", pstKey->stHdcp2xRxKey.aucHdcp2xRxLc128, HV_HDCP_2X_RX_LC128_SIZE);
    HV_LOG_BUF_PRINT("2X: RxCert:", pstKey->stHdcp2xRxKey.aucHdcp2xRxCert, HV_HDCP_2X_RX_CERT_SIZE);
    HV_LOG_BUF_PRINT("2X: RxPrivKey:", pstKey->stHdcp2xRxKey.aucHdcp2xRxPrivKey, HV_HDCP_2X_RX_PRIVATEKEY_SIZE);

    HV_LOG_BUF_PRINT("pstKpriv->p:", pstKpriv->p, HV_HDCP_2X_RX_P_SIZE);
    HV_LOG_BUF_PRINT("pstKpriv->q", pstKpriv->q, HV_HDCP_2X_RX_Q_SIZE);
    HV_LOG_BUF_PRINT("pstKpriv->dp:", pstKpriv->dp, HV_HDCP_2X_RX_DP_SIZE);
    HV_LOG_BUF_PRINT("pstKpriv->dq:", pstKpriv->dq, HV_HDCP_2X_RX_DQ_SIZE);
    HV_LOG_BUF_PRINT("pstKpriv->qinv:", pstKpriv->qinv, HV_HDCP_2X_RX_QINV_SIZE);

    return;
}

void Hv_Shell_HdcpDebug_DumpHdcpKey()
{
    HDCPManager* pstHdcpMngPtr = Hv_Mw_Hdcp_GetHdcpManager();
    HDCP2X_RX_KPriv *pstKpriv = (HDCP2X_RX_KPriv *)(pstHdcpMngPtr->pstHdcpKey->stHdcp2xRxKey.aucHdcp2xRxPrivKey);
    UCHAR8 *pucChar = pstHdcpMngPtr->pstHdcpKey->stHdcp1xRxKey.aucBksv;

    HV_LOG_INFO(HDCP, "\n HDCP Key is Following: ");
    HV_LOG_INFO(HDCP, "1X: Bksv:[5]= %02x %02x %02x %02x %02x", pucChar[0], pucChar[1], pucChar[2], pucChar[3], pucChar[4]);
    HV_LOG_BUF_PRINT("1X: Bksv:", pstHdcpMngPtr->pstHdcpKey->stHdcp1xRxKey.aucBksv, HV_HDCP_1X_SIZE_BKSV);
    HV_LOG_BUF_PRINT("1X: Dks:", pstHdcpMngPtr->pstHdcpKey->stHdcp1xRxKey.aucDks, HV_HDCP_1X_SIZE_DKS_BYTE);
    HV_LOG_BUF_PRINT("2X: RxLc128:", pstHdcpMngPtr->pstHdcpKey->stHdcp2xRxKey.aucHdcp2xRxLc128, HV_HDCP_2X_RX_LC128_SIZE);
    HV_LOG_BUF_PRINT("2X: RxCert:", pstHdcpMngPtr->pstHdcpKey->stHdcp2xRxKey.aucHdcp2xRxCert, HV_HDCP_2X_RX_CERT_SIZE);
    HV_LOG_BUF_PRINT("2X: RxPrivKey:", pstHdcpMngPtr->pstHdcpKey->stHdcp2xRxKey.aucHdcp2xRxPrivKey, HV_HDCP_2X_RX_PRIVATEKEY_SIZE);

    HV_LOG_BUF_PRINT("pstKpriv->p:", pstKpriv->p, HV_HDCP_2X_RX_P_SIZE);
    HV_LOG_BUF_PRINT("pstKpriv->q", pstKpriv->q, HV_HDCP_2X_RX_Q_SIZE);
    HV_LOG_BUF_PRINT("pstKpriv->dp:", pstKpriv->dp, HV_HDCP_2X_RX_DP_SIZE);
    HV_LOG_BUF_PRINT("pstKpriv->dq:", pstKpriv->dq, HV_HDCP_2X_RX_DQ_SIZE);
    HV_LOG_BUF_PRINT("pstKpriv->qinv:", pstKpriv->qinv, HV_HDCP_2X_RX_QINV_SIZE);

    HV_LOG_BUF_PRINT("stMmult: AmodP:", pstHdcpMngPtr->stMmult.AmodP, HV_HDCP_2X_RX_MOD_SIZE << 2);
    HV_LOG_BUF_PRINT("stMmult: AmodQ:", pstHdcpMngPtr->stMmult.AmodQ, HV_HDCP_2X_RX_MOD_SIZE << 2);
    HV_LOG_BUF_PRINT("stMmult: CmodP:", pstHdcpMngPtr->stMmult.CmodP, HV_HDCP_2X_RX_MOD_SIZE << 2);
    HV_LOG_BUF_PRINT("stMmult: CmodQ:", pstHdcpMngPtr->stMmult.CmodQ, HV_HDCP_2X_RX_MOD_SIZE << 2);

    return;
}

#define ENCRY_KEY_FOR_HDCP    0x62986443
extern const UCHAR8 aucAESKey[16];

#if (HV_PROJECT_CONFIG_HDCP_KEY_CLEAR_TEXT == HV_CONFIG_OFF)
extern Status Hv_Mw_Hdcp_DecryptKey(HDCPKeyDataDBRegion *pstHdcpKey);
VOID Hv_Comm_EncryHdcpKey(HDCPKeyDataDBRegion *pstHdcpKey);
#endif
void Hv_Shell_HdcpDebug_UserTest(UCHAR8 ucPortIndex)
{
    //HdcpCtrlData* pstHdcpData = Hv_Mw_Hdcp_GetHdcpData(0);
#if 0
    UCHAR8 aucAKsv[5];
    UCHAR8 aucAn[8];
    HdcpRx* pstHdcpRx = &(pstHdcpData->stHdcpRx);

    Hv_Drv_Hdcp1x_HdmiGetRxAksvFromReg(aucAKsv, ucPortIndex);
    Hv_Drv_Hdcp1x_HdmiGetRxAksvFromReg(pstHdcpRx->stHdcp1xRx.stParams.AKsv, ucPortIndex);
    Hv_Drv_Hdcp1x_HdmiGetRxAnFromReg(aucAn, ucPortIndex);
    Hv_Drv_Hdcp1x_HdmiGetRxAnFromReg(pstHdcpRx->stHdcp1xRx.stParams.An, ucPortIndex);

    UCHAR8 *pucPrint = aucAn;
    HV_LOG_ERROR(HDCP, "An:%x %x %x %x %x %x %x %x",pucPrint[0],pucPrint[1],pucPrint[2],pucPrint[3],pucPrint[4],pucPrint[5],pucPrint[6],pucPrint[7]);
    pucPrint = aucAKsv;
    HV_LOG_ERROR(HDCP, "AKsv:%x %x %x %x %x",pucPrint[0],pucPrint[1],pucPrint[2],pucPrint[3],pucPrint[4]);

    pucPrint = pstHdcpRx->stHdcp1xRx.stParams.An;
    HV_LOG_ERROR(HDCP, "An:%x %x %x %x %x %x %x %x",pucPrint[0],pucPrint[1],pucPrint[2],pucPrint[3],pucPrint[4],pucPrint[5],pucPrint[6],pucPrint[7]);
    pucPrint = pstHdcpRx->stHdcp1xRx.stParams.AKsv;
    HV_LOG_ERROR(HDCP, "AKsv:%x %x %x %x %x",pucPrint[0],pucPrint[1],pucPrint[2],pucPrint[3],pucPrint[4]);
#endif
    HDCPKeyDataDBRegion *pstHdcpKeyData = &gstTestHDCPDataDBRegion.stKey;
    
#if (HV_PROJECT_CONFIG_HDCP_KEY_CLEAR_TEXT == HV_CONFIG_OFF)
    HV_LOG_ERROR(HDCP,"HDCPKeyDataDBRegion: %d", sizeof(HDCPKeyDataDBRegion));

    HV_LOG_ERROR(HDCP, "Hv_Comm_EncryHdcpKey With CheckSum:0x%x(0xec53014f).", pstHdcpKeyData->uiCheckSum);
    HV_LOG_ERROR(HDCP, "1X BKSV With 0x%x,0x%x,0x%x,0x%x,0x%x."
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[0]
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[1]
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[2]
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[3]
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[4]);

    Hv_Comm_EncryHdcpKey(pstHdcpKeyData);
    HV_LOG_ERROR(HDCP, "Hv_Comm_EncryHdcpKey With CheckSum:0x%x(0xd6927efc).", pstHdcpKeyData->uiCheckSum);
    Hv_Shell_HdcpDebug_DumpKey(pstHdcpKeyData);

    if(HV_SUCCESS != Hv_Mw_Hdcp_DecryptKey(pstHdcpKeyData))
    {
        HV_LOG_ERROR(HDCP, "Hv_Mw_Hdcp_DecryptKey ERROR.");
    }

    HV_LOG_ERROR(HDCP, "1X BKSV With 0x%x,0x%x,0x%x,0x%x,0x%x."
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[0]
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[1]
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[2]
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[3]
    , pstHdcpKeyData->stHdcp1xRxKey.aucBksv[4]);
#endif
    return;
}

static CHAR8* GetHdcpKeyCheckResult(HdcpKeyCheckResult bResult)
{
    if(HV_HDCP_KEY_VALID == bResult)
    {
        return "HDCP KEY VALID";
    }
    else if(HV_HDCP_KEY_INVALID == bResult)
    {
        return "HDCP KEY INVALID";
    }
    else if(HV_HDCP_KEY_DEFAULT == bResult)
    {
        return "HDCP KEY DEFAULT";
    }
    else if(HV_HDCP_KEY_EMPTY == bResult)
    {
        return "HDCP KEY EMPTY";
    }

    return "unknown";
}

void Hv_Shell_HdcpDebug_CheckHdcpKey()
{
    HdcpKeyCheckResult eResult = Hv_Mw_Hdcp_CheckHdcpKey(HdcpVesion_Hdcp1X);
    
    HV_LOG_ERROR(HDCP, "HDCP1X %s", GetHdcpKeyCheckResult(eResult));
    eResult = Hv_Mw_Hdcp_CheckHdcpKey(HdcpVesion_Hdcp2X);
    HV_LOG_ERROR(HDCP, "HDCP2X %s", GetHdcpKeyCheckResult(eResult));
    return;
}

/*
 * hdcpdbeug -d 0
 */
int Hv_Shell_Hdcp_RxDebug(int argc, char* argv[])
{
    int         c = -1;
    UCHAR8      ucHdCPPortIndex = HV_BOARD_CONFIG_MAX_HDCP_PORT_NUM;
    UCHAR8      ucPortIndex = 1;
    while ((c = Hv_getOpt(argc, argv, "d:k:t:h:s:c:")) != -1)
    {
        switch(c)
        {
            case 'd':
                ucHdCPPortIndex = atoi(hv_optarg) & 0xFF;
                Hv_Shell_HdcpDebug_DumpPortData(ucHdCPPortIndex, 0);
                Hv_Shell_HdcpDebug_DumpHdcpStatus(ucHdCPPortIndex, 0);
                break;

            case 'k':
                Hv_Shell_HdcpDebug_DumpHdcpKey();
                break;

            case 'h':
                Hv_Shell_HdcpDebug_Usage();
                break;

            case 't':
                ucPortIndex = atoi(hv_optarg) & 0xFF;
                Hv_Shell_HdcpDebug_UserTest(ucPortIndex);
                break;

            case 's':
                ucPortIndex = atoi(hv_optarg) & 0xFF;
                Hv_Shell_HdcpDebug_DumpHdcpStatus(ucPortIndex, 0);
                break;

            case 'c':
                ucPortIndex = atoi(hv_optarg) & 0xFF;
                Hv_Shell_HdcpDebug_CheckHdcpKey();
                break;

            default:
                HV_LOG_ERROR(HDCP,"Command Error. Please reference the usage:\n", c);
                Hv_Shell_HdcpDebug_Usage();
                break;
        }
    }

    hv_optind = 1;
    hv_optarg = NULL;

    return 0;
}

//HV_SHELL_EXPORT_CMD(hdcpdebug, Hv_Shell_Hdcp_RxDebug, "hdcpdebug command line");

//HV_SHELL_EXPORT_CMD(hdcpdebug, Hv_Mw_Hdcp_RxDebug, "hdcpdebug command line");

#endif

