lib/efi: Cleanup read/write routines
[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                         pb_log("    url:  %s\n", ifconf->static_config.url);
64
65                 }
66         }
67         for (i = 0; i < config->network.n_dns_servers; i++)
68                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
69
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));
74                 else
75                         pb_log("  boot device %d: uuid: %s\n",
76                                i, config->autoboot_opts[i].uuid);
77         }
78
79         pb_log("  IPMI boot device 0x%02x%s\n", config->ipmi_bootdev,
80                         config->ipmi_bootdev_persistent ? " (persistent)" : "");
81
82         pb_log("  Modifications allowed to disks: %s\n",
83                         config->allow_writes ? "yes" : "no");
84
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");
89
90         if (config->http_proxy)
91                 pb_log("  HTTP Proxy: %s\n", config->http_proxy);
92         if (config->https_proxy)
93                 pb_log("  HTTPS Proxy: %s\n", config->https_proxy);
94
95
96         pb_log(" language: %s\n", config->lang ?: "");
97 }
98
99 static bool config_debug_on_cmdline(void)
100 {
101         char buf[600];
102         int rc, fd;
103
104         fd = open("/proc/cmdline", O_RDONLY);
105         if (fd < 0)
106                 return false;
107
108         rc = read(fd, buf, sizeof(buf));
109         close(fd);
110
111         if (rc <= 0)
112                 return false;
113
114         return memmem(buf, rc, kernel_cmdline_debug,
115                         strlen(kernel_cmdline_debug)) != NULL;
116 }
117
118 void config_set_defaults(struct config *config)
119 {
120         const char *lang;
121
122         config->autoboot_enabled = true;
123         config->autoboot_timeout_sec = 10;
124         config->autoboot_enabled = true;
125         config->network.interfaces = NULL;
126         config->network.n_interfaces = 0;
127         config->network.dns_servers = NULL;
128         config->network.n_dns_servers = 0;
129         config->http_proxy = NULL;
130         config->https_proxy = NULL;
131         config->safe_mode = false;
132         config->allow_writes = true;
133         config->disable_snapshots = false;
134
135         config->n_consoles = 0;
136         config->consoles = NULL;
137         config->boot_console = NULL;
138
139         config->n_autoboot_opts = 2;
140         config->autoboot_opts = talloc_array(config, struct autoboot_option,
141                                                 config->n_autoboot_opts);
142         config->autoboot_opts[0].boot_type = BOOT_DEVICE_TYPE;
143         config->autoboot_opts[0].type = DEVICE_TYPE_NETWORK;
144         config->autoboot_opts[1].boot_type = BOOT_DEVICE_TYPE;
145         config->autoboot_opts[1].type = DEVICE_TYPE_ANY;
146
147         config->ipmi_bootdev = 0;
148         config->ipmi_bootdev_persistent = false;
149
150         config->debug = config_debug_on_cmdline();
151
152         lang = setlocale(LC_ALL, NULL);
153         pb_log("lang: %s\n", lang);
154         if (lang && strlen(lang))
155                 config->lang = talloc_strdup(config, lang);
156         else
157                 config->lang = NULL;
158
159 }
160
161 int platform_init(void *ctx)
162 {
163         extern struct platform *__start_platforms,  *__stop_platforms;
164         struct platform **p;
165
166         platform_ctx = talloc_new(ctx);
167
168         for (p = &__start_platforms; p < &__stop_platforms; p++) {
169                 pb_debug("%s: Try platform %s\n", __func__, (*p)->name);
170                 if (!(*p)->probe(*p, platform_ctx))
171                         continue;
172                 platform = *p;
173                 break;
174         }
175
176         config = talloc(platform_ctx, struct config);
177         config_set_defaults(config);
178
179         if (platform) {
180                 pb_log("Detected platform type: %s\n", platform->name);
181                 if (platform->load_config)
182                         platform->load_config(platform, config);
183         } else {
184                 pb_log("No platform type detected, some platform-specific "
185                                 "functionality will be disabled\n");
186         }
187
188         dump_config(config);
189
190         return 0;
191 }
192
193 const struct platform *platform_get(void)
194 {
195         return platform;
196 }
197
198 void platform_pre_boot(void)
199 {
200         const struct config *config = config_get();
201
202         if (platform && config && platform->pre_boot)
203                 platform->pre_boot(platform, config);
204 }
205
206 int platform_get_sysinfo(struct system_info *info)
207 {
208         if (platform && platform->get_sysinfo)
209                 return platform->get_sysinfo(platform, info);
210         return -1;
211 }
212
213 int config_set(struct config *newconfig)
214 {
215         int rc;
216
217         if (!platform || !platform->save_config)
218                 return -1;
219
220         if (newconfig == config)
221                 return 0;
222
223         pb_log("new configuration data received\n");
224         dump_config(newconfig);
225
226         rc = platform->save_config(platform, newconfig);
227
228         if (!rc)
229                 config = talloc_steal(platform_ctx, newconfig);
230         else
231                 pb_log("error saving new configuration; changes lost\n");
232
233         return rc;
234 }
235
236 /* A non-exported function to allow the test infrastructure to initialise
237  * (and change) the configuration variables */
238 struct parser_test;
239 struct config __attribute__((unused)) *test_config_init(
240                 struct parser_test *test);
241 struct config *test_config_init(struct parser_test *test)
242 {
243         config = talloc(test, struct config);
244         config_set_defaults(config);
245         return config;
246 }
247
248 const struct config *config_get(void)
249 {
250         return config;
251 }
252
253 void config_set_autoboot(bool autoboot_enabled)
254 {
255         config->autoboot_enabled = autoboot_enabled;
256
257         pb_log("set autoboot: %s\n",
258                         config->autoboot_enabled ? "enabled" : "disabled");
259 }
260
261 int platform_fini(void)
262 {
263         talloc_free(platform_ctx);
264         return 0;
265 }