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