5 #include <types/types.h>
6 #include <talloc/talloc.h>
11 static struct platform *platform;
12 static struct config *config;
14 static void dump_config(struct config *config)
18 pb_log("configuration:\n");
20 if (config->autoboot_enabled)
21 pb_log(" autoboot: enabled, %d sec\n",
22 config->autoboot_timeout_sec);
24 pb_log(" autoboot: disabled\n");
26 if (config->network.n_interfaces || config->network.n_dns_servers)
27 pb_log(" network configuration:\n");
29 for (i = 0; i < config->network.n_interfaces; i++) {
30 struct interface_config *ifconf =
31 config->network.interfaces[i];
33 pb_log(" interface %02x:%02x:%02x:%02x:%02x:%02x\n",
34 ifconf->hwaddr[0], ifconf->hwaddr[1],
35 ifconf->hwaddr[2], ifconf->hwaddr[3],
36 ifconf->hwaddr[4], ifconf->hwaddr[5]);
43 if (ifconf->method == CONFIG_METHOD_DHCP) {
46 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
48 pb_log(" ip: %s\n", ifconf->static_config.address);
49 pb_log(" gw: %s\n", ifconf->static_config.gateway);
53 for (i = 0; i < config->network.n_dns_servers; i++)
54 pb_log(" dns server %s\n", config->network.dns_servers[i]);
57 void config_set_defaults(struct config *config)
59 config->autoboot_enabled = true;
60 config->autoboot_timeout_sec = 10;
61 config->network.interfaces = NULL;
62 config->network.n_interfaces = 0;
63 config->network.dns_servers = NULL;
64 config->network.n_dns_servers = 0;
66 config->n_boot_priorities = 2;
67 config->boot_priorities = talloc_array(config, struct boot_priority,
68 config->n_boot_priorities);
69 config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
70 config->boot_priorities[0].priority = 2;
71 config->boot_priorities[1].type = DEVICE_TYPE_DISK;
72 config->boot_priorities[1].priority = 1;
75 int platform_init(void *ctx)
77 extern struct platform *__start_platforms, *__stop_platforms;
80 platform_ctx = talloc_new(ctx);
82 for (p = &__start_platforms; p < &__stop_platforms; p++) {
83 if (!(*p)->probe(*p, platform_ctx))
89 config = talloc(platform_ctx, struct config);
90 config_set_defaults(config);
93 pb_log("Detected platform type: %s\n", platform->name);
94 if (platform->load_config)
95 platform->load_config(platform, config);
97 pb_log("No platform type detected, some platform-specific "
98 "functionality will be disabled\n");
106 const struct platform *platform_get(void)
111 int config_set(struct config *newconfig)
115 if (!platform || !platform->save_config)
118 if (newconfig == config)
121 pb_log("new configuration data received\n");
122 dump_config(newconfig);
124 rc = platform->save_config(platform, newconfig);
127 config = talloc_steal(platform_ctx, newconfig);
129 pb_log("error saving new configuration; changes lost\n");
134 /* A non-exported function to allow the test infrastructure to initialise
135 * (and change) the configuration variables */
137 struct config __attribute__((unused)) *test_config_init(
138 struct parser_test *test);
139 struct config *test_config_init(struct parser_test *test)
141 config = talloc(test, struct config);
142 config_set_defaults(config);
146 const struct config *config_get(void)
151 void config_set_autoboot(bool autoboot_enabled)
153 config->autoboot_enabled = autoboot_enabled;
155 pb_log("set autoboot: %s\n",
156 config->autoboot_enabled ? "enabled" : "disabled");
159 int platform_fini(void)
161 talloc_free(platform_ctx);