11 #include <netinet/in.h> // htonl
13 // Usage: mkdapimg [-p] [-m <model>] -s <sig> -i <input> -o <output>
15 // e.g.: mkdapimg -s RT3052-AP-DAP1350-3 -i sysupgarde.bin -o factory.bin
17 // If the model string <model> is not given, we will assume that
18 // the leading characters upto the first "-" is the model.
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.
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
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
33 struct img_hdr_struct
{
35 char model
[MAX_MODEL_NAME_LEN
];
36 char sig
[MAX_SIG_LEN
];
41 uint32_t flash_byte_cnt
;
47 perrexit(int code
, char *msg
)
49 fprintf(stderr
, "%s: %s: %s\n", progname
, msg
, strerror(errno
));
56 fprintf(stderr
, "usage: %s [-p] [-m model] [-r region] [-v version] -s signature -i input -o output\n", progname
);
61 main(int ac
, char *av
[])
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];
69 int have_regionversion
= 0;
76 progname
= basename(av
[0]);
77 memset(model
, 0, sizeof(model
));
78 memset(signature
, 0, sizeof(signature
));
79 memset(region
, 0, sizeof(region
));
80 memset(version
, 0, sizeof(version
));
85 c
= getopt(ac
, av
, "pxm:r:v:s:i:o:");
97 if (strlen(optarg
) > MAX_MODEL_NAME_LEN
) {
98 fprintf(stderr
, "%s: model name exceeds %d chars\n",
99 progname
, MAX_MODEL_NAME_LEN
);
102 strcpy(model
, optarg
);
105 if (strlen(optarg
) > MAX_REGION_LEN
) {
106 fprintf(stderr
, "%s: region exceeds %d chars\n",
107 progname
, MAX_REGION_LEN
);
110 have_regionversion
= 1;
111 strcpy(region
, optarg
);
114 if (strlen(optarg
) > MAX_VERSION_LEN
) {
115 fprintf(stderr
, "%s: version exceeds %d chars\n",
116 progname
, MAX_VERSION_LEN
);
119 have_regionversion
= 1;
120 strcpy(version
, optarg
);
123 if (strlen(optarg
) > MAX_SIG_LEN
) {
124 fprintf(stderr
, "%s: signature exceeds %d chars\n",
125 progname
, MAX_SIG_LEN
);
128 strcpy(signature
, optarg
);
131 if ((ifile
= fopen(optarg
, "r")) == NULL
)
135 if ((ofile
= fopen(optarg
, "w")) == NULL
)
143 if (signature
[0] == 0 || ifile
== NULL
|| ofile
== NULL
) {
148 char *p
= strchr(signature
, '-');
150 fprintf(stderr
, "%s: model name unknown\n", progname
);
153 if (p
- signature
> MAX_MODEL_NAME_LEN
) {
155 fprintf(stderr
, "%s: auto model name failed, string %s too long\n", progname
, signature
);
158 strncpy(model
, signature
, p
- signature
);
162 if (fread(&imghdr
, sizeof(imghdr
), 1, ifile
) < 0)
163 perrexit(2, "fread on input");
166 for (bcnt
= 0, cksum
= 0 ; (c
= fgetc(ifile
)) != EOF
; bcnt
++)
169 if (fseek(ifile
, patchmode
? sizeof(imghdr
) : 0, SEEK_SET
) < 0)
170 perrexit(2, "fseek on input");
172 if (patchmode
== 0) {
173 // Fill in the header
174 memset(&imghdr
, 0, sizeof(imghdr
));
175 imghdr
.checksum
= htonl(cksum
);
176 imghdr
.partition
= 0 ; // don't care?
177 imghdr
.hdr_len
= sizeof(imghdr
);
178 if (have_regionversion
) {
179 imghdr
.hdr_len
+= MAX_REGION_LEN
;
180 imghdr
.hdr_len
+= MAX_VERSION_LEN
;
182 imghdr
.flash_byte_cnt
= htonl(bcnt
);
184 if (ntohl(imghdr
.checksum
) != cksum
) {
185 fprintf(stderr
, "%s: patch mode, checksum mismatch\n",
188 fprintf(stderr
, "%s: fixing\n", progname
);
189 imghdr
.checksum
= htonl(cksum
);
192 } else if (ntohl(imghdr
.flash_byte_cnt
) != bcnt
) {
193 fprintf(stderr
, "%s: patch mode, size mismatch\n",
196 fprintf(stderr
, "%s: fixing\n", progname
);
197 imghdr
.flash_byte_cnt
= htonl(bcnt
);
203 strncpy(imghdr
.model
, model
, MAX_MODEL_NAME_LEN
);
204 strncpy(imghdr
.sig
, signature
, MAX_SIG_LEN
);
206 if (fwrite(&imghdr
, sizeof(imghdr
), 1, ofile
) < 0)
207 perrexit(2, "fwrite header on output");
208 if (have_regionversion
) {
209 if (fwrite(®ion
, MAX_REGION_LEN
, 1, ofile
) < 0)
210 perrexit(2, "fwrite header on output");
211 if (fwrite(&version
, MAX_VERSION_LEN
, 1, ofile
) < 0)
212 perrexit(2, "fwrite header on output");
215 while ((c
= fgetc(ifile
)) != EOF
) {
216 if (fputc(c
, ofile
) == EOF
)
217 perrexit(2, "fputc on output");
221 perrexit(2, "fgetc on input");