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->boot_device)
83 pb_log(" boot device %s\n", config->boot_device);
85 if (config->n_boot_priorities)
86 pb_log(" boot priority order:\n");
88 for (i = 0; i < config->n_boot_priorities; i++) {
89 struct boot_priority *prio = &config->boot_priorities[i];
90 pb_log(" %10s: %d\n", device_type_name(prio->type),
94 pb_log(" language: %s\n", config->lang ?: "");
97 static bool config_debug_on_cmdline(void)
102 fd = open("/proc/cmdline", O_RDONLY);
106 rc = read(fd, buf, sizeof(buf));
112 return memmem(buf, rc, kernel_cmdline_debug,
113 strlen(kernel_cmdline_debug)) != NULL;
116 void config_set_defaults(struct config *config)
118 config->autoboot_enabled = true;
119 config->autoboot_timeout_sec = 10;
120 config->autoboot_enabled = true;
121 config->network.interfaces = NULL;
122 config->network.n_interfaces = 0;
123 config->network.dns_servers = NULL;
124 config->network.n_dns_servers = 0;
125 config->boot_device = NULL;
126 config->safe_mode = false;
129 config->n_boot_priorities = 2;
130 config->boot_priorities = talloc_array(config, struct boot_priority,
131 config->n_boot_priorities);
132 config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
133 config->boot_priorities[0].priority = 2;
134 config->boot_priorities[1].type = DEVICE_TYPE_DISK;
135 config->boot_priorities[1].priority = 1;
137 config->debug = config_debug_on_cmdline();
140 int platform_init(void *ctx)
142 extern struct platform *__start_platforms, *__stop_platforms;
145 platform_ctx = talloc_new(ctx);
147 for (p = &__start_platforms; p < &__stop_platforms; p++) {
148 if (!(*p)->probe(*p, platform_ctx))
154 config = talloc(platform_ctx, struct config);
155 config_set_defaults(config);
158 pb_log("Detected platform type: %s\n", platform->name);
159 if (platform->load_config)
160 platform->load_config(platform, config);
162 pb_log("No platform type detected, some platform-specific "
163 "functionality will be disabled\n");
171 const struct platform *platform_get(void)
176 int platform_get_sysinfo(struct system_info *info)
178 if (platform && platform->get_sysinfo)
179 return platform->get_sysinfo(platform, info);
183 int config_set(struct config *newconfig)
187 if (!platform || !platform->save_config)
190 if (newconfig == config)
193 pb_log("new configuration data received\n");
194 dump_config(newconfig);
196 rc = platform->save_config(platform, newconfig);
199 config = talloc_steal(platform_ctx, newconfig);
201 pb_log("error saving new configuration; changes lost\n");
206 /* A non-exported function to allow the test infrastructure to initialise
207 * (and change) the configuration variables */
209 struct config __attribute__((unused)) *test_config_init(
210 struct parser_test *test);
211 struct config *test_config_init(struct parser_test *test)
213 config = talloc(test, struct config);
214 config_set_defaults(config);
218 const struct config *config_get(void)
223 void config_set_autoboot(bool autoboot_enabled)
225 config->autoboot_enabled = autoboot_enabled;
227 pb_log("set autoboot: %s\n",
228 config->autoboot_enabled ? "enabled" : "disabled");
231 int platform_fini(void)
233 talloc_free(platform_ctx);