xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /**
|
| 2 | * @file flags_api.c
|
| 3 | * @brief flags·ÖÇø½Ó¿ÚʵÏÖ
|
| 4 | *
|
| 5 | * Copyright (C) 2023 Sanechips Technology Co., Ltd.
|
| 6 | * @author
|
| 7 | *
|
| 8 | * This program is free software; you can redistribute it and/or modify
|
| 9 | * it under the terms of the GNU General Public License version 2 as
|
| 10 | * published by the Free Software Foundation. £¨±ØÑ¡£ºGPLv2 Licence£©
|
| 11 | *
|
| 12 | */
|
| 13 |
|
| 14 |
|
| 15 | /*******************************************************************************
|
| 16 | * Include header files *
|
| 17 | ******************************************************************************/
|
| 18 | #include <stdio.h>
|
| 19 | #include <stdlib.h>
|
| 20 | #include <unistd.h>
|
| 21 | #include <string.h>
|
| 22 | #include <errno.h>
|
| 23 | #include <sys/ioctl.h>
|
| 24 | #include <sys/stat.h>
|
| 25 | #include <fcntl.h>
|
| 26 | #include <mtd/mtd-abi.h>
|
| 27 |
|
| 28 | #include "pub_flags.h"
|
| 29 |
|
| 30 | #include "flags_log.h"
|
| 31 | #include "flags_api.h"
|
| 32 |
|
| 33 |
|
| 34 | /*******************************************************************************
|
| 35 | * Macro definitions *
|
| 36 | ******************************************************************************/
|
| 37 | #define MAX_PATH_LEN (256)
|
| 38 |
|
| 39 | #define PARTITION_NAME_FLAGS "flags"
|
| 40 |
|
| 41 | #define FLAGS_INIT_VALID_BLOCKS_NUM (8)
|
| 42 |
|
| 43 | #define GOOD_BLOCK (0)
|
| 44 | #define BAD_BLOCK (1)
|
| 45 |
|
| 46 | #define FILE_PATH_PROC_CMDLINE "/proc/cmdline"
|
| 47 |
|
| 48 | #define PROC_CMDLINE_SYSTEM_A_FLAG "system=system_a"
|
| 49 | #define PROC_CMDLINE_SYSTEM_B_FLAG "system=system_b"
|
| 50 |
|
| 51 | #define SYSTEM_INDEX_UNKNOWN (-1)
|
| 52 | #define SYSTEM_INDEX_1 (1)
|
| 53 | #define SYSTEM_INDEX_2 (2)
|
| 54 |
|
| 55 |
|
| 56 | /*******************************************************************************
|
| 57 | * Type definitions *
|
| 58 | ******************************************************************************/
|
| 59 | typedef struct
|
| 60 | {
|
| 61 | unsigned int mtd_totalsize; // mtd device total size
|
| 62 | unsigned int mtd_pageperblock; // mtd device page per block
|
| 63 | unsigned int mtd_blocksize; // mtd device block size
|
| 64 | unsigned int mtd_pagesize; // mtd device page size
|
| 65 | unsigned int mtd_oobsize; // mtd device oob size
|
| 66 | int parti_file_desc; // partition update file description
|
| 67 | } partition_mtd_info_t;
|
| 68 |
|
| 69 |
|
| 70 | typedef enum
|
| 71 | {
|
| 72 | DEVICE_MTD = 0,
|
| 73 | DEVICE_ZFTL = 1,
|
| 74 | DEVICE_MTD_BLOCK,
|
| 75 | } device_type_t;
|
| 76 |
|
| 77 |
|
| 78 | /*******************************************************************************
|
| 79 | * Local variable definitions *
|
| 80 | ******************************************************************************/
|
| 81 |
|
| 82 |
|
| 83 | /*******************************************************************************
|
| 84 | * Global variable definitions *
|
| 85 | ******************************************************************************/
|
| 86 |
|
| 87 |
|
| 88 | /*******************************************************************************
|
| 89 | * Local function declarations *
|
| 90 | ******************************************************************************/
|
| 91 | static int mtd_get(const char *i_parti_name, device_type_t device_type, char *o_mtd_path, unsigned int o_mtd_path_len);
|
| 92 | static int write_flags_info(partition_mtd_info_t *p_mtd_info, int index, unsigned char *content, int len);
|
| 93 |
|
| 94 | static int get_flags_info(T_FLAGS_INFO *p_main, int *p_main_index, T_FLAGS_INFO *p_backup, int *p_backup_index);
|
| 95 | static int set_flags_info(T_FLAGS_INFO *p_flags_info, int *p_main_index, int *p_backup_index);
|
| 96 |
|
| 97 | static void copy_flags_info(T_FLAGS_INFO *dst, T_FLAGS_INFO *src);
|
| 98 |
|
| 99 | static int get_current_system();
|
| 100 |
|
| 101 |
|
| 102 | /*******************************************************************************
|
| 103 | * Local function implementations *
|
| 104 | ******************************************************************************/
|
| 105 | static int mtd_get(const char *i_parti_name, device_type_t device_type, char *o_mtd_path, unsigned int o_mtd_path_len)
|
| 106 | {
|
| 107 | FILE *fp_mtd = 0;
|
| 108 | char buf[128];
|
| 109 | char *line_str;
|
| 110 |
|
| 111 | if (!o_mtd_path_len)
|
| 112 | {
|
| 113 | return -1;
|
| 114 | }
|
| 115 |
|
| 116 | fp_mtd = fopen("/proc/mtd", "r+");
|
| 117 | if (NULL == fp_mtd)
|
| 118 | {
|
| 119 | flags_err("open file error: %s", strerror(errno));
|
| 120 | return -1;
|
| 121 | }
|
| 122 | // printf("[libmtd]: partition name:%s\n", i_parti_name);
|
| 123 |
|
| 124 | while (1)
|
| 125 | {
|
| 126 | int matches = 0;
|
| 127 | char mtdname[64] = {0};
|
| 128 | int mtdnum = 0;
|
| 129 | unsigned int mtdsize, mtderasesize;
|
| 130 | memset(buf, 0, sizeof(buf));
|
| 131 | line_str = fgets(buf, sizeof(buf) - 1, fp_mtd);
|
| 132 |
|
| 133 | if (NULL == line_str)
|
| 134 | {
|
| 135 | flags_err("get info from mtd error: %s", strerror(errno));
|
| 136 | fclose(fp_mtd);
|
| 137 | return -1;
|
| 138 | }
|
| 139 | // mtd5: 00100000 00020000 "fotaflag"
|
| 140 | matches = sscanf(buf, "mtd%d: %x %x \"%63[^\"]",
|
| 141 | &mtdnum, &mtdsize, &mtderasesize, mtdname);
|
| 142 | mtdname[63] = '\0';
|
| 143 |
|
| 144 | if ((matches == 4) && (strcmp(mtdname, i_parti_name) == 0))
|
| 145 | {
|
| 146 | memset(o_mtd_path, 0, o_mtd_path_len);
|
| 147 | if (device_type == DEVICE_MTD_BLOCK)
|
| 148 | {
|
| 149 | snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtdblock%d", mtdnum);
|
| 150 | }
|
| 151 | else if (device_type == DEVICE_MTD)
|
| 152 | {
|
| 153 | snprintf(o_mtd_path, o_mtd_path_len, "/dev/mtd%d", mtdnum);
|
| 154 | }
|
| 155 | else if (device_type == DEVICE_ZFTL)
|
| 156 | {
|
| 157 | snprintf(o_mtd_path, o_mtd_path_len, "/dev/zftl%d", mtdnum);
|
| 158 | }
|
| 159 | else
|
| 160 | {
|
| 161 | flags_err("unknown device type %d", device_type);
|
| 162 | fclose(fp_mtd);
|
| 163 | return -1;
|
| 164 | }
|
| 165 | // printf("[libmtd]: o_mtd_path=[%s]\n", o_mtd_path);
|
| 166 | break;
|
| 167 | }
|
| 168 | }
|
| 169 | fclose(fp_mtd);
|
| 170 | return 0;
|
| 171 | }
|
| 172 |
|
| 173 |
|
| 174 | static int write_flags_info(partition_mtd_info_t *p_mtd_info, int index, unsigned char *content, int len)
|
| 175 | {
|
| 176 | struct erase_info_user erase_info;
|
| 177 | int write_len = 0;
|
| 178 | long long offs = 0;
|
| 179 |
|
| 180 | erase_info.start = index * p_mtd_info->mtd_blocksize;
|
| 181 | erase_info.length = p_mtd_info->mtd_blocksize;
|
| 182 |
|
| 183 | offs = (long long)index * p_mtd_info->mtd_blocksize;
|
| 184 |
|
| 185 | if (ioctl(p_mtd_info->parti_file_desc, WRITEENABLE, 0) != 0)
|
| 186 | {
|
| 187 | flags_err("failed to enable mtd writeable, errno=%d, strerror=%s", errno, strerror(errno));
|
| 188 |
|
| 189 | return -1;
|
| 190 | }
|
| 191 |
|
| 192 | if (ioctl(p_mtd_info->parti_file_desc, MEMGETBADBLOCK, &offs) != 0)
|
| 193 | {
|
| 194 | flags_err("ioctl [MEMGETBADBLOCK] block: %d, change to be bad block, errno=%d, strerror=[%s]", index, errno, strerror(errno));
|
| 195 |
|
| 196 | return BAD_BLOCK;
|
| 197 | }
|
| 198 |
|
| 199 | if (ioctl(p_mtd_info->parti_file_desc, MEMERASE, &erase_info) < 0)
|
| 200 | {
|
| 201 | flags_err("ioctl [MEMERASE] block: %d fail, errno=%d, strerror=[%s]", index, errno, strerror(errno));
|
| 202 |
|
| 203 | return -1;
|
| 204 | }
|
| 205 |
|
| 206 | if (ioctl(p_mtd_info->parti_file_desc, MEMGETBADBLOCK, &offs) != 0)
|
| 207 | {
|
| 208 | flags_err("ioctl [MEMGETBADBLOCK] block:%d , change to be bad block, errno=%d, strerror=[%s]", index, errno, strerror(errno));
|
| 209 |
|
| 210 | return BAD_BLOCK;
|
| 211 | }
|
| 212 |
|
| 213 | if (lseek(p_mtd_info->parti_file_desc, index * p_mtd_info->mtd_blocksize, SEEK_SET) < 0)
|
| 214 | {
|
| 215 | flags_err("lseek fail, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 216 |
|
| 217 | return -1;
|
| 218 | }
|
| 219 |
|
| 220 | write_len = write(p_mtd_info->parti_file_desc, content, p_mtd_info->mtd_blocksize);
|
| 221 |
|
| 222 | if (write_len != p_mtd_info->mtd_blocksize)
|
| 223 | {
|
| 224 | flags_err("write flash fail, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 225 |
|
| 226 | return -1;
|
| 227 | }
|
| 228 |
|
| 229 | if (ioctl(p_mtd_info->parti_file_desc, WRITEDISABLE, 0) != 0)
|
| 230 | {
|
| 231 | flags_err("failed to disable mtd writeable, errno=%d, strerror=%s", errno, strerror(errno));
|
| 232 | }
|
| 233 |
|
| 234 | return 0;
|
| 235 | }
|
| 236 |
|
| 237 |
|
| 238 | static int get_flags_info(T_FLAGS_INFO *p_main, int *p_main_index, T_FLAGS_INFO *p_backup, int *p_backup_index)
|
| 239 | {
|
| 240 | int ret = -1;
|
| 241 | int fd_dst = -1;
|
| 242 | char mtd_path[MAX_PATH_LEN] = {0};
|
| 243 | struct mtd_info_user meminfo = {0};
|
| 244 | partition_mtd_info_t mtd_info = {0};
|
| 245 | int index = 0;
|
| 246 | int block_num = 0;
|
| 247 | int good_index = 0;
|
| 248 | int read_len = 0;
|
| 249 |
|
| 250 | long long offs = 0;
|
| 251 |
|
| 252 | int block_flag = GOOD_BLOCK;
|
| 253 |
|
| 254 | ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
|
| 255 | if (ret < 0)
|
| 256 | {
|
| 257 | flags_err("partition [%s] not find", PARTITION_NAME_FLAGS);
|
| 258 | goto error;
|
| 259 | }
|
| 260 |
|
| 261 | if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
|
| 262 | {
|
| 263 | flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 264 | goto error;
|
| 265 | }
|
| 266 |
|
| 267 | mtd_info.parti_file_desc = fd_dst;
|
| 268 |
|
| 269 | if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
|
| 270 | {
|
| 271 | flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 272 | goto error_close;
|
| 273 | }
|
| 274 |
|
| 275 | mtd_info.mtd_blocksize = meminfo.erasesize;
|
| 276 | mtd_info.mtd_oobsize = meminfo.oobsize;
|
| 277 | mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
|
| 278 | mtd_info.mtd_pagesize = meminfo.writesize;
|
| 279 | mtd_info.mtd_totalsize = meminfo.size;
|
| 280 |
|
| 281 | block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
|
| 282 |
|
| 283 | for (index = 0; (good_index < 2 && index < block_num); index++)
|
| 284 | {
|
| 285 |
|
| 286 | offs = index * mtd_info.mtd_blocksize;
|
| 287 | if (ioctl(mtd_info.parti_file_desc, MEMGETBADBLOCK, &offs) == 0)
|
| 288 | {
|
| 289 | block_flag = GOOD_BLOCK;
|
| 290 | }
|
| 291 | else
|
| 292 | {
|
| 293 | flags_err("flags block [%d] is bad, errno=%d, strerror=[%s]", index, errno, strerror(errno));
|
| 294 | block_flag = BAD_BLOCK;
|
| 295 | }
|
| 296 |
|
| 297 | if (block_flag == GOOD_BLOCK)
|
| 298 | {
|
| 299 | if (lseek(mtd_info.parti_file_desc, index * mtd_info.mtd_blocksize, SEEK_SET) < 0)
|
| 300 | {
|
| 301 | flags_err("lseek error, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 302 | goto error_close;
|
| 303 | }
|
| 304 |
|
| 305 | if (good_index == 0)
|
| 306 | {
|
| 307 | read_len = read(mtd_info.parti_file_desc, (unsigned char*)p_main, sizeof(T_FLAGS_INFO));
|
| 308 | *p_main_index = index;
|
| 309 | }
|
| 310 | else if (good_index == 1)
|
| 311 | {
|
| 312 | read_len = read(mtd_info.parti_file_desc, (unsigned char*)p_backup, sizeof(T_FLAGS_INFO));
|
| 313 | *p_backup_index = index;
|
| 314 | }
|
| 315 | else
|
| 316 | {
|
| 317 | break;
|
| 318 | }
|
| 319 |
|
| 320 | if (read_len < sizeof(T_FLAGS_INFO))
|
| 321 | {
|
| 322 | flags_err("read len (%d) < need len (%d)", read_len, sizeof(T_FLAGS_INFO));
|
| 323 | goto error_close;
|
| 324 | }
|
| 325 |
|
| 326 | good_index++;
|
| 327 | }
|
| 328 |
|
| 329 | }
|
| 330 |
|
| 331 | close(fd_dst);
|
| 332 |
|
| 333 | return 0;
|
| 334 |
|
| 335 | error_close:
|
| 336 | close(fd_dst);
|
| 337 |
|
| 338 | error:
|
| 339 | return -1;
|
| 340 | }
|
| 341 |
|
| 342 |
|
| 343 | static int set_flags_info(T_FLAGS_INFO *p_flags_info, int *p_main_index, int *p_backup_index)
|
| 344 | {
|
| 345 | int ret = -1;
|
| 346 | int fd_dst = -1;
|
| 347 | char mtd_path[MAX_PATH_LEN] = {0};
|
| 348 | struct mtd_info_user meminfo = {0};
|
| 349 | partition_mtd_info_t mtd_info = {0};
|
| 350 |
|
| 351 | unsigned char *real_write_content = NULL;
|
| 352 |
|
| 353 | int index = 0;
|
| 354 | int block_num = 0;
|
| 355 | int main_index = *p_main_index;
|
| 356 | int back_index = *p_backup_index;
|
| 357 |
|
| 358 | ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
|
| 359 | if (ret < 0)
|
| 360 | {
|
| 361 | flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
|
| 362 |
|
| 363 | return -1;
|
| 364 | }
|
| 365 |
|
| 366 | if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
|
| 367 | {
|
| 368 | flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 369 | return -1;
|
| 370 | }
|
| 371 |
|
| 372 | if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
|
| 373 | {
|
| 374 | flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 375 | goto error;
|
| 376 | }
|
| 377 |
|
| 378 | mtd_info.parti_file_desc = fd_dst;
|
| 379 | mtd_info.mtd_blocksize = meminfo.erasesize;
|
| 380 | mtd_info.mtd_oobsize = meminfo.oobsize;
|
| 381 | mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
|
| 382 | mtd_info.mtd_pagesize = meminfo.writesize;
|
| 383 | mtd_info.mtd_totalsize = meminfo.size;
|
| 384 |
|
| 385 | block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
|
| 386 |
|
| 387 | real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
|
| 388 | if (NULL == real_write_content)
|
| 389 | {
|
| 390 | flags_err("malloc block fail");
|
| 391 | goto error;
|
| 392 | }
|
| 393 |
|
| 394 | memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
|
| 395 | memcpy(real_write_content, (char *)p_flags_info, sizeof(T_FLAGS_INFO));
|
| 396 |
|
| 397 | flags_log("begin to write main flags");
|
| 398 |
|
| 399 | for (index = 0; index < block_num; index++)
|
| 400 | {
|
| 401 | if (index == back_index)
|
| 402 | {
|
| 403 | continue;
|
| 404 | }
|
| 405 |
|
| 406 | ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
|
| 407 | if (ret == 0)
|
| 408 | {
|
| 409 | // д³É¹¦£¬Í˳ö£¬²¢¸üеÚÒ»¿éλÖÃ
|
| 410 | flags_log("main flags location: [%d]->[%d]", main_index, index);
|
| 411 | main_index = index;
|
| 412 | break;
|
| 413 | }
|
| 414 | else if (ret == BAD_BLOCK)
|
| 415 | {
|
| 416 | // Óöµ½»µ¿é£¬ÏòºóÌøÒ»¿é
|
| 417 | flags_log("flags block index [%d] is bad", index);
|
| 418 | continue;
|
| 419 | }
|
| 420 | else
|
| 421 | {
|
| 422 | flags_err("write main flags fail");
|
| 423 | main_index = -1;
|
| 424 | break;
|
| 425 | }
|
| 426 | }
|
| 427 |
|
| 428 | flags_log("begin to write backup flags");
|
| 429 |
|
| 430 | for (index = 0; index < block_num; index++)
|
| 431 | {
|
| 432 | if (index == main_index)
|
| 433 | {
|
| 434 | continue;
|
| 435 | }
|
| 436 |
|
| 437 | flags_log("write backup to [%d] block", index);
|
| 438 | ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
|
| 439 | if (ret == 0)
|
| 440 | {
|
| 441 | // д³É¹¦£¬Í˳ö£¬²¢¸üеÚÒ»¿éλÖÃ
|
| 442 | flags_log("backup flags location: [%d]->[%d]", back_index, index);
|
| 443 | back_index = index;
|
| 444 | break;
|
| 445 | }
|
| 446 | else if (ret == BAD_BLOCK)
|
| 447 | {
|
| 448 | // Óöµ½»µ¿é£¬ÏòºóÌøÒ»¿é
|
| 449 | continue;
|
| 450 | }
|
| 451 | else
|
| 452 | {
|
| 453 | flags_err("write backup flags fail");
|
| 454 | back_index = -1;
|
| 455 | break;
|
| 456 | }
|
| 457 | }
|
| 458 |
|
| 459 | if (main_index == -1 && back_index == -1)
|
| 460 | {
|
| 461 | goto error;
|
| 462 | }
|
| 463 | else
|
| 464 | {
|
| 465 | ret = 0;
|
| 466 | goto end;
|
| 467 | }
|
| 468 |
|
| 469 | error:
|
| 470 | ret = -1;
|
| 471 |
|
| 472 | end:
|
| 473 | close(fd_dst);
|
| 474 |
|
| 475 | if(NULL != real_write_content)
|
| 476 | {
|
| 477 | free(real_write_content);
|
| 478 | real_write_content = NULL;
|
| 479 | }
|
| 480 |
|
| 481 | return ret;
|
| 482 | }
|
| 483 |
|
| 484 |
|
| 485 | static void copy_flags_info(T_FLAGS_INFO *dst, T_FLAGS_INFO *src)
|
| 486 | {
|
| 487 | dst->magic_start = src->magic_start;
|
| 488 |
|
| 489 | dst->boot_fota_flag.boot_to = src->boot_fota_flag.boot_to;
|
| 490 | dst->boot_fota_flag.fota_status = src->boot_fota_flag.fota_status;
|
| 491 | dst->boot_fota_flag.system.status = src->boot_fota_flag.system.status;
|
| 492 | dst->boot_fota_flag.system.try_cnt = src->boot_fota_flag.system.try_cnt;
|
| 493 | dst->boot_fota_flag.system2.status = src->boot_fota_flag.system2.status;
|
| 494 | dst->boot_fota_flag.system2.try_cnt = src->boot_fota_flag.system2.try_cnt;
|
| 495 |
|
| 496 | dst->boot_env.dualsys_type = src->boot_env.dualsys_type;
|
| 497 | strncpy(dst->boot_env.system_boot_env, src->boot_env.system_boot_env, sizeof(dst->boot_env.system_boot_env));
|
| 498 | strncpy(dst->boot_env.system2_boot_env, src->boot_env.system2_boot_env, sizeof(dst->boot_env.system2_boot_env));
|
| 499 |
|
| 500 | dst->ubifs_status.fs_status = src->ubifs_status.fs_status;
|
| 501 | strncpy(dst->ubifs_status.fs_mtd_name, src->ubifs_status.fs_mtd_name, sizeof(dst->ubifs_status.fs_mtd_name));
|
| 502 | strncpy(dst->ubifs_status.fs_ubi_vol_name, src->ubifs_status.fs_ubi_vol_name, sizeof(dst->ubifs_status.fs_ubi_vol_name));
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 503 |
|
| 504 | dst->nvro_flag = src->nvro_flag;
|
| 505 |
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 506 | dst->magic_end = src->magic_end;
|
| 507 |
|
| 508 | return;
|
| 509 | }
|
| 510 |
|
| 511 |
|
| 512 | static int get_current_system()
|
| 513 | {
|
| 514 | char buf[1024] = {0};
|
| 515 |
|
| 516 | char *para = NULL;
|
| 517 | int matches = 0;
|
| 518 |
|
| 519 | FILE *fd_cmd = NULL;
|
| 520 | char *line_str = NULL;
|
| 521 |
|
| 522 | int current_system = -1;
|
| 523 |
|
| 524 |
|
| 525 | fd_cmd = fopen(FILE_PATH_PROC_CMDLINE, "r");
|
| 526 | if (!fd_cmd)
|
| 527 | {
|
| 528 | flags_err("open file %s error, error:%s", FILE_PATH_PROC_CMDLINE, strerror(errno));
|
| 529 | return SYSTEM_INDEX_UNKNOWN;
|
| 530 | }
|
| 531 |
|
| 532 | while (!feof(fd_cmd))
|
| 533 | {
|
| 534 | memset(buf, 0, sizeof(buf));
|
| 535 | line_str = fgets(buf, sizeof(buf), fd_cmd);
|
| 536 |
|
| 537 | if (NULL == line_str)
|
| 538 | {
|
| 539 | flags_err("get info from /proc/cmdline error:%s", strerror(errno));
|
| 540 | goto end;
|
| 541 | }
|
| 542 |
|
| 543 | flags_log("buff:%s", buf);
|
| 544 |
|
| 545 | para = strtok(buf, " ");
|
| 546 | while (para)
|
| 547 | {
|
| 548 | flags_log("para:%s", para);
|
| 549 | if (strncmp(para, PROC_CMDLINE_SYSTEM_A_FLAG, strlen(PROC_CMDLINE_SYSTEM_A_FLAG)) == 0)
|
| 550 | {
|
| 551 | current_system = SYSTEM_INDEX_1;
|
| 552 | goto end;
|
| 553 | }
|
| 554 | else if (strncmp(para, PROC_CMDLINE_SYSTEM_B_FLAG, strlen(PROC_CMDLINE_SYSTEM_B_FLAG)) == 0)
|
| 555 | {
|
| 556 | current_system = SYSTEM_INDEX_2;
|
| 557 | goto end;
|
| 558 | }
|
| 559 | else
|
| 560 | {
|
| 561 | //:
|
| 562 | }
|
| 563 | para = strtok(NULL, " ");
|
| 564 | }
|
| 565 | }
|
| 566 |
|
| 567 | end:
|
| 568 | if (fd_cmd)
|
| 569 | {
|
| 570 | fclose(fd_cmd);
|
| 571 | }
|
| 572 |
|
| 573 | return current_system;
|
| 574 | }
|
| 575 |
|
| 576 |
|
| 577 | /*******************************************************************************
|
| 578 | * Global function implementations *
|
| 579 | ******************************************************************************/
|
| 580 | int flags_init()
|
| 581 | {
|
| 582 | T_FLAGS_INFO flags_info = {0};
|
| 583 | int main_index = 0;
|
| 584 | int back_index = 1;
|
| 585 | int ret = -1;
|
| 586 | int fd_dst = -1;
|
| 587 | char mtd_path[MAX_PATH_LEN] = {0};
|
| 588 | struct mtd_info_user meminfo = {0};
|
| 589 | partition_mtd_info_t mtd_info = {0};
|
| 590 |
|
| 591 | unsigned char *real_write_content = NULL;
|
| 592 | T_FLAGS_INFO *p_write = NULL;
|
| 593 |
|
| 594 | int index = 0;
|
| 595 | int block_num = 0;
|
| 596 | int already_write = 0;
|
| 597 |
|
| 598 | flags_info.magic_start = FLAGS_MAGIC;
|
| 599 |
|
| 600 | flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
|
| 601 | flags_info.boot_fota_flag.fota_status = 0;
|
| 602 | flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
|
| 603 | flags_info.boot_fota_flag.system.try_cnt = 0;
|
| 604 | flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
|
| 605 | flags_info.boot_fota_flag.system2.try_cnt = 0;
|
| 606 |
|
| 607 | flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
|
| 608 |
|
| 609 | flags_info.ubifs_status.fs_status = 0;
|
| 610 |
|
| 611 | flags_info.magic_end = FLAGS_MAGIC;
|
| 612 |
|
| 613 | ret = mtd_get(PARTITION_NAME_FLAGS, DEVICE_MTD, mtd_path, MAX_PATH_LEN);
|
| 614 | if (ret < 0)
|
| 615 | {
|
| 616 | flags_err("partition [%s] not found", PARTITION_NAME_FLAGS);
|
| 617 |
|
| 618 | return -1;
|
| 619 | }
|
| 620 |
|
| 621 | if ((fd_dst = open(mtd_path, O_RDWR | O_SYNC)) < 0)
|
| 622 | {
|
| 623 | flags_err("open flash error, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 624 |
|
| 625 | return -1;
|
| 626 | }
|
| 627 |
|
| 628 | if (ioctl(fd_dst, MEMGETINFO, &meminfo) != 0)
|
| 629 | {
|
| 630 | flags_err("get flash info error, errno=%d, strerror=[%s]", errno, strerror(errno));
|
| 631 |
|
| 632 | goto error;
|
| 633 | }
|
| 634 |
|
| 635 | mtd_info.parti_file_desc = fd_dst;
|
| 636 | mtd_info.mtd_blocksize = meminfo.erasesize;
|
| 637 | mtd_info.mtd_oobsize = meminfo.oobsize;
|
| 638 | mtd_info.mtd_pageperblock = meminfo.erasesize / meminfo.writesize;
|
| 639 | mtd_info.mtd_pagesize = meminfo.writesize;
|
| 640 | mtd_info.mtd_totalsize = meminfo.size;
|
| 641 |
|
| 642 | block_num = mtd_info.mtd_totalsize / mtd_info.mtd_blocksize;
|
| 643 |
|
| 644 | real_write_content = (unsigned char *)malloc(mtd_info.mtd_blocksize);
|
| 645 | if (NULL == real_write_content)
|
| 646 | {
|
| 647 | flags_err("malloc for block fail");
|
| 648 |
|
| 649 | goto error;
|
| 650 | }
|
| 651 |
|
| 652 | memset(real_write_content, 0xFF, mtd_info.mtd_blocksize);
|
| 653 | memcpy(real_write_content, (char *)(&flags_info), sizeof(T_FLAGS_INFO));
|
| 654 |
|
| 655 | for (index = 0; index < block_num; index++)
|
| 656 | {
|
| 657 | if (already_write >= FLAGS_INIT_VALID_BLOCKS_NUM)
|
| 658 | {
|
| 659 | break;
|
| 660 | }
|
| 661 |
|
| 662 | ret = write_flags_info(&mtd_info, index, real_write_content, mtd_info.mtd_blocksize);
|
| 663 | if (ret == 0)
|
| 664 | {
|
| 665 | already_write++;
|
| 666 | flags_log("write init valid block num: %d", already_write);
|
| 667 |
|
| 668 | continue;
|
| 669 | }
|
| 670 | else if (BAD_BLOCK == ret)
|
| 671 | {
|
| 672 | continue;
|
| 673 | }
|
| 674 | }
|
| 675 |
|
| 676 | if (already_write >= 2)
|
| 677 | {
|
| 678 | flags_log("init system status success, alread write block: %d", already_write);
|
| 679 | ret = 0;
|
| 680 | }
|
| 681 | else
|
| 682 | {
|
| 683 | flags_log("init system status fail, alread write block: %d", already_write);
|
| 684 | ret = -1;
|
| 685 | }
|
| 686 |
|
| 687 | goto end;
|
| 688 |
|
| 689 | error:
|
| 690 | ret = -1;
|
| 691 |
|
| 692 | end:
|
| 693 | close(fd_dst);
|
| 694 |
|
| 695 | if(NULL != real_write_content)
|
| 696 | {
|
| 697 | free(real_write_content);
|
| 698 | real_write_content = NULL;
|
| 699 | }
|
| 700 |
|
| 701 | return ret;
|
| 702 | }
|
| 703 |
|
| 704 |
|
| 705 | int flags_get(T_FLAGS_INFO *p_flags_info)
|
| 706 | {
|
| 707 | T_FLAGS_INFO main_flag = {0};
|
| 708 | T_FLAGS_INFO backup_flag = {0};
|
| 709 | int main_index = 0;
|
| 710 | int backup_index = 1;
|
| 711 |
|
| 712 | if (NULL == p_flags_info)
|
| 713 | {
|
| 714 | flags_err("invalid param NULL");
|
| 715 |
|
| 716 | return -1;
|
| 717 | }
|
| 718 |
|
| 719 | if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
|
| 720 | {
|
| 721 | flags_err("get flags info fail");
|
| 722 |
|
| 723 | return -1;
|
| 724 | }
|
| 725 |
|
| 726 | if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
|
| 727 | {
|
| 728 | copy_flags_info(p_flags_info, &main_flag);
|
| 729 |
|
| 730 | return 0;
|
| 731 | }
|
| 732 |
|
| 733 | if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
|
| 734 | {
|
| 735 | copy_flags_info(p_flags_info, &backup_flag);
|
| 736 |
|
| 737 | return 0;
|
| 738 | }
|
| 739 |
|
| 740 | flags_err("do not find valid flags info");
|
| 741 |
|
| 742 | return -1;
|
| 743 | }
|
| 744 |
|
| 745 |
|
| 746 | int flags_set(T_FLAGS_INFO *p_flags_info)
|
| 747 | {
|
| 748 | T_FLAGS_INFO main_flag = {0};
|
| 749 | T_FLAGS_INFO backup_flag = {0};
|
| 750 | int main_index = 0;
|
| 751 | int backup_index = 1;
|
| 752 |
|
| 753 | if (NULL == p_flags_info)
|
| 754 | {
|
| 755 | flags_err("invalid param NULL");
|
| 756 |
|
| 757 | return -1;
|
| 758 | }
|
| 759 |
|
| 760 | if ((FLAGS_MAGIC != p_flags_info->magic_start) || (FLAGS_MAGIC != p_flags_info->magic_end))
|
| 761 | {
|
| 762 | flags_err("invalid magic");
|
| 763 |
|
| 764 | return -1;
|
| 765 | }
|
| 766 |
|
| 767 | if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
|
| 768 | {
|
| 769 | flags_err("get flags info fail");
|
| 770 |
|
| 771 | return -1;
|
| 772 | }
|
| 773 |
|
| 774 | if (set_flags_info(p_flags_info, &main_index, &backup_index) != 0)
|
| 775 | {
|
| 776 | flags_err("set ubifs status fail");
|
| 777 |
|
| 778 | return -1;
|
| 779 | }
|
| 780 |
|
| 781 | return 0;
|
| 782 | }
|
| 783 |
|
| 784 |
|
| 785 | int flags_get_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
|
| 786 | {
|
| 787 | T_FLAGS_INFO main_flag = {0};
|
| 788 | T_FLAGS_INFO backup_flag = {0};
|
| 789 | int main_index = 0;
|
| 790 | int backup_index = 1;
|
| 791 |
|
| 792 | if (NULL == p_ubifs_status)
|
| 793 | {
|
| 794 | flags_err("invalid param NULL");
|
| 795 |
|
| 796 | return -1;
|
| 797 | }
|
| 798 |
|
| 799 | if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
|
| 800 | {
|
| 801 | flags_err("get flags info fail");
|
| 802 |
|
| 803 | return -1;
|
| 804 | }
|
| 805 |
|
| 806 | if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
|
| 807 | {
|
| 808 | p_ubifs_status->fs_status = main_flag.ubifs_status.fs_status;
|
| 809 | strncpy(p_ubifs_status->fs_mtd_name, main_flag.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));
|
| 810 | strncpy(p_ubifs_status->fs_ubi_vol_name, main_flag.ubifs_status.fs_ubi_vol_name, sizeof(p_ubifs_status->fs_ubi_vol_name));
|
| 811 |
|
| 812 | return 0;
|
| 813 | }
|
| 814 |
|
| 815 | if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
|
| 816 | {
|
| 817 | p_ubifs_status->fs_status = backup_flag.ubifs_status.fs_status;
|
| 818 | strncpy(p_ubifs_status->fs_mtd_name, backup_flag.ubifs_status.fs_mtd_name, sizeof(p_ubifs_status->fs_mtd_name));
|
| 819 | strncpy(p_ubifs_status->fs_ubi_vol_name, backup_flag.ubifs_status.fs_ubi_vol_name, sizeof(p_ubifs_status->fs_ubi_vol_name));
|
| 820 |
|
| 821 | return 0;
|
| 822 | }
|
| 823 |
|
| 824 | flags_err("do not find valid flags info");
|
| 825 |
|
| 826 | return -1;
|
| 827 | }
|
| 828 |
|
| 829 |
|
| 830 | int flags_set_ubifs_status(T_UBIFS_STATUS *p_ubifs_status)
|
| 831 | {
|
| 832 | T_FLAGS_INFO main_flag = {0};
|
| 833 | T_FLAGS_INFO backup_flag = {0};
|
| 834 | int main_index = 0;
|
| 835 | int backup_index = 1;
|
| 836 |
|
| 837 | T_FLAGS_INFO *flags_info = NULL;
|
| 838 |
|
| 839 | if (NULL == p_ubifs_status)
|
| 840 | {
|
| 841 | flags_err("invalid param NULL");
|
| 842 |
|
| 843 | return -1;
|
| 844 | }
|
| 845 |
|
| 846 | if (get_flags_info(&main_flag, &main_index, &backup_flag, &backup_index) != 0)
|
| 847 | {
|
| 848 | flags_err("get flags info fail");
|
| 849 |
|
| 850 | return -1;
|
| 851 | }
|
| 852 |
|
| 853 | if ((FLAGS_MAGIC == main_flag.magic_start) && (FLAGS_MAGIC == main_flag.magic_end))
|
| 854 | {
|
| 855 | flags_info = &main_flag;
|
| 856 | }
|
| 857 | else if ((FLAGS_MAGIC == backup_flag.magic_start) && (FLAGS_MAGIC == backup_flag.magic_end))
|
| 858 | {
|
| 859 | flags_info = &backup_flag;
|
| 860 | }
|
| 861 | else
|
| 862 | {
|
| 863 | flags_err("get ubifs status invalid");
|
| 864 |
|
| 865 | return -1;
|
| 866 | }
|
| 867 |
|
| 868 | memcpy(&(flags_info->ubifs_status), p_ubifs_status, sizeof(T_UBIFS_STATUS));
|
| 869 |
|
| 870 | if (set_flags_info(flags_info, &main_index, &backup_index) != 0)
|
| 871 | {
|
| 872 | flags_err("set ubifs status fail");
|
| 873 |
|
| 874 | return -1;
|
| 875 | }
|
| 876 |
|
| 877 | return 0;
|
| 878 | }
|
| 879 |
|
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 880 | unsigned int flags_get_nvroflag(void)
|
| 881 | {
|
| 882 | T_FLAGS_INFO t_flag = {0};
|
| 883 |
|
| 884 | if (flags_get(&t_flag) != 0)
|
| 885 | return NVRO_INVALID;
|
| 886 | return t_flag.nvro_flag;
|
| 887 | }
|
| 888 |
|
| 889 | int flags_set_nvroflag(unsigned int flag)
|
| 890 | {
|
| 891 | T_FLAGS_INFO t_flag = {0};
|
| 892 |
|
| 893 | if (flags_get(&t_flag) != 0)
|
| 894 | return -1;
|
| 895 | if (t_flag.nvro_flag == flag)
|
| 896 | return 0;
|
| 897 | if (flag == NVRO_RESTORING)
|
| 898 | {
|
| 899 | if (t_flag.nvro_flag != NVRO_BACKED_UP)
|
| 900 | {
|
| 901 | printf("[error]flags nvro only NVRO_BACKED_UP switch to NVRO_RESTORING\n");
|
| 902 | return -1;
|
| 903 | }
|
| 904 | }
|
| 905 | t_flag.nvro_flag = flag;
|
| 906 |
|
| 907 | return flags_set(&t_flag);
|
| 908 | }
|
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 909 |
|
| 910 | int flags_get_current_system()
|
| 911 | {
|
| 912 | int current = get_current_system();
|
| 913 |
|
| 914 | if (current == 1)
|
| 915 | {
|
| 916 | return DUAL_SYSTEM;
|
| 917 | }
|
| 918 | else if (current == 2)
|
| 919 | {
|
| 920 | return DUAL_SYSTEM2;
|
| 921 | }
|
| 922 |
|
| 923 | return -1;
|
| 924 | }
|
| 925 |
|