8b0359f7493aa1913b5baac27ed4178bf025d124
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
31 struct img_hdr_struct
{
33 char model
[MAX_MODEL_NAME_LEN
];
34 char sig
[MAX_SIG_LEN
];
39 uint32_t flash_byte_cnt
;
45 perrexit(int code
, char *msg
)
47 fprintf(stderr
, "%s: %s: %s\n", progname
, msg
, strerror(errno
));
54 fprintf(stderr
, "usage: %s [-p] [-m model] -s signature -i input -o output\n", progname
);
59 main(int ac
, char *av
[])
61 char model
[MAX_MODEL_NAME_LEN
+1];
62 char signature
[MAX_SIG_LEN
+1];
71 progname
= basename(av
[0]);
72 memset(model
, 0, sizeof(model
));
73 memset(signature
, 0, sizeof(signature
));
78 c
= getopt(ac
, av
, "pxm:s:i:o:");
90 if (strlen(optarg
) > MAX_MODEL_NAME_LEN
) {
91 fprintf(stderr
, "%s: model name exceeds %d chars\n",
92 progname
, MAX_MODEL_NAME_LEN
);
95 strcpy(model
, optarg
);
98 if (strlen(optarg
) > MAX_SIG_LEN
) {
99 fprintf(stderr
, "%s: signature exceeds %d chars\n",
100 progname
, MAX_SIG_LEN
);
103 strcpy(signature
, optarg
);
106 if ((ifile
= fopen(optarg
, "r")) == NULL
)
110 if ((ofile
= fopen(optarg
, "w")) == NULL
)
118 if (signature
[0] == 0 || ifile
== NULL
|| ofile
== NULL
) {
123 char *p
= strchr(signature
, '-');
125 fprintf(stderr
, "%s: model name unknown\n", progname
);
128 if (p
- signature
> MAX_MODEL_NAME_LEN
) {
130 fprintf(stderr
, "%s: auto model name failed, string %s too long\n", progname
, signature
);
133 strncpy(model
, signature
, p
- signature
);
137 if (fread(&imghdr
, sizeof(imghdr
), 1, ifile
) < 0)
138 perrexit(2, "fread on input");
141 for (bcnt
= 0, cksum
= 0 ; (c
= fgetc(ifile
)) != EOF
; bcnt
++)
144 if (fseek(ifile
, patchmode
? sizeof(imghdr
) : 0, SEEK_SET
) < 0)
145 perrexit(2, "fseek on input");
147 if (patchmode
== 0) {
148 // Fill in the header
149 memset(&imghdr
, 0, sizeof(imghdr
));
150 imghdr
.checksum
= htonl(cksum
);
151 imghdr
.partition
= 0 ; // don't care?
152 imghdr
.hdr_len
= sizeof(imghdr
);
153 imghdr
.flash_byte_cnt
= htonl(bcnt
);
155 if (ntohl(imghdr
.checksum
) != cksum
) {
156 fprintf(stderr
, "%s: patch mode, checksum mismatch\n",
159 fprintf(stderr
, "%s: fixing\n", progname
);
160 imghdr
.checksum
= htonl(cksum
);
163 } else if (ntohl(imghdr
.flash_byte_cnt
) != bcnt
) {
164 fprintf(stderr
, "%s: patch mode, size mismatch\n",
167 fprintf(stderr
, "%s: fixing\n", progname
);
168 imghdr
.flash_byte_cnt
= htonl(bcnt
);
174 strncpy(imghdr
.model
, model
, MAX_MODEL_NAME_LEN
);
175 strncpy(imghdr
.sig
, signature
, MAX_SIG_LEN
);
177 if (fwrite(&imghdr
, sizeof(imghdr
), 1, ofile
) < 0)
178 perrexit(2, "fwrite header on output");
180 while ((c
= fgetc(ifile
)) != EOF
) {
181 if (fputc(c
, ofile
) == EOF
)
182 perrexit(2, "fputc on output");
186 perrexit(2, "fgetc on input");