blob: 4b0322db0490ea3452ead05543d69fb601ef2afe [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.lif2330622024-05-15 18:17:18 -0700341 if (p_dst == NULL && p_src == NULL)
lh9ed821d2023-04-07 01:36:19 -0700342 {
xf.lif2330622024-05-15 18:17:18 -0700343 assert(dst_len == 0);
344 assert(src_len == 0);
xf.li84027492024-04-09 00:17:51 -0700345 return 0; //both empty file
lh9ed821d2023-04-07 01:36:19 -0700346 }
xf.lif2330622024-05-15 18:17:18 -0700347 else if (p_dst == NULL || p_src == NULL)
lh9ed821d2023-04-07 01:36:19 -0700348 {
lh9ed821d2023-04-07 01:36:19 -0700349 printf("compare_file size dstbuf = %d, srcbuf = %d\n", dst_len, src_len);
350 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 -0700351 ret = -1;
352 goto out;
lh9ed821d2023-04-07 01:36:19 -0700353 }
xf.lif2330622024-05-15 18:17:18 -0700354 else
lh9ed821d2023-04-07 01:36:19 -0700355 {
xf.lif2330622024-05-15 18:17:18 -0700356 if(dst_len != src_len)
357 {
358 printf("compare_file size dstbuf = %d, srcbuf = %d\n", dst_len, src_len);
359 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "compare_file size dstbuf = %d, srcbuf = %d\n", dst_len, src_len);
360 ret = -1;
361 goto out;
362 }
363
364 if(memcmp(p_src, p_dst, src_len) != 0)
365 {
366 printf("compare_file memcmp not same\n");
367 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "compare_file memcmp not same\n");
368 ret = -1;
369 goto out;
370 }
371 ret = 0;
lh9ed821d2023-04-07 01:36:19 -0700372 }
373
xf.li84027492024-04-09 00:17:51 -0700374out:
375 if (p_src)
376 {
377 free(p_src);
378 }
379 if (p_dst)
380 {
381 free(p_dst);
382 }
lh9ed821d2023-04-07 01:36:19 -0700383
xf.li84027492024-04-09 00:17:51 -0700384 return ret;
lh9ed821d2023-04-07 01:36:19 -0700385}
386
387/**************************************************************************
388* º¯ÊýÃû³Æ£º check_files_access
389* ¹¦ÄÜÃèÊö£º ¼ì²éuserdataÎļþϵͳϵÄÎļþÊÇ·ñ´æÔÚÇÒÓжÁдȨÏÞ
390* ²ÎÊý˵Ã÷£º ÎÞ
391* ·µ »Ø Öµ£º¼ì²éÕý³£·µ»Ø0, ·ñÔò·µ»Ø-1£¬½øÐÐuserdata·ÖÇøµÄ²Á³ý
392* ÆäËü˵Ã÷£º
393**************************************************************************/
394static int check_files_access()
395{
396 if(access(NV_FS_RW_MAIN_PATH,W_OK | R_OK) < 0)
397 {
398 printf("fs_check file: %s permission loss \n",NV_FS_RW_MAIN_PATH);
399 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_MAIN_PATH);
400 return -1;//²é¿´rw_workÎļþÊÇ·ñ¿É¶Áд
401 }
402 if(access(NV_FS_RW_BACKUP_PATH, W_OK | R_OK) < 0)
403 {
404 printf("fs_check file: %s permission loss \n",NV_FS_RW_BACKUP_PATH);
405 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_BACKUP_PATH);
406 return -1;//²é¿´rw_backupÎļþÊÇ·ñ¿É¶Áд
407 }
408 if(access(NV_FS_FAC_SYMBOL_PATH, W_OK | R_OK) < 0)
409 {
410 printf("fs_check file: %s permission loss \n",NV_FS_FAC_SYMBOL_PATH);
411 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_FAC_SYMBOL_PATH);
412 return -1;//²é¿´fac_flagÎļþÊÇ·ñ¿É¶Áд
413 }
414 if(access(NV_FS_RW_MAIN_SYMBOL_PATH, W_OK | R_OK) < 0)
415 {
416 printf("fs_check file: %s permission loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);
417 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);
418 return -1;//²é¿´work_flagÎļþÊÇ·ñ¿É¶Áд
419 }
420 if(access(NV_FS_RW_BACKUP_SYMBOL_PATH, W_OK | R_OK) < 0)
421 {
422 printf("fs_check file: %s permission loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);
423 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);
424 return -1;//²é¿´backup_flagÎļþÊÇ·ñ¿É¶Áд
425 }
xf.lia7d2a2b2025-02-10 01:44:22 -0800426#ifndef USE_CAP_SUPPORT
lh9ed821d2023-04-07 01:36:19 -0700427 if(access(NV_FS_RW_AP_NV_MAIN_PATH, W_OK | R_OK) < 0)
428 {
429 printf("fs_check file: %s permission loss \n",NV_FS_RW_AP_NV_MAIN_PATH);
430 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_AP_NV_MAIN_PATH);
431 return -1;//²é¿´apϹ¤×÷nvÎļþÊÇ·ñ¿É¶Áд
432 }
433 if(access(NV_FS_RW_AP_NV_BACKUP_PATH, W_OK | R_OK) < 0)
434 {
435 printf("fs_check file: %s permission loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);
436 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s permission loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);
437 return -1;//²é¿´apϱ¸·ÝnvÎļþÊÇ·ñ¿É¶Áд
438 }
xf.lia7d2a2b2025-02-10 01:44:22 -0800439#endif
lh9ed821d2023-04-07 01:36:19 -0700440 return 0;
441}
xf.li84027492024-04-09 00:17:51 -0700442
443/* read success return 0, file not exist return 0, other return -1*/
444static int file_read_test(const char *filename)
445{
446 char *buf;
447 size_t read_size;
448 struct stat file_stat;
449 int result = stat(filename, &file_stat);
450
451 if (result == -1 && errno == ENOENT)
452 {
453 return 0; /* file not exist */
454 }
455 else
456 {
457 if (result != 0)
458 {
459 return -1;
460 }
461 }
462
463 buf = read_file(filename, &read_size);
464 if (buf == NULL)
465 {
466 if (read_size == 0)
467 {
468 return 0; /* empty file */
469 }
470 else
471 {
472 return -1; /* file read error */
473 }
474 }
475 else
476 {
477 free(buf);
478 return 0;
479 }
480}
481
482/**************************************************************************
483* º¯ÊýÃû³Æ£º check_files_read
484* ¹¦ÄÜÃèÊö£º ¼ì²éuserdataÎļþϵͳϵÄÎļþreadÊÇ·ñÕý³£
485* ²ÎÊý˵Ã÷£º ÎÞ
486* ·µ »Ø Öµ£º¼ì²éÕý³£·µ»Ø0, ·ñÔò·µ»Ø-1£¬½øÐÐuserdata·ÖÇøµÄ²Á³ý
487* ÆäËü˵Ã÷£º
488**************************************************************************/
489static int check_files_read()
490{
491 if(file_read_test(NV_FS_RW_MAIN_PATH) < 0)
492 {
493 printf("fs_check file: %s read loss \n",NV_FS_RW_MAIN_PATH);
494 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_MAIN_PATH);
495 return -1;//²é¿´rw_workÎļþÊÇ·ñ¿É¶Á
496 }
497 if(file_read_test(NV_FS_RW_BACKUP_PATH) < 0)
498 {
499 printf("fs_check file: %s read loss \n",NV_FS_RW_BACKUP_PATH);
500 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_BACKUP_PATH);
501 return -1;//²é¿´rw_backupÎļþÊÇ·ñ¿É¶Á
502 }
503 if(file_read_test(NV_FS_FAC_SYMBOL_PATH) < 0)
504 {
505 printf("fs_check file: %s read loss \n",NV_FS_FAC_SYMBOL_PATH);
506 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_FAC_SYMBOL_PATH);
507 return -1;//²é¿´fac_flagÎļþÊÇ·ñ¿É¶Á
508 }
509 if(file_read_test(NV_FS_RW_MAIN_SYMBOL_PATH) < 0)
510 {
511 printf("fs_check file: %s read loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);
512 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_MAIN_SYMBOL_PATH);
513 return -1;//²é¿´work_flagÎļþÊÇ·ñ¿É¶Á
514 }
515 if(file_read_test(NV_FS_RW_BACKUP_SYMBOL_PATH) < 0)
516 {
517 printf("fs_check file: %s read loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);
518 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_BACKUP_SYMBOL_PATH);
519 return -1;//²é¿´backup_flagÎļþÊÇ·ñ¿É¶Á
520 }
521 if(file_read_test(NV_FS_RW_AP_NV_MAIN_PATH) < 0)
522 {
523 printf("fs_check file: %s read loss \n",NV_FS_RW_AP_NV_MAIN_PATH);
524 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_AP_NV_MAIN_PATH);
525 return -1;//²é¿´apϹ¤×÷nvÎļþÊÇ·ñ¿É¶Á
526 }
527 if(file_read_test(NV_FS_RW_AP_NV_BACKUP_PATH) < 0)
528 {
529 printf("fs_check file: %s read loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);
530 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check file: %s read loss \n",NV_FS_RW_AP_NV_BACKUP_PATH);
531 return -1;//²é¿´apϱ¸·ÝnvÎļþÊÇ·ñ¿É¶Á
532 }
533
534 return 0;
535}
536
lh9ed821d2023-04-07 01:36:19 -0700537/**************************************************************************
538* º¯ÊýÃû³Æ£º check_userdata_is_space_enough
539* ¹¦ÄÜÃèÊö£º ¼ì²éuserdata·ÖÇøÏÂÃæµÄ¿Õ¼äÊÇ·ñ×ã¹»£¬²»×ãʱÐèÒªÖØÐ»ָ´userdata·ÖÇø
540* ²ÎÊý˵Ã÷£º (IN)
541* (OUT)
542* ·µ »Ø Öµ£º¼ì²é¿Õ¼ä×ã¹»·µ»Ø0, ¿Õ¼ä²»×㣬ÐèÒªÖØÐ»ָ´·ÖÇø·µ»Ø-1
543* ÆäËü˵Ã÷£º
544**************************************************************************/
545int check_userdata_space_enough()
546{
547 int fd = 0;
548 char fsckname[] = "/etc_rw/fscheck";
549 char buf[] = "filesystem checking";
550 int len = strlen(buf);
551 char *ptr = buf;
552 int res = 0;
553 int ret = 0;
554
555#if 0
556 struct statfs diskinfo;
557 statfs("/mnt/userdata", &diskinfo);
558
xf.li84027492024-04-09 00:17:51 -0700559 printf("fs_check f_bsize = %d, f_blocks = %d, f_bfree = %d, f_bavail = %d, f_files = %d, , f_ffree = %d\n", \
560 diskinfo.f_bsize, diskinfo.f_blocks, diskinfo.f_bfree, diskinfo.f_bavail, diskinfo.f_files, diskinfo.f_ffree);
lh9ed821d2023-04-07 01:36:19 -0700561
562 //»ñȡʣÓà¿Õ¼äÊÇ·ñ´óÓÚµÈÓÚÁ½¸öblock
563 if ((diskinfo.f_bsize * diskinfo.f_bfree < CONFIG_BLOCK_SIZE * USERDATA_RESET_FREE_BLOCK_LEVEL) ||
564 (diskinfo.f_bsize * diskinfo.f_bavail < CONFIG_BLOCK_SIZE * USERDATA_RESET_FREE_BLOCK_LEVEL))
565 return -1;
566#endif
567
568 fd = open(fsckname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
569 if (fd < 0)
570 {
571 printf("open %s failed errno %d\n", fsckname, errno);
572 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "open %s failed errno %d\n", fsckname, errno);
573 return -1;
574 }
575 while (len > 0)
576 {
577 res = write(fd, ptr, len);
578 if (res < 0)
579 {
580 if (errno == EINTR)
581 {
582 res = 0;
583 }
584 else
585 {
586 printf("write %s failed errno %d\n", fsckname, errno);
587 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "write %s failed errno %d\n", fsckname, errno);
588 ret = -1;
589 break;
590 }
591 }
592 ptr += res;
593 len -= res;
594 }
595
596
597 if (close(fd) < 0)
598 {
599 printf("close %s failed errno %d\n", fsckname, errno);
600 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "close %s failed errno %d\n", fsckname, errno);
601 return -1;
602 }
603 return ret;
604}
605
606/**************************************************************************
607* º¯ÊýÃû³Æ£º check_userdata_is_normal
608* ¹¦ÄÜÃèÊö£º ¼ì²éuserdata·ÖÇøÏÂÃæµÄÎļþÊÇ·ñÕý³££¬Èç¹û´æÔÚÒì³££¬ÔòÐèÒªÖØÐ»ָ´userdata·ÖÇø
609* ²ÎÊý˵Ã÷£º (IN)
610* dst_file: Ä¿±êÎļþ
611* src_file: Ô´Îļþ
612* (OUT)
613* ·µ »Ø Öµ£º¼ì²éÕý³£·µ»Ø0, ÐèÒªÖØÐ»ָ´·ÖÇø·µ»Ø-1
614* ÆäËü˵Ã÷£º
615**************************************************************************/
616int check_userdata_is_normal()
617{
618 struct stat fac_nv_buf = {0};
619 struct stat work_buf = {0};
620 struct stat backup_buf = {0};
621
622 if (check_userdata_space_enough() < 0)
623 return -1;
624 if (access(NV_FS_RW_TOP_PATH, F_OK) != 0)
625 {
626 if (mkdir(NV_FS_RW_TOP_PATH, 0755) != 0)
627 {
628 printf("fs_check access and mkdir %s failed\n", NV_FS_RW_TOP_PATH);
629 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check access and mkdir %s failed\n", NV_FS_RW_TOP_PATH);
630 return -1;
631 }
632 }
633 /*userdata·ÖÇøÊÇ·ñ¼ì²éÐèÒªÂú×ãÒÔÏÂÁ½¸öÌõ¼þ£º
634 *1)²é¿´userdata·ÖÇøÏÂÃæµÄnvrwall.hashÊÇ·ñ´æÔÚ£¬²»´æÔÚ±íʾÊǸÕÉÕÍê°æ±¾£¬Õâʱºò²»¼ì²é
635 *2)imageϵÄhashºÍpsnvĿ¼ÏÂÃæµÄnvrwall.hashÊÇ·ñÒ»Ö£¬Èç¹ûÒ»Ö½øÐмì²é£¬·ñÔò²»½øÐмì²é£¨¹ýÂËfotaÉý¼¶£©
636 */
637 if(access(NV_FS_RW_HASH_WORK_PATH, F_OK) < 0)
638 return 0;
xf.li84027492024-04-09 00:17:51 -0700639
640 if (check_files_read() < 0)
641 {
642 return -1;//userdata·ÖÇøÏµÄÎļþ¶ÁÊÇ·ñÕý³£
643 }
lh9ed821d2023-04-07 01:36:19 -0700644 if(compare_file(NV_FS_RW_HASH_FAC_PATH, NV_FS_RW_HASH_WORK_PATH) == 0)
645 {
646 if(check_files_access() < 0)
647 {
648 return -1;//userdata·ÖÇøÏµÄÎļþ·ÃÎÊȨÏÞÒì³£ÐèÒª»Ö¸´
649 }
650 if(stat(NV_FS_FAC_MAIN_PATH, &fac_nv_buf) < 0)
651 {
652 return 0;
653 }
654
655 if(stat(NV_FS_RW_MAIN_PATH, &work_buf) < 0)
656 {
657 printf("fs_check stat %s failed\n",NV_FS_RW_MAIN_PATH);
658 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check stat %s failed\n",NV_FS_RW_MAIN_PATH);
659 return -1;
660 }
661 if(stat(NV_FS_RW_BACKUP_PATH, &backup_buf) < 0)
662 {
663 printf("fs_check stat %s failed\n",NV_FS_RW_BACKUP_PATH);
664 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check stat %s failed\n",NV_FS_RW_BACKUP_PATH);
665 return -1;
666 }
667 if(work_buf.st_size < fac_nv_buf.st_size || backup_buf.st_size < fac_nv_buf.st_size)
668 {
669 printf("fs_check rw_backup or rw_work file corrupted\n");
670 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check rw_backup or rw_work file corrupted\n");
671 return -1;//userdata·ÖÇøÏµĹ¤×÷ÇønvºÍ±¸·ÝÇønv±È³ö³§ÇønvС£¬ÐèÒª»Ö¸´
672 }
673 }
674
675 return 0;
676
677}
678
679int mount_fs_partition(struct mtd_fs *p_fs)
680{
681 int ret = -1;
682 int ubi_num = 0;
683 int mtd_blk_num = 0;
684 char mount_cmd[MAX_PATH] = {0};
685 char mtd_path[MAX_PATH] = {0};
686 char attach_cmd[MAX_PATH] = {0};
687
688 if (NULL == p_fs->patition_name || NULL == p_fs->mount_point || NULL == p_fs->fs_type)
689 return -1;
690
691 //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);
692
693 if (strcmp(p_fs->patition_name, "cpfs") == 0) {
694 ret = mtd_find(p_fs->patition_name, mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
695 if (ret < 0) {
696 printf("fs_check partition name is not find\n");
697 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check partition name is not find\n");
698 return -1;
699 }
700 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);
701 } else if (strcmp(p_fs->fs_type, "jffs2") == 0) {
702 ret = mtd_find(p_fs->patition_name, mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
703 if (ret < 0) {
704 printf("fs_check partition name is not find\n");
705 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check partition name is not find\n");
706 return -1;
707 }
708 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);
709 } else if (strcmp(p_fs->fs_type, "ubifs") == 0) {
710 ret = mtd_find(p_fs->patition_name, mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
711 if (ret < 0) {
712 printf("fs_check partition name is not find\n");
713 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check partition name is not find\n");
714 return -1;
715 }
716 sscanf(mtd_path, "/dev/mtdblock%d", &mtd_blk_num);
717 snprintf(attach_cmd, sizeof(attach_cmd), "%s/usr/sbin/ubiattach /dev/ubi_ctrl -m %d", g_path_prefix,mtd_blk_num);
718
719 ret = system_exec_status(zxic_system(attach_cmd));
720 if (ret == SYSTEM_EXEC_FAIL) {
721 printf("fs_check: %s fail\n",attach_cmd);
722 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check: %s fail\n",attach_cmd);
723 return -1;
724 }
725
726 ubi_num = get_ubifs_device_num(mtd_blk_num);
727 if (ubi_num < 0) {
728 printf("fs_check ubi_num not match\n");
729 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check ubi_num not match\n");
730 return -1;
731 }
732 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);
733 } else {
734 printf("fs_check unknown mount type: %s\n", p_fs->fs_type);
735 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check unknown mount type: %s\n", p_fs->fs_type);
736 return -1;
737 }
738
739 ret = zxic_system(mount_cmd);
740
741 if (check_mount_result(p_fs->mount_point) < 0) {
742 printf("fs_check : %s fail\n", mount_cmd);
743 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check : %s fail\n", mount_cmd);
744 return -1;
745 }
746 return 0;
747}
748
749int unmount_fs_partition(struct mtd_fs *p_fs)
750{
751 int ret = -1;
752 int ubi_num = 0;
753 int mtd_blk_num = 0;
754 char umount_cmd[MAX_PATH] = {0};
755 char mtd_path[MAX_PATH] = {0};
756 char detach_cmd[MAX_PATH] = {0};
757
758 if (NULL == p_fs->patition_name || NULL == p_fs->mount_point || NULL == p_fs->fs_type)
759 return -1;
760
761 if (strcmp(p_fs->patition_name, "cpfs") == 0) {
762 snprintf(umount_cmd, sizeof(umount_cmd), "%s/bin/umount -f %s ", g_path_prefix,p_fs->mount_point);
763 } else if (strcmp(p_fs->fs_type, "jffs2") == 0) {
764 snprintf(umount_cmd, sizeof(umount_cmd), "%s/bin/umount -f %s", g_path_prefix,p_fs->mount_point);
765 } else if (strcmp(p_fs->fs_type, "ubifs") == 0) {
766
767 snprintf(umount_cmd, sizeof(umount_cmd), "%s/bin/umount -f %s", g_path_prefix,p_fs->mount_point);
768 zxic_system(umount_cmd);
769 printf("fs_check umount : %s\n", umount_cmd);
770
771 ret = mtd_find(p_fs->patition_name, mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
772 if (ret < 0) {
773 printf("fs_check partition name is not find\n");
774 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check partition name is not find\n");
775 return -1;
776 }
777 sscanf(mtd_path, "/dev/mtdblock%d", &mtd_blk_num);
778
779 ubi_num = get_ubifs_device_num(mtd_blk_num);
780
781 snprintf(detach_cmd, sizeof(detach_cmd), "%s/usr/sbin/ubidetach /dev/ubi_ctrl -d %d", g_path_prefix,ubi_num);
782
783 ret = system_exec_status(zxic_system(detach_cmd));
784 if (ret == SYSTEM_EXEC_FAIL) {
785 printf("fs_check: %s fail\n",detach_cmd);
786 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check: %s fail\n",detach_cmd);
787 return -1;
788 }
789
790 return 0;
791 } else {
792 printf("fs_check unknown umount type: %s\n", p_fs->fs_type);
793 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check unknown umount type: %s\n", p_fs->fs_type);
794 return -1;
795 }
796 printf("fs_check umount : %s\n", umount_cmd);
797 ret = zxic_system(umount_cmd);
798
799 return 0;
800}
801
802int mtd_erase_partition(const char* partition_name)
803{
804 int ret = 0;
805 char mtd_path[MAX_PATH] = {0};
806 int fd_mtd = -1;
807
808 struct mtd_info_user meminfo = {0};
809 struct erase_info_user64 erase_info = {0};
810
811 if (NULL == partition_name) {
812 return -1;
813 }
814 ret = mtd_find(partition_name, mtd_path, DEVICE_MTD, MAX_PATH);
815 if (ret < 0) {
816 printf("fs_check mtd_find %s failed\n", partition_name);
817 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd_find %s failed\n", partition_name);
818 ret = -1;
819 goto out;
820 }
821 fd_mtd = open(mtd_path, O_RDWR);
822 if (fd_mtd < 0) {
823 printf("fs_check open %s error, %s\n", partition_name, strerror(errno));
824 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check open %s error, %s\n", partition_name, strerror(errno));
825 ret = -1;
826 goto out;
827 }
828 if (ioctl(fd_mtd, MEMGETINFO, &meminfo) != 0) {
829 printf("fs_check get %s info error, %s\n", partition_name, strerror(errno));
830 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check get %s info error, %s\n", partition_name, strerror(errno));
831 ret = -1;
832 goto out;
833 }
834 erase_info.length = meminfo.erasesize;
835 for (erase_info.start = 0; erase_info.start < meminfo.size; erase_info.start += meminfo.erasesize) {
836 if (ioctl(fd_mtd, MEMGETBADBLOCK, &(erase_info.start)) > 0) {
837 printf("fs_check mtd, not erasing bad block at 0x%llx\n", erase_info.start);
838 continue;
839 }
840 if (ioctl(fd_mtd, MEMERASE64, &erase_info) < 0) {
841 printf("fs_check mtd, erasing failure at 0x%llx\n", erase_info.start);
842 }
843 }
844 ret = 0;
845out:
846 if (fd_mtd >= 0) {
847 close(fd_mtd);
848 }
849 return ret;
850}
851
852int mtd_write_partition(const char* partition_name, const char* image_file)
853{
854 int ret = 0;
855 char mtd_path[MAX_PATH] = {0};
856 int fd_mtd = -1;
857 struct mtd_info_user meminfo = {0};
858
859 long long index = 0;
860 int len = 0;
861 FILE * fp = NULL;
862 char * buf = NULL;
863 struct stat statbuff = {0};
864
865 if (NULL == partition_name || NULL == image_file)
866 return -1;
867
868 ret = mtd_find(partition_name, mtd_path, DEVICE_MTD, MAX_PATH);
869 if (ret < 0) {
870 printf("fs_check mtd_find %s failed\n", partition_name);
871 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd_find %s failed\n", partition_name);
872 ret = -1;
873 goto out;
874 }
875 fd_mtd = open(mtd_path, O_RDWR);
876 if (fd_mtd < 0) {
877 printf("fs_check open %s error, %s\n", partition_name, strerror(errno));
878 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check open %s error, %s\n", partition_name, strerror(errno));
879 ret = -1;
880 goto out;
881 }
882 if (ioctl(fd_mtd, MEMGETINFO, &meminfo) != 0) {
883 printf("fs_check get %s info error, %s\n", partition_name, strerror(errno));
884 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check get %s info error, %s\n", partition_name, strerror(errno));
885 ret = -1;
886 goto out;
887 }
888
889 if(stat(image_file, &statbuff) < 0)
890 {
891 printf("fs_check stat %s failed\n", image_file);
892 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check stat %s failed\n", image_file);
893 ret = -1;
894 goto out;
895 }
896
897 fp = fopen(image_file, "ro");
898 if (!fp)
899 {
900 printf("fs_check fopen %s failed\n", image_file);
901 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check fopen %s failed\n", image_file);
902 ret = -1;
903 goto out;
904 }
905
906 buf = (char *)malloc(meminfo.erasesize);
907 if(!buf)
908 {
909 printf("fs_check malloc failed\n");
910 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check malloc failed\n");
911 ret = -1;
912 goto out;
913 }
914
915 for (index = 0; index < meminfo.size && len < statbuff.st_size; index += meminfo.erasesize)
916 {
917 if (ioctl(fd_mtd, MEMGETBADBLOCK, &index) > 0)
918 {
919 printf("fs_check mtd, not erasing bad block at %lld\n", index);
920 continue;
921 }
922
xf.li84027492024-04-09 00:17:51 -0700923 memset(buf, 0xff, meminfo.erasesize);
lh9ed821d2023-04-07 01:36:19 -0700924 ret = fread(buf, 1, meminfo.erasesize, fp);
925 if(ret < 0)
926 {
927 printf("fs_check mtd, fread error = %d!\n", ret);
928 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd, fread error = %d!\n", ret);
929 ret = -1;
930 goto out;
931 }
932
933 ret = lseek(fd_mtd, (long)index, SEEK_SET);
934 if(ret < 0)
935 {
936 printf("fs_check mtd, lseek error = %s!\n", strerror(errno));
937 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd, lseek error = %s!\n", strerror(errno));
938 ret = -1;
939 goto out;
940 }
xf.li742dd022023-06-08 01:43:32 -0700941 ret = write(fd_mtd, buf, (size_t)meminfo.erasesize);
942 if (ret < 0 || ret != meminfo.erasesize)
lh9ed821d2023-04-07 01:36:19 -0700943 {
xf.li742dd022023-06-08 01:43:32 -0700944 printf("fs_check mtd, write %s error = %d!\n", partition_name, ret);
945 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 -0700946 ret = -1;
947 goto out;
948 }
949 len += meminfo.erasesize;
950 }
951 if (len < statbuff.st_size)
952 {
953 printf("fs_check mtd, No space left,writelen=%d, filesize=%d\n",len,statbuff.st_size);
954 sc_debug_info_record(MODULE_ID_AP_FS_CHECK, "fs_check mtd, No space left,writelen=%d, filesize=%d\n",len,statbuff.st_size);
955 }
956
957 ret = 0;
958out:
959 if (fd_mtd >= 0)
960 close(fd_mtd);
961
962 if (buf != NULL) {
963 memset(buf, 0, meminfo.erasesize);
964 free(buf);
965 }
966
967 if (fp != NULL)
968 fclose(fp);
969
970 return ret;
971}
972
973
974