Merge remote-tracking rbanch sammj/master
[petitboot] / ui / ncurses / nc-config.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
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.
7  *
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.
12  *
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
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <pb-config/pb-config.h>
27 #include <talloc/talloc.h>
28 #include <types/types.h>
29 #include <log/log.h>
30 #include <i18n/i18n.h>
31
32 #include "nc-cui.h"
33 #include "nc-config.h"
34 #include "nc-widgets.h"
35
36 #define N_FIELDS        32
37
38 extern struct help_text config_help_text;
39
40 enum net_conf_type {
41         NET_CONF_TYPE_DHCP_ALL,
42         NET_CONF_TYPE_DHCP_ONE,
43         NET_CONF_TYPE_STATIC,
44 };
45
46 struct config_screen {
47         struct nc_scr           scr;
48         struct cui              *cui;
49         struct nc_widgetset     *widgetset;
50         WINDOW                  *pad;
51
52         bool                    exit;
53         bool                    show_help;
54         bool                    show_subset;
55         bool                    need_redraw;
56
57         void                    (*on_exit)(struct cui *);
58
59         int                     scroll_y;
60
61         int                     label_x;
62         int                     field_x;
63         int                     network_config_y;
64
65         enum net_conf_type      net_conf_type;
66
67         bool                    autoboot_enabled;
68         bool                    ipmi_override;
69
70         struct {
71                 struct nc_widget_label          *boot_order_l;
72                 struct nc_widget_subset         *boot_order_f;
73                 struct nc_widget_label          *boot_empty_l;
74                 struct nc_widget_button         *boot_add_b;
75                 struct nc_widget_button         *boot_none_b;
76                 struct nc_widget_button         *boot_any_b;
77                 struct nc_widget_textbox        *timeout_f;
78                 struct nc_widget_label          *timeout_l;
79                 struct nc_widget_label          *timeout_help_l;
80
81                 struct nc_widget_label          *ipmi_type_l;
82                 struct nc_widget_label          *ipmi_clear_l;
83                 struct nc_widget_checkbox       *ipmi_clear_cb;
84
85                 struct nc_widget_label          *network_l;
86                 struct nc_widget_select         *network_f;
87
88                 struct nc_widget_label          *iface_l;
89                 struct nc_widget_select         *iface_f;
90                 struct nc_widget_label          *ip_addr_l;
91                 struct nc_widget_textbox        *ip_addr_f;
92                 struct nc_widget_label          *ip_mask_l;
93                 struct nc_widget_textbox        *ip_mask_f;
94                 struct nc_widget_label          *ip_addr_mask_help_l;
95                 struct nc_widget_label          *gateway_l;
96                 struct nc_widget_textbox        *gateway_f;
97                 struct nc_widget_label          *gateway_help_l;
98                 struct nc_widget_label          *dns_l;
99                 struct nc_widget_textbox        *dns_f;
100                 struct nc_widget_label          *dns_dhcp_help_l;
101                 struct nc_widget_label          *dns_help_l;
102
103                 struct nc_widget_label          *safe_mode;
104                 struct nc_widget_button         *ok_b;
105                 struct nc_widget_button         *help_b;
106                 struct nc_widget_button         *cancel_b;
107         } widgets;
108 };
109
110 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
111 {
112         struct config_screen *config_screen;
113
114         assert(scr->sig == pb_config_screen_sig);
115         config_screen = (struct config_screen *)
116                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
117         assert(config_screen->scr.sig == pb_config_screen_sig);
118         return config_screen;
119 }
120
121 static void pad_refresh(struct config_screen *screen)
122 {
123         int y, x, rows, cols;
124
125         getmaxyx(screen->scr.sub_ncw, rows, cols);
126         getbegyx(screen->scr.sub_ncw, y, x);
127
128         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
129 }
130
131 static void config_screen_process_key(struct nc_scr *scr, int key)
132 {
133         struct config_screen *screen = config_screen_from_scr(scr);
134         bool handled;
135
136         handled = widgetset_process_key(screen->widgetset, key);
137
138         if (!handled) {
139                 switch (key) {
140                 case 'x':
141                 case 27: /* esc */
142                         screen->exit = true;
143                         break;
144                 case 'h':
145                         screen->show_help = true;
146                         break;
147                 }
148         }
149
150         if (screen->exit) {
151                 screen->on_exit(screen->cui);
152
153         } else if (screen->show_help) {
154                 screen->show_help = false;
155                 screen->need_redraw = true;
156                 cui_show_help(screen->cui, _("System Configuration"),
157                                 &config_help_text);
158
159         } else if (handled && !screen->show_subset) {
160                 pad_refresh(screen);
161         }
162 }
163
164 static void config_screen_resize(struct nc_scr *scr)
165 {
166         struct config_screen *screen = config_screen_from_scr(scr);
167         (void)screen;
168 }
169
170 static int config_screen_post(struct nc_scr *scr)
171 {
172         struct config_screen *screen = config_screen_from_scr(scr);
173         screen->show_subset = false;
174         widgetset_post(screen->widgetset);
175         nc_scr_frame_draw(scr);
176         if (screen->need_redraw) {
177                 redrawwin(scr->main_ncw);
178                 screen->need_redraw = false;
179         }
180         wrefresh(screen->scr.main_ncw);
181         pad_refresh(screen);
182         return 0;
183 }
184
185 static int config_screen_unpost(struct nc_scr *scr)
186 {
187         struct config_screen *screen = config_screen_from_scr(scr);
188         widgetset_unpost(screen->widgetset);
189         return 0;
190 }
191
192 struct nc_scr *config_screen_scr(struct config_screen *screen)
193 {
194         return &screen->scr;
195 }
196
197 static int screen_process_form(struct config_screen *screen)
198 {
199         const struct system_info *sysinfo = screen->cui->sysinfo;
200         enum net_conf_type net_conf_type;
201         struct interface_config *iface;
202         char *str, *end;
203         struct config *config;
204         int i, n_boot_opts, rc, idx;
205         unsigned int *order;
206         char mac[20];
207
208         config = config_copy(screen, screen->cui->config);
209
210         talloc_free(config->autoboot_opts);
211         config->n_autoboot_opts = 0;
212
213         n_boot_opts = widget_subset_get_order(config, &order,
214                                               screen->widgets.boot_order_f);
215
216         config->autoboot_enabled = n_boot_opts > 0;
217
218         config->n_autoboot_opts = n_boot_opts;
219         config->autoboot_opts = talloc_array(config, struct autoboot_option,
220                                              n_boot_opts);
221
222         for (i = 0; i < n_boot_opts; i++) {
223                 if (order[i] < sysinfo->n_blockdevs) {
224                         /* disk uuid */
225                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
226                         config->autoboot_opts[i].uuid = talloc_strdup(config,
227                                                         sysinfo->blockdevs[order[i]]->uuid);
228                 } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) {
229                         /* net uuid */
230                         order[i] -= sysinfo->n_blockdevs;
231                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID;
232                         mac_str(sysinfo->interfaces[order[i]]->hwaddr,
233                                 sysinfo->interfaces[order[i]]->hwaddr_size,
234                                 mac, sizeof(mac));
235                         config->autoboot_opts[i].uuid = talloc_strdup(config, mac);
236                 } else {
237                         /* device type */
238                         order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces);
239                         config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE;
240                         config->autoboot_opts[i].type = order[i];
241                 }
242         }
243
244         str = widget_textbox_get_value(screen->widgets.timeout_f);
245         if (str) {
246                 unsigned long x;
247                 errno = 0;
248                 x = strtoul(str, &end, 10);
249                 if (!errno && end != str)
250                         config->autoboot_timeout_sec = x;
251         }
252
253         if (screen->ipmi_override)
254                 if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb))
255                         config->ipmi_bootdev = IPMI_BOOTDEV_INVALID;
256
257
258         net_conf_type = widget_select_get_value(screen->widgets.network_f);
259
260         /* if we don't have any network interfaces, prevent per-interface
261          * configuration */
262         if (sysinfo->n_interfaces == 0)
263                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
264
265         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
266                 config->network.n_interfaces = 0;
267
268         } else {
269                 iface = talloc_zero(config, struct interface_config);
270                 config->network.n_interfaces = 1;
271                 config->network.interfaces = talloc_array(config,
272                                 struct interface_config *, 1);
273                 config->network.interfaces[0] = iface;
274
275                 /* copy hwaddr (from the sysinfo interface data) to
276                  * the configuration */
277                 idx = widget_select_get_value(screen->widgets.iface_f);
278                 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
279                                 sizeof(iface->hwaddr));
280         }
281
282         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
283                 iface->method = CONFIG_METHOD_DHCP;
284         }
285
286         if (net_conf_type == NET_CONF_TYPE_STATIC) {
287                 char *ip, *mask, *gateway;
288
289                 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
290                 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
291                 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
292
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);
297                         talloc_free(config);
298                         return -1;
299                 }
300
301                 iface->method = CONFIG_METHOD_STATIC;
302                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
303                                 ip, mask);
304                 iface->static_config.gateway = talloc_strdup(iface, gateway);
305         }
306
307         str = widget_textbox_get_value(screen->widgets.dns_f);
308         talloc_free(config->network.dns_servers);
309         config->network.dns_servers = NULL;
310         config->network.n_dns_servers = 0;
311
312         if (str && strlen(str)) {
313                 char *dns, *tmp;
314                 int i;
315
316                 for (;;) {
317                         dns = strtok_r(str, " \t", &tmp);
318
319                         if (!dns)
320                                 break;
321
322                         i = config->network.n_dns_servers++;
323                         config->network.dns_servers = talloc_realloc(config,
324                                         config->network.dns_servers,
325                                         const char *,
326                                         config->network.n_dns_servers);
327                         config->network.dns_servers[i] =
328                                 talloc_strdup(config, dns);
329
330                         str = NULL;
331                 }
332         }
333
334         config->safe_mode = false;
335         rc = cui_send_config(screen->cui, config);
336         talloc_free(config);
337
338         if (rc)
339                 pb_log("cui_send_config failed!\n");
340         else
341                 pb_debug("config sent!\n");
342
343         return 0;
344 }
345
346 static void ok_click(void *arg)
347 {
348         struct config_screen *screen = arg;
349         if (screen_process_form(screen))
350                 /* errors are written to the status line, so we'll need
351                  * to refresh */
352                 wrefresh(screen->scr.main_ncw);
353         else
354                 screen->exit = true;
355 }
356
357 static void help_click(void *arg)
358 {
359         struct config_screen *screen = arg;
360         screen->show_help = true;
361 }
362
363 static void cancel_click(void *arg)
364 {
365         struct config_screen *screen = arg;
366         screen->exit = true;
367 }
368
369 static int layout_pair(struct config_screen *screen, int y,
370                 struct nc_widget_label *label,
371                 struct nc_widget *field)
372 {
373         struct nc_widget *label_w = widget_label_base(label);
374         widget_move(label_w, y, screen->label_x);
375         widget_move(field, y, screen->field_x);
376         return max(widget_height(label_w), widget_height(field));
377 }
378
379 static void config_screen_layout_widgets(struct config_screen *screen)
380 {
381         struct nc_widget *wl, *wf, *wh;
382         int y, x, help_x;
383         bool show;
384
385         y = 1;
386         help_x = screen->field_x + 2 +
387                 widget_width(widget_textbox_base(screen->widgets.dns_f));
388
389         y += 1;
390
391         wl = widget_label_base(screen->widgets.boot_order_l);
392         widget_set_visible(wl, true);
393         widget_move(wl, y, screen->label_x);
394
395         wf = widget_subset_base(screen->widgets.boot_order_f);
396         widget_move(wf, y, screen->field_x);
397         wl = widget_label_base(screen->widgets.boot_empty_l);
398         widget_move(wl, y, screen->field_x);
399
400         if (widget_subset_height(screen->widgets.boot_order_f)) {
401                 widget_set_visible(wf, true);
402                 widget_set_visible(wl, false);
403                 y += widget_height(wf);
404         } else {
405                 widget_set_visible(wl, true);
406                 widget_set_visible(wf, false);
407                 y += 1;
408         }
409
410         y += 1;
411
412         widget_move(widget_button_base(screen->widgets.boot_add_b),
413                         y, screen->field_x);
414         widget_move(widget_button_base(screen->widgets.boot_any_b),
415                         y, screen->field_x + 12);
416         widget_move(widget_button_base(screen->widgets.boot_none_b),
417                         y, screen->field_x + 30);
418
419         wf = widget_button_base(screen->widgets.boot_add_b);
420         if (widget_subset_n_inactive(screen->widgets.boot_order_f))
421                 widget_set_visible(wf, true);
422         else
423                 widget_set_visible(wf, false);
424
425         y += 2;
426
427         wf = widget_textbox_base(screen->widgets.timeout_f);
428         wl = widget_label_base(screen->widgets.timeout_l);
429         wh = widget_label_base(screen->widgets.timeout_help_l);
430         widget_set_visible(wl, screen->autoboot_enabled);
431         widget_set_visible(wf, screen->autoboot_enabled);
432         widget_set_visible(wh, screen->autoboot_enabled);
433         if (screen->autoboot_enabled) {
434                 widget_set_visible(wh, screen->autoboot_enabled);
435                 widget_move(wl, y, screen->label_x);
436                 widget_move(wf, y, screen->field_x);
437                 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
438                 y += 2;
439         }
440
441         if (screen->ipmi_override) {
442                 wl = widget_label_base(screen->widgets.ipmi_type_l);
443                 widget_set_visible(wl, true);
444                 widget_move(wl, y, screen->label_x);
445                 y += 1;
446
447                 wl = widget_label_base(screen->widgets.ipmi_clear_l);
448                 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
449                 widget_set_visible(wl, true);
450                 widget_set_visible(wf, true);
451                 widget_move(wl, y, screen->label_x);
452                 widget_move(wf, y, screen->field_x);
453                 y += 1;
454         }
455
456         y += 2;
457
458         y += layout_pair(screen, y, screen->widgets.network_l,
459                         widget_select_base(screen->widgets.network_f));
460
461         y += 1;
462
463         /* conditionally show iface select */
464         wl = widget_label_base(screen->widgets.iface_l);
465         wf = widget_select_base(screen->widgets.iface_f);
466
467         show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
468                 screen->net_conf_type == NET_CONF_TYPE_STATIC;
469
470         widget_set_visible(wl, show);
471         widget_set_visible(wf, show);
472
473         if (show)
474                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
475
476         /* conditionally show static IP params */
477         show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
478
479         wl = widget_label_base(screen->widgets.ip_addr_l);
480         wf = widget_textbox_base(screen->widgets.ip_addr_f);
481         widget_set_visible(wl, show);
482         widget_set_visible(wf, show);
483         x = screen->field_x + widget_width(wf) + 1;
484
485         if (show)
486                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
487
488         wl = widget_label_base(screen->widgets.ip_mask_l);
489         wf = widget_textbox_base(screen->widgets.ip_mask_f);
490         widget_set_visible(wl, show);
491         widget_set_visible(wf, show);
492
493         if (show) {
494                 widget_move(wl, y, x);
495                 widget_move(wf, y, x + 2);
496         }
497
498         /* help for IP/mask */
499         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
500         widget_set_visible(wh, show);
501         if (show) {
502                 widget_move(wh, y, help_x);
503                 y++;
504         }
505
506         wl = widget_label_base(screen->widgets.gateway_l);
507         wf = widget_textbox_base(screen->widgets.gateway_f);
508         wh = widget_label_base(screen->widgets.gateway_help_l);
509         widget_set_visible(wl, show);
510         widget_set_visible(wf, show);
511         widget_set_visible(wh, show);
512
513         if (show) {
514                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
515                 widget_move(wh, y, help_x);
516                 y++;
517         }
518
519         wh = widget_label_base(screen->widgets.dns_help_l);
520         layout_pair(screen, y, screen->widgets.dns_l,
521                         widget_textbox_base(screen->widgets.dns_f));
522         widget_move(wh, y, help_x);
523         y++;
524
525         /* we show the DNS/DHCP help if we're configuring DHCP */
526         show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
527         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
528         widget_set_visible(wl, show);
529         if (show) {
530                 widget_move(wl, y, screen->field_x);
531                 y += 1;
532         }
533
534         y += 1;
535
536         show = screen->cui->config->safe_mode;
537         if (show) {
538                 widget_move(widget_label_base(screen->widgets.safe_mode),
539                         y, screen->field_x);
540                 y += 1;
541         }
542
543         widget_move(widget_button_base(screen->widgets.ok_b),
544                         y, screen->field_x);
545         widget_move(widget_button_base(screen->widgets.help_b),
546                         y, screen->field_x + 10);
547         widget_move(widget_button_base(screen->widgets.cancel_b),
548                         y, screen->field_x + 24);
549 }
550
551 static void config_screen_network_change(void *arg, int value)
552 {
553         struct config_screen *screen = arg;
554         screen->net_conf_type = value;
555         widgetset_unpost(screen->widgetset);
556         config_screen_layout_widgets(screen);
557         widgetset_post(screen->widgetset);
558 }
559
560 static void config_screen_boot_order_change(void *arg, int value)
561 {
562         (void)value;
563         struct config_screen *screen = arg;
564         widgetset_unpost(screen->widgetset);
565         config_screen_layout_widgets(screen);
566         widgetset_post(screen->widgetset);
567 }
568
569 static void config_screen_add_device(void *arg)
570 {
571         struct config_screen *screen = arg;
572
573         screen->show_subset = true;
574         cui_show_subset(screen->cui, _("Select an option"),
575                         screen->widgets.boot_order_f);
576 }
577
578 static void config_screen_autoboot_none(void *arg)
579 {
580         struct config_screen *screen = arg;
581         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
582
583         widget_subset_clear_active(subset);
584         screen->autoboot_enabled = false;
585
586         widgetset_unpost(screen->widgetset);
587         config_screen_layout_widgets(screen);
588         widgetset_post(screen->widgetset);
589 }
590
591 static void config_screen_autoboot_any(void *arg)
592 {
593         struct config_screen *screen = arg;
594         const struct system_info *sysinfo = screen->cui->sysinfo;
595         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
596         int idx;
597
598         widget_subset_clear_active(subset);
599
600         idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
601
602         widget_subset_make_active(screen->widgets.boot_order_f, idx);
603
604         screen->autoboot_enabled = true;
605
606         widgetset_unpost(screen->widgetset);
607         config_screen_layout_widgets(screen);
608         widgetset_post(screen->widgetset);
609 }
610
611 static void config_screen_update_subset(void *arg,
612                         struct nc_widget_subset *subset, int idx)
613 {
614         struct config_screen *screen = arg;
615
616         if (idx >= 0)
617                 widget_subset_make_active(subset, idx);
618         if (!screen->autoboot_enabled)
619                 screen->autoboot_enabled = true;
620         config_screen_layout_widgets(screen);
621 }
622
623 static struct interface_config *first_active_interface(
624                 const struct config *config)
625 {
626         unsigned int i;
627
628         for (i = 0; i < config->network.n_interfaces; i++) {
629                 if (config->network.interfaces[i]->ignore)
630                         continue;
631                 return config->network.interfaces[i];
632         }
633         return NULL;
634 }
635
636 static enum net_conf_type find_net_conf_type(const struct config *config)
637 {
638         struct interface_config *ifcfg;
639
640         ifcfg = first_active_interface(config);
641
642         if (!ifcfg)
643                 return NET_CONF_TYPE_DHCP_ALL;
644
645         else if (ifcfg->method == CONFIG_METHOD_DHCP)
646                 return NET_CONF_TYPE_DHCP_ONE;
647
648         else if (ifcfg->method == CONFIG_METHOD_STATIC)
649                 return NET_CONF_TYPE_STATIC;
650
651         assert(0);
652         return NET_CONF_TYPE_DHCP_ALL;
653 }
654
655 static void config_screen_setup_empty(struct config_screen *screen)
656 {
657         widget_new_label(screen->widgetset, 2, screen->field_x,
658                         _("Waiting for configuration data..."));
659         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
660                         4, screen->field_x, 9, _("Cancel"),
661                         cancel_click, screen);
662 }
663
664 static int find_autoboot_idx(const struct system_info *sysinfo,
665                 struct autoboot_option *opt)
666 {
667         unsigned int i;
668
669         if (opt->boot_type == BOOT_DEVICE_TYPE)
670                 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
671
672         for (i = 0; i < sysinfo->n_blockdevs; i++) {
673                 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
674                         return i;
675         }
676
677         for (i = 0; i < sysinfo->n_interfaces; i++) {
678                 struct interface_info *info = sysinfo->interfaces[i];
679                 char mac[20];
680
681                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
682
683                 if (!strcmp(mac, opt->uuid))
684                         return sysinfo->n_blockdevs + i;
685         }
686
687         return -1;
688 }
689
690 static void config_screen_setup_widgets(struct config_screen *screen,
691                 const struct config *config,
692                 const struct system_info *sysinfo)
693 {
694         struct nc_widgetset *set = screen->widgetset;
695         struct interface_config *ifcfg;
696         char *str, *ip, *mask, *gw;
697         enum net_conf_type type;
698         unsigned int i;
699
700         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
701                         == N_FIELDS);
702
703         type = screen->net_conf_type;
704         ifcfg = first_active_interface(config);
705
706         screen->widgets.boot_add_b = widget_new_button(set, 0, 0, 10,
707                                         _("Add Device"), config_screen_add_device,
708                                         screen);
709
710         screen->widgets.boot_none_b = widget_new_button(set, 0, 0, 10,
711                                         _("Clear"),
712                                         config_screen_autoboot_none, screen);
713
714         screen->widgets.boot_any_b = widget_new_button(set, 0, 0, 16,
715                                         _("Clear & Boot Any"), config_screen_autoboot_any,
716                                         screen);
717
718         screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
719                                         _("Boot order:"));
720         screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
721                                         COLS - screen->field_x,
722                                         config_screen_update_subset);
723         screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
724                                         _("(None)"));
725
726         widget_subset_on_change(screen->widgets.boot_order_f,
727                         config_screen_boot_order_change, screen);
728
729         for (i = 0; i < sysinfo->n_blockdevs; i++) {
730                 struct blockdev_info *bd = sysinfo->blockdevs[i];
731                 char *label;
732
733                 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
734                                 bd->name, bd->uuid);
735
736                 widget_subset_add_option(screen->widgets.boot_order_f, label);
737         }
738
739         for (i = 0; i < sysinfo->n_interfaces; i++) {
740                 struct interface_info *info = sysinfo->interfaces[i];
741                 char *label, mac[20];
742
743                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
744
745                 label = talloc_asprintf(screen, _("net:  %s [mac: %s]"),
746                                 info->name, mac);
747
748                 widget_subset_add_option(screen->widgets.boot_order_f, label);
749         }
750
751         for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_NETWORK + 4; i++) {
752                 char *label;
753
754                 if (i == DEVICE_TYPE_ANY)
755                         label = talloc_asprintf(screen, _("Any Device"));
756                 else
757                         label = talloc_asprintf(screen, _("Any %s device"),
758                                                 device_type_display_name(i));
759
760                 widget_subset_add_option(screen->widgets.boot_order_f, label);
761         }
762
763         screen->autoboot_enabled = config->n_autoboot_opts;
764         for (i = 0; i < config->n_autoboot_opts; i++) {
765                 struct autoboot_option *opt = &config->autoboot_opts[i];
766                 int idx;
767
768                 idx = find_autoboot_idx(sysinfo, opt);
769
770                 if (idx >= 0) {
771                         widget_subset_make_active(screen->widgets.boot_order_f,
772                                                   idx);
773                 } else {
774                         if (opt->boot_type == BOOT_DEVICE_TYPE)
775                                 pb_log("%s: Unknown autoboot option: %d\n",
776                                        __func__, opt->type);
777                         else
778                                 pb_log("%s: Unknown autoboot UUID: %s\n",
779                                        __func__, opt->uuid);
780                 }
781         }
782
783
784         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
785         screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
786         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
787         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
788                                         _("seconds"));
789
790         widget_textbox_set_fixed_size(screen->widgets.timeout_f);
791         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
792
793         if (config->ipmi_bootdev) {
794                 char *label = talloc_asprintf(screen,
795                                 _("%s IPMI boot option: %s"),
796                                 config->ipmi_bootdev_persistent ?
797                                 "Persistent" : "Temporary",
798                                 ipmi_bootdev_display_name(config->ipmi_bootdev));
799                 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
800                                                         label);
801                 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
802                                                         _("Clear option:"));
803                 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
804                                                         false);
805                 screen->ipmi_override = true;
806         }
807
808         screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
809         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
810
811         widget_select_add_option(screen->widgets.network_f,
812                                         NET_CONF_TYPE_DHCP_ALL,
813                                         _("DHCP on all active interfaces"),
814                                         type == NET_CONF_TYPE_DHCP_ALL);
815         widget_select_add_option(screen->widgets.network_f,
816                                         NET_CONF_TYPE_DHCP_ONE,
817                                         _("DHCP on a specific interface"),
818                                         type == NET_CONF_TYPE_DHCP_ONE);
819         widget_select_add_option(screen->widgets.network_f,
820                                         NET_CONF_TYPE_STATIC,
821                                         _("Static IP configuration"),
822                                         type == NET_CONF_TYPE_STATIC);
823
824         widget_select_on_change(screen->widgets.network_f,
825                         config_screen_network_change, screen);
826
827         screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
828         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
829
830         for (i = 0; i < sysinfo->n_interfaces; i++) {
831                 struct interface_info *info = sysinfo->interfaces[i];
832                 char str[50], mac[20];
833                 bool is_default;
834
835                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
836                                         sizeof(ifcfg->hwaddr));
837
838                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
839                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
840                                 info->link ? _("link up") : _("link down"));
841
842                 widget_select_add_option(screen->widgets.iface_f,
843                                                 i, str, is_default);
844         }
845
846         gw = ip = mask = NULL;
847         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
848                 char *sep;
849
850                 str = talloc_strdup(screen, ifcfg->static_config.address);
851                 sep = strchr(str, '/');
852                 ip = str;
853
854                 if (sep) {
855                         *sep = '\0';
856                         mask = sep + 1;
857                 }
858                 gw = ifcfg->static_config.gateway;
859         }
860
861         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
862         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
863         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
864         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
865         screen->widgets.ip_addr_mask_help_l =
866                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
867
868         widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
869         widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
870         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
871         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
872
873         screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
874         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
875         screen->widgets.gateway_help_l =
876                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
877
878         widget_textbox_set_fixed_size(screen->widgets.gateway_f);
879         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
880
881         str = talloc_strdup(screen, "");
882         for (i = 0; i < config->network.n_dns_servers; i++) {
883                 str = talloc_asprintf_append(str, "%s%s",
884                                 (i == 0) ? "" : " ",
885                                 config->network.dns_servers[i]);
886         }
887
888         screen->widgets.dns_l = widget_new_label(set, 0, 0,
889                                         _("DNS Server(s):"));
890         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
891         screen->widgets.dns_help_l =
892                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
893
894         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
895
896         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
897                         _("(if not provided by DHCP server)"));
898
899         if (config->safe_mode)
900                 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
901                          _("Selecting 'OK' will exit safe mode"));
902
903         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, _("OK"),
904                         ok_click, screen);
905         screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
906                         help_click, screen);
907         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
908                         cancel_click, screen);
909 }
910
911 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
912 {
913         struct config_screen *screen = arg;
914         int w_y, s_max;
915
916         w_y = widget_y(widget) + widget_focus_y(widget);
917         s_max = getmaxy(screen->scr.sub_ncw) - 1;
918
919         if (w_y < screen->scroll_y)
920                 screen->scroll_y = w_y;
921
922         else if (w_y + screen->scroll_y + 1 > s_max)
923                 screen->scroll_y = 1 + w_y - s_max;
924
925         else
926                 return;
927
928         pad_refresh(screen);
929 }
930
931 static void config_screen_draw(struct config_screen *screen,
932                 const struct config *config,
933                 const struct system_info *sysinfo)
934 {
935         bool repost = false;
936         int height;
937
938         /* The size of the pad we'll need depends on the number of interfaces.
939          *
940          * We use N_FIELDS (which is quite conservative, as some fields share
941          * a line) as a base, then:
942          * - add 6 (as the network select & boot device select fields take 3
943          *   lines each),
944          * - add n_interfaces for every field in the network select field, and
945          * - add (n_blockdevs + n_interfaces) for every field in the boot device
946          *   select field
947          */
948         height = N_FIELDS + 6;
949         if (sysinfo) {
950                 height += sysinfo->n_interfaces;
951                 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
952         }
953         if (!screen->pad || getmaxy(screen->pad) < height) {
954                 if (screen->pad)
955                         delwin(screen->pad);
956                 screen->pad = newpad(height, COLS + 10);
957         }
958
959         if (screen->widgetset) {
960                 widgetset_unpost(screen->widgetset);
961                 talloc_free(screen->widgetset);
962                 repost = true;
963         }
964
965         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
966                         screen->pad);
967         widgetset_set_widget_focus(screen->widgetset,
968                         config_screen_widget_focus, screen);
969
970         if (!config || !sysinfo) {
971                 config_screen_setup_empty(screen);
972         } else {
973                 screen->net_conf_type = find_net_conf_type(config);
974                 config_screen_setup_widgets(screen, config, sysinfo);
975                 config_screen_layout_widgets(screen);
976         }
977
978         if (repost)
979                 widgetset_post(screen->widgetset);
980 }
981
982 void config_screen_update(struct config_screen *screen,
983                 const struct config *config,
984                 const struct system_info *sysinfo)
985 {
986         config_screen_draw(screen, config, sysinfo);
987         pad_refresh(screen);
988 }
989
990 static int config_screen_destroy(void *arg)
991 {
992         struct config_screen *screen = arg;
993         if (screen->pad)
994                 delwin(screen->pad);
995         return 0;
996 }
997
998 struct config_screen *config_screen_init(struct cui *cui,
999                 const struct config *config,
1000                 const struct system_info *sysinfo,
1001                 void (*on_exit)(struct cui *))
1002 {
1003         struct config_screen *screen;
1004
1005         screen = talloc_zero(cui, struct config_screen);
1006         talloc_set_destructor(screen, config_screen_destroy);
1007         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1008                         cui, config_screen_process_key,
1009                         config_screen_post, config_screen_unpost,
1010                         config_screen_resize);
1011
1012         screen->cui = cui;
1013         screen->on_exit = on_exit;
1014         screen->need_redraw = false;
1015         screen->label_x = 2;
1016         screen->field_x = 17;
1017
1018         screen->ipmi_override = false;
1019         screen->show_subset = false;
1020
1021         screen->scr.frame.ltitle = talloc_strdup(screen,
1022                         _("Petitboot System Configuration"));
1023         screen->scr.frame.rtitle = NULL;
1024         screen->scr.frame.help = talloc_strdup(screen,
1025                         _("tab=next, shift+tab=previous, x=exit, h=help"));
1026         nc_scr_frame_draw(&screen->scr);
1027
1028         scrollok(screen->scr.sub_ncw, true);
1029
1030         config_screen_draw(screen, config, sysinfo);
1031
1032         return screen;
1033 }