ndiscvt.c revision 133026
1/* 2 * Copyright (c) 2003 3 * Bill Paul <wpaul@windriver.com>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 19 * 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. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/usr.sbin/ndiscvt/ndiscvt.c 133026 2004-08-02 18:54:01Z wpaul $"); 35 36#include <sys/types.h> 37#include <sys/queue.h> 38#include <sys/socket.h> 39#include <net/if.h> 40#include <stdlib.h> 41#include <unistd.h> 42#include <stdio.h> 43#include <errno.h> 44#include <string.h> 45#include <libgen.h> 46#include <err.h> 47#include <ctype.h> 48 49#include <compat/ndis/pe_var.h> 50 51#include "inf.h" 52 53static int insert_padding(void **, int *); 54extern const char *__progname; 55 56/* 57 * Sections within Windows PE files are defined using virtual 58 * and physical address offsets and virtual and physical sizes. 59 * The physical values define how the section data is stored in 60 * the executable file while the virtual values describe how the 61 * sections will look once loaded into memory. It happens that 62 * the linker in the Microsoft(r) DDK will tend to generate 63 * binaries where the virtual and physical values are identical, 64 * which means in most cases we can just transfer the file 65 * directly to memory without any fixups. This is not always 66 * the case though, so we have to be prepared to handle files 67 * where the in-memory section layout differs from the disk file 68 * section layout. 69 * 70 * There are two kinds of variations that can occur: the relative 71 * virtual address of the section might be different from the 72 * physical file offset, and the virtual section size might be 73 * different from the physical size (for example, the physical 74 * size of the .data section might be 1024 bytes, but the virtual 75 * size might be 1384 bytes, indicating that the data section should 76 * actually use up 1384 bytes in RAM and be padded with zeros). What we 77 * do is read the original file into memory and then make an in-memory 78 * copy with all of the sections relocated, re-sized and zero padded 79 * according to the virtual values specified in the section headers. 80 * We then emit the fixed up image file for use by the if_ndis driver. 81 * This way, we don't have to do the fixups inside the kernel. 82 */ 83 84#define ROUND_DOWN(n, align) (((uintptr_t)n) & ~((align) - 1l)) 85#define ROUND_UP(n, align) ROUND_DOWN(((uintptr_t)n) + (align) - 1l, \ 86 (align)) 87 88#define SET_HDRS(x) \ 89 dos_hdr = (image_dos_header *)x; \ 90 nt_hdr = (image_nt_header *)(x + dos_hdr->idh_lfanew); \ 91 sect_hdr = (image_section_header *)((vm_offset_t)nt_hdr + \ 92 sizeof(image_nt_header)); 93 94static 95int insert_padding(imgbase, imglen) 96 void **imgbase; 97 int *imglen; 98{ 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; 106 107 newimg = malloc(*imglen); 108 109 if (newimg == NULL) 110 return(ENOMEM); 111 112 bcopy(*imgbase, newimg, *imglen); 113 curlen = *imglen; 114 115 if (pe_get_optional_header((vm_offset_t)newimg, &opt_hdr)) 116 return(0); 117 118 sections = pe_numsections((vm_offset_t)newimg); 119 120 SET_HDRS(newimg); 121 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); 128 offaccum += 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); 134 if (tmp == NULL) { 135 free(newimg); 136 return(ENOMEM); 137 } 138 newimg = tmp; 139 SET_HDRS(newimg); 140 sect_hdr += i; 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); 146 sect_hdr++; 147 } 148 149 free(*imgbase); 150 151 *imgbase = newimg; 152 *imglen += offaccum; 153 154 return(0); 155} 156 157static void 158usage(void) 159{ 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); 163 164 exit(1); 165} 166 167static void 168bincvt(char *sysfile, char *outfile, void *img, int fsize) 169{ 170 char *ptr; 171 char tname[] = "/tmp/ndiscvt.XXXXXX"; 172 char sysbuf[1024]; 173 FILE *binfp; 174 175 mkstemp(tname); 176 177 binfp = fopen(tname, "a+"); 178 if (binfp == NULL) 179 err(1, "opening %s failed", tname); 180 181 if (fwrite(img, fsize, 1, binfp) != 1) 182 err(1, "failed to output binary image"); 183 184 fclose(binfp); 185 186 outfile = strdup(basename(outfile)); 187 if (strchr(outfile, '.')) 188 *strchr(outfile, '.') = '\0'; 189 190 snprintf(sysbuf, sizeof(sysbuf), 191 "objcopy -I binary -O elf32-i386-freebsd -B i386 %s %s.o\n", 192 tname, outfile); 193 printf("%s", sysbuf); 194 system(sysbuf); 195 unlink(tname); 196 197 ptr = tname; 198 while (*ptr) { 199 if (*ptr == '/' || *ptr == '.') 200 *ptr = '_'; 201 ptr++; 202 } 203 204 snprintf(sysbuf, sizeof(sysbuf), 205 "objcopy --redefine-sym _binary_%s_start=%s_drv_data_start " 206 "--strip-symbol _binary_%s_size " 207 "--redefine-sym _binary_%s_end=%s_drv_data_end %s.o %s.o\n", 208 tname, sysfile, tname, tname, sysfile, outfile, outfile); 209 printf("%s", sysbuf); 210 system(sysbuf); 211 212 return; 213} 214 215static void 216firmcvt(char *firmfile) 217{ 218 char *basefile, *outfile, *ptr; 219 char sysbuf[1024]; 220 221 outfile = basename(firmfile); 222 basefile = strdup(outfile); 223 224 snprintf(sysbuf, sizeof(sysbuf), 225 "objcopy -I binary -O elf32-i386-freebsd -B i386 %s %s.o\n", 226 firmfile, outfile); 227 printf("%s", sysbuf); 228 system(sysbuf); 229 230 ptr = firmfile; 231 while (*ptr) { 232 if (*ptr == '/' || *ptr == '.') 233 *ptr = '_'; 234 ptr++; 235 } 236 ptr = basefile; 237 while (*ptr) { 238 if (*ptr == '/' || *ptr == '.') 239 *ptr = '_'; 240 else 241 *ptr = tolower(*ptr); 242 ptr++; 243 } 244 245 snprintf(sysbuf, sizeof(sysbuf), 246 "objcopy --redefine-sym _binary_%s_start=%s_start " 247 "--strip-symbol _binary_%s_size " 248 "--redefine-sym _binary_%s_end=%s_end %s.o %s.o\n", 249 firmfile, basefile, firmfile, firmfile, 250 basefile, outfile, outfile); 251 ptr = sysbuf; 252 printf("%s", sysbuf); 253 system(sysbuf); 254 255 snprintf(sysbuf, sizeof(sysbuf), 256 "ld -Bshareable -d -warn-common -o %s.ko %s.o\n", 257 outfile, outfile); 258 printf("%s", sysbuf); 259 system(sysbuf); 260 261 free(basefile); 262 263 exit(0); 264} 265 266int 267main(int argc, char *argv[]) 268{ 269 FILE *fp, *outfp; 270 int i, bin = 0; 271 void *img; 272 int n, fsize, cnt; 273 unsigned char *ptr; 274 char *inffile = NULL, *sysfile = NULL; 275 char *outfile = NULL, *firmfile = NULL; 276 char *dname = NULL; 277 int ch; 278 279 while((ch = getopt(argc, argv, "i:s:o:n:f:O")) != -1) { 280 switch(ch) { 281 case 'f': 282 firmfile = optarg; 283 break; 284 case 'i': 285 inffile = optarg; 286 break; 287 case 's': 288 sysfile = optarg; 289 break; 290 case 'o': 291 outfile = optarg; 292 break; 293 case 'n': 294 dname = optarg; 295 break; 296 case 'O': 297 bin = 1; 298 break; 299 default: 300 usage(); 301 break; 302 } 303 } 304 305 if (firmfile != NULL) 306 firmcvt(firmfile); 307 308 if (sysfile == NULL) 309 usage(); 310 311 /* Open the .SYS file and load it into memory */ 312 fp = fopen(sysfile, "r"); 313 if (fp == NULL) 314 err(1, "opening .SYS file '%s' failed", sysfile); 315 fseek (fp, 0L, SEEK_END); 316 fsize = ftell (fp); 317 rewind (fp); 318 img = calloc(fsize, 1); 319 n = fread (img, fsize, 1, fp); 320 321 fclose(fp); 322 323 if (insert_padding(&img, &fsize)) { 324 fprintf(stderr, "section relocation failed\n"); 325 exit(1); 326 } 327 328 if (outfile == NULL || strcmp(outfile, "-") == 0) 329 outfp = stdout; 330 else { 331 outfp = fopen(outfile, "w"); 332 if (outfp == NULL) 333 err(1, "opening output file '%s' failed", outfile); 334 } 335 336 fprintf(outfp, "\n/*\n"); 337 fprintf(outfp, " * Generated from %s and %s (%d bytes)\n", 338 inffile == NULL ? "<notused>" : inffile, sysfile, fsize); 339 fprintf(outfp, " */\n\n"); 340 341 if (dname != NULL) { 342 if (strlen(dname) > IFNAMSIZ) 343 err(1, "selected device name '%s' is " 344 "too long (max chars: %d)", dname, IFNAMSIZ); 345 fprintf (outfp, "#define NDIS_DEVNAME \"%s\"\n", dname); 346 fprintf (outfp, "#define NDIS_MODNAME %s\n\n", dname); 347 } 348 349 if (inffile == NULL) { 350 fprintf (outfp, "#ifdef NDIS_REGVALS\n"); 351 fprintf (outfp, "ndis_cfg ndis_regvals[] = {\n"); 352 fprintf (outfp, "\t{ NULL, NULL, { 0 }, 0 }\n"); 353 fprintf (outfp, "#endif /* NDIS_REGVALS */\n"); 354 355 fprintf (outfp, "};\n\n"); 356 } else { 357 fp = fopen(inffile, "r"); 358 if (fp == NULL) 359 err(1, "opening .INF file '%s' failed", inffile); 360 361 362 inf_parse(fp, outfp); 363 fclose(fp); 364 } 365 366 fprintf(outfp, "\n#ifdef NDIS_IMAGE\n"); 367 368 if (bin) { 369 sysfile = strdup(basename(sysfile)); 370 ptr = sysfile; 371 while (*ptr) { 372 if (*ptr == '.') 373 *ptr = '_'; 374 ptr++; 375 } 376 fprintf(outfp, 377 "\nextern unsigned char %s_drv_data_start[];\n", 378 sysfile); 379 fprintf(outfp, "static unsigned char *drv_data = " 380 "%s_drv_data_start;\n\n", sysfile); 381 bincvt(sysfile, outfile, img, fsize); 382 goto done; 383 } 384 385 386 fprintf(outfp, "\nextern unsigned char drv_data[];\n\n"); 387 388 fprintf(outfp, "__asm__(\".data\");\n"); 389 fprintf(outfp, "__asm__(\".type drv_data, @object\");\n"); 390 fprintf(outfp, "__asm__(\".size drv_data, %d\");\n", fsize); 391 fprintf(outfp, "__asm__(\"drv_data:\");\n"); 392 393 ptr = img; 394 cnt = 0; 395 while(cnt < fsize) { 396 fprintf (outfp, "__asm__(\".byte "); 397 for (i = 0; i < 10; i++) { 398 cnt++; 399 if (cnt == fsize) { 400 fprintf(outfp, "0x%.2X\");\n", ptr[i]); 401 goto done; 402 } else { 403 if (i == 9) 404 fprintf(outfp, "0x%.2X\");\n", ptr[i]); 405 else 406 fprintf(outfp, "0x%.2X, ", ptr[i]); 407 } 408 } 409 ptr += 10; 410 } 411 412done: 413 414 fprintf(outfp, "#endif /* NDIS_IMAGE */\n"); 415 416 if (fp != NULL) 417 fclose(fp); 418 fclose(outfp); 419 free(img); 420 exit(0); 421} 422