12 #include <file/file.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <process/process.h>
17 #include <types/types.h>
24 static const char *partition = "common";
25 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
26 static const char *devtree_dir = "/proc/device-tree/";
27 static const int ipmi_timeout = 5000; /* milliseconds. */
33 struct list_item list;
36 struct platform_powerpc {
39 bool ipmi_bootdev_persistent;
40 int (*get_ipmi_bootdev)(
41 struct platform_powerpc *platform,
42 uint8_t *bootdev, bool *persistent);
43 int (*clear_ipmi_bootdev)(
44 struct platform_powerpc *platform,
46 int (*set_os_boot_sensor)(
47 struct platform_powerpc *platform);
48 void (*get_platform_versions)(struct system_info *info);
51 static const char *known_params[] = {
60 "petitboot,snapshots?",
64 #define to_platform_powerpc(p) \
65 (struct platform_powerpc *)(p->platform_data)
67 /* a partition max a max size of 64k * 16bytes = 1M */
68 static const int max_partition_size = 64 * 1024 * 16;
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;
414 } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
415 prefix = strlen("mac:");
416 opt->boot_type = BOOT_DEVICE_UUID;
419 type = find_device_type(*pos);
420 if (type != DEVICE_TYPE_UNKNOWN) {
422 opt->boot_type = BOOT_DEVICE_TYPE;
427 if (opt->boot_type == BOOT_DEVICE_UUID) {
429 len = (int)(delim - *pos) - prefix;
433 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
436 /* Always advance pointer to next option or end */
440 *pos += strlen(*pos);
445 static void populate_bootdev_config(struct platform_powerpc *platform,
446 struct config *config)
448 struct autoboot_option *opt, *new = NULL;
449 char *pos, *end, *old_dev = NULL;
450 unsigned int n_new = 0;
454 /* Check for old-style bootdev */
455 val = get_param(platform, "petitboot,bootdev");
456 if (val && strlen(val)) {
457 pos = talloc_strdup(config, val);
458 if (!strncmp(val, "uuid:", strlen("uuid:")))
459 old_dev = talloc_strdup(config,
460 val + strlen("uuid:"));
461 else if (!strncmp(val, "mac:", strlen("mac:")))
462 old_dev = talloc_strdup(config,
463 val + strlen("mac:"));
466 /* Check for ordered bootdevs */
467 val = get_param(platform, "petitboot,bootdevs");
468 if (!val || !strlen(val)) {
471 pos = talloc_strdup(config, val);
472 end = strchr(pos, '\0');
475 while (pos && pos < end) {
476 opt = talloc(config, struct autoboot_option);
478 if (read_bootdev(config, &pos, opt)) {
479 pb_log("bootdev config is in an unknown format "
480 "(expected uuid:... or mac:...)\n");
485 new = talloc_realloc(config, new, struct autoboot_option,
493 if (!n_new && !old_dev) {
494 /* If autoboot has been disabled, clear the default options */
495 if (!config->autoboot_enabled) {
496 talloc_free(config->autoboot_opts);
497 config->n_autoboot_opts = 0;
502 conflict = old_dev && (!n_new ||
503 new[0].boot_type == BOOT_DEVICE_TYPE ||
504 /* Canonical UUIDs are 36 characters long */
505 strncmp(new[0].uuid, old_dev, 36));
508 talloc_free(config->autoboot_opts);
509 config->autoboot_opts = new;
510 config->n_autoboot_opts = n_new;
515 * Difference detected, defer to old format in case it has been updated
518 pb_debug("Old autoboot bootdev detected\n");
519 talloc_free(config->autoboot_opts);
520 config->autoboot_opts = talloc(config, struct autoboot_option);
521 config->autoboot_opts[0].boot_type = BOOT_DEVICE_UUID;
522 config->autoboot_opts[0].uuid = talloc_strdup(config, old_dev);
523 config->n_autoboot_opts = 1;
526 static void populate_config(struct platform_powerpc *platform,
527 struct config *config)
531 unsigned long timeout;
533 /* if the "auto-boot?' property is present and "false", disable auto
535 val = get_param(platform, "auto-boot?");
536 config->autoboot_enabled = !val || strcmp(val, "false");
538 val = get_param(platform, "petitboot,timeout");
540 timeout = strtoul(val, &end, 10);
542 if (timeout >= INT_MAX)
544 config->autoboot_timeout_sec = (int)timeout;
548 val = get_param(platform, "petitboot,language");
549 config->lang = val ? talloc_strdup(config, val) : NULL;
551 populate_network_config(platform, config);
553 populate_bootdev_config(platform, config);
555 if (!config->debug) {
556 val = get_param(platform, "petitboot,debug?");
557 config->debug = val && !strcmp(val, "true");
560 val = get_param(platform, "petitboot,write?");
562 config->allow_writes = !strcmp(val, "true");
564 val = get_param(platform, "petitboot,snapshots?");
566 config->disable_snapshots = !strcmp(val, "false");
569 static char *iface_config_str(void *ctx, struct interface_config *config)
573 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
574 * different hardware address formats */
575 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
576 config->hwaddr[0], config->hwaddr[1],
577 config->hwaddr[2], config->hwaddr[3],
578 config->hwaddr[4], config->hwaddr[5]);
580 if (config->ignore) {
581 str = talloc_asprintf_append(str, "ignore");
583 } else if (config->method == CONFIG_METHOD_DHCP) {
584 str = talloc_asprintf_append(str, "dhcp");
586 } else if (config->method == CONFIG_METHOD_STATIC) {
587 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
588 config->static_config.address,
589 config->static_config.gateway ? "," : "",
590 config->static_config.gateway ?: "",
591 config->static_config.url ? "," : "",
592 config->static_config.url ?: "");
597 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
602 str = talloc_strdup(ctx, "dns,");
603 for (i = 0; i < n; i++) {
604 str = talloc_asprintf_append(str, "%s%s",
612 static void update_string_config(struct platform_powerpc *platform,
613 const char *name, const char *value)
617 cur = get_param(platform, name);
619 /* don't set an empty parameter if it doesn't already exist */
620 if (!cur && !strlen(value))
623 set_param(platform, name, value);
626 static void update_network_config(struct platform_powerpc *platform,
627 struct config *config)
632 val = talloc_strdup(platform, "");
634 for (i = 0; i < config->network.n_interfaces; i++) {
635 char *iface_str = iface_config_str(platform,
636 config->network.interfaces[i]);
637 val = talloc_asprintf_append(val, "%s%s",
638 *val == '\0' ? "" : " ", iface_str);
639 talloc_free(iface_str);
642 if (config->network.n_dns_servers) {
643 char *dns_str = dns_config_str(platform,
644 config->network.dns_servers,
645 config->network.n_dns_servers);
646 val = talloc_asprintf_append(val, "%s%s",
647 *val == '\0' ? "" : " ", dns_str);
648 talloc_free(dns_str);
651 update_string_config(platform, "petitboot,network", val);
656 static void update_bootdev_config(struct platform_powerpc *platform,
657 struct config *config)
659 char *val = NULL, *boot_str = NULL, *tmp = NULL, *first = NULL;
660 struct autoboot_option *opt;
661 const char delim = ' ';
664 if (!config->n_autoboot_opts)
666 else if (config->autoboot_opts[0].boot_type == BOOT_DEVICE_UUID)
667 first = talloc_asprintf(config, "uuid:%s",
668 config->autoboot_opts[0].uuid);
672 for (i = 0; i < config->n_autoboot_opts; i++) {
673 opt = &config->autoboot_opts[i];
674 switch (opt->boot_type) {
675 case BOOT_DEVICE_TYPE:
676 boot_str = talloc_asprintf(config, "%s%c",
677 device_type_name(opt->type),
680 case BOOT_DEVICE_UUID:
681 boot_str = talloc_asprintf(config, "uuid:%s%c",
685 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
688 update_string_config(platform, "petitboot,bootdevs", val);
689 update_string_config(platform, "petitboot,bootdev", first);
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 update_network_config(platform, config);
736 update_bootdev_config(platform, config);
738 return write_nvram(platform);
741 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
744 config->ipmi_bootdev = bootdev;
745 config->ipmi_bootdev_persistent = persistent;
748 case IPMI_BOOTDEV_NONE:
749 case IPMI_BOOTDEV_DISK:
750 case IPMI_BOOTDEV_NETWORK:
751 case IPMI_BOOTDEV_CDROM:
754 case IPMI_BOOTDEV_SETUP:
755 config->autoboot_enabled = false;
757 case IPMI_BOOTDEV_SAFE:
758 config->autoboot_enabled = false;
759 config->safe_mode = true;
764 static int read_bootdev_sysparam(const char *name, uint8_t *val)
770 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
771 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
773 fd = open(path, O_RDONLY);
775 pb_debug("powerpc: can't access sysparam %s\n",
780 rc = read(fd, buf, sizeof(buf));
784 /* bootdev definitions should only be one byte in size */
786 pb_debug("powerpc: sysparam %s read returned %d\n",
791 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
793 if (!ipmi_bootdev_is_valid(buf[0]))
800 static int write_bootdev_sysparam(const char *name, uint8_t val)
805 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
806 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
808 fd = open(path, O_WRONLY);
810 pb_debug("powerpc: can't access sysparam %s for writing\n",
817 rc = write(fd, &val, sizeof(val));
818 if (rc == sizeof(val)) {
823 if (rc <= 0 && errno != EINTR) {
824 pb_log("powerpc: error updating sysparam %s: %s",
825 name, strerror(errno));
834 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
839 static int clear_ipmi_bootdev_sysparams(
840 struct platform_powerpc *platform __attribute__((unused)),
844 /* invalidate default-boot-device setting */
845 write_bootdev_sysparam("default-boot-device", 0xff);
847 /* invalidate next-boot-device setting */
848 write_bootdev_sysparam("next-boot-device", 0xff);
853 static int get_ipmi_bootdev_sysparams(
854 struct platform_powerpc *platform __attribute__((unused)),
855 uint8_t *bootdev, bool *persistent)
857 uint8_t next_bootdev, default_bootdev;
858 bool next_valid, default_valid;
861 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
862 next_valid = rc == 0;
864 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
865 default_valid = rc == 0;
867 /* nothing valid? no need to change the config */
868 if (!next_valid && !default_valid)
871 *persistent = !next_valid;
872 *bootdev = next_valid ? next_bootdev : default_bootdev;
876 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
877 bool persistent __attribute__((unused)))
882 0x05, /* parameter selector: boot flags */
883 0x80, /* data 1: valid */
884 0x00, /* data 2: bootdev: no override */
885 0x00, /* data 3: system defaults */
886 0x00, /* data 4: no request for shared mode, mux defaults */
887 0x00, /* data 5: no instance request */
890 resp_len = sizeof(resp);
892 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
893 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
900 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
901 uint8_t *bootdev, bool *persistent)
907 0x05, /* parameter selector: boot flags */
908 0x00, /* no set selector */
909 0x00, /* no block selector */
912 resp_len = sizeof(resp);
913 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
914 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
919 pb_log("platform: error reading IPMI boot options\n");
923 if (resp_len != sizeof(resp)) {
924 pb_log("platform: unexpected length (%d) in "
925 "boot options response\n", resp_len);
929 pb_debug("IPMI get_bootdev response:\n");
930 for (int i = 0; i < resp_len; i++)
931 pb_debug("%x ", resp[i]);
935 pb_log("platform: non-zero completion code %d from IPMI req\n",
940 /* check for correct parameter version */
941 if ((resp[1] & 0xf) != 0x1) {
942 pb_log("platform: unexpected version (0x%x) in "
943 "boot options response\n", resp[0]);
947 /* check for valid paramters */
948 if (resp[2] & 0x80) {
949 pb_debug("platform: boot options are invalid/locked\n");
955 /* check for valid flags */
956 if (!(resp[3] & 0x80)) {
957 pb_debug("platform: boot flags are invalid, ignoring\n");
961 *persistent = resp[3] & 0x40;
962 *bootdev = (resp[4] >> 2) & 0x0f;
966 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
972 0x00, /* sensor number: os boot */
973 0xA9, /* operation: set everything */
974 0x00, /* sensor reading: none */
975 0x40, /* assertion mask lsb: set state 6 */
976 0x00, /* assertion mask msb: none */
977 0x00, /* deassertion mask lsb: none */
978 0x00, /* deassertion mask msb: none */
979 0x00, /* event data 1: none */
980 0x00, /* event data 2: none */
981 0x00, /* event data 3: none */
984 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
985 if (sensor_number < 0) {
986 pb_log("Couldn't find OS boot sensor in device tree\n");
990 req[0] = sensor_number;
992 resp_len = sizeof(resp);
994 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
998 ipmi_timeout); return 0;
1003 static void get_ipmi_bmc_mac(struct platform *p, uint8_t *buf)
1005 struct platform_powerpc *platform = p->platform_data;
1006 uint16_t resp_len = 8;
1008 uint8_t req[] = { 0x1, 0x5, 0x0, 0x0 };
1011 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_TRANSPORT,
1012 IPMI_CMD_TRANSPORT_GET_LAN_PARAMS,
1017 pb_debug("BMC MAC resp [%d][%d]:\n", rc, resp_len);
1019 if (rc == 0 && resp_len > 0) {
1020 for (i = 2; i < resp_len; i++) {
1021 pb_debug(" %x", resp[i]);
1022 buf[i - 2] = resp[i];
1030 * Retrieve info from the "Get Device ID" IPMI commands.
1031 * See Chapter 20.1 in the IPMIv2 specification.
1033 static void get_ipmi_bmc_versions(struct platform *p, struct system_info *info)
1035 struct platform_powerpc *platform = p->platform_data;
1036 uint16_t resp_len = 16;
1037 uint8_t resp[16], bcd;
1038 uint32_t aux_version;
1041 /* Retrieve info from current side */
1042 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_APP,
1043 IPMI_CMD_APP_GET_DEVICE_ID,
1048 pb_debug("BMC version resp [%d][%d]:\n", rc, resp_len);
1050 for (i = 0; i < resp_len; i++) {
1051 pb_debug(" %x", resp[i]);
1056 if (rc == 0 && resp_len == 16) {
1057 info->bmc_current = talloc_array(info, char *, 4);
1058 info->n_bmc_current = 4;
1060 info->bmc_current[0] = talloc_asprintf(info, "Device ID: 0x%x",
1062 info->bmc_current[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1064 bcd = resp[4] & 0x0f;
1065 bcd += 10 * (resp[4] >> 4);
1066 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1067 info->bmc_current[2] = talloc_asprintf(info,
1068 "Firmware version: %u.%u.%u",
1069 resp[3], bcd, aux_version);
1070 bcd = resp[5] & 0x0f;
1071 bcd += 10 * (resp[5] >> 4);
1072 info->bmc_current[3] = talloc_asprintf(info, "IPMI version: %u",
1075 pb_log("Failed to retrieve Device ID from IPMI\n");
1077 /* Retrieve info from golden side */
1078 memset(resp, 0, sizeof(resp));
1080 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_AMI,
1081 IPMI_CMD_APP_GET_DEVICE_ID_GOLDEN,
1086 pb_debug("BMC golden resp [%d][%d]:\n", rc, resp_len);
1088 for (i = 0; i < resp_len; i++) {
1089 pb_debug(" %x", resp[i]);
1094 if (rc == 0 && resp_len == 16) {
1095 info->bmc_golden = talloc_array(info, char *, 4);
1096 info->n_bmc_golden = 4;
1098 info->bmc_golden[0] = talloc_asprintf(info, "Device ID: 0x%x",
1100 info->bmc_golden[1] = talloc_asprintf(info, "Device Rev: 0x%x",
1102 bcd = resp[4] & 0x0f;
1103 bcd += 10 * (resp[4] >> 4);
1104 memcpy(&aux_version, &resp[12], sizeof(aux_version));
1105 info->bmc_golden[2] = talloc_asprintf(info,
1106 "Firmware version: %u.%u.%u",
1107 resp[3], bcd, aux_version);
1108 bcd = resp[5] & 0x0f;
1109 bcd += 10 * (resp[5] >> 4);
1110 info->bmc_golden[3] = talloc_asprintf(info, "IPMI version: %u",
1113 pb_log("Failed to retrieve Golden Device ID from IPMI\n");
1116 static int load_config(struct platform *p, struct config *config)
1118 struct platform_powerpc *platform = to_platform_powerpc(p);
1121 rc = parse_nvram(platform);
1125 populate_config(platform, config);
1127 if (platform->get_ipmi_bootdev) {
1128 bool bootdev_persistent;
1130 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1131 &bootdev_persistent);
1132 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1133 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1140 static int save_config(struct platform *p, struct config *config)
1142 struct platform_powerpc *platform = to_platform_powerpc(p);
1143 struct config *defaults;
1146 defaults = talloc_zero(platform, struct config);
1147 config_set_defaults(defaults);
1149 rc = update_config(platform, config, defaults);
1151 talloc_free(defaults);
1155 static void pre_boot(struct platform *p, const struct config *config)
1157 struct platform_powerpc *platform = to_platform_powerpc(p);
1159 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1160 platform->clear_ipmi_bootdev(platform, false);
1162 if (platform->set_os_boot_sensor)
1163 platform->set_os_boot_sensor(platform);
1166 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1168 struct platform_powerpc *platform = p->platform_data;
1169 char *buf, *filename;
1172 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1173 rc = read_file(platform, filename, &buf, &len);
1175 sysinfo->type = talloc_steal(sysinfo, buf);
1176 talloc_free(filename);
1178 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1179 rc = read_file(platform, filename, &buf, &len);
1181 sysinfo->identifier = talloc_steal(sysinfo, buf);
1182 talloc_free(filename);
1184 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1186 get_ipmi_bmc_mac(p, sysinfo->bmc_mac);
1189 get_ipmi_bmc_versions(p, sysinfo);
1191 if (platform->get_platform_versions)
1192 platform->get_platform_versions(sysinfo);
1197 static bool probe(struct platform *p, void *ctx)
1199 struct platform_powerpc *platform;
1200 struct stat statbuf;
1203 /* we need a device tree */
1204 rc = stat("/proc/device-tree", &statbuf);
1208 if (!S_ISDIR(statbuf.st_mode))
1211 platform = talloc_zero(ctx, struct platform_powerpc);
1212 list_init(&platform->params);
1214 p->platform_data = platform;
1216 if (ipmi_present()) {
1217 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1218 platform->ipmi = ipmi_open(platform);
1219 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1220 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1221 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1222 } else if (!stat(sysparams_dir, &statbuf)) {
1223 pb_debug("platform: using sysparams for IPMI paramters\n");
1224 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1225 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1228 pb_log("platform: no IPMI parameter support\n");
1231 rc = stat("/proc/device-tree/bmc", &statbuf);
1233 platform->get_platform_versions = hostboot_load_versions;
1239 static struct platform platform_powerpc = {
1241 .dhcp_arch_id = 0x000e,
1243 .load_config = load_config,
1244 .save_config = save_config,
1245 .pre_boot = pre_boot,
1246 .get_sysinfo = get_sysinfo,
1249 register_platform(platform_powerpc);