10 #include <asm/byteorder.h>
12 #include <file/file.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <process/process.h>
23 static const char *partition = "common";
24 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
25 static const char *devtree_dir = "/proc/device-tree/";
27 struct platform_powerpc {
28 struct param_list params;
30 bool ipmi_bootdev_persistent;
31 int (*get_ipmi_bootdev)(
32 struct platform_powerpc *platform,
33 uint8_t *bootdev, bool *persistent);
34 int (*clear_ipmi_bootdev)(
35 struct platform_powerpc *platform,
37 int (*set_os_boot_sensor)(
38 struct platform_powerpc *platform);
39 void (*get_platform_versions)(struct system_info *info);
42 #define to_platform_powerpc(p) \
43 (struct platform_powerpc *)(p->platform_data)
45 static int parse_nvram_params(struct platform_powerpc *platform,
48 char *pos, *name, *value;
49 unsigned int paramlen;
52 /* discard 2 header lines:
59 for (i = 0; i < len; i++) {
67 fprintf(stderr, "failure parsing nvram output\n");
71 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
75 newline = strchr(pos, '\n');
81 paramlen = strlen(pos);
84 value = strchr(pos, '=');
88 namelen = value - name;
92 if (!param_list_is_known_n(&platform->params, name, namelen))
98 param_list_set(&platform->params, name, value, false);
104 static int parse_nvram(struct platform_powerpc *platform)
106 struct process_stdout *stdout;
111 argv[1] = "--print-config";
112 argv[2] = "--partition";
116 rc = process_get_stdout_argv(NULL, &stdout, argv);
119 fprintf(stderr, "nvram process returned "
120 "non-zero exit status\n");
123 rc = parse_nvram_params(platform, stdout->buf, stdout->len);
130 static int write_nvram(struct platform_powerpc *platform)
132 struct process *process;
138 argv[1] = "--update-config";
140 argv[3] = "--partition";
144 process = process_create(platform);
145 process->path = "nvram";
146 process->argv = argv;
148 param_list_for_each(&platform->params, param) {
151 if (!param->modified)
154 paramstr = talloc_asprintf(platform, "%s=%s",
155 param->name, param->value);
158 rc = process_run_sync(process);
160 talloc_free(paramstr);
162 if (rc || !process_exit_ok(process)) {
164 pb_log("nvram update process returned "
165 "non-zero exit status\n");
170 process_release(process);
174 static char *interface_config_str(void *ctx, struct interface_config *config)
178 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
179 * different hardware address formats */
180 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
181 config->hwaddr[0], config->hwaddr[1],
182 config->hwaddr[2], config->hwaddr[3],
183 config->hwaddr[4], config->hwaddr[5]);
185 if (config->ignore) {
186 str = talloc_asprintf_append(str, "ignore");
188 } else if (config->method == CONFIG_METHOD_DHCP) {
189 str = talloc_asprintf_append(str, "dhcp");
191 } else if (config->method == CONFIG_METHOD_STATIC) {
192 str = talloc_asprintf_append(str, "static,%s%s%s%s%s",
193 config->static_config.address,
194 config->static_config.gateway ? "," : "",
195 config->static_config.gateway ?: "",
196 config->static_config.url ? "," : "",
197 config->static_config.url ?: "");
202 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
207 str = talloc_strdup(ctx, "dns,");
208 for (i = 0; i < n; i++) {
209 str = talloc_asprintf_append(str, "%s%s",
217 static void update_network_config(struct param_list *pl, const char *param_name,
218 const struct config *config)
224 * Don't store IPMI overrides to NVRAM. If this was a persistent
225 * override it was already stored in NVRAM by
226 * get_ipmi_network_override()
228 if (config->network.n_interfaces &&
229 config->network.interfaces[0]->override)
232 val = talloc_strdup(pl, "");
234 for (i = 0; i < config->network.n_interfaces; i++) {
235 char *iface_str = interface_config_str(pl,
236 config->network.interfaces[i]);
237 val = talloc_asprintf_append(val, "%s%s",
238 *val == '\0' ? "" : " ", iface_str);
239 talloc_free(iface_str);
242 if (config->network.n_dns_servers) {
243 char *dns_str = dns_config_str(pl,
244 config->network.dns_servers,
245 config->network.n_dns_servers);
246 val = talloc_asprintf_append(val, "%s%s",
247 *val == '\0' ? "" : " ", dns_str);
248 talloc_free(dns_str);
251 param_list_set_non_empty(pl, param_name, val, true);
256 static void update_bootdev_config(struct param_list *pl, const char *param_name,
257 const struct config *config)
259 char *val = NULL, *boot_str = NULL, *tmp = NULL;
260 struct autoboot_option *opt;
261 const char delim = ' ';
264 if (!config->n_autoboot_opts)
267 for (i = 0; i < config->n_autoboot_opts; i++) {
268 opt = &config->autoboot_opts[i];
269 switch (opt->boot_type) {
270 case BOOT_DEVICE_TYPE:
271 boot_str = talloc_asprintf(config, "%s%c",
272 device_type_name(opt->type),
275 case BOOT_DEVICE_UUID:
276 boot_str = talloc_asprintf(config, "uuid:%s%c",
280 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
283 param_list_set_non_empty(pl, param_name, val, true);
287 talloc_free(boot_str);
290 static void update_config(struct param_list *pl, struct config *config,
291 const struct config *defaults)
296 if (config->autoboot_enabled == defaults->autoboot_enabled)
299 val = config->autoboot_enabled ? "true" : "false";
301 param_list_set_non_empty(pl, "auto-boot?", val, true);
303 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
306 val = tmp = talloc_asprintf(pl, "%d",
307 config->autoboot_timeout_sec);
309 param_list_set_non_empty(pl, "petitboot,timeout", val, true);
313 val = config->lang ?: "";
314 param_list_set_non_empty(pl, "petitboot,language", val, true);
316 if (config->allow_writes == defaults->allow_writes)
319 val = config->allow_writes ? "true" : "false";
320 param_list_set_non_empty(pl, "petitboot,write?", val, true);
322 if (!config->manual_console) {
323 val = config->boot_console ?: "";
324 param_list_set_non_empty(pl, "petitboot,console", val, true);
327 val = config->http_proxy ?: "";
328 param_list_set_non_empty(pl, "petitboot,http_proxy", val, true);
329 val = config->https_proxy ?: "";
330 param_list_set_non_empty(pl, "petitboot,https_proxy", val, true);
332 update_network_config(pl, "petitboot,network", config);
333 update_bootdev_config(pl, "petitboot,bootdevs", config);
336 static void config_set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
339 config->ipmi_bootdev = bootdev;
340 config->ipmi_bootdev_persistent = persistent;
342 if (bootdev == IPMI_BOOTDEV_SAFE)
343 config->safe_mode = true;
346 static int read_bootdev_sysparam(const char *name, uint8_t *val)
352 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
353 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
355 fd = open(path, O_RDONLY);
357 pb_debug("powerpc: can't access sysparam %s\n",
362 rc = read(fd, buf, sizeof(buf));
366 /* bootdev definitions should only be one byte in size */
368 pb_debug("powerpc: sysparam %s read returned %d\n",
373 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
375 if (!ipmi_bootdev_is_valid(buf[0]))
382 static int write_bootdev_sysparam(const char *name, uint8_t val)
387 assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
388 snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);
390 fd = open(path, O_WRONLY);
392 pb_debug("powerpc: can't access sysparam %s for writing\n",
399 rc = write(fd, &val, sizeof(val));
400 if (rc == sizeof(val)) {
405 if (rc <= 0 && errno != EINTR) {
406 pb_log("powerpc: error updating sysparam %s: %s",
407 name, strerror(errno));
416 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
421 static int clear_ipmi_bootdev_sysparams(
422 struct platform_powerpc *platform __attribute__((unused)),
426 /* invalidate default-boot-device setting */
427 write_bootdev_sysparam("default-boot-device", 0xff);
429 /* invalidate next-boot-device setting */
430 write_bootdev_sysparam("next-boot-device", 0xff);
435 static int get_ipmi_bootdev_sysparams(
436 struct platform_powerpc *platform __attribute__((unused)),
437 uint8_t *bootdev, bool *persistent)
439 uint8_t next_bootdev, default_bootdev;
440 bool next_valid, default_valid;
443 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
444 next_valid = rc == 0;
446 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
447 default_valid = rc == 0;
449 /* nothing valid? no need to change the config */
450 if (!next_valid && !default_valid)
453 *persistent = !next_valid;
454 *bootdev = next_valid ? next_bootdev : default_bootdev;
458 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
459 bool persistent __attribute__((unused)))
464 0x05, /* parameter selector: boot flags */
465 0x80, /* data 1: valid */
466 0x00, /* data 2: bootdev: no override */
467 0x00, /* data 3: system defaults */
468 0x00, /* data 4: no request for shared mode, mux defaults */
469 0x00, /* data 5: no instance request */
472 resp_len = sizeof(resp);
474 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
475 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
482 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
483 uint8_t *bootdev, bool *persistent)
489 0x05, /* parameter selector: boot flags */
490 0x00, /* no set selector */
491 0x00, /* no block selector */
494 resp_len = sizeof(resp);
495 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
496 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
501 pb_log("platform: error reading IPMI boot options\n");
505 if (resp_len != sizeof(resp)) {
506 pb_log("platform: unexpected length (%d) in "
507 "boot options response\n", resp_len);
511 pb_debug("IPMI get_bootdev response:\n");
512 for (int i = 0; i < resp_len; i++)
513 pb_debug("%x ", resp[i]);
517 pb_log("platform: non-zero completion code %d from IPMI req\n",
522 /* check for correct parameter version */
523 if ((resp[1] & 0xf) != 0x1) {
524 pb_log("platform: unexpected version (0x%x) in "
525 "boot options response\n", resp[0]);
529 /* check for valid paramters */
530 if (resp[2] & 0x80) {
531 pb_debug("platform: boot options are invalid/locked\n");
537 /* check for valid flags */
538 if (!(resp[3] & 0x80)) {
539 pb_debug("platform: boot flags are invalid, ignoring\n");
543 *persistent = resp[3] & 0x40;
544 *bootdev = (resp[4] >> 2) & 0x0f;
548 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
554 0x00, /* sensor number: os boot */
555 0xA9, /* operation: set everything */
556 0x00, /* sensor reading: none */
557 0x40, /* assertion mask lsb: set state 6 */
558 0x00, /* assertion mask msb: none */
559 0x00, /* deassertion mask lsb: none */
560 0x00, /* deassertion mask msb: none */
561 0x00, /* event data 1: none */
562 0x00, /* event data 2: none */
563 0x00, /* event data 3: none */
566 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
567 if (sensor_number < 0) {
568 pb_log("Couldn't find OS boot sensor in device tree\n");
572 req[0] = sensor_number;
574 resp_len = sizeof(resp);
576 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
580 ipmi_timeout); return 0;
585 static void get_ipmi_network_override(struct platform_powerpc *platform,
586 struct config *config)
588 uint16_t min_len = 12, resp_len = 53, version;
589 const uint32_t magic_value = 0x21706221;
590 uint8_t resp[resp_len];
595 0x61, /* parameter selector: OEM section (network) */
596 0x00, /* no set selector */
597 0x00, /* no block selector */
600 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
601 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
606 pb_debug("IPMI net override resp [%d][%d]:\n", rc, resp_len);
608 for (i = 0; i < resp_len; i++) {
609 pb_debug(" %02x", resp[i]);
610 if (i && (i + 1) % 16 == 0 && i != resp_len - 1)
612 else if (i && (i + 1) % 8 == 0)
619 pb_debug("IPMI network config option unavailable\n");
623 if (resp_len < min_len) {
624 pb_debug("IPMI net response too small\n");
629 pb_log("platform: non-zero completion code %d from IPMI network req\n",
634 /* Check for correct parameter version */
635 if ((resp[1] & 0xf) != 0x1) {
636 pb_log("platform: unexpected version (0x%x) in network override response\n",
641 /* Check that the parameters are valid */
642 if (resp[2] & 0x80) {
643 pb_debug("platform: network override is invalid/locked\n");
647 /* Check for valid parameters in the boot flags section */
648 if (!(resp[3] & 0x80)) {
649 pb_debug("platform: network override valid flag not set\n");
652 /* Read the persistent flag; if it is set we need to save this config */
653 persistent = resp[3] & 0x40;
655 pb_debug("platform: network override is persistent\n");
657 /* Check 4-byte cookie value */
659 memcpy(&cookie, &resp[i], sizeof(cookie));
660 cookie = __be32_to_cpu(cookie);
661 if (cookie != magic_value) {
662 pb_log_fn("Incorrect cookie %x\n", cookie);
667 /* Check 2-byte version number */
668 memcpy(&version, &resp[i], sizeof(version));
669 version = __be16_to_cpu(version);
670 i += sizeof(version);
672 pb_debug("Unexpected version: %u\n", version);
676 /* Interpret the rest of the interface config */
677 rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);
679 if (!rc && persistent) {
680 /* Write this new config to NVRAM */
681 update_network_config(&platform->params, "petitboot,network",
683 rc = write_nvram(platform);
685 pb_log("platform: Failed to save persistent interface override\n");
689 static void config_get_active_consoles(struct config *config)
692 char *fsp_prop = NULL;
694 config->n_consoles = 2;
695 config->consoles = talloc_array(config, char *, config->n_consoles);
696 if (!config->consoles)
699 config->consoles[0] = talloc_asprintf(config->consoles,
700 "/dev/hvc0 [IPMI / Serial]");
701 config->consoles[1] = talloc_asprintf(config->consoles,
704 fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
705 if (stat(fsp_prop, &sbuf) == 0) {
706 /* FSP based machines also have a separate serial console */
707 config->consoles = talloc_realloc(config, config->consoles,
708 char *, config->n_consoles + 1);
709 if (!config->consoles)
711 config->consoles[config->n_consoles++] = talloc_asprintf(
713 "/dev/hvc1 [Serial]");
718 config->n_consoles = 0;
719 pb_log("Failed to allocate memory for consoles\n");
722 static int load_config(struct platform *p, struct config *config)
724 struct platform_powerpc *platform = to_platform_powerpc(p);
727 rc = parse_nvram(platform);
729 pb_log_fn("Failed to parse nvram\n");
731 config_populate_all(config, &platform->params);
733 if (platform->get_ipmi_bootdev) {
734 bool bootdev_persistent;
735 uint8_t bootdev = IPMI_BOOTDEV_INVALID;
736 rc = platform->get_ipmi_bootdev(platform, &bootdev,
737 &bootdev_persistent);
738 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
739 config_set_ipmi_bootdev(config, bootdev,
745 get_ipmi_network_override(platform, config);
747 config_get_active_consoles(config);
752 static int save_config(struct platform *p, struct config *config)
754 struct platform_powerpc *platform = to_platform_powerpc(p);
755 struct config *defaults;
757 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
758 platform->clear_ipmi_bootdev) {
759 platform->clear_ipmi_bootdev(platform,
760 config->ipmi_bootdev_persistent);
761 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
762 config->ipmi_bootdev_persistent = false;
765 defaults = talloc_zero(platform, struct config);
766 config_set_defaults(defaults);
768 update_config(&platform->params, config, defaults);
770 talloc_free(defaults);
771 return write_nvram(platform);
774 static void pre_boot(struct platform *p, const struct config *config)
776 struct platform_powerpc *platform = to_platform_powerpc(p);
778 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
779 platform->clear_ipmi_bootdev(platform, false);
781 if (platform->set_os_boot_sensor)
782 platform->set_os_boot_sensor(platform);
785 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
787 struct platform_powerpc *platform = p->platform_data;
788 char *buf, *filename;
791 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
792 rc = read_file(platform, filename, &buf, &len);
794 sysinfo->type = talloc_steal(sysinfo, buf);
795 talloc_free(filename);
797 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
798 rc = read_file(platform, filename, &buf, &len);
800 sysinfo->identifier = talloc_steal(sysinfo, buf);
801 talloc_free(filename);
803 sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);
805 if (platform->ipmi) {
806 ipmi_get_bmc_mac(platform->ipmi, sysinfo->bmc_mac);
807 ipmi_get_bmc_versions(platform->ipmi, sysinfo);
810 if (platform->get_platform_versions)
811 platform->get_platform_versions(sysinfo);
816 static bool probe(struct platform *p, void *ctx)
818 struct platform_powerpc *platform;
823 /* we need a device tree */
824 rc = stat("/proc/device-tree", &statbuf);
828 if (!S_ISDIR(statbuf.st_mode))
831 platform = talloc_zero(ctx, struct platform_powerpc);
832 param_list_init(&platform->params, common_known_params());
834 p->platform_data = platform;
836 bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;
838 if (ipmi_present() && bmc_present) {
839 pb_debug("platform: using direct IPMI for IPMI paramters\n");
840 platform->ipmi = ipmi_open(platform);
841 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
842 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
843 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
844 } else if (!stat(sysparams_dir, &statbuf)) {
845 pb_debug("platform: using sysparams for IPMI paramters\n");
846 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
847 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
850 pb_log("platform: no IPMI parameter support\n");
854 platform->get_platform_versions = hostboot_load_versions;
860 static struct platform platform_powerpc = {
862 .dhcp_arch_id = 0x000e,
864 .load_config = load_config,
865 .save_config = save_config,
866 .pre_boot = pre_boot,
867 .get_sysinfo = get_sysinfo,
870 register_platform(platform_powerpc);