|
|
@@ -178,7 +178,7 @@ static int file_touch(int id)
|
|
|
FILE *fp;
|
|
|
char tmp[200];
|
|
|
|
|
|
- snprintf(tmp, sizeof(tmp), "%s/step%d", UPG_FOLDER, id);
|
|
|
+ snprintf(tmp, sizeof(tmp), "%s/s%d", UPG_FOLDER, id);
|
|
|
fp = fopen(tmp, "w"); // 使用写入模式创建一个新文件
|
|
|
if (fp == NULL) {
|
|
|
return -1;
|
|
|
@@ -190,7 +190,7 @@ static int file_untouch(int id)
|
|
|
{
|
|
|
char tmp[200];
|
|
|
|
|
|
- snprintf(tmp, sizeof(tmp), "%s/step%d", UPG_FOLDER, id);
|
|
|
+ snprintf(tmp, sizeof(tmp), "%s/s%d", UPG_FOLDER, id);
|
|
|
return remove(tmp);
|
|
|
|
|
|
}
|
|
|
@@ -214,7 +214,7 @@ static int folder_init(void)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int file_save(char *path, char *buf, int len)
|
|
|
+static int file_save(char *path, void *buf, int len)
|
|
|
{
|
|
|
int r;
|
|
|
FILE *fp;
|
|
|
@@ -236,7 +236,7 @@ static int file_save(char *path, char *buf, int len)
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
-static int file_load(char* path, char* buf, int len)
|
|
|
+static int file_load(char* path, void* buf, int len)
|
|
|
{
|
|
|
int r;
|
|
|
FILE* fp;
|
|
|
@@ -266,15 +266,15 @@ static int file_remove(char *path)
|
|
|
/*
|
|
|
tar pdutar to run/
|
|
|
*/
|
|
|
-static int file_cover(char *srcDir)
|
|
|
+static int file_cover(char *path)
|
|
|
{
|
|
|
int r;
|
|
|
char cmd[200];
|
|
|
|
|
|
- snprintf(cmd, sizeof(cmd), "tar -xpf %s/ /", srcDir, UPG_TAR_NAME);
|
|
|
+ snprintf(cmd, sizeof(cmd), "tar -xpf %s -C / > /dev/null 2>&1", path);
|
|
|
r = system(cmd);
|
|
|
if(r) {
|
|
|
- LOG("% failed, %d\n", cmd, r);
|
|
|
+ LOG("%s failed\n", cmd);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
@@ -306,7 +306,7 @@ static int dir_is_empty(char *path)
|
|
|
}
|
|
|
|
|
|
//when app run success, copy run/* to bak/
|
|
|
-static int file_bakup(void)
|
|
|
+static int file_backup(void)
|
|
|
{
|
|
|
int r;
|
|
|
char tmp[100];
|
|
|
@@ -367,29 +367,6 @@ static int file_restore(void)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static int rebooot(void)
|
|
|
-{
|
|
|
- int r;
|
|
|
- char cmd[100];
|
|
|
-
|
|
|
- snprintf(cmd, sizeof(cmd), "killall -e smartPDU");
|
|
|
- r = system(cmd);
|
|
|
- if(r) {
|
|
|
- LOG("% failed, %d\n", cmd, r);
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-static int app_start(void)
|
|
|
-{
|
|
|
- return system("/etc/init.d/S90PDUApp start");
|
|
|
-}
|
|
|
-static int app_stop(void)
|
|
|
-{
|
|
|
- return system("/etc/init.d/S90PDUApp stop");
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
#define CHAR(b) (((b)<=9)?((b)+0x30):((b)-10+0x61));
|
|
|
static void byte2char(char *chr, uint8_t b)
|
|
|
@@ -409,20 +386,20 @@ static void print_md5(char *s, md5_t *m)
|
|
|
}
|
|
|
|
|
|
//md5相同返回0,否则返回1
|
|
|
-static int md5_check(char *buf, int len)
|
|
|
+static int md5_check(upgrade_file_t *h)
|
|
|
{
|
|
|
md5_ctx_t ctx;
|
|
|
uint8_t i,tmp[16];
|
|
|
md5_t m1,m2;
|
|
|
|
|
|
md5_init(&ctx);
|
|
|
- md5_update(&ctx, buf, len);
|
|
|
+ md5_update(&ctx, (char*)h, h->dlen+sizeof(upgrade_file_t));
|
|
|
md5_final(&ctx, tmp);
|
|
|
|
|
|
for (i=0; i<16; i++) {
|
|
|
byte2char(&m2.data[i*2], tmp[i]);
|
|
|
}
|
|
|
- memcpy(&m1, buf+len-sizeof(m1), sizeof(m1));
|
|
|
+ memcpy(&m1, h->data+h->dlen, sizeof(md5_t));
|
|
|
|
|
|
print_md5("ori md5: ", &m1);
|
|
|
print_md5("cal md5: ", &m2);
|
|
|
@@ -456,16 +433,17 @@ static int file_split(void)
|
|
|
LOG("%s load failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
+ upgrade_file_t *hfile = (upgrade_file_t*)fbuf;
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
|
|
|
- r = file_save(path, fbuf, sizeof(upgrade_file_t));
|
|
|
+ r = file_save(path, hfile, sizeof(upgrade_file_t));
|
|
|
if(r) {
|
|
|
LOG("file_save %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
|
|
|
- r = file_save(path, fbuf+sizeof(upgrade_file_t), flen-sizeof(upgrade_file_t)-sizeof(md5_t));
|
|
|
+ r = file_save(path, hfile->data, hfile->dlen);
|
|
|
if(r) {
|
|
|
LOG("file_save %s failed\n", path);
|
|
|
return -1;
|
|
|
@@ -477,6 +455,7 @@ static int file_split(void)
|
|
|
LOG("file_remove %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
+ system("ls /root/dl");
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -494,27 +473,22 @@ static int file_check(void)
|
|
|
LOG("file_length %s failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
+
|
|
|
fbuf = (char*)malloc(flen);
|
|
|
if(!fbuf) {
|
|
|
LOG("malloc %d failed\n", flen);
|
|
|
return -1;
|
|
|
}
|
|
|
+
|
|
|
r = file_load(path, fbuf, flen);
|
|
|
if(r) {
|
|
|
LOG("%s load failed\n", path);
|
|
|
return -1;
|
|
|
}
|
|
|
-
|
|
|
- r = md5_check(fbuf, flen);
|
|
|
- if(r) {
|
|
|
- LOG("md5 check failed\n");
|
|
|
- goto fail;
|
|
|
- }
|
|
|
|
|
|
hfile = (upgrade_file_t*)fbuf;
|
|
|
if(hfile->magic!=UPG_FILE_MAGIC ||
|
|
|
- hfile->obj.num>=UPGRADE_NUM_MAX ||
|
|
|
- hfile->obj.stra>=UPGRADE_STRA_MAX) {
|
|
|
+ hfile->dlen!=flen-sizeof(upgrade_file_t)-sizeof(md5_t)) {
|
|
|
r = -1;
|
|
|
LOG("magic: 0x%08x, should be: 0x%08x\n", hfile->magic, UPG_FILE_MAGIC);
|
|
|
LOG("num: %d, should < %d\n", hfile->obj.num, UPGRADE_NUM_MAX);
|
|
|
@@ -523,16 +497,42 @@ static int file_check(void)
|
|
|
LOG("file hdr check failed\n");
|
|
|
goto fail;
|
|
|
}
|
|
|
- free(fbuf);
|
|
|
-
|
|
|
- return 0;
|
|
|
+
|
|
|
+ r = md5_check(hfile);
|
|
|
+ if(r) {
|
|
|
+ LOG("md5 check failed\n");
|
|
|
+ goto fail;
|
|
|
+ }
|
|
|
|
|
|
fail:
|
|
|
free(fbuf);
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+static int file_scan(upg_handle_t *h)
|
|
|
+{
|
|
|
+ //检查DOWN目录,如有升级文件
|
|
|
+
|
|
|
+ //udisk检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
|
|
|
+
|
|
|
+ //tcard检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+//////////////////////////////////////////////////
|
|
|
+static int app_start(void)
|
|
|
+{
|
|
|
+ return system("/etc/init.d/S90PDUApp start");
|
|
|
+}
|
|
|
+static int app_stop(void)
|
|
|
+{
|
|
|
+ return system("/etc/init.d/S90PDUApp stop");
|
|
|
+}
|
|
|
+static int app_restart(void)
|
|
|
+{
|
|
|
+ return system("/etc/init.d/S90PDUApp restart");
|
|
|
+}
|
|
|
static int set_factory(void)
|
|
|
{
|
|
|
int r=0;
|
|
|
@@ -549,63 +549,12 @@ static int set_factory(void)
|
|
|
LOG("sys_set_factory failed!\n");
|
|
|
return -1;
|
|
|
}
|
|
|
- system("reboot -f");
|
|
|
+ app_restart();
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
-static int upg_init(upg_handle_t *h)
|
|
|
-{
|
|
|
- int id,r;
|
|
|
- char tmp[200];
|
|
|
- key_t key;
|
|
|
-
|
|
|
- folder_init();
|
|
|
- file_bakup();
|
|
|
-
|
|
|
- key = ftok(UPG_KEY_PATH, UPG_KEY_SN);
|
|
|
- if(key<0) {
|
|
|
- LOG("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
|
|
|
- }
|
|
|
- h->src = UPGRADE_SRC_NONE;
|
|
|
- h->stat = UPGRADE_STAT_NONE;
|
|
|
- h->key = key;
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-static int upg_check(upg_handle_t *h)
|
|
|
-{
|
|
|
- int i,r;
|
|
|
- char tmp[200];
|
|
|
-
|
|
|
- for(i=1; i<UPGRADE_STEP_MAX; i++) {
|
|
|
- snprintf(tmp, sizeof(tmp), "%s/step%d", UPG_FOLDER, i);
|
|
|
- r = file_exist(tmp);
|
|
|
- if(r) {
|
|
|
- LOG("%s exist\n", tmp);
|
|
|
- //重启后如果是覆盖完成,则将进度置为SUCCESS
|
|
|
- h->step = (i==UPGRADE_STEP_COVER)?UPGRADE_STEP_SUCCESS:i;
|
|
|
- break;
|
|
|
- }
|
|
|
- LOG("%s not exist\n", tmp);
|
|
|
- }
|
|
|
-
|
|
|
- return r;
|
|
|
-}
|
|
|
-
|
|
|
-static int file_scan(upg_handle_t *h)
|
|
|
-{
|
|
|
- //DOWN_FOLDER检查,如有升级文件,则xxxx
|
|
|
-
|
|
|
- //udisk检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
|
|
|
-
|
|
|
- //tcard检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
static void* upg_thread(void *arg)
|
|
|
{
|
|
|
int r=0;
|
|
|
@@ -649,6 +598,43 @@ static void* upg_thread(void *arg)
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
}
|
|
|
+static int upg_init(upg_handle_t *h)
|
|
|
+{
|
|
|
+ int id,r;
|
|
|
+ char tmp[200];
|
|
|
+ key_t key;
|
|
|
+
|
|
|
+ folder_init();
|
|
|
+
|
|
|
+ key = ftok(UPG_KEY_PATH, UPG_KEY_SN);
|
|
|
+ if(key<0) {
|
|
|
+ LOG("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
|
|
|
+ }
|
|
|
+ h->src = UPGRADE_SRC_NONE;
|
|
|
+ h->stat = UPGRADE_STAT_NONE;
|
|
|
+ h->key = key;
|
|
|
+
|
|
|
+ return pthread_create(&h->tid, NULL, upg_thread, h);
|
|
|
+}
|
|
|
+static int upg_check(upg_handle_t *h)
|
|
|
+{
|
|
|
+ int i,r;
|
|
|
+ char tmp[200];
|
|
|
+
|
|
|
+ for(i=1; i<UPGRADE_STEP_MAX; i++) {
|
|
|
+ snprintf(tmp, sizeof(tmp), "%s/s%d", UPG_FOLDER, i);
|
|
|
+ r = file_exist(tmp);
|
|
|
+ if(r) {
|
|
|
+ LOG("%s exist\n", tmp);
|
|
|
+ //重启后如果是覆盖完成,则将进度置为SUCCESS
|
|
|
+ h->step = (i==UPGRADE_STEP_COVER)?UPGRADE_STEP_SUCCESS:i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ LOG("%s not exist\n", tmp);
|
|
|
+ }
|
|
|
+
|
|
|
+ return r;
|
|
|
+}
|
|
|
static int upg_handle(upg_handle_t *h)
|
|
|
{
|
|
|
int r=-1;
|
|
|
@@ -669,6 +655,11 @@ static int upg_handle(upg_handle_t *h)
|
|
|
file_untouch(UPGRADE_STEP_DOWN);
|
|
|
h->step = UPGRADE_STEP_SPLIT;
|
|
|
}
|
|
|
+ else {
|
|
|
+ LOG("remove %s\n", tmp);
|
|
|
+ file_remove(tmp);
|
|
|
+ h->step = UPGRADE_STEP_NONE;
|
|
|
+ }
|
|
|
}
|
|
|
else {
|
|
|
LOG("___ %s not exist, ", tmp);
|
|
|
@@ -692,65 +683,69 @@ static int upg_handle(upg_handle_t *h)
|
|
|
{
|
|
|
r = file_split();
|
|
|
if(r==0) {
|
|
|
+ file_touch(UPGRADE_STEP_SPLIT);
|
|
|
h->step = UPGRADE_STEP_COVER;
|
|
|
}
|
|
|
else {
|
|
|
- snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
|
|
|
- LOG("file_split failed, remove %s\n", tmp);
|
|
|
-
|
|
|
- file_untouch(UPGRADE_STEP_DOWN);
|
|
|
- file_remove(tmp);
|
|
|
h->step = UPGRADE_STEP_NONE;
|
|
|
}
|
|
|
+ file_untouch(UPGRADE_STEP_DOWN);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case UPGRADE_STEP_COVER:
|
|
|
{
|
|
|
LOG("___ UPGRADE_STEP_COVER\n");
|
|
|
- r = app_stop();
|
|
|
- if(r) {
|
|
|
- LOG("app_stop failed\n");
|
|
|
- break;
|
|
|
- }
|
|
|
- file_untouch(UPGRADE_STEP_SPLIT);
|
|
|
-
|
|
|
- r = file_cover(DOWN_FOLDER);
|
|
|
+ app_stop();
|
|
|
+
|
|
|
+ snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
|
|
|
+ r = file_cover(tmp);
|
|
|
if(r==0) {
|
|
|
file_touch(UPGRADE_STEP_COVER);
|
|
|
- h->step = UPGRADE_STEP_SUCCESS;
|
|
|
+ h->step = UPGRADE_STEP_NONE;
|
|
|
+ app_start();
|
|
|
}
|
|
|
else {
|
|
|
- LOG("file_cover failed, need restore!\n");
|
|
|
+ LOG("tar %s failed, delete it!\n", tmp);
|
|
|
+ file_remove(tmp);
|
|
|
+ snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
|
|
|
+ file_remove(tmp);
|
|
|
+
|
|
|
h->step = UPGRADE_STEP_RESTORE;
|
|
|
}
|
|
|
+ file_untouch(UPGRADE_STEP_DOWN);
|
|
|
+ file_untouch(UPGRADE_STEP_SPLIT);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case UPGRADE_STEP_RESTORE:
|
|
|
{
|
|
|
LOG("restore now ...\n");
|
|
|
+ file_untouch(UPGRADE_STEP_COVER);
|
|
|
+
|
|
|
app_stop();
|
|
|
r = file_restore();
|
|
|
- h->step = UPGRADE_STEP_SUCCESS;
|
|
|
+ h->step = UPGRADE_STEP_NONE; //设置为NONE,防止其反复进入
|
|
|
if(r==0) {
|
|
|
LOG("file_restore ok!\n");
|
|
|
}
|
|
|
else {
|
|
|
LOG("file_restore failed\n");
|
|
|
}
|
|
|
+ app_start();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case UPGRADE_STEP_SUCCESS:
|
|
|
{
|
|
|
- app_start();
|
|
|
- if(r) {
|
|
|
- LOG("app_start failed\n");
|
|
|
- break;
|
|
|
+ LOG("___ UPGRADE_STEP_SUCCESS\n");
|
|
|
+ r = file_backup(); //启动成功才证明程序有效,可进行备份
|
|
|
+ if(r==0) { //备份成功,删除下载目录中的文件以节省空间
|
|
|
+ snprintf(tmp, sizeof(tmp), "rm -r %s/*", DOWN_FOLDER);
|
|
|
+ system(tmp);
|
|
|
}
|
|
|
|
|
|
- LOG("___ UPGRADE_STEP_SUCCESS\n");
|
|
|
+ app_start();
|
|
|
file_untouch(UPGRADE_STEP_COVER);
|
|
|
h->step = UPGRADE_STEP_NONE;
|
|
|
}
|
|
|
@@ -765,26 +760,18 @@ static int upg_handle(upg_handle_t *h)
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+/////////////////////////////////////////////////////////////////////////
|
|
|
int main(void)
|
|
|
{
|
|
|
int id,r=0;
|
|
|
upg_handle_t *h=&upgHandle;
|
|
|
|
|
|
LOG("___ upgd running ...\n");
|
|
|
+
|
|
|
upg_init(h);
|
|
|
upg_check(h);
|
|
|
- LOG("___ upgd loop ... \n");
|
|
|
-
|
|
|
- pthread_create(&h->tid, NULL, upg_thread, h);
|
|
|
- LOG("___ upgd loop 222 \n");
|
|
|
-
|
|
|
-
|
|
|
while(1) {
|
|
|
upg_handle(h);
|
|
|
-
|
|
|
sleep(1);
|
|
|
}
|
|
|
|