12 #include <file/file.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <process/process.h>
20 static const char *partition = "common";
21 static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
27 struct list_item list;
30 struct platform_powerpc {
34 static const char *known_params[] = {
42 #define to_platform_powerpc(p) \
43 (struct platform_powerpc *)(p->platform_data)
45 /* a partition max a max size of 64k * 16bytes = 1M */
46 static const int max_partition_size = 64 * 1024 * 16;
48 static bool param_is_known(const char *param, unsigned int len)
50 const char *known_param;
53 for (i = 0; known_params[i]; i++) {
54 known_param = known_params[i];
55 if (len == strlen(known_param) &&
56 !strncmp(param, known_param, len))
63 static int parse_nvram_params(struct platform_powerpc *platform,
66 char *pos, *name, *value;
67 unsigned int paramlen;
70 /* discard 2 header lines:
77 for (i = 0; i < len; i++) {
85 fprintf(stderr, "failure parsing nvram output\n");
89 for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
94 newline = strchr(pos, '\n');
100 paramlen = strlen(pos);
103 value = strchr(pos, '=');
107 namelen = value - name;
111 if (!param_is_known(name, namelen))
116 param = talloc(platform, struct param);
117 param->modified = false;
118 param->name = talloc_strndup(platform, name, namelen);
119 param->value = talloc_strdup(platform, value);
120 list_add(&platform->params, ¶m->list);
126 static int parse_nvram(struct platform_powerpc *platform)
128 struct process *process;
133 argv[1] = "--print-config";
134 argv[2] = "--partition";
138 process = process_create(platform);
139 process->path = "nvram";
140 process->argv = argv;
141 process->keep_stdout = true;
143 rc = process_run_sync(process);
145 if (rc || !process_exit_ok(process)) {
146 fprintf(stderr, "nvram process returned "
147 "non-zero exit status\n");
150 rc = parse_nvram_params(platform, process->stdout_buf,
151 process->stdout_len);
154 process_release(process);
158 static int write_nvram(struct platform_powerpc *platform)
160 struct process *process;
166 argv[1] = "--update-config";
168 argv[3] = "--partition";
172 process = process_create(platform);
173 process->path = "nvram";
174 process->argv = argv;
176 list_for_each_entry(&platform->params, param, list) {
179 if (!param->modified)
182 paramstr = talloc_asprintf(platform, "%s=%s",
183 param->name, param->value);
186 rc = process_run_sync(process);
188 talloc_free(paramstr);
190 if (rc || !process_exit_ok(process)) {
192 pb_log("nvram update process returned "
193 "non-zero exit status\n");
198 process_release(process);
202 static const char *get_param(struct platform_powerpc *platform,
207 list_for_each_entry(&platform->params, param, list)
208 if (!strcmp(param->name, name))
213 static void set_param(struct platform_powerpc *platform, const char *name,
218 list_for_each_entry(&platform->params, param, list) {
219 if (strcmp(param->name, name))
222 if (!strcmp(param->value, value))
225 talloc_free(param->value);
226 param->value = talloc_strdup(param, value);
227 param->modified = true;
232 param = talloc(platform, struct param);
233 param->modified = true;
234 param->name = talloc_strdup(platform, name);
235 param->value = talloc_strdup(platform, value);
236 list_add(&platform->params, ¶m->list);
239 static int parse_hwaddr(struct interface_config *ifconf, char *str)
243 if (strlen(str) != strlen("00:00:00:00:00:00"))
246 for (i = 0; i < HWADDR_SIZE; i++) {
250 byte[0] = str[i * 3 + 0];
251 byte[1] = str[i * 3 + 1];
254 x = strtoul(byte, &endp, 16);
255 if (endp != byte + 2)
258 ifconf->hwaddr[i] = x & 0xff;
264 static int parse_one_interface_config(struct config *config,
267 struct interface_config *ifconf;
270 ifconf = talloc_zero(config, struct interface_config);
272 if (!confstr || !strlen(confstr))
275 /* first token should be the mac address */
276 tok = strtok_r(confstr, ",", &saveptr);
280 if (parse_hwaddr(ifconf, tok))
283 /* second token is the method */
284 tok = strtok_r(NULL, ",", &saveptr);
285 if (!tok || !strlen(tok) || !strcmp(tok, "ignore")) {
286 ifconf->ignore = true;
288 } else if (!strcmp(tok, "dhcp")) {
289 ifconf->method = CONFIG_METHOD_DHCP;
291 } else if (!strcmp(tok, "static")) {
292 ifconf->method = CONFIG_METHOD_STATIC;
294 /* ip/mask, [optional] gateway */
295 tok = strtok_r(NULL, ",", &saveptr);
298 ifconf->static_config.address =
299 talloc_strdup(ifconf, tok);
301 tok = strtok_r(NULL, ",", &saveptr);
303 ifconf->static_config.gateway =
304 talloc_strdup(ifconf, tok);
308 pb_log("Unknown network configuration method %s\n", tok);
312 config->network.interfaces = talloc_realloc(config,
313 config->network.interfaces,
314 struct interface_config *,
315 ++config->network.n_interfaces);
317 config->network.interfaces[config->network.n_interfaces - 1] = ifconf;
325 static int parse_one_dns_config(struct config *config,
330 for (tok = strtok_r(confstr, ",", &saveptr); tok;
331 tok = strtok_r(NULL, ",", &saveptr)) {
333 char *server = talloc_strdup(config, tok);
335 config->network.dns_servers = talloc_realloc(config,
336 config->network.dns_servers, const char *,
337 ++config->network.n_dns_servers);
339 config->network.dns_servers[config->network.n_dns_servers - 1]
346 static void populate_network_config(struct platform_powerpc *platform,
347 struct config *config)
349 char *val, *saveptr = NULL;
353 cval = get_param(platform, "petitboot,network");
354 if (!cval || !strlen(cval))
357 val = talloc_strdup(config, cval);
362 tok = strtok_r(i == 0 ? val : NULL, " ", &saveptr);
366 if (!strncasecmp(tok, "dns,", strlen("dns,")))
367 parse_one_dns_config(config, tok + strlen("dns,"));
369 parse_one_interface_config(config, tok);
376 static void populate_bootdev_config(struct platform_powerpc *platform,
377 struct config *config)
382 config->boot_device = NULL;
384 val = get_param(platform, "petitboot,bootdev");
385 if (!val || !strlen(val))
388 if (!strncmp(val, "uuid:", strlen("uuid:"))) {
389 config->boot_device = talloc_strdup(config,
390 val + strlen("uuid:"));
392 } else if (!strncmp(val, "mac:", strlen("mac:"))) {
393 config->boot_device = talloc_strdup(config,
394 val + strlen("mac:"));
397 pb_log("bootdev config is in an unknown format "
398 "(expected uuid:... or mac:...)");
402 static void populate_config(struct platform_powerpc *platform,
403 struct config *config)
407 unsigned long timeout;
409 /* if the "auto-boot?' property is present and "false", disable auto
411 val = get_param(platform, "auto-boot?");
412 config->autoboot_enabled = !val || strcmp(val, "false");
414 val = get_param(platform, "petitboot,timeout");
416 timeout = strtoul(val, &end, 10);
418 if (timeout >= INT_MAX)
420 config->autoboot_timeout_sec = (int)timeout;
424 populate_network_config(platform, config);
426 populate_bootdev_config(platform, config);
429 static char *iface_config_str(void *ctx, struct interface_config *config)
433 /* todo: HWADDR size is hardcoded as 6, but we may need to handle
434 * different hardware address formats */
435 str = talloc_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x,",
436 config->hwaddr[0], config->hwaddr[1],
437 config->hwaddr[2], config->hwaddr[3],
438 config->hwaddr[4], config->hwaddr[5]);
440 if (config->ignore) {
441 str = talloc_asprintf_append(str, "ignore");
443 } else if (config->method == CONFIG_METHOD_DHCP) {
444 str = talloc_asprintf_append(str, "dhcp");
446 } else if (config->method == CONFIG_METHOD_STATIC) {
447 str = talloc_asprintf_append(str, "static,%s%s%s",
448 config->static_config.address,
449 config->static_config.gateway ? "," : "",
450 config->static_config.gateway ?: "");
455 static char *dns_config_str(void *ctx, const char **dns_servers, int n)
460 str = talloc_strdup(ctx, "dns,");
461 for (i = 0; i < n; i++) {
462 str = talloc_asprintf_append(str, "%s%s",
470 static void update_string_config(struct platform_powerpc *platform,
471 const char *name, const char *value)
475 cur = get_param(platform, name);
477 /* don't set an empty parameter if it doesn't already exist */
478 if (!cur && !strlen(value))
481 set_param(platform, name, value);
484 static void update_network_config(struct platform_powerpc *platform,
485 struct config *config)
490 val = talloc_strdup(platform, "");
492 for (i = 0; i < config->network.n_interfaces; i++) {
493 char *iface_str = iface_config_str(platform,
494 config->network.interfaces[i]);
495 val = talloc_asprintf_append(val, "%s%s",
496 *val == '\0' ? "" : " ", iface_str);
497 talloc_free(iface_str);
500 if (config->network.n_dns_servers) {
501 char *dns_str = dns_config_str(platform,
502 config->network.dns_servers,
503 config->network.n_dns_servers);
504 val = talloc_asprintf_append(val, "%s%s",
505 *val == '\0' ? "" : " ", dns_str);
506 talloc_free(dns_str);
509 update_string_config(platform, "petitboot,network", val);
514 static void update_bootdev_config(struct platform_powerpc *platform,
515 struct config *config)
517 char *val, *tmp = NULL;
519 if (!config->boot_device)
522 tmp = val = talloc_asprintf(platform,
523 "uuid:%s", config->boot_device);
525 update_string_config(platform, "petitboot,bootdev", val);
529 static int update_config(struct platform_powerpc *platform,
530 struct config *config, struct config *defaults)
535 if (config->autoboot_enabled == defaults->autoboot_enabled)
538 val = config->autoboot_enabled ? "true" : "false";
539 update_string_config(platform, "auto-boot?", val);
541 if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
544 val = tmp = talloc_asprintf(platform, "%d",
545 config->autoboot_timeout_sec);
547 update_string_config(platform, "petitboot,timeout", val);
551 update_network_config(platform, config);
553 update_bootdev_config(platform, config);
555 return write_nvram(platform);
558 static void set_exclusive_devtype(struct config *config,
559 enum device_type devtype)
561 config->n_boot_priorities = 2;
562 config->boot_priorities = talloc_realloc(config,
563 config->boot_priorities, struct boot_priority,
564 config->n_boot_priorities);
565 config->boot_priorities[0].type = devtype;
566 config->boot_priorities[0].priority = 0;
567 config->boot_priorities[1].type = DEVICE_TYPE_ANY;
568 config->boot_priorities[1].priority = -1;
571 /* bootdev options that we recognise */
573 IPMI_BOOTDEV_NONE = 0x00,
574 IPMI_BOOTDEV_NETWORK = 0x01,
575 IPMI_BOOTDEV_DISK = 0x2,
576 IPMI_BOOTDEV_SAFE = 0x3,
577 IPMI_BOOTDEV_CDROM = 0x5,
578 IPMI_BOOTDEV_SETUP = 0x6,
581 static int read_bootdev_sysparam(const char *name, uint8_t *val)
587 strcpy(path, sysparams_dir);
588 assert(strlen(name) < sizeof(path) - strlen(path));
591 fd = open(path, O_RDONLY);
593 pb_debug("powerpc: can't access sysparam %s\n",
598 rc = read(fd, buf, sizeof(buf));
602 /* bootdev definitions should only be one byte in size */
604 pb_debug("powerpc: sysparam %s read returned %d\n",
609 pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);
614 case IPMI_BOOTDEV_NONE:
615 case IPMI_BOOTDEV_NETWORK:
616 case IPMI_BOOTDEV_DISK:
617 case IPMI_BOOTDEV_SAFE:
618 case IPMI_BOOTDEV_CDROM:
619 case IPMI_BOOTDEV_SETUP:
626 static int write_bootdev_sysparam(const char *name, uint8_t val)
631 strcpy(path, sysparams_dir);
632 assert(strlen(name) < sizeof(path) - strlen(path));
635 fd = open(path, O_WRONLY);
637 pb_debug("powerpc: can't access sysparam %s for writing\n",
644 rc = write(fd, &val, sizeof(val));
645 if (rc == sizeof(val)) {
650 if (rc <= 0 && errno != EINTR) {
651 pb_log("powerpc: error updating sysparam %s: %s",
652 name, strerror(errno));
661 pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);
666 static void parse_opal_sysparams(struct config *config)
668 uint8_t next_bootdev, default_bootdev;
669 bool next_valid, default_valid;
672 rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
673 next_valid = rc == 0;
675 rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
676 default_valid = rc == 0;
678 /* nothing valid? no need to change the config */
679 if (!next_valid && !default_valid)
683 /* invalidate next-boot-device setting */
684 write_bootdev_sysparam("next-boot-device", 0xff);
686 next_bootdev = default_bootdev;
689 switch (next_bootdev) {
690 case IPMI_BOOTDEV_NONE:
692 case IPMI_BOOTDEV_DISK:
693 set_exclusive_devtype(config, DEVICE_TYPE_DISK);
695 case IPMI_BOOTDEV_NETWORK:
696 set_exclusive_devtype(config, DEVICE_TYPE_NETWORK);
698 case IPMI_BOOTDEV_CDROM:
699 set_exclusive_devtype(config, DEVICE_TYPE_OPTICAL);
701 case IPMI_BOOTDEV_SETUP:
702 config->autoboot_enabled = false;
704 case IPMI_BOOTDEV_SAFE:
705 config->autoboot_enabled = false;
706 config->safe_mode = true;
711 static int load_config(struct platform *p, struct config *config)
713 struct platform_powerpc *platform = to_platform_powerpc(p);
716 rc = parse_nvram(platform);
720 populate_config(platform, config);
722 parse_opal_sysparams(config);
727 static int save_config(struct platform *p, struct config *config)
729 struct platform_powerpc *platform = to_platform_powerpc(p);
730 struct config *defaults;
733 defaults = talloc_zero(platform, struct config);
734 config_set_defaults(defaults);
736 rc = update_config(platform, config, defaults);
738 talloc_free(defaults);
742 static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
744 struct platform_powerpc *platform = p->platform_data;
748 rc = read_file(platform, "/proc/device_tree/model", &buf, &len);
750 sysinfo->type = talloc_steal(sysinfo, buf);
752 rc = read_file(platform, "/proc/device_tree/system-id", &buf, &len);
754 sysinfo->identifier = talloc_steal(sysinfo, buf);
759 static bool probe(struct platform *p, void *ctx)
761 struct platform_powerpc *platform;
765 /* we need a device tree */
766 rc = stat("/proc/device-tree", &statbuf);
770 if (!S_ISDIR(statbuf.st_mode))
773 platform = talloc(ctx, struct platform_powerpc);
774 list_init(&platform->params);
776 p->platform_data = platform;
781 static struct platform platform_powerpc = {
783 .dhcp_arch_id = 0x000e,
785 .load_config = load_config,
786 .save_config = save_config,
787 .get_sysinfo = get_sysinfo,
790 register_platform(platform_powerpc);