1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/*
* (C) Copyright 2013-2023
* Stefano Babic <stefano.babic@swupdate.org>
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "generated/autoconf.h"
#include "swupdate.h"
#include "parsers.h"
#include "hw-compatibility.h"
#ifdef CONFIG_LUAEXTERNAL
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "util.h"
#include "lua_util.h"
#ifndef CONFIG_SETEXTPARSERNAME
#define LUA_PARSER "lua-tools/extparser.lua"
#else
#define LUA_PARSER (CONFIG_EXTPARSERNAME)
#endif
static void sw_append_stream(struct img_type *img, const char *key,
const char *value)
{
const char offset[] = "offset";
char seek_str[MAX_SEEK_STRING_SIZE];
if (!strcmp(key, "type"))
strlcpy(img->type, value,
sizeof(img->type));
if (!strcmp(key, "filename")) {
strlcpy(img->fname, value,
sizeof(img->fname));
img->skip = SKIP_NONE;
}
if (!strcmp(key, "name")) {
strlcpy(img->id.name, value,
sizeof(img->id.name));
}
if (!strcmp(key, "version")) {
strlcpy(img->id.version, value,
sizeof(img->id.version));
}
if (!strcmp(key, "mtdname") || !strcmp(key, "dest"))
strlcpy(img->mtdname, value,
sizeof(img->mtdname));
if (!strcmp(key, "filesystem"))
strlcpy(img->filesystem, value,
sizeof(img->filesystem));
if (!strcmp(key, "volume"))
strlcpy(img->volname, value,
sizeof(img->volname));
if (!strcmp(key, "device_id"))
strlcpy(img->device, value,
sizeof(img->device));
if (!strcmp(key, "device"))
strlcpy(img->device, value,
sizeof(img->device));
if (!strncmp(key, offset, sizeof(offset))) {
strlcpy(seek_str, value,
sizeof(seek_str));
/* convert the offset handling multiplicative suffixes */
img->seek = ustrtoull(seek_str, NULL, 0);
if (errno){
ERROR("offset argument: ustrtoull failed");
}
}
if (!strcmp(key, "script"))
img->is_script = 1;
if (!strcmp(key, "path"))
strlcpy(img->path, value,
sizeof(img->path));
if (!strcmp(key, "sha256"))
ascii_to_hash(img->sha256, value);
if (!strcmp(key, "encrypted"))
img->is_encrypted = true;
if (!strcmp(key, "compressed")) {
if (value != NULL) {
if (!strcmp(value, "zlib")) {
img->compressed = COMPRESSED_ZLIB;
} else if (!strcmp(value, "zstd")) {
img->compressed = COMPRESSED_ZSTD;
} else {
img->compressed = COMPRESSED_TRUE;
}
} else {
img->compressed = COMPRESSED_TRUE;
}
}
if (!strcmp(key, "installed-directly"))
img->install_directly = 1;
if (!strcmp(key, "install-if-different"))
img->id.install_if_different = 1;
if (!strcmp(key, "install-if-higher"))
img->id.install_if_higher = 1;
}
int parse_external(struct swupdate_cfg *software, const char *filename,
char __attribute__((__unused__)) **error)
{
int ret;
unsigned int nstreams;
struct img_type *image;
struct hw_type hardware = {0};
lua_State *L = luaL_newstate(); /* opens Lua */
luaL_openlibs(L); /* opens the standard libraries */
if (luaL_loadfile(L, LUA_PARSER)) {
ERROR("ERROR loading %s", LUA_PARSER);
lua_close(L);
return 1;
}
ret = lua_pcall(L, 0, 0, 0);
if (ret) {
LUAstackDump(L);
ERROR("ERROR preparing Parser in Lua %d", ret);
return 1;
}
if (-1 == get_hw_revision(&hardware))
{
ERROR("ERROR getting hw revision");
return 1;
}
lua_getglobal(L, "xmlparser");
/* passing arguments */
lua_pushstring(L, filename);
lua_pushstring(L, hardware.boardname);
lua_pushstring(L, hardware.revision);
if (lua_pcall(L, 3, 4, 0)) {
LUAstackDump(L);
ERROR("ERROR Calling XML Parser in Lua");
lua_close(L);
return 1;
}
if (lua_type(L, 1) == LUA_TSTRING)
strlcpy(software->name, lua_tostring(L, 1),
sizeof(software->name));
if (lua_type(L, 2) == LUA_TSTRING)
strlcpy(software->version, lua_tostring(L, 2),
sizeof(software->version));
nstreams = 0;
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
printf("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
if (lua_type(L, -1) == LUA_TTABLE) {
lua_pushnil(L);
image = (struct img_type *)calloc(1, sizeof(struct img_type));
if (!image) {
ERROR( "No memory: malloc failed");
return -ENOMEM;
}
while (lua_next(L, -2) != 0) {
sw_append_stream(image, lua_tostring(L, -2),
lua_tostring(L, -1));
lua_pop(L, 1);
}
if (image->is_script)
LIST_INSERT_HEAD(&software->scripts, image, next);
else
LIST_INSERT_HEAD(&software->images, image, next);
nstreams++;
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
LUAstackDump(L);
lua_close(L);
TRACE("Software: %s %s", software->name, software->version);
LIST_FOREACH(image, &software->images, next) {
TRACE("\tName: %s Type: %s", image->fname,
image->type);
}
return !(nstreams > 0);
}
#else
int parse_external(struct swupdate_cfg __attribute__((__unused__)) *software,
const char __attribute__((__unused__)) *filename,
char __attribute__((__unused__)) **error)
{
return -1;
}
#endif
|