xf.li | a06dd22 | 2024-10-14 09:07:20 +0000 | [diff] [blame] | 1 | /*******************************************************
|
| 2 | *
|
| 3 | * @brief:
|
| 4 | * @details: add fota A/B backup service
|
| 5 | * @author: l.yang
|
| 6 | * @date: 2023.8.29
|
| 7 | * @version: V1.0
|
| 8 | * @copyright:Copyright (c) MobileTek
|
| 9 | *
|
| 10 | *********************************************/
|
| 11 |
|
| 12 |
|
| 13 | #include <stdio.h>
|
| 14 | #include <stdlib.h>
|
| 15 | #include <stdbool.h>
|
| 16 | #include <dlfcn.h>
|
| 17 | #include <string.h>
|
| 18 | #include <pthread.h>
|
| 19 | #include <stdint.h>
|
| 20 | #include <unistd.h>
|
| 21 | #include <fcntl.h>
|
| 22 | #include <errno.h>
|
| 23 | #include <liblog/lynq_deflog.h>
|
| 24 |
|
| 25 |
|
| 26 | #ifdef __cplusplus
|
| 27 | extern "C" {
|
| 28 | #endif
|
| 29 |
|
| 30 | #define FOTA_REBOOT_FLAG "/mnt/userdata/.fota_reboot_flag"
|
| 31 | #define FOTA_FLAG_FILE "/mnt/userdata/.back_up_flag"
|
| 32 | #define FOTA_SYNC_FLAG 1
|
| 33 | #define FOTA_CURRENT_SYS "/mnt/userdata/.fota_current_sys"
|
| 34 |
|
| 35 | #define USER_LOG_TAG "LYNQ_FOTA_BACKUP"
|
| 36 |
|
| 37 | extern int lynq_sync_system();
|
| 38 | extern int lynq_fota_get_addr_value(char *tmp_value);
|
| 39 | extern int lynq_fota_set_addr_value(char *value,int size);
|
| 40 | extern int lynq_fota_nrestart(void);
|
| 41 | extern int lynq_get_current_system();
|
| 42 |
|
| 43 |
|
| 44 | #define REBOOT_DONE 1
|
| 45 |
|
| 46 | void set_upgrade_reboot_flag(void)
|
| 47 | {
|
| 48 | FILE *fp = NULL;
|
| 49 | int reboot_flag = REBOOT_DONE;
|
| 50 | fp = fopen(FOTA_REBOOT_FLAG,"w+");
|
| 51 | if(fp == NULL)
|
| 52 | {
|
| 53 | LYERRLOG("Open reboot flag file failed\n");
|
| 54 | return;
|
| 55 | }
|
| 56 |
|
| 57 | fwrite(&reboot_flag,sizeof(int),1,fp);
|
| 58 | fclose(fp);
|
| 59 | system("sync");
|
| 60 | return ;
|
| 61 | }
|
| 62 |
|
| 63 | int check_need_sync()
|
| 64 | {
|
| 65 | int current_sys = 0;
|
| 66 | int record_sys = 0;
|
| 67 | int ret = 0;
|
| 68 | char tmp_sys[8] = {0};
|
| 69 | FILE *fp = NULL;
|
| 70 |
|
| 71 | //not fota
|
| 72 | if(access(FOTA_FLAG_FILE, F_OK) == -1)
|
| 73 | {
|
| 74 | LYINFLOG("Fota flag file no exist\n");
|
| 75 |
|
| 76 | //file no exist,get current sys write to file
|
| 77 | if(access(FOTA_CURRENT_SYS,F_OK) == -1)
|
| 78 | {
|
| 79 | LYINFLOG("Record current sys file no exist\n");
|
| 80 |
|
| 81 | fp = fopen(FOTA_CURRENT_SYS,"w");
|
| 82 | if(fp == NULL)
|
| 83 | {
|
| 84 | LYERRLOG("creat record current file failed\n");
|
| 85 | return -1;
|
| 86 | }
|
| 87 | current_sys = lynq_get_current_system();
|
| 88 | if(current_sys < 0)
|
| 89 | {
|
| 90 | LYERRLOG("Get current system failed %d\n",current_sys);
|
| 91 | fclose(fp);
|
| 92 | return -1;
|
| 93 | }
|
| 94 | else
|
| 95 | {
|
| 96 |
|
| 97 | LYINFLOG("Get current system success %d\n",current_sys);
|
| 98 | fprintf(fp, "%d", current_sys);
|
| 99 | fclose(fp);
|
| 100 | system("sync");
|
| 101 |
|
| 102 | return 0;
|
| 103 | }
|
| 104 | }
|
| 105 | else
|
| 106 | {
|
| 107 | current_sys = lynq_get_current_system();
|
| 108 | if(current_sys < 0)
|
| 109 | {
|
| 110 | LYERRLOG("Get current system failed %d\n",current_sys);
|
| 111 | return -1;
|
| 112 | }
|
| 113 |
|
| 114 | LYINFLOG("Get current system success %d\n",current_sys);
|
| 115 |
|
| 116 | fp = fopen(FOTA_CURRENT_SYS,"r");
|
| 117 | if(fp == NULL)
|
| 118 | {
|
| 119 | LYERRLOG("read file failed \n");
|
| 120 | return -1;
|
| 121 | }
|
| 122 |
|
| 123 | if(fgets(tmp_sys, sizeof(tmp_sys), fp) != NULL)
|
| 124 | {
|
| 125 | record_sys = atoi(tmp_sys);
|
| 126 | }
|
| 127 | else
|
| 128 | {
|
| 129 | LYERRLOG("tmp_sys is NULL");
|
| 130 | fclose(fp);
|
| 131 | return -1;
|
| 132 | }
|
| 133 |
|
| 134 | if( record_sys == current_sys)
|
| 135 | {
|
| 136 | LYINFLOG("System not need sync \n");
|
| 137 | fclose(fp);
|
| 138 | return 0;
|
| 139 | }
|
| 140 | else
|
| 141 | {
|
| 142 | LYINFLOG("System need sync \n");
|
| 143 | ret = lynq_sync_system();
|
| 144 | if(ret < 0 )
|
| 145 | {
|
| 146 | LYERRLOG("A/B sync system failed \n");
|
| 147 | fclose(fp);
|
| 148 | return -1;
|
| 149 | }
|
| 150 | LYINFLOG("A/B sync system success,record current sys \n");
|
| 151 | fclose(fp);
|
| 152 | fp = fopen(FOTA_CURRENT_SYS,"w");
|
| 153 | if(fp == NULL)
|
| 154 | {
|
| 155 | LYERRLOG("creat file failed \n");
|
| 156 | return -1;
|
| 157 | }
|
| 158 |
|
| 159 | fprintf(fp,"%d",current_sys);
|
| 160 | fclose(fp);
|
| 161 | system("sync");
|
| 162 |
|
| 163 | return 0;
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 |
|
| 168 | }
|
| 169 | else
|
| 170 | {
|
| 171 | fp = fopen(FOTA_CURRENT_SYS,"w");
|
| 172 | if(fp == NULL)
|
| 173 | {
|
| 174 | LYERRLOG("Creat file failed \n");
|
| 175 | return -1;
|
| 176 | }
|
| 177 | LYINFLOG("fota flag file exist,record current sys \n");
|
| 178 | current_sys = lynq_get_current_system();
|
| 179 | if(current_sys < 0)
|
| 180 | {
|
| 181 | LYERRLOG("Get current system failed %d\n",current_sys);
|
| 182 | fclose(fp);
|
| 183 | return -1;
|
| 184 | }
|
| 185 |
|
| 186 | fprintf(fp,"%d",current_sys);
|
| 187 | fclose(fp);
|
| 188 | system("sync");
|
| 189 | return 0;
|
| 190 | }
|
| 191 |
|
| 192 |
|
| 193 | }
|
| 194 |
|
| 195 | int main()
|
| 196 | {
|
| 197 | int ret = 0 ;
|
| 198 | int sync_flag = 0;
|
| 199 | char tmp_addr[128] = {0};
|
| 200 | FILE *fp = NULL;
|
| 201 |
|
| 202 | check_need_sync();
|
| 203 |
|
| 204 | fp = fopen(FOTA_FLAG_FILE,"r");
|
| 205 | if(fp == NULL)
|
| 206 | {
|
| 207 | LYERRLOG("No need fota sync\n");
|
| 208 | return -1;
|
| 209 |
|
| 210 | }
|
| 211 |
|
| 212 | fread(&sync_flag,sizeof(int),1,fp);
|
| 213 | fclose(fp);
|
| 214 |
|
| 215 | set_upgrade_reboot_flag();
|
| 216 |
|
| 217 | if(sync_flag == FOTA_SYNC_FLAG)
|
| 218 | {
|
| 219 | ret = lynq_sync_system();
|
| 220 | if(ret != 0)
|
| 221 | {
|
| 222 | LYERRLOG("sync faild\n");
|
| 223 | }
|
| 224 | system("rm -rf /mnt/userdata/.back_up_flag");
|
| 225 |
|
| 226 | }
|
| 227 | else if(sync_flag != FOTA_SYNC_FLAG)
|
| 228 | {
|
| 229 | ret = lynq_fota_get_addr_value(tmp_addr);
|
| 230 | if(ret != 0)
|
| 231 | {
|
| 232 | LYERRLOG("Get addr failed\n");
|
| 233 | return -1;
|
| 234 | }
|
| 235 | LYINFLOG("tmp_addr is %s\n",tmp_addr);
|
| 236 | ret = lynq_fota_set_addr_value(tmp_addr,10);
|
| 237 | if(ret != 0)
|
| 238 | {
|
| 239 | LYERRLOG("Set addr failed\n");
|
| 240 | return -1;
|
| 241 | }
|
| 242 | ret = lynq_fota_nrestart();
|
| 243 | if(ret != 0)
|
| 244 | {
|
| 245 | LYERRLOG("Upgrade failed\n");
|
| 246 | return -1;
|
| 247 | }
|
| 248 |
|
| 249 | }
|
| 250 |
|
| 251 | return 0;
|
| 252 |
|
| 253 | }
|
| 254 | DEFINE_LYNQ_LIB_LOG(LYNQ_FOTA_BACKUP)
|
| 255 |
|
| 256 | #ifdef __cplusplus
|
| 257 | }
|
| 258 | #endif
|
| 259 |
|