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