blob: 23f85f505004ac34f4931b7405bbbddcd61ce429 [file] [log] [blame]
// 2017-07-31 carota Rock FOTA ua porting file
#define ROCK_FOTA_SUPPORT
#ifdef ROCK_FOTA_SUPPORT
#include "iot_rock.h"
#include "iot_rock_ipl.h"
#include "sha.h"
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <hardware/hardware.h>
//#define off64_t __off64_t
#include <hardware/boot_control.h>
#include <hardware/hardware.h>
#include <sys/reboot.h>
#include "liblog/lynq_deflog.h"
#include "mtk_device_wrap.h"
#define ROCK_DEFAULT_BLOCK_SIZE 0x20000
#define ROCK_RAM_LEN (1024*1024)
#define ROCK_BACKUP_LEN ROCK_DEFAULT_BLOCK_SIZE
#define DEV_SYSTEM_A "/dev/disk/by-partlabel/system_a"
#define DEV_SYSTEM_B "/dev/disk/by-partlabel/system_b"
#define DEV_BOOT_A "/dev/disk/by-partlabel/boot_a"
#define DEV_BOOT_B "/dev/disk/by-partlabel/boot_b"
#define DEV_MD1IMG_A "/dev/disk/by-partlabel/md1img_a"
#define DEV_MD1IMG_B "/dev/disk/by-partlabel/md1img_b"
#define DEV_TEE_A "/dev/disk/by-partlabel/tee_a"
#define DEV_TEE_B "/dev/disk/by-partlabel/tee_b"
#define DEV_VBMETA_A "/dev/disk/by-partlabel/vbmeta_a"
#define DEV_VBMETA_B "/dev/disk/by-partlabel/vbmeta_b"
#define DEV_BL2 "/dev/disk/by-partlabel/bl2"
#define DEV_BL33 "/dev/disk/by-partlabel/bl33"
#define DEV_MISC "/dev/disk/by-partlabel/misc"
//#define DEV_DELTA "/dev/disk/by-partlabel/delta"
#define DEV_DELTA "/dev/disk/by-partlabel/delta"
#define FILE_UPDATE_STATE "/data/.update_status"
#define FILE_FOTA_STATE "/data/.fota_status"
#define NAND_PAGE_SIZE 2048
#define BOOTDEV_TYPE_NAND 1
#define BACKUP_ADDR_FLAG 0xffffffff
#define FILE_BACKUP "/data/.backup"
#define SLOT_A 0
#define SLOT_B 1
#define FULL_HEAD "full-ota"
int fd_system_a,fd_system_b,fd_boot_a,fd_boot_b,fd_tee_a,fd_tee_b,fd_bl2,fd_bl33,fd_delta,fd_curr,fd_log,fd_update_status,fd_md1img_a,fd_md1img_b,fd_fota_status,fd_vbmeta_a,fd_vbmeta_b;
int fd_write,fd_read;
static unsigned int delta_offset = 0;
static unsigned int now_patch = 0;
unsigned int current_slot = 0;
unsigned char rock_debug_buffer[512];
DELTA_HEAD da_head;
OTA_STATUS fota_status;
extern const hw_module_t HAL_MODULE_INFO_SYM;
boot_control_module_t* module;
int rock_mismatch(void* ctx, unsigned char* buf, unsigned int start, unsigned int size,
unsigned int source_hash,unsigned int target_hash) {
return 0;
}
int rock_fatal(void* ctx, int error_code) {
return 0;
}
void rock_trace(void* ctx, const char* fmt, ...) {
va_list ap;
memset(rock_debug_buffer,0x0,sizeof(rock_debug_buffer));
va_start (ap, fmt);
vsnprintf(rock_debug_buffer,sizeof(rock_debug_buffer),fmt,ap);
LYDBGLOG("+[UA]: %s",rock_debug_buffer);
va_end (ap);
}
static int save_fota_status()
{
int err;
fd_fota_status = open(FILE_FOTA_STATE,O_RDWR | O_CREAT,0777);
//fd_update_status = open(FILE_UPDATE_STATE,O_RDWR);
if (fd_fota_status < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
write(fd_fota_status, &fota_status,sizeof(fota_status));
sync();
close(fd_fota_status);
}
void rock_progress(void* ctx, int percent) {
int i = 0;
rock_trace(ctx, "rock update progress %d\n", percent);
if (percent > 20) {
i = fota_status.ota_run;
if (fota_status.update_status[i-1].check_delta != PASS) {
fota_status.update_status[i-1].check_delta = PASS;
fota_status.update_status[i-1].check_rom= PASS;
save_fota_status();
}
}
}
int rock_process_block(void* ctx, unsigned char* data, unsigned int start, unsigned int size){
//rock_trace(ctx, "rock update progress block %d\n", size);
int writen = 0;
int ret,err;
if (start == BACKUP_ADDR_FLAG) {
int fd_backup = open(FILE_BACKUP,O_RDWR | O_CREAT,0777);
while (writen < size) {
write(fd_backup,data,ROCK_DEFAULT_BLOCK_SIZE);
sync();
writen += ROCK_DEFAULT_BLOCK_SIZE;
}
close(fd_backup);
return size;
}
writen = 0;
if (mtk_device_wrap_seek(fd_write, start, SEEK_SET) < 0) {
err = errno;
rock_trace(ctx, "mtk_device_wrap_seek write\n");
return err;
}
while (writen < size) {
ret = mtk_device_wrap_write(fd_write,data+writen, ROCK_DEFAULT_BLOCK_SIZE);
writen += ROCK_DEFAULT_BLOCK_SIZE;
}
return size;
}
int rock_write_block(void* ctx, unsigned char* src, unsigned int start, unsigned int size){
#if 0
int writen = 0;
int ret,err;
if (start == BACKUP_ADDR_FLAG) {
int fd_backup = open(FILE_BACKUP,O_RDWR | O_CREAT,0777);
while (writen < size) {
write(fd_backup,src,ROCK_DEFAULT_BLOCK_SIZE);
sync();
writen += ROCK_DEFAULT_BLOCK_SIZE;
}
close(fd_backup);
return size;
}
writen = 0;
if (mtk_device_wrap_seek(fd_curr, start, SEEK_SET) < 0) {
err = errno;
rock_trace(ctx, "mtk_device_wrap_seek write\n");
return err;
}
while (writen < size) {
ret = mtk_device_wrap_write(fd_curr,src+writen, ROCK_DEFAULT_BLOCK_SIZE);
writen += ROCK_DEFAULT_BLOCK_SIZE;
}
#endif
return size;
}
int rock_read_block(void* ctx, unsigned char* dest, unsigned int start, unsigned int size){
int ret,err;
if (start == BACKUP_ADDR_FLAG) {
int fd_backup = open(FILE_BACKUP,O_RDONLY);
read(fd_backup,dest,size);
sync();
close(fd_backup);
return size;
}
if (mtk_device_wrap_seek(fd_read, start, SEEK_SET) < 0) {
err = errno;
rock_trace(ctx, "mtk_device_wrap_seek read block err\n");
}
do {
ret = mtk_device_wrap_read(fd_read, dest, size);
if (ret == 0) {
break;
} else if (ret < 0) {
if (errno == EINTR) {
continue;
}
err = -errno;
rock_trace(ctx,"%s Error reading metadata file\n");
mtk_device_wrap_close(fd_read);
return err;
}
size -= ret;
dest += ret;
} while(size > 0);
return ret;
}
int rock_read_delta(void* ctx, unsigned char* dest, unsigned int offset, unsigned int size){
int ret = 0,err = 0;
if (mtk_device_wrap_seek(fd_delta, offset + delta_offset, SEEK_SET) < 0) {
err = -errno;
rock_trace(ctx, "mtk_device_wrap_seek df_delta err\n");
return err;
}
do {
ret = mtk_device_wrap_read(fd_delta, dest, size);
if (ret == 0) {
break;
} else if (ret < 0) {
if (errno == EINTR) {
continue;
}
err = -errno;
rock_trace(ctx," Error reading metadata file\n");
mtk_device_wrap_close(fd_delta);
return err;
}
size -= ret;
dest += ret;
} while(size > 0);
return ret;
}
int rock_get_blocksize(void* ctx) {
return ROCK_DEFAULT_BLOCK_SIZE;
}
int rock_read_file(void* ctx, void* name, unsigned char* dest, unsigned int offset, unsigned int size){return 0;}
int rock_write_file(void* ctx, void* name, unsigned char* src, unsigned int offset, unsigned int size){return 0;}
int rock_delete_file(void* ctx, void* name){return 0;}
/* ROCK IPL end */
static int init_dev_fd()
{
int err;
//fd_delta = mtk_device_wrap_open(DEV_DELTA,O_RDONLY);
fd_delta = mtk_device_wrap_open(DEV_DELTA,O_RDWR);
if (fd_delta < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_update_status = open(FILE_UPDATE_STATE,O_RDWR | O_CREAT,0777);
//fd_update_status = open(FILE_UPDATE_STATE,O_RDWR);
if (fd_update_status < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
return 0;
}
static int close_dev_fd(int fd)
{
mtk_device_wrap_close(fd);
}
static int reboot_device() {
reboot(RB_AUTOBOOT);
while (1) pause();
}
int test_write_delta(char *source, char *target)
{
int fd_source,fd_target,size;
char delta_data[ROCK_DEFAULT_BLOCK_SIZE];
fd_source = open(source,O_RDONLY);
if (fd_source < 0) {
LYERRLOG("+[UA]: open source error\n");
return 1;
}
fd_target = mtk_device_wrap_open(target,O_RDWR);
if (fd_target < 0) {
mtk_device_wrap_close(fd_target);
close(fd_source);
LYERRLOG("+[UA]: open target error\n");
return 1;
}
while(( size = read(fd_source,delta_data,ROCK_DEFAULT_BLOCK_SIZE))>0) {
mtk_device_wrap_write(fd_target,delta_data,ROCK_DEFAULT_BLOCK_SIZE);
}
mtk_device_wrap_close(fd_target);
close(fd_source);
return 0;
}
int nand_copyto_nand(char *source, char *target)
{
int fd_source,fd_target,size;
char delta_data[ROCK_DEFAULT_BLOCK_SIZE];
fd_source = mtk_device_wrap_open(source,O_RDONLY);
if (fd_source < 0) {
LYERRLOG("+[UA]: open source error\n");
return 1;
}
fd_target = mtk_device_wrap_open(target,O_RDWR);
if (fd_target < 0) {
mtk_device_wrap_close(fd_target);
mtk_device_wrap_close(fd_source);
LYERRLOG("+[UA]: open target error\n");
return 1;
}
while(( size = mtk_device_wrap_read(fd_source,delta_data,ROCK_DEFAULT_BLOCK_SIZE))>0) {
mtk_device_wrap_write(fd_target,delta_data,ROCK_DEFAULT_BLOCK_SIZE);
}
mtk_device_wrap_close(fd_target);
mtk_device_wrap_close(fd_source);
return 0;
}
int delta_copyto_nand(unsigned int start,int size)
{
char delta_data[NAND_PAGE_SIZE];
unsigned int ret = 0;
int err;
if (mtk_device_wrap_seek(fd_delta, start, SEEK_SET) < 0) {
LYERRLOG("+[UA]: delta_copyto_nand seek err\n");
return -1;
}
if (mtk_device_wrap_seek(fd_curr, 0, SEEK_SET) < 0) {
LYERRLOG("+[UA]: delta_copyto_nand seek err\n");
return -1;
}
do {
ret = mtk_device_wrap_read(fd_delta, delta_data, NAND_PAGE_SIZE);
if (ret == 0) {
break;
} else if (ret < 0) {
if (errno == EINTR) {
continue;
}
err = -errno;
return err;
}
size -= NAND_PAGE_SIZE;
//mtk_device_wrap_write(fd_curr,delta_data,NAND_PAGE_SIZE);
mtk_device_wrap_write(fd_curr,delta_data,ret);
} while(size > 0);
return 0;
}
unsigned char ram_buffer[ROCK_RAM_LEN];
static int rock_update_main(unsigned int rom_base, unsigned int backup_base, unsigned int backup_len, int read_rom_directly, int first_run) {
int status,err,start;
int ret = 0;
int i = 0;
int retry_cnt = 0;
IOT_UPDATA_CONTEXT ctx;
UPDATE_INFO up_info;
//OTA_STATUS fota_status;
const hw_module_t* hw_module;
unsigned int slot;
unsigned int update_mode = MODE_NORMAL;
unsigned int delta_size;
unsigned char full_header[9];
char digest_s[SHA_DIGEST_SIZE];
char digest_t[SHA_DIGEST_SIZE];
hw_module = &HAL_MODULE_INFO_SYM;
if (!hw_module ||
strcmp(BOOT_CONTROL_HARDWARE_MODULE_ID, hw_module->id) != 0) {
ret = -EINVAL;
}
if (ret != 0) {
LYERRLOG("+[UA]: Error loading boot_control HAL implementation.\n");
return -1;
}
module = (boot_control_module_t*)hw_module;
module->init(module);
if (module == NULL) {
LYERRLOG("+[UA]: Error getting bootctrl module.\n");
return -1;
}
/************* Bootctrl Init End *************/
current_slot = module->getCurrentSlot(module);
int is_successful = module->isSlotMarkedSuccessful(module, current_slot);
LYVERBLOG("Booting slot = %d, : isSlotMarkedSuccessful= %d\n",current_slot,is_successful);
memset(&ctx, 0, sizeof(ctx));
ctx.rom_base = 0;
ctx.ram_base =(unsigned char *)&ram_buffer[0];
ctx.ram_len = ROCK_RAM_LEN;
ctx.backup_base = BACKUP_ADDR_FLAG;
ctx.backup_len = ROCK_DEFAULT_BLOCK_SIZE;
ctx.update_nvram = 0;
ctx.read_rom_directly = read_rom_directly;
//ctx.first_run = first_run;
ctx.first_run = 1;
init_dev_fd();
memset(&up_info, 0, sizeof(up_info));
memset(&fota_status,0,sizeof(fota_status));
lseek(fd_update_status,0,SEEK_SET);
read(fd_update_status,(unsigned char *)&up_info,sizeof(up_info));
LYVERBLOG("+[UA]: up_info.ota_run = %d\n",up_info.ota_run);
if ((up_info.ota_run>PATCH_BL33)||(up_info.ota_run<PATCH_SYSTEM))
{
up_info.ota_run = 0;
}
up_info.ota_run = 0;
if((up_info.fota_flag[0]=='A')&&(up_info.fota_flag[1]=='-')&&(up_info.fota_flag[2]=='B')){
update_mode = MODE_A2B;
}else if((up_info.fota_flag[0]=='B')&&(up_info.fota_flag[1]=='-')&&(up_info.fota_flag[2]=='A')){
update_mode = MODE_B2A;
}else{
update_mode = MODE_NORMAL;
}
LYVERBLOG("+[UA]: up_info.fota_flag = %s\n",up_info.fota_flag);
LYVERBLOG("+[UA]: update_mode = %d\n",update_mode);
memset(&da_head, 0, sizeof(da_head));
mtk_device_wrap_read(fd_delta, (unsigned char*)&da_head, sizeof(da_head));
rock_trace(&ctx, "da_head.sys:%d,da_head.boot:%d,da_head.tee:%d,da_head.md1img=%d,da_head.vbmeta=%d,da_head.bl33=%d\n", da_head.sys, da_head.boot,da_head.tee,da_head.md1img,da_head.vbmeta,da_head.bl33);
rock_trace(&ctx, "da_head.fullsys:%d,da_head.fullboot:%d,da_head.fulltee:%d,da_head.fullmd1img=%d,da_head.fullvbmeta=%d,da_head.fullbl33=%d\n", da_head.full_sys, da_head.full_boot,da_head.full_tee, da_head.full_md1img,da_head.full_vbmeta,da_head.full_bl33);
if (da_head.sys>0) {
fota_status.update_status[PATCH_SYSTEM - 1].need_update = 1;
}
if (da_head.boot>0) {
fota_status.update_status[PATCH_BOOT - 1].need_update = 1;
}
if (da_head.tee>0) {
fota_status.update_status[PATCH_TEE - 1].need_update = 1;
}
if (da_head.md1img>0) {
fota_status.update_status[PATCH_MD1IMG - 1].need_update = 1;
}
if (da_head.vbmeta>0) {
fota_status.update_status[PATCH_VBMETA - 1].need_update = 1;
}
if (da_head.bl33>0) {
fota_status.update_status[PATCH_BL33 - 1].need_update = 1;
}
if (da_head.full_sys>0) {
fota_status.update_status[FULL_SYSTEM - 1].need_update = 1;
}
if (da_head.full_boot>0) {
fota_status.update_status[FULL_BOOT - 1].need_update = 1;
}
if (da_head.full_tee>0) {
fota_status.update_status[FULL_TEE - 1].need_update = 1;
}
if (da_head.full_md1img>0) {
fota_status.update_status[FULL_MD1IMG - 1].need_update = 1;
}
if (da_head.full_bl33>0) {
fota_status.update_status[FULL_BL33 - 1].need_update = 1;
}
if(da_head.full_vbmeta>0) {
fota_status.update_status[FULL_VBMETA - 1].need_update = 1;
}
fota_status.switch_slot = WAIT;
save_fota_status();
delta_size = da_head.sys + da_head.boot + da_head.tee + da_head.md1img + da_head.vbmeta + da_head.bl33;
if ((da_head.sys>0) && (up_info.ota_run <= PATCH_SYSTEM))
{
now_patch = PATCH_SYSTEM;
delta_offset = DELTA_HEARD_SIZE;
#if 0
if (up_info.ota_run == PATCH_SYSTEM)
{
ctx.first_run = 0;
}else{
ctx.first_run = 1;
}
//up_info.ota_run = 0;
if(current_slot==SLOT_B) {
fd_system_a = mtk_device_wrap_open(DEV_SYSTEM_A,O_RDWR);
if (fd_system_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_system_a;
}else{
fd_system_b = mtk_device_wrap_open(DEV_SYSTEM_B,O_RDWR);
if (fd_system_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_system_b;
}
#endif
ctx.first_run = 1; // always
if(current_slot==SLOT_B) {
system("flash_eraseall /dev/disk/by-partlabel/system_a");
} else {
system("flash_eraseall /dev/disk/by-partlabel/system_b");
}
// if(current_slot==SLOT_B) {
fd_system_a = mtk_device_wrap_open(DEV_SYSTEM_A,O_RDWR);
if (fd_system_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_system_a;
// }else{
fd_system_b = mtk_device_wrap_open(DEV_SYSTEM_B,O_RDWR);
if (fd_system_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_system_b;
// }
if(current_slot==SLOT_B){
fd_read = fd_system_b;
fd_write = fd_system_a;
} else {
fd_read = fd_system_a;
fd_write = fd_system_b;
}
fota_status.ota_run = PATCH_SYSTEM;
fota_status.update_status[PATCH_SYSTEM -1].check_delta = WAIT;
fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
fota_status.update_status[PATCH_SYSTEM-1].update_result= WAIT;
save_fota_status();
up_info.ota_run = PATCH_SYSTEM;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
LYVERBLOG("+[UA]: Start upgrading system.\n");
status = iot_patch(&ctx);
LYVERBLOG("+[UA]: system upgrade result:%d\n",status);
//fota_status.ota_run = 0;
fota_status.update_status[PATCH_SYSTEM -1].update_result= status;
fota_status.update_result= status;
if((status == 0)||(status ==1))
{
fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
fota_status.update_status[PATCH_SYSTEM -1].check_rom = PASS;
}else if(status == E_ROCK_INVALID_DELTA) {
fota_status.update_status[PATCH_SYSTEM -1].check_delta = ERROR;
fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
}else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
fota_status.update_status[PATCH_SYSTEM -1].check_rom = ERROR;
}else{
//fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
//fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
}
save_fota_status();
if ((status != 0) &&(status != 1))
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
return status;
}
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
}
if ((da_head.boot>0) && (up_info.ota_run <= PATCH_BOOT))
{
now_patch = PATCH_BOOT;
delta_offset = DELTA_HEARD_SIZE + da_head.sys;
if (up_info.ota_run == PATCH_BOOT)
{
ctx.first_run = 0;
}else{
ctx.first_run = 1;
}
if(current_slot==SLOT_B) {
system("flash_eraseall /dev/disk/by-partlabel/boot_a");
} else {
system("flash_eraseall /dev/disk/by-partlabel/boot_b");
}
//if(current_slot==SLOT_B) {
fd_boot_a = mtk_device_wrap_open(DEV_BOOT_A,O_RDWR);
if (fd_boot_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_boot_a;
// }else{
fd_boot_b = mtk_device_wrap_open(DEV_BOOT_B,O_RDWR);
if (fd_boot_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_boot_b;
// }
if(current_slot==SLOT_B){
fd_read = fd_boot_b;
fd_write = fd_boot_a;
} else {
fd_read = fd_boot_a;
fd_write = fd_boot_b;
}
fota_status.ota_run = PATCH_BOOT;
fota_status.update_status[PATCH_BOOT-1].check_delta = WAIT;
fota_status.update_status[PATCH_BOOT-1].check_rom = WAIT;
fota_status.update_status[PATCH_BOOT-1].update_result= WAIT;
save_fota_status();
up_info.ota_run = PATCH_BOOT;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
LYVERBLOG("+[UA]: Start upgrading boot.\n");
status = iot_patch(&ctx);
LYVERBLOG("+[UA]: boot upgrade result:%d\n",status);
//up_info.ota_run = 0;
//fota_status.ota_run = 0;
fota_status.update_status[PATCH_BOOT-1].update_result= status;
fota_status.update_result= status;
if((status == 0)||(status ==1))
{
fota_status.update_status[PATCH_BOOT-1].check_delta = PASS;
fota_status.update_status[PATCH_BOOT-1].check_rom = PASS;
}else if(status == E_ROCK_INVALID_DELTA) {
fota_status.update_status[PATCH_BOOT-1].check_delta = ERROR;
fota_status.update_status[PATCH_BOOT-1].check_rom = WAIT;
}else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
fota_status.update_status[PATCH_BOOT-1].check_delta = PASS;
fota_status.update_status[PATCH_BOOT-1].check_rom = ERROR;
}else{
//fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
//fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
}
save_fota_status();
if ((status != 0) &&(status != 1))
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
return status;
}
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
}
if ((da_head.tee>0) && (up_info.ota_run <= PATCH_TEE))
{
now_patch = PATCH_TEE;
delta_offset = DELTA_HEARD_SIZE + da_head.sys + da_head.boot;
if (up_info.ota_run == PATCH_TEE)
{
ctx.first_run = 0;
}else{
ctx.first_run = 1;
}
if(current_slot==SLOT_B) {
system("flash_eraseall /dev/disk/by-partlabel/tee_a");
} else {
system("flash_eraseall /dev/disk/by-partlabel/tee_b");
}
// if(current_slot==SLOT_B) {
fd_tee_a = mtk_device_wrap_open(DEV_TEE_A,O_RDWR);
if (fd_tee_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_tee_a;
// }else{
fd_tee_b = mtk_device_wrap_open(DEV_TEE_B,O_RDWR);
if (fd_tee_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_tee_b;
// }
if(current_slot==SLOT_B){
fd_read = fd_tee_b;
fd_write = fd_tee_a;
} else {
fd_read = fd_tee_a;
fd_write = fd_tee_b;
}
fota_status.ota_run = PATCH_TEE;
fota_status.update_status[PATCH_TEE-1].check_delta = WAIT;
fota_status.update_status[PATCH_TEE-1].check_rom = WAIT;
fota_status.update_status[PATCH_TEE-1].update_result= WAIT;
save_fota_status();
up_info.ota_run = PATCH_TEE;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
LYVERBLOG("+[UA]: Start upgrading tee.\n");
status = iot_patch(&ctx);
LYVERBLOG("+[UA]: tee upgrade result:%d\n",status);
//up_info.ota_run = 0;
//fota_status.ota_run = 0;
fota_status.update_status[PATCH_TEE-1].update_result= status;
fota_status.update_result= status;
if((status == 0)||(status ==1))
{
fota_status.update_status[PATCH_TEE-1].check_delta = PASS;
fota_status.update_status[PATCH_TEE-1].check_rom = PASS;
}else if(status == E_ROCK_INVALID_DELTA) {
fota_status.update_status[PATCH_TEE-1].check_delta = ERROR;
fota_status.update_status[PATCH_TEE-1].check_rom = WAIT;
}else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
fota_status.update_status[PATCH_TEE-1].check_delta = PASS;
fota_status.update_status[PATCH_TEE-1].check_rom = ERROR;
}else{
//fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
//fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
}
save_fota_status();
if ((status != 0) &&(status != 1))
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
return status;
}
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
}
if ((da_head.md1img>0) && (up_info.ota_run <= PATCH_MD1IMG))
{
now_patch = PATCH_MD1IMG;
delta_offset = DELTA_HEARD_SIZE + da_head.sys + da_head.boot + da_head.tee;
if (up_info.ota_run == PATCH_MD1IMG)
{
ctx.first_run = 0;
}else{
ctx.first_run = 1;
}
if(current_slot==SLOT_B) {
system("flash_eraseall /dev/disk/by-partlabel/md1img_a");
} else {
system("flash_eraseall /dev/disk/by-partlabel/md1img_b");
}
// if(current_slot==SLOT_B) {
fd_md1img_a = mtk_device_wrap_open(DEV_MD1IMG_A,O_RDWR);
if (fd_md1img_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_md1img_a;
// }else{
fd_md1img_b = mtk_device_wrap_open(DEV_MD1IMG_B,O_RDWR);
if (fd_md1img_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_md1img_b;
// }
if(current_slot==SLOT_B){
fd_read = fd_md1img_b;
fd_write = fd_md1img_a;
} else {
fd_read = fd_md1img_a;
fd_write = fd_md1img_b;
}
fota_status.ota_run = PATCH_MD1IMG;
fota_status.update_status[PATCH_MD1IMG-1].check_delta = WAIT;
fota_status.update_status[PATCH_MD1IMG-1].check_rom = WAIT;
fota_status.update_status[PATCH_MD1IMG-1].update_result= WAIT;
save_fota_status();
up_info.ota_run = PATCH_MD1IMG;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
LYVERBLOG("+[UA]: Start upgrading md1img.\n");
status = iot_patch(&ctx);
LYVERBLOG("+[UA]: md1img upgrade result:%d\n",status);
//fota_status.ota_run = 0;
fota_status.update_status[PATCH_MD1IMG-1].update_result= status;
fota_status.update_result= status;
if((status == 0)||(status ==1))
{
fota_status.update_status[PATCH_MD1IMG-1].check_delta = PASS;
fota_status.update_status[PATCH_MD1IMG-1].check_rom = PASS;
}else if(status == E_ROCK_INVALID_DELTA) {
fota_status.update_status[PATCH_MD1IMG-1].check_delta = ERROR;
fota_status.update_status[PATCH_MD1IMG-1].check_rom = WAIT;
}else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
fota_status.update_status[PATCH_MD1IMG-1].check_delta = PASS;
fota_status.update_status[PATCH_MD1IMG-1].check_rom = ERROR;
}else{
//fota_status.update_status[PATCH_MD1IMG -1].check_delta = PASS;
//fota_status.update_status[PATCH_MD1IMG -1].check_rom = WAIT;
}
save_fota_status();
if ((status != 0) &&(status != 1))
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
return status;
}
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
}
if ((da_head.vbmeta>0) && (up_info.ota_run <= PATCH_VBMETA))
{
now_patch = PATCH_VBMETA;
delta_offset = DELTA_HEARD_SIZE + da_head.sys + da_head.boot + da_head.tee + da_head.md1img;
if (up_info.ota_run == PATCH_VBMETA)
{
ctx.first_run = 0;
}else{
ctx.first_run = 1;
}
if(current_slot==SLOT_B) {
system("flash_eraseall /dev/disk/by-partlabel/vbmeta_a");
} else {
system("flash_eraseall /dev/disk/by-partlabel/vbmeta_b");
}
// if(current_slot==SLOT_B) {
fd_vbmeta_a = mtk_device_wrap_open(DEV_VBMETA_A,O_RDWR);
if (fd_vbmeta_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_vbmeta_a;
// }else{
fd_vbmeta_b = mtk_device_wrap_open(DEV_VBMETA_B,O_RDWR);
if (fd_vbmeta_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
// fd_curr = fd_vbmeta_b;
// }
if(current_slot==SLOT_B){
fd_read = fd_vbmeta_b;
fd_write = fd_vbmeta_a;
} else {
fd_read = fd_vbmeta_a;
fd_write = fd_vbmeta_b;
}
fota_status.ota_run = PATCH_VBMETA;
fota_status.update_status[PATCH_VBMETA-1].check_delta = WAIT;
fota_status.update_status[PATCH_VBMETA-1].check_rom = WAIT;
fota_status.update_status[PATCH_VBMETA-1].update_result= WAIT;
save_fota_status();
up_info.ota_run = PATCH_VBMETA;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
LYVERBLOG("+[UA]: Start upgrading vbmeta.\n");
status = iot_patch(&ctx);
LYVERBLOG("+[UA]: vbmeta upgrade result:%d\n",status);
up_info.ota_run = 0;
//fota_status.ota_run = 0;
fota_status.update_status[PATCH_VBMETA-1].update_result= status;
fota_status.update_result= status;
if((status == 0)||(status ==1))
{
fota_status.update_status[PATCH_VBMETA-1].check_delta = PASS;
fota_status.update_status[PATCH_VBMETA-1].check_rom = PASS;
}else if(status == E_ROCK_INVALID_DELTA) {
fota_status.update_status[PATCH_VBMETA-1].check_delta = ERROR;
fota_status.update_status[PATCH_VBMETA-1].check_rom = WAIT;
}else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
fota_status.update_status[PATCH_VBMETA-1].check_delta = PASS;
fota_status.update_status[PATCH_VBMETA-1].check_rom = ERROR;
}else{
//fota_status.update_status[PATCH_MD1IMG -1].check_delta = PASS;
//fota_status.update_status[PATCH_MD1IMG -1].check_rom = WAIT;
}
save_fota_status();
if ((status != 0) &&(status != 1))
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
return status;
}
mtk_device_wrap_close(fd_read);
mtk_device_wrap_close(fd_write);
}
if ((da_head.bl33>0) && (up_info.ota_run <= PATCH_BL33))
{
now_patch = PATCH_BL33;
delta_offset = DELTA_HEARD_SIZE + da_head.sys + da_head.boot + da_head.tee + da_head.md1img + da_head.vbmeta;
if (up_info.ota_run == PATCH_BL33)
{
ctx.first_run = 0;
}else{
ctx.first_run = 1;
}
fd_bl33 = mtk_device_wrap_open(DEV_BL33,O_RDWR);
if (fd_bl33 < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_bl33;
fota_status.ota_run = PATCH_BL33;
fota_status.update_status[PATCH_BL33-1].check_delta = WAIT;
fota_status.update_status[PATCH_BL33-1].check_rom = WAIT;
fota_status.update_status[PATCH_BL33-1].update_result= WAIT;
save_fota_status();
up_info.ota_run = PATCH_BL33;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
LYVERBLOG("+[UA]: Start upgrading bl33.\n");
status = iot_patch(&ctx);
LYVERBLOG("+[UA]: bl33 upgrade result:%d\n",status);
up_info.ota_run = 0;
//fota_status.ota_run = 0;
fota_status.update_status[PATCH_BL33-1].update_result= status;
fota_status.update_result= status;
if((status == 0)||(status ==1))
{
fota_status.update_status[PATCH_BL33-1].check_delta = PASS;
fota_status.update_status[PATCH_BL33-1].check_rom = PASS;
}else if(status == E_ROCK_INVALID_DELTA) {
fota_status.update_status[PATCH_BL33-1].check_delta = ERROR;
fota_status.update_status[PATCH_BL33-1].check_rom = WAIT;
}else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
fota_status.update_status[PATCH_BL33-1].check_delta = PASS;
fota_status.update_status[PATCH_BL33-1].check_rom = ERROR;
}else{
//fota_status.update_status[PATCH_BL33 -1].check_delta = PASS;
//fota_status.update_status[PATCH_BL33 -1].check_rom = WAIT;
}
save_fota_status();
if ((status != 0) &&(status != 1))
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
mtk_device_wrap_close(fd_curr);
sync();
close(fd_update_status);
return status;
}
mtk_device_wrap_close(fd_curr);
}
//if(current_slot==SLOT_B)
if ((da_head.full_sys>0)|| (da_head.full_boot>0)|| (da_head.full_tee>0)||(da_head.full_md1img>0)||(da_head.full_vbmeta>0)||(da_head.full_bl33>0))
{
now_patch = 0;
up_info.ota_run = 0;
memset(&fota_status,0,sizeof(fota_status));
fota_status.switch_slot = WAIT;
save_fota_status();
if (mtk_device_wrap_seek(fd_delta, DELTA_HEARD_SIZE + delta_size, SEEK_SET) < 0) {
err = errno;
LYERRLOG("+[UA]: mtk_device_wrap_seek df_delta err\n");
return -1;
}
mtk_device_wrap_read(fd_delta, full_header, DELTA_FULL_HEARD_SIZE);
if (memcmp(full_header, "full-ota", DELTA_FULL_HEARD_SIZE) != 0) {
LYERRLOG("+[UA]: invalid full delta header\r\n");
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = -1;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
//mtk_device_wrap_close(fd_curr);
sync();
close(fd_update_status);
for (i = FULL_SYSTEM;i<=FULL_BL33;i++){
if (fota_status.update_status[i-1].need_update ==1) {
fota_status.ota_run = i;
fota_status.update_status[i-1].check_delta = ERROR;
fota_status.update_status[i-1].check_rom= WAIT;
fota_status.update_status[i-1].update_result= ERROR;
}
}
fota_status.update_result = ERROR;
save_fota_status();
return -1;
}
}
if(da_head.full_sys>0) {
delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE;
if(current_slot==SLOT_B) {
fd_system_a = mtk_device_wrap_open(DEV_SYSTEM_A,O_RDWR);
if (fd_system_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_system_a;
}else{
fd_system_b = mtk_device_wrap_open(DEV_SYSTEM_B,O_RDWR);
if (fd_system_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_system_b;
}
fota_status.ota_run = FULL_SYSTEM;
save_fota_status();
retry_cnt = 0;
LYVERBLOG("+[UA]: Start upgrading system full.\n");
do {
status = delta_copyto_nand(delta_offset,da_head.full_sys);
ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_sys,digest_s);
ROCK_SHA_FILE(fd_curr,0,da_head.full_sys,digest_t);
retry_cnt++;
}while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
mtk_device_wrap_close(fd_curr);
LYVERBLOG("+[UA]: system full retry_cnt = %d\n",retry_cnt);
if (retry_cnt>3) {
if (status == 0) {
status = retry_cnt;
}
if(current_slot==SLOT_B) {
nand_copyto_nand(DEV_SYSTEM_B,DEV_SYSTEM_A);
}
else{
nand_copyto_nand(DEV_SYSTEM_A,DEV_SYSTEM_B);
}
}
LYVERBLOG("+[UA]: system full upgrade result:%d\n",status);
fota_status.update_result = status;
fota_status.update_status[FULL_SYSTEM-1].update_result = status;
save_fota_status();
if (status != 0)
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
return status;
}
}
if(da_head.full_boot>0) {
delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE + da_head.full_sys;
if(current_slot==SLOT_B) {
fd_boot_a = mtk_device_wrap_open(DEV_BOOT_A,O_RDWR);
if (fd_boot_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_boot_a;
}else{
fd_boot_b = mtk_device_wrap_open(DEV_BOOT_B,O_RDWR);
if (fd_boot_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_boot_b;
}
fota_status.ota_run = FULL_BOOT;
save_fota_status();
retry_cnt = 0;
LYVERBLOG("+[UA]: Start upgrading boot full.\n");
do {
status = delta_copyto_nand(delta_offset,da_head.full_boot);
ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_boot,digest_s);
ROCK_SHA_FILE(fd_curr,0,da_head.full_boot,digest_t);
retry_cnt++;
}while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
LYVERBLOG("+[UA]: boot full retry_cnt = %d\n",retry_cnt);
mtk_device_wrap_close(fd_curr);
if (retry_cnt>3) {
if (status == 0) {
status = retry_cnt;
}
if(current_slot==SLOT_B) {
nand_copyto_nand(DEV_BOOT_B,DEV_BOOT_A);
}
else{
nand_copyto_nand(DEV_BOOT_A,DEV_BOOT_B);
}
}
LYVERBLOG("+[UA]: boot full upgrade result:%d\n",status);
fota_status.update_result = status;
fota_status.update_status[FULL_BOOT-1].update_result = status;
save_fota_status();
if (status != 0)
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
return status;
}
}
if(da_head.full_tee>0) {
delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE + da_head.full_sys + da_head.full_boot;
if(current_slot==SLOT_B) {
fd_tee_a = mtk_device_wrap_open(DEV_TEE_A,O_RDWR);
if (fd_tee_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_tee_a;
}else{
fd_tee_b = mtk_device_wrap_open(DEV_TEE_B,O_RDWR);
if (fd_tee_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_tee_b;
}
fota_status.ota_run = FULL_TEE;
save_fota_status();
retry_cnt = 0;
LYVERBLOG("+[UA]: Start upgrading tee full.\n");
do {
status = delta_copyto_nand(delta_offset,da_head.full_tee);
ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_tee,digest_s);
ROCK_SHA_FILE(fd_curr,0,da_head.full_tee,digest_t);
retry_cnt++;
}while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
mtk_device_wrap_close(fd_curr);
LYVERBLOG("+[UA]: tee full retry_cnt = %d\n",retry_cnt);
if (retry_cnt>3) {
if (status == 0) {
status = retry_cnt;
}
if(current_slot==SLOT_B) {
nand_copyto_nand(DEV_TEE_B,DEV_TEE_A);
}
else{
nand_copyto_nand(DEV_TEE_A,DEV_TEE_B);
}
}
printf("+[UA] tee full upgrade result:%d\n",status);
fota_status.update_result = status;
fota_status.update_status[FULL_TEE-1].update_result = status;
save_fota_status();
if (status != 0)
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
return status;
}
}
if(da_head.full_md1img>0) {
delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE + da_head.full_sys + da_head.full_tee;
if(current_slot==SLOT_B) {
fd_md1img_a = mtk_device_wrap_open(DEV_MD1IMG_A,O_RDWR);
if (fd_md1img_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_md1img_a;
}else{
fd_md1img_b = mtk_device_wrap_open(DEV_MD1IMG_B,O_RDWR);
if (fd_md1img_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_md1img_b;
}
fota_status.ota_run = FULL_MD1IMG;
save_fota_status();
retry_cnt = 0;
LYVERBLOG("+[UA]: Start upgrading md1img full.\n");
do {
status = delta_copyto_nand(delta_offset,da_head.full_md1img);
ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_md1img,digest_s);
ROCK_SHA_FILE(fd_curr,0,da_head.full_md1img,digest_t);
retry_cnt++;
}while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
mtk_device_wrap_close(fd_curr);
LYVERBLOG("+[UA]: md1img full retry_cnt = %d\n",retry_cnt);
if (retry_cnt>3) {
if (status == 0) {
status = retry_cnt;
}
if(current_slot==SLOT_B) {
nand_copyto_nand(DEV_MD1IMG_B,DEV_MD1IMG_A);
}
else{
nand_copyto_nand(DEV_MD1IMG_A,DEV_MD1IMG_B);
}
}
LYVERBLOG("+[UA]: md1img upgrade result:%d\n",status);
fota_status.update_result = status;
fota_status.update_status[FULL_MD1IMG-1].update_result = status;
save_fota_status();
if (status != 0)
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
return status;
}
}
if(da_head.full_vbmeta>0) {
delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE + da_head.full_sys + da_head.full_tee + da_head.full_md1img;
if(current_slot==SLOT_B) {
fd_vbmeta_a = mtk_device_wrap_open(DEV_VBMETA_A,O_RDWR);
if (fd_vbmeta_a < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_vbmeta_a;
}else{
fd_vbmeta_b = mtk_device_wrap_open(DEV_VBMETA_B,O_RDWR);
if (fd_vbmeta_b < 0) {
err = errno;
LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
return -err;
}
fd_curr = fd_vbmeta_b;
}
fota_status.ota_run = FULL_VBMETA;
save_fota_status();
retry_cnt = 0;
LYVERBLOG("+[UA]: Start upgrading vbmeta full.\n");
do {
status = delta_copyto_nand(delta_offset,da_head.full_vbmeta);
ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_vbmeta,digest_s);
ROCK_SHA_FILE(fd_curr,0,da_head.full_vbmeta,digest_t);
retry_cnt++;
}while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
mtk_device_wrap_close(fd_curr);
LYVERBLOG("+[UA]: vbmeta full retry_cnt = %d\n",retry_cnt);
if (retry_cnt>3) {
if (status == 0) {
status = retry_cnt;
}
if(current_slot==SLOT_B) {
nand_copyto_nand(DEV_VBMETA_B,DEV_VBMETA_A);
}
else{
nand_copyto_nand(DEV_VBMETA_A,DEV_VBMETA_B);
}
}
LYVERBLOG("+[UA]: vbmeta upgrade result:%d\n",status);
fota_status.update_result = status;
fota_status.update_status[FULL_VBMETA-1].update_result = status;
save_fota_status();
if (status != 0)
{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close(fd_update_status);
return status;
}
}
if(da_head.full_bl33>0) {
}
if(update_mode == MODE_NORMAL){ //need A VS B
if(current_slot == SLOT_A) {
up_info.fota_flag[0] = 'B';
up_info.fota_flag[1] = '-';
up_info.fota_flag[2] = 'A';
}else{
up_info.fota_flag[0] = 'A';
up_info.fota_flag[1] = '-';
up_info.fota_flag[2] = 'B';
}
}else{
up_info.fota_flag[0] = 'e';
up_info.fota_flag[1] = 'n';
up_info.fota_flag[2] = 'd';
}
up_info.update_result = status;
up_info.ota_run = 0;
lseek(fd_update_status,0,SEEK_SET);
write(fd_update_status, &up_info,sizeof(up_info));
sync();
close_dev_fd(fd_delta);
//close_dev_fd(fd_curr);
close(fd_update_status);
sync();
slot = (current_slot == 0) ? 1 : 0;
LYVERBLOG("+[UA]: slot SLOT = %d\n\n",slot);
if(update_mode==MODE_NORMAL){
module->setActiveBootSlot(module,slot);
LYVERBLOG("+[UA]: upgrade is success!!!!\n");
}
fota_status.ota_run = 0;
fota_status.switch_slot = PASS;
fota_status.update_result = status;
save_fota_status();
if(update_mode==MODE_NORMAL){
LYVERBLOG("+[UA]: reboot_device\n");
reboot_device();
}
return status;
}
static void rock_fail_handler() {
}
/* main entrpoint */
int lynq_rock_main(int first_run) {
int ret = 0;
#if 0
printf("-********copy delta ***-\n");
test_write_delta("/data/delta",DEV_DELTA);
#endif
ret = rock_update_main(0, 0, 0, 0, first_run);
if(ret) {
rock_fail_handler();
}
return ret;
}
#endif
//lt add @2021.9.23 for deal with power down \ backup or upgrade.
int lynq_fota_func(void)
{
int fd;
int first_run = 1;
UPDATE_INFO lynq_up_info;
memset(&lynq_up_info, 0, sizeof(lynq_up_info));
fd = open(FILE_UPDATE_STATE,O_RDWR | O_CREAT,0777);
if (fd < 0) {
return -1;
}
read(fd,(unsigned char *)&lynq_up_info,sizeof(lynq_up_info));
close(fd);
if(lynq_up_info.ota_run != 0) {
//Power off, call UA
LYVERBLOG("[+UP]: ***Power off, call UA***\n");
lynq_rock_main(first_run);
}else if (((lynq_up_info.fota_flag[0]=='A')&&(lynq_up_info.fota_flag[1]=='-')&&(lynq_up_info.fota_flag[2]=='B'))||((lynq_up_info.fota_flag[0]=='B')&&(lynq_up_info.fota_flag[1]=='-')&&(lynq_up_info.fota_flag[2]=='A'))){
//Upgrade the other side and call UA
LYVERBLOG("[+UP]: ***Upgrade the other side and call UA***\n");
lynq_rock_main(first_run);
}
/*
else{
//Normal procedure before, to check the upgrade package
// LYVERBLOG("[+UP]: ***Normal procedure before, to check the upgrade package***\n");
// lynq_rock_main(first_run);
}*/
return 0;
}