5 #include <types/types.h>
6 #include <talloc/talloc.h>
13 static struct config *config;
14 static struct config_storage *storage;
17 void config_set_defaults(struct config *config)
19 config->autoboot_enabled = true;
20 config->autoboot_timeout_sec = 10;
21 config->network.interfaces = NULL;
22 config->network.n_interfaces = 0;
23 config->network.dns_servers = NULL;
24 config->network.n_dns_servers = 0;
26 config->n_boot_priorities = 2;
27 config->boot_priorities = talloc_array(config, struct boot_priority,
28 config->n_boot_priorities);
29 config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
30 config->boot_priorities[1].type = DEVICE_TYPE_DISK;
34 static struct interface_config *config_copy_interface(struct config *ctx,
35 struct interface_config *src)
37 struct interface_config *dest = talloc(ctx, struct interface_config);
39 memcpy(dest->hwaddr, src->hwaddr, sizeof(src->hwaddr));
40 dest->ignore = src->ignore;
45 dest->method = src->method;
47 switch (src->method) {
48 case CONFIG_METHOD_DHCP:
50 case CONFIG_METHOD_STATIC:
51 dest->static_config.address =
52 talloc_strdup(dest, src->static_config.address);
53 dest->static_config.gateway =
54 talloc_strdup(dest, src->static_config.gateway);
61 struct config *config_copy(void *ctx, const struct config *src)
66 dest = talloc(ctx, struct config);
67 dest->autoboot_enabled = src->autoboot_enabled;
68 dest->autoboot_timeout_sec = src->autoboot_timeout_sec;
70 dest->network.n_interfaces = src->network.n_interfaces;
71 dest->network.interfaces = talloc_array(dest, struct interface_config *,
72 dest->network.n_interfaces);
73 dest->network.n_dns_servers = src->network.n_dns_servers;
74 dest->network.dns_servers = talloc_array(dest, const char *,
75 dest->network.n_dns_servers);
77 for (i = 0; i < src->network.n_interfaces; i++)
78 dest->network.interfaces[i] = config_copy_interface(dest,
79 src->network.interfaces[i]);
81 for (i = 0; i < src->network.n_dns_servers; i++)
82 dest->network.dns_servers[i] = talloc_strdup(dest,
83 src->network.dns_servers[i]);
85 dest->n_boot_priorities = src->n_boot_priorities;
86 dest->boot_priorities = talloc_array(dest, struct boot_priority,
87 src->n_boot_priorities);
89 for (i = 0; i < src->n_boot_priorities; i++)
90 dest->boot_priorities[i].type = src->boot_priorities[i].type;
95 void dump_config(struct config *config);
96 void dump_config(struct config *config)
100 pb_log("configuration:\n");
102 if (config->autoboot_enabled)
103 pb_log(" autoboot: enabled, %d sec\n",
104 config->autoboot_timeout_sec);
106 pb_log(" autoboot: disabled\n");
108 if (config->network.n_interfaces || config->network.n_dns_servers)
109 pb_log(" network configuration:\n");
111 for (i = 0; i < config->network.n_interfaces; i++) {
112 struct interface_config *ifconf =
113 config->network.interfaces[i];
115 pb_log(" interface %02x:%02x:%02x:%02x:%02x:%02x\n",
116 ifconf->hwaddr[0], ifconf->hwaddr[1],
117 ifconf->hwaddr[2], ifconf->hwaddr[3],
118 ifconf->hwaddr[4], ifconf->hwaddr[5]);
120 if (ifconf->ignore) {
125 if (ifconf->method == CONFIG_METHOD_DHCP) {
128 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
129 pb_log(" static:\n");
130 pb_log(" ip: %s\n", ifconf->static_config.address);
131 pb_log(" gw: %s\n", ifconf->static_config.gateway);
135 for (i = 0; i < config->network.n_dns_servers; i++)
136 pb_log(" dns server %s\n", config->network.dns_servers[i]);
139 int config_init(void *ctx)
141 config_ctx = talloc_new(ctx);
143 config = talloc(config_ctx, struct config);
144 config_set_defaults(config);
146 storage = create_powerpc_nvram_storage(config);
148 storage->load(storage, config);
155 int config_set(struct config *newconfig)
159 if (!storage || !storage->save)
162 if (newconfig == config)
165 pb_log("new configuration data received\n");
166 dump_config(newconfig);
168 rc = storage->save(storage, newconfig);
171 config = talloc_steal(config_ctx, newconfig);
173 pb_log("error saving new configuration; changes lost\n");
178 /* A non-exported function to allow the test infrastructure to initialise
179 * (and change) the configuration variables */
181 struct config __attribute__((unused)) *test_config_init(
182 struct parser_test *test);
183 struct config *test_config_init(struct parser_test *test)
185 config = talloc(test, struct config);
186 config_set_defaults(config);
190 const struct config *config_get(void)
195 void config_set_autoboot(bool autoboot_enabled)
197 config->autoboot_enabled = autoboot_enabled;
199 pb_log("set autoboot: %s\n",
200 config->autoboot_enabled ? "enabled" : "disabled");
203 int config_fini(void)
205 talloc_free(config_ctx);