]> git.ozlabs.org Git - petitboot/blob - discover/platform.c
be60f00fb8205dd4407f0fb14ac84fab5d69b837
[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         if (config->safe_mode)
47                 pb_log(" safe mode: active\n");
48
49         for (i = 0; i < config->network.n_interfaces; i++) {
50                 struct interface_config *ifconf =
51                         config->network.interfaces[i];
52
53                 pb_log("  interface %02x:%02x:%02x:%02x:%02x:%02x\n",
54                                 ifconf->hwaddr[0], ifconf->hwaddr[1],
55                                 ifconf->hwaddr[2], ifconf->hwaddr[3],
56                                 ifconf->hwaddr[4], ifconf->hwaddr[5]);
57
58                 if (ifconf->ignore) {
59                         pb_log("   ignore\n");
60                         continue;
61                 }
62
63                 if (ifconf->method == CONFIG_METHOD_DHCP) {
64                         pb_log("   dhcp\n");
65
66                 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
67                         pb_log("   static:\n");
68                         pb_log("    ip:  %s\n", ifconf->static_config.address);
69                         pb_log("    gw:  %s\n", ifconf->static_config.gateway);
70
71                 }
72         }
73         for (i = 0; i < config->network.n_dns_servers; i++)
74                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
75
76         if (config->n_boot_priorities)
77                 pb_log(" boot priority order:\n");
78
79         for (i = 0; i < config->n_boot_priorities; i++) {
80                 struct boot_priority *prio = &config->boot_priorities[i];
81                 pb_log(" %10s: %d\n", device_type_name(prio->type),
82                                         prio->priority);
83         }
84 }
85
86 void config_set_defaults(struct config *config)
87 {
88         config->autoboot_enabled = true;
89         config->autoboot_timeout_sec = 10;
90         config->network.interfaces = NULL;
91         config->network.n_interfaces = 0;
92         config->network.dns_servers = NULL;
93         config->network.n_dns_servers = 0;
94         config->boot_device = NULL;
95
96         config->n_boot_priorities = 2;
97         config->boot_priorities = talloc_array(config, struct boot_priority,
98                                                 config->n_boot_priorities);
99         config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
100         config->boot_priorities[0].priority = 2;
101         config->boot_priorities[1].type = DEVICE_TYPE_DISK;
102         config->boot_priorities[1].priority = 1;
103 }
104
105 int platform_init(void *ctx)
106 {
107         extern struct platform *__start_platforms,  *__stop_platforms;
108         struct platform **p;
109
110         platform_ctx = talloc_new(ctx);
111
112         for (p = &__start_platforms; p < &__stop_platforms; p++) {
113                 if (!(*p)->probe(*p, platform_ctx))
114                         continue;
115                 platform = *p;
116                 break;
117         }
118
119         config = talloc(platform_ctx, struct config);
120         config_set_defaults(config);
121
122         if (platform) {
123                 pb_log("Detected platform type: %s\n", platform->name);
124                 if (platform->load_config)
125                         platform->load_config(platform, config);
126         } else {
127                 pb_log("No platform type detected, some platform-specific "
128                                 "functionality will be disabled\n");
129         }
130
131         dump_config(config);
132
133         return 0;
134 }
135
136 const struct platform *platform_get(void)
137 {
138         return platform;
139 }
140
141 int config_set(struct config *newconfig)
142 {
143         int rc;
144
145         if (!platform || !platform->save_config)
146                 return -1;
147
148         if (newconfig == config)
149                 return 0;
150
151         pb_log("new configuration data received\n");
152         dump_config(newconfig);
153
154         rc = platform->save_config(platform, newconfig);
155
156         if (!rc)
157                 config = talloc_steal(platform_ctx, newconfig);
158         else
159                 pb_log("error saving new configuration; changes lost\n");
160
161         return rc;
162 }
163
164 /* A non-exported function to allow the test infrastructure to initialise
165  * (and change) the configuration variables */
166 struct parser_test;
167 struct config __attribute__((unused)) *test_config_init(
168                 struct parser_test *test);
169 struct config *test_config_init(struct parser_test *test)
170 {
171         config = talloc(test, struct config);
172         config_set_defaults(config);
173         return config;
174 }
175
176 const struct config *config_get(void)
177 {
178         return config;
179 }
180
181 void config_set_autoboot(bool autoboot_enabled)
182 {
183         config->autoboot_enabled = autoboot_enabled;
184
185         pb_log("set autoboot: %s\n",
186                         config->autoboot_enabled ? "enabled" : "disabled");
187 }
188
189 int platform_fini(void)
190 {
191         talloc_free(platform_ctx);
192         return 0;
193 }