当前位置: 首页 > news >正文

Freertos系统,将U盘里的updata.zip解压到当前的U盘,然后进行升级

将U盘里的updata.zip解压到当前的U盘(U盘路径:E:/updata.zip);

进行升级,升级成功打印:upgrade success!

读者不要借鉴升级步骤,方案不同,升级步骤不同;

#include <sys/ioctl.h>
#include "ite/ug.h"
#include "soar_upgrade.h"
#include "fat/api/api_fat.h"
#include "../../share/zlib/contrib/minizip/unzip.h"#define SOAR_EXTRACT_PATH  "E:/"
#define SOAR_UNZIP_PATH    "E:/updata.zip"static char upgradeUrl[256];
static char pkgFilePath[256];
static ITCFileStream fileStream;/*create dir path*/
void create_directory_tree(const char *path)
{char dir_path[512];strcpy(dir_path, path);/*find*/char *last_slash = strrchr(dir_path, '/');if(last_slash != NULL){*last_slash = '\0';/*create dir path*/if(strlen(dir_path) > 0){f_mkdir(dir_path);}}
}/*extract_current_file*/
void extract_current_file(unzFile zipfile, const char *output_path, unz_file_info *file_info)
{FILE *outfile = fopen(output_path, "wb");if(outfile == NULL){printf("output file cannot be created: %s\n", output_path);return;}/*Read and write file data*/ char buffer[4096];int bytes_read;while((bytes_read = unzReadCurrentFile(zipfile, buffer, sizeof(buffer))) > 0){fwrite(buffer, 1, bytes_read, outfile);}fclose(outfile);
}static ITCStream* OpenUsbPackage(char* path, bool file_flag)
{char *str = "updata/";ITPDriveStatus* driveStatusTable;ITPDriveStatus* driveStatus = NULL;int i;// try to find the package driveioctl(ITP_DEVICE_DRIVE, ITP_IOCTL_GET_TABLE, &driveStatusTable);for (i = ITP_MAX_DRIVE - 1; i >= 0; i--){driveStatus = &driveStatusTable[i];if (driveStatus->avail && driveStatus->disk >= ITP_DISK_MSC00 && driveStatus->disk <= ITP_DISK_MSC17){char buf[PATH_MAX], *ptr, *saveptr;// get file path from list(void)strlcpy(buf, path, sizeof(buf));ptr = strtok_r(buf, " ", &saveptr);do{if(false == file_flag){printf("mount %c: name=%s\n", 'A' + i, driveStatus->name);(void)strlcpy(pkgFilePath, driveStatus->name, sizeof(pkgFilePath));(void)strlcat(pkgFilePath, ptr, sizeof(pkgFilePath));}else{(void)strlcpy(pkgFilePath, driveStatus->name, sizeof(pkgFilePath));(void)strlcat(pkgFilePath, str, sizeof(pkgFilePath));(void)strlcat(pkgFilePath, ptr, sizeof(pkgFilePath));(void)printf("file %s\n", pkgFilePath);}if (itcFileStreamOpen(&fileStream, pkgFilePath, false) == 0){(void)printf("found package file %s\n", pkgFilePath);return &fileStream.stream;}else{//(void)printf("try to fopen(%s) fail:0x%X\n", pkgFilePath, errno);}}while ((ptr = strtok_r(NULL, " ", &saveptr)) != NULL);}}(void)printf("cannot find package file.\n");return NULL;
}static int UpgradePackage(bool file_flag)
{int ret = 0;ITCStream* fwStream = NULL;#ifdef CFG_A_B_DUAL_BOOT_ENABLE//Read boot args to update the next booting flaguint8_t boot_args = 0x0U;if(ugBootargsRead(&boot_args)){ret = -1;(void)printf("Upgrade read boot args failed.\n");return ret;}
#endifif (upgradeUrl[0] == '\0'){// open from USB drivefwStream = OpenUsbPackage(CFG_UPGRADE_FILENAME_LIST,file_flag);if (!fwStream){ret = -1;(void)printf("packages unavailable: %s\n", CFG_UPGRADE_FILENAME_LIST);return ret;}}
#ifdef CFG_NET_ENABLEelse{// download from ftp serverfwStream = OpenFtpPackage(upgradeUrl);if (!fwStream){ret = -1;(void)printf("remote package unavailable: %s\n", upgradeUrl);return ret;}}
#endif // CFG_NET_ENABLEret = ugCheckCrc(fwStream, NULL);if (ret){(void)printf("check crc fail: %d.\n", ret);return ret;}ret = ugUpgradePackage(fwStream);if (ret){(void)printf("upgrade fail: %d.\n", ret);return ret;}#ifdef CFG_UPGRADE_DELETE_PKGFILE_AFTER_FINISHif (upgradeUrl[0] == '\0'){remove(pkgFilePath);}
#endif
#ifdef CFG_A_B_DUAL_BOOT_ENABLE            if(boot_args == 0xAAU){if(ugBootargsWrite(0xBBU)){(void)printf("Upgrade write boot args failed.\n");}else{(void)printf("Next time will boot from sub-image.\n");}}else{if(ugBootargsWrite(0xAAU)){(void)printf("Upgrade write boot args failed.\n");}else{(void)printf("Next time will boot from main-image.\n");}}
#endif    (void)printf("upgrade success!\n");#if defined(CFG_UPGRADE_DELAY_AFTER_FINISH) && CFG_UPGRADE_DELAY_AFTER_FINISH > 0sleep(CFG_UPGRADE_DELAY_AFTER_FINISH);
#endifreturn 0;
}void upgrade_start()
{bool file_flag = false;unzFile zipFile = unzOpen(SOAR_UNZIP_PATH);if(NULL != zipFile){unzGoToFirstFile(zipFile);char filename[256];do {unz_file_info fileInfo;unzGetCurrentFileInfo(zipFile, &fileInfo, filename, sizeof(filename), NULL, 0, NULL, 0);printf("exist file, func:%s,line:%d\n",__func__, __LINE__);if(0 == strcmp("updata/ITEPKG03.PKG", filename)){file_flag = true;}if(unzOpenCurrentFile(zipFile) == UNZ_OK){char full_path[512];snprintf(full_path, sizeof(full_path), "%s%s", SOAR_EXTRACT_PATH, filename);// 创建目录结构(如果需要)create_directory_tree(full_path);// 解压文件内容extract_current_file(zipFile, full_path, &fileInfo);}// Read and write file contentunzCloseCurrentFile(zipFile);} while (unzGoToNextFile(zipFile) == UNZ_OK);}else{printf("zipFile is null!\n");}UpgradePackage(file_flag);	
}void* usb_upgrade_pthread(void* arg)
{while(true){sleep(10);upgrade_start();printf("func:%s,line:%d\n",__func__, __LINE__);}return NULL;
}
/*将U盘里的updata.zip重新拷贝一份到U盘*/
void vFileCopy_test()
{int ret = 0;int retW = 0;FILE  *src_file, *dst_file;// 打开U盘中的源文件src_file = fopen("E:/updata.zip", "r");if(src_file == NULL) {// 文件打开失败printf("********line:%d******\n",__LINE__);return;}// 在根目录创建目标文件dst_file = fopen("E:/updata1.zip", "wb");if(dst_file == NULL) {printf("********line:%d******\n",__LINE__);return;}// 逐块拷贝文件内容char buffer[1024];do {ret = fread(buffer, sizeof(char), sizeof(buffer), src_file);printf("********ret:%d******\n",ret);if(ret == 0) break;retW = fwrite(buffer, sizeof(char), ret, dst_file);if(ret != retW) {printf("********retW:%d******\n",retW);}} while(ret > 0);// 关闭文件和卸载文件系统fclose(src_file);fclose(dst_file);
}

http://www.dtcms.com/a/569784.html

相关文章:

  • 网站功能模块报价wordpress 图库主题
  • LIFT:基于低秩引导的稀疏微调
  • 贸易公司怎么做网站比较好凡科互动投票破解
  • 数据迁移工具之 DataX + DataX-Web(windows)
  • 帝国cms微信小程序 微信授权登录api接口
  • 地产公司做网站维护写代码么品牌网站建设最佳大蝌蚪
  • 工信部网站备案文件手机app免费下载
  • 南京专业网站设计公司价格佛山网站快速排名提升
  • css第一天
  • jquery 网站框架网站管理员在哪里
  • 网站seo在线诊断wordpress调用留言板
  • 【软考架构】案例分析-比较两种架构风格:面向对象风格和解释器风格。
  • uemo网站源码购物类网站建设
  • ymi 和 WowPacketParser 使用教程
  • 影刀RPA分析抖店用户消费行为,AI智能洞察,精准营销效果提升300%![特殊字符]
  • Oracle ADG ,DGBroker管理,异常断电重启主备库的状态
  • 推荐一下做年会视频的网站网址导航网址大全
  • 外贸仿牌AB轮询收款系统
  • 做网站是先做界面还是先做后台国内手机怎么上google浏览器
  • C++实现冒泡排序
  • redis实战day03(消息队列)
  • 网站优化怎么学怀化网站网站建设
  • 【Pandas】pandas Index objects PeriodIndex day_of_week
  • Flink Table API SQL 概念、常用 API 与工程落地
  • rag-mcp
  • 建筑人才网站关于建网站新闻
  • 【设计模式】UML和设计原则
  • 东莞网站开发找谁建筑网站建设公司
  • 2025进博会4310家展商名录
  • 手机软件开发网站个人网站名称有哪些