discover: Include boot priorities in configuration dump
[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
92         config->n_boot_priorities = 2;
93         config->boot_priorities = talloc_array(config, struct boot_priority,
94                                                 config->n_boot_priorities);
95         config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
96         config->boot_priorities[0].priority = 2;
97         config->boot_priorities[1].type = DEVICE_TYPE_DISK;
98         config->boot_priorities[1].priority = 1;
99 }
100
101 int platform_init(void *ctx)
102 {
103         extern struct platform *__start_platforms,  *__stop_platforms;
104         struct platform **p;
105
106         platform_ctx = talloc_new(ctx);
107
108         for (p = &__start_platforms; p < &__stop_platforms; p++) {
109                 if (!(*p)->probe(*p, platform_ctx))
110                         continue;
111                 platform = *p;
112                 break;
113         }
114
115         config = talloc(platform_ctx, struct config);
116         config_set_defaults(config);
117
118         if (platform) {
119                 pb_log("Detected platform type: %s\n", platform->name);
120                 if (platform->load_config)
121                         platform->load_config(platform, config);
122         } else {
123                 pb_log("No platform type detected, some platform-specific "
124                                 "functionality will be disabled\n");
125         }
126
127         dump_config(config);
128
129         return 0;
130 }
131
132 const struct platform *platform_get(void)
133 {
134         return platform;
135 }
136
137 int config_set(struct config *newconfig)
138 {
139         int rc;
140
141         if (!platform || !platform->save_config)
142                 return -1;
143
144         if (newconfig == config)
145                 return 0;
146
147         pb_log("new configuration data received\n");
148         dump_config(newconfig);
149
150         rc = platform->save_config(platform, newconfig);
151
152         if (!rc)
153                 config = talloc_steal(platform_ctx, newconfig);
154         else
155                 pb_log("error saving new configuration; changes lost\n");
156
157         return rc;
158 }
159
160 /* A non-exported function to allow the test infrastructure to initialise
161  * (and change) the configuration variables */
162 struct parser_test;
163 struct config __attribute__((unused)) *test_config_init(
164                 struct parser_test *test);
165 struct config *test_config_init(struct parser_test *test)
166 {
167         config = talloc(test, struct config);
168         config_set_defaults(config);
169         return config;
170 }
171
172 const struct config *config_get(void)
173 {
174         return config;
175 }
176
177 void config_set_autoboot(bool autoboot_enabled)
178 {
179         config->autoboot_enabled = autoboot_enabled;
180
181         pb_log("set autoboot: %s\n",
182                         config->autoboot_enabled ? "enabled" : "disabled");
183 }
184
185 int platform_fini(void)
186 {
187         talloc_free(platform_ctx);
188         return 0;
189 }