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?",
66 #define to_platform_powerpc(p) \
67 (struct platform_powerpc *)(p->platform_data)
69 /* a partition max a max size of 64k * 16bytes = 1M */
70 static const int max_partition_size = 64 * 1024 * 16;
72 static bool param_is_known(const char *param, unsigned int len)
74 const char *known_param;
77 for (i = 0; known_params[i]; i++) {
78 known_param = known_params[i];
79 if (len == strlen(known_param) &&
80 !strncmp(param, known_param, len))
87 static int parse_nvram_params(struct platform_powerpc *platform,
90 char *pos, *name, *value;
91 unsigned int paramlen;
94 /* discard 2 header lines:
101 for (i = 0; i < len; i++) {
109 fprintf(stderr, "failure parsing nvram output\n");
113 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
114 unsigned int namelen;
118 newline = strchr(pos, '\n');
124 paramlen = strlen(pos);
127 value = strchr(pos, '=');
131 namelen = value - name;
135 if (!param_is_known(name, namelen))
140 param = talloc(platform, struct param);
141 param->modified = false;
142 param->name = talloc_strndup(platform, name, namelen);
143 param->value = talloc_strdup(platform, value);
144 list_add(&platform->params, ¶m->list);
150 static int parse_nvram(struct platform_powerpc *platform)
152 struct process *process;
157 argv[1] = "--print-config";
158 argv[2] = "--partition";
162 process = process_create(platform);
163 process->path = "nvram";
164 process->argv = argv;
165 process->keep_stdout = true;
167 rc = process_run_sync(process);
169 if (rc || !process_exit_ok(process)) {
170 fprintf(stderr, "nvram process returned "
171 "non-zero exit status\n");
174 rc = parse_nvram_params(platform, process->stdout_buf,
175 process->stdout_len);
178 process_release(process);
182 static int write_nvram(struct platform_powerpc *platform)
184 struct process *process;
190 argv[1] = "--update-config";
192 argv[3] = "--partition";
196 process = process_create(platform);
197 process->path = "nvram";
198 process->argv = argv;
200 list_for_each_entry(&platform->params, param, list) {
203 if (!param->modified)
206 paramstr = talloc_asprintf(platform, "%s=%s",
207 param->name, param->value);
210 rc = process_run_sync(process);
212 talloc_free(paramstr);
214 if (rc || !process_exit_ok(process)) {
216 pb_log("nvram update process returned "
217 "non-zero exit status\n");
222 process_release(process);
226 static const char *get_param(struct platform_powerpc *platform,
231 list_for_each_entry(&platform->params, param, list)
232 if (!strcmp(param->name, name))
237 static void set_param(struct platform_powerpc *platform, const char *name,
242 list_for_each_entry(&platform->params, param, list) {
243 if (strcmp(param->name, name))
246 if (!strcmp(param->value, value))
249 talloc_free(param->value);
250 param->value = talloc_strdup(param, value);
251 param->modified = true;
256 param = talloc(platform, struct param);
257 param->modified = true;
258 param->name = talloc_strdup(platform, name);
259 param->value = talloc_strdup(platform, value);
260 list_add(&platform->params, ¶m->list);
263 static int parse_hwaddr(struct interface_config *ifconf, char *str)
267 if (strlen(str) != strlen("00:00:00:00:00:00"))
270 for (i = 0; i < HWADDR_SIZE; i++) {
274 byte[0] = str[i * 3 + 0];
275 byte[1] = str[i * 3 + 1];
278 x = strtoul(byte, &endp, 16);
279 if (endp != byte + 2)
282 ifconf->hwaddr[i] = x & 0xff;
288 static int parse_one_interface_config(struct config *config,
291 struct interface_config *ifconf;
294 ifconf = talloc_zero(config, struct interface_config);
296 if (!confstr || !strlen(confstr))
299 /* first token should be the mac address */
300 tok = strtok_r(confstr, ",", &saveptr);
304 if (parse_hwaddr(ifconf, tok))
307 /* second token is the method */
308 tok = strtok_r(NULL, ",", &saveptr);
309 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
310 ifconf->ignore = true;
312 } else if (!strcmp(tok, "dhcp")) {
313 ifconf->method = CONFIG_METHOD_DHCP;
315 } else if (!strcmp(tok, "static")) {
316 ifconf->method = CONFIG_METHOD_STATIC;
318 /* ip/mask, [optional] gateway, [optional] url */
319 tok = strtok_r(NULL, ",", &saveptr);
322 ifconf->static_config.address =
323 talloc_strdup(ifconf, tok);
325 tok = strtok_r(NULL, ",", &saveptr);
327 ifconf->static_config.gateway =
328 talloc_strdup(ifconf, tok);
331 tok = strtok_r(NULL, ",", &saveptr);
333 ifconf->static_config.url =
334 talloc_strdup(ifconf, tok);
338 pb_log("Unknown network configuration method %s\n", tok);
342 config->network.interfaces = talloc_realloc(config,
343 config->network.interfaces,
344 struct interface_config *,
345 ++config->network.n_interfaces);
347 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
355 static int parse_one_dns_config(struct config *config,
358 char *tok, *saveptr = NULL;
360 for (tok = strtok_r(confstr, ",", &saveptr); tok;
361 tok = strtok_r(NULL, ",", &saveptr)) {
363 char *server = talloc_strdup(config, tok);
365 config->network.dns_servers = talloc_realloc(config,
366 config->network.dns_servers, const char *,
367 ++config->network.n_dns_servers);
369 config->network.dns_servers[config->network.n_dns_servers - 1]
376 static void populate_network_config(struct platform_powerpc *platform,
377 struct config *config)
379 char *val, *saveptr = NULL;
383 cval = get_param(platform, "petitboot,network");
384 if (!cval || !strlen(cval))
387 val = talloc_strdup(config, cval);
392 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
396 if (!strncasecmp(tok, "dns,", strlen("dns,")))
397 parse_one_dns_config(config, tok + strlen("dns,"));
399 parse_one_interface_config(config, tok);
406 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
408 char *delim = strchr(*pos, ' ');
409 int len, prefix = 0, rc = -1;
410 enum device_type type;
412 if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
413 prefix = strlen("uuid:");
414 opt->boot_type = BOOT_DEVICE_UUID;
416 } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
417 prefix = strlen("mac:");
418 opt->boot_type = BOOT_DEVICE_UUID;
421 type = find_device_type(*pos);
422 if (type != DEVICE_TYPE_UNKNOWN) {
424 opt->boot_type = BOOT_DEVICE_TYPE;
429 if (opt->boot_type == BOOT_DEVICE_UUID) {
431 len = (int)(delim - *pos) - prefix;
435 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
438 /* Always advance pointer to next option or end */
442 *pos += strlen(*pos);
447 static void populate_bootdev_config(struct platform_powerpc *platform,
448 struct config *config)
450 struct autoboot_option *opt, *new = NULL;
451 char *pos, *end, *old_dev = NULL;
452 unsigned int n_new = 0;
456 /* Check for old-style bootdev */
457 val = get_param(platform, "petitboot,bootdev");
458 if (val && strlen(val)) {
459 pos = talloc_strdup(config, val);
460 if (!strncmp(val, "uuid:", strlen("uuid:")))
461 old_dev = talloc_strdup(config,
462 val + strlen("uuid:"));
463 else if (!strncmp(val, "mac:", strlen("mac:")))
464 old_dev = talloc_strdup(config,
465 val + strlen("mac:"));
468 /* Check for ordered bootdevs */
469 val = get_param(platform, "petitboot,bootdevs");
470 if (!val || !strlen(val)) {
473 pos = talloc_strdup(config, val);
474 end = strchr(pos, '\0');
477 while (pos && pos < end) {
478 opt = talloc(config, struct autoboot_option);
480 if (read_bootdev(config, &pos, opt)) {
481 pb_log("bootdev config is in an unknown format "
482 "(expected uuid:... or mac:...)\n");
487 new = talloc_realloc(config, new, struct autoboot_option,
495 if (!n_new && !old_dev) {
496 /* If autoboot has been disabled, clear the default options */
497 if (!config->autoboot_enabled) {
498 talloc_free(config->autoboot_opts);
499 config->n_autoboot_opts = 0;
504 conflict = old_dev && (!n_new ||
505 new[0].boot_type == BOOT_DEVICE_TYPE ||
506 /* Canonical UUIDs are 36 characters long */
507 strncmp(new[0].uuid, old_dev, 36));
510 talloc_free(config->autoboot_opts);
511 config->autoboot_opts = new;
512 config->n_autoboot_opts = n_new;
517 * Difference detected, defer to old format in case it has been updated
520 pb_debug("Old autoboot bootdev detected\n");
521 talloc_free(config->autoboot_opts);
522 config->autoboot_opts = talloc(config, struct autoboot_option);
523 config->autoboot_opts[0].boot_type = BOOT_DEVICE_UUID;
524 config->autoboot_opts[0].uuid = talloc_strdup(config, old_dev);
525 config->n_autoboot_opts = 1;
528 static void populate_config(struct platform_powerpc *platform,
529 struct config *config)
533 unsigned long timeout;
535 /* if the "auto-boot?' property is present and "false", disable auto
537 val = get_param(platform, "auto-boot?");
538 config->autoboot_enabled = !val || strcmp(val, "false");
540 val = get_param(platform, "petitboot,timeout");
542 timeout = strtoul(val, &end, 10);
544 if (timeout >= INT_MAX)
546 config->autoboot_timeout_sec = (int)timeout;
550 val = get_param(platform, "petitboot,language");
551 config->lang = val ? talloc_strdup(config, val) : NULL;
553 populate_network_config(platform, config);
555 populate_bootdev_config(platform, config);
557 if (!config->debug) {
558 val = get_param(platform, "petitboot,debug?");
559 config->debug = val && !strcmp(val, "true");
562 val = get_param(platform, "petitboot,write?");
564 config->allow_writes = !strcmp(val, "true");
566 val = get_param(platform, "petitboot,snapshots?");
568 config->disable_snapshots = !strcmp(val, "false");
570 val = get_param(platform, "petitboot,tty");
572 config->boot_console = talloc_strdup(config, val);
575 static char *iface_config_str(void *ctx, struct interface_config *config)
579 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
580 * different hardware address formats */
581 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
582 config->hwaddr[0], config->hwaddr[1],
583 config->hwaddr[2], config->hwaddr[3],
584 config->hwaddr[4], config->hwaddr[5]);
586 if (config->ignore) {
587 str = talloc_asprintf_append(str, "ignore");
589 } else if (config->method == CONFIG_METHOD_DHCP) {
590 str = talloc_asprintf_append(str, "dhcp");
592 } else if (config->method == CONFIG_METHOD_STATIC) {
593 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
594 config->static_config.address,
595 config->static_config.gateway ? "," : "",
596 config->static_config.gateway ?: "",
597 config->static_config.url ? "," : "",
598 config->static_config.url ?: "");
603 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
608 str = talloc_strdup(ctx, "dns,");
609 for (i = 0; i < n; i++) {
610 str = talloc_asprintf_append(str, "%s%s",
618 static void update_string_config(struct platform_powerpc *platform,
619 const char *name, const char *value)
623 cur = get_param(platform, name);
625 /* don't set an empty parameter if it doesn't already exist */
626 if (!cur && !strlen(value))
629 set_param(platform, name, value);
632 static void update_network_config(struct platform_powerpc *platform,
633 struct config *config)
639 * Don't store IPMI overrides to NVRAM. If this was a persistent
640 * override it was already stored in NVRAM by
641 * get_ipmi_network_override()
643 if (config->network.n_interfaces &&
644 config->network.interfaces[0]->override)
647 val = talloc_strdup(platform, "");
649 for (i = 0; i < config->network.n_interfaces; i++) {
650 char *iface_str = iface_config_str(platform,
651 config->network.interfaces[i]);
652 val = talloc_asprintf_append(val, "%s%s",
653 *val == '\0' ? "" : " ", iface_str);
654 talloc_free(iface_str);
657 if (config->network.n_dns_servers) {
658 char *dns_str = dns_config_str(platform,
659 config->network.dns_servers,
660 config->network.n_dns_servers);
661 val = talloc_asprintf_append(val, "%s%s",
662 *val == '\0' ? "" : " ", dns_str);
663 talloc_free(dns_str);
666 update_string_config(platform, "petitboot,network", val);
671 static void update_bootdev_config(struct platform_powerpc *platform,
672 struct config *config)
674 char *val = NULL, *boot_str = NULL, *tmp = NULL, *first = NULL;
675 struct autoboot_option *opt;
676 const char delim = ' ';
679 if (!config->n_autoboot_opts)
681 else if (config->autoboot_opts[0].boot_type == BOOT_DEVICE_UUID)
682 first = talloc_asprintf(config, "uuid:%s",
683 config->autoboot_opts[0].uuid);
687 for (i = 0; i < config->n_autoboot_opts; i++) {
688 opt = &config->autoboot_opts[i];
689 switch (opt->boot_type) {
690 case BOOT_DEVICE_TYPE:
691 boot_str = talloc_asprintf(config, "%s%c",
692 device_type_name(opt->type),
695 case BOOT_DEVICE_UUID:
696 boot_str = talloc_asprintf(config, "uuid:%s%c",
700 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
703 update_string_config(platform, "petitboot,bootdevs", val);
704 update_string_config(platform, "petitboot,bootdev", first);
707 talloc_free(boot_str);
710 static int update_config(struct platform_powerpc *platform,
711 struct config *config, struct config *defaults)
716 if (config->autoboot_enabled == defaults->autoboot_enabled)
719 val = config->autoboot_enabled ? "true" : "false";
720 update_string_config(platform, "auto-boot?", val);
722 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
725 val = tmp = talloc_asprintf(platform, "%d",
726 config->autoboot_timeout_sec);
728 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
729 platform->clear_ipmi_bootdev) {
730 platform->clear_ipmi_bootdev(platform,
731 config->ipmi_bootdev_persistent);
732 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
733 config->ipmi_bootdev_persistent = false;
736 update_string_config(platform, "petitboot,timeout", val);
740 val = config->lang ?: "";
741 update_string_config(platform, "petitboot,language", val);
743 if (config->allow_writes == defaults->allow_writes)
746 val = config->allow_writes ? "true" : "false";
747 update_string_config(platform, "petitboot,write?", val);
749 val = config->boot_console ?: "";
750 update_string_config(platform, "petitboot,tty", val);
752 update_network_config(platform, config);
754 update_bootdev_config(platform, config);
756 return write_nvram(platform);
759 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
762 config->ipmi_bootdev = bootdev;
763 config->ipmi_bootdev_persistent = persistent;
766 case IPMI_BOOTDEV_NONE:
767 case IPMI_BOOTDEV_DISK:
768 case IPMI_BOOTDEV_NETWORK:
769 case IPMI_BOOTDEV_CDROM:
772 case IPMI_BOOTDEV_SETUP:
773 config->autoboot_enabled = false;
775 case IPMI_BOOTDEV_SAFE:
776 config->autoboot_enabled = false;
777 config->safe_mode = true;
782 static int read_bootdev_sysparam(const char *name, uint8_t *val)
788 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
789 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
791 fd = open(path, O_RDONLY);
793 pb_debug("powerpc: can't access sysparam %s\n",
798 rc = read(fd, buf, sizeof(buf));
802 /* bootdev definitions should only be one byte in size */
804 pb_debug("powerpc: sysparam %s read returned %d\n",
809 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
811 if (!ipmi_bootdev_is_valid(buf[0]))
818 static int write_bootdev_sysparam(const char *name, uint8_t val)
823 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
824 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
826 fd = open(path, O_WRONLY);
828 pb_debug("powerpc: can't access sysparam %s for writing\n",
835 rc = write(fd, &val, sizeof(val));
836 if (rc == sizeof(val)) {
841 if (rc <= 0 && errno != EINTR) {
842 pb_log("powerpc: error updating sysparam %s: %s",
843 name, strerror(errno));
852 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
857 static int clear_ipmi_bootdev_sysparams(
858 struct platform_powerpc *platform __attribute__((unused)),
862 /* invalidate default-boot-device setting */
863 write_bootdev_sysparam("default-boot-device", 0xff);
865 /* invalidate next-boot-device setting */
866 write_bootdev_sysparam("next-boot-device", 0xff);
871 static int get_ipmi_bootdev_sysparams(
872 struct platform_powerpc *platform __attribute__((unused)),
873 uint8_t *bootdev, bool *persistent)
875 uint8_t next_bootdev, default_bootdev;
876 bool next_valid, default_valid;
879 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
880 next_valid = rc == 0;
882 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
883 default_valid = rc == 0;
885 /* nothing valid? no need to change the config */
886 if (!next_valid && !default_valid)
889 *persistent = !next_valid;
890 *bootdev = next_valid ? next_bootdev : default_bootdev;
894 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
895 bool persistent __attribute__((unused)))
900 0x05, /* parameter selector: boot flags */
901 0x80, /* data 1: valid */
902 0x00, /* data 2: bootdev: no override */
903 0x00, /* data 3: system defaults */
904 0x00, /* data 4: no request for shared mode, mux defaults */
905 0x00, /* data 5: no instance request */
908 resp_len = sizeof(resp);
910 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
911 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
918 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
919 uint8_t *bootdev, bool *persistent)
925 0x05, /* parameter selector: boot flags */
926 0x00, /* no set selector */
927 0x00, /* no block selector */
930 resp_len = sizeof(resp);
931 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
932 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
937 pb_log("platform: error reading IPMI boot options\n");
941 if (resp_len != sizeof(resp)) {
942 pb_log("platform: unexpected length (%d) in "
943 "boot options response\n", resp_len);
947 pb_debug("IPMI get_bootdev response:\n");
948 for (int i = 0; i < resp_len; i++)
949 pb_debug("%x ", resp[i]);
953 pb_log("platform: non-zero completion code %d from IPMI req\n",
958 /* check for correct parameter version */
959 if ((resp[1] & 0xf) != 0x1) {
960 pb_log("platform: unexpected version (0x%x) in "
961 "boot options response\n", resp[0]);
965 /* check for valid paramters */
966 if (resp[2] & 0x80) {
967 pb_debug("platform: boot options are invalid/locked\n");
973 /* check for valid flags */
974 if (!(resp[3] & 0x80)) {
975 pb_debug("platform: boot flags are invalid, ignoring\n");
979 *persistent = resp[3] & 0x40;
980 *bootdev = (resp[4] >> 2) & 0x0f;
984 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
990 0x00, /* sensor number: os boot */
991 0xA9, /* operation: set everything */
992 0x00, /* sensor reading: none */
993 0x40, /* assertion mask lsb: set state 6 */
994 0x00, /* assertion mask msb: none */
995 0x00, /* deassertion mask lsb: none */
996 0x00, /* deassertion mask msb: none */
997 0x00, /* event data 1: none */
998 0x00, /* event data 2: none */
999 0x00, /* event data 3: none */
1002 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
1003 if (sensor_number < 0) {
1004 pb_log("Couldn't find OS boot sensor in device tree\n");
1008 req[0] = sensor_number;
1010 resp_len = sizeof(resp);
1012 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
1013 IPMI_CMD_SENSOR_SET,
1016 ipmi_timeout); return 0;
1021 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
1023 struct platform_powerpc *platform = p->platform_data;
1024 uint16_t resp_len = 8;
1026 uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1029 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1030 IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1035 pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1037 if (rc == 0 && resp_len > 0) {
1038 for (i = 2; i < resp_len; i++) {
1039 pb_debug(" %x", resp[i]);
1040 buf[i - 2] = resp[i];
1048 * Retrieve info from the "Get Device ID" IPMI commands.
1049 * See Chapter 20.1 in the IPMIv2 specification.
1051 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1053 struct platform_powerpc *platform = p->platform_data;
1054 uint16_t resp_len = 16;
1055 uint8_t resp[16], bcd;
1056 uint32_t aux_version;
1059 /* Retrieve info from current side */
1060 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1061 IPMI_CMD_APP_GET_DEVICE_ID,
1066 pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1068 for (i = 0; i < resp_len; i++) {
1069 pb_debug(" %x", resp[i]);
1074 if (rc == 0 && resp_len == 16) {
1075 info->bmc_current = talloc_array(info, char *, 4);
1076 info->n_bmc_current = 4;
1078 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1080 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1082 bcd = resp[4] & 0x0f;
1083 bcd += 10 * (resp[4] >> 4);
1084 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1085 info->bmc_current[2] = talloc_asprintf(info,
1086 "Firmware version: %u.%02u.%05u",
1087 resp[3], bcd, aux_version);
1088 bcd = resp[5] & 0x0f;
1089 bcd += 10 * (resp[5] >> 4);
1090 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1093 pb_log("Failed to retrieve Device ID from IPMI\n");
1095 /* Retrieve info from golden side */
1096 memset(resp, 0, sizeof(resp));
1098 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1099 IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1104 pb_debug("BMC golden resp [%d][%d]:\n", rc, resp_len);
1106 for (i = 0; i < resp_len; i++) {
1107 pb_debug(" %x", resp[i]);
1112 if (rc == 0 && resp_len == 16) {
1113 info->bmc_golden = talloc_array(info, char *, 4);
1114 info->n_bmc_golden = 4;
1116 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1118 info->bmc_golden[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1120 bcd = resp[4] & 0x0f;
1121 bcd += 10 * (resp[4] >> 4);
1122 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1123 info->bmc_golden[2] = talloc_asprintf(info,
1124 "Firmware version: %u.%02u.%u",
1125 resp[3], bcd, aux_version);
1126 bcd = resp[5] & 0x0f;
1127 bcd += 10 * (resp[5] >> 4);
1128 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1131 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1134 static void get_ipmi_network_override(struct platform_powerpc *platform,
1135 struct config *config)
1137 uint16_t min_len = 12, resp_len = 53, version;
1138 const uint32_t magic_value = 0x21706221;
1139 uint8_t resp[resp_len];
1144 0x61, /* parameter selector: OEM section (network) */
1145 0x00, /* no set selector */
1146 0x00, /* no block selector */
1149 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1150 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1155 pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1157 for (i = 0; i < resp_len; i++) {
1158 pb_debug(" %02x", resp[i]);
1159 if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1161 else if (i && (i + 1) % 8 == 0)
1168 pb_debug("IPMI network config option unavailable\n");
1172 if (resp_len < min_len) {
1173 pb_debug("IPMI net response too small\n");
1178 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1183 /* Check for correct parameter version */
1184 if ((resp[1] & 0xf) != 0x1) {
1185 pb_log("platform: unexpected version (0x%x) in network override response\n",
1190 /* Check that the parameters are valid */
1191 if (resp[2] & 0x80) {
1192 pb_debug("platform: network override is invalid/locked\n");
1196 /* Check for valid parameters in the boot flags section */
1197 if (!(resp[3] & 0x80)) {
1198 pb_debug("platform: network override valid flag not set\n");
1201 /* Read the persistent flag; if it is set we need to save this config */
1202 persistent = resp[3] & 0x40;
1204 pb_debug("platform: network override is persistent\n");
1206 /* Check 4-byte cookie value */
1208 memcpy(&cookie, &resp[i], sizeof(cookie));
1209 cookie = __be32_to_cpu(cookie);
1210 if (cookie != magic_value) {
1211 pb_log("%s: Incorrect cookie %x\n", __func__, cookie);
1214 i += sizeof(cookie);
1216 /* Check 2-byte version number */
1217 memcpy(&version, &resp[i], sizeof(version));
1218 version = __be16_to_cpu(version);
1219 i += sizeof(version);
1221 pb_debug("Unexpected version: %u\n", version);
1225 /* Interpret the rest of the interface config */
1226 rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1228 if (!rc && persistent) {
1229 /* Write this new config to NVRAM */
1230 update_network_config(platform, config);
1231 rc = write_nvram(platform);
1233 pb_log("platform: Failed to save persistent interface override\n");
1237 static void get_active_consoles(struct config *config)
1240 char *fsp_prop = NULL;
1242 config->n_consoles = 2;
1243 config->consoles = talloc_array(config, char *, config->n_consoles);
1244 if (!config->consoles)
1247 config->consoles[0] = talloc_asprintf(config->consoles,
1248 "/dev/hvc0 [IPMI / Serial]");
1249 config->consoles[1] = talloc_asprintf(config->consoles,
1252 fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1253 if (stat(fsp_prop, &sbuf) == 0) {
1254 /* FSP based machines also have a separate serial console */
1255 config->consoles = talloc_realloc(config, config->consoles,
1256 char *, config->n_consoles + 1);
1257 if (!config->consoles)
1259 config->consoles[config->n_consoles++] = talloc_asprintf(
1261 "/dev/hvc1 [Serial]");
1266 config->n_consoles = 0;
1267 pb_log("Failed to allocate memory for consoles\n");
1270 static int load_config(struct platform *p, struct config *config)
1272 struct platform_powerpc *platform = to_platform_powerpc(p);
1275 rc = parse_nvram(platform);
1279 populate_config(platform, config);
1281 if (platform->get_ipmi_bootdev) {
1282 bool bootdev_persistent;
1283 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1284 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1285 &bootdev_persistent);
1286 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1287 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1292 get_ipmi_network_override(platform, config);
1294 get_active_consoles(config);
1299 static int save_config(struct platform *p, struct config *config)
1301 struct platform_powerpc *platform = to_platform_powerpc(p);
1302 struct config *defaults;
1305 defaults = talloc_zero(platform, struct config);
1306 config_set_defaults(defaults);
1308 rc = update_config(platform, config, defaults);
1310 talloc_free(defaults);
1314 static void pre_boot(struct platform *p, const struct config *config)
1316 struct platform_powerpc *platform = to_platform_powerpc(p);
1318 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1319 platform->clear_ipmi_bootdev(platform, false);
1321 if (platform->set_os_boot_sensor)
1322 platform->set_os_boot_sensor(platform);
1325 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1327 struct platform_powerpc *platform = p->platform_data;
1328 char *buf, *filename;
1331 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1332 rc = read_file(platform, filename, &buf, &len);
1334 sysinfo->type = talloc_steal(sysinfo, buf);
1335 talloc_free(filename);
1337 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1338 rc = read_file(platform, filename, &buf, &len);
1340 sysinfo->identifier = talloc_steal(sysinfo, buf);
1341 talloc_free(filename);
1343 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1345 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1348 get_ipmi_bmc_versions(p, sysinfo);
1350 if (platform->get_platform_versions)
1351 platform->get_platform_versions(sysinfo);
1356 static bool probe(struct platform *p, void *ctx)
1358 struct platform_powerpc *platform;
1359 struct stat statbuf;
1363 /* we need a device tree */
1364 rc = stat("/proc/device-tree", &statbuf);
1368 if (!S_ISDIR(statbuf.st_mode))
1371 platform = talloc_zero(ctx, struct platform_powerpc);
1372 list_init(&platform->params);
1374 p->platform_data = platform;
1376 bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1378 if (ipmi_present() && bmc_present) {
1379 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1380 platform->ipmi = ipmi_open(platform);
1381 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1382 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1383 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1384 } else if (!stat(sysparams_dir, &statbuf)) {
1385 pb_debug("platform: using sysparams for IPMI paramters\n");
1386 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1387 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1390 pb_log("platform: no IPMI parameter support\n");
1394 platform->get_platform_versions = hostboot_load_versions;
1400 static struct platform platform_powerpc = {
1402 .dhcp_arch_id = 0x000e,
1404 .load_config = load_config,
1405 .save_config = save_config,
1406 .pre_boot = pre_boot,
1407 .get_sysinfo = get_sysinfo,
1410 register_platform(platform_powerpc);