]> git.ozlabs.org Git - petitboot/blob - discover/platform-powerpc.c
discover: Add debug output to sysparams parsing
[petitboot] / discover / platform-powerpc.c
1
2 #include <assert.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <sys/fcntl.h>
9 #include <sys/stat.h>
10
11 #include <talloc/talloc.h>
12 #include <list/list.h>
13 #include <log/log.h>
14 #include <process/process.h>
15
16 #include "platform.h"
17
18 static const char *partition = "common";
19 static const char *sysparams_dir = "/sys/firmware/opal/sysparams";
20
21 struct param {
22         char                    *name;
23         char                    *value;
24         bool                    modified;
25         struct list_item        list;
26 };
27
28 struct platform_powerpc {
29         struct list             params;
30 };
31
32 static const char *known_params[] = {
33         "auto-boot?",
34         "petitboot,network",
35         "petitboot,timeout",
36         NULL,
37 };
38
39 #define to_platform_powerpc(p) \
40         (struct platform_powerpc *)(p->platform_data)
41
42 /* a partition max a max size of 64k * 16bytes = 1M */
43 static const int max_partition_size = 64 * 1024 * 16;
44
45 static bool param_is_known(const char *param, unsigned int len)
46 {
47         const char *known_param;
48         unsigned int i;
49
50         for (i = 0; known_params[i]; i++) {
51                 known_param = known_params[i];
52                 if (len == strlen(known_param) &&
53                                 !strncmp(param, known_param, len))
54                         return true;
55         }
56
57         return false;
58 }
59
60 static int parse_nvram_params(struct platform_powerpc *platform,
61                 char *buf, int len)
62 {
63         char *pos, *name, *value;
64         unsigned int paramlen;
65         int i, count;
66
67         /* discard 2 header lines:
68          * "common" partiton"
69          * ------------------
70          */
71         pos = buf;
72         count = 0;
73
74         for (i = 0; i < len; i++) {
75                 if (pos[i] == '\n')
76                         count++;
77                 if (count == 2)
78                         break;
79         }
80
81         if (i == len) {
82                 fprintf(stderr, "failure parsing nvram output\n");
83                 return -1;
84         }
85
86         for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
87                 unsigned int namelen;
88                 struct param *param;
89                 char *newline;
90
91                 newline = strchr(pos, '\n');
92                 if (!newline)
93                         break;
94
95                 *newline = '\0';
96
97                 paramlen = strlen(pos);
98
99                 name = pos;
100                 value = strchr(pos, '=');
101                 if (!value)
102                         continue;
103
104                 namelen = value - name;
105                 if (namelen == 0)
106                         continue;
107
108                 if (!param_is_known(name, namelen))
109                         continue;
110
111                 value++;
112
113                 param = talloc(platform, struct param);
114                 param->modified = false;
115                 param->name = talloc_strndup(platform, name, namelen);
116                 param->value = talloc_strdup(platform, value);
117                 list_add(&platform->params, &param->list);
118         }
119
120         return 0;
121 }
122
123 static int parse_nvram(struct platform_powerpc *platform)
124 {
125         struct process *process;
126         const char *argv[5];
127         int rc;
128
129         argv[0] = "nvram";
130         argv[1] = "--print-config";
131         argv[2] = "--partition";
132         argv[3] = partition;
133         argv[4] = NULL;
134
135         process = process_create(platform);
136         process->path = "nvram";
137         process->argv = argv;
138         process->keep_stdout = true;
139
140         rc = process_run_sync(process);
141
142         if (rc || !process_exit_ok(process)) {
143                 fprintf(stderr, "nvram process returned "
144                                 "non-zero exit status\n");
145                 rc = -1;
146         } else {
147                 rc = parse_nvram_params(platform, process->stdout_buf,
148                                             process->stdout_len);
149         }
150
151         process_release(process);
152         return rc;
153 }
154
155 static int write_nvram(struct platform_powerpc *platform)
156 {
157         struct process *process;
158         struct param *param;
159         const char *argv[6];
160         int rc;
161
162         argv[0] = "nvram";
163         argv[1] = "--update-config";
164         argv[2] = NULL;
165         argv[3] = "--partition";
166         argv[4] = partition;
167         argv[5] = NULL;
168
169         process = process_create(platform);
170         process->path = "nvram";
171         process->argv = argv;
172
173         list_for_each_entry(&platform->params, param, list) {
174                 char *paramstr;
175
176                 if (!param->modified)
177                         continue;
178
179                 paramstr = talloc_asprintf(platform, "%s=%s",
180                                 param->name, param->value);
181                 argv[2] = paramstr;
182
183                 rc = process_run_sync(process);
184
185                 talloc_free(paramstr);
186
187                 if (rc || !process_exit_ok(process)) {
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 platform_powerpc *platform,
200                 const char *name)
201 {
202         struct param *param;
203
204         list_for_each_entry(&platform->params, param, list)
205                 if (!strcmp(param->name, name))
206                         return param->value;
207         return NULL;
208 }
209
210 static void set_param(struct platform_powerpc *platform, const char *name,
211                 const char *value)
212 {
213         struct param *param;
214
215         list_for_each_entry(&platform->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(platform, struct param);
230         param->modified = true;
231         param->name = talloc_strdup(platform, name);
232         param->value = talloc_strdup(platform, value);
233         list_add(&platform->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 platform_powerpc *platform,
344                 struct config *config)
345 {
346         const char *cval;
347         char *val;
348         int i;
349
350         cval = get_param(platform, "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 platform_powerpc *platform,
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(platform, "auto-boot?");
383         config->autoboot_enabled = !val || strcmp(val, "false");
384
385         val = get_param(platform, "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(platform, 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_string_config(struct platform_powerpc *platform,
440                 const char *name, const char *value)
441 {
442         const char *cur;
443
444         cur = get_param(platform, name);
445
446         /* don't set an empty parameter if it doesn't already exist */
447         if (!cur && !strlen(value))
448                 return;
449
450         set_param(platform, name, value);
451 }
452
453 static void update_network_config(struct platform_powerpc *platform,
454         struct config *config)
455 {
456         unsigned int i;
457         char *val;
458
459         val = talloc_strdup(platform, "");
460
461         for (i = 0; i < config->network.n_interfaces; i++) {
462                 char *iface_str = iface_config_str(platform,
463                                         config->network.interfaces[i]);
464                 val = talloc_asprintf_append(val, "%s%s",
465                                 *val == '\0' ? "" : " ", iface_str);
466                 talloc_free(iface_str);
467         }
468
469         if (config->network.n_dns_servers) {
470                 char *dns_str = dns_config_str(platform,
471                                                 config->network.dns_servers,
472                                                 config->network.n_dns_servers);
473                 val = talloc_asprintf_append(val, "%s%s",
474                                 *val == '\0' ? "" : " ", dns_str);
475                 talloc_free(dns_str);
476         }
477
478         update_string_config(platform, "petitboot,network", val);
479
480         talloc_free(val);
481 }
482
483 static int update_config(struct platform_powerpc *platform,
484                 struct config *config, struct config *defaults)
485 {
486         char *tmp = NULL;
487         const char *val;
488
489         if (config->autoboot_enabled == defaults->autoboot_enabled)
490                 val = "";
491         else
492                 val = config->autoboot_enabled ? "true" : "false";
493         update_string_config(platform, "auto-boot?", val);
494
495         if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
496                 val = "";
497         else
498                 val = tmp = talloc_asprintf(platform, "%d",
499                                 config->autoboot_timeout_sec);
500
501         update_string_config(platform, "petitboot,timeout", val);
502         if (tmp)
503                 talloc_free(tmp);
504
505         update_network_config(platform, config);
506
507         return write_nvram(platform);
508 }
509
510 static void set_exclusive_devtype(struct config *config,
511                 enum device_type devtype)
512 {
513         config->n_boot_priorities = 2;
514         config->boot_priorities = talloc_realloc(config,
515                         config->boot_priorities, struct boot_priority,
516                         config->n_boot_priorities);
517         config->boot_priorities[0].type = devtype;
518         config->boot_priorities[0].priority = 0;
519         config->boot_priorities[1].type = DEVICE_TYPE_ANY;
520         config->boot_priorities[1].priority = -1;
521 }
522
523 /* bootdev options that we recognise */
524 enum ipmi_bootdev {
525         IPMI_BOOTDEV_NONE = 0x00,
526         IPMI_BOOTDEV_NETWORK = 0x01,
527         IPMI_BOOTDEV_DISK = 0x2,
528         IPMI_BOOTDEV_CDROM = 0x5,
529         IPMI_BOOTDEV_SETUP = 0x6,
530 };
531
532 static int read_bootdev_sysparam(const char *name, uint8_t *val)
533 {
534         uint8_t buf[2];
535         char path[50];
536         int fd, rc;
537
538         strcpy(path, sysparams_dir);
539         assert(strlen(name) < sizeof(path) - strlen(path));
540         strcat(path, name);
541
542         fd = open(path, O_RDONLY);
543         if (fd < 0) {
544                 pb_debug("powerpc: can't access sysparam %s\n",
545                                 name);
546                 return -1;
547         }
548
549         rc = read(fd, buf, sizeof(buf));
550
551         close(fd);
552
553         /* bootdev definitions should only be one byte in size */
554         if (rc != 1) {
555                 pb_debug("powerpc: sysparam %s read returned %d\n",
556                                 name, rc);
557                 return -1;
558         }
559
560         pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
561
562         switch (buf[0]) {
563         default:
564                 return -1;
565         case IPMI_BOOTDEV_NONE:
566         case IPMI_BOOTDEV_NETWORK:
567         case IPMI_BOOTDEV_DISK:
568         case IPMI_BOOTDEV_CDROM:
569         case IPMI_BOOTDEV_SETUP:
570                 *val = buf[0];
571         }
572
573         return 0;
574 }
575
576 static void parse_opal_sysparams(struct config *config)
577 {
578         uint8_t next_bootdev, default_bootdev;
579         bool next_valid, default_valid;
580         int rc;
581
582         rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
583         next_valid = rc == 0;
584
585         rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
586         default_valid = rc == 0;
587
588         /* nothing valid? no need to change the config */
589         if (!next_valid && !default_valid)
590                 return;
591
592         if (!next_valid)
593                 next_bootdev = default_bootdev;
594
595         /* todo: copy default to next */
596
597         switch (next_bootdev) {
598         case IPMI_BOOTDEV_NONE:
599                 return;
600         case IPMI_BOOTDEV_DISK:
601                 set_exclusive_devtype(config, DEVICE_TYPE_DISK);
602                 break;
603         case IPMI_BOOTDEV_NETWORK:
604                 set_exclusive_devtype(config, DEVICE_TYPE_NETWORK);
605                 break;
606         case IPMI_BOOTDEV_CDROM:
607                 set_exclusive_devtype(config, DEVICE_TYPE_OPTICAL);
608                 break;
609         case IPMI_BOOTDEV_SETUP:
610                 config->autoboot_enabled = false;
611                 break;
612         }
613 }
614
615 static int load_config(struct platform *p, struct config *config)
616 {
617         struct platform_powerpc *platform = to_platform_powerpc(p);
618         int rc;
619
620         rc = parse_nvram(platform);
621         if (rc)
622                 return rc;
623
624         populate_config(platform, config);
625
626         parse_opal_sysparams(config);
627
628         return 0;
629 }
630
631 static int save_config(struct platform *p, struct config *config)
632 {
633         struct platform_powerpc *platform = to_platform_powerpc(p);
634         struct config *defaults;
635         int rc;
636
637         defaults = talloc_zero(platform, struct config);
638         config_set_defaults(defaults);
639
640         rc = update_config(platform, config, defaults);
641
642         talloc_free(defaults);
643         return rc;
644 }
645
646 static bool probe(struct platform *p, void *ctx)
647 {
648         struct platform_powerpc *platform;
649         struct stat statbuf;
650         int rc;
651
652         /* we need a device tree and a working nvram binary */
653         rc = stat("/proc/device-tree", &statbuf);
654         if (rc)
655                 return false;
656
657         if (!S_ISDIR(statbuf.st_mode))
658                 return false;
659
660         rc = process_run_simple(ctx, "nvram", "--print-config", NULL);
661         if (!WIFEXITED(rc) || WEXITSTATUS(rc) != 0)
662                 return false;
663
664         platform = talloc(ctx, struct platform_powerpc);
665         list_init(&platform->params);
666
667         p->platform_data = platform;
668         return true;
669 }
670
671 static struct platform platform_powerpc = {
672         .name           = "powerpc",
673         .dhcp_arch_id   = 0x000e,
674         .probe          = probe,
675         .load_config    = load_config,
676         .save_config    = save_config,
677 };
678
679 register_platform(platform_powerpc);