lib/pb-config: Add autoboot timeout to configuration vars
[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 }
20
21 static void dump_config(struct config *config)
22 {
23         int i;
24
25         pb_log("configuration:\n");
26
27         if (config->autoboot_enabled)
28                 pb_log(" autoboot: enabled, %d sec\n",
29                                 config->autoboot_timeout_sec);
30         else
31                 pb_log(" autoboot: disabled\n");
32
33         if (config->network.n_interfaces || config->network.n_dns_servers)
34                 pb_log(" network configuration:\n");
35
36         for (i = 0; i < config->network.n_interfaces; i++) {
37                 struct interface_config *ifconf =
38                         config->network.interfaces[i];
39
40                 pb_log("  interface %02x:%02x:%02x:%02x:%02x:%02x\n",
41                                 ifconf->hwaddr[0], ifconf->hwaddr[1],
42                                 ifconf->hwaddr[2], ifconf->hwaddr[3],
43                                 ifconf->hwaddr[4], ifconf->hwaddr[5]);
44
45                 if (ifconf->ignore) {
46                         pb_log("   ignore\n");
47                         continue;
48                 }
49
50                 if (ifconf->method == CONFIG_METHOD_DHCP) {
51                         pb_log("   dhcp\n");
52
53                 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
54                         pb_log("   static:\n");
55                         pb_log("    ip:  %s\n", ifconf->static_config.address);
56                         pb_log("    gw:  %s\n", ifconf->static_config.gateway);
57
58                 }
59         }
60         for (i = 0; i < config->network.n_dns_servers; i++)
61                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
62 }
63
64 int config_init(void *ctx)
65 {
66         config = talloc(ctx, struct config);
67         config_set_defaults(config);
68
69         storage = create_powerpc_nvram_storage(config);
70
71         storage->load(storage, config);
72
73         dump_config(config);
74
75         return 0;
76 }
77
78 const struct config *config_get(void)
79 {
80         return config;
81 }
82
83 void config_set_autoboot(bool autoboot_enabled)
84 {
85         config->autoboot_enabled = autoboot_enabled;
86
87         pb_log("set autoboot: %s\n",
88                         config->autoboot_enabled ? "enabled" : "disabled");
89 }
90
91 int config_fini(void)
92 {
93         talloc_free(config);
94         return 0;
95 }