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