firmware-utils: add hex pattern mode for xorimage
[openwrt/staging/wigyori.git] / tools / firmware-utils / src / xorimage.c
1 /*
2 * xorimage.c - partially based on OpenWrt's addpattern.c
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27
28 static char default_pattern[] = "12345678";
29 static int is_hex_pattern;
30
31
32 int xor_data(uint8_t *data, size_t len, const uint8_t *pattern, int p_len, int p_off)
33 {
34 int offset = p_off;
35 while (len--) {
36 *data ^= pattern[offset];
37 data++;
38 offset = (offset + 1) % p_len;
39 }
40 return offset;
41 }
42
43
44 void usage(void) __attribute__ (( __noreturn__ ));
45
46 void usage(void)
47 {
48 fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>] [-x]\n");
49 exit(EXIT_FAILURE);
50 }
51
52
53 int main(int argc, char **argv)
54 {
55 char buf[1024]; /* keep this at 1k or adjust garbage calc below */
56 FILE *in = stdin;
57 FILE *out = stdout;
58 char *ifn = NULL;
59 char *ofn = NULL;
60 const char *pattern = default_pattern;
61 char hex_pattern[128];
62 unsigned int hex_buf;
63 int c;
64 int v0, v1, v2;
65 size_t n;
66 int p_len, p_off = 0;
67
68 while ((c = getopt(argc, argv, "i:o:p:xh")) != -1) {
69 switch (c) {
70 case 'i':
71 ifn = optarg;
72 break;
73 case 'o':
74 ofn = optarg;
75 break;
76 case 'p':
77 pattern = optarg;
78 break;
79 case 'x':
80 is_hex_pattern = true;
81 break;
82 case 'h':
83 default:
84 usage();
85 }
86 }
87
88 if (optind != argc || optind == 1) {
89 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
90 usage();
91 }
92
93 if (ifn && !(in = fopen(ifn, "r"))) {
94 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
95 usage();
96 }
97
98 if (ofn && !(out = fopen(ofn, "w"))) {
99 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
100 usage();
101 }
102
103 p_len = strlen(pattern);
104
105 if (p_len == 0) {
106 fprintf(stderr, "pattern cannot be empty\n");
107 usage();
108 }
109
110 if (is_hex_pattern) {
111 int i;
112
113 if ((p_len / 2) > sizeof(hex_pattern)) {
114 fprintf(stderr, "provided hex pattern is too long\n");
115 usage();
116 }
117
118 if (p_len % 2 != 0) {
119 fprintf(stderr, "the number of characters (hex) is incorrect\n");
120 usage();
121 }
122
123 for (i = 0; i < (p_len / 2); i++) {
124 if (sscanf(pattern + (i * 2), "%2x", &hex_buf) < 0) {
125 fprintf(stderr, "invalid hex digit around %d\n", i * 2);
126 usage();
127 }
128 hex_pattern[i] = (char)hex_buf;
129 }
130 }
131
132 while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
133 if (n < sizeof(buf)) {
134 if (ferror(in)) {
135 FREAD_ERROR:
136 fprintf(stderr, "fread error\n");
137 return EXIT_FAILURE;
138 }
139 }
140
141 if (is_hex_pattern) {
142 p_off = xor_data(buf, n, hex_pattern, (p_len / 2),
143 p_off);
144 } else {
145 p_off = xor_data(buf, n, pattern, p_len, p_off);
146 }
147
148 if (!fwrite(buf, n, 1, out)) {
149 FWRITE_ERROR:
150 fprintf(stderr, "fwrite error\n");
151 return EXIT_FAILURE;
152 }
153 }
154
155 if (ferror(in)) {
156 goto FREAD_ERROR;
157 }
158
159 if (fflush(out)) {
160 goto FWRITE_ERROR;
161 }
162
163 fclose(in);
164 fclose(out);
165
166 return EXIT_SUCCESS;
167 }