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;
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_button *ipmi_clear_b;
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_button *update_password_l;
123 struct nc_widget_label *net_override_l;
124 struct nc_widget_label *safe_mode;
125 struct nc_widget_button *ok_b;
126 struct nc_widget_button *help_b;
127 struct nc_widget_button *cancel_b;
131 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
133 struct config_screen *config_screen;
135 assert(scr->sig == pb_config_screen_sig);
136 config_screen = (struct config_screen *)
137 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
138 assert(config_screen->scr.sig == pb_config_screen_sig);
139 return config_screen;
142 static void pad_refresh(struct config_screen *screen)
144 int y, x, rows, cols;
146 getmaxyx(screen->scr.sub_ncw, rows, cols);
147 getbegyx(screen->scr.sub_ncw, y, x);
149 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
152 static void config_screen_process_key(struct nc_scr *scr, int key)
154 struct config_screen *screen = config_screen_from_scr(scr);
157 handled = widgetset_process_key(screen->widgetset, key);
166 screen->show_help = true;
172 screen->on_exit(screen->cui);
174 } else if (screen->show_help) {
175 screen->show_help = false;
176 screen->need_redraw = true;
177 cui_show_help(screen->cui, _("System Configuration"),
180 } else if (handled && (screen->cui->current == scr)) {
185 static void config_screen_resize(struct nc_scr *scr)
187 struct config_screen *screen = config_screen_from_scr(scr);
191 static int config_screen_unpost(struct nc_scr *scr)
193 struct config_screen *screen = config_screen_from_scr(scr);
194 widgetset_unpost(screen->widgetset);
198 struct nc_scr *config_screen_scr(struct config_screen *screen)
203 static int screen_process_form(struct config_screen *screen)
205 const struct system_info *sysinfo = screen->cui->sysinfo;
206 enum net_conf_type net_conf_type;
207 struct interface_config *iface;
210 struct config *config;
211 int i, n_boot_opts, rc;
212 unsigned int *order, idx;
215 config = config_copy(screen, screen->cui->config);
217 talloc_free(config->autoboot_opts);
218 config->n_autoboot_opts = 0;
220 n_boot_opts = widget_subset_get_order(config, &order,
221 screen->widgets.boot_order_f);
223 config->autoboot_enabled = widget_select_get_value(
224 screen->widgets.autoboot_f);
226 config->n_autoboot_opts = n_boot_opts;
227 config->autoboot_opts = talloc_array(config, struct autoboot_option,
230 for (i = 0; i < n_boot_opts; i++) {
231 if (order[i] < sysinfo->n_blockdevs) {
233 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
234 config->autoboot_opts[i].uuid = talloc_strdup(config,
235 sysinfo->blockdevs[order[i]]->uuid);
236 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
238 order[i] -= sysinfo->n_blockdevs;
239 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
240 mac_str(sysinfo->interfaces[order[i]]->hwaddr,
241 sysinfo->interfaces[order[i]]->hwaddr_size,
243 config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
246 order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
247 config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
248 config->autoboot_opts[i].type = order[i];
252 str = widget_textbox_get_value(screen->widgets.timeout_f);
256 x = strtoul(str, &end, 10);
257 if (!errno && end != str)
258 config->autoboot_timeout_sec = x;
261 net_conf_type = widget_select_get_value(screen->widgets.network_f);
263 /* if we don't have any network interfaces, prevent per-interface
265 if (sysinfo->n_interfaces == 0)
266 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
268 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
269 config->network.n_interfaces = 0;
272 iface = talloc_zero(config, struct interface_config);
273 config->network.n_interfaces = 1;
274 config->network.interfaces = talloc_array(config,
275 struct interface_config *, 1);
276 config->network.interfaces[0] = iface;
278 /* copy hwaddr (from the sysinfo interface data) to
279 * the configuration */
280 idx = widget_select_get_value(screen->widgets.iface_f);
281 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
282 sizeof(iface->hwaddr));
285 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
286 iface->method = CONFIG_METHOD_DHCP;
289 if (net_conf_type == NET_CONF_TYPE_STATIC) {
290 char *ip, *mask, *gateway, *url;
292 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
293 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
294 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
295 url = widget_textbox_get_value(screen->widgets.url_f);
297 if (!ip || !*ip || !mask || !*mask) {
298 screen->scr.frame.status =
299 _("No IP / mask values are set");
300 nc_scr_frame_draw(&screen->scr);
305 iface->method = CONFIG_METHOD_STATIC;
306 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
308 iface->static_config.gateway = talloc_strdup(iface, gateway);
309 iface->static_config.url = talloc_strdup(iface, url);
312 str = widget_textbox_get_value(screen->widgets.dns_f);
313 talloc_free(config->network.dns_servers);
314 config->network.dns_servers = NULL;
315 config->network.n_dns_servers = 0;
317 if (str && strlen(str)) {
322 dns = strtok_r(str, " \t", &tmp);
327 i = config->network.n_dns_servers++;
328 config->network.dns_servers = talloc_realloc(config,
329 config->network.dns_servers,
331 config->network.n_dns_servers);
332 config->network.dns_servers[i] =
333 talloc_strdup(config, dns);
339 talloc_free(config->http_proxy);
340 talloc_free(config->https_proxy);
341 str = widget_textbox_get_value(screen->widgets.http_proxy_f);
342 config->http_proxy = talloc_strdup(config, str);
343 str = widget_textbox_get_value(screen->widgets.https_proxy_f);
344 config->https_proxy = talloc_strdup(config, str);
346 allow_write = widget_select_get_value(screen->widgets.allow_write_f);
347 if (allow_write != config->allow_writes)
348 config->allow_writes = allow_write;
350 if (config->n_consoles && !config->manual_console) {
351 idx = widget_select_get_value(screen->widgets.boot_console_f);
352 if (!config->boot_console) {
353 config->boot_console = talloc_strdup(config,
354 config->consoles[idx]);
355 } else if (strncmp(config->boot_console, config->consoles[idx],
356 strlen(config->boot_console)) != 0) {
357 talloc_free(config->boot_console);
358 config->boot_console = talloc_strdup(config,
359 config->consoles[idx]);
363 config->safe_mode = false;
364 rc = cui_send_config(screen->cui, config);
368 pb_log("cui_send_config failed!\n");
370 pb_debug("config sent!\n");
375 static void config_screen_config_cb(struct nc_scr *scr)
377 struct config_screen *screen = config_screen_from_scr(scr);
379 if (!screen_process_form(screen))
384 static void password_click(void *arg)
386 struct config_screen *screen = arg;
388 screen->need_update = true;
389 cui_show_auth(screen->cui, screen->scr.main_ncw, true, NULL);
393 static void ok_click(void *arg)
395 struct config_screen *screen = arg;
397 if (discover_client_authenticated(screen->cui->client)) {
398 if (screen_process_form(screen))
399 /* errors are written to the status line, so we'll need
401 wrefresh(screen->scr.main_ncw);
405 cui_show_auth(screen->cui, screen->scr.main_ncw, false,
406 config_screen_config_cb);
410 static void help_click(void *arg)
412 struct config_screen *screen = arg;
413 screen->show_help = true;
416 static void cancel_click(void *arg)
418 struct config_screen *screen = arg;
422 static void ipmi_clear_click(void *arg)
424 struct config_screen *screen = arg;
425 struct config *config;
428 config = config_copy(screen, screen->cui->config);
429 config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
430 config->safe_mode = false;
432 rc = cui_send_config(screen->cui, config);
436 pb_log("cui_send_config failed!\n");
438 pb_debug("config sent!\n");
442 static int layout_pair(struct config_screen *screen, int y,
443 struct nc_widget_label *label,
444 struct nc_widget *field)
446 struct nc_widget *label_w = widget_label_base(label);
447 widget_move(label_w, y, screen->label_x);
448 widget_move(field, y, screen->field_x);
449 return max(widget_height(label_w), widget_height(field));
452 static void config_screen_layout_widgets(struct config_screen *screen)
454 struct nc_widget *wl, *wf, *wh;
459 /* currently, the longest label we have is the DNS-servers
460 * widget, so layout our screen based on that */
461 help_x = screen->field_x + 2 +
462 widget_width(widget_textbox_base(screen->widgets.dns_f));
464 wl = widget_label_base(screen->widgets.autoboot_l);
465 widget_set_visible(wl, true);
466 widget_move(wl, y, screen->label_x);
468 wf = widget_select_base(screen->widgets.autoboot_f);
469 widget_set_visible(wf, true);
470 widget_move(wf, y, screen->field_x);
471 y += widget_height(wf);
473 show = screen->autoboot_enabled;
478 wl = widget_label_base(screen->widgets.boot_order_l);
479 widget_set_visible(wl, show);
480 widget_move(wl, y, screen->label_x);
482 wf = widget_subset_base(screen->widgets.boot_order_f);
483 widget_move(wf, y, screen->field_x);
484 wl = widget_label_base(screen->widgets.boot_empty_l);
485 widget_move(wl, y, screen->field_x);
487 if (widget_subset_height(screen->widgets.boot_order_f)) {
488 widget_set_visible(wl, false);
489 widget_set_visible(wf, show);
490 y += show ? widget_height(wf) : 0;
492 widget_set_visible(wl, show);
493 widget_set_visible(wf, false);
499 widget_move(widget_button_base(screen->widgets.boot_add_b),
500 y++, screen->field_x);
501 widget_move(widget_button_base(screen->widgets.boot_any_b),
502 y++, screen->field_x);
503 widget_move(widget_button_base(screen->widgets.boot_none_b),
507 wf = widget_button_base(screen->widgets.boot_add_b);
508 if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
509 widget_set_visible(wf, true);
511 widget_set_visible(wf, false);
516 widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
517 widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
519 wf = widget_textbox_base(screen->widgets.timeout_f);
520 wl = widget_label_base(screen->widgets.timeout_l);
521 wh = widget_label_base(screen->widgets.timeout_help_l);
522 widget_set_visible(wl, screen->autoboot_enabled);
523 widget_set_visible(wf, screen->autoboot_enabled);
524 widget_set_visible(wh, screen->autoboot_enabled);
525 if (screen->autoboot_enabled) {
526 widget_set_visible(wh, screen->autoboot_enabled);
527 widget_move(wl, y, screen->label_x);
528 widget_move(wf, y, screen->field_x);
529 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
534 if (screen->ipmi_override) {
535 wl = widget_label_base(screen->widgets.ipmi_type_l);
536 widget_set_visible(wl, true);
537 widget_move(wl, y, screen->label_x);
540 wl = widget_label_base(screen->widgets.ipmi_clear_l);
541 wf = widget_button_base(screen->widgets.ipmi_clear_b);
542 widget_set_visible(wl, true);
543 widget_set_visible(wf, true);
544 widget_move(wl, y, screen->label_x);
545 widget_move(wf, y, screen->field_x);
551 y += layout_pair(screen, y, screen->widgets.network_l,
552 widget_select_base(screen->widgets.network_f));
556 /* conditionally show iface select */
557 wl = widget_label_base(screen->widgets.iface_l);
558 wf = widget_select_base(screen->widgets.iface_f);
560 show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
561 screen->net_conf_type == NET_CONF_TYPE_STATIC;
563 widget_set_visible(wl, show);
564 widget_set_visible(wf, show);
567 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
569 /* conditionally show static IP params */
570 show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
572 wl = widget_label_base(screen->widgets.ip_addr_l);
573 wf = widget_textbox_base(screen->widgets.ip_addr_f);
574 widget_set_visible(wl, show);
575 widget_set_visible(wf, show);
576 x = screen->field_x + widget_width(wf) + 1;
579 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
581 wl = widget_label_base(screen->widgets.ip_mask_l);
582 wf = widget_textbox_base(screen->widgets.ip_mask_f);
583 widget_set_visible(wl, show);
584 widget_set_visible(wf, show);
587 widget_move(wl, y, x);
588 widget_move(wf, y, x + 2);
591 /* help for IP/mask */
592 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
593 widget_set_visible(wh, show);
595 widget_move(wh, y, help_x);
599 wl = widget_label_base(screen->widgets.gateway_l);
600 wf = widget_textbox_base(screen->widgets.gateway_f);
601 wh = widget_label_base(screen->widgets.gateway_help_l);
602 widget_set_visible(wl, show);
603 widget_set_visible(wf, show);
604 widget_set_visible(wh, show);
607 layout_pair(screen, y, screen->widgets.gateway_l, wf);
608 widget_move(wh, y, help_x);
612 wl = widget_label_base(screen->widgets.url_l);
613 wf = widget_textbox_base(screen->widgets.url_f);
614 wh = widget_label_base(screen->widgets.url_help_l);
615 widget_set_visible(wl, show);
616 widget_set_visible(wf, show);
617 widget_set_visible(wh, show);
620 layout_pair(screen, y, screen->widgets.url_l, wf);
621 widget_move(wh, y, help_x);
625 wh = widget_label_base(screen->widgets.dns_help_l);
626 layout_pair(screen, y, screen->widgets.dns_l,
627 widget_textbox_base(screen->widgets.dns_f));
628 widget_move(wh, y, help_x);
631 /* we show the DNS/DHCP help if we're configuring DHCP */
632 show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
633 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
634 widget_set_visible(wl, show);
636 widget_move(wl, y, screen->field_x);
640 wf = widget_textbox_base(screen->widgets.http_proxy_f);
641 layout_pair(screen, y, screen->widgets.http_proxy_l, wf);
643 wf = widget_textbox_base(screen->widgets.https_proxy_f);
644 layout_pair(screen, y, screen->widgets.https_proxy_l, wf);
649 layout_pair(screen, y, screen->widgets.allow_write_l,
650 widget_select_base(screen->widgets.allow_write_f));
651 y += widget_height(widget_select_base(screen->widgets.allow_write_f));
655 if (screen->widgets.manual_console_l) {
656 layout_pair(screen, y++, screen->widgets.boot_console_l,
657 widget_label_base(screen->widgets.manual_console_l));
658 widget_move(widget_label_base(screen->widgets.current_console_l),
660 widget_set_visible(widget_select_base(
661 screen->widgets.boot_console_f), false);
663 } else if (widget_height(widget_select_base(screen->widgets.boot_console_f))) {
664 layout_pair(screen, y, screen->widgets.boot_console_l,
665 widget_select_base(screen->widgets.boot_console_f));
666 y += widget_height(widget_select_base(screen->widgets.boot_console_f));
667 widget_move(widget_label_base(screen->widgets.current_console_l),
671 widget_set_visible(widget_label_base(
672 screen->widgets.boot_console_l), false);
673 widget_set_visible(widget_select_base(
674 screen->widgets.boot_console_f), false);
675 widget_set_visible(widget_label_base(
676 screen->widgets.current_console_l), false);
680 widget_move(widget_button_base(screen->widgets.update_password_l),
685 if (screen->net_override) {
686 widget_move(widget_label_base(screen->widgets.net_override_l),
688 widget_set_visible(widget_label_base(screen->widgets.net_override_l),
693 if (screen->cui->config->safe_mode) {
694 widget_move(widget_label_base(screen->widgets.safe_mode),
696 widget_set_visible(widget_label_base(screen->widgets.safe_mode),
701 widget_move(widget_button_base(screen->widgets.ok_b),
703 widget_move(widget_button_base(screen->widgets.help_b),
704 y, screen->field_x + 14);
705 widget_move(widget_button_base(screen->widgets.cancel_b),
706 y, screen->field_x + 28);
709 static void config_screen_network_change(void *arg, int value)
711 struct config_screen *screen = arg;
712 screen->net_conf_type = value;
713 widgetset_unpost(screen->widgetset);
714 config_screen_layout_widgets(screen);
715 widgetset_post(screen->widgetset);
718 static void config_screen_boot_order_change(void *arg, int value)
721 struct config_screen *screen = arg;
722 widgetset_unpost(screen->widgetset);
723 config_screen_layout_widgets(screen);
724 widgetset_post(screen->widgetset);
727 static void config_screen_autoboot_change(void *arg, int value)
729 struct config_screen *screen = arg;
730 screen->autoboot_enabled = !!value;
731 widgetset_unpost(screen->widgetset);
732 config_screen_layout_widgets(screen);
733 widgetset_post(screen->widgetset);
736 static void config_screen_add_device(void *arg)
738 struct config_screen *screen = arg;
740 cui_show_subset(screen->cui, _("Select a boot device to add"),
741 screen->widgets.boot_order_f);
744 static void config_screen_autoboot_none(void *arg)
746 struct config_screen *screen = arg;
747 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
749 widget_subset_clear_active(subset);
751 widgetset_unpost(screen->widgetset);
752 config_screen_layout_widgets(screen);
753 widgetset_post(screen->widgetset);
756 static void config_screen_autoboot_any(void *arg)
758 struct config_screen *screen = arg;
759 const struct system_info *sysinfo = screen->cui->sysinfo;
760 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
763 widget_subset_clear_active(subset);
765 idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
767 widget_subset_make_active(screen->widgets.boot_order_f, idx);
769 screen->autoboot_enabled = true;
771 widgetset_unpost(screen->widgetset);
772 config_screen_layout_widgets(screen);
773 widgetset_post(screen->widgetset);
776 static void config_screen_update_subset(void *arg,
777 struct nc_widget_subset *subset, int idx)
779 struct config_screen *screen = arg;
782 widget_subset_make_active(subset, idx);
783 if (!screen->autoboot_enabled)
784 screen->autoboot_enabled = true;
785 config_screen_layout_widgets(screen);
788 static struct interface_config *first_active_interface(
789 const struct config *config)
793 for (i = 0; i < config->network.n_interfaces; i++) {
794 if (config->network.interfaces[i]->ignore)
796 return config->network.interfaces[i];
801 static enum net_conf_type find_net_conf_type(const struct config *config)
803 struct interface_config *ifcfg;
805 ifcfg = first_active_interface(config);
808 return NET_CONF_TYPE_DHCP_ALL;
810 else if (ifcfg->method == CONFIG_METHOD_DHCP)
811 return NET_CONF_TYPE_DHCP_ONE;
813 else if (ifcfg->method == CONFIG_METHOD_STATIC)
814 return NET_CONF_TYPE_STATIC;
817 return NET_CONF_TYPE_DHCP_ALL;
820 static void config_screen_setup_empty(struct config_screen *screen)
822 widget_new_label(screen->widgetset, 2, screen->field_x,
823 _("Waiting for configuration data..."));
824 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
825 4, screen->field_x, 9, _("Cancel"),
826 cancel_click, screen);
829 static int find_autoboot_idx(const struct system_info *sysinfo,
830 struct autoboot_option *opt)
834 if (opt->boot_type == BOOT_DEVICE_TYPE)
835 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
837 for (i = 0; i < sysinfo->n_blockdevs; i++) {
838 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
842 for (i = 0; i < sysinfo->n_interfaces; i++) {
843 struct interface_info *info = sysinfo->interfaces[i];
846 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
848 if (!strcmp(mac, opt->uuid))
849 return sysinfo->n_blockdevs + i;
855 static void config_screen_setup_widgets(struct config_screen *screen,
856 const struct config *config,
857 const struct system_info *sysinfo)
859 struct nc_widgetset *set = screen->widgetset;
860 struct interface_config *ifcfg;
861 char *str, *ip, *mask, *gw, *url, *tty, *label;
862 enum net_conf_type type;
864 int add_len, clear_len, any_len, min_len = 20;
867 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
870 type = screen->net_conf_type;
871 ifcfg = first_active_interface(config);
873 screen->autoboot_enabled = config->autoboot_enabled;
875 screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
877 screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
878 COLS - screen->field_x - 1);
880 widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
881 !screen->autoboot_enabled);
882 widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
883 screen->autoboot_enabled);
885 widget_select_on_change(screen->widgets.autoboot_f,
886 config_screen_autoboot_change, screen);
888 add_len = max(min_len, strncols(_("Add Device")));
889 clear_len = max(min_len, strncols(_("Clear")));
890 any_len = max(min_len, strncols(_("Clear & Boot Any")));
892 screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
894 config_screen_add_device, screen);
896 screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
898 config_screen_autoboot_none, screen);
900 screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
901 _("Clear & Boot Any"),
902 config_screen_autoboot_any, screen);
904 screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
906 screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
907 COLS - screen->field_x,
908 config_screen_update_subset);
909 screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
912 widget_subset_on_change(screen->widgets.boot_order_f,
913 config_screen_boot_order_change, screen);
915 for (i = 0; i < sysinfo->n_blockdevs; i++) {
916 struct blockdev_info *bd = sysinfo->blockdevs[i];
918 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
921 widget_subset_add_option(screen->widgets.boot_order_f, label);
924 for (i = 0; i < sysinfo->n_interfaces; i++) {
925 struct interface_info *info = sysinfo->interfaces[i];
928 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
930 label = talloc_asprintf(screen, _("net: %s [mac: %s]"),
933 widget_subset_add_option(screen->widgets.boot_order_f, label);
936 for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
938 if (i == DEVICE_TYPE_ANY)
939 label = talloc_asprintf(screen, _("Any Device"));
941 label = talloc_asprintf(screen, _("Any %s device"),
942 device_type_display_name(i));
944 widget_subset_add_option(screen->widgets.boot_order_f, label);
947 for (i = 0; i < config->n_autoboot_opts; i++) {
948 struct autoboot_option *opt = &config->autoboot_opts[i];
951 idx = find_autoboot_idx(sysinfo, opt);
954 widget_subset_make_active(screen->widgets.boot_order_f,
957 if (opt->boot_type == BOOT_DEVICE_TYPE)
958 pb_log("%s: Unknown autoboot option: %d\n",
959 __func__, opt->type);
961 pb_log("%s: Unknown autoboot UUID: %s\n",
962 __func__, opt->uuid);
967 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
968 screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
969 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
970 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
973 widget_textbox_set_fixed_size(screen->widgets.timeout_f);
974 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
976 if (config->ipmi_bootdev) {
977 label = talloc_asprintf(screen,
978 _("%s IPMI boot option: %s"),
979 config->ipmi_bootdev_persistent ?
980 _("Persistent") : _("Temporary"),
981 ipmi_bootdev_display_name(config->ipmi_bootdev));
982 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
984 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
986 screen->widgets.ipmi_clear_b = widget_new_button(set, 0, 0,
987 strncols(_("Clear IPMI override now")) + 10,
988 _("Clear IPMI override now"),
989 ipmi_clear_click, screen);
990 screen->ipmi_override = true;
993 screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
994 screen->widgets.network_f = widget_new_select(set, 0, 0,
995 COLS - screen->field_x - 1);
997 widget_select_add_option(screen->widgets.network_f,
998 NET_CONF_TYPE_DHCP_ALL,
999 _("DHCP on all active interfaces"),
1000 type == NET_CONF_TYPE_DHCP_ALL);
1001 widget_select_add_option(screen->widgets.network_f,
1002 NET_CONF_TYPE_DHCP_ONE,
1003 _("DHCP on a specific interface"),
1004 type == NET_CONF_TYPE_DHCP_ONE);
1005 widget_select_add_option(screen->widgets.network_f,
1006 NET_CONF_TYPE_STATIC,
1007 _("Static IP configuration"),
1008 type == NET_CONF_TYPE_STATIC);
1010 widget_select_on_change(screen->widgets.network_f,
1011 config_screen_network_change, screen);
1013 screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
1014 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
1016 for (i = 0; i < sysinfo->n_interfaces; i++) {
1017 struct interface_info *info = sysinfo->interfaces[i];
1018 char str[50], mac[20];
1021 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
1022 sizeof(ifcfg->hwaddr));
1024 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
1025 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
1026 info->link ? _("link up") : _("link down"));
1028 widget_select_add_option(screen->widgets.iface_f,
1029 i, str, is_default);
1032 url = gw = ip = mask = NULL;
1033 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
1036 str = talloc_strdup(screen, ifcfg->static_config.address);
1037 sep = strchr(str, '/');
1044 gw = ifcfg->static_config.gateway;
1045 url = ifcfg->static_config.url;
1048 screen->net_override = ifcfg && ifcfg->override;
1049 if (screen->net_override) {
1050 screen->widgets.net_override_l = widget_new_label(set, 0, 0,
1051 _("Network Override Active! 'OK' will overwrite interface config"));
1054 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
1055 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
1056 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
1057 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
1058 screen->widgets.ip_addr_mask_help_l =
1059 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
1061 widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
1062 widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
1063 widget_textbox_set_validator_ip(screen->widgets.ip_addr_f);
1064 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 127);
1066 screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
1067 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
1068 screen->widgets.gateway_help_l =
1069 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
1071 widget_textbox_set_fixed_size(screen->widgets.gateway_f);
1072 widget_textbox_set_validator_ip(screen->widgets.gateway_f);
1074 screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
1075 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
1076 widget_textbox_set_validator_url(screen->widgets.url_f);
1077 screen->widgets.url_help_l =
1078 widget_new_label(set, 0, 0, _("(eg. tftp://)"));
1080 str = talloc_strdup(screen, "");
1081 for (i = 0; i < config->network.n_dns_servers; i++) {
1082 str = talloc_asprintf_append(str, "%s%s",
1083 (i == 0) ? "" : " ",
1084 config->network.dns_servers[i]);
1087 screen->widgets.dns_l = widget_new_label(set, 0, 0,
1088 _("DNS Server(s):"));
1089 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
1090 screen->widgets.dns_help_l =
1091 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
1093 widget_textbox_set_validator_ip_multi(screen->widgets.dns_f);
1095 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
1096 _("(if not provided by DHCP server)"));
1098 screen->widgets.http_proxy_l = widget_new_label(set, 0, 0,
1100 screen->widgets.http_proxy_f = widget_new_textbox(set, 0, 0, 32,
1101 config->http_proxy);
1102 screen->widgets.https_proxy_l = widget_new_label(set, 0, 0,
1104 screen->widgets.https_proxy_f = widget_new_textbox(set, 0, 0, 32,
1105 config->https_proxy);
1107 if (config->safe_mode)
1108 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
1109 _("Selecting 'OK' will exit safe mode"));
1111 screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
1113 screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
1114 COLS - screen->field_x - 1);
1116 widget_select_add_option(screen->widgets.allow_write_f, 0,
1117 _("Prevent all writes to disk"),
1118 !config->allow_writes);
1120 widget_select_add_option(screen->widgets.allow_write_f, 1,
1121 _("Allow bootloader scripts to modify disks"),
1122 config->allow_writes);
1124 screen->widgets.boot_console_l = widget_new_label(set, 0, 0,
1125 _("Boot console:"));
1126 screen->widgets.boot_console_f = widget_new_select(set, 0, 0,
1127 COLS - screen->field_x - 1);
1129 for (i = 0; i < config->n_consoles; i++){
1130 found = config->boot_console &&
1131 strncmp(config->boot_console, config->consoles[i],
1132 strlen(config->boot_console)) == 0;
1133 widget_select_add_option(screen->widgets.boot_console_f, i,
1134 config->consoles[i], found);
1137 if (config->manual_console) {
1138 label = talloc_asprintf(screen, _("Manually set: '%s'"),
1139 config->boot_console);
1140 screen->widgets.manual_console_l = widget_new_label(set, 0, 0, label);
1143 tty = talloc_asprintf(screen, _("Current interface: %s"),
1144 ttyname(STDIN_FILENO));
1145 screen->widgets.current_console_l = widget_new_label(set, 0 , 0, tty);
1147 #ifdef CRYPT_SUPPORT
1148 screen->widgets.update_password_l = widget_new_button(set, 0, 0, 30,
1149 _("Update system password"), password_click, screen);
1152 screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
1154 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
1155 help_click, screen);
1156 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
1157 cancel_click, screen);
1160 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
1162 struct config_screen *screen = arg;
1163 int w_y, w_height, w_focus, s_max, adjust;
1165 w_height = widget_height(widget);
1166 w_focus = widget_focus_y(widget);
1167 w_y = widget_y(widget) + w_focus;
1168 s_max = getmaxy(screen->scr.sub_ncw) - 1;
1170 if (w_y < screen->scroll_y)
1171 screen->scroll_y = w_y;
1173 else if (w_y + screen->scroll_y + 1 > s_max) {
1174 /* Fit as much of the widget into the screen as possible */
1175 adjust = min(s_max - 1, w_height - w_focus);
1176 if (w_y + adjust >= screen->scroll_y + s_max)
1177 screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
1181 pad_refresh(screen);
1184 static void config_screen_draw(struct config_screen *screen,
1185 const struct config *config,
1186 const struct system_info *sysinfo)
1188 bool repost = false;
1191 /* The size of the pad we'll need depends on the number of interfaces.
1193 * We use N_FIELDS (which is quite conservative, as some fields share
1194 * a line) as a base, then:
1195 * - add 6 (as the network select & boot device select fields take 3
1197 * - add n_interfaces for every field in the network select field, and
1198 * - add (n_blockdevs + n_interfaces) for every field in the boot device
1201 height = N_FIELDS + 6;
1203 height += sysinfo->n_interfaces;
1204 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
1206 if (!screen->pad || getmaxy(screen->pad) < height) {
1208 delwin(screen->pad);
1209 screen->pad = newpad(height, COLS + 10);
1212 if (screen->widgetset) {
1213 widgetset_unpost(screen->widgetset);
1214 talloc_free(screen->widgetset);
1218 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
1220 widgetset_set_widget_focus(screen->widgetset,
1221 config_screen_widget_focus, screen);
1223 if (!config || !sysinfo) {
1224 config_screen_setup_empty(screen);
1226 screen->net_conf_type = find_net_conf_type(config);
1227 config_screen_setup_widgets(screen, config, sysinfo);
1228 config_screen_layout_widgets(screen);
1232 widgetset_post(screen->widgetset);
1235 void config_screen_update(struct config_screen *screen,
1236 const struct config *config,
1237 const struct system_info *sysinfo)
1239 if (screen->cui->current != config_screen_scr(screen)) {
1240 screen->need_update = true;
1244 config_screen_draw(screen, config, sysinfo);
1245 pad_refresh(screen);
1248 static int config_screen_post(struct nc_scr *scr)
1250 struct config_screen *screen = config_screen_from_scr(scr);
1252 /* We may have been posted after an auth action completed */
1254 screen->on_exit(screen->cui);
1258 if (screen->need_update) {
1259 config_screen_draw(screen, screen->cui->config,
1260 screen->cui->sysinfo);
1261 screen->need_update = false;
1263 widgetset_post(screen->widgetset);
1266 nc_scr_frame_draw(scr);
1267 if (screen->need_redraw) {
1268 redrawwin(scr->main_ncw);
1269 screen->need_redraw = false;
1271 wrefresh(screen->scr.main_ncw);
1272 pad_refresh(screen);
1276 static int config_screen_destroy(void *arg)
1278 struct config_screen *screen = arg;
1280 delwin(screen->pad);
1284 struct config_screen *config_screen_init(struct cui *cui,
1285 const struct config *config,
1286 const struct system_info *sysinfo,
1287 void (*on_exit)(struct cui *))
1289 struct config_screen *screen;
1291 screen = talloc_zero(cui, struct config_screen);
1292 talloc_set_destructor(screen, config_screen_destroy);
1293 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1294 cui, config_screen_process_key,
1295 config_screen_post, config_screen_unpost,
1296 config_screen_resize);
1299 screen->on_exit = on_exit;
1300 screen->need_redraw = false;
1301 screen->need_update = false;
1302 screen->label_x = 2;
1303 screen->field_x = 17;
1305 screen->ipmi_override = false;
1307 screen->scr.frame.ltitle = talloc_strdup(screen,
1308 _("Petitboot System Configuration"));
1309 screen->scr.frame.rtitle = NULL;
1310 screen->scr.frame.help = talloc_strdup(screen,
1311 _("tab=next, shift+tab=previous, x=exit, h=help"));
1312 nc_scr_frame_draw(&screen->scr);
1314 scrollok(screen->scr.sub_ncw, true);
1316 config_screen_draw(screen, config, sysinfo);