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 09/18] blspec: separate bootentries from blspec entries
Date: Fri, 22 Jul 2016 14:44:23 +0200	[thread overview]
Message-ID: <1469191472-14491-10-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1469191472-14491-1-git-send-email-s.hauer@pengutronix.de>

This completes the separation of the blspec code from the boot
code. With this the boot code only handles generic boot entries
of type struct bootentry which are embedded into the type
(blspec/bootscript) specific structs.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/boot.c  | 100 ++++++++++++++++++++++++++++++++++++++++++-------------
 common/blspec.c  |  28 +++++++++++++++-
 include/blspec.h |  60 +++------------------------------
 3 files changed, 108 insertions(+), 80 deletions(-)

diff --git a/commands/boot.c b/commands/boot.c
index 19f2fb5..1ae2745 100644
--- a/commands/boot.c
+++ b/commands/boot.c
@@ -36,19 +36,69 @@ static int verbose;
 static int dryrun;
 static int timeout;
 
+int bootentries_add_entry(struct bootentries *entries, struct bootentry *entry)
+{
+	list_add_tail(&entry->list, &entries->entries);
+
+	return 0;
+}
+
+static struct bootentries *bootentries_alloc(void)
+{
+	struct bootentries *bootentries;
+
+	bootentries = xzalloc(sizeof(*bootentries));
+	INIT_LIST_HEAD(&bootentries->entries);
+
+	if (IS_ENABLED(CONFIG_MENU))
+		bootentries->menu = menu_alloc();
+
+	return bootentries;
+}
+
+static void bootentries_free(struct bootentries *bootentries)
+{
+	struct bootentry *be, *tmp;
+
+	list_for_each_entry_safe(be, tmp, &bootentries->entries, list) {
+		list_del(&be->list);
+		free(be->title);
+		free(be->description);
+		free(be->me.display);
+		be->release(be);
+	}
+
+	if (bootentries->menu)
+		free(bootentries->menu->display);
+	free(bootentries->menu);
+	free(bootentries);
+}
+
+struct bootentry_script {
+	struct bootentry entry;
+	char *scriptpath;
+};
+
 /*
  * Start a single boot script. 'path' is a full path to a boot script.
  */
-static int boot_script(char *path)
+static int bootscript_boot(struct bootentry *entry, int verbose, int dryrun)
 {
+	struct bootentry_script *bs = container_of(entry, struct bootentry_script, entry);
 	int ret;
+
 	struct bootm_data data = {};
 
+	if (dryrun) {
+		printf("Would run %s\n", bs->scriptpath);
+		return 0;
+	}
+
 	globalvar_set_match("linux.bootargs.dyn.", "");
 
-	ret = run_command(path);
+	ret = run_command(bs->scriptpath);
 	if (ret) {
-		printf("Running %s failed\n", path);
+		printf("Running %s failed\n", bs->scriptpath);
 		goto out;
 	}
 
@@ -61,7 +111,7 @@ static int boot_script(char *path)
 
 	ret = bootm_boot(&data);
 	if (ret)
-		pr_err("Booting %s failed: %s\n", basename(path), strerror(-ret));
+		pr_err("Booting %s failed: %s\n", basename(bs->scriptpath), strerror(-ret));
 out:
 	return ret;
 }
@@ -81,7 +131,6 @@ BAREBOX_MAGICVAR_NAMED(global_watchdog_timeout, global.boot.watchdog_timeout,
 static int boot_entry(struct bootentry *be)
 {
 	int ret;
-	struct blspec_entry *entry = container_of(be, struct blspec_entry, entry);
 
 	if (IS_ENABLED(CONFIG_WATCHDOG) && boot_watchdog_timeout) {
 		ret = watchdog_set_timeout(boot_watchdog_timeout);
@@ -89,14 +138,7 @@ static int boot_entry(struct bootentry *be)
 			pr_warn("Failed to enable watchdog: %s\n", strerror(-ret));
 	}
 
-	if (entry->scriptpath) {
-		ret = boot_script(entry->scriptpath);
-	} else {
-		if (IS_ENABLED(CONFIG_BLSPEC))
-			ret = blspec_boot(be, verbose, dryrun);
-		else
-			ret = -ENOSYS;
-	}
+	ret = be->boot(be, verbose, dryrun);
 
 	return ret;
 }
@@ -115,23 +157,35 @@ static void bootsource_action(struct menu *m, struct menu_entry *me)
 	read_key();
 }
 
+static void bootscript_entry_release(struct bootentry *entry)
+{
+	struct bootentry_script *bs = container_of(entry, struct bootentry_script, entry);
+
+	free(bs->scriptpath);
+	free(bs->entry.me.display);
+	free(bs);
+}
+
 /*
  * bootscript_create_entry - create a boot entry from a script name
  */
 static int bootscript_create_entry(struct bootentries *bootentries, const char *name)
 {
-	struct blspec_entry *be;
+	struct bootentry_script *bs;
 	enum filetype type;
 
 	type = file_name_detect_type(name);
 	if (type != filetype_sh)
 		return -EINVAL;
 
-	be = blspec_entry_alloc(bootentries);
-	be->entry.me.type = MENU_ENTRY_NORMAL;
-	be->scriptpath = xstrdup(name);
-	be->entry.title = xstrdup(basename(be->scriptpath));
-	be->entry.description = basprintf("script: %s", name);
+	bs = xzalloc(sizeof(*bs));
+	bs->entry.me.type = MENU_ENTRY_NORMAL;
+	bs->entry.release = bootscript_entry_release;
+	bs->entry.boot = bootscript_boot;
+	bs->scriptpath = xstrdup(name);
+	bs->entry.title = xstrdup(basename(bs->scriptpath));
+	bs->entry.description = basprintf("script: %s", name);
+	bootentries_add_entry(bootentries, &bs->entry);
 
 	return 0;
 }
@@ -238,7 +292,7 @@ static struct bootentries *bootentries_collect(char *entries[], int num_entries)
 	struct bootentries *bootentries;
 	int i;
 
-	bootentries = blspec_alloc();
+	bootentries = bootentries_alloc();
 
 	if (IS_ENABLED(CONFIG_MENU))
 		bootentries->menu->display = basprintf("boot");
@@ -293,7 +347,7 @@ static void bootsources_menu(char *entries[], int num_entries)
 
 	free(back_entry);
 
-	blspec_free(bootentries);
+	bootentries_free(bootentries);
 }
 
 /*
@@ -314,7 +368,7 @@ static void bootsources_list(char *entries[], int num_entries)
 	bootentries_for_each_entry(bootentries, entry)
 		printf("%-20s %s\n", entry->title, entry->description);
 
-	blspec_free(bootentries);
+	bootentries_free(bootentries);
 }
 
 /*
@@ -335,7 +389,7 @@ static int boot(const char *name)
 	struct bootentry *entry;
 	int ret;
 
-	bootentries = blspec_alloc();
+	bootentries = bootentries_alloc();
 	ret = bootentry_parse_one(bootentries, name);
 	if (ret < 0)
 		return ret;
diff --git a/common/blspec.c b/common/blspec.c
index dff0928..aa70685 100644
--- a/common/blspec.c
+++ b/common/blspec.c
@@ -55,6 +55,29 @@ const char *blspec_entry_var_get(struct blspec_entry *entry, const char *name)
 	return ret ? NULL : str;
 }
 
+static void blspec_entry_free(struct bootentry *be)
+{
+	struct blspec_entry *entry = container_of(be, struct blspec_entry, entry);
+
+	of_delete_node(entry->node);
+	free(entry->configpath);
+	free(entry->rootpath);
+	free(entry);
+}
+
+static struct blspec_entry *blspec_entry_alloc(struct bootentries *bootentries)
+{
+	struct blspec_entry *entry;
+
+	entry = xzalloc(sizeof(*entry));
+
+	entry->node = of_new_node(NULL, NULL);
+	entry->entry.release = blspec_entry_free;
+	entry->entry.boot = blspec_boot;
+
+	return entry;
+}
+
 /*
  * blspec_entry_open - open an entry given a path
  */
@@ -397,7 +420,7 @@ int blspec_scan_directory(struct bootentries *bootentries, const char *root)
 		entry->cdev = get_cdev_by_mountpath(root);
 
 		if (!entry_is_of_compatible(entry)) {
-			blspec_entry_free(entry);
+			blspec_entry_free(&entry->entry);
 			continue;
 		}
 
@@ -417,6 +440,9 @@ int blspec_scan_directory(struct bootentries *bootentries, const char *root)
 		free(hwdevname);
 
 		entry->entry.me.type = MENU_ENTRY_NORMAL;
+		entry->entry.release = blspec_entry_free;
+
+		bootentries_add_entry(bootentries, &entry->entry);
 	}
 
 	ret = found;
diff --git a/include/blspec.h b/include/blspec.h
index c956f0d..7a16ae7 100644
--- a/include/blspec.h
+++ b/include/blspec.h
@@ -14,6 +14,8 @@ struct bootentry {
 	struct menu_entry me;
 	char *title;
 	char *description;
+	int (*boot)(struct bootentry *entry, int verbose, int dryrun);
+	void (*release)(struct bootentry *entry);
 };
 
 struct blspec_entry {
@@ -23,8 +25,6 @@ struct blspec_entry {
 	struct cdev *cdev;
 	char *rootpath;
 	char *configpath;
-
-	char *scriptpath;
 };
 
 int blspec_entry_var_set(struct blspec_entry *entry, const char *name,
@@ -39,61 +39,9 @@ int blspec_scan_device(struct bootentries *bootentries, struct device_d *dev);
 int blspec_scan_devicename(struct bootentries *bootentries, const char *devname);
 int blspec_scan_directory(struct bootentries *bootentries, const char *root);
 
+int bootentries_add_entry(struct bootentries *entries, struct bootentry *entry);
+
 #define bootentries_for_each_entry(bootentries, entry) \
 	list_for_each_entry(entry, &bootentries->entries, list)
 
-static inline struct blspec_entry *blspec_entry_alloc(struct bootentries *bootentries)
-{
-	struct blspec_entry *entry;
-
-	entry = xzalloc(sizeof(*entry));
-
-	entry->node = of_new_node(NULL, NULL);
-
-	list_add_tail(&entry->entry.list, &bootentries->entries);
-
-	return entry;
-}
-
-static inline void blspec_entry_free(struct blspec_entry *entry)
-{
-	list_del(&entry->entry.list);
-	of_delete_node(entry->node);
-	free(entry->entry.me.display);
-	free(entry->entry.title);
-	free(entry->entry.description);
-	free(entry->scriptpath);
-	free(entry->configpath);
-	free(entry->rootpath);
-	free(entry);
-}
-
-static inline struct bootentries *blspec_alloc(void)
-{
-	struct bootentries *bootentries;
-
-	bootentries = xzalloc(sizeof(*bootentries));
-	INIT_LIST_HEAD(&bootentries->entries);
-
-	if (IS_ENABLED(CONFIG_MENU))
-		bootentries->menu = menu_alloc();
-
-	return bootentries;
-}
-
-static inline void blspec_free(struct bootentries *bootentries)
-{
-	struct bootentry *be, *tmp;
-	struct blspec_entry *entry;
-
-	list_for_each_entry_safe(be, tmp, &bootentries->entries, list) {
-		entry = container_of(be, struct blspec_entry, entry);
-		blspec_entry_free(entry);
-	}
-	if (bootentries->menu)
-		free(bootentries->menu->display);
-	free(bootentries->menu);
-	free(bootentries);
-}
-
 #endif /* __LOADER_H__ */
-- 
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 ` Sascha Hauer [this message]
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 ` [PATCH 13/18] boot: Use struct bootentries to pass around data Sascha Hauer
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-10-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