]> git.ozlabs.org Git - petitboot/blob - lib/pb-config/pb-config.c
protocol: Add block device information to system info
[petitboot] / lib / pb-config / pb-config.c
1
2 #include <log/log.h>
3 #include <types/types.h>
4 #include <talloc/talloc.h>
5
6 #include "pb-config.h"
7
8 #include "storage.h"
9
10 void                            *config_ctx;
11 static struct config            *config;
12 static struct config_storage    *storage;
13
14
15 void config_set_defaults(struct config *config)
16 {
17         config->autoboot_enabled = true;
18         config->autoboot_timeout_sec = 10;
19         config->network.interfaces = NULL;
20         config->network.n_interfaces = 0;
21         config->network.dns_servers = NULL;
22         config->network.n_dns_servers = 0;
23
24         config->n_boot_priorities = 2;
25         config->boot_priorities = talloc_array(config, struct boot_priority,
26                                                 config->n_boot_priorities);
27         config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
28         config->boot_priorities[1].type = DEVICE_TYPE_DISK;
29
30 }
31
32 void dump_config(struct config *config);
33 void dump_config(struct config *config)
34 {
35         unsigned int i;
36
37         pb_log("configuration:\n");
38
39         if (config->autoboot_enabled)
40                 pb_log(" autoboot: enabled, %d sec\n",
41                                 config->autoboot_timeout_sec);
42         else
43                 pb_log(" autoboot: disabled\n");
44
45         if (config->network.n_interfaces || config->network.n_dns_servers)
46                 pb_log(" network configuration:\n");
47
48         for (i = 0; i < config->network.n_interfaces; i++) {
49                 struct interface_config *ifconf =
50                         config->network.interfaces[i];
51
52                 pb_log("  interface %02x:%02x:%02x:%02x:%02x:%02x\n",
53                                 ifconf->hwaddr[0], ifconf->hwaddr[1],
54                                 ifconf->hwaddr[2], ifconf->hwaddr[3],
55                                 ifconf->hwaddr[4], ifconf->hwaddr[5]);
56
57                 if (ifconf->ignore) {
58                         pb_log("   ignore\n");
59                         continue;
60                 }
61
62                 if (ifconf->method == CONFIG_METHOD_DHCP) {
63                         pb_log("   dhcp\n");
64
65                 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
66                         pb_log("   static:\n");
67                         pb_log("    ip:  %s\n", ifconf->static_config.address);
68                         pb_log("    gw:  %s\n", ifconf->static_config.gateway);
69
70                 }
71         }
72         for (i = 0; i < config->network.n_dns_servers; i++)
73                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
74 }
75
76 int config_init(void *ctx)
77 {
78         config_ctx = talloc_new(ctx);
79
80         config = talloc(config_ctx, struct config);
81         config_set_defaults(config);
82
83         storage = create_powerpc_nvram_storage(config);
84
85         storage->load(storage, config);
86
87         dump_config(config);
88
89         return 0;
90 }
91
92 int config_set(struct config *newconfig)
93 {
94         int rc;
95
96         if (!storage || !storage->save)
97                 return -1;
98
99         if (newconfig == config)
100                 return 0;
101
102         pb_log("new configuration data received\n");
103         dump_config(newconfig);
104
105         rc = storage->save(storage, newconfig);
106
107         if (!rc)
108                 config = talloc_steal(config_ctx, newconfig);
109         else
110                 pb_log("error saving new configuration; changes lost\n");
111
112         return rc;
113 }
114
115 /* A non-exported function to allow the test infrastructure to initialise
116  * (and change) the configuration variables */
117 struct parser_test;
118 struct config __attribute__((unused)) *test_config_init(
119                 struct parser_test *test);
120 struct config *test_config_init(struct parser_test *test)
121 {
122         config = talloc(test, struct config);
123         config_set_defaults(config);
124         return config;
125 }
126
127 const struct config *config_get(void)
128 {
129         return config;
130 }
131
132 void config_set_autoboot(bool autoboot_enabled)
133 {
134         config->autoboot_enabled = autoboot_enabled;
135
136         pb_log("set autoboot: %s\n",
137                         config->autoboot_enabled ? "enabled" : "disabled");
138 }
139
140 int config_fini(void)
141 {
142         talloc_free(config_ctx);
143         return 0;
144 }