blob: 735f35064670fad0da3e2047ace691e182cd8ec5 [file] [log] [blame]
l.yang7d7b51c2023-08-04 17:02:48 +08001/*******************************************************
2*
3* @brief:
4* @details: add fota upgrade api
5* @author: l.yang
6* @date: 2023.8.3
7* @version: V1.0
8* @copyright:Copyright (c) MobileTek
9*
10*********************************************/
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/types.h>
16#include <errno.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19#include <sys/ioctl.h>
20#include <dirent.h>
21#include <getopt.h>
22#include <unistd.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#include "include/lynq-qser-fota.h"
29#include "liblog/lynq_deflog.h"
30
31
32#define USER_LOG_TAG "LYNQ_FOTA"
33
34
35#define SYSTEM_A "34650"
36#define SYSTEM_B "39019"
37
38char path[] = {0};
39
40#define BOOTABLE "45227"
41#define UNBOOTABLE "47806"
42
43
44typedef struct
45{
46 int system;
47 int status;
48 int try_cnt;
49} z_system_info_t;
50
51typedef struct
52{
53 int upgrade_status;
54 int total_size;
55 int upgraded_size;
56} z_upgrade_status_info_t;
57
58typedef struct
59{
60 int boot_to;
61 int fota_status;
62 z_system_info_t system_1;
63 z_system_info_t system_2;
64} z_upgrade_system_info_t;
65
66typedef struct
67{
68 z_upgrade_status_info_t *status;
69 void(* status_cb)(z_upgrade_status_info_t *status);
70} z_upgrade_flush_status_t;
71
72extern int zxic_dual_verify();
73extern int zxic_dual_upgrade(z_upgrade_flush_status_t *flush_status);
74extern int zxic_dual_get_upgrade_status(z_upgrade_status_info_t *upgrade_info);
75extern int zxic_dual_get_current_system();
76extern int zxic_dual_get_boot_to_system();
77extern int zxic_dual_set_boot_to_system(int system, int reboot_flag);
78extern int zxic_dual_get_system_status(z_upgrade_system_info_t *system_info);
79extern int zxic_dual_set_system_status(int system, int status);
80extern int zxic_dual_get_fota_status_for_nv();
81extern int zxic_dual_set_fota_status_for_nv(int status);
82extern int zxic_dual_sync_system(void);
83extern int zxic_dual_get_upgrade_type(void);
84extern void zxic_dual_get_sync_status(int *sync_status);
85extern int zxic_dual_set_sync_status(int sync_status);
86extern int zxic_dual_config_package_path(char *upgrade_package_path, int length);
87
88int lynq_fota_verify();
89int lynq_fota_upgrade();
90int lynq_get_current_system();
91int lynq_get_boot_to_system();
92int lynq_set_boot_to_system(char *option_para);
93int lynq_set_system_a_status(char *option_para);
94int lynq_set_system_b_status(char *option_para);
95int lynq_sync_system();
96int lynq_get_upgrade_type();
97int lynq_get_sync_status();
98int lynq_set_sync_status(char *option_para);
99
100
101/*****************************************
102* @brief:rock_update_main
103* @param count [IN]:NA
104* @param sum [OUT]:NA
105* @return :success 0, failed -1
106* @todo:NA
107* @see:NA
108* @warning:NA
109******************************************/
110int rock_update_main(int reboot_flag)
111{
112
113 int ret = 0;
114 int current_slot = 0;
115
116 ret = lynq_fota_verify();
117 if(ret != 0)
118 {
119 LYINFLOG("Verify package failed exit upgrade");
120 return -1;
121 }
122
123
124 LYINFLOG("-----Begin to upgrade ----");
125 ret = lynq_fota_upgrade();
126 if(ret != 0)
127 {
128 LYINFLOG("Fota upgrade failed!!!");
129 return -1;
130 }
131
132 ret = lynq_get_upgrade_status();
133 if(ret != 0)
134 {
135 LYINFLOG("Upgrade failed ");
136 return -1;
137 }
138
139 current_slot = lynq_get_current_system();
140 if(current_slot < 0)
141 {
142 LYINFLOG("Get current system failed");
143 return -1;
144 }
145
146
147 if(current_slot == atoi(SYSTEM_A))
148 {
149 LYINFLOG("current system is system a");
150
151
152 ret = lynq_set_boot_to_system((char *)SYSTEM_B);
153 if(ret != 0)
154 {
155 LYINFLOG("set system B boot failed ");
156 return -1;
157 }
158
159
160 ret = lynq_set_system_a_status((char *)UNBOOTABLE);
161 if(ret < 0)
162 {
163 LYINFLOG("Set system A status to [%s] error ", UNBOOTABLE);
164 return -1;
165 }
166
167 ret = lynq_set_system_b_status((char *)BOOTABLE);
168 if(ret < 0)
169 {
170 LYINFLOG("Set system B status to [%s] error ", BOOTABLE);
171 return -1;
172 }
173
174 }
175 else if( current_slot == atoi(SYSTEM_B))
176 {
177 LYINFLOG("current system is system a");
178
179
180 ret = lynq_set_boot_to_system((char *)SYSTEM_A);
181 if(ret != 0)
182 {
183 LYINFLOG("set system B boot failed ");
184 return -1;
185 }
186
187
188 ret = lynq_set_system_b_status((char *)UNBOOTABLE);
189 if(ret < 0)
190 {
191 LYINFLOG("Set system B status to [%s] error ", UNBOOTABLE);
192 return -1;
193 }
194
195 ret = lynq_set_system_a_status((char *)BOOTABLE);
196 if(ret < 0)
197 {
198 LYINFLOG("Set system A status to [%s] error ", BOOTABLE);
199 return -1;
200 }
201
202 }
203
204 if(reboot_flag == 1)
205 {
206 LYINFLOG("Enter reboot device");
207 system("reboot");
208 }
209
210 return 0;
211}
212
213/*****************************************
214* @brief:upgrade callback function
215* @param count [IN]:z_upgrade_status_info_t *p_status
216* @param sum [OUT]:NA
217* @return :NA
218* @todo:
219* @see:NA
220* @warning:NA
221******************************************/
222void lynq_g_flush_upgrade_status(z_upgrade_status_info_t *p_status)
223{
224
225 LYINFLOG("Current status:%d ", p_status->upgrade_status);
226
227 LYINFLOG("Total size:%d ", p_status->total_size);
228
229 LYINFLOG("Updated size:%d ", p_status->upgraded_size);
230 return ;
231}
232
233/*****************************************
234* @brief:lynq_fota_set_addr_value
235* @param count [IN]:char *path
236* @param sum [OUT]:
237* @return :success 0, failed -1
238* @todo:NA
239* @see:NA
240* @warning:NA
241******************************************/
242int lynq_fota_set_addr_value(char *value,int size)
243{
244 int ret =0;
245
246 LYLOGSET(LOG_INFO);
247 LYLOGEINIT(USER_LOG_TAG);
248
249 if (NULL == value || size <= 0)
250 {
251 LYINFLOG("Bad package path!!!");
252 return -1;
253 }
254 ret = zxic_dual_config_package_path(value, strlen(value));
255 if(ret == -1)
256 {
257 LYINFLOG("Configure upgrade package path fail");
258 return -1;
259 }
260
261 LYINFLOG("Configure upgrade package path success");
262 return 0;
263}
264
265
266/*****************************************
267* @brief:lynq_fota_verify
268* @param count [IN]:NA
269* @param sum [OUT]:NA
270* @return :success 0, failed -1
271* @todo:NA
272* @see:NA
273* @warning:NA
274******************************************/
275int lynq_fota_verify()
276{
277 int ret = 0;
278
279 LYINFLOG("-----Begin to verify upgrade package----");
280 ret = zxic_dual_verify();
281 if(ret != 0)
282 {
283 LYINFLOG("Verify upgrade package failed");
284 return -1;
285 }
286
287 return 0;
288
289}
290
291/*****************************************
292* @brief:upgrade function
293* @param count [IN]:NA
294* @param sum [OUT]:NA
295* @return :success 0, failed -1
296* @todo:NA
297* @see:NA
298* @warning:NA
299******************************************/
300int lynq_fota_upgrade()
301{
302 int ret = 0;
303 z_upgrade_flush_status_t flush_status;
304 z_upgrade_status_info_t status;
305 memset(&status, 0, sizeof(z_upgrade_status_info_t));
306 flush_status.status = &status;
307 flush_status.status_cb = &lynq_g_flush_upgrade_status;
308
309
310 LYINFLOG("Enter lynq_fota_upgrade !!!!");
311 ret = zxic_dual_upgrade(&flush_status);
312 if(ret != 0)
313 {
314 LYINFLOG("Fota upgrade failed!!!");
315 return -1;
316 }
317 return 0;
318}
319
320/*****************************************
321* @brief:lynq_fota_nrestart no reboot
322* @param count [IN]:NA
323* @param sum [OUT]:NA
324* @return :success 0, failed -1
325* @todo:NA
326* @see:NA
327* @warning:NA
328******************************************/
329int lynq_fota_nrestart(void)
330{
331 int ret = 0;
332 ret = rock_update_main(0);
333 if(ret != 0)
334 {
335 LYINFLOG("Upgrade failed !!!!");
336 return -1;
337 }
338 else
339 {
340 LYINFLOG("upgrade success!!!");
341 }
342 return 0;
343}
344
345/*****************************************
346* @brief:lynq_rock_main,upgrade done reboot
347* @param count [IN]:int first_run
348* @param sum [OUT]:NA
349* @return :success 0, failed -1
350* @todo:NA
351* @see:NA
352* @warning:NA
353******************************************/
354int lynq_rock_main(int first_run)
355{
356 if(first_run <= 0)
357 {
358 LYINFLOG("Bad input value !!!");
359 return -1;
360 }
361
362 int ret = 0;
363 ret = rock_update_main(1);
364 LYINFLOG("rock_update_main ret = %d\n", ret);
365 if(ret != 0)
366 {
367 LYINFLOG("fota update fail!\n");
368 }
369 return 0;
370
371}
372
373/*****************************************
374* @brief:lynq_get_current_system
375* @param count [IN]:NA
376* @param sum [OUT]:NA
377* @return :success: curretn slot , failed:-1
378* @todo:NA
379* @see:NA
380* @warning:NA
381******************************************/
382int lynq_get_current_system()
383{
384 int ret = 0;
385
386 ret = zxic_dual_get_current_system();
387 if(ret < 0)
388 {
389 LYINFLOG("lynq get current system faile");
390 return -1;
391 }
392 if(ret == 34650)
393 {
394 LYINFLOG("Get current system is system A");
395 }
396 else if(ret == 39019)
397 {
398 LYINFLOG("Get current system is system B");
399 }
400
401 return ret;
402
403}
404
405/*****************************************
406* @brief:lynq_get_boot_to_system
407* @param count [IN]:NA
408* @param sum [OUT]:NA
409* @return :success: 0, failed:-1
410* @todo:NA
411* @see:NA
412* @warning:NA
413******************************************/
414int lynq_get_boot_to_system()
415{
416 int boot_to = 0;
417 boot_to = zxic_dual_get_boot_to_system();
418 if(boot_to < 0)
419 {
420 LYINFLOG("lynq get boot to system failed");
421 return -1;
422 }
423 else
424 {
425 LYINFLOG("Boot to system:0x%08X[%d]",boot_to);
426 }
427
428 return 0;
429
430}
431
432/*****************************************
433* @brief:lynq_set_boot_to_system
434* @param count [IN]:char *option_para
435* @param sum [OUT]:NA
436* @return :success: 0, failed:-1
437* @todo:NA
438* @see:NA
439* @warning:NA
440******************************************/
441int lynq_set_boot_to_system(char *option_para)
442{
443 int ret = 0;
444 if(option_para == NULL)
445 {
446 LYINFLOG("Input invalid value! null option parameters! ");
447 return -1;
448 }
449
450 ret = zxic_dual_set_boot_to_system(atoi(option_para), 0);
451 if(ret < 0)
452 {
453 LYINFLOG("Set boot to [%s] error ", option_para);
454 return -1;
455 }
456
457 return 0;
458}
459
460/*****************************************
461* @brief:lynq_get_upgrade_status
462* @param count [IN]:NA
463* @param sum [OUT]:NA
464* @return :success: 0, failed:-1
465* @todo:NA
466* @see:NA
467* @warning:NA
468******************************************/
469int lynq_get_upgrade_status(void)
470{
471 z_upgrade_status_info_t status;
472 int ret = 0;
473
474
475 ret = zxic_dual_get_upgrade_status(&status);
476 if(ret < 0)
477 {
478 LYINFLOG("Get upgrade status fail! ");
479 return -1;
480 }
481
482 LYINFLOG("Current upgrade info: ");
483 LYINFLOG("Current upgrade status:%d ", status.upgrade_status);
484 LYINFLOG("Current upgrade total size:%d ", status.total_size);
485 LYINFLOG("Current upgrade updated size:%d ", status.upgraded_size);
486 if(status.upgrade_status != 4)
487 {
488 LYINFLOG("Upgrade failed");
489 return -1;
490 }
491
492 return 0;
493
494}
495
496/*****************************************
497* @brief:lynq_get_upgrade_status
498* @param count [IN]:NA
499* @param sum [OUT]:NA
500* @return :success: 0, failed:-1
501* @todo:NA
502* @see:NA
503* @warning:NA
504******************************************/
505int lynq_get_system_info()
506{
507 int ret = 0;
508 z_upgrade_system_info_t system_info;
509
510 ret = zxic_dual_get_system_status(&system_info);
511 if(ret < 0)
512 {
513 LYINFLOG("Get upgrade status fail! ");
514 return -1;
515 }
516 else
517 {
518 LYINFLOG("System info: ");
519 LYINFLOG("Boot to:0x%08X[%d] ", system_info.boot_to, system_info.boot_to);
520 LYINFLOG("Fota status:%d ", system_info.fota_status);
521 LYINFLOG("System1 system:0x%08X [%d] ", system_info.system_1.system, system_info.system_1.system);
522 LYINFLOG("System1 status:%d ", system_info.system_1.status);
523 LYINFLOG("System1 try_cnt:%d ", system_info.system_1.try_cnt);
524 LYINFLOG("System2 system:0x%08X [%d] ", system_info.system_2.system, system_info.system_2.system);
525 LYINFLOG("System2 status:%d ", system_info.system_2.status);
526 LYINFLOG("System2 try_cnt:%d ", system_info.system_2.try_cnt);
527 }
528
529 return 0;
530
531}
532
533/*****************************************
534* @brief:lynq_set_system_a_status
535* @param count [IN]:char *option_para
536* @param sum [OUT]:NA
537* @return :success: 0, failed:-1
538* @todo:NA
539* @see:NA
540* @warning:NA
541******************************************/
542int lynq_set_system_a_status(char *option_para)
543{
544 int ret = -1;
545 if (NULL == option_para)
546 {
547 LYINFLOG("Invalid option_para value ");
548 return -1;
549 }
550
551 //34650 A slot status : bootable:45527 unbootalbe:47806 success:23579
552 ret = zxic_dual_set_system_status(34650, atoi(option_para));
553 if(ret < 0)
554 {
555 LYINFLOG("Set system 1 status to [%s] error ", option_para);
556 return -1;
557 }
558
559 return 0;
560
561}
562
563/*****************************************
564* @brief:lynq_set_system_b_status
565* @param count [IN]:char *option_para
566* @param sum [OUT]:NA
567* @return :success: 0, failed:-1
568* @todo:NA
569* @see:NA
570* @warning:NA
571*****************************************/
572int lynq_set_system_b_status(char * option_para)
573{
574 int ret = -1;
575 if (NULL == option_para)
576 {
577 LYINFLOG("Invalid option_para value ");
578 return -1;
579 }
580
581 //39019 B slot status: bootable:45227 unbootalbe:47806 success:23579
582 ret = zxic_dual_set_system_status(39019, atoi(option_para));
583 if(ret < 0)
584 {
585 LYINFLOG("Set system 2 status to [%s] error ", option_para);
586 }
587
588 return 0;
589}
590
591/*****************************************
592* @brief:lynq_get_fota_status_for_nv
593* @param count [IN]:NA
594* @param sum [OUT]:NA
595* @return :success: 0, failed:-1
596* @todo:NA
597* @see:NA
598* @warning:NA
599*****************************************/
600int lynq_get_fota_status_for_nv()
601{
602 int status = 0;
603 status = zxic_dual_get_fota_status_for_nv();
604 if(status != 0)
605 {
606 LYINFLOG("Fota status:%d ", status);
607 return -1;
608 }
609 return 0;
610}
611
612/*****************************************
613* @brief:lynq_set_fota_status_for_nv
614* @param count [IN]:char *option_para
615* @param sum [OUT]:NA
616* @return :success: 0, failed:-1
617* @todo:NA
618* @see:NA
619* @warning:NA
620*****************************************/
621int lynq_set_fota_status_for_nv(char * option_para)
622{
623 int ret = 0;
624
625 if (NULL == option_para)
626 {
627 LYINFLOG("Invalid value! null option parameters! ");
628 return -1;
629 }
630
631 ret = zxic_dual_set_fota_status_for_nv(atoi(option_para));
632 if(ret < 0)
633 {
634 LYINFLOG("Set fota_status to [%s] error ", option_para);
635 }
636
637 return ret;
638}
639
640/*****************************************
641* @brief:lynq_sync_system
642* @param count [IN]:char *option_para
643* @param sum [OUT]:NA
644* @return :success: 0, failed:-1
645* @todo:NA
646* @see:NA
647* @warning:NA
648*****************************************/
649int lynq_sync_system()
650{
651 int ret = -1;
652
653 ret = zxic_dual_sync_system();
654 if( ret != 0)
655 {
656 LYINFLOG("lynq sync system failed !!!!");
657 return -1;
658 }
659
660 return 0;
661}
662
663/*****************************************
664* @brief:lynq_get_upgrade_type
665* @param count [IN]:NA
666* @param sum [OUT]:NA
667* @return :success: 0, failed:-1
668* @todo:NA
669* @see:NA
670* @warning:NA
671*****************************************/
672int lynq_get_upgrade_type()
673{
674 int upgrade_type = -1;
675
676 upgrade_type = zxic_dual_get_upgrade_type();
677 if(upgrade_type < 0)
678 {
679 LYINFLOG("Get upgrade type failed \n");
680 return -1;
681 }
682 else
683 {
684 LYINFLOG("upgrade type is %d", upgrade_type);
685 }
686 return 0;
687}
688
689/*****************************************
690* @brief:lynq_get_sync_status
691* @param count [IN]:char *option_para
692* @param sum [OUT]:NA
693* @return :success: 0, failed:-1
694* @todo:NA
695* @see:NA
696* @warning:NA
697*****************************************/
698int lynq_get_sync_status()
699{
700 int sync_status = -2;
701 zxic_dual_get_sync_status(&sync_status);
702 LYINFLOG("Current sync status is %d", sync_status);
703 return 0;
704}
705
706/*****************************************
707* @brief:lynq_set_sync_status
708* @param count [IN]:char *option_para
709* @param sum [OUT]:NA
710* @return :success: 0, failed:-1
711* @todo:NA
712* @see:NA
713* @warning:NA
714*****************************************/
715int lynq_set_sync_status(char * option_para)
716{
717 int ret = 0;
718
719 if (NULL == option_para)
720 {
721 LYINFLOG("Command input invalid value! null option parameters! ");
722 return -1;
723 }
724
725 ret = zxic_dual_set_sync_status(atoi(option_para));
726 if (0 != ret)
727 {
728 LYINFLOG("set sync status fail");
729 return -1;
730 }
731
732 return 0;
733}
734
735
736#ifdef __cplusplus
737}
738#endif
739