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