0031d83dd08ded11f03f42f52db6249450bfa2fe
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 sysupgrade.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;
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
));
86 c
= getopt(ac
, av
, "pxm:r:v:s:i:o:");
98 if (strlen(optarg
) > MAX_MODEL_NAME_LEN
) {
99 fprintf(stderr
, "%s: model name exceeds %d chars\n",
100 progname
, MAX_MODEL_NAME_LEN
);
103 strcpy(model
, optarg
);
106 if (strlen(optarg
) > MAX_REGION_LEN
) {
107 fprintf(stderr
, "%s: region exceeds %d chars\n",
108 progname
, MAX_REGION_LEN
);
111 have_regionversion
= 1;
112 strcpy(region
, optarg
);
115 if (strlen(optarg
) > MAX_VERSION_LEN
) {
116 fprintf(stderr
, "%s: version exceeds %d chars\n",
117 progname
, MAX_VERSION_LEN
);
120 have_regionversion
= 1;
121 strcpy(version
, optarg
);
124 if (strlen(optarg
) > MAX_SIG_LEN
) {
125 fprintf(stderr
, "%s: signature exceeds %d chars\n",
126 progname
, MAX_SIG_LEN
);
129 strcpy(signature
, optarg
);
132 if ((ifile
= fopen(optarg
, "r")) == NULL
)
136 if ((ofile
= fopen(optarg
, "w")) == NULL
)
144 if (signature
[0] == 0 || ifile
== NULL
|| ofile
== NULL
) {
149 char *p
= strchr(signature
, '-');
151 fprintf(stderr
, "%s: model name unknown\n", progname
);
154 if (p
- signature
> MAX_MODEL_NAME_LEN
) {
156 fprintf(stderr
, "%s: auto model name failed, string %s too long\n", progname
, signature
);
159 strncpy(model
, signature
, p
- signature
);
163 if (fread(&imghdr
, sizeof(imghdr
), 1, ifile
) < 0)
164 perrexit(2, "fread on input");
167 for (bcnt
= 0, cksum
= 0 ; (c
= fgetc(ifile
)) != EOF
; bcnt
++)
170 if (fseek(ifile
, patchmode
? sizeof(imghdr
) : 0, SEEK_SET
) < 0)
171 perrexit(2, "fseek on input");
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
;
183 imghdr
.flash_byte_cnt
= htonl(bcnt
);
185 if (ntohl(imghdr
.checksum
) != cksum
) {
186 fprintf(stderr
, "%s: patch mode, checksum mismatch\n",
189 fprintf(stderr
, "%s: fixing\n", progname
);
190 imghdr
.checksum
= htonl(cksum
);
193 } else if (ntohl(imghdr
.flash_byte_cnt
) != bcnt
) {
194 fprintf(stderr
, "%s: patch mode, size mismatch\n",
197 fprintf(stderr
, "%s: fixing\n", progname
);
198 imghdr
.flash_byte_cnt
= htonl(bcnt
);
204 strncpy(imghdr
.model
, model
, MAX_MODEL_NAME_LEN
);
205 strncpy(imghdr
.sig
, signature
, MAX_SIG_LEN
);
207 if (fwrite(&imghdr
, sizeof(imghdr
), 1, ofile
) < 0)
208 perrexit(2, "fwrite header on output");
209 if (have_regionversion
) {
210 if (fwrite(®ion
, 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");
216 while ((c
= fgetc(ifile
)) != EOF
) {
217 if (fputc(c
, ofile
) == EOF
)
218 perrexit(2, "fputc on output");
222 perrexit(2, "fgetc on input");