xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /** |
| 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 | ******************************************************************************/ |
| 37 | |
| 38 | |
| 39 | /******************************************************************************* |
| 40 | * Type definitions * |
| 41 | ******************************************************************************/ |
| 42 | typedef struct |
| 43 | { |
| 44 | int option_value; |
| 45 | int (*func)(char *); |
| 46 | } option_handle_t; |
| 47 | |
| 48 | |
| 49 | |
| 50 | /******************************************************************************* |
| 51 | * Static function declarations * |
| 52 | ******************************************************************************/ |
| 53 | static int excute_command_help(char *option_para); |
| 54 | static int excute_command_init(char *option_para); |
| 55 | static int excute_command_get(char *option_para); |
| 56 | static int excute_command_set(char *option_para); |
| 57 | static int excute_command_get_ubifs_status(char *option_para); |
| 58 | static int excute_command_set_ubifs_status(char *option_para); |
| 59 | |
| 60 | |
| 61 | /******************************************************************************* |
| 62 | * Global variable declarations * |
| 63 | ******************************************************************************/ |
| 64 | static char * g_short_string = "higsub"; |
| 65 | |
| 66 | static struct option g_long_options[] = |
| 67 | { |
| 68 | {"help", no_argument, NULL, 'h'}, |
| 69 | {"init", no_argument, NULL, 'i'}, |
| 70 | {"get", no_argument, NULL, 'g'}, |
| 71 | {"set", no_argument, NULL, 's'}, |
| 72 | {"get-ubifs-status", no_argument, NULL, 'u'}, |
| 73 | {"set-ubifs-status", no_argument, NULL, 'b'}, |
| 74 | {0, 0, 0, 0} |
| 75 | }; |
| 76 | |
| 77 | static option_handle_t g_option_handle[] = |
| 78 | { |
| 79 | {'h', excute_command_help}, |
| 80 | {'i', excute_command_init}, |
| 81 | {'g', excute_command_get}, |
| 82 | {'s', excute_command_set}, |
| 83 | {'u', excute_command_get_ubifs_status}, |
| 84 | {'b', excute_command_set_ubifs_status} |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | /******************************************************************************* |
| 89 | * Static function define * |
| 90 | ******************************************************************************/ |
| 91 | static void usage(void) |
| 92 | { |
| 93 | printf("flags_tool [-higsub] \n " |
| 94 | " -h, --help show usage \n" |
| 95 | " -i, --init init flags \n" |
| 96 | " -g, --get get flags data \n" |
| 97 | " -s, --set set flags data \n" |
| 98 | " -u, --get-ubifs-status get ubifs status \n" |
| 99 | " -b, --set-ubifs-status set ubifs status \n"); |
| 100 | |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static int excute_command_help(char *option_para) |
| 106 | { |
| 107 | usage(); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | static int excute_command_init(char *option_para) |
| 114 | { |
| 115 | if (flags_init() != 0) |
| 116 | { |
| 117 | printf("Err: flags init fail \n"); |
| 118 | |
| 119 | return -1; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | printf("Log: flags init success \n"); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static int excute_command_get(char *option_para) |
| 131 | { |
| 132 | T_FLAGS_INFO flags_info = {0}; |
| 133 | |
| 134 | if (flags_get(&flags_info) != 0) |
| 135 | { |
| 136 | printf("Err: flags get fail \n"); |
| 137 | |
| 138 | return -1; |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | printf("magic_start = 0x%x \n", flags_info.magic_start); |
| 143 | |
| 144 | printf("boot_fota_flag.boot_to = 0x%x \n", flags_info.boot_fota_flag.boot_to); |
| 145 | printf("boot_fota_flag.fota_status = %u \n", flags_info.boot_fota_flag.fota_status); |
| 146 | printf("boot_fota_flag.system.status = 0x%x \n", flags_info.boot_fota_flag.system.status); |
| 147 | printf("boot_fota_flag.system.try_cnt = %d \n", flags_info.boot_fota_flag.system.try_cnt); |
| 148 | printf("boot_fota_flag.system2.status = 0x%x \n", flags_info.boot_fota_flag.system2.status); |
| 149 | printf("boot_fota_flag.system2.try_cnt = %d \n", flags_info.boot_fota_flag.system2.try_cnt); |
| 150 | |
| 151 | printf("boot_env.dualsys_type = 0x%x \n", flags_info.boot_env.dualsys_type); |
| 152 | printf("boot_env.system_boot_env = %s \n", flags_info.boot_env.system_boot_env); |
| 153 | printf("boot_env.system2_boot_env = %s \n", flags_info.boot_env.system2_boot_env); |
| 154 | |
| 155 | printf("ubifs_status.fs_status = %d \n", flags_info.ubifs_status.fs_status); |
| 156 | printf("ubifs_status.fs_mtd_name = %s \n", flags_info.ubifs_status.fs_mtd_name); |
| 157 | printf("ubifs_status.fs_ubi_vol_name = %s \n", flags_info.ubifs_status.fs_ubi_vol_name); |
| 158 | |
| 159 | printf("magic_end = 0x%x \n", flags_info.magic_end); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | static int excute_command_set(char *option_para) |
| 167 | { |
| 168 | T_FLAGS_INFO flags_info = {0}; |
| 169 | |
| 170 | char system_boot_env[128] = "bootEnv1"; |
| 171 | char system2_boot_env[128] = "2bootEnv"; |
| 172 | |
| 173 | char fs_mtd_name[16] = "nameMTD"; |
| 174 | char fs_ubi_vol_name[16] = "nameVOL"; |
| 175 | |
| 176 | flags_info.magic_start = FLAGS_MAGIC; |
| 177 | |
| 178 | flags_info.boot_fota_flag.boot_to = DUAL_SYSTEM; |
| 179 | flags_info.boot_fota_flag.fota_status = 0; |
| 180 | flags_info.boot_fota_flag.system.status = DUALSYSTEM_STATUS_BOOTABLE; |
| 181 | flags_info.boot_fota_flag.system.try_cnt = 5; |
| 182 | flags_info.boot_fota_flag.system2.status = DUALSYSTEM_STATUS_BOOTABLE; |
| 183 | flags_info.boot_fota_flag.system2.try_cnt = 13; |
| 184 | |
| 185 | flags_info.boot_env.dualsys_type = DUALSYSTEM_AB; |
| 186 | strncpy(flags_info.boot_env.system_boot_env, system_boot_env, sizeof(flags_info.boot_env.system_boot_env)); |
| 187 | strncpy(flags_info.boot_env.system2_boot_env, system2_boot_env, sizeof(flags_info.boot_env.system2_boot_env)); |
| 188 | |
| 189 | flags_info.ubifs_status.fs_status = 0; |
| 190 | strncpy(flags_info.ubifs_status.fs_mtd_name, fs_mtd_name, sizeof(flags_info.ubifs_status.fs_mtd_name)); |
| 191 | strncpy(flags_info.ubifs_status.fs_ubi_vol_name, fs_ubi_vol_name, sizeof(flags_info.ubifs_status.fs_ubi_vol_name)); |
| 192 | |
| 193 | flags_info.magic_end = FLAGS_MAGIC; |
| 194 | |
| 195 | if (flags_set(&flags_info) != 0) |
| 196 | { |
| 197 | printf("Err: set flags fail \n"); |
| 198 | |
| 199 | return -1; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | printf("Log: set flags success \n"); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
| 210 | static int excute_command_get_ubifs_status(char *option_para) |
| 211 | { |
| 212 | T_UBIFS_STATUS ubifs_status = {0}; |
| 213 | |
| 214 | if (flags_get_ubifs_status(&ubifs_status) != 0) |
| 215 | { |
| 216 | printf("Err: get ubifs status fail \n"); |
| 217 | |
| 218 | return -1; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | printf("ubifs_status.fs_status = %d \n", ubifs_status.fs_status); |
| 223 | printf("ubifs_status.fs_mtd_name = %s \n", ubifs_status.fs_mtd_name); |
| 224 | printf("ubifs_status.fs_ubi_vol_name = %s \n", ubifs_status.fs_ubi_vol_name); |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | |
| 231 | static int excute_command_set_ubifs_status(char *option_para) |
| 232 | { |
| 233 | T_UBIFS_STATUS ubifs_status = {0}; |
| 234 | |
| 235 | char fs_mtd_name[16] = "mtdName"; |
| 236 | char fs_ubi_vol_name[16] = "volName"; |
| 237 | |
| 238 | ubifs_status.fs_status = 2; |
| 239 | strncpy(ubifs_status.fs_mtd_name, fs_mtd_name, sizeof(ubifs_status.fs_mtd_name)); |
| 240 | strncpy(ubifs_status.fs_ubi_vol_name, fs_ubi_vol_name, sizeof(ubifs_status.fs_ubi_vol_name)); |
| 241 | |
| 242 | if (flags_set_ubifs_status(&ubifs_status) != 0) |
| 243 | { |
| 244 | printf("Err: set ubifs status fail \n"); |
| 245 | |
| 246 | return -1; |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | printf("Log: set ubifs status success \n"); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /******************************************************************************* |
| 258 | * Global function declarations * |
| 259 | ******************************************************************************/ |
| 260 | int main(int argc, char *argv[]) |
| 261 | { |
| 262 | int i = 0; |
| 263 | int option_index = 0; |
| 264 | int cmd_num = 0; |
| 265 | |
| 266 | int ret = -1; |
| 267 | int ch = -1; |
| 268 | |
| 269 | printf("Log: build date: %s %s \n", __DATE__, __TIME__); |
| 270 | |
| 271 | while ((ch = getopt_long(argc, argv, g_short_string, g_long_options, &option_index)) != -1) |
| 272 | { |
| 273 | for (i = 0; i < sizeof(g_option_handle) / sizeof(option_handle_t); i++) |
| 274 | { |
| 275 | if (ch != g_option_handle[i].option_value) |
| 276 | { |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | cmd_num++; |
| 281 | |
| 282 | if (NULL == g_option_handle[i].func) |
| 283 | { |
| 284 | printf("Err: command short string is: %c, but option handle func is NULL \n", ch); |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | ret = g_option_handle[i].func(optarg); |
| 289 | if (ret < 0) |
| 290 | { |
| 291 | ret = -1; |
| 292 | goto end; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (0 == cmd_num) |
| 298 | { |
| 299 | printf("Err: can not find valid command \n"); |
| 300 | usage(); |
| 301 | ret = -1; |
| 302 | goto end; |
| 303 | } |
| 304 | |
| 305 | ret = 0; |
| 306 | |
| 307 | end: |
| 308 | return ret ; |
| 309 | } |
| 310 | |