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