]> git.ozlabs.org Git - petitboot/blob - discover/platform.c
ui/ncurses: Add support for help screens
[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[0].priority = 2;
71         config->boot_priorities[1].type = DEVICE_TYPE_DISK;
72         config->boot_priorities[1].priority = 1;
73 }
74
75 int platform_init(void *ctx)
76 {
77         extern struct platform *__start_platforms,  *__stop_platforms;
78         struct platform **p;
79
80         platform_ctx = talloc_new(ctx);
81
82         for (p = &__start_platforms; p < &__stop_platforms; p++) {
83                 if (!(*p)->probe(*p, platform_ctx))
84                         continue;
85                 platform = *p;
86                 break;
87         }
88
89         config = talloc(platform_ctx, struct config);
90         config_set_defaults(config);
91
92         if (platform) {
93                 pb_log("Detected platform type: %s\n", platform->name);
94                 if (platform->load_config)
95                         platform->load_config(platform, config);
96         } else {
97                 pb_log("No platform type detected, some platform-specific "
98                                 "functionality will be disabled\n");
99         }
100
101         dump_config(config);
102
103         return 0;
104 }
105
106 const struct platform *platform_get(void)
107 {
108         return platform;
109 }
110
111 int config_set(struct config *newconfig)
112 {
113         int rc;
114
115         if (!platform || !platform->save_config)
116                 return -1;
117
118         if (newconfig == config)
119                 return 0;
120
121         pb_log("new configuration data received\n");
122         dump_config(newconfig);
123
124         rc = platform->save_config(platform, newconfig);
125
126         if (!rc)
127                 config = talloc_steal(platform_ctx, newconfig);
128         else
129                 pb_log("error saving new configuration; changes lost\n");
130
131         return rc;
132 }
133
134 /* A non-exported function to allow the test infrastructure to initialise
135  * (and change) the configuration variables */
136 struct parser_test;
137 struct config __attribute__((unused)) *test_config_init(
138                 struct parser_test *test);
139 struct config *test_config_init(struct parser_test *test)
140 {
141         config = talloc(test, struct config);
142         config_set_defaults(config);
143         return config;
144 }
145
146 const struct config *config_get(void)
147 {
148         return config;
149 }
150
151 void config_set_autoboot(bool autoboot_enabled)
152 {
153         config->autoboot_enabled = autoboot_enabled;
154
155         pb_log("set autoboot: %s\n",
156                         config->autoboot_enabled ? "enabled" : "disabled");
157 }
158
159 int platform_fini(void)
160 {
161         talloc_free(platform_ctx);
162         return 0;
163 }