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