@@ -3,5 +3,6 @@ | |||
zyxel-revert | |||
compress | |||
decompress | |||
cfgpatch | |||
*.log | |||
*.rom |
@@ -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) |
@@ -0,0 +1,29 @@ | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <unistd.h> | |||
#include <string.h> | |||
#include "configdata.h" | |||
#include "filedata.h" | |||
/* | |||
* $ cfgpatch <config-in> <config-out> | |||
*/ | |||
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; | |||
} |
@@ -0,0 +1,86 @@ | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/types.h> | |||
#include <sys/socket.h> | |||
#include <arpa/inet.h> | |||
#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; | |||
} |
@@ -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_ */ |
@@ -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); | |||