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/";
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_stdout *stdout;
155 argv[1] = "--print-config";
156 argv[2] = "--partition";
160 rc = process_get_stdout_argv(NULL, &stdout, argv);
163 fprintf(stderr, "nvram process returned "
164 "non-zero exit status\n");
167 rc = parse_nvram_params(platform, stdout->buf, stdout->len);
174 static int write_nvram(struct platform_powerpc *platform)
176 struct process *process;
182 argv[1] = "--update-config";
184 argv[3] = "--partition";
188 process = process_create(platform);
189 process->path = "nvram";
190 process->argv = argv;
192 list_for_each_entry(&platform->params, param, list) {
195 if (!param->modified)
198 paramstr = talloc_asprintf(platform, "%s=%s",
199 param->name, param->value);
202 rc = process_run_sync(process);
204 talloc_free(paramstr);
206 if (rc || !process_exit_ok(process)) {
208 pb_log("nvram update process returned "
209 "non-zero exit status\n");
214 process_release(process);
218 static const char *get_param(struct platform_powerpc *platform,
223 list_for_each_entry(&platform->params, param, list)
224 if (!strcmp(param->name, name))
229 static void set_param(struct platform_powerpc *platform, const char *name,
234 list_for_each_entry(&platform->params, param, list) {
235 if (strcmp(param->name, name))
238 if (!strcmp(param->value, value))
241 talloc_free(param->value);
242 param->value = talloc_strdup(param, value);
243 param->modified = true;
248 param = talloc(platform, struct param);
249 param->modified = true;
250 param->name = talloc_strdup(platform, name);
251 param->value = talloc_strdup(platform, value);
252 list_add(&platform->params, ¶m->list);
255 static int parse_hwaddr(struct interface_config *ifconf, char *str)
259 if (strlen(str) != strlen("00:00:00:00:00:00"))
262 for (i = 0; i < HWADDR_SIZE; i++) {
266 byte[0] = str[i * 3 + 0];
267 byte[1] = str[i * 3 + 1];
270 x = strtoul(byte, &endp, 16);
271 if (endp != byte + 2)
274 ifconf->hwaddr[i] = x & 0xff;
280 static int parse_one_interface_config(struct config *config,
283 struct interface_config *ifconf;
284 char *tok, *tok_gw, *tok_url, *saveptr;
286 ifconf = talloc_zero(config, struct interface_config);
288 if (!confstr || !strlen(confstr))
291 /* first token should be the mac address */
292 tok = strtok_r(confstr, ",", &saveptr);
296 if (parse_hwaddr(ifconf, tok))
299 /* second token is the method */
300 tok = strtok_r(NULL, ",", &saveptr);
301 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
302 ifconf->ignore = true;
304 } else if (!strcmp(tok, "dhcp")) {
305 ifconf->method = CONFIG_METHOD_DHCP;
307 } else if (!strcmp(tok, "static")) {
308 ifconf->method = CONFIG_METHOD_STATIC;
310 /* ip/mask, [optional] gateway, [optional] url */
311 tok = strtok_r(NULL, ",", &saveptr);
314 ifconf->static_config.address =
315 talloc_strdup(ifconf, tok);
318 * If a url is set but not a gateway, we can accidentally
319 * interpret the url as the gateway. To avoid changing the
320 * parameter format check if the "gateway" is actually a
321 * pb-url if it's the last token.
323 tok_gw = strtok_r(NULL, ",", &saveptr);
324 tok_url = strtok_r(NULL, ",", &saveptr);
327 if (tok_url || !is_url(tok_gw))
328 ifconf->static_config.gateway =
329 talloc_strdup(ifconf, tok_gw);
335 ifconf->static_config.url =
336 talloc_strdup(ifconf, tok_url);
338 pb_log("Unknown network configuration method %s\n", tok);
342 config->network.interfaces = talloc_realloc(config,
343 config->network.interfaces,
344 struct interface_config *,
345 ++config->network.n_interfaces);
347 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
355 static int parse_one_dns_config(struct config *config,
358 char *tok, *saveptr = NULL;
360 for (tok = strtok_r(confstr, ",", &saveptr); tok;
361 tok = strtok_r(NULL, ",", &saveptr)) {
363 char *server = talloc_strdup(config, tok);
365 config->network.dns_servers = talloc_realloc(config,
366 config->network.dns_servers, const char *,
367 ++config->network.n_dns_servers);
369 config->network.dns_servers[config->network.n_dns_servers - 1]
376 static void populate_network_config(struct platform_powerpc *platform,
377 struct config *config)
379 char *val, *saveptr = NULL;
383 cval = get_param(platform, "petitboot,network");
384 if (!cval || !strlen(cval))
387 val = talloc_strdup(config, cval);
392 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
396 if (!strncasecmp(tok, "dns,", strlen("dns,")))
397 parse_one_dns_config(config, tok + strlen("dns,"));
399 parse_one_interface_config(config, tok);
406 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
408 char *delim = strchr(*pos, ' ');
409 int len, prefix = 0, rc = -1;
410 enum device_type type;
412 if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
413 prefix = strlen("uuid:");
414 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;
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;
431 len = strlen(*pos) - prefix;
434 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
439 /* Always advance pointer to next option or end */
443 *pos += strlen(*pos);
448 static void populate_bootdev_config(struct platform_powerpc *platform,
449 struct config *config)
451 struct autoboot_option *opt, *new = NULL;
453 unsigned int n_new = 0;
456 /* Check for ordered bootdevs */
457 val = get_param(platform, "petitboot,bootdevs");
458 if (!val || !strlen(val)) {
461 pos = talloc_strdup(config, val);
462 end = strchr(pos, '\0');
465 while (pos && pos < end) {
466 opt = talloc(config, struct autoboot_option);
468 if (read_bootdev(config, &pos, opt)) {
469 pb_log("bootdev config is in an unknown format "
470 "(expected uuid:... or mac:...)\n");
475 new = talloc_realloc(config, new, struct autoboot_option,
484 /* If autoboot has been disabled, clear the default options */
485 if (!config->autoboot_enabled) {
486 talloc_free(config->autoboot_opts);
487 config->n_autoboot_opts = 0;
492 talloc_free(config->autoboot_opts);
493 config->autoboot_opts = new;
494 config->n_autoboot_opts = n_new;
497 static void populate_config(struct platform_powerpc *platform,
498 struct config *config)
502 unsigned long timeout;
504 /* if the "auto-boot?' property is present and "false", disable auto
506 val = get_param(platform, "auto-boot?");
507 config->autoboot_enabled = !val || strcmp(val, "false");
509 val = get_param(platform, "petitboot,timeout");
511 timeout = strtoul(val, &end, 10);
513 if (timeout >= INT_MAX)
515 config->autoboot_timeout_sec = (int)timeout;
519 val = get_param(platform, "petitboot,language");
520 config->lang = val ? talloc_strdup(config, val) : NULL;
522 populate_network_config(platform, config);
524 populate_bootdev_config(platform, config);
526 if (!config->debug) {
527 val = get_param(platform, "petitboot,debug?");
528 config->debug = val && !strcmp(val, "true");
531 val = get_param(platform, "petitboot,write?");
533 config->allow_writes = !strcmp(val, "true");
535 val = get_param(platform, "petitboot,snapshots?");
537 config->disable_snapshots = !strcmp(val, "false");
539 val = get_param(platform, "petitboot,console");
541 config->boot_console = talloc_strdup(config, val);
542 /* If a full path is already set we don't want to override it */
543 config->manual_console = config->boot_console &&
544 !strchr(config->boot_console, '[');
546 val = get_param(platform, "petitboot,http_proxy");
548 config->http_proxy = talloc_strdup(config, val);
549 val = get_param(platform, "petitboot,https_proxy");
551 config->https_proxy = talloc_strdup(config, val);
554 static char *iface_config_str(void *ctx, struct interface_config *config)
558 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
559 * different hardware address formats */
560 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
561 config->hwaddr[0], config->hwaddr[1],
562 config->hwaddr[2], config->hwaddr[3],
563 config->hwaddr[4], config->hwaddr[5]);
565 if (config->ignore) {
566 str = talloc_asprintf_append(str, "ignore");
568 } else if (config->method == CONFIG_METHOD_DHCP) {
569 str = talloc_asprintf_append(str, "dhcp");
571 } else if (config->method == CONFIG_METHOD_STATIC) {
572 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
573 config->static_config.address,
574 config->static_config.gateway ? "," : "",
575 config->static_config.gateway ?: "",
576 config->static_config.url ? "," : "",
577 config->static_config.url ?: "");
582 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
587 str = talloc_strdup(ctx, "dns,");
588 for (i = 0; i < n; i++) {
589 str = talloc_asprintf_append(str, "%s%s",
597 static void update_string_config(struct platform_powerpc *platform,
598 const char *name, const char *value)
602 cur = get_param(platform, name);
604 /* don't set an empty parameter if it doesn't already exist */
605 if (!cur && !strlen(value))
608 set_param(platform, name, value);
611 static void update_network_config(struct platform_powerpc *platform,
612 struct config *config)
618 * Don't store IPMI overrides to NVRAM. If this was a persistent
619 * override it was already stored in NVRAM by
620 * get_ipmi_network_override()
622 if (config->network.n_interfaces &&
623 config->network.interfaces[0]->override)
626 val = talloc_strdup(platform, "");
628 for (i = 0; i < config->network.n_interfaces; i++) {
629 char *iface_str = iface_config_str(platform,
630 config->network.interfaces[i]);
631 val = talloc_asprintf_append(val, "%s%s",
632 *val == '\0' ? "" : " ", iface_str);
633 talloc_free(iface_str);
636 if (config->network.n_dns_servers) {
637 char *dns_str = dns_config_str(platform,
638 config->network.dns_servers,
639 config->network.n_dns_servers);
640 val = talloc_asprintf_append(val, "%s%s",
641 *val == '\0' ? "" : " ", dns_str);
642 talloc_free(dns_str);
645 update_string_config(platform, "petitboot,network", val);
650 static void update_bootdev_config(struct platform_powerpc *platform,
651 struct config *config)
653 char *val = NULL, *boot_str = NULL, *tmp = NULL;
654 struct autoboot_option *opt;
655 const char delim = ' ';
658 if (!config->n_autoboot_opts)
661 for (i = 0; i < config->n_autoboot_opts; i++) {
662 opt = &config->autoboot_opts[i];
663 switch (opt->boot_type) {
664 case BOOT_DEVICE_TYPE:
665 boot_str = talloc_asprintf(config, "%s%c",
666 device_type_name(opt->type),
669 case BOOT_DEVICE_UUID:
670 boot_str = talloc_asprintf(config, "uuid:%s%c",
674 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
677 update_string_config(platform, "petitboot,bootdevs", val);
680 talloc_free(boot_str);
683 static int update_config(struct platform_powerpc *platform,
684 struct config *config, struct config *defaults)
689 if (config->autoboot_enabled == defaults->autoboot_enabled)
692 val = config->autoboot_enabled ? "true" : "false";
693 update_string_config(platform, "auto-boot?", val);
695 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
698 val = tmp = talloc_asprintf(platform, "%d",
699 config->autoboot_timeout_sec);
701 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
702 platform->clear_ipmi_bootdev) {
703 platform->clear_ipmi_bootdev(platform,
704 config->ipmi_bootdev_persistent);
705 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
706 config->ipmi_bootdev_persistent = false;
709 update_string_config(platform, "petitboot,timeout", val);
713 val = config->lang ?: "";
714 update_string_config(platform, "petitboot,language", val);
716 if (config->allow_writes == defaults->allow_writes)
719 val = config->allow_writes ? "true" : "false";
720 update_string_config(platform, "petitboot,write?", val);
722 if (!config->manual_console) {
723 val = config->boot_console ?: "";
724 update_string_config(platform, "petitboot,console", val);
727 val = config->http_proxy ?: "";
728 update_string_config(platform, "petitboot,http_proxy", val);
729 val = config->https_proxy ?: "";
730 update_string_config(platform, "petitboot,https_proxy", val);
732 update_network_config(platform, config);
734 update_bootdev_config(platform, config);
736 return write_nvram(platform);
739 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
742 config->ipmi_bootdev = bootdev;
743 config->ipmi_bootdev_persistent = persistent;
745 if (bootdev == IPMI_BOOTDEV_SAFE)
746 config->safe_mode = true;
749 static int read_bootdev_sysparam(const char *name, uint8_t *val)
755 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
756 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
758 fd = open(path, O_RDONLY);
760 pb_debug("powerpc: can't access sysparam %s\n",
765 rc = read(fd, buf, sizeof(buf));
769 /* bootdev definitions should only be one byte in size */
771 pb_debug("powerpc: sysparam %s read returned %d\n",
776 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
778 if (!ipmi_bootdev_is_valid(buf[0]))
785 static int write_bootdev_sysparam(const char *name, uint8_t val)
790 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
791 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
793 fd = open(path, O_WRONLY);
795 pb_debug("powerpc: can't access sysparam %s for writing\n",
802 rc = write(fd, &val, sizeof(val));
803 if (rc == sizeof(val)) {
808 if (rc <= 0 && errno != EINTR) {
809 pb_log("powerpc: error updating sysparam %s: %s",
810 name, strerror(errno));
819 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
824 static int clear_ipmi_bootdev_sysparams(
825 struct platform_powerpc *platform __attribute__((unused)),
829 /* invalidate default-boot-device setting */
830 write_bootdev_sysparam("default-boot-device", 0xff);
832 /* invalidate next-boot-device setting */
833 write_bootdev_sysparam("next-boot-device", 0xff);
838 static int get_ipmi_bootdev_sysparams(
839 struct platform_powerpc *platform __attribute__((unused)),
840 uint8_t *bootdev, bool *persistent)
842 uint8_t next_bootdev, default_bootdev;
843 bool next_valid, default_valid;
846 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
847 next_valid = rc == 0;
849 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
850 default_valid = rc == 0;
852 /* nothing valid? no need to change the config */
853 if (!next_valid && !default_valid)
856 *persistent = !next_valid;
857 *bootdev = next_valid ? next_bootdev : default_bootdev;
861 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
862 bool persistent __attribute__((unused)))
867 0x05, /* parameter selector: boot flags */
868 0x80, /* data 1: valid */
869 0x00, /* data 2: bootdev: no override */
870 0x00, /* data 3: system defaults */
871 0x00, /* data 4: no request for shared mode, mux defaults */
872 0x00, /* data 5: no instance request */
875 resp_len = sizeof(resp);
877 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
878 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
885 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
886 uint8_t *bootdev, bool *persistent)
892 0x05, /* parameter selector: boot flags */
893 0x00, /* no set selector */
894 0x00, /* no block selector */
897 resp_len = sizeof(resp);
898 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
899 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
904 pb_log("platform: error reading IPMI boot options\n");
908 if (resp_len != sizeof(resp)) {
909 pb_log("platform: unexpected length (%d) in "
910 "boot options response\n", resp_len);
914 pb_debug("IPMI get_bootdev response:\n");
915 for (int i = 0; i < resp_len; i++)
916 pb_debug("%x ", resp[i]);
920 pb_log("platform: non-zero completion code %d from IPMI req\n",
925 /* check for correct parameter version */
926 if ((resp[1] & 0xf) != 0x1) {
927 pb_log("platform: unexpected version (0x%x) in "
928 "boot options response\n", resp[0]);
932 /* check for valid paramters */
933 if (resp[2] & 0x80) {
934 pb_debug("platform: boot options are invalid/locked\n");
940 /* check for valid flags */
941 if (!(resp[3] & 0x80)) {
942 pb_debug("platform: boot flags are invalid, ignoring\n");
946 *persistent = resp[3] & 0x40;
947 *bootdev = (resp[4] >> 2) & 0x0f;
951 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
957 0x00, /* sensor number: os boot */
958 0xA9, /* operation: set everything */
959 0x00, /* sensor reading: none */
960 0x40, /* assertion mask lsb: set state 6 */
961 0x00, /* assertion mask msb: none */
962 0x00, /* deassertion mask lsb: none */
963 0x00, /* deassertion mask msb: none */
964 0x00, /* event data 1: none */
965 0x00, /* event data 2: none */
966 0x00, /* event data 3: none */
969 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
970 if (sensor_number < 0) {
971 pb_log("Couldn't find OS boot sensor in device tree\n");
975 req[0] = sensor_number;
977 resp_len = sizeof(resp);
979 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
983 ipmi_timeout); return 0;
988 static void get_ipmi_network_override(struct platform_powerpc *platform,
989 struct config *config)
991 uint16_t min_len = 12, resp_len = 53, version;
992 const uint32_t magic_value = 0x21706221;
993 uint8_t resp[resp_len];
998 0x61, /* parameter selector: OEM section (network) */
999 0x00, /* no set selector */
1000 0x00, /* no block selector */
1003 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
1004 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
1009 pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
1011 for (i = 0; i < resp_len; i++) {
1012 pb_debug(" %02x", resp[i]);
1013 if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
1015 else if (i && (i + 1) % 8 == 0)
1022 pb_debug("IPMI network config option unavailable\n");
1026 if (resp_len < min_len) {
1027 pb_debug("IPMI net response too small\n");
1032 pb_log("platform: non-zero completion code %d from IPMI network req\n",
1037 /* Check for correct parameter version */
1038 if ((resp[1] & 0xf) != 0x1) {
1039 pb_log("platform: unexpected version (0x%x) in network override response\n",
1044 /* Check that the parameters are valid */
1045 if (resp[2] & 0x80) {
1046 pb_debug("platform: network override is invalid/locked\n");
1050 /* Check for valid parameters in the boot flags section */
1051 if (!(resp[3] & 0x80)) {
1052 pb_debug("platform: network override valid flag not set\n");
1055 /* Read the persistent flag; if it is set we need to save this config */
1056 persistent = resp[3] & 0x40;
1058 pb_debug("platform: network override is persistent\n");
1060 /* Check 4-byte cookie value */
1062 memcpy(&cookie, &resp[i], sizeof(cookie));
1063 cookie = __be32_to_cpu(cookie);
1064 if (cookie != magic_value) {
1065 pb_log_fn("Incorrect cookie %x\n", cookie);
1068 i += sizeof(cookie);
1070 /* Check 2-byte version number */
1071 memcpy(&version, &resp[i], sizeof(version));
1072 version = __be16_to_cpu(version);
1073 i += sizeof(version);
1075 pb_debug("Unexpected version: %u\n", version);
1079 /* Interpret the rest of the interface config */
1080 rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
1082 if (!rc && persistent) {
1083 /* Write this new config to NVRAM */
1084 update_network_config(platform, config);
1085 rc = write_nvram(platform);
1087 pb_log("platform: Failed to save persistent interface override\n");
1091 static void get_active_consoles(struct config *config)
1094 char *fsp_prop = NULL;
1096 config->n_consoles = 2;
1097 config->consoles = talloc_array(config, char *, config->n_consoles);
1098 if (!config->consoles)
1101 config->consoles[0] = talloc_asprintf(config->consoles,
1102 "/dev/hvc0 [IPMI / Serial]");
1103 config->consoles[1] = talloc_asprintf(config->consoles,
1106 fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
1107 if (stat(fsp_prop, &sbuf) == 0) {
1108 /* FSP based machines also have a separate serial console */
1109 config->consoles = talloc_realloc(config, config->consoles,
1110 char *, config->n_consoles + 1);
1111 if (!config->consoles)
1113 config->consoles[config->n_consoles++] = talloc_asprintf(
1115 "/dev/hvc1 [Serial]");
1120 config->n_consoles = 0;
1121 pb_log("Failed to allocate memory for consoles\n");
1124 static int load_config(struct platform *p, struct config *config)
1126 struct platform_powerpc *platform = to_platform_powerpc(p);
1129 rc = parse_nvram(platform);
1131 pb_log_fn("Failed to parse nvram\n");
1133 populate_config(platform, config);
1135 if (platform->get_ipmi_bootdev) {
1136 bool bootdev_persistent;
1137 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
1138 rc = platform->get_ipmi_bootdev(platform, &bootdev,
1139 &bootdev_persistent);
1140 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
1141 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1146 get_ipmi_network_override(platform, config);
1148 get_active_consoles(config);
1153 static int save_config(struct platform *p, struct config *config)
1155 struct platform_powerpc *platform = to_platform_powerpc(p);
1156 struct config *defaults;
1159 defaults = talloc_zero(platform, struct config);
1160 config_set_defaults(defaults);
1162 rc = update_config(platform, config, defaults);
1164 talloc_free(defaults);
1168 static void pre_boot(struct platform *p, const struct config *config)
1170 struct platform_powerpc *platform = to_platform_powerpc(p);
1172 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1173 platform->clear_ipmi_bootdev(platform, false);
1175 if (platform->set_os_boot_sensor)
1176 platform->set_os_boot_sensor(platform);
1179 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1181 struct platform_powerpc *platform = p->platform_data;
1182 char *buf, *filename;
1185 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1186 rc = read_file(platform, filename, &buf, &len);
1188 sysinfo->type = talloc_steal(sysinfo, buf);
1189 talloc_free(filename);
1191 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1192 rc = read_file(platform, filename, &buf, &len);
1194 sysinfo->identifier = talloc_steal(sysinfo, buf);
1195 talloc_free(filename);
1197 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
1199 if (platform->ipmi) {
1200 ipmi_get_bmc_mac(platform->ipmi, sysinfo->bmc_mac);
1201 ipmi_get_bmc_versions(platform->ipmi, sysinfo);
1204 if (platform->get_platform_versions)
1205 platform->get_platform_versions(sysinfo);
1210 static bool probe(struct platform *p, void *ctx)
1212 struct platform_powerpc *platform;
1213 struct stat statbuf;
1217 /* we need a device tree */
1218 rc = stat("/proc/device-tree", &statbuf);
1222 if (!S_ISDIR(statbuf.st_mode))
1225 platform = talloc_zero(ctx, struct platform_powerpc);
1226 list_init(&platform->params);
1228 p->platform_data = platform;
1230 bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
1232 if (ipmi_present() && bmc_present) {
1233 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1234 platform->ipmi = ipmi_open(platform);
1235 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1236 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1237 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1238 } else if (!stat(sysparams_dir, &statbuf)) {
1239 pb_debug("platform: using sysparams for IPMI paramters\n");
1240 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1241 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1244 pb_log("platform: no IPMI parameter support\n");
1248 platform->get_platform_versions = hostboot_load_versions;
1254 static struct platform platform_powerpc = {
1256 .dhcp_arch_id = 0x000e,
1258 .load_config = load_config,
1259 .save_config = save_config,
1260 .pre_boot = pre_boot,
1261 .get_sysinfo = get_sysinfo,
1264 register_platform(platform_powerpc);