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);
43 int (*set_os_boot_sensor)(
44 struct platform_powerpc *platform);
47 static const char *known_params[] = {
57 #define to_platform_powerpc(p) \
58 (struct platform_powerpc *)(p->platform_data)
60 /* a partition max a max size of 64k * 16bytes = 1M */
61 static const int max_partition_size = 64 * 1024 * 16;
63 static bool param_is_known(const char *param, unsigned int len)
65 const char *known_param;
68 for (i = 0; known_params[i]; i++) {
69 known_param = known_params[i];
70 if (len == strlen(known_param) &&
71 !strncmp(param, known_param, len))
78 static int parse_nvram_params(struct platform_powerpc *platform,
81 char *pos, *name, *value;
82 unsigned int paramlen;
85 /* discard 2 header lines:
92 for (i = 0; i < len; i++) {
100 fprintf(stderr, "failure parsing nvram output\n");
104 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
105 unsigned int namelen;
109 newline = strchr(pos, '\n');
115 paramlen = strlen(pos);
118 value = strchr(pos, '=');
122 namelen = value - name;
126 if (!param_is_known(name, namelen))
131 param = talloc(platform, struct param);
132 param->modified = false;
133 param->name = talloc_strndup(platform, name, namelen);
134 param->value = talloc_strdup(platform, value);
135 list_add(&platform->params, ¶m->list);
141 static int parse_nvram(struct platform_powerpc *platform)
143 struct process *process;
148 argv[1] = "--print-config";
149 argv[2] = "--partition";
153 process = process_create(platform);
154 process->path = "nvram";
155 process->argv = argv;
156 process->keep_stdout = true;
158 rc = process_run_sync(process);
160 if (rc || !process_exit_ok(process)) {
161 fprintf(stderr, "nvram process returned "
162 "non-zero exit status\n");
165 rc = parse_nvram_params(platform, process->stdout_buf,
166 process->stdout_len);
169 process_release(process);
173 static int write_nvram(struct platform_powerpc *platform)
175 struct process *process;
181 argv[1] = "--update-config";
183 argv[3] = "--partition";
187 process = process_create(platform);
188 process->path = "nvram";
189 process->argv = argv;
191 list_for_each_entry(&platform->params, param, list) {
194 if (!param->modified)
197 paramstr = talloc_asprintf(platform, "%s=%s",
198 param->name, param->value);
201 rc = process_run_sync(process);
203 talloc_free(paramstr);
205 if (rc || !process_exit_ok(process)) {
207 pb_log("nvram update process returned "
208 "non-zero exit status\n");
213 process_release(process);
217 static const char *get_param(struct platform_powerpc *platform,
222 list_for_each_entry(&platform->params, param, list)
223 if (!strcmp(param->name, name))
228 static void set_param(struct platform_powerpc *platform, const char *name,
233 list_for_each_entry(&platform->params, param, list) {
234 if (strcmp(param->name, name))
237 if (!strcmp(param->value, value))
240 talloc_free(param->value);
241 param->value = talloc_strdup(param, value);
242 param->modified = true;
247 param = talloc(platform, struct param);
248 param->modified = true;
249 param->name = talloc_strdup(platform, name);
250 param->value = talloc_strdup(platform, value);
251 list_add(&platform->params, ¶m->list);
254 static int parse_hwaddr(struct interface_config *ifconf, char *str)
258 if (strlen(str) != strlen("00:00:00:00:00:00"))
261 for (i = 0; i < HWADDR_SIZE; i++) {
265 byte[0] = str[i * 3 + 0];
266 byte[1] = str[i * 3 + 1];
269 x = strtoul(byte, &endp, 16);
270 if (endp != byte + 2)
273 ifconf->hwaddr[i] = x & 0xff;
279 static int parse_one_interface_config(struct config *config,
282 struct interface_config *ifconf;
285 ifconf = talloc_zero(config, struct interface_config);
287 if (!confstr || !strlen(confstr))
290 /* first token should be the mac address */
291 tok = strtok_r(confstr, ",", &saveptr);
295 if (parse_hwaddr(ifconf, tok))
298 /* second token is the method */
299 tok = strtok_r(NULL, ",", &saveptr);
300 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
301 ifconf->ignore = true;
303 } else if (!strcmp(tok, "dhcp")) {
304 ifconf->method = CONFIG_METHOD_DHCP;
306 } else if (!strcmp(tok, "static")) {
307 ifconf->method = CONFIG_METHOD_STATIC;
309 /* ip/mask, [optional] gateway */
310 tok = strtok_r(NULL, ",", &saveptr);
313 ifconf->static_config.address =
314 talloc_strdup(ifconf, tok);
316 tok = strtok_r(NULL, ",", &saveptr);
318 ifconf->static_config.gateway =
319 talloc_strdup(ifconf, tok);
323 pb_log("Unknown network configuration method %s\n", tok);
327 config->network.interfaces = talloc_realloc(config,
328 config->network.interfaces,
329 struct interface_config *,
330 ++config->network.n_interfaces);
332 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
340 static int parse_one_dns_config(struct config *config,
343 char *tok, *saveptr = NULL;
345 for (tok = strtok_r(confstr, ",", &saveptr); tok;
346 tok = strtok_r(NULL, ",", &saveptr)) {
348 char *server = talloc_strdup(config, tok);
350 config->network.dns_servers = talloc_realloc(config,
351 config->network.dns_servers, const char *,
352 ++config->network.n_dns_servers);
354 config->network.dns_servers[config->network.n_dns_servers - 1]
361 static void populate_network_config(struct platform_powerpc *platform,
362 struct config *config)
364 char *val, *saveptr = NULL;
368 cval = get_param(platform, "petitboot,network");
369 if (!cval || !strlen(cval))
372 val = talloc_strdup(config, cval);
377 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
381 if (!strncasecmp(tok, "dns,", strlen("dns,")))
382 parse_one_dns_config(config, tok + strlen("dns,"));
384 parse_one_interface_config(config, tok);
391 static int read_bootdev(void *ctx, char **pos, struct autoboot_option *opt)
393 char *delim = strchr(*pos, ' ');
394 int len, prefix = 0, rc = -1;
395 enum device_type type;
397 if (!strncmp(*pos, "uuid:", strlen("uuid:"))) {
398 prefix = strlen("uuid:");
399 opt->boot_type = BOOT_DEVICE_UUID;
401 } else if (!strncmp(*pos, "mac:", strlen("mac:"))) {
402 prefix = strlen("mac:");
403 opt->boot_type = BOOT_DEVICE_UUID;
406 type = find_device_type(*pos);
407 if (type != DEVICE_TYPE_UNKNOWN) {
409 opt->boot_type = BOOT_DEVICE_TYPE;
414 if (opt->boot_type == BOOT_DEVICE_UUID) {
416 len = (int)(delim - *pos) - prefix;
420 opt->uuid = talloc_strndup(ctx, *pos + prefix, len);
423 /* Always advance pointer to next option or end */
427 *pos += strlen(*pos);
432 static void populate_bootdev_config(struct platform_powerpc *platform,
433 struct config *config)
438 config->boot_device = NULL;
440 val = get_param(platform, "petitboot,bootdev");
441 if (!val || !strlen(val))
444 if (!strncmp(val, "uuid:", strlen("uuid:"))) {
445 config->boot_device = talloc_strdup(config,
446 val + strlen("uuid:"));
448 } else if (!strncmp(val, "mac:", strlen("mac:"))) {
449 config->boot_device = talloc_strdup(config,
450 val + strlen("mac:"));
453 pb_log("bootdev config is in an unknown format "
454 "(expected uuid:... or mac:...)");
458 static void populate_config(struct platform_powerpc *platform,
459 struct config *config)
463 unsigned long timeout;
465 /* if the "auto-boot?' property is present and "false", disable auto
467 val = get_param(platform, "auto-boot?");
468 config->autoboot_enabled = !val || strcmp(val, "false");
470 val = get_param(platform, "petitboot,timeout");
472 timeout = strtoul(val, &end, 10);
474 if (timeout >= INT_MAX)
476 config->autoboot_timeout_sec = (int)timeout;
480 val = get_param(platform, "petitboot,language");
481 config->lang = val ? talloc_strdup(config, val) : NULL;
483 populate_network_config(platform, config);
485 populate_bootdev_config(platform, config);
487 if (!config->debug) {
488 val = get_param(platform, "petitboot,debug?");
489 config->debug = val && !strcmp(val, "true");
493 static char *iface_config_str(void *ctx, struct interface_config *config)
497 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
498 * different hardware address formats */
499 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
500 config->hwaddr[0], config->hwaddr[1],
501 config->hwaddr[2], config->hwaddr[3],
502 config->hwaddr[4], config->hwaddr[5]);
504 if (config->ignore) {
505 str = talloc_asprintf_append(str, "ignore");
507 } else if (config->method == CONFIG_METHOD_DHCP) {
508 str = talloc_asprintf_append(str, "dhcp");
510 } else if (config->method == CONFIG_METHOD_STATIC) {
511 str = talloc_asprintf_append(str, "static,%s%s%s",
512 config->static_config.address,
513 config->static_config.gateway ? "," : "",
514 config->static_config.gateway ?: "");
519 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
524 str = talloc_strdup(ctx, "dns,");
525 for (i = 0; i < n; i++) {
526 str = talloc_asprintf_append(str, "%s%s",
534 static void update_string_config(struct platform_powerpc *platform,
535 const char *name, const char *value)
539 cur = get_param(platform, name);
541 /* don't set an empty parameter if it doesn't already exist */
542 if (!cur && !strlen(value))
545 set_param(platform, name, value);
548 static void update_network_config(struct platform_powerpc *platform,
549 struct config *config)
554 val = talloc_strdup(platform, "");
556 for (i = 0; i < config->network.n_interfaces; i++) {
557 char *iface_str = iface_config_str(platform,
558 config->network.interfaces[i]);
559 val = talloc_asprintf_append(val, "%s%s",
560 *val == '\0' ? "" : " ", iface_str);
561 talloc_free(iface_str);
564 if (config->network.n_dns_servers) {
565 char *dns_str = dns_config_str(platform,
566 config->network.dns_servers,
567 config->network.n_dns_servers);
568 val = talloc_asprintf_append(val, "%s%s",
569 *val == '\0' ? "" : " ", dns_str);
570 talloc_free(dns_str);
573 update_string_config(platform, "petitboot,network", val);
578 static void update_bootdev_config(struct platform_powerpc *platform,
579 struct config *config)
581 char *val, *tmp = NULL;
583 if (!config->boot_device)
586 tmp = val = talloc_asprintf(platform,
587 "uuid:%s", config->boot_device);
589 update_string_config(platform, "petitboot,bootdev", val);
593 static int update_config(struct platform_powerpc *platform,
594 struct config *config, struct config *defaults)
599 if (config->autoboot_enabled == defaults->autoboot_enabled)
602 val = config->autoboot_enabled ? "true" : "false";
603 update_string_config(platform, "auto-boot?", val);
605 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
608 val = tmp = talloc_asprintf(platform, "%d",
609 config->autoboot_timeout_sec);
611 update_string_config(platform, "petitboot,timeout", val);
615 val = config->lang ?: "";
616 update_string_config(platform, "petitboot,language", val);
618 update_network_config(platform, config);
620 update_bootdev_config(platform, config);
622 return write_nvram(platform);
625 static void set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
628 config->ipmi_bootdev = bootdev;
629 config->ipmi_bootdev_persistent = persistent;
632 case IPMI_BOOTDEV_NONE:
633 case IPMI_BOOTDEV_DISK:
634 case IPMI_BOOTDEV_NETWORK:
635 case IPMI_BOOTDEV_CDROM:
637 case IPMI_BOOTDEV_SETUP:
638 config->autoboot_enabled = false;
640 case IPMI_BOOTDEV_SAFE:
641 config->autoboot_enabled = false;
642 config->safe_mode = true;
647 static int read_bootdev_sysparam(const char *name, uint8_t *val)
653 strcpy(path, sysparams_dir);
654 assert(strlen(name) < sizeof(path) - strlen(path));
657 fd = open(path, O_RDONLY);
659 pb_debug("powerpc: can't access sysparam %s\n",
664 rc = read(fd, buf, sizeof(buf));
668 /* bootdev definitions should only be one byte in size */
670 pb_debug("powerpc: sysparam %s read returned %d\n",
675 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
677 if (!ipmi_bootdev_is_valid(buf[0]))
684 static int write_bootdev_sysparam(const char *name, uint8_t val)
689 strcpy(path, sysparams_dir);
690 assert(strlen(name) < sizeof(path) - strlen(path));
693 fd = open(path, O_WRONLY);
695 pb_debug("powerpc: can't access sysparam %s for writing\n",
702 rc = write(fd, &val, sizeof(val));
703 if (rc == sizeof(val)) {
708 if (rc <= 0 && errno != EINTR) {
709 pb_log("powerpc: error updating sysparam %s: %s",
710 name, strerror(errno));
719 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
724 static int clear_ipmi_bootdev_sysparams(
725 struct platform_powerpc *platform __attribute__((unused)))
727 /* invalidate next-boot-device setting */
728 write_bootdev_sysparam("next-boot-device", 0xff);
732 static int get_ipmi_bootdev_sysparams(
733 struct platform_powerpc *platform __attribute__((unused)),
734 uint8_t *bootdev, bool *persistent)
736 uint8_t next_bootdev, default_bootdev;
737 bool next_valid, default_valid;
740 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
741 next_valid = rc == 0;
743 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
744 default_valid = rc == 0;
746 /* nothing valid? no need to change the config */
747 if (!next_valid && !default_valid)
750 *persistent = !next_valid;
751 *bootdev = next_valid ? next_bootdev : default_bootdev;
755 static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform)
760 0x05, /* parameter selector: boot flags */
761 0x80, /* data 1: valid */
762 0x00, /* data 2: bootdev: no override */
763 0x00, /* data 3: system defaults */
764 0x00, /* data 4: no request for shared mode, mux defaults */
765 0x00, /* data 5: no instance request */
768 resp_len = sizeof(resp);
770 ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
771 IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
778 static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
779 uint8_t *bootdev, bool *persistent)
785 0x05, /* parameter selector: boot flags */
786 0x00, /* no set selector */
787 0x00, /* no block selector */
790 resp_len = sizeof(resp);
791 rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
792 IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
797 pb_log("platform: error reading IPMI boot options\n");
801 if (resp_len != sizeof(resp)) {
802 pb_log("platform: unexpected length (%d) in "
803 "boot options response\n", resp_len);
808 pb_log("platform: non-zero completion code %d from IPMI req\n",
813 /* check for correct parameter version */
814 if ((resp[1] & 0xf) != 0x1) {
815 pb_log("platform: unexpected version (0x%x) in "
816 "boot options response\n", resp[0]);
820 /* check for valid paramters */
821 if (resp[2] & 0x80) {
822 pb_debug("platform: boot options are invalid/locked\n");
828 /* check for valid flags */
829 if (!(resp[3] & 0x80)) {
830 pb_debug("platform: boot flags are invalid, ignoring\n");
834 *persistent = resp[3] & 0x40;
835 *bootdev = (resp[4] >> 2) & 0x0f;
839 static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
845 0x00, /* sensor number: os boot */
846 0xA9, /* operation: set everything */
847 0x00, /* sensor reading: none */
848 0x40, /* assertion mask lsb: set state 6 */
849 0x00, /* assertion mask msb: none */
850 0x00, /* deassertion mask lsb: none */
851 0x00, /* deassertion mask msb: none */
852 0x00, /* event data 1: none */
853 0x00, /* event data 2: none */
854 0x00, /* event data 3: none */
857 sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
858 if (sensor_number < 0) {
859 pb_log("Couldn't find OS boot sensor in device tree\n");
863 req[0] = sensor_number;
865 resp_len = sizeof(resp);
867 ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
871 ipmi_timeout); return 0;
876 static int load_config(struct platform *p, struct config *config)
878 struct platform_powerpc *platform = to_platform_powerpc(p);
881 rc = parse_nvram(platform);
885 populate_config(platform, config);
887 if (platform->get_ipmi_bootdev) {
888 bool bootdev_persistent;
890 rc = platform->get_ipmi_bootdev(platform, &bootdev,
891 &bootdev_persistent);
892 if (!rc && ipmi_bootdev_is_valid(bootdev)) {
893 set_ipmi_bootdev(config, bootdev, bootdev_persistent);
900 static int save_config(struct platform *p, struct config *config)
902 struct platform_powerpc *platform = to_platform_powerpc(p);
903 struct config *defaults;
906 defaults = talloc_zero(platform, struct config);
907 config_set_defaults(defaults);
909 rc = update_config(platform, config, defaults);
911 talloc_free(defaults);
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);
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);
947 static bool probe(struct platform *p, void *ctx)
949 struct platform_powerpc *platform;
953 /* we need a device tree */
954 rc = stat("/proc/device-tree", &statbuf);
958 if (!S_ISDIR(statbuf.st_mode))
961 platform = talloc_zero(ctx, struct platform_powerpc);
962 list_init(&platform->params);
964 p->platform_data = platform;
966 if (ipmi_present()) {
967 pb_debug("platform: using direct IPMI for IPMI paramters\n");
968 platform->ipmi = ipmi_open(platform);
969 platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
970 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
971 platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
973 } else if (!stat(sysparams_dir, &statbuf)) {
974 pb_debug("platform: using sysparams for IPMI paramters\n");
975 platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
976 platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;
979 pb_log("platform: no IPMI parameter support\n");
986 static struct platform platform_powerpc = {
988 .dhcp_arch_id = 0x000e,
990 .load_config = load_config,
991 .save_config = save_config,
992 .pre_boot = pre_boot,
993 .get_sysinfo = get_sysinfo,
996 register_platform(platform_powerpc);