discover: Remove unnecessary event passing
[petitboot] / lib / pb-config / pb-config.c
1
2 #include <log/log.h>
3 #include <talloc/talloc.h>
4
5 #include "pb-config.h"
6
7 #include "storage.h"
8
9 static struct config            *config;
10 static struct config_storage    *storage;
11
12
13 static void config_set_defaults(struct config *config)
14 {
15         config->autoboot_enabled = true;
16         config->autoboot_timeout_sec = 10;
17         config->network.interfaces = NULL;
18         config->network.n_interfaces = 0;
19         config->network.dns_servers = NULL;
20         config->network.n_dns_servers = 0;
21 }
22
23 static void dump_config(struct config *config)
24 {
25         int i;
26
27         pb_log("configuration:\n");
28
29         if (config->autoboot_enabled)
30                 pb_log(" autoboot: enabled, %d sec\n",
31                                 config->autoboot_timeout_sec);
32         else
33                 pb_log(" autoboot: disabled\n");
34
35         if (config->network.n_interfaces || config->network.n_dns_servers)
36                 pb_log(" network configuration:\n");
37
38         for (i = 0; i < config->network.n_interfaces; i++) {
39                 struct interface_config *ifconf =
40                         config->network.interfaces[i];
41
42                 pb_log("  interface %02x:%02x:%02x:%02x:%02x:%02x\n",
43                                 ifconf->hwaddr[0], ifconf->hwaddr[1],
44                                 ifconf->hwaddr[2], ifconf->hwaddr[3],
45                                 ifconf->hwaddr[4], ifconf->hwaddr[5]);
46
47                 if (ifconf->ignore) {
48                         pb_log("   ignore\n");
49                         continue;
50                 }
51
52                 if (ifconf->method == CONFIG_METHOD_DHCP) {
53                         pb_log("   dhcp\n");
54
55                 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
56                         pb_log("   static:\n");
57                         pb_log("    ip:  %s\n", ifconf->static_config.address);
58                         pb_log("    gw:  %s\n", ifconf->static_config.gateway);
59
60                 }
61         }
62         for (i = 0; i < config->network.n_dns_servers; i++)
63                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
64 }
65
66 int config_init(void *ctx)
67 {
68         config = talloc(ctx, struct config);
69         config_set_defaults(config);
70
71         storage = create_powerpc_nvram_storage(config);
72
73         storage->load(storage, config);
74
75         dump_config(config);
76
77         return 0;
78 }
79
80 /* A non-exported function to allow the test infrastructure to initialise
81  * (and change) the configuration variables */
82 struct parser_test;
83 struct config __attribute__((unused)) *test_config_init(
84                 struct parser_test *test);
85 struct config *test_config_init(struct parser_test *test)
86 {
87         config = talloc(test, struct config);
88         config_set_defaults(config);
89         return config;
90 }
91
92 const struct config *config_get(void)
93 {
94         return config;
95 }
96
97 void config_set_autoboot(bool autoboot_enabled)
98 {
99         config->autoboot_enabled = autoboot_enabled;
100
101         pb_log("set autoboot: %s\n",
102                         config->autoboot_enabled ? "enabled" : "disabled");
103 }
104
105 int config_fini(void)
106 {
107         talloc_free(config);
108         return 0;
109 }