diff mbox series

handlers: associate each handler with a mask

Message ID 1506957473-1645-1-git-send-email-sbabic@denx.de
State Accepted
Headers show
Series handlers: associate each handler with a mask | expand

Commit Message

Stefano Babic Oct. 2, 2017, 3:17 p.m. UTC
Do not allow that handlers thought for a section can be defined in
another section of sw-description. Scripts must be defined inside the
"scripts" section and cannot be overridden. Up now, scripts could be
defined even in the "files" and "images" by overriding the "type"
argument.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 core/handler.c                    |  3 +-
 core/parser.c                     | 83 ++++++++++++++++++++++++++-------------
 corelib/lua_interface.c           |  3 +-
 handlers/archive_handler.c        |  6 ++-
 handlers/boot_handler.c           |  6 ++-
 handlers/flash_hamming1_handler.c |  3 +-
 handlers/flash_handler.c          |  3 +-
 handlers/lua_scripthandler.c      |  2 +-
 handlers/raw_handler.c            |  6 ++-
 handlers/remote_handler.c         |  3 +-
 handlers/shell_scripthandler.c    |  9 +++--
 handlers/ubivol_handler.c         |  6 ++-
 include/handler.h                 | 18 ++++++++-
 13 files changed, 106 insertions(+), 45 deletions(-)
diff mbox series

Patch

diff --git a/core/handler.c b/core/handler.c
index 168dd80..7e9f3ed 100644
--- a/core/handler.c
+++ b/core/handler.c
@@ -33,7 +33,7 @@  struct installer_handler supported_types[MAX_INSTALLER_HANDLER];
 static unsigned long nr_installers = 0;
 
 int register_handler(const char *desc,
-		handler installer, void *data)
+		handler installer, HANDLER_MASK mask, void *data)
 {
 
 	if (nr_installers > MAX_INSTALLER_HANDLER - 1)
@@ -43,6 +43,7 @@  int register_handler(const char *desc,
 		      sizeof(supported_types[nr_installers].desc));
 	supported_types[nr_installers].installer = installer;
 	supported_types[nr_installers].data = data;
+	supported_types[nr_installers].mask = mask;
 	nr_installers++;
 
 	return 0;
diff --git a/core/parser.c b/core/parser.c
index d31d322..ce00bec 100644
--- a/core/parser.c
+++ b/core/parser.c
@@ -63,6 +63,48 @@  static int check_missing_hash(struct imglist *list)
 }
 #endif
 
+static int check_handler(struct img_type *item, unsigned int mask, const char *desc)
+{
+	struct installer_handler *hnd;
+
+	hnd = find_handler(item);
+	if (!hnd) {
+		ERROR("feature '%s' required for "
+		      "'%s' in %s is absent!",
+		      item->type, item->fname,
+		      SW_DESCRIPTION_FILENAME);
+		return -EINVAL;
+	}
+
+	if (!(hnd->mask & mask)) {
+		ERROR("feature '%s' is not allowed for "
+		      "'%s' in %s is absent!",
+		      item->type, desc,
+		      SW_DESCRIPTION_FILENAME);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int check_handler_list(struct imglist *list, unsigned int allowedmask,
+				const char *desc)
+{
+	struct img_type *item;
+	int ret;
+	if (!LIST_EMPTY(list)) {
+		LIST_FOREACH(item, list, next)
+		{
+			ret = check_handler(item, allowedmask, desc);
+
+			if (ret < 0)
+				return ret;
+		}
+	}
+
+	return 0;
+}
+
 int parse(struct swupdate_cfg *sw, const char *descfile)
 {
 	int ret = -1;
@@ -85,39 +127,26 @@  int parse(struct swupdate_cfg *sw, const char *descfile)
 		return ret;
 	}
 
-	struct img_type *item;
-	if (!LIST_EMPTY(&sw->scripts)) {
-		LIST_FOREACH(item, &sw->scripts, next)
-		{
-			if (!find_handler(item)) {
-				ERROR("feature '%s' required for script "
-				      "'%s' in %s is absent!",
-				      item->type, item->fname,
-				      SW_DESCRIPTION_FILENAME);
-				return -1;
-			}
-		}
-	}
-	if (!LIST_EMPTY(&sw->images)) {
-		LIST_FOREACH(item, &sw->images, next)
-		{
-			if (!find_handler(item)) {
-				ERROR("feature '%s' required for image "
-				      "'%s' in %s is absent!",
-				      item->type, item->fname,
-				      SW_DESCRIPTION_FILENAME);
-				return -1;
-			}
-		}
-	}
+	ret = check_handler_list(&sw->scripts, SCRIPT_HANDLER, "scripts");
+	ret |= check_handler_list(&sw->images, IMAGE_HANDLER | FILE_HANDLER,
+					"images / files");
+	ret |= check_handler_list(&sw->partitions, PARTITION_HANDLER,
+					"partitions");
+	if (ret)
+		return -EINVAL;
+
+	/*
+	 *  Bootloader is slightly different, it has no image
+	 *  but a list of variables
+	 */
 	struct img_type item_uboot = {.type = "uboot"};
 	struct img_type item_bootloader = {.type = "bootenv"};
 	if (!LIST_EMPTY(&sw->bootloader) &&
-		       	(!find_handler(&item_uboot) &&
+			(!find_handler(&item_uboot) &&
 			 !find_handler(&item_bootloader))) {
 		ERROR("bootloader support absent but %s has bootloader section!",
 		      SW_DESCRIPTION_FILENAME);
-		return -1;
+		return -EINVAL;
 	}
 
 #ifdef CONFIG_SIGNED_IMAGES
diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 0553c1d..73458ba 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -463,7 +463,8 @@  static int l_register_handler( lua_State *L ) {
 		*l_func_ref = luaL_ref (L, LUA_REGISTRYINDEX);
 		/* pop the arguments from the stack */
 		lua_pop (L, 2);
-		register_handler(handler_desc,l_handler_wrapper,l_func_ref);
+		register_handler(handler_desc, l_handler_wrapper,
+				 ANY_HANDLER, l_func_ref);
 		return 0;
 	}
 }
diff --git a/handlers/archive_handler.c b/handlers/archive_handler.c
index f86d1f2..ceb60a3 100644
--- a/handlers/archive_handler.c
+++ b/handlers/archive_handler.c
@@ -253,12 +253,14 @@  static int install_archive_image(struct img_type *img,
 __attribute__((constructor))
 void archive_handler(void)
 {
-	register_handler("archive", install_archive_image, NULL);
+	register_handler("archive", install_archive_image,
+				IMAGE_HANDLER | FILE_HANDLER, NULL);
 }
 
 /* This is an alias for the parsers */
 __attribute__((constructor))
 void untar_handler(void)
 {
-	register_handler("tar", install_archive_image, NULL);
+	register_handler("tar", install_archive_image,
+				IMAGE_HANDLER | FILE_HANDLER, NULL);
 }
diff --git a/handlers/boot_handler.c b/handlers/boot_handler.c
index 86c55d0..8cda3f6 100644
--- a/handlers/boot_handler.c
+++ b/handlers/boot_handler.c
@@ -75,10 +75,12 @@  static int install_boot_environment(struct img_type *img,
 __attribute__((constructor))
 static void uboot_handler(void)
 {
-	register_handler("uboot", install_boot_environment, NULL);
+	register_handler("uboot", install_boot_environment,
+				BOOTLOADER_HANDLER, NULL);
 }
 __attribute__((constructor))
 static void boot_handler(void)
 {
-	register_handler("bootloader", install_boot_environment, NULL);
+	register_handler("bootloader", install_boot_environment,
+				BOOTLOADER_HANDLER, NULL);
 }
diff --git a/handlers/flash_hamming1_handler.c b/handlers/flash_hamming1_handler.c
index 8b54d3a..511155e 100644
--- a/handlers/flash_hamming1_handler.c
+++ b/handlers/flash_hamming1_handler.c
@@ -331,5 +331,6 @@  static int install_flash_hamming_image(struct img_type *img,
 __attribute__((constructor))
 void flash_1bit_hamming_handler(void)
 {
-	register_handler("flash-hamming1", install_flash_hamming_image, (void *)1);
+	register_handler("flash-hamming1", install_flash_hamming_image,
+				IMAGE_HANDLER | FILE_HANDLER,  (void *)1);
 }
diff --git a/handlers/flash_handler.c b/handlers/flash_handler.c
index ff27aa7..a244b37 100644
--- a/handlers/flash_handler.c
+++ b/handlers/flash_handler.c
@@ -349,5 +349,6 @@  static int install_flash_image(struct img_type *img,
 __attribute__((constructor))
 void flash_handler(void)
 {
-	register_handler("flash", install_flash_image, NULL);
+	register_handler("flash", install_flash_image,
+				IMAGE_HANDLER | FILE_HANDLER, NULL);
 }
diff --git a/handlers/lua_scripthandler.c b/handlers/lua_scripthandler.c
index 1793878..e9cfd46 100644
--- a/handlers/lua_scripthandler.c
+++ b/handlers/lua_scripthandler.c
@@ -123,5 +123,5 @@  static int start_lua_script(struct img_type *img, void *data)
  __attribute__((constructor))
 static void lua_handler(void)
 {
-	register_handler("lua", start_lua_script, NULL);
+	register_handler("lua", start_lua_script, SCRIPT_HANDLER, NULL);
 }
diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
index 91e9bb7..0893ecc 100644
--- a/handlers/raw_handler.c
+++ b/handlers/raw_handler.c
@@ -106,11 +106,13 @@  static int install_raw_file(struct img_type *img,
 __attribute__((constructor))
 void raw_handler(void)
 {
-	register_handler("raw", install_raw_image, NULL);
+	register_handler("raw", install_raw_image,
+				IMAGE_HANDLER, NULL);
 }
 
 	__attribute__((constructor))
 void raw_filecopy_handler(void)
 {
-	register_handler("rawfile", install_raw_file, NULL);
+	register_handler("rawfile", install_raw_file,
+				FILE_HANDLER, NULL);
 }
diff --git a/handlers/remote_handler.c b/handlers/remote_handler.c
index 59661c0..47535da 100644
--- a/handlers/remote_handler.c
+++ b/handlers/remote_handler.c
@@ -211,5 +211,6 @@  cleanup:
 __attribute__((constructor))
 void remote_handler(void)
 {
-	register_handler("remote", install_remote_image, NULL);
+	register_handler("remote", install_remote_image,
+				IMAGE_HANDLER, NULL);
 }
diff --git a/handlers/shell_scripthandler.c b/handlers/shell_scripthandler.c
index 9095a5f..016c246 100644
--- a/handlers/shell_scripthandler.c
+++ b/handlers/shell_scripthandler.c
@@ -130,17 +130,20 @@  static int start_postinstall_script(struct img_type *img, void *data)
  __attribute__((constructor))
 static void shell_handler(void)
 {
-	register_handler("shellscript", start_shell_script, NULL);
+	register_handler("shellscript", start_shell_script,
+				SCRIPT_HANDLER, NULL);
 }
 
  __attribute__((constructor))
 static void shell_preinstall_handler(void)
 {
-	register_handler("preinstall", start_preinstall_script, NULL);
+	register_handler("preinstall", start_preinstall_script,
+				SCRIPT_HANDLER, NULL);
 }
 
  __attribute__((constructor))
 static void shell_postinstall_handler(void)
 {
-	register_handler("postinstall", start_postinstall_script, NULL);
+	register_handler("postinstall", start_postinstall_script,
+				SCRIPT_HANDLER, NULL);
 }
diff --git a/handlers/ubivol_handler.c b/handlers/ubivol_handler.c
index 0e262b6..3018b5b 100644
--- a/handlers/ubivol_handler.c
+++ b/handlers/ubivol_handler.c
@@ -251,6 +251,8 @@  static int adjust_volume(struct img_type *cfg,
 __attribute__((constructor))
 void ubi_handler(void)
 {
-	register_handler("ubivol", install_ubivol_image, NULL);
-	register_handler("ubipartition", adjust_volume, NULL);
+	register_handler("ubivol", install_ubivol_image,
+				IMAGE_HANDLER, NULL);
+	register_handler("ubipartition", adjust_volume,
+				PARTITION_HANDLER, NULL);
 }
diff --git a/include/handler.h b/include/handler.h
index 359be71..0659ff6 100644
--- a/include/handler.h
+++ b/include/handler.h
@@ -29,15 +29,31 @@  typedef enum {
 	POSTINSTALL
 } script_fn ;
 
+/*
+ * Use enum for mask to easy transfer to LUA
+ * scripts
+ */
+typedef enum {
+	IMAGE_HANDLER = 1,
+	FILE_HANDLER = 2,
+	SCRIPT_HANDLER = 4,
+	BOOTLOADER_HANDLER = 8,
+	PARTITION_HANDLER = 16
+} HANDLER_MASK;
+
+#define ANY_HANDLER (IMAGE_HANDLER | FILE_HANDLER | SCRIPT_HANDLER | \
+			BOOTLOADER_HANDLER | PARTITION_HANDLER)
+
 typedef int (*handler)(struct img_type *img, void *data);
 struct installer_handler{
 	char	desc[64];
 	handler installer;
 	void	*data;
+	unsigned int mask;
 };
 
 int register_handler(const char *desc, 
-		handler installer, void *data);
+		handler installer, HANDLER_MASK mask, void *data);
 
 struct installer_handler *find_handler(struct img_type *img);
 void print_registered_handlers(void);