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