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;
72 struct nc_widget_label *autoboot_l;
73 struct nc_widget_select *autoboot_f;
74 struct nc_widget_label *boot_order_l;
75 struct nc_widget_subset *boot_order_f;
76 struct nc_widget_label *boot_empty_l;
77 struct nc_widget_button *boot_add_b;
78 struct nc_widget_button *boot_none_b;
79 struct nc_widget_button *boot_any_b;
80 struct nc_widget_textbox *timeout_f;
81 struct nc_widget_label *timeout_l;
82 struct nc_widget_label *timeout_help_l;
84 struct nc_widget_label *ipmi_type_l;
85 struct nc_widget_label *ipmi_clear_l;
86 struct nc_widget_checkbox *ipmi_clear_cb;
88 struct nc_widget_label *network_l;
89 struct nc_widget_select *network_f;
91 struct nc_widget_label *iface_l;
92 struct nc_widget_select *iface_f;
93 struct nc_widget_label *ip_addr_l;
94 struct nc_widget_textbox *ip_addr_f;
95 struct nc_widget_label *ip_mask_l;
96 struct nc_widget_textbox *ip_mask_f;
97 struct nc_widget_label *ip_addr_mask_help_l;
98 struct nc_widget_label *gateway_l;
99 struct nc_widget_textbox *gateway_f;
100 struct nc_widget_label *gateway_help_l;
101 struct nc_widget_label *url_l;
102 struct nc_widget_textbox *url_f;
103 struct nc_widget_label *url_help_l;
104 struct nc_widget_label *dns_l;
105 struct nc_widget_textbox *dns_f;
106 struct nc_widget_label *dns_dhcp_help_l;
107 struct nc_widget_label *dns_help_l;
109 struct nc_widget_label *allow_write_l;
110 struct nc_widget_select *allow_write_f;
111 struct nc_widget_label *boot_tty_l;
112 struct nc_widget_select *boot_tty_f;
113 struct nc_widget_label *current_tty_l;
115 struct nc_widget_label *safe_mode;
116 struct nc_widget_button *ok_b;
117 struct nc_widget_button *help_b;
118 struct nc_widget_button *cancel_b;
122 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
124 struct config_screen *config_screen;
126 assert(scr->sig == pb_config_screen_sig);
127 config_screen = (struct config_screen *)
128 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
129 assert(config_screen->scr.sig == pb_config_screen_sig);
130 return config_screen;
133 static void pad_refresh(struct config_screen *screen)
135 int y, x, rows, cols;
137 getmaxyx(screen->scr.sub_ncw, rows, cols);
138 getbegyx(screen->scr.sub_ncw, y, x);
140 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
143 static void config_screen_process_key(struct nc_scr *scr, int key)
145 struct config_screen *screen = config_screen_from_scr(scr);
148 handled = widgetset_process_key(screen->widgetset, key);
157 screen->show_help = true;
163 screen->on_exit(screen->cui);
165 } else if (screen->show_help) {
166 screen->show_help = false;
167 screen->need_redraw = true;
168 cui_show_help(screen->cui, _("System Configuration"),
171 } else if (handled && !screen->show_subset) {
176 static void config_screen_resize(struct nc_scr *scr)
178 struct config_screen *screen = config_screen_from_scr(scr);
182 static int config_screen_unpost(struct nc_scr *scr)
184 struct config_screen *screen = config_screen_from_scr(scr);
185 widgetset_unpost(screen->widgetset);
189 struct nc_scr *config_screen_scr(struct config_screen *screen)
194 static int screen_process_form(struct config_screen *screen)
196 const struct system_info *sysinfo = screen->cui->sysinfo;
197 enum net_conf_type net_conf_type;
198 struct interface_config *iface;
199 bool allow_write, autoboot;
201 struct config *config;
202 int i, n_boot_opts, rc, idx;
203 unsigned int *order, tty;
206 config = config_copy(screen, screen->cui->config);
208 talloc_free(config->autoboot_opts);
209 config->n_autoboot_opts = 0;
211 n_boot_opts = widget_subset_get_order(config, &order,
212 screen->widgets.boot_order_f);
214 autoboot = widget_select_get_value(screen->widgets.autoboot_f);
215 config->autoboot_enabled = autoboot && n_boot_opts;
217 config->n_autoboot_opts = n_boot_opts;
218 config->autoboot_opts = talloc_array(config, struct autoboot_option,
221 for (i = 0; i < n_boot_opts; i++) {
222 if (order[i] < sysinfo->n_blockdevs) {
224 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
225 config->autoboot_opts[i].uuid = talloc_strdup(config,
226 sysinfo->blockdevs[order[i]]->uuid);
227 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
229 order[i] -= sysinfo->n_blockdevs;
230 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
231 mac_str(sysinfo->interfaces[order[i]]->hwaddr,
232 sysinfo->interfaces[order[i]]->hwaddr_size,
234 config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
237 order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
238 config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
239 config->autoboot_opts[i].type = order[i];
243 str = widget_textbox_get_value(screen->widgets.timeout_f);
247 x = strtoul(str, &end, 10);
248 if (!errno && end != str)
249 config->autoboot_timeout_sec = x;
252 if (screen->ipmi_override)
253 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
254 config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
257 net_conf_type = widget_select_get_value(screen->widgets.network_f);
259 /* if we don't have any network interfaces, prevent per-interface
261 if (sysinfo->n_interfaces == 0)
262 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
264 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
265 config->network.n_interfaces = 0;
268 iface = talloc_zero(config, struct interface_config);
269 config->network.n_interfaces = 1;
270 config->network.interfaces = talloc_array(config,
271 struct interface_config *, 1);
272 config->network.interfaces[0] = iface;
274 /* copy hwaddr (from the sysinfo interface data) to
275 * the configuration */
276 idx = widget_select_get_value(screen->widgets.iface_f);
277 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
278 sizeof(iface->hwaddr));
281 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
282 iface->method = CONFIG_METHOD_DHCP;
285 if (net_conf_type == NET_CONF_TYPE_STATIC) {
286 char *ip, *mask, *gateway, *url;
288 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
289 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
290 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
291 url = widget_textbox_get_value(screen->widgets.url_f);
293 if (!ip || !*ip || !mask || !*mask) {
294 screen->scr.frame.status =
295 _("No IP / mask values are set");
296 nc_scr_frame_draw(&screen->scr);
301 iface->method = CONFIG_METHOD_STATIC;
302 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
304 iface->static_config.gateway = talloc_strdup(iface, gateway);
305 iface->static_config.url = talloc_strdup(iface, url);
308 str = widget_textbox_get_value(screen->widgets.dns_f);
309 talloc_free(config->network.dns_servers);
310 config->network.dns_servers = NULL;
311 config->network.n_dns_servers = 0;
313 if (str && strlen(str)) {
318 dns = strtok_r(str, " \t", &tmp);
323 i = config->network.n_dns_servers++;
324 config->network.dns_servers = talloc_realloc(config,
325 config->network.dns_servers,
327 config->network.n_dns_servers);
328 config->network.dns_servers[i] =
329 talloc_strdup(config, dns);
335 allow_write = widget_select_get_value(screen->widgets.allow_write_f);
336 if (allow_write != config->allow_writes)
337 config->allow_writes = allow_write;
340 tty = widget_select_get_value(screen->widgets.boot_tty_f);
341 if (!config->boot_tty) {
342 config->boot_tty = talloc_strdup(config,
343 config->tty_list[tty]);
344 } else if (strncmp(config->boot_tty, config->tty_list[tty],
345 strlen(config->boot_tty)) != 0) {
346 talloc_free(config->boot_tty);
347 config->boot_tty = talloc_strdup(config,
348 config->tty_list[tty]);
352 config->safe_mode = false;
353 rc = cui_send_config(screen->cui, config);
357 pb_log("cui_send_config failed!\n");
359 pb_debug("config sent!\n");
364 static void ok_click(void *arg)
366 struct config_screen *screen = arg;
367 if (screen_process_form(screen))
368 /* errors are written to the status line, so we'll need
370 wrefresh(screen->scr.main_ncw);
375 static void help_click(void *arg)
377 struct config_screen *screen = arg;
378 screen->show_help = true;
381 static void cancel_click(void *arg)
383 struct config_screen *screen = arg;
387 static int layout_pair(struct config_screen *screen, int y,
388 struct nc_widget_label *label,
389 struct nc_widget *field)
391 struct nc_widget *label_w = widget_label_base(label);
392 widget_move(label_w, y, screen->label_x);
393 widget_move(field, y, screen->field_x);
394 return max(widget_height(label_w), widget_height(field));
397 static void config_screen_layout_widgets(struct config_screen *screen)
399 struct nc_widget *wl, *wf, *wh;
404 /* currently, the longest label we have is the DNS-servers
405 * widget, so layout our screen based on that */
406 help_x = screen->field_x + 2 +
407 widget_width(widget_textbox_base(screen->widgets.dns_f));
409 wl = widget_label_base(screen->widgets.autoboot_l);
410 widget_set_visible(wl, true);
411 widget_move(wl, y, screen->label_x);
413 wf = widget_select_base(screen->widgets.autoboot_f);
414 widget_set_visible(wf, true);
415 widget_move(wf, y, screen->field_x);
416 y += widget_height(wf);
418 show = screen->autoboot_enabled;
423 wl = widget_label_base(screen->widgets.boot_order_l);
424 widget_set_visible(wl, show);
425 widget_move(wl, y, screen->label_x);
427 wf = widget_subset_base(screen->widgets.boot_order_f);
428 widget_move(wf, y, screen->field_x);
429 wl = widget_label_base(screen->widgets.boot_empty_l);
430 widget_move(wl, y, screen->field_x);
432 if (widget_subset_height(screen->widgets.boot_order_f)) {
433 widget_set_visible(wl, false);
434 widget_set_visible(wf, show);
435 y += show ? widget_height(wf) : 0;
437 widget_set_visible(wl, show);
438 widget_set_visible(wf, false);
444 widget_move(widget_button_base(screen->widgets.boot_add_b),
445 y++, screen->field_x);
446 widget_move(widget_button_base(screen->widgets.boot_any_b),
447 y++, screen->field_x);
448 widget_move(widget_button_base(screen->widgets.boot_none_b),
452 wf = widget_button_base(screen->widgets.boot_add_b);
453 if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
454 widget_set_visible(wf, true);
456 widget_set_visible(wf, false);
461 widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
462 widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
464 wf = widget_textbox_base(screen->widgets.timeout_f);
465 wl = widget_label_base(screen->widgets.timeout_l);
466 wh = widget_label_base(screen->widgets.timeout_help_l);
467 widget_set_visible(wl, screen->autoboot_enabled);
468 widget_set_visible(wf, screen->autoboot_enabled);
469 widget_set_visible(wh, screen->autoboot_enabled);
470 if (screen->autoboot_enabled) {
471 widget_set_visible(wh, screen->autoboot_enabled);
472 widget_move(wl, y, screen->label_x);
473 widget_move(wf, y, screen->field_x);
474 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
478 if (screen->ipmi_override) {
479 wl = widget_label_base(screen->widgets.ipmi_type_l);
480 widget_set_visible(wl, true);
481 widget_move(wl, y, screen->label_x);
484 wl = widget_label_base(screen->widgets.ipmi_clear_l);
485 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
486 widget_set_visible(wl, true);
487 widget_set_visible(wf, true);
488 widget_move(wl, y, screen->label_x);
489 widget_move(wf, y, screen->field_x);
495 y += layout_pair(screen, y, screen->widgets.network_l,
496 widget_select_base(screen->widgets.network_f));
500 /* conditionally show iface select */
501 wl = widget_label_base(screen->widgets.iface_l);
502 wf = widget_select_base(screen->widgets.iface_f);
504 show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
505 screen->net_conf_type == NET_CONF_TYPE_STATIC;
507 widget_set_visible(wl, show);
508 widget_set_visible(wf, show);
511 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
513 /* conditionally show static IP params */
514 show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
516 wl = widget_label_base(screen->widgets.ip_addr_l);
517 wf = widget_textbox_base(screen->widgets.ip_addr_f);
518 widget_set_visible(wl, show);
519 widget_set_visible(wf, show);
520 x = screen->field_x + widget_width(wf) + 1;
523 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
525 wl = widget_label_base(screen->widgets.ip_mask_l);
526 wf = widget_textbox_base(screen->widgets.ip_mask_f);
527 widget_set_visible(wl, show);
528 widget_set_visible(wf, show);
531 widget_move(wl, y, x);
532 widget_move(wf, y, x + 2);
535 /* help for IP/mask */
536 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
537 widget_set_visible(wh, show);
539 widget_move(wh, y, help_x);
543 wl = widget_label_base(screen->widgets.gateway_l);
544 wf = widget_textbox_base(screen->widgets.gateway_f);
545 wh = widget_label_base(screen->widgets.gateway_help_l);
546 widget_set_visible(wl, show);
547 widget_set_visible(wf, show);
548 widget_set_visible(wh, show);
551 layout_pair(screen, y, screen->widgets.gateway_l, wf);
552 widget_move(wh, y, help_x);
556 wl = widget_label_base(screen->widgets.url_l);
557 wf = widget_textbox_base(screen->widgets.url_f);
558 wh = widget_label_base(screen->widgets.url_help_l);
559 widget_set_visible(wl, show);
560 widget_set_visible(wf, show);
561 widget_set_visible(wh, show);
564 layout_pair(screen, y, screen->widgets.url_l, wf);
565 widget_move(wh, y, help_x);
569 wh = widget_label_base(screen->widgets.dns_help_l);
570 layout_pair(screen, y, screen->widgets.dns_l,
571 widget_textbox_base(screen->widgets.dns_f));
572 widget_move(wh, y, help_x);
575 /* we show the DNS/DHCP help if we're configuring DHCP */
576 show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
577 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
578 widget_set_visible(wl, show);
580 widget_move(wl, y, screen->field_x);
586 show = screen->cui->config->safe_mode;
588 widget_move(widget_label_base(screen->widgets.safe_mode),
593 layout_pair(screen, y, screen->widgets.allow_write_l,
594 widget_select_base(screen->widgets.allow_write_f));
595 y += widget_height(widget_select_base(screen->widgets.allow_write_f));
599 if (widget_height(widget_select_base(screen->widgets.boot_tty_f))) {
600 layout_pair(screen, y, screen->widgets.boot_tty_l,
601 widget_select_base(screen->widgets.boot_tty_f));
602 y += widget_height(widget_select_base(screen->widgets.boot_tty_f));
603 widget_move(widget_label_base(screen->widgets.current_tty_l),
607 widget_set_visible(widget_label_base(
608 screen->widgets.boot_tty_l), false);
609 widget_set_visible(widget_select_base(
610 screen->widgets.boot_tty_f), false);
611 widget_set_visible(widget_label_base(
612 screen->widgets.current_tty_l), false);
615 widget_move(widget_button_base(screen->widgets.ok_b),
617 widget_move(widget_button_base(screen->widgets.help_b),
618 y, screen->field_x + 14);
619 widget_move(widget_button_base(screen->widgets.cancel_b),
620 y, screen->field_x + 28);
623 static void config_screen_network_change(void *arg, int value)
625 struct config_screen *screen = arg;
626 screen->net_conf_type = value;
627 widgetset_unpost(screen->widgetset);
628 config_screen_layout_widgets(screen);
629 widgetset_post(screen->widgetset);
632 static void config_screen_boot_order_change(void *arg, int value)
635 struct config_screen *screen = arg;
636 widgetset_unpost(screen->widgetset);
637 config_screen_layout_widgets(screen);
638 widgetset_post(screen->widgetset);
641 static void config_screen_autoboot_change(void *arg, int value)
643 struct config_screen *screen = arg;
644 screen->autoboot_enabled = !!value;
645 widgetset_unpost(screen->widgetset);
646 config_screen_layout_widgets(screen);
647 widgetset_post(screen->widgetset);
650 static void config_screen_add_device(void *arg)
652 struct config_screen *screen = arg;
654 screen->show_subset = true;
655 cui_show_subset(screen->cui, _("Select a boot device to add"),
656 screen->widgets.boot_order_f);
659 static void config_screen_autoboot_none(void *arg)
661 struct config_screen *screen = arg;
662 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
664 widget_subset_clear_active(subset);
666 widgetset_unpost(screen->widgetset);
667 config_screen_layout_widgets(screen);
668 widgetset_post(screen->widgetset);
671 static void config_screen_autoboot_any(void *arg)
673 struct config_screen *screen = arg;
674 const struct system_info *sysinfo = screen->cui->sysinfo;
675 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
678 widget_subset_clear_active(subset);
680 idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
682 widget_subset_make_active(screen->widgets.boot_order_f, idx);
684 screen->autoboot_enabled = true;
686 widgetset_unpost(screen->widgetset);
687 config_screen_layout_widgets(screen);
688 widgetset_post(screen->widgetset);
691 static void config_screen_update_subset(void *arg,
692 struct nc_widget_subset *subset, int idx)
694 struct config_screen *screen = arg;
697 widget_subset_make_active(subset, idx);
698 if (!screen->autoboot_enabled)
699 screen->autoboot_enabled = true;
700 config_screen_layout_widgets(screen);
703 static struct interface_config *first_active_interface(
704 const struct config *config)
708 for (i = 0; i < config->network.n_interfaces; i++) {
709 if (config->network.interfaces[i]->ignore)
711 return config->network.interfaces[i];
716 static enum net_conf_type find_net_conf_type(const struct config *config)
718 struct interface_config *ifcfg;
720 ifcfg = first_active_interface(config);
723 return NET_CONF_TYPE_DHCP_ALL;
725 else if (ifcfg->method == CONFIG_METHOD_DHCP)
726 return NET_CONF_TYPE_DHCP_ONE;
728 else if (ifcfg->method == CONFIG_METHOD_STATIC)
729 return NET_CONF_TYPE_STATIC;
732 return NET_CONF_TYPE_DHCP_ALL;
735 static void config_screen_setup_empty(struct config_screen *screen)
737 widget_new_label(screen->widgetset, 2, screen->field_x,
738 _("Waiting for configuration data..."));
739 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
740 4, screen->field_x, 9, _("Cancel"),
741 cancel_click, screen);
744 static int find_autoboot_idx(const struct system_info *sysinfo,
745 struct autoboot_option *opt)
749 if (opt->boot_type == BOOT_DEVICE_TYPE)
750 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
752 for (i = 0; i < sysinfo->n_blockdevs; i++) {
753 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
757 for (i = 0; i < sysinfo->n_interfaces; i++) {
758 struct interface_info *info = sysinfo->interfaces[i];
761 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
763 if (!strcmp(mac, opt->uuid))
764 return sysinfo->n_blockdevs + i;
770 static void config_screen_setup_widgets(struct config_screen *screen,
771 const struct config *config,
772 const struct system_info *sysinfo)
774 struct nc_widgetset *set = screen->widgetset;
775 struct interface_config *ifcfg;
776 char *str, *ip, *mask, *gw, *url, *tty;
777 enum net_conf_type type;
779 int add_len, clear_len, any_len, min_len = 20;
782 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
785 type = screen->net_conf_type;
786 ifcfg = first_active_interface(config);
788 screen->autoboot_enabled = config->autoboot_enabled;
790 screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
792 screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
793 COLS - screen->field_x - 1);
795 widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
796 !screen->autoboot_enabled);
797 widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
798 screen->autoboot_enabled);
800 widget_select_on_change(screen->widgets.autoboot_f,
801 config_screen_autoboot_change, screen);
803 add_len = max(min_len, strncols(_("Add Device")));
804 clear_len = max(min_len, strncols(_("Clear")));
805 any_len = max(min_len, strncols(_("Clear & Boot Any")));
807 screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
809 config_screen_add_device, screen);
811 screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
813 config_screen_autoboot_none, screen);
815 screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
816 _("Clear & Boot Any"),
817 config_screen_autoboot_any, screen);
819 screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
821 screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
822 COLS - screen->field_x,
823 config_screen_update_subset);
824 screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
827 widget_subset_on_change(screen->widgets.boot_order_f,
828 config_screen_boot_order_change, screen);
830 for (i = 0; i < sysinfo->n_blockdevs; i++) {
831 struct blockdev_info *bd = sysinfo->blockdevs[i];
834 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
837 widget_subset_add_option(screen->widgets.boot_order_f, label);
840 for (i = 0; i < sysinfo->n_interfaces; i++) {
841 struct interface_info *info = sysinfo->interfaces[i];
842 char *label, mac[20];
844 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
846 label = talloc_asprintf(screen, _("net: %s [mac: %s]"),
849 widget_subset_add_option(screen->widgets.boot_order_f, label);
852 for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
855 if (i == DEVICE_TYPE_ANY)
856 label = talloc_asprintf(screen, _("Any Device"));
858 label = talloc_asprintf(screen, _("Any %s device"),
859 device_type_display_name(i));
861 widget_subset_add_option(screen->widgets.boot_order_f, label);
864 for (i = 0; i < config->n_autoboot_opts; i++) {
865 struct autoboot_option *opt = &config->autoboot_opts[i];
868 idx = find_autoboot_idx(sysinfo, opt);
871 widget_subset_make_active(screen->widgets.boot_order_f,
874 if (opt->boot_type == BOOT_DEVICE_TYPE)
875 pb_log("%s: Unknown autoboot option: %d\n",
876 __func__, opt->type);
878 pb_log("%s: Unknown autoboot UUID: %s\n",
879 __func__, opt->uuid);
884 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
885 screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
886 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
887 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
890 widget_textbox_set_fixed_size(screen->widgets.timeout_f);
891 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
893 if (config->ipmi_bootdev) {
894 char *label = talloc_asprintf(screen,
895 _("%s IPMI boot option: %s"),
896 config->ipmi_bootdev_persistent ?
897 "Persistent" : "Temporary",
898 ipmi_bootdev_display_name(config->ipmi_bootdev));
899 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
901 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
903 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
905 screen->ipmi_override = true;
908 screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
909 screen->widgets.network_f = widget_new_select(set, 0, 0,
910 COLS - screen->field_x - 1);
912 widget_select_add_option(screen->widgets.network_f,
913 NET_CONF_TYPE_DHCP_ALL,
914 _("DHCP on all active interfaces"),
915 type == NET_CONF_TYPE_DHCP_ALL);
916 widget_select_add_option(screen->widgets.network_f,
917 NET_CONF_TYPE_DHCP_ONE,
918 _("DHCP on a specific interface"),
919 type == NET_CONF_TYPE_DHCP_ONE);
920 widget_select_add_option(screen->widgets.network_f,
921 NET_CONF_TYPE_STATIC,
922 _("Static IP configuration"),
923 type == NET_CONF_TYPE_STATIC);
925 widget_select_on_change(screen->widgets.network_f,
926 config_screen_network_change, screen);
928 screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
929 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
931 for (i = 0; i < sysinfo->n_interfaces; i++) {
932 struct interface_info *info = sysinfo->interfaces[i];
933 char str[50], mac[20];
936 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
937 sizeof(ifcfg->hwaddr));
939 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
940 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
941 info->link ? _("link up") : _("link down"));
943 widget_select_add_option(screen->widgets.iface_f,
947 url = gw = ip = mask = NULL;
948 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
951 str = talloc_strdup(screen, ifcfg->static_config.address);
952 sep = strchr(str, '/');
959 gw = ifcfg->static_config.gateway;
960 url = ifcfg->static_config.url;
963 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
964 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
965 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
966 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
967 screen->widgets.ip_addr_mask_help_l =
968 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
970 widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
971 widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
972 widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
973 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
975 screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
976 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
977 screen->widgets.gateway_help_l =
978 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
980 widget_textbox_set_fixed_size(screen->widgets.gateway_f);
981 widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
983 screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
984 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
985 screen->widgets.url_help_l =
986 widget_new_label(set, 0, 0, _("(eg. tftp://)"));
988 str = talloc_strdup(screen, "");
989 for (i = 0; i < config->network.n_dns_servers; i++) {
990 str = talloc_asprintf_append(str, "%s%s",
992 config->network.dns_servers[i]);
995 screen->widgets.dns_l = widget_new_label(set, 0, 0,
996 _("DNS Server(s):"));
997 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
998 screen->widgets.dns_help_l =
999 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
1001 widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
1003 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
1004 _("(if not provided by DHCP server)"));
1006 if (config->safe_mode)
1007 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
1008 _("Selecting 'OK' will exit safe mode"));
1010 screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
1012 screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
1013 COLS - screen->field_x - 1);
1015 widget_select_add_option(screen->widgets.allow_write_f, 0,
1016 _("Prevent all writes to disk"),
1017 !config->allow_writes);
1019 widget_select_add_option(screen->widgets.allow_write_f, 1,
1020 _("Allow bootloader scripts to modify disks"),
1021 config->allow_writes);
1023 screen->widgets.boot_tty_l = widget_new_label(set, 0, 0,
1025 screen->widgets.boot_tty_f = widget_new_select(set, 0, 0,
1026 COLS - screen->field_x - 1);
1028 for (i = 0; i < config->n_tty; i++){
1029 found = config->boot_tty &&
1030 strncmp(config->boot_tty, config->tty_list[i],
1031 strlen(config->boot_tty)) == 0;
1032 widget_select_add_option(screen->widgets.boot_tty_f, i,
1033 config->tty_list[i], found);
1036 tty = talloc_asprintf(screen, _("Current interface: %s"),
1037 ttyname(STDIN_FILENO));
1038 screen->widgets.current_tty_l = widget_new_label(set, 0 , 0, tty);
1040 screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
1042 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
1043 help_click, screen);
1044 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
1045 cancel_click, screen);
1048 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
1050 struct config_screen *screen = arg;
1051 int w_y, w_height, w_focus, s_max, adjust;
1053 w_height = widget_height(widget);
1054 w_focus = widget_focus_y(widget);
1055 w_y = widget_y(widget) + w_focus;
1056 s_max = getmaxy(screen->scr.sub_ncw) - 1;
1058 if (w_y < screen->scroll_y)
1059 screen->scroll_y = w_y;
1061 else if (w_y + screen->scroll_y + 1 > s_max) {
1062 /* Fit as much of the widget into the screen as possible */
1063 adjust = min(s_max - 1, w_height - w_focus);
1064 if (w_y + adjust >= screen->scroll_y + s_max)
1065 screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
1069 pad_refresh(screen);
1072 static void config_screen_draw(struct config_screen *screen,
1073 const struct config *config,
1074 const struct system_info *sysinfo)
1076 bool repost = false;
1079 /* The size of the pad we'll need depends on the number of interfaces.
1081 * We use N_FIELDS (which is quite conservative, as some fields share
1082 * a line) as a base, then:
1083 * - add 6 (as the network select & boot device select fields take 3
1085 * - add n_interfaces for every field in the network select field, and
1086 * - add (n_blockdevs + n_interfaces) for every field in the boot device
1089 height = N_FIELDS + 6;
1091 height += sysinfo->n_interfaces;
1092 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
1094 if (!screen->pad || getmaxy(screen->pad) < height) {
1096 delwin(screen->pad);
1097 screen->pad = newpad(height, COLS + 10);
1100 if (screen->widgetset) {
1101 widgetset_unpost(screen->widgetset);
1102 talloc_free(screen->widgetset);
1106 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
1108 widgetset_set_widget_focus(screen->widgetset,
1109 config_screen_widget_focus, screen);
1111 if (!config || !sysinfo) {
1112 config_screen_setup_empty(screen);
1114 screen->net_conf_type = find_net_conf_type(config);
1115 config_screen_setup_widgets(screen, config, sysinfo);
1116 config_screen_layout_widgets(screen);
1120 widgetset_post(screen->widgetset);
1123 void config_screen_update(struct config_screen *screen,
1124 const struct config *config,
1125 const struct system_info *sysinfo)
1127 if (screen->cui->current != config_screen_scr(screen)) {
1128 screen->need_update = true;
1132 config_screen_draw(screen, config, sysinfo);
1133 pad_refresh(screen);
1136 static int config_screen_post(struct nc_scr *scr)
1138 struct config_screen *screen = config_screen_from_scr(scr);
1139 screen->show_subset = false;
1141 if (screen->need_update) {
1142 config_screen_draw(screen, screen->cui->config,
1143 screen->cui->sysinfo);
1144 screen->need_update = false;
1146 widgetset_post(screen->widgetset);
1149 nc_scr_frame_draw(scr);
1150 if (screen->need_redraw) {
1151 redrawwin(scr->main_ncw);
1152 screen->need_redraw = false;
1154 wrefresh(screen->scr.main_ncw);
1155 pad_refresh(screen);
1159 static int config_screen_destroy(void *arg)
1161 struct config_screen *screen = arg;
1163 delwin(screen->pad);
1167 struct config_screen *config_screen_init(struct cui *cui,
1168 const struct config *config,
1169 const struct system_info *sysinfo,
1170 void (*on_exit)(struct cui *))
1172 struct config_screen *screen;
1174 screen = talloc_zero(cui, struct config_screen);
1175 talloc_set_destructor(screen, config_screen_destroy);
1176 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1177 cui, config_screen_process_key,
1178 config_screen_post, config_screen_unpost,
1179 config_screen_resize);
1182 screen->on_exit = on_exit;
1183 screen->need_redraw = false;
1184 screen->need_update = false;
1185 screen->label_x = 2;
1186 screen->field_x = 17;
1188 screen->ipmi_override = false;
1189 screen->show_subset = false;
1191 screen->scr.frame.ltitle = talloc_strdup(screen,
1192 _("Petitboot System Configuration"));
1193 screen->scr.frame.rtitle = NULL;
1194 screen->scr.frame.help = talloc_strdup(screen,
1195 _("tab=next, shift+tab=previous, x=exit, h=help"));
1196 nc_scr_frame_draw(&screen->scr);
1198 scrollok(screen->scr.sub_ncw, true);
1200 config_screen_draw(screen, config, sysinfo);