0031d83dd08ded11f03f42f52db6249450bfa2fe
[openwrt/staging/hauke.git] / tools / firmware-utils / src / mkdapimg.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <libgen.h>
6 #include <stdarg.h>
7 #include <getopt.h>
8 #include <string.h>
9 #include <errno.h>
10
11 #include <netinet/in.h> // htonl
12
13 // Usage: mkdapimg [-p] [-m <model>] -s <sig> -i <input> -o <output>
14 //
15 // e.g.: mkdapimg -s RT3052-AP-DAP1350-3 -i sysupgrade.bin -o factory.bin
16 //
17 // If the model string <model> is not given, we will assume that
18 // the leading characters upto the first "-" is the model.
19 //
20 // The "-p" (patch) option is used to patch the exisiting image with the
21 // specified model and signature.
22 // The "-x" (fix) option will recalculate the payload size and checksum
23 // during the patch mode operation.
24
25 // The img_hdr_struct was taken from the D-Link SDK:
26 // DAP-1350_A1_FW1.11NA_GPL/GPL_Source_Code/Uboot/DAP-1350/httpd/header.h
27
28 #define MAX_MODEL_NAME_LEN 20
29 #define MAX_SIG_LEN 30
30 #define MAX_REGION_LEN 4
31 #define MAX_VERSION_LEN 12
32
33 struct img_hdr_struct {
34 uint32_t checksum;
35 char model[MAX_MODEL_NAME_LEN];
36 char sig[MAX_SIG_LEN];
37 uint8_t partition;
38 uint8_t hdr_len;
39 uint8_t rsv1;
40 uint8_t rsv2;
41 uint32_t flash_byte_cnt;
42 } imghdr ;
43
44 char *progname;
45
46 void
47 perrexit(int code, char *msg)
48 {
49 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(errno));
50 exit(code);
51 }
52
53 void
54 usage()
55 {
56 fprintf(stderr, "usage: %s [-p] [-m model] [-r region] [-v version] -s signature -i input -o output\n", progname);
57 exit(1);
58 }
59
60 int
61 main(int ac, char *av[])
62 {
63 char model[MAX_MODEL_NAME_LEN+1];
64 char signature[MAX_SIG_LEN+1];
65 char region[MAX_REGION_LEN+1];
66 char version[MAX_VERSION_LEN+1];
67 int patchmode = 0;
68 int fixmode = 0;
69 int have_regionversion = 0;
70
71 FILE *ifile = NULL;
72 FILE *ofile = NULL;
73 int c;
74 uint32_t cksum;
75 uint32_t bcnt;
76
77 progname = basename(av[0]);
78 memset(model, 0, sizeof(model));
79 memset(signature, 0, sizeof(signature));
80 memset(region, 0, sizeof(region));
81 memset(version, 0, sizeof(version));
82
83 while ( 1 ) {
84 int c;
85
86 c = getopt(ac, av, "pxm:r:v:s:i:o:");
87 if (c == -1)
88 break;
89
90 switch (c) {
91 case 'p':
92 patchmode = 1;
93 break;
94 case 'x':
95 fixmode = 1;
96 break;
97 case 'm':
98 if (strlen(optarg) > MAX_MODEL_NAME_LEN) {
99 fprintf(stderr, "%s: model name exceeds %d chars\n",
100 progname, MAX_MODEL_NAME_LEN);
101 exit(1);
102 }
103 strcpy(model, optarg);
104 break;
105 case 'r':
106 if (strlen(optarg) > MAX_REGION_LEN) {
107 fprintf(stderr, "%s: region exceeds %d chars\n",
108 progname, MAX_REGION_LEN);
109 exit(1);
110 }
111 have_regionversion = 1;
112 strcpy(region, optarg);
113 break;
114 case 'v':
115 if (strlen(optarg) > MAX_VERSION_LEN) {
116 fprintf(stderr, "%s: version exceeds %d chars\n",
117 progname, MAX_VERSION_LEN);
118 exit(1);
119 }
120 have_regionversion = 1;
121 strcpy(version, optarg);
122 break;
123 case 's':
124 if (strlen(optarg) > MAX_SIG_LEN) {
125 fprintf(stderr, "%s: signature exceeds %d chars\n",
126 progname, MAX_SIG_LEN);
127 exit(1);
128 }
129 strcpy(signature, optarg);
130 break;
131 case 'i':
132 if ((ifile = fopen(optarg, "r")) == NULL)
133 perrexit(1, optarg);
134 break;
135 case 'o':
136 if ((ofile = fopen(optarg, "w")) == NULL)
137 perrexit(1, optarg);
138 break;
139 default:
140 usage();
141 }
142 }
143
144 if (signature[0] == 0 || ifile == NULL || ofile == NULL) {
145 usage();
146 }
147
148 if (model[0] == 0) {
149 char *p = strchr(signature, '-');
150 if (p == NULL) {
151 fprintf(stderr, "%s: model name unknown\n", progname);
152 exit(1);
153 }
154 if (p - signature > MAX_MODEL_NAME_LEN) {
155 *p = 0;
156 fprintf(stderr, "%s: auto model name failed, string %s too long\n", progname, signature);
157 exit(1);
158 }
159 strncpy(model, signature, p - signature);
160 }
161
162 if (patchmode) {
163 if (fread(&imghdr, sizeof(imghdr), 1, ifile) < 0)
164 perrexit(2, "fread on input");
165 }
166
167 for (bcnt = 0, cksum = 0 ; (c = fgetc(ifile)) != EOF ; bcnt++)
168 cksum += c & 0xff;
169
170 if (fseek(ifile, patchmode ? sizeof(imghdr) : 0, SEEK_SET) < 0)
171 perrexit(2, "fseek on input");
172
173 if (patchmode == 0) {
174 // Fill in the header
175 memset(&imghdr, 0, sizeof(imghdr));
176 imghdr.checksum = htonl(cksum);
177 imghdr.partition = 0 ; // don't care?
178 imghdr.hdr_len = sizeof(imghdr);
179 if (have_regionversion) {
180 imghdr.hdr_len += MAX_REGION_LEN;
181 imghdr.hdr_len += MAX_VERSION_LEN;
182 }
183 imghdr.flash_byte_cnt = htonl(bcnt);
184 } else {
185 if (ntohl(imghdr.checksum) != cksum) {
186 fprintf(stderr, "%s: patch mode, checksum mismatch\n",
187 progname);
188 if (fixmode) {
189 fprintf(stderr, "%s: fixing\n", progname);
190 imghdr.checksum = htonl(cksum);
191 } else
192 exit(3);
193 } else if (ntohl(imghdr.flash_byte_cnt) != bcnt) {
194 fprintf(stderr, "%s: patch mode, size mismatch\n",
195 progname);
196 if (fixmode) {
197 fprintf(stderr, "%s: fixing\n", progname);
198 imghdr.flash_byte_cnt = htonl(bcnt);
199 } else
200 exit(3);
201 }
202 }
203
204 strncpy(imghdr.model, model, MAX_MODEL_NAME_LEN);
205 strncpy(imghdr.sig, signature, MAX_SIG_LEN);
206
207 if (fwrite(&imghdr, sizeof(imghdr), 1, ofile) < 0)
208 perrexit(2, "fwrite header on output");
209 if (have_regionversion) {
210 if (fwrite(&region, MAX_REGION_LEN, 1, ofile) < 0)
211 perrexit(2, "fwrite header on output");
212 if (fwrite(&version, MAX_VERSION_LEN, 1, ofile) < 0)
213 perrexit(2, "fwrite header on output");
214 }
215
216 while ((c = fgetc(ifile)) != EOF) {
217 if (fputc(c, ofile) == EOF)
218 perrexit(2, "fputc on output");
219 }
220
221 if (ferror(ifile))
222 perrexit(2, "fgetc on input");
223
224
225 fclose(ofile);
226 fclose(ifile);
227 }