66e776edd530548f9815c91942520362f5205e59
[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 int config_set(struct config *newconfig)
106 {
107         int rc;
108
109         if (!platform || !platform->save_config)
110                 return -1;
111
112         if (newconfig == config)
113                 return 0;
114
115         pb_log("new configuration data received\n");
116         dump_config(newconfig);
117
118         rc = platform->save_config(platform, newconfig);
119
120         if (!rc)
121                 config = talloc_steal(platform_ctx, newconfig);
122         else
123                 pb_log("error saving new configuration; changes lost\n");
124
125         return rc;
126 }
127
128 /* A non-exported function to allow the test infrastructure to initialise
129  * (and change) the configuration variables */
130 struct parser_test;
131 struct config __attribute__((unused)) *test_config_init(
132                 struct parser_test *test);
133 struct config *test_config_init(struct parser_test *test)
134 {
135         config = talloc(test, struct config);
136         config_set_defaults(config);
137         return config;
138 }
139
140 const struct config *config_get(void)
141 {
142         return config;
143 }
144
145 void config_set_autoboot(bool autoboot_enabled)
146 {
147         config->autoboot_enabled = autoboot_enabled;
148
149         pb_log("set autoboot: %s\n",
150                         config->autoboot_enabled ? "enabled" : "disabled");
151 }
152
153 int platform_fini(void)
154 {
155         talloc_free(platform_ctx);
156         return 0;
157 }