blob: e035ab9821da7724de261300906590b549816654 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/**
2* @file main.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 <string.h>
21#include <sys/types.h>
22#include <errno.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <sys/ioctl.h>
26#include <dirent.h>
27#include <getopt.h>
28#include <unistd.h>
29
30#include "pub_flags.h"
31#include "flags_api.h"
32
33
34/*******************************************************************************
35 * Macro definitions *
36 ******************************************************************************/
xf.liaa4d92f2023-09-13 00:18:58 -070037#define FLAGS_UTILS_GET _IOWR('b', 1, T_FLAGS_INFO)
38#define FLAGS_UTILS_SET _IOWR('b', 2, T_FLAGS_INFO)
xf.li6c8fc1e2023-08-12 00:11:09 -070039
40
41/*******************************************************************************
42 * Type definitions *
43 ******************************************************************************/
44typedef struct
45{
46 int option_value;
47 int (*func)(char *);
48} option_handle_t;
49
50
51
52/*******************************************************************************
53 * Static function declarations *
54 ******************************************************************************/
55static int excute_command_help(char *option_para);
56static int excute_command_init(char *option_para);
57static int excute_command_get(char *option_para);
58static int excute_command_set(char *option_para);
xf.lice873192023-11-08 17:10:35 -080059static int excute_command_switch(char *option_para);
xf.li6c8fc1e2023-08-12 00:11:09 -070060static int excute_command_get_ubifs_status(char *option_para);
61static int excute_command_set_ubifs_status(char *option_para);
62
xf.liaa4d92f2023-09-13 00:18:58 -070063static int excute_command_kernel_get(char *option_para);
64static int excute_command_kernel_set(char *option_para);
65
xf.lice873192023-11-08 17:10:35 -080066static int excute_command_get_nocrc(char *option_para);
67static int excute_command_set_nocrc(char *option_para);
68
xf.li6c8fc1e2023-08-12 00:11:09 -070069
70/*******************************************************************************
71 * Global variable declarations *
72 ******************************************************************************/
xf.lice873192023-11-08 17:10:35 -080073static char * g_short_string = "higswubtqac:";
xf.li6c8fc1e2023-08-12 00:11:09 -070074
75static struct option g_long_options[] =
76{
77 {"help", no_argument, NULL, 'h'},
78 {"init", no_argument, NULL, 'i'},
79 {"get", no_argument, NULL, 'g'},
80 {"set", no_argument, NULL, 's'},
xf.lice873192023-11-08 17:10:35 -080081 {"switch", no_argument, NULL, 'w'},
xf.li6c8fc1e2023-08-12 00:11:09 -070082 {"get-ubifs-status", no_argument, NULL, 'u'},
83 {"set-ubifs-status", no_argument, NULL, 'b'},
xf.liaa4d92f2023-09-13 00:18:58 -070084 {"kernel-get", no_argument, NULL, 't'},
85 {"kernel-set", no_argument, NULL, 'q'},
xf.lice873192023-11-08 17:10:35 -080086 {"get-nocrc", no_argument, NULL, 'a'},
87 {"set-nocrc", required_argument, NULL, 'c'},
xf.li6c8fc1e2023-08-12 00:11:09 -070088 {0, 0, 0, 0}
89};
90
91static option_handle_t g_option_handle[] =
92{
93 {'h', excute_command_help},
94 {'i', excute_command_init},
95 {'g', excute_command_get},
96 {'s', excute_command_set},
xf.lice873192023-11-08 17:10:35 -080097 {'w', excute_command_switch},
xf.li6c8fc1e2023-08-12 00:11:09 -070098 {'u', excute_command_get_ubifs_status},
xf.liaa4d92f2023-09-13 00:18:58 -070099 {'b', excute_command_set_ubifs_status},
100 {'t', excute_command_kernel_get},
xf.lice873192023-11-08 17:10:35 -0800101 {'q', excute_command_kernel_set},
102 {'a', excute_command_get_nocrc},
103 {'c', excute_command_set_nocrc}
xf.li6c8fc1e2023-08-12 00:11:09 -0700104};
105
106
107/*******************************************************************************
108 * Static function define *
109 ******************************************************************************/
110static void usage(void)
111{
xf.lice873192023-11-08 17:10:35 -0800112 printf("flags_tool [-higswub] \n "
xf.li6c8fc1e2023-08-12 00:11:09 -0700113 " -h, --help show usage \n"
114 " -i, --init init flags \n"
115 " -g, --get get flags data \n"
116 " -s, --set set flags data \n"
xf.lice873192023-11-08 17:10:35 -0800117 " -w, --switch system A/B switch \n"
xf.li6c8fc1e2023-08-12 00:11:09 -0700118 " -u, --get-ubifs-status get ubifs status \n"
xf.liaa4d92f2023-09-13 00:18:58 -0700119 " -b, --set-ubifs-status set ubifs status \n"
120 " -t, --kernel-get kernel-get flags data \n"
xf.lice873192023-11-08 17:10:35 -0800121 " -q, --kernel-set kernel-set flags data \n"
122 " -a, --get-nocrc get-nocrc flags data \n"
123 " -c, --set-nocrc set-nocrc flags data \n");
xf.li6c8fc1e2023-08-12 00:11:09 -0700124
125 return;
126}
127
128
129static int excute_command_help(char *option_para)
130{
131 usage();
132
133 return 0;
134}
135
136
137static int excute_command_init(char *option_para)
138{
139 if (flags_init() != 0)
140 {
141 printf("Err: flags init fail \n");
142
143 return -1;
144 }
145 else
146 {
147 printf("Log: flags init success \n");
148
149 return 0;
150 }
151}
152
153
154static int excute_command_get(char *option_para)
155{
xf.lice873192023-11-08 17:10:35 -0800156 int i = 0;
xf.li6c8fc1e2023-08-12 00:11:09 -0700157 T_FLAGS_INFO flags_info = {0};
158
159 if (flags_get(&flags_info) != 0)
160 {
161 printf("Err: flags get fail \n");
162
163 return -1;
164 }
165 else
166 {
167 printf("magic_start = 0x%x \n", flags_info.magic_start);
168
169 printf("boot_fota_flag.boot_to = 0x%x \n", flags_info.boot_fota_flag.boot_to);
170 printf("boot_fota_flag.fota_status = %u \n", flags_info.boot_fota_flag.fota_status);
171 printf("boot_fota_flag.system.status = 0x%x \n", flags_info.boot_fota_flag.system.status);
172 printf("boot_fota_flag.system.try_cnt = %d \n", flags_info.boot_fota_flag.system.try_cnt);
173 printf("boot_fota_flag.system2.status = 0x%x \n", flags_info.boot_fota_flag.system2.status);
174 printf("boot_fota_flag.system2.try_cnt = %d \n", flags_info.boot_fota_flag.system2.try_cnt);
175
176 printf("boot_env.dualsys_type = 0x%x \n", flags_info.boot_env.dualsys_type);
177 printf("boot_env.system_boot_env = %s \n", flags_info.boot_env.system_boot_env);
178 printf("boot_env.system2_boot_env = %s \n", flags_info.boot_env.system2_boot_env);
179
180 printf("ubifs_status.fs_status = %d \n", flags_info.ubifs_status.fs_status);
181 printf("ubifs_status.fs_mtd_name = %s \n", flags_info.ubifs_status.fs_mtd_name);
182 printf("ubifs_status.fs_ubi_vol_name = %s \n", flags_info.ubifs_status.fs_ubi_vol_name);
183
xf.lice873192023-11-08 17:10:35 -0800184 printf("nvro_flag = %u \n", flags_info.nvro_flag);
185
186 printf("ota_system = %d \n", flags_info.ota_system);
187
188 for (i = 0; i < OTA_PARTITION_NUM_MAX; i++)
189 {
190 printf("index=%d, mtdnum=%d, len=%u \n", i, flags_info.ota_partiton_info[i].mtdnum, flags_info.ota_partiton_info[i].len);
191 }
192
xf.li6c8fc1e2023-08-12 00:11:09 -0700193 printf("magic_end = 0x%x \n", flags_info.magic_end);
194
195 return 0;
196 }
197}
198
199
200static int excute_command_set(char *option_para)
201{
202 T_FLAGS_INFO flags_info = {0};
203
204 char system_boot_env[128] = "bootEnv1";
205 char system2_boot_env[128] = "2bootEnv";
206
207 char fs_mtd_name[16] = "nameMTD";
208 char fs_ubi_vol_name[16] = "nameVOL";
209
210 flags_info.magic_start = FLAGS_MAGIC;
211
212 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
213 flags_info.boot_fota_flag.fota_status = 0;
214 flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
215 flags_info.boot_fota_flag.system.try_cnt = 5;
216 flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
217 flags_info.boot_fota_flag.system2.try_cnt = 13;
218
219 flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
220 strncpy(flags_info.boot_env.system_boot_env, system_boot_env, sizeof(flags_info.boot_env.system_boot_env));
221 strncpy(flags_info.boot_env.system2_boot_env, system2_boot_env, sizeof(flags_info.boot_env.system2_boot_env));
222
223 flags_info.ubifs_status.fs_status = 0;
224 strncpy(flags_info.ubifs_status.fs_mtd_name, fs_mtd_name, sizeof(flags_info.ubifs_status.fs_mtd_name));
225 strncpy(flags_info.ubifs_status.fs_ubi_vol_name, fs_ubi_vol_name, sizeof(flags_info.ubifs_status.fs_ubi_vol_name));
226
227 flags_info.magic_end = FLAGS_MAGIC;
228
229 if (flags_set(&flags_info) != 0)
230 {
231 printf("Err: set flags fail \n");
232
233 return -1;
234 }
235 else
236 {
237 printf("Log: set flags success \n");
238
239 return 0;
240 }
241}
242
xf.lice873192023-11-08 17:10:35 -0800243static int excute_command_switch(char *option_para)
244{
245 T_FLAGS_INFO flags_info = {0};
246 int ret = -1;
247
248 ret = flags_get(&flags_info);
249 if (ret < 0)
250 {
251 printf("flags switch flags_get fail\n");
252 return ret;
253 }
254 if (flags_info.boot_fota_flag.boot_to == DUAL_SYSTEM)
255 {
256 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM2; //A->B
257 printf("flags switch system A to system B\n");
258 }
259 else
260 {
261 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM; //B->A
262 printf("flags switch system B to system A\n");
263 }
264 ret = flags_set(&flags_info);
265 if (ret < 0)
266 {
267 printf("flags switch flags_set fail\n");
268 return ret;
269 }
270 printf("flags AB system switch sucess\n");
271
272 return 0;
273}
xf.li6c8fc1e2023-08-12 00:11:09 -0700274
275static int excute_command_get_ubifs_status(char *option_para)
276{
277 T_UBIFS_STATUS ubifs_status = {0};
278
279 if (flags_get_ubifs_status(&ubifs_status) != 0)
280 {
281 printf("Err: get ubifs status fail \n");
282
283 return -1;
284 }
285 else
286 {
287 printf("ubifs_status.fs_status = %d \n", ubifs_status.fs_status);
288 printf("ubifs_status.fs_mtd_name = %s \n", ubifs_status.fs_mtd_name);
289 printf("ubifs_status.fs_ubi_vol_name = %s \n", ubifs_status.fs_ubi_vol_name);
290
291 return 0;
292 }
293}
294
295
296static int excute_command_set_ubifs_status(char *option_para)
297{
298 T_UBIFS_STATUS ubifs_status = {0};
299
300 char fs_mtd_name[16] = "mtdName";
301 char fs_ubi_vol_name[16] = "volName";
302
303 ubifs_status.fs_status = 2;
304 strncpy(ubifs_status.fs_mtd_name, fs_mtd_name, sizeof(ubifs_status.fs_mtd_name));
305 strncpy(ubifs_status.fs_ubi_vol_name, fs_ubi_vol_name, sizeof(ubifs_status.fs_ubi_vol_name));
306
307 if (flags_set_ubifs_status(&ubifs_status) != 0)
308 {
309 printf("Err: set ubifs status fail \n");
310
311 return -1;
312 }
313 else
314 {
315 printf("Log: set ubifs status success \n");
316
317 return 0;
318 }
319}
320
321
xf.liaa4d92f2023-09-13 00:18:58 -0700322static int excute_command_kernel_get(char *option_para)
323{
xf.lice873192023-11-08 17:10:35 -0800324 int i = 0;
xf.liaa4d92f2023-09-13 00:18:58 -0700325 int fd = -1;
326 T_FLAGS_INFO flags_info = {0};
327
328 fd = open("/dev/chardevnode0", O_RDWR);
329 if (-1 == open)
330 {
331 printf("kernel get open chardevnode fail, errno=%d \n", errno);
332 return -1;
333 }
334
335 if (ioctl(fd, FLAGS_UTILS_GET, &flags_info) < 0)
336 {
337 printf("Err: ioctl cmd(0x%x) fail, errno=%d \n", FLAGS_UTILS_GET, errno);
338
339 close(fd);
340 return -1;
341 }
342
343 printf("magic_start = 0x%x \n", flags_info.magic_start);
344
345 printf("boot_fota_flag.boot_to = 0x%x \n", flags_info.boot_fota_flag.boot_to);
346 printf("boot_fota_flag.fota_status = %u \n", flags_info.boot_fota_flag.fota_status);
347 printf("boot_fota_flag.system.status = 0x%x \n", flags_info.boot_fota_flag.system.status);
348 printf("boot_fota_flag.system.try_cnt = %d \n", flags_info.boot_fota_flag.system.try_cnt);
349 printf("boot_fota_flag.system2.status = 0x%x \n", flags_info.boot_fota_flag.system2.status);
350 printf("boot_fota_flag.system2.try_cnt = %d \n", flags_info.boot_fota_flag.system2.try_cnt);
351
352 printf("boot_env.dualsys_type = 0x%x \n", flags_info.boot_env.dualsys_type);
353 printf("boot_env.system_boot_env = %s \n", flags_info.boot_env.system_boot_env);
354 printf("boot_env.system2_boot_env = %s \n", flags_info.boot_env.system2_boot_env);
355
356 printf("ubifs_status.fs_status = %d \n", flags_info.ubifs_status.fs_status);
357 printf("ubifs_status.fs_mtd_name = %s \n", flags_info.ubifs_status.fs_mtd_name);
358 printf("ubifs_status.fs_ubi_vol_name = %s \n", flags_info.ubifs_status.fs_ubi_vol_name);
359
xf.lice873192023-11-08 17:10:35 -0800360 printf("nvro_flag = %u \n", flags_info.nvro_flag);
361
362 printf("ota_system = %d \n", flags_info.ota_system);
363
364 for (i = 0; i < OTA_PARTITION_NUM_MAX; i++)
365 {
366 printf("index=%d, mtdnum=%d, len=%u \n", i, flags_info.ota_partiton_info[i].mtdnum, flags_info.ota_partiton_info[i].len);
367 }
368
xf.liaa4d92f2023-09-13 00:18:58 -0700369 printf("magic_end = 0x%x \n", flags_info.magic_end);
370
371 close(fd);
372 return 0;
373}
374
375
376static int excute_command_kernel_set(char *option_para)
377{
378 int fd = -1;
379 T_FLAGS_INFO flags_info = {0};
380
381 char system_boot_env[128] = "bootEnv1";
382 char system2_boot_env[128] = "2bootEnv";
383
384 char fs_mtd_name[16] = "nameMTD";
385 char fs_ubi_vol_name[16] = "nameVOL";
386
387 fd = open("/dev/chardevnode0", O_RDWR);
388 if (-1 == open)
389 {
390 printf("kernel set open chardevnode fail, errno=%d \n", errno);
391 return -1;
392 }
393
394 flags_info.magic_start = FLAGS_MAGIC;
395
396 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
397 flags_info.boot_fota_flag.fota_status = 0;
398 flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
399 flags_info.boot_fota_flag.system.try_cnt = 5;
400 flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
401 flags_info.boot_fota_flag.system2.try_cnt = 13;
402
403 flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
404 strncpy(flags_info.boot_env.system_boot_env, system_boot_env, sizeof(flags_info.boot_env.system_boot_env));
405 strncpy(flags_info.boot_env.system2_boot_env, system2_boot_env, sizeof(flags_info.boot_env.system2_boot_env));
406
407 flags_info.ubifs_status.fs_status = 0;
408 strncpy(flags_info.ubifs_status.fs_mtd_name, fs_mtd_name, sizeof(flags_info.ubifs_status.fs_mtd_name));
409 strncpy(flags_info.ubifs_status.fs_ubi_vol_name, fs_ubi_vol_name, sizeof(flags_info.ubifs_status.fs_ubi_vol_name));
410
411 flags_info.magic_end = FLAGS_MAGIC;
412
413 if (ioctl(fd, FLAGS_UTILS_SET, &flags_info) < 0)
414 {
415 printf("Err: ioctl cmd(0x%x) fail, errno=%d \n", FLAGS_UTILS_SET, errno);
416
417 close(fd);
418 return -1;
419 }
420
421 printf("Log: kernel set flags success \n");
422
423 close(fd);
424 return 0;
425}
426
427
xf.lice873192023-11-08 17:10:35 -0800428static int excute_command_get_nocrc(char *option_para)
429{
430 int i = 0;
431 T_FLAGS_INFO flags_info = {0};
432
433 if (flags_get_nocrc(&flags_info) != 0)
434 {
435 printf("Err: flags get fail \n");
436 return -1;
437 }
438 else
439 {
440 printf("magic_start = 0x%x \n", flags_info.magic_start);
441
442 printf("boot_fota_flag.boot_to = 0x%x \n", flags_info.boot_fota_flag.boot_to);
443 printf("boot_fota_flag.fota_status = %u \n", flags_info.boot_fota_flag.fota_status);
444 printf("boot_fota_flag.system.status = 0x%x \n", flags_info.boot_fota_flag.system.status);
445 printf("boot_fota_flag.system.try_cnt = %d \n", flags_info.boot_fota_flag.system.try_cnt);
446 printf("boot_fota_flag.system2.status = 0x%x \n", flags_info.boot_fota_flag.system2.status);
447 printf("boot_fota_flag.system2.try_cnt = %d \n", flags_info.boot_fota_flag.system2.try_cnt);
448
449 printf("boot_env.dualsys_type = 0x%x \n", flags_info.boot_env.dualsys_type);
450 printf("boot_env.system_boot_env = %s \n", flags_info.boot_env.system_boot_env);
451 printf("boot_env.system2_boot_env = %s \n", flags_info.boot_env.system2_boot_env);
452
453 printf("ubifs_status.fs_status = %d \n", flags_info.ubifs_status.fs_status);
454 printf("ubifs_status.fs_mtd_name = %s \n", flags_info.ubifs_status.fs_mtd_name);
455 printf("ubifs_status.fs_ubi_vol_name = %s \n", flags_info.ubifs_status.fs_ubi_vol_name);
456
457 printf("nvro_flag = %u \n", flags_info.nvro_flag);
458 printf("crc32 = %u \n", flags_info.crc32);
459
460 printf("ota_system = %d \n", flags_info.ota_system);
461
462 for (i = 0; i < OTA_PARTITION_NUM_MAX; i++)
463 {
464 printf("index=%d, mtdnum=%d, len=%u \n", i, flags_info.ota_partiton_info[i].mtdnum, flags_info.ota_partiton_info[i].len);
465 }
466
467 printf("magic_end = 0x%x \n", flags_info.magic_end);
468
469 return 0;
470 }
471}
472
473
474static int excute_command_set_nocrc(char *option_para)
475{
476 T_FLAGS_INFO flags_info = {0};
477
478 const char delim[2] = ",";
479 char *token_1 = NULL;
480 char *token_2 = NULL;
481 char *token_3 = NULL;
482 char *token_4 = NULL;
483 char *token_5 = NULL;
484 char *token_6 = NULL;
485
486 if (NULL == option_para)
487 {
488 usage();
489 printf("Command input invalid value! null option parameters! \n");
490 return -1;
491 }
492
493 if (flags_get_nocrc(&flags_info) != 0)
494 {
495 printf("Err: flags get fail \n");
496 return -1;
497 }
498
499 token_1 = strtok(option_para, delim);
500 if (NULL == token_1)
501 {
502 printf("token_1 delim fail: NULL \n");
503 return -1;
504 }
505
506 token_2 = strtok(NULL, delim);
507 if (NULL == token_2)
508 {
509 printf("token_2 delim fail: NULL \n");
510 return -1;
511 }
512
513 token_3 = strtok(NULL, delim);
514 if (NULL == token_3)
515 {
516 printf("token_3 delim fail: NULL \n");
517 return -1;
518 }
519
520 token_4 = strtok(NULL, delim);
521 if (NULL == token_4)
522 {
523 printf("token_4 delim fail: NULL \n");
524 return -1;
525 }
526
527 token_5 = strtok(NULL, delim);
528 if (NULL == token_5)
529 {
530 printf("token_5 delim fail: NULL \n");
531 return -1;
532 }
533
534 token_6 = strtok(NULL, delim);
535 if (NULL != token_6)
536 {
537 printf("too many command input, fail \n");
538 return -1;
539 }
540
541 flags_info.boot_fota_flag.system2.try_cnt = atoi(token_1);
542
543 strncpy(flags_info.boot_env.system_boot_env, token_2, sizeof(flags_info.boot_env.system_boot_env));
544
545 flags_info.ubifs_status.fs_status = atoi(token_3);
546 strncpy(flags_info.ubifs_status.fs_mtd_name, token_4, sizeof(flags_info.ubifs_status.fs_mtd_name));
547
548 flags_info.magic_end = atoi(token_5);
549
550 if (flags_set_nocrc(&flags_info) != 0)
551 {
552 printf("Err: set flags fail \n");
553 return -1;
554 }
555 else
556 {
557 printf("Log: set flags success \n");
558 return 0;
559 }
560}
561
562
xf.li6c8fc1e2023-08-12 00:11:09 -0700563/*******************************************************************************
564 * Global function declarations *
565 ******************************************************************************/
566int main(int argc, char *argv[])
567{
568 int i = 0;
569 int option_index = 0;
570 int cmd_num = 0;
571
572 int ret = -1;
573 int ch = -1;
574
575 printf("Log: build date: %s %s \n", __DATE__, __TIME__);
576
577 while ((ch = getopt_long(argc, argv, g_short_string, g_long_options, &option_index)) != -1)
578 {
579 for (i = 0; i < sizeof(g_option_handle) / sizeof(option_handle_t); i++)
580 {
581 if (ch != g_option_handle[i].option_value)
582 {
583 continue;
584 }
585
586 cmd_num++;
587
588 if (NULL == g_option_handle[i].func)
589 {
590 printf("Err: command short string is: %c, but option handle func is NULL \n", ch);
591 break;
592 }
593
594 ret = g_option_handle[i].func(optarg);
595 if (ret < 0)
596 {
597 ret = -1;
598 goto end;
599 }
600 }
601 }
602
603 if (0 == cmd_num)
604 {
605 printf("Err: can not find valid command \n");
606 usage();
607 ret = -1;
608 goto end;
609 }
610
611 ret = 0;
612
613end:
614 return ret ;
615}
616