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);
}
