5 #include <types/types.h>
6 #include <talloc/talloc.h>
11 static struct platform *platform;
12 static struct config *config;
14 static const char *device_type_name(enum device_type type)
17 case DEVICE_TYPE_DISK:
19 case DEVICE_TYPE_OPTICAL:
21 case DEVICE_TYPE_NETWORK:
25 case DEVICE_TYPE_UNKNOWN:
31 static void dump_config(struct config *config)
35 pb_log("configuration:\n");
37 if (config->autoboot_enabled)
38 pb_log(" autoboot: enabled, %d sec\n",
39 config->autoboot_timeout_sec);
41 pb_log(" autoboot: disabled\n");
43 if (config->network.n_interfaces || config->network.n_dns_servers)
44 pb_log(" network configuration:\n");
46 for (i = 0; i < config->network.n_interfaces; i++) {
47 struct interface_config *ifconf =
48 config->network.interfaces[i];
50 pb_log(" interface %02x:%02x:%02x:%02x:%02x:%02x\n",
51 ifconf->hwaddr[0], ifconf->hwaddr[1],
52 ifconf->hwaddr[2], ifconf->hwaddr[3],
53 ifconf->hwaddr[4], ifconf->hwaddr[5]);
60 if (ifconf->method == CONFIG_METHOD_DHCP) {
63 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
65 pb_log(" ip: %s\n", ifconf->static_config.address);
66 pb_log(" gw: %s\n", ifconf->static_config.gateway);
70 for (i = 0; i < config->network.n_dns_servers; i++)
71 pb_log(" dns server %s\n", config->network.dns_servers[i]);
73 if (config->n_boot_priorities)
74 pb_log(" boot priority order:\n");
76 for (i = 0; i < config->n_boot_priorities; i++) {
77 struct boot_priority *prio = &config->boot_priorities[i];
78 pb_log(" %10s: %d\n", device_type_name(prio->type),
83 void config_set_defaults(struct config *config)
85 config->autoboot_enabled = true;
86 config->autoboot_timeout_sec = 10;
87 config->network.interfaces = NULL;
88 config->network.n_interfaces = 0;
89 config->network.dns_servers = NULL;
90 config->network.n_dns_servers = 0;
91 config->boot_device = NULL;
93 config->n_boot_priorities = 2;
94 config->boot_priorities = talloc_array(config, struct boot_priority,
95 config->n_boot_priorities);
96 config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
97 config->boot_priorities[0].priority = 2;
98 config->boot_priorities[1].type = DEVICE_TYPE_DISK;
99 config->boot_priorities[1].priority = 1;
102 int platform_init(void *ctx)
104 extern struct platform *__start_platforms, *__stop_platforms;
107 platform_ctx = talloc_new(ctx);
109 for (p = &__start_platforms; p < &__stop_platforms; p++) {
110 if (!(*p)->probe(*p, platform_ctx))
116 config = talloc(platform_ctx, struct config);
117 config_set_defaults(config);
120 pb_log("Detected platform type: %s\n", platform->name);
121 if (platform->load_config)
122 platform->load_config(platform, config);
124 pb_log("No platform type detected, some platform-specific "
125 "functionality will be disabled\n");
133 const struct platform *platform_get(void)
138 int config_set(struct config *newconfig)
142 if (!platform || !platform->save_config)
145 if (newconfig == config)
148 pb_log("new configuration data received\n");
149 dump_config(newconfig);
151 rc = platform->save_config(platform, newconfig);
154 config = talloc_steal(platform_ctx, newconfig);
156 pb_log("error saving new configuration; changes lost\n");
161 /* A non-exported function to allow the test infrastructure to initialise
162 * (and change) the configuration variables */
164 struct config __attribute__((unused)) *test_config_init(
165 struct parser_test *test);
166 struct config *test_config_init(struct parser_test *test)
168 config = talloc(test, struct config);
169 config_set_defaults(config);
173 const struct config *config_get(void)
178 void config_set_autoboot(bool autoboot_enabled)
180 config->autoboot_enabled = autoboot_enabled;
182 pb_log("set autoboot: %s\n",
183 config->autoboot_enabled ? "enabled" : "disabled");
186 int platform_fini(void)
188 talloc_free(platform_ctx);