mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 13/18] boot: Use struct bootentries to pass around data
Date: Fri, 22 Jul 2016 14:44:27 +0200	[thread overview]
Message-ID: <1469191472-14491-14-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1469191472-14491-1-git-send-email-s.hauer@pengutronix.de>

We have a struct bootentries type to collect different boot entries,
so use this to pass around data between functions rather than using
an array of strings. With this we also no longer have to convert a
string to a boot entry multiple times.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/boot.c | 131 ++++++++++----------------------------------------------
 1 file changed, 22 insertions(+), 109 deletions(-)

diff --git a/commands/boot.c b/commands/boot.c
index f193c93..58968a2 100644
--- a/commands/boot.c
+++ b/commands/boot.c
@@ -134,6 +134,8 @@ static int boot_entry(struct bootentry *be)
 {
 	int ret;
 
+	printf("booting %s\n", be->title);
+
 	if (IS_ENABLED(CONFIG_WATCHDOG) && boot_watchdog_timeout) {
 		ret = watchdog_set_timeout(boot_watchdog_timeout);
 		if (ret)
@@ -142,6 +144,9 @@ static int boot_entry(struct bootentry *be)
 
 	ret = be->boot(be, verbose, dryrun);
 
+	if (ret)
+		printf("booting %s failed: %s\n", be->title, strerror(-ret));
+
 	return ret;
 }
 
@@ -287,33 +292,10 @@ static int bootentry_parse_one(struct bootentries *bootentries, const char *name
 }
 
 /*
- * bootentries_collect - collect bootentries from an array of names
- */
-static struct bootentries *bootentries_collect(char *entries[], int num_entries)
-{
-	struct bootentries *bootentries;
-	int i;
-
-	bootentries = bootentries_alloc();
-
-	if (!num_entries)
-		bootscript_scan_path(bootentries, "/env/boot");
-
-	if (IS_ENABLED(CONFIG_BLSPEC) && !num_entries)
-		blspec_scan_devices(bootentries);
-
-	for (i = 0; i < num_entries; i++)
-		bootentry_parse_one(bootentries, entries[i]);
-
-	return bootentries;
-}
-
-/*
  * bootsources_menu - show a menu from an array of names
  */
-static void bootsources_menu(char *entries[], int num_entries)
+static void bootsources_menu(struct bootentries *bootentries)
 {
-	struct bootentries *bootentries = NULL;
 	struct bootentry *entry;
 	struct menu_entry *back_entry;
 
@@ -322,10 +304,6 @@ static void bootsources_menu(char *entries[], int num_entries)
 		return;
 	}
 
-	bootentries = bootentries_collect(entries, num_entries);
-	if (!bootentries)
-		return;
-
 	bootentries_for_each_entry(bootentries, entry) {
 		if (!entry->me.display)
 			entry->me.display = xstrdup(entry->title);
@@ -345,77 +323,29 @@ static void bootsources_menu(char *entries[], int num_entries)
 	menu_show(bootentries->menu);
 
 	free(back_entry);
-
-	bootentries_free(bootentries);
 }
 
 /*
  * bootsources_list - list boot entries from an array of names
  */
-static void bootsources_list(char *entries[], int num_entries)
+static void bootsources_list(struct bootentries *bootentries)
 {
-	struct bootentries *bootentries;
 	struct bootentry *entry;
 
-	bootentries = bootentries_collect(entries, num_entries);
-	if (!bootentries)
-		return;
-
 	printf("%-20s\n", "title");
 	printf("%-20s\n", "------");
 
 	bootentries_for_each_entry(bootentries, entry)
 		printf("%-20s %s\n", entry->title, entry->description);
-
-	bootentries_free(bootentries);
-}
-
-/*
- * boot a script or a bootspec entry. 'name' can be:
- * - a filename under /env/boot/
- * - a full path to a boot script
- * - a device name
- * - a partition name under /dev/
- * - a full path to a directory which
- *   - contains boot scripts, or
- *   - contains a loader/entries/ directory containing bootspec entries
- *
- * Returns a negative error on failure, or 0 on a successful dryrun boot.
- */
-static int boot(const char *name)
-{
-	struct bootentries *bootentries;
-	struct bootentry *entry;
-	int ret;
-
-	bootentries = bootentries_alloc();
-	ret = bootentry_parse_one(bootentries, name);
-	if (ret < 0)
-		return ret;
-
-	if (!ret) {
-		printf("Nothing bootable found on %s\n", name);
-		return -ENOENT;
-	}
-
-	bootentries_for_each_entry(bootentries, entry) {
-		printf("booting %s\n", entry->title);
-		ret = boot_entry(entry);
-		if (!ret)
-			break;
-		printf("booting %s failed: %s\n", entry->title, strerror(-ret));
-	}
-
-	return ret;
 }
 
 static int do_boot(int argc, char *argv[])
 {
 	char *freep = NULL;
 	int opt, ret = 0, do_list = 0, do_menu = 0;
-	char **sources;
-	int num_sources;
 	int i;
+	struct bootentries *entries;
+	struct bootentry *entry;
 
 	verbose = 0;
 	dryrun = 0;
@@ -444,12 +374,14 @@ static int do_boot(int argc, char *argv[])
 		}
 	}
 
+	entries = bootentries_alloc();
+
 	if (optind < argc) {
-		num_sources = argc - optind;
-		sources = xmemdup(&argv[optind], sizeof(char *) * num_sources);
+		for (i = optind; i < argc; i++)
+			bootentry_parse_one(entries, argv[i]);
 	} else {
 		const char *def;
-		char *sep;
+		char *sep, *name;
 
 		def = getenv("global.boot.default");
 		if (!def)
@@ -457,49 +389,30 @@ static int do_boot(int argc, char *argv[])
 
 		sep = freep = xstrdup(def);
 
-		num_sources = 0;
-
-		while (1) {
-			num_sources++;
+		while ((name = strsep(&sep, " ")) != NULL)
+			bootentry_parse_one(entries, name);
 
-			sep = strchr(sep, ' ');
-			if (!sep)
-				break;
-			sep++;
-		}
-
-		sources = xmalloc(sizeof(char *) * num_sources);
-
-		sep = freep;
-
-		for (i = 0; i < num_sources; i++) {
-			sources[i] = sep;
-			sep = strchr(sep, ' ');
-			if (sep)
-				*sep = 0;
-			sep++;
-		}
+		free(freep);
 	}
 
 	if (do_list) {
-		bootsources_list(sources, num_sources);
+		bootsources_list(entries);
 		goto out;
 	}
 
 	if (do_menu) {
-		bootsources_menu(sources, num_sources);
+		bootsources_menu(entries);
 		goto out;
 	}
 
-	for (i = 0; i < num_sources; i++) {
-		ret = boot(sources[i]);
+	bootentries_for_each_entry(entries, entry) {
+		ret = boot_entry(entry);
 		if (!ret)
 			break;
 	}
 
 out:
-	free(sources);
-	free(freep);
+	bootentries_free(entries);
 
 	return ret;
 }
-- 
2.8.1


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

  parent reply	other threads:[~2016-07-22 12:45 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-22 12:44 Sascha Hauer
2016-07-22 12:44 ` [PATCH 01/18] blspec: remove unused blspec_boot_devicename Sascha Hauer
2016-07-22 12:44 ` [PATCH 02/18] blspec: Remove once/default handling Sascha Hauer
2016-07-22 12:44 ` [PATCH 03/18] blspec: remove unused function prototype Sascha Hauer
2016-07-22 12:44 ` [PATCH 04/18] boot: Call blspec_scan_directory() only on strings containing an absolute path Sascha Hauer
2016-07-22 12:44 ` [PATCH 05/18] include: Move bulk of boot.h to bootm.h Sascha Hauer
2016-07-22 12:44 ` [PATCH 06/18] blpec: rename struct lspec -> bootentries Sascha Hauer
2016-07-22 12:44 ` [PATCH 07/18] blspec: factor out a struct bootentry Sascha Hauer
2016-07-22 12:44 ` [PATCH 08/18] bootentries: Add title/description Sascha Hauer
2016-07-22 12:44 ` [PATCH 09/18] blspec: separate bootentries from blspec entries Sascha Hauer
2016-07-22 12:44 ` [PATCH 10/18] blspec: Make blspec_boot static Sascha Hauer
2016-07-22 12:44 ` [PATCH 11/18] bootentries: Move menu display string allocation to bootentries_alloc() Sascha Hauer
2016-07-22 12:44 ` [PATCH 12/18] bootentries: Move struct bootentries to include/boot.h Sascha Hauer
2016-07-22 12:44 ` Sascha Hauer [this message]
2016-07-22 12:44 ` [PATCH 14/18] boot: Move code to common/ Sascha Hauer
2016-07-22 12:44 ` [PATCH 15/18] boot: add single quotes when printing boot target names Sascha Hauer
2016-07-22 12:44 ` [PATCH 16/18] boot command: Explicitly complain when boot target list is empty Sascha Hauer
2016-07-22 12:44 ` [PATCH 17/18] blspec: Turn message back to debug level Sascha Hauer
2016-07-22 12:44 ` [PATCH 18/18] boot: Print a message when a boot target string does not lead to a boot target Sascha Hauer

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=1469191472-14491-14-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@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