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