blob: c2ef0e64f258ed47ffdbced6f715d3cb677e958e [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#define _GNU_SOURCE
2#include <stdio.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <sys/vfs.h>
xf.li84027492024-04-09 00:17:51 -070010#include <assert.h>
lh9ed821d2023-04-07 01:36:19 -070011
12
13#include <mtd/mtd-abi.h>
14#include <errno.h>
15
16#include <sys/ioctl.h>
17#include "fs_check.h"
18#include "cfg_api.h"
19#include "pub_debug_info.h"
20
21/*******************************************************************************
22 * Macro definitions *
23 ******************************************************************************/
24#define MOUNTS_INFO_FILE "/proc/mounts"
25
26#define NV_FS_FAC_MAIN_PATH "/mnt/imagefs/nvrwall.bin"
27#define NV_FS_RW_HASH_FAC_PATH "/mnt/imagefs/nvrwall.hash"
28#define NV_FS_RW_TOP_PATH "/etc_rw/psnv"
29#define NV_FS_RW_HASH_WORK_PATH "/etc_rw/psnv/nvrwall.hash"
30#define NV_FS_RW_MAIN_PATH "/etc_rw/psnv/rw_work"
31#define NV_FS_RW_BACKUP_PATH "/etc_rw/psnv/rw_backup"
32#define NV_FS_FAC_SYMBOL_PATH "/etc_rw/psnv/fac_flag"
33#define NV_FS_RW_MAIN_SYMBOL_PATH "/etc_rw/psnv/work_flag"
34#define NV_FS_RW_BACKUP_SYMBOL_PATH "/etc_rw/psnv/backup_flag"
35
36#define NV_FS_RW_AP_NV_MAIN_PATH "/etc_rw/nv/main/cfg"
37#define NV_FS_RW_AP_NV_BACKUP_PATH "/etc_rw/nv/backup/cfg"
38
39#define MOUNTS_LINE_LEN (256)
40#define MOUNTS_LINE_ELEMENT_LEN (64)
41#define MAX_PATH (256)
42#define BUF_MAX_LEN (32)
43#define UBI_DEV_MAX_NUM (10)
44#define SYSTEM_EXEC_FAIL (0)
45#define SYSTEM_EXEC_SUCC (1)
46#define USERDATA_RESET_FREE_BLOCK_LEVEL (2)
47#define USERDATA_FREE_DATA_SIZE (128)
48
49typedef enum {
50 DEVICE_MTD = 0,
51 DEVICE_ZFTL = 1,
52 DEVICE_MTD_BLOCK,
53} device_type_t;
54
55char *g_path_prefix = "";
56
57static int check_mount_result(const char *parti_mp)
58{
59 char *line_p, *temp_p;
60 char line[MOUNTS_LINE_LEN] = {0};
61 char line_element[MOUNTS_LINE_ELEMENT_LEN] = {0};
62 int found;
63 FILE *fp = NULL;
64 int mp_str_len;
65 if (parti_mp == NULL)
66 return -1;
67 if ((fp = fopen(MOUNTS_INFO_FILE, "r")) == NULL) {
68 printf("fs_check fopen %s failed, error:%s\n", MOUNTS_INFO_FILE, strerror(errno));
69 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check fopen %s failed, error:%s\n", MOUNTS_INFO_FILE, strerror(errno));
70 goto error;
71 }
72
73 found = 0;
74 while (1) {
75 memset(line, 0, sizeof(line));
76 if (NULL == fgets(line, sizeof(line), fp)) {
77 break;
78 }
79 //upi_log("line: %s", line);
80 line_p = line;
81 while (*line_p != '\0' && *line_p != ' ') { // first element
82 line_p++;
83 }
84 line_p++;
85 memset(line_element, 0, sizeof(line_element));
86 temp_p = line_element;
87 while (*line_p != '\0' && *line_p != ' ') { // second element, this is what we need
88 *temp_p = *line_p;
89 temp_p++;
90
91 line_p++;
92 }
93 //upi_log("line_element: %s", line_element);
94 mp_str_len = strlen(parti_mp);
95 if (mp_str_len <= strlen(line_element)) {
96 if (strncmp(line_element + strlen(line_element) - mp_str_len, parti_mp, mp_str_len) == 0) {
97 found = 1;
98 break;
99 }
100 }
101 }
102 if (found == 0) {
103 printf("fs_check did not find any mount info about %s\n", parti_mp);
104 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check did not find any mount info about %s\n", parti_mp);
105 goto error;
106 }
107 if (NULL != fp)
108 fclose(fp);
109 return 0;
110error:
111 if (fp != NULL)
112 fclose(fp);
113 return -1;
114}
115
116int mtd_find(const char *i_parti_name, char *o_mtd_path, device_type_t device_type, unsigned int o_mtd_path_len)
117{
118 FILE *fd_mtd = 0;
119 char buf[128];
120 char *line_str;
121
122 if (!o_mtd_path_len)
123 return -1;
124
125 fd_mtd = fopen("/proc/mtd", "r+");
126 if (NULL == fd_mtd) {
127 printf("fs_check open file error:%s", strerror(errno));
128 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check open file error:%s", strerror(errno));
129 goto error0;
130 }
131 //printf("fs_check partition name:%s\n", i_parti_name);
132
133 while (1) {
134 int matches = 0;
135 char mtdname[64] = {0};
136 int mtdnum = 0;
137 unsigned int mtdsize, mtderasesize;
138 memset(buf, 0x00, sizeof(buf));
139 line_str = fgets(buf, sizeof(buf), fd_mtd);
140
141 if (NULL == line_str) {
142 printf("fs_check get info from mtd error:%s\n", strerror(errno));
143 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check get info from mtd error:%s\n", strerror(errno));
144 goto error1;
145 }
146 //mtd5: 00100000 00020000 "fotaflag"
147 matches = sscanf(buf, "mtd%d: %x %x \"%63[^\"]",
148 &mtdnum, &mtdsize, &mtderasesize, mtdname);
149 mtdname[63] = '\0';
150
151 if ((matches == 4) && (strcmp(mtdname, i_parti_name) == 0)) {
152 memset(o_mtd_path, 0x00, o_mtd_path_len);
153 if (device_type == DEVICE_MTD_BLOCK) {
154 snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtdblock%d", mtdnum);
155 } else if (device_type == DEVICE_MTD) {
156 snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtd%d", mtdnum);
157 } else if (device_type == DEVICE_ZFTL) {
158 snprintf(o_mtd_path, o_mtd_path_len, "/dev/zftl%d", mtdnum);
159 } else {
160 printf("fs_check unknown device type %d\n", device_type);
161 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check unknown device type %d\n", device_type);
162 goto error1;
163 }
164 //printf("fs_check o_mtd_path=[%s]\n", o_mtd_path);
165 break;
166 }
167
168 }
169 fclose(fd_mtd);
170 return 0;
171
172error1:
173 fclose(fd_mtd);
174error0:
175 return -1;
176}
177
178int system_exec_status(int status)
179{
180 if (-1 == status)
181 return SYSTEM_EXEC_FAIL;
182
183 if (!WIFEXITED(status))
184 return SYSTEM_EXEC_FAIL;
185
186 if (WEXITSTATUS(status))
187 return SYSTEM_EXEC_FAIL;
188
189 return SYSTEM_EXEC_SUCC;
190}
191
192int get_ubifs_device_num(int recv_mtdnum)
193{
194 int vol_num = -1;
195 int fd_ubi = -1;
196 int ret = -1;
197 int mytmp = 0;
198 int add_len = 10;
199
200 struct stat st = {0};
201
xf.li84027492024-04-09 00:17:51 -0700202 char read_buf[BUF_MAX_LEN] = {0};
203 char ubidev_path[MAX_PATH] = {0};
lh9ed821d2023-04-07 01:36:19 -0700204
205 for (; mytmp < UBI_DEV_MAX_NUM; mytmp++)
206 {
207 snprintf(ubidev_path, sizeof(ubidev_path), "/sys/devices/virtual/ubi/ubi%d", mytmp);
208 ret = stat(ubidev_path, &st);
209 if (ret < 0) {
210 printf("fs_check get stat info from error:%s\n", strerror(errno));
211 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check get stat info from error:%s\n", strerror(errno));
212 break;
213 }
214 if (S_ISDIR(st.st_mode)) {
215 strncat(ubidev_path, "/mtd_num", add_len);
216 fd_ubi = open(ubidev_path, O_RDONLY);
217 if (fd_ubi < 0) {
218 printf("fs_check open file error:%s", strerror(errno));
219 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check open file error:%s", strerror(errno));
220 break;
221 }
222 memset(read_buf, 0, sizeof(read_buf));
223 ret = read(fd_ubi, read_buf, sizeof(read_buf));
224 if (ret < 0) {
225 printf("fs_check get info from ubi error:%s\n", strerror(errno));
226 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check get info from ubi error:%s\n", strerror(errno));
227 close(fd_ubi);
228 break;
229 }
230 if (atoi(read_buf) == recv_mtdnum) {
231 vol_num = mytmp;
232 close(fd_ubi);
233 break;
234 }
235 close(fd_ubi);
236 }
237 }
238
239 return vol_num;
240}
241
242
243/**************************************************************************
244* º¯ÊýÃû³Æ£º read_file
245* ¹¦ÄÜÃèÊö£º ¶ÁÈ¡Îļþ
246* ²ÎÊý˵Ã÷£º (IN)
247* p_file: Îļþ
248* len: ³¤¶È
249* (OUT)
xf.li84027492024-04-09 00:17:51 -0700250* ·µ »Ø Öµ Îļþ¶ÁÈ¡³É¹¦Çé¿ö£¨1¡¢·µ»Ø·ÇNULL£»2¡¢¿ÕÎļþ·µ»ØNULLÇÒlenµÈÓÚ0£©£¬
251 Îļþ¶Áȡʧ°ÜÇé¿ö£º·µ»ØNULL£¬ÇÒlen´óÓÚ0
lh9ed821d2023-04-07 01:36:19 -0700252* ÆäËü˵Ã÷£º·µ»ØÖµµ÷ÓÃÕßÊÍ·Å
253**************************************************************************/
xf.li84027492024-04-09 00:17:51 -0700254static char *read_file(const char *p_file, unsigned int *len)
lh9ed821d2023-04-07 01:36:19 -0700255{
256 FILE * fd = 0;
257 struct stat buf = {0};
xf.li84027492024-04-09 00:17:51 -0700258 char * p_buf = NULL;
259 size_t read_size;
lh9ed821d2023-04-07 01:36:19 -0700260
xf.li84027492024-04-09 00:17:51 -0700261 *len = 0xffff; /* init file length > 0 for error */
lh9ed821d2023-04-07 01:36:19 -0700262 if(p_file == NULL)
263 return NULL;
264
265 if(stat(p_file, &buf) < 0)
266 {
267 printf("read_file stat %s failed\n", p_file);
268 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "read_file stat %s failed\n", p_file);
269 return NULL;
270 }
xf.li84027492024-04-09 00:17:51 -0700271 if (buf.st_size == 0)
272 {
273 *len = 0; /* empty file */
274 return NULL;
275 }
276 *len = buf.st_size;
277
278 fd = fopen(p_file, "rb");
lh9ed821d2023-04-07 01:36:19 -0700279 if(!fd)
280 {
281 printf("read_file open %s fail\n", p_file);
282 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "read_file open %s fail\n", p_file);
283 return NULL;
284 }
285
286 p_buf = (char *)malloc(buf.st_size);
287 if (p_buf == NULL)
288 {
289 printf("read_file malloc fail\n");
290 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "read_file malloc fail\n");
291 fclose(fd);
292 return NULL;
293 }
294
xf.li84027492024-04-09 00:17:51 -0700295 read_size = fread(p_buf,1, buf.st_size,fd);
296 if (read_size != buf.st_size)
lh9ed821d2023-04-07 01:36:19 -0700297 {
298 printf("read_file fread %s fail\n", p_file);
299 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "read_file fread %s fail\n", p_file);
300 fclose(fd);
301 free(p_buf);
302 return NULL;
303 }
xf.li84027492024-04-09 00:17:51 -0700304 if (ferror(fd))
305 {
306 clearerr(fd);
307 printf("read_file ferror %s fail\n", p_file);
308 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "read_file ferror %s fail\n", p_file);
309 fclose(fd);
310 free(p_buf);
311 return NULL;
312 }
lh9ed821d2023-04-07 01:36:19 -0700313 fclose(fd);
xf.li84027492024-04-09 00:17:51 -0700314
lh9ed821d2023-04-07 01:36:19 -0700315 return p_buf;
316}
317
318/**************************************************************************
319* º¯ÊýÃû³Æ£º compare_file
320* ¹¦ÄÜÃèÊö£º ±È½ÏÄ¿±êÎļþºÍÔ´ÎļþÊÇ·ñÒ»Ñù
321* ²ÎÊý˵Ã÷£º (IN)
322* p_dst_file: Ä¿±êÎļþ
323* p_src_file: Ô´Îļþ
324* (OUT)
325* ·µ »Ø Öµ£ºÎļþÒ»Ñù·µ»Ø0, ·ñÔò·µ»Ø-1
326* ÆäËü˵Ã÷£º
327**************************************************************************/
328static int compare_file(const char *p_dst_file, const char *p_src_file)
329{
xf.li84027492024-04-09 00:17:51 -0700330 char *p_dst;
331 char *p_src;
lh9ed821d2023-04-07 01:36:19 -0700332 unsigned int dst_len;
333 unsigned int src_len;
xf.li84027492024-04-09 00:17:51 -0700334 int ret = 0;
lh9ed821d2023-04-07 01:36:19 -0700335
336 if(p_dst_file == NULL || p_src_file == NULL)
337 return -1;
338
339 p_dst = read_file(p_dst_file, &dst_len);
lh9ed821d2023-04-07 01:36:19 -0700340 p_src = read_file(p_src_file, &src_len);
xf.li84027492024-04-09 00:17:51 -0700341 if ((dst_len == 0) && (src_len == 0))
lh9ed821d2023-04-07 01:36:19 -0700342 {
xf.li84027492024-04-09 00:17:51 -0700343 assert(p_dst == NULL);
344 assert(p_src == NULL);
345 return 0; //both empty file
lh9ed821d2023-04-07 01:36:19 -0700346 }
xf.li84027492024-04-09 00:17:51 -0700347
lh9ed821d2023-04-07 01:36:19 -0700348 if(dst_len != src_len)
349 {
lh9ed821d2023-04-07 01:36:19 -0700350 printf("compare_file size dstbuf = %d, srcbuf = %d\n", dst_len, src_len);
351 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "compare_file size dstbuf = %d, srcbuf = %d\n", dst_len, src_len);
xf.li84027492024-04-09 00:17:51 -0700352 ret = -1;
353 goto out;
lh9ed821d2023-04-07 01:36:19 -0700354 }
355
356 if(memcmp(p_src, p_dst, src_len) != 0)
357 {
lh9ed821d2023-04-07 01:36:19 -0700358 printf("compare_file memcmp not same\n");
359 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "compare_file memcmp not same\n");
xf.li84027492024-04-09 00:17:51 -0700360 ret = -1;
361 goto out;
lh9ed821d2023-04-07 01:36:19 -0700362 }
xf.li84027492024-04-09 00:17:51 -0700363 ret = 0;
lh9ed821d2023-04-07 01:36:19 -0700364
xf.li84027492024-04-09 00:17:51 -0700365out:
366 if (p_src)
367 {
368 free(p_src);
369 }
370 if (p_dst)
371 {
372 free(p_dst);
373 }
lh9ed821d2023-04-07 01:36:19 -0700374
xf.li84027492024-04-09 00:17:51 -0700375 return ret;
lh9ed821d2023-04-07 01:36:19 -0700376}
377
378/**************************************************************************
379* º¯ÊýÃû³Æ£º check_files_access
380* ¹¦ÄÜÃèÊö£º ¼ì²éuserdataÎļþϵͳϵÄÎļþÊÇ·ñ´æÔÚÇÒÓжÁдȨÏÞ
381* ²ÎÊý˵Ã÷£º ÎÞ
382* ·µ »Ø Öµ£º¼ì²éÕý³£·µ»Ø0, ·ñÔò·µ»Ø-1£¬½øÐÐuserdata·ÖÇøµÄ²Á³ý
383* ÆäËü˵Ã÷£º
384**************************************************************************/
385static int check_files_access()
386{
387 if(access(NV_FS_RW_MAIN_PATH,W_OK | R_OK) < 0)
388 {
389 printf("fs_check file: %s permission loss \n",NV_FS_RW_MAIN_PATH);
390 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_MAIN_PATH);
391 return -1;//²é¿´rw_workÎļþÊÇ·ñ¿É¶Áд
392 }
393 if(access(NV_FS_RW_BACKUP_PATH, W_OK | R_OK) < 0)
394 {
395 printf("fs_check file: %s permission loss \n",NV_FS_RW_BACKUP_PATH);
396 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_BACKUP_PATH);
397 return -1;//²é¿´rw_backupÎļþÊÇ·ñ¿É¶Áд
398 }
399 if(access(NV_FS_FAC_SYMBOL_PATH, W_OK | R_OK) < 0)
400 {
401 printf("fs_check file: %s permission loss \n",NV_FS_FAC_SYMBOL_PATH);
402 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_FAC_SYMBOL_PATH);
403 return -1;//²é¿´fac_flagÎļþÊÇ·ñ¿É¶Áд
404 }
405 if(access(NV_FS_RW_MAIN_SYMBOL_PATH, W_OK | R_OK) < 0)
406 {
407 printf("fs_check file: %s permission loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);
408 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);
409 return -1;//²é¿´work_flagÎļþÊÇ·ñ¿É¶Áд
410 }
411 if(access(NV_FS_RW_BACKUP_SYMBOL_PATH, W_OK | R_OK) < 0)
412 {
413 printf("fs_check file: %s permission loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);
414 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);
415 return -1;//²é¿´backup_flagÎļþÊÇ·ñ¿É¶Áд
416 }
417 if(access(NV_FS_RW_AP_NV_MAIN_PATH, W_OK | R_OK) < 0)
418 {
419 printf("fs_check file: %s permission loss \n",NV_FS_RW_AP_NV_MAIN_PATH);
420 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_AP_NV_MAIN_PATH);
421 return -1;//²é¿´apϹ¤×÷nvÎļþÊÇ·ñ¿É¶Áд
422 }
423 if(access(NV_FS_RW_AP_NV_BACKUP_PATH, W_OK | R_OK) < 0)
424 {
425 printf("fs_check file: %s permission loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);
426 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);
427 return -1;//²é¿´apϱ¸·ÝnvÎļþÊÇ·ñ¿É¶Áд
428 }
429 return 0;
430}
xf.li84027492024-04-09 00:17:51 -0700431
432/* read success return 0, file not exist return 0, other return -1*/
433static int file_read_test(const char *filename)
434{
435 char *buf;
436 size_t read_size;
437 struct stat file_stat;
438 int result = stat(filename, &file_stat);
439
440 if (result == -1 && errno == ENOENT)
441 {
442 return 0; /* file not exist */
443 }
444 else
445 {
446 if (result != 0)
447 {
448 return -1;
449 }
450 }
451
452 buf = read_file(filename, &read_size);
453 if (buf == NULL)
454 {
455 if (read_size == 0)
456 {
457 return 0; /* empty file */
458 }
459 else
460 {
461 return -1; /* file read error */
462 }
463 }
464 else
465 {
466 free(buf);
467 return 0;
468 }
469}
470
471/**************************************************************************
472* º¯ÊýÃû³Æ£º check_files_read
473* ¹¦ÄÜÃèÊö£º ¼ì²éuserdataÎļþϵͳϵÄÎļþreadÊÇ·ñÕý³£
474* ²ÎÊý˵Ã÷£º ÎÞ
475* ·µ »Ø Öµ£º¼ì²éÕý³£·µ»Ø0, ·ñÔò·µ»Ø-1£¬½øÐÐuserdata·ÖÇøµÄ²Á³ý
476* ÆäËü˵Ã÷£º
477**************************************************************************/
478static int check_files_read()
479{
480 if(file_read_test(NV_FS_RW_MAIN_PATH) < 0)
481 {
482 printf("fs_check file: %s read loss \n",NV_FS_RW_MAIN_PATH);
483 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_MAIN_PATH);
484 return -1;//²é¿´rw_workÎļþÊÇ·ñ¿É¶Á
485 }
486 if(file_read_test(NV_FS_RW_BACKUP_PATH) < 0)
487 {
488 printf("fs_check file: %s read loss \n",NV_FS_RW_BACKUP_PATH);
489 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_BACKUP_PATH);
490 return -1;//²é¿´rw_backupÎļþÊÇ·ñ¿É¶Á
491 }
492 if(file_read_test(NV_FS_FAC_SYMBOL_PATH) < 0)
493 {
494 printf("fs_check file: %s read loss \n",NV_FS_FAC_SYMBOL_PATH);
495 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_FAC_SYMBOL_PATH);
496 return -1;//²é¿´fac_flagÎļþÊÇ·ñ¿É¶Á
497 }
498 if(file_read_test(NV_FS_RW_MAIN_SYMBOL_PATH) < 0)
499 {
500 printf("fs_check file: %s read loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);
501 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);
502 return -1;//²é¿´work_flagÎļþÊÇ·ñ¿É¶Á
503 }
504 if(file_read_test(NV_FS_RW_BACKUP_SYMBOL_PATH) < 0)
505 {
506 printf("fs_check file: %s read loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);
507 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);
508 return -1;//²é¿´backup_flagÎļþÊÇ·ñ¿É¶Á
509 }
510 if(file_read_test(NV_FS_RW_AP_NV_MAIN_PATH) < 0)
511 {
512 printf("fs_check file: %s read loss \n",NV_FS_RW_AP_NV_MAIN_PATH);
513 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_AP_NV_MAIN_PATH);
514 return -1;//²é¿´apϹ¤×÷nvÎļþÊÇ·ñ¿É¶Á
515 }
516 if(file_read_test(NV_FS_RW_AP_NV_BACKUP_PATH) < 0)
517 {
518 printf("fs_check file: %s read loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);
519 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);
520 return -1;//²é¿´apϱ¸·ÝnvÎļþÊÇ·ñ¿É¶Á
521 }
522
523 return 0;
524}
525
lh9ed821d2023-04-07 01:36:19 -0700526/**************************************************************************
527* º¯ÊýÃû³Æ£º check_userdata_is_space_enough
528* ¹¦ÄÜÃèÊö£º ¼ì²éuserdata·ÖÇøÏÂÃæµÄ¿Õ¼äÊÇ·ñ×ã¹»£¬²»×ãʱÐèÒªÖØÐ»ָ´userdata·ÖÇø
529* ²ÎÊý˵Ã÷£º (IN)
530* (OUT)
531* ·µ »Ø Öµ£º¼ì²é¿Õ¼ä×ã¹»·µ»Ø0, ¿Õ¼ä²»×㣬ÐèÒªÖØÐ»ָ´·ÖÇø·µ»Ø-1
532* ÆäËü˵Ã÷£º
533**************************************************************************/
534int check_userdata_space_enough()
535{
536 int fd = 0;
537 char fsckname[] = "/etc_rw/fscheck";
538 char buf[] = "filesystem checking";
539 int len = strlen(buf);
540 char *ptr = buf;
541 int res = 0;
542 int ret = 0;
543
544#if 0
545 struct statfs diskinfo;
546 statfs("/mnt/userdata", &diskinfo);
547
xf.li84027492024-04-09 00:17:51 -0700548 printf("fs_check f_bsize = %d, f_blocks = %d, f_bfree = %d, f_bavail = %d, f_files = %d, , f_ffree = %d\n", \
549 diskinfo.f_bsize, diskinfo.f_blocks, diskinfo.f_bfree, diskinfo.f_bavail, diskinfo.f_files, diskinfo.f_ffree);
lh9ed821d2023-04-07 01:36:19 -0700550
551 //»ñȡʣÓà¿Õ¼äÊÇ·ñ´óÓÚµÈÓÚÁ½¸öblock
552 if ((diskinfo.f_bsize * diskinfo.f_bfree < CONFIG_BLOCK_SIZE * USERDATA_RESET_FREE_BLOCK_LEVEL) ||
553 (diskinfo.f_bsize * diskinfo.f_bavail < CONFIG_BLOCK_SIZE * USERDATA_RESET_FREE_BLOCK_LEVEL))
554 return -1;
555#endif
556
557 fd = open(fsckname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
558 if (fd < 0)
559 {
560 printf("open %s failed errno %d\n", fsckname, errno);
561 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "open %s failed errno %d\n", fsckname, errno);
562 return -1;
563 }
564 while (len > 0)
565 {
566 res = write(fd, ptr, len);
567 if (res < 0)
568 {
569 if (errno == EINTR)
570 {
571 res = 0;
572 }
573 else
574 {
575 printf("write %s failed errno %d\n", fsckname, errno);
576 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "write %s failed errno %d\n", fsckname, errno);
577 ret = -1;
578 break;
579 }
580 }
581 ptr += res;
582 len -= res;
583 }
584
585
586 if (close(fd) < 0)
587 {
588 printf("close %s failed errno %d\n", fsckname, errno);
589 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "close %s failed errno %d\n", fsckname, errno);
590 return -1;
591 }
592 return ret;
593}
594
595/**************************************************************************
596* º¯ÊýÃû³Æ£º check_userdata_is_normal
597* ¹¦ÄÜÃèÊö£º ¼ì²éuserdata·ÖÇøÏÂÃæµÄÎļþÊÇ·ñÕý³££¬Èç¹û´æÔÚÒì³££¬ÔòÐèÒªÖØÐ»ָ´userdata·ÖÇø
598* ²ÎÊý˵Ã÷£º (IN)
599* dst_file: Ä¿±êÎļþ
600* src_file: Ô´Îļþ
601* (OUT)
602* ·µ »Ø Öµ£º¼ì²éÕý³£·µ»Ø0, ÐèÒªÖØÐ»ָ´·ÖÇø·µ»Ø-1
603* ÆäËü˵Ã÷£º
604**************************************************************************/
605int check_userdata_is_normal()
606{
607 struct stat fac_nv_buf = {0};
608 struct stat work_buf = {0};
609 struct stat backup_buf = {0};
610
611 if (check_userdata_space_enough() < 0)
612 return -1;
613 if (access(NV_FS_RW_TOP_PATH, F_OK) != 0)
614 {
615 if (mkdir(NV_FS_RW_TOP_PATH, 0755) != 0)
616 {
617 printf("fs_check access and mkdir %s failed\n", NV_FS_RW_TOP_PATH);
618 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check access and mkdir %s failed\n", NV_FS_RW_TOP_PATH);
619 return -1;
620 }
621 }
622 /*userdata·ÖÇøÊÇ·ñ¼ì²éÐèÒªÂú×ãÒÔÏÂÁ½¸öÌõ¼þ£º
623 *1)²é¿´userdata·ÖÇøÏÂÃæµÄnvrwall.hashÊÇ·ñ´æÔÚ£¬²»´æÔÚ±íʾÊǸÕÉÕÍê°æ±¾£¬Õâʱºò²»¼ì²é
624 *2)imageϵÄhashºÍpsnvĿ¼ÏÂÃæµÄnvrwall.hashÊÇ·ñÒ»Ö£¬Èç¹ûÒ»Ö½øÐмì²é£¬·ñÔò²»½øÐмì²é£¨¹ýÂËfotaÉý¼¶£©
625 */
626 if(access(NV_FS_RW_HASH_WORK_PATH, F_OK) < 0)
627 return 0;
xf.li84027492024-04-09 00:17:51 -0700628
629 if (check_files_read() < 0)
630 {
631 return -1;//userdata·ÖÇøÏµÄÎļþ¶ÁÊÇ·ñÕý³£
632 }
lh9ed821d2023-04-07 01:36:19 -0700633 if(compare_file(NV_FS_RW_HASH_FAC_PATH, NV_FS_RW_HASH_WORK_PATH) == 0)
634 {
635 if(check_files_access() < 0)
636 {
637 return -1;//userdata·ÖÇøÏµÄÎļþ·ÃÎÊȨÏÞÒì³£ÐèÒª»Ö¸´
638 }
639 if(stat(NV_FS_FAC_MAIN_PATH, &fac_nv_buf) < 0)
640 {
641 return 0;
642 }
643
644 if(stat(NV_FS_RW_MAIN_PATH, &work_buf) < 0)
645 {
646 printf("fs_check stat %s failed\n",NV_FS_RW_MAIN_PATH);
647 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check stat %s failed\n",NV_FS_RW_MAIN_PATH);
648 return -1;
649 }
650 if(stat(NV_FS_RW_BACKUP_PATH, &backup_buf) < 0)
651 {
652 printf("fs_check stat %s failed\n",NV_FS_RW_BACKUP_PATH);
653 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check stat %s failed\n",NV_FS_RW_BACKUP_PATH);
654 return -1;
655 }
656 if(work_buf.st_size < fac_nv_buf.st_size || backup_buf.st_size < fac_nv_buf.st_size)
657 {
658 printf("fs_check rw_backup or rw_work file corrupted\n");
659 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check rw_backup or rw_work file corrupted\n");
660 return -1;//userdata·ÖÇøÏµĹ¤×÷ÇønvºÍ±¸·ÝÇønv±È³ö³§ÇønvС£¬ÐèÒª»Ö¸´
661 }
662 }
663
664 return 0;
665
666}
667
668int mount_fs_partition(struct mtd_fs *p_fs)
669{
670 int ret = -1;
671 int ubi_num = 0;
672 int mtd_blk_num = 0;
673 char mount_cmd[MAX_PATH] = {0};
674 char mtd_path[MAX_PATH] = {0};
675 char attach_cmd[MAX_PATH] = {0};
676
677 if (NULL == p_fs->patition_name || NULL == p_fs->mount_point || NULL == p_fs->fs_type)
678 return -1;
679
680 //printf("fs_check i_parti_name=%s, parti_mp=%s, parti_mt=%s\n", p_fs->patition_name, p_fs->mount_point, p_fs->fs_type);
681
682 if (strcmp(p_fs->patition_name, "cpfs") == 0) {
683 ret = mtd_find(p_fs->patition_name, mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
684 if (ret < 0) {
685 printf("fs_check partition name is not find\n");
686 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check partition name is not find\n");
687 return -1;
688 }
689 snprintf(mount_cmd, sizeof(mount_cmd), "%s/bin/mount -t yaffs2 -o \"inband-tags\" %s %s", g_path_prefix,mtd_path, p_fs->mount_point);
690 } else if (strcmp(p_fs->fs_type, "jffs2") == 0) {
691 ret = mtd_find(p_fs->patition_name, mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
692 if (ret < 0) {
693 printf("fs_check partition name is not find\n");
694 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check partition name is not find\n");
695 return -1;
696 }
697 snprintf(mount_cmd, sizeof(mount_cmd), "%s/bin/mount -t jffs2 %s %s %s", g_path_prefix,p_fs->mount_opt ,mtd_path, p_fs->mount_point);
698 } else if (strcmp(p_fs->fs_type, "ubifs") == 0) {
699 ret = mtd_find(p_fs->patition_name, mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
700 if (ret < 0) {
701 printf("fs_check partition name is not find\n");
702 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check partition name is not find\n");
703 return -1;
704 }
705 sscanf(mtd_path, "/dev/mtdblock%d", &mtd_blk_num);
706 snprintf(attach_cmd, sizeof(attach_cmd), "%s/usr/sbin/ubiattach /dev/ubi_ctrl -m %d", g_path_prefix,mtd_blk_num);
707
708 ret = system_exec_status(zxic_system(attach_cmd));
709 if (ret == SYSTEM_EXEC_FAIL) {
710 printf("fs_check: %s fail\n",attach_cmd);
711 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check: %s fail\n",attach_cmd);
712 return -1;
713 }
714
715 ubi_num = get_ubifs_device_num(mtd_blk_num);
716 if (ubi_num < 0) {
717 printf("fs_check ubi_num not match\n");
718 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check ubi_num not match\n");
719 return -1;
720 }
721 snprintf(mount_cmd, sizeof(mount_cmd), "%s/bin/mount -t ubifs -o rw ubi%d_0 %s", g_path_prefix,ubi_num, p_fs->mount_point);
722 } else {
723 printf("fs_check unknown mount type: %s\n", p_fs->fs_type);
724 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check unknown mount type: %s\n", p_fs->fs_type);
725 return -1;
726 }
727
728 ret = zxic_system(mount_cmd);
729
730 if (check_mount_result(p_fs->mount_point) < 0) {
731 printf("fs_check : %s fail\n", mount_cmd);
732 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check : %s fail\n", mount_cmd);
733 return -1;
734 }
735 return 0;
736}
737
738int unmount_fs_partition(struct mtd_fs *p_fs)
739{
740 int ret = -1;
741 int ubi_num = 0;
742 int mtd_blk_num = 0;
743 char umount_cmd[MAX_PATH] = {0};
744 char mtd_path[MAX_PATH] = {0};
745 char detach_cmd[MAX_PATH] = {0};
746
747 if (NULL == p_fs->patition_name || NULL == p_fs->mount_point || NULL == p_fs->fs_type)
748 return -1;
749
750 if (strcmp(p_fs->patition_name, "cpfs") == 0) {
751 snprintf(umount_cmd, sizeof(umount_cmd), "%s/bin/umount -f %s ", g_path_prefix,p_fs->mount_point);
752 } else if (strcmp(p_fs->fs_type, "jffs2") == 0) {
753 snprintf(umount_cmd, sizeof(umount_cmd), "%s/bin/umount -f %s", g_path_prefix,p_fs->mount_point);
754 } else if (strcmp(p_fs->fs_type, "ubifs") == 0) {
755
756 snprintf(umount_cmd, sizeof(umount_cmd), "%s/bin/umount -f %s", g_path_prefix,p_fs->mount_point);
757 zxic_system(umount_cmd);
758 printf("fs_check umount : %s\n", umount_cmd);
759
760 ret = mtd_find(p_fs->patition_name, mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
761 if (ret < 0) {
762 printf("fs_check partition name is not find\n");
763 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check partition name is not find\n");
764 return -1;
765 }
766 sscanf(mtd_path, "/dev/mtdblock%d", &mtd_blk_num);
767
768 ubi_num = get_ubifs_device_num(mtd_blk_num);
769
770 snprintf(detach_cmd, sizeof(detach_cmd), "%s/usr/sbin/ubidetach /dev/ubi_ctrl -d %d", g_path_prefix,ubi_num);
771
772 ret = system_exec_status(zxic_system(detach_cmd));
773 if (ret == SYSTEM_EXEC_FAIL) {
774 printf("fs_check: %s fail\n",detach_cmd);
775 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check: %s fail\n",detach_cmd);
776 return -1;
777 }
778
779 return 0;
780 } else {
781 printf("fs_check unknown umount type: %s\n", p_fs->fs_type);
782 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check unknown umount type: %s\n", p_fs->fs_type);
783 return -1;
784 }
785 printf("fs_check umount : %s\n", umount_cmd);
786 ret = zxic_system(umount_cmd);
787
788 return 0;
789}
790
791int mtd_erase_partition(const char* partition_name)
792{
793 int ret = 0;
794 char mtd_path[MAX_PATH] = {0};
795 int fd_mtd = -1;
796
797 struct mtd_info_user meminfo = {0};
798 struct erase_info_user64 erase_info = {0};
799
800 if (NULL == partition_name) {
801 return -1;
802 }
803 ret = mtd_find(partition_name, mtd_path, DEVICE_MTD, MAX_PATH);
804 if (ret < 0) {
805 printf("fs_check mtd_find %s failed\n", partition_name);
806 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd_find %s failed\n", partition_name);
807 ret = -1;
808 goto out;
809 }
810 fd_mtd = open(mtd_path, O_RDWR);
811 if (fd_mtd < 0) {
812 printf("fs_check open %s error, %s\n", partition_name, strerror(errno));
813 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check open %s error, %s\n", partition_name, strerror(errno));
814 ret = -1;
815 goto out;
816 }
817 if (ioctl(fd_mtd, MEMGETINFO, &meminfo) != 0) {
818 printf("fs_check get %s info error, %s\n", partition_name, strerror(errno));
819 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check get %s info error, %s\n", partition_name, strerror(errno));
820 ret = -1;
821 goto out;
822 }
823 erase_info.length = meminfo.erasesize;
824 for (erase_info.start = 0; erase_info.start < meminfo.size; erase_info.start += meminfo.erasesize) {
825 if (ioctl(fd_mtd, MEMGETBADBLOCK, &(erase_info.start)) > 0) {
826 printf("fs_check mtd, not erasing bad block at 0x%llx\n", erase_info.start);
827 continue;
828 }
829 if (ioctl(fd_mtd, MEMERASE64, &erase_info) < 0) {
830 printf("fs_check mtd, erasing failure at 0x%llx\n", erase_info.start);
831 }
832 }
833 ret = 0;
834out:
835 if (fd_mtd >= 0) {
836 close(fd_mtd);
837 }
838 return ret;
839}
840
841int mtd_write_partition(const char* partition_name, const char* image_file)
842{
843 int ret = 0;
844 char mtd_path[MAX_PATH] = {0};
845 int fd_mtd = -1;
846 struct mtd_info_user meminfo = {0};
847
848 long long index = 0;
849 int len = 0;
850 FILE * fp = NULL;
851 char * buf = NULL;
852 struct stat statbuff = {0};
853
854 if (NULL == partition_name || NULL == image_file)
855 return -1;
856
857 ret = mtd_find(partition_name, mtd_path, DEVICE_MTD, MAX_PATH);
858 if (ret < 0) {
859 printf("fs_check mtd_find %s failed\n", partition_name);
860 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd_find %s failed\n", partition_name);
861 ret = -1;
862 goto out;
863 }
864 fd_mtd = open(mtd_path, O_RDWR);
865 if (fd_mtd < 0) {
866 printf("fs_check open %s error, %s\n", partition_name, strerror(errno));
867 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check open %s error, %s\n", partition_name, strerror(errno));
868 ret = -1;
869 goto out;
870 }
871 if (ioctl(fd_mtd, MEMGETINFO, &meminfo) != 0) {
872 printf("fs_check get %s info error, %s\n", partition_name, strerror(errno));
873 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check get %s info error, %s\n", partition_name, strerror(errno));
874 ret = -1;
875 goto out;
876 }
877
878 if(stat(image_file, &statbuff) < 0)
879 {
880 printf("fs_check stat %s failed\n", image_file);
881 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check stat %s failed\n", image_file);
882 ret = -1;
883 goto out;
884 }
885
886 fp = fopen(image_file, "ro");
887 if (!fp)
888 {
889 printf("fs_check fopen %s failed\n", image_file);
890 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check fopen %s failed\n", image_file);
891 ret = -1;
892 goto out;
893 }
894
895 buf = (char *)malloc(meminfo.erasesize);
896 if(!buf)
897 {
898 printf("fs_check malloc failed\n");
899 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check malloc failed\n");
900 ret = -1;
901 goto out;
902 }
903
904 for (index = 0; index < meminfo.size && len < statbuff.st_size; index += meminfo.erasesize)
905 {
906 if (ioctl(fd_mtd, MEMGETBADBLOCK, &index) > 0)
907 {
908 printf("fs_check mtd, not erasing bad block at %lld\n", index);
909 continue;
910 }
911
xf.li84027492024-04-09 00:17:51 -0700912 memset(buf, 0xff, meminfo.erasesize);
lh9ed821d2023-04-07 01:36:19 -0700913 ret = fread(buf, 1, meminfo.erasesize, fp);
914 if(ret < 0)
915 {
916 printf("fs_check mtd, fread error = %d!\n", ret);
917 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd, fread error = %d!\n", ret);
918 ret = -1;
919 goto out;
920 }
921
922 ret = lseek(fd_mtd, (long)index, SEEK_SET);
923 if(ret < 0)
924 {
925 printf("fs_check mtd, lseek error = %s!\n", strerror(errno));
926 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd, lseek error = %s!\n", strerror(errno));
927 ret = -1;
928 goto out;
929 }
xf.li742dd022023-06-08 01:43:32 -0700930 ret = write(fd_mtd, buf, (size_t)meminfo.erasesize);
931 if (ret < 0 || ret != meminfo.erasesize)
lh9ed821d2023-04-07 01:36:19 -0700932 {
xf.li742dd022023-06-08 01:43:32 -0700933 printf("fs_check mtd, write %s error = %d!\n", partition_name, ret);
934 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd, write %s error = %d!\n", partition_name, ret);
lh9ed821d2023-04-07 01:36:19 -0700935 ret = -1;
936 goto out;
937 }
938 len += meminfo.erasesize;
939 }
940 if (len < statbuff.st_size)
941 {
942 printf("fs_check mtd, No space left,writelen=%d, filesize=%d\n",len,statbuff.st_size);
943 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd, No space left,writelen=%d, filesize=%d\n",len,statbuff.st_size);
944 }
945
946 ret = 0;
947out:
948 if (fd_mtd >= 0)
949 close(fd_mtd);
950
951 if (buf != NULL) {
952 memset(buf, 0, meminfo.erasesize);
953 free(buf);
954 }
955
956 if (fp != NULL)
957 fclose(fp);
958
959 return ret;
960}
961
962
963