blob: 03dccb43dae86a4b638fec7fec4abbaa6b262b2c [file] [log] [blame]
hj.shao4a9a5052025-07-19 03:56:43 -07001#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <sys/stat.h>
5#include <sys/types.h>
6#include <unistd.h>
7#include <dlfcn.h>
8#include <stdint.h>
b.liu68a94c92025-05-24 12:53:41 +08009
hj.shao4a9a5052025-07-19 03:56:43 -070010#include <errno.h>
11#include <stdbool.h>
b.liu68a94c92025-05-24 12:53:41 +080012
hj.shao4a9a5052025-07-19 03:56:43 -070013#include "gsw_ota_ua_interface.h"
14#include "gsw_log_interface.h"
b.liu68a94c92025-05-24 12:53:41 +080015
b.liu68a94c92025-05-24 12:53:41 +080016
hj.shao4a9a5052025-07-19 03:56:43 -070017#define LYNQ_OTA_INPROCESS 3
18#define LYNQ_OTA_SUCCESS 4
19#define LYNQ_OTA_FAILED 5
20
21
22
23#define LIB_PATH "/lib/libmbtk_lib.so"
24
25#define GSW_OTA "[HAL][GSW_OTA]"
26
27#define StatFunc(x,y) stat(x,y)
28
29typedef enum
30{
31 GSW_UPDATE_SUCCEED = 0, //update succeed
32 GSW_UPDATE_INPROGRESS, //update in progress
33 GSW_UPDATE_BACKUP, //A/B partition sync in progress
34 GSW_UPDATE_FAILED, //update failed
35 GSW_UPDATE_WAITEDONE, //update in-active part finished,not switch update part.
36 GSW_UPDATE_NEEDSYNC, //switch update part, need sync A/B part
37 GSW_UPDATE_CANCEL //updata cancel success
38}gsw_update_state_t;
39
40typedef enum
41{
42 GSW_UPDATE_NOERROR=0, //升级成功
43 GSW_UPDATE_ARGUMENTERROR, //升级程序启动参数错误
44 GSW_UPDATE_SDCARDNOEXIST, //未挂载外置FLASH等存储设备
45 GSW_UPDATE_PACKAGENOEXIST, //升级包不存在
46 GSW_UPDATE_UNZIPFAILED, //解压升级包出错
47 GSW_UPDATE_PARTITIONFLUSHERROR,//写入分区出错
48 GSW_UPDATE_XMLPARSEERROR, //解析 fotaconfig.xml 文件出错
49 GSW_UPDATE_DIFFUBIUNATTACH, //差分升级 UBI 分区挂载异常
50 GSW_UPDATE_NOSPACELEFT, //空间不足
51 GSW_UPDATE_FILECHECKFAILED, //MD5 值校验失败
52 GSW_UPDATE_BSPATCHFAILED, //bspatch 合成新文件夹失败
53 GSW_UPDATE_NOFINDPARTITION, //待升级分区不存在
54 GSW_UPDATE_UBIVOLUMEERROR, //差分升级,待升级 UBI 卷不存在
55 GSW_UPDATE_NOFOTACONFIGFILE, //升级包中无 fotaconfig.xml 文件
56 GSW_UPDATE_GETOLDSOFTWAREFAILED,//读取原始版本固件失败
57 GSW_UPDATE_FILENOTEXIST, //文件不存在
58 GSW_UPDATE_UPGRADECANCELED, //升级或分区同步被取消
59 GSW_UPDATE_NONEEDCANCEL, //取消升级失败
60 GSW_UPDATE_NOGOING //升级或分区同步正在进行,不可重复操作
61}gsw_update_exit_code_t;
62
63
64typedef struct
65{
66 unsigned int percentage; //update progress0-100
67 gsw_update_state_t update_state;
68 gsw_update_exit_code_t exit_code;
69}gsw_update_info_s;
70
71typedef struct
72{
73 gsw_update_state_t update_state;
74 uint8_t is_damaged; //TURE: damaged FALSE:no damaged
75 uint8_t damaged_partname[16];
76}gsw_system_status_s;
77
78typedef int (*mbtk_fota_get_active_absys_type)(void);
79typedef int (*mbtk_fota_fw_write)(char* fname, int segment_size);
80typedef int (*mbtk_fota_fw_write_by_url)(char* url, int segment_size,
81 int conn_timeout, int download_timeout);
82
83typedef int(*fota_callback)(int state, int percent);
84typedef int (*mbtk_fota_init)(fota_callback cb);
85typedef int (*mbtk_fota_status)(void);
86
87static int segment_size =0;
88static int Process_flag = 0;
89char addr_buf[256] = {0};
90static void *handle = NULL;
91
92
93static int s_ota_flag = -1;
94
95int fota_cb(int status, int percent)
96{
97 Process_flag = percent;
98 return 0;
99}
100
101static int funstat( char *filename)
b.liu68a94c92025-05-24 12:53:41 +0800102{
103 int ret = 0;
104 struct stat tmep_s;
105
106 memset(&tmep_s, 0, sizeof(stat));
107 ret = StatFunc(filename, &tmep_s);
108
109 if (ret)
110 {
hj.shao4a9a5052025-07-19 03:56:43 -0700111 LOGE(GSW_OTA, "stat %s failed! error_code:%s", filename ,strerror(errno));
b.liu68a94c92025-05-24 12:53:41 +0800112 return -1;
113 }
114
115 if (tmep_s.st_size > 0)
116 {
117 segment_size = tmep_s.st_size;
118 }
119
120 return 0;
hj.shao4a9a5052025-07-19 03:56:43 -0700121}
122
123int32_t init_handle()
124{
125 if(handle == NULL)
126 {
127 handle = dlopen(LIB_PATH, RTLD_NOW );
128 if(handle == NULL)
129 {
130 return GSW_HAL_NORMAL_FAIL;
131 }
132 }
133
134 return GSW_HAL_SUCCESS;
135}
136
137/**
138* @brief Start install modem software
139* @param [in] char* file_path
140* @param [out] NULL
141* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
142*/
143int32_t gsw_update_modem_start_autobackup(char* file_path)
144{
145
146 if(file_path == NULL)
147 {
148 LOGE(GSW_OTA, "invalid file_path \n");
149 return GSW_HAL_NORMAL_FAIL;
150 }
151
152
153 mbtk_fota_fw_write func_ptr_update = NULL;
154 mbtk_fota_init func_ptr_init= NULL;
155 mbtk_fota_fw_write_by_url func_ptr_update_url = NULL;
156
157
158
159 int ret = -1;
160 ret = init_handle();
161 if(ret < 0)
162 {
163 LOGE(GSW_OTA, "init_handle fail");
164 return GSW_HAL_NORMAL_FAIL;
165 }
166
167 func_ptr_update_url = (mbtk_fota_fw_write_by_url)dlsym(handle, "mbtk_fota_fw_write_by_url");
168 if(func_ptr_update_url == NULL)
169 {
170 LOGE(GSW_OTA, "Error: %s", dlerror());
171 return GSW_HAL_NORMAL_FAIL;
172 }
173
174
175 // Get the function pointer
176 func_ptr_update = (mbtk_fota_fw_write)dlsym(handle, "mbtk_fota_fw_write");
177 if (func_ptr_update == NULL)
178 {
179 LOGE(GSW_OTA, "Error: %s", dlerror());
180 return GSW_HAL_NORMAL_FAIL;
181 }
182
183 func_ptr_init = (mbtk_fota_init)dlsym(handle, "mbtk_fota_init");
184 if(func_ptr_init == NULL)
185 {
186 LOGE(GSW_OTA, "Error: %s", dlerror());
187 return GSW_HAL_NORMAL_FAIL;
188 }
189
190
191 if(s_ota_flag == -1)
192 {
193 ret = func_ptr_init(fota_cb);
194 if(ret < 0)
195 {
196 LOGE(GSW_OTA, "Error: mbtk_fota_init failed");
197
198 return GSW_HAL_NORMAL_FAIL;
199 }
200 else
201 {
202 s_ota_flag = 0;
203 }
204 }
205
206 if(strncmp(file_path, "http", 4) == 0)
207 {
208 segment_size = 62914560;
209 ret = func_ptr_update_url(file_path, segment_size,10, 600);
210 }
211 else
212 {
213 if(strstr(file_path,"fota.delta") == NULL)
214 {
215 LOGE(GSW_OTA, "Bad file path ");
216 return GSW_HAL_NORMAL_FAIL;
217 }
218 else
219 {
220 if(access(file_path,F_OK) !=0)
221 {
222 LOGE(GSW_OTA, "update file no exist");
223 return GSW_HAL_NORMAL_FAIL;
224 }
225 }
226
227 ret = funstat(file_path);
b.liu68a94c92025-05-24 12:53:41 +0800228 if (ret)
229 {
hj.shao4a9a5052025-07-19 03:56:43 -0700230 LOGE(GSW_OTA, "get segment_size fail");
231 return GSW_HAL_NORMAL_FAIL;
232 }
233 ret = func_ptr_update(file_path, segment_size);
234 if(ret < 0)
235 {
236 LOGE(GSW_OTA, "Error: mbtk_fota_fw_write failed\n");
237 return GSW_HAL_NORMAL_FAIL;
238 }
239 }
240 return GSW_HAL_SUCCESS;
241
242}
243
244/**
245* @brief check the modem update condition
246* @param [in] NULL
247* @param [out] NULL
248* @retval TRUE/FALSE
249*/
250bool gsw_update_modem_check_condition(void)
251{
252 char command[32] = {0};
253 char buffer[64] = {0};
254 const char *process_ota = "{otad}";
255
256 snprintf(command,sizeof(command), "ps | grep %s | grep -v grep", process_ota);
257 FILE *fp = popen(command, "r");
258 if (fp == NULL)
259 {
260 return false;
261 }
262
263 if(fgets(buffer, sizeof(buffer), fp)!= NULL)
264 {
265 pclose(fp);
266 return true;
267 }
268 pclose(fp);
269 return false;
270
271}
272
273/**
274* @brief get update modem result
275* @param [in] NULL
276* @param [out] NULL
277* @retval E_GSW_OTA_RET
278*/
279int32_t gsw_update_modem_result_query(void)
280{
281
282 mbtk_fota_status func_ptr_get_result = NULL;
283 int ret = -1;
284
285 ret = init_handle();
286 if(ret < 0)
287 {
288 LOGE(GSW_OTA, "init_handle fail");
289 return GSW_HAL_NORMAL_FAIL;
290 }
291
292 // Get the function pointer
293 func_ptr_get_result = (mbtk_fota_status)dlsym(handle, "mbtk_fota_status");
294 if (func_ptr_get_result == NULL)
295 {
296 LOGE(GSW_OTA, "Error: %s\n", dlerror());
297 return GSW_HAL_NORMAL_FAIL;
298 }
299 ret = func_ptr_get_result();
300 if(ret < 0 && ret !=-1)
301 {
302 LOGE(GSW_OTA, "Error: mbtk_fota_status failed\n");
303 return GSW_HAL_NORMAL_FAIL;
304 }
305
306
307 if(ret == LYNQ_OTA_INPROCESS)
308 {
309 return GSW_OTA_INPROCESS;
310
311 }
312 else if(ret == LYNQ_OTA_SUCCESS)
313 {
314 return GSW_OTA_SUCCESS;
315 }
316 else if(ret == -1)
317 {
318 return GSW_OTA_NO_TASK;
319 }
320
321 return GSW_OTA_FAILURE;
322
323
324}
325
326/**
327* @brief Start install modem software
328* @param [in] char* file_path
329* @param [out] NULL
330* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
331*/
332int32_t gsw_update_modem_start_nobackup(char* file_path)
333{
334
335 return GSW_HAL_SUCCESS;
336}
337
338
339/**
340* @brief get current system
341* @param [in] NULL
342* @param [out] NULL
343* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
344*/
345int32_t gsw_update_modem_get_system(void)
346{
347 int ret = -1;
348
349 mbtk_fota_get_active_absys_type func_ptr;
350 ret = init_handle();
351 if(ret < 0)
352 {
353 LOGE(GSW_OTA, "init_handle fail");
354 return GSW_HAL_NORMAL_FAIL;
355 }
356
357 // Get the function pointer
358 func_ptr = (mbtk_fota_get_active_absys_type)dlsym(handle, "mbtk_fota_get_active_absys_type");
359 if (func_ptr == NULL)
360 {
361 LOGE(GSW_OTA, "Error: %s", dlerror());
362 return GSW_HAL_NORMAL_FAIL;
363 }
364 ret = func_ptr();
365 if(ret < 0)
366 {
367 LOGE(GSW_OTA, "Error: mbtk_fota_get_active_absys_type failed");
368 return GSW_HAL_NORMAL_FAIL;
369
370 }
371
372
373 return ret;
374
375}
376
377/**
378* @brief cancel update
379* @param [in] NULL
380* @param [out] NULL
381* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
382*/
383int32_t gsw_update_modem_cancel(void)
384{
385
386 return GSW_HAL_SUCCESS;
387}
388
389/**
390* @brief get modem update info
391* @param [in] NULL
392* @param [out] gsw_update_info_s
393* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
394*/
395int32_t gsw_update_modem_get_info(gsw_update_info_s *update_info)
396{
397 int ret = -1;
398
399 mbtk_fota_status func_ptr_get_info = NULL;
400
401 ret = init_handle();
402 if(ret < 0)
403 {
404 LOGE(GSW_OTA, "init_handle fail");
405 return GSW_HAL_NORMAL_FAIL;
406 }
407
408 // Get the function pointer
409 func_ptr_get_info = (mbtk_fota_status)dlsym(handle, "mbtk_fota_status");
410 if (func_ptr_get_info == NULL)
411 {
412 LOGE(GSW_OTA, "Error: %s\n", dlerror());
413 return GSW_HAL_NORMAL_FAIL;
414 }
415 ret = func_ptr_get_info();
416 if(ret < 0 && ret != -1)
417 {
418 LOGE(GSW_OTA, "Error: mbtk_fota_status failed\n");
419 return GSW_HAL_NORMAL_FAIL;
420 }
421
422 //升级成功之后,未重启,不可重复升级
423 if(ret == LYNQ_OTA_INPROCESS)
424 {
425 update_info->exit_code = GSW_UPDATE_NOGOING;
426 update_info->percentage = Process_flag;
427 update_info->update_state = GSW_UPDATE_INPROGRESS;
428 }
429 if(ret == LYNQ_OTA_SUCCESS)
430 {
431 update_info->exit_code = GSW_UPDATE_NOERROR;
432 update_info->percentage = Process_flag;
433 update_info->update_state = GSW_UPDATE_WAITEDONE;
434 }
435
436
437 if(ret == LYNQ_OTA_FAILED || ret == -1)
438 {
439 update_info->exit_code = GSW_UPDATE_GETOLDSOFTWAREFAILED;
440 update_info->percentage = Process_flag;
441 update_info->update_state = GSW_UPDATE_FAILED;
442 }
443
444 return GSW_HAL_SUCCESS;
445
446}
447
448
449/**
450* @brief get modem system status
451* @param [in] NULL
452* @param [out] gsw_system_status_s
453* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
454*/
455int32_t gsw_update_modem_get_status(gsw_system_status_s *system_status)
456{
457 return GSW_HAL_SUCCESS;
458}
459
460/**
461* @brief A/B system sync
462* @param [in] NULL
463* @param [out] NULL
464* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
465*/
466int32_t gsw_update_modem_sync(void)
467{
468 return GSW_HAL_SUCCESS;
469
470}
471
472/**
473* @brief A/B system switch
474* @param [in] NULL
475* @param [out] NULL
476* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
477*/
478int32_t gsw_update_modem_switch(void)
479{
480 return GSW_HAL_SUCCESS;
481}
482
483int32_t gsw_update_modem_process(void *bufdata)
484{
485 return gsw_update_modem_start_autobackup(UPDATE_MODEM_PACKAGE);
486}
487
488int32_t gsw_get_ab_system(gsw_system_type *type)
489{
490 int32_t ret = GSW_HAL_NORMAL_FAIL;
491
492 ret = gsw_update_modem_get_system();
493 if (GSW_HAL_NORMAL_FAIL != ret)
494 {
495 *type = (gsw_system_type)ret;
496 }
497
498 return ret;
499}
500
501