discover: Set default language
[petitboot] / discover / platform.c
1
2 #define _GNU_SOURCE
3
4 #include <fcntl.h>
5 #include <string.h>
6 #include <locale.h>
7
8 #include <log/log.h>
9 #include <file/file.h>
10 #include <types/types.h>
11 #include <talloc/talloc.h>
12
13 #include "platform.h"
14
15 void                    *platform_ctx;
16 static struct platform  *platform;
17 static struct config    *config;
18
19 static const char *kernel_cmdline_debug = "petitboot.debug";
20
21 static void dump_config(struct config *config)
22 {
23         unsigned int i;
24
25         pb_log("configuration:\n");
26
27         if (config->autoboot_enabled)
28                 pb_log(" autoboot: enabled, %d sec\n",
29                                 config->autoboot_timeout_sec);
30         else
31                 pb_log(" autoboot: disabled\n");
32
33         if (config->network.n_interfaces || config->network.n_dns_servers)
34                 pb_log(" network configuration:\n");
35
36         if (config->safe_mode)
37                 pb_log(" safe mode: active\n");
38
39         if (config->disable_snapshots)
40                 pb_log(" dm-snapshots disabled\n");
41
42         for (i = 0; i < config->network.n_interfaces; i++) {
43                 struct interface_config *ifconf =
44                         config->network.interfaces[i];
45
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]);
50
51                 if (ifconf->ignore) {
52                         pb_log("   ignore\n");
53                         continue;
54                 }
55
56                 if (ifconf->method == CONFIG_METHOD_DHCP) {
57                         pb_log("   dhcp\n");
58
59                 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
60                         pb_log("   static:\n");
61                         pb_log("    ip:  %s\n", ifconf->static_config.address);
62                         pb_log("    gw:  %s\n", ifconf->static_config.gateway);
63
64                 }
65         }
66         for (i = 0; i < config->network.n_dns_servers; i++)
67                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
68
69         for (i = 0; i < config->n_autoboot_opts; i++) {
70                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
71                         pb_log("  boot device %d: %s\n", i,
72                                device_type_name(config->autoboot_opts[i].type));
73                 else
74                         pb_log("  boot device %d: uuid: %s\n",
75                                i, config->autoboot_opts[i].uuid);
76         }
77
78         pb_log("  IPMI boot device 0x%02x%s\n", config->ipmi_bootdev,
79                         config->ipmi_bootdev_persistent ? " (persistent)" : "");
80
81
82         pb_log(" language: %s\n", config->lang ?: "");
83 }
84
85 static bool config_debug_on_cmdline(void)
86 {
87         char buf[600];
88         int rc, fd;
89
90         fd = open("/proc/cmdline", O_RDONLY);
91         if (fd < 0)
92                 return false;
93
94         rc = read(fd, buf, sizeof(buf));
95         close(fd);
96
97         if (rc <= 0)
98                 return false;
99
100         return memmem(buf, rc, kernel_cmdline_debug,
101                         strlen(kernel_cmdline_debug)) != NULL;
102 }
103
104 void config_set_defaults(struct config *config)
105 {
106         const char *lang;
107
108         config->autoboot_enabled = true;
109         config->autoboot_timeout_sec = 10;
110         config->autoboot_enabled = true;
111         config->network.interfaces = NULL;
112         config->network.n_interfaces = 0;
113         config->network.dns_servers = NULL;
114         config->network.n_dns_servers = 0;
115         config->safe_mode = false;
116         config->allow_writes = true;
117         config->disable_snapshots = false;
118
119         config->n_autoboot_opts = 2;
120         config->autoboot_opts = talloc_array(config, struct autoboot_option,
121                                                 config->n_autoboot_opts);
122         config->autoboot_opts[0].boot_type = BOOT_DEVICE_TYPE;
123         config->autoboot_opts[0].type = DEVICE_TYPE_NETWORK;
124         config->autoboot_opts[1].boot_type = BOOT_DEVICE_TYPE;
125         config->autoboot_opts[1].type = DEVICE_TYPE_ANY;
126
127         config->ipmi_bootdev = 0;
128         config->ipmi_bootdev_persistent = false;
129
130         config->debug = config_debug_on_cmdline();
131
132         lang = setlocale(LC_ALL, NULL);
133         pb_log("lang: %s\n", lang);
134         if (lang && strlen(lang))
135                 config->lang = talloc_strdup(config, lang);
136         else
137                 config->lang = NULL;
138
139 }
140
141 int platform_init(void *ctx)
142 {
143         extern struct platform *__start_platforms,  *__stop_platforms;
144         struct platform **p;
145
146         platform_ctx = talloc_new(ctx);
147
148         for (p = &__start_platforms; p < &__stop_platforms; p++) {
149                 if (!(*p)->probe(*p, platform_ctx))
150                         continue;
151                 platform = *p;
152                 break;
153         }
154
155         config = talloc(platform_ctx, struct config);
156         config_set_defaults(config);
157
158         if (platform) {
159                 pb_log("Detected platform type: %s\n", platform->name);
160                 if (platform->load_config)
161                         platform->load_config(platform, config);
162         } else {
163                 pb_log("No platform type detected, some platform-specific "
164                                 "functionality will be disabled\n");
165         }
166
167         dump_config(config);
168
169         return 0;
170 }
171
172 const struct platform *platform_get(void)
173 {
174         return platform;
175 }
176
177 void platform_pre_boot(void)
178 {
179         const struct config *config = config_get();
180
181         if (platform && config && platform->pre_boot)
182                 platform->pre_boot(platform, config);
183 }
184
185 int platform_get_sysinfo(struct system_info *info)
186 {
187         if (platform && platform->get_sysinfo)
188                 return platform->get_sysinfo(platform, info);
189         return -1;
190 }
191
192 int config_set(struct config *newconfig)
193 {
194         int rc;
195
196         if (!platform || !platform->save_config)
197                 return -1;
198
199         if (newconfig == config)
200                 return 0;
201
202         pb_log("new configuration data received\n");
203         dump_config(newconfig);
204
205         rc = platform->save_config(platform, newconfig);
206
207         if (!rc)
208                 config = talloc_steal(platform_ctx, newconfig);
209         else
210                 pb_log("error saving new configuration; changes lost\n");
211
212         return rc;
213 }
214
215 /* A non-exported function to allow the test infrastructure to initialise
216  * (and change) the configuration variables */
217 struct parser_test;
218 struct config __attribute__((unused)) *test_config_init(
219                 struct parser_test *test);
220 struct config *test_config_init(struct parser_test *test)
221 {
222         config = talloc(test, struct config);
223         config_set_defaults(config);
224         return config;
225 }
226
227 const struct config *config_get(void)
228 {
229         return config;
230 }
231
232 void config_set_autoboot(bool autoboot_enabled)
233 {
234         config->autoboot_enabled = autoboot_enabled;
235
236         pb_log("set autoboot: %s\n",
237                         config->autoboot_enabled ? "enabled" : "disabled");
238 }
239
240 int platform_fini(void)
241 {
242         talloc_free(platform_ctx);
243         return 0;
244 }