From f88f369fa637ee188b4abfe6c948c8ab313341a4 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Mon, 30 Apr 2007 16:35:22 +0200 Subject: [PATCH] added cfgpatch --- .gitignore | 1 + Makefile | 11 ++++--- cfgpatch.c | 29 ++++++++++++++++++ configdata.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ configdata.h | 16 ++++++++++ decompress.c | 2 +- 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 cfgpatch.c create mode 100644 configdata.c create mode 100644 configdata.h diff --git a/.gitignore b/.gitignore index fcd92a3..e0b6e2b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ zyxel-revert compress decompress +cfgpatch *.log *.rom diff --git a/Makefile b/Makefile index 5829e17..8ce1b6f 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,17 @@ CFLAGS := -O2 -pipe -Wall -all: zyxel-revert compress decompress +all: zyxel-revert compress decompress cfgpatch zyxel-revert: configfile.o event.o filedata.o logging.o context.o serial.o statemachine.o xmodem.o zyxel-revert.o $(CC) $(CFLAGS) $^ -o $@ -compress: lzsc.o filedata.o romfile.o compress.o +compress: filedata.o lzsc.o romfile.o compress.o $(CC) $(CFLAGS) $^ -o $@ -decompress: lzsd.o filedata.o romfile.o decompress.o +decompress: filedata.o lzsd.o romfile.o decompress.o + $(CC) $(CFLAGS) $^ -o $@ + +cfgpatch: configdata.o filedata.o cfgpatch.o $(CC) $(CFLAGS) $^ -o $@ %.o: %.c @@ -18,7 +21,7 @@ decompress: lzsd.o filedata.o romfile.o decompress.o $(CC) $(CFLAGS) -MM -c $< -o $@ clean: - rm -f zyxel-revert compress decompress *.d *.o *.log + rm -f zyxel-revert compress decompress cfgpatch *.d *.o *.log DEPS := $(wildcard *.c) -include $(DEPS:.c=.d) diff --git a/cfgpatch.c b/cfgpatch.c new file mode 100644 index 0000000..69d116f --- /dev/null +++ b/cfgpatch.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +#include "configdata.h" +#include "filedata.h" + +/* + * $ cfgpatch + */ +int main(int argc, char *argv[]) +{ + struct filedata *config = get_filedata(argv[1]); + + config_patch(config->data, CFG_LOCATION, "location"); + config_patch(config->data, CFG_SYSNAME, "sysname"); + config_patch(config->data, CFG_CONTACT, "contact"); + + config_patch(config->data, CFG_IPADDRESS, "10.10.200.10"); + config_patch(config->data, CFG_NETMASK, "16"); + config_patch(config->data, CFG_DEFAULTGW, "10.10.250.250"); + config_patch(config->data, CFG_NAMESERVER, "10.10.0.1"); + + put_filedata(argv[2], config); + + free(config); + return 0; +} diff --git a/configdata.c b/configdata.c new file mode 100644 index 0000000..b948b83 --- /dev/null +++ b/configdata.c @@ -0,0 +1,86 @@ +#include +#include + +#include +#include +#include + +#include "configdata.h" + +struct cfg_patch { + int code; + int mask; + int (* patch)(void *config, struct cfg_patch *patch, int code, const char *parameter); + int offset; + int size; + int min; + int max; +}; + +static int patch_8bit(void *config, struct cfg_patch *patch, int code, const char *parameter) +{ + int value = atoi(parameter); + if (value < patch->min || value > patch->max) + return -1; + + *((uint8_t *)(config + patch->offset)) = value; + return 0; +} + +static int patch_string(void *config, struct cfg_patch *patch, int code, const char *parameter) +{ + strncpy(config + patch->offset, parameter, patch->size); + return 0; +} + +static int patch_ip(void *config, struct cfg_patch *patch, int code, const char *parameter) +{ + return (inet_pton(AF_INET, parameter, config + patch->offset) <= 0) ? -1 : 0; +} + +static struct cfg_patch patcharr[] = {{ + .code = CFG_LOCATION, + .patch = patch_string, + .offset = 0x0034, + .size = 0x1d, +}, { + .code = CFG_SYSNAME, + .patch = patch_string, + .offset = 0x0054, + .size = 0x1d, +}, { + .code = CFG_CONTACT, + .patch = patch_string, + .offset = 0x0074, + .size = 0x1d, +}, { + .code = CFG_DEFAULTGW, + .patch = patch_ip, + .offset = 0x10fc, +}, { + .code = CFG_IPADDRESS, + .patch = patch_ip, + .offset = 0x2df8, +}, { + .code = CFG_NETMASK, + .patch = patch_8bit, + .offset = 0x2dfe, + .min = 0x00, + .max = 0x20, +}, { + .code = CFG_NAMESERVER, + .patch = patch_ip, + .offset = 0x32dc, +}}; + +int config_patch(void *config, int code, const char *parameter) +{ + int i; + for (i = 0; i < (sizeof(patcharr) / sizeof(struct cfg_patch)); i++) { + int mask = (patcharr[i].mask != 0) ? patcharr[i].mask : ~0; + + if ((patcharr[i].code & mask) == code) + return patcharr[i].patch(config, &patcharr[i], code, parameter); + } + return -1; +} diff --git a/configdata.h b/configdata.h new file mode 100644 index 0000000..256fcbe --- /dev/null +++ b/configdata.h @@ -0,0 +1,16 @@ +#ifndef _CONFIGDATA_H_ +#define _CONFIGDATA_H_ + +enum { + CFG_LOCATION = 0x0001, + CFG_SYSNAME, + CFG_CONTACT, + CFG_DEFAULTGW, + CFG_IPADDRESS, + CFG_NETMASK, + CFG_NAMESERVER, +}; + +int config_patch(void *config, int code, const char *parameter); + +#endif /* _CONFIGDATA_H_ */ diff --git a/decompress.c b/decompress.c index 4e71bd3..5861175 100644 --- a/decompress.c +++ b/decompress.c @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) char outname[64]; strncpy(outname, argv[1], sizeof(outname)); - strcat(outname, ".own_decomp"); + strcat(outname, ".decomp"); put_filedata(outname, config);