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