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 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 set_proxy_variables(struct config *config)
529 if (config->http_proxy)
530 setenv("http_proxy", config->http_proxy, 1);
532 unsetenv("http_proxy");
534 if (config->https_proxy)
535 setenv("https_proxy", config->https_proxy, 1);
537 unsetenv("https_proxy");
540 static void populate_config(struct platform_powerpc *platform,
541 struct config *config)
545 unsigned long timeout;
547 /* if the "auto-boot?' property is present and "false", disable auto
549 val = get_param(platform, "auto-boot?");
550 config->autoboot_enabled = !val || strcmp(val, "false");
552 val = get_param(platform, "petitboot,timeout");
554 timeout = strtoul(val, &end, 10);
556 if (timeout >= INT_MAX)
558 config->autoboot_timeout_sec = (int)timeout;
562 val = get_param(platform, "petitboot,language");
563 config->lang = val ? talloc_strdup(config, val) : NULL;
565 populate_network_config(platform, config);
567 populate_bootdev_config(platform, config);
569 if (!config->debug) {
570 val = get_param(platform, "petitboot,debug?");
571 config->debug = val && !strcmp(val, "true");
574 val = get_param(platform, "petitboot,write?");
576 config->allow_writes = !strcmp(val, "true");
578 val = get_param(platform, "petitboot,snapshots?");
580 config->disable_snapshots = !strcmp(val, "false");
582 val = get_param(platform, "petitboot,console");
584 config->boot_console = talloc_strdup(config, val);
585 /* If a full path is already set we don't want to override it */
586 config->manual_console = config->boot_console &&
587 !strchr(config->boot_console, '[');
589 val = get_param(platform, "petitboot,http_proxy");
591 config->http_proxy = talloc_strdup(config, val);
592 val = get_param(platform, "petitboot,https_proxy");
594 config->https_proxy = talloc_strdup(config, val);
595 set_proxy_variables(config);
598 static char *iface_config_str(void *ctx, struct interface_config *config)
602 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
603 * different hardware address formats */
604 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
605 config->hwaddr[0], config->hwaddr[1],
606 config->hwaddr[2], config->hwaddr[3],
607 config->hwaddr[4], config->hwaddr[5]);
609 if (config->ignore) {
610 str = talloc_asprintf_append(str, "ignore");
612 } else if (config->method == CONFIG_METHOD_DHCP) {
613 str = talloc_asprintf_append(str, "dhcp");
615 } else if (config->method == CONFIG_METHOD_STATIC) {
616 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
617 config->static_config.address,
618 config->static_config.gateway ? "," : "",
619 config->static_config.gateway ?: "",
620 config->static_config.url ? "," : "",
621 config->static_config.url ?: "");
626 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
631 str = talloc_strdup(ctx, "dns,");
632 for (i = 0; i < n; i++) {
633 str = talloc_asprintf_append(str, "%s%s",
641 static void update_string_config(struct platform_powerpc *platform,
642 const char *name, const char *value)
646 cur = get_param(platform, name);
648 /* don't set an empty parameter if it doesn't already exist */
649 if (!cur && !strlen(value))
652 set_param(platform, name, value);
655 static void update_network_config(struct platform_powerpc *platform,
656 struct config *config)
662 * Don't store IPMI overrides to NVRAM. If this was a persistent
663 * override it was already stored in NVRAM by
664 * get_ipmi_network_override()
666 if (config->network.n_interfaces &&
667 config->network.interfaces[0]->override)
670 val = talloc_strdup(platform, "");
672 for (i = 0; i < config->network.n_interfaces; i++) {
673 char *iface_str = iface_config_str(platform,
674 config->network.interfaces[i]);
675 val = talloc_asprintf_append(val, "%s%s",
676 *val == '\0' ? "" : " ", iface_str);
677 talloc_free(iface_str);
680 if (config->network.n_dns_servers) {
681 char *dns_str = dns_config_str(platform,
682 config->network.dns_servers,
683 config->network.n_dns_servers);
684 val = talloc_asprintf_append(val, "%s%s",
685 *val == '\0' ? "" : " ", dns_str);
686 talloc_free(dns_str);
689 update_string_config(platform, "petitboot,network", val);
694 static void update_bootdev_config(struct platform_powerpc *platform,
695 struct config *config)
697 char *val = NULL, *boot_str = NULL, *tmp = NULL, *first = NULL;
698 struct autoboot_option *opt;
699 const char delim = ' ';
702 if (!config->n_autoboot_opts)
704 else if (config->autoboot_opts[0].boot_type == BOOT_DEVICE_UUID)
705 first = talloc_asprintf(config, "uuid:%s",
706 config->autoboot_opts[0].uuid);
710 for (i = 0; i < config->n_autoboot_opts; i++) {
711 opt = &config->autoboot_opts[i];
712 switch (opt->boot_type) {
713 case BOOT_DEVICE_TYPE:
714 boot_str = talloc_asprintf(config, "%s%c",
715 device_type_name(opt->type),
718 case BOOT_DEVICE_UUID:
719 boot_str = talloc_asprintf(config, "uuid:%s%c",
723 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
726 update_string_config(platform, "petitboot,bootdevs", val);
727 update_string_config(platform, "petitboot,bootdev", first);
730 talloc_free(boot_str);
733 static int update_config(struct platform_powerpc *platform,
734 struct config *config, struct config *defaults)
739 if (config->autoboot_enabled == defaults->autoboot_enabled)
742 val = config->autoboot_enabled ? "true" : "false";
743 update_string_config(platform, "auto-boot?", val);
745 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
748 val = tmp = talloc_asprintf(platform, "%d",
749 config->autoboot_timeout_sec);
751 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
752 platform->clear_ipmi_bootdev) {
753 platform->clear_ipmi_bootdev(platform,
754 config->ipmi_bootdev_persistent);
755 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
756 config->ipmi_bootdev_persistent = false;
759 update_string_config(platform, "petitboot,timeout", val);
763 val = config->lang ?: "";
764 update_string_config(platform, "petitboot,language", val);
766 if (config->allow_writes == defaults->allow_writes)
769 val = config->allow_writes ? "true" : "false";
770 update_string_config(platform, "petitboot,write?", val);
772 if (!config->manual_console) {
773 val = config->boot_console ?: "";
774 update_string_config(platform, "petitboot,console", val);
777 val = config->http_proxy ?: "";
778 update_string_config(platform, "petitboot,http_proxy", val);
779 val = config->https_proxy ?: "";
780 update_string_config(platform, "petitboot,https_proxy", val);
781 set_proxy_variables(config);
783 update_network_config(platform, config);
785 update_bootdev_config(platform, config);
787 return write_nvram(platform);
790 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
793 config->ipmi_bootdev = bootdev;
794 config->ipmi_bootdev_persistent = persistent;
797 case IPMI_BOOTDEV_NONE:
798 case IPMI_BOOTDEV_DISK:
799 case IPMI_BOOTDEV_NETWORK:
800 case IPMI_BOOTDEV_CDROM:
803 case IPMI_BOOTDEV_SETUP:
804 config->autoboot_enabled = false;
806 case IPMI_BOOTDEV_SAFE:
807 config->autoboot_enabled = false;
808 config->safe_mode = true;
813 static int read_bootdev_sysparam(const char *name, uint8_t *val)
819 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
820 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
822 fd = open(path, O_RDONLY);
824 pb_debug("powerpc: can't access sysparam %s\n",
829 rc = read(fd, buf, sizeof(buf));
833 /* bootdev definitions should only be one byte in size */
835 pb_debug("powerpc: sysparam %s read returned %d\n",
840 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
842 if (!ipmi_bootdev_is_valid(buf[0]))
849 static int write_bootdev_sysparam(const char *name, uint8_t val)
854 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
855 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
857 fd = open(path, O_WRONLY);
859 pb_debug("powerpc: can't access sysparam %s for writing\n",
866 rc = write(fd, &val, sizeof(val));
867 if (rc == sizeof(val)) {
872 if (rc <= 0 && errno != EINTR) {
873 pb_log("powerpc: error updating sysparam %s: %s",
874 name, strerror(errno));
883 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
888 static int clear_ipmi_bootdev_sysparams(
889 struct platform_powerpc *platform __attribute__((unused)),
893 /* invalidate default-boot-device setting */
894 write_bootdev_sysparam("default-boot-device", 0xff);
896 /* invalidate next-boot-device setting */
897 write_bootdev_sysparam("next-boot-device", 0xff);
902 static int get_ipmi_bootdev_sysparams(
903 struct platform_powerpc *platform __attribute__((unused)),
904 uint8_t *bootdev, bool *persistent)
906 uint8_t next_bootdev, default_bootdev;
907 bool next_valid, default_valid;
910 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
911 next_valid = rc == 0;
913 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
914 default_valid = rc == 0;
916 /* nothing valid? no need to change the config */
917 if (!next_valid && !default_valid)
920 *persistent = !next_valid;
921 *bootdev = next_valid ? next_bootdev : default_bootdev;
925 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
926 bool persistent __attribute__((unused)))
931 0x05, /* parameter selector: boot flags */
932 0x80, /* data 1: valid */
933 0x00, /* data 2: bootdev: no override */
934 0x00, /* data 3: system defaults */
935 0x00, /* data 4: no request for shared mode, mux defaults */
936 0x00, /* data 5: no instance request */
939 resp_len = sizeof(resp);
941 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
942 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
949 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
950 uint8_t *bootdev, bool *persistent)
956 0x05, /* parameter selector: boot flags */
957 0x00, /* no set selector */
958 0x00, /* no block selector */
961 resp_len = sizeof(resp);
962 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
963 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
968 pb_log("platform: error reading IPMI boot options\n");
972 if (resp_len != sizeof(resp)) {
973 pb_log("platform: unexpected length (%d) in "
974 "boot options response\n", resp_len);
978 pb_debug("IPMI get_bootdev response:\n");
979 for (int i = 0; i < resp_len; i++)
980 pb_debug("%x ", resp[i]);
984 pb_log("platform: non-zero completion code %d from IPMI req\n",
989 /* check for correct parameter version */
990 if ((resp[1] & 0xf) != 0x1) {
991 pb_log("platform: unexpected version (0x%x) in "
992 "boot options response\n", resp[0]);
996 /* check for valid paramters */
997 if (resp[2] & 0x80) {
998 pb_debug("platform: boot options are invalid/locked\n");
1002 *persistent = false;
1004 /* check for valid flags */
1005 if (!(resp[3] & 0x80)) {
1006 pb_debug("platform: boot flags are invalid, ignoring\n");
1010 *persistent = resp[3] & 0x40;
1011 *bootdev = (resp[4] >> 2) & 0x0f;
1015 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
1021 0x00, /* sensor number: os boot */
1022 0xA9, /* operation: set everything */
1023 0x00, /* sensor reading: none */
1024 0x40, /* assertion mask lsb: set state 6 */
1025 0x00, /* assertion mask msb: none */
1026 0x00, /* deassertion mask lsb: none */
1027 0x00, /* deassertion mask msb: none */
1028 0x00, /* event data 1: none */
1029 0x00, /* event data 2: none */
1030 0x00, /* event data 3: none */
1033 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
1034 if (sensor_number < 0) {
1035 pb_log("Couldn't find OS boot sensor in device tree\n");
1039 req[0] = sensor_number;
1041 resp_len = sizeof(resp);
1043 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
1044 IPMI_CMD_SENSOR_SET,
1047 ipmi_timeout); return 0;
1052 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
1054 struct platform_powerpc *platform = p->platform_data;
1055 uint16_t resp_len = 8;
1057 uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1060 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1061 IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1066 pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1068 if (rc == 0 && resp_len > 0) {
1069 for (i = 2; i < resp_len; i++) {
1070 pb_debug(" %x", resp[i]);
1071 buf[i - 2] = resp[i];
1079 * Retrieve info from the "Get Device ID" IPMI commands.
1080 * See Chapter 20.1 in the IPMIv2 specification.
1082 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1084 struct platform_powerpc *platform = p->platform_data;
1085 uint16_t resp_len = 16;
1086 uint8_t resp[16], bcd;
1087 uint32_t aux_version;
1090 /* Retrieve info from current side */
1091 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1092 IPMI_CMD_APP_GET_DEVICE_ID,
1097 pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1099 for (i = 0; i < resp_len; i++) {
1100 pb_debug(" %x", resp[i]);
1105 if (rc == 0 && resp_len == 16) {
1106 info->bmc_current = talloc_array(info, char *, 4);
1107 info->n_bmc_current = 4;
1109 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1111 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1113 bcd = resp[4] & 0x0f;
1114 bcd += 10 * (resp[4] >> 4);
1115 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1116 info->bmc_current[2] = talloc_asprintf(info,
1117 "Firmware version: %u.%02u.%05u",
1118 resp[3], bcd, aux_version);
1119 bcd = resp[5] & 0x0f;
1120 bcd += 10 * (resp[5] >> 4);
1121 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1124 pb_log("Failed to retrieve Device ID from IPMI\n");
1126 /* Retrieve info from golden side */
1127 memset(resp, 0, sizeof(resp));
1129 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1130 IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1135 pb_debug("BMC golden resp [%d][%d]:\n", rc, resp_len);
1137 for (i = 0; i < resp_len; i++) {
1138 pb_debug(" %x", resp[i]);
1143 if (rc == 0 && resp_len == 16) {
1144 info->bmc_golden = talloc_array(info, char *, 4);
1145 info->n_bmc_golden = 4;
1147 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1149 info->bmc_golden[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1151 bcd = resp[4] & 0x0f;
1152 bcd += 10 * (resp[4] >> 4);
1153 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1154 info->bmc_golden[2] = talloc_asprintf(info,
1155 "Firmware version: %u.%02u.%u",
1156 resp[3], bcd, aux_version);
1157 bcd = resp[5] & 0x0f;
1158 bcd += 10 * (resp[5] >> 4);
1159 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1162 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1165 static void get_ipmi_network_override(struct platform_powerpc *platform,
1166 struct config *config)
1168 uint16_t min_len = 12, resp_len = 53, version;
1169 const uint32_t magic_value = 0x21706221;
1170 uint8_t resp[resp_len];
1175 0x61, /* parameter selector: OEM section (network) */
1176 0x00, /* no set selector */
1177 0x00, /* no block selector */
1180 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1181 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1186 pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1188 for (i = 0; i < resp_len; i++) {
1189 pb_debug(" %02x", resp[i]);
1190 if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1192 else if (i && (i + 1) % 8 == 0)
1199 pb_debug("IPMI network config option unavailable\n");
1203 if (resp_len < min_len) {
1204 pb_debug("IPMI net response too small\n");
1209 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1214 /* Check for correct parameter version */
1215 if ((resp[1] & 0xf) != 0x1) {
1216 pb_log("platform: unexpected version (0x%x) in network override response\n",
1221 /* Check that the parameters are valid */
1222 if (resp[2] & 0x80) {
1223 pb_debug("platform: network override is invalid/locked\n");
1227 /* Check for valid parameters in the boot flags section */
1228 if (!(resp[3] & 0x80)) {
1229 pb_debug("platform: network override valid flag not set\n");
1232 /* Read the persistent flag; if it is set we need to save this config */
1233 persistent = resp[3] & 0x40;
1235 pb_debug("platform: network override is persistent\n");
1237 /* Check 4-byte cookie value */
1239 memcpy(&cookie, &resp[i], sizeof(cookie));
1240 cookie = __be32_to_cpu(cookie);
1241 if (cookie != magic_value) {
1242 pb_log("%s: Incorrect cookie %x\n", __func__, cookie);
1245 i += sizeof(cookie);
1247 /* Check 2-byte version number */
1248 memcpy(&version, &resp[i], sizeof(version));
1249 version = __be16_to_cpu(version);
1250 i += sizeof(version);
1252 pb_debug("Unexpected version: %u\n", version);
1256 /* Interpret the rest of the interface config */
1257 rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1259 if (!rc && persistent) {
1260 /* Write this new config to NVRAM */
1261 update_network_config(platform, config);
1262 rc = write_nvram(platform);
1264 pb_log("platform: Failed to save persistent interface override\n");
1268 static void get_active_consoles(struct config *config)
1271 char *fsp_prop = NULL;
1273 config->n_consoles = 2;
1274 config->consoles = talloc_array(config, char *, config->n_consoles);
1275 if (!config->consoles)
1278 config->consoles[0] = talloc_asprintf(config->consoles,
1279 "/dev/hvc0 [IPMI / Serial]");
1280 config->consoles[1] = talloc_asprintf(config->consoles,
1283 fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1284 if (stat(fsp_prop, &sbuf) == 0) {
1285 /* FSP based machines also have a separate serial console */
1286 config->consoles = talloc_realloc(config, config->consoles,
1287 char *, config->n_consoles + 1);
1288 if (!config->consoles)
1290 config->consoles[config->n_consoles++] = talloc_asprintf(
1292 "/dev/hvc1 [Serial]");
1297 config->n_consoles = 0;
1298 pb_log("Failed to allocate memory for consoles\n");
1301 static int load_config(struct platform *p, struct config *config)
1303 struct platform_powerpc *platform = to_platform_powerpc(p);
1306 rc = parse_nvram(platform);
1310 populate_config(platform, config);
1312 if (platform->get_ipmi_bootdev) {
1313 bool bootdev_persistent;
1314 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1315 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1316 &bootdev_persistent);
1317 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1318 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1323 get_ipmi_network_override(platform, config);
1325 get_active_consoles(config);
1330 static int save_config(struct platform *p, struct config *config)
1332 struct platform_powerpc *platform = to_platform_powerpc(p);
1333 struct config *defaults;
1336 defaults = talloc_zero(platform, struct config);
1337 config_set_defaults(defaults);
1339 rc = update_config(platform, config, defaults);
1341 talloc_free(defaults);
1345 static void pre_boot(struct platform *p, const struct config *config)
1347 struct platform_powerpc *platform = to_platform_powerpc(p);
1349 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1350 platform->clear_ipmi_bootdev(platform, false);
1352 if (platform->set_os_boot_sensor)
1353 platform->set_os_boot_sensor(platform);
1356 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1358 struct platform_powerpc *platform = p->platform_data;
1359 char *buf, *filename;
1362 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1363 rc = read_file(platform, filename, &buf, &len);
1365 sysinfo->type = talloc_steal(sysinfo, buf);
1366 talloc_free(filename);
1368 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1369 rc = read_file(platform, filename, &buf, &len);
1371 sysinfo->identifier = talloc_steal(sysinfo, buf);
1372 talloc_free(filename);
1374 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1376 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1379 get_ipmi_bmc_versions(p, sysinfo);
1381 if (platform->get_platform_versions)
1382 platform->get_platform_versions(sysinfo);
1387 static bool probe(struct platform *p, void *ctx)
1389 struct platform_powerpc *platform;
1390 struct stat statbuf;
1394 /* we need a device tree */
1395 rc = stat("/proc/device-tree", &statbuf);
1399 if (!S_ISDIR(statbuf.st_mode))
1402 platform = talloc_zero(ctx, struct platform_powerpc);
1403 list_init(&platform->params);
1405 p->platform_data = platform;
1407 bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1409 if (ipmi_present() && bmc_present) {
1410 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1411 platform->ipmi = ipmi_open(platform);
1412 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1413 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1414 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1415 } else if (!stat(sysparams_dir, &statbuf)) {
1416 pb_debug("platform: using sysparams for IPMI paramters\n");
1417 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1418 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1421 pb_log("platform: no IPMI parameter support\n");
1425 platform->get_platform_versions = hostboot_load_versions;
1431 static struct platform platform_powerpc = {
1433 .dhcp_arch_id = 0x000e,
1435 .load_config = load_config,
1436 .save_config = save_config,
1437 .pre_boot = pre_boot,
1438 .get_sysinfo = get_sysinfo,
1441 register_platform(platform_powerpc);