1123475Swpaul/*
2123475Swpaul * Copyright (c) 2003
3123475Swpaul *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4123475Swpaul *
5123475Swpaul * Redistribution and use in source and binary forms, with or without
6123475Swpaul * modification, are permitted provided that the following conditions
7123475Swpaul * are met:
8123475Swpaul * 1. Redistributions of source code must retain the above copyright
9123475Swpaul *    notice, this list of conditions and the following disclaimer.
10123475Swpaul * 2. Redistributions in binary form must reproduce the above copyright
11123475Swpaul *    notice, this list of conditions and the following disclaimer in the
12123475Swpaul *    documentation and/or other materials provided with the distribution.
13123475Swpaul * 3. All advertising materials mentioning features or use of this software
14123475Swpaul *    must display the following acknowledgement:
15123475Swpaul *	This product includes software developed by Bill Paul.
16123475Swpaul * 4. Neither the name of the author nor the names of any co-contributors
17123475Swpaul *    may be used to endorse or promote products derived from this software
18123475Swpaul *    without specific prior written permission.
19123475Swpaul *
20123475Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21123475Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22123475Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23123475Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24123475Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25123475Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26123475Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27123475Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28123475Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29123475Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30123475Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
31123475Swpaul */
32123475Swpaul
33123475Swpaul#include <sys/cdefs.h>
34123475Swpaul__FBSDID("$FreeBSD$");
35123475Swpaul
36123475Swpaul#include <sys/types.h>
37123475Swpaul#include <sys/queue.h>
38124060Swpaul#include <sys/socket.h>
39124060Swpaul#include <net/if.h>
40123475Swpaul#include <stdlib.h>
41151703Swpaul#include <stddef.h>
42123475Swpaul#include <unistd.h>
43123475Swpaul#include <stdio.h>
44123475Swpaul#include <errno.h>
45123475Swpaul#include <string.h>
46132973Swpaul#include <libgen.h>
47123475Swpaul#include <err.h>
48132973Swpaul#include <ctype.h>
49123475Swpaul
50123475Swpaul#include <compat/ndis/pe_var.h>
51123475Swpaul
52123475Swpaul#include "inf.h"
53123475Swpaul
54123475Swpaulstatic int insert_padding(void **, int *);
55123475Swpaulextern const char *__progname;
56123475Swpaul
57123475Swpaul/*
58133026Swpaul * Sections within Windows PE files are defined using virtual
59133026Swpaul * and physical address offsets and virtual and physical sizes.
60133026Swpaul * The physical values define how the section data is stored in
61133026Swpaul * the executable file while the virtual values describe how the
62133026Swpaul * sections will look once loaded into memory. It happens that
63133026Swpaul * the linker in the Microsoft(r) DDK will tend to generate
64133026Swpaul * binaries where the virtual and physical values are identical,
65133026Swpaul * which means in most cases we can just transfer the file
66133026Swpaul * directly to memory without any fixups. This is not always
67133026Swpaul * the case though, so we have to be prepared to handle files
68133026Swpaul * where the in-memory section layout differs from the disk file
69133026Swpaul * section layout.
70123475Swpaul *
71133026Swpaul * There are two kinds of variations that can occur: the relative
72133026Swpaul * virtual address of the section might be different from the
73133026Swpaul * physical file offset, and the virtual section size might be
74133026Swpaul * different from the physical size (for example, the physical
75133026Swpaul * size of the .data section might be 1024 bytes, but the virtual
76133026Swpaul * size might be 1384 bytes, indicating that the data section should
77133026Swpaul * actually use up 1384 bytes in RAM and be padded with zeros). What we
78133026Swpaul * do is read the original file into memory and then make an in-memory
79133026Swpaul * copy with all of the sections relocated, re-sized and zero padded
80133026Swpaul * according to the virtual values specified in the section headers.
81133026Swpaul * We then emit the fixed up image file for use by the if_ndis driver.
82133026Swpaul * This way, we don't have to do the fixups inside the kernel.
83123475Swpaul */
84123475Swpaul
85133026Swpaul#define ROUND_DOWN(n, align)    (((uintptr_t)n) & ~((align) - 1l))
86133026Swpaul#define ROUND_UP(n, align)      ROUND_DOWN(((uintptr_t)n) + (align) - 1l, \
87133026Swpaul                                (align))
88123475Swpaul
89123475Swpaul#define SET_HDRS(x)	\
90123475Swpaul	dos_hdr = (image_dos_header *)x;				\
91123475Swpaul	nt_hdr = (image_nt_header *)(x + dos_hdr->idh_lfanew);		\
92151703Swpaul	sect_hdr = IMAGE_FIRST_SECTION(nt_hdr);
93123475Swpaul
94201387Sedstatic int
95201387Sedinsert_padding(void **imgbase, int *imglen)
96123475Swpaul{
97123475Swpaul        image_section_header	*sect_hdr;
98123475Swpaul        image_dos_header	*dos_hdr;
99123475Swpaul        image_nt_header		*nt_hdr;
100123475Swpaul	image_optional_header	opt_hdr;
101123475Swpaul        int			i = 0, sections, curlen = 0;
102133026Swpaul	int			offaccum = 0, oldraddr, oldrlen;
103123475Swpaul	uint8_t			*newimg, *tmp;
104123475Swpaul
105123475Swpaul	newimg = malloc(*imglen);
106123475Swpaul
107123475Swpaul	if (newimg == NULL)
108123475Swpaul		return(ENOMEM);
109123475Swpaul
110123475Swpaul	bcopy(*imgbase, newimg, *imglen);
111123475Swpaul	curlen = *imglen;
112123475Swpaul
113123475Swpaul	if (pe_get_optional_header((vm_offset_t)newimg, &opt_hdr))
114123475Swpaul		return(0);
115123475Swpaul
116123475Swpaul        sections = pe_numsections((vm_offset_t)newimg);
117123475Swpaul
118123475Swpaul	SET_HDRS(newimg);
119123475Swpaul
120123475Swpaul	for (i = 0; i < sections; i++) {
121123475Swpaul		oldraddr = sect_hdr->ish_rawdataaddr;
122123475Swpaul		oldrlen = sect_hdr->ish_rawdatasize;
123133026Swpaul		sect_hdr->ish_rawdataaddr = sect_hdr->ish_vaddr;
124133026Swpaul		offaccum += ROUND_UP(sect_hdr->ish_vaddr - oldraddr,
125133026Swpaul		    opt_hdr.ioh_filealign);
126133026Swpaul		offaccum +=
127133026Swpaul		    ROUND_UP(sect_hdr->ish_misc.ish_vsize,
128133026Swpaul			     opt_hdr.ioh_filealign) -
129133026Swpaul		    ROUND_UP(sect_hdr->ish_rawdatasize,
130133026Swpaul			     opt_hdr.ioh_filealign);
131133026Swpaul		tmp = realloc(newimg, *imglen + offaccum);
132133026Swpaul		if (tmp == NULL) {
133133026Swpaul			free(newimg);
134133026Swpaul			return(ENOMEM);
135123475Swpaul		}
136133026Swpaul		newimg = tmp;
137133026Swpaul		SET_HDRS(newimg);
138133026Swpaul		sect_hdr += i;
139123475Swpaul		bzero(newimg + sect_hdr->ish_rawdataaddr,
140123475Swpaul		    ROUND_UP(sect_hdr->ish_misc.ish_vsize,
141123475Swpaul		    opt_hdr.ioh_filealign));
142123475Swpaul		bcopy((uint8_t *)(*imgbase) + oldraddr,
143123475Swpaul		    newimg + sect_hdr->ish_rawdataaddr, oldrlen);
144123475Swpaul		sect_hdr++;
145123475Swpaul	}
146123475Swpaul
147123475Swpaul	free(*imgbase);
148123475Swpaul
149123475Swpaul	*imgbase = newimg;
150123475Swpaul	*imglen += offaccum;
151123475Swpaul
152123475Swpaul	return(0);
153123475Swpaul}
154123475Swpaul
155123475Swpaulstatic void
156123475Swpaulusage(void)
157123475Swpaul{
158132973Swpaul	fprintf(stderr, "Usage: %s [-O] [-i <inffile>] -s <sysfile> "
159124089Sgreen	    "[-n devname] [-o outfile]\n", __progname);
160132973Swpaul	fprintf(stderr, "       %s -f <firmfile>\n", __progname);
161132973Swpaul
162123475Swpaul	exit(1);
163123475Swpaul}
164123475Swpaul
165132973Swpaulstatic void
166132973Swpaulbincvt(char *sysfile, char *outfile, void *img, int fsize)
167132973Swpaul{
168132973Swpaul	char			*ptr;
169132973Swpaul	char			tname[] = "/tmp/ndiscvt.XXXXXX";
170132973Swpaul	char			sysbuf[1024];
171132973Swpaul	FILE			*binfp;
172132973Swpaul
173132973Swpaul	mkstemp(tname);
174132973Swpaul
175132973Swpaul	binfp = fopen(tname, "a+");
176132973Swpaul	if (binfp == NULL)
177132973Swpaul		err(1, "opening %s failed", tname);
178132973Swpaul
179132973Swpaul	if (fwrite(img, fsize, 1, binfp) != 1)
180132973Swpaul		err(1, "failed to output binary image");
181132973Swpaul
182132973Swpaul	fclose(binfp);
183132973Swpaul
184132973Swpaul	outfile = strdup(basename(outfile));
185132973Swpaul	if (strchr(outfile, '.'))
186132973Swpaul		*strchr(outfile, '.') = '\0';
187132973Swpaul
188132973Swpaul	snprintf(sysbuf, sizeof(sysbuf),
189142075Swpaul#ifdef __i386__
190132973Swpaul	    "objcopy -I binary -O elf32-i386-freebsd -B i386 %s %s.o\n",
191142075Swpaul#endif
192142075Swpaul#ifdef __amd64__
193218822Sdim	    "objcopy -I binary -O elf64-x86-64-freebsd -B i386 %s %s.o\n",
194142075Swpaul#endif
195132973Swpaul	    tname, outfile);
196132973Swpaul	printf("%s", sysbuf);
197132973Swpaul	system(sysbuf);
198132973Swpaul	unlink(tname);
199132973Swpaul
200132973Swpaul	ptr = tname;
201132973Swpaul	while (*ptr) {
202132973Swpaul		if (*ptr == '/' || *ptr == '.')
203132973Swpaul			*ptr = '_';
204132973Swpaul		ptr++;
205132973Swpaul	}
206132973Swpaul
207132973Swpaul	snprintf(sysbuf, sizeof(sysbuf),
208178213Sthompsa	    "objcopy --redefine-sym _binary_%s_start=ndis_%s_drv_data_start "
209132973Swpaul	    "--strip-symbol _binary_%s_size "
210178213Sthompsa	    "--redefine-sym _binary_%s_end=ndis_%s_drv_data_end %s.o %s.o\n",
211132973Swpaul	    tname, sysfile, tname, tname, sysfile, outfile, outfile);
212132973Swpaul	printf("%s", sysbuf);
213132973Swpaul	system(sysbuf);
214132973Swpaul
215132973Swpaul	return;
216132973Swpaul}
217132973Swpaul
218132973Swpaulstatic void
219132973Swpaulfirmcvt(char *firmfile)
220132973Swpaul{
221132973Swpaul	char			*basefile, *outfile, *ptr;
222132973Swpaul	char			sysbuf[1024];
223132973Swpaul
224142075Swpaul	outfile = strdup(basename(firmfile));
225132973Swpaul	basefile = strdup(outfile);
226132973Swpaul
227132973Swpaul	snprintf(sysbuf, sizeof(sysbuf),
228142075Swpaul#ifdef __i386__
229132973Swpaul	    "objcopy -I binary -O elf32-i386-freebsd -B i386 %s %s.o\n",
230142075Swpaul#endif
231142075Swpaul#ifdef __amd64__
232218822Sdim	    "objcopy -I binary -O elf64-x86-64-freebsd -B i386 %s %s.o\n",
233142075Swpaul#endif
234132973Swpaul	    firmfile, outfile);
235132973Swpaul	printf("%s", sysbuf);
236132973Swpaul	system(sysbuf);
237132973Swpaul
238132973Swpaul	ptr = firmfile;
239132973Swpaul	while (*ptr) {
240132973Swpaul		if (*ptr == '/' || *ptr == '.')
241132973Swpaul			*ptr = '_';
242132973Swpaul		ptr++;
243132973Swpaul	}
244132973Swpaul	ptr = basefile;
245132973Swpaul	while (*ptr) {
246132973Swpaul		if (*ptr == '/' || *ptr == '.')
247132973Swpaul			*ptr = '_';
248132973Swpaul		else
249132973Swpaul			*ptr = tolower(*ptr);
250132973Swpaul		ptr++;
251132973Swpaul	}
252132973Swpaul
253132973Swpaul	snprintf(sysbuf, sizeof(sysbuf),
254132973Swpaul	    "objcopy --redefine-sym _binary_%s_start=%s_start "
255132973Swpaul	    "--strip-symbol _binary_%s_size "
256132973Swpaul	    "--redefine-sym _binary_%s_end=%s_end %s.o %s.o\n",
257132973Swpaul	    firmfile, basefile, firmfile, firmfile,
258132973Swpaul	    basefile, outfile, outfile);
259132973Swpaul	ptr = sysbuf;
260132973Swpaul	printf("%s", sysbuf);
261132973Swpaul	system(sysbuf);
262132973Swpaul
263132973Swpaul	snprintf(sysbuf, sizeof(sysbuf),
264132973Swpaul	    "ld -Bshareable -d -warn-common -o %s.ko %s.o\n",
265132973Swpaul	    outfile, outfile);
266132973Swpaul	printf("%s", sysbuf);
267132973Swpaul	system(sysbuf);
268132973Swpaul
269132973Swpaul	free(basefile);
270132973Swpaul
271132973Swpaul	exit(0);
272132973Swpaul}
273132973Swpaul
274123475Swpaulint
275123475Swpaulmain(int argc, char *argv[])
276123475Swpaul{
277132973Swpaul	FILE			*fp, *outfp;
278132973Swpaul	int			i, bin = 0;
279132973Swpaul	void			*img;
280132973Swpaul	int			n, fsize, cnt;
281132973Swpaul	unsigned char		*ptr;
282132973Swpaul	char			*inffile = NULL, *sysfile = NULL;
283132973Swpaul	char			*outfile = NULL, *firmfile = NULL;
284132973Swpaul	char			*dname = NULL;
285132973Swpaul	int			ch;
286123475Swpaul
287132973Swpaul	while((ch = getopt(argc, argv, "i:s:o:n:f:O")) != -1) {
288123475Swpaul		switch(ch) {
289132973Swpaul		case 'f':
290132973Swpaul			firmfile = optarg;
291132973Swpaul			break;
292123475Swpaul		case 'i':
293123475Swpaul			inffile = optarg;
294123475Swpaul			break;
295123475Swpaul		case 's':
296123475Swpaul			sysfile = optarg;
297123475Swpaul			break;
298123475Swpaul		case 'o':
299123475Swpaul			outfile = optarg;
300123475Swpaul			break;
301124060Swpaul		case 'n':
302124060Swpaul			dname = optarg;
303124060Swpaul			break;
304132973Swpaul		case 'O':
305132973Swpaul			bin = 1;
306132973Swpaul			break;
307123475Swpaul		default:
308123475Swpaul			usage();
309123475Swpaul			break;
310123475Swpaul		}
311123475Swpaul	}
312123475Swpaul
313132973Swpaul	if (firmfile != NULL)
314132973Swpaul		firmcvt(firmfile);
315132973Swpaul
316123475Swpaul	if (sysfile == NULL)
317123475Swpaul		usage();
318123475Swpaul
319123475Swpaul	/* Open the .SYS file and load it into memory */
320123475Swpaul	fp = fopen(sysfile, "r");
321123475Swpaul	if (fp == NULL)
322123475Swpaul		err(1, "opening .SYS file '%s' failed", sysfile);
323123475Swpaul	fseek (fp, 0L, SEEK_END);
324123475Swpaul	fsize = ftell (fp);
325123475Swpaul	rewind (fp);
326123475Swpaul	img = calloc(fsize, 1);
327123475Swpaul	n = fread (img, fsize, 1, fp);
328243074Seadler	if (n == 0)
329243074Seadler		err(1, "reading .SYS file '%s' failed", sysfile);
330123475Swpaul
331123475Swpaul	fclose(fp);
332123475Swpaul
333123475Swpaul	if (insert_padding(&img, &fsize)) {
334123475Swpaul		fprintf(stderr, "section relocation failed\n");
335123475Swpaul		exit(1);
336123475Swpaul	}
337123475Swpaul
338123475Swpaul	if (outfile == NULL || strcmp(outfile, "-") == 0)
339123475Swpaul		outfp = stdout;
340123475Swpaul	else {
341123475Swpaul		outfp = fopen(outfile, "w");
342123475Swpaul		if (outfp == NULL)
343123475Swpaul			err(1, "opening output file '%s' failed", outfile);
344123475Swpaul	}
345123475Swpaul
346123475Swpaul	fprintf(outfp, "\n/*\n");
347123475Swpaul	fprintf(outfp, " * Generated from %s and %s (%d bytes)\n",
348124060Swpaul	    inffile == NULL ? "<notused>" : inffile, sysfile, fsize);
349123475Swpaul	fprintf(outfp, " */\n\n");
350123475Swpaul
351124060Swpaul	if (dname != NULL) {
352124060Swpaul		if (strlen(dname) > IFNAMSIZ)
353124060Swpaul			err(1, "selected device name '%s' is "
354124060Swpaul			    "too long (max chars: %d)", dname, IFNAMSIZ);
355124060Swpaul		fprintf (outfp, "#define NDIS_DEVNAME \"%s\"\n", dname);
356124085Swpaul		fprintf (outfp, "#define NDIS_MODNAME %s\n\n", dname);
357124060Swpaul	}
358124060Swpaul
359124060Swpaul	if (inffile == NULL) {
360126706Swpaul		fprintf (outfp, "#ifdef NDIS_REGVALS\n");
361123475Swpaul		fprintf (outfp, "ndis_cfg ndis_regvals[] = {\n");
362124060Swpaul        	fprintf (outfp, "\t{ NULL, NULL, { 0 }, 0 }\n");
363126706Swpaul		fprintf (outfp, "#endif /* NDIS_REGVALS */\n");
364123475Swpaul
365123475Swpaul		fprintf (outfp, "};\n\n");
366123475Swpaul	} else {
367123475Swpaul		fp = fopen(inffile, "r");
368123475Swpaul		if (fp == NULL)
369123475Swpaul			err(1, "opening .INF file '%s' failed", inffile);
370123475Swpaul
371123475Swpaul
372123475Swpaul		inf_parse(fp, outfp);
373123475Swpaul		fclose(fp);
374123475Swpaul	}
375123475Swpaul
376126706Swpaul	fprintf(outfp, "\n#ifdef NDIS_IMAGE\n");
377132973Swpaul
378132973Swpaul	if (bin) {
379132973Swpaul		sysfile = strdup(basename(sysfile));
380162491Skan		ptr = (unsigned char *)sysfile;
381132973Swpaul		while (*ptr) {
382132973Swpaul			if (*ptr == '.')
383132973Swpaul				*ptr = '_';
384132973Swpaul			ptr++;
385132973Swpaul		}
386132973Swpaul		fprintf(outfp,
387178213Sthompsa		    "\nextern unsigned char ndis_%s_drv_data_start[];\n",
388132973Swpaul		    sysfile);
389132973Swpaul		fprintf(outfp, "static unsigned char *drv_data = "
390178213Sthompsa		    "ndis_%s_drv_data_start;\n\n", sysfile);
391132973Swpaul		bincvt(sysfile, outfile, img, fsize);
392132973Swpaul		goto done;
393132973Swpaul	}
394132973Swpaul
395132973Swpaul
396126706Swpaul	fprintf(outfp, "\nextern unsigned char drv_data[];\n\n");
397123475Swpaul
398123653Swpaul	fprintf(outfp, "__asm__(\".data\");\n");
399141524Swpaul	fprintf(outfp, "__asm__(\".globl  drv_data\");\n");
400123653Swpaul	fprintf(outfp, "__asm__(\".type   drv_data, @object\");\n");
401123653Swpaul	fprintf(outfp, "__asm__(\".size   drv_data, %d\");\n", fsize);
402123653Swpaul	fprintf(outfp, "__asm__(\"drv_data:\");\n");
403123653Swpaul
404123475Swpaul	ptr = img;
405123475Swpaul	cnt = 0;
406123475Swpaul	while(cnt < fsize) {
407123653Swpaul		fprintf (outfp, "__asm__(\".byte ");
408123653Swpaul		for (i = 0; i < 10; i++) {
409123475Swpaul			cnt++;
410123475Swpaul			if (cnt == fsize) {
411123653Swpaul				fprintf(outfp, "0x%.2X\");\n", ptr[i]);
412123475Swpaul				goto done;
413123653Swpaul			} else {
414123653Swpaul				if (i == 9)
415123653Swpaul					fprintf(outfp, "0x%.2X\");\n", ptr[i]);
416123653Swpaul				else
417123653Swpaul					fprintf(outfp, "0x%.2X, ", ptr[i]);
418123653Swpaul			}
419123475Swpaul		}
420123653Swpaul		ptr += 10;
421123475Swpaul	}
422123475Swpaul
423123475Swpauldone:
424123475Swpaul
425126706Swpaul	fprintf(outfp, "#endif /* NDIS_IMAGE */\n");
426132973Swpaul
427123475Swpaul	if (fp != NULL)
428123475Swpaul		fclose(fp);
429123475Swpaul	fclose(outfp);
430123475Swpaul	free(img);
431123475Swpaul	exit(0);
432123475Swpaul}
433