2 * Copyright (C) 2013 IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #if defined(HAVE_CONFIG_H)
26 #include <pb-config/pb-config.h>
27 #include <talloc/talloc.h>
28 #include <types/types.h>
30 #include <i18n/i18n.h>
33 #include "nc-config.h"
34 #include "nc-widgets.h"
38 extern struct help_text config_help_text;
41 NET_CONF_TYPE_DHCP_ALL,
42 NET_CONF_TYPE_DHCP_ONE,
46 struct config_screen {
49 struct nc_widgetset *widgetset;
58 void (*on_exit)(struct cui *);
66 enum net_conf_type net_conf_type;
68 bool autoboot_enabled;
73 struct nc_widget_label *autoboot_l;
74 struct nc_widget_select *autoboot_f;
75 struct nc_widget_label *boot_order_l;
76 struct nc_widget_subset *boot_order_f;
77 struct nc_widget_label *boot_empty_l;
78 struct nc_widget_button *boot_add_b;
79 struct nc_widget_button *boot_none_b;
80 struct nc_widget_button *boot_any_b;
81 struct nc_widget_textbox *timeout_f;
82 struct nc_widget_label *timeout_l;
83 struct nc_widget_label *timeout_help_l;
85 struct nc_widget_label *ipmi_type_l;
86 struct nc_widget_label *ipmi_clear_l;
87 struct nc_widget_checkbox *ipmi_clear_cb;
89 struct nc_widget_label *network_l;
90 struct nc_widget_select *network_f;
92 struct nc_widget_label *iface_l;
93 struct nc_widget_select *iface_f;
94 struct nc_widget_label *ip_addr_l;
95 struct nc_widget_textbox *ip_addr_f;
96 struct nc_widget_label *ip_mask_l;
97 struct nc_widget_textbox *ip_mask_f;
98 struct nc_widget_label *ip_addr_mask_help_l;
99 struct nc_widget_label *gateway_l;
100 struct nc_widget_textbox *gateway_f;
101 struct nc_widget_label *gateway_help_l;
102 struct nc_widget_label *url_l;
103 struct nc_widget_textbox *url_f;
104 struct nc_widget_label *url_help_l;
105 struct nc_widget_label *dns_l;
106 struct nc_widget_textbox *dns_f;
107 struct nc_widget_label *dns_dhcp_help_l;
108 struct nc_widget_label *dns_help_l;
109 struct nc_widget_label *http_proxy_l;
110 struct nc_widget_textbox *http_proxy_f;
111 struct nc_widget_label *https_proxy_l;
112 struct nc_widget_textbox *https_proxy_f;
114 struct nc_widget_label *allow_write_l;
115 struct nc_widget_select *allow_write_f;
116 struct nc_widget_label *boot_console_l;
117 struct nc_widget_select *boot_console_f;
118 struct nc_widget_label *manual_console_l;
119 struct nc_widget_label *current_console_l;
121 struct nc_widget_label *net_override_l;
122 struct nc_widget_label *safe_mode;
123 struct nc_widget_button *ok_b;
124 struct nc_widget_button *help_b;
125 struct nc_widget_button *cancel_b;
129 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
131 struct config_screen *config_screen;
133 assert(scr->sig == pb_config_screen_sig);
134 config_screen = (struct config_screen *)
135 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
136 assert(config_screen->scr.sig == pb_config_screen_sig);
137 return config_screen;
140 static void pad_refresh(struct config_screen *screen)
142 int y, x, rows, cols;
144 getmaxyx(screen->scr.sub_ncw, rows, cols);
145 getbegyx(screen->scr.sub_ncw, y, x);
147 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
150 static void config_screen_process_key(struct nc_scr *scr, int key)
152 struct config_screen *screen = config_screen_from_scr(scr);
155 handled = widgetset_process_key(screen->widgetset, key);
164 screen->show_help = true;
170 screen->on_exit(screen->cui);
172 } else if (screen->show_help) {
173 screen->show_help = false;
174 screen->need_redraw = true;
175 cui_show_help(screen->cui, _("System Configuration"),
178 } else if (handled && !screen->show_subset) {
183 static void config_screen_resize(struct nc_scr *scr)
185 struct config_screen *screen = config_screen_from_scr(scr);
189 static int config_screen_unpost(struct nc_scr *scr)
191 struct config_screen *screen = config_screen_from_scr(scr);
192 widgetset_unpost(screen->widgetset);
196 struct nc_scr *config_screen_scr(struct config_screen *screen)
201 static int screen_process_form(struct config_screen *screen)
203 const struct system_info *sysinfo = screen->cui->sysinfo;
204 enum net_conf_type net_conf_type;
205 struct interface_config *iface;
206 bool allow_write, autoboot;
208 struct config *config;
209 int i, n_boot_opts, rc;
210 unsigned int *order, idx;
213 config = config_copy(screen, screen->cui->config);
215 talloc_free(config->autoboot_opts);
216 config->n_autoboot_opts = 0;
218 n_boot_opts = widget_subset_get_order(config, &order,
219 screen->widgets.boot_order_f);
221 autoboot = widget_select_get_value(screen->widgets.autoboot_f);
222 config->autoboot_enabled = autoboot && n_boot_opts;
224 config->n_autoboot_opts = n_boot_opts;
225 config->autoboot_opts = talloc_array(config, struct autoboot_option,
228 for (i = 0; i < n_boot_opts; i++) {
229 if (order[i] < sysinfo->n_blockdevs) {
231 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
232 config->autoboot_opts[i].uuid = talloc_strdup(config,
233 sysinfo->blockdevs[order[i]]->uuid);
234 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
236 order[i] -= sysinfo->n_blockdevs;
237 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
238 mac_str(sysinfo->interfaces[order[i]]->hwaddr,
239 sysinfo->interfaces[order[i]]->hwaddr_size,
241 config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
244 order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
245 config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
246 config->autoboot_opts[i].type = order[i];
250 str = widget_textbox_get_value(screen->widgets.timeout_f);
254 x = strtoul(str, &end, 10);
255 if (!errno && end != str)
256 config->autoboot_timeout_sec = x;
259 if (screen->ipmi_override)
260 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
261 config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
264 net_conf_type = widget_select_get_value(screen->widgets.network_f);
266 /* if we don't have any network interfaces, prevent per-interface
268 if (sysinfo->n_interfaces == 0)
269 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
271 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
272 config->network.n_interfaces = 0;
275 iface = talloc_zero(config, struct interface_config);
276 config->network.n_interfaces = 1;
277 config->network.interfaces = talloc_array(config,
278 struct interface_config *, 1);
279 config->network.interfaces[0] = iface;
281 /* copy hwaddr (from the sysinfo interface data) to
282 * the configuration */
283 idx = widget_select_get_value(screen->widgets.iface_f);
284 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
285 sizeof(iface->hwaddr));
288 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
289 iface->method = CONFIG_METHOD_DHCP;
292 if (net_conf_type == NET_CONF_TYPE_STATIC) {
293 char *ip, *mask, *gateway, *url;
295 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
296 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
297 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
298 url = widget_textbox_get_value(screen->widgets.url_f);
300 if (!ip || !*ip || !mask || !*mask) {
301 screen->scr.frame.status =
302 _("No IP / mask values are set");
303 nc_scr_frame_draw(&screen->scr);
308 iface->method = CONFIG_METHOD_STATIC;
309 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
311 iface->static_config.gateway = talloc_strdup(iface, gateway);
312 iface->static_config.url = talloc_strdup(iface, url);
315 str = widget_textbox_get_value(screen->widgets.dns_f);
316 talloc_free(config->network.dns_servers);
317 config->network.dns_servers = NULL;
318 config->network.n_dns_servers = 0;
320 if (str && strlen(str)) {
325 dns = strtok_r(str, " \t", &tmp);
330 i = config->network.n_dns_servers++;
331 config->network.dns_servers = talloc_realloc(config,
332 config->network.dns_servers,
334 config->network.n_dns_servers);
335 config->network.dns_servers[i] =
336 talloc_strdup(config, dns);
342 talloc_free(config->http_proxy);
343 talloc_free(config->https_proxy);
344 str = widget_textbox_get_value(screen->widgets.http_proxy_f);
345 config->http_proxy = talloc_strdup(config, str);
346 str = widget_textbox_get_value(screen->widgets.https_proxy_f);
347 config->https_proxy = talloc_strdup(config, str);
349 allow_write = widget_select_get_value(screen->widgets.allow_write_f);
350 if (allow_write != config->allow_writes)
351 config->allow_writes = allow_write;
353 if (config->n_consoles && !config->manual_console) {
354 idx = widget_select_get_value(screen->widgets.boot_console_f);
355 if (!config->boot_console) {
356 config->boot_console = talloc_strdup(config,
357 config->consoles[idx]);
358 } else if (strncmp(config->boot_console, config->consoles[idx],
359 strlen(config->boot_console)) != 0) {
360 talloc_free(config->boot_console);
361 config->boot_console = talloc_strdup(config,
362 config->consoles[idx]);
366 config->safe_mode = false;
367 rc = cui_send_config(screen->cui, config);
371 pb_log("cui_send_config failed!\n");
373 pb_debug("config sent!\n");
378 static void ok_click(void *arg)
380 struct config_screen *screen = arg;
381 if (screen_process_form(screen))
382 /* errors are written to the status line, so we'll need
384 wrefresh(screen->scr.main_ncw);
389 static void help_click(void *arg)
391 struct config_screen *screen = arg;
392 screen->show_help = true;
395 static void cancel_click(void *arg)
397 struct config_screen *screen = arg;
401 static int layout_pair(struct config_screen *screen, int y,
402 struct nc_widget_label *label,
403 struct nc_widget *field)
405 struct nc_widget *label_w = widget_label_base(label);
406 widget_move(label_w, y, screen->label_x);
407 widget_move(field, y, screen->field_x);
408 return max(widget_height(label_w), widget_height(field));
411 static void config_screen_layout_widgets(struct config_screen *screen)
413 struct nc_widget *wl, *wf, *wh;
418 /* currently, the longest label we have is the DNS-servers
419 * widget, so layout our screen based on that */
420 help_x = screen->field_x + 2 +
421 widget_width(widget_textbox_base(screen->widgets.dns_f));
423 wl = widget_label_base(screen->widgets.autoboot_l);
424 widget_set_visible(wl, true);
425 widget_move(wl, y, screen->label_x);
427 wf = widget_select_base(screen->widgets.autoboot_f);
428 widget_set_visible(wf, true);
429 widget_move(wf, y, screen->field_x);
430 y += widget_height(wf);
432 show = screen->autoboot_enabled;
437 wl = widget_label_base(screen->widgets.boot_order_l);
438 widget_set_visible(wl, show);
439 widget_move(wl, y, screen->label_x);
441 wf = widget_subset_base(screen->widgets.boot_order_f);
442 widget_move(wf, y, screen->field_x);
443 wl = widget_label_base(screen->widgets.boot_empty_l);
444 widget_move(wl, y, screen->field_x);
446 if (widget_subset_height(screen->widgets.boot_order_f)) {
447 widget_set_visible(wl, false);
448 widget_set_visible(wf, show);
449 y += show ? widget_height(wf) : 0;
451 widget_set_visible(wl, show);
452 widget_set_visible(wf, false);
458 widget_move(widget_button_base(screen->widgets.boot_add_b),
459 y++, screen->field_x);
460 widget_move(widget_button_base(screen->widgets.boot_any_b),
461 y++, screen->field_x);
462 widget_move(widget_button_base(screen->widgets.boot_none_b),
466 wf = widget_button_base(screen->widgets.boot_add_b);
467 if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
468 widget_set_visible(wf, true);
470 widget_set_visible(wf, false);
475 widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
476 widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
478 wf = widget_textbox_base(screen->widgets.timeout_f);
479 wl = widget_label_base(screen->widgets.timeout_l);
480 wh = widget_label_base(screen->widgets.timeout_help_l);
481 widget_set_visible(wl, screen->autoboot_enabled);
482 widget_set_visible(wf, screen->autoboot_enabled);
483 widget_set_visible(wh, screen->autoboot_enabled);
484 if (screen->autoboot_enabled) {
485 widget_set_visible(wh, screen->autoboot_enabled);
486 widget_move(wl, y, screen->label_x);
487 widget_move(wf, y, screen->field_x);
488 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
493 if (screen->ipmi_override) {
494 wl = widget_label_base(screen->widgets.ipmi_type_l);
495 widget_set_visible(wl, true);
496 widget_move(wl, y, screen->label_x);
499 wl = widget_label_base(screen->widgets.ipmi_clear_l);
500 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
501 widget_set_visible(wl, true);
502 widget_set_visible(wf, true);
503 widget_move(wl, y, screen->label_x);
504 widget_move(wf, y, screen->field_x);
510 y += layout_pair(screen, y, screen->widgets.network_l,
511 widget_select_base(screen->widgets.network_f));
515 /* conditionally show iface select */
516 wl = widget_label_base(screen->widgets.iface_l);
517 wf = widget_select_base(screen->widgets.iface_f);
519 show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
520 screen->net_conf_type == NET_CONF_TYPE_STATIC;
522 widget_set_visible(wl, show);
523 widget_set_visible(wf, show);
526 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
528 /* conditionally show static IP params */
529 show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
531 wl = widget_label_base(screen->widgets.ip_addr_l);
532 wf = widget_textbox_base(screen->widgets.ip_addr_f);
533 widget_set_visible(wl, show);
534 widget_set_visible(wf, show);
535 x = screen->field_x + widget_width(wf) + 1;
538 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
540 wl = widget_label_base(screen->widgets.ip_mask_l);
541 wf = widget_textbox_base(screen->widgets.ip_mask_f);
542 widget_set_visible(wl, show);
543 widget_set_visible(wf, show);
546 widget_move(wl, y, x);
547 widget_move(wf, y, x + 2);
550 /* help for IP/mask */
551 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
552 widget_set_visible(wh, show);
554 widget_move(wh, y, help_x);
558 wl = widget_label_base(screen->widgets.gateway_l);
559 wf = widget_textbox_base(screen->widgets.gateway_f);
560 wh = widget_label_base(screen->widgets.gateway_help_l);
561 widget_set_visible(wl, show);
562 widget_set_visible(wf, show);
563 widget_set_visible(wh, show);
566 layout_pair(screen, y, screen->widgets.gateway_l, wf);
567 widget_move(wh, y, help_x);
571 wl = widget_label_base(screen->widgets.url_l);
572 wf = widget_textbox_base(screen->widgets.url_f);
573 wh = widget_label_base(screen->widgets.url_help_l);
574 widget_set_visible(wl, show);
575 widget_set_visible(wf, show);
576 widget_set_visible(wh, show);
579 layout_pair(screen, y, screen->widgets.url_l, wf);
580 widget_move(wh, y, help_x);
584 wh = widget_label_base(screen->widgets.dns_help_l);
585 layout_pair(screen, y, screen->widgets.dns_l,
586 widget_textbox_base(screen->widgets.dns_f));
587 widget_move(wh, y, help_x);
590 /* we show the DNS/DHCP help if we're configuring DHCP */
591 show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
592 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
593 widget_set_visible(wl, show);
595 widget_move(wl, y, screen->field_x);
599 wf = widget_textbox_base(screen->widgets.http_proxy_f);
600 layout_pair(screen, y, screen->widgets.http_proxy_l, wf);
602 wf = widget_textbox_base(screen->widgets.https_proxy_f);
603 layout_pair(screen, y, screen->widgets.https_proxy_l, wf);
608 layout_pair(screen, y, screen->widgets.allow_write_l,
609 widget_select_base(screen->widgets.allow_write_f));
610 y += widget_height(widget_select_base(screen->widgets.allow_write_f));
614 if (screen->widgets.manual_console_l) {
615 layout_pair(screen, y++, screen->widgets.boot_console_l,
616 widget_label_base(screen->widgets.manual_console_l));
617 widget_move(widget_label_base(screen->widgets.current_console_l),
619 widget_set_visible(widget_select_base(
620 screen->widgets.boot_console_f), false);
622 } else if (widget_height(widget_select_base(screen->widgets.boot_console_f))) {
623 layout_pair(screen, y, screen->widgets.boot_console_l,
624 widget_select_base(screen->widgets.boot_console_f));
625 y += widget_height(widget_select_base(screen->widgets.boot_console_f));
626 widget_move(widget_label_base(screen->widgets.current_console_l),
630 widget_set_visible(widget_label_base(
631 screen->widgets.boot_console_l), false);
632 widget_set_visible(widget_select_base(
633 screen->widgets.boot_console_f), false);
634 widget_set_visible(widget_label_base(
635 screen->widgets.current_console_l), false);
638 if (screen->net_override) {
639 widget_move(widget_label_base(screen->widgets.net_override_l),
641 widget_set_visible(widget_label_base(screen->widgets.net_override_l),
646 if (screen->cui->config->safe_mode) {
647 widget_move(widget_label_base(screen->widgets.safe_mode),
649 widget_set_visible(widget_label_base(screen->widgets.safe_mode),
654 widget_move(widget_button_base(screen->widgets.ok_b),
656 widget_move(widget_button_base(screen->widgets.help_b),
657 y, screen->field_x + 14);
658 widget_move(widget_button_base(screen->widgets.cancel_b),
659 y, screen->field_x + 28);
662 static void config_screen_network_change(void *arg, int value)
664 struct config_screen *screen = arg;
665 screen->net_conf_type = value;
666 widgetset_unpost(screen->widgetset);
667 config_screen_layout_widgets(screen);
668 widgetset_post(screen->widgetset);
671 static void config_screen_boot_order_change(void *arg, int value)
674 struct config_screen *screen = arg;
675 widgetset_unpost(screen->widgetset);
676 config_screen_layout_widgets(screen);
677 widgetset_post(screen->widgetset);
680 static void config_screen_autoboot_change(void *arg, int value)
682 struct config_screen *screen = arg;
683 screen->autoboot_enabled = !!value;
684 widgetset_unpost(screen->widgetset);
685 config_screen_layout_widgets(screen);
686 widgetset_post(screen->widgetset);
689 static void config_screen_add_device(void *arg)
691 struct config_screen *screen = arg;
693 screen->show_subset = true;
694 cui_show_subset(screen->cui, _("Select a boot device to add"),
695 screen->widgets.boot_order_f);
698 static void config_screen_autoboot_none(void *arg)
700 struct config_screen *screen = arg;
701 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
703 widget_subset_clear_active(subset);
705 widgetset_unpost(screen->widgetset);
706 config_screen_layout_widgets(screen);
707 widgetset_post(screen->widgetset);
710 static void config_screen_autoboot_any(void *arg)
712 struct config_screen *screen = arg;
713 const struct system_info *sysinfo = screen->cui->sysinfo;
714 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
717 widget_subset_clear_active(subset);
719 idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
721 widget_subset_make_active(screen->widgets.boot_order_f, idx);
723 screen->autoboot_enabled = true;
725 widgetset_unpost(screen->widgetset);
726 config_screen_layout_widgets(screen);
727 widgetset_post(screen->widgetset);
730 static void config_screen_update_subset(void *arg,
731 struct nc_widget_subset *subset, int idx)
733 struct config_screen *screen = arg;
736 widget_subset_make_active(subset, idx);
737 if (!screen->autoboot_enabled)
738 screen->autoboot_enabled = true;
739 config_screen_layout_widgets(screen);
742 static struct interface_config *first_active_interface(
743 const struct config *config)
747 for (i = 0; i < config->network.n_interfaces; i++) {
748 if (config->network.interfaces[i]->ignore)
750 return config->network.interfaces[i];
755 static enum net_conf_type find_net_conf_type(const struct config *config)
757 struct interface_config *ifcfg;
759 ifcfg = first_active_interface(config);
762 return NET_CONF_TYPE_DHCP_ALL;
764 else if (ifcfg->method == CONFIG_METHOD_DHCP)
765 return NET_CONF_TYPE_DHCP_ONE;
767 else if (ifcfg->method == CONFIG_METHOD_STATIC)
768 return NET_CONF_TYPE_STATIC;
771 return NET_CONF_TYPE_DHCP_ALL;
774 static void config_screen_setup_empty(struct config_screen *screen)
776 widget_new_label(screen->widgetset, 2, screen->field_x,
777 _("Waiting for configuration data..."));
778 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
779 4, screen->field_x, 9, _("Cancel"),
780 cancel_click, screen);
783 static int find_autoboot_idx(const struct system_info *sysinfo,
784 struct autoboot_option *opt)
788 if (opt->boot_type == BOOT_DEVICE_TYPE)
789 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
791 for (i = 0; i < sysinfo->n_blockdevs; i++) {
792 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
796 for (i = 0; i < sysinfo->n_interfaces; i++) {
797 struct interface_info *info = sysinfo->interfaces[i];
800 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
802 if (!strcmp(mac, opt->uuid))
803 return sysinfo->n_blockdevs + i;
809 static void config_screen_setup_widgets(struct config_screen *screen,
810 const struct config *config,
811 const struct system_info *sysinfo)
813 struct nc_widgetset *set = screen->widgetset;
814 struct interface_config *ifcfg;
815 char *str, *ip, *mask, *gw, *url, *tty, *label;
816 enum net_conf_type type;
818 int add_len, clear_len, any_len, min_len = 20;
821 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
824 type = screen->net_conf_type;
825 ifcfg = first_active_interface(config);
827 screen->autoboot_enabled = config->autoboot_enabled;
829 screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
831 screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
832 COLS - screen->field_x - 1);
834 widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
835 !screen->autoboot_enabled);
836 widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
837 screen->autoboot_enabled);
839 widget_select_on_change(screen->widgets.autoboot_f,
840 config_screen_autoboot_change, screen);
842 add_len = max(min_len, strncols(_("Add Device")));
843 clear_len = max(min_len, strncols(_("Clear")));
844 any_len = max(min_len, strncols(_("Clear & Boot Any")));
846 screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
848 config_screen_add_device, screen);
850 screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
852 config_screen_autoboot_none, screen);
854 screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
855 _("Clear & Boot Any"),
856 config_screen_autoboot_any, screen);
858 screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
860 screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
861 COLS - screen->field_x,
862 config_screen_update_subset);
863 screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
866 widget_subset_on_change(screen->widgets.boot_order_f,
867 config_screen_boot_order_change, screen);
869 for (i = 0; i < sysinfo->n_blockdevs; i++) {
870 struct blockdev_info *bd = sysinfo->blockdevs[i];
872 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
875 widget_subset_add_option(screen->widgets.boot_order_f, label);
878 for (i = 0; i < sysinfo->n_interfaces; i++) {
879 struct interface_info *info = sysinfo->interfaces[i];
882 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
884 label = talloc_asprintf(screen, _("net: %s [mac: %s]"),
887 widget_subset_add_option(screen->widgets.boot_order_f, label);
890 for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
892 if (i == DEVICE_TYPE_ANY)
893 label = talloc_asprintf(screen, _("Any Device"));
895 label = talloc_asprintf(screen, _("Any %s device"),
896 device_type_display_name(i));
898 widget_subset_add_option(screen->widgets.boot_order_f, label);
901 for (i = 0; i < config->n_autoboot_opts; i++) {
902 struct autoboot_option *opt = &config->autoboot_opts[i];
905 idx = find_autoboot_idx(sysinfo, opt);
908 widget_subset_make_active(screen->widgets.boot_order_f,
911 if (opt->boot_type == BOOT_DEVICE_TYPE)
912 pb_log("%s: Unknown autoboot option: %d\n",
913 __func__, opt->type);
915 pb_log("%s: Unknown autoboot UUID: %s\n",
916 __func__, opt->uuid);
921 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
922 screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
923 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
924 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
927 widget_textbox_set_fixed_size(screen->widgets.timeout_f);
928 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
930 if (config->ipmi_bootdev) {
931 label = talloc_asprintf(screen,
932 _("%s IPMI boot option: %s"),
933 config->ipmi_bootdev_persistent ?
934 "Persistent" : "Temporary",
935 ipmi_bootdev_display_name(config->ipmi_bootdev));
936 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
938 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
940 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
942 screen->ipmi_override = true;
945 screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
946 screen->widgets.network_f = widget_new_select(set, 0, 0,
947 COLS - screen->field_x - 1);
949 widget_select_add_option(screen->widgets.network_f,
950 NET_CONF_TYPE_DHCP_ALL,
951 _("DHCP on all active interfaces"),
952 type == NET_CONF_TYPE_DHCP_ALL);
953 widget_select_add_option(screen->widgets.network_f,
954 NET_CONF_TYPE_DHCP_ONE,
955 _("DHCP on a specific interface"),
956 type == NET_CONF_TYPE_DHCP_ONE);
957 widget_select_add_option(screen->widgets.network_f,
958 NET_CONF_TYPE_STATIC,
959 _("Static IP configuration"),
960 type == NET_CONF_TYPE_STATIC);
962 widget_select_on_change(screen->widgets.network_f,
963 config_screen_network_change, screen);
965 screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
966 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
968 for (i = 0; i < sysinfo->n_interfaces; i++) {
969 struct interface_info *info = sysinfo->interfaces[i];
970 char str[50], mac[20];
973 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
974 sizeof(ifcfg->hwaddr));
976 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
977 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
978 info->link ? _("link up") : _("link down"));
980 widget_select_add_option(screen->widgets.iface_f,
984 url = gw = ip = mask = NULL;
985 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
988 str = talloc_strdup(screen, ifcfg->static_config.address);
989 sep = strchr(str, '/');
996 gw = ifcfg->static_config.gateway;
997 url = ifcfg->static_config.url;
1000 screen->net_override = ifcfg && ifcfg->override;
1001 if (screen->net_override) {
1002 screen->widgets.net_override_l = widget_new_label(set, 0, 0,
1003 _("Network Override Active! 'OK' will overwrite interface config"));
1006 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
1007 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
1008 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
1009 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
1010 screen->widgets.ip_addr_mask_help_l =
1011 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
1013 widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
1014 widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
1015 widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
1016 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
1018 screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
1019 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
1020 screen->widgets.gateway_help_l =
1021 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
1023 widget_textbox_set_fixed_size(screen->widgets.gateway_f);
1024 widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
1026 screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
1027 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
1028 screen->widgets.url_help_l =
1029 widget_new_label(set, 0, 0, _("(eg. tftp://)"));
1031 str = talloc_strdup(screen, "");
1032 for (i = 0; i < config->network.n_dns_servers; i++) {
1033 str = talloc_asprintf_append(str, "%s%s",
1034 (i == 0) ? "" : " ",
1035 config->network.dns_servers[i]);
1038 screen->widgets.dns_l = widget_new_label(set, 0, 0,
1039 _("DNS Server(s):"));
1040 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
1041 screen->widgets.dns_help_l =
1042 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
1044 widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
1046 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
1047 _("(if not provided by DHCP server)"));
1049 screen->widgets.http_proxy_l = widget_new_label(set, 0, 0,
1051 screen->widgets.http_proxy_f = widget_new_textbox(set, 0, 0, 32,
1052 config->http_proxy);
1053 screen->widgets.https_proxy_l = widget_new_label(set, 0, 0,
1055 screen->widgets.https_proxy_f = widget_new_textbox(set, 0, 0, 32,
1056 config->https_proxy);
1058 if (config->safe_mode)
1059 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
1060 _("Selecting 'OK' will exit safe mode"));
1062 screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
1064 screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
1065 COLS - screen->field_x - 1);
1067 widget_select_add_option(screen->widgets.allow_write_f, 0,
1068 _("Prevent all writes to disk"),
1069 !config->allow_writes);
1071 widget_select_add_option(screen->widgets.allow_write_f, 1,
1072 _("Allow bootloader scripts to modify disks"),
1073 config->allow_writes);
1075 screen->widgets.boot_console_l = widget_new_label(set, 0, 0,
1076 _("Boot console:"));
1077 screen->widgets.boot_console_f = widget_new_select(set, 0, 0,
1078 COLS - screen->field_x - 1);
1080 for (i = 0; i < config->n_consoles; i++){
1081 found = config->boot_console &&
1082 strncmp(config->boot_console, config->consoles[i],
1083 strlen(config->boot_console)) == 0;
1084 widget_select_add_option(screen->widgets.boot_console_f, i,
1085 config->consoles[i], found);
1088 if (config->manual_console) {
1089 label = talloc_asprintf(screen, _("Manually set: '%s'"),
1090 config->boot_console);
1091 screen->widgets.manual_console_l = widget_new_label(set, 0, 0, label);
1094 tty = talloc_asprintf(screen, _("Current interface: %s"),
1095 ttyname(STDIN_FILENO));
1096 screen->widgets.current_console_l = widget_new_label(set, 0 , 0, tty);
1098 screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
1100 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
1101 help_click, screen);
1102 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
1103 cancel_click, screen);
1106 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
1108 struct config_screen *screen = arg;
1109 int w_y, w_height, w_focus, s_max, adjust;
1111 w_height = widget_height(widget);
1112 w_focus = widget_focus_y(widget);
1113 w_y = widget_y(widget) + w_focus;
1114 s_max = getmaxy(screen->scr.sub_ncw) - 1;
1116 if (w_y < screen->scroll_y)
1117 screen->scroll_y = w_y;
1119 else if (w_y + screen->scroll_y + 1 > s_max) {
1120 /* Fit as much of the widget into the screen as possible */
1121 adjust = min(s_max - 1, w_height - w_focus);
1122 if (w_y + adjust >= screen->scroll_y + s_max)
1123 screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
1127 pad_refresh(screen);
1130 static void config_screen_draw(struct config_screen *screen,
1131 const struct config *config,
1132 const struct system_info *sysinfo)
1134 bool repost = false;
1137 /* The size of the pad we'll need depends on the number of interfaces.
1139 * We use N_FIELDS (which is quite conservative, as some fields share
1140 * a line) as a base, then:
1141 * - add 6 (as the network select & boot device select fields take 3
1143 * - add n_interfaces for every field in the network select field, and
1144 * - add (n_blockdevs + n_interfaces) for every field in the boot device
1147 height = N_FIELDS + 6;
1149 height += sysinfo->n_interfaces;
1150 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
1152 if (!screen->pad || getmaxy(screen->pad) < height) {
1154 delwin(screen->pad);
1155 screen->pad = newpad(height, COLS + 10);
1158 if (screen->widgetset) {
1159 widgetset_unpost(screen->widgetset);
1160 talloc_free(screen->widgetset);
1164 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
1166 widgetset_set_widget_focus(screen->widgetset,
1167 config_screen_widget_focus, screen);
1169 if (!config || !sysinfo) {
1170 config_screen_setup_empty(screen);
1172 screen->net_conf_type = find_net_conf_type(config);
1173 config_screen_setup_widgets(screen, config, sysinfo);
1174 config_screen_layout_widgets(screen);
1178 widgetset_post(screen->widgetset);
1181 void config_screen_update(struct config_screen *screen,
1182 const struct config *config,
1183 const struct system_info *sysinfo)
1185 if (screen->cui->current != config_screen_scr(screen)) {
1186 screen->need_update = true;
1190 config_screen_draw(screen, config, sysinfo);
1191 pad_refresh(screen);
1194 static int config_screen_post(struct nc_scr *scr)
1196 struct config_screen *screen = config_screen_from_scr(scr);
1197 screen->show_subset = false;
1199 if (screen->need_update) {
1200 config_screen_draw(screen, screen->cui->config,
1201 screen->cui->sysinfo);
1202 screen->need_update = false;
1204 widgetset_post(screen->widgetset);
1207 nc_scr_frame_draw(scr);
1208 if (screen->need_redraw) {
1209 redrawwin(scr->main_ncw);
1210 screen->need_redraw = false;
1212 wrefresh(screen->scr.main_ncw);
1213 pad_refresh(screen);
1217 static int config_screen_destroy(void *arg)
1219 struct config_screen *screen = arg;
1221 delwin(screen->pad);
1225 struct config_screen *config_screen_init(struct cui *cui,
1226 const struct config *config,
1227 const struct system_info *sysinfo,
1228 void (*on_exit)(struct cui *))
1230 struct config_screen *screen;
1232 screen = talloc_zero(cui, struct config_screen);
1233 talloc_set_destructor(screen, config_screen_destroy);
1234 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1235 cui, config_screen_process_key,
1236 config_screen_post, config_screen_unpost,
1237 config_screen_resize);
1240 screen->on_exit = on_exit;
1241 screen->need_redraw = false;
1242 screen->need_update = false;
1243 screen->label_x = 2;
1244 screen->field_x = 17;
1246 screen->ipmi_override = false;
1247 screen->show_subset = false;
1249 screen->scr.frame.ltitle = talloc_strdup(screen,
1250 _("Petitboot System Configuration"));
1251 screen->scr.frame.rtitle = NULL;
1252 screen->scr.frame.help = talloc_strdup(screen,
1253 _("tab=next, shift+tab=previous, x=exit, h=help"));
1254 nc_scr_frame_draw(&screen->scr);
1256 scrollok(screen->scr.sub_ncw, true);
1258 config_screen_draw(screen, config, sysinfo);