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