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