[v2,13/13] ui/ncurses: Keep track of the default boot option
diff mbox series

Message ID 20181128042012.25916-14-sam@mendozajonas.com
State Accepted
Headers show
Series
  • User support and client permissions
Related show

Commit Message

Samuel Mendoza-Jonas Nov. 28, 2018, 4:20 a.m. UTC
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 <sam@mendozajonas.com>
---
 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(-)

Patch
diff mbox series

diff --git a/discover/device-handler.c b/discover/device-handler.c
index 3c7943e1..6ccf26c3 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 5de382d2..d8771fcb 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 f5392c89..39760d91 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 88b8a91c..16e8c286 100644
--- a/ui/ncurses/nc-cui.c
+++ b/ui/ncurses/nc-cui.c
@@ -318,11 +318,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;
@@ -858,8 +863,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);
@@ -944,6 +950,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);
 
@@ -988,6 +1015,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;
@@ -1010,6 +1038,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)