added cfgpatch

This commit is contained in:
Olaf Rempel 2007-04-30 16:35:22 +02:00
parent 956e34db7b
commit f88f369fa6
6 changed files with 140 additions and 5 deletions

1
.gitignore vendored
View File

@ -3,5 +3,6 @@
zyxel-revert zyxel-revert
compress compress
decompress decompress
cfgpatch
*.log *.log
*.rom *.rom

View File

@ -1,14 +1,17 @@
CFLAGS := -O2 -pipe -Wall 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 zyxel-revert: configfile.o event.o filedata.o logging.o context.o serial.o statemachine.o xmodem.o zyxel-revert.o
$(CC) $(CFLAGS) $^ -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 $@ $(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 $@ $(CC) $(CFLAGS) $^ -o $@
%.o: %.c %.o: %.c
@ -18,7 +21,7 @@ decompress: lzsd.o filedata.o romfile.o decompress.o
$(CC) $(CFLAGS) -MM -c $< -o $@ $(CC) $(CFLAGS) -MM -c $< -o $@
clean: clean:
rm -f zyxel-revert compress decompress *.d *.o *.log rm -f zyxel-revert compress decompress cfgpatch *.d *.o *.log
DEPS := $(wildcard *.c) DEPS := $(wildcard *.c)
-include $(DEPS:.c=.d) -include $(DEPS:.c=.d)

29
cfgpatch.c Normal file
View File

@ -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;
}

86
configdata.c Normal file
View File

@ -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;
}

16
configdata.h Normal file
View File

@ -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_ */

View File

@ -18,7 +18,7 @@ int main(int argc, char *argv[])
char outname[64]; char outname[64];
strncpy(outname, argv[1], sizeof(outname)); strncpy(outname, argv[1], sizeof(outname));
strcat(outname, ".own_decomp"); strcat(outname, ".decomp");
put_filedata(outname, config); put_filedata(outname, config);