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[] = {
60 "petitboot,snapshots?",
62 "petitboot,http_proxy",
63 "petitboot,https_proxy",
67 #define to_platform_powerpc(p) \
68 (struct platform_powerpc *)(p->platform_data)
70 static bool param_is_known(const char *param, unsigned int len)
72 const char *known_param;
75 for (i = 0; known_params[i]; i++) {
76 known_param = known_params[i];
77 if (len == strlen(known_param) &&
78 !strncmp(param, known_param, len))
85 static int parse_nvram_params(struct platform_powerpc *platform,
88 char *pos, *name, *value;
89 unsigned int paramlen;
92 /* discard 2 header lines:
99 for (i = 0; i < len; i++) {
107 fprintf(stderr, "failure parsing nvram output\n");
111 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
112 unsigned int namelen;
116 newline = strchr(pos, '\n');
122 paramlen = strlen(pos);
125 value = strchr(pos, '=');
129 namelen = value - name;
133 if (!param_is_known(name, namelen))
138 param = talloc(platform, struct param);
139 param->modified = false;
140 param->name = talloc_strndup(platform, name, namelen);
141 param->value = talloc_strdup(platform, value);
142 list_add(&platform->params, ¶m->list);
148 static int parse_nvram(struct platform_powerpc *platform)
150 struct process *process;
155 argv[1] = "--print-config";
156 argv[2] = "--partition";
160 process = process_create(platform);
161 process->path = "nvram";
162 process->argv = argv;
163 process->keep_stdout = true;
165 rc = process_run_sync(process);
167 if (rc || !process_exit_ok(process)) {
168 fprintf(stderr, "nvram process returned "
169 "non-zero exit status\n");
172 rc = parse_nvram_params(platform, process->stdout_buf,
173 process->stdout_len);
176 process_release(process);
180 static int write_nvram(struct platform_powerpc *platform)
182 struct process *process;
188 argv[1] = "--update-config";
190 argv[3] = "--partition";
194 process = process_create(platform);
195 process->path = "nvram";
196 process->argv = argv;
198 list_for_each_entry(&platform->params, param, list) {
201 if (!param->modified)
204 paramstr = talloc_asprintf(platform, "%s=%s",
205 param->name, param->value);
208 rc = process_run_sync(process);
210 talloc_free(paramstr);
212 if (rc || !process_exit_ok(process)) {
214 pb_log("nvram update process returned "
215 "non-zero exit status\n");
220 process_release(process);
224 static const char *get_param(struct platform_powerpc *platform,
229 list_for_each_entry(&platform->params, param, list)
230 if (!strcmp(param->name, name))
235 static void set_param(struct platform_powerpc *platform, const char *name,
240 list_for_each_entry(&platform->params, param, list) {
241 if (strcmp(param->name, name))
244 if (!strcmp(param->value, value))
247 talloc_free(param->value);
248 param->value = talloc_strdup(param, value);
249 param->modified = true;
254 param = talloc(platform, struct param);
255 param->modified = true;
256 param->name = talloc_strdup(platform, name);
257 param->value = talloc_strdup(platform, value);
258 list_add(&platform->params, ¶m->list);
261 static int parse_hwaddr(struct interface_config *ifconf, char *str)
265 if (strlen(str) != strlen("00:00:00:00:00:00"))
268 for (i = 0; i < HWADDR_SIZE; i++) {
272 byte[0] = str[i * 3 + 0];
273 byte[1] = str[i * 3 + 1];
276 x = strtoul(byte, &endp, 16);
277 if (endp != byte + 2)
280 ifconf->hwaddr[i] = x & 0xff;
286 static int parse_one_interface_config(struct config *config,
289 struct interface_config *ifconf;
292 ifconf = talloc_zero(config, struct interface_config);
294 if (!confstr || !strlen(confstr))
297 /* first token should be the mac address */
298 tok = strtok_r(confstr, ",", &saveptr);
302 if (parse_hwaddr(ifconf, tok))
305 /* second token is the method */
306 tok = strtok_r(NULL, ",", &saveptr);
307 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
308 ifconf->ignore = true;
310 } else if (!strcmp(tok, "dhcp")) {
311 ifconf->method = CONFIG_METHOD_DHCP;
313 } else if (!strcmp(tok, "static")) {
314 ifconf->method = CONFIG_METHOD_STATIC;
316 /* ip/mask, [optional] gateway, [optional] url */
317 tok = strtok_r(NULL, ",", &saveptr);
320 ifconf->static_config.address =
321 talloc_strdup(ifconf, tok);
323 tok = strtok_r(NULL, ",", &saveptr);
325 ifconf->static_config.gateway =
326 talloc_strdup(ifconf, tok);
329 tok = strtok_r(NULL, ",", &saveptr);
331 ifconf->static_config.url =
332 talloc_strdup(ifconf, tok);
336 pb_log("Unknown network configuration method %s\n", tok);
340 config->network.interfaces = talloc_realloc(config,
341 config->network.interfaces,
342 struct interface_config *,
343 ++config->network.n_interfaces);
345 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
353 static int parse_one_dns_config(struct config *config,
356 char *tok, *saveptr = NULL;
358 for (tok = strtok_r(confstr, ",", &saveptr); tok;
359 tok = strtok_r(NULL, ",", &saveptr)) {
361 char *server = talloc_strdup(config, tok);
363 config->network.dns_servers = talloc_realloc(config,
364 config->network.dns_servers, const char *,
365 ++config->network.n_dns_servers);
367 config->network.dns_servers[config->network.n_dns_servers - 1]
374 static void populate_network_config(struct platform_powerpc *platform,
375 struct config *config)
377 char *val, *saveptr = NULL;
381 cval = get_param(platform, "petitboot,network");
382 if (!cval || !strlen(cval))
385 val = talloc_strdup(config, cval);
390 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
394 if (!strncasecmp(tok, "dns,", strlen("dns,")))
395 parse_one_dns_config(config, tok + strlen("dns,"));
397 parse_one_interface_config(config, tok);
404 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
406 char *delim = strchr(*pos, ' ');
407 int len, prefix = 0, rc = -1;
408 enum device_type type;
410 if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
411 prefix = strlen("uuid:");
412 opt->boot_type = BOOT_DEVICE_UUID;
413 } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
414 prefix = strlen("mac:");
415 opt->boot_type = BOOT_DEVICE_UUID;
417 type = find_device_type(*pos);
418 if (type != DEVICE_TYPE_UNKNOWN) {
420 opt->boot_type = BOOT_DEVICE_TYPE;
425 if (opt->boot_type == BOOT_DEVICE_UUID) {
427 len = (int)(delim - *pos) - prefix;
429 len = strlen(*pos) - prefix;
432 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;
451 unsigned int n_new = 0;
454 /* Check for ordered bootdevs */
455 val = get_param(platform, "petitboot,bootdevs");
456 if (!val || !strlen(val)) {
459 pos = talloc_strdup(config, val);
460 end = strchr(pos, '\0');
463 while (pos && pos < end) {
464 opt = talloc(config, struct autoboot_option);
466 if (read_bootdev(config, &pos, opt)) {
467 pb_log("bootdev config is in an unknown format "
468 "(expected uuid:... or mac:...)\n");
473 new = talloc_realloc(config, new, struct autoboot_option,
482 /* If autoboot has been disabled, clear the default options */
483 if (!config->autoboot_enabled) {
484 talloc_free(config->autoboot_opts);
485 config->n_autoboot_opts = 0;
490 talloc_free(config->autoboot_opts);
491 config->autoboot_opts = new;
492 config->n_autoboot_opts = n_new;
495 static void set_proxy_variables(struct config *config)
497 if (config->http_proxy)
498 setenv("http_proxy", config->http_proxy, 1);
500 unsetenv("http_proxy");
502 if (config->https_proxy)
503 setenv("https_proxy", config->https_proxy, 1);
505 unsetenv("https_proxy");
508 static void populate_config(struct platform_powerpc *platform,
509 struct config *config)
513 unsigned long timeout;
515 /* if the "auto-boot?' property is present and "false", disable auto
517 val = get_param(platform, "auto-boot?");
518 config->autoboot_enabled = !val || strcmp(val, "false");
520 val = get_param(platform, "petitboot,timeout");
522 timeout = strtoul(val, &end, 10);
524 if (timeout >= INT_MAX)
526 config->autoboot_timeout_sec = (int)timeout;
530 val = get_param(platform, "petitboot,language");
531 config->lang = val ? talloc_strdup(config, val) : NULL;
533 populate_network_config(platform, config);
535 populate_bootdev_config(platform, config);
537 if (!config->debug) {
538 val = get_param(platform, "petitboot,debug?");
539 config->debug = val && !strcmp(val, "true");
542 val = get_param(platform, "petitboot,write?");
544 config->allow_writes = !strcmp(val, "true");
546 val = get_param(platform, "petitboot,snapshots?");
548 config->disable_snapshots = !strcmp(val, "false");
550 val = get_param(platform, "petitboot,console");
552 config->boot_console = talloc_strdup(config, val);
553 /* If a full path is already set we don't want to override it */
554 config->manual_console = config->boot_console &&
555 !strchr(config->boot_console, '[');
557 val = get_param(platform, "petitboot,http_proxy");
559 config->http_proxy = talloc_strdup(config, val);
560 val = get_param(platform, "petitboot,https_proxy");
562 config->https_proxy = talloc_strdup(config, val);
563 set_proxy_variables(config);
566 static char *iface_config_str(void *ctx, struct interface_config *config)
570 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
571 * different hardware address formats */
572 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
573 config->hwaddr[0], config->hwaddr[1],
574 config->hwaddr[2], config->hwaddr[3],
575 config->hwaddr[4], config->hwaddr[5]);
577 if (config->ignore) {
578 str = talloc_asprintf_append(str, "ignore");
580 } else if (config->method == CONFIG_METHOD_DHCP) {
581 str = talloc_asprintf_append(str, "dhcp");
583 } else if (config->method == CONFIG_METHOD_STATIC) {
584 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
585 config->static_config.address,
586 config->static_config.gateway ? "," : "",
587 config->static_config.gateway ?: "",
588 config->static_config.url ? "," : "",
589 config->static_config.url ?: "");
594 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
599 str = talloc_strdup(ctx, "dns,");
600 for (i = 0; i < n; i++) {
601 str = talloc_asprintf_append(str, "%s%s",
609 static void update_string_config(struct platform_powerpc *platform,
610 const char *name, const char *value)
614 cur = get_param(platform, name);
616 /* don't set an empty parameter if it doesn't already exist */
617 if (!cur && !strlen(value))
620 set_param(platform, name, value);
623 static void update_network_config(struct platform_powerpc *platform,
624 struct config *config)
630 * Don't store IPMI overrides to NVRAM. If this was a persistent
631 * override it was already stored in NVRAM by
632 * get_ipmi_network_override()
634 if (config->network.n_interfaces &&
635 config->network.interfaces[0]->override)
638 val = talloc_strdup(platform, "");
640 for (i = 0; i < config->network.n_interfaces; i++) {
641 char *iface_str = iface_config_str(platform,
642 config->network.interfaces[i]);
643 val = talloc_asprintf_append(val, "%s%s",
644 *val == '\0' ? "" : " ", iface_str);
645 talloc_free(iface_str);
648 if (config->network.n_dns_servers) {
649 char *dns_str = dns_config_str(platform,
650 config->network.dns_servers,
651 config->network.n_dns_servers);
652 val = talloc_asprintf_append(val, "%s%s",
653 *val == '\0' ? "" : " ", dns_str);
654 talloc_free(dns_str);
657 update_string_config(platform, "petitboot,network", val);
662 static void update_bootdev_config(struct platform_powerpc *platform,
663 struct config *config)
665 char *val = NULL, *boot_str = NULL, *tmp = NULL;
666 struct autoboot_option *opt;
667 const char delim = ' ';
670 if (!config->n_autoboot_opts)
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);
692 talloc_free(boot_str);
695 static int update_config(struct platform_powerpc *platform,
696 struct config *config, struct config *defaults)
701 if (config->autoboot_enabled == defaults->autoboot_enabled)
704 val = config->autoboot_enabled ? "true" : "false";
705 update_string_config(platform, "auto-boot?", val);
707 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
710 val = tmp = talloc_asprintf(platform, "%d",
711 config->autoboot_timeout_sec);
713 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
714 platform->clear_ipmi_bootdev) {
715 platform->clear_ipmi_bootdev(platform,
716 config->ipmi_bootdev_persistent);
717 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
718 config->ipmi_bootdev_persistent = false;
721 update_string_config(platform, "petitboot,timeout", val);
725 val = config->lang ?: "";
726 update_string_config(platform, "petitboot,language", val);
728 if (config->allow_writes == defaults->allow_writes)
731 val = config->allow_writes ? "true" : "false";
732 update_string_config(platform, "petitboot,write?", val);
734 if (!config->manual_console) {
735 val = config->boot_console ?: "";
736 update_string_config(platform, "petitboot,console", val);
739 val = config->http_proxy ?: "";
740 update_string_config(platform, "petitboot,http_proxy", val);
741 val = config->https_proxy ?: "";
742 update_string_config(platform, "petitboot,https_proxy", val);
743 set_proxy_variables(config);
745 update_network_config(platform, config);
747 update_bootdev_config(platform, config);
749 return write_nvram(platform);
752 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
755 config->ipmi_bootdev = bootdev;
756 config->ipmi_bootdev_persistent = persistent;
759 case IPMI_BOOTDEV_NONE:
760 case IPMI_BOOTDEV_DISK:
761 case IPMI_BOOTDEV_NETWORK:
762 case IPMI_BOOTDEV_CDROM:
765 case IPMI_BOOTDEV_SETUP:
766 config->autoboot_enabled = false;
768 case IPMI_BOOTDEV_SAFE:
769 config->autoboot_enabled = false;
770 config->safe_mode = true;
775 static int read_bootdev_sysparam(const char *name, uint8_t *val)
781 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
782 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
784 fd = open(path, O_RDONLY);
786 pb_debug("powerpc: can't access sysparam %s\n",
791 rc = read(fd, buf, sizeof(buf));
795 /* bootdev definitions should only be one byte in size */
797 pb_debug("powerpc: sysparam %s read returned %d\n",
802 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
804 if (!ipmi_bootdev_is_valid(buf[0]))
811 static int write_bootdev_sysparam(const char *name, uint8_t val)
816 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
817 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
819 fd = open(path, O_WRONLY);
821 pb_debug("powerpc: can't access sysparam %s for writing\n",
828 rc = write(fd, &val, sizeof(val));
829 if (rc == sizeof(val)) {
834 if (rc <= 0 && errno != EINTR) {
835 pb_log("powerpc: error updating sysparam %s: %s",
836 name, strerror(errno));
845 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
850 static int clear_ipmi_bootdev_sysparams(
851 struct platform_powerpc *platform __attribute__((unused)),
855 /* invalidate default-boot-device setting */
856 write_bootdev_sysparam("default-boot-device", 0xff);
858 /* invalidate next-boot-device setting */
859 write_bootdev_sysparam("next-boot-device", 0xff);
864 static int get_ipmi_bootdev_sysparams(
865 struct platform_powerpc *platform __attribute__((unused)),
866 uint8_t *bootdev, bool *persistent)
868 uint8_t next_bootdev, default_bootdev;
869 bool next_valid, default_valid;
872 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
873 next_valid = rc == 0;
875 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
876 default_valid = rc == 0;
878 /* nothing valid? no need to change the config */
879 if (!next_valid && !default_valid)
882 *persistent = !next_valid;
883 *bootdev = next_valid ? next_bootdev : default_bootdev;
887 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
888 bool persistent __attribute__((unused)))
893 0x05, /* parameter selector: boot flags */
894 0x80, /* data 1: valid */
895 0x00, /* data 2: bootdev: no override */
896 0x00, /* data 3: system defaults */
897 0x00, /* data 4: no request for shared mode, mux defaults */
898 0x00, /* data 5: no instance request */
901 resp_len = sizeof(resp);
903 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
904 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
911 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
912 uint8_t *bootdev, bool *persistent)
918 0x05, /* parameter selector: boot flags */
919 0x00, /* no set selector */
920 0x00, /* no block selector */
923 resp_len = sizeof(resp);
924 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
925 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
930 pb_log("platform: error reading IPMI boot options\n");
934 if (resp_len != sizeof(resp)) {
935 pb_log("platform: unexpected length (%d) in "
936 "boot options response\n", resp_len);
940 pb_debug("IPMI get_bootdev response:\n");
941 for (int i = 0; i < resp_len; i++)
942 pb_debug("%x ", resp[i]);
946 pb_log("platform: non-zero completion code %d from IPMI req\n",
951 /* check for correct parameter version */
952 if ((resp[1] & 0xf) != 0x1) {
953 pb_log("platform: unexpected version (0x%x) in "
954 "boot options response\n", resp[0]);
958 /* check for valid paramters */
959 if (resp[2] & 0x80) {
960 pb_debug("platform: boot options are invalid/locked\n");
966 /* check for valid flags */
967 if (!(resp[3] & 0x80)) {
968 pb_debug("platform: boot flags are invalid, ignoring\n");
972 *persistent = resp[3] & 0x40;
973 *bootdev = (resp[4] >> 2) & 0x0f;
977 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
983 0x00, /* sensor number: os boot */
984 0xA9, /* operation: set everything */
985 0x00, /* sensor reading: none */
986 0x40, /* assertion mask lsb: set state 6 */
987 0x00, /* assertion mask msb: none */
988 0x00, /* deassertion mask lsb: none */
989 0x00, /* deassertion mask msb: none */
990 0x00, /* event data 1: none */
991 0x00, /* event data 2: none */
992 0x00, /* event data 3: none */
995 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
996 if (sensor_number < 0) {
997 pb_log("Couldn't find OS boot sensor in device tree\n");
1001 req[0] = sensor_number;
1003 resp_len = sizeof(resp);
1005 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
1006 IPMI_CMD_SENSOR_SET,
1009 ipmi_timeout); return 0;
1014 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
1016 struct platform_powerpc *platform = p->platform_data;
1017 uint16_t resp_len = 8;
1019 uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1022 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1023 IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1028 pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1030 if (rc == 0 && resp_len > 0) {
1031 for (i = 2; i < resp_len; i++) {
1032 pb_debug(" %x", resp[i]);
1033 buf[i - 2] = resp[i];
1041 * Retrieve info from the "Get Device ID" IPMI commands.
1042 * See Chapter 20.1 in the IPMIv2 specification.
1044 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1046 struct platform_powerpc *platform = p->platform_data;
1047 uint16_t resp_len = 16;
1048 uint8_t resp[16], bcd;
1049 uint32_t aux_version;
1052 /* Retrieve info from current side */
1053 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1054 IPMI_CMD_APP_GET_DEVICE_ID,
1059 pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1061 for (i = 0; i < resp_len; i++) {
1062 pb_debug(" %x", resp[i]);
1067 if (rc == 0 && resp_len == 16) {
1068 info->bmc_current = talloc_array(info, char *, 4);
1069 info->n_bmc_current = 4;
1071 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1073 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1075 bcd = resp[4] & 0x0f;
1076 bcd += 10 * (resp[4] >> 4);
1077 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1078 info->bmc_current[2] = talloc_asprintf(info,
1079 "Firmware version: %u.%02u.%05u",
1080 resp[3], bcd, aux_version);
1081 bcd = resp[5] & 0x0f;
1082 bcd += 10 * (resp[5] >> 4);
1083 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1086 pb_log("Failed to retrieve Device ID from IPMI\n");
1088 /* Retrieve info from golden side */
1089 memset(resp, 0, sizeof(resp));
1091 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1092 IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1097 pb_debug("BMC golden 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_golden = talloc_array(info, char *, 4);
1107 info->n_bmc_golden = 4;
1109 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1111 info->bmc_golden[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_golden[2] = talloc_asprintf(info,
1117 "Firmware version: %u.%02u.%u",
1118 resp[3], bcd, aux_version);
1119 bcd = resp[5] & 0x0f;
1120 bcd += 10 * (resp[5] >> 4);
1121 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1124 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1127 static void get_ipmi_network_override(struct platform_powerpc *platform,
1128 struct config *config)
1130 uint16_t min_len = 12, resp_len = 53, version;
1131 const uint32_t magic_value = 0x21706221;
1132 uint8_t resp[resp_len];
1137 0x61, /* parameter selector: OEM section (network) */
1138 0x00, /* no set selector */
1139 0x00, /* no block selector */
1142 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1143 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1148 pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1150 for (i = 0; i < resp_len; i++) {
1151 pb_debug(" %02x", resp[i]);
1152 if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1154 else if (i && (i + 1) % 8 == 0)
1161 pb_debug("IPMI network config option unavailable\n");
1165 if (resp_len < min_len) {
1166 pb_debug("IPMI net response too small\n");
1171 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1176 /* Check for correct parameter version */
1177 if ((resp[1] & 0xf) != 0x1) {
1178 pb_log("platform: unexpected version (0x%x) in network override response\n",
1183 /* Check that the parameters are valid */
1184 if (resp[2] & 0x80) {
1185 pb_debug("platform: network override is invalid/locked\n");
1189 /* Check for valid parameters in the boot flags section */
1190 if (!(resp[3] & 0x80)) {
1191 pb_debug("platform: network override valid flag not set\n");
1194 /* Read the persistent flag; if it is set we need to save this config */
1195 persistent = resp[3] & 0x40;
1197 pb_debug("platform: network override is persistent\n");
1199 /* Check 4-byte cookie value */
1201 memcpy(&cookie, &resp[i], sizeof(cookie));
1202 cookie = __be32_to_cpu(cookie);
1203 if (cookie != magic_value) {
1204 pb_log("%s: Incorrect cookie %x\n", __func__, cookie);
1207 i += sizeof(cookie);
1209 /* Check 2-byte version number */
1210 memcpy(&version, &resp[i], sizeof(version));
1211 version = __be16_to_cpu(version);
1212 i += sizeof(version);
1214 pb_debug("Unexpected version: %u\n", version);
1218 /* Interpret the rest of the interface config */
1219 rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1221 if (!rc && persistent) {
1222 /* Write this new config to NVRAM */
1223 update_network_config(platform, config);
1224 rc = write_nvram(platform);
1226 pb_log("platform: Failed to save persistent interface override\n");
1230 static void get_active_consoles(struct config *config)
1233 char *fsp_prop = NULL;
1235 config->n_consoles = 2;
1236 config->consoles = talloc_array(config, char *, config->n_consoles);
1237 if (!config->consoles)
1240 config->consoles[0] = talloc_asprintf(config->consoles,
1241 "/dev/hvc0 [IPMI / Serial]");
1242 config->consoles[1] = talloc_asprintf(config->consoles,
1245 fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1246 if (stat(fsp_prop, &sbuf) == 0) {
1247 /* FSP based machines also have a separate serial console */
1248 config->consoles = talloc_realloc(config, config->consoles,
1249 char *, config->n_consoles + 1);
1250 if (!config->consoles)
1252 config->consoles[config->n_consoles++] = talloc_asprintf(
1254 "/dev/hvc1 [Serial]");
1259 config->n_consoles = 0;
1260 pb_log("Failed to allocate memory for consoles\n");
1263 static int load_config(struct platform *p, struct config *config)
1265 struct platform_powerpc *platform = to_platform_powerpc(p);
1268 rc = parse_nvram(platform);
1272 populate_config(platform, config);
1274 if (platform->get_ipmi_bootdev) {
1275 bool bootdev_persistent;
1276 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1277 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1278 &bootdev_persistent);
1279 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1280 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1285 get_ipmi_network_override(platform, config);
1287 get_active_consoles(config);
1292 static int save_config(struct platform *p, struct config *config)
1294 struct platform_powerpc *platform = to_platform_powerpc(p);
1295 struct config *defaults;
1298 defaults = talloc_zero(platform, struct config);
1299 config_set_defaults(defaults);
1301 rc = update_config(platform, config, defaults);
1303 talloc_free(defaults);
1307 static void pre_boot(struct platform *p, const struct config *config)
1309 struct platform_powerpc *platform = to_platform_powerpc(p);
1311 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1312 platform->clear_ipmi_bootdev(platform, false);
1314 if (platform->set_os_boot_sensor)
1315 platform->set_os_boot_sensor(platform);
1318 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1320 struct platform_powerpc *platform = p->platform_data;
1321 char *buf, *filename;
1324 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1325 rc = read_file(platform, filename, &buf, &len);
1327 sysinfo->type = talloc_steal(sysinfo, buf);
1328 talloc_free(filename);
1330 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1331 rc = read_file(platform, filename, &buf, &len);
1333 sysinfo->identifier = talloc_steal(sysinfo, buf);
1334 talloc_free(filename);
1336 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1338 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1341 get_ipmi_bmc_versions(p, sysinfo);
1343 if (platform->get_platform_versions)
1344 platform->get_platform_versions(sysinfo);
1349 static bool probe(struct platform *p, void *ctx)
1351 struct platform_powerpc *platform;
1352 struct stat statbuf;
1356 /* we need a device tree */
1357 rc = stat("/proc/device-tree", &statbuf);
1361 if (!S_ISDIR(statbuf.st_mode))
1364 platform = talloc_zero(ctx, struct platform_powerpc);
1365 list_init(&platform->params);
1367 p->platform_data = platform;
1369 bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1371 if (ipmi_present() && bmc_present) {
1372 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1373 platform->ipmi = ipmi_open(platform);
1374 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1375 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1376 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1377 } else if (!stat(sysparams_dir, &statbuf)) {
1378 pb_debug("platform: using sysparams for IPMI paramters\n");
1379 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1380 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1383 pb_log("platform: no IPMI parameter support\n");
1387 platform->get_platform_versions = hostboot_load_versions;
1393 static struct platform platform_powerpc = {
1395 .dhcp_arch_id = 0x000e,
1397 .load_config = load_config,
1398 .save_config = save_config,
1399 .pre_boot = pre_boot,
1400 .get_sysinfo = get_sysinfo,
1403 register_platform(platform_powerpc);