]> git.ozlabs.org Git - petitboot/blob - discover/platform-powerpc.c
docker: Add build container files
[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 <fcntl.h>
8 #include <sys/types.h>
9 #include <sys/wait.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 #include <url/url.h>
20
21 #include "hostboot.h"
22 #include "platform.h"
23 #include "ipmi.h"
24 #include "dt.h"
25
26 static const char *partition = "common";
27 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
28 static const char *devtree_dir = "/proc/device-tree/";
29 static const int ipmi_timeout = 10000; /* milliseconds. */
30
31 struct param {
32         char                    *name;
33         char                    *value;
34         bool                    modified;
35         struct list_item        list;
36 };
37
38 struct platform_powerpc {
39         struct list     params;
40         struct ipmi     *ipmi;
41         bool            ipmi_bootdev_persistent;
42         int             (*get_ipmi_bootdev)(
43                                 struct platform_powerpc *platform,
44                                 uint8_t *bootdev, bool *persistent);
45         int             (*clear_ipmi_bootdev)(
46                                 struct platform_powerpc *platform,
47                                 bool persistent);
48         int             (*set_os_boot_sensor)(
49                                 struct platform_powerpc *platform);
50         void            (*get_platform_versions)(struct system_info *info);
51 };
52
53 static const char *known_params[] = {
54         "auto-boot?",
55         "petitboot,network",
56         "petitboot,timeout",
57         "petitboot,bootdevs",
58         "petitboot,language",
59         "petitboot,debug?",
60         "petitboot,write?",
61         "petitboot,snapshots?",
62         "petitboot,console",
63         "petitboot,http_proxy",
64         "petitboot,https_proxy",
65         NULL,
66 };
67
68 #define to_platform_powerpc(p) \
69         (struct platform_powerpc *)(p->platform_data)
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, *tok_gw, *tok_url, *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                 /*
325                  * If a url is set but not a gateway, we can accidentally
326                  * interpret the url as the gateway. To avoid changing the
327                  * parameter format check if the "gateway" is actually a
328                  * pb-url if it's the last token.
329                  */
330                 tok_gw = strtok_r(NULL, ",", &saveptr);
331                 tok_url = strtok_r(NULL, ",", &saveptr);
332
333                 if (tok_gw) {
334                         if (tok_url || !is_url(tok_gw))
335                                 ifconf->static_config.gateway =
336                                         talloc_strdup(ifconf, tok_gw);
337                         else
338                                         tok_url = tok_gw;
339                 }
340
341                 if (tok_url)
342                         ifconf->static_config.url =
343                                 talloc_strdup(ifconf, tok_url);
344         } else {
345                 pb_log("Unknown network configuration method %s\n", tok);
346                 goto out_err;
347         }
348
349         config->network.interfaces = talloc_realloc(config,
350                         config->network.interfaces,
351                         struct interface_config *,
352                         ++config->network.n_interfaces);
353
354         config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
355
356         return 0;
357 out_err:
358         talloc_free(ifconf);
359         return -1;
360 }
361
362 static int parse_one_dns_config(struct config *config,
363                 char *confstr)
364 {
365         char *tok, *saveptr = NULL;
366
367         for (tok = strtok_r(confstr, ",", &saveptr); tok;
368                         tok = strtok_r(NULL, ",", &saveptr)) {
369
370                 char *server = talloc_strdup(config, tok);
371
372                 config->network.dns_servers = talloc_realloc(config,
373                                 config->network.dns_servers, const char *,
374                                 ++config->network.n_dns_servers);
375
376                 config->network.dns_servers[config->network.n_dns_servers - 1]
377                                 = server;
378         }
379
380         return 0;
381 }
382
383 static void populate_network_config(struct platform_powerpc *platform,
384                 struct config *config)
385 {
386         char *val, *saveptr = NULL;
387         const char *cval;
388         int i;
389
390         cval = get_param(platform, "petitboot,network");
391         if (!cval || !strlen(cval))
392                 return;
393
394         val = talloc_strdup(config, cval);
395
396         for (i = 0; ; i++) {
397                 char *tok;
398
399                 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
400                 if (!tok)
401                         break;
402
403                 if (!strncasecmp(tok, "dns,", strlen("dns,")))
404                         parse_one_dns_config(config, tok + strlen("dns,"));
405                 else
406                         parse_one_interface_config(config, tok);
407
408         }
409
410         talloc_free(val);
411 }
412
413 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
414 {
415         char *delim = strchr(*pos, ' ');
416         int len, prefix = 0, rc = -1;
417         enum device_type type;
418
419         if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
420                 prefix = strlen("uuid:");
421                 opt->boot_type = BOOT_DEVICE_UUID;
422         } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
423                 prefix = strlen("mac:");
424                 opt->boot_type = BOOT_DEVICE_UUID;
425         } else {
426                 type = find_device_type(*pos);
427                 if (type != DEVICE_TYPE_UNKNOWN) {
428                         opt->type = type;
429                         opt->boot_type = BOOT_DEVICE_TYPE;
430                         rc = 0;
431                 }
432         }
433
434         if (opt->boot_type == BOOT_DEVICE_UUID) {
435                 if (delim)
436                         len = (int)(delim - *pos) - prefix;
437                 else
438                         len = strlen(*pos) - prefix;
439
440                 if (len) {
441                         opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
442                         rc = 0;
443                 }
444         }
445
446         /* Always advance pointer to next option or end */
447         if (delim)
448                 *pos = delim + 1;
449         else
450                 *pos += strlen(*pos);
451
452         return rc;
453 }
454
455 static void populate_bootdev_config(struct platform_powerpc *platform,
456                 struct config *config)
457 {
458         struct autoboot_option *opt, *new = NULL;
459         char *pos, *end;
460         unsigned int n_new = 0;
461         const char *val;
462
463         /* Check for ordered bootdevs */
464         val = get_param(platform, "petitboot,bootdevs");
465         if (!val || !strlen(val)) {
466                 pos = end = NULL;
467         } else {
468                 pos = talloc_strdup(config, val);
469                 end = strchr(pos, '\0');
470         }
471
472         while (pos && pos < end) {
473                 opt = talloc(config, struct autoboot_option);
474
475                 if (read_bootdev(config, &pos, opt)) {
476                         pb_log("bootdev config is in an unknown format "
477                                "(expected uuid:... or mac:...)\n");
478                         talloc_free(opt);
479                         continue;
480                 }
481
482                 new = talloc_realloc(config, new, struct autoboot_option,
483                                      n_new + 1);
484                 new[n_new] = *opt;
485                 n_new++;
486                 talloc_free(opt);
487
488         }
489
490         if (!n_new) {
491                 /* If autoboot has been disabled, clear the default options */
492                 if (!config->autoboot_enabled) {
493                         talloc_free(config->autoboot_opts);
494                         config->n_autoboot_opts = 0;
495                 }
496                 return;
497         }
498
499         talloc_free(config->autoboot_opts);
500         config->autoboot_opts = new;
501         config->n_autoboot_opts = n_new;
502 }
503
504 static void populate_config(struct platform_powerpc *platform,
505                 struct config *config)
506 {
507         const char *val;
508         char *end;
509         unsigned long timeout;
510
511         /* if the "auto-boot?' property is present and "false", disable auto
512          * boot */
513         val = get_param(platform, "auto-boot?");
514         config->autoboot_enabled = !val || strcmp(val, "false");
515
516         val = get_param(platform, "petitboot,timeout");
517         if (val) {
518                 timeout = strtoul(val, &end, 10);
519                 if (end != val) {
520                         if (timeout >= INT_MAX)
521                                 timeout = INT_MAX;
522                         config->autoboot_timeout_sec = (int)timeout;
523                 }
524         }
525
526         val = get_param(platform, "petitboot,language");
527         config->lang = val ? talloc_strdup(config, val) : NULL;
528
529         populate_network_config(platform, config);
530
531         populate_bootdev_config(platform, config);
532
533         if (!config->debug) {
534                 val = get_param(platform, "petitboot,debug?");
535                 config->debug = val && !strcmp(val, "true");
536         }
537
538         val = get_param(platform, "petitboot,write?");
539         if (val)
540                 config->allow_writes = !strcmp(val, "true");
541
542         val = get_param(platform, "petitboot,snapshots?");
543         if (val)
544                 config->disable_snapshots = !strcmp(val, "false");
545
546         val = get_param(platform, "petitboot,console");
547         if (val)
548                 config->boot_console = talloc_strdup(config, val);
549         /* If a full path is already set we don't want to override it */
550         config->manual_console = config->boot_console &&
551                                         !strchr(config->boot_console, '[');
552
553         val = get_param(platform, "petitboot,http_proxy");
554         if (val)
555                 config->http_proxy = talloc_strdup(config, val);
556         val = get_param(platform, "petitboot,https_proxy");
557         if (val)
558                 config->https_proxy = talloc_strdup(config, val);
559 }
560
561 static char *iface_config_str(void *ctx, struct interface_config *config)
562 {
563         char *str;
564
565         /* todo: HWADDR size is hardcoded as 6, but we may need to handle
566          * different hardware address formats */
567         str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
568                         config->hwaddr[0], config->hwaddr[1],
569                         config->hwaddr[2], config->hwaddr[3],
570                         config->hwaddr[4], config->hwaddr[5]);
571
572         if (config->ignore) {
573                 str = talloc_asprintf_append(str, "ignore");
574
575         } else if (config->method == CONFIG_METHOD_DHCP) {
576                 str = talloc_asprintf_append(str, "dhcp");
577
578         } else if (config->method == CONFIG_METHOD_STATIC) {
579                 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
580                                 config->static_config.address,
581                                 config->static_config.gateway ? "," : "",
582                                 config->static_config.gateway ?: "",
583                                 config->static_config.url ? "," : "",
584                                 config->static_config.url ?: "");
585         }
586         return str;
587 }
588
589 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
590 {
591         char *str;
592         int i;
593
594         str = talloc_strdup(ctx, "dns,");
595         for (i = 0; i < n; i++) {
596                 str = talloc_asprintf_append(str, "%s%s",
597                                 i == 0 ? "" : ",",
598                                 dns_servers[i]);
599         }
600
601         return str;
602 }
603
604 static void update_string_config(struct platform_powerpc *platform,
605                 const char *name, const char *value)
606 {
607         const char *cur;
608
609         cur = get_param(platform, name);
610
611         /* don't set an empty parameter if it doesn't already exist */
612         if (!cur && !strlen(value))
613                 return;
614
615         set_param(platform, name, value);
616 }
617
618 static void update_network_config(struct platform_powerpc *platform,
619         struct config *config)
620 {
621         unsigned int i;
622         char *val;
623
624         /*
625          * Don't store IPMI overrides to NVRAM. If this was a persistent
626          * override it was already stored in NVRAM by
627          * get_ipmi_network_override()
628          */
629         if (config->network.n_interfaces &&
630                 config->network.interfaces[0]->override)
631                 return;
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;
661         struct autoboot_option *opt;
662         const char delim = ' ';
663         unsigned int i;
664
665         if (!config->n_autoboot_opts)
666                 val = "";
667
668         for (i = 0; i < config->n_autoboot_opts; i++) {
669                 opt = &config->autoboot_opts[i];
670                 switch (opt->boot_type) {
671                         case BOOT_DEVICE_TYPE:
672                                 boot_str = talloc_asprintf(config, "%s%c",
673                                                 device_type_name(opt->type),
674                                                 delim);
675                                 break;
676                         case BOOT_DEVICE_UUID:
677                                 boot_str = talloc_asprintf(config, "uuid:%s%c",
678                                                 opt->uuid, delim);
679                                 break;
680                         }
681                         tmp = val = talloc_asprintf_append(val, "%s", boot_str);
682         }
683
684         update_string_config(platform, "petitboot,bootdevs", val);
685         talloc_free(tmp);
686         if (boot_str)
687                 talloc_free(boot_str);
688 }
689
690 static int update_config(struct platform_powerpc *platform,
691                 struct config *config, struct config *defaults)
692 {
693         char *tmp = NULL;
694         const char *val;
695
696         if (config->autoboot_enabled == defaults->autoboot_enabled)
697                 val = "";
698         else
699                 val = config->autoboot_enabled ? "true" : "false";
700         update_string_config(platform, "auto-boot?", val);
701
702         if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
703                 val = "";
704         else
705                 val = tmp = talloc_asprintf(platform, "%d",
706                                 config->autoboot_timeout_sec);
707
708         if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
709             platform->clear_ipmi_bootdev) {
710                 platform->clear_ipmi_bootdev(platform,
711                                 config->ipmi_bootdev_persistent);
712                 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
713                 config->ipmi_bootdev_persistent = false;
714         }
715
716         update_string_config(platform, "petitboot,timeout", val);
717         if (tmp)
718                 talloc_free(tmp);
719
720         val = config->lang ?: "";
721         update_string_config(platform, "petitboot,language", val);
722
723         if (config->allow_writes == defaults->allow_writes)
724                 val = "";
725         else
726                 val = config->allow_writes ? "true" : "false";
727         update_string_config(platform, "petitboot,write?", val);
728
729         if (!config->manual_console) {
730                 val = config->boot_console ?: "";
731                 update_string_config(platform, "petitboot,console", val);
732         }
733
734         val = config->http_proxy ?: "";
735         update_string_config(platform, "petitboot,http_proxy", val);
736         val = config->https_proxy ?: "";
737         update_string_config(platform, "petitboot,https_proxy", val);
738
739         update_network_config(platform, config);
740
741         update_bootdev_config(platform, config);
742
743         return write_nvram(platform);
744 }
745
746 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
747                 bool persistent)
748 {
749         config->ipmi_bootdev = bootdev;
750         config->ipmi_bootdev_persistent = persistent;
751
752         if (bootdev == IPMI_BOOTDEV_SAFE)
753                 config->safe_mode = true;
754 }
755
756 static int read_bootdev_sysparam(const char *name, uint8_t *val)
757 {
758         uint8_t buf[2];
759         char path[50];
760         int fd, rc;
761
762         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
763         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
764
765         fd = open(path, O_RDONLY);
766         if (fd < 0) {
767                 pb_debug("powerpc: can't access sysparam %s\n",
768                                 name);
769                 return -1;
770         }
771
772         rc = read(fd, buf, sizeof(buf));
773
774         close(fd);
775
776         /* bootdev definitions should only be one byte in size */
777         if (rc != 1) {
778                 pb_debug("powerpc: sysparam %s read returned %d\n",
779                                 name, rc);
780                 return -1;
781         }
782
783         pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
784
785         if (!ipmi_bootdev_is_valid(buf[0]))
786                 return -1;
787
788         *val = buf[0];
789         return 0;
790 }
791
792 static int write_bootdev_sysparam(const char *name, uint8_t val)
793 {
794         char path[50];
795         int fd, rc;
796
797         assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
798         snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
799
800         fd = open(path, O_WRONLY);
801         if (fd < 0) {
802                 pb_debug("powerpc: can't access sysparam %s for writing\n",
803                                 name);
804                 return -1;
805         }
806
807         for (;;) {
808                 errno = 0;
809                 rc = write(fd, &val, sizeof(val));
810                 if (rc == sizeof(val)) {
811                         rc = 0;
812                         break;
813                 }
814
815                 if (rc <= 0 && errno != EINTR) {
816                         pb_log("powerpc: error updating sysparam %s: %s",
817                                         name, strerror(errno));
818                         rc = -1;
819                         break;
820                 }
821         }
822
823         close(fd);
824
825         if (!rc)
826                 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
827
828         return rc;
829 }
830
831 static int clear_ipmi_bootdev_sysparams(
832                 struct platform_powerpc *platform __attribute__((unused)),
833                 bool persistent)
834 {
835         if (persistent) {
836                 /* invalidate default-boot-device setting */
837                 write_bootdev_sysparam("default-boot-device", 0xff);
838         } else {
839                 /* invalidate next-boot-device setting */
840                 write_bootdev_sysparam("next-boot-device", 0xff);
841         }
842         return 0;
843 }
844
845 static int get_ipmi_bootdev_sysparams(
846                 struct platform_powerpc *platform __attribute__((unused)),
847                 uint8_t *bootdev, bool *persistent)
848 {
849         uint8_t next_bootdev, default_bootdev;
850         bool next_valid, default_valid;
851         int rc;
852
853         rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
854         next_valid = rc == 0;
855
856         rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
857         default_valid = rc == 0;
858
859         /* nothing valid? no need to change the config */
860         if (!next_valid && !default_valid)
861                 return -1;
862
863         *persistent = !next_valid;
864         *bootdev = next_valid ? next_bootdev : default_bootdev;
865         return 0;
866 }
867
868 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
869                                    bool persistent __attribute__((unused)))
870 {
871         uint16_t resp_len;
872         uint8_t resp[1];
873         uint8_t req[] = {
874                 0x05, /* parameter selector: boot flags */
875                 0x80, /* data 1: valid */
876                 0x00, /* data 2: bootdev: no override */
877                 0x00, /* data 3: system defaults */
878                 0x00, /* data 4: no request for shared mode, mux defaults */
879                 0x00, /* data 5: no instance request */
880         };
881
882         resp_len = sizeof(resp);
883
884         ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
885                         IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
886                         req, sizeof(req),
887                         resp, &resp_len,
888                         ipmi_timeout);
889         return 0;
890 }
891
892 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
893                 uint8_t *bootdev, bool *persistent)
894 {
895         uint16_t resp_len;
896         uint8_t resp[8];
897         int rc;
898         uint8_t req[] = {
899                 0x05, /* parameter selector: boot flags */
900                 0x00, /* no set selector */
901                 0x00, /* no block selector */
902         };
903
904         resp_len = sizeof(resp);
905         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
906                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
907                         req, sizeof(req),
908                         resp, &resp_len,
909                         ipmi_timeout);
910         if (rc) {
911                 pb_log("platform: error reading IPMI boot options\n");
912                 return -1;
913         }
914
915         if (resp_len != sizeof(resp)) {
916                 pb_log("platform: unexpected length (%d) in "
917                                 "boot options response\n", resp_len);
918                 return -1;
919         }
920
921         pb_debug("IPMI get_bootdev response:\n");
922         for (int i = 0; i < resp_len; i++)
923                 pb_debug("%x ", resp[i]);
924         pb_debug("\n");
925
926         if (resp[0] != 0) {
927                 pb_log("platform: non-zero completion code %d from IPMI req\n",
928                                 resp[0]);
929                 return -1;
930         }
931
932         /* check for correct parameter version */
933         if ((resp[1] & 0xf) != 0x1) {
934                 pb_log("platform: unexpected version (0x%x) in "
935                                 "boot options response\n", resp[0]);
936                 return -1;
937         }
938
939         /* check for valid paramters */
940         if (resp[2] & 0x80) {
941                 pb_debug("platform: boot options are invalid/locked\n");
942                 return -1;
943         }
944
945         *persistent = false;
946
947         /* check for valid flags */
948         if (!(resp[3] & 0x80)) {
949                 pb_debug("platform: boot flags are invalid, ignoring\n");
950                 return -1;
951         }
952
953         *persistent = resp[3] & 0x40;
954         *bootdev = (resp[4] >> 2) & 0x0f;
955         return 0;
956 }
957
958 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
959 {
960         int sensor_number;
961         uint16_t resp_len;
962         uint8_t resp[1];
963         uint8_t req[] = {
964                 0x00, /* sensor number: os boot */
965                 0xA9, /* operation: set everything */
966                 0x00, /* sensor reading: none */
967                 0x40, /* assertion mask lsb: set state 6 */
968                 0x00, /* assertion mask msb: none */
969                 0x00, /* deassertion mask lsb: none */
970                 0x00, /* deassertion mask msb: none */
971                 0x00, /* event data 1: none */
972                 0x00, /* event data 2: none */
973                 0x00, /* event data 3: none */
974         };
975
976         sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
977         if (sensor_number < 0) {
978                 pb_log("Couldn't find OS boot sensor in device tree\n");
979                 return -1;
980         }
981
982         req[0] = sensor_number;
983
984         resp_len = sizeof(resp);
985
986         ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
987                         IPMI_CMD_SENSOR_SET,
988                         req, sizeof(req),
989                         resp, &resp_len,
990                         ipmi_timeout); return 0;
991
992         return 0;
993 }
994
995 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
996 {
997         struct platform_powerpc *platform = p->platform_data;
998         uint16_t resp_len = 8;
999         uint8_t resp[8];
1000         uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1001         int i, rc;
1002
1003         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1004                         IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1005                         req, sizeof(req),
1006                         resp, &resp_len,
1007                         ipmi_timeout);
1008
1009         pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1010
1011         if (rc == 0 && resp_len > 0) {
1012                 for (i = 2; i < resp_len; i++) {
1013                         pb_debug(" %x", resp[i]);
1014                         buf[i - 2] = resp[i];
1015                 }
1016                 pb_debug("\n");
1017         }
1018
1019 }
1020
1021 /*
1022  * Retrieve info from the "Get Device ID" IPMI commands.
1023  * See Chapter 20.1 in the IPMIv2 specification.
1024  */
1025 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1026 {
1027         struct platform_powerpc *platform = p->platform_data;
1028         uint16_t resp_len = 16;
1029         uint8_t resp[16], bcd;
1030         int i, rc;
1031
1032         /* Retrieve info from current side */
1033         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1034                         IPMI_CMD_APP_GET_DEVICE_ID,
1035                         NULL, 0,
1036                         resp, &resp_len,
1037                         ipmi_timeout);
1038
1039         pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1040         if (resp_len > 0) {
1041                 for (i = 0; i < resp_len; i++) {
1042                         pb_debug(" %x", resp[i]);
1043                 }
1044                 pb_debug("\n");
1045         }
1046
1047         if (rc == 0 && (resp_len == 12 || resp_len == 16)) {
1048                 info->bmc_current = talloc_array(info, char *, 4);
1049                 info->n_bmc_current = 4;
1050
1051                 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1052                                                 resp[1]);
1053                 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1054                                                 resp[2]);
1055                 bcd = resp[4] & 0x0f;
1056                 bcd += 10 * (resp[4] >> 4);
1057                 /* rev1.rev2.aux_revision */
1058                 info->bmc_current[2] = talloc_asprintf(info,
1059                                 "Firmware version: %u.%02u",
1060                                 resp[3], bcd);
1061                 if (resp_len == 16) {
1062                         info->bmc_current[2] = talloc_asprintf_append(
1063                                         info->bmc_current[2],
1064                                         ".%02x%02x%02x%02x",
1065                                         resp[12], resp[13], resp[14], resp[15]);
1066                 }
1067                 bcd = resp[5] & 0x0f;
1068                 bcd += 10 * (resp[5] >> 4);
1069                 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1070                                                 bcd);
1071         } else
1072                 pb_log("Failed to retrieve Device ID from IPMI\n");
1073
1074         /* Retrieve info from golden side */
1075         memset(resp, 0, sizeof(resp));
1076         resp_len = 16;
1077         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1078                         IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1079                         NULL, 0,
1080                         resp, &resp_len,
1081                         ipmi_timeout);
1082
1083         pb_debug("BMC golden resp [%d][%d]:\n", rc, resp_len);
1084         if (resp_len > 0) {
1085                 for (i = 0; i < resp_len; i++) {
1086                         pb_debug(" %x", resp[i]);
1087                 }
1088                 pb_debug("\n");
1089         }
1090
1091         if (rc == 0 && (resp_len == 12 || resp_len == 16)) {
1092                 info->bmc_golden = talloc_array(info, char *, 4);
1093                 info->n_bmc_golden = 4;
1094
1095                 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1096                                                 resp[1]);
1097                 info->bmc_golden[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1098                                                 resp[2]);
1099                 bcd = resp[4] & 0x0f;
1100                 bcd += 10 * (resp[4] >> 4);
1101                 /* rev1.rev2.aux_revision */
1102                 info->bmc_golden[2] = talloc_asprintf(info,
1103                                 "Firmware version: %u.%02u",
1104                                 resp[3], bcd);
1105                 if (resp_len == 16) {
1106                         info->bmc_golden[2] = talloc_asprintf_append(
1107                                         info->bmc_golden[2],
1108                                         ".%02x%02x%02x%02x",
1109                                         resp[12], resp[13], resp[14], resp[15]);
1110                 }
1111                 bcd = resp[5] & 0x0f;
1112                 bcd += 10 * (resp[5] >> 4);
1113                 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1114                                                 bcd);
1115         } else
1116                 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1117 }
1118
1119 static void get_ipmi_network_override(struct platform_powerpc *platform,
1120                         struct config *config)
1121 {
1122         uint16_t min_len = 12, resp_len = 53, version;
1123         const uint32_t magic_value = 0x21706221;
1124         uint8_t resp[resp_len];
1125         uint32_t cookie;
1126         bool persistent;
1127         int i, rc;
1128         uint8_t req[] = {
1129                 0x61, /* parameter selector: OEM section (network) */
1130                 0x00, /* no set selector */
1131                 0x00, /* no block selector */
1132         };
1133
1134         rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1135                         IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1136                         req, sizeof(req),
1137                         resp, &resp_len,
1138                         ipmi_timeout);
1139
1140         pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1141         if (resp_len > 0) {
1142                 for (i = 0; i < resp_len; i++) {
1143                         pb_debug(" %02x", resp[i]);
1144                         if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1145                                 pb_debug("\n");
1146                         else if (i && (i + 1) % 8 == 0)
1147                                 pb_debug(" ");
1148                 }
1149                 pb_debug("\n");
1150         }
1151
1152         if (rc) {
1153                 pb_debug("IPMI network config option unavailable\n");
1154                 return;
1155         }
1156
1157         if (resp_len < min_len) {
1158                 pb_debug("IPMI net response too small\n");
1159                 return;
1160         }
1161
1162         if (resp[0] != 0) {
1163                 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1164                        resp[0]);
1165                 return;
1166         }
1167
1168         /* Check for correct parameter version */
1169         if ((resp[1] & 0xf) != 0x1) {
1170                 pb_log("platform: unexpected version (0x%x) in network override response\n",
1171                        resp[0]);
1172                 return;
1173         }
1174
1175         /* Check that the parameters are valid */
1176         if (resp[2] & 0x80) {
1177                 pb_debug("platform: network override is invalid/locked\n");
1178                 return;
1179         }
1180
1181         /* Check for valid parameters in the boot flags section */
1182         if (!(resp[3] & 0x80)) {
1183                 pb_debug("platform: network override valid flag not set\n");
1184                 return;
1185         }
1186         /* Read the persistent flag; if it is set we need to save this config */
1187         persistent = resp[3] & 0x40;
1188         if (persistent)
1189                 pb_debug("platform: network override is persistent\n");
1190
1191         /* Check 4-byte cookie value */
1192         i = 4;
1193         memcpy(&cookie, &resp[i], sizeof(cookie));
1194         cookie = __be32_to_cpu(cookie);
1195         if (cookie != magic_value) {
1196                 pb_log("%s: Incorrect cookie %x\n", __func__, cookie);
1197                 return;
1198         }
1199         i += sizeof(cookie);
1200
1201         /* Check 2-byte version number */
1202         memcpy(&version, &resp[i], sizeof(version));
1203         version = __be16_to_cpu(version);
1204         i += sizeof(version);
1205         if (version != 1) {
1206                 pb_debug("Unexpected version: %u\n", version);
1207                 return;
1208         }
1209
1210         /* Interpret the rest of the interface config */
1211         rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1212
1213         if (!rc && persistent) {
1214                 /* Write this new config to NVRAM */
1215                 update_network_config(platform, config);
1216                 rc = write_nvram(platform);
1217                 if (rc)
1218                         pb_log("platform: Failed to save persistent interface override\n");
1219         }
1220 }
1221
1222 static void get_active_consoles(struct config *config)
1223 {
1224         struct stat sbuf;
1225         char *fsp_prop = NULL;
1226
1227         config->n_consoles = 2;
1228         config->consoles = talloc_array(config, char *, config->n_consoles);
1229         if (!config->consoles)
1230                 goto err;
1231
1232         config->consoles[0] = talloc_asprintf(config->consoles,
1233                                         "/dev/hvc0 [IPMI / Serial]");
1234         config->consoles[1] = talloc_asprintf(config->consoles,
1235                                         "/dev/tty1 [VGA]");
1236
1237         fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1238         if (stat(fsp_prop, &sbuf) == 0) {
1239                 /* FSP based machines also have a separate serial console */
1240                 config->consoles = talloc_realloc(config, config->consoles,
1241                                                 char *, config->n_consoles + 1);
1242                 if (!config->consoles)
1243                         goto err;
1244                 config->consoles[config->n_consoles++] = talloc_asprintf(
1245                                                 config->consoles,
1246                                                 "/dev/hvc1 [Serial]");
1247         }
1248
1249         return;
1250 err:
1251         config->n_consoles = 0;
1252         pb_log("Failed to allocate memory for consoles\n");
1253 }
1254
1255 static int load_config(struct platform *p, struct config *config)
1256 {
1257         struct platform_powerpc *platform = to_platform_powerpc(p);
1258         int rc;
1259
1260         rc = parse_nvram(platform);
1261         if (rc)
1262                 pb_log("%s: Failed to parse nvram\n", __func__);
1263
1264         populate_config(platform, config);
1265
1266         if (platform->get_ipmi_bootdev) {
1267                 bool bootdev_persistent;
1268                 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1269                 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1270                                 &bootdev_persistent);
1271                 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1272                         set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1273                 }
1274         }
1275
1276         if (platform->ipmi)
1277                 get_ipmi_network_override(platform, config);
1278
1279         get_active_consoles(config);
1280
1281         return 0;
1282 }
1283
1284 static int save_config(struct platform *p, struct config *config)
1285 {
1286         struct platform_powerpc *platform = to_platform_powerpc(p);
1287         struct config *defaults;
1288         int rc;
1289
1290         defaults = talloc_zero(platform, struct config);
1291         config_set_defaults(defaults);
1292
1293         rc = update_config(platform, config, defaults);
1294
1295         talloc_free(defaults);
1296         return rc;
1297 }
1298
1299 static void pre_boot(struct platform *p, const struct config *config)
1300 {
1301         struct platform_powerpc *platform = to_platform_powerpc(p);
1302
1303         if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1304                 platform->clear_ipmi_bootdev(platform, false);
1305
1306         if (platform->set_os_boot_sensor)
1307                 platform->set_os_boot_sensor(platform);
1308 }
1309
1310 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1311 {
1312         struct platform_powerpc *platform = p->platform_data;
1313         char *buf, *filename;
1314         int len, rc;
1315
1316         filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1317         rc = read_file(platform, filename, &buf, &len);
1318         if (rc == 0)
1319                 sysinfo->type = talloc_steal(sysinfo, buf);
1320         talloc_free(filename);
1321
1322         filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1323         rc = read_file(platform, filename, &buf, &len);
1324         if (rc == 0)
1325                 sysinfo->identifier = talloc_steal(sysinfo, buf);
1326         talloc_free(filename);
1327
1328         sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1329         if (platform->ipmi)
1330                 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1331
1332         if (platform->ipmi)
1333                 get_ipmi_bmc_versions(p, sysinfo);
1334
1335         if (platform->get_platform_versions)
1336                 platform->get_platform_versions(sysinfo);
1337
1338         return 0;
1339 }
1340
1341 static bool probe(struct platform *p, void *ctx)
1342 {
1343         struct platform_powerpc *platform;
1344         struct stat statbuf;
1345         bool bmc_present;
1346         int rc;
1347
1348         /* we need a device tree */
1349         rc = stat("/proc/device-tree", &statbuf);
1350         if (rc)
1351                 return false;
1352
1353         if (!S_ISDIR(statbuf.st_mode))
1354                 return false;
1355
1356         platform = talloc_zero(ctx, struct platform_powerpc);
1357         list_init(&platform->params);
1358
1359         p->platform_data = platform;
1360
1361         bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1362
1363         if (ipmi_present() && bmc_present) {
1364                 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1365                 platform->ipmi = ipmi_open(platform);
1366                 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1367                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1368                 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1369         } else if (!stat(sysparams_dir, &statbuf)) {
1370                 pb_debug("platform: using sysparams for IPMI paramters\n");
1371                 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1372                 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1373
1374         } else {
1375                 pb_log("platform: no IPMI parameter support\n");
1376         }
1377
1378         if (bmc_present)
1379                 platform->get_platform_versions = hostboot_load_versions;
1380
1381         return true;
1382 }
1383
1384
1385 static struct platform platform_powerpc = {
1386         .name                   = "powerpc",
1387         .dhcp_arch_id           = 0x000e,
1388         .probe                  = probe,
1389         .load_config            = load_config,
1390         .save_config            = save_config,
1391         .pre_boot               = pre_boot,
1392         .get_sysinfo            = get_sysinfo,
1393 };
1394
1395 register_platform(platform_powerpc);