10 #include <types/types.h>
11 #include <talloc/talloc.h>
16 static struct platform *platform;
17 static struct config *config;
19 static const char *kernel_cmdline_debug = "petitboot.debug";
21 static void dump_config(struct config *config)
25 pb_log("configuration:\n");
27 if (config->autoboot_enabled)
28 pb_log(" autoboot: enabled, %d sec\n",
29 config->autoboot_timeout_sec);
31 pb_log(" autoboot: disabled\n");
33 if (config->network.n_interfaces || config->network.n_dns_servers)
34 pb_log(" network configuration:\n");
36 if (config->safe_mode)
37 pb_log(" safe mode: active\n");
39 if (config->disable_snapshots)
40 pb_log(" dm-snapshots disabled\n");
42 for (i = 0; i < config->network.n_interfaces; i++) {
43 struct interface_config *ifconf =
44 config->network.interfaces[i];
46 pb_log(" interface %02x:%02x:%02x:%02x:%02x:%02x\n",
47 ifconf->hwaddr[0], ifconf->hwaddr[1],
48 ifconf->hwaddr[2], ifconf->hwaddr[3],
49 ifconf->hwaddr[4], ifconf->hwaddr[5]);
56 if (ifconf->method == CONFIG_METHOD_DHCP) {
59 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
61 pb_log(" ip: %s\n", ifconf->static_config.address);
62 pb_log(" gw: %s\n", ifconf->static_config.gateway);
63 pb_log(" url: %s\n", ifconf->static_config.url);
67 for (i = 0; i < config->network.n_dns_servers; i++)
68 pb_log(" dns server %s\n", config->network.dns_servers[i]);
70 for (i = 0; i < config->n_autoboot_opts; i++) {
71 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
72 pb_log(" boot device %d: %s\n", i,
73 device_type_name(config->autoboot_opts[i].type));
75 pb_log(" boot device %d: uuid: %s\n",
76 i, config->autoboot_opts[i].uuid);
79 pb_log(" IPMI boot device 0x%02x%s\n", config->ipmi_bootdev,
80 config->ipmi_bootdev_persistent ? " (persistent)" : "");
82 pb_log(" Modifications allowed to disks: %s\n",
83 config->allow_writes ? "yes" : "no");
85 pb_log(" Default UI to boot on: %s\n",
86 config->boot_console ?: "none set");
87 if (config->manual_console)
88 pb_log(" (Manually set)\n");
91 pb_log(" language: %s\n", config->lang ?: "");
94 static bool config_debug_on_cmdline(void)
99 fd = open("/proc/cmdline", O_RDONLY);
103 rc = read(fd, buf, sizeof(buf));
109 return memmem(buf, rc, kernel_cmdline_debug,
110 strlen(kernel_cmdline_debug)) != NULL;
113 void config_set_defaults(struct config *config)
117 config->autoboot_enabled = true;
118 config->autoboot_timeout_sec = 10;
119 config->autoboot_enabled = true;
120 config->network.interfaces = NULL;
121 config->network.n_interfaces = 0;
122 config->network.dns_servers = NULL;
123 config->network.n_dns_servers = 0;
124 config->safe_mode = false;
125 config->allow_writes = true;
126 config->disable_snapshots = false;
128 config->n_consoles = 0;
129 config->consoles = NULL;
130 config->boot_console = NULL;
132 config->n_autoboot_opts = 2;
133 config->autoboot_opts = talloc_array(config, struct autoboot_option,
134 config->n_autoboot_opts);
135 config->autoboot_opts[0].boot_type = BOOT_DEVICE_TYPE;
136 config->autoboot_opts[0].type = DEVICE_TYPE_NETWORK;
137 config->autoboot_opts[1].boot_type = BOOT_DEVICE_TYPE;
138 config->autoboot_opts[1].type = DEVICE_TYPE_ANY;
140 config->ipmi_bootdev = 0;
141 config->ipmi_bootdev_persistent = false;
143 config->debug = config_debug_on_cmdline();
145 lang = setlocale(LC_ALL, NULL);
146 pb_log("lang: %s\n", lang);
147 if (lang && strlen(lang))
148 config->lang = talloc_strdup(config, lang);
154 int platform_init(void *ctx)
156 extern struct platform *__start_platforms, *__stop_platforms;
159 platform_ctx = talloc_new(ctx);
161 for (p = &__start_platforms; p < &__stop_platforms; p++) {
162 if (!(*p)->probe(*p, platform_ctx))
168 config = talloc(platform_ctx, struct config);
169 config_set_defaults(config);
172 pb_log("Detected platform type: %s\n", platform->name);
173 if (platform->load_config)
174 platform->load_config(platform, config);
176 pb_log("No platform type detected, some platform-specific "
177 "functionality will be disabled\n");
185 const struct platform *platform_get(void)
190 void platform_pre_boot(void)
192 const struct config *config = config_get();
194 if (platform && config && platform->pre_boot)
195 platform->pre_boot(platform, config);
198 int platform_get_sysinfo(struct system_info *info)
200 if (platform && platform->get_sysinfo)
201 return platform->get_sysinfo(platform, info);
205 int config_set(struct config *newconfig)
209 if (!platform || !platform->save_config)
212 if (newconfig == config)
215 pb_log("new configuration data received\n");
216 dump_config(newconfig);
218 rc = platform->save_config(platform, newconfig);
221 config = talloc_steal(platform_ctx, newconfig);
223 pb_log("error saving new configuration; changes lost\n");
228 /* A non-exported function to allow the test infrastructure to initialise
229 * (and change) the configuration variables */
231 struct config __attribute__((unused)) *test_config_init(
232 struct parser_test *test);
233 struct config *test_config_init(struct parser_test *test)
235 config = talloc(test, struct config);
236 config_set_defaults(config);
240 const struct config *config_get(void)
245 void config_set_autoboot(bool autoboot_enabled)
247 config->autoboot_enabled = autoboot_enabled;
249 pb_log("set autoboot: %s\n",
250 config->autoboot_enabled ? "enabled" : "disabled");
253 int platform_fini(void)
255 talloc_free(platform_ctx);