3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
50 #include <compat/ndis/pe_var.h>
54 static int insert_padding(void **, int *);
55 extern const char *__progname
;
58 * Sections within Windows PE files are defined using virtual
59 * and physical address offsets and virtual and physical sizes.
60 * The physical values define how the section data is stored in
61 * the executable file while the virtual values describe how the
62 * sections will look once loaded into memory. It happens that
63 * the linker in the Microsoft(r) DDK will tend to generate
64 * binaries where the virtual and physical values are identical,
65 * which means in most cases we can just transfer the file
66 * directly to memory without any fixups. This is not always
67 * the case though, so we have to be prepared to handle files
68 * where the in-memory section layout differs from the disk file
71 * There are two kinds of variations that can occur: the relative
72 * virtual address of the section might be different from the
73 * physical file offset, and the virtual section size might be
74 * different from the physical size (for example, the physical
75 * size of the .data section might be 1024 bytes, but the virtual
76 * size might be 1384 bytes, indicating that the data section should
77 * actually use up 1384 bytes in RAM and be padded with zeros). What we
78 * do is read the original file into memory and then make an in-memory
79 * copy with all of the sections relocated, re-sized and zero padded
80 * according to the virtual values specified in the section headers.
81 * We then emit the fixed up image file for use by the if_ndis driver.
82 * This way, we don't have to do the fixups inside the kernel.
85 #define ROUND_DOWN(n, align) (((uintptr_t)n) & ~((align) - 1l))
86 #define ROUND_UP(n, align) ROUND_DOWN(((uintptr_t)n) + (align) - 1l, \
90 dos_hdr = (image_dos_header *)x; \
91 nt_hdr = (image_nt_header *)(x + dos_hdr->idh_lfanew); \
92 sect_hdr = IMAGE_FIRST_SECTION(nt_hdr);
95 int insert_padding(imgbase
, imglen
)
99 image_section_header
*sect_hdr
;
100 image_dos_header
*dos_hdr
;
101 image_nt_header
*nt_hdr
;
102 image_optional_header opt_hdr
;
103 int i
= 0, sections
, curlen
= 0;
104 int offaccum
= 0, oldraddr
, oldrlen
;
105 uint8_t *newimg
, *tmp
;
107 newimg
= malloc(*imglen
);
112 bcopy(*imgbase
, newimg
, *imglen
);
115 if (pe_get_optional_header((vm_offset_t
)newimg
, &opt_hdr
))
118 sections
= pe_numsections((vm_offset_t
)newimg
);
122 for (i
= 0; i
< sections
; i
++) {
123 oldraddr
= sect_hdr
->ish_rawdataaddr
;
124 oldrlen
= sect_hdr
->ish_rawdatasize
;
125 sect_hdr
->ish_rawdataaddr
= sect_hdr
->ish_vaddr
;
126 offaccum
+= ROUND_UP(sect_hdr
->ish_vaddr
- oldraddr
,
127 opt_hdr
.ioh_filealign
);
129 ROUND_UP(sect_hdr
->ish_misc
.ish_vsize
,
130 opt_hdr
.ioh_filealign
) -
131 ROUND_UP(sect_hdr
->ish_rawdatasize
,
132 opt_hdr
.ioh_filealign
);
133 tmp
= realloc(newimg
, *imglen
+ offaccum
);
141 bzero(newimg
+ sect_hdr
->ish_rawdataaddr
,
142 ROUND_UP(sect_hdr
->ish_misc
.ish_vsize
,
143 opt_hdr
.ioh_filealign
));
144 bcopy((uint8_t *)(*imgbase
) + oldraddr
,
145 newimg
+ sect_hdr
->ish_rawdataaddr
, oldrlen
);
160 fprintf(stderr
, "Usage: %s [-O] [-i <inffile>] -s <sysfile> "
161 "[-n devname] [-o outfile]\n", __progname
);
162 fprintf(stderr
, " %s -f <firmfile>\n", __progname
);
168 bincvt(char *sysfile
, char *outfile
, void *img
, int fsize
)
171 char tname
[] = "/tmp/ndiscvt.XXXXXX";
177 binfp
= fopen(tname
, "a+");
179 err(1, "opening %s failed", tname
);
181 if (fwrite(img
, fsize
, 1, binfp
) != 1)
182 err(1, "failed to output binary image");
186 outfile
= strdup(basename(outfile
));
187 if (strchr(outfile
, '.'))
188 *strchr(outfile
, '.') = '\0';
190 snprintf(sysbuf
, sizeof(sysbuf
),
192 "objcopy -I binary -O elf32-i386-freebsd -B i386 %s %s.o\n",
195 "objcopy -I binary -O elf64-x86-64 -B i386 %s %s.o\n",
198 printf("%s", sysbuf
);
204 if (*ptr
== '/' || *ptr
== '.')
209 snprintf(sysbuf
, sizeof(sysbuf
),
210 "objcopy --redefine-sym _binary_%s_start=ndis_%s_drv_data_start "
211 "--strip-symbol _binary_%s_size "
212 "--redefine-sym _binary_%s_end=ndis_%s_drv_data_end %s.o %s.o\n",
213 tname
, sysfile
, tname
, tname
, sysfile
, outfile
, outfile
);
214 printf("%s", sysbuf
);
221 firmcvt(char *firmfile
)
223 char *basefile
, *outfile
, *ptr
;
226 outfile
= strdup(basename(firmfile
));
227 basefile
= strdup(outfile
);
229 snprintf(sysbuf
, sizeof(sysbuf
),
231 "objcopy -I binary -O elf32-i386-freebsd -B i386 %s %s.o\n",
234 "objcopy -I binary -O elf64-x86-64 -B i386 %s %s.o\n",
237 printf("%s", sysbuf
);
242 if (*ptr
== '/' || *ptr
== '.')
248 if (*ptr
== '/' || *ptr
== '.')
251 *ptr
= tolower(*ptr
);
255 snprintf(sysbuf
, sizeof(sysbuf
),
256 "objcopy --redefine-sym _binary_%s_start=%s_start "
257 "--strip-symbol _binary_%s_size "
258 "--redefine-sym _binary_%s_end=%s_end %s.o %s.o\n",
259 firmfile
, basefile
, firmfile
, firmfile
,
260 basefile
, outfile
, outfile
);
262 printf("%s", sysbuf
);
265 snprintf(sysbuf
, sizeof(sysbuf
),
266 "ld -Bshareable -d -warn-common -o %s.ko %s.o\n",
268 printf("%s", sysbuf
);
277 main(int argc
, char *argv
[])
284 char *inffile
= NULL
, *sysfile
= NULL
;
285 char *outfile
= NULL
, *firmfile
= NULL
;
289 while((ch
= getopt(argc
, argv
, "i:s:o:n:f:O")) != -1) {
315 if (firmfile
!= NULL
)
321 /* Open the .SYS file and load it into memory */
322 fp
= fopen(sysfile
, "r");
324 err(1, "opening .SYS file '%s' failed", sysfile
);
325 fseek (fp
, 0L, SEEK_END
);
328 img
= calloc(fsize
, 1);
329 n
= fread (img
, fsize
, 1, fp
);
333 if (insert_padding(&img
, &fsize
)) {
334 fprintf(stderr
, "section relocation failed\n");
338 if (outfile
== NULL
|| strcmp(outfile
, "-") == 0)
341 outfp
= fopen(outfile
, "w");
343 err(1, "opening output file '%s' failed", outfile
);
346 fprintf(outfp
, "\n/*\n");
347 fprintf(outfp
, " * Generated from %s and %s (%d bytes)\n",
348 inffile
== NULL
? "<notused>" : inffile
, sysfile
, fsize
);
349 fprintf(outfp
, " */\n\n");
352 if (strlen(dname
) > IFNAMSIZ
)
353 err(1, "selected device name '%s' is "
354 "too long (max chars: %d)", dname
, IFNAMSIZ
);
355 fprintf (outfp
, "#define NDIS_DEVNAME \"%s\"\n", dname
);
356 fprintf (outfp
, "#define NDIS_MODNAME %s\n\n", dname
);
359 if (inffile
== NULL
) {
360 fprintf (outfp
, "#ifdef NDIS_REGVALS\n");
361 fprintf (outfp
, "ndis_cfg ndis_regvals[] = {\n");
362 fprintf (outfp
, "\t{ NULL, NULL, { 0 }, 0 }\n");
363 fprintf (outfp
, "#endif /* NDIS_REGVALS */\n");
365 fprintf (outfp
, "};\n\n");
367 fp
= fopen(inffile
, "r");
369 err(1, "opening .INF file '%s' failed", inffile
);
372 inf_parse(fp
, outfp
);
376 fprintf(outfp
, "\n#ifdef NDIS_IMAGE\n");
379 sysfile
= strdup(basename(sysfile
));
380 ptr
= (unsigned char *)sysfile
;
387 "\nextern unsigned char ndis_%s_drv_data_start[];\n",
389 fprintf(outfp
, "static unsigned char *drv_data = "
390 "ndis_%s_drv_data_start;\n\n", sysfile
);
391 bincvt(sysfile
, outfile
, img
, fsize
);
396 fprintf(outfp
, "\nextern unsigned char drv_data[];\n\n");
398 fprintf(outfp
, "__asm__(\".data\");\n");
399 fprintf(outfp
, "__asm__(\".globl drv_data\");\n");
400 fprintf(outfp
, "__asm__(\".type drv_data, @object\");\n");
401 fprintf(outfp
, "__asm__(\".size drv_data, %d\");\n", fsize
);
402 fprintf(outfp
, "__asm__(\"drv_data:\");\n");
407 fprintf (outfp
, "__asm__(\".byte ");
408 for (i
= 0; i
< 10; i
++) {
411 fprintf(outfp
, "0x%.2X\");\n", ptr
[i
]);
415 fprintf(outfp
, "0x%.2X\");\n", ptr
[i
]);
417 fprintf(outfp
, "0x%.2X, ", ptr
[i
]);
425 fprintf(outfp
, "#endif /* NDIS_IMAGE */\n");