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