hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2020 Philippe Gerum  <rpm@xenomai.org>
 */
 
#include <unistd.h>
#include <getopt.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <search.h>
#include <error.h>
#include <errno.h>
#include <sys/utsname.h>
 
#define DEFAULT_KCONFIG    "/proc/config.gz"
#define DEFAULT_CHECKLIST  DATADIR"/kconf-checklist"
 
#define short_optlist "@hqf:L:a:H:"
 
static int hash_size = 16384;
 
static bool quiet;
 
static const struct option options[] = {
   {"file", required_argument, NULL, 'f'},
   {"check-list", required_argument, NULL, 'L'},
   {"arch", required_argument, NULL, 'a'},
   {"hash-size", required_argument, NULL, 'H'},
   {"quiet", no_argument, NULL, 'q'},
   {"help", no_argument, NULL, 'h'},
   {0}
};
 
static char *hash_config(FILE *kconfp)
{
   char buf[BUFSIZ], *sym, *val, *arch = NULL;
   ENTRY entry, *e;
   int ret;
 
   ret = hcreate(hash_size);
   if (!ret)
       error(1, errno, "hcreate(%d)", hash_size);
 
   while (fgets(buf, sizeof(buf), kconfp)) {
       if (*buf == '#') {
           sscanf(buf, "# Linux/%m[^ ]", &arch);
           continue;
       }
       ret = sscanf(buf, "%m[^=]=%ms\n", &sym, &val);
       if (ret != 2)
           continue;
       if (strncmp(sym, "CONFIG_", 7))
           continue;
       if (strcmp(val, "y") && strcmp(val, "m"))
           continue;
       entry.key = sym;
       entry.data = NULL;
       e = hsearch(entry, FIND);
       if (e)
           continue; /* uhh? */
       entry.data = val;
       if (!hsearch(entry, ENTER))
           error(1, ENOMEM, "h-table full -- try -H");
   }
 
   return arch;
}
 
static char *next_token(char **next)
{
   char *p = *next, *s;
 
   for (;;) {
       if (!*p) {
           *next = p;
           return strdup("");
       }
       if (!isspace(*p))
           break;
       p++;
   }
 
   s = p;
 
   if (!isalnum(*p) && *p != '_') {
       *next = p + 1;
       return strndup(s, 1);
   }
 
   do {
       if (!isalnum(*p) && *p != '_') {
           *next = p;
           return strndup(s, p - s);
       }
   } while (*++p);
 
   *next = p;
 
   return strdup(s);
}
 
static const char *get_arch_alias(const char *arch)
{
   if (!strcmp(arch, "arm64"))
       return "aarch64";
 
   if (!strcmp(arch, "arm"))
       return "aarch32";
 
   return arch;
}
 
static int apply_checklist(FILE *checkfp, const char *cpuarch)
{
   char buf[BUFSIZ], *token, *next, *sym, *val;
   int lineno = 0, failed = 0;
   bool not, notcond;
   const char *arch;
   ENTRY entry, *e;
 
   while (fgets(buf, sizeof(buf), checkfp)) {
       lineno++;
       next = buf;
 
       token = next_token(&next);
       if (!*token || !strcmp(token, "#")) {
           free(token);
           continue;
       }
 
       not = *token == '!';
       if (not) {
           free(token);
           token = next_token(&next);
       }
 
       sym = token;
       if (strncmp(sym, "CONFIG_", 7))
           error(1, EINVAL,
               "invalid check list symbol '%s' at line %d",
               sym, lineno);
 
       token = next_token(&next);
       val = NULL;
       if (*token == '=') {
           free(token);
           val = next_token(&next);
           token = next_token(&next);
       }
 
       if (!strcmp(token, "if")) {
           free(token);
           token = next_token(&next);
           notcond = *token == '!';
           if (notcond) {
               free(token);
               token = next_token(&next);
           }
           if (strncmp(token, "CONFIG_", 7))
               error(1, EINVAL,
                   "invalid condition symbol '%s' at line %d",
                   token, lineno);
           entry.key = token;
           entry.data = NULL;
           e = hsearch(entry, FIND);
           free(token);
           if (!((e && !notcond) || (!e && notcond)))
               continue;
           token = next_token(&next);
       }
 
       if (!strcmp(token, "on")) {
           free(token);
           token = next_token(&next);
           arch = get_arch_alias(token);
           if (strncmp(cpuarch, arch, strlen(arch))) {
               free(token);
               continue;
           }
       }
 
       free(token);
 
       entry.key = sym;
       entry.data = NULL;
       e = hsearch(entry, FIND);
 
       if (val && !strcmp(val, "n"))
           not = !not;
 
       if (e && (not || (val && strcmp(val, e->data)))) {
           if (!quiet)
               printf("%s=%s\n", sym, (const char *)e->data);
           failed++;
       } else if (!e && !not) {
           if (!quiet)
               printf("%s=n\n", sym);
           failed++;
       }
 
       free(sym);
       if (val)
           free(val);
   }
 
   return failed;
}
 
static void usage(char *arg0)
{
   fprintf(stderr, "usage: %s [options]:\n", basename(arg0));
   fprintf(stderr, "-f --file=<.config>     Kconfig file to check [=/proc/config.gz]\n");
   fprintf(stderr, "-L --check-list=<file>  configuration check list [="DATADIR"/kconf-checklist]\n");
   fprintf(stderr, "-a --arch=<cpuarch>     CPU architecture assumed\n");
   fprintf(stderr, "-H --hash-size=<N>      set the hash table size [=16384]\n");
   fprintf(stderr, "-q --quiet              suppress output\n");
   fprintf(stderr, "-h --help               this help\n");
}
 
int main(int argc, char *const argv[])
{
   const char *kconfig = DEFAULT_KCONFIG, *check_list = NULL,
       *defarch, *arch = NULL, *p;
   FILE *kconfp, *checkfp;
   struct utsname ubuf;
   int c, ret;
   char *cmd;
 
   opterr = 0;
 
   for (;;) {
       c = getopt_long(argc, argv, short_optlist, options, NULL);
       if (c == -1)
           break;
 
       switch (c) {
       case 0:
           break;
       case 'f':
           kconfig = optarg;
           break;
       case 'L':
           check_list = optarg;
           break;
       case 'a':
           arch = optarg;
           break;
       case 'H':
           hash_size = atoi(optarg);
           if (hash_size < 16384)
               hash_size = 16384;
           break;
       case 'q':
           quiet = true;
           break;
       case 'h':
           usage(argv[0]);
           return 0;
       case '@':
           printf("check kernel configuration\n");
           return 0;
       default:
           usage(argv[0]);
           return 1;
       }
   }
   if (optind < argc) {
       usage(argv[0]);
       return 1;
   }
 
   /*
    * We may be given a gzipped input file. Finding gunzip on a
    * minimalist rootfs (e.g. busybox) may be more likely than
    * having the zlib development files available from a common
    * cross-toolchain. So go for popen-ing gunzip on the target
    * instead of depending on libz on the development host.
    */
   if (!strcmp(kconfig, "-")) {
       kconfp = stdin;
   } else {
       p = strstr(kconfig, ".gz");
       if (!p || strcmp(p, ".gz")) {
           kconfp = fopen(kconfig, "r");
       } else {
           if (access(kconfig, R_OK))
               error(1, errno, "cannot access %s%s",
                   kconfig,
                   strcmp(kconfig, DEFAULT_KCONFIG) ? "" :
                   "\n(you need CONFIG_IKCONFIG_PROC enabled)");
           ret = asprintf(&cmd, "gunzip -c %s", kconfig);
           if (ret < 0)
               error(1, ENOMEM, "asprintf()");
           kconfp = popen(cmd, "r");
           free(cmd);
       }
       if (kconfp == NULL)
           error(1, errno, "cannot open %s for reading", kconfig);
   }
 
   defarch = hash_config(kconfp);
 
   if (check_list == NULL)
       check_list = DEFAULT_CHECKLIST;
 
   if (access(check_list, R_OK))
       error(1, errno, "cannot access %s", check_list);
 
   checkfp = fopen(check_list, "r");
   if (checkfp == NULL)
       error(1, errno, "cannot open %s for reading", check_list);
 
   if (arch == NULL) {
       if (defarch) {
           arch = get_arch_alias(defarch);
       } else {
           ret = uname(&ubuf);
           if (ret)
               error(1, errno, "utsname()");
           arch = ubuf.machine;
       }
   } else {
       arch = get_arch_alias(arch);
   }
 
   return apply_checklist(checkfp, arch);
}