discover: check config.boot_device before setting a default
[petitboot] / discover / platform.c
1
2 #include <string.h>
3
4 #include <log/log.h>
5 #include <types/types.h>
6 #include <talloc/talloc.h>
7
8 #include "platform.h"
9
10 void                    *platform_ctx;
11 static struct platform  *platform;
12 static struct config    *config;
13
14 static const char *device_type_name(enum device_type type)
15 {
16         switch (type) {
17         case DEVICE_TYPE_DISK:
18                 return "disk";
19         case DEVICE_TYPE_OPTICAL:
20                 return "optical";
21         case DEVICE_TYPE_NETWORK:
22                 return "network";
23         case DEVICE_TYPE_ANY:
24                 return "any";
25         case DEVICE_TYPE_UNKNOWN:
26         default:
27                 return "unknown";
28         }
29 }
30
31 static void dump_config(struct config *config)
32 {
33         unsigned int i;
34
35         pb_log("configuration:\n");
36
37         if (config->autoboot_enabled)
38                 pb_log(" autoboot: enabled, %d sec\n",
39                                 config->autoboot_timeout_sec);
40         else
41                 pb_log(" autoboot: disabled\n");
42
43         if (config->network.n_interfaces || config->network.n_dns_servers)
44                 pb_log(" network configuration:\n");
45
46         for (i = 0; i < config->network.n_interfaces; i++) {
47                 struct interface_config *ifconf =
48                         config->network.interfaces[i];
49
50                 pb_log("  interface %02x:%02x:%02x:%02x:%02x:%02x\n",
51                                 ifconf->hwaddr[0], ifconf->hwaddr[1],
52                                 ifconf->hwaddr[2], ifconf->hwaddr[3],
53                                 ifconf->hwaddr[4], ifconf->hwaddr[5]);
54
55                 if (ifconf->ignore) {
56                         pb_log("   ignore\n");
57                         continue;
58                 }
59
60                 if (ifconf->method == CONFIG_METHOD_DHCP) {
61                         pb_log("   dhcp\n");
62
63                 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
64                         pb_log("   static:\n");
65                         pb_log("    ip:  %s\n", ifconf->static_config.address);
66                         pb_log("    gw:  %s\n", ifconf->static_config.gateway);
67
68                 }
69         }
70         for (i = 0; i < config->network.n_dns_servers; i++)
71                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
72
73         if (config->n_boot_priorities)
74                 pb_log(" boot priority order:\n");
75
76         for (i = 0; i < config->n_boot_priorities; i++) {
77                 struct boot_priority *prio = &config->boot_priorities[i];
78                 pb_log(" %10s: %d\n", device_type_name(prio->type),
79                                         prio->priority);
80         }
81 }
82
83 void config_set_defaults(struct config *config)
84 {
85         config->autoboot_enabled = true;
86         config->autoboot_timeout_sec = 10;
87         config->network.interfaces = NULL;
88         config->network.n_interfaces = 0;
89         config->network.dns_servers = NULL;
90         config->network.n_dns_servers = 0;
91         config->boot_device = NULL;
92
93         config->n_boot_priorities = 2;
94         config->boot_priorities = talloc_array(config, struct boot_priority,
95                                                 config->n_boot_priorities);
96         config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
97         config->boot_priorities[0].priority = 2;
98         config->boot_priorities[1].type = DEVICE_TYPE_DISK;
99         config->boot_priorities[1].priority = 1;
100 }
101
102 int platform_init(void *ctx)
103 {
104         extern struct platform *__start_platforms,  *__stop_platforms;
105         struct platform **p;
106
107         platform_ctx = talloc_new(ctx);
108
109         for (p = &__start_platforms; p < &__stop_platforms; p++) {
110                 if (!(*p)->probe(*p, platform_ctx))
111                         continue;
112                 platform = *p;
113                 break;
114         }
115
116         config = talloc(platform_ctx, struct config);
117         config_set_defaults(config);
118
119         if (platform) {
120                 pb_log("Detected platform type: %s\n", platform->name);
121                 if (platform->load_config)
122                         platform->load_config(platform, config);
123         } else {
124                 pb_log("No platform type detected, some platform-specific "
125                                 "functionality will be disabled\n");
126         }
127
128         dump_config(config);
129
130         return 0;
131 }
132
133 const struct platform *platform_get(void)
134 {
135         return platform;
136 }
137
138 int config_set(struct config *newconfig)
139 {
140         int rc;
141
142         if (!platform || !platform->save_config)
143                 return -1;
144
145         if (newconfig == config)
146                 return 0;
147
148         pb_log("new configuration data received\n");
149         dump_config(newconfig);
150
151         rc = platform->save_config(platform, newconfig);
152
153         if (!rc)
154                 config = talloc_steal(platform_ctx, newconfig);
155         else
156                 pb_log("error saving new configuration; changes lost\n");
157
158         return rc;
159 }
160
161 /* A non-exported function to allow the test infrastructure to initialise
162  * (and change) the configuration variables */
163 struct parser_test;
164 struct config __attribute__((unused)) *test_config_init(
165                 struct parser_test *test);
166 struct config *test_config_init(struct parser_test *test)
167 {
168         config = talloc(test, struct config);
169         config_set_defaults(config);
170         return config;
171 }
172
173 const struct config *config_get(void)
174 {
175         return config;
176 }
177
178 void config_set_autoboot(bool autoboot_enabled)
179 {
180         config->autoboot_enabled = autoboot_enabled;
181
182         pb_log("set autoboot: %s\n",
183                         config->autoboot_enabled ? "enabled" : "disabled");
184 }
185
186 int platform_fini(void)
187 {
188         talloc_free(platform_ctx);
189         return 0;
190 }