]> git.ozlabs.org Git - petitboot/blob - discover/platform.c
b1d0f19bafadd8a1ca1059bfe530c04de26eb801
[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         for (i = 0; i < config->n_autoboot_opts; i++) {
66                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
67                         pb_log("  boot device %d: %s\n", i,
68                                device_type_name(config->autoboot_opts[i].type));
69                 else
70                         pb_log("  boot device %d: uuid: %s\n",
71                                i, config->autoboot_opts[i].uuid);
72         }
73
74         pb_log("  IPMI boot device 0x%02x%s\n", config->ipmi_bootdev,
75                         config->ipmi_bootdev_persistent ? " (persistent)" : "");
76
77
78         pb_log(" language: %s\n", config->lang ?: "");
79 }
80
81 static bool config_debug_on_cmdline(void)
82 {
83         char buf[600];
84         int rc, fd;
85
86         fd = open("/proc/cmdline", O_RDONLY);
87         if (fd < 0)
88                 return false;
89
90         rc = read(fd, buf, sizeof(buf));
91         close(fd);
92
93         if (rc <= 0)
94                 return false;
95
96         return memmem(buf, rc, kernel_cmdline_debug,
97                         strlen(kernel_cmdline_debug)) != NULL;
98 }
99
100 void config_set_defaults(struct config *config)
101 {
102         config->autoboot_enabled = true;
103         config->autoboot_timeout_sec = 10;
104         config->autoboot_enabled = true;
105         config->network.interfaces = NULL;
106         config->network.n_interfaces = 0;
107         config->network.dns_servers = NULL;
108         config->network.n_dns_servers = 0;
109         config->safe_mode = false;
110         config->lang = NULL;
111         config->allow_writes = true;
112
113         config->n_autoboot_opts = 2;
114         config->autoboot_opts = talloc_array(config, struct autoboot_option,
115                                                 config->n_autoboot_opts);
116         config->autoboot_opts[0].boot_type = BOOT_DEVICE_TYPE;
117         config->autoboot_opts[0].type = DEVICE_TYPE_NETWORK;
118         config->autoboot_opts[1].boot_type = BOOT_DEVICE_TYPE;
119         config->autoboot_opts[1].type = DEVICE_TYPE_ANY;
120
121         config->ipmi_bootdev = 0;
122         config->ipmi_bootdev_persistent = false;
123
124         config->debug = config_debug_on_cmdline();
125 }
126
127 int platform_init(void *ctx)
128 {
129         extern struct platform *__start_platforms,  *__stop_platforms;
130         struct platform **p;
131
132         platform_ctx = talloc_new(ctx);
133
134         for (p = &__start_platforms; p < &__stop_platforms; p++) {
135                 if (!(*p)->probe(*p, platform_ctx))
136                         continue;
137                 platform = *p;
138                 break;
139         }
140
141         config = talloc(platform_ctx, struct config);
142         config_set_defaults(config);
143
144         if (platform) {
145                 pb_log("Detected platform type: %s\n", platform->name);
146                 if (platform->load_config)
147                         platform->load_config(platform, config);
148         } else {
149                 pb_log("No platform type detected, some platform-specific "
150                                 "functionality will be disabled\n");
151         }
152
153         dump_config(config);
154
155         return 0;
156 }
157
158 const struct platform *platform_get(void)
159 {
160         return platform;
161 }
162
163 void platform_pre_boot(void)
164 {
165         const struct config *config = config_get();
166
167         if (platform && config && platform->pre_boot)
168                 platform->pre_boot(platform, config);
169 }
170
171 int platform_get_sysinfo(struct system_info *info)
172 {
173         if (platform && platform->get_sysinfo)
174                 return platform->get_sysinfo(platform, info);
175         return -1;
176 }
177
178 int config_set(struct config *newconfig)
179 {
180         int rc;
181
182         if (!platform || !platform->save_config)
183                 return -1;
184
185         if (newconfig == config)
186                 return 0;
187
188         pb_log("new configuration data received\n");
189         dump_config(newconfig);
190
191         rc = platform->save_config(platform, newconfig);
192
193         if (!rc)
194                 config = talloc_steal(platform_ctx, newconfig);
195         else
196                 pb_log("error saving new configuration; changes lost\n");
197
198         return rc;
199 }
200
201 /* A non-exported function to allow the test infrastructure to initialise
202  * (and change) the configuration variables */
203 struct parser_test;
204 struct config __attribute__((unused)) *test_config_init(
205                 struct parser_test *test);
206 struct config *test_config_init(struct parser_test *test)
207 {
208         config = talloc(test, struct config);
209         config_set_defaults(config);
210         return config;
211 }
212
213 const struct config *config_get(void)
214 {
215         return config;
216 }
217
218 void config_set_autoboot(bool autoboot_enabled)
219 {
220         config->autoboot_enabled = autoboot_enabled;
221
222         pb_log("set autoboot: %s\n",
223                         config->autoboot_enabled ? "enabled" : "disabled");
224 }
225
226 int platform_fini(void)
227 {
228         talloc_free(platform_ctx);
229         return 0;
230 }