1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef BASE_STARTUP_INIT_CMDS_H 16 #define BASE_STARTUP_INIT_CMDS_H 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <sys/stat.h> 20 #include <sys/time.h> 21 #include <sys/types.h> 22 #include <time.h> 23 24 #include "cJSON.h" 25 #ifdef __cplusplus 26 #if __cplusplus 27 extern "C" { 28 #endif 29 #endif 30 31 #define DEFAULT_DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH) // mkdir, default mode 32 #define SPACES_CNT_IN_CMD_MAX 10 // mount, max number of spaces in cmdline 33 #define SPACES_CNT_IN_CMD_MIN 2 // mount, min number of spaces in cmdline 34 35 #define LOADCFG_BUF_SIZE 128 // loadcfg, max buffer for one cmdline 36 #define LOADCFG_MAX_FILE_LEN 51200 // loadcfg, max file size is 50K 37 #define LOADCFG_MAX_LOOP 20 // loadcfg, to prevent to be trapped in infite loop 38 #define OCTAL_TYPE 8 // 8 means octal to decimal 39 #define MAX_BUFFER 256UL 40 #define AUTHORITY_MAX_SIZE 128 41 42 #define MAX_CMD_NAME_LEN 32 43 #define MAX_CMD_CONTENT_LEN 256 44 #define MAX_CMD_CNT_IN_ONE_JOB 200 45 #define MAX_COPY_BUF_SIZE 256 46 #define DEFAULT_COPY_ARGS_CNT 2 47 48 #define OPTIONS_SIZE 128 49 50 #define SUPPORT_MAX_ARG_FOR_EXEC 10 51 // one cmd line 52 typedef struct { 53 int cmdIndex; 54 char cmdContent[MAX_CMD_CONTENT_LEN + 1]; 55 } CmdLine; 56 57 typedef struct { 58 int cmdNum; 59 CmdLine cmds[0]; 60 } CmdLines; 61 62 struct CmdArgs { 63 int argc; 64 char *argv[0]; 65 }; 66 67 struct CmdTable { 68 char name[MAX_CMD_NAME_LEN]; 69 unsigned char minArg; 70 unsigned char maxArg; 71 void (*DoFuncion)(const struct CmdArgs *ctx); 72 }; 73 74 typedef struct INIT_TIMING_STAT { 75 struct timespec startTime; 76 struct timespec endTime; 77 } INIT_TIMING_STAT; 78 79 int GetParamValue(const char *symValue, unsigned int symLen, char *paramValue, unsigned int paramLen); 80 const struct CmdArgs *GetCmdArg(const char *cmdContent, const char *delim, int argsCount); 81 void FreeCmdArg(struct CmdArgs *cmd); 82 void DoCmdByName(const char *name, const char *cmdContent); 83 void DoCmdByIndex(int index, const char *cmdContent); 84 const char *GetMatchCmd(const char *cmdStr, int *index); 85 const char *GetCmdKey(int index); 86 const struct CmdTable *GetCmdTable(int *number); 87 int GetCmdLinesFromJson(const cJSON *root, CmdLines **cmdLines); 88 const struct CmdTable *GetCmdByName(const char *name); 89 void ExecReboot(const char *value); 90 char *BuildStringFromCmdArg(const struct CmdArgs *ctx, int startIndex); 91 void ExecCmd(const struct CmdTable *cmd, const char *cmdContent); 92 int SetFileCryptPolicy(const char *dir); 93 94 void OpenHidebug(const char *name); 95 long long InitDiffTime(INIT_TIMING_STAT *stat); 96 #ifdef __cplusplus 97 #if __cplusplus 98 } 99 #endif 100 #endif 101 102 #endif // BASE_STARTUP_INITLITE_CMDS_H 103