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_move(wf, y, screen->field_x);
415 y += widget_height(wf);
417 show = screen->autoboot_enabled;
422 wl = widget_label_base(screen->widgets.boot_order_l);
423 widget_set_visible(wl, show);
424 widget_move(wl, y, screen->label_x);
426 wf = widget_subset_base(screen->widgets.boot_order_f);
427 widget_move(wf, y, screen->field_x);
428 wl = widget_label_base(screen->widgets.boot_empty_l);
429 widget_move(wl, y, screen->field_x);
431 if (widget_subset_height(screen->widgets.boot_order_f)) {
432 widget_set_visible(wl, false);
433 widget_set_visible(wf, show);
434 y += show ? widget_height(wf) : 0;
436 widget_set_visible(wl, show);
437 widget_set_visible(wf, false);
443 widget_move(widget_button_base(screen->widgets.boot_add_b),
444 y++, screen->field_x);
445 widget_move(widget_button_base(screen->widgets.boot_any_b),
446 y++, screen->field_x);
447 widget_move(widget_button_base(screen->widgets.boot_none_b),
451 wf = widget_button_base(screen->widgets.boot_add_b);
452 if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
453 widget_set_visible(wf, true);
455 widget_set_visible(wf, false);
460 widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
461 widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
463 wf = widget_textbox_base(screen->widgets.timeout_f);
464 wl = widget_label_base(screen->widgets.timeout_l);
465 wh = widget_label_base(screen->widgets.timeout_help_l);
466 widget_set_visible(wl, screen->autoboot_enabled);
467 widget_set_visible(wf, screen->autoboot_enabled);
468 widget_set_visible(wh, screen->autoboot_enabled);
469 if (screen->autoboot_enabled) {
470 widget_set_visible(wh, screen->autoboot_enabled);
471 widget_move(wl, y, screen->label_x);
472 widget_move(wf, y, screen->field_x);
473 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
477 if (screen->ipmi_override) {
478 wl = widget_label_base(screen->widgets.ipmi_type_l);
479 widget_set_visible(wl, true);
480 widget_move(wl, y, screen->label_x);
483 wl = widget_label_base(screen->widgets.ipmi_clear_l);
484 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
485 widget_set_visible(wl, true);
486 widget_set_visible(wf, true);
487 widget_move(wl, y, screen->label_x);
488 widget_move(wf, y, screen->field_x);
494 y += layout_pair(screen, y, screen->widgets.network_l,
495 widget_select_base(screen->widgets.network_f));
499 /* conditionally show iface select */
500 wl = widget_label_base(screen->widgets.iface_l);
501 wf = widget_select_base(screen->widgets.iface_f);
503 show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
504 screen->net_conf_type == NET_CONF_TYPE_STATIC;
506 widget_set_visible(wl, show);
507 widget_set_visible(wf, show);
510 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
512 /* conditionally show static IP params */
513 show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
515 wl = widget_label_base(screen->widgets.ip_addr_l);
516 wf = widget_textbox_base(screen->widgets.ip_addr_f);
517 widget_set_visible(wl, show);
518 widget_set_visible(wf, show);
519 x = screen->field_x + widget_width(wf) + 1;
522 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
524 wl = widget_label_base(screen->widgets.ip_mask_l);
525 wf = widget_textbox_base(screen->widgets.ip_mask_f);
526 widget_set_visible(wl, show);
527 widget_set_visible(wf, show);
530 widget_move(wl, y, x);
531 widget_move(wf, y, x + 2);
534 /* help for IP/mask */
535 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
536 widget_set_visible(wh, show);
538 widget_move(wh, y, help_x);
542 wl = widget_label_base(screen->widgets.gateway_l);
543 wf = widget_textbox_base(screen->widgets.gateway_f);
544 wh = widget_label_base(screen->widgets.gateway_help_l);
545 widget_set_visible(wl, show);
546 widget_set_visible(wf, show);
547 widget_set_visible(wh, show);
550 layout_pair(screen, y, screen->widgets.gateway_l, wf);
551 widget_move(wh, y, help_x);
555 wl = widget_label_base(screen->widgets.url_l);
556 wf = widget_textbox_base(screen->widgets.url_f);
557 wh = widget_label_base(screen->widgets.url_help_l);
558 widget_set_visible(wl, show);
559 widget_set_visible(wf, show);
560 widget_set_visible(wh, show);
563 layout_pair(screen, y, screen->widgets.url_l, wf);
564 widget_move(wh, y, help_x);
568 wh = widget_label_base(screen->widgets.dns_help_l);
569 layout_pair(screen, y, screen->widgets.dns_l,
570 widget_textbox_base(screen->widgets.dns_f));
571 widget_move(wh, y, help_x);
574 /* we show the DNS/DHCP help if we're configuring DHCP */
575 show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
576 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
577 widget_set_visible(wl, show);
579 widget_move(wl, y, screen->field_x);
585 show = screen->cui->config->safe_mode;
587 widget_move(widget_label_base(screen->widgets.safe_mode),
592 layout_pair(screen, y, screen->widgets.allow_write_l,
593 widget_select_base(screen->widgets.allow_write_f));
594 y += widget_height(widget_select_base(screen->widgets.allow_write_f));
598 if (widget_height(widget_select_base(screen->widgets.boot_tty_f))) {
599 layout_pair(screen, y, screen->widgets.boot_tty_l,
600 widget_select_base(screen->widgets.boot_tty_f));
601 y += widget_height(widget_select_base(screen->widgets.boot_tty_f));
602 widget_move(widget_label_base(screen->widgets.current_tty_l),
606 widget_set_visible(widget_label_base(
607 screen->widgets.boot_tty_l), false);
608 widget_set_visible(widget_select_base(
609 screen->widgets.boot_tty_f), false);
610 widget_set_visible(widget_label_base(
611 screen->widgets.current_tty_l), false);
614 widget_move(widget_button_base(screen->widgets.ok_b),
616 widget_move(widget_button_base(screen->widgets.help_b),
617 y, screen->field_x + 14);
618 widget_move(widget_button_base(screen->widgets.cancel_b),
619 y, screen->field_x + 28);
622 static void config_screen_network_change(void *arg, int value)
624 struct config_screen *screen = arg;
625 screen->net_conf_type = value;
626 widgetset_unpost(screen->widgetset);
627 config_screen_layout_widgets(screen);
628 widgetset_post(screen->widgetset);
631 static void config_screen_boot_order_change(void *arg, int value)
634 struct config_screen *screen = arg;
635 widgetset_unpost(screen->widgetset);
636 config_screen_layout_widgets(screen);
637 widgetset_post(screen->widgetset);
640 static void config_screen_autoboot_change(void *arg, int value)
642 struct config_screen *screen = arg;
643 screen->autoboot_enabled = !!value;
644 widgetset_unpost(screen->widgetset);
645 config_screen_layout_widgets(screen);
646 widgetset_post(screen->widgetset);
649 static void config_screen_add_device(void *arg)
651 struct config_screen *screen = arg;
653 screen->show_subset = true;
654 cui_show_subset(screen->cui, _("Select a boot device to add"),
655 screen->widgets.boot_order_f);
658 static void config_screen_autoboot_none(void *arg)
660 struct config_screen *screen = arg;
661 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
663 widget_subset_clear_active(subset);
665 widgetset_unpost(screen->widgetset);
666 config_screen_layout_widgets(screen);
667 widgetset_post(screen->widgetset);
670 static void config_screen_autoboot_any(void *arg)
672 struct config_screen *screen = arg;
673 const struct system_info *sysinfo = screen->cui->sysinfo;
674 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
677 widget_subset_clear_active(subset);
679 idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
681 widget_subset_make_active(screen->widgets.boot_order_f, idx);
683 screen->autoboot_enabled = true;
685 widgetset_unpost(screen->widgetset);
686 config_screen_layout_widgets(screen);
687 widgetset_post(screen->widgetset);
690 static void config_screen_update_subset(void *arg,
691 struct nc_widget_subset *subset, int idx)
693 struct config_screen *screen = arg;
696 widget_subset_make_active(subset, idx);
697 if (!screen->autoboot_enabled)
698 screen->autoboot_enabled = true;
699 config_screen_layout_widgets(screen);
702 static struct interface_config *first_active_interface(
703 const struct config *config)
707 for (i = 0; i < config->network.n_interfaces; i++) {
708 if (config->network.interfaces[i]->ignore)
710 return config->network.interfaces[i];
715 static enum net_conf_type find_net_conf_type(const struct config *config)
717 struct interface_config *ifcfg;
719 ifcfg = first_active_interface(config);
722 return NET_CONF_TYPE_DHCP_ALL;
724 else if (ifcfg->method == CONFIG_METHOD_DHCP)
725 return NET_CONF_TYPE_DHCP_ONE;
727 else if (ifcfg->method == CONFIG_METHOD_STATIC)
728 return NET_CONF_TYPE_STATIC;
731 return NET_CONF_TYPE_DHCP_ALL;
734 static void config_screen_setup_empty(struct config_screen *screen)
736 widget_new_label(screen->widgetset, 2, screen->field_x,
737 _("Waiting for configuration data..."));
738 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
739 4, screen->field_x, 9, _("Cancel"),
740 cancel_click, screen);
743 static int find_autoboot_idx(const struct system_info *sysinfo,
744 struct autoboot_option *opt)
748 if (opt->boot_type == BOOT_DEVICE_TYPE)
749 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
751 for (i = 0; i < sysinfo->n_blockdevs; i++) {
752 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
756 for (i = 0; i < sysinfo->n_interfaces; i++) {
757 struct interface_info *info = sysinfo->interfaces[i];
760 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
762 if (!strcmp(mac, opt->uuid))
763 return sysinfo->n_blockdevs + i;
769 static void config_screen_setup_widgets(struct config_screen *screen,
770 const struct config *config,
771 const struct system_info *sysinfo)
773 struct nc_widgetset *set = screen->widgetset;
774 struct interface_config *ifcfg;
775 char *str, *ip, *mask, *gw, *url, *tty;
776 enum net_conf_type type;
778 int add_len, clear_len, any_len, min_len = 20;
781 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
784 type = screen->net_conf_type;
785 ifcfg = first_active_interface(config);
787 screen->autoboot_enabled = config->autoboot_enabled;
789 screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
791 screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
792 COLS - screen->field_x - 1);
794 widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
795 !screen->autoboot_enabled);
796 widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
797 screen->autoboot_enabled);
799 widget_select_on_change(screen->widgets.autoboot_f,
800 config_screen_autoboot_change, screen);
802 add_len = max(min_len, strncols(_("Add Device")));
803 clear_len = max(min_len, strncols(_("Clear")));
804 any_len = max(min_len, strncols(_("Clear & Boot Any")));
806 screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
808 config_screen_add_device, screen);
810 screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
812 config_screen_autoboot_none, screen);
814 screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
815 _("Clear & Boot Any"),
816 config_screen_autoboot_any, screen);
818 screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
820 screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
821 COLS - screen->field_x,
822 config_screen_update_subset);
823 screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
826 widget_subset_on_change(screen->widgets.boot_order_f,
827 config_screen_boot_order_change, screen);
829 for (i = 0; i < sysinfo->n_blockdevs; i++) {
830 struct blockdev_info *bd = sysinfo->blockdevs[i];
833 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
836 widget_subset_add_option(screen->widgets.boot_order_f, label);
839 for (i = 0; i < sysinfo->n_interfaces; i++) {
840 struct interface_info *info = sysinfo->interfaces[i];
841 char *label, mac[20];
843 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
845 label = talloc_asprintf(screen, _("net: %s [mac: %s]"),
848 widget_subset_add_option(screen->widgets.boot_order_f, label);
851 for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
854 if (i == DEVICE_TYPE_ANY)
855 label = talloc_asprintf(screen, _("Any Device"));
857 label = talloc_asprintf(screen, _("Any %s device"),
858 device_type_display_name(i));
860 widget_subset_add_option(screen->widgets.boot_order_f, label);
863 for (i = 0; i < config->n_autoboot_opts; i++) {
864 struct autoboot_option *opt = &config->autoboot_opts[i];
867 idx = find_autoboot_idx(sysinfo, opt);
870 widget_subset_make_active(screen->widgets.boot_order_f,
873 if (opt->boot_type == BOOT_DEVICE_TYPE)
874 pb_log("%s: Unknown autoboot option: %d\n",
875 __func__, opt->type);
877 pb_log("%s: Unknown autoboot UUID: %s\n",
878 __func__, opt->uuid);
883 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
884 screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
885 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
886 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
889 widget_textbox_set_fixed_size(screen->widgets.timeout_f);
890 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
892 if (config->ipmi_bootdev) {
893 char *label = talloc_asprintf(screen,
894 _("%s IPMI boot option: %s"),
895 config->ipmi_bootdev_persistent ?
896 "Persistent" : "Temporary",
897 ipmi_bootdev_display_name(config->ipmi_bootdev));
898 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
900 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
902 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
904 screen->ipmi_override = true;
907 screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
908 screen->widgets.network_f = widget_new_select(set, 0, 0,
909 COLS - screen->field_x - 1);
911 widget_select_add_option(screen->widgets.network_f,
912 NET_CONF_TYPE_DHCP_ALL,
913 _("DHCP on all active interfaces"),
914 type == NET_CONF_TYPE_DHCP_ALL);
915 widget_select_add_option(screen->widgets.network_f,
916 NET_CONF_TYPE_DHCP_ONE,
917 _("DHCP on a specific interface"),
918 type == NET_CONF_TYPE_DHCP_ONE);
919 widget_select_add_option(screen->widgets.network_f,
920 NET_CONF_TYPE_STATIC,
921 _("Static IP configuration"),
922 type == NET_CONF_TYPE_STATIC);
924 widget_select_on_change(screen->widgets.network_f,
925 config_screen_network_change, screen);
927 screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
928 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
930 for (i = 0; i < sysinfo->n_interfaces; i++) {
931 struct interface_info *info = sysinfo->interfaces[i];
932 char str[50], mac[20];
935 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
936 sizeof(ifcfg->hwaddr));
938 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
939 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
940 info->link ? _("link up") : _("link down"));
942 widget_select_add_option(screen->widgets.iface_f,
946 url = gw = ip = mask = NULL;
947 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
950 str = talloc_strdup(screen, ifcfg->static_config.address);
951 sep = strchr(str, '/');
958 gw = ifcfg->static_config.gateway;
959 url = ifcfg->static_config.url;
962 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
963 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
964 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
965 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
966 screen->widgets.ip_addr_mask_help_l =
967 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
969 widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
970 widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
971 widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
972 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
974 screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
975 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
976 screen->widgets.gateway_help_l =
977 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
979 widget_textbox_set_fixed_size(screen->widgets.gateway_f);
980 widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
982 screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
983 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
984 screen->widgets.url_help_l =
985 widget_new_label(set, 0, 0, _("(eg. tftp://)"));
987 str = talloc_strdup(screen, "");
988 for (i = 0; i < config->network.n_dns_servers; i++) {
989 str = talloc_asprintf_append(str, "%s%s",
991 config->network.dns_servers[i]);
994 screen->widgets.dns_l = widget_new_label(set, 0, 0,
995 _("DNS Server(s):"));
996 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
997 screen->widgets.dns_help_l =
998 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
1000 widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
1002 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
1003 _("(if not provided by DHCP server)"));
1005 if (config->safe_mode)
1006 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
1007 _("Selecting 'OK' will exit safe mode"));
1009 screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
1011 screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
1012 COLS - screen->field_x - 1);
1014 widget_select_add_option(screen->widgets.allow_write_f, 0,
1015 _("Prevent all writes to disk"),
1016 !config->allow_writes);
1018 widget_select_add_option(screen->widgets.allow_write_f, 1,
1019 _("Allow bootloader scripts to modify disks"),
1020 config->allow_writes);
1022 screen->widgets.boot_tty_l = widget_new_label(set, 0, 0,
1024 screen->widgets.boot_tty_f = widget_new_select(set, 0, 0,
1025 COLS - screen->field_x - 1);
1027 for (i = 0; i < config->n_tty; i++){
1028 found = config->boot_tty &&
1029 strncmp(config->boot_tty, config->tty_list[i],
1030 strlen(config->boot_tty)) == 0;
1031 widget_select_add_option(screen->widgets.boot_tty_f, i,
1032 config->tty_list[i], found);
1035 tty = talloc_asprintf(screen, _("Current interface: %s"),
1036 ttyname(STDIN_FILENO));
1037 screen->widgets.current_tty_l = widget_new_label(set, 0 , 0, tty);
1039 screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
1041 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
1042 help_click, screen);
1043 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
1044 cancel_click, screen);
1047 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
1049 struct config_screen *screen = arg;
1050 int w_y, w_height, w_focus, s_max, adjust;
1052 w_height = widget_height(widget);
1053 w_focus = widget_focus_y(widget);
1054 w_y = widget_y(widget) + w_focus;
1055 s_max = getmaxy(screen->scr.sub_ncw) - 1;
1057 if (w_y < screen->scroll_y)
1058 screen->scroll_y = w_y;
1060 else if (w_y + screen->scroll_y + 1 > s_max) {
1061 /* Fit as much of the widget into the screen as possible */
1062 adjust = min(s_max - 1, w_height - w_focus);
1063 if (w_y + adjust >= screen->scroll_y + s_max)
1064 screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
1068 pad_refresh(screen);
1071 static void config_screen_draw(struct config_screen *screen,
1072 const struct config *config,
1073 const struct system_info *sysinfo)
1075 bool repost = false;
1078 /* The size of the pad we'll need depends on the number of interfaces.
1080 * We use N_FIELDS (which is quite conservative, as some fields share
1081 * a line) as a base, then:
1082 * - add 6 (as the network select & boot device select fields take 3
1084 * - add n_interfaces for every field in the network select field, and
1085 * - add (n_blockdevs + n_interfaces) for every field in the boot device
1088 height = N_FIELDS + 6;
1090 height += sysinfo->n_interfaces;
1091 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
1093 if (!screen->pad || getmaxy(screen->pad) < height) {
1095 delwin(screen->pad);
1096 screen->pad = newpad(height, COLS + 10);
1099 if (screen->widgetset) {
1100 widgetset_unpost(screen->widgetset);
1101 talloc_free(screen->widgetset);
1105 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
1107 widgetset_set_widget_focus(screen->widgetset,
1108 config_screen_widget_focus, screen);
1110 if (!config || !sysinfo) {
1111 config_screen_setup_empty(screen);
1113 screen->net_conf_type = find_net_conf_type(config);
1114 config_screen_setup_widgets(screen, config, sysinfo);
1115 config_screen_layout_widgets(screen);
1119 widgetset_post(screen->widgetset);
1122 void config_screen_update(struct config_screen *screen,
1123 const struct config *config,
1124 const struct system_info *sysinfo)
1126 if (screen->cui->current != config_screen_scr(screen)) {
1127 screen->need_update = true;
1131 config_screen_draw(screen, config, sysinfo);
1132 pad_refresh(screen);
1135 static int config_screen_post(struct nc_scr *scr)
1137 struct config_screen *screen = config_screen_from_scr(scr);
1138 screen->show_subset = false;
1140 if (screen->need_update) {
1141 config_screen_draw(screen, screen->cui->config,
1142 screen->cui->sysinfo);
1143 screen->need_update = false;
1145 widgetset_post(screen->widgetset);
1148 nc_scr_frame_draw(scr);
1149 if (screen->need_redraw) {
1150 redrawwin(scr->main_ncw);
1151 screen->need_redraw = false;
1153 wrefresh(screen->scr.main_ncw);
1154 pad_refresh(screen);
1158 static int config_screen_destroy(void *arg)
1160 struct config_screen *screen = arg;
1162 delwin(screen->pad);
1166 struct config_screen *config_screen_init(struct cui *cui,
1167 const struct config *config,
1168 const struct system_info *sysinfo,
1169 void (*on_exit)(struct cui *))
1171 struct config_screen *screen;
1173 screen = talloc_zero(cui, struct config_screen);
1174 talloc_set_destructor(screen, config_screen_destroy);
1175 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1176 cui, config_screen_process_key,
1177 config_screen_post, config_screen_unpost,
1178 config_screen_resize);
1181 screen->on_exit = on_exit;
1182 screen->need_redraw = false;
1183 screen->need_update = false;
1184 screen->label_x = 2;
1185 screen->field_x = 17;
1187 screen->ipmi_override = false;
1188 screen->show_subset = false;
1190 screen->scr.frame.ltitle = talloc_strdup(screen,
1191 _("Petitboot System Configuration"));
1192 screen->scr.frame.rtitle = NULL;
1193 screen->scr.frame.help = talloc_strdup(screen,
1194 _("tab=next, shift+tab=previous, x=exit, h=help"));
1195 nc_scr_frame_draw(&screen->scr);
1197 scrollok(screen->scr.sub_ncw, true);
1199 config_screen_draw(screen, config, sysinfo);