lib/process: Add add_stderr flag to process module
[petitboot] / lib / pb-config / storage-powerpc-nvram.c
1
2 #include <string.h>
3 #include <stdlib.h>
4 #include <limits.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7
8 #include <talloc/talloc.h>
9 #include <list/list.h>
10 #include <log/log.h>
11 #include <process/process.h>
12
13 #include "pb-config.h"
14 #include "storage.h"
15
16 static const char *partition = "common";
17
18 struct param {
19         char                    *name;
20         char                    *value;
21         bool                    modified;
22         struct list_item        list;
23 };
24
25 struct powerpc_nvram_storage {
26         struct config_storage   storage;
27         struct list             params;
28 };
29
30 static const char *known_params[] = {
31         "auto-boot?",
32         "petitboot,network",
33         "petitboot,timeout",
34         NULL,
35 };
36
37 #define to_powerpc_nvram_storage(s) \
38         container_of(s, struct powerpc_nvram_storage, storage)
39
40 /* a partition max a max size of 64k * 16bytes = 1M */
41 static const int max_partition_size = 64 * 1024 * 16;
42
43 static bool param_is_known(const char *param, unsigned int len)
44 {
45         const char *known_param;
46         unsigned int i;
47
48         for (i = 0; known_params[i]; i++) {
49                 known_param = known_params[i];
50                 if (len == strlen(known_param) &&
51                                 !strncmp(param, known_param, len))
52                         return true;
53         }
54
55         return false;
56 }
57
58 static int parse_nvram_params(struct powerpc_nvram_storage *nv,
59                 char *buf, int len)
60 {
61         char *pos, *name, *value;
62         unsigned int paramlen;
63         int i, count;
64
65         /* discard 2 header lines:
66          * "common" partiton"
67          * ------------------
68          */
69         pos = buf;
70         count = 0;
71
72         for (i = 0; i < len; i++) {
73                 if (pos[i] == '\n')
74                         count++;
75                 if (count == 2)
76                         break;
77         }
78
79         if (i == len) {
80                 fprintf(stderr, "failure parsing nvram output\n");
81                 return -1;
82         }
83
84         for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
85                 unsigned int namelen;
86                 struct param *param;
87                 char *newline;
88
89                 newline = strchr(pos, '\n');
90                 if (!newline)
91                         break;
92
93                 *newline = '\0';
94
95                 paramlen = strlen(pos);
96
97                 name = pos;
98                 value = strchr(pos, '=');
99                 if (!value)
100                         continue;
101
102                 namelen = value - name;
103                 if (namelen == 0)
104                         continue;
105
106                 if (!param_is_known(name, namelen))
107                         continue;
108
109                 value++;
110
111                 param = talloc(nv, struct param);
112                 param->modified = false;
113                 param->name = talloc_strndup(nv, name, namelen);
114                 param->value = talloc_strdup(nv, value);
115                 list_add(&nv->params, &param->list);
116         }
117
118         return 0;
119 }
120
121 static int parse_nvram(struct powerpc_nvram_storage *nv)
122 {
123         struct process *process;
124         const char *argv[5];
125         int rc;
126
127         argv[0] = "nvram";
128         argv[1] = "--print-config";
129         argv[2] = "--partition";
130         argv[3] = partition;
131         argv[4] = NULL;
132
133         process = process_create(nv);
134         process->path = "nvram";
135         process->argv = argv;
136         process->keep_stdout = true;
137
138         rc = process_run_sync(process);
139
140         if (rc || !WIFEXITED(process->exit_status)
141                         || WEXITSTATUS(process->exit_status)) {
142                 fprintf(stderr, "nvram process returned "
143                                 "non-zero exit status\n");
144                 rc = -1;
145         } else {
146                 rc = parse_nvram_params(nv, process->stdout_buf,
147                                             process->stdout_len);
148         }
149
150         process_release(process);
151         return rc;
152 }
153
154 static int write_nvram(struct powerpc_nvram_storage *nv)
155 {
156         struct process *process;
157         struct param *param;
158         const char *argv[6];
159         int rc;
160
161         argv[0] = "nvram";
162         argv[1] = "--update-config";
163         argv[2] = NULL;
164         argv[3] = "--partition";
165         argv[4] = partition;
166         argv[5] = NULL;
167
168         process = process_create(nv);
169         process->path = "nvram";
170         process->argv = argv;
171
172         list_for_each_entry(&nv->params, param, list) {
173                 char *paramstr;
174
175                 if (!param->modified)
176                         continue;
177
178                 paramstr = talloc_asprintf(nv, "%s=%s",
179                                 param->name, param->value);
180                 argv[2] = paramstr;
181
182                 rc = process_run_sync(process);
183
184                 talloc_free(paramstr);
185
186                 if (rc || !WIFEXITED(process->exit_status)
187                                 || WEXITSTATUS(process->exit_status)) {
188                         rc = -1;
189                         pb_log("nvram update process returned "
190                                         "non-zero exit status\n");
191                         break;
192                 }
193         }
194
195         process_release(process);
196         return rc;
197 }
198
199 static const char *get_param(struct powerpc_nvram_storage *nv,
200                 const char *name)
201 {
202         struct param *param;
203
204         list_for_each_entry(&nv->params, param, list)
205                 if (!strcmp(param->name, name))
206                         return param->value;
207         return NULL;
208 }
209
210 static void set_param(struct powerpc_nvram_storage *nv, const char *name,
211                 const char *value)
212 {
213         struct param *param;
214
215         list_for_each_entry(&nv->params, param, list) {
216                 if (strcmp(param->name, name))
217                         continue;
218
219                 if (!strcmp(param->value, value))
220                         return;
221
222                 talloc_free(param->value);
223                 param->value = talloc_strdup(param, value);
224                 param->modified = true;
225                 return;
226         }
227
228
229         param = talloc(nv, struct param);
230         param->modified = true;
231         param->name = talloc_strdup(nv, name);
232         param->value = talloc_strdup(nv, value);
233         list_add(&nv->params, &param->list);
234 }
235
236 static int parse_hwaddr(struct interface_config *ifconf, char *str)
237 {
238         int i;
239
240         if (strlen(str) != strlen("00:00:00:00:00:00"))
241                 return -1;
242
243         for (i = 0; i < HWADDR_SIZE; i++) {
244                 char byte[3], *endp;
245                 unsigned long x;
246
247                 byte[0] = str[i * 3 + 0];
248                 byte[1] = str[i * 3 + 1];
249                 byte[2] = '\0';
250
251                 x = strtoul(byte, &endp, 16);
252                 if (endp != byte + 2)
253                         return -1;
254
255                 ifconf->hwaddr[i] = x & 0xff;
256         }
257
258         return 0;
259 }
260
261 static int parse_one_interface_config(struct config *config,
262                 char *confstr)
263 {
264         struct interface_config *ifconf;
265         char *tok, *saveptr;
266
267         ifconf = talloc_zero(config, struct interface_config);
268
269         if (!confstr || !strlen(confstr))
270                 goto out_err;
271
272         /* first token should be the mac address */
273         tok = strtok_r(confstr, ",", &saveptr);
274         if (!tok)
275                 goto out_err;
276
277         if (parse_hwaddr(ifconf, tok))
278                 goto out_err;
279
280         /* second token is the method */
281         tok = strtok_r(NULL, ",", &saveptr);
282         if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
283                 ifconf->ignore = true;
284
285         } else if (!strcmp(tok, "dhcp")) {
286                 ifconf->method = CONFIG_METHOD_DHCP;
287
288         } else if (!strcmp(tok, "static")) {
289                 ifconf->method = CONFIG_METHOD_STATIC;
290
291                 /* ip/mask, [optional] gateway */
292                 tok = strtok_r(NULL, ",", &saveptr);
293                 if (!tok)
294                         goto out_err;
295                 ifconf->static_config.address =
296                         talloc_strdup(ifconf, tok);
297
298                 tok = strtok_r(NULL, ",", &saveptr);
299                 if (tok) {
300                         ifconf->static_config.gateway =
301                                 talloc_strdup(ifconf, tok);
302                 }
303
304         } else {
305                 pb_log("Unknown network configuration method %s\n", tok);
306                 goto out_err;
307         }
308
309         config->network.interfaces = talloc_realloc(config,
310                         config->network.interfaces,
311                         struct interface_config *,
312                         ++config->network.n_interfaces);
313
314         config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
315
316         return 0;
317 out_err:
318         talloc_free(ifconf);
319         return -1;
320 }
321
322 static int parse_one_dns_config(struct config *config,
323                 char *confstr)
324 {
325         char *tok, *saveptr;
326
327         for (tok = strtok_r(confstr, ",", &saveptr); tok;
328                         tok = strtok_r(NULL, ",", &saveptr)) {
329
330                 char *server = talloc_strdup(config, tok);
331
332                 config->network.dns_servers = talloc_realloc(config,
333                                 config->network.dns_servers, const char *,
334                                 ++config->network.n_dns_servers);
335
336                 config->network.dns_servers[config->network.n_dns_servers - 1]
337                                 = server;
338         }
339
340         return 0;
341 }
342
343 static void populate_network_config(struct powerpc_nvram_storage *nv,
344                 struct config *config)
345 {
346         const char *cval;
347         char *val;
348         int i;
349
350         cval = get_param(nv, "petitboot,network");
351         if (!cval || !strlen(cval))
352                 return;
353
354         val = talloc_strdup(config, cval);
355
356         for (i = 0; ; i++) {
357                 char *tok, *saveptr;
358
359                 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
360                 if (!tok)
361                         break;
362
363                 if (!strncasecmp(tok, "dns,", strlen("dns,")))
364                         parse_one_dns_config(config, tok + strlen("dns,"));
365                 else
366                         parse_one_interface_config(config, tok);
367
368         }
369
370         talloc_free(val);
371 }
372
373 static void populate_config(struct powerpc_nvram_storage *nv,
374                 struct config *config)
375 {
376         const char *val;
377         char *end;
378         unsigned long timeout;
379
380         /* if the "auto-boot?' property is present and "false", disable auto
381          * boot */
382         val = get_param(nv, "auto-boot?");
383         config->autoboot_enabled = !val || strcmp(val, "false");
384
385         val = get_param(nv, "petitboot,timeout");
386         if (val) {
387                 timeout = strtoul(val, &end, 10);
388                 if (end != val) {
389                         if (timeout >= INT_MAX)
390                                 timeout = INT_MAX;
391                         config->autoboot_timeout_sec = (int)timeout;
392                 }
393         }
394
395         populate_network_config(nv, config);
396 }
397
398 static char *iface_config_str(void *ctx, struct interface_config *config)
399 {
400         char *str;
401
402         /* todo: HWADDR size is hardcoded as 6, but we may need to handle
403          * different hardware address formats */
404         str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
405                         config->hwaddr[0], config->hwaddr[1],
406                         config->hwaddr[2], config->hwaddr[3],
407                         config->hwaddr[4], config->hwaddr[5]);
408
409         if (config->ignore) {
410                 str = talloc_asprintf_append(str, "ignore");
411
412         } else if (config->method == CONFIG_METHOD_DHCP) {
413                 str = talloc_asprintf_append(str, "dhcp");
414
415         } else if (config->method == CONFIG_METHOD_STATIC) {
416                 str = talloc_asprintf_append(str, "static,%s%s%s",
417                                 config->static_config.address,
418                                 config->static_config.gateway ? "," : "",
419                                 config->static_config.gateway ?: "");
420         }
421         return str;
422 }
423
424 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
425 {
426         char *str;
427         int i;
428
429         str = talloc_strdup(ctx, "dns,");
430         for (i = 0; i < n; i++) {
431                 str = talloc_asprintf_append(str, "%s%s",
432                                 i == 0 ? "" : ",",
433                                 dns_servers[i]);
434         }
435
436         return str;
437 }
438
439 static void update_network_config(struct powerpc_nvram_storage *nv,
440         struct config *config)
441 {
442         unsigned int i;
443         char *val;
444
445         val = talloc_strdup(nv, "");
446
447         for (i = 0; i < config->network.n_interfaces; i++) {
448                 char *iface_str = iface_config_str(nv,
449                                         config->network.interfaces[i]);
450                 val = talloc_asprintf_append(val, "%s%s",
451                                 *val == '\0' ? "" : " ", iface_str);
452                 talloc_free(iface_str);
453         }
454
455         if (config->network.n_dns_servers) {
456                 char *dns_str = dns_config_str(nv, config->network.dns_servers,
457                                                 config->network.n_dns_servers);
458                 val = talloc_asprintf_append(val, "%s%s",
459                                 *val == '\0' ? "" : " ", dns_str);
460                 talloc_free(dns_str);
461         }
462
463         set_param(nv, "petitboot,network", val);
464
465         talloc_free(val);
466 }
467
468 static int update_config(struct powerpc_nvram_storage *nv,
469                 struct config *config, struct config *defaults)
470 {
471         char *val;
472
473         if (config->autoboot_enabled != defaults->autoboot_enabled) {
474                 val = config->autoboot_enabled ? "true" : "false";
475                 set_param(nv, "auto-boot?", val);
476         }
477
478         if (config->autoboot_timeout_sec != defaults->autoboot_timeout_sec) {
479                 val = talloc_asprintf(nv, "%d", config->autoboot_timeout_sec);
480                 set_param(nv, "petitboot,timeout", val);
481                 talloc_free(val);
482         }
483
484         update_network_config(nv, config);
485
486         return write_nvram(nv);
487 }
488
489 static int load(struct config_storage *st, struct config *config)
490 {
491         struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
492         int rc;
493
494         rc = parse_nvram(nv);
495         if (rc)
496                 return rc;
497
498         populate_config(nv, config);
499
500         return 0;
501 }
502
503 static int save(struct config_storage *st, struct config *config)
504 {
505         struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
506         struct config *defaults;
507         int rc;
508
509         defaults = talloc_zero(nv, struct config);
510         config_set_defaults(defaults);
511
512         rc = update_config(nv, config, defaults);
513
514         talloc_free(defaults);
515         return rc;
516 }
517
518 struct config_storage *create_powerpc_nvram_storage(void *ctx)
519 {
520         struct powerpc_nvram_storage *nv;
521
522         nv = talloc(ctx, struct powerpc_nvram_storage);
523         nv->storage.load = load;
524         nv->storage.save = save;
525         list_init(&nv->params);
526
527         return &nv->storage;
528 }