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