]> git.ozlabs.org Git - petitboot/blob - lib/pb-config/pb-config.c
315d9905659369ea8224f4ef8318de459138786e
[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->network.n_interfaces || config->network.n_dns_servers)
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         for (i = 0; i < config->network.n_dns_servers; i++)
57                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
58 }
59
60 int config_init(void *ctx)
61 {
62         config = talloc(ctx, struct config);
63         config_set_defaults(config);
64
65         storage = create_powerpc_nvram_storage(config);
66
67         storage->load(storage, config);
68
69         dump_config(config);
70
71         return 0;
72 }
73
74 const struct config *config_get(void)
75 {
76         return config;
77 }
78
79 void config_set_autoboot(bool autoboot_enabled)
80 {
81         config->autoboot_enabled = autoboot_enabled;
82
83         pb_log("set autoboot: %s\n",
84                         config->autoboot_enabled ? "enabled" : "disabled");
85 }
86
87 int config_fini(void)
88 {
89         talloc_free(config);
90         return 0;
91 }