]> git.ozlabs.org Git - petitboot/blob - discover/platform-powerpc.c
discover/event: Rename param -> event_param
[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         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, "petitboot,network", val);
646
647         talloc_free(val);
648 }
649
650 static void update_bootdev_config(struct platform_powerpc *platform,
651                 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, "petitboot,bootdevs", val);
678         talloc_free(tmp);
679         if (boot_str)
680                 talloc_free(boot_str);
681 }
682
683 static int 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         if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
702             platform->clear_ipmi_bootdev) {
703                 platform->clear_ipmi_bootdev(platform,
704                                 config->ipmi_bootdev_persistent);
705                 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
706                 config->ipmi_bootdev_persistent = false;
707         }
708
709         update_string_config(platform, "petitboot,timeout", val);
710         if (tmp)
711                 talloc_free(tmp);
712
713         val = config->lang ?: "";
714         update_string_config(platform, "petitboot,language", val);
715
716         if (config->allow_writes == defaults->allow_writes)
717                 val = "";
718         else
719                 val = config->allow_writes ? "true" : "false";
720         update_string_config(platform, "petitboot,write?", val);
721
722         if (!config->manual_console) {
723                 val = config->boot_console ?: "";
724                 update_string_config(platform, "petitboot,console", val);
725         }
726
727         val = config->http_proxy ?: "";
728         update_string_config(platform, "petitboot,http_proxy", val);
729         val = config->https_proxy ?: "";
730         update_string_config(platform, "petitboot,https_proxy", val);
731
732         update_network_config(platform, config);
733
734         update_bootdev_config(platform, config);
735
736         return write_nvram(platform);
737 }
738
739 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
740                 bool persistent)
741 {
742         config->ipmi_bootdev = bootdev;
743         config->ipmi_bootdev_persistent = persistent;
744
745         if (bootdev == IPMI_BOOTDEV_SAFE)
746                 config->safe_mode = true;
747 }
748
749 static int read_bootdev_sysparam(const char *name, uint8_t *val)
750 {
751         uint8_t buf[2];
752         char path[50];
753         int fd, rc;
754
755         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
756         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
757
758         fd = open(path, O_RDONLY);
759         if (fd < 0) {
760                 pb_debug("powerpc: can't access sysparam %s\n",
761                                 name);
762                 return -1;
763         }
764
765         rc = read(fd, buf, sizeof(buf));
766
767         close(fd);
768
769         /* bootdev definitions should only be one byte in size */
770         if (rc != 1) {
771                 pb_debug("powerpc: sysparam %s read returned %d\n",
772                                 name, rc);
773                 return -1;
774         }
775
776         pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
777
778         if (!ipmi_bootdev_is_valid(buf[0]))
779                 return -1;
780
781         *val = buf[0];
782         return 0;
783 }
784
785 static int write_bootdev_sysparam(const char *name, uint8_t val)
786 {
787         char path[50];
788         int fd, rc;
789
790         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
791         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
792
793         fd = open(path, O_WRONLY);
794         if (fd < 0) {
795                 pb_debug("powerpc: can't access sysparam %s for writing\n",
796                                 name);
797                 return -1;
798         }
799
800         for (;;) {
801                 errno = 0;
802                 rc = write(fd, &val, sizeof(val));
803                 if (rc == sizeof(val)) {
804                         rc = 0;
805                         break;
806                 }
807
808                 if (rc <= 0 && errno != EINTR) {
809                         pb_log("powerpc: error updating sysparam %s: %s",
810                                         name, strerror(errno));
811                         rc = -1;
812                         break;
813                 }
814         }
815
816         close(fd);
817
818         if (!rc)
819                 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
820
821         return rc;
822 }
823
824 static int clear_ipmi_bootdev_sysparams(
825                 struct platform_powerpc *platform __attribute__((unused)),
826                 bool persistent)
827 {
828         if (persistent) {
829                 /* invalidate default-boot-device setting */
830                 write_bootdev_sysparam("default-boot-device", 0xff);
831         } else {
832                 /* invalidate next-boot-device setting */
833                 write_bootdev_sysparam("next-boot-device", 0xff);
834         }
835         return 0;
836 }
837
838 static int get_ipmi_bootdev_sysparams(
839                 struct platform_powerpc *platform __attribute__((unused)),
840                 uint8_t *bootdev, bool *persistent)
841 {
842         uint8_t next_bootdev, default_bootdev;
843         bool next_valid, default_valid;
844         int rc;
845
846         rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
847         next_valid = rc == 0;
848
849         rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
850         default_valid = rc == 0;
851
852         /* nothing valid? no need to change the config */
853         if (!next_valid && !default_valid)
854                 return -1;
855
856         *persistent = !next_valid;
857         *bootdev = next_valid ? next_bootdev : default_bootdev;
858         return 0;
859 }
860
861 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
862                                    bool persistent __attribute__((unused)))
863 {
864         uint16_t resp_len;
865         uint8_t resp[1];
866         uint8_t req[] = {
867                 0x05, /* parameter selector: boot flags */
868                 0x80, /* data 1: valid */
869                 0x00, /* data 2: bootdev: no override */
870                 0x00, /* data 3: system defaults */
871                 0x00, /* data 4: no request for shared mode, mux defaults */
872                 0x00, /* data 5: no instance request */
873         };
874
875         resp_len = sizeof(resp);
876
877         ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
878                         IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
879                         req, sizeof(req),
880                         resp, &resp_len,
881                         ipmi_timeout);
882         return 0;
883 }
884
885 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
886                 uint8_t *bootdev, bool *persistent)
887 {
888         uint16_t resp_len;
889         uint8_t resp[8];
890         int rc;
891         uint8_t req[] = {
892                 0x05, /* parameter selector: boot flags */
893                 0x00, /* no set selector */
894                 0x00, /* no block selector */
895         };
896
897         resp_len = sizeof(resp);
898         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
899                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
900                         req, sizeof(req),
901                         resp, &resp_len,
902                         ipmi_timeout);
903         if (rc) {
904                 pb_log("platform: error reading IPMI boot options\n");
905                 return -1;
906         }
907
908         if (resp_len != sizeof(resp)) {
909                 pb_log("platform: unexpected length (%d) in "
910                                 "boot options response\n", resp_len);
911                 return -1;
912         }
913
914         pb_debug("IPMI get_bootdev response:\n");
915         for (int i = 0; i < resp_len; i++)
916                 pb_debug("%x ", resp[i]);
917         pb_debug("\n");
918
919         if (resp[0] != 0) {
920                 pb_log("platform: non-zero completion code %d from IPMI req\n",
921                                 resp[0]);
922                 return -1;
923         }
924
925         /* check for correct parameter version */
926         if ((resp[1] & 0xf) != 0x1) {
927                 pb_log("platform: unexpected version (0x%x) in "
928                                 "boot options response\n", resp[0]);
929                 return -1;
930         }
931
932         /* check for valid paramters */
933         if (resp[2] & 0x80) {
934                 pb_debug("platform: boot options are invalid/locked\n");
935                 return -1;
936         }
937
938         *persistent = false;
939
940         /* check for valid flags */
941         if (!(resp[3] & 0x80)) {
942                 pb_debug("platform: boot flags are invalid, ignoring\n");
943                 return -1;
944         }
945
946         *persistent = resp[3] & 0x40;
947         *bootdev = (resp[4] >> 2) & 0x0f;
948         return 0;
949 }
950
951 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
952 {
953         int sensor_number;
954         uint16_t resp_len;
955         uint8_t resp[1];
956         uint8_t req[] = {
957                 0x00, /* sensor number: os boot */
958                 0xA9, /* operation: set everything */
959                 0x00, /* sensor reading: none */
960                 0x40, /* assertion mask lsb: set state 6 */
961                 0x00, /* assertion mask msb: none */
962                 0x00, /* deassertion mask lsb: none */
963                 0x00, /* deassertion mask msb: none */
964                 0x00, /* event data 1: none */
965                 0x00, /* event data 2: none */
966                 0x00, /* event data 3: none */
967         };
968
969         sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
970         if (sensor_number < 0) {
971                 pb_log("Couldn't find OS boot sensor in device tree\n");
972                 return -1;
973         }
974
975         req[0] = sensor_number;
976
977         resp_len = sizeof(resp);
978
979         ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
980                         IPMI_CMD_SENSOR_SET,
981                         req, sizeof(req),
982                         resp, &resp_len,
983                         ipmi_timeout); return 0;
984
985         return 0;
986 }
987
988 static void get_ipmi_network_override(struct platform_powerpc *platform,
989                         struct config *config)
990 {
991         uint16_t min_len = 12, resp_len = 53, version;
992         const uint32_t magic_value = 0x21706221;
993         uint8_t resp[resp_len];
994         uint32_t cookie;
995         bool persistent;
996         int i, rc;
997         uint8_t req[] = {
998                 0x61, /* parameter selector: OEM section (network) */
999                 0x00, /* no set selector */
1000                 0x00, /* no block selector */
1001         };
1002
1003         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1004                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1005                         req, sizeof(req),
1006                         resp, &resp_len,
1007                         ipmi_timeout);
1008
1009         pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1010         if (resp_len > 0) {
1011                 for (i = 0; i < resp_len; i++) {
1012                         pb_debug(" %02x", resp[i]);
1013                         if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1014                                 pb_debug("\n");
1015                         else if (i && (i + 1) % 8 == 0)
1016                                 pb_debug(" ");
1017                 }
1018                 pb_debug("\n");
1019         }
1020
1021         if (rc) {
1022                 pb_debug("IPMI network config option unavailable\n");
1023                 return;
1024         }
1025
1026         if (resp_len < min_len) {
1027                 pb_debug("IPMI net response too small\n");
1028                 return;
1029         }
1030
1031         if (resp[0] != 0) {
1032                 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1033                        resp[0]);
1034                 return;
1035         }
1036
1037         /* Check for correct parameter version */
1038         if ((resp[1] & 0xf) != 0x1) {
1039                 pb_log("platform: unexpected version (0x%x) in network override response\n",
1040                        resp[0]);
1041                 return;
1042         }
1043
1044         /* Check that the parameters are valid */
1045         if (resp[2] & 0x80) {
1046                 pb_debug("platform: network override is invalid/locked\n");
1047                 return;
1048         }
1049
1050         /* Check for valid parameters in the boot flags section */
1051         if (!(resp[3] & 0x80)) {
1052                 pb_debug("platform: network override valid flag not set\n");
1053                 return;
1054         }
1055         /* Read the persistent flag; if it is set we need to save this config */
1056         persistent = resp[3] & 0x40;
1057         if (persistent)
1058                 pb_debug("platform: network override is persistent\n");
1059
1060         /* Check 4-byte cookie value */
1061         i = 4;
1062         memcpy(&cookie, &resp[i], sizeof(cookie));
1063         cookie = __be32_to_cpu(cookie);
1064         if (cookie != magic_value) {
1065                 pb_log_fn("Incorrect cookie %x\n", cookie);
1066                 return;
1067         }
1068         i += sizeof(cookie);
1069
1070         /* Check 2-byte version number */
1071         memcpy(&version, &resp[i], sizeof(version));
1072         version = __be16_to_cpu(version);
1073         i += sizeof(version);
1074         if (version != 1) {
1075                 pb_debug("Unexpected version: %u\n", version);
1076                 return;
1077         }
1078
1079         /* Interpret the rest of the interface config */
1080         rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1081
1082         if (!rc && persistent) {
1083                 /* Write this new config to NVRAM */
1084                 update_network_config(platform, config);
1085                 rc = write_nvram(platform);
1086                 if (rc)
1087                         pb_log("platform: Failed to save persistent interface override\n");
1088         }
1089 }
1090
1091 static void get_active_consoles(struct config *config)
1092 {
1093         struct stat sbuf;
1094         char *fsp_prop = NULL;
1095
1096         config->n_consoles = 2;
1097         config->consoles = talloc_array(config, char *, config->n_consoles);
1098         if (!config->consoles)
1099                 goto err;
1100
1101         config->consoles[0] = talloc_asprintf(config->consoles,
1102                                         "/dev/hvc0 [IPMI / Serial]");
1103         config->consoles[1] = talloc_asprintf(config->consoles,
1104                                         "/dev/tty1 [VGA]");
1105
1106         fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1107         if (stat(fsp_prop, &sbuf) == 0) {
1108                 /* FSP based machines also have a separate serial console */
1109                 config->consoles = talloc_realloc(config, config->consoles,
1110                                                 char *, config->n_consoles + 1);
1111                 if (!config->consoles)
1112                         goto err;
1113                 config->consoles[config->n_consoles++] = talloc_asprintf(
1114                                                 config->consoles,
1115                                                 "/dev/hvc1 [Serial]");
1116         }
1117
1118         return;
1119 err:
1120         config->n_consoles = 0;
1121         pb_log("Failed to allocate memory for consoles\n");
1122 }
1123
1124 static int load_config(struct platform *p, struct config *config)
1125 {
1126         struct platform_powerpc *platform = to_platform_powerpc(p);
1127         int rc;
1128
1129         rc = parse_nvram(platform);
1130         if (rc)
1131                 pb_log_fn("Failed to parse nvram\n");
1132
1133         populate_config(platform, config);
1134
1135         if (platform->get_ipmi_bootdev) {
1136                 bool bootdev_persistent;
1137                 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1138                 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1139                                 &bootdev_persistent);
1140                 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1141                         set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1142                 }
1143         }
1144
1145         if (platform->ipmi)
1146                 get_ipmi_network_override(platform, config);
1147
1148         get_active_consoles(config);
1149
1150         return 0;
1151 }
1152
1153 static int save_config(struct platform *p, struct config *config)
1154 {
1155         struct platform_powerpc *platform = to_platform_powerpc(p);
1156         struct config *defaults;
1157         int rc;
1158
1159         defaults = talloc_zero(platform, struct config);
1160         config_set_defaults(defaults);
1161
1162         rc = update_config(platform, config, defaults);
1163
1164         talloc_free(defaults);
1165         return rc;
1166 }
1167
1168 static void pre_boot(struct platform *p, const struct config *config)
1169 {
1170         struct platform_powerpc *platform = to_platform_powerpc(p);
1171
1172         if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1173                 platform->clear_ipmi_bootdev(platform, false);
1174
1175         if (platform->set_os_boot_sensor)
1176                 platform->set_os_boot_sensor(platform);
1177 }
1178
1179 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1180 {
1181         struct platform_powerpc *platform = p->platform_data;
1182         char *buf, *filename;
1183         int len, rc;
1184
1185         filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1186         rc = read_file(platform, filename, &buf, &len);
1187         if (rc == 0)
1188                 sysinfo->type = talloc_steal(sysinfo, buf);
1189         talloc_free(filename);
1190
1191         filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1192         rc = read_file(platform, filename, &buf, &len);
1193         if (rc == 0)
1194                 sysinfo->identifier = talloc_steal(sysinfo, buf);
1195         talloc_free(filename);
1196
1197         sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1198
1199         if (platform->ipmi) {
1200                 ipmi_get_bmc_mac(platform->ipmi, sysinfo->bmc_mac);
1201                 ipmi_get_bmc_versions(platform->ipmi, sysinfo);
1202         }
1203
1204         if (platform->get_platform_versions)
1205                 platform->get_platform_versions(sysinfo);
1206
1207         return 0;
1208 }
1209
1210 static bool probe(struct platform *p, void *ctx)
1211 {
1212         struct platform_powerpc *platform;
1213         struct stat statbuf;
1214         bool bmc_present;
1215         int rc;
1216
1217         /* we need a device tree */
1218         rc = stat("/proc/device-tree", &statbuf);
1219         if (rc)
1220                 return false;
1221
1222         if (!S_ISDIR(statbuf.st_mode))
1223                 return false;
1224
1225         platform = talloc_zero(ctx, struct platform_powerpc);
1226         list_init(&platform->params);
1227
1228         p->platform_data = platform;
1229
1230         bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1231
1232         if (ipmi_present() && bmc_present) {
1233                 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1234                 platform->ipmi = ipmi_open(platform);
1235                 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1236                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1237                 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1238         } else if (!stat(sysparams_dir, &statbuf)) {
1239                 pb_debug("platform: using sysparams for IPMI paramters\n");
1240                 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1241                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1242
1243         } else {
1244                 pb_log("platform: no IPMI parameter support\n");
1245         }
1246
1247         if (bmc_present)
1248                 platform->get_platform_versions = hostboot_load_versions;
1249
1250         return true;
1251 }
1252
1253
1254 static struct platform platform_powerpc = {
1255         .name                   = "powerpc",
1256         .dhcp_arch_id           = 0x000e,
1257         .probe                  = probe,
1258         .load_config            = load_config,
1259         .save_config            = save_config,
1260         .pre_boot               = pre_boot,
1261         .get_sysinfo            = get_sysinfo,
1262 };
1263
1264 register_platform(platform_powerpc);