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