/*
 * Copyright (c) 2020, MediaTek Inc. All rights reserved.
 *
 * This software/firmware and related documentation ("MediaTek Software") are
 * protected under relevant copyright laws.
 * The information contained herein is confidential and proprietary to
 * MediaTek Inc. and/or its licensors.
 * Except as otherwise provided in the applicable licensing terms with
 * MediaTek Inc. and/or its licensors, any reproduction, modification, use or
 * disclosure of MediaTek Software, and information contained herein, in whole
 * or in part, shall be strictly prohibited.
*/
#include <asm/byteorder.h>
#include <common.h>
#include <command.h>
#include <debug_impl.h>
#include <ecinfo.h>
#include <environment.h>

#include <mapmem.h>
#include <malloc.h>
#include <memalign.h>

#include <stddef.h>

#include <vsprintf.h>
#include <utility.h>
#include <sony_customer.h>
#include <dts_parser.h>

#define SetBootFile "setBoot"
#define KEY_SEPARATOR ":"
#define SEPARATOR_OFFSET 1
#define ANDROIDBOOT_PREFIX "androidboot."
//#define Cus_SOTUNER "sotuner."
#define Cus_SOPQ "sopq."
#define UnderLine "_"
#define MAX_PanelIndex_Length 6
#define LdDataIndex_Length 8

/* EC Debug Ready */
//int EC_DEBUG_PREPARE = 0;
int EC_SETBOOT_FINISH = 0;
char PanelIndex[6] = "9999";

int setBoot(void){

    UBOOT_TRACE("EC setBoot IN \n");

//    char * PanelIndex = get_panel_index_buf();
    if(PanelIndex != NULL) {
        if(strcmp(PanelIndex, "9999") != 0) {
            UBOOT_DEBUG("EC_DEBUG Current PanelIndex is %s \n", PanelIndex);
        }
    } else {
        printf("EC_DEBUG Current PanelIndex is %s skip setBoot, please check PanelIndex\n", PanelIndex);
        return 5;
    }

    if(EC_SETBOOT_FINISH == 0) {
        UBOOT_DEBUG("EC setBoot only execute once, this is first time\n");
        EC_SETBOOT_FINISH = 1;
    } else {
        UBOOT_DEBUG("EC setBoot only execute once, this is second time\n");
        return 3;
    }

    int NumECMap, i;
    char SetBootMapRes[128][128];
    char * buf = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * separator_p = NULL;
    char * separate_value = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * ec_key = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * ec_value = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * subec_bootconfig = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * subec_cmdline = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * sopq_key = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * sopq_host = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * sopq_pqu = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * sopq_hal = (char *) malloc(MAX_VALUE_LENGTH + 1);
    UBOOT_DEBUG("EC_DEBUG Current PanelIndex %s\n", PanelIndex);
    char SOPQ_DVCFG_CONST_KEY[MAX_VALUE_LENGTH] = "PQ_DVCFG_";
    char SOPQ_FlashPQ_CONST_KEY[MAX_VALUE_LENGTH] = "PQ_QMAP_";
    char SOPQ_SOPQD_CONST_KEY[MAX_VALUE_LENGTH] = "PQ_SONY_";
    char SOPQ_LDM_CONST_KEY[MAX_VALUE_LENGTH] = "PQ_LD_";
    char SOPQ_HOST_PanelIndex[MAX_VALUE_LENGTH] = "SOPQ_HOST_";
    char SOPQ_PQU_PanelIndex[MAX_VALUE_LENGTH] = "SOPQ_PQU_";
    char SOPQ_HAL_PanelIndex[MAX_VALUE_LENGTH] = "SOPQ_HAL_";
    char * SubECValue = (char *) malloc(MAX_VALUE_LENGTH + 1);
    memset(buf, '\0', MAX_VALUE_LENGTH + 1);
    memset(SubECValue, '\0', MAX_VALUE_LENGTH + 1);
    memset(ec_key, '\0', MAX_VALUE_LENGTH + 1);
    memset(separate_value, '\0', MAX_VALUE_LENGTH + 1);
    memset(ec_value, '\0', MAX_VALUE_LENGTH + 1);
    memset(subec_cmdline, '\0', MAX_VALUE_LENGTH + 1);
    memset(sopq_key, '\0', MAX_VALUE_LENGTH + 1);
    memset(sopq_host, '\0', MAX_VALUE_LENGTH + 1);
    memset(sopq_pqu, '\0', MAX_VALUE_LENGTH + 1);
    memset(sopq_hal, '\0', MAX_VALUE_LENGTH + 1);
    memset(subec_bootconfig, '\0', MAX_VALUE_LENGTH + 1);
    strcpy(subec_bootconfig, ANDROIDBOOT_PREFIX); //recover to be "androidboot."

    subec_map_result(SetBootFile, SetBootMapRes, &NumECMap);

    for(i = 0; i < NumECMap; i ++){
        memset(buf, '\0', MAX_VALUE_LENGTH + 1);
        memset(SubECValue, '\0', MAX_VALUE_LENGTH + 1);
        memset(ec_key, '\0', MAX_VALUE_LENGTH + 1);
        memset(separate_value, '\0', MAX_VALUE_LENGTH + 1);
        memset(ec_value, '\0', MAX_VALUE_LENGTH + 1);
        memset(subec_cmdline, '\0', MAX_VALUE_LENGTH + 1);
        memset(sopq_key, '\0', MAX_VALUE_LENGTH + 1);
        memset(subec_bootconfig, '\0', MAX_VALUE_LENGTH + 1);
        strcpy(subec_bootconfig, ANDROIDBOOT_PREFIX); //recover to be "androidboot."

        separator_p = strstr(SetBootMapRes[i], KEY_SEPARATOR);
        if(separator_p == NULL)
           continue;

        strncpy(ec_key, SetBootMapRes[i], (separator_p - SetBootMapRes[i])); //EC_KEY:SUBEC_VALUE =>EC_KEY ld_variation
        strcpy(separate_value, separator_p + SEPARATOR_OFFSET); //EC_KEY:SUBEC_VALUE=>SUBEC_VALUE sopq.ld_variation
        if (get_ec_value(ec_key, ec_value, MAX_VALUE_LENGTH) < 0) {
            UBOOT_DEBUG("EC SetBoot cannot get ACK from main ec, using key \"%s\" continue\n",  ec_key);
            continue;
        } else {
            if(strstr(separate_value, Cus_SOPQ) != NULL) {
                if(strcmp(ec_key, "dolby_vision_variation") == 0) {
                    strcat(SOPQ_DVCFG_CONST_KEY, ec_value); //PQ_DVCFG_var009
                    strcat(SOPQ_DVCFG_CONST_KEY, UnderLine); //PQ_DVCFG_var009_
                    strcat(SOPQ_DVCFG_CONST_KEY, PanelIndex); //PQ_DVCFG_var009_0045
                    if (ExGetJsonValue("SOPQ_DVCFG", SOPQ_DVCFG_CONST_KEY, SubECValue, 0) < 0) {
                        UBOOT_DEBUG("cannot find %s from subec SOPQ_DVCFG, \"%s\" return\n", "dolby_vision_variation", SOPQ_DVCFG_CONST_KEY);
                        UBOOT_DEBUG("Turn to use default value dvcf_Default\n");
                        strcat(subec_bootconfig, separate_value); //androidboot.sopq.dolby_vision_variation
                        if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfig, "dvcf_Default") < 0){ //androidboot.sopq.ld_variation=ec_value
                            UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfig, "dvcf_Default");
                            continue;
                        }
                    } else {
                        strcat(subec_bootconfig, separate_value); //androidboot.sopq.dolby_vision_variation
                        UBOOT_DEBUG("EC SetBoot Map Success EC KEY %s PanelIndex %s \n", ec_key, PanelIndex);
                        UBOOT_DEBUG("EC SetBoot succeed to get Bootconfig #%d %s=%s\n", i, subec_bootconfig, SubECValue);
                        if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfig, SubECValue) < 0){ //androidboot.sopq.ld_variation=ec_value
                            UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfig, SubECValue);
                            continue;
                        }
                    }
                    add_bootargs(subec_bootconfig, buf, 0);
                    continue;
                } else if (strcmp(ec_key, "flashpq_variation") == 0) {
                    strcat(SOPQ_FlashPQ_CONST_KEY, ec_value); //PQ_QMAP_var009
                    strcat(SOPQ_FlashPQ_CONST_KEY, UnderLine); //PQ_QMAP_var009_
                    strcat(SOPQ_FlashPQ_CONST_KEY, PanelIndex); //PQ_QMAP_var009_0045
                    if (ExGetJsonValue("SOPQ_FlashPQ", SOPQ_FlashPQ_CONST_KEY, SubECValue, 0) < 0) {
                        UBOOT_DEBUG("cannot find %s from subec SOPQ_FlashPQ, \"%s\" return\n", "flashpq_variation", SOPQ_FlashPQ_CONST_KEY);
                        UBOOT_DEBUG("Turn to use default value 0001\n");
                        strcat(subec_bootconfig, separate_value); //androidboot.sopq.flashpq_variation
                        if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfig, "0001") < 0){ //androidboot.sopq.ld_variation=ec_value
                            UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfig, "0001");
                            continue;
                        }
                    } else {
                        strcat(subec_bootconfig, separate_value); //androidboot.sopq.dolby_vision_variation
                        UBOOT_DEBUG("EC SetBoot Map Success EC KEY %s PanelIndex %s \n", ec_key, PanelIndex);
                        UBOOT_DEBUG("EC SetBoot succeed to get Bootconfig #%d %s=%s\n", i, subec_bootconfig, SubECValue);
                        if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfig, SubECValue) < 0){ //androidboot.sopq.ld_variation=ec_value
                            UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfig, SubECValue);
                            continue;
                        }
                    }
                    add_bootargs(subec_bootconfig, buf, 0);
                    continue;
                } else if (strcmp(ec_key, "sopq_variation") == 0) {
                    strcpy(sopq_host, "androidboot.sopqhostdata");
                    strcpy(sopq_pqu, "androidboot.sopqpqudata");
                    strcpy(sopq_hal, "androidboot.sopqhaldata");
                    strcat(sopq_key, SOPQ_SOPQD_CONST_KEY); //PQ_SONY_
                    strcat(sopq_key, ec_value); //PQ_SONY_var009
                    strcat(sopq_key, UnderLine); //PQ_SONY_var009_
                    strcat(sopq_key, PanelIndex); //PQ_SONY_var009_0045
                    if(strncmp(&ec_value[12], "043", 3) == 0) {
                        strcat(SOPQ_HOST_PanelIndex, "043");
                    } else if (strncmp(&ec_value[12], "050", 3) == 0) {
                        strcat(SOPQ_HOST_PanelIndex, "050");
                    } else if (strncmp(&ec_value[12], "055", 3) == 0) {
                        strcat(SOPQ_HOST_PanelIndex, "055");
                    } else if (strncmp(&ec_value[12], "065", 3) == 0) {
                        strcat(SOPQ_HOST_PanelIndex, "065");
                    } else if (strncmp(&ec_value[12], "075", 3) == 0) {
                        strcat(SOPQ_HOST_PanelIndex, "075");
                    } else if (strncmp(&ec_value[12], "085", 3) == 0) {
                        strcat(SOPQ_HOST_PanelIndex, "085");
                    } else if (strncmp(&ec_value[12], "100", 3) == 0) {
                        strcat(SOPQ_HOST_PanelIndex, "100");
                    }
                    if (ExGetJsonValue(SOPQ_HOST_PanelIndex, sopq_key, SubECValue, 0) < 0) {
                        UBOOT_DEBUG("cannot find %s from subec SOPQ_SOPQD, \"%s\" return\n", SOPQ_HOST_PanelIndex, sopq_key);
                        strcpy(SubECValue, "0000");
                    }
                    UBOOT_DEBUG("EC SetBoot Map Success EC KEY %s PanelIndex %s \n", ec_key, PanelIndex);
                    UBOOT_DEBUG("EC SetBoot succeed to get Bootconfig #%d %s=%s\n", i, sopq_host, SubECValue);
                    if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", sopq_host, SubECValue) < 0){ //androidboot.sopqhostdata=ec_value
                        UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", sopq_host, SubECValue);
                        continue;
                    }
                    add_bootargs(sopq_host, buf, 0);

                    memset(sopq_key, '\0', MAX_VALUE_LENGTH + 1);
                    strcat(sopq_key, SOPQ_SOPQD_CONST_KEY); //PQ_SONY_
                    strcat(sopq_key, ec_value); //PQ_SONY_var009
                    strcat(sopq_key, UnderLine); //PQ_SONY_var009_
                    strcat(sopq_key, PanelIndex); //PQ_SONY_var009_0045
                    if(strncmp(&ec_value[12], "043", 3) == 0) {
                        strcat(SOPQ_PQU_PanelIndex, "043");
                    } else if (strncmp(&ec_value[12], "050", 3) == 0) {
                        strcat(SOPQ_PQU_PanelIndex, "050");
					} else if (strncmp(&ec_value[12], "055", 3) == 0) {
                        strcat(SOPQ_PQU_PanelIndex, "055");
					} else if (strncmp(&ec_value[12], "065", 3) == 0) {
                        strcat(SOPQ_PQU_PanelIndex, "065");
					} else if (strncmp(&ec_value[12], "075", 3) == 0) {
                        strcat(SOPQ_PQU_PanelIndex, "075");
					} else if (strncmp(&ec_value[12], "085", 3) == 0) {
                        strcat(SOPQ_PQU_PanelIndex, "085");
					} else if (strncmp(&ec_value[12], "100", 3) == 0) {
                        strcat(SOPQ_PQU_PanelIndex, "100");
					}
                    if (ExGetJsonValue(SOPQ_PQU_PanelIndex, sopq_key, SubECValue, 0) < 0) {
                        UBOOT_DEBUG("cannot find %s from subec SOPQ_SOPQD, \"%s\" return\n", SOPQ_PQU_PanelIndex, sopq_key);
                        strcpy(SubECValue, "0000");
                    }
                    UBOOT_DEBUG("EC SetBoot Map Success EC KEY %s PanelIndex %s \n", ec_key, PanelIndex);
                    UBOOT_DEBUG("EC SetBoot succeed to get Bootconfig #%d %s=%s\n", i, sopq_pqu, SubECValue);
                    if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", sopq_pqu, SubECValue) < 0){ //androidboot.sopq.ld_variation=ec_value
                        UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", sopq_pqu, SubECValue);
                        continue;
                    }
                    add_bootargs(sopq_pqu, buf, 0);

                    memset(sopq_key, '\0', MAX_VALUE_LENGTH + 1);
                    strcat(sopq_key, SOPQ_SOPQD_CONST_KEY); //PQ_SONY_
                    strcat(sopq_key, ec_value); //PQ_SONY_var009
                    strcat(sopq_key, UnderLine); //PQ_SONY_var009_
                    strcat(sopq_key, PanelIndex); //PQ_SONY_var009_0045
                    if(strncmp(&ec_value[12], "043", 3) == 0) {
                        strcat(SOPQ_HAL_PanelIndex, "043");
                    } else if (strncmp(&ec_value[12], "050", 3) == 0) {
                        strcat(SOPQ_HAL_PanelIndex, "050");
                    } else if (strncmp(&ec_value[12], "055", 3) == 0) {
                        strcat(SOPQ_HAL_PanelIndex, "055");
                    } else if (strncmp(&ec_value[12], "065", 3) == 0) {
                        strcat(SOPQ_HAL_PanelIndex, "065");
                    } else if (strncmp(&ec_value[12], "075", 3) == 0) {
                        strcat(SOPQ_HAL_PanelIndex, "075");
                    } else if (strncmp(&ec_value[12], "085", 3) == 0) {
                        strcat(SOPQ_HAL_PanelIndex, "085");
                    } else if (strncmp(&ec_value[12], "100", 3) == 0) {
                        strcat(SOPQ_HAL_PanelIndex, "100");
                    }
                    if (ExGetJsonValue(SOPQ_HAL_PanelIndex, sopq_key, SubECValue, 0) < 0) {
                        UBOOT_DEBUG("cannot find %s from subec SOPQ_SOPQD, \"%s\" return\n", SOPQ_HAL_PanelIndex, sopq_key);
                        strcpy(SubECValue, "0000");
                    }
                    UBOOT_DEBUG("EC SetBoot Map Success EC KEY %s PanelIndex %s \n", ec_key, PanelIndex);
                    UBOOT_DEBUG("EC SetBoot succeed to get Bootconfig #%d %s=%s\n", i, sopq_hal, SubECValue);
                    if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", sopq_hal, SubECValue) < 0){ //androidboot.sopqhaldata=ec_value
                        UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", sopq_hal, SubECValue);
                        continue;
                    }
                    add_bootargs(sopq_hal, buf, 0);
                    continue;
                } else if (strcmp(ec_key, "ld_variation") == 0) {
                    strcat(SOPQ_LDM_CONST_KEY, ec_value); //PQ_LD_var009
                    strcat(SOPQ_LDM_CONST_KEY, UnderLine); //PQ_LD_var009_
                    strcat(SOPQ_LDM_CONST_KEY, PanelIndex); //PQ_LD_var009_0045
                    if (ExGetJsonValue("SOPQ_LDM", SOPQ_LDM_CONST_KEY, SubECValue, 0) < 0) {
                        UBOOT_DEBUG("cannot find %s from subec SOPQ_LDM, \"%s\" return\n", "ld_variation", SOPQ_LDM_CONST_KEY);
                        UBOOT_DEBUG("Turn to use default value AAA_0043_AAA_0043_AAA_0043_AAA_0043\n");
                        strcpy(SubECValue, "AAA_0043_AAA_0043_AAA_0043_AAA_0043");
                        strcat(subec_bootconfig, separate_value); //androidboot.sopq.ld_variation
                        if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfig, "AAA_0043_AAA_0043_AAA_0043_AAA_0043") < 0){ //androidboot.sopq.ld_variation=ec_value
                            UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfig, "AAA_0043_AAA_0043_AAA_0043_AAA_0043");
                            continue;
                        }
                    } else {
                        strcat(subec_bootconfig, separate_value); //androidboot.sopq.dolby_vision_variation
                        UBOOT_DEBUG("EC SetBoot Map Success EC KEY %s PanelIndex %s \n", ec_key, PanelIndex);
                        UBOOT_DEBUG("EC SetBoot succeed to get Bootconfig #%d %s=%s\n", i, subec_bootconfig, SubECValue);
                        if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfig, SubECValue) < 0){ //androidboot.sopq.ld_variation=ec_value
                            UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfig, SubECValue);
                            continue;
                        }
                    }
                    add_bootargs(subec_bootconfig, buf, 0);
                    memset(buf, '\0', sizeof(buf));
                    memset(ec_key, '\0', sizeof(ec_key));
                    memset(separate_value, '\0', sizeof(separate_value));
                    memset(ec_value, '\0', sizeof(ec_value));
                    memset(subec_bootconfig, '\0', sizeof(subec_bootconfig));
                    strcpy(subec_bootconfig, ANDROIDBOOT_PREFIX); //recover to be "androidboot."
                    char subec_bootconfigEdge2D[128] = "mediatek_drm_tv.Edge2D";
                    char subec_bootconfigCompensation[128] = "mediatek_drm_tv.Compensation";
                    char subec_bootconfigBacklightGamma[128] = "mediatek_drm_tv.BacklightGamma";
                    char bufEdge2D[128] = "\0";
                    char bufCompensation[128] = "\0";
                    char bufBacklightGamma[128] = "\0";
                    char buf_LDM[MAX_VALUE_LENGTH] = "\0";
                    strncpy(bufEdge2D, &SubECValue[0], LdDataIndex_Length);
                    if (snprintf(buf_LDM, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfigEdge2D, bufEdge2D) < 0){ //androidboot.sopq.ld_variation=ec_value
                        UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfigEdge2D, bufEdge2D);
                        continue;
                    }
                    add_bootargs(subec_bootconfigEdge2D, buf_LDM, 0);
                    strncpy(bufCompensation, &SubECValue[9], LdDataIndex_Length);
                    if (snprintf(buf_LDM, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfigCompensation, bufCompensation) < 0){ //androidboot.sopq.ld_variation=ec_value
                        UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfigCompensation, bufCompensation);
                        continue;
                    }
                    add_bootargs(subec_bootconfigCompensation, buf_LDM, 0);
                    strncpy(bufBacklightGamma, &SubECValue[18], LdDataIndex_Length);
                    if (snprintf(buf_LDM, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfigBacklightGamma, bufBacklightGamma) < 0){ //androidboot.sopq.ld_variation=ec_value
                        UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfigBacklightGamma, bufBacklightGamma);
                        continue;
                    }
                    add_bootargs(subec_bootconfigBacklightGamma, buf_LDM, 0);
                    memset(SubECValue, '\0', sizeof(SubECValue) + 1);
                    continue;
                }
            }

            strcat(subec_bootconfig, separate_value); //androidboot.sopq.ld_variation
            UBOOT_DEBUG("EC SetBoot succeed to get Bootconfig #%d %s=%s\n", i, subec_bootconfig, ec_value);
            if (snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", subec_bootconfig, ec_value) < 0){ //androidboot.sopq.ld_variation=ec_value
                UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_bootconfig, ec_value);
                continue;
            }
            add_bootargs(subec_bootconfig, buf, 0);
            memset(buf, '\0', MAX_VALUE_LENGTH + 1);
/*
            if(strstr(separate_value, Cus_SOTUNER) != NULL) {
                if(strstr(separate_value, "sotuner.trap_analog_vhf_low_config")){
                   strcat(subec_cmdline, "avlc");
                } else if(strstr(separate_value, "sotuner.trap_analog_vhf_high_config")){
                   strcat(subec_cmdline, "avhc");
                } else if(strstr(separate_value, "sotuner.trap_analog_uhf_config")){
                   strcat(subec_cmdline, "auc");
                } else if(strstr(separate_value, "sotuner.trap_digital_vhf_low_config")){
                   strcat(subec_cmdline, "dvlc");
                } else if(strstr(separate_value, "sotuner.trap_digital_vhf_high_config")){
                   strcat(subec_cmdline, "dvhc");
                } else if(strstr(separate_value, "sotuner.trap_digital_uhf_config")){
                   strcat(subec_cmdline, "duc");
                }
                UBOOT_DEBUG("EC SetBoot succeed to get sotuner cmdline  #%d %s=%s\n", i, subec_cmdline, ec_value);
                if(snprintf(buf, MAX_VALUE_LENGTH, "%s=%s", subec_cmdline, ec_value) < 0){
                   UBOOT_DEBUG("EC SetBoot cannot generate %s from %s\n", subec_cmdline, ec_value);
                   continue;
                }
                add_bootargs(subec_cmdline, buf, 0);
                continue;
            }
*/
        }
    }
    UBOOT_TRACE("EC setBoot OUT \n");

    return CMD_RET_SUCCESS;
}
int do_ec_uboot_debug(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]){
	uint32_t u32PanelIndex = 0;
	char *panel_index = NULL;

    if(EC_READY == 0) {
        UBOOT_TRACE("EC init fail !!\n");
        return 3;
    } else {
        UBOOT_TRACE("EC init succeed :)\n");
    }
/*
    if(EC_DEBUG_PREPARE == 0) {
        UBOOT_TRACE("EC_DEBUG_PREPARE is not ready !! Wait\n");
        EC_DEBUG_PREPARE = 1;
        return 3;
    } else {
        UBOOT_TRACE("EC Debug init succeed :)\n");
    }
*/
    if (parse_dt("/video_out/panel-cus-setting", integer_dt_parser, (void*)&u32PanelIndex, "PanelIndex") != 0) {
        UBOOT_ERROR("[%s][%d]parsing PanelIndex error\n", __FUNCTION__, __LINE__);
    }else{
        UBOOT_INFO("[%s][%d]parsing PanelIndex success u32PanelIndex:%d\n", __FUNCTION__, __LINE__, u32PanelIndex);
        if (snprintf(PanelIndex, 6, "%04u", u32PanelIndex) < 0){
            UBOOT_DEBUG("_parse_dts_info cannot generate %s \n", PanelIndex);
        }
        UBOOT_INFO("[%s][%d] u32PanelIndex:%s\n", __FUNCTION__, __LINE__, PanelIndex);
    }

    panel_index = env_get("panel_index");
    if (panel_index != NULL)
    {
        strncpy(PanelIndex, panel_index, 4);
    }

    if(strcmp(PanelIndex, "9999") != 0) {
        UBOOT_TRACE("EC_DEBUG Current PanelIndex is %s \n", PanelIndex);
        setBoot();
    } else {
        printf("EC_DEBUG Current PanelIndex is %s skip setBoot, please check PanelIndex\n", PanelIndex);
    }

    int ret = CMD_RET_USAGE;
    char buf[MAX_VALUE_LENGTH];
    /* EC Version */
    char * version = (char *) malloc(MAX_VERSION_LENGTH + 1);

    /* EC VALUE */
    char * value = (char *) malloc(MAX_VALUE_LENGTH + 1);
    char * shutdown_disable = (char *) malloc(MAX_VALUE_LENGTH + 1);

    if (get_ec_value("shutdown_disable", shutdown_disable, MAX_VALUE_LENGTH) < 0)
    {
        UBOOT_DEBUG("cannot read %s from ec, \"%s\" return\n", "shutdown_disable", shutdown_disable);
        return 1;
    } else {
        if (snprintf(buf, MAX_VALUE_LENGTH, "error_detect.shutdown_disable=%s", shutdown_disable) < 0){
            UBOOT_DEBUG("cannot generate %s from %s\n", "shutdown_disable", shutdown_disable);
            return 1;
        }
        add_bootargs("error_detect.shutdown_disable", buf, 0);
    }

    switch (argc){
        case 2:
            if(!strcmp(argv[1], "2")) {
                ret = get_ec_version(version, MAX_VERSION_LENGTH + 1);
                printf("EC DEBUG EC version %s\n", version);
                free(version);
            } else {
                printf("EC DEBUG Error input, try again\n");
            }
            break;
        case 3:
            if(!strcmp(argv[2], "version")) {
                ret = get_ec_version(version, MAX_VERSION_LENGTH + 1);
                printf("EC DEBUG EC version %s\n", version);
                free(version);
            } else {
                ret = get_ec_value(argv[2], value, MAX_VALUE_LENGTH);
                if (ret == 0) {
                    printf("EC DEBUG EC info check Json Pair \"%s\": \"%s\"\n", argv[2], value);
                    //map_deinit(&EC_MAP);  mark for loop debug
                    free(value);
                } else {
                    printf("EC DEBUG EC info check Json Pair not found!! input again!!\n");
                }
            }
            break;
    }

    return ret;
}
U_BOOT_CMD(
    ec_uboot_debug, 3, 1, do_ec_uboot_debug,
    "show EC infomation in Uboot.\nPlease Check init state first.\n",
    "<INTERFATE> <CMDTYPE> <CMD> <KEY>\n"
    "CMDTYPE: '1' get_ec_value, '2' get_ec_version\n"
    "CMD: '1' key, '2' version or empty\n"
    "KEY: key string\n"
);

int do_EXJsonDebug(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]){
/*
    if(EC_DEBUG_PREPARE == 0) {
        UBOOT_TRACE("Ex EC_DEBUG_PREPARE is not ready !!\n");
        return 1;
    } else {
        UBOOT_TRACE("Ex EC Debug init succeed :)\n");
    }
*/
    int ret = 5;
    /* Json Version */
    char * version = (char *) malloc(MAX_VERSION_LENGTH + 1);

    /* Json VALUE */
    char * value = (char *) malloc(MAX_VALUE_LENGTH + 1);

    if(EX_EC_READY == 0) {
        UBOOT_DEBUG("EXJson init fail !!\n");
        return 1;
    } else {
        UBOOT_DEBUG("EXJson succeed :)\n");
    }

    switch (argc){
        case 2:
            if(!strcmp(argv[1], "000")) {
                //ret = ExGetJsonVersion(version, MAX_VERSION_LENGTH + 1);
                UBOOT_DEBUG("Json Init Status %d\n", EX_EC_READY);
                free(version);
            } else {
                printf("EC DEBUG Error input, try again\n");
            }
            break;
        case 3:
            if(!strcmp(argv[1], "debug")) {
                //ret = ExGetJsonVersion(version, MAX_VERSION_LENGTH + 1);
                printf("ECJson Please input your Map Name %s\n", argv[2]);
                free(version);
            } else {
                /***************************************************************************
                *Expanding Json format 3
                *Open mboot Json debug .
                *Only support those files depoly in ext4 partition
                } else if(strcmp(argv[1], "JSONMAP_NAME") == 0) {
                    ret = ExGetJsonValue(argv[1], argv[2], value, MAX_VALUE_LENGTH);
                    if (ret == 0)
                        printf("ExGetJsonValue check Json Pair \"%s\": \"%s\"\n", argv[3], value);
                    else
                        printf("ExGetJsonValue Json Pair not found!! input again!!\n");
                    map_deinit(&JSONMAP_NAME);
                *How to get absolute path?
                *console# pwd
                ***************************************************************************/
                if(strcmp(argv[1], "SOPQ_FlashPQ") == 0) {
                    ret = ExGetJsonValue(argv[1], argv[2], value, MAX_VALUE_LENGTH);
                    if (ret == 0)
                        printf("ExGetJsonValue check Json Pair \"%s\": \"%s\"\n", argv[2], value);
                    else
                        printf("ExGetJsonValue Json Pair not found!! input again!!\n");
                    //map_deinit(&SOPQ_FlashPQ);
                } else if(strcmp(argv[1], "SOPQ_LDM") == 0) {
                    ret = ExGetJsonValue(argv[1], argv[2], value, MAX_VALUE_LENGTH);
                    if (ret == 0)
                        printf("ExGetJsonValue check Json Pair \"%s\": \"%s\"\n", argv[2], value);
                    else
                        printf("ExGetJsonValue Json Pair not found!! input again!!\n");
                    //map_deinit(&SOPQ_LDM);
                } else if(strcmp(argv[1], "SOPQ_SOPQD") == 0) {
                    ret = ExGetJsonValue(argv[1], argv[2], value, MAX_VALUE_LENGTH);
                    if (ret == 0)
                        printf("ExGetJsonValue check Json Pair \"%s\": \"%s\"\n", argv[2], value);
                    else
                        printf("ExGetJsonValue Json Pair not found!! input again!!\n");
                    //map_deinit(&SOPQ_SOPQD);
                }
                free(value);
            }
            break;
    }

    return ret;
}

U_BOOT_CMD(
    ExJsonDebug, 3, 1, do_EXJsonDebug,
    "show Json infomation in Uboot.\nPlease Check init state first.\n",
    "<INTERFATE> <JsonMapName> <KEY>\n"
    "JsonMapName: Json Map struct name in code\n"
    "KEY: key string\n"
);
