discover: Implement device priorities
[petitboot] / lib / pb-config / pb-config.c
1
2 #include <log/log.h>
3 #include <types/types.h>
4 #include <talloc/talloc.h>
5
6 #include "pb-config.h"
7
8 #include "storage.h"
9
10 static struct config            *config;
11 static struct config_storage    *storage;
12
13
14 static void config_set_defaults(struct config *config)
15 {
16         config->autoboot_enabled = true;
17         config->autoboot_timeout_sec = 10;
18         config->network.interfaces = NULL;
19         config->network.n_interfaces = 0;
20         config->network.dns_servers = NULL;
21         config->network.n_dns_servers = 0;
22
23         config->n_boot_priorities = 2;
24         config->boot_priorities = talloc_array(config, struct boot_priority,
25                                                 config->n_boot_priorities);
26         config->boot_priorities[0].type = DEVICE_TYPE_NETWORK;
27         config->boot_priorities[1].type = DEVICE_TYPE_DISK;
28
29 }
30
31 static void dump_config(struct config *config)
32 {
33         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
74 int config_init(void *ctx)
75 {
76         config = talloc(ctx, struct config);
77         config_set_defaults(config);
78
79         storage = create_powerpc_nvram_storage(config);
80
81         storage->load(storage, config);
82
83         dump_config(config);
84
85         return 0;
86 }
87
88 /* A non-exported function to allow the test infrastructure to initialise
89  * (and change) the configuration variables */
90 struct parser_test;
91 struct config __attribute__((unused)) *test_config_init(
92                 struct parser_test *test);
93 struct config *test_config_init(struct parser_test *test)
94 {
95         config = talloc(test, struct config);
96         config_set_defaults(config);
97         return config;
98 }
99
100 const struct config *config_get(void)
101 {
102         return config;
103 }
104
105 void config_set_autoboot(bool autoboot_enabled)
106 {
107         config->autoboot_enabled = autoboot_enabled;
108
109         pb_log("set autoboot: %s\n",
110                         config->autoboot_enabled ? "enabled" : "disabled");
111 }
112
113 int config_fini(void)
114 {
115         talloc_free(config);
116         return 0;
117 }