11 #include <asm/byteorder.h>
13 #include <file/file.h>
14 #include <talloc/talloc.h>
15 #include <list/list.h>
17 #include <process/process.h>
18 #include <types/types.h>
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. */
34 struct list_item list;
37 struct platform_powerpc {
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,
47 int (*set_os_boot_sensor)(
48 struct platform_powerpc *platform);
49 void (*get_platform_versions)(struct system_info *info);
52 static const char *known_params[] = {
61 "petitboot,snapshots?",
63 "petitboot,http_proxy",
64 "petitboot,https_proxy",
68 #define to_platform_powerpc(p) \
69 (struct platform_powerpc *)(p->platform_data)
71 /* a partition max a max size of 64k * 16bytes = 1M */
72 static const int max_partition_size = 64 * 1024 * 16;
74 static bool param_is_known(const char *param, unsigned int len)
76 const char *known_param;
79 for (i = 0; known_params[i]; i++) {
80 known_param = known_params[i];
81 if (len == strlen(known_param) &&
82 !strncmp(param, known_param, len))
89 static int parse_nvram_params(struct platform_powerpc *platform,
92 char *pos, *name, *value;
93 unsigned int paramlen;
96 /* discard 2 header lines:
103 for (i = 0; i < len; i++) {
111 fprintf(stderr, "failure parsing nvram output\n");
115 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
116 unsigned int namelen;
120 newline = strchr(pos, '\n');
126 paramlen = strlen(pos);
129 value = strchr(pos, '=');
133 namelen = value - name;
137 if (!param_is_known(name, namelen))
142 param = talloc(platform, struct param);
143 param->modified = false;
144 param->name = talloc_strndup(platform, name, namelen);
145 param->value = talloc_strdup(platform, value);
146 list_add(&platform->params, ¶m->list);
152 static int parse_nvram(struct platform_powerpc *platform)
154 struct process *process;
159 argv[1] = "--print-config";
160 argv[2] = "--partition";
164 process = process_create(platform);
165 process->path = "nvram";
166 process->argv = argv;
167 process->keep_stdout = true;
169 rc = process_run_sync(process);
171 if (rc || !process_exit_ok(process)) {
172 fprintf(stderr, "nvram process returned "
173 "non-zero exit status\n");
176 rc = parse_nvram_params(platform, process->stdout_buf,
177 process->stdout_len);
180 process_release(process);
184 static int write_nvram(struct platform_powerpc *platform)
186 struct process *process;
192 argv[1] = "--update-config";
194 argv[3] = "--partition";
198 process = process_create(platform);
199 process->path = "nvram";
200 process->argv = argv;
202 list_for_each_entry(&platform->params, param, list) {
205 if (!param->modified)
208 paramstr = talloc_asprintf(platform, "%s=%s",
209 param->name, param->value);
212 rc = process_run_sync(process);
214 talloc_free(paramstr);
216 if (rc || !process_exit_ok(process)) {
218 pb_log("nvram update process returned "
219 "non-zero exit status\n");
224 process_release(process);
228 static const char *get_param(struct platform_powerpc *platform,
233 list_for_each_entry(&platform->params, param, list)
234 if (!strcmp(param->name, name))
239 static void set_param(struct platform_powerpc *platform, const char *name,
244 list_for_each_entry(&platform->params, param, list) {
245 if (strcmp(param->name, name))
248 if (!strcmp(param->value, value))
251 talloc_free(param->value);
252 param->value = talloc_strdup(param, value);
253 param->modified = true;
258 param = talloc(platform, struct param);
259 param->modified = true;
260 param->name = talloc_strdup(platform, name);
261 param->value = talloc_strdup(platform, value);
262 list_add(&platform->params, ¶m->list);
265 static int parse_hwaddr(struct interface_config *ifconf, char *str)
269 if (strlen(str) != strlen("00:00:00:00:00:00"))
272 for (i = 0; i < HWADDR_SIZE; i++) {
276 byte[0] = str[i * 3 + 0];
277 byte[1] = str[i * 3 + 1];
280 x = strtoul(byte, &endp, 16);
281 if (endp != byte + 2)
284 ifconf->hwaddr[i] = x & 0xff;
290 static int parse_one_interface_config(struct config *config,
293 struct interface_config *ifconf;
296 ifconf = talloc_zero(config, struct interface_config);
298 if (!confstr || !strlen(confstr))
301 /* first token should be the mac address */
302 tok = strtok_r(confstr, ",", &saveptr);
306 if (parse_hwaddr(ifconf, tok))
309 /* second token is the method */
310 tok = strtok_r(NULL, ",", &saveptr);
311 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
312 ifconf->ignore = true;
314 } else if (!strcmp(tok, "dhcp")) {
315 ifconf->method = CONFIG_METHOD_DHCP;
317 } else if (!strcmp(tok, "static")) {
318 ifconf->method = CONFIG_METHOD_STATIC;
320 /* ip/mask, [optional] gateway, [optional] url */
321 tok = strtok_r(NULL, ",", &saveptr);
324 ifconf->static_config.address =
325 talloc_strdup(ifconf, tok);
327 tok = strtok_r(NULL, ",", &saveptr);
329 ifconf->static_config.gateway =
330 talloc_strdup(ifconf, tok);
333 tok = strtok_r(NULL, ",", &saveptr);
335 ifconf->static_config.url =
336 talloc_strdup(ifconf, tok);
340 pb_log("Unknown network configuration method %s\n", tok);
344 config->network.interfaces = talloc_realloc(config,
345 config->network.interfaces,
346 struct interface_config *,
347 ++config->network.n_interfaces);
349 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
357 static int parse_one_dns_config(struct config *config,
360 char *tok, *saveptr = NULL;
362 for (tok = strtok_r(confstr, ",", &saveptr); tok;
363 tok = strtok_r(NULL, ",", &saveptr)) {
365 char *server = talloc_strdup(config, tok);
367 config->network.dns_servers = talloc_realloc(config,
368 config->network.dns_servers, const char *,
369 ++config->network.n_dns_servers);
371 config->network.dns_servers[config->network.n_dns_servers - 1]
378 static void populate_network_config(struct platform_powerpc *platform,
379 struct config *config)
381 char *val, *saveptr = NULL;
385 cval = get_param(platform, "petitboot,network");
386 if (!cval || !strlen(cval))
389 val = talloc_strdup(config, cval);
394 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
398 if (!strncasecmp(tok, "dns,", strlen("dns,")))
399 parse_one_dns_config(config, tok + strlen("dns,"));
401 parse_one_interface_config(config, tok);
408 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
410 char *delim = strchr(*pos, ' ');
411 int len, prefix = 0, rc = -1;
412 enum device_type type;
414 if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
415 prefix = strlen("uuid:");
416 opt->boot_type = BOOT_DEVICE_UUID;
418 } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
419 prefix = strlen("mac:");
420 opt->boot_type = BOOT_DEVICE_UUID;
423 type = find_device_type(*pos);
424 if (type != DEVICE_TYPE_UNKNOWN) {
426 opt->boot_type = BOOT_DEVICE_TYPE;
431 if (opt->boot_type == BOOT_DEVICE_UUID) {
433 len = (int)(delim - *pos) - prefix;
437 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
440 /* Always advance pointer to next option or end */
444 *pos += strlen(*pos);
449 static void populate_bootdev_config(struct platform_powerpc *platform,
450 struct config *config)
452 struct autoboot_option *opt, *new = NULL;
453 char *pos, *end, *old_dev = NULL;
454 unsigned int n_new = 0;
458 /* Check for old-style bootdev */
459 val = get_param(platform, "petitboot,bootdev");
460 if (val && strlen(val)) {
461 pos = talloc_strdup(config, val);
462 if (!strncmp(val, "uuid:", strlen("uuid:")))
463 old_dev = talloc_strdup(config,
464 val + strlen("uuid:"));
465 else if (!strncmp(val, "mac:", strlen("mac:")))
466 old_dev = talloc_strdup(config,
467 val + strlen("mac:"));
470 /* Check for ordered bootdevs */
471 val = get_param(platform, "petitboot,bootdevs");
472 if (!val || !strlen(val)) {
475 pos = talloc_strdup(config, val);
476 end = strchr(pos, '\0');
479 while (pos && pos < end) {
480 opt = talloc(config, struct autoboot_option);
482 if (read_bootdev(config, &pos, opt)) {
483 pb_log("bootdev config is in an unknown format "
484 "(expected uuid:... or mac:...)\n");
489 new = talloc_realloc(config, new, struct autoboot_option,
497 if (!n_new && !old_dev) {
498 /* If autoboot has been disabled, clear the default options */
499 if (!config->autoboot_enabled) {
500 talloc_free(config->autoboot_opts);
501 config->n_autoboot_opts = 0;
506 conflict = old_dev && (!n_new ||
507 new[0].boot_type == BOOT_DEVICE_TYPE ||
508 /* Canonical UUIDs are 36 characters long */
509 strncmp(new[0].uuid, old_dev, 36));
512 talloc_free(config->autoboot_opts);
513 config->autoboot_opts = new;
514 config->n_autoboot_opts = n_new;
519 * Difference detected, defer to old format in case it has been updated
522 pb_debug("Old autoboot bootdev detected\n");
523 talloc_free(config->autoboot_opts);
524 config->autoboot_opts = talloc(config, struct autoboot_option);
525 config->autoboot_opts[0].boot_type = BOOT_DEVICE_UUID;
526 config->autoboot_opts[0].uuid = talloc_strdup(config, old_dev);
527 config->n_autoboot_opts = 1;
530 static void set_proxy_variables(struct config *config)
532 if (config->http_proxy)
533 setenv("http_proxy", config->http_proxy, 1);
535 unsetenv("http_proxy");
537 if (config->https_proxy)
538 setenv("https_proxy", config->https_proxy, 1);
540 unsetenv("https_proxy");
543 static void populate_config(struct platform_powerpc *platform,
544 struct config *config)
548 unsigned long timeout;
550 /* if the "auto-boot?' property is present and "false", disable auto
552 val = get_param(platform, "auto-boot?");
553 config->autoboot_enabled = !val || strcmp(val, "false");
555 val = get_param(platform, "petitboot,timeout");
557 timeout = strtoul(val, &end, 10);
559 if (timeout >= INT_MAX)
561 config->autoboot_timeout_sec = (int)timeout;
565 val = get_param(platform, "petitboot,language");
566 config->lang = val ? talloc_strdup(config, val) : NULL;
568 populate_network_config(platform, config);
570 populate_bootdev_config(platform, config);
572 if (!config->debug) {
573 val = get_param(platform, "petitboot,debug?");
574 config->debug = val && !strcmp(val, "true");
577 val = get_param(platform, "petitboot,write?");
579 config->allow_writes = !strcmp(val, "true");
581 val = get_param(platform, "petitboot,snapshots?");
583 config->disable_snapshots = !strcmp(val, "false");
585 val = get_param(platform, "petitboot,console");
587 config->boot_console = talloc_strdup(config, val);
588 /* If a full path is already set we don't want to override it */
589 config->manual_console = config->boot_console &&
590 !strchr(config->boot_console, '[');
592 val = get_param(platform, "petitboot,http_proxy");
594 config->http_proxy = talloc_strdup(config, val);
595 val = get_param(platform, "petitboot,https_proxy");
597 config->https_proxy = talloc_strdup(config, val);
598 set_proxy_variables(config);
601 static char *iface_config_str(void *ctx, struct interface_config *config)
605 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
606 * different hardware address formats */
607 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
608 config->hwaddr[0], config->hwaddr[1],
609 config->hwaddr[2], config->hwaddr[3],
610 config->hwaddr[4], config->hwaddr[5]);
612 if (config->ignore) {
613 str = talloc_asprintf_append(str, "ignore");
615 } else if (config->method == CONFIG_METHOD_DHCP) {
616 str = talloc_asprintf_append(str, "dhcp");
618 } else if (config->method == CONFIG_METHOD_STATIC) {
619 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
620 config->static_config.address,
621 config->static_config.gateway ? "," : "",
622 config->static_config.gateway ?: "",
623 config->static_config.url ? "," : "",
624 config->static_config.url ?: "");
629 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
634 str = talloc_strdup(ctx, "dns,");
635 for (i = 0; i < n; i++) {
636 str = talloc_asprintf_append(str, "%s%s",
644 static void update_string_config(struct platform_powerpc *platform,
645 const char *name, const char *value)
649 cur = get_param(platform, name);
651 /* don't set an empty parameter if it doesn't already exist */
652 if (!cur && !strlen(value))
655 set_param(platform, name, value);
658 static void update_network_config(struct platform_powerpc *platform,
659 struct config *config)
665 * Don't store IPMI overrides to NVRAM. If this was a persistent
666 * override it was already stored in NVRAM by
667 * get_ipmi_network_override()
669 if (config->network.n_interfaces &&
670 config->network.interfaces[0]->override)
673 val = talloc_strdup(platform, "");
675 for (i = 0; i < config->network.n_interfaces; i++) {
676 char *iface_str = iface_config_str(platform,
677 config->network.interfaces[i]);
678 val = talloc_asprintf_append(val, "%s%s",
679 *val == '\0' ? "" : " ", iface_str);
680 talloc_free(iface_str);
683 if (config->network.n_dns_servers) {
684 char *dns_str = dns_config_str(platform,
685 config->network.dns_servers,
686 config->network.n_dns_servers);
687 val = talloc_asprintf_append(val, "%s%s",
688 *val == '\0' ? "" : " ", dns_str);
689 talloc_free(dns_str);
692 update_string_config(platform, "petitboot,network", val);
697 static void update_bootdev_config(struct platform_powerpc *platform,
698 struct config *config)
700 char *val = NULL, *boot_str = NULL, *tmp = NULL, *first = NULL;
701 struct autoboot_option *opt;
702 const char delim = ' ';
705 if (!config->n_autoboot_opts)
707 else if (config->autoboot_opts[0].boot_type == BOOT_DEVICE_UUID)
708 first = talloc_asprintf(config, "uuid:%s",
709 config->autoboot_opts[0].uuid);
713 for (i = 0; i < config->n_autoboot_opts; i++) {
714 opt = &config->autoboot_opts[i];
715 switch (opt->boot_type) {
716 case BOOT_DEVICE_TYPE:
717 boot_str = talloc_asprintf(config, "%s%c",
718 device_type_name(opt->type),
721 case BOOT_DEVICE_UUID:
722 boot_str = talloc_asprintf(config, "uuid:%s%c",
726 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
729 update_string_config(platform, "petitboot,bootdevs", val);
730 update_string_config(platform, "petitboot,bootdev", first);
733 talloc_free(boot_str);
736 static int update_config(struct platform_powerpc *platform,
737 struct config *config, struct config *defaults)
742 if (config->autoboot_enabled == defaults->autoboot_enabled)
745 val = config->autoboot_enabled ? "true" : "false";
746 update_string_config(platform, "auto-boot?", val);
748 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
751 val = tmp = talloc_asprintf(platform, "%d",
752 config->autoboot_timeout_sec);
754 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
755 platform->clear_ipmi_bootdev) {
756 platform->clear_ipmi_bootdev(platform,
757 config->ipmi_bootdev_persistent);
758 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
759 config->ipmi_bootdev_persistent = false;
762 update_string_config(platform, "petitboot,timeout", val);
766 val = config->lang ?: "";
767 update_string_config(platform, "petitboot,language", val);
769 if (config->allow_writes == defaults->allow_writes)
772 val = config->allow_writes ? "true" : "false";
773 update_string_config(platform, "petitboot,write?", val);
775 if (!config->manual_console) {
776 val = config->boot_console ?: "";
777 update_string_config(platform, "petitboot,console", val);
780 val = config->http_proxy ?: "";
781 update_string_config(platform, "petitboot,http_proxy", val);
782 val = config->https_proxy ?: "";
783 update_string_config(platform, "petitboot,https_proxy", val);
784 set_proxy_variables(config);
786 update_network_config(platform, config);
788 update_bootdev_config(platform, config);
790 return write_nvram(platform);
793 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
796 config->ipmi_bootdev = bootdev;
797 config->ipmi_bootdev_persistent = persistent;
800 case IPMI_BOOTDEV_NONE:
801 case IPMI_BOOTDEV_DISK:
802 case IPMI_BOOTDEV_NETWORK:
803 case IPMI_BOOTDEV_CDROM:
806 case IPMI_BOOTDEV_SETUP:
807 config->autoboot_enabled = false;
809 case IPMI_BOOTDEV_SAFE:
810 config->autoboot_enabled = false;
811 config->safe_mode = true;
816 static int read_bootdev_sysparam(const char *name, uint8_t *val)
822 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
823 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
825 fd = open(path, O_RDONLY);
827 pb_debug("powerpc: can't access sysparam %s\n",
832 rc = read(fd, buf, sizeof(buf));
836 /* bootdev definitions should only be one byte in size */
838 pb_debug("powerpc: sysparam %s read returned %d\n",
843 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
845 if (!ipmi_bootdev_is_valid(buf[0]))
852 static int write_bootdev_sysparam(const char *name, uint8_t val)
857 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
858 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
860 fd = open(path, O_WRONLY);
862 pb_debug("powerpc: can't access sysparam %s for writing\n",
869 rc = write(fd, &val, sizeof(val));
870 if (rc == sizeof(val)) {
875 if (rc <= 0 && errno != EINTR) {
876 pb_log("powerpc: error updating sysparam %s: %s",
877 name, strerror(errno));
886 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
891 static int clear_ipmi_bootdev_sysparams(
892 struct platform_powerpc *platform __attribute__((unused)),
896 /* invalidate default-boot-device setting */
897 write_bootdev_sysparam("default-boot-device", 0xff);
899 /* invalidate next-boot-device setting */
900 write_bootdev_sysparam("next-boot-device", 0xff);
905 static int get_ipmi_bootdev_sysparams(
906 struct platform_powerpc *platform __attribute__((unused)),
907 uint8_t *bootdev, bool *persistent)
909 uint8_t next_bootdev, default_bootdev;
910 bool next_valid, default_valid;
913 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
914 next_valid = rc == 0;
916 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
917 default_valid = rc == 0;
919 /* nothing valid? no need to change the config */
920 if (!next_valid && !default_valid)
923 *persistent = !next_valid;
924 *bootdev = next_valid ? next_bootdev : default_bootdev;
928 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
929 bool persistent __attribute__((unused)))
934 0x05, /* parameter selector: boot flags */
935 0x80, /* data 1: valid */
936 0x00, /* data 2: bootdev: no override */
937 0x00, /* data 3: system defaults */
938 0x00, /* data 4: no request for shared mode, mux defaults */
939 0x00, /* data 5: no instance request */
942 resp_len = sizeof(resp);
944 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
945 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
952 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
953 uint8_t *bootdev, bool *persistent)
959 0x05, /* parameter selector: boot flags */
960 0x00, /* no set selector */
961 0x00, /* no block selector */
964 resp_len = sizeof(resp);
965 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
966 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
971 pb_log("platform: error reading IPMI boot options\n");
975 if (resp_len != sizeof(resp)) {
976 pb_log("platform: unexpected length (%d) in "
977 "boot options response\n", resp_len);
981 pb_debug("IPMI get_bootdev response:\n");
982 for (int i = 0; i < resp_len; i++)
983 pb_debug("%x ", resp[i]);
987 pb_log("platform: non-zero completion code %d from IPMI req\n",
992 /* check for correct parameter version */
993 if ((resp[1] & 0xf) != 0x1) {
994 pb_log("platform: unexpected version (0x%x) in "
995 "boot options response\n", resp[0]);
999 /* check for valid paramters */
1000 if (resp[2] & 0x80) {
1001 pb_debug("platform: boot options are invalid/locked\n");
1005 *persistent = false;
1007 /* check for valid flags */
1008 if (!(resp[3] & 0x80)) {
1009 pb_debug("platform: boot flags are invalid, ignoring\n");
1013 *persistent = resp[3] & 0x40;
1014 *bootdev = (resp[4] >> 2) & 0x0f;
1018 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
1024 0x00, /* sensor number: os boot */
1025 0xA9, /* operation: set everything */
1026 0x00, /* sensor reading: none */
1027 0x40, /* assertion mask lsb: set state 6 */
1028 0x00, /* assertion mask msb: none */
1029 0x00, /* deassertion mask lsb: none */
1030 0x00, /* deassertion mask msb: none */
1031 0x00, /* event data 1: none */
1032 0x00, /* event data 2: none */
1033 0x00, /* event data 3: none */
1036 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
1037 if (sensor_number < 0) {
1038 pb_log("Couldn't find OS boot sensor in device tree\n");
1042 req[0] = sensor_number;
1044 resp_len = sizeof(resp);
1046 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
1047 IPMI_CMD_SENSOR_SET,
1050 ipmi_timeout); return 0;
1055 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
1057 struct platform_powerpc *platform = p->platform_data;
1058 uint16_t resp_len = 8;
1060 uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1063 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1064 IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1069 pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1071 if (rc == 0 && resp_len > 0) {
1072 for (i = 2; i < resp_len; i++) {
1073 pb_debug(" %x", resp[i]);
1074 buf[i - 2] = resp[i];
1082 * Retrieve info from the "Get Device ID" IPMI commands.
1083 * See Chapter 20.1 in the IPMIv2 specification.
1085 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1087 struct platform_powerpc *platform = p->platform_data;
1088 uint16_t resp_len = 16;
1089 uint8_t resp[16], bcd;
1090 uint32_t aux_version;
1093 /* Retrieve info from current side */
1094 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1095 IPMI_CMD_APP_GET_DEVICE_ID,
1100 pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1102 for (i = 0; i < resp_len; i++) {
1103 pb_debug(" %x", resp[i]);
1108 if (rc == 0 && resp_len == 16) {
1109 info->bmc_current = talloc_array(info, char *, 4);
1110 info->n_bmc_current = 4;
1112 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1114 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1116 bcd = resp[4] & 0x0f;
1117 bcd += 10 * (resp[4] >> 4);
1118 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1119 info->bmc_current[2] = talloc_asprintf(info,
1120 "Firmware version: %u.%02u.%05u",
1121 resp[3], bcd, aux_version);
1122 bcd = resp[5] & 0x0f;
1123 bcd += 10 * (resp[5] >> 4);
1124 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1127 pb_log("Failed to retrieve Device ID from IPMI\n");
1129 /* Retrieve info from golden side */
1130 memset(resp, 0, sizeof(resp));
1132 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1133 IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1138 pb_debug("BMC golden resp [%d][%d]:\n", rc, resp_len);
1140 for (i = 0; i < resp_len; i++) {
1141 pb_debug(" %x", resp[i]);
1146 if (rc == 0 && resp_len == 16) {
1147 info->bmc_golden = talloc_array(info, char *, 4);
1148 info->n_bmc_golden = 4;
1150 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1152 info->bmc_golden[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1154 bcd = resp[4] & 0x0f;
1155 bcd += 10 * (resp[4] >> 4);
1156 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1157 info->bmc_golden[2] = talloc_asprintf(info,
1158 "Firmware version: %u.%02u.%u",
1159 resp[3], bcd, aux_version);
1160 bcd = resp[5] & 0x0f;
1161 bcd += 10 * (resp[5] >> 4);
1162 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1165 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1168 static void get_ipmi_network_override(struct platform_powerpc *platform,
1169 struct config *config)
1171 uint16_t min_len = 12, resp_len = 53, version;
1172 const uint32_t magic_value = 0x21706221;
1173 uint8_t resp[resp_len];
1178 0x61, /* parameter selector: OEM section (network) */
1179 0x00, /* no set selector */
1180 0x00, /* no block selector */
1183 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1184 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1189 pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1191 for (i = 0; i < resp_len; i++) {
1192 pb_debug(" %02x", resp[i]);
1193 if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1195 else if (i && (i + 1) % 8 == 0)
1202 pb_debug("IPMI network config option unavailable\n");
1206 if (resp_len < min_len) {
1207 pb_debug("IPMI net response too small\n");
1212 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1217 /* Check for correct parameter version */
1218 if ((resp[1] & 0xf) != 0x1) {
1219 pb_log("platform: unexpected version (0x%x) in network override response\n",
1224 /* Check that the parameters are valid */
1225 if (resp[2] & 0x80) {
1226 pb_debug("platform: network override is invalid/locked\n");
1230 /* Check for valid parameters in the boot flags section */
1231 if (!(resp[3] & 0x80)) {
1232 pb_debug("platform: network override valid flag not set\n");
1235 /* Read the persistent flag; if it is set we need to save this config */
1236 persistent = resp[3] & 0x40;
1238 pb_debug("platform: network override is persistent\n");
1240 /* Check 4-byte cookie value */
1242 memcpy(&cookie, &resp[i], sizeof(cookie));
1243 cookie = __be32_to_cpu(cookie);
1244 if (cookie != magic_value) {
1245 pb_log("%s: Incorrect cookie %x\n", __func__, cookie);
1248 i += sizeof(cookie);
1250 /* Check 2-byte version number */
1251 memcpy(&version, &resp[i], sizeof(version));
1252 version = __be16_to_cpu(version);
1253 i += sizeof(version);
1255 pb_debug("Unexpected version: %u\n", version);
1259 /* Interpret the rest of the interface config */
1260 rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1262 if (!rc && persistent) {
1263 /* Write this new config to NVRAM */
1264 update_network_config(platform, config);
1265 rc = write_nvram(platform);
1267 pb_log("platform: Failed to save persistent interface override\n");
1271 static void get_active_consoles(struct config *config)
1274 char *fsp_prop = NULL;
1276 config->n_consoles = 2;
1277 config->consoles = talloc_array(config, char *, config->n_consoles);
1278 if (!config->consoles)
1281 config->consoles[0] = talloc_asprintf(config->consoles,
1282 "/dev/hvc0 [IPMI / Serial]");
1283 config->consoles[1] = talloc_asprintf(config->consoles,
1286 fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1287 if (stat(fsp_prop, &sbuf) == 0) {
1288 /* FSP based machines also have a separate serial console */
1289 config->consoles = talloc_realloc(config, config->consoles,
1290 char *, config->n_consoles + 1);
1291 if (!config->consoles)
1293 config->consoles[config->n_consoles++] = talloc_asprintf(
1295 "/dev/hvc1 [Serial]");
1300 config->n_consoles = 0;
1301 pb_log("Failed to allocate memory for consoles\n");
1304 static int load_config(struct platform *p, struct config *config)
1306 struct platform_powerpc *platform = to_platform_powerpc(p);
1309 rc = parse_nvram(platform);
1313 populate_config(platform, config);
1315 if (platform->get_ipmi_bootdev) {
1316 bool bootdev_persistent;
1317 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1318 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1319 &bootdev_persistent);
1320 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1321 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1326 get_ipmi_network_override(platform, config);
1328 get_active_consoles(config);
1333 static int save_config(struct platform *p, struct config *config)
1335 struct platform_powerpc *platform = to_platform_powerpc(p);
1336 struct config *defaults;
1339 defaults = talloc_zero(platform, struct config);
1340 config_set_defaults(defaults);
1342 rc = update_config(platform, config, defaults);
1344 talloc_free(defaults);
1348 static void pre_boot(struct platform *p, const struct config *config)
1350 struct platform_powerpc *platform = to_platform_powerpc(p);
1352 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1353 platform->clear_ipmi_bootdev(platform, false);
1355 if (platform->set_os_boot_sensor)
1356 platform->set_os_boot_sensor(platform);
1359 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1361 struct platform_powerpc *platform = p->platform_data;
1362 char *buf, *filename;
1365 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1366 rc = read_file(platform, filename, &buf, &len);
1368 sysinfo->type = talloc_steal(sysinfo, buf);
1369 talloc_free(filename);
1371 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1372 rc = read_file(platform, filename, &buf, &len);
1374 sysinfo->identifier = talloc_steal(sysinfo, buf);
1375 talloc_free(filename);
1377 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1379 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1382 get_ipmi_bmc_versions(p, sysinfo);
1384 if (platform->get_platform_versions)
1385 platform->get_platform_versions(sysinfo);
1390 static bool probe(struct platform *p, void *ctx)
1392 struct platform_powerpc *platform;
1393 struct stat statbuf;
1397 /* we need a device tree */
1398 rc = stat("/proc/device-tree", &statbuf);
1402 if (!S_ISDIR(statbuf.st_mode))
1405 platform = talloc_zero(ctx, struct platform_powerpc);
1406 list_init(&platform->params);
1408 p->platform_data = platform;
1410 bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1412 if (ipmi_present() && bmc_present) {
1413 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1414 platform->ipmi = ipmi_open(platform);
1415 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1416 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1417 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1418 } else if (!stat(sysparams_dir, &statbuf)) {
1419 pb_debug("platform: using sysparams for IPMI paramters\n");
1420 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1421 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1424 pb_log("platform: no IPMI parameter support\n");
1428 platform->get_platform_versions = hostboot_load_versions;
1434 static struct platform platform_powerpc = {
1436 .dhcp_arch_id = 0x000e,
1438 .load_config = load_config,
1439 .save_config = save_config,
1440 .pre_boot = pre_boot,
1441 .get_sysinfo = get_sysinfo,
1444 register_platform(platform_powerpc);