]> git.ozlabs.org Git - petitboot/blob - discover/platform-powerpc.c
7370d7dcc20a21e78dacc57e2efc8ebe7e3cdb0f
[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 = 0;
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         unsigned int n_new = 0;
442         const char *val;
443         bool conflict;
444
445         /* Check for old-style bootdev */
446         val = get_param(platform, "petitboot,bootdev");
447         if (val && strlen(val)) {
448                 pos = talloc_strdup(config, val);
449                 if (!strncmp(val, "uuid:", strlen("uuid:")))
450                         old_dev = talloc_strdup(config,
451                                                 val + strlen("uuid:"));
452                 else if (!strncmp(val, "mac:", strlen("mac:")))
453                         old_dev = talloc_strdup(config,
454                                                 val + strlen("mac:"));
455         }
456
457         /* Check for ordered bootdevs */
458         val = get_param(platform, "petitboot,bootdevs");
459         if (!val || !strlen(val)) {
460                 pos = end = NULL;
461         } else {
462                 pos = talloc_strdup(config, val);
463                 end = strchr(pos, '\0');
464         }
465
466         while (pos && pos < end) {
467                 opt = talloc(config, struct autoboot_option);
468
469                 if (read_bootdev(config, &pos, opt)) {
470                         pb_log("bootdev config is in an unknown format "
471                                "(expected uuid:... or mac:...)\n");
472                         talloc_free(opt);
473                         continue;
474                 }
475
476                 new = talloc_realloc(config, new, struct autoboot_option,
477                                      n_new + 1);
478                 new[n_new] = *opt;
479                 n_new++;
480                 talloc_free(opt);
481
482         }
483
484         if (!n_new && !old_dev) {
485                 /* If autoboot has been disabled, clear the default options */
486                 if (!config->autoboot_enabled) {
487                         talloc_free(config->autoboot_opts);
488                         config->n_autoboot_opts = 0;
489                 }
490                 return;
491         }
492
493         conflict = old_dev && (!n_new ||
494                                     new[0].boot_type == BOOT_DEVICE_TYPE ||
495                                     /* Canonical UUIDs are 36 characters long */
496                                     strncmp(new[0].uuid, old_dev, 36));
497
498         if (!conflict) {
499                 talloc_free(config->autoboot_opts);
500                 config->autoboot_opts = new;
501                 config->n_autoboot_opts = n_new;
502                 return;
503         }
504
505         /*
506          * Difference detected, defer to old format in case it has been updated
507          * recently
508          */
509         pb_debug("Old autoboot bootdev detected\n");
510         talloc_free(config->autoboot_opts);
511         config->autoboot_opts = talloc(config, struct autoboot_option);
512         config->autoboot_opts[0].boot_type = BOOT_DEVICE_UUID;
513         config->autoboot_opts[0].uuid = talloc_strdup(config, old_dev);
514         config->n_autoboot_opts = 1;
515 }
516
517 static void populate_config(struct platform_powerpc *platform,
518                 struct config *config)
519 {
520         const char *val;
521         char *end;
522         unsigned long timeout;
523
524         /* if the "auto-boot?' property is present and "false", disable auto
525          * boot */
526         val = get_param(platform, "auto-boot?");
527         config->autoboot_enabled = !val || strcmp(val, "false");
528
529         val = get_param(platform, "petitboot,timeout");
530         if (val) {
531                 timeout = strtoul(val, &end, 10);
532                 if (end != val) {
533                         if (timeout >= INT_MAX)
534                                 timeout = INT_MAX;
535                         config->autoboot_timeout_sec = (int)timeout;
536                 }
537         }
538
539         val = get_param(platform, "petitboot,language");
540         config->lang = val ? talloc_strdup(config, val) : NULL;
541
542         populate_network_config(platform, config);
543
544         populate_bootdev_config(platform, config);
545
546         if (!config->debug) {
547                 val = get_param(platform, "petitboot,debug?");
548                 config->debug = val && !strcmp(val, "true");
549         }
550
551         val = get_param(platform, "petitboot,write?");
552         if (val)
553                 config->allow_writes = !strcmp(val, "true");
554
555         val = get_param(platform, "petitboot,snapshots?");
556         if (val)
557                 config->disable_snapshots = !strcmp(val, "false");
558 }
559
560 static char *iface_config_str(void *ctx, struct interface_config *config)
561 {
562         char *str;
563
564         /* todo: HWADDR size is hardcoded as 6, but we may need to handle
565          * different hardware address formats */
566         str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
567                         config->hwaddr[0], config->hwaddr[1],
568                         config->hwaddr[2], config->hwaddr[3],
569                         config->hwaddr[4], config->hwaddr[5]);
570
571         if (config->ignore) {
572                 str = talloc_asprintf_append(str, "ignore");
573
574         } else if (config->method == CONFIG_METHOD_DHCP) {
575                 str = talloc_asprintf_append(str, "dhcp");
576
577         } else if (config->method == CONFIG_METHOD_STATIC) {
578                 str = talloc_asprintf_append(str, "static,%s%s%s",
579                                 config->static_config.address,
580                                 config->static_config.gateway ? "," : "",
581                                 config->static_config.gateway ?: "");
582         }
583         return str;
584 }
585
586 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
587 {
588         char *str;
589         int i;
590
591         str = talloc_strdup(ctx, "dns,");
592         for (i = 0; i < n; i++) {
593                 str = talloc_asprintf_append(str, "%s%s",
594                                 i == 0 ? "" : ",",
595                                 dns_servers[i]);
596         }
597
598         return str;
599 }
600
601 static void update_string_config(struct platform_powerpc *platform,
602                 const char *name, const char *value)
603 {
604         const char *cur;
605
606         cur = get_param(platform, name);
607
608         /* don't set an empty parameter if it doesn't already exist */
609         if (!cur && !strlen(value))
610                 return;
611
612         set_param(platform, name, value);
613 }
614
615 static void update_network_config(struct platform_powerpc *platform,
616         struct config *config)
617 {
618         unsigned int i;
619         char *val;
620
621         val = talloc_strdup(platform, "");
622
623         for (i = 0; i < config->network.n_interfaces; i++) {
624                 char *iface_str = iface_config_str(platform,
625                                         config->network.interfaces[i]);
626                 val = talloc_asprintf_append(val, "%s%s",
627                                 *val == '\0' ? "" : " ", iface_str);
628                 talloc_free(iface_str);
629         }
630
631         if (config->network.n_dns_servers) {
632                 char *dns_str = dns_config_str(platform,
633                                                 config->network.dns_servers,
634                                                 config->network.n_dns_servers);
635                 val = talloc_asprintf_append(val, "%s%s",
636                                 *val == '\0' ? "" : " ", dns_str);
637                 talloc_free(dns_str);
638         }
639
640         update_string_config(platform, "petitboot,network", val);
641
642         talloc_free(val);
643 }
644
645 static void update_bootdev_config(struct platform_powerpc *platform,
646                 struct config *config)
647 {
648         char *val = NULL, *boot_str = NULL, *tmp = NULL, *first = NULL;
649         struct autoboot_option *opt;
650         const char delim = ' ';
651         unsigned int i;
652
653         if (!config->n_autoboot_opts)
654                 first = val = "";
655         else if (config->autoboot_opts[0].boot_type == BOOT_DEVICE_UUID)
656                 first = talloc_asprintf(config, "uuid:%s",
657                                         config->autoboot_opts[0].uuid);
658         else
659                 first = "";
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         update_string_config(platform, "petitboot,bootdev", first);
679         talloc_free(tmp);
680         if (boot_str)
681                 talloc_free(boot_str);
682 }
683
684 static int update_config(struct platform_powerpc *platform,
685                 struct config *config, struct config *defaults)
686 {
687         char *tmp = NULL;
688         const char *val;
689
690         if (config->autoboot_enabled == defaults->autoboot_enabled)
691                 val = "";
692         else
693                 val = config->autoboot_enabled ? "true" : "false";
694         update_string_config(platform, "auto-boot?", val);
695
696         if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
697                 val = "";
698         else
699                 val = tmp = talloc_asprintf(platform, "%d",
700                                 config->autoboot_timeout_sec);
701
702         if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
703             platform->clear_ipmi_bootdev) {
704                 platform->clear_ipmi_bootdev(platform,
705                                 config->ipmi_bootdev_persistent);
706                 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
707                 config->ipmi_bootdev_persistent = false;
708         }
709
710         update_string_config(platform, "petitboot,timeout", val);
711         if (tmp)
712                 talloc_free(tmp);
713
714         val = config->lang ?: "";
715         update_string_config(platform, "petitboot,language", val);
716
717         if (config->allow_writes == defaults->allow_writes)
718                 val = "";
719         else
720                 val = config->allow_writes ? "true" : "false";
721         update_string_config(platform, "petitboot,write?", val);
722
723         update_network_config(platform, config);
724
725         update_bootdev_config(platform, config);
726
727         return write_nvram(platform);
728 }
729
730 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
731                 bool persistent)
732 {
733         config->ipmi_bootdev = bootdev;
734         config->ipmi_bootdev_persistent = persistent;
735
736         switch (bootdev) {
737         case IPMI_BOOTDEV_NONE:
738         case IPMI_BOOTDEV_DISK:
739         case IPMI_BOOTDEV_NETWORK:
740         case IPMI_BOOTDEV_CDROM:
741         default:
742                 break;
743         case IPMI_BOOTDEV_SETUP:
744                 config->autoboot_enabled = false;
745                 break;
746         case IPMI_BOOTDEV_SAFE:
747                 config->autoboot_enabled = false;
748                 config->safe_mode = true;
749                 break;
750         }
751 }
752
753 static int read_bootdev_sysparam(const char *name, uint8_t *val)
754 {
755         uint8_t buf[2];
756         char path[50];
757         int fd, rc;
758
759         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
760         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
761
762         fd = open(path, O_RDONLY);
763         if (fd < 0) {
764                 pb_debug("powerpc: can't access sysparam %s\n",
765                                 name);
766                 return -1;
767         }
768
769         rc = read(fd, buf, sizeof(buf));
770
771         close(fd);
772
773         /* bootdev definitions should only be one byte in size */
774         if (rc != 1) {
775                 pb_debug("powerpc: sysparam %s read returned %d\n",
776                                 name, rc);
777                 return -1;
778         }
779
780         pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
781
782         if (!ipmi_bootdev_is_valid(buf[0]))
783                 return -1;
784
785         *val = buf[0];
786         return 0;
787 }
788
789 static int write_bootdev_sysparam(const char *name, uint8_t val)
790 {
791         char path[50];
792         int fd, rc;
793
794         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
795         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
796
797         fd = open(path, O_WRONLY);
798         if (fd < 0) {
799                 pb_debug("powerpc: can't access sysparam %s for writing\n",
800                                 name);
801                 return -1;
802         }
803
804         for (;;) {
805                 errno = 0;
806                 rc = write(fd, &val, sizeof(val));
807                 if (rc == sizeof(val)) {
808                         rc = 0;
809                         break;
810                 }
811
812                 if (rc <= 0 && errno != EINTR) {
813                         pb_log("powerpc: error updating sysparam %s: %s",
814                                         name, strerror(errno));
815                         rc = -1;
816                         break;
817                 }
818         }
819
820         close(fd);
821
822         if (!rc)
823                 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
824
825         return rc;
826 }
827
828 static int clear_ipmi_bootdev_sysparams(
829                 struct platform_powerpc *platform __attribute__((unused)),
830                 bool persistent)
831 {
832         if (persistent) {
833                 /* invalidate default-boot-device setting */
834                 write_bootdev_sysparam("default-boot-device", 0xff);
835         } else {
836                 /* invalidate next-boot-device setting */
837                 write_bootdev_sysparam("next-boot-device", 0xff);
838         }
839         return 0;
840 }
841
842 static int get_ipmi_bootdev_sysparams(
843                 struct platform_powerpc *platform __attribute__((unused)),
844                 uint8_t *bootdev, bool *persistent)
845 {
846         uint8_t next_bootdev, default_bootdev;
847         bool next_valid, default_valid;
848         int rc;
849
850         rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
851         next_valid = rc == 0;
852
853         rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
854         default_valid = rc == 0;
855
856         /* nothing valid? no need to change the config */
857         if (!next_valid && !default_valid)
858                 return -1;
859
860         *persistent = !next_valid;
861         *bootdev = next_valid ? next_bootdev : default_bootdev;
862         return 0;
863 }
864
865 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
866                                    bool persistent __attribute__((unused)))
867 {
868         uint16_t resp_len;
869         uint8_t resp[1];
870         uint8_t req[] = {
871                 0x05, /* parameter selector: boot flags */
872                 0x80, /* data 1: valid */
873                 0x00, /* data 2: bootdev: no override */
874                 0x00, /* data 3: system defaults */
875                 0x00, /* data 4: no request for shared mode, mux defaults */
876                 0x00, /* data 5: no instance request */
877         };
878
879         resp_len = sizeof(resp);
880
881         ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
882                         IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
883                         req, sizeof(req),
884                         resp, &resp_len,
885                         ipmi_timeout);
886         return 0;
887 }
888
889 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
890                 uint8_t *bootdev, bool *persistent)
891 {
892         uint16_t resp_len;
893         uint8_t resp[8];
894         int rc;
895         uint8_t req[] = {
896                 0x05, /* parameter selector: boot flags */
897                 0x00, /* no set selector */
898                 0x00, /* no block selector */
899         };
900
901         resp_len = sizeof(resp);
902         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
903                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
904                         req, sizeof(req),
905                         resp, &resp_len,
906                         ipmi_timeout);
907         if (rc) {
908                 pb_log("platform: error reading IPMI boot options\n");
909                 return -1;
910         }
911
912         if (resp_len != sizeof(resp)) {
913                 pb_log("platform: unexpected length (%d) in "
914                                 "boot options response\n", resp_len);
915                 return -1;
916         }
917
918         if (resp[0] != 0) {
919                 pb_log("platform: non-zero completion code %d from IPMI req\n",
920                                 resp[0]);
921                 return -1;
922         }
923
924         /* check for correct parameter version */
925         if ((resp[1] & 0xf) != 0x1) {
926                 pb_log("platform: unexpected version (0x%x) in "
927                                 "boot options response\n", resp[0]);
928                 return -1;
929         }
930
931         /* check for valid paramters */
932         if (resp[2] & 0x80) {
933                 pb_debug("platform: boot options are invalid/locked\n");
934                 return -1;
935         }
936
937         *persistent = false;
938
939         /* check for valid flags */
940         if (!(resp[3] & 0x80)) {
941                 pb_debug("platform: boot flags are invalid, ignoring\n");
942                 return 0;
943         }
944
945         *persistent = resp[3] & 0x40;
946         *bootdev = (resp[4] >> 2) & 0x0f;
947         return 0;
948 }
949
950 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
951 {
952         int sensor_number;
953         uint16_t resp_len;
954         uint8_t resp[1];
955         uint8_t req[] = {
956                 0x00, /* sensor number: os boot */
957                 0xA9, /* operation: set everything */
958                 0x00, /* sensor reading: none */
959                 0x40, /* assertion mask lsb: set state 6 */
960                 0x00, /* assertion mask msb: none */
961                 0x00, /* deassertion mask lsb: none */
962                 0x00, /* deassertion mask msb: none */
963                 0x00, /* event data 1: none */
964                 0x00, /* event data 2: none */
965                 0x00, /* event data 3: none */
966         };
967
968         sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
969         if (sensor_number < 0) {
970                 pb_log("Couldn't find OS boot sensor in device tree\n");
971                 return -1;
972         }
973
974         req[0] = sensor_number;
975
976         resp_len = sizeof(resp);
977
978         ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
979                         IPMI_CMD_SENSOR_SET,
980                         req, sizeof(req),
981                         resp, &resp_len,
982                         ipmi_timeout); return 0;
983
984         return 0;
985 }
986
987 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
988 {
989         struct platform_powerpc *platform = p->platform_data;
990         uint16_t resp_len = 8;
991         uint8_t resp[8];
992         uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
993         int i, rc;
994
995         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
996                         IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
997                         req, sizeof(req),
998                         resp, &resp_len,
999                         ipmi_timeout);
1000
1001         pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1002
1003         if (rc == 0 && resp_len > 0) {
1004                 for (i = 2; i < resp_len; i++) {
1005                         pb_debug(" %x", resp[i]);
1006                         buf[i - 2] = resp[i];
1007                 }
1008                 pb_debug("\n");
1009         }
1010 }
1011
1012 static int load_config(struct platform *p, struct config *config)
1013 {
1014         struct platform_powerpc *platform = to_platform_powerpc(p);
1015         int rc;
1016
1017         rc = parse_nvram(platform);
1018         if (rc)
1019                 return rc;
1020
1021         populate_config(platform, config);
1022
1023         if (platform->get_ipmi_bootdev) {
1024                 bool bootdev_persistent;
1025                 uint8_t bootdev;
1026                 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1027                                 &bootdev_persistent);
1028                 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1029                         set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1030                 }
1031         }
1032
1033         return 0;
1034 }
1035
1036 static int save_config(struct platform *p, struct config *config)
1037 {
1038         struct platform_powerpc *platform = to_platform_powerpc(p);
1039         struct config *defaults;
1040         int rc;
1041
1042         defaults = talloc_zero(platform, struct config);
1043         config_set_defaults(defaults);
1044
1045         rc = update_config(platform, config, defaults);
1046
1047         talloc_free(defaults);
1048         return rc;
1049 }
1050
1051 static void pre_boot(struct platform *p, const struct config *config)
1052 {
1053         struct platform_powerpc *platform = to_platform_powerpc(p);
1054
1055         if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1056                 platform->clear_ipmi_bootdev(platform, false);
1057
1058         if (platform->set_os_boot_sensor)
1059                 platform->set_os_boot_sensor(platform);
1060 }
1061
1062 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1063 {
1064         struct platform_powerpc *platform = p->platform_data;
1065         char *buf, *filename;
1066         int len, rc;
1067
1068         filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1069         rc = read_file(platform, filename, &buf, &len);
1070         if (rc == 0)
1071                 sysinfo->type = talloc_steal(sysinfo, buf);
1072         talloc_free(filename);
1073
1074         filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1075         rc = read_file(platform, filename, &buf, &len);
1076         if (rc == 0)
1077                 sysinfo->identifier = talloc_steal(sysinfo, buf);
1078         talloc_free(filename);
1079
1080         sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1081         if (platform->ipmi)
1082                 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1083
1084         return 0;
1085 }
1086
1087 static bool probe(struct platform *p, void *ctx)
1088 {
1089         struct platform_powerpc *platform;
1090         struct stat statbuf;
1091         int rc;
1092
1093         /* we need a device tree */
1094         rc = stat("/proc/device-tree", &statbuf);
1095         if (rc)
1096                 return false;
1097
1098         if (!S_ISDIR(statbuf.st_mode))
1099                 return false;
1100
1101         platform = talloc_zero(ctx, struct platform_powerpc);
1102         list_init(&platform->params);
1103
1104         p->platform_data = platform;
1105
1106         if (ipmi_present()) {
1107                 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1108                 platform->ipmi = ipmi_open(platform);
1109                 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1110                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1111                 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1112         } else if (!stat(sysparams_dir, &statbuf)) {
1113                 pb_debug("platform: using sysparams for IPMI paramters\n");
1114                 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1115                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1116
1117         } else {
1118                 pb_log("platform: no IPMI parameter support\n");
1119         }
1120
1121         return true;
1122 }
1123
1124
1125 static struct platform platform_powerpc = {
1126         .name                   = "powerpc",
1127         .dhcp_arch_id           = 0x000e,
1128         .probe                  = probe,
1129         .load_config            = load_config,
1130         .save_config            = save_config,
1131         .pre_boot               = pre_boot,
1132         .get_sysinfo            = get_sysinfo,
1133 };
1134
1135 register_platform(platform_powerpc);