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>
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. */
35 struct list_item list;
38 struct platform_powerpc {
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,
48 int (*set_os_boot_sensor)(
49 struct platform_powerpc *platform);
50 void (*get_platform_versions)(struct system_info *info);
53 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 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;
291 char *tok, *tok_gw, *tok_url, *saveptr;
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);
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.
330 tok_gw = strtok_r(NULL, ",", &saveptr);
331 tok_url = strtok_r(NULL, ",", &saveptr);
334 if (tok_url || !is_url(tok_gw))
335 ifconf->static_config.gateway =
336 talloc_strdup(ifconf, tok_gw);
342 ifconf->static_config.url =
343 talloc_strdup(ifconf, tok_url);
345 pb_log("Unknown network configuration method %s\n", tok);
349 config->network.interfaces = talloc_realloc(config,
350 config->network.interfaces,
351 struct interface_config *,
352 ++config->network.n_interfaces);
354 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
362 static int parse_one_dns_config(struct config *config,
365 char *tok, *saveptr = NULL;
367 for (tok = strtok_r(confstr, ",", &saveptr); tok;
368 tok = strtok_r(NULL, ",", &saveptr)) {
370 char *server = talloc_strdup(config, tok);
372 config->network.dns_servers = talloc_realloc(config,
373 config->network.dns_servers, const char *,
374 ++config->network.n_dns_servers);
376 config->network.dns_servers[config->network.n_dns_servers - 1]
383 static void populate_network_config(struct platform_powerpc *platform,
384 struct config *config)
386 char *val, *saveptr = NULL;
390 cval = get_param(platform, "petitboot,network");
391 if (!cval || !strlen(cval))
394 val = talloc_strdup(config, cval);
399 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
403 if (!strncasecmp(tok, "dns,", strlen("dns,")))
404 parse_one_dns_config(config, tok + strlen("dns,"));
406 parse_one_interface_config(config, tok);
413 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
415 char *delim = strchr(*pos, ' ');
416 int len, prefix = 0, rc = -1;
417 enum device_type type;
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;
426 type = find_device_type(*pos);
427 if (type != DEVICE_TYPE_UNKNOWN) {
429 opt->boot_type = BOOT_DEVICE_TYPE;
434 if (opt->boot_type == BOOT_DEVICE_UUID) {
436 len = (int)(delim - *pos) - prefix;
438 len = strlen(*pos) - prefix;
441 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
446 /* Always advance pointer to next option or end */
450 *pos += strlen(*pos);
455 static void populate_bootdev_config(struct platform_powerpc *platform,
456 struct config *config)
458 struct autoboot_option *opt, *new = NULL;
460 unsigned int n_new = 0;
463 /* Check for ordered bootdevs */
464 val = get_param(platform, "petitboot,bootdevs");
465 if (!val || !strlen(val)) {
468 pos = talloc_strdup(config, val);
469 end = strchr(pos, '\0');
472 while (pos && pos < end) {
473 opt = talloc(config, struct autoboot_option);
475 if (read_bootdev(config, &pos, opt)) {
476 pb_log("bootdev config is in an unknown format "
477 "(expected uuid:... or mac:...)\n");
482 new = talloc_realloc(config, new, struct autoboot_option,
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;
499 talloc_free(config->autoboot_opts);
500 config->autoboot_opts = new;
501 config->n_autoboot_opts = n_new;
504 static void populate_config(struct platform_powerpc *platform,
505 struct config *config)
509 unsigned long timeout;
511 /* if the "auto-boot?' property is present and "false", disable auto
513 val = get_param(platform, "auto-boot?");
514 config->autoboot_enabled = !val || strcmp(val, "false");
516 val = get_param(platform, "petitboot,timeout");
518 timeout = strtoul(val, &end, 10);
520 if (timeout >= INT_MAX)
522 config->autoboot_timeout_sec = (int)timeout;
526 val = get_param(platform, "petitboot,language");
527 config->lang = val ? talloc_strdup(config, val) : NULL;
529 populate_network_config(platform, config);
531 populate_bootdev_config(platform, config);
533 if (!config->debug) {
534 val = get_param(platform, "petitboot,debug?");
535 config->debug = val && !strcmp(val, "true");
538 val = get_param(platform, "petitboot,write?");
540 config->allow_writes = !strcmp(val, "true");
542 val = get_param(platform, "petitboot,snapshots?");
544 config->disable_snapshots = !strcmp(val, "false");
546 val = get_param(platform, "petitboot,console");
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, '[');
553 val = get_param(platform, "petitboot,http_proxy");
555 config->http_proxy = talloc_strdup(config, val);
556 val = get_param(platform, "petitboot,https_proxy");
558 config->https_proxy = talloc_strdup(config, val);
561 static char *iface_config_str(void *ctx, struct interface_config *config)
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]);
572 if (config->ignore) {
573 str = talloc_asprintf_append(str, "ignore");
575 } else if (config->method == CONFIG_METHOD_DHCP) {
576 str = talloc_asprintf_append(str, "dhcp");
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 ?: "");
589 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
594 str = talloc_strdup(ctx, "dns,");
595 for (i = 0; i < n; i++) {
596 str = talloc_asprintf_append(str, "%s%s",
604 static void update_string_config(struct platform_powerpc *platform,
605 const char *name, const char *value)
609 cur = get_param(platform, name);
611 /* don't set an empty parameter if it doesn't already exist */
612 if (!cur && !strlen(value))
615 set_param(platform, name, value);
618 static void update_network_config(struct platform_powerpc *platform,
619 struct config *config)
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()
629 if (config->network.n_interfaces &&
630 config->network.interfaces[0]->override)
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;
661 struct autoboot_option *opt;
662 const char delim = ' ';
665 if (!config->n_autoboot_opts)
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),
676 case BOOT_DEVICE_UUID:
677 boot_str = talloc_asprintf(config, "uuid:%s%c",
681 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
684 update_string_config(platform, "petitboot,bootdevs", val);
687 talloc_free(boot_str);
690 static int update_config(struct platform_powerpc *platform,
691 struct config *config, struct config *defaults)
696 if (config->autoboot_enabled == defaults->autoboot_enabled)
699 val = config->autoboot_enabled ? "true" : "false";
700 update_string_config(platform, "auto-boot?", val);
702 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
705 val = tmp = talloc_asprintf(platform, "%d",
706 config->autoboot_timeout_sec);
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;
716 update_string_config(platform, "petitboot,timeout", val);
720 val = config->lang ?: "";
721 update_string_config(platform, "petitboot,language", val);
723 if (config->allow_writes == defaults->allow_writes)
726 val = config->allow_writes ? "true" : "false";
727 update_string_config(platform, "petitboot,write?", val);
729 if (!config->manual_console) {
730 val = config->boot_console ?: "";
731 update_string_config(platform, "petitboot,console", val);
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);
739 update_network_config(platform, config);
741 update_bootdev_config(platform, config);
743 return write_nvram(platform);
746 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
749 config->ipmi_bootdev = bootdev;
750 config->ipmi_bootdev_persistent = persistent;
752 if (bootdev == IPMI_BOOTDEV_SAFE)
753 config->safe_mode = true;
756 static int read_bootdev_sysparam(const char *name, uint8_t *val)
762 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
763 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
765 fd = open(path, O_RDONLY);
767 pb_debug("powerpc: can't access sysparam %s\n",
772 rc = read(fd, buf, sizeof(buf));
776 /* bootdev definitions should only be one byte in size */
778 pb_debug("powerpc: sysparam %s read returned %d\n",
783 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
785 if (!ipmi_bootdev_is_valid(buf[0]))
792 static int write_bootdev_sysparam(const char *name, uint8_t val)
797 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
798 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
800 fd = open(path, O_WRONLY);
802 pb_debug("powerpc: can't access sysparam %s for writing\n",
809 rc = write(fd, &val, sizeof(val));
810 if (rc == sizeof(val)) {
815 if (rc <= 0 && errno != EINTR) {
816 pb_log("powerpc: error updating sysparam %s: %s",
817 name, strerror(errno));
826 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
831 static int clear_ipmi_bootdev_sysparams(
832 struct platform_powerpc *platform __attribute__((unused)),
836 /* invalidate default-boot-device setting */
837 write_bootdev_sysparam("default-boot-device", 0xff);
839 /* invalidate next-boot-device setting */
840 write_bootdev_sysparam("next-boot-device", 0xff);
845 static int get_ipmi_bootdev_sysparams(
846 struct platform_powerpc *platform __attribute__((unused)),
847 uint8_t *bootdev, bool *persistent)
849 uint8_t next_bootdev, default_bootdev;
850 bool next_valid, default_valid;
853 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
854 next_valid = rc == 0;
856 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
857 default_valid = rc == 0;
859 /* nothing valid? no need to change the config */
860 if (!next_valid && !default_valid)
863 *persistent = !next_valid;
864 *bootdev = next_valid ? next_bootdev : default_bootdev;
868 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
869 bool persistent __attribute__((unused)))
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 */
882 resp_len = sizeof(resp);
884 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
885 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
892 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
893 uint8_t *bootdev, bool *persistent)
899 0x05, /* parameter selector: boot flags */
900 0x00, /* no set selector */
901 0x00, /* no block selector */
904 resp_len = sizeof(resp);
905 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
906 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
911 pb_log("platform: error reading IPMI boot options\n");
915 if (resp_len != sizeof(resp)) {
916 pb_log("platform: unexpected length (%d) in "
917 "boot options response\n", resp_len);
921 pb_debug("IPMI get_bootdev response:\n");
922 for (int i = 0; i < resp_len; i++)
923 pb_debug("%x ", resp[i]);
927 pb_log("platform: non-zero completion code %d from IPMI req\n",
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]);
939 /* check for valid paramters */
940 if (resp[2] & 0x80) {
941 pb_debug("platform: boot options are invalid/locked\n");
947 /* check for valid flags */
948 if (!(resp[3] & 0x80)) {
949 pb_debug("platform: boot flags are invalid, ignoring\n");
953 *persistent = resp[3] & 0x40;
954 *bootdev = (resp[4] >> 2) & 0x0f;
958 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
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 */
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");
982 req[0] = sensor_number;
984 resp_len = sizeof(resp);
986 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
990 ipmi_timeout); return 0;
995 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
997 struct platform_powerpc *platform = p->platform_data;
998 uint16_t resp_len = 8;
1000 uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1003 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1004 IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1009 pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
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];
1022 * Retrieve info from the "Get Device ID" IPMI commands.
1023 * See Chapter 20.1 in the IPMIv2 specification.
1025 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1027 struct platform_powerpc *platform = p->platform_data;
1028 uint16_t resp_len = 16;
1029 uint8_t resp[16], bcd;
1032 /* Retrieve info from current side */
1033 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1034 IPMI_CMD_APP_GET_DEVICE_ID,
1039 pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1041 for (i = 0; i < resp_len; i++) {
1042 pb_debug(" %x", resp[i]);
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;
1051 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1053 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
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",
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]);
1067 bcd = resp[5] & 0x0f;
1068 bcd += 10 * (resp[5] >> 4);
1069 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1072 pb_log("Failed to retrieve Device ID from IPMI\n");
1074 /* Retrieve info from golden side */
1075 memset(resp, 0, sizeof(resp));
1077 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1078 IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1083 pb_debug("BMC golden resp [%d][%d]:\n", rc, resp_len);
1085 for (i = 0; i < resp_len; i++) {
1086 pb_debug(" %x", resp[i]);
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;
1095 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1097 info->bmc_golden[1] = talloc_asprintf(info, "Device Rev: 0x%x",
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",
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]);
1111 bcd = resp[5] & 0x0f;
1112 bcd += 10 * (resp[5] >> 4);
1113 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1116 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1119 static void get_ipmi_network_override(struct platform_powerpc *platform,
1120 struct config *config)
1122 uint16_t min_len = 12, resp_len = 53, version;
1123 const uint32_t magic_value = 0x21706221;
1124 uint8_t resp[resp_len];
1129 0x61, /* parameter selector: OEM section (network) */
1130 0x00, /* no set selector */
1131 0x00, /* no block selector */
1134 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1135 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1140 pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
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)
1146 else if (i && (i + 1) % 8 == 0)
1153 pb_debug("IPMI network config option unavailable\n");
1157 if (resp_len < min_len) {
1158 pb_debug("IPMI net response too small\n");
1163 pb_log("platform: non-zero completion code %d from IPMI network req\n",
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",
1175 /* Check that the parameters are valid */
1176 if (resp[2] & 0x80) {
1177 pb_debug("platform: network override is invalid/locked\n");
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");
1186 /* Read the persistent flag; if it is set we need to save this config */
1187 persistent = resp[3] & 0x40;
1189 pb_debug("platform: network override is persistent\n");
1191 /* Check 4-byte cookie value */
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);
1199 i += sizeof(cookie);
1201 /* Check 2-byte version number */
1202 memcpy(&version, &resp[i], sizeof(version));
1203 version = __be16_to_cpu(version);
1204 i += sizeof(version);
1206 pb_debug("Unexpected version: %u\n", version);
1210 /* Interpret the rest of the interface config */
1211 rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1213 if (!rc && persistent) {
1214 /* Write this new config to NVRAM */
1215 update_network_config(platform, config);
1216 rc = write_nvram(platform);
1218 pb_log("platform: Failed to save persistent interface override\n");
1222 static void get_active_consoles(struct config *config)
1225 char *fsp_prop = NULL;
1227 config->n_consoles = 2;
1228 config->consoles = talloc_array(config, char *, config->n_consoles);
1229 if (!config->consoles)
1232 config->consoles[0] = talloc_asprintf(config->consoles,
1233 "/dev/hvc0 [IPMI / Serial]");
1234 config->consoles[1] = talloc_asprintf(config->consoles,
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)
1244 config->consoles[config->n_consoles++] = talloc_asprintf(
1246 "/dev/hvc1 [Serial]");
1251 config->n_consoles = 0;
1252 pb_log("Failed to allocate memory for consoles\n");
1255 static int load_config(struct platform *p, struct config *config)
1257 struct platform_powerpc *platform = to_platform_powerpc(p);
1260 rc = parse_nvram(platform);
1262 pb_log("%s: Failed to parse nvram\n", __func__);
1264 populate_config(platform, config);
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);
1277 get_ipmi_network_override(platform, config);
1279 get_active_consoles(config);
1284 static int save_config(struct platform *p, struct config *config)
1286 struct platform_powerpc *platform = to_platform_powerpc(p);
1287 struct config *defaults;
1290 defaults = talloc_zero(platform, struct config);
1291 config_set_defaults(defaults);
1293 rc = update_config(platform, config, defaults);
1295 talloc_free(defaults);
1299 static void pre_boot(struct platform *p, const struct config *config)
1301 struct platform_powerpc *platform = to_platform_powerpc(p);
1303 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1304 platform->clear_ipmi_bootdev(platform, false);
1306 if (platform->set_os_boot_sensor)
1307 platform->set_os_boot_sensor(platform);
1310 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1312 struct platform_powerpc *platform = p->platform_data;
1313 char *buf, *filename;
1316 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1317 rc = read_file(platform, filename, &buf, &len);
1319 sysinfo->type = talloc_steal(sysinfo, buf);
1320 talloc_free(filename);
1322 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1323 rc = read_file(platform, filename, &buf, &len);
1325 sysinfo->identifier = talloc_steal(sysinfo, buf);
1326 talloc_free(filename);
1328 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1330 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1333 get_ipmi_bmc_versions(p, sysinfo);
1335 if (platform->get_platform_versions)
1336 platform->get_platform_versions(sysinfo);
1341 static bool probe(struct platform *p, void *ctx)
1343 struct platform_powerpc *platform;
1344 struct stat statbuf;
1348 /* we need a device tree */
1349 rc = stat("/proc/device-tree", &statbuf);
1353 if (!S_ISDIR(statbuf.st_mode))
1356 platform = talloc_zero(ctx, struct platform_powerpc);
1357 list_init(&platform->params);
1359 p->platform_data = platform;
1361 bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
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;
1375 pb_log("platform: no IPMI parameter support\n");
1379 platform->get_platform_versions = hostboot_load_versions;
1385 static struct platform platform_powerpc = {
1387 .dhcp_arch_id = 0x000e,
1389 .load_config = load_config,
1390 .save_config = save_config,
1391 .pre_boot = pre_boot,
1392 .get_sysinfo = get_sysinfo,
1395 register_platform(platform_powerpc);