From: Stefan Herbrechtsmeier <
stefan.herb...@weidmueller.com>
Export get_bootenv and set_bootenv functions to the embedded Lua scripts.
The get function reads the bootloader environment variable direct and
the set function postone the write of the bootloader environment variable
to the end of the update process.
Changes in v2:
- Postpone set bootloader environment to the end of the update
corelib/lua_interface.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
include/lua_util.h | 5 +++--
parser/parser.c | 2 +-
3 files changed, 46 insertions(+), 4 deletions(-)
diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 25d84e6..57f5908 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -16,6 +16,7 @@
#include "lua_util.h"
#include "util.h"
#include "handler.h"
+#include "bootloader.h"
#define LUA_TYPE_PEMBSCR 1
#define LUA_TYPE_HANDLER 2
@@ -648,6 +649,36 @@ static int l_info(lua_State *L) {
return 0;
}
+static int l_get_bootenv(lua_State *L) {
+ const char *name = luaL_checkstring(L, 1);
+ char *value = NULL;
+
+ if (strlen(name))
+ value = bootloader_env_get(name);
+ lua_pop(L, 1);
+
+ lua_pushstring(L, value);
+ free(value);
+
+ return 1;
+}
+
+static int l_set_bootenv(lua_State *L) {
+ struct dict *bootenv = (struct dict *)lua_touserdata(L, lua_upvalueindex(1));
+ const char *name = luaL_checkstring(L, 1);
+ const char *value = luaL_checkstring(L, 2);
+
+ if (strlen(name)) {
+ if (strlen(value))
+ dict_set_value(bootenv, name, value);
+ else
+ dict_remove(bootenv, name);
+ }
+ lua_pop(L, 2);
+
+ return 0;
+}
+
#ifdef CONFIG_HANDLER_IN_LUA
static int l_get_tmpdir(lua_State *L)
{
@@ -667,6 +698,12 @@ static const luaL_Reg l_swupdate[] = {
{ NULL, NULL }
};
+static const luaL_Reg l_swupdate_bootenv[] = {
+ { "get_bootenv", l_get_bootenv },
+ { "set_bootenv", l_set_bootenv },
+ { NULL, NULL }
+};
+
#ifdef CONFIG_HANDLER_IN_LUA
static const luaL_Reg l_swupdate_handler[] = {
{ "register_handler", l_register_handler },
@@ -911,17 +948,21 @@ int lua_handlers_init(void)
int lua_handlers_init(void) {return 0;}
#endif
-lua_State *lua_parser_init(const char *buf)
+lua_State *lua_parser_init(const char *buf, struct dict *bootenv)
{
lua_State *L = luaL_newstate(); /* opens Lua */
if (!L)
return NULL;
+
lua_pushlightuserdata(L, (void*)LUA_TYPE_PEMBSCR);
lua_setglobal(L, "SWUPDATE_LUA_TYPE"); /* prime L as LUA_TYPE_PEMBSCR */
luaL_openlibs(L); /* opens the standard libraries */
luaL_requiref(L, "swupdate", luaopen_swupdate, 1 );
+ lua_pushlightuserdata(L, (void *)bootenv);
+ luaL_setfuncs(L, l_swupdate_bootenv, 1);
lua_pop(L, 1); /* remove unused copy left on stack */
+
if (luaL_loadstring(L, buf) || lua_pcall(L, 0, 0, 0)) {
LUAstackDump(L);
ERROR("ERROR preparing Lua embedded script in parser");
diff --git a/include/lua_util.h b/include/lua_util.h
index 479d175..ee5e2cf 100644
--- a/include/lua_util.h
+++ b/include/lua_util.h
@@ -17,7 +17,7 @@
void LUAstackDump (lua_State *L);
int run_lua_script(const char *script, const char *function, char *parms);
-lua_State *lua_parser_init(const char *buf);
+lua_State *lua_parser_init(const char *buf, struct dict *bootenv);
int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img);
int lua_handlers_init(void);
#define lua_parser_exit(L) lua_close((lua_State *)L)
@@ -63,7 +63,8 @@ void luaL_pushresult(luaL_Buffer_52 *B);
#define lua_State void
#define lua_parser_exit(L)
-static inline lua_State *lua_parser_init(const char __attribute__ ((__unused__)) *buf) { return NULL;}
+static inline lua_State *lua_parser_init(const char __attribute__ ((__unused__)) *buf,
+ struct dict __attribute__ ((__unused__)) *bootenv) { return NULL;}
static inline int lua_parser_fn(lua_State __attribute__ ((__unused__)) *L,
const char __attribute__ ((__unused__)) *fcn,
struct img_type __attribute__ ((__unused__)) *img) { return -1; }
diff --git a/parser/parser.c b/parser/parser.c
index 66a0ba9..06f03b6 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -610,7 +610,7 @@ static int parser(parsertype p, void *cfg, struct swupdate_cfg *swcfg)
if (swcfg->embscript) {
TRACE("Found Lua Software:\n%s\n", swcfg->embscript);
- L = lua_parser_init(swcfg->embscript);
+ L = lua_parser_init(swcfg->embscript, &swcfg->bootloader);
if (!L) {
ERROR("Required embedded script that cannot be loaded");
return -1;
--
2.7.4