Browse Source

added cfgpatch

Olaf Rempel 11 years ago
parent
commit
f88f369fa6
6 changed files with 140 additions and 5 deletions
  1. 1
    0
      .gitignore
  2. 7
    4
      Makefile
  3. 29
    0
      cfgpatch.c
  4. 86
    0
      configdata.c
  5. 16
    0
      configdata.h
  6. 1
    1
      decompress.c

+ 1
- 0
.gitignore View File

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

+ 7
- 4
Makefile View File

@@ -1,14 +1,17 @@
1 1
 CFLAGS := -O2 -pipe -Wall
2 2
 
3
-all: zyxel-revert compress decompress
3
+all: zyxel-revert compress decompress cfgpatch
4 4
 
5 5
 zyxel-revert: configfile.o event.o filedata.o logging.o context.o serial.o statemachine.o xmodem.o zyxel-revert.o
6 6
 	$(CC) $(CFLAGS) $^ -o $@
7 7
 
8
-compress: lzsc.o filedata.o romfile.o compress.o
8
+compress: filedata.o lzsc.o romfile.o compress.o
9 9
 	$(CC) $(CFLAGS) $^ -o $@
10 10
 
11
-decompress: lzsd.o filedata.o romfile.o decompress.o
11
+decompress: filedata.o lzsd.o romfile.o decompress.o
12
+	$(CC) $(CFLAGS) $^ -o $@
13
+
14
+cfgpatch: configdata.o filedata.o cfgpatch.o
12 15
 	$(CC) $(CFLAGS) $^ -o $@
13 16
 
14 17
 %.o: %.c
@@ -18,7 +21,7 @@ decompress: lzsd.o filedata.o romfile.o decompress.o
18 21
 	$(CC) $(CFLAGS) -MM -c $< -o $@
19 22
 
20 23
 clean:
21
-	rm -f zyxel-revert compress decompress *.d *.o *.log
24
+	rm -f zyxel-revert compress decompress cfgpatch *.d *.o *.log
22 25
 
23 26
 DEPS := $(wildcard *.c)
24 27
 -include $(DEPS:.c=.d)

+ 29
- 0
cfgpatch.c View File

@@ -0,0 +1,29 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+#include <unistd.h>
4
+#include <string.h>
5
+
6
+#include "configdata.h"
7
+#include "filedata.h"
8
+
9
+/*
10
+ * $ cfgpatch <config-in> <config-out>
11
+ */
12
+int main(int argc, char *argv[])
13
+{
14
+	struct filedata *config = get_filedata(argv[1]);
15
+
16
+	config_patch(config->data, CFG_LOCATION, "location");
17
+	config_patch(config->data, CFG_SYSNAME, "sysname");
18
+	config_patch(config->data, CFG_CONTACT, "contact");
19
+
20
+	config_patch(config->data, CFG_IPADDRESS, "10.10.200.10");
21
+	config_patch(config->data, CFG_NETMASK, "16");
22
+	config_patch(config->data, CFG_DEFAULTGW, "10.10.250.250");
23
+	config_patch(config->data, CFG_NAMESERVER, "10.10.0.1");
24
+
25
+	put_filedata(argv[2], config);
26
+
27
+	free(config);
28
+	return 0;
29
+}

+ 86
- 0
configdata.c View File

@@ -0,0 +1,86 @@
1
+#include <stdlib.h>
2
+#include <string.h>
3
+
4
+#include <sys/types.h>
5
+#include <sys/socket.h>
6
+#include <arpa/inet.h>
7
+
8
+#include "configdata.h"
9
+
10
+struct cfg_patch {
11
+	int code;
12
+	int mask;
13
+	int (* patch)(void *config, struct cfg_patch *patch, int code, const char *parameter);
14
+	int offset;
15
+	int size;
16
+	int min;
17
+	int max;
18
+};
19
+
20
+static int patch_8bit(void *config, struct cfg_patch *patch, int code, const char *parameter)
21
+{
22
+	int value = atoi(parameter);
23
+	if (value < patch->min || value > patch->max)
24
+		return -1;
25
+
26
+	*((uint8_t *)(config + patch->offset)) = value;
27
+	return 0;
28
+}
29
+
30
+static int patch_string(void *config, struct cfg_patch *patch, int code, const char *parameter)
31
+{
32
+	strncpy(config + patch->offset, parameter, patch->size);
33
+	return 0;
34
+}
35
+
36
+static int patch_ip(void *config, struct cfg_patch *patch, int code, const char *parameter)
37
+{
38
+	return (inet_pton(AF_INET, parameter, config + patch->offset) <= 0) ? -1 : 0;
39
+}
40
+
41
+static struct cfg_patch patcharr[] = {{
42
+	.code = CFG_LOCATION,
43
+	.patch = patch_string,
44
+	.offset = 0x0034,
45
+	.size = 0x1d,
46
+}, {
47
+	.code = CFG_SYSNAME,
48
+	.patch = patch_string,
49
+	.offset = 0x0054,
50
+	.size = 0x1d,
51
+}, {
52
+	.code = CFG_CONTACT,
53
+	.patch = patch_string,
54
+	.offset = 0x0074,
55
+	.size = 0x1d,
56
+}, {
57
+	.code = CFG_DEFAULTGW,
58
+	.patch = patch_ip,
59
+	.offset = 0x10fc,
60
+}, {
61
+	.code = CFG_IPADDRESS,
62
+	.patch = patch_ip,
63
+	.offset = 0x2df8,
64
+}, {
65
+	.code = CFG_NETMASK,
66
+	.patch = patch_8bit,
67
+	.offset = 0x2dfe,
68
+	.min = 0x00,
69
+	.max = 0x20,
70
+}, {
71
+	.code = CFG_NAMESERVER,
72
+	.patch = patch_ip,
73
+	.offset = 0x32dc,
74
+}};
75
+
76
+int config_patch(void *config, int code, const char *parameter)
77
+{
78
+	int i;
79
+	for (i = 0; i < (sizeof(patcharr) / sizeof(struct cfg_patch)); i++) {
80
+		int mask = (patcharr[i].mask != 0) ? patcharr[i].mask : ~0;
81
+
82
+		if ((patcharr[i].code & mask) == code)
83
+			return patcharr[i].patch(config, &patcharr[i], code, parameter);
84
+	}
85
+	return -1;
86
+}

+ 16
- 0
configdata.h View File

@@ -0,0 +1,16 @@
1
+#ifndef _CONFIGDATA_H_
2
+#define _CONFIGDATA_H_
3
+
4
+enum {
5
+	CFG_LOCATION = 0x0001,
6
+	CFG_SYSNAME,
7
+	CFG_CONTACT,
8
+	CFG_DEFAULTGW,
9
+	CFG_IPADDRESS,
10
+	CFG_NETMASK,
11
+	CFG_NAMESERVER,
12
+};
13
+
14
+int config_patch(void *config, int code, const char *parameter);
15
+
16
+#endif /* _CONFIGDATA_H_ */

+ 1
- 1
decompress.c View File

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

Loading…
Cancel
Save