c开发机顶盒实战应用代码片段2

#define UPGRADE_DEBUG 1
#ifdef UPGRADE_DEBUG
#define stb_printf(...) printf (__VA_ARGS__)
//#define stb_printf(...)
#else
#define stb_printf(...) do { } while (0)
#endif
/***********************************************************************
*                常量定义
***********************************************************************/
#define stb_failure -1
#define stb_success 0

/*外部文件一些定义,暂时*/


/***********************************************************************
*              本地使用宏定义
***********************************************************************/
#define UPGRADE_KEY_LEN   64
#define UPGRADE_DATA_TIME_LEN  128
#define UPGRADE_FILE_NAME_LEN  256
#define UPGRADE_URL_LEN    512
#define URL_LEN 512
#define VERSION_LEN 32*2
#define DATA_LEN 16*2

#define UPGRADEFILE_MD5      "/tmp/UpgradeMd5"    /*升级文件实际MD5值*/
#define MD5_CMD         "md5sum"      /*MD5校验命令*/
#define SERVER_VERSION_NAME     "UpgradeVersion"     /*服务器上版本文件名*/
#define USB_UPGRADE_ADDR      "/mnt/usbupgrade/stbupgrade/"   /*USB存储设备升级地址*/
#define UPGRADE_LOCAL_bootld_FILE    "/home/upgrade/bootld"    /*本地临时启动引导文件名*/
#define UPGRADE_LOCAL_bmp_FILE    "/home/upgrade/splash.bmp"     /*本地临时开机画面文件名*/
#define UPGRADE_LOCAL_kernel_FILE    "/home/upgrade/kernel.bin"    /*本地临时内核文件名*/
#define UPGRADE_LOCAL_app_FILE     "/home/upgrade/JX010100.bin"    /*本地临时内核文件名*/
#define UPGRADE_LOCAL_stbid_FILE    "/home/upgrade/stb_id"    /*本地临时STBID文件名*/
#define UPGRADE_LOCAL_bootkey_FILE    "/home/upgrade/osflag"    /*本地临时STBID文件名*/
#define UPGRADE_VERSION_LOCAL_ADDR   "/home/upgrade/UpgradeVersion"   /*本地临时版本文件名*/

#define UPGRADE_STBID_LOCAL_ADDR    "/home/info/stbid.cfg"    /*本地临时版本文件名*/
#define UPGRADE_FLAG_FILE      "/home/info/stbupgrade_img"  /*升级进行中的标志文件*/
#define UPGRADE_BMP_FLAG_FILE     "/home/info/stbupgrade_bmp_img" /*升级BMP进行中的标志文件*/

#define FLASH_DEL_BOOTKEY_BLOCK                      "FLASH_DEL_BOOTKEY_BLOCK"
#define FLASH_BLOCK_BOOTKEY     "LAST_OSFLAGS"      /*升级进行中的标志文件*/
#define FLASH_BLOCK_CFE             "CURRENT_CFE"      /*升级进行中的标志文件*/
#define FLASH_BLOCK_BMP            "CURRENT_SPLASH"      /*升级进行中的标志文件*/
#define FLASH_BLOCK_APP            "LAST_APP"      /*升级进行中的标志文件*/
#define FLASH_BLOCK_KERNEL                 "LAST_KERNEL"      /*升级进行中的标志文件*/

#define FLASH_BLOCK_temp_partition    "/dev/mtd0"      /*升级进行中的标志文件*/
#define FLASH_BLOCK_temp_partition_handle  "/dev/mtdblock0"     /*升级进行中的标志文件*/

#define SYSTEM_FLASH_BLOCK_temp_path   "/home/upgrade"     /*升级进行中的标志文件*/
#define PROGRESS_BAR_PICTURE                             "upgrade/upgrade_bar1.jpg"
#define UPGRADE_DONT_POWEROFF                         "upgrade/nopoweroff.jpg"
#define UPGRADE_FAIL_GOTO_EPG                           "upgrade/gotoepg.jpg"
#define UPGRADE_INSTALLING                                   "upgrade/installing.jpg"
#define UPGRADE_DOWNLOADING                             "upgrade/downloading.jpg"
#define PROGRESS_BAR_PERCENT                              "upgrade/percent.jpg"
#define  PROGRESS_PERCENT_X       560 // 262 
#define  PROGRESS_PERCENT_Y       430 // 380
#define  PROGRESS_PERCENT_W        630 

#define  PROGRESS_BAR_X        150//250// 4 
#define  PROGRESS_BAR_Y        540// 440
#define  PROGRESS_BAR_W        630 

/***********************************************************************
*           本地使用数据结构定义
***********************************************************************/

typedef struct _upgrade_parameter
{
 char upgrade_addr[UPGRADE_URL_LEN];      /*升级地址*/
 char upgrade_data[UPGRADE_DATA_TIME_LEN];    /*日期*/
 char upgrade_companyInstruct[UPGRADE_KEY_LEN];   /*厂商指示*/
 char upgrade_stbid[UPGRADE_KEY_LEN];      /*STBID*/
 char upgrade_swVersion[UPGRADE_KEY_LEN];    /*服务器上软件版本*/
 char upgrade_bootldFileName[UPGRADE_FILE_NAME_LEN];  /*bootloader文件名*/
 char upgrade_bmpFileName[UPGRADE_FILE_NAME_LEN];  /*开机画面文件名*/
 char upgrade_kernelFileName[UPGRADE_FILE_NAME_LEN];  /*kernel文件名*/
 char upgrade_appFileName[UPGRADE_FILE_NAME_LEN];  /*app文件名*/
 char upgrade_bootkeyFileName[UPGRADE_FILE_NAME_LEN];  /*app文件名*/
 char upgrade_bootld_MD5[UPGRADE_KEY_LEN];    /*bootloader文件MD5值*/
 char upgrade_bmp_MD5[UPGRADE_KEY_LEN];    /*bmp文件MD5值*/
 char upgrade_kernel_MD5[UPGRADE_KEY_LEN];    /*kernel文件MD5值*/
 char upgrade_app_MD5[UPGRADE_KEY_LEN];     /*app文件MD5值*/
 char upgrade_bootkey_MD5[UPGRADE_KEY_LEN];     /*app文件MD5值*/
 unsigned update_flag_bootldpartition;      /*引导分区*/
 unsigned update_flag_bmppartition;      /*开机画面分区*/
 unsigned update_flag_kernelpartition;      /*内核分区*/
 unsigned update_flag_apppartition;      /*应用分区*/
 unsigned update_flag_stbidfile;       /*STBID文件*/
 unsigned update_flag_bootkeypartition;       /*BOOTKEY文件*/
 unsigned long upgrade_bootldFileSize;      /*bootloader文件大小*/
 unsigned long upgrade_bmpFileSize;      /*bmp文件大小*/
 unsigned long upgrade_kernelFileSize;      /*kernel文件大小*/
 unsigned long upgrade_appFileSize;      /*app文件大小*/
 unsigned long upgrade_bootkeyFileSize;      /*bootkey文件大小*/
}upgrade_parameter;

typedef enum _stb_upgrade_mode
{
 STB_Upgrade_Unknown,
 STB_Upgrade_Preset,
 STB_Upgrade_USB,
 STB_Upgrade_Net_Issue
} STB_Upgrade_Mode;
typedef enum _upgrade_status
{
        UPGRADE_CHANGE_MODE = -2,
        UPGRADE_ERROR = -1,
        UPGRADE_OK,
        UPGRADE_DOWNLOAD_CFE,
        UPGRADE_DOWNLOAD_BMP,
        UPGRADE_DOWNLOAD_KERNEL,
        UPGRADE_DOWNLOAD_APP,
        UPGRADE_DOWNLOAD_BOOTKEY,
        UPGRADE_COPY,
        UPGRADE_COPY_CFE,
        UPGRADE_COPY_BMP,
        UPGRADE_COPY_KERNEL,
        UPGRADE_COPY_APP,
        UPGRADE_COPY_BOOTKEY,
        UPGRADE_CHECK_CFE_MD5,
        UPGRADE_CHECK_BMP_MD5,
        UPGRADE_CHECK_KERNEL_MD5,
        UPGRADE_CHECK_APP_MD5,
        UPGRADE_CHECK_BOOTKEY_MD5,
        UPGRADE_STATUS_UNKNOW
}UPGRADE_STATUS;


typedef enum _ftp_upgrade
{
 FTP_UPGRADE_SUCCESS,
 FTP_UPGRADE_FAILED,
 FTP_UPGRADE_UNKOWN
}FTP_UPGRADE_STAT;

typedef enum _upgrade_type
{
 UPGRADE_TYPE_FTP,
 UPGRADE_TYPE_HTTP,
 UPGRADE_TYPE_USB,
 UPGRADE_TYPE_UNKONW
}UPGRADE_TYPE;
/***********************************************************************
*                全局变量
***********************************************************************/
static upgrade_parameter upgradepara;
static char preset_net_upgrade_addr[512]={0};
static unsigned upgrade_partition_flag = 0;
static unsigned int cfe_size=0;
static unsigned int bmp_size=0;
static unsigned int kernel_size=0;
static unsigned int app_size=0;
static unsigned int percent = 0;
static unsigned int osflagflag = 0;
static   UPGRADE_STATUS mode=UPGRADE_OK;/*1->cfe,2->bmp,3->kernel,4->copy,-1-->return,-2-->change mode*/
extern  Int32 current_len_ftp;
extern  int down_size_http;


static FTP_UPGRADE_STAT ftp_stat = FTP_UPGRADE_UNKOWN;
static UPGRADE_TYPE upgrade_type = UPGRADE_TYPE_UNKONW;

static char s_ftp_filename[10] = "ix8100"; //debug for ftp
static char s_ftp_password[10] = "ix8100"; //debug for ftp
static int s_ftp_portnum = 9100;

extern HUIWND  h_ihd_main_menu_new;
extern HUIWND h_ihd_my_video;

/***********************************************************************
*                局部变量
***********************************************************************/
static unsigned stb_upgrade_draw_diplay(const unsigned flag_numb);
static unsigned stb_upgrade_complete_reboot(bool reboot_flag);


static int stb_upgrade_usb_mount();
/***********************************************************************
*                局部函数原型
***********************************************************************/
    /**********************************************************************
    * 函数名称: strtriml
    * 功能描述: 去除字符串右边的空字符
    * 访问的表: 无
    * 修改的表: 无
    * 输入参数: char * buf 字符串指针
    * 输出参数: 无
    * 返 回 值: 字符串指针
    * 其它说明: 无
    * 修改日期        版本号     修改人       修改内容
    * -----------------------------------------------
    *
    ***********************************************************************/
    static int stb_upgrade_progress_count(int size,UPGRADE_STATUS mode,int type)
    {
        int percent = 0;
        if(size == 0 ||cfe_size + bmp_size + kernel_size +app_size == 0)
        {
            return 0;
        }
        if(type == 1)
        {
            percent = size * 100 /(cfe_size + bmp_size + kernel_size + app_size);
        }
        else if(type == 2)
        {
            percent = (size + cfe_size) * 100 /(cfe_size + bmp_size + kernel_size + app_size);
        }
        else if(type == 3)
        {
            percent = (size + cfe_size + bmp_size) * 100 /(cfe_size + bmp_size + kernel_size + app_size);
        }
        else if(type == 4)
        {
            percent = (size + cfe_size + bmp_size + kernel_size) * 100 /(cfe_size + bmp_size + kernel_size + app_size);
        }
        stb_printf("===============================size=[%d],percent = [%d],total=[%d]\n",size,percent,cfe_size + bmp_size + kernel_size + app_size);
        if(percent < 0 || percent > 100)
        {
            return 0;
        }
        if(mode == UPGRADE_COPY_CFE)
        {
            return percent / 5;
        }
        else
        {
            return percent * 4 /5 + 20;
        }
    }

/**********************************************************************
* 函数名称: strtriml
* 功能描述: 去除字符串右边的空字符
* 访问的表: 无
* 修改的表: 无
* 输入参数: char * buf 字符串指针
* 输出参数: 无
* 返 回 值: 字符串指针
* 其它说明: 无
* 修改日期        版本号     修改人       修改内容
* -----------------------------------------------
*
***********************************************************************/
static char * strtriml(char * buf)
{
 int len,i;
 char * tmp = NULL;

 len = strlen(buf);
 if(len==0)
 {
  return buf;
 }
 tmp = (char*)malloc(len);
 memset(tmp,0x00,len);
 for(i = 0;i < len;i++)
 {
  if (buf[i] !=' ')
  {
   break;
  }
 }
 if (i < len)
 {
  strncpy(tmp,(buf+i),(len-i));  /*i is the number of space in the left*/
 }
 strncpy(buf,tmp,len);
 free(tmp);
 return buf;
}
/**********************************************************************
* 函数名称: strtrimr
* 功能描述: 去除字符串左边的空字符
* 访问的表: 无
* 修改的表: 无
* 输入参数: char * buf 字符串指针
* 输出参数: 无
* 返 回 值: 字符串指针
* 其它说明: 无
* 修改日期        版本号     修改人       修改内容
* -----------------------------------------------
*
***********************************************************************/
static char * strtrimr(char * buf)
{
 int len,i;   
 char * tmp=NULL;
 
 len = strlen(buf);
 if(len==0)
 {
  return buf;
 }
 tmp = (char*)malloc(len);
 memset(tmp,0x00,len);
 for(i = 0;i < len;i++)
 {
  if (buf[len-i-1] !=' ')
  {
   break;
  }
 }
 if (i < len)
 {
  strncpy(tmp,buf,len-i);
 }
 strncpy(buf,tmp,len);
 free(tmp);
 return buf;
}

/**********************************************************************
* 函数名称: stb_file_copy
* 功能描述: 从一个位置拷贝到另一位置
* 访问的表: 无
* 修改的表: 无
* 输入参数: char *src:源文件地址,char *dst:目标文件地址
* 输出参数:
* 返 回 值:
* 其它说明: 无
* 修改日期        版本号     修改人       修改内容
* -----------------------------------------------
*
***********************************************************************/
static int stb_file_copy(char *src,char *dst)
{
 FILE *fr,*fw;
 char buf[2*1024] = {0};
 int nr=1,nw=0;
 if(src == NULL || dst == NULL)
 {
  stb_printf("error file source!\n");
  return stb_failure;
 }
 fr = fopen(src,"rb");
 if(fr == NULL)
 {
  stb_printf("open source file error!-- %s -- \n",src);
  return stb_failure;
 }
 fw = fopen(dst,"wb");
 if(fw == NULL)
 {
  fclose(fr);
  stb_printf("open destnation file error!---%s --\n", dst);
  return stb_failure;
 }
 while(nr > 0)
 {
  nr = fread(buf,1,sizeof(buf),fr);
  if(nr == 0)
  {
   break;
  }
  nw = fwrite(buf,1,nr,fw);
  if(nr != nw)
  {
   fclose(fr);
   fclose(fw);
   stb_printf("fwrite error!\n");
   return stb_failure;
  }
 }
 fclose(fr);
 fclose(fw);
 return stb_success;
}

/**********************************************************************
* 函数名称: stb_get_file_size
* 功能描述: 获取指定文件的文件大小
* 输入参数:
  const char *filename :文件路径
* 返 回 值:
  文件实际大小
* 其它说明: 无
* 修改日期        版本号     修改人       修改内容
* -----------------------------------------------
*
***********************************************************************/
static unsigned long stb_get_file_size(const char *filename)
{
 struct stat buf;
 
 if(stat(filename, &buf)<0)
 {
  return 0;
 }
 return (unsigned long)buf.st_size;
}

/*对已下载文件MD5校验*/
/*0 on success,-1 on error*/
/**********************************************************************
* 函数名称: stb_upgrade_MD5_checkout
* 功能描述: 检测指定文件的MD5值是否与给定的值相同
* 输入参数:
  const char *filename :文件路径
  const char *MD5_val : 给定文件的MD5值
* 返 回 值:
  匹配:0
  否则:-1
* 其它说明: 无
* 修改日期        版本号     修改人       修改内容
* -----------------------------------------------
*
***********************************************************************/
static int stb_upgrade_MD5_checkout(const char *filename, const char *MD5_val)
{
 
 FILE *fp = NULL;
 char linebuf[64]={0};
 char cmd[256]={0}; 

#ifdef VFORK_SUPPORT
 sprintf(cmd, "%s %s >%s", MD5_CMD, filename, UPGRADEFILE_MD5);
 if(DareSTB_system(cmd,-1))
 {
  stb_printf("###>>>md5sum failed!\n");
  return -1;
 }
#else
 sprintf(cmd, "%s %s >%s", MD5_CMD, filename, UPGRADEFILE_MD5);
 DareSTB_system(cmd,-1);
#endif
 if((fp = fopen(UPGRADEFILE_MD5, "rb"))==NULL)
 {
  stb_printf("###>>>open the md5 file error!\n");
  return -1;
 }
 fgets(linebuf,64,fp);
 fclose(fp);
 remove(UPGRADEFILE_MD5);
 
 stb_printf("---right MD5:%s\n", MD5_val);
 stb_printf("---local MD5:%s\n", linebuf);
 if(strncmp(linebuf,MD5_val,32)!=0)
 {
  stb_printf("###>>>MD5_checkout error!\n");
  return -1;
 }

 //down_size_http = 0;
 //current_len_ftp = 0;
 return 0;
}
/**********************************************************************
* 函数名称: get_line_string_from_file
* 功能描述: 从文件中读取一行
* 输入参数:
  FILE *fp 文件句柄;
  char *buffer 一行字符串
  int maxlen 缓冲区最大长度
* 返 回 值:
  实际读的长度
* 其它说明: 无
* 修改日期        版本号     修改人       修改内容
* -----------------------------------------------
*
***********************************************************************/
static unsigned  get_line_string_from_file(FILE *fp, char *buffer, const unsigned maxlen)
{
 int  i=0, j=0;
 char ch1;

 for(i = 0, j = 0; i < maxlen; j++)
 {
  if(fread(&ch1, sizeof(char), 1, fp) != 1)
  {
   if(feof(fp) != 0)
   {
    if(j == 0)
    {
     stb_printf("upgrade version end\n");
     return -1;               /* 文件结束 */
    }
    else
    {
     break;
    }
   }
   if(ferror(fp) != 0)
   {
    return -2;        /* 读文件出错 */
   }
   return -2;
  }
  else
  {
   if(ch1 == '\n' || ch1 == 0x00) /* 换行 */
   {
    break;
   }
   if(ch1 == '\f' || ch1 == 0x1A)        /* '\f':换页符也算有效字符 */
   {
    buffer[i++] = ch1;
    break;
   }
   if(ch1 != '\r')
   {
    buffer[i++] = ch1;    /* 忽略回车符 */
   }
  }
 }
 buffer[i] = '\0';
 return i;
}

/******************************************************************
函数原型:stb_upgrade_uninit

功能描述:卸载升级初始化函数

参数:无

返回值: 
 成功:stb_success
 失败:stb_failure
 
注意:在退出程序或升级时调用
******************************************************************/
static int stb_upgrade_version_parse(char *version_file, upgrade_parameter *para)
{
 stb_printf("\nstb_upgrade_version_parse --- begin ---version_file = %s------ jason\n", version_file);
#define SEC_SYMBOL_LEFT '['
#define SEC_SYMBOL_RIGHT ']'

#define UP_START_SYMBOL  ">#S#:"
#define UP_END_SYMBOL  ">#E#:"
#define EACH_LINE_BUF  512
 FILE *fp = NULL;
 char each_line[EACH_LINE_BUF]="\0";
 char key[256];
 char tmpchar_s[6];
 char tmpchar_e[6];
 char tmp_swv_value[64]={0};
 char tmp_swv_value1[64]={0};
 char find_tmp[64]={0};
 char value[512];
 int n;
 char *find_value;
 int len;

 memset(para, 0, sizeof(upgrade_parameter));
 if((fp = fopen(version_file, "rb"))==NULL)
 {
  stb_printf("open file %s error!\n", version_file);
  return stb_failure;
 }
 while(!feof(fp))
 {
  n=get_line_string_from_file(fp, each_line, sizeof(each_line));
  stb_printf("each_line [ %s ]  ------- jason!\n", each_line);
  if(n==-2)
  {
   stb_printf("n == -2, there is an error ------- jason!\n");
   fclose(fp);
   return stb_failure;
  }
  else if(n==-1)
  {
   stb_printf("n == -1, there is an error ------- jason!\n");
   break;
  }
   
  if((each_line[0]==SEC_SYMBOL_LEFT)&&(each_line[n-1]==SEC_SYMBOL_RIGHT))
  {
   continue;
  }
  
  {
   tmpchar_s[0] = each_line[0];
   tmpchar_s[1] = each_line[1];
   tmpchar_s[2] = each_line[2];
   tmpchar_s[3] = each_line[3];
   tmpchar_s[4] = each_line[4];
   tmpchar_s[5] = '\0';
   if(!strncasecmp(tmpchar_s, UP_START_SYMBOL,5))
   {
    stb_printf("*************tmpchar_s[%s]------- jason!\n",tmpchar_s);
    if((find_value = strchr(each_line,'=')) != NULL)
    {
     stb_printf("*************find_value[%s]------- jason!\n",find_value);
     
     *find_value='\0';
     find_value++;
     strcpy(tmp_swv_value, find_value);
     strtriml(strtrimr(tmp_swv_value)); /*remove left & right space*/
     strncpy(tmp_swv_value1,tmp_swv_value,strlen(SOFTWAREVERSION_PREFIX));
     tmp_swv_value1[14] = '0';
     tmp_swv_value1[15] = '0';
     strncpy(find_tmp,SOFTWAREVERSION_PREFIX,strlen(SOFTWAREVERSION_PREFIX));
     find_tmp[14] = '0';
     find_tmp[15] = '0';

     stb_printf("*************find_tmp[%s]------- jason!\n",find_tmp);
     if(!strncasecmp(tmp_swv_value1, find_tmp,strlen(SOFTWAREVERSION_PREFIX)))
     {
      stb_printf("*************tmp_swv_value1[%s]------- jason!\n",tmp_swv_value1);
      strncpy(para->upgrade_companyInstruct, tmp_swv_value, strlen(SOFTWAREVERSION_PREFIX));
      while(!feof(fp))
      {
       n=get_line_string_from_file(fp, each_line, EACH_LINE_BUF);
       stb_printf("*************each_line[%s]------- jason!\n",each_line);
       if(n==-2)
       {
        stb_printf("n == -2, there is an error 11111111------- jason!\n");
        fclose(fp);
        return -1;
       }
       else if(n==-1)
       {
        stb_printf("n == -1, there is an error 11111111------- jason!\n");
        break;
       }
       tmpchar_e[0] = each_line[0];
       tmpchar_e[1] = each_line[1];
       tmpchar_e[2] = each_line[2];
       tmpchar_e[3] = each_line[3];
       tmpchar_e[4] = each_line[4];
       tmpchar_e[5] = '\0';
       if(!strncasecmp(tmpchar_e, UP_END_SYMBOL,5))
       {
        stb_printf("*************tmpchar_e[%s]------- jason!\n",tmpchar_s);
        break;
       }
       if((find_value = strchr(each_line,'=')) != NULL)
       {
        *find_value='\0';
        find_value++;
        strcpy(key, each_line);
        strtriml(strtrimr(key)); /*remove left & right space*/
        strcpy(value, find_value);
        strtriml(strtrimr(value)); /*remove left & right space*/
        if(!strcasecmp(key, "UpdateData"))
        {
         len = (strlen(value)>DATA_LEN)?DATA_LEN:(strlen(value));
         strncpy(para->upgrade_data, value, len);
        }
        else if(!strcasecmp(key, "UpdateBootldFileName"))
        {
         len = (strlen(value)>URL_LEN)?URL_LEN:(strlen(value));
         strncpy(para->upgrade_bootldFileName, value, len);
        }
        else if(!strcasecmp(key, "UpdateBmpFileName"))
        {
         len = (strlen(value)>URL_LEN)?URL_LEN:(strlen(value));
         strncpy(para->upgrade_bmpFileName, value, len);
        }
        else if(!strcasecmp(key, "UpdateKernelFileName"))
        {
         len = (strlen(value)>URL_LEN)?URL_LEN:(strlen(value));
         strncpy(para->upgrade_kernelFileName, value, len);
        }
        else if(!strcasecmp(key, "UpdateAppFileName"))
        {
         len = (strlen(value)>URL_LEN)?URL_LEN:(strlen(value));
         strncpy(para->upgrade_appFileName, value, len);
        }
        else if(!strcasecmp(key, "UpdateOSFlagFileName"))
        {
         len = (strlen(value)>URL_LEN)?URL_LEN:(strlen(value));
         strncpy(para->upgrade_bootkeyFileName, value, len);
        }
        else if(!strcasecmp(key, "UpdateVersion"))
        {
         len = (strlen(value)>VERSION_LEN)?VERSION_LEN:strlen(value);
         strncpy(para->upgrade_swVersion, value, len);
        }
        else if(!strcasecmp(key, "UpdateStbid"))
        {
         len = (strlen(value)>VERSION_LEN)?VERSION_LEN:strlen(value);
         strncpy(para->upgrade_stbid, value, len);
        }
        else if(!strcasecmp(key, "UpdateBootldMD5"))
        {
         len = (strlen(value)>VERSION_LEN)?VERSION_LEN:strlen(value);
         strncpy(para->upgrade_bootld_MD5, value, len);
        }
        else if(!strcasecmp(key, "UpdateBmpMD5"))
        {
         len = (strlen(value)>VERSION_LEN)?VERSION_LEN:strlen(value);
         strncpy(para->upgrade_bmp_MD5, value, len);
        }
        else if(!strcasecmp(key, "UpdateKernelMD5"))
        {
         len = (strlen(value)>VERSION_LEN)?VERSION_LEN:strlen(value);
         strncpy(para->upgrade_kernel_MD5, value, len);
        }
        else if(!strcasecmp(key, "UpdateAppMD5"))
        {
         len = (strlen(value)>VERSION_LEN)?VERSION_LEN:strlen(value);
         strncpy(para->upgrade_app_MD5, value, len);
        }
        else if(!strcasecmp(key, "UpdateOSFlagMD5"))
        {
         len = (strlen(value)>VERSION_LEN)?VERSION_LEN:strlen(value);
         strncpy(para->upgrade_bootkey_MD5, value, len);
        }
        else if(!strcasecmp(key, "UpdateBootldFileSize"))
        {
         para->upgrade_bootldFileSize=atol(value);
        }
        else if(!strcasecmp(key, "UpdateBmpFileSize"))
        {
         para->upgrade_bmpFileSize=atol(value);
        }
        else if(!strcasecmp(key, "UpdateKernelFileSize"))
        {
         para->upgrade_kernelFileSize=atol(value);
        }
        else if(!strcasecmp(key, "UpdateAppFileSize"))
        {
         para->upgrade_appFileSize=atol(value);
        }
        else if(!strcasecmp(key, "UpdateOSFlagFileSize"))
        {
         para->upgrade_bootkeyFileSize=atol(value);
        }
        else if(!strcasecmp(key, "UpdateBootldFlag"))
        {
         para->update_flag_bootldpartition=atoi(value);
        }
        else if(!strcasecmp(key, "UpdateBmpFlag"))
        {
         para->update_flag_bmppartition=atoi(value);
        }
        else if(!strcasecmp(key, "UpdateKernelFlag"))
        {
         para->update_flag_kernelpartition=atoi(value);
        }
        else if(!strcasecmp(key, "UpdateAppFlag"))
        {
         para->update_flag_apppartition=atoi(value);
        }
        else if(!strcasecmp(key, "UpdateOSFlag"))
        {
         para->update_flag_bootkeypartition=atoi(value);
        }
        else if(!strcasecmp(key, "UpdateStbidFlag"))
        {
         para->update_flag_stbidfile=atoi(value);
        }
       }
      }   
     }
     else
     {
      continue;
     }
    }
   }
   else
   {
    continue;
   }
  }
 }
 fclose(fp);
 stb_printf("stb_upgrade_version_parse --- end --------- jason\n\n");
 return 0;
}


/***********************************************************************
*                全局函数实现
***********************************************************************/
/******************************************************************
函数原型:stb_upgrade_init

功能描述:升级初始化函数

参数:无

返回值: 
 成功:stb_success
 失败:stb_failure
 
注意:在系统初始化时调用
******************************************************************/
unsigned  stb_upgrade_init(void)
{
 int res = UTFDeleteWindow(h_ihd_main_menu_new);
 memset(&upgradepara, 0, sizeof(upgradepara));
        DareMW_gfx_cleanall(); 
       DareMW_gfx_draw_message(UPGRADE_DONT_POWEROFF, 1, 400, 160,0xff);
 DareMW_gfx_draw_message(UPGRADE_DONT_POWEROFF, 1, 400, 160,0xff);
 return stb_success;
}

unsigned  stb_upgrade_init_usb(void)
{
 memset(&upgradepara, 0, sizeof(upgradepara));
        DareMW_gfx_cleanall(); 
       DareMW_gfx_draw_message(UPGRADE_DONT_POWEROFF, 1, 400, 160,0xff);
 DareMW_gfx_draw_message(UPGRADE_DONT_POWEROFF, 1, 400, 160,0xff);
 return stb_success;
}


/******************************************************************
函数原型:stb_upgrade_uninit

功能描述:卸载升级初始化函数

参数:无

返回值: 
 成功:stb_success
 失败:stb_failure
 
注意:在退出程序或升级时调用
******************************************************************/
unsigned  stb_upgrade_uninit(void)
{
 memset(&upgradepara, 0, sizeof(upgradepara));
 return stb_success;
}

/******************************************************************
函数原型:stb_upgrade_conditions_check

功能描述:升级条件检测,是否有需要升级的条件

参数:无

返回值: 
 0:没有合适的升级条件
 1:上次升级失败,需要升级
 2:软件有更新请升级
 
注意:
******************************************************************/
unsigned stb_upgrade_conditions_check(void)
{
 FILE *fp0;
 stb_printf("%s,%d,  %s !\n",__FILE__,__LINE__, __func__);
 stb_printf("current software version: %s \n", SOFTWAREVERSION);
 if( (fp0 = fopen(UPGRADE_FLAG_FILE,"rb")) != NULL )
 {
  /*上次升级失败*/
  stb_printf("%s,%d,upgrade failed last time \n",__FILE__,__LINE__);
  fclose(fp0);
  return 1;
 }
 else if(strncmp(upgradepara.upgrade_swVersion , SOFTWAREVERSION ,32)>0)  
 {
  /*软件有更新*/
  stb_printf("%s,%d,need upgrade!\n",__FILE__,__LINE__);
  return 2;
 }
 else
 {
  /*没有软件有更新和失败*/
  stb_printf("%s,%d,not need upgrade!\n",__FILE__,__LINE__);
  return 0;
 }
 return 0;
}

static int stb_upgrade_usb_mount()
{
 //system("mkdir /mnt/usbupgrade");
 DareSTB_system("mkdir /mnt/usbupgrade",-1);
 DG_MW_USB_STATUS UsbStatus;
 int ret = DG_MW_usb_attached_state_get(&UsbStatus);
 if (UsbStatus.usb_sdb1_status == 1)
 {
  //system("mount /dev/sdb1 -t vfat /mnt/usbupgrade/");
  DareSTB_system("mount /dev/sdb1 -t vfat /mnt/usbupgrade/",-1);
 }
 else if (UsbStatus.usb_sdc1_status == 1)
 {
  //system("mount /dev/sdc1 -t vfat /mnt/usbupgrade/");
  DareSTB_system("mount /dev/sdc1 -t vfat /mnt/usbupgrade/",-1);
 }
 else if (UsbStatus.usb_sdd1_status == 1)
 {
  //system("mount /dev/sdd1 -t vfat /mnt/usbupgrade/");
  DareSTB_system("mount /dev/sdd1 -t vfat /mnt/usbupgrade/",-1);
 }
 else
 {
 }
}

unsigned dare_upgrade_conditions_check(void)
{
 
 char tmp_issue[URL_LEN] = {0};
 char tmp_addr[URL_LEN] = {0};
 
 strcpy(tmp_issue, "");
 char test[20] = {0};
 char server[30] = {0};
 int ret = Dare_GetServerIP(test);
 if(ret == D_FAILURE)
 {
  strcpy(tmp_issue, "http://www.cnblogs.com/steven9801/admin/ftp://61.55.135.131/");
 }
 else
 {
  memcpy(server, test, 3);
  server[3] = '.';
  memcpy(server+4, test+3, 3);
  server[7] = '.';
  memcpy(server+8, test+6, 3);
  server[11] = '.';
  memcpy(server+12, test+9, 3);

  strcpy(tmp_issue, "ftp://");
  strcat(tmp_issue, server);

  stb_printf("%s,%d, server ip [ %s ]\n",__FILE__,__LINE__, tmp_issue);
 }

 strcpy(tmp_addr, tmp_issue);
 stb_printf("%s,%d, tmp_addr [ %s ]\n",__FILE__,__LINE__, tmp_addr);
 int len = 0;
 len = strlen(tmp_addr);
 if(len > 0)
 {
  if(tmp_addr[len-1]!='/') /*判断文件路径后带/ */
  {
   strcat(tmp_addr, "/");
  }
 }
 else
 {
  return false;
 } 
 
 if(strncasecmp(tmp_issue, "http://www.cnblogs.com/steven9801/admin/ftp://%22,6)==0/)
 {
     //if(DareMW_ftp_get(tmp_addr,9100,"ix8100","ix8100",UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME,1))
     if(DareMW_ftp_get(tmp_addr,s_ftp_portnum, s_ftp_filename, s_ftp_password,UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME,1))
  {
   stb_printf("%s,%d, ftp get [ %s ]\n",__FILE__,__LINE__, UPGRADE_VERSION_LOCAL_ADDR);
   return false;
  }
 }

 stb_upgrade_version_parse(UPGRADE_VERSION_LOCAL_ADDR, &upgradepara);
 
 if(2 == stb_upgrade_conditions_check())
 {
  stb_printf("%s,%d, ftp 可以进行升级!\n",__FILE__,__LINE__);
  return true;
 }
 else
 {
  stb_printf("%s,%d, ftp 无法升级!\n",__FILE__,__LINE__);
  return false;
 }
}


/******************************************************************
函数原型:stb_upgrade_valid_addr_check

功能描述:检测一个有效的升级服务器地址,

参数:
 
返回值:
 成功:stb_success
 失败:stb_failure
 
注意:按照预置、网络下发、本地的顺序检测
******************************************************************/
unsigned stb_upgrade_valid_addr_check(D_BOOL b_netupgrade)
{
 FILE *fp0;
 char tmp_addr[URL_LEN] = {0};
 char tmp_issue[URL_LEN] = {0};
 char tmp_usb[URL_LEN] = {0};
 char cmd[256] = {0};
 unsigned addr_falg = 0;
 unsigned len = 0;

 if (b_netupgrade)
 {
  stb_printf("尝试进行网络升级\n");
  goto server_issue_addr;
 }

usb_addr:

 stb_printf("%s,%d, usb 升级\n",__FILE__,__LINE__);
 
 upgrade_type = UPGRADE_TYPE_USB;
 
 strcpy(tmp_addr, USB_UPGRADE_ADDR);
 len = strlen(tmp_addr);
 
 if(len > 0)
 {
  if(tmp_addr[len-1]!='/') /*判断文件路径后带/ */
  {
   strcat(tmp_addr, "/");
  }
 }
 else
 {
  goto server_issue_addr;
 }
 char tmp_usb_addr[URL_LEN] = {0};
 stb_upgrade_usb_mount();
 strcpy(tmp_usb_addr, "/mnt/usbupgrade/stbupgrade/UpgradeVersion");

 if( (fp0 = fopen(tmp_usb_addr,"rb")) != NULL )
 {
  printf("open the version file: %s \n", tmp_usb_addr);
  addr_falg = 2;
  fclose(fp0);
  sprintf(cmd,"cp -rf %s %s",tmp_usb_addr ,UPGRADE_VERSION_LOCAL_ADDR);
  if(DareSTB_system(cmd,-1))
  {
   stb_printf("send system cmd Failed!\n");
   goto server_issue_addr;
  }
  else
  {
   printf("get the version file ok ---- usb upgrade !\n");
   goto next_run;
  }
 }
 else
 {
  stb_printf("%s,%d, open file [ %s ] error \n",__FILE__,__LINE__, tmp_usb_addr);
  goto server_issue_addr;
 }


server_issue_addr:

 memset(tmp_issue, 0, URL_LEN);
 char test[20] = {0};
 char server[30] = {0};
 int ret = Dare_GetServerIP(test);
#if 1  //---------debug for ftp download 
 if(ret == D_FAILURE)
 {
  strcpy(tmp_issue, "http://www.cnblogs.com/steven9801/admin/ftp://61.55.135.131/");
 }
 else
 {
  memcpy(server, test, 3);
  server[3] = '.';
  memcpy(server+4, test+3, 3);
  server[7] = '.';
  memcpy(server+8, test+6, 3);
  server[11] = '.';
  memcpy(server+12, test+9, 3);

  strcpy(tmp_issue, "ftp://");
  strcat(tmp_issue, server);

  stb_printf("%s,%d, server ip [ %s ]  \n",__FILE__,__LINE__, tmp_issue);
 }
#endif
 //strcpy(tmp_issue, "http://www.cnblogs.com/steven9801/admin/ftp://172.16.8.74%22);//----debug for ftp

 if(tmp_issue != NULL)
 {
  addr_falg = 3;

  strcpy(tmp_addr, tmp_issue);
  stb_printf("%s,%d, addr  [ %s ] \n",__FILE__,__LINE__, tmp_addr);
  len = strlen(tmp_addr);
  if(len > 0)
  {
   if(tmp_addr[len-1]!='/') /*判断文件路径后带/ */
   {
    strcat(tmp_addr, "/");
   }
  }
  else
  {
   stb_printf("%s,%d, upgrade failed  [ %s ] \n",__FILE__,__LINE__, tmp_addr);
   goto upgrate_failure;
  }
  
  if(strncasecmp(tmp_issue, "http://www.cnblogs.com/steven9801/admin/ftp://%22,6)==0/)
  {
   stb_printf("try get the upgrade version from ftp \n");
   upgrade_type = UPGRADE_TYPE_FTP;
   //if(STB_ERROR == DareMW_ftp_get(tmp_addr,9100,"ix8100","ix8100",UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME,1))
   if(STB_ERROR == DareMW_ftp_get(tmp_addr,s_ftp_portnum, s_ftp_filename, s_ftp_password,UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME,1))
   {
    stb_printf("from ftp server download UPGRADE_VERSION file Failed!\n");
    goto upgrate_failure;
   }
   else
   {
    stb_printf("ftp get upgrade version success \n");
    FILE *fp = NULL;
    if((fp = fopen(UPGRADE_VERSION_LOCAL_ADDR, "rb"))==NULL)
    {
     //若没有拿到升级文件,则使用 USB 升级
     stb_printf("open file %s error!\n", UPGRADE_VERSION_LOCAL_ADDR);
     goto upgrate_failure;
    }
    fclose(fp);
    goto next_run;
   }
  }
  else
  {
   stb_printf("%s,%d, not ftp \n",__FILE__,__LINE__);
   goto upgrate_failure;
  }
 }

next_run:

 stb_printf("%s,%d, addr check\n",__FILE__,__LINE__);

 stb_upgrade_version_parse(UPGRADE_VERSION_LOCAL_ADDR, &upgradepara);

 /*是否有可以升级的条件*/
 if(upgradepara.update_flag_bootldpartition || upgradepara.update_flag_bmppartition
 || upgradepara.update_flag_kernelpartition || upgradepara.update_flag_apppartition
 || upgradepara.update_flag_stbidfile || upgradepara.update_flag_bootkeypartition)
 {
  if(addr_falg)
  {
   strcpy(upgradepara.upgrade_addr, tmp_addr);
  }

  stb_printf("%s,%d, upgrade addr [ %s ]\n",__FILE__,__LINE__, upgradepara.upgrade_addr);

  switch(stb_upgrade_conditions_check())
  {
   case 1:
    /*上次升级失败*/
    stb_printf("last upgrade failed!\n");
    return stb_success;
    break;
   case 2:
    /*有软件更新*/
    stb_printf("we need upgrade!-----addr_check\n");
    return stb_success;
    break;
   default:
    if (upgrade_type == UPGRADE_TYPE_USB)
    {
     //当前是usb 升级,并且失败
     // 则尝试进行ftp  升级
     stb_printf("usb upgrade failed, goto ftp upgrade !\n\n");
     upgrade_type = UPGRADE_TYPE_FTP;
     goto server_issue_addr;
    }
    else if (upgrade_type == UPGRADE_TYPE_FTP)
    { 
     stb_printf("ftp upgrade failed, can not upgrade !\n");
     break;
    }
    else
    {}
    break;
  }
 }
 else
 {
  stb_printf("  没有可以升级的条件,返回\n");
 }
upgrate_failure:
  
 return stb_failure;
}

/******************************************************************
函数原型:stb_upgrade_get_version_control

功能描述:获取版本控制文件并解析

参数:
 const STB_Upgrade_Mode upgrade_mode :下载地址模式
 
返回值:
 成功:preset:1;usb:2;net_issue:3
 失败:0
 
注意:
******************************************************************/
unsigned stb_upgrade_get_version_control(const STB_Upgrade_Mode upgrade_mode)
{
 FILE *fp0;
 char tmp[URL_LEN] = {0};
 char cmd[256] = {0};
 unsigned addr_falg = 0;
 
 switch(upgrade_mode)
 {
  case STB_Upgrade_Preset:
   if(preset_net_upgrade_addr != NULL)
   {
    if(strncasecmp(preset_net_upgrade_addr, "http://www.cnblogs.com/steven9801/admin/ftp://%22,6)==0/)
    {
     //if(STB_ERROR == DareMW_ftp_get(preset_net_upgrade_addr,9100,"ix8100","ix8100",UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME,1))
     if(STB_ERROR == DareMW_ftp_get(preset_net_upgrade_addr,s_ftp_portnum,s_ftp_filename, s_ftp_password,UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME,1))
     {
      stb_printf("preset from ftp server download UPGRADE_VERSION file Failed!\n");
      addr_falg = 0;
     }
     else
     {
      addr_falg = 1;
     }
    }
    else if(strncasecmp(preset_net_upgrade_addr, "http://%22,7)==0/)
    {
           
                                        if(DG_MW_http_get(preset_net_upgrade_addr,UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME))
     //if(http_download_file(preset_net_upgrade_addr, SERVER_VERSION_NAME,UPGRADE_VERSION_LOCAL_ADDR) == stb_failure)
     {
      stb_printf("preset from http server download UPGRADE_VERSION file Failed!\n");
      addr_falg = 0;
     }
     else
     {
      addr_falg = 1;
     }
    }
   }
   break;
  case STB_Upgrade_USB:
   if( (fp0 = fopen("/mnt/usbupgrade/stbupgrade/UpgradeVersion","rb")) != NULL )
   {
    fclose(fp0);
    sprintf(cmd,"cp -rf /mnt/usbupgrade/stbupgrade/UpgradeVersion %s",UPGRADE_VERSION_LOCAL_ADDR);
    if(DareSTB_system(cmd,-1))
    {
     stb_printf("send system cmd Failed!\n");
     addr_falg = 0;
    }
    else
    {
     addr_falg = 2;
    }
   }
   break;
  case STB_Upgrade_Net_Issue:
   dare_program_get_UpgradeDomain(tmp,URL_LEN);  
   if(tmp != NULL)
   {
    if(strncasecmp(tmp, "http://www.cnblogs.com/steven9801/admin/ftp://%22,6)==0/)
    {
                 //if(STB_ERROR == DareMW_ftp_get(tmp,9100,"ix8100","ix8100",UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME,1))
                 if(STB_ERROR == DareMW_ftp_get(tmp,s_ftp_portnum,s_ftp_filename, s_ftp_password,UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME,1))
     {
      stb_printf("from ftp server download UPGRADE_VERSION file Failed!\n");
      addr_falg = 0;
     }
     else
     {
      addr_falg = 3;
     }
    }
    else if(strncasecmp(tmp, "http://%22,7)==0/)
    {
            if(DG_MW_http_get(tmp,UPGRADE_VERSION_LOCAL_ADDR,SERVER_VERSION_NAME))
     //if(http_download_file(tmp, SERVER_VERSION_NAME,UPGRADE_VERSION_LOCAL_ADDR) == stb_failure)
     {
      stb_printf("from http server download UPGRADE_VERSION file Failed!\n");
      addr_falg = 0;
     }
     else
     {
      addr_falg = 3;
     }
    }
   }
   break;
 }
 
 return addr_falg;
 
}

/******************************************************************
函数原型:stb_down_upgrade_file

功能描述:获取升级文件并校验

参数:
 const char * upgrade_addr:有效的软件升级地址
 
返回值:
 成功:stb_success
 失败:stb_failure
 
注意:
******************************************************************/
unsigned stb_down_upgrade_file(const char * upgrade_addr,pthread_t *tid1, D_BOOL bnetupgrade)
{
 char cmd[256] = {0};
 FILE *fpt0 = NULL,*fpt1 = NULL;
 unsigned flag_p0 = 0,flag_p1 = 0;

 char tmp_addr[URL_LEN] = {0};

 printf("stb_down_upgrade_file =====%s ============\n", upgrade_addr);
 {      
            sprintf(cmd,"umount %s",SYSTEM_FLASH_BLOCK_temp_path);
            if(DareSTB_system(cmd,-1))
            {
                stb_printf("umount  %s failed\n",FLASH_BLOCK_temp_partition);
                //return stb_failure;
            }
  sprintf(cmd,"flash_eraseall  %s",FLASH_BLOCK_temp_partition);
  if(DareSTB_system(cmd,-1))
  {
                stb_printf("flash_eraseall  %s failed\n",FLASH_BLOCK_temp_partition);
                //return stb_failure;
  }
 }
 stb_printf("upgrade_addr is  %s ! ---- jason \n", upgrade_addr);
 if(upgrade_addr == NULL)
 {
            stb_printf("upgrade_addr is NULL!\n");
            return stb_failure;
 }
 DareSTB_system("mkdir -p /home/upgrade",-1);
 sprintf(cmd,"mount  %s  %s",FLASH_BLOCK_temp_partition_handle, SYSTEM_FLASH_BLOCK_temp_path);
 if(DareSTB_system(cmd,-1))
 {
            stb_printf("mount  %s  %s failed\n",FLASH_BLOCK_temp_partition_handle, SYSTEM_FLASH_BLOCK_temp_path);
            //return stb_failure;/*if mount twice,the second must be fail */
 }
 //DareMW_gfx_draw_message(UPGRADE_DOWNLOADING, 1, 180, 290,0xff);
 DareMW_gfx_draw_message(UPGRADE_DOWNLOADING, 1, 430, 290,0xff);
 /*display thread need 192k*/
 //*tid1=DG_task_create("upgrade", D_DEFAULT_PRIORITY, D_DEFAULT_STACK_SIZE*6, (void *)stb_upgrade_draw_diplay, NULL);
 pthread_create(tid1, NULL, (void *)stb_upgrade_draw_diplay, NULL);

 if (bnetupgrade == D_FALSE)
 {
  goto usb_download;
 }

ftp_download: 
        stb_printf("%s,%d,upgrade_addr[%s]\n",__FILE__,__LINE__,upgrade_addr);
 if(strncasecmp(upgrade_addr, "http://www.cnblogs.com/steven9801/admin/ftp://%22,6)==0/)
 {
  upgrade_type = UPGRADE_TYPE_FTP;
  
            cfe_size = bmp_size = kernel_size = app_size = 0;
           
  if(upgradepara.update_flag_bootldpartition)
  {
                 //cfe_size = DareMW_ftp_get_info(upgrade_addr,21,"anonymous",NULL,upgradepara.upgrade_bootldFileName);
                 cfe_size = DareMW_ftp_get_info(upgrade_addr,9100,"ix8100","ix8100",upgradepara.upgrade_bootldFileName);
  }
  if(upgradepara.update_flag_bmppartition)
  {
    //bmp_size = DareMW_ftp_get_info(upgrade_addr,21,"anonymous",NULL,upgradepara.upgrade_bmpFileName);
    bmp_size = DareMW_ftp_get_info(upgrade_addr,9100,"ix8100","ix8100",upgradepara.upgrade_bmpFileName);
  }
  if(upgradepara.update_flag_kernelpartition)
  {
      //kernel_size= DareMW_ftp_get_info(upgrade_addr,21,"anonymous",NULL,upgradepara.upgrade_kernelFileName);
      kernel_size= DareMW_ftp_get_info(upgrade_addr,9100,"ix8100","ix8100",upgradepara.upgrade_kernelFileName);
  }
  if(upgradepara.update_flag_apppartition)
  {
   //app_size= DareMW_ftp_get_info(upgrade_addr,21,"anonymous",NULL,upgradepara.upgrade_appFileName);
   app_size= DareMW_ftp_get_info(upgrade_addr,9100,"ix8100","ix8100",upgradepara.upgrade_appFileName);
  }
  
  stb_printf(" upgradepara.upgrade_appFileName = %s ----- jason \n", upgradepara.upgrade_appFileName);
  
  stb_printf("cfe[%d],bmp[%d],kernel[%d],app[%d]\n",cfe_size,bmp_size,kernel_size,app_size);
  if(upgradepara.update_flag_bootldpartition)
  {
   /*下载启动引导程序文件*/
   mode = UPGRADE_DOWNLOAD_CFE;

                    //if(STB_ERROR == DareMW_ftp_get(upgrade_addr,9100,"ix8100","ix8100",UPGRADE_LOCAL_bootld_FILE,upgradepara.upgrade_bootldFileName,1))
                    if(STB_ERROR == DareMW_ftp_get(upgrade_addr,s_ftp_portnum,s_ftp_filename, s_ftp_password,UPGRADE_LOCAL_bootld_FILE,upgradepara.upgrade_bootldFileName,1))
   {
    
    stb_printf("from ftp server download UPGRADE_LOCAL_bootld_FILE file Failed!\n");
    return stb_failure;
   }
   else
   {
    if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_bootld_FILE, upgradepara.upgrade_bootld_MD5) == 0)
    {
                                mode = UPGRADE_CHANGE_MODE;
                                current_len_ftp = 0;   
    }
    else
    {
     return stb_failure;
    }
   }
  }
  if(upgradepara.update_flag_bootkeypartition)
  {
   /*下载启动引导程序文件*/
                   // if(STB_ERROR == DareMW_ftp_get(upgrade_addr,9100,"ix8100","ix8100",UPGRADE_LOCAL_bootkey_FILE,upgradepara.upgrade_bootkeyFileName,1))
                    if(STB_ERROR == DareMW_ftp_get(upgrade_addr,s_ftp_portnum,s_ftp_filename, s_ftp_password,UPGRADE_LOCAL_bootkey_FILE,upgradepara.upgrade_bootkeyFileName,1))
   {
    
    stb_printf("from ftp server download UPGRADE_LOCAL_bootkey_FILE file Failed!\n");
    return stb_failure;
   }
   else
   {
    if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_bootkey_FILE, upgradepara.upgrade_bootkey_MD5) == 0)
    {
                                //mode = UPGRADE_CHANGE_MODE;
                                //current_len_ftp = 0;
            //percent = stb_upgrade_progress_count(cfe_size, 1,1);
    }
    else
    {
                                return stb_failure;
    }
   }
  }
  if(upgradepara.update_flag_bmppartition)
  {
   /*下载开机画面文件*/
   mode = UPGRADE_DOWNLOAD_BMP;

                    //if(STB_ERROR == DareMW_ftp_get(upgrade_addr,9100,"ix8100","ix8100",UPGRADE_LOCAL_bmp_FILE,upgradepara.upgrade_bmpFileName,1))
                    if(STB_ERROR == DareMW_ftp_get(upgrade_addr,s_ftp_portnum,s_ftp_filename, s_ftp_password,UPGRADE_LOCAL_bmp_FILE,upgradepara.upgrade_bmpFileName,1))
   {
    
    stb_printf("from ftp server download UPGRADE_LOCAL_bmp_FILE file Failed!\n");

    return stb_failure;
   }
   else
   {
    if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_bmp_FILE, upgradepara.upgrade_bmp_MD5) == 0)
    {
                   current_len_ftp = 0;
                   mode = UPGRADE_CHANGE_MODE;
             //percent = stb_upgrade_progress_count(bmp_size, 1,2);
    }
    else
    {
            //must delete the file
      return stb_failure;
    }
   }
  }
  if(upgradepara.update_flag_kernelpartition)
  {
   stb_printf("update kernel ------[ ftp ]-----------download\n");
   /*下载内核文件*/
   mode = UPGRADE_DOWNLOAD_KERNEL;

                    //if(STB_ERROR == DareMW_ftp_get(upgrade_addr,9100,"ix8100","ix8100",UPGRADE_LOCAL_kernel_FILE,upgradepara.upgrade_kernelFileName,1))
                    if(STB_ERROR == DareMW_ftp_get(upgrade_addr,s_ftp_portnum,s_ftp_filename, s_ftp_password,UPGRADE_LOCAL_kernel_FILE,upgradepara.upgrade_kernelFileName,1))
   {
    
    stb_printf("from ftp server download UPGRADE_LOCAL_kernel_FILE file Failed!\n");

    return stb_failure;
   }
   else
   {
    if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_kernel_FILE, upgradepara.upgrade_kernel_MD5) == 0)
    {
                   current_len_ftp = 0;
                   mode = UPGRADE_CHANGE_MODE;
            //percent = stb_upgrade_progress_count(kernel_size, 1,3);
    }
    else
    {
            //must delete the file
     return stb_failure;
    }
   }
  }
  if(upgradepara.update_flag_apppartition)
  {
   /*下载app文件*/
   printf("ftp download application begin -------------jason\n");
   mode = UPGRADE_DOWNLOAD_APP;

            // if(STB_ERROR == DareMW_ftp_get(upgrade_addr,9100,"ix8100","ix8100",UPGRADE_LOCAL_app_FILE,upgradepara.upgrade_appFileName,1))
             if(STB_ERROR == DareMW_ftp_get(upgrade_addr,s_ftp_portnum,s_ftp_filename, s_ftp_password,UPGRADE_LOCAL_app_FILE,upgradepara.upgrade_appFileName,1))
   {
    
    stb_printf("from ftp server download UPGRADE_LOCAL_kernel_FILE file Failed!\n");

    return stb_failure;
   }
   else
   {
    if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_app_FILE, upgradepara.upgrade_app_MD5) == 0)
    {
     return stb_success;
    }
    else
    {
     return stb_failure;
    }
   }
  }
 }
 else
 {
  printf("不是ftp 路径, USB 升级中...... \n");
 }

usb_download:

 memset(tmp_addr, 0, URL_LEN);
 strcpy(tmp_addr, USB_UPGRADE_ADDR);
 int len = strlen(tmp_addr);
 if(len > 0)
 {
  if(tmp_addr[len-1]!='/') /*判断文件路径后带/ */
  {
   strcat(tmp_addr, "/");
  }
 }
 else
 {
  stb_printf("tmp_addr [ %s ] error \n", tmp_addr);
  goto ftp_download;
 }

 char tmp_usb_addr[URL_LEN] = {0};
 FILE *fp0 = NULL;
 strcpy(tmp_usb_addr, tmp_addr);
 strcat(tmp_usb_addr, "UpgradeVersion");

 upgrade_type = UPGRADE_TYPE_USB;
 stb_printf("----------usb 升级地址【 %s 】----------jason\n", tmp_usb_addr);
 int usb_sysret = 0;
 
 if( (fp0 = fopen(tmp_usb_addr,"rb")) != NULL )
 {
  fclose(fp0);
  sprintf(cmd,"cp -rf %s %s",tmp_usb_addr ,UPGRADE_VERSION_LOCAL_ADDR);
  if(DareSTB_system(cmd,-1))
  {
   stb_printf("send system cmd Failed!\n");
   goto ftp_download;
  }
  else
  {
   goto next_run;
  }
 }

 
next_run:

 upgrade_type = UPGRADE_TYPE_USB;
 stb_printf("使用USB 升级软件 !\n");
 char tmp_buf[URL_LEN]={0};
 if(upgradepara.update_flag_bootldpartition)
 {
  /*下载启动引导程序文件*/
  mode = UPGRADE_DOWNLOAD_CFE;
  strcpy(tmp_buf, upgrade_addr);
  strcat(tmp_buf, upgradepara.upgrade_bootldFileName);
  if(stb_file_copy(tmp_buf, UPGRADE_LOCAL_bootld_FILE) == stb_failure)
  {
   stb_printf("from usb device download UPGRADE_LOCAL_bootld_FILE file Failed!\n");
   return stb_failure;
  }
  else
  {
   if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_bootld_FILE, upgradepara.upgrade_bootld_MD5) == 0)
   {
           //percent = stb_upgrade_progress_count(cfe_size, 1,1);
   }
   else
   {
           //must delete the file
    return stb_failure;
   }
  }
 }
 if(upgradepara.update_flag_bootkeypartition)
 {
  /*下载启动引导程序文件*/
  //mode = UPGRADE_DOWNLOAD_BOOTKEY;
  strcpy(tmp_buf, upgrade_addr);
  strcat(tmp_buf, upgradepara.upgrade_bootkeyFileName);
  if(stb_file_copy(tmp_buf, UPGRADE_LOCAL_bootkey_FILE) == stb_failure)
  {
   stb_printf("from usb device download UPGRADE_LOCAL_bootkey_FILE file Failed!\n");
   return stb_failure;
  }
  else
  {
   if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_bootkey_FILE, upgradepara.upgrade_bootkey_MD5) == 0)
   {
           //percent = stb_upgrade_progress_count(cfe_size, 1,1);
   }
   else
   {
           //must delete the file
    return stb_failure;
   }
  }
 }
 if(upgradepara.update_flag_bmppartition)
 {
  /*下载开机画面文件*/
  mode = UPGRADE_DOWNLOAD_BMP;
  strcpy(tmp_buf, upgrade_addr);
  strcat(tmp_buf, upgradepara.upgrade_bmpFileName);
  if(stb_file_copy(tmp_buf, UPGRADE_LOCAL_bmp_FILE) == stb_failure)
  {
   stb_printf("from usb device download UPGRADE_LOCAL_bmp_FILE file Failed!\n");
   return stb_failure;
  }
  else
  {
   if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_bmp_FILE, upgradepara.upgrade_bmp_MD5) == 0)
   {
    stb_printf("download starting logo success : %s \n", UPGRADE_LOCAL_bmp_FILE);
           //percent = stb_upgrade_progress_count(bmp_size, 1,2);
   }
   else
   {
           //must delete the file
    return stb_failure;
   }
  }
 }
 if(upgradepara.update_flag_kernelpartition)
 {
  stb_printf("update kernel ------[ usb ]-----------download\n");
  /*下载内核文件*/
  mode = UPGRADE_DOWNLOAD_KERNEL;
  strcpy(tmp_buf, upgrade_addr);
  strcat(tmp_buf, upgradepara.upgrade_kernelFileName);
  if(stb_file_copy(tmp_buf, UPGRADE_LOCAL_kernel_FILE) == stb_failure)
  {
   stb_printf("from usb device download UPGRADE_LOCAL_kernel_FILE file Failed!\n");
   return stb_failure;
  }
  else
  {
   if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_kernel_FILE, upgradepara.upgrade_kernel_MD5) == 0)
   {
           //percent = stb_upgrade_progress_count(kernel_size, 1,3);
   }
   else
   {
           //must delete the file
    return stb_failure;
   }
  }
 }
 if(upgradepara.update_flag_apppartition)
 {
  /*下载app文件*/
  mode = UPGRADE_DOWNLOAD_APP;
  strcpy(tmp_buf, upgrade_addr);
  strcat(tmp_buf, upgradepara.upgrade_appFileName);
  stb_printf("下载app文件 [ %s ] \n ", tmp_buf);
  if(stb_file_copy(tmp_buf, UPGRADE_LOCAL_app_FILE) == stb_failure)
  {
   stb_printf("from usb device download UPGRADE_LOCAL_kernel_FILE file Failed!\n");
   if (bnetupgrade == D_FALSE)   
   {
    stb_printf("usb download failed, got ftp download app \n");
    goto ftp_download;
   }
   return stb_failure;
  }
  else
  {
   if(stb_upgrade_MD5_checkout(UPGRADE_LOCAL_app_FILE, upgradepara.upgrade_app_MD5) == 0)
   {
           //percent = stb_upgrade_progress_count(kernel_size, 1,3);
   }
   else
   {
           //must delete the file
    return stb_failure;
   }
  }
 } 

 return stb_success;
}

/******************************************************************
函数原型:stb_erasure_upgrade_partition

功能描述:擦除升级分区

参数:
 const unsigned partition_num:需擦除的分区
 
返回值:
 成功:stb_success
 失败:stb_failure
 
注意:
******************************************************************/
unsigned stb_erasure_upgrade_partition(const char * partition_num)
{
 /*擦除*/
 char cmd[256] = {0};
 
 sprintf(cmd,"flash_eraseall  %s",partition_num);
 if(DareSTB_system(cmd,-1))
 {
  return stb_failure;
 }
 return stb_success;
}

/******************************************************************
函数原型:stb_upgrade_write_partition

功能描述:写指定的文件到指定的分区

参数:
 const unsigned partition_num:升级分区
 const char upgradefile:升级文件
 
返回值:
 成功:stb_success
 失败:stb_failure
 
注意:
******************************************************************/
unsigned stb_upgrade_write_partition(const char * partition_num,const char * upgradefile)
{
 /*写数据*/
 char cmd[256] = {0};
 
 sprintf(cmd,"nandwrite -p %s  %s",partition_num, upgradefile);
 if(DareSTB_system(cmd,-1))
 {
  return stb_failure;
 }
 return stb_success;
}
int stb_upgrade_download_percent()
{
    int down_size = 0;
    int ftp_size;
    int http_size;
    //while(1)
    {
        if(mode <= UPGRADE_ERROR)
        {
            return 0;
            //break;
        }
        //sleep(1);
        if(cfe_size + bmp_size + kernel_size + app_size == 0)
        {
            return 0;
            //continue;
        }
        //ftp_size = DareMW_ftp_get_currentsize();
        //http_size = DG_MW_http_downloaded_size();
        //down_size = ftp_size + http_size;
        down_size = current_len_ftp+ down_size_http;
        //stb_printf("===========ftp[%d],http[%d]===total[%d]======\n",current_len_ftp,down_size_http,cfe_size + bmp_size + kernel_size + app_size);
        if(mode == UPGRADE_DOWNLOAD_CFE)
        {
            percent = down_size * 100/(cfe_size + bmp_size + kernel_size + app_size);
        }
        else if(mode == UPGRADE_DOWNLOAD_BMP)
        {
            percent = (down_size + cfe_size) * 100 /(cfe_size + bmp_size + kernel_size + app_size);
        }
        else if(mode == UPGRADE_DOWNLOAD_KERNEL)
        {
            percent = (down_size + cfe_size + bmp_size) * 100 /(cfe_size + bmp_size + kernel_size + app_size);
        }
        else if(mode == UPGRADE_DOWNLOAD_APP)
        {
            percent = (down_size + cfe_size + bmp_size + kernel_size) * 100 /(cfe_size + bmp_size + kernel_size + app_size);
        }
        else
        {
            return 0;
        }
    }
    return;
}
/******************************************************************
函数原型:stb_upgrade_start

功能描述:写指定的文件到指定的分区

参数:
 const unsigned partition_num:升级分区
 const char upgradefile:升级文件
 
返回值:
 成功:stb_success
 失败:stb_failure
 
注意:
******************************************************************/
int stb_upgrade_start(D_BOOL b_netupgrade)
{
        char cmd[256] = {0};
        FILE *fpt0 = NULL,*fpt1 = NULL;
        unsigned flag_p0 = 0,flag_p1 = 0;
        pthread_t tid1=0;
        int i;
        mode = UPGRADE_OK;
        //stb_upgrade_init();

#if 0
        DareMW_gfx_cleanall(); 
       // DareMW_gfx_draw_message(UPGRADE_DONT_POWEROFF, 1, 140, 160,0xff);
       DareMW_gfx_draw_message(UPGRADE_DONT_POWEROFF, 1, 400, 160,0xff);
 DareMW_gfx_draw_message(UPGRADE_DONT_POWEROFF, 1, 400, 160,0xff);
#endif
  stb_printf("\n***************************************\n");
  stb_printf("*****************************************\n");
  stb_printf("*****************************************\n");
  stb_printf("**********begin upgrade********************\n");
  stb_printf("*****************************************\n");
  stb_printf("*****************************************\n");
  stb_printf("*****************************************\n\n");

        if(stb_upgrade_valid_addr_check(b_netupgrade) == stb_failure)
        {
            /*无有效升级地址*/
            stb_printf("升级地址无效\n");
            mode = UPGRADE_ERROR;
            return stb_failure;
        }
 /*download and set download files's size*/
 if(stb_down_upgrade_file(upgradepara.upgrade_addr,&tid1, b_netupgrade) == stb_failure)
 {  
  /*升级文件下载失败*/
  DareMW_gfx_cleanall(); 
  //DareMW_gfx_draw_message(UPGRADE_FAIL_GOTO_EPG, 1, 156, 290,0xff);
  //DareMW_gfx_draw_message(UPGRADE_FAIL_GOTO_EPG, 1, 256, 290,0xff);
  sleep(3);
  mode = UPGRADE_ERROR;
  stb_printf("================down load failed====================\n");
  return stb_failure;
 }
 mode = UPGRADE_COPY;
 stb_printf("================down load ok====================\n");
 //DareMW_gfx_draw_message(UPGRADE_INSTALLING, 1, 180, 290,0xff);
 DareMW_gfx_draw_message(UPGRADE_INSTALLING, 1, 430, 290,0xff);
 
 if(upgradepara.update_flag_bootldpartition)
 {
  /*升级引导分区*/
         //mode = UPGRADE_COPY_CFE;
  if(stb_erasure_upgrade_partition(getenv(FLASH_BLOCK_CFE)) == stb_failure)
  {
                    stb_printf("erasure FLASH_BLOCK_bootld partition fail!\n");
                    mode = UPGRADE_ERROR;
                    return stb_failure;
  }
  if(stb_upgrade_write_partition(getenv(FLASH_BLOCK_CFE), UPGRADE_LOCAL_bootld_FILE) == stb_failure)
  {
                    stb_printf("nandwrite UPGRADE_LOCAL_bootld_FILE to FLASH_BLOCK_bootld partition fail!\n");
                    mode = UPGRADE_ERROR;
                    return stb_failure;
  }
  percent = stb_upgrade_progress_count(cfe_size, 2,1);
 }
 if(upgradepara.update_flag_bmppartition)
 {
  /*升级开机画面分区*/
  //mode = UPGRADE_COPY_BMP;
  stb_printf("begin to upgrade the starting logo bmp  \n");
  if(stb_erasure_upgrade_partition(getenv(FLASH_BLOCK_BMP)) == stb_failure)
  {
                    stb_printf("erasure FLASH_BLOCK_bmp partition fail!\n");
                    mode = UPGRADE_ERROR;
                    return stb_failure;
  }
  stb_printf("erase the starting logo bmp  success \n");
  if(stb_upgrade_write_partition(getenv(FLASH_BLOCK_BMP), UPGRADE_LOCAL_bmp_FILE) == stb_failure)
  {
                    stb_printf("nandwrite UPGRADE_LOCAL_bmp_FILE to FLASH_BLOCK_bmp partition fail!\n");
                    mode = UPGRADE_ERROR;
                    return stb_failure;
  }
  stb_printf("erase the starting logo bmp  success \n");
  percent = stb_upgrade_progress_count(bmp_size, 2,2);
 }
        if(upgradepara.update_flag_kernelpartition)
        {
                //mode = UPGRADE_COPY_KERNEL;
                stb_printf("begain to upgrade main kernel!\n");
         if(stb_erasure_upgrade_partition(getenv(FLASH_BLOCK_KERNEL)) == stb_failure)
         {
          stb_printf("erasure FLASH_BLOCK_kernel_main partition fail!\n");
                     mode = UPGRADE_ERROR;
          return stb_failure;
         }
  else
  {
    stb_printf("erase 内核成功!\n");
  }
  
         if(stb_upgrade_write_partition(getenv(FLASH_BLOCK_KERNEL), UPGRADE_LOCAL_kernel_FILE) == stb_failure)
         {
          stb_printf("nandwrite UPGRADE_LOCAL_kernel_FILE to FLASH_BLOCK_kernel_main partition fail!\n");
                     mode = UPGRADE_ERROR;
          return stb_failure;
         }
  else
  {
    stb_printf("升级内核成功!\n");
  }
        }
        if(upgradepara.update_flag_apppartition)
        {
                stb_printf("begain to upgrade main app!\n");
                //mode = UPGRADE_COPY_APP;
         if(stb_erasure_upgrade_partition(getenv(FLASH_BLOCK_APP)) == stb_failure)
         {
          stb_printf("erasure FLASH_BLOCK_kernel_main partition fail!\n");
                                mode = UPGRADE_ERROR;
          return stb_failure;
         }
  else
  {
    stb_printf("erase 应用程序成功!\n");
  }
  
         if(stb_upgrade_write_partition(getenv(FLASH_BLOCK_APP), UPGRADE_LOCAL_app_FILE) == stb_failure)
         {
          stb_printf("nandwrite UPGRADE_LOCAL_kernel_FILE to FLASH_BLOCK_kernel_main partition fail!\n");
                                mode = UPGRADE_ERROR;
          return stb_failure;
         }
  else
  {
    stb_printf("升级应用程序成功!\n");
  }
        }

 mode = UPGRADE_ERROR; //add by jason,升级完成,不要描画了
 sleep(3);
 DareMW_gfx_cleanall();   
        /*更新启动参数*/
        stb_printf("upgrade success!now reboot!\n");
  
#if 1  
        if(osflagflag == 0)
        {
            if(upgradepara.update_flag_bootkeypartition)
            {
             /*升级引导分区*/
             DareSTB_system("osflagsutil "UPGRADE_LOCAL_bootkey_FILE,-1);
             mode = UPGRADE_COPY_BOOTKEY;
             //percent = stb_upgrade_progress_count(cfe_size, 2,1);
            }
            else
            {
                DareSTB_system("osflagsutil ./osflags.xml",-1);
            }
        }
#endif
  
        stb_printf("=============over============!\n");
  DareMW_gfx_cleanall(); 
        stb_upgrade_complete_reboot(1);
  
        return 0;
}

/******************************************************************
函数原型:stb_upgrade_complete_reboot

功能描述:写指定的文件到指定的分区

参数:
 const unsigned partition_num:升级分区
 const char upgradefile:升级文件
 
返回值:
 成功:stb_success
 失败:stb_failure
 
注意:
******************************************************************/
static unsigned stb_upgrade_complete_reboot(bool reboot_flag)
{
 if(reboot_flag)
 {
  if(DareSTB_system("reboot",-1))
  {
   stb_printf("stb systtem reboot fail!\n");
   return stb_failure;
  }
 }
 else
 {
  stb_printf("not must reboot stb system!\n");
  return stb_success;
 }
}

/******************************************************************
函数原型:stb_upgrade_start

功能描述:写指定的文件到指定的分区

参数:
 const unsigned partition_num:升级分区
 const char upgradefile:升级文件
 
返回值:
 成功:stb_success
 失败:stb_failure
 
注意:
******************************************************************/
static unsigned stb_upgrade_draw_diplay(const unsigned flag_numb)
{
        int ret=-1;
        BMPInfo BInfo;
        BMPInfo BInfo_p;
        BMPInfo BInfo_0;
        BMPInfo BInfo_1;
        unsigned char *bits = NULL;
        unsigned char *bits_0 = NULL;
        unsigned char *bits_1 = NULL;
        unsigned char *bits_p = NULL;
        unsigned char *bits_tmp = NULL;
        DG_RECT src_rect,dst_rect;
        char tmp[10]={0};
        char *ptr;
        int flag = 0;
        int percent_tmp=0;
        int n=0,i=0;
        int x,y,w,h;
        int tens,unit;
        x = PROGRESS_BAR_X;
        y = PROGRESS_BAR_Y;

        /*main upgrade bar*/  
        bits = DareMW_jpeg_decode(PROGRESS_BAR_PICTURE,&bits,&BInfo.width,&BInfo.height);
        if(bits == NULL)
        {
            stb_printf("JPG decode error!\n");
            return -1;
        }
        /*percent sign*/
        bits_p = DareMW_jpeg_decode(PROGRESS_BAR_PERCENT,&bits_p,&BInfo_p.width,&BInfo_p.height);
        if(bits_p == NULL)
        {
            stb_printf("JPG decode error!\n");
            return -1;
        }
        /*blak blok for clear upgrade bar*/
        bits_tmp = (unsigned char *)malloc(BInfo.width*BInfo.height*4);
        memset((unsigned char*)bits_tmp,0xff000000,BInfo.width*BInfo.height*4);
        percent = 0;/*init*/

   
        while(1)
        {    
            if(mode == UPGRADE_ERROR)/*over*/
            {
                break;
            }
            //if(mode >= UPGRADE_DOWNLOAD_CFE || mode <= UPGRADE_DOWNLOAD_BOOTKEY)/*download mode*/
            if(mode != UPGRADE_COPY)/*download mode*/
            {
                stb_upgrade_download_percent();
                #if 0
                if(percent == percent_tmp)
                {
                    continue;
                }
                #endif
                n = percent / 5;
                if(n <= percent_tmp)
                {
                    continue;
                }

    DG_AL_gdi_refurbish_output_screen(0);
    DG_AL_gdi_refurbish_output_screen(1);
                /*DRAW PERCENT*/
                if(n<20)
                {
                    tens = n*5/10;
                    unit = n*5%10;
                    char ten[20];
                    char uni[20];
                    sprintf(ten,"upgrade/%d.jpg",tens);
                    sprintf(uni,"upgrade/%d.jpg",unit);
                    stb_printf("ten[%s],uni[%s]\n",ten,uni);
                    bits_0 = DareMW_jpeg_decode(ten,&bits_0,&BInfo_0.width,&BInfo_0.height);
                    if(bits_0 == NULL)
                    {
                        stb_printf("JPG decode error!\n");
                        return -1;
                    }
                    stb_printf("ten_w[%d],ten_h[%d]\n",BInfo_0.width,BInfo_0.height);
                    bits_1 = DareMW_jpeg_decode(uni,&bits_1,&BInfo_1.width,&BInfo_1.height);
                    if(bits_1 == NULL)
                    {
                        stb_printf("JPG decode error!\n");
                        return -1;
                    }
                    stb_printf("uni_w[%d],uni_h[%d]\n",BInfo_1.width,BInfo_1.height);
                    src_rect.left = PROGRESS_PERCENT_X;
                    src_rect.top = PROGRESS_PERCENT_Y;
                    src_rect.right = PROGRESS_PERCENT_X+BInfo_0.width+BInfo_1.width+BInfo_p.width;
                    src_rect.bottom = PROGRESS_PERCENT_Y+BInfo_p.height;
                    dst_rect = src_rect;
                    stb_printf("src_x[%d],src_y[%d],src_w[%d],src_h[%d]\n",src_rect.left,src_rect.top,src_rect.right,src_rect.bottom);

                    DG_AL_gdi_draw_image(PROGRESS_PERCENT_X, PROGRESS_PERCENT_Y, BInfo_0.width,BInfo_0.height, bits_0,BInfo_0.width,0);
                    DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_1.width,BInfo_1.height, bits_1,BInfo_1.width,0);
                    DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_1.width+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_p.width,BInfo_p.height, bits_p,BInfo_p.width,0);
                    DG_AL_gdi_update_osd(src_rect,dst_rect);


 
                    free(bits_0);bits_0 = NULL;
                    free(bits_1);bits_1 = NULL;
                    //stb_printf("=======percent[%d]==x[%d]=w[%d]=width[%d]=\n",percent,x,src_rect.right,src_rect.bottom);
                }
                else
                {
                    bits_0 = DareMW_jpeg_decode("upgrade/0.jpg",&bits_0,&BInfo_0.width,&BInfo_0.height);
                    if(bits == NULL)
                    {
                        stb_printf("JPG decode error!\n");
                        return -1;
                    }
                    stb_printf("ten_w[%d],ten_h[%d]\n",BInfo_0.width,BInfo_0.height);
                    bits_1 = DareMW_jpeg_decode("upgrade/1.jpg",&bits_1,&BInfo_1.width,&BInfo_1.height);
                    if(bits == NULL)
                    {
                        stb_printf("JPG decode error!\n");
                        return -1;
                    }
                    stb_printf("uni_w[%d],uni_h[%d]\n",BInfo_1.width,BInfo_1.height);
                    src_rect.left = PROGRESS_PERCENT_X;
                    src_rect.top = PROGRESS_PERCENT_Y;
                    src_rect.right = PROGRESS_PERCENT_X+BInfo_0.width+BInfo_0.width+BInfo_1.width+BInfo_p.width+40;
                    src_rect.bottom = PROGRESS_PERCENT_Y+BInfo_p.height;
                    dst_rect = src_rect;
                    stb_printf("src_x[%d],src_y[%d],src_w[%d],src_h[%d]\n",src_rect.left,src_rect.top,src_rect.right,src_rect.bottom);
                    DG_AL_gdi_draw_image(PROGRESS_PERCENT_X, PROGRESS_PERCENT_Y, BInfo_1.width,BInfo_1.height, bits_1,BInfo_1.width,0);
                    DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_0.width,BInfo_0.height, bits_0,BInfo_0.width,0);
                    DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_0.width+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_0.width,BInfo_0.height, bits_0,BInfo_0.width,0);
                    DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_1.width+BInfo_0.width+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_p.width,BInfo_p.height, bits_p,BInfo_p.width,0);
                    DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_1.width+BInfo_0.width+BInfo_0.width+BInfo_p.width,PROGRESS_PERCENT_Y , BInfo.width,BInfo.height, bits_tmp,BInfo.width,0);
                    DG_AL_gdi_update_osd(src_rect,dst_rect);
                    free(bits_0);bits_0 = NULL;
                    free(bits_1);bits_1 = NULL;
                }
                /*end draw percent*/
                x = PROGRESS_BAR_X;
                if(n>20)
                {
                    n=20;
                }
                for(i = 0;i < n;i++)
                {
   DG_AL_gdi_refurbish_output_screen(0);
   DG_AL_gdi_refurbish_output_screen(1);
                    DG_AL_gdi_draw_image(x, y, BInfo.width,BInfo.height, bits,BInfo.width,0);
                    x += BInfo.width;
                }
                src_rect.left = PROGRESS_BAR_X;
                src_rect.top = PROGRESS_BAR_Y;
                //if(x+BInfo.width > IPTV_EPG_WIDTH)
    if(x+BInfo.width > IPTV_SCR_WIDTH)  
                {
     //src_rect.right = IPTV_EPG_WIDTH;
                    src_rect.right = IPTV_SCR_WIDTH;
                }
                else
                {
                  //  src_rect.right = PROGRESS_BAR_X+BInfo.width * n;
                    src_rect.right = 1280;
                }
                //if(y+BInfo.height > IPTV_EPG_HEIGHT)
                if(y+BInfo.height > IPTV_SCR_HEIGHT)
                {
                   // src_rect.bottom = IPTV_EPG_HEIGHT;
                    src_rect.bottom = IPTV_SCR_HEIGHT;
                }
                else
                {
                    src_rect.bottom = y+BInfo.height;
                }
            }/*end download mode*/
            //else if(mode>=UPGRADE_COPY_CFE||mode<=UPGRADE_COPY_BOOTKEY)/*install mode*/
            else
            {
                //sleep(2);/*we draw once every one second*/
                x = PROGRESS_BAR_X;
                y = PROGRESS_BAR_Y;
                if(!flag)
                {
                    flag = 1;
                    /*clear download progress bar*/
                    for(i = 0;i<20;i++)
                    {
    DG_AL_gdi_refurbish_output_screen(0);
    DG_AL_gdi_refurbish_output_screen(1);
                      DG_AL_gdi_draw_image(x, y, BInfo.width,BInfo.height, (unsigned char*)bits_tmp,BInfo.width,0);
                        x += BInfo.width;
                    }
                    src_rect.left = PROGRESS_BAR_X;
                    src_rect.top = PROGRESS_BAR_Y;
                    //src_rect.right = IPTV_EPG_WIDTH;
                    src_rect.right = 1280;
                    if(y+BInfo.height > IPTV_EPG_HEIGHT)
                    {
                        src_rect.bottom = IPTV_EPG_HEIGHT;
                    }
                    else
                    {
                        src_rect.bottom = y+BInfo.height;
                    }
                    dst_rect = src_rect;
                   
                    DG_AL_gdi_update_osd(src_rect,dst_rect);
                    n = 1;
                    /*clear percent bar screen*/
                    x = PROGRESS_PERCENT_X;
                    for(i=0;i<5;i++)
                    {
    DG_AL_gdi_refurbish_output_screen(0);
    DG_AL_gdi_refurbish_output_screen(1);
    DG_AL_gdi_draw_image(x, PROGRESS_PERCENT_Y, BInfo.width,BInfo.height, (unsigned char*)bits_tmp,BInfo.width,0);
                        x += BInfo.width;
                    }
                    src_rect.left = PROGRESS_PERCENT_X;
                    src_rect.top = PROGRESS_PERCENT_Y;
                    src_rect.right = PROGRESS_PERCENT_X+BInfo.width*5;
                    if(PROGRESS_PERCENT_Y+BInfo.height > IPTV_EPG_HEIGHT)
                    {
                        src_rect.bottom = IPTV_EPG_HEIGHT;
                    }
                    else
                    {
                        src_rect.bottom = PROGRESS_PERCENT_Y+BInfo.height;
                    }
                    dst_rect = src_rect;
                    DG_AL_gdi_update_osd(src_rect,dst_rect);
                    //stb_printf("src_x[%d],src_y[%d],src_w[%d],src_h[%d]\n",src_rect.left,src_rect.top,src_rect.right,src_rect.bottom);
                }/*end clear download progress bar*/

    DG_AL_gdi_refurbish_output_screen(0);
    DG_AL_gdi_refurbish_output_screen(1);
                /*DRAW INSTALL PERCENT*/
                if(n<20)
                {
                 int temp;
     temp = n * 2;
     if(temp < 20)
     {
      tens = temp*5/10;
                     unit = temp*5%10;
                     char ten[20];
                     char uni[20];
                     sprintf(ten,"upgrade/%d.jpg",tens);
                     sprintf(uni,"upgrade/%d.jpg",unit);
                     stb_printf("ten[%s],uni[%s]\n",ten,uni);
                     bits_0 = DareMW_jpeg_decode(ten,&bits_0,&BInfo_0.width,&BInfo_0.height);
                     if(bits == NULL)
                     {
                         stb_printf("JPG decode error!\n");
                         return -1;
                     }
                     //stb_printf("ten_w[%d],ten_h[%d]\n",BInfo_0.width,BInfo_0.height);
                     bits_1 = DareMW_jpeg_decode(uni,&bits_1,&BInfo_1.width,&BInfo_1.height);
                     if(bits == NULL)
                     {
                         stb_printf("JPG decode error!\n");
                         return -1;
                     }
                     //stb_printf("uni_w[%d],uni_h[%d]\n",BInfo_1.width,BInfo_1.height);
                     src_rect.left = PROGRESS_PERCENT_X;
                     src_rect.top = PROGRESS_PERCENT_Y;
                     src_rect.right = PROGRESS_PERCENT_X+BInfo_0.width+BInfo_1.width+BInfo_p.width;
                     src_rect.bottom = PROGRESS_PERCENT_Y+BInfo_p.height;
                     dst_rect = src_rect;
                     stb_printf("src_x[%d],src_y[%d],src_w[%d],src_h[%d]\n",src_rect.left,src_rect.top,src_rect.right,src_rect.bottom);
                     DG_AL_gdi_draw_image(PROGRESS_PERCENT_X, PROGRESS_PERCENT_Y, BInfo_0.width,BInfo_0.height, bits_0,BInfo_0.width,0);
                     DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_1.width,BInfo_1.height, bits_1,BInfo_1.width,0);
                     DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_1.width+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_p.width,BInfo_p.height, bits_p,BInfo_p.width,0);
                     DG_AL_gdi_update_osd(src_rect,dst_rect);
                     if(bits_0)
                     {
                         free(bits_0);
                         bits_0 = NULL;
                     }
                     if(bits_1)
                     {
                         free(bits_1);
                         bits_1 = NULL;
                     }
     }
     else if(temp == 20)
     {
      bits_0 = DareMW_jpeg_decode("upgrade/0.jpg",&bits_0,&BInfo_0.width,&BInfo_0.height);
                     if(bits == NULL)
                     {
                         stb_printf("JPG decode error!\n");
                         return -1;
                     }
                     stb_printf("ten_w[%d],ten_h[%d]\n",BInfo_0.width,BInfo_0.height);
                     bits_1 = DareMW_jpeg_decode("upgrade/1.jpg",&bits_1,&BInfo_1.width,&BInfo_1.height);
                     if(bits == NULL)
                     {
                         stb_printf("JPG decode error!\n");
                         return -1;
                     }
                     stb_printf("uni_w[%d],uni_h[%d]\n",BInfo_1.width,BInfo_1.height);
                     src_rect.left = PROGRESS_PERCENT_X;
                     src_rect.top = PROGRESS_PERCENT_Y;
                     src_rect.right = PROGRESS_PERCENT_X+BInfo_0.width+BInfo_0.width+BInfo_1.width+BInfo_p.width+40;
                     src_rect.bottom = PROGRESS_PERCENT_Y+BInfo_p.height;
                     dst_rect = src_rect;
                     stb_printf("src_x[%d],src_y[%d],src_w[%d],src_h[%d]\n",src_rect.left,src_rect.top,src_rect.right,src_rect.bottom);
                     DG_AL_gdi_draw_image(PROGRESS_PERCENT_X, PROGRESS_PERCENT_Y, BInfo_1.width,BInfo_1.height, bits_1,BInfo_1.width,0);
                     DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_0.width,BInfo_0.height, bits_0,BInfo_0.width,0);
                     DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_0.width+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_0.width,BInfo_0.height, bits_0,BInfo_0.width,0);
                     DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_1.width+BInfo_0.width+BInfo_0.width, PROGRESS_PERCENT_Y, BInfo_p.width,BInfo_p.height, bits_p,BInfo_p.width,0);
                     //DG_AL_gdi_draw_image(PROGRESS_PERCENT_X+BInfo_1.width+BInfo_0.width+BInfo_0.width+BInfo_p.width,PROGRESS_PERCENT_Y , BInfo.width,BInfo.height, bits_tmp,BInfo.width,0);
                     DG_AL_gdi_update_osd(src_rect,dst_rect);
                     if(bits_0)
                     {
                         free(bits_0);
                         bits_0 = NULL;
                     }
                     if(bits_1)
                     {
                         free(bits_1);
                         bits_1 = NULL;
                     }
                     if(bits_p)
                     {
                         free(bits_p);
                         bits_p = NULL;/*do not need any more*/
                     }
     }
     else
     {
     }
                }/*end draw install bar*/

                x = PROGRESS_BAR_X;
                for(i=0;i<n;i++)
                {
                 DG_AL_gdi_refurbish_output_screen(0);
   DG_AL_gdi_refurbish_output_screen(1);
                    /*draw install progress bar*/
                    DG_AL_gdi_draw_image(x, y, BInfo.width,BInfo.height, bits,BInfo.width,0);
                    x += BInfo.width;
     DG_AL_gdi_draw_image(x, y, BInfo.width,BInfo.height, bits,BInfo.width,0);
                    x += BInfo.width;
                }
                n++;/*every time install progress bar plus 1,ai ,it's fake*/
                src_rect.left = PROGRESS_BAR_X;
                src_rect.top = PROGRESS_BAR_Y;
                //src_rect.right = PROGRESS_BAR_X+BInfo.width*(n+1);
    src_rect.right = 1280;
    
               // if(x+src_rect.right > IPTV_EPG_WIDTH)
                if(x+src_rect.right > IPTV_SCR_WIDTH)
                {
                    //src_rect.right = IPTV_EPG_WIDTH;
     src_rect.right = IPTV_SCR_WIDTH;
                }
               // if(y+BInfo.height > IPTV_EPG_HEIGHT)
    if(y+BInfo.height > IPTV_SCR_HEIGHT)  
                {
                    //src_rect.bottom = IPTV_EPG_HEIGHT;
     src_rect.bottom = IPTV_SCR_HEIGHT;
                }
                else
                {
                    src_rect.bottom = y+BInfo.height;
                }
            }/*end install mode*/
           
            dst_rect = src_rect;
            DG_AL_gdi_update_osd(src_rect,dst_rect);/*draw install bar*/
            sleep(1);/*we draw once every two second*/

            percent_tmp = n;
            if(n > 20)
            {
                DareMW_gfx_decode_release(bits,1);
                if(bits_tmp != NULL)
                {
                    free(bits_tmp);
                    bits_tmp = NULL;
                }
                //sleep(1);/*before have sleeped 2s*/
                if(upgradepara.update_flag_bootkeypartition)
                {
                 /*升级引导分区*/
                 DareSTB_system("osflagsutil "UPGRADE_LOCAL_bootkey_FILE,-1);
                    osflagflag = 1;
                 mode = UPGRADE_COPY_BOOTKEY;
                 //percent = stb_upgrade_progress_count(cfe_size, 2,1);
                }
                else
                {
                    DareSTB_system("osflagsutil ./osflags.xml",-1);
                    osflagflag = 1;
                }
                stb_upgrade_complete_reboot(1);
            }
        }
        /*zen me zhe me luan*/
        if(bits != NULL)
        {
            free(bits);
        }
        if(bits_p != NULL)
        {
            free(bits_p);
        }
        if(bits_tmp != NULL)
        {
            free(bits_tmp);
        }
        if(bits_0 != NULL)
        {
            free(bits_0);
        }
        if(bits_1 != NULL)
        {
            free(bits_1);
        }
        DareMW_gfx_cleanall(); 
        return 0;
}

原文地址:https://www.cnblogs.com/fx2008/p/2163841.html