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?",
65 #define to_platform_powerpc(p) \
66 (struct platform_powerpc *)(p->platform_data)
68 /* a partition max a max size of 64k * 16bytes = 1M */
69 static const int max_partition_size = 64 * 1024 * 16;
71 static bool param_is_known(const char *param, unsigned int len)
73 const char *known_param;
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))
86 static int parse_nvram_params(struct platform_powerpc *platform,
89 char *pos, *name, *value;
90 unsigned int paramlen;
93 /* discard 2 header lines:
100 for (i = 0; i < len; i++) {
108 fprintf(stderr, "failure parsing nvram output\n");
112 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
113 unsigned int namelen;
117 newline = strchr(pos, '\n');
123 paramlen = strlen(pos);
126 value = strchr(pos, '=');
130 namelen = value - name;
134 if (!param_is_known(name, namelen))
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, ¶m->list);
149 static int parse_nvram(struct platform_powerpc *platform)
151 struct process *process;
156 argv[1] = "--print-config";
157 argv[2] = "--partition";
161 process = process_create(platform);
162 process->path = "nvram";
163 process->argv = argv;
164 process->keep_stdout = true;
166 rc = process_run_sync(process);
168 if (rc || !process_exit_ok(process)) {
169 fprintf(stderr, "nvram process returned "
170 "non-zero exit status\n");
173 rc = parse_nvram_params(platform, process->stdout_buf,
174 process->stdout_len);
177 process_release(process);
181 static int write_nvram(struct platform_powerpc *platform)
183 struct process *process;
189 argv[1] = "--update-config";
191 argv[3] = "--partition";
195 process = process_create(platform);
196 process->path = "nvram";
197 process->argv = argv;
199 list_for_each_entry(&platform->params, param, list) {
202 if (!param->modified)
205 paramstr = talloc_asprintf(platform, "%s=%s",
206 param->name, param->value);
209 rc = process_run_sync(process);
211 talloc_free(paramstr);
213 if (rc || !process_exit_ok(process)) {
215 pb_log("nvram update process returned "
216 "non-zero exit status\n");
221 process_release(process);
225 static const char *get_param(struct platform_powerpc *platform,
230 list_for_each_entry(&platform->params, param, list)
231 if (!strcmp(param->name, name))
236 static void set_param(struct platform_powerpc *platform, const char *name,
241 list_for_each_entry(&platform->params, param, list) {
242 if (strcmp(param->name, name))
245 if (!strcmp(param->value, value))
248 talloc_free(param->value);
249 param->value = talloc_strdup(param, value);
250 param->modified = true;
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, ¶m->list);
262 static int parse_hwaddr(struct interface_config *ifconf, char *str)
266 if (strlen(str) != strlen("00:00:00:00:00:00"))
269 for (i = 0; i < HWADDR_SIZE; i++) {
273 byte[0] = str[i * 3 + 0];
274 byte[1] = str[i * 3 + 1];
277 x = strtoul(byte, &endp, 16);
278 if (endp != byte + 2)
281 ifconf->hwaddr[i] = x & 0xff;
287 static int parse_one_interface_config(struct config *config,
290 struct interface_config *ifconf;
293 ifconf = talloc_zero(config, struct interface_config);
295 if (!confstr || !strlen(confstr))
298 /* first token should be the mac address */
299 tok = strtok_r(confstr, ",", &saveptr);
303 if (parse_hwaddr(ifconf, tok))
306 /* second token is the method */
307 tok = strtok_r(NULL, ",", &saveptr);
308 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
309 ifconf->ignore = true;
311 } else if (!strcmp(tok, "dhcp")) {
312 ifconf->method = CONFIG_METHOD_DHCP;
314 } else if (!strcmp(tok, "static")) {
315 ifconf->method = CONFIG_METHOD_STATIC;
317 /* ip/mask, [optional] gateway, [optional] url */
318 tok = strtok_r(NULL, ",", &saveptr);
321 ifconf->static_config.address =
322 talloc_strdup(ifconf, tok);
324 tok = strtok_r(NULL, ",", &saveptr);
326 ifconf->static_config.gateway =
327 talloc_strdup(ifconf, tok);
330 tok = strtok_r(NULL, ",", &saveptr);
332 ifconf->static_config.url =
333 talloc_strdup(ifconf, tok);
337 pb_log("Unknown network configuration method %s\n", tok);
341 config->network.interfaces = talloc_realloc(config,
342 config->network.interfaces,
343 struct interface_config *,
344 ++config->network.n_interfaces);
346 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
354 static int parse_one_dns_config(struct config *config,
357 char *tok, *saveptr = NULL;
359 for (tok = strtok_r(confstr, ",", &saveptr); tok;
360 tok = strtok_r(NULL, ",", &saveptr)) {
362 char *server = talloc_strdup(config, tok);
364 config->network.dns_servers = talloc_realloc(config,
365 config->network.dns_servers, const char *,
366 ++config->network.n_dns_servers);
368 config->network.dns_servers[config->network.n_dns_servers - 1]
375 static void populate_network_config(struct platform_powerpc *platform,
376 struct config *config)
378 char *val, *saveptr = NULL;
382 cval = get_param(platform, "petitboot,network");
383 if (!cval || !strlen(cval))
386 val = talloc_strdup(config, cval);
391 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
395 if (!strncasecmp(tok, "dns,", strlen("dns,")))
396 parse_one_dns_config(config, tok + strlen("dns,"));
398 parse_one_interface_config(config, tok);
405 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
407 char *delim = strchr(*pos, ' ');
408 int len, prefix = 0, rc = -1;
409 enum device_type type;
411 if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
412 prefix = strlen("uuid:");
413 opt->boot_type = BOOT_DEVICE_UUID;
415 } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
416 prefix = strlen("mac:");
417 opt->boot_type = BOOT_DEVICE_UUID;
420 type = find_device_type(*pos);
421 if (type != DEVICE_TYPE_UNKNOWN) {
423 opt->boot_type = BOOT_DEVICE_TYPE;
428 if (opt->boot_type == BOOT_DEVICE_UUID) {
430 len = (int)(delim - *pos) - prefix;
434 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
437 /* Always advance pointer to next option or end */
441 *pos += strlen(*pos);
446 static void populate_bootdev_config(struct platform_powerpc *platform,
447 struct config *config)
449 struct autoboot_option *opt, *new = NULL;
450 char *pos, *end, *old_dev = NULL;
451 unsigned int n_new = 0;
455 /* Check for old-style bootdev */
456 val = get_param(platform, "petitboot,bootdev");
457 if (val && strlen(val)) {
458 pos = talloc_strdup(config, val);
459 if (!strncmp(val, "uuid:", strlen("uuid:")))
460 old_dev = talloc_strdup(config,
461 val + strlen("uuid:"));
462 else if (!strncmp(val, "mac:", strlen("mac:")))
463 old_dev = talloc_strdup(config,
464 val + strlen("mac:"));
467 /* Check for ordered bootdevs */
468 val = get_param(platform, "petitboot,bootdevs");
469 if (!val || !strlen(val)) {
472 pos = talloc_strdup(config, val);
473 end = strchr(pos, '\0');
476 while (pos && pos < end) {
477 opt = talloc(config, struct autoboot_option);
479 if (read_bootdev(config, &pos, opt)) {
480 pb_log("bootdev config is in an unknown format "
481 "(expected uuid:... or mac:...)\n");
486 new = talloc_realloc(config, new, struct autoboot_option,
494 if (!n_new && !old_dev) {
495 /* If autoboot has been disabled, clear the default options */
496 if (!config->autoboot_enabled) {
497 talloc_free(config->autoboot_opts);
498 config->n_autoboot_opts = 0;
503 conflict = old_dev && (!n_new ||
504 new[0].boot_type == BOOT_DEVICE_TYPE ||
505 /* Canonical UUIDs are 36 characters long */
506 strncmp(new[0].uuid, old_dev, 36));
509 talloc_free(config->autoboot_opts);
510 config->autoboot_opts = new;
511 config->n_autoboot_opts = n_new;
516 * Difference detected, defer to old format in case it has been updated
519 pb_debug("Old autoboot bootdev detected\n");
520 talloc_free(config->autoboot_opts);
521 config->autoboot_opts = talloc(config, struct autoboot_option);
522 config->autoboot_opts[0].boot_type = BOOT_DEVICE_UUID;
523 config->autoboot_opts[0].uuid = talloc_strdup(config, old_dev);
524 config->n_autoboot_opts = 1;
527 static void populate_config(struct platform_powerpc *platform,
528 struct config *config)
532 unsigned long timeout;
534 /* if the "auto-boot?' property is present and "false", disable auto
536 val = get_param(platform, "auto-boot?");
537 config->autoboot_enabled = !val || strcmp(val, "false");
539 val = get_param(platform, "petitboot,timeout");
541 timeout = strtoul(val, &end, 10);
543 if (timeout >= INT_MAX)
545 config->autoboot_timeout_sec = (int)timeout;
549 val = get_param(platform, "petitboot,language");
550 config->lang = val ? talloc_strdup(config, val) : NULL;
552 populate_network_config(platform, config);
554 populate_bootdev_config(platform, config);
556 if (!config->debug) {
557 val = get_param(platform, "petitboot,debug?");
558 config->debug = val && !strcmp(val, "true");
561 val = get_param(platform, "petitboot,write?");
563 config->allow_writes = !strcmp(val, "true");
565 val = get_param(platform, "petitboot,snapshots?");
567 config->disable_snapshots = !strcmp(val, "false");
570 static char *iface_config_str(void *ctx, struct interface_config *config)
574 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
575 * different hardware address formats */
576 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
577 config->hwaddr[0], config->hwaddr[1],
578 config->hwaddr[2], config->hwaddr[3],
579 config->hwaddr[4], config->hwaddr[5]);
581 if (config->ignore) {
582 str = talloc_asprintf_append(str, "ignore");
584 } else if (config->method == CONFIG_METHOD_DHCP) {
585 str = talloc_asprintf_append(str, "dhcp");
587 } else if (config->method == CONFIG_METHOD_STATIC) {
588 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
589 config->static_config.address,
590 config->static_config.gateway ? "," : "",
591 config->static_config.gateway ?: "",
592 config->static_config.url ? "," : "",
593 config->static_config.url ?: "");
598 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
603 str = talloc_strdup(ctx, "dns,");
604 for (i = 0; i < n; i++) {
605 str = talloc_asprintf_append(str, "%s%s",
613 static void update_string_config(struct platform_powerpc *platform,
614 const char *name, const char *value)
618 cur = get_param(platform, name);
620 /* don't set an empty parameter if it doesn't already exist */
621 if (!cur && !strlen(value))
624 set_param(platform, name, value);
627 static void update_network_config(struct platform_powerpc *platform,
628 struct config *config)
633 val = talloc_strdup(platform, "");
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);
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);
652 update_string_config(platform, "petitboot,network", val);
657 static void update_bootdev_config(struct platform_powerpc *platform,
658 struct config *config)
660 char *val = NULL, *boot_str = NULL, *tmp = NULL, *first = NULL;
661 struct autoboot_option *opt;
662 const char delim = ' ';
665 if (!config->n_autoboot_opts)
667 else if (config->autoboot_opts[0].boot_type == BOOT_DEVICE_UUID)
668 first = talloc_asprintf(config, "uuid:%s",
669 config->autoboot_opts[0].uuid);
673 for (i = 0; i < config->n_autoboot_opts; i++) {
674 opt = &config->autoboot_opts[i];
675 switch (opt->boot_type) {
676 case BOOT_DEVICE_TYPE:
677 boot_str = talloc_asprintf(config, "%s%c",
678 device_type_name(opt->type),
681 case BOOT_DEVICE_UUID:
682 boot_str = talloc_asprintf(config, "uuid:%s%c",
686 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
689 update_string_config(platform, "petitboot,bootdevs", val);
690 update_string_config(platform, "petitboot,bootdev", first);
693 talloc_free(boot_str);
696 static int update_config(struct platform_powerpc *platform,
697 struct config *config, struct config *defaults)
702 if (config->autoboot_enabled == defaults->autoboot_enabled)
705 val = config->autoboot_enabled ? "true" : "false";
706 update_string_config(platform, "auto-boot?", val);
708 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
711 val = tmp = talloc_asprintf(platform, "%d",
712 config->autoboot_timeout_sec);
714 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
715 platform->clear_ipmi_bootdev) {
716 platform->clear_ipmi_bootdev(platform,
717 config->ipmi_bootdev_persistent);
718 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
719 config->ipmi_bootdev_persistent = false;
722 update_string_config(platform, "petitboot,timeout", val);
726 val = config->lang ?: "";
727 update_string_config(platform, "petitboot,language", val);
729 if (config->allow_writes == defaults->allow_writes)
732 val = config->allow_writes ? "true" : "false";
733 update_string_config(platform, "petitboot,write?", val);
735 update_network_config(platform, config);
737 update_bootdev_config(platform, config);
739 return write_nvram(platform);
742 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
745 config->ipmi_bootdev = bootdev;
746 config->ipmi_bootdev_persistent = persistent;
749 case IPMI_BOOTDEV_NONE:
750 case IPMI_BOOTDEV_DISK:
751 case IPMI_BOOTDEV_NETWORK:
752 case IPMI_BOOTDEV_CDROM:
755 case IPMI_BOOTDEV_SETUP:
756 config->autoboot_enabled = false;
758 case IPMI_BOOTDEV_SAFE:
759 config->autoboot_enabled = false;
760 config->safe_mode = true;
765 static int read_bootdev_sysparam(const char *name, uint8_t *val)
771 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
772 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
774 fd = open(path, O_RDONLY);
776 pb_debug("powerpc: can't access sysparam %s\n",
781 rc = read(fd, buf, sizeof(buf));
785 /* bootdev definitions should only be one byte in size */
787 pb_debug("powerpc: sysparam %s read returned %d\n",
792 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
794 if (!ipmi_bootdev_is_valid(buf[0]))
801 static int write_bootdev_sysparam(const char *name, uint8_t val)
806 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
807 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
809 fd = open(path, O_WRONLY);
811 pb_debug("powerpc: can't access sysparam %s for writing\n",
818 rc = write(fd, &val, sizeof(val));
819 if (rc == sizeof(val)) {
824 if (rc <= 0 && errno != EINTR) {
825 pb_log("powerpc: error updating sysparam %s: %s",
826 name, strerror(errno));
835 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
840 static int clear_ipmi_bootdev_sysparams(
841 struct platform_powerpc *platform __attribute__((unused)),
845 /* invalidate default-boot-device setting */
846 write_bootdev_sysparam("default-boot-device", 0xff);
848 /* invalidate next-boot-device setting */
849 write_bootdev_sysparam("next-boot-device", 0xff);
854 static int get_ipmi_bootdev_sysparams(
855 struct platform_powerpc *platform __attribute__((unused)),
856 uint8_t *bootdev, bool *persistent)
858 uint8_t next_bootdev, default_bootdev;
859 bool next_valid, default_valid;
862 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
863 next_valid = rc == 0;
865 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
866 default_valid = rc == 0;
868 /* nothing valid? no need to change the config */
869 if (!next_valid && !default_valid)
872 *persistent = !next_valid;
873 *bootdev = next_valid ? next_bootdev : default_bootdev;
877 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
878 bool persistent __attribute__((unused)))
883 0x05, /* parameter selector: boot flags */
884 0x80, /* data 1: valid */
885 0x00, /* data 2: bootdev: no override */
886 0x00, /* data 3: system defaults */
887 0x00, /* data 4: no request for shared mode, mux defaults */
888 0x00, /* data 5: no instance request */
891 resp_len = sizeof(resp);
893 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
894 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
901 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
902 uint8_t *bootdev, bool *persistent)
908 0x05, /* parameter selector: boot flags */
909 0x00, /* no set selector */
910 0x00, /* no block selector */
913 resp_len = sizeof(resp);
914 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
915 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
920 pb_log("platform: error reading IPMI boot options\n");
924 if (resp_len != sizeof(resp)) {
925 pb_log("platform: unexpected length (%d) in "
926 "boot options response\n", resp_len);
930 pb_debug("IPMI get_bootdev response:\n");
931 for (int i = 0; i < resp_len; i++)
932 pb_debug("%x ", resp[i]);
936 pb_log("platform: non-zero completion code %d from IPMI req\n",
941 /* check for correct parameter version */
942 if ((resp[1] & 0xf) != 0x1) {
943 pb_log("platform: unexpected version (0x%x) in "
944 "boot options response\n", resp[0]);
948 /* check for valid paramters */
949 if (resp[2] & 0x80) {
950 pb_debug("platform: boot options are invalid/locked\n");
956 /* check for valid flags */
957 if (!(resp[3] & 0x80)) {
958 pb_debug("platform: boot flags are invalid, ignoring\n");
962 *persistent = resp[3] & 0x40;
963 *bootdev = (resp[4] >> 2) & 0x0f;
967 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
973 0x00, /* sensor number: os boot */
974 0xA9, /* operation: set everything */
975 0x00, /* sensor reading: none */
976 0x40, /* assertion mask lsb: set state 6 */
977 0x00, /* assertion mask msb: none */
978 0x00, /* deassertion mask lsb: none */
979 0x00, /* deassertion mask msb: none */
980 0x00, /* event data 1: none */
981 0x00, /* event data 2: none */
982 0x00, /* event data 3: none */
985 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
986 if (sensor_number < 0) {
987 pb_log("Couldn't find OS boot sensor in device tree\n");
991 req[0] = sensor_number;
993 resp_len = sizeof(resp);
995 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
999 ipmi_timeout); return 0;
1004 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
1006 struct platform_powerpc *platform = p->platform_data;
1007 uint16_t resp_len = 8;
1009 uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1012 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1013 IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1018 pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1020 if (rc == 0 && resp_len > 0) {
1021 for (i = 2; i < resp_len; i++) {
1022 pb_debug(" %x", resp[i]);
1023 buf[i - 2] = resp[i];
1031 * Retrieve info from the "Get Device ID" IPMI commands.
1032 * See Chapter 20.1 in the IPMIv2 specification.
1034 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1036 struct platform_powerpc *platform = p->platform_data;
1037 uint16_t resp_len = 16;
1038 uint8_t resp[16], bcd;
1039 uint32_t aux_version;
1042 /* Retrieve info from current side */
1043 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1044 IPMI_CMD_APP_GET_DEVICE_ID,
1049 pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1051 for (i = 0; i < resp_len; i++) {
1052 pb_debug(" %x", resp[i]);
1057 if (rc == 0 && resp_len == 16) {
1058 info->bmc_current = talloc_array(info, char *, 4);
1059 info->n_bmc_current = 4;
1061 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1063 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1065 bcd = resp[4] & 0x0f;
1066 bcd += 10 * (resp[4] >> 4);
1067 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1068 info->bmc_current[2] = talloc_asprintf(info,
1069 "Firmware version: %u.%02u.%u",
1070 resp[3], bcd, aux_version);
1071 bcd = resp[5] & 0x0f;
1072 bcd += 10 * (resp[5] >> 4);
1073 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1076 pb_log("Failed to retrieve Device ID from IPMI\n");
1078 /* Retrieve info from golden side */
1079 memset(resp, 0, sizeof(resp));
1081 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1082 IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1087 pb_debug("BMC golden resp [%d][%d]:\n", rc, resp_len);
1089 for (i = 0; i < resp_len; i++) {
1090 pb_debug(" %x", resp[i]);
1095 if (rc == 0 && resp_len == 16) {
1096 info->bmc_golden = talloc_array(info, char *, 4);
1097 info->n_bmc_golden = 4;
1099 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1101 info->bmc_golden[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1103 bcd = resp[4] & 0x0f;
1104 bcd += 10 * (resp[4] >> 4);
1105 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1106 info->bmc_golden[2] = talloc_asprintf(info,
1107 "Firmware version: %u.%02u.%u",
1108 resp[3], bcd, aux_version);
1109 bcd = resp[5] & 0x0f;
1110 bcd += 10 * (resp[5] >> 4);
1111 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1114 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1117 static void get_ipmi_network_override(struct platform_powerpc *platform,
1118 struct config *config)
1120 uint16_t min_len = 12, resp_len = 53, version;
1121 const uint32_t magic_value = 0x21706221;
1122 uint8_t resp[resp_len];
1126 0x61, /* parameter selector: OEM section (network) */
1127 0x00, /* no set selector */
1128 0x00, /* no block selector */
1131 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1132 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1137 pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1139 for (i = 0; i < resp_len; i++) {
1140 pb_debug(" %02x", resp[i]);
1141 if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1143 else if (i && (i + 1) % 8 == 0)
1150 pb_debug("IPMI network config option unavailable\n");
1154 if (resp_len < min_len) {
1155 pb_debug("IPMI net response too small\n");
1160 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1165 /* Check for correct parameter version */
1166 if ((resp[1] & 0xf) != 0x1) {
1167 pb_log("platform: unexpected version (0x%x) in network override response\n",
1172 /* Check for valid parameters. For now ignore the persistent flag */
1173 if (resp[2] & 0x80) {
1174 pb_debug("platform: network override is invalid/locked\n");
1178 /* Check for valid parameters in the boot flags section, ignoring the
1180 if (!(resp[3] & 0x80)) {
1181 pb_debug("platform: network override valid flag not set\n");
1185 /* Check 4-byte cookie value */
1187 memcpy(&cookie, &resp[i], sizeof(cookie));
1188 cookie = __be32_to_cpu(cookie);
1189 if (cookie != magic_value) {
1190 pb_log("%s: Incorrect cookie %x\n", __func__, cookie);
1193 i += sizeof(cookie);
1195 /* Check 2-byte version number */
1196 memcpy(&version, &resp[i], sizeof(version));
1197 version = __be16_to_cpu(version);
1198 i += sizeof(version);
1200 pb_debug("Unexpected version: %u\n", version);
1204 /* Interpret the rest of the interface config */
1205 parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1208 static int load_config(struct platform *p, struct config *config)
1210 struct platform_powerpc *platform = to_platform_powerpc(p);
1213 rc = parse_nvram(platform);
1217 populate_config(platform, config);
1219 if (platform->get_ipmi_bootdev) {
1220 bool bootdev_persistent;
1222 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1223 &bootdev_persistent);
1224 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1225 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1230 get_ipmi_network_override(platform, config);
1235 static int save_config(struct platform *p, struct config *config)
1237 struct platform_powerpc *platform = to_platform_powerpc(p);
1238 struct config *defaults;
1241 defaults = talloc_zero(platform, struct config);
1242 config_set_defaults(defaults);
1244 rc = update_config(platform, config, defaults);
1246 talloc_free(defaults);
1250 static void pre_boot(struct platform *p, const struct config *config)
1252 struct platform_powerpc *platform = to_platform_powerpc(p);
1254 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1255 platform->clear_ipmi_bootdev(platform, false);
1257 if (platform->set_os_boot_sensor)
1258 platform->set_os_boot_sensor(platform);
1261 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1263 struct platform_powerpc *platform = p->platform_data;
1264 char *buf, *filename;
1267 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1268 rc = read_file(platform, filename, &buf, &len);
1270 sysinfo->type = talloc_steal(sysinfo, buf);
1271 talloc_free(filename);
1273 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1274 rc = read_file(platform, filename, &buf, &len);
1276 sysinfo->identifier = talloc_steal(sysinfo, buf);
1277 talloc_free(filename);
1279 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1281 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1284 get_ipmi_bmc_versions(p, sysinfo);
1286 if (platform->get_platform_versions)
1287 platform->get_platform_versions(sysinfo);
1292 static bool probe(struct platform *p, void *ctx)
1294 struct platform_powerpc *platform;
1295 struct stat statbuf;
1298 /* we need a device tree */
1299 rc = stat("/proc/device-tree", &statbuf);
1303 if (!S_ISDIR(statbuf.st_mode))
1306 platform = talloc_zero(ctx, struct platform_powerpc);
1307 list_init(&platform->params);
1309 p->platform_data = platform;
1311 if (ipmi_present()) {
1312 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1313 platform->ipmi = ipmi_open(platform);
1314 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1315 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1316 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1317 } else if (!stat(sysparams_dir, &statbuf)) {
1318 pb_debug("platform: using sysparams for IPMI paramters\n");
1319 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1320 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1323 pb_log("platform: no IPMI parameter support\n");
1326 rc = stat("/proc/device-tree/bmc", &statbuf);
1328 platform->get_platform_versions = hostboot_load_versions;
1334 static struct platform platform_powerpc = {
1336 .dhcp_arch_id = 0x000e,
1338 .load_config = load_config,
1339 .save_config = save_config,
1340 .pre_boot = pre_boot,
1341 .get_sysinfo = get_sysinfo,
1344 register_platform(platform_powerpc);