blob: 7e64a7832f5eae3f39b2135470d5b2680443d89b [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);
59static int excute_command_get_ubifs_status(char *option_para);
60static int excute_command_set_ubifs_status(char *option_para);
61
xf.liaa4d92f2023-09-13 00:18:58 -070062static int excute_command_kernel_get(char *option_para);
63static int excute_command_kernel_set(char *option_para);
64
xf.li6c8fc1e2023-08-12 00:11:09 -070065
66/*******************************************************************************
67 * Global variable declarations *
68 ******************************************************************************/
xf.liaa4d92f2023-09-13 00:18:58 -070069static char * g_short_string = "higsubtq";
xf.li6c8fc1e2023-08-12 00:11:09 -070070
71static struct option g_long_options[] =
72{
73 {"help", no_argument, NULL, 'h'},
74 {"init", no_argument, NULL, 'i'},
75 {"get", no_argument, NULL, 'g'},
76 {"set", no_argument, NULL, 's'},
77 {"get-ubifs-status", no_argument, NULL, 'u'},
78 {"set-ubifs-status", no_argument, NULL, 'b'},
xf.liaa4d92f2023-09-13 00:18:58 -070079 {"kernel-get", no_argument, NULL, 't'},
80 {"kernel-set", no_argument, NULL, 'q'},
xf.li6c8fc1e2023-08-12 00:11:09 -070081 {0, 0, 0, 0}
82};
83
84static option_handle_t g_option_handle[] =
85{
86 {'h', excute_command_help},
87 {'i', excute_command_init},
88 {'g', excute_command_get},
89 {'s', excute_command_set},
90 {'u', excute_command_get_ubifs_status},
xf.liaa4d92f2023-09-13 00:18:58 -070091 {'b', excute_command_set_ubifs_status},
92 {'t', excute_command_kernel_get},
93 {'q', excute_command_kernel_set}
xf.li6c8fc1e2023-08-12 00:11:09 -070094};
95
96
97/*******************************************************************************
98 * Static function define *
99 ******************************************************************************/
100static void usage(void)
101{
102 printf("flags_tool [-higsub] \n "
103 " -h, --help show usage \n"
104 " -i, --init init flags \n"
105 " -g, --get get flags data \n"
106 " -s, --set set flags data \n"
107 " -u, --get-ubifs-status get ubifs status \n"
xf.liaa4d92f2023-09-13 00:18:58 -0700108 " -b, --set-ubifs-status set ubifs status \n"
109 " -t, --kernel-get kernel-get flags data \n"
110 " -q, --kernel-set kernel-set flags data \n");
xf.li6c8fc1e2023-08-12 00:11:09 -0700111
112 return;
113}
114
115
116static int excute_command_help(char *option_para)
117{
118 usage();
119
120 return 0;
121}
122
123
124static int excute_command_init(char *option_para)
125{
126 if (flags_init() != 0)
127 {
128 printf("Err: flags init fail \n");
129
130 return -1;
131 }
132 else
133 {
134 printf("Log: flags init success \n");
135
136 return 0;
137 }
138}
139
140
141static int excute_command_get(char *option_para)
142{
143 T_FLAGS_INFO flags_info = {0};
144
145 if (flags_get(&flags_info) != 0)
146 {
147 printf("Err: flags get fail \n");
148
149 return -1;
150 }
151 else
152 {
153 printf("magic_start = 0x%x \n", flags_info.magic_start);
154
155 printf("boot_fota_flag.boot_to = 0x%x \n", flags_info.boot_fota_flag.boot_to);
156 printf("boot_fota_flag.fota_status = %u \n", flags_info.boot_fota_flag.fota_status);
157 printf("boot_fota_flag.system.status = 0x%x \n", flags_info.boot_fota_flag.system.status);
158 printf("boot_fota_flag.system.try_cnt = %d \n", flags_info.boot_fota_flag.system.try_cnt);
159 printf("boot_fota_flag.system2.status = 0x%x \n", flags_info.boot_fota_flag.system2.status);
160 printf("boot_fota_flag.system2.try_cnt = %d \n", flags_info.boot_fota_flag.system2.try_cnt);
161
162 printf("boot_env.dualsys_type = 0x%x \n", flags_info.boot_env.dualsys_type);
163 printf("boot_env.system_boot_env = %s \n", flags_info.boot_env.system_boot_env);
164 printf("boot_env.system2_boot_env = %s \n", flags_info.boot_env.system2_boot_env);
165
166 printf("ubifs_status.fs_status = %d \n", flags_info.ubifs_status.fs_status);
167 printf("ubifs_status.fs_mtd_name = %s \n", flags_info.ubifs_status.fs_mtd_name);
168 printf("ubifs_status.fs_ubi_vol_name = %s \n", flags_info.ubifs_status.fs_ubi_vol_name);
169
170 printf("magic_end = 0x%x \n", flags_info.magic_end);
171
172 return 0;
173 }
174}
175
176
177static int excute_command_set(char *option_para)
178{
179 T_FLAGS_INFO flags_info = {0};
180
181 char system_boot_env[128] = "bootEnv1";
182 char system2_boot_env[128] = "2bootEnv";
183
184 char fs_mtd_name[16] = "nameMTD";
185 char fs_ubi_vol_name[16] = "nameVOL";
186
187 flags_info.magic_start = FLAGS_MAGIC;
188
189 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
190 flags_info.boot_fota_flag.fota_status = 0;
191 flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
192 flags_info.boot_fota_flag.system.try_cnt = 5;
193 flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
194 flags_info.boot_fota_flag.system2.try_cnt = 13;
195
196 flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
197 strncpy(flags_info.boot_env.system_boot_env, system_boot_env, sizeof(flags_info.boot_env.system_boot_env));
198 strncpy(flags_info.boot_env.system2_boot_env, system2_boot_env, sizeof(flags_info.boot_env.system2_boot_env));
199
200 flags_info.ubifs_status.fs_status = 0;
201 strncpy(flags_info.ubifs_status.fs_mtd_name, fs_mtd_name, sizeof(flags_info.ubifs_status.fs_mtd_name));
202 strncpy(flags_info.ubifs_status.fs_ubi_vol_name, fs_ubi_vol_name, sizeof(flags_info.ubifs_status.fs_ubi_vol_name));
203
204 flags_info.magic_end = FLAGS_MAGIC;
205
206 if (flags_set(&flags_info) != 0)
207 {
208 printf("Err: set flags fail \n");
209
210 return -1;
211 }
212 else
213 {
214 printf("Log: set flags success \n");
215
216 return 0;
217 }
218}
219
220
221static int excute_command_get_ubifs_status(char *option_para)
222{
223 T_UBIFS_STATUS ubifs_status = {0};
224
225 if (flags_get_ubifs_status(&ubifs_status) != 0)
226 {
227 printf("Err: get ubifs status fail \n");
228
229 return -1;
230 }
231 else
232 {
233 printf("ubifs_status.fs_status = %d \n", ubifs_status.fs_status);
234 printf("ubifs_status.fs_mtd_name = %s \n", ubifs_status.fs_mtd_name);
235 printf("ubifs_status.fs_ubi_vol_name = %s \n", ubifs_status.fs_ubi_vol_name);
236
237 return 0;
238 }
239}
240
241
242static int excute_command_set_ubifs_status(char *option_para)
243{
244 T_UBIFS_STATUS ubifs_status = {0};
245
246 char fs_mtd_name[16] = "mtdName";
247 char fs_ubi_vol_name[16] = "volName";
248
249 ubifs_status.fs_status = 2;
250 strncpy(ubifs_status.fs_mtd_name, fs_mtd_name, sizeof(ubifs_status.fs_mtd_name));
251 strncpy(ubifs_status.fs_ubi_vol_name, fs_ubi_vol_name, sizeof(ubifs_status.fs_ubi_vol_name));
252
253 if (flags_set_ubifs_status(&ubifs_status) != 0)
254 {
255 printf("Err: set ubifs status fail \n");
256
257 return -1;
258 }
259 else
260 {
261 printf("Log: set ubifs status success \n");
262
263 return 0;
264 }
265}
266
267
xf.liaa4d92f2023-09-13 00:18:58 -0700268static int excute_command_kernel_get(char *option_para)
269{
270 int fd = -1;
271 T_FLAGS_INFO flags_info = {0};
272
273 fd = open("/dev/chardevnode0", O_RDWR);
274 if (-1 == open)
275 {
276 printf("kernel get open chardevnode fail, errno=%d \n", errno);
277 return -1;
278 }
279
280 if (ioctl(fd, FLAGS_UTILS_GET, &flags_info) < 0)
281 {
282 printf("Err: ioctl cmd(0x%x) fail, errno=%d \n", FLAGS_UTILS_GET, errno);
283
284 close(fd);
285 return -1;
286 }
287
288 printf("magic_start = 0x%x \n", flags_info.magic_start);
289
290 printf("boot_fota_flag.boot_to = 0x%x \n", flags_info.boot_fota_flag.boot_to);
291 printf("boot_fota_flag.fota_status = %u \n", flags_info.boot_fota_flag.fota_status);
292 printf("boot_fota_flag.system.status = 0x%x \n", flags_info.boot_fota_flag.system.status);
293 printf("boot_fota_flag.system.try_cnt = %d \n", flags_info.boot_fota_flag.system.try_cnt);
294 printf("boot_fota_flag.system2.status = 0x%x \n", flags_info.boot_fota_flag.system2.status);
295 printf("boot_fota_flag.system2.try_cnt = %d \n", flags_info.boot_fota_flag.system2.try_cnt);
296
297 printf("boot_env.dualsys_type = 0x%x \n", flags_info.boot_env.dualsys_type);
298 printf("boot_env.system_boot_env = %s \n", flags_info.boot_env.system_boot_env);
299 printf("boot_env.system2_boot_env = %s \n", flags_info.boot_env.system2_boot_env);
300
301 printf("ubifs_status.fs_status = %d \n", flags_info.ubifs_status.fs_status);
302 printf("ubifs_status.fs_mtd_name = %s \n", flags_info.ubifs_status.fs_mtd_name);
303 printf("ubifs_status.fs_ubi_vol_name = %s \n", flags_info.ubifs_status.fs_ubi_vol_name);
304
305 printf("magic_end = 0x%x \n", flags_info.magic_end);
306
307 close(fd);
308 return 0;
309}
310
311
312static int excute_command_kernel_set(char *option_para)
313{
314 int fd = -1;
315 T_FLAGS_INFO flags_info = {0};
316
317 char system_boot_env[128] = "bootEnv1";
318 char system2_boot_env[128] = "2bootEnv";
319
320 char fs_mtd_name[16] = "nameMTD";
321 char fs_ubi_vol_name[16] = "nameVOL";
322
323 fd = open("/dev/chardevnode0", O_RDWR);
324 if (-1 == open)
325 {
326 printf("kernel set open chardevnode fail, errno=%d \n", errno);
327 return -1;
328 }
329
330 flags_info.magic_start = FLAGS_MAGIC;
331
332 flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM;
333 flags_info.boot_fota_flag.fota_status = 0;
334 flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE;
335 flags_info.boot_fota_flag.system.try_cnt = 5;
336 flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE;
337 flags_info.boot_fota_flag.system2.try_cnt = 13;
338
339 flags_info.boot_env.dualsys_type = DUALSYSTEM_AB;
340 strncpy(flags_info.boot_env.system_boot_env, system_boot_env, sizeof(flags_info.boot_env.system_boot_env));
341 strncpy(flags_info.boot_env.system2_boot_env, system2_boot_env, sizeof(flags_info.boot_env.system2_boot_env));
342
343 flags_info.ubifs_status.fs_status = 0;
344 strncpy(flags_info.ubifs_status.fs_mtd_name, fs_mtd_name, sizeof(flags_info.ubifs_status.fs_mtd_name));
345 strncpy(flags_info.ubifs_status.fs_ubi_vol_name, fs_ubi_vol_name, sizeof(flags_info.ubifs_status.fs_ubi_vol_name));
346
347 flags_info.magic_end = FLAGS_MAGIC;
348
349 if (ioctl(fd, FLAGS_UTILS_SET, &flags_info) < 0)
350 {
351 printf("Err: ioctl cmd(0x%x) fail, errno=%d \n", FLAGS_UTILS_SET, errno);
352
353 close(fd);
354 return -1;
355 }
356
357 printf("Log: kernel set flags success \n");
358
359 close(fd);
360 return 0;
361}
362
363
xf.li6c8fc1e2023-08-12 00:11:09 -0700364/*******************************************************************************
365 * Global function declarations *
366 ******************************************************************************/
367int main(int argc, char *argv[])
368{
369 int i = 0;
370 int option_index = 0;
371 int cmd_num = 0;
372
373 int ret = -1;
374 int ch = -1;
375
376 printf("Log: build date: %s %s \n", __DATE__, __TIME__);
377
378 while ((ch = getopt_long(argc, argv, g_short_string, g_long_options, &option_index)) != -1)
379 {
380 for (i = 0; i < sizeof(g_option_handle) / sizeof(option_handle_t); i++)
381 {
382 if (ch != g_option_handle[i].option_value)
383 {
384 continue;
385 }
386
387 cmd_num++;
388
389 if (NULL == g_option_handle[i].func)
390 {
391 printf("Err: command short string is: %c, but option handle func is NULL \n", ch);
392 break;
393 }
394
395 ret = g_option_handle[i].func(optarg);
396 if (ret < 0)
397 {
398 ret = -1;
399 goto end;
400 }
401 }
402 }
403
404 if (0 == cmd_num)
405 {
406 printf("Err: can not find valid command \n");
407 usage();
408 ret = -1;
409 goto end;
410 }
411
412 ret = 0;
413
414end:
415 return ret ;
416}
417