lib/pb-config: Add config_copy
[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 || !process_exit_ok(process)) {
141                 fprintf(stderr, "nvram process returned "
142                                 "non-zero exit status\n");
143                 rc = -1;
144         } else {
145                 rc = parse_nvram_params(nv, process->stdout_buf,
146                                             process->stdout_len);
147         }
148
149         process_release(process);
150         return rc;
151 }
152
153 static int write_nvram(struct powerpc_nvram_storage *nv)
154 {
155         struct process *process;
156         struct param *param;
157         const char *argv[6];
158         int rc;
159
160         argv[0] = "nvram";
161         argv[1] = "--update-config";
162         argv[2] = NULL;
163         argv[3] = "--partition";
164         argv[4] = partition;
165         argv[5] = NULL;
166
167         process = process_create(nv);
168         process->path = "nvram";
169         process->argv = argv;
170
171         list_for_each_entry(&nv->params, param, list) {
172                 char *paramstr;
173
174                 if (!param->modified)
175                         continue;
176
177                 paramstr = talloc_asprintf(nv, "%s=%s",
178                                 param->name, param->value);
179                 argv[2] = paramstr;
180
181                 rc = process_run_sync(process);
182
183                 talloc_free(paramstr);
184
185                 if (rc || !process_exit_ok(process)) {
186                         rc = -1;
187                         pb_log("nvram update process returned "
188                                         "non-zero exit status\n");
189                         break;
190                 }
191         }
192
193         process_release(process);
194         return rc;
195 }
196
197 static const char *get_param(struct powerpc_nvram_storage *nv,
198                 const char *name)
199 {
200         struct param *param;
201
202         list_for_each_entry(&nv->params, param, list)
203                 if (!strcmp(param->name, name))
204                         return param->value;
205         return NULL;
206 }
207
208 static void set_param(struct powerpc_nvram_storage *nv, const char *name,
209                 const char *value)
210 {
211         struct param *param;
212
213         list_for_each_entry(&nv->params, param, list) {
214                 if (strcmp(param->name, name))
215                         continue;
216
217                 if (!strcmp(param->value, value))
218                         return;
219
220                 talloc_free(param->value);
221                 param->value = talloc_strdup(param, value);
222                 param->modified = true;
223                 return;
224         }
225
226
227         param = talloc(nv, struct param);
228         param->modified = true;
229         param->name = talloc_strdup(nv, name);
230         param->value = talloc_strdup(nv, value);
231         list_add(&nv->params, &param->list);
232 }
233
234 static int parse_hwaddr(struct interface_config *ifconf, char *str)
235 {
236         int i;
237
238         if (strlen(str) != strlen("00:00:00:00:00:00"))
239                 return -1;
240
241         for (i = 0; i < HWADDR_SIZE; i++) {
242                 char byte[3], *endp;
243                 unsigned long x;
244
245                 byte[0] = str[i * 3 + 0];
246                 byte[1] = str[i * 3 + 1];
247                 byte[2] = '\0';
248
249                 x = strtoul(byte, &endp, 16);
250                 if (endp != byte + 2)
251                         return -1;
252
253                 ifconf->hwaddr[i] = x & 0xff;
254         }
255
256         return 0;
257 }
258
259 static int parse_one_interface_config(struct config *config,
260                 char *confstr)
261 {
262         struct interface_config *ifconf;
263         char *tok, *saveptr;
264
265         ifconf = talloc_zero(config, struct interface_config);
266
267         if (!confstr || !strlen(confstr))
268                 goto out_err;
269
270         /* first token should be the mac address */
271         tok = strtok_r(confstr, ",", &saveptr);
272         if (!tok)
273                 goto out_err;
274
275         if (parse_hwaddr(ifconf, tok))
276                 goto out_err;
277
278         /* second token is the method */
279         tok = strtok_r(NULL, ",", &saveptr);
280         if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
281                 ifconf->ignore = true;
282
283         } else if (!strcmp(tok, "dhcp")) {
284                 ifconf->method = CONFIG_METHOD_DHCP;
285
286         } else if (!strcmp(tok, "static")) {
287                 ifconf->method = CONFIG_METHOD_STATIC;
288
289                 /* ip/mask, [optional] gateway */
290                 tok = strtok_r(NULL, ",", &saveptr);
291                 if (!tok)
292                         goto out_err;
293                 ifconf->static_config.address =
294                         talloc_strdup(ifconf, tok);
295
296                 tok = strtok_r(NULL, ",", &saveptr);
297                 if (tok) {
298                         ifconf->static_config.gateway =
299                                 talloc_strdup(ifconf, tok);
300                 }
301
302         } else {
303                 pb_log("Unknown network configuration method %s\n", tok);
304                 goto out_err;
305         }
306
307         config->network.interfaces = talloc_realloc(config,
308                         config->network.interfaces,
309                         struct interface_config *,
310                         ++config->network.n_interfaces);
311
312         config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
313
314         return 0;
315 out_err:
316         talloc_free(ifconf);
317         return -1;
318 }
319
320 static int parse_one_dns_config(struct config *config,
321                 char *confstr)
322 {
323         char *tok, *saveptr;
324
325         for (tok = strtok_r(confstr, ",", &saveptr); tok;
326                         tok = strtok_r(NULL, ",", &saveptr)) {
327
328                 char *server = talloc_strdup(config, tok);
329
330                 config->network.dns_servers = talloc_realloc(config,
331                                 config->network.dns_servers, const char *,
332                                 ++config->network.n_dns_servers);
333
334                 config->network.dns_servers[config->network.n_dns_servers - 1]
335                                 = server;
336         }
337
338         return 0;
339 }
340
341 static void populate_network_config(struct powerpc_nvram_storage *nv,
342                 struct config *config)
343 {
344         const char *cval;
345         char *val;
346         int i;
347
348         cval = get_param(nv, "petitboot,network");
349         if (!cval || !strlen(cval))
350                 return;
351
352         val = talloc_strdup(config, cval);
353
354         for (i = 0; ; i++) {
355                 char *tok, *saveptr;
356
357                 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
358                 if (!tok)
359                         break;
360
361                 if (!strncasecmp(tok, "dns,", strlen("dns,")))
362                         parse_one_dns_config(config, tok + strlen("dns,"));
363                 else
364                         parse_one_interface_config(config, tok);
365
366         }
367
368         talloc_free(val);
369 }
370
371 static void populate_config(struct powerpc_nvram_storage *nv,
372                 struct config *config)
373 {
374         const char *val;
375         char *end;
376         unsigned long timeout;
377
378         /* if the "auto-boot?' property is present and "false", disable auto
379          * boot */
380         val = get_param(nv, "auto-boot?");
381         config->autoboot_enabled = !val || strcmp(val, "false");
382
383         val = get_param(nv, "petitboot,timeout");
384         if (val) {
385                 timeout = strtoul(val, &end, 10);
386                 if (end != val) {
387                         if (timeout >= INT_MAX)
388                                 timeout = INT_MAX;
389                         config->autoboot_timeout_sec = (int)timeout;
390                 }
391         }
392
393         populate_network_config(nv, config);
394 }
395
396 static char *iface_config_str(void *ctx, struct interface_config *config)
397 {
398         char *str;
399
400         /* todo: HWADDR size is hardcoded as 6, but we may need to handle
401          * different hardware address formats */
402         str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
403                         config->hwaddr[0], config->hwaddr[1],
404                         config->hwaddr[2], config->hwaddr[3],
405                         config->hwaddr[4], config->hwaddr[5]);
406
407         if (config->ignore) {
408                 str = talloc_asprintf_append(str, "ignore");
409
410         } else if (config->method == CONFIG_METHOD_DHCP) {
411                 str = talloc_asprintf_append(str, "dhcp");
412
413         } else if (config->method == CONFIG_METHOD_STATIC) {
414                 str = talloc_asprintf_append(str, "static,%s%s%s",
415                                 config->static_config.address,
416                                 config->static_config.gateway ? "," : "",
417                                 config->static_config.gateway ?: "");
418         }
419         return str;
420 }
421
422 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
423 {
424         char *str;
425         int i;
426
427         str = talloc_strdup(ctx, "dns,");
428         for (i = 0; i < n; i++) {
429                 str = talloc_asprintf_append(str, "%s%s",
430                                 i == 0 ? "" : ",",
431                                 dns_servers[i]);
432         }
433
434         return str;
435 }
436
437 static void update_string_config(struct powerpc_nvram_storage *nv,
438                 const char *name, const char *value)
439 {
440         const char *cur;
441
442         cur = get_param(nv, name);
443
444         /* don't set an empty parameter if it doesn't already exist */
445         if (!cur && !strlen(value))
446                 return;
447
448         set_param(nv, name, value);
449 }
450
451 static void update_network_config(struct powerpc_nvram_storage *nv,
452         struct config *config)
453 {
454         unsigned int i;
455         char *val;
456
457         val = talloc_strdup(nv, "");
458
459         for (i = 0; i < config->network.n_interfaces; i++) {
460                 char *iface_str = iface_config_str(nv,
461                                         config->network.interfaces[i]);
462                 val = talloc_asprintf_append(val, "%s%s",
463                                 *val == '\0' ? "" : " ", iface_str);
464                 talloc_free(iface_str);
465         }
466
467         if (config->network.n_dns_servers) {
468                 char *dns_str = dns_config_str(nv, config->network.dns_servers,
469                                                 config->network.n_dns_servers);
470                 val = talloc_asprintf_append(val, "%s%s",
471                                 *val == '\0' ? "" : " ", dns_str);
472                 talloc_free(dns_str);
473         }
474
475         update_string_config(nv, "petitboot,network", val);
476
477         talloc_free(val);
478 }
479
480 static int update_config(struct powerpc_nvram_storage *nv,
481                 struct config *config, struct config *defaults)
482 {
483         char *tmp = NULL;
484         const char *val;
485
486         if (config->autoboot_enabled == defaults->autoboot_enabled)
487                 val = "";
488         else
489                 val = config->autoboot_enabled ? "true" : "false";
490         update_string_config(nv, "auto-boot?", val);
491
492         if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
493                 val = "";
494         else
495                 val = tmp = talloc_asprintf(nv, "%d",
496                                 config->autoboot_timeout_sec);
497
498         update_string_config(nv, "petitboot,timeout", val);
499         if (tmp)
500                 talloc_free(tmp);
501
502         update_network_config(nv, config);
503
504         return write_nvram(nv);
505 }
506
507 static int load(struct config_storage *st, struct config *config)
508 {
509         struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
510         int rc;
511
512         rc = parse_nvram(nv);
513         if (rc)
514                 return rc;
515
516         populate_config(nv, config);
517
518         return 0;
519 }
520
521 static int save(struct config_storage *st, struct config *config)
522 {
523         struct powerpc_nvram_storage *nv = to_powerpc_nvram_storage(st);
524         struct config *defaults;
525         int rc;
526
527         defaults = talloc_zero(nv, struct config);
528         config_set_defaults(defaults);
529
530         rc = update_config(nv, config, defaults);
531
532         talloc_free(defaults);
533         return rc;
534 }
535
536 struct config_storage *create_powerpc_nvram_storage(void *ctx)
537 {
538         struct powerpc_nvram_storage *nv;
539
540         nv = talloc(ctx, struct powerpc_nvram_storage);
541         nv->storage.load = load;
542         nv->storage.save = save;
543         list_init(&nv->params);
544
545         return &nv->storage;
546 }