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