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