ui/ncurses: Display current ipmi bootdev settings
[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         if (screen->autoboot_enabled) {
433                 widget_set_visible(wh, screen->autoboot_enabled);
434                 widget_move(wl, y, screen->label_x);
435                 widget_move(wf, y, screen->field_x);
436                 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
437                 y += 2;
438         }
439
440         if (screen->ipmi_override) {
441                 wl = widget_label_base(screen->widgets.ipmi_type_l);
442                 widget_set_visible(wl, true);
443                 widget_move(wl, y, screen->label_x);
444                 y += 1;
445
446                 wl = widget_label_base(screen->widgets.ipmi_clear_l);
447                 wf = widget_checkbox_base(screen->widgets.ipmi_clear_cb);
448                 widget_set_visible(wl, true);
449                 widget_set_visible(wf, true);
450                 widget_move(wl, y, screen->label_x);
451                 widget_move(wf, y, screen->field_x);
452                 y += 1;
453         }
454
455         y += 2;
456
457         y += layout_pair(screen, y, screen->widgets.network_l,
458                         widget_select_base(screen->widgets.network_f));
459
460         y += 1;
461
462         /* conditionally show iface select */
463         wl = widget_label_base(screen->widgets.iface_l);
464         wf = widget_select_base(screen->widgets.iface_f);
465
466         show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
467                 screen->net_conf_type == NET_CONF_TYPE_STATIC;
468
469         widget_set_visible(wl, show);
470         widget_set_visible(wf, show);
471
472         if (show)
473                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
474
475         /* conditionally show static IP params */
476         show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
477
478         wl = widget_label_base(screen->widgets.ip_addr_l);
479         wf = widget_textbox_base(screen->widgets.ip_addr_f);
480         widget_set_visible(wl, show);
481         widget_set_visible(wf, show);
482         x = screen->field_x + widget_width(wf) + 1;
483
484         if (show)
485                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
486
487         wl = widget_label_base(screen->widgets.ip_mask_l);
488         wf = widget_textbox_base(screen->widgets.ip_mask_f);
489         widget_set_visible(wl, show);
490         widget_set_visible(wf, show);
491
492         if (show) {
493                 widget_move(wl, y, x);
494                 widget_move(wf, y, x + 2);
495         }
496
497         /* help for IP/mask */
498         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
499         widget_set_visible(wh, show);
500         if (show) {
501                 widget_move(wh, y, help_x);
502                 y++;
503         }
504
505         wl = widget_label_base(screen->widgets.gateway_l);
506         wf = widget_textbox_base(screen->widgets.gateway_f);
507         wh = widget_label_base(screen->widgets.gateway_help_l);
508         widget_set_visible(wl, show);
509         widget_set_visible(wf, show);
510         widget_set_visible(wh, show);
511
512         if (show) {
513                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
514                 widget_move(wh, y, help_x);
515                 y++;
516         }
517
518         wh = widget_label_base(screen->widgets.dns_help_l);
519         layout_pair(screen, y, screen->widgets.dns_l,
520                         widget_textbox_base(screen->widgets.dns_f));
521         widget_move(wh, y, help_x);
522         y++;
523
524         /* we show the DNS/DHCP help if we're configuring DHCP */
525         show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
526         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
527         widget_set_visible(wl, show);
528         if (show) {
529                 widget_move(wl, y, screen->field_x);
530                 y += 1;
531         }
532
533         y += 1;
534
535         show = screen->cui->config->safe_mode;
536         if (show) {
537                 widget_move(widget_label_base(screen->widgets.safe_mode),
538                         y, screen->field_x);
539                 y += 1;
540         }
541
542         widget_move(widget_button_base(screen->widgets.ok_b),
543                         y, screen->field_x);
544         widget_move(widget_button_base(screen->widgets.help_b),
545                         y, screen->field_x + 10);
546         widget_move(widget_button_base(screen->widgets.cancel_b),
547                         y, screen->field_x + 24);
548 }
549
550 static void config_screen_network_change(void *arg, int value)
551 {
552         struct config_screen *screen = arg;
553         screen->net_conf_type = value;
554         widgetset_unpost(screen->widgetset);
555         config_screen_layout_widgets(screen);
556         widgetset_post(screen->widgetset);
557 }
558
559 static void config_screen_boot_order_change(void *arg, int value)
560 {
561         (void)value;
562         struct config_screen *screen = arg;
563         widgetset_unpost(screen->widgetset);
564         config_screen_layout_widgets(screen);
565         widgetset_post(screen->widgetset);
566 }
567
568 static void config_screen_add_device(void *arg)
569 {
570         struct config_screen *screen = arg;
571
572         screen->show_subset = true;
573         cui_show_subset(screen->cui, _("Select an option"),
574                         screen->widgets.boot_order_f);
575 }
576
577 static void config_screen_autoboot_none(void *arg)
578 {
579         struct config_screen *screen = arg;
580         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
581
582         widget_subset_clear_active(subset);
583         screen->autoboot_enabled = false;
584
585         widgetset_unpost(screen->widgetset);
586         config_screen_layout_widgets(screen);
587         widgetset_post(screen->widgetset);
588 }
589
590 static void config_screen_autoboot_any(void *arg)
591 {
592         struct config_screen *screen = arg;
593         const struct system_info *sysinfo = screen->cui->sysinfo;
594         struct nc_widget_subset *subset = screen->widgets.boot_order_f;
595         int idx;
596
597         widget_subset_clear_active(subset);
598
599         idx = sysinfo->n_blockdevs + sysinfo->n_interfaces + DEVICE_TYPE_ANY;
600
601         widget_subset_make_active(screen->widgets.boot_order_f, idx);
602
603         screen->autoboot_enabled = true;
604
605         widgetset_unpost(screen->widgetset);
606         config_screen_layout_widgets(screen);
607         widgetset_post(screen->widgetset);
608 }
609
610 static void config_screen_update_subset(void *arg,
611                         struct nc_widget_subset *subset, int idx)
612 {
613         struct config_screen *screen = arg;
614
615         if (idx >= 0)
616                 widget_subset_make_active(subset, idx);
617         if (!screen->autoboot_enabled)
618                 screen->autoboot_enabled = true;
619         config_screen_layout_widgets(screen);
620 }
621
622 static struct interface_config *first_active_interface(
623                 const struct config *config)
624 {
625         unsigned int i;
626
627         for (i = 0; i < config->network.n_interfaces; i++) {
628                 if (config->network.interfaces[i]->ignore)
629                         continue;
630                 return config->network.interfaces[i];
631         }
632         return NULL;
633 }
634
635 static enum net_conf_type find_net_conf_type(const struct config *config)
636 {
637         struct interface_config *ifcfg;
638
639         ifcfg = first_active_interface(config);
640
641         if (!ifcfg)
642                 return NET_CONF_TYPE_DHCP_ALL;
643
644         else if (ifcfg->method == CONFIG_METHOD_DHCP)
645                 return NET_CONF_TYPE_DHCP_ONE;
646
647         else if (ifcfg->method == CONFIG_METHOD_STATIC)
648                 return NET_CONF_TYPE_STATIC;
649
650         assert(0);
651         return NET_CONF_TYPE_DHCP_ALL;
652 }
653
654 static void config_screen_setup_empty(struct config_screen *screen)
655 {
656         widget_new_label(screen->widgetset, 2, screen->field_x,
657                         _("Waiting for configuration data..."));
658         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
659                         4, screen->field_x, 9, _("Cancel"),
660                         cancel_click, screen);
661 }
662
663 static int find_autoboot_idx(const struct system_info *sysinfo,
664                 struct autoboot_option *opt)
665 {
666         unsigned int i;
667
668         if (opt->boot_type == BOOT_DEVICE_TYPE)
669                 return sysinfo->n_blockdevs + sysinfo->n_interfaces + opt->type;
670
671         for (i = 0; i < sysinfo->n_blockdevs; i++) {
672                 if (!strcmp(sysinfo->blockdevs[i]->uuid, opt->uuid))
673                         return i;
674         }
675
676         for (i = 0; i < sysinfo->n_interfaces; i++) {
677                 struct interface_info *info = sysinfo->interfaces[i];
678                 char mac[20];
679
680                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
681
682                 if (!strcmp(mac, opt->uuid))
683                         return sysinfo->n_blockdevs + i;
684         }
685
686         return -1;
687 }
688
689 static void config_screen_setup_widgets(struct config_screen *screen,
690                 const struct config *config,
691                 const struct system_info *sysinfo)
692 {
693         struct nc_widgetset *set = screen->widgetset;
694         struct interface_config *ifcfg;
695         char *str, *ip, *mask, *gw;
696         enum net_conf_type type;
697         unsigned int i;
698
699         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
700                         == N_FIELDS);
701
702         type = screen->net_conf_type;
703         ifcfg = first_active_interface(config);
704
705         screen->widgets.boot_add_b = widget_new_button(set, 0, 0, 10,
706                                         _("Add Device"), config_screen_add_device,
707                                         screen);
708
709         screen->widgets.boot_none_b = widget_new_button(set, 0, 0, 10,
710                                         _("Clear"),
711                                         config_screen_autoboot_none, screen);
712
713         screen->widgets.boot_any_b = widget_new_button(set, 0, 0, 16,
714                                         _("Clear & Boot Any"), config_screen_autoboot_any,
715                                         screen);
716
717         screen->widgets.boot_order_l = widget_new_label(set, 0, 0,
718                                         _("Boot order:"));
719         screen->widgets.boot_order_f = widget_new_subset(set, 0, 0,
720                                         COLS - screen->field_x,
721                                         config_screen_update_subset);
722         screen->widgets.boot_empty_l = widget_new_label(set, 0, 0,
723                                         _("(None)"));
724
725         widget_subset_on_change(screen->widgets.boot_order_f,
726                         config_screen_boot_order_change, screen);
727
728         for (i = 0; i < sysinfo->n_blockdevs; i++) {
729                 struct blockdev_info *bd = sysinfo->blockdevs[i];
730                 char *label;
731
732                 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
733                                 bd->name, bd->uuid);
734
735                 widget_subset_add_option(screen->widgets.boot_order_f, label);
736         }
737
738         for (i = 0; i < sysinfo->n_interfaces; i++) {
739                 struct interface_info *info = sysinfo->interfaces[i];
740                 char *label, mac[20];
741
742                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
743
744                 label = talloc_asprintf(screen, _("net:  %s [mac: %s]"),
745                                 info->name, mac);
746
747                 widget_subset_add_option(screen->widgets.boot_order_f, label);
748         }
749
750         for (i = DEVICE_TYPE_NETWORK; i < DEVICE_TYPE_NETWORK + 4; i++) {
751                 char *label;
752
753                 if (i == DEVICE_TYPE_ANY)
754                         label = talloc_asprintf(screen, _("Any Device"));
755                 else
756                         label = talloc_asprintf(screen, _("Any %s device"),
757                                                 device_type_display_name(i));
758
759                 widget_subset_add_option(screen->widgets.boot_order_f, label);
760         }
761
762         screen->autoboot_enabled = config->n_autoboot_opts;
763         for (i = 0; i < config->n_autoboot_opts; i++) {
764                 struct autoboot_option *opt = &config->autoboot_opts[i];
765                 int idx;
766
767                 idx = find_autoboot_idx(sysinfo, opt);
768
769                 if (idx >= 0) {
770                         widget_subset_make_active(screen->widgets.boot_order_f,
771                                                   idx);
772                 } else {
773                         if (opt->boot_type == BOOT_DEVICE_TYPE)
774                                 pb_log("%s: Unknown autoboot option: %d\n",
775                                        __func__, opt->type);
776                         else
777                                 pb_log("%s: Unknown autoboot UUID: %s\n",
778                                        __func__, opt->uuid);
779                 }
780         }
781
782
783         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
784         screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
785         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
786         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
787                                         _("seconds"));
788
789         widget_textbox_set_fixed_size(screen->widgets.timeout_f);
790         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
791
792         if (config->ipmi_bootdev) {
793                 char *label = talloc_asprintf(screen,
794                                 _("%s IPMI boot option: %s"),
795                                 config->ipmi_bootdev_persistent ?
796                                 "Persistent" : "Temporary",
797                                 ipmi_bootdev_display_name(config->ipmi_bootdev));
798                 screen->widgets.ipmi_type_l = widget_new_label(set, 0, 0,
799                                                         label);
800                 screen->widgets.ipmi_clear_l = widget_new_label(set, 0, 0,
801                                                         _("Clear option:"));
802                 screen->widgets.ipmi_clear_cb = widget_new_checkbox(set, 0, 0,
803                                                         false);
804                 screen->ipmi_override = true;
805         }
806
807         screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
808         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
809
810         widget_select_add_option(screen->widgets.network_f,
811                                         NET_CONF_TYPE_DHCP_ALL,
812                                         _("DHCP on all active interfaces"),
813                                         type == NET_CONF_TYPE_DHCP_ALL);
814         widget_select_add_option(screen->widgets.network_f,
815                                         NET_CONF_TYPE_DHCP_ONE,
816                                         _("DHCP on a specific interface"),
817                                         type == NET_CONF_TYPE_DHCP_ONE);
818         widget_select_add_option(screen->widgets.network_f,
819                                         NET_CONF_TYPE_STATIC,
820                                         _("Static IP configuration"),
821                                         type == NET_CONF_TYPE_STATIC);
822
823         widget_select_on_change(screen->widgets.network_f,
824                         config_screen_network_change, screen);
825
826         screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
827         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
828
829         for (i = 0; i < sysinfo->n_interfaces; i++) {
830                 struct interface_info *info = sysinfo->interfaces[i];
831                 char str[50], mac[20];
832                 bool is_default;
833
834                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
835                                         sizeof(ifcfg->hwaddr));
836
837                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
838                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
839                                 info->link ? _("link up") : _("link down"));
840
841                 widget_select_add_option(screen->widgets.iface_f,
842                                                 i, str, is_default);
843         }
844
845         gw = ip = mask = NULL;
846         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
847                 char *sep;
848
849                 str = talloc_strdup(screen, ifcfg->static_config.address);
850                 sep = strchr(str, '/');
851                 ip = str;
852
853                 if (sep) {
854                         *sep = '\0';
855                         mask = sep + 1;
856                 }
857                 gw = ifcfg->static_config.gateway;
858         }
859
860         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
861         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
862         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
863         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
864         screen->widgets.ip_addr_mask_help_l =
865                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
866
867         widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
868         widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
869         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
870         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
871
872         screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
873         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
874         screen->widgets.gateway_help_l =
875                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
876
877         widget_textbox_set_fixed_size(screen->widgets.gateway_f);
878         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
879
880         str = talloc_strdup(screen, "");
881         for (i = 0; i < config->network.n_dns_servers; i++) {
882                 str = talloc_asprintf_append(str, "%s%s",
883                                 (i == 0) ? "" : " ",
884                                 config->network.dns_servers[i]);
885         }
886
887         screen->widgets.dns_l = widget_new_label(set, 0, 0,
888                                         _("DNS Server(s):"));
889         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
890         screen->widgets.dns_help_l =
891                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
892
893         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
894
895         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
896                         _("(if not provided by DHCP server)"));
897
898         if (config->safe_mode)
899                 screen->widgets.safe_mode = widget_new_label(set, 0, 0,
900                          _("Selecting 'OK' will exit safe mode"));
901
902         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, _("OK"),
903                         ok_click, screen);
904         screen->widgets.help_b = widget_new_button(set, 0, 0, 10, _("Help"),
905                         help_click, screen);
906         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 10, _("Cancel"),
907                         cancel_click, screen);
908 }
909
910 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
911 {
912         struct config_screen *screen = arg;
913         int w_y, s_max;
914
915         w_y = widget_y(widget) + widget_focus_y(widget);
916         s_max = getmaxy(screen->scr.sub_ncw) - 1;
917
918         if (w_y < screen->scroll_y)
919                 screen->scroll_y = w_y;
920
921         else if (w_y + screen->scroll_y + 1 > s_max)
922                 screen->scroll_y = 1 + w_y - s_max;
923
924         else
925                 return;
926
927         pad_refresh(screen);
928 }
929
930 static void config_screen_draw(struct config_screen *screen,
931                 const struct config *config,
932                 const struct system_info *sysinfo)
933 {
934         bool repost = false;
935         int height;
936
937         /* The size of the pad we'll need depends on the number of interfaces.
938          *
939          * We use N_FIELDS (which is quite conservative, as some fields share
940          * a line) as a base, then:
941          * - add 6 (as the network select & boot device select fields take 3
942          *   lines each),
943          * - add n_interfaces for every field in the network select field, and
944          * - add (n_blockdevs + n_interfaces) for every field in the boot device
945          *   select field
946          */
947         height = N_FIELDS + 6;
948         if (sysinfo) {
949                 height += sysinfo->n_interfaces;
950                 height += (sysinfo->n_blockdevs + sysinfo->n_interfaces);
951         }
952         if (!screen->pad || getmaxy(screen->pad) < height) {
953                 if (screen->pad)
954                         delwin(screen->pad);
955                 screen->pad = newpad(height, COLS + 10);
956         }
957
958         if (screen->widgetset) {
959                 widgetset_unpost(screen->widgetset);
960                 talloc_free(screen->widgetset);
961                 repost = true;
962         }
963
964         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
965                         screen->pad);
966         widgetset_set_widget_focus(screen->widgetset,
967                         config_screen_widget_focus, screen);
968
969         if (!config || !sysinfo) {
970                 config_screen_setup_empty(screen);
971         } else {
972                 screen->net_conf_type = find_net_conf_type(config);
973                 config_screen_setup_widgets(screen, config, sysinfo);
974                 config_screen_layout_widgets(screen);
975         }
976
977         if (repost)
978                 widgetset_post(screen->widgetset);
979 }
980
981 void config_screen_update(struct config_screen *screen,
982                 const struct config *config,
983                 const struct system_info *sysinfo)
984 {
985         config_screen_draw(screen, config, sysinfo);
986         pad_refresh(screen);
987 }
988
989 static int config_screen_destroy(void *arg)
990 {
991         struct config_screen *screen = arg;
992         if (screen->pad)
993                 delwin(screen->pad);
994         return 0;
995 }
996
997 struct config_screen *config_screen_init(struct cui *cui,
998                 const struct config *config,
999                 const struct system_info *sysinfo,
1000                 void (*on_exit)(struct cui *))
1001 {
1002         struct config_screen *screen;
1003
1004         screen = talloc_zero(cui, struct config_screen);
1005         talloc_set_destructor(screen, config_screen_destroy);
1006         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
1007                         cui, config_screen_process_key,
1008                         config_screen_post, config_screen_unpost,
1009                         config_screen_resize);
1010
1011         screen->cui = cui;
1012         screen->on_exit = on_exit;
1013         screen->need_redraw = false;
1014         screen->label_x = 2;
1015         screen->field_x = 17;
1016
1017         screen->ipmi_override = false;
1018         screen->show_subset = false;
1019
1020         screen->scr.frame.ltitle = talloc_strdup(screen,
1021                         _("Petitboot System Configuration"));
1022         screen->scr.frame.rtitle = NULL;
1023         screen->scr.frame.help = talloc_strdup(screen,
1024                         _("tab=next, shift+tab=previous, x=exit, h=help"));
1025         nc_scr_frame_draw(&screen->scr);
1026
1027         scrollok(screen->scr.sub_ncw, true);
1028
1029         config_screen_draw(screen, config, sysinfo);
1030
1031         return screen;
1032 }