From 87017f0478536fcb927010618be483a5efe9260c Mon Sep 17 00:00:00 2001 From: Samuel Mendoza-Jonas Date: Thu, 28 Jun 2018 14:45:19 +1000 Subject: [PATCH] ui/ncurses: Keep track of the default boot option Keep track of the default boot option, and prefix its display name with a '(*)' to point it out to the user. This avoids having to authenticate with pb-discover even if only booting the default option. Signed-off-by: Samuel Mendoza-Jonas --- discover/device-handler.c | 4 ++++ lib/pb-protocol/pb-protocol.c | 6 ++++++ lib/types/types.h | 1 + ui/ncurses/nc-cui.c | 35 +++++++++++++++++++++++++++++++++-- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/discover/device-handler.c b/discover/device-handler.c index e446cab..e75f412 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -930,6 +930,10 @@ static void set_default(struct device_handler *handler, return; } + if (handler->default_boot_option) + handler->default_boot_option->option->is_autoboot_default = false; + opt->option->is_autoboot_default = true; + handler->sec_to_boot = config_get()->autoboot_timeout_sec; handler->default_boot_option = opt; handler->default_boot_option_priority = new_prio; diff --git a/lib/pb-protocol/pb-protocol.c b/lib/pb-protocol/pb-protocol.c index 5de382d..d8771fc 100644 --- a/lib/pb-protocol/pb-protocol.c +++ b/lib/pb-protocol/pb-protocol.c @@ -204,6 +204,7 @@ int pb_protocol_boot_option_len(const struct boot_option *opt) 4 + optional_strlen(opt->boot_args) + 4 + optional_strlen(opt->args_sig_file) + sizeof(opt->is_default) + + sizeof(opt->is_autoboot_default) + sizeof(opt->type); } @@ -434,6 +435,8 @@ int pb_protocol_serialise_boot_option(const struct boot_option *opt, *(bool *)pos = opt->is_default; pos += sizeof(bool); + *(bool *)pos = opt->is_autoboot_default; + pos += sizeof(bool); *(uint32_t *)pos = __cpu_to_be32(opt->type); pos += 4; @@ -925,6 +928,9 @@ int pb_protocol_deserialise_boot_option(struct boot_option *opt, opt->is_default = *(bool *)(pos); pos += sizeof(bool); len -= sizeof(bool); + opt->is_autoboot_default = *(bool *)(pos); + pos += sizeof(bool); + len -= sizeof(bool); if (read_u32(&pos, &len, &opt->type)) return -1; diff --git a/lib/types/types.h b/lib/types/types.h index f5392c8..39760d9 100644 --- a/lib/types/types.h +++ b/lib/types/types.h @@ -54,6 +54,7 @@ struct boot_option { char *boot_args; char *args_sig_file; bool is_default; + bool is_autoboot_default; struct list_item list; diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 495f785..d80e2c3 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -392,11 +392,16 @@ static void cui_boot_cb(struct nc_scr *scr) static int cui_boot_check(struct pmenu_item *item) { + struct cui_opt_data *cod = cod_from_item(item); struct cui *cui = cui_from_item(item); if (discover_client_authenticated(cui->client)) return cui_boot(item); + /* Client doesn't need authentication to boot the default option */ + if (cui->default_item == cod->opt_hash) + return cui_boot(item); + cui_show_auth(cui, item->pmenu->scr.main_ncw, false, cui_boot_cb); return 0; @@ -932,8 +937,9 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt, dev_hdr = pmenu_find_device(menu, dev, opt); /* All actual boot entries are 'tabbed' across */ - name = talloc_asprintf(menu, "%s%s", - tab, opt->name ? : "Unknown Name"); + name = talloc_asprintf(menu, "%s%s%s", + tab, opt->is_autoboot_default ? "(*) " : "", + opt->name ? : "Unknown Name"); /* Save the item in opt->ui_info for cui_device_remove() */ opt->ui_info = i = pmenu_item_create(menu, name); @@ -1018,6 +1024,27 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt, pb_log_fn("set_menu_items failed: %d\n", result); } + /* Update the default option */ + if (opt->is_autoboot_default) { + struct cui_opt_data *tmp; + struct pmenu_item *item; + unsigned int j; + if (cui->default_item) { + for (j = 0; j < cui->main->item_count; j++) { + item = item_userptr(cui->main->items[j]); + tmp = cod_from_item(item); + if (tmp->opt_hash == cui->default_item) { + char *label = talloc_asprintf(menu, "%s%s", + tab, tmp->name ? : "Unknown Name"); + pmenu_item_update(item, label); + talloc_free(label); + break; + } + } + } + cui->default_item = cod->opt_hash; + } + /* Re-attach the items array. */ result = set_menu_items(menu->ncm, menu->items); @@ -1062,6 +1089,7 @@ static int cui_boot_option_add(struct device *dev, struct boot_option *opt, static void cui_device_remove(struct device *dev, void *arg) { struct cui *cui = cui_from_arg(arg); + struct cui_opt_data *cod; struct boot_option *opt; unsigned int i; int rows, cols, top, last; @@ -1084,6 +1112,9 @@ static void cui_device_remove(struct device *dev, void *arg) list_for_each_entry(&dev->boot_options, opt, list) { struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info); + cod = cod_from_item(item); + if (cui->default_item == cod->opt_hash) + cui->default_item = 0; assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev)); if (opt->type == DISCOVER_PLUGIN_OPTION) -- 2.39.2