]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-cui.c
Add support for GPG signature enforcement on booted
[petitboot] / ui / ncurses / nc-cui.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28
29 #include "log/log.h"
30 #include "pb-protocol/pb-protocol.h"
31 #include "talloc/talloc.h"
32 #include "waiter/waiter.h"
33 #include "process/process.h"
34 #include "i18n/i18n.h"
35 #include "ui/common/discover-client.h"
36 #include "ui/common/ui-system.h"
37 #include "nc-cui.h"
38 #include "nc-boot-editor.h"
39 #include "nc-config.h"
40 #include "nc-add-url.h"
41 #include "nc-sysinfo.h"
42 #include "nc-lang.h"
43 #include "nc-helpscreen.h"
44 #include "nc-subset.h"
45
46 extern const struct help_text main_menu_help_text;
47
48 static struct pmenu *main_menu_init(struct cui *cui);
49
50 static void cui_start(void)
51 {
52         initscr();                      /* Initialize ncurses. */
53         cbreak();                       /* Disable line buffering. */
54         noecho();                       /* Disable getch() echo. */
55         keypad(stdscr, TRUE);           /* Enable num keypad keys. */
56         nonl();                         /* Disable new-line translation. */
57         intrflush(stdscr, FALSE);       /* Disable interrupt flush. */
58         curs_set(0);                    /* Make cursor invisible */
59         nodelay(stdscr, TRUE);          /* Enable non-blocking getch() */
60
61         /* We may be operating with an incorrect $TERM type; in this case
62          * the keymappings will be slightly broken. We want at least
63          * backspace to work though, so we'll define both DEL and ^H to
64          * map to backspace */
65         define_key("\x7f", KEY_BACKSPACE);
66         define_key("\x08", KEY_BACKSPACE);
67
68         /* we need backtab too, for form navigation. vt220 doesn't include
69          * this (kcbt), but we don't want to require a full linux/xterm termcap
70          */
71         define_key("\x1b[Z", KEY_BTAB);
72
73         /* We'll define a few other keys too since they're commonly
74          * used for navigation but the escape character will cause
75          * Petitboot to exit if they're left undefined */
76         define_key("\x1b\x5b\x35\x7e", KEY_PPAGE);
77         define_key("\x1b\x5b\x36\x7e", KEY_NPAGE);
78         define_key("\x1b\x5b\x31\x7e", KEY_HOME);
79         define_key("\x1b\x5b\x34\x7e", KEY_END);
80         define_key("\x1b\x4f\x48", KEY_HOME);
81         define_key("\x1b\x4f\x46", KEY_END);
82         define_key("OH", KEY_HOME);
83         define_key("OF", KEY_END);
84         define_key("\x1b\x5b\x41", KEY_UP);
85         define_key("\x1b\x5b\x42", KEY_DOWN);
86         define_key("\x1b\x5b\x33\x7e", KEY_DC);
87
88         while (getch() != ERR)          /* flush stdin */
89                 (void)0;
90 }
91
92 static void cui_atexit(void)
93 {
94         clear();
95         refresh();
96         endwin();
97 }
98
99 /**
100  * cui_abort - Signal the main cui program loop to exit.
101  *
102  * Sets cui.abort, which causes the cui_run() routine to return.
103  */
104
105 void cui_abort(struct cui *cui)
106 {
107         pb_log("%s: exiting\n", __func__);
108         cui->abort = 1;
109 }
110
111 /**
112  * cui_resize - Signal the main cui program loop to resize
113  *
114  * Called at SIGWINCH.
115  */
116
117 void cui_resize(struct cui *cui)
118 {
119         pb_debug("%s: resizing\n", __func__);
120         cui->resize = 1;
121 }
122
123 /**
124  * cui_on_exit - A generic main menu exit callback.
125  */
126
127 void cui_on_exit(struct pmenu *menu)
128 {
129         cui_abort(cui_from_pmenu(menu));
130 }
131
132 /**
133  * cui_run_cmd - A generic cb to run the supplied command.
134  */
135
136 int cui_run_cmd(struct pmenu_item *item)
137 {
138         int result;
139         struct cui *cui = cui_from_item(item);
140         const char **cmd_argv = item->data;
141
142         nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
143
144         def_prog_mode();
145
146         result = process_run_simple_argv(item, cmd_argv);
147
148         reset_prog_mode();
149         redrawwin(cui->current->main_ncw);
150
151         if (result) {
152                 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
153                 nc_scr_status_printf(cui->current, _("Failed: %s"),
154                                 cmd_argv[0]);
155         }
156
157         return result;
158 }
159
160 /**
161  * cui_boot - A generic cb to run kexec.
162  */
163
164 static int cui_boot(struct pmenu_item *item)
165 {
166         int result;
167         struct cui *cui = cui_from_item(item);
168         struct cui_opt_data *cod = cod_from_item(item);
169
170         assert(cui->current == &cui->main->scr);
171
172         pb_debug("%s: %s\n", __func__, cod->name);
173
174         nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
175
176         result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
177
178         if (result) {
179                 nc_scr_status_printf(cui->current,
180                                 _("Failed: boot %s"), cod->bd->image);
181         }
182
183         return 0;
184 }
185
186 static void cui_boot_editor_on_exit(struct cui *cui,
187                 struct pmenu_item *item,
188                 struct pb_boot_data *bd)
189 {
190         struct pmenu *menu = cui->main;
191         struct cui_opt_data *cod;
192         int idx, top, rows, cols;
193         static int user_idx = 0;
194
195         /* Was the edit cancelled? */
196         if (!bd) {
197                 cui_set_current(cui, &cui->main->scr);
198                 talloc_free(cui->boot_editor);
199                 cui->boot_editor = NULL;
200                 return;
201         }
202
203         /* Is this was a new item, we'll need to update the menu */
204         if (!item) {
205                 int insert_pt;
206
207                 cod = talloc_zero(NULL, struct cui_opt_data);
208                 cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
209
210                 item = pmenu_item_create(menu, cod->name);
211                 if (!item) {
212                         talloc_free(cod);
213                         goto out;
214                 }
215
216                 item->on_edit = cui_item_edit;
217                 item->on_execute = cui_boot;
218                 item->data = cod;
219
220                 talloc_steal(item, cod);
221
222                 /* Detach the items array. */
223                 set_menu_items(menu->ncm, NULL);
224
225                 /* Insert new item at insert_pt. */
226                 insert_pt = pmenu_grow(menu, 1);
227                 pmenu_item_insert(menu, item, insert_pt);
228
229                 /* Re-attach the items array. */
230                 set_menu_items(menu->ncm, menu->items);
231
232                 /* If our index is above the current top row, align
233                  * us to the new top. Otherwise, align us to the new
234                  * bottom */
235                 menu_format(cui->main->ncm, &rows, &cols);
236                 top = top_row(cui->main->ncm);
237                 idx = item_index(item->nci);
238
239                 if (top >= idx)
240                         top = idx;
241                 else
242                         top = idx < rows ? 0 : idx - rows + 1;
243
244                 set_top_row(cui->main->ncm, top);
245                 set_current_item(item->pmenu->ncm, item->nci);
246
247                 nc_scr_post(&menu->scr);
248         } else {
249                 cod = item->data;
250         }
251
252         cod->bd = talloc_steal(cod, bd);
253
254 out:
255         cui_set_current(cui, &cui->main->scr);
256         talloc_free(cui->boot_editor);
257         cui->boot_editor = NULL;
258 }
259
260 void cui_item_edit(struct pmenu_item *item)
261 {
262         struct cui *cui = cui_from_item(item);
263         cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
264                                         cui_boot_editor_on_exit);
265         cui_set_current(cui, boot_editor_scr(cui->boot_editor));
266 }
267
268 void cui_item_new(struct pmenu *menu)
269 {
270         struct cui *cui = cui_from_pmenu(menu);
271         cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
272                                         cui_boot_editor_on_exit);
273         cui_set_current(cui, boot_editor_scr(cui->boot_editor));
274 }
275
276 static void cui_sysinfo_exit(struct cui *cui)
277 {
278         cui_set_current(cui, &cui->main->scr);
279         talloc_free(cui->sysinfo_screen);
280         cui->sysinfo_screen = NULL;
281 }
282
283 void cui_show_sysinfo(struct cui *cui)
284 {
285         cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
286                         cui_sysinfo_exit);
287         cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
288 }
289
290 static void cui_config_exit(struct cui *cui)
291 {
292         cui_set_current(cui, &cui->main->scr);
293         talloc_free(cui->config_screen);
294         cui->config_screen = NULL;
295 }
296
297 void cui_show_config(struct cui *cui)
298 {
299         cui->config_screen = config_screen_init(cui, cui->config,
300                         cui->sysinfo, cui_config_exit);
301         cui_set_current(cui, config_screen_scr(cui->config_screen));
302 }
303
304 static void cui_lang_exit(struct cui *cui)
305 {
306         cui_set_current(cui, &cui->main->scr);
307         talloc_free(cui->lang_screen);
308         cui->lang_screen = NULL;
309 }
310
311 void cui_show_lang(struct cui *cui)
312 {
313         cui->lang_screen = lang_screen_init(cui, cui->config, cui_lang_exit);
314         cui_set_current(cui, lang_screen_scr(cui->lang_screen));
315 }
316
317 static void cui_add_url_exit(struct cui *cui)
318 {
319         cui_set_current(cui, &cui->main->scr);
320         talloc_free(cui->add_url_screen);
321         cui->add_url_screen = NULL;
322 }
323
324 void cui_show_add_url(struct cui *cui)
325 {
326         cui->add_url_screen = add_url_screen_init(cui, cui_add_url_exit);
327         cui_set_current(cui, add_url_screen_scr(cui->add_url_screen));
328 }
329
330 static void cui_help_exit(struct cui *cui)
331 {
332         cui_set_current(cui, help_screen_return_scr(cui->help_screen));
333         talloc_free(cui->help_screen);
334         cui->help_screen = NULL;
335 }
336
337 void cui_show_help(struct cui *cui, const char *title,
338                 const struct help_text *text)
339 {
340         if (!cui->current)
341                 return;
342
343         if (cui->help_screen)
344                 return;
345
346         cui->help_screen = help_screen_init(cui, cui->current,
347                         title, text, cui_help_exit);
348
349         if (cui->help_screen)
350                 cui_set_current(cui, help_screen_scr(cui->help_screen));
351 }
352
353 static void cui_subset_exit(struct cui *cui)
354 {
355         cui_set_current(cui, subset_screen_return_scr(cui->subset_screen));
356         talloc_free(cui->subset_screen);
357         cui->subset_screen = NULL;
358 }
359
360 void cui_show_subset(struct cui *cui, const char *title,
361                      void *arg)
362 {
363         if (!cui->current)
364                 return;
365
366         if (cui->subset_screen)
367                 return;
368
369         cui->subset_screen = subset_screen_init(cui, cui->current,
370                         title, arg, cui_subset_exit);
371
372         if (cui->subset_screen)
373                 cui_set_current(cui, subset_screen_scr(cui->subset_screen));
374 }
375
376 /**
377  * cui_set_current - Set the currently active screen and redraw it.
378  */
379
380 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
381 {
382         struct nc_scr *old;
383
384         DBGS("%p -> %p\n", cui->current, scr);
385
386         assert(cui->current != scr);
387
388         old = cui->current;
389         nc_scr_unpost(old);
390
391         cui->current = scr;
392
393         nc_scr_post(cui->current);
394
395         return old;
396 }
397
398 static bool process_global_keys(struct cui *cui, int key)
399 {
400         switch (key) {
401         case 0xc:
402                 if (cui->current && cui->current->main_ncw)
403                         wrefresh(curscr);
404                 return true;
405         }
406         return false;
407 }
408
409 /**
410  * cui_process_key - Process input on stdin.
411  */
412
413 static int cui_process_key(void *arg)
414 {
415         struct cui *cui = cui_from_arg(arg);
416
417         assert(cui->current);
418
419         for (;;) {
420                 int c = getch();
421
422                 pb_debug("%s: got key %d\n", __func__, c);
423
424                 if (c == ERR)
425                         break;
426
427                 if (!cui->has_input) {
428                         cui->has_input = true;
429                         if (cui->client) {
430                                 pb_log("UI input received (key = %d), aborting "
431                                         "default boot\n", c);
432                                 discover_client_cancel_default(cui->client);
433                         } else {
434                                 pb_log("UI input received (key = %d), aborting "
435                                         "once server connects\n", c);
436                         }
437                 }
438
439                 if (process_global_keys(cui, c))
440                         continue;
441
442                 cui->current->process_key(cui->current, c);
443         }
444
445         return 0;
446 }
447
448 /**
449  * cui_process_js - Process joystick events.
450  */
451
452 static int cui_process_js(void *arg)
453 {
454         struct cui *cui = cui_from_arg(arg);
455         int c;
456
457         c = pjs_process_event(cui->pjs);
458
459         if (c) {
460                 ungetch(c);
461                 cui_process_key(arg);
462         }
463
464         return 0;
465 }
466
467 /**
468  * cui_handle_resize - Handle the term resize.
469  */
470
471 static void cui_handle_resize(struct cui *cui)
472 {
473         struct winsize ws;
474
475         if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
476                 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
477                 return;
478         }
479
480         pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
481
482         wclear(cui->current->main_ncw);
483         resize_term(ws.ws_row, ws.ws_col);
484         cui->current->resize(cui->current);
485
486         /* For some reason this makes ncurses redraw the screen */
487         getch();
488         redrawwin(cui->current->main_ncw);
489         wrefresh(cui->current->main_ncw);
490 }
491
492 /**
493  * cui_device_add - Client device_add callback.
494  *
495  * Creates menu_items for all the device boot_options and inserts those
496  * menu_items into the main menu.  Redraws the main menu if it is active.
497  */
498
499 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
500                 void *arg)
501 {
502         struct pmenu_item *i, *dev_hdr = NULL;
503         struct cui *cui = cui_from_arg(arg);
504         struct cui_opt_data *cod;
505         const char *tab = "  ";
506         unsigned int insert_pt;
507         int result, rows, cols;
508         ITEM *selected;
509         char *name;
510
511         pb_debug("%s: %p %s\n", __func__, opt, opt->id);
512
513         selected = current_item(cui->main->ncm);
514         menu_format(cui->main->ncm, &rows, &cols);
515
516         if (cui->current == &cui->main->scr)
517                 nc_scr_unpost(cui->current);
518
519         /* Check if the boot device is new */
520         dev_hdr = pmenu_find_device(cui->main, dev, opt);
521
522         /* All actual boot entries are 'tabbed' across */
523         name = talloc_asprintf(cui->main, "%s%s",
524                         tab, opt->name ? : "Unknown Name");
525
526         /* Save the item in opt->ui_info for cui_device_remove() */
527         opt->ui_info = i = pmenu_item_create(cui->main, name);
528         talloc_free(name);
529         if (!i)
530                 return -1;
531
532         i->on_edit = cui_item_edit;
533         i->on_execute = cui_boot;
534         i->data = cod = talloc(i, struct cui_opt_data);
535
536         cod->opt = opt;
537         cod->dev = dev;
538         cod->opt_hash = pb_opt_hash(dev, opt);
539         cod->name = opt->name;
540         cod->bd = talloc(i, struct pb_boot_data);
541
542         cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
543         cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
544         cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
545         cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
546         cod->bd->args_sig_file = talloc_strdup(cod->bd, opt->args_sig_file);
547
548         /* This disconnects items array from menu. */
549         result = set_menu_items(cui->main->ncm, NULL);
550
551         if (result)
552                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
553
554         /* Insert new items at insert_pt. */
555         if (dev_hdr) {
556                 insert_pt = pmenu_grow(cui->main, 2);
557                 pmenu_item_insert(cui->main, dev_hdr, insert_pt);
558                 pb_log("%s: adding new device hierarchy %s\n",
559                         __func__,opt->device_id);
560                 pmenu_item_insert(cui->main, i, insert_pt+1);
561         } else {
562                 insert_pt = pmenu_grow(cui->main, 1);
563                 pmenu_item_add(cui->main, i, insert_pt);
564         }
565
566         pb_log("%s: adding opt '%s'\n", __func__, cod->name);
567         pb_log("   image  '%s'\n", cod->bd->image);
568         pb_log("   initrd '%s'\n", cod->bd->initrd);
569         pb_log("   args   '%s'\n", cod->bd->args);
570         pb_log("   argsig '%s'\n", cod->bd->args_sig_file);
571
572         /* Re-attach the items array. */
573         result = set_menu_items(cui->main->ncm, cui->main->items);
574
575         if (result)
576                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
577
578         if (0) {
579                 pb_log("%s\n", __func__);
580                 pmenu_dump_items(cui->main->items,
581                         item_count(cui->main->ncm) + 1);
582         }
583
584         if (!item_visible(selected)) {
585                 int idx, top;
586
587                 top = top_row(cui->main->ncm);
588                 idx = item_index(selected);
589
590                 /* If our index is above the current top row, align
591                  * us to the new top. Otherwise, align us to the new
592                  * bottom */
593                 top = top < idx ? idx - rows + 1 : idx;
594
595                 set_top_row(cui->main->ncm, top);
596                 set_current_item(cui->main->ncm, selected);
597         }
598
599         if (cui->current == &cui->main->scr)
600                 nc_scr_post(cui->current);
601
602         return 0;
603 }
604
605 /**
606  * cui_device_remove - Client device remove callback.
607  *
608  * Removes all the menu_items for the device from the main menu and redraws the
609  * main menu if it is active.
610  */
611
612 static void cui_device_remove(struct device *dev, void *arg)
613 {
614         struct cui *cui = cui_from_arg(arg);
615         struct boot_option *opt;
616         unsigned int i;
617         int rows, cols, top, last;
618         int result;
619
620         pb_log("%s: %p %s\n", __func__, dev, dev->id);
621
622         if (cui->current == &cui->main->scr)
623                 nc_scr_unpost(cui->current);
624
625         /* This disconnects items array from menu. */
626
627         result = set_menu_items(cui->main->ncm, NULL);
628
629         if (result)
630                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
631
632         list_for_each_entry(&dev->boot_options, opt, list) {
633                 struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
634
635                 assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
636                 pmenu_remove(cui->main, item);
637         }
638
639         /* Manually remove remaining device hierarchy item */
640         for (i=0; i < cui->main->item_count; i++) {
641                 struct pmenu_item *item = item_userptr(cui->main->items[i]);
642                 if (!item || !item->data )
643                         continue;
644
645                 struct cui_opt_data *data = item->data;
646                 if (data && data->dev && data->dev == dev)
647                         pmenu_remove(cui->main,item);
648         }
649
650         /* Re-attach the items array. */
651
652         result = set_menu_items(cui->main->ncm, cui->main->items);
653
654         /* Move cursor to 'Exit' menu entry */
655         menu_format(cui->main->ncm, &rows, &cols);
656         last = cui->main->item_count - 1;
657         set_current_item(cui->main->ncm, cui->main->items[last]);
658         if (!item_visible(cui->main->items[last])) {
659                 top = last < rows ? 0 : last - rows + 1;
660                 set_top_row(cui->main->ncm, top);
661         }
662
663         if (result)
664                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
665
666         if (0) {
667                 pb_log("%s\n", __func__);
668                 pmenu_dump_items(cui->main->items,
669                         item_count(cui->main->ncm) + 1);
670         }
671
672         if (cui->current == &cui->main->scr)
673                 nc_scr_post(cui->current);
674 }
675
676 static void cui_update_status(struct boot_status *status, void *arg)
677 {
678         struct cui *cui = cui_from_arg(arg);
679
680         nc_scr_status_printf(cui->current,
681                         "%s: %s",
682                         status->type == BOOT_STATUS_ERROR ?
683                                 _("Error") : _("Info"),
684                         status->message);
685
686 }
687
688 static void cui_update_mm_title(struct cui *cui)
689 {
690         struct nc_frame *frame = &cui->main->scr.frame;
691
692         talloc_free(frame->rtitle);
693
694         frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
695         if (cui->sysinfo->identifier)
696                 frame->rtitle = talloc_asprintf_append(frame->rtitle,
697                                 " %s", cui->sysinfo->identifier);
698
699         if (cui->current == &cui->main->scr)
700                 nc_scr_post(cui->current);
701 }
702
703 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
704 {
705         struct cui *cui = cui_from_arg(arg);
706         cui->sysinfo = talloc_steal(cui, sysinfo);
707
708         /* if we're currently displaying the system info screen, inform it
709          * of the updated information. */
710         if (cui->sysinfo_screen)
711                 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
712
713         if (cui->subset_screen)
714                 subset_screen_update(cui->subset_screen);
715
716         /* ... and do the same with the config screen... */
717         if (cui->config_screen)
718                 config_screen_update(cui->config_screen, cui->config, sysinfo);
719
720         /* ... and the boot editor. */
721         if (cui->boot_editor)
722                 boot_editor_update(cui->boot_editor, sysinfo);
723
724         cui_update_mm_title(cui);
725 }
726
727 static void cui_update_language(struct cui *cui, char *lang)
728 {
729         bool repost_menu;
730         char *cur_lang;
731
732         cur_lang = setlocale(LC_ALL, NULL);
733         if (cur_lang && !strcmp(cur_lang, lang))
734                 return;
735
736         setlocale(LC_ALL, lang);
737
738         /* we'll need to update the menu: drop all items and repopulate */
739         repost_menu = cui->current == &cui->main->scr;
740         if (repost_menu)
741                 nc_scr_unpost(cui->current);
742
743         talloc_free(cui->main);
744         cui->main = main_menu_init(cui);
745
746         if (repost_menu) {
747                 cui->current = &cui->main->scr;
748                 nc_scr_post(cui->current);
749         }
750
751         discover_client_enumerate(cui->client);
752 }
753
754 static void cui_update_config(struct config *config, void *arg)
755 {
756         struct cui *cui = cui_from_arg(arg);
757         cui->config = talloc_steal(cui, config);
758
759         if (config->lang)
760                 cui_update_language(cui, config->lang);
761
762         if (cui->subset_screen)
763                 subset_screen_update(cui->subset_screen);
764
765         if (cui->config_screen)
766                 config_screen_update(cui->config_screen, config, cui->sysinfo);
767
768         if (config->safe_mode)
769                 nc_scr_status_printf(cui->current,
770                                 _("SAFE MODE: select '%s' to continue"),
771                                 _("Rescan devices"));
772 }
773
774 int cui_send_config(struct cui *cui, struct config *config)
775 {
776         return discover_client_send_config(cui->client, config);
777 }
778
779 int cui_send_url(struct cui *cui, char * url)
780 {
781         return discover_client_send_url(cui->client, url);
782 }
783
784 void cui_send_reinit(struct cui *cui)
785 {
786         discover_client_send_reinit(cui->client);
787 }
788
789 static int menu_sysinfo_execute(struct pmenu_item *item)
790 {
791         cui_show_sysinfo(cui_from_item(item));
792         return 0;
793 }
794
795 static int menu_config_execute(struct pmenu_item *item)
796 {
797         cui_show_config(cui_from_item(item));
798         return 0;
799 }
800
801 static int menu_lang_execute(struct pmenu_item *item)
802 {
803         cui_show_lang(cui_from_item(item));
804         return 0;
805 }
806
807 static int menu_reinit_execute(struct pmenu_item *item)
808 {
809         if (cui_from_item(item)->client)
810                 cui_send_reinit(cui_from_item(item));
811         return 0;
812 }
813
814 static int menu_add_url_execute(struct pmenu_item *item)
815 {
816         if (cui_from_item(item)->client)
817                 cui_show_add_url(cui_from_item(item));
818         return 0;
819 }
820
821 /**
822  * pb_mm_init - Setup the main menu instance.
823  */
824 static struct pmenu *main_menu_init(struct cui *cui)
825 {
826         struct pmenu_item *i;
827         struct pmenu *m;
828         int result;
829
830         m = pmenu_init(cui, 7, cui_on_exit);
831         if (!m) {
832                 pb_log("%s: failed\n", __func__);
833                 return NULL;
834         }
835
836         m->on_new = cui_item_new;
837
838         m->scr.frame.ltitle = talloc_asprintf(m,
839                 "Petitboot (" PACKAGE_VERSION ")");
840         m->scr.frame.rtitle = NULL;
841         m->scr.frame.help = talloc_strdup(m,
842                 _("Enter=accept, e=edit, n=new, x=exit, l=language, h=help"));
843         m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
844
845         /* add a separator */
846         i = pmenu_item_create(m, " ");
847         item_opts_off(i->nci, O_SELECTABLE);
848         pmenu_item_insert(m, i, 0);
849
850         /* add system items */
851         i = pmenu_item_create(m, _("System information"));
852         i->on_execute = menu_sysinfo_execute;
853         pmenu_item_insert(m, i, 1);
854
855         i = pmenu_item_create(m, _("System configuration"));
856         i->on_execute = menu_config_execute;
857         pmenu_item_insert(m, i, 2);
858
859         /* this label isn't translated, so we don't want a gettext() here */
860         i = pmenu_item_create(m, "Language");
861         i->on_execute = menu_lang_execute;
862         pmenu_item_insert(m, i, 3);
863
864         i = pmenu_item_create(m, _("Rescan devices"));
865         i->on_execute = menu_reinit_execute;
866         pmenu_item_insert(m, i, 4);
867
868         i = pmenu_item_create(m, _("Retrieve config from URL"));
869         i->on_execute = menu_add_url_execute;
870         pmenu_item_insert(m, i, 5);
871
872         i = pmenu_item_create(m, _("Exit to shell"));
873         i->on_execute = pmenu_exit_cb;
874         pmenu_item_insert(m, i, 6);
875
876         result = pmenu_setup(m);
877
878         if (result) {
879                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
880                         strerror(errno));
881                 goto fail_setup;
882         }
883
884         m->help_title = _("main menu");
885         m->help_text = &main_menu_help_text;
886
887         menu_opts_off(m->ncm, O_SHOWDESC);
888         set_menu_mark(m->ncm, " *");
889         set_current_item(m->ncm, i->nci);
890
891         return m;
892
893 fail_setup:
894         talloc_free(m);
895         return NULL;
896 }
897
898 static struct discover_client_ops cui_client_ops = {
899         .device_add = NULL,
900         .boot_option_add = cui_boot_option_add,
901         .device_remove = cui_device_remove,
902         .update_status = cui_update_status,
903         .update_sysinfo = cui_update_sysinfo,
904         .update_config = cui_update_config,
905 };
906
907 /* cui_server_wait - Connect to the discover server.
908  * @arg: Pointer to the cui instance.
909  *
910  * A timeout callback that attempts to connect to the discover server; on
911  * failure it registers itself with a one second timeout to try again.
912  * On success the cui->client struct will be set.
913  *
914  * Since this updates the status line when called it must not be called
915  * before the UI is ready.
916  */
917 static int cui_server_wait(void *arg)
918 {
919         struct cui *cui = cui_from_arg(arg);
920
921         if (cui->client) {
922                 pb_debug("We already have a server!\n");
923                 return 0;
924         }
925
926         /* We haven't yet connected to the server */
927         pb_log("Trying to connect...\n");
928         cui->client = discover_client_init(cui->waitset,
929                         &cui_client_ops, cui);
930
931         if (!cui->client) {
932                 waiter_register_timeout(cui->waitset, 1000, cui_server_wait,
933                                         cui);
934                 nc_scr_status_printf(cui->current, "Info: Waiting for server");
935         } else {
936                 nc_scr_status_printf(cui->current, "Info: Connected to server!");
937                 talloc_steal(cui, cui->client);
938
939                 if (cui->has_input) {
940                         pb_log("Aborting default boot on server connect\n");
941                         discover_client_cancel_default(cui->client);
942                 }
943         }
944
945         return 0;
946 }
947
948 /**
949  * cui_init - Setup the cui instance.
950  * @platform_info: A value for the struct cui platform_info member.
951  *
952  * Returns a pointer to a struct cui on success, or NULL on error.
953  *
954  * Allocates the cui instance, sets up the client and stdin waiters, and
955  * sets up the ncurses menu screen.
956  */
957
958 struct cui *cui_init(void* platform_info,
959         int (*js_map)(const struct js_event *e), int start_daemon, int timeout)
960 {
961         struct cui *cui;
962         unsigned int i;
963
964         cui = talloc_zero(NULL, struct cui);
965         if (!cui) {
966                 pb_log("%s: alloc cui failed.\n", __func__);
967                 fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
968                 goto fail_alloc;
969         }
970
971         cui->c_sig = pb_cui_sig;
972         cui->platform_info = platform_info;
973         cui->waitset = waitset_create(cui);
974
975         process_init(cui, cui->waitset, false);
976
977         /* Loop here for scripts that just started the server. */
978
979 retry_start:
980         for (i = start_daemon ? 2 : 15; i && timeout; i--) {
981                 cui->client = discover_client_init(cui->waitset,
982                                 &cui_client_ops, cui);
983                 if (cui->client || !i)
984                         break;
985                 pb_log("%s: waiting for server %d\n", __func__, i);
986                 sleep(1);
987         }
988
989         if (!cui->client && start_daemon) {
990                 int result;
991
992                 start_daemon = 0;
993
994                 result = pb_start_daemon(cui);
995
996                 if (!result)
997                         goto retry_start;
998
999                 pb_log("%s: discover_client_init failed.\n", __func__);
1000                 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
1001                         __func__);
1002                 fprintf(stderr, _("could not start pb-discover, the petitboot "
1003                         "daemon.\n"));
1004                 goto fail_client_init;
1005         }
1006
1007         if (!cui->client && !timeout) {
1008                 /* Have the first timeout fire immediately so we can check
1009                  * for the server as soon as the UI is ready */
1010                 waiter_register_timeout(cui->waitset, 0,
1011                                         cui_server_wait, cui);
1012         } else if (!cui->client) {
1013                 pb_log("%s: discover_client_init failed.\n", __func__);
1014                 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
1015                         __func__);
1016                 fprintf(stderr, _("check that pb-discover, "
1017                         "the petitboot daemon is running.\n"));
1018                 goto fail_client_init;
1019         }
1020
1021         atexit(cui_atexit);
1022         talloc_steal(cui, cui->client);
1023         cui_start();
1024
1025         cui->main = main_menu_init(cui);
1026         if (!cui->main)
1027                 goto fail_client_init;
1028
1029         waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
1030                         cui_process_key, cui);
1031
1032         if (js_map) {
1033
1034                 cui->pjs = pjs_init(cui, js_map);
1035
1036                 if (cui->pjs)
1037                         waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
1038                                         WAIT_IN, cui_process_js, cui);
1039         }
1040
1041         return cui;
1042
1043 fail_client_init:
1044         talloc_free(cui);
1045 fail_alloc:
1046         return NULL;
1047 }
1048
1049 /**
1050  * cui_run - The main cui program loop.
1051  * @cui: The cui instance.
1052  * @main: The menu to use as the main menu.
1053  *
1054  * Runs the cui engine.  Does not return until indicated to do so by some
1055  * user action, or an error occurs.  Frees the cui object on return.
1056  * Returns 0 on success (return to shell), -1 on error (should restart).
1057  */
1058
1059 int cui_run(struct cui *cui)
1060 {
1061         assert(main);
1062
1063         cui->current = &cui->main->scr;
1064         cui->default_item = 0;
1065
1066         nc_scr_post(cui->current);
1067
1068         while (1) {
1069                 int result = waiter_poll(cui->waitset);
1070
1071                 if (result < 0) {
1072                         pb_log("%s: poll: %s\n", __func__, strerror(errno));
1073                         break;
1074                 }
1075
1076                 if (cui->abort)
1077                         break;
1078
1079                 while (cui->resize) {
1080                         cui->resize = 0;
1081                         cui_handle_resize(cui);
1082                 }
1083         }
1084
1085         cui_atexit();
1086
1087         return cui->abort ? 0 : -1;
1088 }