10 #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 <crypt/crypt.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/";
29 struct platform_powerpc {
30 struct param_list *params;
32 char *ipmi_mailbox_original_config;
33 int (*get_ipmi_bootdev)(
34 struct platform_powerpc *platform,
35 uint8_t *bootdev, bool *persistent);
36 int (*clear_ipmi_bootdev)(
37 struct platform_powerpc *platform,
39 int (*get_ipmi_boot_mailbox)(
40 struct platform_powerpc *platform,
42 int (*clear_ipmi_boot_mailbox)(
43 struct platform_powerpc *platform);
44 int (*set_os_boot_sensor)(
45 struct platform_powerpc *platform);
46 void (*get_platform_versions)(struct system_info *info);
49 #define to_platform_powerpc(p) \
50 (struct platform_powerpc *)(p->platform_data)
52 static int parse_nvram_params(struct platform_powerpc *platform,
55 char *pos, *name, *value;
56 unsigned int paramlen;
59 /* discard 2 header lines:
66 for (i = 0; i < len; i++) {
74 fprintf(stderr, "failure parsing nvram output\n");
78 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
82 newline = strchr(pos, '\n');
88 paramlen = strlen(pos);
91 value = strchr(pos, '=');
95 namelen = value - name;
99 if (!param_list_is_known_n(platform->params, name, namelen))
105 param_list_set(platform->params, name, value, false);
111 static int parse_nvram(struct platform_powerpc *platform)
113 struct process_stdout *stdout;
118 argv[1] = "--print-config";
119 argv[2] = "--partition";
123 rc = process_get_stdout_argv(NULL, &stdout, argv);
126 fprintf(stderr, "nvram process returned "
127 "non-zero exit status\n");
130 rc = parse_nvram_params(platform, stdout->buf, stdout->len);
137 static int write_nvram(struct platform_powerpc *platform)
139 struct process *process;
145 argv[1] = "--update-config";
147 argv[3] = "--partition";
151 process = process_create(platform);
152 process->path = "nvram";
153 process->argv = argv;
155 param_list_for_each(platform->params, param) {
158 if (!param->modified)
161 paramstr = talloc_asprintf(platform, "%s=%s",
162 param->name, param->value);
165 rc = process_run_sync(process);
167 talloc_free(paramstr);
169 if (rc || !process_exit_ok(process)) {
171 pb_log("nvram update process returned "
172 "non-zero exit status\n");
177 process_release(process);
181 static void params_update_all(struct param_list *pl,
182 const struct config *config, const struct config *defaults)
187 if (config->autoboot_enabled == defaults->autoboot_enabled)
190 val = config->autoboot_enabled ? "true" : "false";
192 param_list_set_non_empty(pl, "auto-boot?", val, true);
194 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
197 val = tmp = talloc_asprintf(pl, "%d",
198 config->autoboot_timeout_sec);
200 param_list_set_non_empty(pl, "petitboot,timeout", val, true);
204 val = config->lang ?: "";
205 param_list_set_non_empty(pl, "petitboot,language", val, true);
207 if (config->allow_writes == defaults->allow_writes)
210 val = config->allow_writes ? "true" : "false";
211 param_list_set_non_empty(pl, "petitboot,write?", val, true);
213 if (!config->manual_console) {
214 val = config->boot_console ?: "";
215 param_list_set_non_empty(pl, "petitboot,console", val, true);
218 val = config->http_proxy ?: "";
219 param_list_set_non_empty(pl, "petitboot,http_proxy", val, true);
220 val = config->https_proxy ?: "";
221 param_list_set_non_empty(pl, "petitboot,https_proxy", val, true);
223 params_update_network_values(pl, "petitboot,network", config);
224 params_update_bootdev_values(pl, "petitboot,bootdevs", config);
227 static void config_set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
230 config->ipmi_bootdev = bootdev;
231 config->ipmi_bootdev_persistent = persistent;
233 if (bootdev == IPMI_BOOTDEV_SAFE)
234 config->safe_mode = true;
237 static int read_bootdev_sysparam(const char *name, uint8_t *val)
243 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
244 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
246 fd = open(path, O_RDONLY);
248 pb_debug("powerpc: can't access sysparam %s\n",
253 rc = read(fd, buf, sizeof(buf));
257 /* bootdev definitions should only be one byte in size */
259 pb_debug("powerpc: sysparam %s read returned %d\n",
264 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
266 if (!ipmi_bootdev_is_valid(buf[0]))
273 static int write_bootdev_sysparam(const char *name, uint8_t val)
278 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
279 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
281 fd = open(path, O_WRONLY);
283 pb_debug("powerpc: can't access sysparam %s for writing\n",
290 rc = write(fd, &val, sizeof(val));
291 if (rc == sizeof(val)) {
296 if (rc <= 0 && errno != EINTR) {
297 pb_log("powerpc: error updating sysparam %s: %s",
298 name, strerror(errno));
307 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
312 static int clear_ipmi_bootdev_sysparams(
313 struct platform_powerpc *platform __attribute__((unused)),
317 /* invalidate default-boot-device setting */
318 write_bootdev_sysparam("default-boot-device", 0xff);
320 /* invalidate next-boot-device setting */
321 write_bootdev_sysparam("next-boot-device", 0xff);
326 static int get_ipmi_bootdev_sysparams(
327 struct platform_powerpc *platform __attribute__((unused)),
328 uint8_t *bootdev, bool *persistent)
330 uint8_t next_bootdev, default_bootdev;
331 bool next_valid, default_valid;
334 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
335 next_valid = rc == 0;
337 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
338 default_valid = rc == 0;
340 /* nothing valid? no need to change the config */
341 if (!next_valid && !default_valid)
344 *persistent = !next_valid;
345 *bootdev = next_valid ? next_bootdev : default_bootdev;
349 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
350 bool persistent __attribute__((unused)))
355 0x05, /* parameter selector: boot flags */
356 0x80, /* data 1: valid */
357 0x00, /* data 2: bootdev: no override */
358 0x00, /* data 3: system defaults */
359 0x00, /* data 4: no request for shared mode, mux defaults */
360 0x00, /* data 5: no instance request */
363 resp_len = sizeof(resp);
365 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
366 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
373 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
374 uint8_t *bootdev, bool *persistent)
381 0x05, /* parameter selector: boot flags */
382 0x00, /* no set selector */
383 0x00, /* no block selector */
386 resp_len = sizeof(resp);
387 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
388 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
393 pb_log("platform: error reading IPMI boot options\n");
397 if (resp_len != sizeof(resp)) {
398 pb_log("platform: unexpected length (%d) in "
399 "boot options response\n", resp_len);
403 debug_buf = format_buffer(platform, resp, resp_len);
404 pb_debug_fn("IPMI get_bootdev response:\n%s\n", debug_buf);
405 talloc_free(debug_buf);
408 pb_log("platform: non-zero completion code %d from IPMI req\n",
413 /* check for correct parameter version */
414 if ((resp[1] & 0xf) != 0x1) {
415 pb_log("platform: unexpected version (0x%x) in "
416 "boot options response\n", resp[0]);
420 /* check for valid paramters */
421 if (resp[2] & 0x80) {
422 pb_debug("platform: boot options are invalid/locked\n");
428 /* check for valid flags */
429 if (!(resp[3] & 0x80)) {
430 pb_debug("platform: boot flags are invalid, ignoring\n");
434 *persistent = resp[3] & 0x40;
435 *bootdev = (resp[4] >> 2) & 0x0f;
439 static int get_ipmi_boot_mailbox_block(struct platform_powerpc *platform,
440 char *buf, uint8_t block)
442 size_t blocksize = 16;
443 uint8_t resp[3 + 16];
448 0x07, /* parameter selector: boot initiator mailbox */
449 block, /* set selector */
450 0x00, /* no block selector */
453 resp_len = sizeof(resp);
454 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
455 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
460 pb_log("platform: error reading IPMI boot options\n");
464 if (resp_len < sizeof(resp)) {
466 pb_log("platform: unexpected length (%d) in "
467 "boot options mailbox response\n",
473 pb_debug_fn("block %hu empty\n", block);
477 blocksize = sizeof(resp) - 3;
478 pb_debug_fn("Mailbox block %hu returns only %zu bytes in block\n",
482 debug_buf = format_buffer(platform, resp, resp_len);
483 pb_debug_fn("IPMI bootdev mailbox block %hu:\n%s\n", block, debug_buf);
484 talloc_free(debug_buf);
487 pb_log("platform: non-zero completion code %d from IPMI req\n",
492 /* check for correct parameter version */
493 if ((resp[1] & 0xf) != 0x1) {
494 pb_log("platform: unexpected version (0x%x) in "
495 "boot mailbox response\n", resp[0]);
499 /* check for valid paramters */
500 if (resp[2] & 0x80) {
501 pb_debug("platform: boot mailbox parameters are invalid/locked\n");
505 memcpy(buf, &resp[3], blocksize);
510 static int get_ipmi_boot_mailbox(struct platform_powerpc *platform,
513 char *mailbox_buffer, *prefix;
514 const size_t blocksize = 16;
515 char block_buffer[blocksize];
521 mailbox_buffer = NULL;
525 * The BMC may hold up to 255 blocks of data but more likely the number
526 * will be closer to the minimum of 5 set by the specification and error
529 for (i = 0; i < UCHAR_MAX; i++) {
530 rc = get_ipmi_boot_mailbox_block(platform, block_buffer, i);
531 if (rc < 3 && i == 0) {
533 * Immediate failure, no blocks read or missing IANA
539 /* Error or no bytes read */
545 * The first three bytes of block zero are an IANA
546 * Enterprise ID number. Check it matches the IBM
549 if (block_buffer[0] != 0x02 ||
550 block_buffer[1] != 0x00 ||
551 block_buffer[2] != 0x00) {
552 pb_log_fn("IANA number unrecognised: 0x%x:0x%x:0x%x\n",
560 mailbox_buffer = talloc_realloc(platform, mailbox_buffer,
561 char, mailbox_size + rc);
562 if (!mailbox_buffer) {
563 pb_log_fn("Failed to allocate mailbox buffer\n");
566 memcpy(mailbox_buffer + mailbox_size, block_buffer, rc);
571 pb_log_fn("Only %hu blocks read, spec requires at least 5.\n"
572 "Send a bug report to your preferred BMC vendor!\n",
575 pb_debug_fn("%hu blocks read (%zu bytes)\n", i, mailbox_size);
577 if (mailbox_size < 3 + strlen("petitboot,bootdevs="))
580 prefix = talloc_strndup(mailbox_buffer, mailbox_buffer + 3,
581 strlen("petitboot,bootdevs="));
583 pb_log_fn("Couldn't check prefix\n");
584 talloc_free(mailbox_buffer);
588 if (strncmp(prefix, "petitboot,bootdevs=",
589 strlen("petitboot,bootdevs=")) != 0 ) {
590 /* Empty or garbage */
591 pb_debug_fn("Buffer looks unconfigured\n");
592 talloc_free(mailbox_buffer);
597 /* Don't include IANA number in buffer */
598 content_size = mailbox_size - 3 - strlen("petitboot,bootdevs=");
599 *buf = talloc_memdup(platform,
600 mailbox_buffer + 3 + strlen("petitboot,bootdevs="),
602 (*buf)[content_size] = '\0';
604 talloc_free(mailbox_buffer);
608 static int clear_ipmi_boot_mailbox(struct platform_powerpc *platform)
610 uint8_t req[18] = {0}; /* req (2) + blocksize (16) */
616 req[0] = 0x07; /* parameter selector: boot initiator mailbox */
618 resp_len = sizeof(resp);
620 for (i = 0; i < UCHAR_MAX; i++) {
621 req[1] = i; /* set selector */
622 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
623 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
630 pb_log_fn("error clearing IPMI boot mailbox, "
631 "rc %d resp[0] %hu\n",
639 pb_debug_fn("Cleared %hu blocks\n", i);
644 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
650 0x00, /* sensor number: os boot */
651 0xA9, /* operation: set everything */
652 0x00, /* sensor reading: none */
653 0x40, /* assertion mask lsb: set state 6 */
654 0x00, /* assertion mask msb: none */
655 0x00, /* deassertion mask lsb: none */
656 0x00, /* deassertion mask msb: none */
657 0x00, /* event data 1: none */
658 0x00, /* event data 2: none */
659 0x00, /* event data 3: none */
662 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
663 if (sensor_number < 0) {
664 pb_log("Couldn't find OS boot sensor in device tree\n");
668 req[0] = sensor_number;
670 resp_len = sizeof(resp);
672 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
676 ipmi_timeout); return 0;
681 static void get_ipmi_network_override(struct platform_powerpc *platform,
682 struct config *config)
684 uint16_t min_len = 12, resp_len = 53, version;
685 const uint32_t magic_value = 0x21706221;
686 uint8_t resp[resp_len];
692 0x61, /* parameter selector: OEM section (network) */
693 0x00, /* no set selector */
694 0x00, /* no block selector */
697 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
698 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
703 debug_buf = format_buffer(platform, resp, resp_len);
704 pb_debug_fn("IPMI net override response:\n%s\n", debug_buf);
705 talloc_free(debug_buf);
708 pb_debug("IPMI network config option unavailable\n");
712 if (resp_len < min_len) {
713 pb_debug("IPMI net response too small\n");
718 pb_log("platform: non-zero completion code %d from IPMI network req\n",
723 /* Check for correct parameter version */
724 if ((resp[1] & 0xf) != 0x1) {
725 pb_log("platform: unexpected version (0x%x) in network override response\n",
730 /* Check that the parameters are valid */
731 if (resp[2] & 0x80) {
732 pb_debug("platform: network override is invalid/locked\n");
736 /* Check for valid parameters in the boot flags section */
737 if (!(resp[3] & 0x80)) {
738 pb_debug("platform: network override valid flag not set\n");
741 /* Read the persistent flag; if it is set we need to save this config */
742 persistent = resp[3] & 0x40;
744 pb_debug("platform: network override is persistent\n");
746 /* Check 4-byte cookie value */
748 memcpy(&cookie, &resp[i], sizeof(cookie));
749 cookie = __be32_to_cpu(cookie);
750 if (cookie != magic_value) {
751 pb_log_fn("Incorrect cookie %x\n", cookie);
756 /* Check 2-byte version number */
757 memcpy(&version, &resp[i], sizeof(version));
758 version = __be16_to_cpu(version);
759 i += sizeof(version);
761 pb_debug("Unexpected version: %u\n", version);
765 /* Interpret the rest of the interface config */
766 rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
768 if (!rc && persistent) {
769 /* Write this new config to NVRAM */
770 params_update_network_values(platform->params,
771 "petitboot,network", config);
772 rc = write_nvram(platform);
774 pb_log("platform: Failed to save persistent interface override\n");
778 static void config_get_active_consoles(struct config *config)
781 char *fsp_prop = NULL;
783 config->n_consoles = 2;
784 config->consoles = talloc_array(config, char *, config->n_consoles);
785 if (!config->consoles)
788 config->consoles[0] = talloc_asprintf(config->consoles,
789 "/dev/hvc0 [IPMI / Serial]");
790 config->consoles[1] = talloc_asprintf(config->consoles,
793 fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
794 if (stat(fsp_prop, &sbuf) == 0) {
795 /* FSP based machines also have a separate serial console */
796 config->consoles = talloc_realloc(config, config->consoles,
797 char *, config->n_consoles + 1);
798 if (!config->consoles)
800 config->consoles[config->n_consoles++] = talloc_asprintf(
802 "/dev/hvc1 [Serial]");
807 config->n_consoles = 0;
808 pb_log("Failed to allocate memory for consoles\n");
811 static int load_config(struct platform *p, struct config *config)
813 struct platform_powerpc *platform = to_platform_powerpc(p);
817 rc = parse_nvram(platform);
819 pb_log_fn("Failed to parse nvram\n");
822 * If we have an IPMI mailbox configuration available use it instead of
823 * the boot order found in NVRAM.
825 if (platform->get_ipmi_boot_mailbox) {
828 rc = platform->get_ipmi_boot_mailbox(platform, &mailbox);
829 if (!rc && mailbox) {
830 platform->ipmi_mailbox_original_config =
833 param_list_get_value(
834 platform->params, "petitboot,bootdevs"));
835 param_list_set(platform->params, "petitboot,bootdevs",
837 param = param_list_get_param(platform->params,
838 "petitboot,bootdevs");
839 /* Avoid writing this to NVRAM */
840 param->modified = false;
841 config->ipmi_bootdev_mailbox = true;
842 talloc_free(mailbox);
846 config_populate_all(config, platform->params);
848 if (platform->get_ipmi_bootdev) {
849 bool bootdev_persistent;
850 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
851 rc = platform->get_ipmi_bootdev(platform, &bootdev,
852 &bootdev_persistent);
853 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
854 config_set_ipmi_bootdev(config, bootdev,
860 get_ipmi_network_override(platform, config);
862 config_get_active_consoles(config);
865 hash = param_list_get_value(platform->params, "petitboot,password");
867 rc = crypt_set_password_hash(platform, hash);
869 pb_log("Failed to set password hash\n");
875 static int save_config(struct platform *p, struct config *config)
877 struct platform_powerpc *platform = to_platform_powerpc(p);
878 struct config *defaults;
881 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
882 platform->clear_ipmi_bootdev) {
883 platform->clear_ipmi_bootdev(platform,
884 config->ipmi_bootdev_persistent);
885 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
886 config->ipmi_bootdev_persistent = false;
889 if (!config->ipmi_bootdev_mailbox &&
890 platform->ipmi_mailbox_original_config) {
891 param = param_list_get_param(platform->params,
892 "petitboot,bootdevs");
893 /* Restore old boot order if unmodified */
894 if (!param->modified) {
895 param_list_set(platform->params, "petitboot,bootdevs",
896 platform->ipmi_mailbox_original_config,
898 param->modified = false;
899 config_populate_bootdev(config, platform->params);
901 platform->clear_ipmi_boot_mailbox(platform);
902 talloc_free(platform->ipmi_mailbox_original_config);
903 platform->ipmi_mailbox_original_config = NULL;
906 defaults = talloc_zero(platform, struct config);
907 config_set_defaults(defaults);
909 params_update_all(platform->params, config, defaults);
911 talloc_free(defaults);
912 return write_nvram(platform);
915 static void pre_boot(struct platform *p, const struct config *config)
917 struct platform_powerpc *platform = to_platform_powerpc(p);
919 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
920 platform->clear_ipmi_bootdev(platform, false);
922 if (platform->set_os_boot_sensor)
923 platform->set_os_boot_sensor(platform);
926 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
928 struct platform_powerpc *platform = p->platform_data;
929 char *buf, *filename;
932 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
933 rc = read_file(platform, filename, &buf, &len);
935 sysinfo->type = talloc_steal(sysinfo, buf);
936 talloc_free(filename);
938 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
939 rc = read_file(platform, filename, &buf, &len);
941 sysinfo->identifier = talloc_steal(sysinfo, buf);
942 talloc_free(filename);
944 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
946 if (platform->ipmi) {
947 ipmi_get_bmc_mac(platform->ipmi, sysinfo->bmc_mac);
948 ipmi_get_bmc_versions(platform->ipmi, sysinfo);
951 if (platform->get_platform_versions)
952 platform->get_platform_versions(sysinfo);
957 static bool restrict_clients(struct platform *p)
959 struct platform_powerpc *platform = to_platform_powerpc(p);
961 return param_list_get_value(platform->params, "petitboot,password") != NULL;
964 static int set_password(struct platform *p, const char *hash)
966 struct platform_powerpc *platform = to_platform_powerpc(p);
968 param_list_set(platform->params, "petitboot,password", hash, true);
969 write_nvram(platform);
974 static bool probe(struct platform *p, void *ctx)
976 struct platform_powerpc *platform;
981 /* we need a device tree */
982 rc = stat("/proc/device-tree", &statbuf);
986 if (!S_ISDIR(statbuf.st_mode))
989 platform = talloc_zero(ctx, struct platform_powerpc);
990 platform->params = talloc_zero(platform, struct param_list);
991 param_list_init(platform->params, common_known_params());
993 p->platform_data = platform;
995 bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
997 if (ipmi_present() && bmc_present) {
998 pb_debug("platform: using direct IPMI for IPMI paramters\n");
999 platform->ipmi = ipmi_open(platform);
1000 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1001 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1002 platform->get_ipmi_boot_mailbox = get_ipmi_boot_mailbox;
1003 platform->clear_ipmi_boot_mailbox = clear_ipmi_boot_mailbox;
1004 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1005 } else if (!stat(sysparams_dir, &statbuf)) {
1006 pb_debug("platform: using sysparams for IPMI paramters\n");
1007 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1008 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1011 pb_log("platform: no IPMI parameter support\n");
1015 platform->get_platform_versions = hostboot_load_versions;
1021 static struct platform platform_powerpc = {
1023 .dhcp_arch_id = 0x000e,
1025 .load_config = load_config,
1026 .save_config = save_config,
1027 .pre_boot = pre_boot,
1028 .get_sysinfo = get_sysinfo,
1029 .restrict_clients = restrict_clients,
1030 .set_password = set_password,
1033 register_platform(platform_powerpc);