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