]> git.ozlabs.org Git - petitboot/blob - discover/platform-powerpc.c
1ae96ac67f850726765db3b7babfa42f267b0585
[petitboot] / discover / platform-powerpc.c
1
2 #include <assert.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <sys/stat.h>
11 #include <asm/byteorder.h>
12
13 #include <file/file.h>
14 #include <talloc/talloc.h>
15 #include <list/list.h>
16 #include <log/log.h>
17 #include <param_list/param_list.h>
18 #include <process/process.h>
19 #include <types/types.h>
20 #include <url/url.h>
21
22 #include "hostboot.h"
23 #include "platform.h"
24 #include "ipmi.h"
25 #include "dt.h"
26
27 static const char *partition = "common";
28 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
29 static const char *devtree_dir = "/proc/device-tree/";
30
31 struct platform_powerpc {
32         struct param_list params;
33         struct ipmi     *ipmi;
34         bool            ipmi_bootdev_persistent;
35         int             (*get_ipmi_bootdev)(
36                                 struct platform_powerpc *platform,
37                                 uint8_t *bootdev, bool *persistent);
38         int             (*clear_ipmi_bootdev)(
39                                 struct platform_powerpc *platform,
40                                 bool persistent);
41         int             (*set_os_boot_sensor)(
42                                 struct platform_powerpc *platform);
43         void            (*get_platform_versions)(struct system_info *info);
44 };
45
46 #define to_platform_powerpc(p) \
47         (struct platform_powerpc *)(p->platform_data)
48
49 static int parse_nvram_params(struct platform_powerpc *platform,
50                 char *buf, int len)
51 {
52         char *pos, *name, *value;
53         unsigned int paramlen;
54         int i, count;
55
56         /* discard 2 header lines:
57          * "common" partiton"
58          * ------------------
59          */
60         pos = buf;
61         count = 0;
62
63         for (i = 0; i < len; i++) {
64                 if (pos[i] == '\n')
65                         count++;
66                 if (count == 2)
67                         break;
68         }
69
70         if (i == len) {
71                 fprintf(stderr, "failure parsing nvram output\n");
72                 return -1;
73         }
74
75         for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
76                 unsigned int namelen;
77                 char *newline;
78
79                 newline = strchr(pos, '\n');
80                 if (!newline)
81                         break;
82
83                 *newline = '\0';
84
85                 paramlen = strlen(pos);
86
87                 name = pos;
88                 value = strchr(pos, '=');
89                 if (!value)
90                         continue;
91
92                 namelen = value - name;
93                 if (namelen == 0)
94                         continue;
95
96                 if (!param_list_is_known_n(&platform->params, name, namelen))
97                         continue;
98
99                 *value = '\0';
100                 value++;
101
102                 param_list_set(&platform->params, name, value, false);
103         }
104
105         return 0;
106 }
107
108 static int parse_nvram(struct platform_powerpc *platform)
109 {
110         struct process_stdout *stdout;
111         const char *argv[5];
112         int rc;
113
114         argv[0] = "nvram";
115         argv[1] = "--print-config";
116         argv[2] = "--partition";
117         argv[3] = partition;
118         argv[4] = NULL;
119
120         rc = process_get_stdout_argv(NULL, &stdout, argv);
121
122         if (rc) {
123                 fprintf(stderr, "nvram process returned "
124                                 "non-zero exit status\n");
125                 rc = -1;
126         } else {
127                 rc = parse_nvram_params(platform, stdout->buf, stdout->len);
128         }
129
130         talloc_free(stdout);
131         return rc;
132 }
133
134 static int write_nvram(struct platform_powerpc *platform)
135 {
136         struct process *process;
137         struct param *param;
138         const char *argv[6];
139         int rc = 0;
140
141         argv[0] = "nvram";
142         argv[1] = "--update-config";
143         argv[2] = NULL;
144         argv[3] = "--partition";
145         argv[4] = partition;
146         argv[5] = NULL;
147
148         process = process_create(platform);
149         process->path = "nvram";
150         process->argv = argv;
151
152         param_list_for_each(&platform->params, param) {
153                 char *paramstr;
154
155                 if (!param->modified)
156                         continue;
157
158                 paramstr = talloc_asprintf(platform, "%s=%s",
159                                 param->name, param->value);
160                 argv[2] = paramstr;
161
162                 rc = process_run_sync(process);
163
164                 talloc_free(paramstr);
165
166                 if (rc || !process_exit_ok(process)) {
167                         rc = -1;
168                         pb_log("nvram update process returned "
169                                         "non-zero exit status\n");
170                         break;
171                 }
172         }
173
174         process_release(process);
175         return rc;
176 }
177
178 static int parse_hwaddr(struct interface_config *ifconf, char *str)
179 {
180         int i;
181
182         if (strlen(str) != strlen("00:00:00:00:00:00"))
183                 return -1;
184
185         for (i = 0; i < HWADDR_SIZE; i++) {
186                 char byte[3], *endp;
187                 unsigned long x;
188
189                 byte[0] = str[i * 3 + 0];
190                 byte[1] = str[i * 3 + 1];
191                 byte[2] = '\0';
192
193                 x = strtoul(byte, &endp, 16);
194                 if (endp != byte + 2)
195                         return -1;
196
197                 ifconf->hwaddr[i] = x & 0xff;
198         }
199
200         return 0;
201 }
202
203 static int parse_one_interface_config(struct config *config, char *confstr)
204 {
205         struct interface_config *ifconf;
206         char *tok, *tok_gw, *tok_url, *saveptr;
207
208         ifconf = talloc_zero(config, struct interface_config);
209
210         if (!confstr || !strlen(confstr))
211                 goto out_err;
212
213         /* first token should be the mac address */
214         tok = strtok_r(confstr, ",", &saveptr);
215         if (!tok)
216                 goto out_err;
217
218         if (parse_hwaddr(ifconf, tok))
219                 goto out_err;
220
221         /* second token is the method */
222         tok = strtok_r(NULL, ",", &saveptr);
223         if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
224                 ifconf->ignore = true;
225
226         } else if (!strcmp(tok, "dhcp")) {
227                 ifconf->method = CONFIG_METHOD_DHCP;
228
229         } else if (!strcmp(tok, "static")) {
230                 ifconf->method = CONFIG_METHOD_STATIC;
231
232                 /* ip/mask, [optional] gateway, [optional] url */
233                 tok = strtok_r(NULL, ",", &saveptr);
234                 if (!tok)
235                         goto out_err;
236                 ifconf->static_config.address =
237                         talloc_strdup(ifconf, tok);
238
239                 /*
240                  * If a url is set but not a gateway, we can accidentally
241                  * interpret the url as the gateway. To avoid changing the
242                  * parameter format check if the "gateway" is actually a
243                  * pb-url if it's the last token.
244                  */
245                 tok_gw = strtok_r(NULL, ",", &saveptr);
246                 tok_url = strtok_r(NULL, ",", &saveptr);
247
248                 if (tok_gw) {
249                         if (tok_url || !is_url(tok_gw))
250                                 ifconf->static_config.gateway =
251                                         talloc_strdup(ifconf, tok_gw);
252                         else
253                                         tok_url = tok_gw;
254                 }
255
256                 if (tok_url)
257                         ifconf->static_config.url =
258                                 talloc_strdup(ifconf, tok_url);
259         } else {
260                 pb_log("Unknown network configuration method %s\n", tok);
261                 goto out_err;
262         }
263
264         config->network.interfaces = talloc_realloc(config,
265                         config->network.interfaces,
266                         struct interface_config *,
267                         ++config->network.n_interfaces);
268
269         config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
270
271         return 0;
272 out_err:
273         talloc_free(ifconf);
274         return -1;
275 }
276
277 static int parse_one_dns_config(struct config *config, char *confstr)
278 {
279         char *tok, *saveptr = NULL;
280
281         for (tok = strtok_r(confstr, ",", &saveptr); tok;
282                         tok = strtok_r(NULL, ",", &saveptr)) {
283
284                 char *server = talloc_strdup(config, tok);
285
286                 config->network.dns_servers = talloc_realloc(config,
287                                 config->network.dns_servers, const char *,
288                                 ++config->network.n_dns_servers);
289
290                 config->network.dns_servers[config->network.n_dns_servers - 1]
291                                 = server;
292         }
293
294         return 0;
295 }
296
297 static void populate_network_config(struct config *config, const char *cval)
298 {
299         char *val, *saveptr = NULL;
300         int i;
301
302         if (!cval || !strlen(cval))
303                 return;
304
305         val = talloc_strdup(config, cval);
306
307         for (i = 0; ; i++) {
308                 char *tok;
309
310                 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
311                 if (!tok)
312                         break;
313
314                 if (!strncasecmp(tok, "dns,", strlen("dns,")))
315                         parse_one_dns_config(config, tok + strlen("dns,"));
316                 else
317                         parse_one_interface_config(config, tok);
318
319         }
320
321         talloc_free(val);
322 }
323
324 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
325 {
326         char *delim = strchr(*pos, ' ');
327         int len, prefix = 0, rc = -1;
328         enum device_type type;
329
330         if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
331                 prefix = strlen("uuid:");
332                 opt->boot_type = BOOT_DEVICE_UUID;
333         } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
334                 prefix = strlen("mac:");
335                 opt->boot_type = BOOT_DEVICE_UUID;
336         } else {
337                 type = find_device_type(*pos);
338                 if (type != DEVICE_TYPE_UNKNOWN) {
339                         opt->type = type;
340                         opt->boot_type = BOOT_DEVICE_TYPE;
341                         rc = 0;
342                 }
343         }
344
345         if (opt->boot_type == BOOT_DEVICE_UUID) {
346                 if (delim)
347                         len = (int)(delim - *pos) - prefix;
348                 else
349                         len = strlen(*pos) - prefix;
350
351                 if (len) {
352                         opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
353                         rc = 0;
354                 }
355         }
356
357         /* Always advance pointer to next option or end */
358         if (delim)
359                 *pos = delim + 1;
360         else
361                 *pos += strlen(*pos);
362
363         return rc;
364 }
365
366 static void populate_bootdev_config(struct config *config,
367         const struct param_list *pl)
368 {
369         struct autoboot_option *opt, *new = NULL;
370         char *pos, *end;
371         unsigned int n_new = 0;
372         const char *val;
373
374         /* Check for ordered bootdevs */
375         val = param_list_get_value(pl, "petitboot,bootdevs");
376         if (!val || !strlen(val)) {
377                 pos = end = NULL;
378         } else {
379                 pos = talloc_strdup(config, val);
380                 end = strchr(pos, '\0');
381         }
382
383         while (pos && pos < end) {
384                 opt = talloc(config, struct autoboot_option);
385
386                 if (read_bootdev(config, &pos, opt)) {
387                         pb_log("bootdev config is in an unknown format "
388                                "(expected uuid:... or mac:...)\n");
389                         talloc_free(opt);
390                         continue;
391                 }
392
393                 new = talloc_realloc(config, new, struct autoboot_option,
394                                      n_new + 1);
395                 new[n_new] = *opt;
396                 n_new++;
397                 talloc_free(opt);
398
399         }
400
401         if (!n_new) {
402                 /* If autoboot has been disabled, clear the default options */
403                 if (!config->autoboot_enabled) {
404                         talloc_free(config->autoboot_opts);
405                         config->n_autoboot_opts = 0;
406                 }
407                 return;
408         }
409
410         talloc_free(config->autoboot_opts);
411         config->autoboot_opts = new;
412         config->n_autoboot_opts = n_new;
413 }
414
415 static void populate_config(struct config *config, const struct param_list *pl)
416 {
417         const char *val;
418         char *end;
419         unsigned long timeout;
420
421         /* if the "auto-boot?' property is present and "false", disable auto
422          * boot */
423         val = param_list_get_value(pl, "auto-boot?");
424         config->autoboot_enabled = !val || strcmp(val, "false");
425
426         val = param_list_get_value(pl, "petitboot,timeout");
427         if (val) {
428                 timeout = strtoul(val, &end, 10);
429                 if (end != val) {
430                         if (timeout >= INT_MAX)
431                                 timeout = INT_MAX;
432                         config->autoboot_timeout_sec = (int)timeout;
433                 }
434         }
435
436         val = param_list_get_value(pl, "petitboot,language");
437         config->lang = val ? talloc_strdup(config, val) : NULL;
438
439         val = param_list_get_value(pl, "petitboot,network");
440         populate_network_config(config, val);
441
442         populate_bootdev_config(config, pl);
443
444         if (!config->debug) {
445                 val = param_list_get_value(pl, "petitboot,debug?");
446                 config->debug = val && !strcmp(val, "true");
447         }
448
449         val = param_list_get_value(pl, "petitboot,write?");
450         if (val)
451                 config->allow_writes = !strcmp(val, "true");
452
453         val = param_list_get_value(pl, "petitboot,snapshots?");
454         if (val)
455                 config->disable_snapshots = !strcmp(val, "false");
456
457         val = param_list_get_value(pl, "petitboot,console");
458         if (val)
459                 config->boot_console = talloc_strdup(config, val);
460         /* If a full path is already set we don't want to override it */
461         config->manual_console = config->boot_console &&
462                                         !strchr(config->boot_console, '[');
463
464         val = param_list_get_value(pl, "petitboot,http_proxy");
465         if (val)
466                 config->http_proxy = talloc_strdup(config, val);
467         val = param_list_get_value(pl, "petitboot,https_proxy");
468         if (val)
469                 config->https_proxy = talloc_strdup(config, val);
470 }
471
472 static char *iface_config_str(void *ctx, struct interface_config *config)
473 {
474         char *str;
475
476         /* todo: HWADDR size is hardcoded as 6, but we may need to handle
477          * different hardware address formats */
478         str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
479                         config->hwaddr[0], config->hwaddr[1],
480                         config->hwaddr[2], config->hwaddr[3],
481                         config->hwaddr[4], config->hwaddr[5]);
482
483         if (config->ignore) {
484                 str = talloc_asprintf_append(str, "ignore");
485
486         } else if (config->method == CONFIG_METHOD_DHCP) {
487                 str = talloc_asprintf_append(str, "dhcp");
488
489         } else if (config->method == CONFIG_METHOD_STATIC) {
490                 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
491                                 config->static_config.address,
492                                 config->static_config.gateway ? "," : "",
493                                 config->static_config.gateway ?: "",
494                                 config->static_config.url ? "," : "",
495                                 config->static_config.url ?: "");
496         }
497         return str;
498 }
499
500 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
501 {
502         char *str;
503         int i;
504
505         str = talloc_strdup(ctx, "dns,");
506         for (i = 0; i < n; i++) {
507                 str = talloc_asprintf_append(str, "%s%s",
508                                 i == 0 ? "" : ",",
509                                 dns_servers[i]);
510         }
511
512         return str;
513 }
514
515 static void update_network_config(struct param_list *pl, const char *param_name,
516         const struct config *config)
517 {
518         unsigned int i;
519         char *val;
520
521         /*
522          * Don't store IPMI overrides to NVRAM. If this was a persistent
523          * override it was already stored in NVRAM by
524          * get_ipmi_network_override()
525          */
526         if (config->network.n_interfaces &&
527                 config->network.interfaces[0]->override)
528                 return;
529
530         val = talloc_strdup(pl, "");
531
532         for (i = 0; i < config->network.n_interfaces; i++) {
533                 char *iface_str = iface_config_str(pl,
534                                         config->network.interfaces[i]);
535                 val = talloc_asprintf_append(val, "%s%s",
536                                 *val == '\0' ? "" : " ", iface_str);
537                 talloc_free(iface_str);
538         }
539
540         if (config->network.n_dns_servers) {
541                 char *dns_str = dns_config_str(pl,
542                                                 config->network.dns_servers,
543                                                 config->network.n_dns_servers);
544                 val = talloc_asprintf_append(val, "%s%s",
545                                 *val == '\0' ? "" : " ", dns_str);
546                 talloc_free(dns_str);
547         }
548
549         param_list_set_non_empty(pl, param_name, val, true);
550         talloc_free(val);
551 }
552
553 static void update_bootdev_config(struct param_list *pl, const char *param_name,
554         const struct config *config)
555 {
556         char *val = NULL, *boot_str = NULL, *tmp = NULL;
557         struct autoboot_option *opt;
558         const char delim = ' ';
559         unsigned int i;
560
561         if (!config->n_autoboot_opts)
562                 val = "";
563
564         for (i = 0; i < config->n_autoboot_opts; i++) {
565                 opt = &config->autoboot_opts[i];
566                 switch (opt->boot_type) {
567                         case BOOT_DEVICE_TYPE:
568                                 boot_str = talloc_asprintf(config, "%s%c",
569                                                 device_type_name(opt->type),
570                                                 delim);
571                                 break;
572                         case BOOT_DEVICE_UUID:
573                                 boot_str = talloc_asprintf(config, "uuid:%s%c",
574                                                 opt->uuid, delim);
575                                 break;
576                         }
577                         tmp = val = talloc_asprintf_append(val, "%s", boot_str);
578         }
579
580         param_list_set_non_empty(pl, param_name, val, true);
581         talloc_free(tmp);
582         if (boot_str)
583                 talloc_free(boot_str);
584 }
585
586 static void update_config(struct param_list *pl, struct config *config,
587         const struct config *defaults)
588 {
589         char *tmp = NULL;
590         const char *val;
591
592         if (config->autoboot_enabled == defaults->autoboot_enabled)
593                 val = "";
594         else
595                 val = config->autoboot_enabled ? "true" : "false";
596
597         param_list_set_non_empty(pl, "auto-boot?", val, true);
598
599         if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
600                 val = "";
601         else
602                 val = tmp = talloc_asprintf(pl, "%d",
603                         config->autoboot_timeout_sec);
604
605         param_list_set_non_empty(pl, "petitboot,timeout", val, true);
606         if (tmp)
607                 talloc_free(tmp);
608
609         val = config->lang ?: "";
610         param_list_set_non_empty(pl, "petitboot,language", val, true);
611
612         if (config->allow_writes == defaults->allow_writes)
613                 val = "";
614         else
615                 val = config->allow_writes ? "true" : "false";
616         param_list_set_non_empty(pl, "petitboot,write?", val, true);
617
618         if (!config->manual_console) {
619                 val = config->boot_console ?: "";
620                 param_list_set_non_empty(pl, "petitboot,console", val, true);
621         }
622
623         val = config->http_proxy ?: "";
624         param_list_set_non_empty(pl, "petitboot,http_proxy", val, true);
625         val = config->https_proxy ?: "";
626         param_list_set_non_empty(pl, "petitboot,https_proxy", val, true);
627
628         update_network_config(pl, "petitboot,network", config);
629         update_bootdev_config(pl, "petitboot,bootdevs", config);
630 }
631
632 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
633                 bool persistent)
634 {
635         config->ipmi_bootdev = bootdev;
636         config->ipmi_bootdev_persistent = persistent;
637
638         if (bootdev == IPMI_BOOTDEV_SAFE)
639                 config->safe_mode = true;
640 }
641
642 static int read_bootdev_sysparam(const char *name, uint8_t *val)
643 {
644         uint8_t buf[2];
645         char path[50];
646         int fd, rc;
647
648         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
649         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
650
651         fd = open(path, O_RDONLY);
652         if (fd < 0) {
653                 pb_debug("powerpc: can't access sysparam %s\n",
654                                 name);
655                 return -1;
656         }
657
658         rc = read(fd, buf, sizeof(buf));
659
660         close(fd);
661
662         /* bootdev definitions should only be one byte in size */
663         if (rc != 1) {
664                 pb_debug("powerpc: sysparam %s read returned %d\n",
665                                 name, rc);
666                 return -1;
667         }
668
669         pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
670
671         if (!ipmi_bootdev_is_valid(buf[0]))
672                 return -1;
673
674         *val = buf[0];
675         return 0;
676 }
677
678 static int write_bootdev_sysparam(const char *name, uint8_t val)
679 {
680         char path[50];
681         int fd, rc;
682
683         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
684         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
685
686         fd = open(path, O_WRONLY);
687         if (fd < 0) {
688                 pb_debug("powerpc: can't access sysparam %s for writing\n",
689                                 name);
690                 return -1;
691         }
692
693         for (;;) {
694                 errno = 0;
695                 rc = write(fd, &val, sizeof(val));
696                 if (rc == sizeof(val)) {
697                         rc = 0;
698                         break;
699                 }
700
701                 if (rc <= 0 && errno != EINTR) {
702                         pb_log("powerpc: error updating sysparam %s: %s",
703                                         name, strerror(errno));
704                         rc = -1;
705                         break;
706                 }
707         }
708
709         close(fd);
710
711         if (!rc)
712                 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
713
714         return rc;
715 }
716
717 static int clear_ipmi_bootdev_sysparams(
718                 struct platform_powerpc *platform __attribute__((unused)),
719                 bool persistent)
720 {
721         if (persistent) {
722                 /* invalidate default-boot-device setting */
723                 write_bootdev_sysparam("default-boot-device", 0xff);
724         } else {
725                 /* invalidate next-boot-device setting */
726                 write_bootdev_sysparam("next-boot-device", 0xff);
727         }
728         return 0;
729 }
730
731 static int get_ipmi_bootdev_sysparams(
732                 struct platform_powerpc *platform __attribute__((unused)),
733                 uint8_t *bootdev, bool *persistent)
734 {
735         uint8_t next_bootdev, default_bootdev;
736         bool next_valid, default_valid;
737         int rc;
738
739         rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
740         next_valid = rc == 0;
741
742         rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
743         default_valid = rc == 0;
744
745         /* nothing valid? no need to change the config */
746         if (!next_valid && !default_valid)
747                 return -1;
748
749         *persistent = !next_valid;
750         *bootdev = next_valid ? next_bootdev : default_bootdev;
751         return 0;
752 }
753
754 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
755                                    bool persistent __attribute__((unused)))
756 {
757         uint16_t resp_len;
758         uint8_t resp[1];
759         uint8_t req[] = {
760                 0x05, /* parameter selector: boot flags */
761                 0x80, /* data 1: valid */
762                 0x00, /* data 2: bootdev: no override */
763                 0x00, /* data 3: system defaults */
764                 0x00, /* data 4: no request for shared mode, mux defaults */
765                 0x00, /* data 5: no instance request */
766         };
767
768         resp_len = sizeof(resp);
769
770         ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
771                         IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
772                         req, sizeof(req),
773                         resp, &resp_len,
774                         ipmi_timeout);
775         return 0;
776 }
777
778 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
779                 uint8_t *bootdev, bool *persistent)
780 {
781         uint16_t resp_len;
782         uint8_t resp[8];
783         int rc;
784         uint8_t req[] = {
785                 0x05, /* parameter selector: boot flags */
786                 0x00, /* no set selector */
787                 0x00, /* no block selector */
788         };
789
790         resp_len = sizeof(resp);
791         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
792                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
793                         req, sizeof(req),
794                         resp, &resp_len,
795                         ipmi_timeout);
796         if (rc) {
797                 pb_log("platform: error reading IPMI boot options\n");
798                 return -1;
799         }
800
801         if (resp_len != sizeof(resp)) {
802                 pb_log("platform: unexpected length (%d) in "
803                                 "boot options response\n", resp_len);
804                 return -1;
805         }
806
807         pb_debug("IPMI get_bootdev response:\n");
808         for (int i = 0; i < resp_len; i++)
809                 pb_debug("%x ", resp[i]);
810         pb_debug("\n");
811
812         if (resp[0] != 0) {
813                 pb_log("platform: non-zero completion code %d from IPMI req\n",
814                                 resp[0]);
815                 return -1;
816         }
817
818         /* check for correct parameter version */
819         if ((resp[1] & 0xf) != 0x1) {
820                 pb_log("platform: unexpected version (0x%x) in "
821                                 "boot options response\n", resp[0]);
822                 return -1;
823         }
824
825         /* check for valid paramters */
826         if (resp[2] & 0x80) {
827                 pb_debug("platform: boot options are invalid/locked\n");
828                 return -1;
829         }
830
831         *persistent = false;
832
833         /* check for valid flags */
834         if (!(resp[3] & 0x80)) {
835                 pb_debug("platform: boot flags are invalid, ignoring\n");
836                 return -1;
837         }
838
839         *persistent = resp[3] & 0x40;
840         *bootdev = (resp[4] >> 2) & 0x0f;
841         return 0;
842 }
843
844 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
845 {
846         int sensor_number;
847         uint16_t resp_len;
848         uint8_t resp[1];
849         uint8_t req[] = {
850                 0x00, /* sensor number: os boot */
851                 0xA9, /* operation: set everything */
852                 0x00, /* sensor reading: none */
853                 0x40, /* assertion mask lsb: set state 6 */
854                 0x00, /* assertion mask msb: none */
855                 0x00, /* deassertion mask lsb: none */
856                 0x00, /* deassertion mask msb: none */
857                 0x00, /* event data 1: none */
858                 0x00, /* event data 2: none */
859                 0x00, /* event data 3: none */
860         };
861
862         sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
863         if (sensor_number < 0) {
864                 pb_log("Couldn't find OS boot sensor in device tree\n");
865                 return -1;
866         }
867
868         req[0] = sensor_number;
869
870         resp_len = sizeof(resp);
871
872         ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
873                         IPMI_CMD_SENSOR_SET,
874                         req, sizeof(req),
875                         resp, &resp_len,
876                         ipmi_timeout); return 0;
877
878         return 0;
879 }
880
881 static void get_ipmi_network_override(struct platform_powerpc *platform,
882                         struct config *config)
883 {
884         uint16_t min_len = 12, resp_len = 53, version;
885         const uint32_t magic_value = 0x21706221;
886         uint8_t resp[resp_len];
887         uint32_t cookie;
888         bool persistent;
889         int i, rc;
890         uint8_t req[] = {
891                 0x61, /* parameter selector: OEM section (network) */
892                 0x00, /* no set selector */
893                 0x00, /* no block selector */
894         };
895
896         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
897                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
898                         req, sizeof(req),
899                         resp, &resp_len,
900                         ipmi_timeout);
901
902         pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
903         if (resp_len > 0) {
904                 for (i = 0; i < resp_len; i++) {
905                         pb_debug(" %02x", resp[i]);
906                         if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
907                                 pb_debug("\n");
908                         else if (i && (i + 1) % 8 == 0)
909                                 pb_debug(" ");
910                 }
911                 pb_debug("\n");
912         }
913
914         if (rc) {
915                 pb_debug("IPMI network config option unavailable\n");
916                 return;
917         }
918
919         if (resp_len < min_len) {
920                 pb_debug("IPMI net response too small\n");
921                 return;
922         }
923
924         if (resp[0] != 0) {
925                 pb_log("platform: non-zero completion code %d from IPMI network req\n",
926                        resp[0]);
927                 return;
928         }
929
930         /* Check for correct parameter version */
931         if ((resp[1] & 0xf) != 0x1) {
932                 pb_log("platform: unexpected version (0x%x) in network override response\n",
933                        resp[0]);
934                 return;
935         }
936
937         /* Check that the parameters are valid */
938         if (resp[2] & 0x80) {
939                 pb_debug("platform: network override is invalid/locked\n");
940                 return;
941         }
942
943         /* Check for valid parameters in the boot flags section */
944         if (!(resp[3] & 0x80)) {
945                 pb_debug("platform: network override valid flag not set\n");
946                 return;
947         }
948         /* Read the persistent flag; if it is set we need to save this config */
949         persistent = resp[3] & 0x40;
950         if (persistent)
951                 pb_debug("platform: network override is persistent\n");
952
953         /* Check 4-byte cookie value */
954         i = 4;
955         memcpy(&cookie, &resp[i], sizeof(cookie));
956         cookie = __be32_to_cpu(cookie);
957         if (cookie != magic_value) {
958                 pb_log_fn("Incorrect cookie %x\n", cookie);
959                 return;
960         }
961         i += sizeof(cookie);
962
963         /* Check 2-byte version number */
964         memcpy(&version, &resp[i], sizeof(version));
965         version = __be16_to_cpu(version);
966         i += sizeof(version);
967         if (version != 1) {
968                 pb_debug("Unexpected version: %u\n", version);
969                 return;
970         }
971
972         /* Interpret the rest of the interface config */
973         rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
974
975         if (!rc && persistent) {
976                 /* Write this new config to NVRAM */
977                 update_network_config(&platform->params, "petitboot,network",
978                         config);
979                 rc = write_nvram(platform);
980                 if (rc)
981                         pb_log("platform: Failed to save persistent interface override\n");
982         }
983 }
984
985 static void get_active_consoles(struct config *config)
986 {
987         struct stat sbuf;
988         char *fsp_prop = NULL;
989
990         config->n_consoles = 2;
991         config->consoles = talloc_array(config, char *, config->n_consoles);
992         if (!config->consoles)
993                 goto err;
994
995         config->consoles[0] = talloc_asprintf(config->consoles,
996                                         "/dev/hvc0 [IPMI / Serial]");
997         config->consoles[1] = talloc_asprintf(config->consoles,
998                                         "/dev/tty1 [VGA]");
999
1000         fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1001         if (stat(fsp_prop, &sbuf) == 0) {
1002                 /* FSP based machines also have a separate serial console */
1003                 config->consoles = talloc_realloc(config, config->consoles,
1004                                                 char *, config->n_consoles + 1);
1005                 if (!config->consoles)
1006                         goto err;
1007                 config->consoles[config->n_consoles++] = talloc_asprintf(
1008                                                 config->consoles,
1009                                                 "/dev/hvc1 [Serial]");
1010         }
1011
1012         return;
1013 err:
1014         config->n_consoles = 0;
1015         pb_log("Failed to allocate memory for consoles\n");
1016 }
1017
1018 static int load_config(struct platform *p, struct config *config)
1019 {
1020         struct platform_powerpc *platform = to_platform_powerpc(p);
1021         int rc;
1022
1023         rc = parse_nvram(platform);
1024         if (rc)
1025                 pb_log_fn("Failed to parse nvram\n");
1026
1027         populate_config(config, &platform->params);
1028
1029         if (platform->get_ipmi_bootdev) {
1030                 bool bootdev_persistent;
1031                 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1032                 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1033                                 &bootdev_persistent);
1034                 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1035                         set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1036                 }
1037         }
1038
1039         if (platform->ipmi)
1040                 get_ipmi_network_override(platform, config);
1041
1042         get_active_consoles(config);
1043
1044         return 0;
1045 }
1046
1047 static int save_config(struct platform *p, struct config *config)
1048 {
1049         struct platform_powerpc *platform = to_platform_powerpc(p);
1050         struct config *defaults;
1051
1052         if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
1053             platform->clear_ipmi_bootdev) {
1054                 platform->clear_ipmi_bootdev(platform,
1055                                 config->ipmi_bootdev_persistent);
1056                 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
1057                 config->ipmi_bootdev_persistent = false;
1058         }
1059
1060         defaults = talloc_zero(platform, struct config);
1061         config_set_defaults(defaults);
1062
1063         update_config(&platform->params, config, defaults);
1064
1065         talloc_free(defaults);
1066         return write_nvram(platform);
1067 }
1068
1069 static void pre_boot(struct platform *p, const struct config *config)
1070 {
1071         struct platform_powerpc *platform = to_platform_powerpc(p);
1072
1073         if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1074                 platform->clear_ipmi_bootdev(platform, false);
1075
1076         if (platform->set_os_boot_sensor)
1077                 platform->set_os_boot_sensor(platform);
1078 }
1079
1080 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1081 {
1082         struct platform_powerpc *platform = p->platform_data;
1083         char *buf, *filename;
1084         int len, rc;
1085
1086         filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1087         rc = read_file(platform, filename, &buf, &len);
1088         if (rc == 0)
1089                 sysinfo->type = talloc_steal(sysinfo, buf);
1090         talloc_free(filename);
1091
1092         filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1093         rc = read_file(platform, filename, &buf, &len);
1094         if (rc == 0)
1095                 sysinfo->identifier = talloc_steal(sysinfo, buf);
1096         talloc_free(filename);
1097
1098         sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1099
1100         if (platform->ipmi) {
1101                 ipmi_get_bmc_mac(platform->ipmi, sysinfo->bmc_mac);
1102                 ipmi_get_bmc_versions(platform->ipmi, sysinfo);
1103         }
1104
1105         if (platform->get_platform_versions)
1106                 platform->get_platform_versions(sysinfo);
1107
1108         return 0;
1109 }
1110
1111 static bool probe(struct platform *p, void *ctx)
1112 {
1113         struct platform_powerpc *platform;
1114         struct stat statbuf;
1115         bool bmc_present;
1116         int rc;
1117
1118         /* we need a device tree */
1119         rc = stat("/proc/device-tree", &statbuf);
1120         if (rc)
1121                 return false;
1122
1123         if (!S_ISDIR(statbuf.st_mode))
1124                 return false;
1125
1126         platform = talloc_zero(ctx, struct platform_powerpc);
1127         param_list_init(&platform->params, common_known_params());
1128
1129         p->platform_data = platform;
1130
1131         bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1132
1133         if (ipmi_present() && bmc_present) {
1134                 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1135                 platform->ipmi = ipmi_open(platform);
1136                 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1137                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1138                 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1139         } else if (!stat(sysparams_dir, &statbuf)) {
1140                 pb_debug("platform: using sysparams for IPMI paramters\n");
1141                 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1142                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1143
1144         } else {
1145                 pb_log("platform: no IPMI parameter support\n");
1146         }
1147
1148         if (bmc_present)
1149                 platform->get_platform_versions = hostboot_load_versions;
1150
1151         return true;
1152 }
1153
1154
1155 static struct platform platform_powerpc = {
1156         .name                   = "powerpc",
1157         .dhcp_arch_id           = 0x000e,
1158         .probe                  = probe,
1159         .load_config            = load_config,
1160         .save_config            = save_config,
1161         .pre_boot               = pre_boot,
1162         .get_sysinfo            = get_sysinfo,
1163 };
1164
1165 register_platform(platform_powerpc);