#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ospTypes.h"
#include "ospCfgToBin.h"
#include "ospHeap.h"
#include "ospSem.h"
#include "ospSoftQue.h"
#include "ospMsg.h"
#include "ucp_printf.h"

/***************************************************************************/
/*                             ARM  <--->  APE                             */
#define ARM_READ_CFG_FILE_OK    (0xA0A0A0A0)    /* ARM侧与APE侧约定 */
#define ARM_CFG_FILE_OFFSET     (0x00400000)    /* 配置文件起始地址相对于PHY空间，偏移4MB */

#define ARM_CFG_PHY_ADDR        (0x10400000)    /* 配置文件首地址 */
#define ARM_CFG_PHY_SIZE        (0x4000000)     /* 配置文件区域长度：64M */
#define APE_CFG_INFO_ONE        sizeof(osp_cfg_file_reg)

#define ARM_CFG_INDXN           (8)     /* 4字节：FlAG；4字节：NUM */
#define ARM_CFG_FLAG            (4)
#define ARM_CFG_NUM             (4)

#define ARM_CFG_INFO_ADDR       (ARM_CFG_PHY_ADDR + ARM_CFG_INDXN)    /* 配置文件头信息地址 */
#define ARM_CFG_INFO_SIZE       (APE_CFG_INFO_ONE * APE_CFG_FILE_NUM)

#define ARM_CFG_FILE_ADDR       (ARM_CFG_INFO_ADDR + ARM_CFG_INFO_SIZE) /* 配置文件文件信息地址           */
#define ARM_CFG_FILE_SIZE       (ARM_CFG_PHY_SIZE - ARM_CFG_INFO_SIZE -ARM_CFG_INDXN)   /* 配置文件文件总长度 */

/***************************************************************************/
/*                             ARM  <--->  ARM                             */
#define ARM_SPE_CFG_PHY_SIZE    (0x1400000)     /* 约定在20M以内 */
#define ARM_SPE_CFG_INFO_ONE    sizeof(osp_spe_cfg_file_reg)
#define ARM_SPE_CFG_INFO_SIZE   (ARM_SPE_CFG_INFO_ONE *  APE_CFG_FILE_NUM)
#define ARM_SPE_CFG_OFFSET      (0x0A000000)    /* 目前改到APE_TEXT段，从0x9A00 0000开始，故需要偏移0x0A00 0000 */

#if 0
#define ARM_SPE_CFG_PHY_ADDR    (0xB8000000)    /* 指定配置文件首地址 */
#define ARM_SPE_CFG_PHY_SIZE    (0x10000)       /* 指定配置文件区域长度：64K */

#define ARM_SPE_CFG_INFO_ADDR   (ARM_SPE_CFG_PHY_ADDR + ARM_CFG_INDXN)    /* 指定配置文件头信息地址 */

#define ARM_SPE_CFG_FILE_ADDR   (ARM_SPE_CFG_INFO_ADDR + ARM_SPE_CFG_INFO_SIZE)     /* 指定配置文件文件信息地址*/
#endif
/***************************************************************************/
/*                             ARM  <--->  APE                             */
osp_cfg_file_reg g_osp_cfg_file_reg[APE_CFG_FILE_NUM];
uint32_t g_osp_cfg_file_idx = 0;
void *g_ptr_static_mem = NULL;
uint64_t g_static_mem_size = 0;

/***************************************************************************/
/*                             ARM  <--->  ARM                             */
osp_spe_cfg_file_reg g_spe_osp_cfg_file_reg[APE_CFG_FILE_NUM];
uint32_t g_spe_osp_cfg_file_idx = 0;
void *g_spe_ptr_static_mem = NULL;
uint64_t g_spe_static_mem_size = 0;

/***************************************************************************/
/*                             ARM  <--->  APE                             */
/* ARM侧，配置文件模块初始化 */
int32_t osp_cfg_file_init()
{
    memset(&g_osp_cfg_file_reg, 0, sizeof(g_osp_cfg_file_reg));
    g_osp_cfg_file_idx = 0;
    g_static_mem_size  = 0;

    g_ptr_static_mem = get_static_mem(APE_PHY, &g_static_mem_size);
    if (0 == g_static_mem_size)
    {
        UCP_PRINT_ERROR("get_static_mem(APE_PHY) error......\r\n");
        return OSP_ERROR;
    }
    g_ptr_static_mem += ARM_CFG_FILE_OFFSET;    /* 在内存规划中，配置文件的起始地址需要偏移 */

    /* 先把NUM位写成0 */
    *(uint32_t*)(g_ptr_static_mem+ARM_CFG_NUM) = 0;

    return OSP_OK;
}

/***********************************************************/
/* 约定如下：
    1. 所有原始文件为文本文件
    2. 所有原始文件存放在目录： /ramfs/cfgDat/
    3. 所有原始文件都以.dat为后缀
    4. 需要将文本文件转化成二进制文件
    5. 所有二进制文件存放在目录： /ramfs/config/
*/

/* 将所有文本文件转化成二进制文件                */
int32_t osp_cfg_file_to_bin()
{
    FILE *file       = NULL; /* 遍历临时文件，获取所有文件名 */
    FILE *read_file  = NULL; /* 原始文件 */
    FILE *write_file = NULL; /* 目的文件 */
    uint8_t len = 0;
    uint8_t osp_cfg_file_idx = 0; /* 文件个数 */
    uint32_t value = 0;
    char *p_line_file_name = NULL;
    char line_file_name_full[APE_CFG_FILE_NAME_LEN*2] = {0};
    char write_file_name[APE_CFG_FILE_NAME_LEN] = {0};
    char line_buf[100] = {0};

    /* 遍历原始目录，获取所有文件的文件名及路径输出到:tmpfile */
    system("find /ramfs/cfgDat/* -name '*.dat' > tmpfile");
    
    /* 遍历原始目录，获取所有文件的文件名输出到:tmpfilename */
    //system("find /ramfs/cfgDat/* -name '*.dat' | sed 's#.*/##' > tmpfilename");

    /* 打开tmpfile内所有需要处理的文件 */
    file = fopen("./tmpfile", "rb");
    if (NULL == file)
    {
        UCP_PRINT_ERROR("Open file(tmpfile) error.\r\n");
        return OSP_FILE_ERROR;
    }
    /* 逐一转化（文本文件 --> 二进制文件               */
    while(osp_cfg_file_idx < APE_CFG_FILE_NUM)
    {
        /* 每行是原始文件的全路径            */
        if (NULL == fgets(line_file_name_full, sizeof(line_file_name_full), file))
        {
            /* 遍历至最后结束 */
            break;
        }
        p_line_file_name = strrchr(line_file_name_full, '/');
        p_line_file_name++;

        len = strlen(line_file_name_full);
        line_file_name_full[len - 1] = '\0';

        read_file = fopen(line_file_name_full, "rb");
        if (NULL == read_file)
        {
            UCP_PRINT_ERROR("Can not open src file: %s \r\n", line_file_name_full);
            fclose(file);
            return OSP_FILE_ERROR;
        }

        sprintf(write_file_name, "/ramfs/config/%s", p_line_file_name);
        UCP_PRINT_DEBUG("dst file: %s \r\n", write_file_name);

        write_file = fopen(write_file_name, "wb+");
        if (NULL == write_file)
        {
            UCP_PRINT_ERROR("Can not open dst file: %s \r\n", write_file_name);
            fclose(file);
            fclose(read_file);
            return OSP_FILE_ERROR;
        }

        /* 开始转化 */
        while(fgets(line_buf, 100, read_file))
        {
            sscanf(line_buf, "0x%x", &value);
            fwrite(&value, sizeof(value), 1, write_file);
        }

        osp_cfg_file_idx++;

        fflush(read_file);
        fclose(read_file);
        fflush(write_file);
        fclose(write_file);
    }

    fclose(file);
    system("rm tmpfile");

    return OSP_OK;
}

/* 将二进制文件定入DDR */
int32_t osp_bin_cfg_file_to_ddr()
{
    char *p_line_file_name = NULL;
    char *p_line_file_name_begin = NULL;
    char line_file_full_name[2*APE_CFG_FILE_NAME_LEN] = {0};
    char file_name[APE_CFG_FILE_NAME_LEN] = {0};

    uint32_t len = 0;
    uint32_t tmp_len = 0;
    uint8_t  osp_cfg_file_idx = 0;    /* 文件个数 */
    uint32_t ret = 0;                 /* 返回值判断 */
    uint32_t file_len = 0;            /* 文件长度 */
    uint32_t ddr_offset = 0;
    
    FILE *file = NULL;
    FILE *read_file = NULL;

    /* 全路径文件名写入： tmpfile文件            */
    system("find /ramfs/config/* -name '*' > tmpfile");

    /* 遍历 */
    file = fopen("./tmpfile", "rb");
    if (NULL == file)
    {
        UCP_PRINT_ERROR("[to_ddr]: Open file(tmpfile) error.\r\n");
        return OSP_FILE_ERROR;
    }

    while(osp_cfg_file_idx < APE_CFG_FILE_NUM)
    {
        /* 每行是原始文件的全路径 */
        if (NULL == fgets(line_file_full_name, sizeof(line_file_full_name), file))
        {
            /* 遍历至最后结束 */
            break;
        }
        p_line_file_name = strrchr(line_file_full_name, '/');
        p_line_file_name++;
        
        len = strlen(line_file_full_name);
        line_file_full_name[len - 1] = '\0';

        p_line_file_name_begin = line_file_full_name;
        memcpy(file_name, p_line_file_name, len-(p_line_file_name - p_line_file_name_begin));
        //UCP_PRINT_DEBUG("[to_ddr]: file name : %s \r\n", file_name);

        read_file = fopen(line_file_full_name, "rb");
        if (NULL == read_file)
        {
            UCP_PRINT_ERROR("[to_ddr]: Can not open src file: %s \r\n", line_file_full_name);
            fclose(file);
            return OSP_FILE_ERROR;
        }

        /* 找到文件结束符 */
        ret = fseek(read_file, 0, SEEK_END);
        if (ret)
        {
            UCP_PRINT_ERROR("[to_ddr]: Can not fseek: %s \r\n", line_file_full_name);
            fclose(file);
            fclose(read_file);
            return OSP_FILE_ERROR;
        }

        /* 计算文件长度 */
        file_len = (uint32_t)ftell(read_file);
        UCP_PRINT_DEBUG("[to_ddr]: file: %s ,Size: %d\r\n", line_file_full_name, file_len);

        /* 保护：文件太多，空间不够 */
        if ((file_len + ddr_offset) > ARM_CFG_FILE_SIZE)
        {
            UCP_PRINT_ERROR("[to_ddr]: too much file, no space... \r\n");
            fclose(read_file);
            fclose(file);
            system("rm tmpfile");
            return OSP_ERROR;
        }

        /* 回到文件头部 */
        ret = fseek(read_file, 0, SEEK_SET);

        /* 更新全局变量 */
        len = 0;
        while(len < file_len)
        {
            tmp_len = fread((char*)(g_ptr_static_mem+ARM_CFG_INDXN+ARM_CFG_INFO_SIZE+ddr_offset+len), 1, file_len, read_file);
            len += tmp_len;
        }

        /* 更新全局变量 */
        g_osp_cfg_file_reg[g_osp_cfg_file_idx].len = file_len;
        memcpy(g_osp_cfg_file_reg[g_osp_cfg_file_idx].name, file_name, strlen(file_name));
        g_osp_cfg_file_reg[g_osp_cfg_file_idx].phy_addr = ARM_CFG_FILE_ADDR+ddr_offset;
        g_osp_cfg_file_idx++;
        ddr_offset += file_len;

        osp_cfg_file_idx++;
        fclose(read_file);
    }

    /* 更新配置文件头信息 */
    *(uint32_t *)(g_ptr_static_mem + ARM_CFG_NUM) = g_osp_cfg_file_idx;
    memcpy((g_ptr_static_mem+ARM_CFG_INDXN), g_osp_cfg_file_reg, APE_CFG_INFO_ONE*g_osp_cfg_file_idx);
    *(uint32_t *)g_ptr_static_mem = ARM_READ_CFG_FILE_OK;

    fclose(file);
    system("rm tmpfile");

    return OSP_OK;
}

int32_t osp_read_cfg_file(void)
{
    int32_t ret = -1;

    /* 配置文件初始化 */
    ret = osp_cfg_file_init();
    if (OSP_OK != ret)
    {
        UCP_PRINT_ERROR("[osp_read_cfg_file]: osp_cfg_file_init return error: %d\r\n", ret);
        return ret;
    }

    /* 读配置文件 */
    ret = osp_cfg_file_to_bin();
    if (OSP_OK != ret)
    {
        UCP_PRINT_ERROR("[osp_read_cfg_file]: osp_cfg_file_to_bin return error: %d\r\n", ret);
        return ret;
    }

    /* 转化并写入 */
    ret = osp_bin_cfg_file_to_ddr();
    if (OSP_OK != ret)
    {
        UCP_PRINT_ERROR("[osp_read_cfg_file]: osp_bin_cfg_file_to_ddr return error: %d\r\n", ret);
        return ret;
    }
    return OSP_OK;
}

/***************************************************************************/
/*                             ARM  <--->  ARM                             */
int32_t osp_spe_cfg_file_init()
{
    memset(&g_spe_osp_cfg_file_reg, 0, sizeof(g_spe_osp_cfg_file_reg));
    g_spe_osp_cfg_file_idx = 0;
    g_spe_static_mem_size = 0;

#if 0
    g_spe_ptr_static_mem = malloc(ARM_SPE_CFG_PHY_SIZE);
    if (NULL == g_spe_ptr_static_mem)
    {
        UCP_PRINT_ERROR("get_static_mem(ARM_STACK) error......\r\n");
        return OSP_ERROR;
    }
    return OSP_OK;
#endif

    g_spe_ptr_static_mem = get_static_mem(APE_TEXT, &g_spe_static_mem_size);
    if (0 == g_spe_static_mem_size)
    {
        UCP_PRINT_ERROR("get_static_mem(ARM_STACK) error......\r\n");
        return OSP_ERROR;
    }
    g_spe_ptr_static_mem += ARM_SPE_CFG_OFFSET;    /* 在内存规划中，配置文件的起始地址需要偏移 */

    return OSP_OK;
}

int32_t osp_read_spe_cfg_to_bin(char* in_path)
{
    FILE *file       = NULL; /* 遍历临时文件，获取所有文件名 */
    FILE *read_file  = NULL; /* 原始文件 */
    FILE *write_file = NULL; /* 目的文件 */
    uint8_t len = 0;
    uint8_t osp_cfg_file_idx = 0;   /* 文件个数 */
    uint32_t value = 0;
    char *p_line_file_name = NULL;
    char line_file_name_full[APE_CFG_FILE_NAME_LEN*2] = {0};
    char write_file_name[APE_CFG_FILE_NAME_LEN] = {0};
    char line_buf[100] = {0};
    char cmd_string[APE_CFG_FILE_NAME_LEN*2] = {0};

    if (NULL == in_path)
    {
        UCP_PRINT_ERROR("osp_read_spe_cfg_to_bin() in_path error......\r\n");
        return OSP_PAR_ILL;
    }

    system("rm -rf /ramfs/config_bak/*.*");

    /* 遍历原始目录，获取所有文件的文件名及路径输出到:tmpfile */
    sprintf(cmd_string, "find /%s/* -name '*.dat' > tmpfile", in_path);
    system(cmd_string);

    /* 遍历原始目录，获取所有文件的文件名输出到:tmpfilename */
    //system("find /ramfs/cfgDat/* -name '*.dat' | sed 's#.*/##' > tmpfilename");

    /* 打开tmpfile内所有需要处理的文件 */
    file = fopen("./tmpfile", "rb");
    if (NULL == file)
    {
        UCP_PRINT_ERROR("Open file(tmpfile) error.\r\n");
        return OSP_FILE_ERROR;
    }
    /* 逐一转化（文本文件 --> 二进制文件               */
    while(osp_cfg_file_idx < APE_CFG_FILE_NUM)
    {
        /* 每行是原始文件的全路径            */
        if (NULL == fgets(line_file_name_full, sizeof(line_file_name_full), file))
        {
            /* 遍历至最后结束 */
            break;
        }
        p_line_file_name = strrchr(line_file_name_full, '/');
        p_line_file_name++;

        len = strlen(line_file_name_full);
        line_file_name_full[len - 1] = '\0';

        read_file = fopen(line_file_name_full, "rb");
        if (NULL == read_file)
        {
            UCP_PRINT_ERROR("Can not open src file: %s \r\n", line_file_name_full);
            fclose(file);
            return OSP_FILE_ERROR;
        }

        sprintf(write_file_name, "/ramfs/config_bak/%s", p_line_file_name);
        //UCP_PRINT_DEBUG("dst file: %s \r\n", write_file_name);

        write_file = fopen(write_file_name, "wb+");
        if (NULL == write_file)
        {
            UCP_PRINT_ERROR("Can not open dst file: %s \r\n", write_file_name);
            fclose(file);
            fclose(read_file);
            return OSP_FILE_ERROR;
        }

        /* 开始转化 */
        while(fgets(line_buf, 100, read_file))
        {
            sscanf(line_buf, "0x%x", &value);
            fwrite(&value, sizeof(value), 1, write_file);
        }

        osp_cfg_file_idx++;

        fflush(read_file);
        fclose(read_file);
        fflush(write_file);
        fclose(write_file);
    }

    fclose(file);
    system("rm tmpfile");

    return OSP_OK;
}

int32_t osp_spe_bin_cfg_file_to_ddr()
{
    char *p_line_file_name = NULL;
    char *p_line_file_name_begin = NULL;
    char line_file_full_name[2*APE_CFG_FILE_NAME_LEN]={0};
    char file_name[APE_CFG_FILE_NAME_LEN] = {0};

    uint32_t len = 0;
    uint32_t tmp_len = 0;
    uint8_t osp_cfg_file_idx = 0;     /* 文件个数 */
    uint32_t ret = 0;                 /* 返回值判断 */
    uint32_t file_len = 0;            /* 文件长度 */
    uint32_t ddr_offset = 0;

    FILE *file = NULL;
    FILE *read_file = NULL;

    /* 全路径文件名写入： tmpfile文件            */
    system("find /ramfs/config_bak/* -name '*' > tmpfile");

    /* 遍历 */
    file = fopen("./tmpfile", "rb");
    if (NULL == file)
    {
        UCP_PRINT_ERROR("[to_ddr]: Open file(tmpfile) error.\r\n");
        return OSP_FILE_ERROR;
    }

    while(osp_cfg_file_idx < APE_CFG_FILE_NUM)
    {
        /* 每行是原始文件的全路径 */
        if (NULL == fgets(line_file_full_name, sizeof(line_file_full_name), file))
        {
            /* 遍历至最后结束 */
            break;
        }
        p_line_file_name = strrchr(line_file_full_name, '/');
        p_line_file_name++;

        len = strlen(line_file_full_name);
        line_file_full_name[len - 1] = '\0';

        p_line_file_name_begin = line_file_full_name;
        memcpy(file_name, p_line_file_name, len-(p_line_file_name - p_line_file_name_begin));
        //UCP_PRINT_DEBUG("[to_ddr]: file name : %s \r\n", file_name);

        read_file = fopen(line_file_full_name, "rb");
        if (NULL == read_file)
        {
            UCP_PRINT_ERROR("[to_ddr]: Can not open src file: %s \r\n", line_file_full_name);
            fclose(file);
            return OSP_FILE_ERROR;
        }

        /* 找到文件结束符 */
        ret = fseek(read_file, 0, SEEK_END);
        if (ret)
        {
            UCP_PRINT_ERROR("[to_ddr]: Can not fseek: %s \r\n", line_file_full_name);
            fclose(file);
            fclose(read_file);
            return OSP_FILE_ERROR;
        }

        /* 计算文件长度 */
        file_len = (uint32_t)ftell(read_file);
        UCP_PRINT_DEBUG("[to_ddr]: file: %s ,Size: %d\r\n", line_file_full_name, file_len);

        /* 回到文件头部 */
        ret = fseek(read_file, 0, SEEK_SET);

        /* 更新全局变量 */
        len = 0;
        while(len < file_len)
        {
            tmp_len = fread((char*)(g_spe_ptr_static_mem+ARM_CFG_INDXN+ARM_SPE_CFG_INFO_SIZE+ddr_offset+len), 1, file_len, read_file);
            len += tmp_len;
        }

        /* 更新全局变量 */
        g_spe_osp_cfg_file_reg[g_spe_osp_cfg_file_idx].len = file_len;
        memcpy(g_spe_osp_cfg_file_reg[g_spe_osp_cfg_file_idx].name, file_name, strlen(file_name));
        g_spe_osp_cfg_file_reg[g_spe_osp_cfg_file_idx].vir_addr = (uint64_t)(g_spe_ptr_static_mem 
                                                                    + ARM_CFG_INDXN 
                                                                    + ARM_SPE_CFG_INFO_SIZE 
                                                                    + ddr_offset);
        g_spe_osp_cfg_file_idx++;
        ddr_offset += file_len;

        osp_cfg_file_idx++;
        fclose(read_file);
    }

    /* 更新配置文件头信息 */
    *(uint32_t *)(g_spe_ptr_static_mem+ARM_CFG_NUM) = g_spe_osp_cfg_file_idx;
    memcpy((g_spe_ptr_static_mem + ARM_CFG_INDXN), g_spe_osp_cfg_file_reg, ARM_SPE_CFG_INFO_ONE * g_spe_osp_cfg_file_idx);
    *(uint32_t *)g_spe_ptr_static_mem = ARM_READ_CFG_FILE_OK;

    fclose(file);
    system("rm tmpfile");

    return OSP_OK;

}


/********************************************************************/
/*                     TestMac Interface                            */
/********************************************************************/
int32_t osp_read_spe_cfg_file(char* path)
{
    uint32_t ret = 0;

    /* 每次调用前，清空全局变量 */
    ret = osp_spe_cfg_file_init();
    if (OSP_OK != ret)
    {
        UCP_PRINT_ERROR("[osp_read_spe_cfg_file]: osp_cfg_file_init return error: %d\r\n", ret);
        return ret;
    }

    /* 读文本配置文件 */
    ret = osp_read_spe_cfg_to_bin(path);
    if (OSP_OK != ret)
    {
        UCP_PRINT_ERROR("[osp_read_spe_cfg_file]: osp_read_spe_cfg_to_bin return error: %d\r\n", ret);
        return ret;
    }

    /* 转化 */
    ret = osp_spe_bin_cfg_file_to_ddr();
    if (OSP_OK != ret)
    {
        UCP_PRINT_ERROR("[osp_read_spe_cfg_file]: osp_spe_bin_cfg_file_to_ddr return error: %d\r\n", ret);
        return ret;
    }
    return OSP_OK;
}

int32_t osp_get_cfg_file(char* name, uint64_t *paddr, uint32_t *psize)
{
    uint16_t loop = 0;
    osp_spe_cfg_file_reg *head = NULL;
    if (strlen(name) > APE_CFG_FILE_NAME_LEN)
    {
        UCP_PRINT_ERROR("[osp_get_cfg_file]: name too long\r\n");
        return OSP_ERROR;
    }
    for (loop = 0; loop < APE_CFG_FILE_NUM; loop++)
    {
        head = g_spe_osp_cfg_file_reg + loop;
        if (0 == memcmp(head->name, name, strlen(name)))
        {
            /* 物理层需要物理地址，而还是虚拟地址 */
            osp_virt_to_phy(APE_TEXT, head->vir_addr, paddr);
            //*paddr = head->vir_addr;
            *psize = head->len;
            return OSP_OK;
        }
    }
    UCP_PRINT_DEBUG("[osp_get_cfg_file]: no this file(%s)\r\n", name);
    return OSP_ERROR;
}

#ifdef HEARTBEAT_ENABLE
#define OSP_IM2DDR_FLAG_OK      (0x5a5a5a5a)
#define OSP_IM2DDR_SIZE         (0x40000)       /* 256k */
#define DUMP_IM_CORE_FILE_NAME  "./dump_im_core%02d_%04d%02d%02d_%02d%02d%02d"  /* dump dm file */
#define DUMP_IM_CORE_FILE_LEN   (36)

int8_t  osp_get_im2ddr_to_file(uint8_t u8CoreId)
{
    int32_t  ret       = OSP_OK;
    uint32_t u32_im2ddr_flag = 0;
    char  file_name[DUMP_IM_CORE_FILE_LEN]= {0};
     char *pbuf        = g_ptr_static_mem;
    FILE *fp    = NULL;
    struct tm *t;
    time_t tt;

    time(&tt);
    t = localtime(&tt);

    UCP_PRINT_DEBUG("[osp_get_im2ddr_to_file]: Start ... \r\n");

    while(1)
    {
        u32_im2ddr_flag = *(uint32_t*)(g_ptr_static_mem);
        if (OSP_IM2DDR_FLAG_OK == u32_im2ddr_flag)
        {
            UCP_PRINT_DEBUG("[osp_get_im2ddr_to_file]: ape write im2ddr ok\r\n");
            break;
        }
    }

    sprintf(file_name, DUMP_IM_CORE_FILE_NAME, u8CoreId, t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
    UCP_PRINT_DEBUG("[osp_get_im2ddr_to_file]: file_name = %s\r\n", file_name);

    fp = fopen(file_name, "wb+");
    if (fp < 0)
    {
        UCP_PRINT_ERROR("[osp_get_im2ddr_to_file]:  file open failure  \n");
        return OSP_ERROR;
    }
        
    ret = fwrite((pbuf+4), sizeof(char), OSP_IM2DDR_SIZE, fp);
    if(ret < 0)
    {
        UCP_PRINT_ERROR("[osp_get_im2ddr_to_file]: fwrite error ret:%d\n", ret);
        fclose(fp);
        return OSP_ERROR;
    }

    fflush(fp);
    fclose(fp);

    UCP_PRINT_DEBUG("[osp_get_im2ddr_to_file]: End ... \r\n");
    return OSP_OK;
}
#endif


