12 #include <file/file.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <process/process.h>
22 static const char *partition = "common";
23 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
24 static const char *devtree_dir = "/proc/device-tree/";
25 static const int ipmi_timeout = 500; /* milliseconds */
31 struct list_item list;
34 struct platform_powerpc {
37 bool ipmi_bootdev_persistent;
38 int (*get_ipmi_bootdev)(
39 struct platform_powerpc *platform,
40 uint8_t *bootdev, bool *persistent);
41 int (*clear_ipmi_bootdev)(
42 struct platform_powerpc *platform,
44 int (*set_os_boot_sensor)(
45 struct platform_powerpc *platform);
48 static const char *known_params[] = {
59 #define to_platform_powerpc(p) \
60 (struct platform_powerpc *)(p->platform_data)
62 /* a partition max a max size of 64k * 16bytes = 1M */
63 static const int max_partition_size = 64 * 1024 * 16;
65 static bool param_is_known(const char *param, unsigned int len)
67 const char *known_param;
70 for (i = 0; known_params[i]; i++) {
71 known_param = known_params[i];
72 if (len == strlen(known_param) &&
73 !strncmp(param, known_param, len))
80 static int parse_nvram_params(struct platform_powerpc *platform,
83 char *pos, *name, *value;
84 unsigned int paramlen;
87 /* discard 2 header lines:
94 for (i = 0; i < len; i++) {
102 fprintf(stderr, "failure parsing nvram output\n");
106 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
107 unsigned int namelen;
111 newline = strchr(pos, '\n');
117 paramlen = strlen(pos);
120 value = strchr(pos, '=');
124 namelen = value - name;
128 if (!param_is_known(name, namelen))
133 param = talloc(platform, struct param);
134 param->modified = false;
135 param->name = talloc_strndup(platform, name, namelen);
136 param->value = talloc_strdup(platform, value);
137 list_add(&platform->params, ¶m->list);
143 static int parse_nvram(struct platform_powerpc *platform)
145 struct process *process;
150 argv[1] = "--print-config";
151 argv[2] = "--partition";
155 process = process_create(platform);
156 process->path = "nvram";
157 process->argv = argv;
158 process->keep_stdout = true;
160 rc = process_run_sync(process);
162 if (rc || !process_exit_ok(process)) {
163 fprintf(stderr, "nvram process returned "
164 "non-zero exit status\n");
167 rc = parse_nvram_params(platform, process->stdout_buf,
168 process->stdout_len);
171 process_release(process);
175 static int write_nvram(struct platform_powerpc *platform)
177 struct process *process;
183 argv[1] = "--update-config";
185 argv[3] = "--partition";
189 process = process_create(platform);
190 process->path = "nvram";
191 process->argv = argv;
193 list_for_each_entry(&platform->params, param, list) {
196 if (!param->modified)
199 paramstr = talloc_asprintf(platform, "%s=%s",
200 param->name, param->value);
203 rc = process_run_sync(process);
205 talloc_free(paramstr);
207 if (rc || !process_exit_ok(process)) {
209 pb_log("nvram update process returned "
210 "non-zero exit status\n");
215 process_release(process);
219 static const char *get_param(struct platform_powerpc *platform,
224 list_for_each_entry(&platform->params, param, list)
225 if (!strcmp(param->name, name))
230 static void set_param(struct platform_powerpc *platform, const char *name,
235 list_for_each_entry(&platform->params, param, list) {
236 if (strcmp(param->name, name))
239 if (!strcmp(param->value, value))
242 talloc_free(param->value);
243 param->value = talloc_strdup(param, value);
244 param->modified = true;
249 param = talloc(platform, struct param);
250 param->modified = true;
251 param->name = talloc_strdup(platform, name);
252 param->value = talloc_strdup(platform, value);
253 list_add(&platform->params, ¶m->list);
256 static int parse_hwaddr(struct interface_config *ifconf, char *str)
260 if (strlen(str) != strlen("00:00:00:00:00:00"))
263 for (i = 0; i < HWADDR_SIZE; i++) {
267 byte[0] = str[i * 3 + 0];
268 byte[1] = str[i * 3 + 1];
271 x = strtoul(byte, &endp, 16);
272 if (endp != byte + 2)
275 ifconf->hwaddr[i] = x & 0xff;
281 static int parse_one_interface_config(struct config *config,
284 struct interface_config *ifconf;
287 ifconf = talloc_zero(config, struct interface_config);
289 if (!confstr || !strlen(confstr))
292 /* first token should be the mac address */
293 tok = strtok_r(confstr, ",", &saveptr);
297 if (parse_hwaddr(ifconf, tok))
300 /* second token is the method */
301 tok = strtok_r(NULL, ",", &saveptr);
302 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
303 ifconf->ignore = true;
305 } else if (!strcmp(tok, "dhcp")) {
306 ifconf->method = CONFIG_METHOD_DHCP;
308 } else if (!strcmp(tok, "static")) {
309 ifconf->method = CONFIG_METHOD_STATIC;
311 /* ip/mask, [optional] gateway */
312 tok = strtok_r(NULL, ",", &saveptr);
315 ifconf->static_config.address =
316 talloc_strdup(ifconf, tok);
318 tok = strtok_r(NULL, ",", &saveptr);
320 ifconf->static_config.gateway =
321 talloc_strdup(ifconf, tok);
325 pb_log("Unknown network configuration method %s\n", tok);
329 config->network.interfaces = talloc_realloc(config,
330 config->network.interfaces,
331 struct interface_config *,
332 ++config->network.n_interfaces);
334 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
342 static int parse_one_dns_config(struct config *config,
345 char *tok, *saveptr = NULL;
347 for (tok = strtok_r(confstr, ",", &saveptr); tok;
348 tok = strtok_r(NULL, ",", &saveptr)) {
350 char *server = talloc_strdup(config, tok);
352 config->network.dns_servers = talloc_realloc(config,
353 config->network.dns_servers, const char *,
354 ++config->network.n_dns_servers);
356 config->network.dns_servers[config->network.n_dns_servers - 1]
363 static void populate_network_config(struct platform_powerpc *platform,
364 struct config *config)
366 char *val, *saveptr = NULL;
370 cval = get_param(platform, "petitboot,network");
371 if (!cval || !strlen(cval))
374 val = talloc_strdup(config, cval);
379 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
383 if (!strncasecmp(tok, "dns,", strlen("dns,")))
384 parse_one_dns_config(config, tok + strlen("dns,"));
386 parse_one_interface_config(config, tok);
393 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
395 char *delim = strchr(*pos, ' ');
396 int len, prefix = 0, rc = -1;
397 enum device_type type;
399 if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
400 prefix = strlen("uuid:");
401 opt->boot_type = BOOT_DEVICE_UUID;
403 } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
404 prefix = strlen("mac:");
405 opt->boot_type = BOOT_DEVICE_UUID;
408 type = find_device_type(*pos);
409 if (type != DEVICE_TYPE_UNKNOWN) {
411 opt->boot_type = BOOT_DEVICE_TYPE;
416 if (opt->boot_type == BOOT_DEVICE_UUID) {
418 len = (int)(delim - *pos) - prefix;
422 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
425 /* Always advance pointer to next option or end */
429 *pos += strlen(*pos);
434 static void populate_bootdev_config(struct platform_powerpc *platform,
435 struct config *config)
437 struct autoboot_option *opt, *new = NULL;
438 char *pos, *end, *old_dev = NULL;
439 const char delim = ' ';
440 unsigned int n_new = 0;
444 /* Check for old-style bootdev */
445 val = get_param(platform, "petitboot,bootdev");
446 if (val && strlen(val)) {
447 pos = talloc_strdup(config, val);
448 if (!strncmp(val, "uuid:", strlen("uuid:")))
449 old_dev = talloc_strdup(config,
450 val + strlen("uuid:"));
451 else if (!strncmp(val, "mac:", strlen("mac:")))
452 old_dev = talloc_strdup(config,
453 val + strlen("mac:"));
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:...)");
472 if (strchr(pos, delim))
477 new = talloc_realloc(config, new, struct autoboot_option,
485 if (!n_new && !old_dev) {
486 /* If autoboot has been disabled, clear the default options */
487 if (!config->autoboot_enabled) {
488 talloc_free(config->autoboot_opts);
489 config->n_autoboot_opts = 0;
494 conflict = old_dev && (!n_new ||
495 new[0].boot_type == BOOT_DEVICE_TYPE ||
496 /* Canonical UUIDs are 36 characters long */
497 strncmp(new[0].uuid, old_dev, 36));
500 talloc_free(config->autoboot_opts);
501 config->autoboot_opts = new;
502 config->n_autoboot_opts = n_new;
507 * Difference detected, defer to old format in case it has been updated
510 pb_debug("Old autoboot bootdev detected\n");
511 talloc_free(config->autoboot_opts);
512 config->autoboot_opts = talloc(config, struct autoboot_option);
513 config->autoboot_opts[0].boot_type = BOOT_DEVICE_UUID;
514 config->autoboot_opts[0].uuid = talloc_strdup(config, old_dev);
515 config->n_autoboot_opts = 1;
518 static void populate_config(struct platform_powerpc *platform,
519 struct config *config)
523 unsigned long timeout;
525 /* if the "auto-boot?' property is present and "false", disable auto
527 val = get_param(platform, "auto-boot?");
528 config->autoboot_enabled = !val || strcmp(val, "false");
530 val = get_param(platform, "petitboot,timeout");
532 timeout = strtoul(val, &end, 10);
534 if (timeout >= INT_MAX)
536 config->autoboot_timeout_sec = (int)timeout;
540 val = get_param(platform, "petitboot,language");
541 config->lang = val ? talloc_strdup(config, val) : NULL;
543 populate_network_config(platform, config);
545 populate_bootdev_config(platform, config);
547 if (!config->debug) {
548 val = get_param(platform, "petitboot,debug?");
549 config->debug = val && !strcmp(val, "true");
553 static char *iface_config_str(void *ctx, struct interface_config *config)
557 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
558 * different hardware address formats */
559 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
560 config->hwaddr[0], config->hwaddr[1],
561 config->hwaddr[2], config->hwaddr[3],
562 config->hwaddr[4], config->hwaddr[5]);
564 if (config->ignore) {
565 str = talloc_asprintf_append(str, "ignore");
567 } else if (config->method == CONFIG_METHOD_DHCP) {
568 str = talloc_asprintf_append(str, "dhcp");
570 } else if (config->method == CONFIG_METHOD_STATIC) {
571 str = talloc_asprintf_append(str, "static,%s%s%s",
572 config->static_config.address,
573 config->static_config.gateway ? "," : "",
574 config->static_config.gateway ?: "");
579 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
584 str = talloc_strdup(ctx, "dns,");
585 for (i = 0; i < n; i++) {
586 str = talloc_asprintf_append(str, "%s%s",
594 static void update_string_config(struct platform_powerpc *platform,
595 const char *name, const char *value)
599 cur = get_param(platform, name);
601 /* don't set an empty parameter if it doesn't already exist */
602 if (!cur && !strlen(value))
605 set_param(platform, name, value);
608 static void update_network_config(struct platform_powerpc *platform,
609 struct config *config)
614 val = talloc_strdup(platform, "");
616 for (i = 0; i < config->network.n_interfaces; i++) {
617 char *iface_str = iface_config_str(platform,
618 config->network.interfaces[i]);
619 val = talloc_asprintf_append(val, "%s%s",
620 *val == '\0' ? "" : " ", iface_str);
621 talloc_free(iface_str);
624 if (config->network.n_dns_servers) {
625 char *dns_str = dns_config_str(platform,
626 config->network.dns_servers,
627 config->network.n_dns_servers);
628 val = talloc_asprintf_append(val, "%s%s",
629 *val == '\0' ? "" : " ", dns_str);
630 talloc_free(dns_str);
633 update_string_config(platform, "petitboot,network", val);
638 static void update_bootdev_config(struct platform_powerpc *platform,
639 struct config *config)
641 char *val = NULL, *boot_str = NULL, *tmp = NULL, *first = NULL;
642 struct autoboot_option *opt;
643 const char delim = ' ';
646 if (!config->n_autoboot_opts)
648 else if (config->autoboot_opts[0].boot_type == BOOT_DEVICE_UUID)
649 first = talloc_asprintf(config, "uuid:%s",
650 config->autoboot_opts[0].uuid);
654 for (i = 0; i < config->n_autoboot_opts; i++) {
655 opt = &config->autoboot_opts[i];
656 switch (opt->boot_type) {
657 case BOOT_DEVICE_TYPE:
658 boot_str = talloc_asprintf(config, "%s%c",
659 device_type_name(opt->type),
662 case BOOT_DEVICE_UUID:
663 boot_str = talloc_asprintf(config, "uuid:%s%c",
667 tmp = val = talloc_asprintf_append(val, "%s", boot_str);
670 update_string_config(platform, "petitboot,bootdevs", val);
671 update_string_config(platform, "petitboot,bootdev", first);
674 talloc_free(boot_str);
677 static int update_config(struct platform_powerpc *platform,
678 struct config *config, struct config *defaults)
683 if (config->autoboot_enabled == defaults->autoboot_enabled)
686 val = config->autoboot_enabled ? "true" : "false";
687 update_string_config(platform, "auto-boot?", val);
689 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
692 val = tmp = talloc_asprintf(platform, "%d",
693 config->autoboot_timeout_sec);
695 if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
696 platform->clear_ipmi_bootdev) {
697 platform->clear_ipmi_bootdev(platform,
698 config->ipmi_bootdev_persistent);
699 config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
700 config->ipmi_bootdev_persistent = false;
703 update_string_config(platform, "petitboot,timeout", val);
707 val = config->lang ?: "";
708 update_string_config(platform, "petitboot,language", val);
710 update_network_config(platform, config);
712 update_bootdev_config(platform, config);
714 return write_nvram(platform);
717 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
720 config->ipmi_bootdev = bootdev;
721 config->ipmi_bootdev_persistent = persistent;
724 case IPMI_BOOTDEV_NONE:
725 case IPMI_BOOTDEV_DISK:
726 case IPMI_BOOTDEV_NETWORK:
727 case IPMI_BOOTDEV_CDROM:
730 case IPMI_BOOTDEV_SETUP:
731 config->autoboot_enabled = false;
733 case IPMI_BOOTDEV_SAFE:
734 config->autoboot_enabled = false;
735 config->safe_mode = true;
740 static int read_bootdev_sysparam(const char *name, uint8_t *val)
746 strcpy(path, sysparams_dir);
747 assert(strlen(name) < sizeof(path) - strlen(path));
750 fd = open(path, O_RDONLY);
752 pb_debug("powerpc: can't access sysparam %s\n",
757 rc = read(fd, buf, sizeof(buf));
761 /* bootdev definitions should only be one byte in size */
763 pb_debug("powerpc: sysparam %s read returned %d\n",
768 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
770 if (!ipmi_bootdev_is_valid(buf[0]))
777 static int write_bootdev_sysparam(const char *name, uint8_t val)
782 strcpy(path, sysparams_dir);
783 assert(strlen(name) < sizeof(path) - strlen(path));
786 fd = open(path, O_WRONLY);
788 pb_debug("powerpc: can't access sysparam %s for writing\n",
795 rc = write(fd, &val, sizeof(val));
796 if (rc == sizeof(val)) {
801 if (rc <= 0 && errno != EINTR) {
802 pb_log("powerpc: error updating sysparam %s: %s",
803 name, strerror(errno));
812 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
817 static int clear_ipmi_bootdev_sysparams(
818 struct platform_powerpc *platform __attribute__((unused)),
822 /* invalidate default-boot-device setting */
823 write_bootdev_sysparam("default-boot-device", 0xff);
825 /* invalidate next-boot-device setting */
826 write_bootdev_sysparam("next-boot-device", 0xff);
831 static int get_ipmi_bootdev_sysparams(
832 struct platform_powerpc *platform __attribute__((unused)),
833 uint8_t *bootdev, bool *persistent)
835 uint8_t next_bootdev, default_bootdev;
836 bool next_valid, default_valid;
839 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
840 next_valid = rc == 0;
842 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
843 default_valid = rc == 0;
845 /* nothing valid? no need to change the config */
846 if (!next_valid && !default_valid)
849 *persistent = !next_valid;
850 *bootdev = next_valid ? next_bootdev : default_bootdev;
854 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
855 bool persistent __attribute__((unused)))
860 0x05, /* parameter selector: boot flags */
861 0x80, /* data 1: valid */
862 0x00, /* data 2: bootdev: no override */
863 0x00, /* data 3: system defaults */
864 0x00, /* data 4: no request for shared mode, mux defaults */
865 0x00, /* data 5: no instance request */
868 resp_len = sizeof(resp);
870 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
871 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
878 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
879 uint8_t *bootdev, bool *persistent)
885 0x05, /* parameter selector: boot flags */
886 0x00, /* no set selector */
887 0x00, /* no block selector */
890 resp_len = sizeof(resp);
891 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
892 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
897 pb_log("platform: error reading IPMI boot options\n");
901 if (resp_len != sizeof(resp)) {
902 pb_log("platform: unexpected length (%d) in "
903 "boot options response\n", resp_len);
908 pb_log("platform: non-zero completion code %d from IPMI req\n",
913 /* check for correct parameter version */
914 if ((resp[1] & 0xf) != 0x1) {
915 pb_log("platform: unexpected version (0x%x) in "
916 "boot options response\n", resp[0]);
920 /* check for valid paramters */
921 if (resp[2] & 0x80) {
922 pb_debug("platform: boot options are invalid/locked\n");
928 /* check for valid flags */
929 if (!(resp[3] & 0x80)) {
930 pb_debug("platform: boot flags are invalid, ignoring\n");
934 *persistent = resp[3] & 0x40;
935 *bootdev = (resp[4] >> 2) & 0x0f;
939 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
945 0x00, /* sensor number: os boot */
946 0xA9, /* operation: set everything */
947 0x00, /* sensor reading: none */
948 0x40, /* assertion mask lsb: set state 6 */
949 0x00, /* assertion mask msb: none */
950 0x00, /* deassertion mask lsb: none */
951 0x00, /* deassertion mask msb: none */
952 0x00, /* event data 1: none */
953 0x00, /* event data 2: none */
954 0x00, /* event data 3: none */
957 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
958 if (sensor_number < 0) {
959 pb_log("Couldn't find OS boot sensor in device tree\n");
963 req[0] = sensor_number;
965 resp_len = sizeof(resp);
967 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
971 ipmi_timeout); return 0;
976 static int load_config(struct platform *p, struct config *config)
978 struct platform_powerpc *platform = to_platform_powerpc(p);
981 rc = parse_nvram(platform);
985 populate_config(platform, config);
987 if (platform->get_ipmi_bootdev) {
988 bool bootdev_persistent;
990 rc = platform->get_ipmi_bootdev(platform, &bootdev,
991 &bootdev_persistent);
992 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
993 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
1000 static int save_config(struct platform *p, struct config *config)
1002 struct platform_powerpc *platform = to_platform_powerpc(p);
1003 struct config *defaults;
1006 defaults = talloc_zero(platform, struct config);
1007 config_set_defaults(defaults);
1009 rc = update_config(platform, config, defaults);
1011 talloc_free(defaults);
1015 static void pre_boot(struct platform *p, const struct config *config)
1017 struct platform_powerpc *platform = to_platform_powerpc(p);
1019 if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
1020 platform->clear_ipmi_bootdev(platform, false);
1022 if (platform->set_os_boot_sensor)
1023 platform->set_os_boot_sensor(platform);
1026 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
1028 struct platform_powerpc *platform = p->platform_data;
1029 char *buf, *filename;
1032 filename = talloc_asprintf(platform, "%smodel", devtree_dir);
1033 rc = read_file(platform, filename, &buf, &len);
1035 sysinfo->type = talloc_steal(sysinfo, buf);
1036 talloc_free(filename);
1038 filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
1039 rc = read_file(platform, filename, &buf, &len);
1041 sysinfo->identifier = talloc_steal(sysinfo, buf);
1042 talloc_free(filename);
1047 static bool probe(struct platform *p, void *ctx)
1049 struct platform_powerpc *platform;
1050 struct stat statbuf;
1053 /* we need a device tree */
1054 rc = stat("/proc/device-tree", &statbuf);
1058 if (!S_ISDIR(statbuf.st_mode))
1061 platform = talloc_zero(ctx, struct platform_powerpc);
1062 list_init(&platform->params);
1064 p->platform_data = platform;
1066 if (ipmi_present()) {
1067 pb_debug("platform: using direct IPMI for IPMI paramters\n");
1068 platform->ipmi = ipmi_open(platform);
1069 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
1070 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
1071 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
1073 } else if (!stat(sysparams_dir, &statbuf)) {
1074 pb_debug("platform: using sysparams for IPMI paramters\n");
1075 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
1076 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
1079 pb_log("platform: no IPMI parameter support\n");
1086 static struct platform platform_powerpc = {
1088 .dhcp_arch_id = 0x000e,
1090 .load_config = load_config,
1091 .save_config = save_config,
1092 .pre_boot = pre_boot,
1093 .get_sysinfo = get_sysinfo,
1096 register_platform(platform_powerpc);