diff mbox series

[RFC,13/13] ui/ncurses: Keep track of the default boot option

Message ID 20180628064151.13370-14-sam@mendozajonas.com
State RFC
Headers show
Series User support and client permissions | expand

Commit Message

Sam Mendoza-Jonas June 28, 2018, 6:41 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     |  3 +++
 lib/pb-protocol/pb-protocol.c |  6 ++++++
 lib/types/types.h             |  1 +
 ui/ncurses/nc-cui.c           | 35 +++++++++++++++++++++++++++++++++--
 4 files changed, 43 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/discover/device-handler.c b/discover/device-handler.c
index 6a5ff3f..847ebd4 100644
--- a/discover/device-handler.c
+++ b/discover/device-handler.c
@@ -889,6 +889,9 @@  static void set_default(struct device_handler *handler,
 		return;
 	}
 
+	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 c468097..2990a41 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);
 }
 
@@ -418,6 +419,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;
@@ -885,6 +888,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 9ab2a43..92510a1 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 7b9812c..8cdb43a 100644
--- a/ui/ncurses/nc-cui.c
+++ b/ui/ncurses/nc-cui.c
@@ -272,11 +272,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;
@@ -770,8 +775,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);
@@ -856,6 +862,27 @@  static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
 			pb_log("%s: set_menu_items failed: %d\n", __func__, 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);
 
@@ -900,6 +927,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;
@@ -922,6 +950,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)