1 // SPDX-License-Identifier: GPL-2.0-only
12 #include <netinet/in.h> // htonl
14 // Usage: mkdapimg [-p] [-m <model>] -s <sig> -i <input> -o <output>
16 // e.g.: mkdapimg -s RT3052-AP-DAP1350-3 -i sysupgrade.bin -o factory.bin
18 // If the model string <model> is not given, we will assume that
19 // the leading characters upto the first "-" is the model.
21 // The "-p" (patch) option is used to patch the exisiting image with the
22 // specified model and signature.
23 // The "-x" (fix) option will recalculate the payload size and checksum
24 // during the patch mode operation.
26 // The img_hdr_struct was taken from the D-Link SDK:
27 // DAP-1350_A1_FW1.11NA_GPL/GPL_Source_Code/Uboot/DAP-1350/httpd/header.h
29 #define MAX_MODEL_NAME_LEN 20
30 #define MAX_SIG_LEN 30
31 #define MAX_REGION_LEN 4
32 #define MAX_VERSION_LEN 12
34 struct img_hdr_struct
{
36 char model
[MAX_MODEL_NAME_LEN
];
37 char sig
[MAX_SIG_LEN
];
42 uint32_t flash_byte_cnt
;
48 perrexit(int code
, char *msg
)
50 fprintf(stderr
, "%s: %s: %s\n", progname
, msg
, strerror(errno
));
57 fprintf(stderr
, "usage: %s [-p] [-m model] [-r region] [-v version] -s signature -i input -o output\n", progname
);
62 main(int ac
, char *av
[])
64 char model
[MAX_MODEL_NAME_LEN
+1];
65 char signature
[MAX_SIG_LEN
+1];
66 char region
[MAX_REGION_LEN
+1];
67 char version
[MAX_VERSION_LEN
+1];
70 int have_regionversion
= 0;
78 progname
= basename(av
[0]);
79 memset(model
, 0, sizeof(model
));
80 memset(signature
, 0, sizeof(signature
));
81 memset(region
, 0, sizeof(region
));
82 memset(version
, 0, sizeof(version
));
87 c
= getopt(ac
, av
, "pxm:r:v:s:i:o:");
99 if (strlen(optarg
) > MAX_MODEL_NAME_LEN
) {
100 fprintf(stderr
, "%s: model name exceeds %d chars\n",
101 progname
, MAX_MODEL_NAME_LEN
);
104 strcpy(model
, optarg
);
107 if (strlen(optarg
) > MAX_REGION_LEN
) {
108 fprintf(stderr
, "%s: region exceeds %d chars\n",
109 progname
, MAX_REGION_LEN
);
112 have_regionversion
= 1;
113 strcpy(region
, optarg
);
116 if (strlen(optarg
) > MAX_VERSION_LEN
) {
117 fprintf(stderr
, "%s: version exceeds %d chars\n",
118 progname
, MAX_VERSION_LEN
);
121 have_regionversion
= 1;
122 strcpy(version
, optarg
);
125 if (strlen(optarg
) > MAX_SIG_LEN
) {
126 fprintf(stderr
, "%s: signature exceeds %d chars\n",
127 progname
, MAX_SIG_LEN
);
130 strcpy(signature
, optarg
);
133 if ((ifile
= fopen(optarg
, "r")) == NULL
)
137 if ((ofile
= fopen(optarg
, "w")) == NULL
)
145 if (signature
[0] == 0 || ifile
== NULL
|| ofile
== NULL
) {
150 char *p
= strchr(signature
, '-');
152 fprintf(stderr
, "%s: model name unknown\n", progname
);
155 if (p
- signature
> MAX_MODEL_NAME_LEN
) {
157 fprintf(stderr
, "%s: auto model name failed, string %s too long\n", progname
, signature
);
160 strncpy(model
, signature
, p
- signature
);
164 if (fread(&imghdr
, sizeof(imghdr
), 1, ifile
) < 0)
165 perrexit(2, "fread on input");
168 for (bcnt
= 0, cksum
= 0 ; (c
= fgetc(ifile
)) != EOF
; bcnt
++)
171 if (fseek(ifile
, patchmode
? sizeof(imghdr
) : 0, SEEK_SET
) < 0)
172 perrexit(2, "fseek on input");
174 if (patchmode
== 0) {
175 // Fill in the header
176 memset(&imghdr
, 0, sizeof(imghdr
));
177 imghdr
.checksum
= htonl(cksum
);
178 imghdr
.partition
= 0 ; // don't care?
179 imghdr
.hdr_len
= sizeof(imghdr
);
180 if (have_regionversion
) {
181 imghdr
.hdr_len
+= MAX_REGION_LEN
;
182 imghdr
.hdr_len
+= MAX_VERSION_LEN
;
184 imghdr
.flash_byte_cnt
= htonl(bcnt
);
186 if (ntohl(imghdr
.checksum
) != cksum
) {
187 fprintf(stderr
, "%s: patch mode, checksum mismatch\n",
190 fprintf(stderr
, "%s: fixing\n", progname
);
191 imghdr
.checksum
= htonl(cksum
);
194 } else if (ntohl(imghdr
.flash_byte_cnt
) != bcnt
) {
195 fprintf(stderr
, "%s: patch mode, size mismatch\n",
198 fprintf(stderr
, "%s: fixing\n", progname
);
199 imghdr
.flash_byte_cnt
= htonl(bcnt
);
205 strncpy(imghdr
.model
, model
, MAX_MODEL_NAME_LEN
);
206 strncpy(imghdr
.sig
, signature
, MAX_SIG_LEN
);
208 if (fwrite(&imghdr
, sizeof(imghdr
), 1, ofile
) < 0)
209 perrexit(2, "fwrite header on output");
210 if (have_regionversion
) {
211 if (fwrite(®ion
, MAX_REGION_LEN
, 1, ofile
) < 0)
212 perrexit(2, "fwrite header on output");
213 if (fwrite(&version
, MAX_VERSION_LEN
, 1, ofile
) < 0)
214 perrexit(2, "fwrite header on output");
217 while ((c
= fgetc(ifile
)) != EOF
) {
218 if (fputc(c
, ofile
) == EOF
)
219 perrexit(2, "fputc on output");
223 perrexit(2, "fgetc on input");