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>
32 #include "ui/common/discover-client.h"
34 #include "nc-config.h"
35 #include "nc-widgets.h"
39 extern struct help_text config_help_text;
42 NET_CONF_TYPE_DHCP_ALL,
43 NET_CONF_TYPE_DHCP_ONE,
47 struct config_screen {
50 struct nc_widgetset *widgetset;
58 void (*on_exit)(struct cui *);
66 enum net_conf_type net_conf_type;
68 bool autoboot_enabled;
74 struct nc_widget_label *autoboot_l;
75 struct nc_widget_select *autoboot_f;
76 struct nc_widget_label *boot_order_l;
77 struct nc_widget_subset *boot_order_f;
78 struct nc_widget_label *boot_empty_l;
79 struct nc_widget_button *boot_add_b;
80 struct nc_widget_button *boot_none_b;
81 struct nc_widget_button *boot_any_b;
82 struct nc_widget_textbox *timeout_f;
83 struct nc_widget_label *timeout_l;
84 struct nc_widget_label *timeout_help_l;
86 struct nc_widget_label *ipmi_type_l;
87 struct nc_widget_label *ipmi_clear_l;
88 struct nc_widget_button *ipmi_clear_b;
90 struct nc_widget_label *ipmi_mailbox_l;
91 struct nc_widget_button *ipmi_mailbox_b;
93 struct nc_widget_label *network_l;
94 struct nc_widget_select *network_f;
96 struct nc_widget_label *iface_l;
97 struct nc_widget_select *iface_f;
98 struct nc_widget_label *ip_addr_l;
99 struct nc_widget_textbox *ip_addr_f;
100 struct nc_widget_label *ip_mask_l;
101 struct nc_widget_textbox *ip_mask_f;
102 struct nc_widget_label *ip_addr_mask_help_l;
103 struct nc_widget_label *gateway_l;
104 struct nc_widget_textbox *gateway_f;
105 struct nc_widget_label *gateway_help_l;
106 struct nc_widget_label *url_l;
107 struct nc_widget_textbox *url_f;
108 struct nc_widget_label *url_help_l;
109 struct nc_widget_label *dns_l;
110 struct nc_widget_textbox *dns_f;
111 struct nc_widget_label *dns_dhcp_help_l;
112 struct nc_widget_label *dns_help_l;
113 struct nc_widget_label *http_proxy_l;
114 struct nc_widget_textbox *http_proxy_f;
115 struct nc_widget_label *https_proxy_l;
116 struct nc_widget_textbox *https_proxy_f;
118 struct nc_widget_label *allow_write_l;
119 struct nc_widget_select *allow_write_f;
120 struct nc_widget_label *boot_console_l;
121 struct nc_widget_select *boot_console_f;
122 struct nc_widget_label *manual_console_l;
123 struct nc_widget_label *current_console_l;
125 struct nc_widget_button *update_password_l;
127 struct nc_widget_label *net_override_l;
128 struct nc_widget_label *safe_mode;
129 struct nc_widget_button *ok_b;
130 struct nc_widget_button *help_b;
131 struct nc_widget_button *cancel_b;
135 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
137 struct config_screen *config_screen;
139 assert(scr->sig == pb_config_screen_sig);
140 config_screen = (struct config_screen *)
141 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
142 assert(config_screen->scr.sig == pb_config_screen_sig);
143 return config_screen;
146 static void pad_refresh(struct config_screen *screen)
148 int y, x, rows, cols;
150 getmaxyx(screen->scr.sub_ncw, rows, cols);
151 getbegyx(screen->scr.sub_ncw, y, x);
153 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
156 static void config_screen_process_key(struct nc_scr *scr, int key)
158 struct config_screen *screen = config_screen_from_scr(scr);
161 handled = widgetset_process_key(screen->widgetset, key);
170 screen->show_help = true;
176 screen->on_exit(screen->cui);
178 } else if (screen->show_help) {
179 screen->show_help = false;
180 screen->need_redraw = true;
181 cui_show_help(screen->cui, _("System Configuration"),
184 } else if (handled && (screen->cui->current == scr)) {
189 static void config_screen_resize(struct nc_scr *scr)
191 struct config_screen *screen = config_screen_from_scr(scr);
195 static int config_screen_unpost(struct nc_scr *scr)
197 struct config_screen *screen = config_screen_from_scr(scr);
198 widgetset_unpost(screen->widgetset);
202 struct nc_scr *config_screen_scr(struct config_screen *screen)
207 static int screen_process_form(struct config_screen *screen)
209 const struct system_info *sysinfo = screen->cui->sysinfo;
210 enum net_conf_type net_conf_type;
211 struct interface_config *iface;
214 struct config *config;
215 int i, n_boot_opts, rc;
216 unsigned int *order, idx;
219 config = config_copy(screen, screen->cui->config);
221 talloc_free(config->autoboot_opts);
222 config->n_autoboot_opts = 0;
224 n_boot_opts = widget_subset_get_order(config, &order,
225 screen->widgets.boot_order_f);
227 config->autoboot_enabled = widget_select_get_value(
228 screen->widgets.autoboot_f);
230 config->n_autoboot_opts = n_boot_opts;
231 config->autoboot_opts = talloc_array(config, struct autoboot_option,
234 for (i = 0; i < n_boot_opts; i++) {
235 if (order[i] < sysinfo->n_blockdevs) {
237 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
238 config->autoboot_opts[i].uuid = talloc_strdup(config,
239 sysinfo->blockdevs[order[i]]->uuid);
240 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
242 order[i] -= sysinfo->n_blockdevs;
243 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
244 mac_str(sysinfo->interfaces[order[i]]->hwaddr,
245 sysinfo->interfaces[order[i]]->hwaddr_size,
247 config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
250 order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
251 config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
252 config->autoboot_opts[i].type = order[i];
256 str = widget_textbox_get_value(screen->widgets.timeout_f);
260 x = strtoul(str, &end, 10);
261 if (!errno && end != str)
262 config->autoboot_timeout_sec = x;
265 net_conf_type = widget_select_get_value(screen->widgets.network_f);
267 /* if we don't have any network interfaces, prevent per-interface
269 if (sysinfo->n_interfaces == 0)
270 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
272 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
273 config->network.n_interfaces = 0;
276 iface = talloc_zero(config, struct interface_config);
277 config->network.n_interfaces = 1;
278 config->network.interfaces = talloc_array(config,
279 struct interface_config *, 1);
280 config->network.interfaces[0] = iface;
282 /* copy hwaddr (from the sysinfo interface data) to
283 * the configuration */
284 idx = widget_select_get_value(screen->widgets.iface_f);
285 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
286 sizeof(iface->hwaddr));
289 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
290 iface->method = CONFIG_METHOD_DHCP;
293 if (net_conf_type == NET_CONF_TYPE_STATIC) {
294 char *ip, *mask, *gateway, *url;
296 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
297 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
298 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
299 url = widget_textbox_get_value(screen->widgets.url_f);
301 if (!ip || !*ip || !mask || !*mask) {
302 screen->scr.frame.status =
303 _("No IP / mask values are set");
304 nc_scr_frame_draw(&screen->scr);
309 iface->method = CONFIG_METHOD_STATIC;
310 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
312 iface->static_config.gateway = talloc_strdup(iface, gateway);
313 iface->static_config.url = talloc_strdup(iface, url);
316 str = widget_textbox_get_value(screen->widgets.dns_f);
317 talloc_free(config->network.dns_servers);
318 config->network.dns_servers = NULL;
319 config->network.n_dns_servers = 0;
321 if (str && strlen(str)) {
326 dns = strtok_r(str, " \t", &tmp);
331 i = config->network.n_dns_servers++;
332 config->network.dns_servers = talloc_realloc(config,
333 config->network.dns_servers,
335 config->network.n_dns_servers);
336 config->network.dns_servers[i] =
337 talloc_strdup(config, dns);
343 talloc_free(config->http_proxy);
344 talloc_free(config->https_proxy);
345 str = widget_textbox_get_value(screen->widgets.http_proxy_f);
346 config->http_proxy = talloc_strdup(config, str);
347 str = widget_textbox_get_value(screen->widgets.https_proxy_f);
348 config->https_proxy = talloc_strdup(config, str);
350 allow_write = widget_select_get_value(screen->widgets.allow_write_f);
351 if (allow_write != config->allow_writes)
352 config->allow_writes = allow_write;
354 if (config->n_consoles && !config->manual_console) {
355 idx = widget_select_get_value(screen->widgets.boot_console_f);
356 if (!config->boot_console) {
357 config->boot_console = talloc_strdup(config,
358 config->consoles[idx]);
359 } else if (strncmp(config->boot_console, config->consoles[idx],
360 strlen(config->boot_console)) != 0) {
361 talloc_free(config->boot_console);
362 config->boot_console = talloc_strdup(config,
363 config->consoles[idx]);
367 config->safe_mode = false;
368 rc = cui_send_config(screen->cui, config);
372 pb_log("cui_send_config failed!\n");
374 pb_debug("config sent!\n");
379 static void config_screen_config_cb(struct nc_scr *scr)
381 struct config_screen *screen = config_screen_from_scr(scr);
383 if (!screen_process_form(screen))
388 static void password_click(void *arg)
390 struct config_screen *screen = arg;
392 screen->need_update = true;
393 cui_show_auth(screen->cui, screen->scr.main_ncw, true, NULL);
397 static void ok_click(void *arg)
399 struct config_screen *screen = arg;
401 if (discover_client_authenticated(screen->cui->client)) {
402 if (screen_process_form(screen))
403 /* errors are written to the status line, so we'll need
405 wrefresh(screen->scr.main_ncw);
409 cui_show_auth(screen->cui, screen->scr.main_ncw, false,
410 config_screen_config_cb);
414 static void help_click(void *arg)
416 struct config_screen *screen = arg;
417 screen->show_help = true;
420 static void cancel_click(void *arg)
422 struct config_screen *screen = arg;
426 static void ipmi_clear_click(void *arg)
428 struct config_screen *screen = arg;
429 struct config *config;
432 config = config_copy(screen, screen->cui->config);
433 config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
434 config->safe_mode = false;
436 rc = cui_send_config(screen->cui, config);
440 pb_log("cui_send_config failed!\n");
442 pb_debug("config sent!\n");
446 static void ipmi_clear_mailbox_click(void *arg)
448 struct config_screen *screen = arg;
449 struct config *config;
452 config = config_copy(screen, screen->cui->config);
453 config->ipmi_bootdev_mailbox = false;
454 config->safe_mode = false;
456 rc = cui_send_config(screen->cui, config);
460 pb_log("cui_send_config failed!\n");
462 pb_debug("config sent!\n");
467 static int layout_pair(struct config_screen *screen, int y,
468 struct nc_widget_label *label,
469 struct nc_widget *field)
471 struct nc_widget *label_w = widget_label_base(label);
472 widget_move(label_w, y, screen->label_x);
473 widget_move(field, y, screen->field_x);
474 return max(widget_height(label_w), widget_height(field));
477 static void config_screen_layout_widgets(struct config_screen *screen)
479 struct nc_widget *wl, *wf, *wh;
484 /* currently, the longest label we have is the DNS-servers
485 * widget, so layout our screen based on that */
486 help_x = screen->field_x + 2 +
487 widget_width(widget_textbox_base(screen->widgets.dns_f));
489 wl = widget_label_base(screen->widgets.autoboot_l);
490 widget_set_visible(wl, true);
491 widget_move(wl, y, screen->label_x);
493 wf = widget_select_base(screen->widgets.autoboot_f);
494 widget_set_visible(wf, true);
495 widget_move(wf, y, screen->field_x);
496 y += widget_height(wf);
498 show = screen->autoboot_enabled;
503 wl = widget_label_base(screen->widgets.boot_order_l);
504 widget_set_visible(wl, show);
505 widget_move(wl, y, screen->label_x);
507 wf = widget_subset_base(screen->widgets.boot_order_f);
508 widget_move(wf, y, screen->field_x);
509 wl = widget_label_base(screen->widgets.boot_empty_l);
510 widget_move(wl, y, screen->field_x);
512 if (widget_subset_height(screen->widgets.boot_order_f)) {
513 widget_set_visible(wl, false);
514 widget_set_visible(wf, show);
515 y += show ? widget_height(wf) : 0;
517 widget_set_visible(wl, show);
518 widget_set_visible(wf, false);
524 widget_move(widget_button_base(screen->widgets.boot_add_b),
525 y++, screen->field_x);
526 widget_move(widget_button_base(screen->widgets.boot_any_b),
527 y++, screen->field_x);
528 widget_move(widget_button_base(screen->widgets.boot_none_b),
532 wf = widget_button_base(screen->widgets.boot_add_b);
533 if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
534 widget_set_visible(wf, true);
536 widget_set_visible(wf, false);
541 widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
542 widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
544 wf = widget_textbox_base(screen->widgets.timeout_f);
545 wl = widget_label_base(screen->widgets.timeout_l);
546 wh = widget_label_base(screen->widgets.timeout_help_l);
547 widget_set_visible(wl, screen->autoboot_enabled);
548 widget_set_visible(wf, screen->autoboot_enabled);
549 widget_set_visible(wh, screen->autoboot_enabled);
550 if (screen->autoboot_enabled) {
551 widget_set_visible(wh, screen->autoboot_enabled);
552 widget_move(wl, y, screen->label_x);
553 widget_move(wf, y, screen->field_x);
554 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
559 if (screen->ipmi_override) {
560 wl = widget_label_base(screen->widgets.ipmi_type_l);
561 widget_set_visible(wl, true);
562 widget_move(wl, y, screen->label_x);
565 wl = widget_label_base(screen->widgets.ipmi_clear_l);
566 wf = widget_button_base(screen->widgets.ipmi_clear_b);
567 widget_set_visible(wl, true);
568 widget_set_visible(wf, true);
569 widget_move(wl, y, screen->label_x);
570 widget_move(wf, y, screen->field_x);
574 if (screen->ipmi_mailbox) {
575 wl = widget_label_base(screen->widgets.ipmi_mailbox_l);
576 widget_set_visible(wl, true);
577 widget_move(wl, y, screen->label_x);
580 wf = widget_button_base(screen->widgets.ipmi_mailbox_b);
581 widget_set_visible(wf, true);
582 widget_move(wf, y, screen->field_x);
588 y += layout_pair(screen, y, screen->widgets.network_l,
589 widget_select_base(screen->widgets.network_f));
593 /* conditionally show iface select */
594 wl = widget_label_base(screen->widgets.iface_l);
595 wf = widget_select_base(screen->widgets.iface_f);
597 show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
598 screen->net_conf_type == NET_CONF_TYPE_STATIC;
600 widget_set_visible(wl, show);
601 widget_set_visible(wf, show);
604 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
606 /* conditionally show static IP params */
607 show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
609 wl = widget_label_base(screen->widgets.ip_addr_l);
610 wf = widget_textbox_base(screen->widgets.ip_addr_f);
611 widget_set_visible(wl, show);
612 widget_set_visible(wf, show);
613 x = screen->field_x + widget_width(wf) + 1;
616 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
618 wl = widget_label_base(screen->widgets.ip_mask_l);
619 wf = widget_textbox_base(screen->widgets.ip_mask_f);
620 widget_set_visible(wl, show);
621 widget_set_visible(wf, show);
624 widget_move(wl, y, x);
625 widget_move(wf, y, x + 2);
628 /* help for IP/mask */
629 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
630 widget_set_visible(wh, show);
632 widget_move(wh, y, help_x);
636 wl = widget_label_base(screen->widgets.gateway_l);
637 wf = widget_textbox_base(screen->widgets.gateway_f);
638 wh = widget_label_base(screen->widgets.gateway_help_l);
639 widget_set_visible(wl, show);
640 widget_set_visible(wf, show);
641 widget_set_visible(wh, show);
644 layout_pair(screen, y, screen->widgets.gateway_l, wf);
645 widget_move(wh, y, help_x);
649 wl = widget_label_base(screen->widgets.url_l);
650 wf = widget_textbox_base(screen->widgets.url_f);
651 wh = widget_label_base(screen->widgets.url_help_l);
652 widget_set_visible(wl, show);
653 widget_set_visible(wf, show);
654 widget_set_visible(wh, show);
657 layout_pair(screen, y, screen->widgets.url_l, wf);
658 widget_move(wh, y, help_x);
662 wh = widget_label_base(screen->widgets.dns_help_l);
663 layout_pair(screen, y, screen->widgets.dns_l,
664 widget_textbox_base(screen->widgets.dns_f));
665 widget_move(wh, y, help_x);
668 /* we show the DNS/DHCP help if we're configuring DHCP */
669 show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
670 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
671 widget_set_visible(wl, show);
673 widget_move(wl, y, screen->field_x);
677 wf = widget_textbox_base(screen->widgets.http_proxy_f);
678 layout_pair(screen, y, screen->widgets.http_proxy_l, wf);
680 wf = widget_textbox_base(screen->widgets.https_proxy_f);
681 layout_pair(screen, y, screen->widgets.https_proxy_l, wf);
686 layout_pair(screen, y, screen->widgets.allow_write_l,
687 widget_select_base(screen->widgets.allow_write_f));
688 y += widget_height(widget_select_base(screen->widgets.allow_write_f));
692 if (screen->widgets.manual_console_l) {
693 layout_pair(screen, y++, screen->widgets.boot_console_l,
694 widget_label_base(screen->widgets.manual_console_l));
695 widget_move(widget_label_base(screen->widgets.current_console_l),
697 widget_set_visible(widget_select_base(
698 screen->widgets.boot_console_f), false);
700 } else if (widget_height(widget_select_base(screen->widgets.boot_console_f))) {
701 layout_pair(screen, y, screen->widgets.boot_console_l,
702 widget_select_base(screen->widgets.boot_console_f));
703 y += widget_height(widget_select_base(screen->widgets.boot_console_f));
704 widget_move(widget_label_base(screen->widgets.current_console_l),
708 widget_set_visible(widget_label_base(
709 screen->widgets.boot_console_l), false);
710 widget_set_visible(widget_select_base(
711 screen->widgets.boot_console_f), false);
712 widget_set_visible(widget_label_base(
713 screen->widgets.current_console_l), false);
717 widget_move(widget_button_base(screen->widgets.update_password_l),
722 if (screen->net_override) {
723 widget_move(widget_label_base(screen->widgets.net_override_l),
725 widget_set_visible(widget_label_base(screen->widgets.net_override_l),
730 if (screen->cui->config->safe_mode) {
731 widget_move(widget_label_base(screen->widgets.safe_mode),
733 widget_set_visible(widget_label_base(screen->widgets.safe_mode),
738 widget_move(widget_button_base(screen->widgets.ok_b),
740 widget_move(widget_button_base(screen->widgets.help_b),
741 y, screen->field_x + 14);
742 widget_move(widget_button_base(screen->widgets.cancel_b),
743 y, screen->field_x + 28);
746 static void config_screen_network_change(void *arg, int value)
748 struct config_screen *screen = arg;
749 screen->net_conf_type = value;
750 widgetset_unpost(screen->widgetset);
751 config_screen_layout_widgets(screen);
752 widgetset_post(screen->widgetset);
755 static void config_screen_boot_order_change(void *arg, int value)
758 struct config_screen *screen = arg;
759 widgetset_unpost(screen->widgetset);
760 config_screen_layout_widgets(screen);
761 widgetset_post(screen->widgetset);
764 static void config_screen_autoboot_change(void *arg, int value)
766 struct config_screen *screen = arg;
767 screen->autoboot_enabled = !!value;
768 widgetset_unpost(screen->widgetset);
769 config_screen_layout_widgets(screen);
770 widgetset_post(screen->widgetset);
773 static void config_screen_add_device(void *arg)
775 struct config_screen *screen = arg;
777 cui_show_subset(screen->cui, _("Select a boot device to add"),
778 screen->widgets.boot_order_f);
781 static void config_screen_autoboot_none(void *arg)
783 struct config_screen *screen = arg;
784 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
786 widget_subset_clear_active(subset);
788 widgetset_unpost(screen->widgetset);
789 config_screen_layout_widgets(screen);
790 widgetset_post(screen->widgetset);
793 static void config_screen_autoboot_any(void *arg)
795 struct config_screen *screen = arg;
796 const struct system_info *sysinfo = screen->cui->sysinfo;
797 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
800 widget_subset_clear_active(subset);
802 idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
804 widget_subset_make_active(screen->widgets.boot_order_f, idx);
806 screen->autoboot_enabled = true;
808 widgetset_unpost(screen->widgetset);
809 config_screen_layout_widgets(screen);
810 widgetset_post(screen->widgetset);
813 static void config_screen_update_subset(void *arg,
814 struct nc_widget_subset *subset, int idx)
816 struct config_screen *screen = arg;
819 widget_subset_make_active(subset, idx);
820 if (!screen->autoboot_enabled)
821 screen->autoboot_enabled = true;
822 config_screen_layout_widgets(screen);
825 static struct interface_config *first_active_interface(
826 const struct config *config)
830 for (i = 0; i < config->network.n_interfaces; i++) {
831 if (config->network.interfaces[i]->ignore)
833 return config->network.interfaces[i];
838 static enum net_conf_type find_net_conf_type(const struct config *config)
840 struct interface_config *ifcfg;
842 ifcfg = first_active_interface(config);
845 return NET_CONF_TYPE_DHCP_ALL;
847 else if (ifcfg->method == CONFIG_METHOD_DHCP)
848 return NET_CONF_TYPE_DHCP_ONE;
850 else if (ifcfg->method == CONFIG_METHOD_STATIC)
851 return NET_CONF_TYPE_STATIC;
854 return NET_CONF_TYPE_DHCP_ALL;
857 static void config_screen_setup_empty(struct config_screen *screen)
859 widget_new_label(screen->widgetset, 2, screen->field_x,
860 _("Waiting for configuration data..."));
861 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
862 4, screen->field_x, 9, _("Cancel"),
863 cancel_click, screen);
866 static int find_autoboot_idx(const struct system_info *sysinfo,
867 struct autoboot_option *opt)
871 if (opt->boot_type == BOOT_DEVICE_TYPE)
872 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
874 for (i = 0; i < sysinfo->n_blockdevs; i++) {
875 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
879 for (i = 0; i < sysinfo->n_interfaces; i++) {
880 struct interface_info *info = sysinfo->interfaces[i];
883 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
885 if (!strcmp(mac, opt->uuid))
886 return sysinfo->n_blockdevs + i;
892 static void config_screen_setup_widgets(struct config_screen *screen,
893 const struct config *config,
894 const struct system_info *sysinfo)
896 struct nc_widgetset *set = screen->widgetset;
897 struct interface_config *ifcfg;
898 char *str, *ip, *mask, *gw, *url, *tty, *label;
899 enum net_conf_type type;
901 int add_len, clear_len, any_len, min_len = 20;
904 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
907 type = screen->net_conf_type;
908 ifcfg = first_active_interface(config);
910 screen->autoboot_enabled = config->autoboot_enabled;
912 screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
914 screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
915 COLS - screen->field_x - 1);
917 widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
918 !screen->autoboot_enabled);
919 widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
920 screen->autoboot_enabled);
922 widget_select_on_change(screen->widgets.autoboot_f,
923 config_screen_autoboot_change, screen);
925 add_len = max(min_len, strncols(_("Add Device")));
926 clear_len = max(min_len, strncols(_("Clear")));
927 any_len = max(min_len, strncols(_("Clear & Boot Any")));
929 screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
931 config_screen_add_device, screen);
933 screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
935 config_screen_autoboot_none, screen);
937 screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
938 _("Clear & Boot Any"),
939 config_screen_autoboot_any, screen);
941 screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
943 screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
944 COLS - screen->field_x,
945 config_screen_update_subset);
946 screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
949 widget_subset_on_change(screen->widgets.boot_order_f,
950 config_screen_boot_order_change, screen);
952 for (i = 0; i < sysinfo->n_blockdevs; i++) {
953 struct blockdev_info *bd = sysinfo->blockdevs[i];
955 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
958 widget_subset_add_option(screen->widgets.boot_order_f, label);
961 for (i = 0; i < sysinfo->n_interfaces; i++) {
962 struct interface_info *info = sysinfo->interfaces[i];
965 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
967 label = talloc_asprintf(screen, _("net: %s [mac: %s]"),
970 widget_subset_add_option(screen->widgets.boot_order_f, label);
973 for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
975 if (i == DEVICE_TYPE_ANY)
976 label = talloc_asprintf(screen, _("Any Device"));
978 label = talloc_asprintf(screen, _("Any %s device"),
979 device_type_display_name(i));
981 widget_subset_add_option(screen->widgets.boot_order_f, label);
984 for (i = 0; i < config->n_autoboot_opts; i++) {
985 struct autoboot_option *opt = &config->autoboot_opts[i];
988 idx = find_autoboot_idx(sysinfo, opt);
991 widget_subset_make_active(screen->widgets.boot_order_f,
994 if (opt->boot_type == BOOT_DEVICE_TYPE)
995 pb_log("%s: Unknown autoboot option: %d\n",
996 __func__, opt->type);
998 pb_log("%s: Unknown autoboot UUID: %s\n",
999 __func__, opt->uuid);
1004 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
1005 screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
1006 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
1007 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
1010 widget_textbox_set_fixed_size(screen->widgets.timeout_f);
1011 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
1013 if (config->ipmi_bootdev) {
1014 label = talloc_asprintf(screen,
1015 _("%s IPMI boot option: %s"),
1016 config->ipmi_bootdev_persistent ?
1017 _("Persistent") : _("Temporary"),
1018 ipmi_bootdev_display_name(config->ipmi_bootdev));
1019 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
1021 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
1022 _("Clear option:"));
1023 screen->widgets.ipmi_clear_b = widget_new_button(set, 0, 0,
1024 strncols(_("Clear IPMI override now")) + 10,
1025 _("Clear IPMI override now"),
1026 ipmi_clear_click, screen);
1027 screen->ipmi_override = true;
1030 if (config->ipmi_bootdev_mailbox) {
1031 screen->widgets.ipmi_mailbox_l = widget_new_label(set, 0, 0,
1032 _("IPMI boot order mailbox config present"));
1033 screen->widgets.ipmi_mailbox_b = widget_new_button(set, 0, 0,
1034 strncols(_("Clear IPMI boot order mailbox now")) + 10,
1035 _("Clear IPMI boot order mailbox now"),
1036 ipmi_clear_mailbox_click, screen);
1037 screen->ipmi_mailbox = true;
1040 screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
1041 screen->widgets.network_f = widget_new_select(set, 0, 0,
1042 COLS - screen->field_x - 1);
1044 widget_select_add_option(screen->widgets.network_f,
1045 NET_CONF_TYPE_DHCP_ALL,
1046 _("DHCP on all active interfaces"),
1047 type == NET_CONF_TYPE_DHCP_ALL);
1048 widget_select_add_option(screen->widgets.network_f,
1049 NET_CONF_TYPE_DHCP_ONE,
1050 _("DHCP on a specific interface"),
1051 type == NET_CONF_TYPE_DHCP_ONE);
1052 widget_select_add_option(screen->widgets.network_f,
1053 NET_CONF_TYPE_STATIC,
1054 _("Static IP configuration"),
1055 type == NET_CONF_TYPE_STATIC);
1057 widget_select_on_change(screen->widgets.network_f,
1058 config_screen_network_change, screen);
1060 screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
1061 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
1063 for (i = 0; i < sysinfo->n_interfaces; i++) {
1064 struct interface_info *info = sysinfo->interfaces[i];
1065 char str[50], mac[20];
1068 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
1069 sizeof(ifcfg->hwaddr));
1071 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
1072 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
1073 info->link ? _("link up") : _("link down"));
1075 widget_select_add_option(screen->widgets.iface_f,
1076 i, str, is_default);
1079 url = gw = ip = mask = NULL;
1080 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
1083 str = talloc_strdup(screen, ifcfg->static_config.address);
1084 sep = strchr(str, '/');
1091 gw = ifcfg->static_config.gateway;
1092 url = ifcfg->static_config.url;
1095 screen->net_override = ifcfg && ifcfg->override;
1096 if (screen->net_override) {
1097 screen->widgets.net_override_l = widget_new_label(set, 0, 0,
1098 _("Network Override Active! 'OK' will overwrite interface config"));
1101 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
1102 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
1103 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
1104 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
1105 screen->widgets.ip_addr_mask_help_l =
1106 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
1108 widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
1109 widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
1110 widget_textbox_set_validator_ip(screen->widgets.ip_addr_f);
1111 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 127);
1113 screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
1114 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
1115 screen->widgets.gateway_help_l =
1116 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
1118 widget_textbox_set_fixed_size(screen->widgets.gateway_f);
1119 widget_textbox_set_validator_ip(screen->widgets.gateway_f);
1121 screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
1122 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
1123 widget_textbox_set_validator_url(screen->widgets.url_f);
1124 screen->widgets.url_help_l =
1125 widget_new_label(set, 0, 0, _("(eg. tftp://)"));
1127 str = talloc_strdup(screen, "");
1128 for (i = 0; i < config->network.n_dns_servers; i++) {
1129 str = talloc_asprintf_append(str, "%s%s",
1130 (i == 0) ? "" : " ",
1131 config->network.dns_servers[i]);
1134 screen->widgets.dns_l = widget_new_label(set, 0, 0,
1135 _("DNS Server(s):"));
1136 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
1137 screen->widgets.dns_help_l =
1138 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
1140 widget_textbox_set_validator_ip_multi(screen->widgets.dns_f);
1142 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
1143 _("(if not provided by DHCP server)"));
1145 screen->widgets.http_proxy_l = widget_new_label(set, 0, 0,
1147 screen->widgets.http_proxy_f = widget_new_textbox(set, 0, 0, 32,
1148 config->http_proxy);
1149 screen->widgets.https_proxy_l = widget_new_label(set, 0, 0,
1151 screen->widgets.https_proxy_f = widget_new_textbox(set, 0, 0, 32,
1152 config->https_proxy);
1154 if (config->safe_mode)
1155 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
1156 _("Selecting 'OK' will exit safe mode"));
1158 screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
1160 screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
1161 COLS - screen->field_x - 1);
1163 widget_select_add_option(screen->widgets.allow_write_f, 0,
1164 _("Prevent all writes to disk"),
1165 !config->allow_writes);
1167 widget_select_add_option(screen->widgets.allow_write_f, 1,
1168 _("Allow bootloader scripts to modify disks"),
1169 config->allow_writes);
1171 screen->widgets.boot_console_l = widget_new_label(set, 0, 0,
1172 _("Boot console:"));
1173 screen->widgets.boot_console_f = widget_new_select(set, 0, 0,
1174 COLS - screen->field_x - 1);
1176 for (i = 0; i < config->n_consoles; i++){
1177 found = config->boot_console &&
1178 strncmp(config->boot_console, config->consoles[i],
1179 strlen(config->boot_console)) == 0;
1180 widget_select_add_option(screen->widgets.boot_console_f, i,
1181 config->consoles[i], found);
1184 if (config->manual_console) {
1185 label = talloc_asprintf(screen, _("Manually set: '%s'"),
1186 config->boot_console);
1187 screen->widgets.manual_console_l = widget_new_label(set, 0, 0, label);
1190 tty = talloc_asprintf(screen, _("Current interface: %s"),
1191 ttyname(STDIN_FILENO));
1192 screen->widgets.current_console_l = widget_new_label(set, 0 , 0, tty);
1194 #ifdef CRYPT_SUPPORT
1195 screen->widgets.update_password_l = widget_new_button(set, 0, 0, 30,
1196 _("Update system password"), password_click, screen);
1199 screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
1201 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
1202 help_click, screen);
1203 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
1204 cancel_click, screen);
1207 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
1209 struct config_screen *screen = arg;
1210 int w_y, w_height, w_focus, s_max, adjust;
1212 w_height = widget_height(widget);
1213 w_focus = widget_focus_y(widget);
1214 w_y = widget_y(widget) + w_focus;
1215 s_max = getmaxy(screen->scr.sub_ncw) - 1;
1217 if (w_y < screen->scroll_y)
1218 screen->scroll_y = w_y;
1220 else if (w_y + screen->scroll_y + 1 > s_max) {
1221 /* Fit as much of the widget into the screen as possible */
1222 adjust = min(s_max - 1, w_height - w_focus);
1223 if (w_y + adjust >= screen->scroll_y + s_max)
1224 screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
1228 pad_refresh(screen);
1231 static void config_screen_draw(struct config_screen *screen,
1232 const struct config *config,
1233 const struct system_info *sysinfo)
1235 bool repost = false;
1238 /* The size of the pad we'll need depends on the number of interfaces.
1240 * We use N_FIELDS (which is quite conservative, as some fields share
1241 * a line) as a base, then:
1242 * - add 6 (as the network select & boot device select fields take 3
1244 * - add n_interfaces for every field in the network select field, and
1245 * - add (n_blockdevs + n_interfaces) for every field in the boot device
1248 height = N_FIELDS + 6;
1250 height += sysinfo->n_interfaces;
1251 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
1253 if (!screen->pad || getmaxy(screen->pad) < height) {
1255 delwin(screen->pad);
1256 screen->pad = newpad(height, COLS + 10);
1259 if (screen->widgetset) {
1260 widgetset_unpost(screen->widgetset);
1261 talloc_free(screen->widgetset);
1265 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
1267 widgetset_set_widget_focus(screen->widgetset,
1268 config_screen_widget_focus, screen);
1270 if (!config || !sysinfo) {
1271 config_screen_setup_empty(screen);
1273 screen->net_conf_type = find_net_conf_type(config);
1274 config_screen_setup_widgets(screen, config, sysinfo);
1275 config_screen_layout_widgets(screen);
1279 widgetset_post(screen->widgetset);
1282 void config_screen_update(struct config_screen *screen,
1283 const struct config *config,
1284 const struct system_info *sysinfo)
1286 if (screen->cui->current != config_screen_scr(screen)) {
1287 screen->need_update = true;
1291 config_screen_draw(screen, config, sysinfo);
1292 pad_refresh(screen);
1295 static int config_screen_post(struct nc_scr *scr)
1297 struct config_screen *screen = config_screen_from_scr(scr);
1299 /* We may have been posted after an auth action completed */
1301 screen->on_exit(screen->cui);
1305 if (screen->need_update) {
1306 config_screen_draw(screen, screen->cui->config,
1307 screen->cui->sysinfo);
1308 screen->need_update = false;
1310 widgetset_post(screen->widgetset);
1313 nc_scr_frame_draw(scr);
1314 if (screen->need_redraw) {
1315 redrawwin(scr->main_ncw);
1316 screen->need_redraw = false;
1318 wrefresh(screen->scr.main_ncw);
1319 pad_refresh(screen);
1323 static int config_screen_destroy(void *arg)
1325 struct config_screen *screen = arg;
1327 delwin(screen->pad);
1331 struct config_screen *config_screen_init(struct cui *cui,
1332 const struct config *config,
1333 const struct system_info *sysinfo,
1334 void (*on_exit)(struct cui *))
1336 struct config_screen *screen;
1338 screen = talloc_zero(cui, struct config_screen);
1339 talloc_set_destructor(screen, config_screen_destroy);
1340 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1341 cui, config_screen_process_key,
1342 config_screen_post, config_screen_unpost,
1343 config_screen_resize);
1346 screen->on_exit = on_exit;
1347 screen->need_redraw = false;
1348 screen->need_update = false;
1349 screen->label_x = 2;
1350 screen->field_x = 17;
1352 screen->ipmi_override = false;
1354 screen->scr.frame.ltitle = talloc_strdup(screen,
1355 _("Petitboot System Configuration"));
1356 screen->scr.frame.rtitle = NULL;
1357 screen->scr.frame.help = talloc_strdup(screen,
1358 _("tab=next, shift+tab=previous, x=exit, h=help"));
1359 nc_scr_frame_draw(&screen->scr);
1361 scrollok(screen->scr.sub_ncw, true);
1363 config_screen_draw(screen, config, sysinfo);