9 #include <types/types.h>
10 #include <talloc/talloc.h>
15 static struct platform *platform;
16 static struct config *config;
18 static const char *kernel_cmdline_debug = "petitboot.debug";
20 static const char *device_type_name(enum device_type type)
23 case DEVICE_TYPE_DISK:
25 case DEVICE_TYPE_OPTICAL:
27 case DEVICE_TYPE_NETWORK:
31 case DEVICE_TYPE_UNKNOWN:
37 static void dump_config(struct config *config)
41 pb_log("configuration:\n");
43 if (config->autoboot_enabled)
44 pb_log(" autoboot: enabled, %d sec\n",
45 config->autoboot_timeout_sec);
47 pb_log(" autoboot: disabled\n");
49 if (config->network.n_interfaces || config->network.n_dns_servers)
50 pb_log(" network configuration:\n");
52 if (config->safe_mode)
53 pb_log(" safe mode: active\n");
55 for (i = 0; i < config->network.n_interfaces; i++) {
56 struct interface_config *ifconf =
57 config->network.interfaces[i];
59 pb_log(" interface %02x:%02x:%02x:%02x:%02x:%02x\n",
60 ifconf->hwaddr[0], ifconf->hwaddr[1],
61 ifconf->hwaddr[2], ifconf->hwaddr[3],
62 ifconf->hwaddr[4], ifconf->hwaddr[5]);
69 if (ifconf->method == CONFIG_METHOD_DHCP) {
72 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
74 pb_log(" ip: %s\n", ifconf->static_config.address);
75 pb_log(" gw: %s\n", ifconf->static_config.gateway);
79 for (i = 0; i < config->network.n_dns_servers; i++)
80 pb_log(" dns server %s\n", config->network.dns_servers[i]);
82 if (config->n_boot_priorities)
83 pb_log(" boot priority order:\n");
85 for (i = 0; i < config->n_boot_priorities; i++) {
86 struct boot_priority *prio = &config->boot_priorities[i];
87 pb_log(" %10s: %d\n", device_type_name(prio->type),
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)
115 config->autoboot_enabled = true;
116 config->autoboot_timeout_sec = 10;
117 config->autoboot_enabled = true;
118 config->network.interfaces = NULL;
119 config->network.n_interfaces = 0;
120 config->network.dns_servers = NULL;
121 config->network.n_dns_servers = 0;
122 config->boot_device = NULL;
123 config->safe_mode = false;
126 config->n_boot_priorities = 2;
127 config->boot_priorities = talloc_array(config, struct boot_priority,
128 config->n_boot_priorities);
129 config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
130 config->boot_priorities[0].priority = 2;
131 config->boot_priorities[1].type = DEVICE_TYPE_DISK;
132 config->boot_priorities[1].priority = 1;
134 config->debug = config_debug_on_cmdline();
137 int platform_init(void *ctx)
139 extern struct platform *__start_platforms, *__stop_platforms;
142 platform_ctx = talloc_new(ctx);
144 for (p = &__start_platforms; p < &__stop_platforms; p++) {
145 if (!(*p)->probe(*p, platform_ctx))
151 config = talloc(platform_ctx, struct config);
152 config_set_defaults(config);
155 pb_log("Detected platform type: %s\n", platform->name);
156 if (platform->load_config)
157 platform->load_config(platform, config);
159 pb_log("No platform type detected, some platform-specific "
160 "functionality will be disabled\n");
168 const struct platform *platform_get(void)
173 int platform_get_sysinfo(struct system_info *info)
175 if (platform && platform->get_sysinfo)
176 return platform->get_sysinfo(platform, info);
180 int config_set(struct config *newconfig)
184 if (!platform || !platform->save_config)
187 if (newconfig == config)
190 pb_log("new configuration data received\n");
191 dump_config(newconfig);
193 rc = platform->save_config(platform, newconfig);
196 config = talloc_steal(platform_ctx, newconfig);
198 pb_log("error saving new configuration; changes lost\n");
203 /* A non-exported function to allow the test infrastructure to initialise
204 * (and change) the configuration variables */
206 struct config __attribute__((unused)) *test_config_init(
207 struct parser_test *test);
208 struct config *test_config_init(struct parser_test *test)
210 config = talloc(test, struct config);
211 config_set_defaults(config);
215 const struct config *config_get(void)
220 void config_set_autoboot(bool autoboot_enabled)
222 config->autoboot_enabled = autoboot_enabled;
224 pb_log("set autoboot: %s\n",
225 config->autoboot_enabled ? "enabled" : "disabled");
228 int platform_fini(void)
230 talloc_free(platform_ctx);