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;
112 struct nc_widget_label *safe_mode;
113 struct nc_widget_button *ok_b;
114 struct nc_widget_button *help_b;
115 struct nc_widget_button *cancel_b;
119 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
121 struct config_screen *config_screen;
123 assert(scr->sig == pb_config_screen_sig);
124 config_screen = (struct config_screen *)
125 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
126 assert(config_screen->scr.sig == pb_config_screen_sig);
127 return config_screen;
130 static void pad_refresh(struct config_screen *screen)
132 int y, x, rows, cols;
134 getmaxyx(screen->scr.sub_ncw, rows, cols);
135 getbegyx(screen->scr.sub_ncw, y, x);
137 prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
140 static void config_screen_process_key(struct nc_scr *scr, int key)
142 struct config_screen *screen = config_screen_from_scr(scr);
145 handled = widgetset_process_key(screen->widgetset, key);
154 screen->show_help = true;
160 screen->on_exit(screen->cui);
162 } else if (screen->show_help) {
163 screen->show_help = false;
164 screen->need_redraw = true;
165 cui_show_help(screen->cui, _("System Configuration"),
168 } else if (handled && !screen->show_subset) {
173 static void config_screen_resize(struct nc_scr *scr)
175 struct config_screen *screen = config_screen_from_scr(scr);
179 static int config_screen_unpost(struct nc_scr *scr)
181 struct config_screen *screen = config_screen_from_scr(scr);
182 widgetset_unpost(screen->widgetset);
186 struct nc_scr *config_screen_scr(struct config_screen *screen)
191 static int screen_process_form(struct config_screen *screen)
193 const struct system_info *sysinfo = screen->cui->sysinfo;
194 enum net_conf_type net_conf_type;
195 struct interface_config *iface;
196 bool allow_write, autoboot;
198 struct config *config;
199 int i, n_boot_opts, rc, idx;
203 config = config_copy(screen, screen->cui->config);
205 talloc_free(config->autoboot_opts);
206 config->n_autoboot_opts = 0;
208 n_boot_opts = widget_subset_get_order(config, &order,
209 screen->widgets.boot_order_f);
211 autoboot = widget_select_get_value(screen->widgets.autoboot_f);
212 config->autoboot_enabled = autoboot && n_boot_opts;
214 config->n_autoboot_opts = n_boot_opts;
215 config->autoboot_opts = talloc_array(config, struct autoboot_option,
218 for (i = 0; i < n_boot_opts; i++) {
219 if (order[i] < sysinfo->n_blockdevs) {
221 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
222 config->autoboot_opts[i].uuid = talloc_strdup(config,
223 sysinfo->blockdevs[order[i]]->uuid);
224 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
226 order[i] -= sysinfo->n_blockdevs;
227 config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
228 mac_str(sysinfo->interfaces[order[i]]->hwaddr,
229 sysinfo->interfaces[order[i]]->hwaddr_size,
231 config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
234 order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
235 config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
236 config->autoboot_opts[i].type = order[i];
240 str = widget_textbox_get_value(screen->widgets.timeout_f);
244 x = strtoul(str, &end, 10);
245 if (!errno && end != str)
246 config->autoboot_timeout_sec = x;
249 if (screen->ipmi_override)
250 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
251 config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
254 net_conf_type = widget_select_get_value(screen->widgets.network_f);
256 /* if we don't have any network interfaces, prevent per-interface
258 if (sysinfo->n_interfaces == 0)
259 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
261 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
262 config->network.n_interfaces = 0;
265 iface = talloc_zero(config, struct interface_config);
266 config->network.n_interfaces = 1;
267 config->network.interfaces = talloc_array(config,
268 struct interface_config *, 1);
269 config->network.interfaces[0] = iface;
271 /* copy hwaddr (from the sysinfo interface data) to
272 * the configuration */
273 idx = widget_select_get_value(screen->widgets.iface_f);
274 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
275 sizeof(iface->hwaddr));
278 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
279 iface->method = CONFIG_METHOD_DHCP;
282 if (net_conf_type == NET_CONF_TYPE_STATIC) {
283 char *ip, *mask, *gateway, *url;
285 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
286 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
287 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
288 url = widget_textbox_get_value(screen->widgets.url_f);
290 if (!ip || !*ip || !mask || !*mask) {
291 screen->scr.frame.status =
292 _("No IP / mask values are set");
293 nc_scr_frame_draw(&screen->scr);
298 iface->method = CONFIG_METHOD_STATIC;
299 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
301 iface->static_config.gateway = talloc_strdup(iface, gateway);
302 iface->static_config.url = talloc_strdup(iface, url);
305 str = widget_textbox_get_value(screen->widgets.dns_f);
306 talloc_free(config->network.dns_servers);
307 config->network.dns_servers = NULL;
308 config->network.n_dns_servers = 0;
310 if (str && strlen(str)) {
315 dns = strtok_r(str, " \t", &tmp);
320 i = config->network.n_dns_servers++;
321 config->network.dns_servers = talloc_realloc(config,
322 config->network.dns_servers,
324 config->network.n_dns_servers);
325 config->network.dns_servers[i] =
326 talloc_strdup(config, dns);
332 allow_write = widget_select_get_value(screen->widgets.allow_write_f);
333 if (allow_write != config->allow_writes)
334 config->allow_writes = allow_write;
336 config->safe_mode = false;
337 rc = cui_send_config(screen->cui, config);
341 pb_log("cui_send_config failed!\n");
343 pb_debug("config sent!\n");
348 static void ok_click(void *arg)
350 struct config_screen *screen = arg;
351 if (screen_process_form(screen))
352 /* errors are written to the status line, so we'll need
354 wrefresh(screen->scr.main_ncw);
359 static void help_click(void *arg)
361 struct config_screen *screen = arg;
362 screen->show_help = true;
365 static void cancel_click(void *arg)
367 struct config_screen *screen = arg;
371 static int layout_pair(struct config_screen *screen, int y,
372 struct nc_widget_label *label,
373 struct nc_widget *field)
375 struct nc_widget *label_w = widget_label_base(label);
376 widget_move(label_w, y, screen->label_x);
377 widget_move(field, y, screen->field_x);
378 return max(widget_height(label_w), widget_height(field));
381 static void config_screen_layout_widgets(struct config_screen *screen)
383 struct nc_widget *wl, *wf, *wh;
388 /* currently, the longest label we have is the DNS-servers
389 * widget, so layout our screen based on that */
390 help_x = screen->field_x + 2 +
391 widget_width(widget_textbox_base(screen->widgets.dns_f));
393 wl = widget_label_base(screen->widgets.autoboot_l);
394 widget_set_visible(wl, true);
395 widget_move(wl, y, screen->label_x);
397 wf = widget_select_base(screen->widgets.autoboot_f);
398 widget_move(wf, y, screen->field_x);
399 y += widget_height(wf);
401 show = screen->autoboot_enabled;
406 wl = widget_label_base(screen->widgets.boot_order_l);
407 widget_set_visible(wl, show);
408 widget_move(wl, y, screen->label_x);
410 wf = widget_subset_base(screen->widgets.boot_order_f);
411 widget_move(wf, y, screen->field_x);
412 wl = widget_label_base(screen->widgets.boot_empty_l);
413 widget_move(wl, y, screen->field_x);
415 if (widget_subset_height(screen->widgets.boot_order_f)) {
416 widget_set_visible(wl, false);
417 widget_set_visible(wf, show);
418 y += show ? widget_height(wf) : 0;
420 widget_set_visible(wl, show);
421 widget_set_visible(wf, false);
427 widget_move(widget_button_base(screen->widgets.boot_add_b),
428 y++, screen->field_x);
429 widget_move(widget_button_base(screen->widgets.boot_any_b),
430 y++, screen->field_x);
431 widget_move(widget_button_base(screen->widgets.boot_none_b),
435 wf = widget_button_base(screen->widgets.boot_add_b);
436 if (widget_subset_n_inactive(screen->widgets.boot_order_f) && show)
437 widget_set_visible(wf, true);
439 widget_set_visible(wf, false);
444 widget_set_visible(widget_button_base(screen->widgets.boot_any_b), show);
445 widget_set_visible(widget_button_base(screen->widgets.boot_none_b), show);
447 wf = widget_textbox_base(screen->widgets.timeout_f);
448 wl = widget_label_base(screen->widgets.timeout_l);
449 wh = widget_label_base(screen->widgets.timeout_help_l);
450 widget_set_visible(wl, screen->autoboot_enabled);
451 widget_set_visible(wf, screen->autoboot_enabled);
452 widget_set_visible(wh, screen->autoboot_enabled);
453 if (screen->autoboot_enabled) {
454 widget_set_visible(wh, screen->autoboot_enabled);
455 widget_move(wl, y, screen->label_x);
456 widget_move(wf, y, screen->field_x);
457 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
461 if (screen->ipmi_override) {
462 wl = widget_label_base(screen->widgets.ipmi_type_l);
463 widget_set_visible(wl, true);
464 widget_move(wl, y, screen->label_x);
467 wl = widget_label_base(screen->widgets.ipmi_clear_l);
468 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
469 widget_set_visible(wl, true);
470 widget_set_visible(wf, true);
471 widget_move(wl, y, screen->label_x);
472 widget_move(wf, y, screen->field_x);
478 y += layout_pair(screen, y, screen->widgets.network_l,
479 widget_select_base(screen->widgets.network_f));
483 /* conditionally show iface select */
484 wl = widget_label_base(screen->widgets.iface_l);
485 wf = widget_select_base(screen->widgets.iface_f);
487 show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
488 screen->net_conf_type == NET_CONF_TYPE_STATIC;
490 widget_set_visible(wl, show);
491 widget_set_visible(wf, show);
494 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
496 /* conditionally show static IP params */
497 show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
499 wl = widget_label_base(screen->widgets.ip_addr_l);
500 wf = widget_textbox_base(screen->widgets.ip_addr_f);
501 widget_set_visible(wl, show);
502 widget_set_visible(wf, show);
503 x = screen->field_x + widget_width(wf) + 1;
506 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
508 wl = widget_label_base(screen->widgets.ip_mask_l);
509 wf = widget_textbox_base(screen->widgets.ip_mask_f);
510 widget_set_visible(wl, show);
511 widget_set_visible(wf, show);
514 widget_move(wl, y, x);
515 widget_move(wf, y, x + 2);
518 /* help for IP/mask */
519 wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
520 widget_set_visible(wh, show);
522 widget_move(wh, y, help_x);
526 wl = widget_label_base(screen->widgets.gateway_l);
527 wf = widget_textbox_base(screen->widgets.gateway_f);
528 wh = widget_label_base(screen->widgets.gateway_help_l);
529 widget_set_visible(wl, show);
530 widget_set_visible(wf, show);
531 widget_set_visible(wh, show);
534 layout_pair(screen, y, screen->widgets.gateway_l, wf);
535 widget_move(wh, y, help_x);
539 wl = widget_label_base(screen->widgets.url_l);
540 wf = widget_textbox_base(screen->widgets.url_f);
541 wh = widget_label_base(screen->widgets.url_help_l);
542 widget_set_visible(wl, show);
543 widget_set_visible(wf, show);
544 widget_set_visible(wh, show);
547 layout_pair(screen, y, screen->widgets.url_l, wf);
548 widget_move(wh, y, help_x);
552 wh = widget_label_base(screen->widgets.dns_help_l);
553 layout_pair(screen, y, screen->widgets.dns_l,
554 widget_textbox_base(screen->widgets.dns_f));
555 widget_move(wh, y, help_x);
558 /* we show the DNS/DHCP help if we're configuring DHCP */
559 show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
560 wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
561 widget_set_visible(wl, show);
563 widget_move(wl, y, screen->field_x);
569 show = screen->cui->config->safe_mode;
571 widget_move(widget_label_base(screen->widgets.safe_mode),
576 layout_pair(screen, y, screen->widgets.allow_write_l,
577 widget_select_base(screen->widgets.allow_write_f));
578 y += widget_height(widget_select_base(screen->widgets.allow_write_f));
582 widget_move(widget_button_base(screen->widgets.ok_b),
584 widget_move(widget_button_base(screen->widgets.help_b),
585 y, screen->field_x + 14);
586 widget_move(widget_button_base(screen->widgets.cancel_b),
587 y, screen->field_x + 28);
590 static void config_screen_network_change(void *arg, int value)
592 struct config_screen *screen = arg;
593 screen->net_conf_type = value;
594 widgetset_unpost(screen->widgetset);
595 config_screen_layout_widgets(screen);
596 widgetset_post(screen->widgetset);
599 static void config_screen_boot_order_change(void *arg, int value)
602 struct config_screen *screen = arg;
603 widgetset_unpost(screen->widgetset);
604 config_screen_layout_widgets(screen);
605 widgetset_post(screen->widgetset);
608 static void config_screen_autoboot_change(void *arg, int value)
610 struct config_screen *screen = arg;
611 screen->autoboot_enabled = !!value;
612 widgetset_unpost(screen->widgetset);
613 config_screen_layout_widgets(screen);
614 widgetset_post(screen->widgetset);
617 static void config_screen_add_device(void *arg)
619 struct config_screen *screen = arg;
621 screen->show_subset = true;
622 cui_show_subset(screen->cui, _("Select a boot device to add"),
623 screen->widgets.boot_order_f);
626 static void config_screen_autoboot_none(void *arg)
628 struct config_screen *screen = arg;
629 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
631 widget_subset_clear_active(subset);
633 widgetset_unpost(screen->widgetset);
634 config_screen_layout_widgets(screen);
635 widgetset_post(screen->widgetset);
638 static void config_screen_autoboot_any(void *arg)
640 struct config_screen *screen = arg;
641 const struct system_info *sysinfo = screen->cui->sysinfo;
642 struct nc_widget_subset *subset = screen->widgets.boot_order_f;
645 widget_subset_clear_active(subset);
647 idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
649 widget_subset_make_active(screen->widgets.boot_order_f, idx);
651 screen->autoboot_enabled = true;
653 widgetset_unpost(screen->widgetset);
654 config_screen_layout_widgets(screen);
655 widgetset_post(screen->widgetset);
658 static void config_screen_update_subset(void *arg,
659 struct nc_widget_subset *subset, int idx)
661 struct config_screen *screen = arg;
664 widget_subset_make_active(subset, idx);
665 if (!screen->autoboot_enabled)
666 screen->autoboot_enabled = true;
667 config_screen_layout_widgets(screen);
670 static struct interface_config *first_active_interface(
671 const struct config *config)
675 for (i = 0; i < config->network.n_interfaces; i++) {
676 if (config->network.interfaces[i]->ignore)
678 return config->network.interfaces[i];
683 static enum net_conf_type find_net_conf_type(const struct config *config)
685 struct interface_config *ifcfg;
687 ifcfg = first_active_interface(config);
690 return NET_CONF_TYPE_DHCP_ALL;
692 else if (ifcfg->method == CONFIG_METHOD_DHCP)
693 return NET_CONF_TYPE_DHCP_ONE;
695 else if (ifcfg->method == CONFIG_METHOD_STATIC)
696 return NET_CONF_TYPE_STATIC;
699 return NET_CONF_TYPE_DHCP_ALL;
702 static void config_screen_setup_empty(struct config_screen *screen)
704 widget_new_label(screen->widgetset, 2, screen->field_x,
705 _("Waiting for configuration data..."));
706 screen->widgets.cancel_b = widget_new_button(screen->widgetset,
707 4, screen->field_x, 9, _("Cancel"),
708 cancel_click, screen);
711 static int find_autoboot_idx(const struct system_info *sysinfo,
712 struct autoboot_option *opt)
716 if (opt->boot_type == BOOT_DEVICE_TYPE)
717 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
719 for (i = 0; i < sysinfo->n_blockdevs; i++) {
720 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
724 for (i = 0; i < sysinfo->n_interfaces; i++) {
725 struct interface_info *info = sysinfo->interfaces[i];
728 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
730 if (!strcmp(mac, opt->uuid))
731 return sysinfo->n_blockdevs + i;
737 static void config_screen_setup_widgets(struct config_screen *screen,
738 const struct config *config,
739 const struct system_info *sysinfo)
741 struct nc_widgetset *set = screen->widgetset;
742 struct interface_config *ifcfg;
743 char *str, *ip, *mask, *gw, *url;
744 enum net_conf_type type;
746 int add_len, clear_len, any_len, min_len = 20;
748 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
751 type = screen->net_conf_type;
752 ifcfg = first_active_interface(config);
754 screen->autoboot_enabled = config->autoboot_enabled;
756 screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
758 screen->widgets.autoboot_f = widget_new_select(set, 0, 0,
759 COLS - screen->field_x - 1);
761 widget_select_add_option(screen->widgets.autoboot_f, 0, _("Disabled"),
762 !screen->autoboot_enabled);
763 widget_select_add_option(screen->widgets.autoboot_f, 1, _("Enabled"),
764 screen->autoboot_enabled);
766 widget_select_on_change(screen->widgets.autoboot_f,
767 config_screen_autoboot_change, screen);
769 add_len = max(min_len, strncols(_("Add Device")));
770 clear_len = max(min_len, strncols(_("Clear")));
771 any_len = max(min_len, strncols(_("Clear & Boot Any")));
773 screen->widgets.boot_add_b = widget_new_button(set, 0, 0, add_len,
775 config_screen_add_device, screen);
777 screen->widgets.boot_none_b = widget_new_button(set, 0, 0, clear_len,
779 config_screen_autoboot_none, screen);
781 screen->widgets.boot_any_b = widget_new_button(set, 0, 0, any_len,
782 _("Clear & Boot Any"),
783 config_screen_autoboot_any, screen);
785 screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
787 screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
788 COLS - screen->field_x,
789 config_screen_update_subset);
790 screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
793 widget_subset_on_change(screen->widgets.boot_order_f,
794 config_screen_boot_order_change, screen);
796 for (i = 0; i < sysinfo->n_blockdevs; i++) {
797 struct blockdev_info *bd = sysinfo->blockdevs[i];
800 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
803 widget_subset_add_option(screen->widgets.boot_order_f, label);
806 for (i = 0; i < sysinfo->n_interfaces; i++) {
807 struct interface_info *info = sysinfo->interfaces[i];
808 char *label, mac[20];
810 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
812 label = talloc_asprintf(screen, _("net: %s [mac: %s]"),
815 widget_subset_add_option(screen->widgets.boot_order_f, label);
818 for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_UNKNOWN; i++) {
821 if (i == DEVICE_TYPE_ANY)
822 label = talloc_asprintf(screen, _("Any Device"));
824 label = talloc_asprintf(screen, _("Any %s device"),
825 device_type_display_name(i));
827 widget_subset_add_option(screen->widgets.boot_order_f, label);
830 for (i = 0; i < config->n_autoboot_opts; i++) {
831 struct autoboot_option *opt = &config->autoboot_opts[i];
834 idx = find_autoboot_idx(sysinfo, opt);
837 widget_subset_make_active(screen->widgets.boot_order_f,
840 if (opt->boot_type == BOOT_DEVICE_TYPE)
841 pb_log("%s: Unknown autoboot option: %d\n",
842 __func__, opt->type);
844 pb_log("%s: Unknown autoboot UUID: %s\n",
845 __func__, opt->uuid);
850 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
851 screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
852 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
853 screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
856 widget_textbox_set_fixed_size(screen->widgets.timeout_f);
857 widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
859 if (config->ipmi_bootdev) {
860 char *label = talloc_asprintf(screen,
861 _("%s IPMI boot option: %s"),
862 config->ipmi_bootdev_persistent ?
863 "Persistent" : "Temporary",
864 ipmi_bootdev_display_name(config->ipmi_bootdev));
865 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
867 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
869 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
871 screen->ipmi_override = true;
874 screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
875 screen->widgets.network_f = widget_new_select(set, 0, 0,
876 COLS - screen->field_x - 1);
878 widget_select_add_option(screen->widgets.network_f,
879 NET_CONF_TYPE_DHCP_ALL,
880 _("DHCP on all active interfaces"),
881 type == NET_CONF_TYPE_DHCP_ALL);
882 widget_select_add_option(screen->widgets.network_f,
883 NET_CONF_TYPE_DHCP_ONE,
884 _("DHCP on a specific interface"),
885 type == NET_CONF_TYPE_DHCP_ONE);
886 widget_select_add_option(screen->widgets.network_f,
887 NET_CONF_TYPE_STATIC,
888 _("Static IP configuration"),
889 type == NET_CONF_TYPE_STATIC);
891 widget_select_on_change(screen->widgets.network_f,
892 config_screen_network_change, screen);
894 screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
895 screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
897 for (i = 0; i < sysinfo->n_interfaces; i++) {
898 struct interface_info *info = sysinfo->interfaces[i];
899 char str[50], mac[20];
902 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
903 sizeof(ifcfg->hwaddr));
905 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
906 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
907 info->link ? _("link up") : _("link down"));
909 widget_select_add_option(screen->widgets.iface_f,
913 url = gw = ip = mask = NULL;
914 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
917 str = talloc_strdup(screen, ifcfg->static_config.address);
918 sep = strchr(str, '/');
925 gw = ifcfg->static_config.gateway;
926 url = ifcfg->static_config.url;
929 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
930 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
931 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
932 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
933 screen->widgets.ip_addr_mask_help_l =
934 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
936 widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
937 widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
938 widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
939 widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
941 screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
942 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
943 screen->widgets.gateway_help_l =
944 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
946 widget_textbox_set_fixed_size(screen->widgets.gateway_f);
947 widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
949 screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
950 screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
951 screen->widgets.url_help_l =
952 widget_new_label(set, 0, 0, _("(eg. tftp://)"));
954 str = talloc_strdup(screen, "");
955 for (i = 0; i < config->network.n_dns_servers; i++) {
956 str = talloc_asprintf_append(str, "%s%s",
958 config->network.dns_servers[i]);
961 screen->widgets.dns_l = widget_new_label(set, 0, 0,
962 _("DNS Server(s):"));
963 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
964 screen->widgets.dns_help_l =
965 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
967 widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
969 screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
970 _("(if not provided by DHCP server)"));
972 if (config->safe_mode)
973 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
974 _("Selecting 'OK' will exit safe mode"));
976 screen->widgets.allow_write_l = widget_new_label(set, 0, 0,
978 screen->widgets.allow_write_f = widget_new_select(set, 0, 0,
979 COLS - screen->field_x - 1);
981 widget_select_add_option(screen->widgets.allow_write_f, 0,
982 _("Prevent all writes to disk"),
983 !config->allow_writes);
985 widget_select_add_option(screen->widgets.allow_write_f, 1,
986 _("Allow bootloader scripts to modify disks"),
987 config->allow_writes);
989 screen->widgets.ok_b = widget_new_button(set, 0, 0, 10, _("OK"),
991 screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
993 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
994 cancel_click, screen);
997 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
999 struct config_screen *screen = arg;
1000 int w_y, w_height, w_focus, s_max, adjust;
1002 w_height = widget_height(widget);
1003 w_focus = widget_focus_y(widget);
1004 w_y = widget_y(widget) + w_focus;
1005 s_max = getmaxy(screen->scr.sub_ncw) - 1;
1007 if (w_y < screen->scroll_y)
1008 screen->scroll_y = w_y;
1010 else if (w_y + screen->scroll_y + 1 > s_max) {
1011 /* Fit as much of the widget into the screen as possible */
1012 adjust = min(s_max - 1, w_height - w_focus);
1013 if (w_y + adjust >= screen->scroll_y + s_max)
1014 screen->scroll_y = max(0, 1 + w_y + adjust - s_max);
1018 pad_refresh(screen);
1021 static void config_screen_draw(struct config_screen *screen,
1022 const struct config *config,
1023 const struct system_info *sysinfo)
1025 bool repost = false;
1028 /* The size of the pad we'll need depends on the number of interfaces.
1030 * We use N_FIELDS (which is quite conservative, as some fields share
1031 * a line) as a base, then:
1032 * - add 6 (as the network select & boot device select fields take 3
1034 * - add n_interfaces for every field in the network select field, and
1035 * - add (n_blockdevs + n_interfaces) for every field in the boot device
1038 height = N_FIELDS + 6;
1040 height += sysinfo->n_interfaces;
1041 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
1043 if (!screen->pad || getmaxy(screen->pad) < height) {
1045 delwin(screen->pad);
1046 screen->pad = newpad(height, COLS + 10);
1049 if (screen->widgetset) {
1050 widgetset_unpost(screen->widgetset);
1051 talloc_free(screen->widgetset);
1055 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
1057 widgetset_set_widget_focus(screen->widgetset,
1058 config_screen_widget_focus, screen);
1060 if (!config || !sysinfo) {
1061 config_screen_setup_empty(screen);
1063 screen->net_conf_type = find_net_conf_type(config);
1064 config_screen_setup_widgets(screen, config, sysinfo);
1065 config_screen_layout_widgets(screen);
1069 widgetset_post(screen->widgetset);
1072 void config_screen_update(struct config_screen *screen,
1073 const struct config *config,
1074 const struct system_info *sysinfo)
1076 if (screen->cui->current != config_screen_scr(screen)) {
1077 screen->need_update = true;
1081 config_screen_draw(screen, config, sysinfo);
1082 pad_refresh(screen);
1085 static int config_screen_post(struct nc_scr *scr)
1087 struct config_screen *screen = config_screen_from_scr(scr);
1088 screen->show_subset = false;
1090 if (screen->need_update) {
1091 config_screen_draw(screen, screen->cui->config,
1092 screen->cui->sysinfo);
1093 screen->need_update = false;
1095 widgetset_post(screen->widgetset);
1098 nc_scr_frame_draw(scr);
1099 if (screen->need_redraw) {
1100 redrawwin(scr->main_ncw);
1101 screen->need_redraw = false;
1103 wrefresh(screen->scr.main_ncw);
1104 pad_refresh(screen);
1108 static int config_screen_destroy(void *arg)
1110 struct config_screen *screen = arg;
1112 delwin(screen->pad);
1116 struct config_screen *config_screen_init(struct cui *cui,
1117 const struct config *config,
1118 const struct system_info *sysinfo,
1119 void (*on_exit)(struct cui *))
1121 struct config_screen *screen;
1123 screen = talloc_zero(cui, struct config_screen);
1124 talloc_set_destructor(screen, config_screen_destroy);
1125 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1126 cui, config_screen_process_key,
1127 config_screen_post, config_screen_unpost,
1128 config_screen_resize);
1131 screen->on_exit = on_exit;
1132 screen->need_redraw = false;
1133 screen->need_update = false;
1134 screen->label_x = 2;
1135 screen->field_x = 17;
1137 screen->ipmi_override = false;
1138 screen->show_subset = false;
1140 screen->scr.frame.ltitle = talloc_strdup(screen,
1141 _("Petitboot System Configuration"));
1142 screen->scr.frame.rtitle = NULL;
1143 screen->scr.frame.help = talloc_strdup(screen,
1144 _("tab=next, shift+tab=previous, x=exit, h=help"));
1145 nc_scr_frame_draw(&screen->scr);
1147 scrollok(screen->scr.sub_ncw, true);
1149 config_screen_draw(screen, config, sysinfo);