diff mbox series

Lua: export bootenv functions to Lua handlers

Message ID 20181029090411.6536-1-christian.storm@siemens.com
State Accepted
Headers show
Series Lua: export bootenv functions to Lua handlers | expand

Commit Message

Storm, Christian Oct. 29, 2018, 9:04 a.m. UTC
Commit cca9446 exposes get_bootenv and set_bootenv to
Lua scripts so that they can modify the bootloader
environment. Do the same for Lua handlers.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/lua_interface.c | 11 +++++++++++
 include/swupdate.h      |  1 +
 parser/parser.c         |  4 ++++
 3 files changed, 16 insertions(+)

Comments

Stefano Babic Oct. 30, 2018, 2:13 p.m. UTC | #1
On 29/10/18 10:04, Christian Storm wrote:
> Commit cca9446 exposes get_bootenv and set_bootenv to
> Lua scripts so that they can modify the bootloader
> environment. Do the same for Lua handlers.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/lua_interface.c | 11 +++++++++++
>  include/swupdate.h      |  1 +
>  parser/parser.c         |  4 ++++
>  3 files changed, 16 insertions(+)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index 30fb909..d4ebe4a 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -920,6 +920,17 @@ static int l_handler_wrapper(struct img_type *img, void *data) {
>  		return -1;
>  	}
>  
> +	if (img->bootloader) {
> +		lua_getglobal(gL, "swupdate");
> +		if (!lua_istable(gL, -1)) {
> +			ERROR("Lua stack corrupted.");
> +			return -1;
> +		}
> +		lua_pushlightuserdata(gL, (void *)img->bootloader);
> +		luaL_setfuncs(gL, l_swupdate_bootenv, 1);
> +		lua_pop(gL, 1);
> +	}
> +
>  	l_func_ref = *((int*)data);
>  	/* get the callback function */
>  	lua_rawgeti(gL, LUA_REGISTRYINDEX, l_func_ref );
> diff --git a/include/swupdate.h b/include/swupdate.h
> index 741d24c..05604e8 100644
> --- a/include/swupdate.h
> +++ b/include/swupdate.h
> @@ -67,6 +67,7 @@ struct img_type {
>  	int is_script;
>  	int is_partitioner;
>  	struct dict properties;
> +	struct dict *bootloader; /* pointer to swupdate_cfg's bootloader dict for handlers to modify */
>  	long long partsize;
>  	int fdin;	/* Used for streaming file */
>  	off_t offset;	/* offset in cpio file */
> diff --git a/parser/parser.c b/parser/parser.c
> index 26cc5a4..19181d1 100644
> --- a/parser/parser.c
> +++ b/parser/parser.c
> @@ -510,6 +510,8 @@ static int parse_images(parsertype p, void *cfg, struct swupdate_cfg *swcfg, lua
>  
>  		add_properties(p, elem, image);
>  
> +		image->bootloader = &swcfg->bootloader;
> +
>  		skip = run_embscript(p, elem, image, L, swcfg->embscript);
>  		if (skip < 0) {
>  			free_image(image);
> @@ -586,6 +588,8 @@ static int parse_files(parsertype p, void *cfg, struct swupdate_cfg *swcfg, lua_
>  
>  		add_properties(p, elem, file);
>  
> +		file->bootloader = &swcfg->bootloader;
> +
>  		skip = run_embscript(p, elem, file, L, swcfg->embscript);
>  		if (skip < 0) {
>  			free_image(file);
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 30fb909..d4ebe4a 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -920,6 +920,17 @@  static int l_handler_wrapper(struct img_type *img, void *data) {
 		return -1;
 	}
 
+	if (img->bootloader) {
+		lua_getglobal(gL, "swupdate");
+		if (!lua_istable(gL, -1)) {
+			ERROR("Lua stack corrupted.");
+			return -1;
+		}
+		lua_pushlightuserdata(gL, (void *)img->bootloader);
+		luaL_setfuncs(gL, l_swupdate_bootenv, 1);
+		lua_pop(gL, 1);
+	}
+
 	l_func_ref = *((int*)data);
 	/* get the callback function */
 	lua_rawgeti(gL, LUA_REGISTRYINDEX, l_func_ref );
diff --git a/include/swupdate.h b/include/swupdate.h
index 741d24c..05604e8 100644
--- a/include/swupdate.h
+++ b/include/swupdate.h
@@ -67,6 +67,7 @@  struct img_type {
 	int is_script;
 	int is_partitioner;
 	struct dict properties;
+	struct dict *bootloader; /* pointer to swupdate_cfg's bootloader dict for handlers to modify */
 	long long partsize;
 	int fdin;	/* Used for streaming file */
 	off_t offset;	/* offset in cpio file */
diff --git a/parser/parser.c b/parser/parser.c
index 26cc5a4..19181d1 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -510,6 +510,8 @@  static int parse_images(parsertype p, void *cfg, struct swupdate_cfg *swcfg, lua
 
 		add_properties(p, elem, image);
 
+		image->bootloader = &swcfg->bootloader;
+
 		skip = run_embscript(p, elem, image, L, swcfg->embscript);
 		if (skip < 0) {
 			free_image(image);
@@ -586,6 +588,8 @@  static int parse_files(parsertype p, void *cfg, struct swupdate_cfg *swcfg, lua_
 
 		add_properties(p, elem, file);
 
+		file->bootloader = &swcfg->bootloader;
+
 		skip = run_embscript(p, elem, file, L, swcfg->embscript);
 		if (skip < 0) {
 			free_image(file);