ce5fce530a7990ff9996cf85532e0046bd7a5913
[coreboot.git] / payloads / libpayload / util / kconfig / confdata.c
1 /*
2  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3  * Released under the terms of the GNU GPL v2.0.
4  */
5
6 #include <sys/stat.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <unistd.h>
13
14 #define LKC_DIRECT_LINK
15 #include "lkc.h"
16
17 static void conf_warning(const char *fmt, ...)
18         __attribute__ ((format (printf, 1, 2)));
19
20 static const char *conf_filename;
21 static int conf_lineno, conf_warnings, conf_unsaved;
22
23 const char conf_def_filename[] = ".config";
24
25 const char conf_defname[] = "util/defconfig";
26
27 const char *conf_confnames[] = {
28         ".config",
29         NULL,
30 };
31
32 static void conf_warning(const char *fmt, ...)
33 {
34         va_list ap;
35         va_start(ap, fmt);
36         fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
37         vfprintf(stderr, fmt, ap);
38         fprintf(stderr, "\n");
39         va_end(ap);
40         conf_warnings++;
41 }
42
43 static char *conf_expand_value(const char *in)
44 {
45         struct symbol *sym;
46         const char *src;
47         static char res_value[SYMBOL_MAXLENGTH];
48         char *dst, name[SYMBOL_MAXLENGTH];
49
50         res_value[0] = 0;
51         dst = name;
52         while ((src = strchr(in, '$'))) {
53                 strncat(res_value, in, src - in);
54                 src++;
55                 dst = name;
56                 while (isalnum(*src) || *src == '_')
57                         *dst++ = *src++;
58                 *dst = 0;
59                 sym = sym_lookup(name, 0);
60                 sym_calc_value(sym);
61                 strcat(res_value, sym_get_string_value(sym));
62                 in = src;
63         }
64         strcat(res_value, in);
65
66         return res_value;
67 }
68
69 char *conf_get_default_confname(void)
70 {
71         struct stat buf;
72         static char fullname[PATH_MAX+1];
73         char *env, *name;
74
75         name = conf_expand_value(conf_defname);
76         env = getenv(SRCTREE);
77         if (env) {
78                 sprintf(fullname, "%s/%s", env, name);
79                 if (!stat(fullname, &buf))
80                         return fullname;
81         }
82         return name;
83 }
84
85 int conf_read_simple(const char *name)
86 {
87         FILE *in = NULL;
88         char line[1024];
89         char *p, *p2;
90         struct symbol *sym;
91         int i;
92
93         if (name) {
94                 in = zconf_fopen(name);
95         } else {
96                 const char **names = conf_confnames;
97                 while ((name = *names++)) {
98                         name = conf_expand_value(name);
99                         in = zconf_fopen(name);
100                         if (in) {
101                                 printf(_("#\n"
102                                          "# using defaults found in %s\n"
103                                          "#\n"), name);
104                                 break;
105                         }
106                 }
107         }
108         if (!in)
109                 return 1;
110
111         conf_filename = name;
112         conf_lineno = 0;
113         conf_warnings = 0;
114         conf_unsaved = 0;
115
116         for_all_symbols(i, sym) {
117                 sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
118                 if (sym_is_choice(sym))
119                         sym->flags &= ~SYMBOL_NEW;
120                 sym->flags &= ~SYMBOL_VALID;
121                 switch (sym->type) {
122                 case S_INT:
123                 case S_HEX:
124                 case S_STRING:
125                         if (sym->user.val)
126                                 free(sym->user.val);
127                 default:
128                         sym->user.val = NULL;
129                         sym->user.tri = no;
130                 }
131         }
132
133         while (fgets(line, sizeof(line), in)) {
134                 conf_lineno++;
135                 sym = NULL;
136                 switch (line[0]) {
137                 case '#':
138                         if (memcmp(line + 2, "CONFIG_", 7))
139                                 continue;
140                         p = strchr(line + 9, ' ');
141                         if (!p)
142                                 continue;
143                         *p++ = 0;
144                         if (strncmp(p, "is not set", 10))
145                                 continue;
146                         sym = sym_find(line + 9);
147                         if (!sym) {
148                                 conf_warning("trying to assign nonexistent symbol %s", line + 9);
149                                 break;
150                         } else if (!(sym->flags & SYMBOL_NEW)) {
151                                 conf_warning("trying to reassign symbol %s", sym->name);
152                                 break;
153                         }
154                         switch (sym->type) {
155                         case S_BOOLEAN:
156                         case S_TRISTATE:
157                                 sym->user.tri = no;
158                                 sym->flags &= ~SYMBOL_NEW;
159                                 break;
160                         default:
161                                 ;
162                         }
163                         break;
164                 case 'C':
165                         if (memcmp(line, "CONFIG_", 7)) {
166                                 conf_warning("unexpected data");
167                                 continue;
168                         }
169                         p = strchr(line + 7, '=');
170                         if (!p)
171                                 continue;
172                         *p++ = 0;
173                         p2 = strchr(p, '\n');
174                         if (p2)
175                                 *p2 = 0;
176                         sym = sym_find(line + 7);
177                         if (!sym) {
178                                 conf_warning("trying to assign nonexistent symbol %s", line + 7);
179                                 break;
180                         } else if (!(sym->flags & SYMBOL_NEW)) {
181                                 conf_warning("trying to reassign symbol %s", sym->name);
182                                 break;
183                         }
184                         switch (sym->type) {
185                         case S_TRISTATE:
186                                 if (p[0] == 'm') {
187                                         sym->user.tri = mod;
188                                         sym->flags &= ~SYMBOL_NEW;
189                                         break;
190                                 }
191                         case S_BOOLEAN:
192                                 if (p[0] == 'y') {
193                                         sym->user.tri = yes;
194                                         sym->flags &= ~SYMBOL_NEW;
195                                         break;
196                                 }
197                                 if (p[0] == 'n') {
198                                         sym->user.tri = no;
199                                         sym->flags &= ~SYMBOL_NEW;
200                                         break;
201                                 }
202                                 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
203                                 break;
204                         case S_STRING:
205                                 if (*p++ != '"')
206                                         break;
207                                 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
208                                         if (*p2 == '"') {
209                                                 *p2 = 0;
210                                                 break;
211                                         }
212                                         memmove(p2, p2 + 1, strlen(p2));
213                                 }
214                                 if (!p2) {
215                                         conf_warning("invalid string found");
216                                         continue;
217                                 }
218                         case S_INT:
219                         case S_HEX:
220                                 if (sym_string_valid(sym, p)) {
221                                         sym->user.val = strdup(p);
222                                         sym->flags &= ~SYMBOL_NEW;
223                                 } else {
224                                         conf_warning("symbol value '%s' invalid for %s", p, sym->name);
225                                         continue;
226                                 }
227                                 break;
228                         default:
229                                 ;
230                         }
231                         break;
232                 case '\n':
233                         break;
234                 default:
235                         conf_warning("unexpected data");
236                         continue;
237                 }
238                 if (sym && sym_is_choice_value(sym)) {
239                         struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
240                         switch (sym->user.tri) {
241                         case no:
242                                 break;
243                         case mod:
244                                 if (cs->user.tri == yes) {
245                                         conf_warning("%s creates inconsistent choice state", sym->name);
246                                         cs->flags |= SYMBOL_NEW;
247                                 }
248                                 break;
249                         case yes:
250                                 if (cs->user.tri != no) {
251                                         conf_warning("%s creates inconsistent choice state", sym->name);
252                                         cs->flags |= SYMBOL_NEW;
253                                 } else
254                                         cs->user.val = sym;
255                                 break;
256                         }
257                         cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
258                 }
259         }
260         fclose(in);
261
262         if (modules_sym)
263                 sym_calc_value(modules_sym);
264         return 0;
265 }
266
267 int conf_read(const char *name)
268 {
269         struct symbol *sym;
270         struct property *prop;
271         struct expr *e;
272         int i;
273
274         if (conf_read_simple(name))
275                 return 1;
276
277         for_all_symbols(i, sym) {
278                 sym_calc_value(sym);
279                 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
280                         goto sym_ok;
281                 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
282                         /* check that calculated value agrees with saved value */
283                         switch (sym->type) {
284                         case S_BOOLEAN:
285                         case S_TRISTATE:
286                                 if (sym->user.tri != sym_get_tristate_value(sym))
287                                         break;
288                                 if (!sym_is_choice(sym))
289                                         goto sym_ok;
290                         default:
291                                 if (!strcmp(sym->curr.val, sym->user.val))
292                                         goto sym_ok;
293                                 break;
294                         }
295                 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
296                         /* no previous value and not saved */
297                         goto sym_ok;
298                 conf_unsaved++;
299                 /* maybe print value in verbose mode... */
300         sym_ok:
301                 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
302                         if (sym->visible == no)
303                                 sym->flags |= SYMBOL_NEW;
304                         switch (sym->type) {
305                         case S_STRING:
306                         case S_INT:
307                         case S_HEX:
308                                 if (!sym_string_within_range(sym, sym->user.val)) {
309                                         sym->flags |= SYMBOL_NEW;
310                                         sym->flags &= ~SYMBOL_VALID;
311                                 }
312                         default:
313                                 break;
314                         }
315                 }
316                 if (!sym_is_choice(sym))
317                         continue;
318                 prop = sym_get_choice_prop(sym);
319                 for (e = prop->expr; e; e = e->left.expr)
320                         if (e->right.sym->visible != no)
321                                 sym->flags |= e->right.sym->flags & SYMBOL_NEW;
322         }
323
324         sym_change_count = conf_warnings || conf_unsaved;
325
326         return 0;
327 }
328
329 int conf_write(const char *name)
330 {
331         FILE *out, *out_h;
332         struct symbol *sym;
333         struct menu *menu;
334         const char *basename;
335         char dirname[128], tmpname[128], newname[128];
336         int type, l;
337         const char *str;
338         time_t now;
339         int use_timestamp = 1;
340         char *env;
341
342         dirname[0] = 0;
343         if (name && name[0]) {
344                 struct stat st;
345                 char *slash;
346
347                 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
348                         strcpy(dirname, name);
349                         strcat(dirname, "/");
350                         basename = conf_def_filename;
351                 } else if ((slash = strrchr(name, '/'))) {
352                         int size = slash - name + 1;
353                         memcpy(dirname, name, size);
354                         dirname[size] = 0;
355                         if (slash[1])
356                                 basename = slash + 1;
357                         else
358                                 basename = conf_def_filename;
359                 } else
360                         basename = name;
361         } else
362                 basename = conf_def_filename;
363
364         sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
365         out = fopen(newname, "w");
366         if (!out)
367                 return 1;
368         out_h = NULL;
369         if (!name) {
370                 out_h = fopen(".tmpconfig.h", "w");
371                 if (!out_h)
372                         return 1;
373                 file_write_dep(NULL);
374         }
375         sym = sym_lookup("KERNELVERSION", 0);
376         sym_calc_value(sym);
377         time(&now);
378         env = getenv("KCONFIG_NOTIMESTAMP");
379         if (env && *env)
380                 use_timestamp = 0;
381
382         fprintf(out, _("#\n"
383                        "# Automatically generated make config: don't edit\n"
384                        "# Libpayload version: %s\n"
385                        "%s%s"
386                        "#\n"),
387                      sym_get_string_value(sym),
388                      use_timestamp ? "# " : "",
389                      use_timestamp ? ctime(&now) : "");
390         if (out_h) {
391                 char buf[sizeof("#define AUTOCONF_TIMESTAMP "
392                                 "\"YYYY-MM-DD HH:MM:SS some_timezone\"\n")];
393                 buf[0] = '\0';
394                 if (use_timestamp) {
395                         size_t ret = \
396                                 strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
397                                         "\"%Y-%m-%d %H:%M:%S %Z\"\n", localtime(&now));
398                         /* if user has Factory timezone or some other odd install, the
399                          * %Z above will overflow the string leaving us with undefined
400                          * results ... so let's try again without the timezone.
401                          */
402                         if (ret == 0)
403                                 strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
404                                         "\"%Y-%m-%d %H:%M:%S\"\n", localtime(&now));
405                 }
406                 fprintf(out_h, "/*\n"
407                                " * Automatically generated C config: don't edit\n"
408                                " * Libpayload version: %s\n"
409                                " */\n"
410                                "%s"
411                                "\n",
412                                sym_get_string_value(sym),
413                                buf);
414         }
415         if (!sym_change_count)
416                 sym_clear_all_valid();
417
418         menu = rootmenu.list;
419         while (menu) {
420                 sym = menu->sym;
421                 if (!sym) {
422                         if (!menu_is_visible(menu))
423                                 goto next;
424                         str = menu_get_prompt(menu);
425                         fprintf(out, "\n"
426                                      "#\n"
427                                      "# %s\n"
428                                      "#\n", str);
429                         if (out_h)
430                                 fprintf(out_h, "\n"
431                                                "/*\n"
432                                                " * %s\n"
433                                                " */\n", str);
434                 } else if (!(sym->flags & SYMBOL_CHOICE)) {
435                         sym_calc_value(sym);
436 /* bbox: we want to all syms
437                         if (!(sym->flags & SYMBOL_WRITE))
438                                 goto next;
439 */
440                         sym->flags &= ~SYMBOL_WRITE;
441                         type = sym->type;
442                         if (type == S_TRISTATE) {
443                                 sym_calc_value(modules_sym);
444                                 if (modules_sym->curr.tri == no)
445                                         type = S_BOOLEAN;
446                         }
447                         switch (type) {
448                         case S_BOOLEAN:
449                         case S_TRISTATE:
450                                 switch (sym_get_tristate_value(sym)) {
451                                 case no:
452                                         fprintf(out, "# CONFIG_%s is not set\n", sym->name);
453                                         if (out_h) {
454                                                 fprintf(out_h, "#undef CONFIG_%s\n", sym->name);
455                                                 /* bbox */
456                                                 fprintf(out_h, "#define ENABLE_%s 0\n", sym->name);
457                                                 fprintf(out_h, "#define USE_%s(...)\n", sym->name);
458                                                 fprintf(out_h, "#define SKIP_%s(...) __VA_ARGS__\n", sym->name);
459                                         }
460                                         break;
461                                 case mod:
462                                         fprintf(out, "CONFIG_%s=m\n", sym->name);
463                                         if (out_h)
464                                                 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
465                                         break;
466                                 case yes:
467                                         fprintf(out, "CONFIG_%s=y\n", sym->name);
468                                         if (out_h) {
469                                                 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
470                                                 /* bbox */
471                                                 fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
472                                                 fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
473                                                 fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
474                                         }
475                                         break;
476                                 }
477                                 break;
478                         case S_STRING:
479                                 // fix me
480                                 str = sym_get_string_value(sym);
481                                 fprintf(out, "CONFIG_%s=\"", sym->name);
482                                 if (out_h)
483                                         fprintf(out_h, "#define CONFIG_%s \"", sym->name);
484                                 do {
485                                         l = strcspn(str, "\"\\");
486                                         if (l) {
487                                                 fwrite(str, l, 1, out);
488                                                 if (out_h)
489                                                         fwrite(str, l, 1, out_h);
490                                         }
491                                         str += l;
492                                         while (*str == '\\' || *str == '"') {
493                                                 fprintf(out, "\\%c", *str);
494                                                 if (out_h)
495                                                         fprintf(out_h, "\\%c", *str);
496                                                 str++;
497                                         }
498                                 } while (*str);
499                                 fputs("\"\n", out);
500                                 if (out_h) {
501                                         fputs("\"\n", out_h);
502                                         /* bbox */
503                                         fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
504                                         fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
505                                         fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
506                                 }
507                                 break;
508                         case S_HEX:
509                                 str = sym_get_string_value(sym);
510                                 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
511                                         fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
512                                         if (out_h) {
513                                                 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
514                                                 /* bbox */
515                                                 fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
516                                                 fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
517                                                 fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
518                                         }
519                                         break;
520                                 }
521                         case S_INT:
522                                 str = sym_get_string_value(sym);
523                                 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
524                                 if (out_h) {
525                                         fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
526                                         /* bbox */
527                                         fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
528                                         fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
529                                         fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
530                                 }
531                                 break;
532                         }
533                 }
534
535         next:
536                 if (menu->list) {
537                         menu = menu->list;
538                         continue;
539                 }
540                 if (menu->next)
541                         menu = menu->next;
542                 else while ((menu = menu->parent)) {
543                         if (menu->next) {
544                                 menu = menu->next;
545                                 break;
546                         }
547                 }
548         }
549         fclose(out);
550         if (out_h) {
551                 fclose(out_h);
552                 rename(".tmpconfig.h", "include/autoconf.h");
553         }
554         if (!name || basename != conf_def_filename) {
555                 if (!name)
556                         name = conf_def_filename;
557                 sprintf(tmpname, "%s.old", name);
558                 rename(name, tmpname);
559         }
560         sprintf(tmpname, "%s%s", dirname, basename);
561         if (rename(newname, tmpname))
562                 return 1;
563
564         sym_change_count = 0;
565
566         return 0;
567 }