s32 nv_flash_read(u8* ptr, u32 size, u32 count, FILE* fp)
{
    u32 real_size = 0;
    s32 ret = -1;
    struct nv_flash_file_header_stru* ffp = (struct nv_flash_file_header_stru*)fp;
    u32 len = size*count;


    nv_file_debug(NV_FILE_READ_API,0,0,size,count);

    if((NULL == ffp)||(ffp->fp != ffp))
    {
        return BSP_ERR_NV_INVALID_PARAM;
    }

    real_size = ((ffp->seek+len) < ffp->length)? len: (ffp->length - ffp->seek );

    ret = (s32)nv_mtd_read(ffp,(ffp->off+ffp->seek),real_size,ptr);/*读取注意文件seek位置*/
    if(ret != NAND_OK)
    {
        nv_file_debug(NV_FILE_READ_API,2,(u32)ret,real_size,ffp->flash_type);
        nv_mntn_record("\n[%s]\n",__func__);
        nv_flash_help(NV_FILE_READ_API);
        return -1;
    }
    ffp->seek += real_size;
    return (s32)real_size;
}
void nv_flash_help(u32 type)
{
    u32 i;
    if(type == NV_FILE_OPS_MAX_API)
    {
        for(i = 0;i< NV_FILE_OPS_MAX_API;i++)
        {
            printf("************flash fun id %d************\n",i);
            printf("call num             : 0x%x\n",g_flash_debug[i].callnum);
            printf("out branch (reseved1): 0x%x\n",g_flash_debug[i].reseved1);
            printf("reseved2             : 0x%x\n",g_flash_debug[i].reseved2);
            printf("reseved3             : 0x%x\n",g_flash_debug[i].reseved3);
            printf("reseved4             : 0x%x\n",g_flash_debug[i].reseved4);
            printf("***************************************\n");
        }
        return ;
    }

    i = type;

    nv_mntn_record("************flash fun id %d************\n",i);
    nv_mntn_record("call num             : 0x%x\n",g_flash_debug[i].callnum);
    nv_mntn_record("out branch (reseved1): 0x%x\n",g_flash_debug[i].reseved1);
    nv_mntn_record("reseved2             : 0x%x\n",g_flash_debug[i].reseved2);
    nv_mntn_record("reseved3             : 0x%x\n",g_flash_debug[i].reseved3);
    nv_mntn_record("reseved4             : 0x%x\n",g_flash_debug[i].reseved4);
    nv_mntn_record("**************************************\n");
}
s32 nv_flash_write(u8* ptr, u32 size, u32 count, FILE* fp)
{
    s32 ret = -1;
    u32 len = size*count;
    struct nv_flash_file_header_stru* ffp = (struct nv_flash_file_header_stru*)fp;
    u32* sec_off = NULL;
    struct nv_file_info_stru* file_info;

    nv_file_debug(NV_FILE_WRITE_API,0,0,size,count);

    if((NULL == ffp)||(ffp->fp != ffp))
    {
        nv_file_debug(NV_FILE_WRITE_API,1,0,size,count);
        goto nv_flash_write_err;
    }
    switch(ffp->flash_type)
    {
        case NV_FILE_BACKUP:
            sec_off = g_sec_info.nv_bak;
            file_info = &g_flash_info.bak_sec;
            break;
        case NV_FILE_SYS_NV:
            sec_off = g_sec_info.sys_nv;
            file_info = &g_flash_info.sys_nv;
            break;
        case NV_FILE_DEFAULT:
            sec_off = g_sec_info.nv_default;
            file_info = &g_flash_info.def_sec;
            break;
        default:
            return -1;
    }
    ret = (s32)nv_mtd_write(ffp,(ffp->off+ffp->seek),len,ptr);
    if(ret)
    {
        nv_file_debug(NV_FILE_WRITE_API,3,(u32)ret,len,ffp->flash_type);
        goto nv_flash_write_err;
    }

    ret = (s32)nv_count_file_sec_info(ffp->name,len,sec_off);
    if(ret)
    {
        nv_file_debug(NV_FILE_WRITE_API,4,(u32)ret,len,ffp->flash_type);
        goto nv_flash_write_err;
    }

    file_info->magic_num = NV_FILE_EXIST;
    file_info->len       = len;
    file_info->off       = 0;
    ffp->seek += len;
    return (s32)len;

nv_flash_write_err:
    nv_mntn_record("\n[%s]\n",__func__);
    nv_flash_help(NV_FILE_WRITE_API);
    return BSP_ERR_NV_INVALID_PARAM;
}
u32 nv_flash_init(void)
{
    u32 ret = NV_ERROR;
    u32 i = 0;

    nv_file_debug(NV_FILE_INIT_API,0,0,0,0);

    /*first init every file sem*/
    for(i = 0;i<NV_FILE_BUTT;i++)
    {
        osl_sem_init(1,&g_nv_file[i].file_sem);
    }

    memset(&g_flash_info,0,sizeof(struct nv_flash_global_ctrl_stru));
    memset(&g_sec_info,0xff,sizeof(struct nv_sec_file_block_info));

    /*get dload info*/
    ret = nv_dload_file_info_init();
    if(ret)
    {
        nv_file_debug(NV_FILE_INIT_API,1,ret,0,0);
        goto nv_flash_init_err;
    }
    /*get sys nv file info*/
    ret = nv_sec_file_info_init(g_nv_file[NV_FILE_SYS_NV].name,&g_flash_info.sys_nv,g_sec_info.sys_nv);
    if(ret)
    {
        nv_file_debug(NV_FILE_INIT_API,2,ret,0,0);
        goto nv_flash_init_err;
    }
    /*get backup info*/
    ret = nv_sec_file_info_init(g_nv_file[NV_FILE_BACKUP].name,&g_flash_info.bak_sec,g_sec_info.nv_bak);
    if(ret)
    {
        nv_file_debug(NV_FILE_INIT_API,3,ret,0,0);
        goto nv_flash_init_err;
    }

    /*get default info*/
    ret = nv_sec_file_info_init(g_nv_file[NV_FILE_DEFAULT].name,&g_flash_info.def_sec,g_sec_info.nv_default);
    if(ret)
    {
        nv_file_debug(NV_FILE_INIT_API,4,ret,0,0);
        goto nv_flash_init_err;
    }
    return NV_OK;
nv_flash_init_err:
    nv_mntn_record("\n[%s]\n",__func__);
    nv_flash_help(NV_FILE_INIT_API);
    return NV_ERROR;
}
s32 nv_emmc_write(u8* ptr, u32 size, u32 count, FILE* fp)
{
    u32 ret = NV_ERROR;
    u32 len = size*count;
    struct nv_emmc_file_header_stru* fd = (struct nv_emmc_file_header_stru*)fp;
    struct nv_file_info_stru* file_info;

    nv_file_debug(NV_FILE_WRITE_API,0,0,size,count);

    if((NULL == fd)||(fd->fp != fd))
    {
        nv_file_debug(NV_FILE_WRITE_API,1,0,size,count);
        goto nv_flash_write_err;
    }
    switch(fd->emmc_type)
    {
        case NV_FILE_BACKUP:
            file_info = &g_emmc_info.bak_sec;
            break;
        case NV_FILE_SYS_NV:
            file_info = &g_emmc_info.sys_nv;
            break;
        case NV_FILE_DEFAULT:
            file_info = &g_emmc_info.def_sec;
            break;
        default:
            return -1;
    }
    ret = (u32)nv_mtd_write(fd,(fd->off+fd->seek),len,ptr);
    if(ret)
    {
        nv_file_debug(NV_FILE_WRITE_API,3,ret,len,fd->emmc_type);
        goto nv_flash_write_err;
    }

    file_info->magic_num = NV_FILE_EXIST;
    file_info->len       = len;
    file_info->off       = 0;
    fd->seek += len;
    return (s32)len;

nv_flash_write_err:
    nv_mntn_record("\n[%s]\n",__func__);
    nv_emmc_help(NV_FILE_WRITE_API);
    return BSP_ERR_NV_INVALID_PARAM;
}
s32 nv_flash_seek(FILE* fp,s32 offset,s32 whence)
{
    u32 ret = 0;
    struct nv_flash_file_header_stru* ffp = (struct nv_flash_file_header_stru*)fp;

    nv_file_debug(NV_FILE_SEEK_API,0,(u32)offset,(u32)whence,0);

    if((NULL == ffp)||(ffp->fp != ffp))
    {
        nv_file_debug(NV_FILE_SEEK_API,1,(u32)offset,(u32)whence,0);
        goto out;
    }
    ret = ffp->seek;
    switch(whence)
    {
        case SEEK_SET:
            nv_file_debug(NV_FILE_SEEK_API,2,(u32)offset,(u32)whence,ret);
            ret = (u32)offset;
            break;
        case SEEK_CUR:
            nv_file_debug(NV_FILE_SEEK_API,3,(u32)offset,(u32)whence,ret);
            ret += (u32)offset;
            break;
        case SEEK_END:
            nv_file_debug(NV_FILE_SEEK_API,4,(u32)offset,(u32)whence,ret);
            ret = ffp->length + (u32)offset;
            break;
        default:
            nv_file_debug(NV_FILE_SEEK_API,5,(u32)offset,(u32)whence,ret);
            goto out;
    }
    ffp->seek = ret;
    return NV_OK;
out:
    nv_mntn_record("\n[%s]\n",__func__);
    nv_flash_help(NV_FILE_SEEK_API);
    return BSP_ERR_NV_INVALID_PARAM;
}
s32 nv_flash_remove(const s8* path)
{
    s32 ret = -1;
    struct nv_flash_file_header_stru* ffp = NULL;
    struct erase_info erase;
    struct mtd_info* mtd = NULL;
    u32 i = 0;

    nv_file_debug(NV_FILE_REMOVE_API,0,0,0,0);

    for(i=0;i<NV_FILE_BUTT;i++)
    {
        if(0 == strcmp(path,g_nv_file[i].path))
        {
            ffp = &g_nv_file[i];
            break;
        }
    }

    if(NULL == ffp)
    {
        nv_file_debug(NV_FILE_REMOVE_API,1,0,0,0);
        return -1;
    }
    switch(ffp->flash_type)
    {
        case NV_FILE_DLOAD:
            g_flash_info.nv_dload.nv_bin.magic_num = NV_FLASH_NULL;
            break;
        case NV_FILE_BACKUP:
            memset(&g_flash_info.bak_sec,NV_FLASH_FILL,sizeof(struct nv_file_info_stru));
            goto flash_erase;
        case NV_FILE_CUST_CARD_1:
            g_flash_info.nv_dload.cust_xml[0].magic_num = NV_FLASH_NULL;
            break;
        case NV_FILE_XNV_CARD_1:
            g_flash_info.nv_dload.xnv_xml[0].magic_num = NV_FLASH_NULL;
            break;
        case NV_FILE_CUST_CARD_2:
            g_flash_info.nv_dload.cust_xml[1].magic_num = NV_FLASH_NULL;
            break;
        case NV_FILE_XNV_CARD_2:
            g_flash_info.nv_dload.xnv_xml[1].magic_num = NV_FLASH_NULL;
            break;
        case NV_FILE_XNV_MAP_CARD_1:
            g_flash_info.nv_dload.xnv_map[0].magic_num = NV_FLASH_NULL;
            break;
        case NV_FILE_XNV_MAP_CARD_2:
            g_flash_info.nv_dload.xnv_map[1].magic_num = NV_FLASH_NULL;
            break;
        case NV_FILE_DEFAULT:
            memset(&g_flash_info.def_sec,NV_FLASH_FILL,sizeof(struct nv_file_info_stru));
            goto flash_erase;
        case NV_FILE_SYS_NV:
            memset(&g_flash_info.sys_nv,NV_FLASH_FILL,sizeof(g_flash_info.sys_nv));
            goto flash_erase;
        default:
            return BSP_ERR_NV_INVALID_PARAM;
    }
    if(true == nv_dload_exist_file())
    {
        return NV_OK;
    }
flash_erase:
    mtd = get_mtd_device_nm(ffp->name);
    if(IS_ERR(mtd))
    {
        printf("[%s]:get mtd device err! %s\n",__func__,ffp->name);
        return -1;
    }
    erase.addr = 0;
    erase.mtd = mtd;
    erase.len = mtd->size;
    erase.callback = NULL;
    erase.priv     = 0;
    erase.time     = 10000;
    erase.retries  = 2;

    ret = mtd_erase(mtd,&erase);
    put_mtd_device(mtd);
    if(ret)
    {
        nv_file_debug(NV_FILE_REMOVE_API,2,(u32)ret,(u32)ffp->flash_type,0);
        nv_mntn_record("[%s]:ret 0x%x,mtd->name %s\n",__func__,ret,mtd->name);
        return ret;
    }

    return NV_OK;
 }