]> git.ozlabs.org Git - petitboot/blob - lib/pb-config/pb-config.c
discover/grub2: Allow to separate the --id argument using a space char
[petitboot] / lib / pb-config / pb-config.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 "pb-config.h"
9
10 static struct interface_config *config_copy_interface(struct config *ctx,
11                 struct interface_config *src)
12 {
13         struct interface_config *dest = talloc_zero(ctx,
14                                                 struct interface_config);
15
16         memcpy(dest->hwaddr, src->hwaddr, sizeof(src->hwaddr));
17         dest->ignore = src->ignore;
18
19         if (dest->ignore)
20                 return dest;
21
22         dest->method = src->method;
23
24         switch (src->method) {
25         case CONFIG_METHOD_DHCP:
26                 break;
27         case CONFIG_METHOD_STATIC:
28                 dest->static_config.address =
29                         talloc_strdup(dest, src->static_config.address);
30                 dest->static_config.gateway =
31                         talloc_strdup(dest, src->static_config.gateway);
32                 dest->static_config.url =
33                         talloc_strdup(dest, src->static_config.url);
34                 break;
35         }
36         dest->override = src->override;
37
38         return dest;
39 }
40
41 struct config *config_copy(void *ctx, const struct config *src)
42 {
43         struct config *dest;
44         unsigned int i;
45
46         if (!src)
47                 return NULL;
48
49         dest = talloc_zero(ctx, struct config);
50         dest->autoboot_enabled = src->autoboot_enabled;
51         dest->autoboot_timeout_sec = src->autoboot_timeout_sec;
52         dest->safe_mode = src->safe_mode;
53
54         dest->network.n_interfaces = src->network.n_interfaces;
55         dest->network.interfaces = talloc_array(dest, struct interface_config *,
56                                         dest->network.n_interfaces);
57         dest->network.n_dns_servers = src->network.n_dns_servers;
58         dest->network.dns_servers = talloc_array(dest, const char *,
59                                         dest->network.n_dns_servers);
60
61         for (i = 0; i < src->network.n_interfaces; i++)
62                 dest->network.interfaces[i] = config_copy_interface(dest,
63                                 src->network.interfaces[i]);
64
65         for (i = 0; i < src->network.n_dns_servers; i++)
66                 dest->network.dns_servers[i] = talloc_strdup(dest,
67                                 src->network.dns_servers[i]);
68
69         dest->http_proxy = talloc_strdup(dest, src->http_proxy);
70         dest->https_proxy = talloc_strdup(dest, src->https_proxy);
71
72         dest->n_autoboot_opts = src->n_autoboot_opts;
73         dest->autoboot_opts = talloc_array(dest, struct autoboot_option,
74                                         dest->n_autoboot_opts);
75
76         for (i = 0; i < src->n_autoboot_opts; i++) {
77                 dest->autoboot_opts[i].boot_type =
78                         src->autoboot_opts[i].boot_type;
79                 if (src->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
80                         dest->autoboot_opts[i].type =
81                                 src->autoboot_opts[i].type;
82                 else
83                         dest->autoboot_opts[i].uuid =
84                                 talloc_strdup(dest, src->autoboot_opts[i].uuid);
85         }
86
87         dest->ipmi_bootdev = src->ipmi_bootdev;
88         dest->ipmi_bootdev_persistent = src->ipmi_bootdev_persistent;
89         dest->ipmi_bootdev_mailbox = src->ipmi_bootdev_mailbox;
90
91         dest->allow_writes = src->allow_writes;
92
93         dest->n_consoles = src->n_consoles;
94         if (src->consoles) {
95                 dest->consoles = talloc_array(dest, char *, src->n_consoles);
96                 for (i = 0; i < src->n_consoles && src->n_consoles; i++)
97                         if (src->consoles[i])
98                                 dest->consoles[i] = talloc_strdup(
99                                                 dest->consoles,
100                                                 src->consoles[i]);
101         }
102
103         if (src->boot_console)
104                 dest->boot_console = talloc_strdup(dest, src->boot_console);
105         dest->manual_console = src->manual_console;
106
107         if (src->lang && strlen(src->lang))
108                 dest->lang = talloc_strdup(dest, src->lang);
109         else
110                 dest->lang = NULL;
111
112         return dest;
113 }