ncurses: Fix bad strncmp
[petitboot] / ui / ncurses / nc-cui.c
index 35c9debfbe1113ff315eb0e9cc610b50641aea24..20a90483cd07accc318546d9fc26aab48c9f72dc 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#if defined(HAVE_CONFIG_H)
 #include "config.h"
-
-#define _GNU_SOURCE
+#endif
 
 #include <assert.h>
+#include <ctype.h>
 #include <errno.h>
 #include <stdlib.h>
-#include <string.h>
 #include <locale.h>
+#include <string.h>
 #include <sys/ioctl.h>
+#include <sys/reboot.h>
 
 #include "log/log.h"
 #include "pb-protocol/pb-protocol.h"
 #include "talloc/talloc.h"
 #include "waiter/waiter.h"
 #include "process/process.h"
+#include "i18n/i18n.h"
 #include "ui/common/discover-client.h"
+#include "ui/common/ui-system.h"
 #include "nc-cui.h"
+#include "nc-boot-editor.h"
+#include "nc-config.h"
+#include "nc-add-url.h"
+#include "nc-sysinfo.h"
+#include "nc-lang.h"
+#include "nc-helpscreen.h"
+#include "nc-statuslog.h"
+#include "nc-subset.h"
+#include "nc-plugin.h"
+#include "console-codes.h"
+
+extern const struct help_text main_menu_help_text;
+extern const struct help_text plugin_menu_help_text;
+
+static bool cui_detached = false;
+
+static struct pmenu *main_menu_init(struct cui *cui);
+static struct pmenu *plugin_menu_init(struct cui *cui);
+
+static void cui_cancel_autoboot_on_exit(struct cui *cui);
 
-static struct cui_opt_data *cod_from_item(struct pmenu_item *item)
+static bool lockdown_active(void)
 {
-       return item->data;
+       bool lockdown = false;
+       if (access(LOCKDOWN_FILE, F_OK) != -1)
+               lockdown = true;
+       return lockdown;
+}
+
+static void cui_start(void)
+{
+       initscr();                      /* Initialize ncurses. */
+       cbreak();                       /* Disable line buffering. */
+       noecho();                       /* Disable getch() echo. */
+       keypad(stdscr, TRUE);           /* Enable num keypad keys. */
+       nonl();                         /* Disable new-line translation. */
+       intrflush(stdscr, FALSE);       /* Disable interrupt flush. */
+       curs_set(0);                    /* Make cursor invisible */
+       nodelay(stdscr, TRUE);          /* Enable non-blocking getch() */
+
+       /* We may be operating with an incorrect $TERM type; in this case
+        * the keymappings will be slightly broken. We want at least
+        * backspace to work though, so we'll define both DEL and ^H to
+        * map to backspace */
+       define_key("\x7f", KEY_BACKSPACE);
+       define_key("\x08", KEY_BACKSPACE);
+
+       /* we need backtab too, for form navigation. vt220 doesn't include
+        * this (kcbt), but we don't want to require a full linux/xterm termcap
+        */
+       define_key("\x1b[Z", KEY_BTAB);
+
+       /* We'll define a few other keys too since they're commonly
+        * used for navigation but the escape character will cause
+        * Petitboot to exit if they're left undefined */
+       define_key("\x1b\x5b\x35\x7e", KEY_PPAGE);
+       define_key("\x1b\x5b\x36\x7e", KEY_NPAGE);
+       define_key("\x1b\x5b\x31\x7e", KEY_HOME);
+       define_key("\x1b\x5b\x34\x7e", KEY_END);
+       define_key("\x1b\x4f\x48", KEY_HOME);
+       define_key("\x1b\x4f\x46", KEY_END);
+       define_key("OH", KEY_HOME);
+       define_key("OF", KEY_END);
+
+       /* Arrow keys in normal cursor mode */
+       define_key("\x1b\x5b\x41", KEY_UP);
+       define_key("\x1b\x5b\x42", KEY_DOWN);
+       define_key("\x1b\x5b\x43", KEY_RIGHT);
+       define_key("\x1b\x5b\x44", KEY_LEFT);
+       /* Arrow keys in "application" cursor mode */
+       define_key("\x1b\x4f\x41", KEY_UP);
+       define_key("\x1b\x4f\x42", KEY_DOWN);
+       define_key("\x1b\x4f\x43", KEY_RIGHT);
+       define_key("\x1b\x4f\x44", KEY_LEFT);
+
+       define_key("\x1b\x5b\x33\x7e", KEY_DC);
+
+       while (getch() != ERR)          /* flush stdin */
+               (void)0;
+}
+
+static void cui_atexit(void)
+{
+       if (cui_detached)
+               return;
+
+       clear();
+       refresh();
+       endwin();
+
+       bool lockdown = lockdown_active();
+
+       while (lockdown) {
+               sync();
+               reboot(RB_AUTOBOOT);
+       }
 }
 
 /**
@@ -60,46 +156,97 @@ void cui_abort(struct cui *cui)
 
 void cui_resize(struct cui *cui)
 {
-       pb_log("%s: resizing\n", __func__);
+       pb_debug("%s: resizing\n", __func__);
        cui->resize = 1;
 }
 
 /**
- * cui_on_exit - A generic main menu ESC callback.
+ * cui_on_exit - A generic main menu exit callback.
  */
 
 void cui_on_exit(struct pmenu *menu)
 {
-       cui_abort(cui_from_pmenu(menu));
+       struct cui *cui = cui_from_pmenu(menu);
+       char *sh_cmd;
+
+       cui_cancel_autoboot_on_exit(cui);
+
+       sh_cmd = talloc_asprintf(cui,
+               "echo \"Exiting petitboot. Type 'exit' to return.\";\
+                echo \"You may run 'pb-sos' to gather diagnostic data\";\
+                %s", pb_system_apps.sh);
+
+       if (!sh_cmd) {
+               pb_log("Failed to allocate shell arguments\n");
+               return;
+       }
+
+       const char *argv[] = {
+               pb_system_apps.sh,
+               "-c",
+               sh_cmd,
+               NULL
+       };
+
+       cui_run_cmd(cui, argv);
+
+       nc_scr_status_printf(cui->current, _("Returned from shell"));
+       talloc_free(sh_cmd);
 }
 
 /**
  * cui_run_cmd - A generic cb to run the supplied command.
  */
 
-int cui_run_cmd(struct pmenu_item *item)
+int cui_run_cmd(struct cui *cui, const char **cmd_argv)
 {
+       struct process *process;
        int result;
-       struct cui *cui = cui_from_item(item);
-       const char **cmd_argv = item->data;
 
-       nc_scr_status_printf(cui->current, "Running %s...", cmd_argv[0]);
+       process = process_create(cui);
+       if (!process)
+               return -1;
+
+       process->path = cmd_argv[0];
+       process->argv = cmd_argv;
+       process->raw_stdout = true;
+
+       nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
+
+       nc_scr_unpost(cui->current);
+       clear();
+       refresh();
 
        def_prog_mode();
+       endwin();
 
-       result = process_run_simple_argv(item, cmd_argv);
+       result = process_run_sync(process);
 
        reset_prog_mode();
+       refresh();
+
        redrawwin(cui->current->main_ncw);
+       nc_scr_post(cui->current);
 
        if (result) {
                pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
-               nc_scr_status_printf(cui->current, "Failed: %s", cmd_argv[0]);
+               nc_scr_status_printf(cui->current, _("Failed: %s"),
+                               cmd_argv[0]);
        }
 
+       process_release(process);
+
        return result;
 }
 
+int cui_run_cmd_from_item(struct pmenu_item *item)
+{
+       struct cui *cui = cui_from_item(item);
+       const char **cmd_argv = item->data;
+
+       return cui_run_cmd(cui, cmd_argv);
+}
+
 /**
  * cui_boot - A generic cb to run kexec.
  */
@@ -112,75 +259,246 @@ static int cui_boot(struct pmenu_item *item)
 
        assert(cui->current == &cui->main->scr);
 
-       pb_log("%s: %s\n", __func__, cod->name);
-       if (!cod->opt) {
-               pb_log("%s: missing opt?\n", __func__);
-               return -1;
-       }
+       pb_debug("%s: %s\n", __func__, cod->name);
 
-       nc_scr_status_printf(cui->current, "Booting %s...", cod->name);
-
-       def_prog_mode();
+       nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
 
        result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
 
-       reset_prog_mode();
-       redrawwin(cui->current->main_ncw);
-
        if (result) {
                nc_scr_status_printf(cui->current,
-                               "Failed: boot %s", cod->bd->image);
+                               _("Failed: boot %s"), cod->bd->image);
        }
 
        return 0;
 }
 
-/**
- * cui_boot_editor_on_exit - The boot_editor on_exit callback.
- */
-
-static void cui_boot_editor_on_exit(struct boot_editor *boot_editor, enum boot_editor_result boot_editor_result,
-       struct pb_boot_data *bd)
+static void cui_boot_editor_on_exit(struct cui *cui,
+               struct pmenu_item *item,
+               struct pb_boot_data *bd)
 {
-       struct cui *cui = cui_from_arg(boot_editor->scr.ui_ctx);
+       struct pmenu *menu = cui->main;
+       struct cui_opt_data *cod;
+       int idx, top, rows, cols;
+       static int user_idx = 0;
+
+       /* Was the edit cancelled? */
+       if (!bd) {
+               cui_set_current(cui, &cui->main->scr);
+               talloc_free(cui->boot_editor);
+               cui->boot_editor = NULL;
+               return;
+       }
 
-       if (boot_editor_result == boot_editor_update) {
-               struct pmenu_item *i = pmenu_find_selected(cui->main);
-               struct cui_opt_data *cod = cod_from_item(i);
+       /* Is this was a new item, we'll need to update the menu */
+       if (!item) {
+               int insert_pt;
 
-               assert(bd);
+               cod = talloc_zero(NULL, struct cui_opt_data);
+               cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
 
-               talloc_steal(i, bd);
-               talloc_free(cod->bd);
-               cod->bd = bd;
+               item = pmenu_item_create(menu, cod->name);
+               if (!item) {
+                       talloc_free(cod);
+                       goto out;
+               }
+
+               item->on_edit = cui_item_edit;
+               item->on_execute = cui_boot;
+               item->data = cod;
+
+               talloc_steal(item, cod);
+
+               /* Detach the items array. */
+               set_menu_items(menu->ncm, NULL);
+
+               /* Insert new item at insert_pt. */
+               insert_pt = pmenu_grow(menu, 1);
+               pmenu_item_insert(menu, item, insert_pt);
+
+               /* Re-attach the items array. */
+               set_menu_items(menu->ncm, menu->items);
+
+               /* If our index is above the current top row, align
+                * us to the new top. Otherwise, align us to the new
+                * bottom */
+               menu_format(cui->main->ncm, &rows, &cols);
+               top = top_row(cui->main->ncm);
+               idx = item_index(item->nci);
 
-               pmenu_item_replace(i, cod->name);
+               if (top >= idx)
+                       top = idx;
+               else
+                       top = idx < rows ? 0 : idx - rows + 1;
 
-               /* FIXME: need to make item visible somehow */
-               set_current_item(cui->main->ncm, i->nci);
+               set_top_row(cui->main->ncm, top);
+               set_current_item(item->pmenu->ncm, item->nci);
 
-               pb_log("%s: updating opt '%s'\n", __func__, cod->name);
-               pb_log(" image  '%s'\n", cod->bd->image);
-               pb_log(" initrd '%s'\n", cod->bd->initrd);
-               pb_log(" dtb    '%s'\n", cod->bd->dtb);
-               pb_log(" args   '%s'\n", cod->bd->args);
+               nc_scr_post(&menu->scr);
+       } else {
+               cod = item->data;
        }
 
+       cod->bd = talloc_steal(cod, bd);
+
+out:
        cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->boot_editor);
+       cui->boot_editor = NULL;
+}
+
+void cui_item_edit(struct pmenu_item *item)
+{
+       struct cui *cui = cui_from_item(item);
+       cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
+                                       cui_boot_editor_on_exit);
+       cui_set_current(cui, boot_editor_scr(cui->boot_editor));
+}
+
+void cui_item_new(struct pmenu *menu)
+{
+       struct cui *cui = cui_from_pmenu(menu);
+       cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
+                                       cui_boot_editor_on_exit);
+       cui_set_current(cui, boot_editor_scr(cui->boot_editor));
+}
+
+static void cui_sysinfo_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->sysinfo_screen);
+       cui->sysinfo_screen = NULL;
+}
+
+void cui_show_sysinfo(struct cui *cui)
+{
+       cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
+                       cui_sysinfo_exit);
+       cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
+}
+
+static void cui_config_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->config_screen);
+       cui->config_screen = NULL;
+}
 
-       talloc_free(boot_editor);
+void cui_show_config(struct cui *cui)
+{
+       cui->config_screen = config_screen_init(cui, cui->config,
+                       cui->sysinfo, cui_config_exit);
+       cui_set_current(cui, config_screen_scr(cui->config_screen));
+}
+
+static void cui_lang_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->lang_screen);
+       cui->lang_screen = NULL;
+}
+
+void cui_show_lang(struct cui *cui)
+{
+       cui->lang_screen = lang_screen_init(cui, cui->config, cui_lang_exit);
+       cui_set_current(cui, lang_screen_scr(cui->lang_screen));
+}
+
+static void cui_statuslog_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->statuslog_screen);
+       cui->statuslog_screen = NULL;
+}
+
+void cui_show_statuslog(struct cui *cui)
+{
+       cui->statuslog_screen = statuslog_screen_init(cui, cui_statuslog_exit);
+       cui_set_current(cui, statuslog_screen_scr(cui->statuslog_screen));
+}
+
+static void cui_add_url_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->main->scr);
+       talloc_free(cui->add_url_screen);
+       cui->add_url_screen = NULL;
+}
+
+static void cui_plugin_exit(struct cui *cui)
+{
+       cui_set_current(cui, &cui->plugin_menu->scr);
+       talloc_free(cui->plugin_screen);
+       cui->plugin_screen = NULL;
+}
+
+static void cui_plugin_menu_exit(struct pmenu *menu)
+{
+       struct cui *cui = cui_from_pmenu(menu);
+       cui_set_current(cui, &cui->main->scr);
 }
 
-int cui_boot_editor_run(struct pmenu_item *item)
+void cui_show_add_url(struct cui *cui)
+{
+       cui->add_url_screen = add_url_screen_init(cui, cui_add_url_exit);
+       cui_set_current(cui, add_url_screen_scr(cui->add_url_screen));
+}
+
+void cui_show_plugin_menu(struct cui *cui)
+{
+       cui_set_current(cui, &cui->plugin_menu->scr);
+}
+
+void cui_show_plugin(struct pmenu_item *item)
 {
        struct cui *cui = cui_from_item(item);
-       struct cui_opt_data *cod = cod_from_item(item);
-       struct boot_editor *boot_editor;
+       cui->plugin_screen = plugin_screen_init(cui, item, cui_plugin_exit);
+       cui_set_current(cui, plugin_screen_scr(cui->plugin_screen));
+}
 
-       boot_editor = boot_editor_init(cui, cod->bd, cui_boot_editor_on_exit);
-       cui_set_current(cui, &boot_editor->scr);
+static void cui_help_exit(struct cui *cui)
+{
+       cui_set_current(cui, help_screen_return_scr(cui->help_screen));
+       talloc_free(cui->help_screen);
+       cui->help_screen = NULL;
+}
 
-       return 0;
+void cui_show_help(struct cui *cui, const char *title,
+               const struct help_text *text)
+{
+       if (!cui->current)
+               return;
+
+       if (cui->help_screen)
+               return;
+
+       cui->help_screen = help_screen_init(cui, cui->current,
+                       title, text, cui_help_exit);
+
+       if (cui->help_screen)
+               cui_set_current(cui, help_screen_scr(cui->help_screen));
+}
+
+static void cui_subset_exit(struct cui *cui)
+{
+       cui_set_current(cui, subset_screen_return_scr(cui->subset_screen));
+       talloc_free(cui->subset_screen);
+       cui->subset_screen = NULL;
+}
+
+void cui_show_subset(struct cui *cui, const char *title,
+                    void *arg)
+{
+       if (!cui->current)
+               return;
+
+       if (cui->subset_screen)
+               return;
+
+       cui->subset_screen = subset_screen_init(cui, cui->current,
+                       title, arg, cui_subset_exit);
+
+       if (cui->subset_screen)
+               cui_set_current(cui, subset_screen_scr(cui->subset_screen));
 }
 
 /**
@@ -196,10 +514,11 @@ struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
        assert(cui->current != scr);
 
        old = cui->current;
-       old->unpost(old);
+       nc_scr_unpost(old);
 
        cui->current = scr;
-       cui->current->post(cui->current);
+
+       nc_scr_post(cui->current);
 
        return old;
 }
@@ -208,10 +527,8 @@ static bool process_global_keys(struct cui *cui, int key)
 {
        switch (key) {
        case 0xc:
-               if (cui->current && cui->current->main_ncw) {
-                       redrawwin(cui->current->main_ncw);
-                       wrefresh(cui->current->main_ncw);
-               }
+               if (cui->current && cui->current->main_ncw)
+                       wrefresh(curscr);
                return true;
        }
        return false;
@@ -224,19 +541,55 @@ static bool process_global_keys(struct cui *cui, int key)
 static int cui_process_key(void *arg)
 {
        struct cui *cui = cui_from_arg(arg);
+       unsigned int i;
+       char *sequence;
+       int grab;
 
        assert(cui->current);
 
-       if (!cui->has_input)
-               discover_client_cancel_default(cui->client);
-       cui->has_input = true;
-
        for (;;) {
                int c = getch();
 
+               pb_debug("%s: got key %d\n", __func__, c);
+
                if (c == ERR)
                        break;
 
+               if (c == 27) {
+                       /*
+                        * If this is a console code sequence try to parse it
+                        * and don't treat this as a key press.
+                        */
+                       grab = getch();
+                       if (grab != ERR && grab != 27) {
+                               ungetch(grab);
+                               pb_debug("%s: Caught unhandled command sequence\n",
+                                               __func__);
+                               sequence = handle_control_sequence(cui, c);
+                               pb_debug("Caught sequence ");
+                               if (sequence) {
+                                       pb_debug("(%zu): ", strlen(sequence));
+                                       for (i = 0; i < strlen(sequence); i++)
+                                               pb_debug("0%o ", sequence[i]);
+                                       pb_debug("\n");
+                               } else
+                                       pb_debug("(0): (none)\n");
+                               continue;
+                       }
+               }
+
+               if (!cui->has_input) {
+                       cui->has_input = true;
+                       if (cui->client) {
+                               pb_log("UI input received (key = %d), aborting "
+                                       "default boot\n", c);
+                               discover_client_cancel_default(cui->client);
+                       } else {
+                               pb_log("UI input received (key = %d), aborting "
+                                       "once server connects\n", c);
+                       }
+               }
+
                if (process_global_keys(cui, c))
                        continue;
 
@@ -278,7 +631,7 @@ static void cui_handle_resize(struct cui *cui)
                return;
        }
 
-       pb_log("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
+       pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
 
        wclear(cui->current->main_ncw);
        resize_term(ws.ws_row, ws.ws_col);
@@ -290,125 +643,162 @@ static void cui_handle_resize(struct cui *cui)
        wrefresh(cui->current->main_ncw);
 }
 
-/**
- * cui_on_open - Open new item callback.
- */
-
-void cui_on_open(struct pmenu *menu)
-{
-       unsigned int insert_pt;
-       struct pmenu_item *i;
-       struct cui_opt_data *cod;
-
-       menu->scr.unpost(&menu->scr);
-
-       /* This disconnects items array from menu. */
-
-       set_menu_items(menu->ncm, NULL);
-
-       /* Insert new items at insert_pt. */
-
-       insert_pt = pmenu_grow(menu, 1);
-       i = pmenu_item_alloc(menu);
-
-       i->on_edit = cui_boot_editor_run;
-       i->on_execute = cui_boot;
-       i->data = cod = talloc_zero(i, struct cui_opt_data);
-
-       cod->name = talloc_asprintf(i, "User item %u:", insert_pt);
-       cod->bd = talloc_zero(i, struct pb_boot_data);
-
-       pmenu_item_setup(menu, i, insert_pt, talloc_strdup(i, cod->name));
-
-       /* Re-attach the items array. */
-
-       set_menu_items(menu->ncm, menu->items);
-
-       menu->scr.post(&menu->scr);
-       set_current_item(menu->ncm, i->nci);
-
-       i->on_edit(i);
-}
-
 /**
  * cui_device_add - Client device_add callback.
  *
  * Creates menu_items for all the device boot_options and inserts those
  * menu_items into the main menu.  Redraws the main menu if it is active.
+ * If a 'plugin' type boot_option appears the plugin menu is updated instead.
  */
 
 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
                void *arg)
 {
+       struct pmenu_item *i, *dev_hdr = NULL;
        struct cui *cui = cui_from_arg(arg);
        struct cui_opt_data *cod;
+       const char *tab = "  ";
        unsigned int insert_pt;
-       struct pmenu_item *i;
+       int result, rows, cols;
+       struct pmenu *menu;
+       bool plugin_option;
        ITEM *selected;
-       int result;
-
-       pb_log("%s: %p %s\n", __func__, opt, opt->id);
+       char *name;
 
-       selected = current_item(cui->main->ncm);
+       plugin_option = opt->type == DISCOVER_PLUGIN_OPTION;
+       menu = plugin_option ? cui->plugin_menu : cui->main;
 
-       if (cui->current == &cui->main->scr)
-               cui->current->unpost(cui->current);
+       pb_debug("%s: %p %s\n", __func__, opt, opt->id);
 
-       /* This disconnects items array from menu. */
+       selected = current_item(menu->ncm);
+       menu_format(menu->ncm, &rows, &cols);
 
-       result = set_menu_items(cui->main->ncm, NULL);
+       if (cui->current == &cui->main->scr)
+               nc_scr_unpost(cui->current);
+       if (plugin_option && cui->current == &cui->plugin_menu->scr)
+               nc_scr_unpost(cui->current);
 
-       if (result)
-               pb_log("%s: set_menu_items failed: %d\n", __func__, result);
+       /* Check if the boot device is new */
+       dev_hdr = pmenu_find_device(menu, dev, opt);
 
-       /* Insert new items at insert_pt. */
-       insert_pt = pmenu_grow(cui->main, 1);
+       /* All actual boot entries are 'tabbed' across */
+       name = talloc_asprintf(menu, "%s%s",
+                       tab, opt->name ? : "Unknown Name");
 
        /* Save the item in opt->ui_info for cui_device_remove() */
+       opt->ui_info = i = pmenu_item_create(menu, name);
+       talloc_free(name);
+       if (!i)
+               return -1;
 
-       opt->ui_info = i = pmenu_item_alloc(cui->main);
+       if (plugin_option) {
+               i->on_edit = NULL;
+               i->on_execute = plugin_install_plugin;
+       } else {
+               i->on_edit = cui_item_edit;
+               i->on_execute = cui_boot;
+       }
 
-       i->on_edit = cui_boot_editor_run;
-       i->on_execute = cui_boot;
        i->data = cod = talloc(i, struct cui_opt_data);
-
        cod->opt = opt;
        cod->dev = dev;
        cod->opt_hash = pb_opt_hash(dev, opt);
        cod->name = opt->name;
-       cod->bd = talloc(i, struct pb_boot_data);
 
-       cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
-       cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
-       cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
-       cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
+       if (plugin_option) {
+               cod->pd = talloc(i, struct pb_plugin_data);
+               cod->pd->plugin_file = talloc_strdup(cod,
+                               opt->boot_image_file);
+       } else {
+               cod->bd = talloc(i, struct pb_boot_data);
+               cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
+               cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
+               cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
+               cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
+               cod->bd->args_sig_file = talloc_strdup(cod->bd, opt->args_sig_file);
+       }
+
+       /* This disconnects items array from menu. */
+       result = set_menu_items(menu->ncm, NULL);
+
+       if (result)
+               pb_log("%s: set_menu_items failed: %d\n", __func__, result);
 
-       pmenu_item_setup(cui->main, i, insert_pt, cod->name);
+       /* Insert new items at insert_pt. */
+       if (dev_hdr) {
+               insert_pt = pmenu_grow(menu, 2);
+               pmenu_item_insert(menu, dev_hdr, insert_pt);
+               pb_log("%s: adding new device hierarchy %s\n",
+                       __func__, opt->device_id);
+               pmenu_item_insert(menu, i, insert_pt+1);
+       } else {
+               insert_pt = pmenu_grow(menu, 1);
+               pmenu_item_add(menu, i, insert_pt);
+       }
 
-       pb_log("%s: adding opt '%s'\n", __func__, cod->name);
-       pb_log("   image  '%s'\n", cod->bd->image);
-       pb_log("   initrd '%s'\n", cod->bd->initrd);
-       pb_log("   args   '%s'\n", cod->bd->args);
+       if (plugin_option) {
+               pb_log("%s: adding plugin '%s'\n", __func__, cod->name);
+               pb_log("   file  '%s'\n", cod->pd->plugin_file);
+       } else {
+               pb_log("%s: adding opt '%s'\n", __func__, cod->name);
+               pb_log("   image  '%s'\n", cod->bd->image);
+               pb_log("   initrd '%s'\n", cod->bd->initrd);
+               pb_log("   args   '%s'\n", cod->bd->args);
+               pb_log("   argsig '%s'\n", cod->bd->args_sig_file);
+       }
+
+       /* Update the plugin menu label if needed */
+       if (plugin_option) {
+               struct pmenu_item *item;
+               unsigned int j;
+               result = set_menu_items(cui->main->ncm, NULL);
+               for (j = 0 ; j < cui->main->item_count; j++) {
+                       item = item_userptr(cui->main->items[j]);
+                       if (strncmp(item->nci->name.str, "Plugins", strlen("Plugins")))
+                               continue;
+                       cui->n_plugins++;
+                       char *label = talloc_asprintf(item, "Plugins (%u)",
+                                       cui->n_plugins);
+                       pmenu_item_update(item, label);
+                       talloc_free(label);
+                       break;
+               }
+               result = set_menu_items(cui->main->ncm, cui->main->items);
+               if (result)
+                       pb_log("%s: set_menu_items failed: %d\n", __func__, result);
+       }
 
        /* Re-attach the items array. */
-       result = set_menu_items(cui->main->ncm, cui->main->items);
+       result = set_menu_items(menu->ncm, menu->items);
 
        if (result)
                pb_log("%s: set_menu_items failed: %d\n", __func__, result);
 
        if (0) {
                pb_log("%s\n", __func__);
-               pmenu_dump_items(cui->main->items,
-                       item_count(cui->main->ncm) + 1);
+               pmenu_dump_items(menu->items,
+                       item_count(menu->ncm) + 1);
        }
 
-       /* FIXME: need to make item visible somehow */
-       menu_driver(cui->main->ncm, REQ_SCR_UPAGE);
-       menu_driver(cui->main->ncm, REQ_SCR_DPAGE);
-       set_current_item(cui->main->ncm, selected);
+       if (!item_visible(selected)) {
+               int idx, top;
 
-       if (cui->current == &cui->main->scr)
-               cui->current->post(cui->current);
+               top = top_row(menu->ncm);
+               idx = item_index(selected);
+
+               /* If our index is above the current top row, align
+                * us to the new top. Otherwise, align us to the new
+                * bottom */
+               top = top < idx ? idx - rows + 1 : idx;
+
+               set_top_row(menu->ncm, top);
+               set_current_item(menu->ncm, selected);
+       }
+
+       if (cui->current == &menu->scr)
+               nc_scr_post(cui->current);
+       if (plugin_option && cui->current == &cui->main->scr)
+               nc_scr_post(cui->current);
 
        return 0;
 }
@@ -419,35 +809,82 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
  * Removes all the menu_items for the device from the main menu and redraws the
  * main menu if it is active.
  */
-
 static void cui_device_remove(struct device *dev, void *arg)
 {
        struct cui *cui = cui_from_arg(arg);
-       int result;
        struct boot_option *opt;
+       unsigned int i;
+       int rows, cols, top, last;
+       int result;
 
        pb_log("%s: %p %s\n", __func__, dev, dev->id);
 
        if (cui->current == &cui->main->scr)
-               cui->current->unpost(cui->current);
+               nc_scr_unpost(cui->current);
+       if (cui->current == &cui->plugin_menu->scr)
+               nc_scr_unpost(cui->current);
 
        /* This disconnects items array from menu. */
 
        result = set_menu_items(cui->main->ncm, NULL);
+       result |= set_menu_items(cui->plugin_menu->ncm, NULL);
 
        if (result)
                pb_log("%s: set_menu_items failed: %d\n", __func__, result);
 
        list_for_each_entry(&dev->boot_options, opt, list) {
-               struct pmenu_item *i = pmenu_item_from_arg(opt->ui_info);
+               struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
+
+               assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
+               if (opt->type == DISCOVER_PLUGIN_OPTION)
+                       pmenu_remove(cui->plugin_menu, item);
+               else
+                       pmenu_remove(cui->main, item);
+       }
+
+       /* Manually remove remaining device hierarchy item */
+       for (i=0; i < cui->main->item_count; i++) {
+               struct pmenu_item *item = item_userptr(cui->main->items[i]);
+               if (!item || !item->data )
+                       continue;
+
+               struct cui_opt_data *data = item->data;
+               if (data && data->dev && data->dev == dev)
+                       pmenu_remove(cui->main,item);
+       }
+       /* Look in plugins menu too */
+       for (i=0; i < cui->plugin_menu->item_count; i++) {
+               struct pmenu_item *item = item_userptr(cui->plugin_menu->items[i]);
+               if (!item || !item->data )
+                       continue;
 
-               assert(pb_protocol_device_cmp(dev, cod_from_item(i)->dev));
-               pmenu_remove(cui->main, i);
+               struct cui_opt_data *data = item->data;
+               if (data && data->dev && data->dev == dev)
+                       pmenu_remove(cui->plugin_menu,item);
        }
 
        /* Re-attach the items array. */
 
        result = set_menu_items(cui->main->ncm, cui->main->items);
+       result |= set_menu_items(cui->plugin_menu->ncm, cui->plugin_menu->items);
+
+       /* Move cursor to 'Exit' menu entry for the main menu.. */
+       menu_format(cui->main->ncm, &rows, &cols);
+       last = cui->main->item_count - 1;
+       set_current_item(cui->main->ncm, cui->main->items[last]);
+       if (!item_visible(cui->main->items[last])) {
+               top = last < rows ? 0 : last - rows + 1;
+               set_top_row(cui->main->ncm, top);
+       }
+
+       /* ..and the plugin menu */
+       menu_format(cui->plugin_menu->ncm, &rows, &cols);
+       last = cui->plugin_menu->item_count - 1;
+       set_current_item(cui->plugin_menu->ncm, cui->plugin_menu->items[last]);
+       if (!item_visible(cui->plugin_menu->items[last])) {
+               top = last < rows ? 0 : last - rows + 1;
+               set_top_row(cui->plugin_menu->ncm, top);
+       }
 
        if (result)
                pb_log("%s: set_menu_items failed: %d\n", __func__, result);
@@ -459,18 +896,170 @@ static void cui_device_remove(struct device *dev, void *arg)
        }
 
        if (cui->current == &cui->main->scr)
-               cui->current->post(cui->current);
+               nc_scr_post(cui->current);
+       if (cui->current == &cui->plugin_menu->scr)
+               nc_scr_post(cui->current);
+}
+
+static void cui_update_status(struct status *status, void *arg)
+{
+       struct cui *cui = cui_from_arg(arg);
+
+       statuslog_append_steal(cui, cui->statuslog, status);
+
+       /* Ignore status messages from the backlog */
+       if (!status->backlog)
+               nc_scr_status_printf(cui->current, "%s", status->message);
 }
 
-static void cui_update_status(struct boot_status *status, void *arg)
+/*
+ * Handle a new installed plugin option and update its associated
+ * (uninstalled) menu item if it exists.
+ */
+static int cui_plugin_option_add(struct plugin_option *opt, void *arg)
 {
+       struct cui_opt_data *cod;
        struct cui *cui = cui_from_arg(arg);
+       struct pmenu_item *item = NULL;
+       struct boot_option *dummy_opt;
+       struct device *dev;
+       unsigned int i;
+       int result;
+
+fallback:
+       /* Find uninstalled plugin by matching on plugin_file */
+       for (i = 0; i < cui->plugin_menu->item_count; i++) {
+               item = item_userptr(cui->plugin_menu->items[i]);
+               if (!item)
+                       continue;
+               cod = cod_from_item(item);
+               if (!cod || !cod->pd)
+                       continue;
+               if (strncmp(cod->pd->plugin_file, opt->plugin_file,
+                                       strlen(cod->pd->plugin_file)) == 0)
+                       break;
+       }
+
+       /*
+        * If pb-plugin was run manually there may not be an associated
+        * plugin-type boot_option. Pass a fake device and option to
+        * cui_boot_option_add() so we have an item to work with.
+        */
+       if (!item || i >= cui->plugin_menu->item_count) {
+               pb_log("New plugin option %s doesn't have a source item\n",
+                               opt->id);
+               dev = talloc_zero(cui, struct device);
+               dev->id = dev->name = talloc_asprintf(dev, "(unknown)");
+               dev->type = DEVICE_TYPE_UNKNOWN;
+               dummy_opt = talloc_zero(cui, struct boot_option);
+               dummy_opt->device_id = talloc_strdup(dummy_opt, dev->id);
+               dummy_opt->id = dummy_opt->name = talloc_asprintf(dummy_opt, "dummy");
+               dummy_opt->boot_image_file = talloc_strdup(dummy_opt, opt->plugin_file);
+               dummy_opt->type = DISCOVER_PLUGIN_OPTION;
+               cui_boot_option_add(dev, dummy_opt, cui);
+               goto fallback;
+       }
+
+       /*
+        * If this option was faked above move the context under
+        * the item so it is cleaned up later in cui_plugins_remove().
+        */
+       if (strcmp(cod->opt->id, "dummy") == 0 &&
+                       cod->dev->type == DEVICE_TYPE_UNKNOWN) {
+               talloc_steal(item, cod->dev);
+               talloc_steal(item, cod->opt);
+       }
+
+       talloc_free(cod->name);
+       /* Name is still tabbed across */
+       cod->name = talloc_asprintf(cod, _("  %s [installed]"), opt->name);
+
+       cod->pd->opt = opt;
+       item->on_execute = NULL;
+       item->on_edit = cui_show_plugin;
+
+       if (cui->current == &cui->plugin_menu->scr)
+               nc_scr_unpost(cui->current);
+
+       /* This disconnects items array from menu. */
+       result = set_menu_items(cui->plugin_menu->ncm, NULL);
+
+       if (result == E_OK)
+               pmenu_item_update(item, cod->name);
+
+       /* Re-attach the items array. */
+       result = set_menu_items(cui->plugin_menu->ncm, cui->plugin_menu->items);
+
+       if (cui->current == &cui->plugin_menu->scr)
+               nc_scr_post(cui->current);
+
+       return result;
+}
+
+/*
+ * Most plugin menu items will be removed via cui_device_remove(). However if
+ * pb-plugin has been run manually it is possible that there a plugin items
+ * not associated with a device - remove them here.
+ */
+static int cui_plugins_remove(void *arg)
+{
+       struct cui *cui = cui_from_arg(arg);
+       struct pmenu_item *item = NULL;
+       struct cui_opt_data *cod;
+       unsigned int i = 0;
+
+       pb_debug("%s\n", __func__);
+
+       if (cui->current == &cui->plugin_menu->scr)
+               nc_scr_unpost(cui->current);
+       if (cui->current == &cui->main->scr)
+               nc_scr_unpost(cui->current);
+
+       /* This disconnects items array from menu. */
+       set_menu_items(cui->plugin_menu->ncm, NULL);
+
+       while (i < cui->plugin_menu->item_count) {
+               item = item_userptr(cui->plugin_menu->items[i]);
+               if (!item || !item->data) {
+                       i++;
+                       continue;
+               }
+               cod = cod_from_item(item);
+               if (!cod->opt && !cod->dev) {
+                       i++;
+                       continue;
+               }
 
-       nc_scr_status_printf(cui->current,
-                       "%s: %s",
-                       status->type == BOOT_STATUS_ERROR ? "Error" : "Info",
-                       status->message);
+               pmenu_remove(cui->plugin_menu, item);
+               /* plugin_menu entries will shift, jump to bottom to make sure
+                * we remove all plugin option items */
+               i = 0;
+       }
+
+       /* Re-attach the items array. */
+       set_menu_items(cui->plugin_menu->ncm, cui->plugin_menu->items);
+
+       set_menu_items(cui->main->ncm, NULL);
+       for (i = 0; i < cui->main->item_count; i++) {
+               item = item_userptr(cui->main->items[i]);
+               if (strncmp(item->nci->name.str, "Plugins", strlen("Plugins")))
+                       continue;
+               cui->n_plugins = 0;
+               pmenu_item_update(item, "Plugins (0)");
+               break;
+       }
+
+       set_menu_items(cui->main->ncm, cui->main->items);
 
+       if (cui->current == &cui->main->scr)
+               nc_scr_post(cui->current);
+
+       /* If we're currently in a plugin screen jump back to the plugin menu */
+       if (cui->plugin_screen &&
+                       cui->current == plugin_screen_scr(cui->plugin_screen))
+               cui_plugin_exit(cui);
+
+       return 0;
 }
 
 static void cui_update_mm_title(struct cui *cui)
@@ -485,24 +1074,353 @@ static void cui_update_mm_title(struct cui *cui)
                                " %s", cui->sysinfo->identifier);
 
        if (cui->current == &cui->main->scr)
-               cui->current->post(cui->current);
+               nc_scr_post(cui->current);
+
+       frame = &cui->plugin_menu->scr.frame;
+
+       talloc_free(frame->rtitle);
+
+       frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
+       if (cui->sysinfo->identifier)
+               frame->rtitle = talloc_asprintf_append(frame->rtitle,
+                               " %s", cui->sysinfo->identifier);
+
+       if (cui->current == &cui->main->scr)
+               nc_scr_post(cui->current);
 }
 
 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
 {
        struct cui *cui = cui_from_arg(arg);
        cui->sysinfo = talloc_steal(cui, sysinfo);
+
+       /* if we're currently displaying the system info screen, inform it
+        * of the updated information. */
+       if (cui->sysinfo_screen)
+               sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
+
+       if (cui->subset_screen)
+               subset_screen_update(cui->subset_screen);
+
+       /* ... and do the same with the config screen... */
+       if (cui->config_screen)
+               config_screen_update(cui->config_screen, cui->config, sysinfo);
+
+       /* ... and the boot editor. */
+       if (cui->boot_editor)
+               boot_editor_update(cui->boot_editor, sysinfo);
+
        cui_update_mm_title(cui);
 }
 
+static void cui_update_language(struct cui *cui, char *lang)
+{
+       bool repost_menu;
+       char *cur_lang;
+
+       cur_lang = setlocale(LC_ALL, NULL);
+       if (cur_lang && !strcmp(cur_lang, lang))
+               return;
+
+       setlocale(LC_ALL, lang);
+
+       /* we'll need to update the menu: drop all items and repopulate */
+       repost_menu = cui->current == &cui->main->scr ||
+               cui->current == &cui->plugin_menu->scr;
+       if (repost_menu)
+               nc_scr_unpost(cui->current);
+
+       talloc_free(cui->main);
+       cui->main = main_menu_init(cui);
+       cui->plugin_menu = plugin_menu_init(cui);
+
+       if (repost_menu) {
+               cui->current = &cui->main->scr;
+               nc_scr_post(cui->current);
+       }
+
+       discover_client_enumerate(cui->client);
+}
+
+static void cui_update_config(struct config *config, void *arg)
+{
+       struct cui *cui = cui_from_arg(arg);
+       cui->config = talloc_steal(cui, config);
+
+       if (config->lang)
+               cui_update_language(cui, config->lang);
+
+       if (cui->subset_screen)
+               subset_screen_update(cui->subset_screen);
+
+       if (cui->config_screen)
+               config_screen_update(cui->config_screen, config, cui->sysinfo);
+
+       if (config->safe_mode)
+               nc_scr_status_printf(cui->current,
+                               _("SAFE MODE: select '%s' to continue"),
+                               _("Rescan devices"));
+}
+
+int cui_send_config(struct cui *cui, struct config *config)
+{
+       return discover_client_send_config(cui->client, config);
+}
+
+int cui_send_url(struct cui *cui, char * url)
+{
+       return discover_client_send_url(cui->client, url);
+}
+
+int cui_send_plugin_install(struct cui *cui, char *file)
+{
+       return discover_client_send_plugin_install(cui->client, file);
+}
+
+void cui_send_reinit(struct cui *cui)
+{
+       discover_client_send_reinit(cui->client);
+}
+
+static int menu_sysinfo_execute(struct pmenu_item *item)
+{
+       cui_show_sysinfo(cui_from_item(item));
+       return 0;
+}
+
+static int menu_config_execute(struct pmenu_item *item)
+{
+       cui_show_config(cui_from_item(item));
+       return 0;
+}
+
+static int menu_lang_execute(struct pmenu_item *item)
+{
+       cui_show_lang(cui_from_item(item));
+       return 0;
+}
+
+static int menu_statuslog_execute(struct pmenu_item *item)
+{
+       cui_show_statuslog(cui_from_item(item));
+       return 0;
+}
+
+static int menu_reinit_execute(struct pmenu_item *item)
+{
+       if (cui_from_item(item)->client)
+               cui_send_reinit(cui_from_item(item));
+       return 0;
+}
+
+static int menu_add_url_execute(struct pmenu_item *item)
+{
+       if (cui_from_item(item)->client)
+               cui_show_add_url(cui_from_item(item));
+       return 0;
+}
+
+static int menu_plugin_execute(struct pmenu_item *item)
+{
+       if (cui_from_item(item)->client)
+               cui_show_plugin_menu(cui_from_item(item));
+       return 0;
+}
+
+/**
+ * pb_mm_init - Setup the main menu instance.
+ */
+static struct pmenu *main_menu_init(struct cui *cui)
+{
+       struct pmenu_item *i;
+       struct pmenu *m;
+       int result;
+       bool lockdown = lockdown_active();
+
+       m = pmenu_init(cui, 9, cui_on_exit);
+       if (!m) {
+               pb_log("%s: failed\n", __func__);
+               return NULL;
+       }
+
+       m->n_hot_keys = 1;
+       m->hot_keys = talloc_array(m, hot_key_fn, m->n_hot_keys);
+       if (!m->hot_keys) {
+               pb_log("%s: failed to allocate hot_keys\n", __func__);
+               talloc_free(m);
+               return NULL;
+       }
+       m->hot_keys[0] = pmenu_main_hot_keys;
+       m->on_new = cui_item_new;
+
+       m->scr.frame.ltitle = talloc_asprintf(m,
+               "Petitboot (" PACKAGE_VERSION ")");
+       m->scr.frame.rtitle = NULL;
+       m->scr.frame.help = talloc_strdup(m,
+               _("Enter=accept, e=edit, n=new, x=exit, l=language, g=log, h=help"));
+       m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
+
+       /* add a separator */
+       i = pmenu_item_create(m, " ");
+       item_opts_off(i->nci, O_SELECTABLE);
+       pmenu_item_insert(m, i, 0);
+
+       /* add system items */
+       i = pmenu_item_create(m, _("System information"));
+       i->on_execute = menu_sysinfo_execute;
+       pmenu_item_insert(m, i, 1);
+
+       i = pmenu_item_create(m, _("System configuration"));
+       i->on_execute = menu_config_execute;
+       pmenu_item_insert(m, i, 2);
+
+       i = pmenu_item_create(m, _("System status log"));
+       i->on_execute = menu_statuslog_execute;
+       pmenu_item_insert(m, i, 3);
+
+       /* this label isn't translated, so we don't want a gettext() here */
+       i = pmenu_item_create(m, "Language");
+       i->on_execute = menu_lang_execute;
+       pmenu_item_insert(m, i, 4);
+
+       i = pmenu_item_create(m, _("Rescan devices"));
+       i->on_execute = menu_reinit_execute;
+       pmenu_item_insert(m, i, 5);
+
+       i = pmenu_item_create(m, _("Retrieve config from URL"));
+       i->on_execute = menu_add_url_execute;
+       pmenu_item_insert(m, i, 6);
+
+       i = pmenu_item_create(m, _("Plugins (0)"));
+       i->on_execute = menu_plugin_execute;
+       pmenu_item_insert(m, i, 7);
+
+       if (lockdown)
+               i = pmenu_item_create(m, _("Reboot"));
+       else
+               i = pmenu_item_create(m, _("Exit to shell"));
+       i->on_execute = pmenu_exit_cb;
+       pmenu_item_insert(m, i, 8);
+
+       result = pmenu_setup(m);
+
+       if (result) {
+               pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
+                       strerror(errno));
+               goto fail_setup;
+       }
+
+       m->help_title = _("main menu");
+       m->help_text = &main_menu_help_text;
+
+       menu_opts_off(m->ncm, O_SHOWDESC);
+       set_menu_mark(m->ncm, " *");
+       set_current_item(m->ncm, i->nci);
+
+       return m;
+
+fail_setup:
+       talloc_free(m);
+       return NULL;
+}
+
+/*
+ * plugin_menu_init: Set up the plugin menu instance
+ */
+static struct pmenu *plugin_menu_init(struct cui *cui)
+{
+       struct pmenu_item *i;
+       struct pmenu *m;
+       int result;
+
+       m = pmenu_init(cui, 2, cui_plugin_menu_exit);
+       m->scr.frame.ltitle = talloc_asprintf(m, _("Petitboot Plugins"));
+       m->scr.frame.rtitle = talloc_asprintf(m, "%s", "");
+       m->scr.frame.help = talloc_strdup(m,
+               _("Enter=install, e=details, x=exit, h=help"));
+       m->scr.frame.status = talloc_asprintf(m,
+                       _("Available Petitboot Plugins"));
+
+       /* add a separator */
+       i = pmenu_item_create(m, " ");
+       item_opts_off(i->nci, O_SELECTABLE);
+       pmenu_item_insert(m, i, 0);
+
+       i = pmenu_item_create(m, _("Return to Main Menu"));
+       i->on_execute = pmenu_exit_cb;
+       pmenu_item_insert(m, i, 1);
+
+       result = pmenu_setup(m);
+
+       if (result) {
+               pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
+                       strerror(errno));
+               goto fail_setup;
+       }
+
+       m->help_title = _("plugin menu");
+       m->help_text = &plugin_menu_help_text;
+
+       return m;
+
+fail_setup:
+       talloc_free(m);
+       return NULL;
+}
+
 static struct discover_client_ops cui_client_ops = {
        .device_add = NULL,
        .boot_option_add = cui_boot_option_add,
        .device_remove = cui_device_remove,
+       .plugin_option_add = cui_plugin_option_add,
+       .plugins_remove = cui_plugins_remove,
        .update_status = cui_update_status,
        .update_sysinfo = cui_update_sysinfo,
+       .update_config = cui_update_config,
 };
 
+/* cui_server_wait - Connect to the discover server.
+ * @arg: Pointer to the cui instance.
+ *
+ * A timeout callback that attempts to connect to the discover server; on
+ * failure it registers itself with a one second timeout to try again.
+ * On success the cui->client struct will be set.
+ *
+ * Since this updates the status line when called it must not be called
+ * before the UI is ready.
+ */
+static int cui_server_wait(void *arg)
+{
+       struct cui *cui = cui_from_arg(arg);
+
+       if (cui->client) {
+               pb_debug("We already have a server!\n");
+               return 0;
+       }
+
+       /* We haven't yet connected to the server */
+       pb_log("Trying to connect...\n");
+       cui->client = discover_client_init(cui->waitset,
+                       &cui_client_ops, cui);
+
+       if (!cui->client) {
+               waiter_register_timeout(cui->waitset, 1000, cui_server_wait,
+                                       cui);
+               nc_scr_status_printf(cui->current,
+                                    "Info: Waiting for device discovery");
+       } else {
+               nc_scr_status_free(cui->current);
+               talloc_steal(cui, cui->client);
+
+               if (cui->has_input) {
+                       pb_log("Aborting default boot on pb-discover connect\n");
+                       discover_client_cancel_default(cui->client);
+               }
+       }
+
+       return 0;
+}
+
 /**
  * cui_init - Setup the cui instance.
  * @platform_info: A value for the struct cui platform_info member.
@@ -514,31 +1432,29 @@ static struct discover_client_ops cui_client_ops = {
  */
 
 struct cui *cui_init(void* platform_info,
-       int (*js_map)(const struct js_event *e), int start_deamon)
+       int (*js_map)(const struct js_event *e), int start_daemon, int timeout)
 {
        struct cui *cui;
        unsigned int i;
 
        cui = talloc_zero(NULL, struct cui);
-
        if (!cui) {
                pb_log("%s: alloc cui failed.\n", __func__);
-               fprintf(stderr, "%s: alloc cui failed.\n", __func__);
+               fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
                goto fail_alloc;
        }
 
        cui->c_sig = pb_cui_sig;
        cui->platform_info = platform_info;
        cui->waitset = waitset_create(cui);
+       cui->statuslog = statuslog_init(cui);
 
        process_init(cui, cui->waitset, false);
 
-       setlocale(LC_ALL, "");
-
        /* Loop here for scripts that just started the server. */
 
 retry_start:
-       for (i = start_deamon ? 2 : 10; i; i--) {
+       for (i = start_daemon ? 2 : 15; i && timeout; i--) {
                cui->client = discover_client_init(cui->waitset,
                                &cui_client_ops, cui);
                if (cui->client || !i)
@@ -547,10 +1463,10 @@ retry_start:
                sleep(1);
        }
 
-       if (!cui->client && start_deamon) {
+       if (!cui->client && start_daemon) {
                int result;
 
-               start_deamon = 0;
+               start_daemon = 0;
 
                result = pb_start_daemon(cui);
 
@@ -558,24 +1474,38 @@ retry_start:
                        goto retry_start;
 
                pb_log("%s: discover_client_init failed.\n", __func__);
-               fprintf(stderr, "%s: error: discover_client_init failed.\n",
+               fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
                        __func__);
-               fprintf(stderr, "could not start pb-discover, the petitboot "
-                       "daemon.\n");
+               fprintf(stderr, _("could not start pb-discover, the petitboot "
+                       "daemon.\n"));
                goto fail_client_init;
        }
 
-       if (!cui->client) {
+       if (!cui->client && !timeout) {
+               /* Have the first timeout fire immediately so we can check
+                * for the server as soon as the UI is ready */
+               waiter_register_timeout(cui->waitset, 0,
+                                       cui_server_wait, cui);
+       } else if (!cui->client) {
                pb_log("%s: discover_client_init failed.\n", __func__);
-               fprintf(stderr, "%s: error: discover_client_init failed.\n",
+               fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
                        __func__);
-               fprintf(stderr, "check that pb-discover, "
-                       "the petitboot daemon is running.\n");
+               fprintf(stderr, _("check that pb-discover, "
+                       "the petitboot daemon is running.\n"));
                goto fail_client_init;
        }
 
-       atexit(nc_atexit);
-       nc_start();
+       atexit(cui_atexit);
+       talloc_steal(cui, cui->client);
+       cui_start();
+
+       cui->main = main_menu_init(cui);
+       if (!cui->main)
+               goto fail_client_init;
+
+       cui->plugin_menu = plugin_menu_init(cui);
+       if (!cui->plugin_menu)
+               goto fail_client_init;
 
        waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
                        cui_process_key, cui);
@@ -597,6 +1527,41 @@ fail_alloc:
        return NULL;
 }
 
+/**
+ * cui_cancel_autoboot_on_exit - On exit spin until the server is available.
+ *
+ * If the program exits before connecting to the server autoboot won't be
+ * cancelled even though there has been keyboard activity. A child is forked
+ * which will spin until the server is connected and told to cancel autoboot.
+ */
+static void cui_cancel_autoboot_on_exit(struct cui *cui)
+{
+       pid_t pid;
+
+       if (!cui->client) {
+               /* Fork a child to tell the server to cancel autoboot */
+               pid = fork();
+               if (!pid) {
+                       cui_detached = true;
+
+                       /* Loop until connection established */
+                       while (!cui->client) {
+                               cui->client = discover_client_init(cui->waitset,
+                                               &cui_client_ops, cui);
+                               if (!cui->client)
+                                       sleep(1);
+                       }
+
+                       talloc_steal(cui, cui->client);
+                       discover_client_cancel_default(cui->client);
+                       exit(EXIT_SUCCESS);
+               }
+               if (pid < 0)
+                       pb_log("Failed to fork child on exit: %m\n");
+       } else
+               discover_client_cancel_default(cui->client);
+}
+
 /**
  * cui_run - The main cui program loop.
  * @cui: The cui instance.
@@ -607,15 +1572,15 @@ fail_alloc:
  * Returns 0 on success (return to shell), -1 on error (should restart).
  */
 
-int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
+int cui_run(struct cui *cui)
 {
-       assert(main);
+       assert(cui);
+       assert(cui->main);
 
-       cui->main = main;
        cui->current = &cui->main->scr;
-       cui->default_item = default_item;
+       cui->default_item = 0;
 
-       cui->current->post(cui->current);
+       nc_scr_post(cui->current);
 
        while (1) {
                int result = waiter_poll(cui->waitset);
@@ -634,7 +1599,9 @@ int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
                }
        }
 
-       nc_atexit();
+       cui_cancel_autoboot_on_exit(cui);
+
+       cui_atexit();
 
        return cui->abort ? 0 : -1;
 }