1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2018 Cyril Hrubis <[email protected]> 4 */ 5 6 #ifndef TST_KCONFIG_H__ 7 #define TST_KCONFIG_H__ 8 9 #include <stdbool.h> 10 11 /** 12 * Initialization helper macro for struct tst_kconfig_var. Requires <string.h> 13 */ 14 #define TST_KCONFIG_INIT(confname) { \ 15 .id = confname, \ 16 .id_len = strlen(confname) \ 17 } 18 19 struct tst_kconfig_var { 20 char id[64]; 21 unsigned int id_len; 22 char choice; 23 char *val; 24 }; 25 26 /** 27 * 28 * Reads a kernel config, parses it and writes results into an array of 29 * tst_kconfig_var structures. 30 * 31 * The path to the kernel config should be autodetected in most of the cases as 32 * the code looks for know locations. It can be explicitely set/overrided with 33 * the KCONFIG_PATH environment variable as well. 34 * 35 * The caller has to initialize the tst_kconfig_var structure. The id has to be 36 * filled with config variable name such as 'CONFIG_FOO', the id_len should 37 * hold the id string length and the choice and val has to be zeroed. 38 * 39 * After a call to this function each choice be set as follows: 40 * 41 * 'm' - config option set to m 42 * 'y' - config option set to y 43 * 'v' - config option set to other value 44 * 'n' - config option is not set 45 * 0 - config option not found 46 * 47 * In the case that match is set to 'v' the val pointer points to a newly 48 * allocated string that holds the value. 49 * 50 * @param vars An array of caller initalized tst_kconfig_var structures. 51 * @param vars_len Length of the vars array. 52 */ 53 void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len); 54 55 /** 56 * Checks if required kernel configuration options are set in the kernel 57 * config. Return 0 if every config is satisfied and return 1 if at least 58 * one is missing. 59 * 60 * The config options can be passed in two different formats, either 61 * "CONFIG_FOO" in which case the option has to be set in order to continue the 62 * test or with an explicit value "CONFIG_FOO=bar" in which case the value has 63 * to match. 64 * 65 * @param kconfigs NULL-terminated array of config strings needed for the testrun. 66 */ 67 int tst_kconfig_check(const char *const kconfigs[]); 68 69 /** 70 * Macro to prepare a tst_kcmdline_var structure with a given parameter name. 71 * 72 * It initializes the .key field with the provided name, sets the .value field 73 * to an empty string, and marks the parameter as not found (.found = false). 74 * 75 * This macro is typically used to prepopulate an array with configuration 76 * parameters before processing the actual command line arguments. 77 */ 78 #define TST_KCMDLINE_INIT(paraname) { \ 79 .key = paraname, \ 80 .value = "", \ 81 .found = false \ 82 } 83 84 /** 85 * Structure for storing command-line parameter key and its corresponding 86 * value, and a flag indicating whether the parameter was found. 87 */ 88 struct tst_kcmdline_var { 89 const char *key; 90 char value[128]; 91 bool found; 92 }; 93 94 /** 95 * Parses command-line parameters from /proc/cmdline and stores them in params array. 96 * params: The array of tst_kcmdline_var structures to be filled with parsed key-value pairs. 97 * params_len: The length of the params array, indicating how many parameters to parse. 98 */ 99 void tst_kcmdline_parse(struct tst_kcmdline_var params[], size_t params_len); 100 101 #endif /* TST_KCONFIG_H__ */ 102