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