]> git.ozlabs.org Git - petitboot/blob - discover/platform-powerpc.c
ui/ncurses: Make server connect message more clear
[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 #include <asm/byteorder.h>
12
13 #include <file/file.h>
14 #include <talloc/talloc.h>
15 #include <list/list.h>
16 #include <log/log.h>
17 #include <process/process.h>
18 #include <types/types.h>
19
20 #include "hostboot.h"
21 #include "platform.h"
22 #include "ipmi.h"
23 #include "dt.h"
24
25 static const char *partition = "common";
26 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
27 static const char *devtree_dir = "/proc/device-tree/";
28 static const int ipmi_timeout = 5000; /* milliseconds. */
29
30 struct param {
31         char                    *name;
32         char                    *value;
33         bool                    modified;
34         struct list_item        list;
35 };
36
37 struct platform_powerpc {
38         struct list     params;
39         struct ipmi     *ipmi;
40         bool            ipmi_bootdev_persistent;
41         int             (*get_ipmi_bootdev)(
42                                 struct platform_powerpc *platform,
43                                 uint8_t *bootdev, bool *persistent);
44         int             (*clear_ipmi_bootdev)(
45                                 struct platform_powerpc *platform,
46                                 bool persistent);
47         int             (*set_os_boot_sensor)(
48                                 struct platform_powerpc *platform);
49         void            (*get_platform_versions)(struct system_info *info);
50 };
51
52 static const char *known_params[] = {
53         "auto-boot?",
54         "petitboot,network",
55         "petitboot,timeout",
56         "petitboot,bootdev",
57         "petitboot,bootdevs",
58         "petitboot,language",
59         "petitboot,debug?",
60         "petitboot,write?",
61         "petitboot,snapshots?",
62         "petitboot,tty",
63         NULL,
64 };
65
66 #define to_platform_powerpc(p) \
67         (struct platform_powerpc *)(p->platform_data)
68
69 /* a partition max a max size of 64k * 16bytes = 1M */
70 static const int max_partition_size = 64 * 1024 * 16;
71
72 static bool param_is_known(const char *param, unsigned int len)
73 {
74         const char *known_param;
75         unsigned int i;
76
77         for (i = 0; known_params[i]; i++) {
78                 known_param = known_params[i];
79                 if (len == strlen(known_param) &&
80                                 !strncmp(param, known_param, len))
81                         return true;
82         }
83
84         return false;
85 }
86
87 static int parse_nvram_params(struct platform_powerpc *platform,
88                 char *buf, int len)
89 {
90         char *pos, *name, *value;
91         unsigned int paramlen;
92         int i, count;
93
94         /* discard 2 header lines:
95          * "common" partiton"
96          * ------------------
97          */
98         pos = buf;
99         count = 0;
100
101         for (i = 0; i < len; i++) {
102                 if (pos[i] == '\n')
103                         count++;
104                 if (count == 2)
105                         break;
106         }
107
108         if (i == len) {
109                 fprintf(stderr, "failure parsing nvram output\n");
110                 return -1;
111         }
112
113         for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
114                 unsigned int namelen;
115                 struct param *param;
116                 char *newline;
117
118                 newline = strchr(pos, '\n');
119                 if (!newline)
120                         break;
121
122                 *newline = '\0';
123
124                 paramlen = strlen(pos);
125
126                 name = pos;
127                 value = strchr(pos, '=');
128                 if (!value)
129                         continue;
130
131                 namelen = value - name;
132                 if (namelen == 0)
133                         continue;
134
135                 if (!param_is_known(name, namelen))
136                         continue;
137
138                 value++;
139
140                 param = talloc(platform, struct param);
141                 param->modified = false;
142                 param->name = talloc_strndup(platform, name, namelen);
143                 param->value = talloc_strdup(platform, value);
144                 list_add(&platform->params, &param->list);
145         }
146
147         return 0;
148 }
149
150 static int parse_nvram(struct platform_powerpc *platform)
151 {
152         struct process *process;
153         const char *argv[5];
154         int rc;
155
156         argv[0] = "nvram";
157         argv[1] = "--print-config";
158         argv[2] = "--partition";
159         argv[3] = partition;
160         argv[4] = NULL;
161
162         process = process_create(platform);
163         process->path = "nvram";
164         process->argv = argv;
165         process->keep_stdout = true;
166
167         rc = process_run_sync(process);
168
169         if (rc || !process_exit_ok(process)) {
170                 fprintf(stderr, "nvram process returned "
171                                 "non-zero exit status\n");
172                 rc = -1;
173         } else {
174                 rc = parse_nvram_params(platform, process->stdout_buf,
175                                             process->stdout_len);
176         }
177
178         process_release(process);
179         return rc;
180 }
181
182 static int write_nvram(struct platform_powerpc *platform)
183 {
184         struct process *process;
185         struct param *param;
186         const char *argv[6];
187         int rc = 0;
188
189         argv[0] = "nvram";
190         argv[1] = "--update-config";
191         argv[2] = NULL;
192         argv[3] = "--partition";
193         argv[4] = partition;
194         argv[5] = NULL;
195
196         process = process_create(platform);
197         process->path = "nvram";
198         process->argv = argv;
199
200         list_for_each_entry(&platform->params, param, list) {
201                 char *paramstr;
202
203                 if (!param->modified)
204                         continue;
205
206                 paramstr = talloc_asprintf(platform, "%s=%s",
207                                 param->name, param->value);
208                 argv[2] = paramstr;
209
210                 rc = process_run_sync(process);
211
212                 talloc_free(paramstr);
213
214                 if (rc || !process_exit_ok(process)) {
215                         rc = -1;
216                         pb_log("nvram update process returned "
217                                         "non-zero exit status\n");
218                         break;
219                 }
220         }
221
222         process_release(process);
223         return rc;
224 }
225
226 static const char *get_param(struct platform_powerpc *platform,
227                 const char *name)
228 {
229         struct param *param;
230
231         list_for_each_entry(&platform->params, param, list)
232                 if (!strcmp(param->name, name))
233                         return param->value;
234         return NULL;
235 }
236
237 static void set_param(struct platform_powerpc *platform, const char *name,
238                 const char *value)
239 {
240         struct param *param;
241
242         list_for_each_entry(&platform->params, param, list) {
243                 if (strcmp(param->name, name))
244                         continue;
245
246                 if (!strcmp(param->value, value))
247                         return;
248
249                 talloc_free(param->value);
250                 param->value = talloc_strdup(param, value);
251                 param->modified = true;
252                 return;
253         }
254
255
256         param = talloc(platform, struct param);
257         param->modified = true;
258         param->name = talloc_strdup(platform, name);
259         param->value = talloc_strdup(platform, value);
260         list_add(&platform->params, &param->list);
261 }
262
263 static int parse_hwaddr(struct interface_config *ifconf, char *str)
264 {
265         int i;
266
267         if (strlen(str) != strlen("00:00:00:00:00:00"))
268                 return -1;
269
270         for (i = 0; i < HWADDR_SIZE; i++) {
271                 char byte[3], *endp;
272                 unsigned long x;
273
274                 byte[0] = str[i * 3 + 0];
275                 byte[1] = str[i * 3 + 1];
276                 byte[2] = '\0';
277
278                 x = strtoul(byte, &endp, 16);
279                 if (endp != byte + 2)
280                         return -1;
281
282                 ifconf->hwaddr[i] = x & 0xff;
283         }
284
285         return 0;
286 }
287
288 static int parse_one_interface_config(struct config *config,
289                 char *confstr)
290 {
291         struct interface_config *ifconf;
292         char *tok, *saveptr;
293
294         ifconf = talloc_zero(config, struct interface_config);
295
296         if (!confstr || !strlen(confstr))
297                 goto out_err;
298
299         /* first token should be the mac address */
300         tok = strtok_r(confstr, ",", &saveptr);
301         if (!tok)
302                 goto out_err;
303
304         if (parse_hwaddr(ifconf, tok))
305                 goto out_err;
306
307         /* second token is the method */
308         tok = strtok_r(NULL, ",", &saveptr);
309         if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
310                 ifconf->ignore = true;
311
312         } else if (!strcmp(tok, "dhcp")) {
313                 ifconf->method = CONFIG_METHOD_DHCP;
314
315         } else if (!strcmp(tok, "static")) {
316                 ifconf->method = CONFIG_METHOD_STATIC;
317
318                 /* ip/mask, [optional] gateway, [optional] url */
319                 tok = strtok_r(NULL, ",", &saveptr);
320                 if (!tok)
321                         goto out_err;
322                 ifconf->static_config.address =
323                         talloc_strdup(ifconf, tok);
324
325                 tok = strtok_r(NULL, ",", &saveptr);
326                 if (tok) {
327                         ifconf->static_config.gateway =
328                                 talloc_strdup(ifconf, tok);
329                 }
330
331                 tok = strtok_r(NULL, ",", &saveptr);
332                 if (tok) {
333                         ifconf->static_config.url =
334                                 talloc_strdup(ifconf, tok);
335                 }
336
337         } else {
338                 pb_log("Unknown network configuration method %s\n", tok);
339                 goto out_err;
340         }
341
342         config->network.interfaces = talloc_realloc(config,
343                         config->network.interfaces,
344                         struct interface_config *,
345                         ++config->network.n_interfaces);
346
347         config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
348
349         return 0;
350 out_err:
351         talloc_free(ifconf);
352         return -1;
353 }
354
355 static int parse_one_dns_config(struct config *config,
356                 char *confstr)
357 {
358         char *tok, *saveptr = NULL;
359
360         for (tok = strtok_r(confstr, ",", &saveptr); tok;
361                         tok = strtok_r(NULL, ",", &saveptr)) {
362
363                 char *server = talloc_strdup(config, tok);
364
365                 config->network.dns_servers = talloc_realloc(config,
366                                 config->network.dns_servers, const char *,
367                                 ++config->network.n_dns_servers);
368
369                 config->network.dns_servers[config->network.n_dns_servers - 1]
370                                 = server;
371         }
372
373         return 0;
374 }
375
376 static void populate_network_config(struct platform_powerpc *platform,
377                 struct config *config)
378 {
379         char *val, *saveptr = NULL;
380         const char *cval;
381         int i;
382
383         cval = get_param(platform, "petitboot,network");
384         if (!cval || !strlen(cval))
385                 return;
386
387         val = talloc_strdup(config, cval);
388
389         for (i = 0; ; i++) {
390                 char *tok;
391
392                 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
393                 if (!tok)
394                         break;
395
396                 if (!strncasecmp(tok, "dns,", strlen("dns,")))
397                         parse_one_dns_config(config, tok + strlen("dns,"));
398                 else
399                         parse_one_interface_config(config, tok);
400
401         }
402
403         talloc_free(val);
404 }
405
406 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
407 {
408         char *delim = strchr(*pos, ' ');
409         int len, prefix = 0, rc = -1;
410         enum device_type type;
411
412         if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
413                 prefix = strlen("uuid:");
414                 opt->boot_type = BOOT_DEVICE_UUID;
415         } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
416                 prefix = strlen("mac:");
417                 opt->boot_type = BOOT_DEVICE_UUID;
418         } else {
419                 type = find_device_type(*pos);
420                 if (type != DEVICE_TYPE_UNKNOWN) {
421                         opt->type = type;
422                         opt->boot_type = BOOT_DEVICE_TYPE;
423                         rc = 0;
424                 }
425         }
426
427         if (opt->boot_type == BOOT_DEVICE_UUID) {
428                 if (delim)
429                         len = (int)(delim - *pos) - prefix;
430                 else
431                         len = strlen(*pos) - prefix;
432
433                 if (len) {
434                         opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
435                         rc = 0;
436                 }
437         }
438
439         /* Always advance pointer to next option or end */
440         if (delim)
441                 *pos = delim + 1;
442         else
443                 *pos += strlen(*pos);
444
445         return rc;
446 }
447
448 static void populate_bootdev_config(struct platform_powerpc *platform,
449                 struct config *config)
450 {
451         struct autoboot_option *opt, *new = NULL;
452         char *pos, *end, *old_dev = NULL;
453         unsigned int n_new = 0;
454         const char *val;
455         bool conflict;
456         size_t len = 0;
457
458         /* Check for old-style bootdev */
459         val = get_param(platform, "petitboot,bootdev");
460         if (val && strlen(val)) {
461                 pos = talloc_strdup(config, val);
462                 if (!strncmp(val, "uuid:", strlen("uuid:")))
463                         len = strlen("uuid:");
464                 else if (!strncmp(val, "mac:", strlen("mac:")))
465                         len = strlen("mac:");
466                 /* Make sure someone hasn't set a blank UUID */
467                 if (len && *(val + len) != '\0')
468                         old_dev = talloc_strdup(config, val + len);
469         }
470
471         /* Check for ordered bootdevs */
472         val = get_param(platform, "petitboot,bootdevs");
473         if (!val || !strlen(val)) {
474                 pos = end = NULL;
475         } else {
476                 pos = talloc_strdup(config, val);
477                 end = strchr(pos, '\0');
478         }
479
480         while (pos && pos < end) {
481                 opt = talloc(config, struct autoboot_option);
482
483                 if (read_bootdev(config, &pos, opt)) {
484                         pb_log("bootdev config is in an unknown format "
485                                "(expected uuid:... or mac:...)\n");
486                         talloc_free(opt);
487                         continue;
488                 }
489
490                 new = talloc_realloc(config, new, struct autoboot_option,
491                                      n_new + 1);
492                 new[n_new] = *opt;
493                 n_new++;
494                 talloc_free(opt);
495
496         }
497
498         if (!n_new && !old_dev) {
499                 /* If autoboot has been disabled, clear the default options */
500                 if (!config->autoboot_enabled) {
501                         talloc_free(config->autoboot_opts);
502                         config->n_autoboot_opts = 0;
503                 }
504                 return;
505         }
506
507         conflict = old_dev && (!n_new ||
508                                     new[0].boot_type == BOOT_DEVICE_TYPE ||
509                                     /* Canonical UUIDs are 36 characters long */
510                                     strncmp(new[0].uuid, old_dev, 36));
511
512         if (!conflict) {
513                 talloc_free(config->autoboot_opts);
514                 config->autoboot_opts = new;
515                 config->n_autoboot_opts = n_new;
516                 return;
517         }
518
519         /*
520          * Difference detected, defer to old format in case it has been updated
521          * recently
522          */
523         pb_debug("Old autoboot bootdev detected\n");
524         talloc_free(config->autoboot_opts);
525         config->autoboot_opts = talloc(config, struct autoboot_option);
526         config->autoboot_opts[0].boot_type = BOOT_DEVICE_UUID;
527         config->autoboot_opts[0].uuid = talloc_strdup(config, old_dev);
528         config->n_autoboot_opts = 1;
529 }
530
531 static void populate_config(struct platform_powerpc *platform,
532                 struct config *config)
533 {
534         const char *val;
535         char *end;
536         unsigned long timeout;
537
538         /* if the "auto-boot?' property is present and "false", disable auto
539          * boot */
540         val = get_param(platform, "auto-boot?");
541         config->autoboot_enabled = !val || strcmp(val, "false");
542
543         val = get_param(platform, "petitboot,timeout");
544         if (val) {
545                 timeout = strtoul(val, &end, 10);
546                 if (end != val) {
547                         if (timeout >= INT_MAX)
548                                 timeout = INT_MAX;
549                         config->autoboot_timeout_sec = (int)timeout;
550                 }
551         }
552
553         val = get_param(platform, "petitboot,language");
554         config->lang = val ? talloc_strdup(config, val) : NULL;
555
556         populate_network_config(platform, config);
557
558         populate_bootdev_config(platform, config);
559
560         if (!config->debug) {
561                 val = get_param(platform, "petitboot,debug?");
562                 config->debug = val && !strcmp(val, "true");
563         }
564
565         val = get_param(platform, "petitboot,write?");
566         if (val)
567                 config->allow_writes = !strcmp(val, "true");
568
569         val = get_param(platform, "petitboot,snapshots?");
570         if (val)
571                 config->disable_snapshots = !strcmp(val, "false");
572
573         val = get_param(platform, "petitboot,tty");
574         if (val)
575                 config->boot_tty = talloc_strdup(config, val);
576 }
577
578 static char *iface_config_str(void *ctx, struct interface_config *config)
579 {
580         char *str;
581
582         /* todo: HWADDR size is hardcoded as 6, but we may need to handle
583          * different hardware address formats */
584         str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
585                         config->hwaddr[0], config->hwaddr[1],
586                         config->hwaddr[2], config->hwaddr[3],
587                         config->hwaddr[4], config->hwaddr[5]);
588
589         if (config->ignore) {
590                 str = talloc_asprintf_append(str, "ignore");
591
592         } else if (config->method == CONFIG_METHOD_DHCP) {
593                 str = talloc_asprintf_append(str, "dhcp");
594
595         } else if (config->method == CONFIG_METHOD_STATIC) {
596                 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
597                                 config->static_config.address,
598                                 config->static_config.gateway ? "," : "",
599                                 config->static_config.gateway ?: "",
600                                 config->static_config.url ? "," : "",
601                                 config->static_config.url ?: "");
602         }
603         return str;
604 }
605
606 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
607 {
608         char *str;
609         int i;
610
611         str = talloc_strdup(ctx, "dns,");
612         for (i = 0; i < n; i++) {
613                 str = talloc_asprintf_append(str, "%s%s",
614                                 i == 0 ? "" : ",",
615                                 dns_servers[i]);
616         }
617
618         return str;
619 }
620
621 static void update_string_config(struct platform_powerpc *platform,
622                 const char *name, const char *value)
623 {
624         const char *cur;
625
626         cur = get_param(platform, name);
627
628         /* don't set an empty parameter if it doesn't already exist */
629         if (!cur && !strlen(value))
630                 return;
631
632         set_param(platform, name, value);
633 }
634
635 static void update_network_config(struct platform_powerpc *platform,
636         struct config *config)
637 {
638         unsigned int i;
639         char *val;
640
641         val = talloc_strdup(platform, "");
642
643         for (i = 0; i < config->network.n_interfaces; i++) {
644                 char *iface_str = iface_config_str(platform,
645                                         config->network.interfaces[i]);
646                 val = talloc_asprintf_append(val, "%s%s",
647                                 *val == '\0' ? "" : " ", iface_str);
648                 talloc_free(iface_str);
649         }
650
651         if (config->network.n_dns_servers) {
652                 char *dns_str = dns_config_str(platform,
653                                                 config->network.dns_servers,
654                                                 config->network.n_dns_servers);
655                 val = talloc_asprintf_append(val, "%s%s",
656                                 *val == '\0' ? "" : " ", dns_str);
657                 talloc_free(dns_str);
658         }
659
660         update_string_config(platform, "petitboot,network", val);
661
662         talloc_free(val);
663 }
664
665 static void update_bootdev_config(struct platform_powerpc *platform,
666                 struct config *config)
667 {
668         char *val = NULL, *boot_str = NULL, *tmp = NULL, *first = NULL;
669         struct autoboot_option *opt;
670         const char delim = ' ';
671         unsigned int i;
672
673         if (!config->n_autoboot_opts)
674                 first = val = "";
675         else if (config->autoboot_opts[0].boot_type == BOOT_DEVICE_UUID)
676                 first = talloc_asprintf(config, "uuid:%s",
677                                         config->autoboot_opts[0].uuid);
678         else
679                 first = "";
680
681         for (i = 0; i < config->n_autoboot_opts; i++) {
682                 opt = &config->autoboot_opts[i];
683                 switch (opt->boot_type) {
684                         case BOOT_DEVICE_TYPE:
685                                 boot_str = talloc_asprintf(config, "%s%c",
686                                                 device_type_name(opt->type),
687                                                 delim);
688                                 break;
689                         case BOOT_DEVICE_UUID:
690                                 boot_str = talloc_asprintf(config, "uuid:%s%c",
691                                                 opt->uuid, delim);
692                                 break;
693                         }
694                         tmp = val = talloc_asprintf_append(val, "%s", boot_str);
695         }
696
697         update_string_config(platform, "petitboot,bootdevs", val);
698         update_string_config(platform, "petitboot,bootdev", first);
699         talloc_free(tmp);
700         if (boot_str)
701                 talloc_free(boot_str);
702 }
703
704 static int update_config(struct platform_powerpc *platform,
705                 struct config *config, struct config *defaults)
706 {
707         char *tmp = NULL;
708         const char *val;
709
710         if (config->autoboot_enabled == defaults->autoboot_enabled)
711                 val = "";
712         else
713                 val = config->autoboot_enabled ? "true" : "false";
714         update_string_config(platform, "auto-boot?", val);
715
716         if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
717                 val = "";
718         else
719                 val = tmp = talloc_asprintf(platform, "%d",
720                                 config->autoboot_timeout_sec);
721
722         if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
723             platform->clear_ipmi_bootdev) {
724                 platform->clear_ipmi_bootdev(platform,
725                                 config->ipmi_bootdev_persistent);
726                 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
727                 config->ipmi_bootdev_persistent = false;
728         }
729
730         update_string_config(platform, "petitboot,timeout", val);
731         if (tmp)
732                 talloc_free(tmp);
733
734         val = config->lang ?: "";
735         update_string_config(platform, "petitboot,language", val);
736
737         if (config->allow_writes == defaults->allow_writes)
738                 val = "";
739         else
740                 val = config->allow_writes ? "true" : "false";
741         update_string_config(platform, "petitboot,write?", val);
742
743         val = config->boot_tty ?: "";
744         update_string_config(platform, "petitboot,tty", val);
745
746         update_network_config(platform, config);
747
748         update_bootdev_config(platform, config);
749
750         return write_nvram(platform);
751 }
752
753 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
754                 bool persistent)
755 {
756         config->ipmi_bootdev = bootdev;
757         config->ipmi_bootdev_persistent = persistent;
758
759         switch (bootdev) {
760         case IPMI_BOOTDEV_NONE:
761         case IPMI_BOOTDEV_DISK:
762         case IPMI_BOOTDEV_NETWORK:
763         case IPMI_BOOTDEV_CDROM:
764         default:
765                 break;
766         case IPMI_BOOTDEV_SETUP:
767                 config->autoboot_enabled = false;
768                 break;
769         case IPMI_BOOTDEV_SAFE:
770                 config->autoboot_enabled = false;
771                 config->safe_mode = true;
772                 break;
773         }
774 }
775
776 static int read_bootdev_sysparam(const char *name, uint8_t *val)
777 {
778         uint8_t buf[2];
779         char path[50];
780         int fd, rc;
781
782         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
783         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
784
785         fd = open(path, O_RDONLY);
786         if (fd < 0) {
787                 pb_debug("powerpc: can't access sysparam %s\n",
788                                 name);
789                 return -1;
790         }
791
792         rc = read(fd, buf, sizeof(buf));
793
794         close(fd);
795
796         /* bootdev definitions should only be one byte in size */
797         if (rc != 1) {
798                 pb_debug("powerpc: sysparam %s read returned %d\n",
799                                 name, rc);
800                 return -1;
801         }
802
803         pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
804
805         if (!ipmi_bootdev_is_valid(buf[0]))
806                 return -1;
807
808         *val = buf[0];
809         return 0;
810 }
811
812 static int write_bootdev_sysparam(const char *name, uint8_t val)
813 {
814         char path[50];
815         int fd, rc;
816
817         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
818         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
819
820         fd = open(path, O_WRONLY);
821         if (fd < 0) {
822                 pb_debug("powerpc: can't access sysparam %s for writing\n",
823                                 name);
824                 return -1;
825         }
826
827         for (;;) {
828                 errno = 0;
829                 rc = write(fd, &val, sizeof(val));
830                 if (rc == sizeof(val)) {
831                         rc = 0;
832                         break;
833                 }
834
835                 if (rc <= 0 && errno != EINTR) {
836                         pb_log("powerpc: error updating sysparam %s: %s",
837                                         name, strerror(errno));
838                         rc = -1;
839                         break;
840                 }
841         }
842
843         close(fd);
844
845         if (!rc)
846                 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
847
848         return rc;
849 }
850
851 static int clear_ipmi_bootdev_sysparams(
852                 struct platform_powerpc *platform __attribute__((unused)),
853                 bool persistent)
854 {
855         if (persistent) {
856                 /* invalidate default-boot-device setting */
857                 write_bootdev_sysparam("default-boot-device", 0xff);
858         } else {
859                 /* invalidate next-boot-device setting */
860                 write_bootdev_sysparam("next-boot-device", 0xff);
861         }
862         return 0;
863 }
864
865 static int get_ipmi_bootdev_sysparams(
866                 struct platform_powerpc *platform __attribute__((unused)),
867                 uint8_t *bootdev, bool *persistent)
868 {
869         uint8_t next_bootdev, default_bootdev;
870         bool next_valid, default_valid;
871         int rc;
872
873         rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
874         next_valid = rc == 0;
875
876         rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
877         default_valid = rc == 0;
878
879         /* nothing valid? no need to change the config */
880         if (!next_valid && !default_valid)
881                 return -1;
882
883         *persistent = !next_valid;
884         *bootdev = next_valid ? next_bootdev : default_bootdev;
885         return 0;
886 }
887
888 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
889                                    bool persistent __attribute__((unused)))
890 {
891         uint16_t resp_len;
892         uint8_t resp[1];
893         uint8_t req[] = {
894                 0x05, /* parameter selector: boot flags */
895                 0x80, /* data 1: valid */
896                 0x00, /* data 2: bootdev: no override */
897                 0x00, /* data 3: system defaults */
898                 0x00, /* data 4: no request for shared mode, mux defaults */
899                 0x00, /* data 5: no instance request */
900         };
901
902         resp_len = sizeof(resp);
903
904         ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
905                         IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
906                         req, sizeof(req),
907                         resp, &resp_len,
908                         ipmi_timeout);
909         return 0;
910 }
911
912 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
913                 uint8_t *bootdev, bool *persistent)
914 {
915         uint16_t resp_len;
916         uint8_t resp[8];
917         int rc;
918         uint8_t req[] = {
919                 0x05, /* parameter selector: boot flags */
920                 0x00, /* no set selector */
921                 0x00, /* no block selector */
922         };
923
924         resp_len = sizeof(resp);
925         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
926                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
927                         req, sizeof(req),
928                         resp, &resp_len,
929                         ipmi_timeout);
930         if (rc) {
931                 pb_log("platform: error reading IPMI boot options\n");
932                 return -1;
933         }
934
935         if (resp_len != sizeof(resp)) {
936                 pb_log("platform: unexpected length (%d) in "
937                                 "boot options response\n", resp_len);
938                 return -1;
939         }
940
941         pb_debug("IPMI get_bootdev response:\n");
942         for (int i = 0; i < resp_len; i++)
943                 pb_debug("%x ", resp[i]);
944         pb_debug("\n");
945
946         if (resp[0] != 0) {
947                 pb_log("platform: non-zero completion code %d from IPMI req\n",
948                                 resp[0]);
949                 return -1;
950         }
951
952         /* check for correct parameter version */
953         if ((resp[1] & 0xf) != 0x1) {
954                 pb_log("platform: unexpected version (0x%x) in "
955                                 "boot options response\n", resp[0]);
956                 return -1;
957         }
958
959         /* check for valid paramters */
960         if (resp[2] & 0x80) {
961                 pb_debug("platform: boot options are invalid/locked\n");
962                 return -1;
963         }
964
965         *persistent = false;
966
967         /* check for valid flags */
968         if (!(resp[3] & 0x80)) {
969                 pb_debug("platform: boot flags are invalid, ignoring\n");
970                 return -1;
971         }
972
973         *persistent = resp[3] & 0x40;
974         *bootdev = (resp[4] >> 2) & 0x0f;
975         return 0;
976 }
977
978 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
979 {
980         int sensor_number;
981         uint16_t resp_len;
982         uint8_t resp[1];
983         uint8_t req[] = {
984                 0x00, /* sensor number: os boot */
985                 0xA9, /* operation: set everything */
986                 0x00, /* sensor reading: none */
987                 0x40, /* assertion mask lsb: set state 6 */
988                 0x00, /* assertion mask msb: none */
989                 0x00, /* deassertion mask lsb: none */
990                 0x00, /* deassertion mask msb: none */
991                 0x00, /* event data 1: none */
992                 0x00, /* event data 2: none */
993                 0x00, /* event data 3: none */
994         };
995
996         sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
997         if (sensor_number < 0) {
998                 pb_log("Couldn't find OS boot sensor in device tree\n");
999                 return -1;
1000         }
1001
1002         req[0] = sensor_number;
1003
1004         resp_len = sizeof(resp);
1005
1006         ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
1007                         IPMI_CMD_SENSOR_SET,
1008                         req, sizeof(req),
1009                         resp, &resp_len,
1010                         ipmi_timeout); return 0;
1011
1012         return 0;
1013 }
1014
1015 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
1016 {
1017         struct platform_powerpc *platform = p->platform_data;
1018         uint16_t resp_len = 8;
1019         uint8_t resp[8];
1020         uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1021         int i, rc;
1022
1023         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1024                         IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1025                         req, sizeof(req),
1026                         resp, &resp_len,
1027                         ipmi_timeout);
1028
1029         pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1030
1031         if (rc == 0 && resp_len > 0) {
1032                 for (i = 2; i < resp_len; i++) {
1033                         pb_debug(" %x", resp[i]);
1034                         buf[i - 2] = resp[i];
1035                 }
1036                 pb_debug("\n");
1037         }
1038
1039 }
1040
1041 /*
1042  * Retrieve info from the "Get Device ID" IPMI commands.
1043  * See Chapter 20.1 in the IPMIv2 specification.
1044  */
1045 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1046 {
1047         struct platform_powerpc *platform = p->platform_data;
1048         uint16_t resp_len = 16;
1049         uint8_t resp[16], bcd;
1050         uint32_t aux_version;
1051         int i, rc;
1052
1053         /* Retrieve info from current side */
1054         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1055                         IPMI_CMD_APP_GET_DEVICE_ID,
1056                         NULL, 0,
1057                         resp, &resp_len,
1058                         ipmi_timeout);
1059
1060         pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1061         if (resp_len > 0) {
1062                 for (i = 0; i < resp_len; i++) {
1063                         pb_debug(" %x", resp[i]);
1064                 }
1065                 pb_debug("\n");
1066         }
1067
1068         if (rc == 0 && resp_len == 16) {
1069                 info->bmc_current = talloc_array(info, char *, 4);
1070                 info->n_bmc_current = 4;
1071
1072                 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1073                                                 resp[1]);
1074                 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1075                                                 resp[2]);
1076                 bcd = resp[4] & 0x0f;
1077                 bcd += 10 * (resp[4] >> 4);
1078                 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1079                 info->bmc_current[2] = talloc_asprintf(info,
1080                                                 "Firmware version: %u.%02u.%05u",
1081                                                 resp[3], bcd, aux_version);
1082                 bcd = resp[5] & 0x0f;
1083                 bcd += 10 * (resp[5] >> 4);
1084                 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1085                                                 bcd);
1086         } else
1087                 pb_log("Failed to retrieve Device ID from IPMI\n");
1088
1089         /* Retrieve info from golden side */
1090         memset(resp, 0, sizeof(resp));
1091         resp_len = 16;
1092         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1093                         IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1094                         NULL, 0,
1095                         resp, &resp_len,
1096                         ipmi_timeout);
1097
1098         pb_debug("BMC golden resp [%d][%d]:\n", rc, resp_len);
1099         if (resp_len > 0) {
1100                 for (i = 0; i < resp_len; i++) {
1101                         pb_debug(" %x", resp[i]);
1102                 }
1103                 pb_debug("\n");
1104         }
1105
1106         if (rc == 0 && resp_len == 16) {
1107                 info->bmc_golden = talloc_array(info, char *, 4);
1108                 info->n_bmc_golden = 4;
1109
1110                 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1111                                                 resp[1]);
1112                 info->bmc_golden[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1113                                                 resp[2]);
1114                 bcd = resp[4] & 0x0f;
1115                 bcd += 10 * (resp[4] >> 4);
1116                 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1117                 info->bmc_golden[2] = talloc_asprintf(info,
1118                                                 "Firmware version: %u.%02u.%u",
1119                                                 resp[3], bcd, aux_version);
1120                 bcd = resp[5] & 0x0f;
1121                 bcd += 10 * (resp[5] >> 4);
1122                 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1123                                                 bcd);
1124         } else
1125                 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1126 }
1127
1128 static void get_ipmi_network_override(struct platform_powerpc *platform,
1129                         struct config *config)
1130 {
1131         uint16_t min_len = 12, resp_len = 53, version;
1132         const uint32_t magic_value = 0x21706221;
1133         uint8_t resp[resp_len];
1134         uint32_t cookie;
1135         bool persistent;
1136         int i, rc;
1137         uint8_t req[] = {
1138                 0x61, /* parameter selector: OEM section (network) */
1139                 0x00, /* no set selector */
1140                 0x00, /* no block selector */
1141         };
1142
1143         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1144                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1145                         req, sizeof(req),
1146                         resp, &resp_len,
1147                         ipmi_timeout);
1148
1149         pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1150         if (resp_len > 0) {
1151                 for (i = 0; i < resp_len; i++) {
1152                         pb_debug(" %02x", resp[i]);
1153                         if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1154                                 pb_debug("\n");
1155                         else if (i && (i + 1) % 8 == 0)
1156                                 pb_debug(" ");
1157                 }
1158                 pb_debug("\n");
1159         }
1160
1161         if (rc) {
1162                 pb_debug("IPMI network config option unavailable\n");
1163                 return;
1164         }
1165
1166         if (resp_len < min_len) {
1167                 pb_debug("IPMI net response too small\n");
1168                 return;
1169         }
1170
1171         if (resp[0] != 0) {
1172                 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1173                        resp[0]);
1174                 return;
1175         }
1176
1177         /* Check for correct parameter version */
1178         if ((resp[1] & 0xf) != 0x1) {
1179                 pb_log("platform: unexpected version (0x%x) in network override response\n",
1180                        resp[0]);
1181                 return;
1182         }
1183
1184         /* Check that the parameters are valid */
1185         if (resp[2] & 0x80) {
1186                 pb_debug("platform: network override is invalid/locked\n");
1187                 return;
1188         }
1189
1190         /* Check for valid parameters in the boot flags section */
1191         if (!(resp[3] & 0x80)) {
1192                 pb_debug("platform: network override valid flag not set\n");
1193                 return;
1194         }
1195         /* Read the persistent flag; if it is set we need to save this config */
1196         persistent = resp[3] & 0x40;
1197         if (persistent)
1198                 pb_debug("platform: network override is persistent\n");
1199
1200         /* Check 4-byte cookie value */
1201         i = 4;
1202         memcpy(&cookie, &resp[i], sizeof(cookie));
1203         cookie = __be32_to_cpu(cookie);
1204         if (cookie != magic_value) {
1205                 pb_log("%s: Incorrect cookie %x\n", __func__, cookie);
1206                 return;
1207         }
1208         i += sizeof(cookie);
1209
1210         /* Check 2-byte version number */
1211         memcpy(&version, &resp[i], sizeof(version));
1212         version = __be16_to_cpu(version);
1213         i += sizeof(version);
1214         if (version != 1) {
1215                 pb_debug("Unexpected version: %u\n", version);
1216                 return;
1217         }
1218
1219         /* Interpret the rest of the interface config */
1220         rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1221
1222         if (!rc && persistent) {
1223                 /* Write this new config to NVRAM */
1224                 update_network_config(platform, config);
1225                 rc = write_nvram(platform);
1226                 if (rc)
1227                         pb_log("platform: Failed to save persistent interface override\n");
1228         }
1229 }
1230
1231 static void get_active_consoles(struct config *config)
1232 {
1233         struct stat sbuf;
1234         char *fsp_prop = NULL;
1235
1236         config->n_tty = 2;
1237         config->tty_list = talloc_array(config, char *, config->n_tty);
1238         if (!config->tty_list)
1239                 goto err;
1240
1241         config->tty_list[0] = talloc_asprintf(config->tty_list,
1242                                         "/dev/hvc0 [IPMI / Serial]");
1243         config->tty_list[1] = talloc_asprintf(config->tty_list,
1244                                         "/dev/tty1 [VGA]");
1245
1246         fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1247         if (stat(fsp_prop, &sbuf) == 0) {
1248                 /* FSP based machines also have a separate serial console */
1249                 config->tty_list = talloc_realloc(config, config->tty_list,
1250                                                 char *, config->n_tty + 1);
1251                 if (!config->tty_list)
1252                         goto err;
1253                 config->tty_list[config->n_tty++] = talloc_asprintf(
1254                                                 config->tty_list,
1255                                                 "/dev/hvc1 [Serial]");
1256         }
1257
1258         return;
1259 err:
1260         config->n_tty = 0;
1261         pb_log("Failed to allocate memory for tty_list\n");
1262 }
1263
1264 static int load_config(struct platform *p, struct config *config)
1265 {
1266         struct platform_powerpc *platform = to_platform_powerpc(p);
1267         int rc;
1268
1269         rc = parse_nvram(platform);
1270         if (rc)
1271                 return rc;
1272
1273         populate_config(platform, config);
1274
1275         if (platform->get_ipmi_bootdev) {
1276                 bool bootdev_persistent;
1277                 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1278                 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1279                                 &bootdev_persistent);
1280                 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1281                         set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1282                 }
1283         }
1284
1285         if (platform->ipmi)
1286                 get_ipmi_network_override(platform, config);
1287
1288         get_active_consoles(config);
1289
1290         return 0;
1291 }
1292
1293 static int save_config(struct platform *p, struct config *config)
1294 {
1295         struct platform_powerpc *platform = to_platform_powerpc(p);
1296         struct config *defaults;
1297         int rc;
1298
1299         defaults = talloc_zero(platform, struct config);
1300         config_set_defaults(defaults);
1301
1302         rc = update_config(platform, config, defaults);
1303
1304         talloc_free(defaults);
1305         return rc;
1306 }
1307
1308 static void pre_boot(struct platform *p, const struct config *config)
1309 {
1310         struct platform_powerpc *platform = to_platform_powerpc(p);
1311
1312         if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1313                 platform->clear_ipmi_bootdev(platform, false);
1314
1315         if (platform->set_os_boot_sensor)
1316                 platform->set_os_boot_sensor(platform);
1317 }
1318
1319 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1320 {
1321         struct platform_powerpc *platform = p->platform_data;
1322         char *buf, *filename;
1323         int len, rc;
1324
1325         filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1326         rc = read_file(platform, filename, &buf, &len);
1327         if (rc == 0)
1328                 sysinfo->type = talloc_steal(sysinfo, buf);
1329         talloc_free(filename);
1330
1331         filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1332         rc = read_file(platform, filename, &buf, &len);
1333         if (rc == 0)
1334                 sysinfo->identifier = talloc_steal(sysinfo, buf);
1335         talloc_free(filename);
1336
1337         sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1338         if (platform->ipmi)
1339                 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1340
1341         if (platform->ipmi)
1342                 get_ipmi_bmc_versions(p, sysinfo);
1343
1344         if (platform->get_platform_versions)
1345                 platform->get_platform_versions(sysinfo);
1346
1347         return 0;
1348 }
1349
1350 static bool probe(struct platform *p, void *ctx)
1351 {
1352         struct platform_powerpc *platform;
1353         struct stat statbuf;
1354         bool bmc_present;
1355         int rc;
1356
1357         /* we need a device tree */
1358         rc = stat("/proc/device-tree", &statbuf);
1359         if (rc)
1360                 return false;
1361
1362         if (!S_ISDIR(statbuf.st_mode))
1363                 return false;
1364
1365         platform = talloc_zero(ctx, struct platform_powerpc);
1366         list_init(&platform->params);
1367
1368         p->platform_data = platform;
1369
1370         bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1371
1372         if (ipmi_present() && bmc_present) {
1373                 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1374                 platform->ipmi = ipmi_open(platform);
1375                 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1376                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1377                 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1378         } else if (!stat(sysparams_dir, &statbuf)) {
1379                 pb_debug("platform: using sysparams for IPMI paramters\n");
1380                 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1381                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1382
1383         } else {
1384                 pb_log("platform: no IPMI parameter support\n");
1385         }
1386
1387         if (bmc_present)
1388                 platform->get_platform_versions = hostboot_load_versions;
1389
1390         return true;
1391 }
1392
1393
1394 static struct platform platform_powerpc = {
1395         .name                   = "powerpc",
1396         .dhcp_arch_id           = 0x000e,
1397         .probe                  = probe,
1398         .load_config            = load_config,
1399         .save_config            = save_config,
1400         .pre_boot               = pre_boot,
1401         .get_sysinfo            = get_sysinfo,
1402 };
1403
1404 register_platform(platform_powerpc);