mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/2] commands: boot: refactor boot entry creation to use iterators
Date: Wed, 12 Feb 2020 15:16:37 +0100	[thread overview]
Message-ID: <20200212141638.11114-1-a.fatoum@pengutronix.de> (raw)

We currently create all boot entries before attempting boot. This is
less than optimal, because this may involve probing devices that won't
be used for actual boot.

In preparation for changing this, refactor the code, so we only have one
loop we need to touch.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/boot.c | 44 ++++++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 16 deletions(-)

diff --git a/commands/boot.c b/commands/boot.c
index 0257b3dd4f18..ce4eeac6534c 100644
--- a/commands/boot.c
+++ b/commands/boot.c
@@ -21,13 +21,29 @@
 
 #include <linux/stat.h>
 
+static char *next_argv(void *context)
+{
+	char ***argv = context;
+	char *next = **argv;
+	(*argv)++;
+	return next;
+}
+
+static char *next_word(void *context)
+{
+	return strsep(context, " ");
+}
+
 static int do_boot(int argc, char *argv[])
 {
 	char *freep = NULL;
 	int opt, ret = 0, do_list = 0, do_menu = 0;
-	int i, dryrun = 0, verbose = 0, timeout = -1;
+	int dryrun = 0, verbose = 0, timeout = -1;
 	struct bootentries *entries;
 	struct bootentry *entry;
+	void *handle;
+	const char *name;
+	char *(*next)(void *);
 
 	while ((opt = getopt(argc, argv, "vldmt:w:")) > 0) {
 		switch (opt) {
@@ -54,31 +70,26 @@ static int do_boot(int argc, char *argv[])
 		}
 	}
 
-	entries = bootentries_alloc();
-
 	if (optind < argc) {
-		for (i = optind; i < argc; i++) {
-			ret = bootentry_create_from_name(entries, argv[i]);
-			if (ret <= 0)
-				printf("Nothing bootable found on '%s'\n", argv[i]);
-	       }
+		handle = &argv[optind];
+		next = next_argv;
 	} else {
 		const char *def;
-		char *sep, *name;
 
 		def = getenv("global.boot.default");
 		if (!def)
 			return 0;
 
-		sep = freep = xstrdup(def);
+		handle = freep = xstrdup(def);
+		next = next_word;
+	}
 
-		while ((name = strsep(&sep, " ")) != NULL) {
-			ret = bootentry_create_from_name(entries, name);
-			if (ret <= 0)
-				printf("Nothing bootable found on '%s'\n", name);
-		}
+	entries = bootentries_alloc();
 
-		free(freep);
+	while ((name = next(&handle)) != NULL) {
+		ret = bootentry_create_from_name(entries, name);
+		if (ret <= 0)
+			printf("Nothing bootable found on '%s'\n", name);
 	}
 
 	if (list_empty(&entries->entries)) {
@@ -104,6 +115,7 @@ static int do_boot(int argc, char *argv[])
 
 out:
 	bootentries_free(entries);
+	free(freep);
 
 	return ret;
 }
-- 
2.25.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

             reply	other threads:[~2020-02-12 14:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-12 14:16 Ahmad Fatoum [this message]
2020-02-12 14:16 ` [PATCH 2/2] commands: boot: create boot entries on demand Ahmad Fatoum
2020-02-14  7:56 ` [PATCH 1/2] commands: boot: refactor boot entry creation to use iterators Sascha Hauer
2020-02-14  8:12   ` Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200212141638.11114-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox