Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Jul 2018 18:18:20 +0000 (UTC)
From:      Warner Losh <imp@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r336768 - head/usr.sbin/efibootmgr
Message-ID:  <201807271818.w6RIIKaA008684@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: imp
Date: Fri Jul 27 18:18:20 2018
New Revision: 336768
URL: https://svnweb.freebsd.org/changeset/base/336768

Log:
  Add -b bootnum to allow creation of a specific boot number (rather
  than the auotmatic selection). This is important in some scripting
  environments.
  
  Also, remove bogus checks for bootnum != 0. 0 is a valid bootnum.
  
  Sponsored by: Netflix

Modified:
  head/usr.sbin/efibootmgr/efibootmgr.8
  head/usr.sbin/efibootmgr/efibootmgr.c

Modified: head/usr.sbin/efibootmgr/efibootmgr.8
==============================================================================
--- head/usr.sbin/efibootmgr/efibootmgr.8	Fri Jul 27 17:39:36 2018	(r336767)
+++ head/usr.sbin/efibootmgr/efibootmgr.8	Fri Jul 27 18:18:20 2018	(r336768)
@@ -36,7 +36,7 @@
 .Op Fl T
 .Op Fl o Ar bootorder
 .Op Fl verbose
-.Op Fl c -k Ar kernel -l Ar loader [-L Ar label] [--dry-run] 
+.Op Fl c -k Ar kernel -l Ar loader [-L Ar label] [--dry-run] [-b Bootvar]
 .Sh "DESCRIPTION"
 .Nm
 manipulates how UEFI Boot Managers boot the system.
@@ -70,6 +70,9 @@ Create a new Boot Variable
 The path to and name of the loader.
 .It Fl k -kernel Ar kernel
 The path to and name of the kernel.
+.It Fl b Bootvar
+When creating an entry, use Bootvar as the index.
+Fail if it already exists.
 .It Fl L -label Ar label
 An optional description for the entry.
 .It Fl D -dry-run

Modified: head/usr.sbin/efibootmgr/efibootmgr.c
==============================================================================
--- head/usr.sbin/efibootmgr/efibootmgr.c	Fri Jul 27 17:39:36 2018	(r336767)
+++ head/usr.sbin/efibootmgr/efibootmgr.c	Fri Jul 27 18:18:20 2018	(r336768)
@@ -82,6 +82,7 @@ typedef struct _bmgr_opts {
 	bool    delete_bootnext;
 	bool    del_timeout;
 	bool    dry_run;
+	bool	has_bootnum;
 	bool    once;
 	int	cp_src;
 	bool    set_active;
@@ -170,7 +171,7 @@ set_bootvar(const char *name, uint8_t *data, size_t si
 
 #define USAGE \
 	"   [-aAnNB Bootvar] [-t timeout] [-T] [-o bootorder] [-O] [--verbose] [--help] \n\
-  [-c -l loader [-k kernel ] [-L label] [--dry-run]]"
+  [-c -l loader [-k kernel ] [-L label] [--dry-run] [-b Bootvar]]"
 
 #define CREATE_USAGE \
 	"       efibootmgr -c -l loader [-k kernel] [-L label] [--dry-run]"
@@ -201,6 +202,10 @@ parse_args(int argc, char *argv[])
 			opts.set_active = true;
 			opts.bootnum = strtoul(optarg, NULL, 16);
 			break;
+		case 'b':
+			opts.has_bootnum = true;
+			opts.bootnum = strtoul(optarg, NULL, 16);
+			break;
 		case 'B':
 			opts.delete = true;
 			opts.bootnum = strtoul(optarg, NULL, 16);
@@ -266,12 +271,7 @@ parse_args(int argc, char *argv[])
 			errx(1, "%s",CREATE_USAGE);
 		return;
 	}
-	if (opts.set_bootnext && !(opts.bootnum))
-		errx(1, "%s", BOOTNEXT_USAGE);
 
-	if ((opts.set_active ||  opts.set_inactive) && !(opts.bootnum))
-		errx(1, "%s", ACTIVE_USAGE);
-
 	if (opts.order && !(opts.order))
 		errx(1, "%s", ORDER_USAGE);
 }
@@ -557,7 +557,23 @@ make_next_boot_var_name(void)
 	return name;
 }
 
+static char *
+make_boot_var_name(uint16_t bootnum)
+{
+	struct entry *v;
+	char *name;
 
+	LIST_FOREACH(v, &efivars, entries) {
+		if (v->idx == bootnum)
+			return NULL;
+	}
+
+	asprintf(&name, "%s%04X", "Boot", bootnum);
+	if (name == NULL)
+		err(1, "asprintf");
+	return name;
+}
+
 static size_t
 create_loadopt(uint8_t *buf, size_t bufmax, uint32_t attributes, efidp dp, size_t dp_size,
     const char *description, const uint8_t *optional_data, size_t optional_data_size)
@@ -605,7 +621,8 @@ create_loadopt(uint8_t *buf, size_t bufmax, uint32_t a
 
 
 static int
-make_boot_var(const char *label, const char *loader, const char *kernel, const char *env, bool dry_run)
+make_boot_var(const char *label, const char *loader, const char *kernel, const char *env, bool dry_run,
+	int bootnum)
 {
 	struct entry *new_ent;
 	uint32_t load_attrs = 0;
@@ -617,7 +634,10 @@ make_boot_var(const char *label, const char *loader, c
 
 	assert(label != NULL);
 
-	bootvar = make_next_boot_var_name();
+	if (bootnum == -1)
+		bootvar = make_next_boot_var_name();
+	else
+		bootvar = make_boot_var_name((uint16_t)bootnum);
 	if (bootvar == NULL)
 		err(1, "bootvar creation");
 	if (loader == NULL)
@@ -894,7 +914,8 @@ main(int argc, char *argv[])
 		 * side effect, adds to boot order, but not yet active.
 		 */
 		make_boot_var(opts.label ? opts.label : "",
-		    opts.loader, opts.kernel, opts.env, opts.dry_run);
+		    opts.loader, opts.kernel, opts.env, opts.dry_run,
+		    opts.has_bootnum ? opts.bootnum : -1);
 	else if (opts.set_active || opts.set_inactive )
 		handle_activity(opts.bootnum, opts.set_active);
 	else if (opts.order != NULL)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201807271818.w6RIIKaA008684>