config: Split interface configuration from network configuration
[petitboot] / lib / pb-config / storage-powerpc-nvram.c
1
2 #include <string.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/wait.h>
6
7 #include <talloc/talloc.h>
8 #include <list/list.h>
9 #include <log/log.h>
10
11 #include "pb-config.h"
12 #include "storage.h"
13
14 static const char *partition = "common";
15
16 struct param {
17         char                    *name;
18         char                    *value;
19         bool                    modified;
20         struct list_item        list;
21 };
22
23 struct powerpc_nvram_storage {
24         struct config_storage   storage;
25         struct list             params;
26 };
27
28 static const char *known_params[] = {
29         "auto-boot?",
30         "petitboot,network",
31         NULL,
32 };
33
34 #define to_powerpc_nvram_storage(s) \
35         container_of(s, struct powerpc_nvram_storage, storage)
36
37 /* a partition max a max size of 64k * 16bytes = 1M */
38 static const int max_partition_size = 64 * 1024 * 16;
39
40 static bool param_is_known(const char *param, unsigned int len)
41 {
42         const char *known_param;
43         unsigned int i;
44
45         for (i = 0; known_params[i]; i++) {
46                 known_param = known_params[i];
47                 if (len == strlen(known_param) &&
48                                 !strncmp(param, known_param, len))
49                         return true;
50         }
51
52         return false;
53 }
54
55 static int parse_nvram_params(struct powerpc_nvram_storage *nv,
56                 char *buf, int len)
57 {
58         char *pos, *name, *value;
59         unsigned int paramlen;
60         int i, count;
61
62         /* discard 2 header lines:
63          * "common" partiton"
64          * ------------------
65          */
66         pos = buf;
67         count = 0;
68
69         for (i = 0; i < len; i++) {
70                 if (pos[i] == '\n')
71                         count++;
72                 if (count == 2)
73                         break;
74         }
75
76         if (i == len) {
77                 fprintf(stderr, "failure parsing nvram output\n");
78                 return -1;
79         }
80
81         for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
82                 unsigned int namelen;
83                 struct param *param;
84                 char *newline;
85
86                 newline = strchr(pos, '\n');
87                 if (!newline)
88                         break;
89
90                 *newline = '\0';
91
92                 paramlen = strlen(pos);
93
94                 name = pos;
95                 value = strchr(pos, '=');
96                 if (!value)
97                         continue;
98
99                 namelen = value - name;
100                 if (namelen == 0)
101                         continue;
102
103                 if (!param_is_known(name, namelen))
104                         continue;
105
106                 value++;
107
108                 param = talloc(nv, struct param);
109                 param->modified = false;
110                 param->name = talloc_strndup(nv, name, namelen);
111                 param->value = talloc_strdup(nv, value);
112                 list_add(&nv->params, &param->list);
113         }
114
115         return 0;
116 }
117
118 static int parse_nvram(struct powerpc_nvram_storage *nv)
119 {
120         int rc, len, buf_len;
121         int pipefds[2], status;
122         char *buf;
123         pid_t pid;
124
125         rc = pipe(pipefds);
126         if (rc) {
127                 perror("pipe");
128                 return -1;
129         }
130
131         pid = fork();
132
133         if (pid < 0) {
134                 perror("fork");
135                 return -1;
136         }
137
138         if (pid == 0) {
139                 close(STDIN_FILENO);
140                 close(pipefds[0]);
141                 dup2(pipefds[1], STDOUT_FILENO);
142                 execlp("nvram", "nvram", "--print-config",
143                                 "--partition", partition, NULL);
144                 exit(EXIT_FAILURE);
145         }
146
147         close(pipefds[1]);
148
149         len = 0;
150         buf_len = max_partition_size;
151         buf = talloc_array(nv, char, buf_len);
152
153         for (;;) {
154                 rc = read(pipefds[0], buf + len, buf_len - len);
155
156                 if (rc < 0) {
157                         perror("read");
158                         break;
159                 }
160
161                 if (rc == 0)
162                         break;
163
164                 len += rc;
165         }
166
167         waitpid(pid, &status, 0);
168         if (!WIFEXITED(status) || WEXITSTATUS(status)) {
169                 fprintf(stderr, "nvram process returned "
170                                 "non-zero exit status\n");
171                 return -1;
172         }
173
174         if (rc < 0)
175                 return rc;
176
177         return parse_nvram_params(nv, buf, len);
178 }
179
180 static const char *get_param(struct powerpc_nvram_storage *nv,
181                 const char *name)
182 {
183         struct param *param;
184
185         list_for_each_entry(&nv->params, param, list)
186                 if (!strcmp(param->name, name))
187                         return param->value;
188         return NULL;
189 }
190
191 static int parse_hwaddr(struct interface_config *ifconf, char *str)
192 {
193         int i;
194
195         if (strlen(str) != strlen("00:00:00:00:00:00"))
196                 return -1;
197
198         for (i = 0; i < HWADDR_SIZE; i++) {
199                 char byte[3], *endp;
200                 unsigned long x;
201
202                 byte[0] = str[i * 3 + 0];
203                 byte[1] = str[i * 3 + 1];
204                 byte[2] = '\0';
205
206                 x = strtoul(byte, &endp, 16);
207                 if (endp != byte + 2)
208                         return -1;
209
210                 ifconf->hwaddr[i] = x & 0xff;
211         }
212
213         return 0;
214 }
215
216 static int parse_one_interface_config(struct config *config,
217                 char *confstr)
218 {
219         struct interface_config *ifconf;
220         char *tok, *saveptr;
221
222         ifconf = talloc(config, struct interface_config);
223
224         if (!confstr || !strlen(confstr))
225                 goto out_err;
226
227         /* first token should be the mac address */
228         tok = strtok_r(confstr, ",", &saveptr);
229         if (!tok)
230                 goto out_err;
231
232         if (parse_hwaddr(ifconf, tok))
233                 goto out_err;
234
235         /* second token is the method */
236         tok = strtok_r(NULL, ",", &saveptr);
237         if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
238                 ifconf->ignore = true;
239
240         } else if (!strcmp(tok, "dhcp")) {
241                 ifconf->method = CONFIG_METHOD_DHCP;
242
243         } else if (!strcmp(tok, "static")) {
244                 ifconf->method = CONFIG_METHOD_STATIC;
245
246                 /* ip/mask, [optional] gateway, [optional] dns */
247                 tok = strtok_r(NULL, ",", &saveptr);
248                 if (!tok)
249                         goto out_err;
250                 ifconf->static_config.address =
251                         talloc_strdup(ifconf, tok);
252
253                 tok = strtok_r(NULL, ",", &saveptr);
254                 if (tok) {
255                         ifconf->static_config.gateway =
256                                 talloc_strdup(ifconf, tok);
257                         tok = strtok_r(NULL, ",", &saveptr);
258                 }
259
260                 if (tok) {
261                         ifconf->static_config.dns =
262                                 talloc_strdup(config, tok);
263                 }
264         } else {
265                 pb_log("Unknown network configuration method %s\n", tok);
266                 goto out_err;
267         }
268
269         config->network.interfaces = talloc_realloc(config,
270                         config->network.interfaces,
271                         struct interface_config *,
272                         ++config->network.n_interfaces);
273
274         config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
275
276         return 0;
277 out_err:
278         talloc_free(ifconf);
279         return -1;
280 }
281
282 static void populate_network_config(struct powerpc_nvram_storage *nv,
283                 struct config *config)
284 {
285         const char *cval;
286         char *val;
287         int i;
288
289         cval = get_param(nv, "petitboot,network");
290         if (!cval || !strlen(cval))
291                 return;
292
293         val = talloc_strdup(config, cval);
294
295         for (i = 0; ; i++) {
296                 char *tok, *saveptr;
297
298                 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
299                 if (!tok)
300                         break;
301
302                 parse_one_interface_config(config, tok);
303
304         }
305
306         talloc_free(val);
307 }
308
309 static void populate_config(struct powerpc_nvram_storage *nv,
310                 struct config *config)
311 {
312         const char *val;
313
314         /* if the "auto-boot?' property is present and "false", disable auto
315          * boot */
316         val = get_param(nv, "auto-boot?");
317         config->autoboot_enabled = !val || strcmp(val, "false");
318
319         populate_network_config(nv, config);
320 }
321
322 static int load(struct config_storage *st, struct config *config)
323 {
324         struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
325         int rc;
326
327         rc = parse_nvram(nv);
328         if (rc)
329                 return rc;
330
331         populate_config(nv, config);
332
333         return 0;
334 }
335
336 struct config_storage *create_powerpc_nvram_storage(void *ctx)
337 {
338         struct powerpc_nvram_storage *nv;
339
340         nv = talloc(ctx, struct powerpc_nvram_storage);
341         nv->storage.load = load;
342         list_init(&nv->params);
343
344         return &nv->storage;
345 }