ndiscvt.c revision 123653
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 123653 2003-12-18 21:47:14Z wpaul $"); 35 36#include <sys/types.h> 37#include <sys/queue.h> 38#include <stdlib.h> 39#include <unistd.h> 40#include <stdio.h> 41#include <errno.h> 42#include <string.h> 43#include <err.h> 44 45#include <compat/ndis/pe_var.h> 46 47#include "inf.h" 48 49static int insert_padding(void **, int *); 50extern const char *__progname; 51 52/* 53 * Sections in object code files can be sparse. That is, the 54 * section may occupy more space in memory that it does when 55 * stored in a disk file. In Windows PE files, each section header 56 * has a 'virtual size' and 'raw data size' field. The latter 57 * specifies the amount of section data actually stored in the 58 * disk file, and the former describes how much space the section 59 * should actually occupy in memory. If the vsize is larger than 60 * the rsize, we need to allocate some extra storage and fill 61 * it with zeros. (Think BSS.) 62 * 63 * The typical method of loading an executable file involves 64 * reading each segment into memory using the vaddr/vsize from 65 * each section header. We try to make a small optimization however 66 * and only pad/move segments when it's absolutely necessary, i.e. 67 * if the vsize is larger than the rsize. This conserves a little 68 * bit of memory, at the cost of having to fixup some of the values 69 * in the section headers. 70 */ 71 72#define ROUND_UP(x, y) \ 73 (((x) + (y)) - ((x) % (y))) 74 75#define SET_HDRS(x) \ 76 dos_hdr = (image_dos_header *)x; \ 77 nt_hdr = (image_nt_header *)(x + dos_hdr->idh_lfanew); \ 78 sect_hdr = (image_section_header *)((vm_offset_t)nt_hdr + \ 79 sizeof(image_nt_header)); 80 81static 82int insert_padding(imgbase, imglen) 83 void **imgbase; 84 int *imglen; 85{ 86 image_section_header *sect_hdr; 87 image_dos_header *dos_hdr; 88 image_nt_header *nt_hdr; 89 image_optional_header opt_hdr; 90 int i = 0, sections, curlen = 0; 91 int offaccum = 0, diff, oldraddr, oldrlen; 92 uint8_t *newimg, *tmp; 93 94 newimg = malloc(*imglen); 95 96 if (newimg == NULL) 97 return(ENOMEM); 98 99 bcopy(*imgbase, newimg, *imglen); 100 curlen = *imglen; 101 102 if (pe_get_optional_header((vm_offset_t)newimg, &opt_hdr)) 103 return(0); 104 105 sections = pe_numsections((vm_offset_t)newimg); 106 107 SET_HDRS(newimg); 108 109 for (i = 0; i < sections; i++) { 110 /* 111 * If we have accumulated any padding offset, 112 * add it to the raw data address of this segment. 113 */ 114 oldraddr = sect_hdr->ish_rawdataaddr; 115 oldrlen = sect_hdr->ish_rawdatasize; 116 if (offaccum) 117 sect_hdr->ish_rawdataaddr += offaccum; 118 if (sect_hdr->ish_misc.ish_vsize > 119 sect_hdr->ish_rawdatasize) { 120 diff = ROUND_UP(sect_hdr->ish_misc.ish_vsize - 121 sect_hdr->ish_rawdatasize, 122 opt_hdr.ioh_filealign); 123 offaccum += ROUND_UP(diff - 124 (sect_hdr->ish_misc.ish_vsize - 125 sect_hdr->ish_rawdatasize), 126 opt_hdr.ioh_filealign); 127 sect_hdr->ish_rawdatasize = 128 ROUND_UP(sect_hdr->ish_rawdatasize, 129 opt_hdr.ioh_filealign); 130 tmp = realloc(newimg, *imglen + offaccum); 131 if (tmp == NULL) { 132 free(newimg); 133 return(ENOMEM); 134 } 135 newimg = tmp; 136 SET_HDRS(newimg); 137 sect_hdr += i; 138 } 139 bzero(newimg + sect_hdr->ish_rawdataaddr, 140 ROUND_UP(sect_hdr->ish_misc.ish_vsize, 141 opt_hdr.ioh_filealign)); 142 bcopy((uint8_t *)(*imgbase) + oldraddr, 143 newimg + sect_hdr->ish_rawdataaddr, oldrlen); 144 sect_hdr++; 145 } 146 147 free(*imgbase); 148 149 *imgbase = newimg; 150 *imglen += offaccum; 151 152 return(0); 153} 154 155static void 156usage(void) 157{ 158 fprintf(stderr, "Usage: %s [-i <inffile>] -s <sysfile> " 159 "[-o outfile]\n", __progname); 160 exit(1); 161} 162 163int 164main(int argc, char *argv[]) 165{ 166 FILE *fp, *outfp; 167 void *img; 168 int n, fsize, cnt; 169 unsigned char *ptr; 170 int i; 171 char *inffile = NULL, *sysfile = NULL, *outfile = NULL; 172 int ch; 173 174 while((ch = getopt(argc, argv, "i:s:o:")) != -1) { 175 switch(ch) { 176 case 'i': 177 inffile = optarg; 178 break; 179 case 's': 180 sysfile = optarg; 181 break; 182 case 'o': 183 outfile = optarg; 184 break; 185 default: 186 usage(); 187 break; 188 } 189 } 190 191 if (sysfile == NULL) 192 usage(); 193 194 /* Open the .SYS file and load it into memory */ 195 fp = fopen(sysfile, "r"); 196 if (fp == NULL) 197 err(1, "opening .SYS file '%s' failed", sysfile); 198 fseek (fp, 0L, SEEK_END); 199 fsize = ftell (fp); 200 rewind (fp); 201 img = calloc(fsize, 1); 202 n = fread (img, fsize, 1, fp); 203 204 fclose(fp); 205 206 if (insert_padding(&img, &fsize)) { 207 fprintf(stderr, "section relocation failed\n"); 208 exit(1); 209 } 210 211 if (outfile == NULL || strcmp(outfile, "-") == 0) 212 outfp = stdout; 213 else { 214 outfp = fopen(outfile, "w"); 215 if (outfp == NULL) 216 err(1, "opening output file '%s' failed", outfile); 217 } 218 219 fprintf(outfp, "\n/*\n"); 220 fprintf(outfp, " * Generated from %s and %s (%d bytes)\n", 221 inffile, sysfile, fsize); 222 fprintf(outfp, " */\n\n"); 223 224 if (fp == NULL) { 225 fprintf (outfp, "ndis_cfg ndis_regvals[] = {\n"); 226 fprintf (outfp, "\t{ NULL, NULL, ndis_parm_int, { 0 } }\n"); 227 228 fprintf (outfp, "};\n\n"); 229 } else { 230 fp = fopen(inffile, "r"); 231 if (fp == NULL) 232 err(1, "opening .INF file '%s' failed", inffile); 233 234 235 inf_parse(fp, outfp); 236 fclose(fp); 237 } 238 239 fprintf(outfp, "\n\nextern unsigned char drv_data[];\n\n"); 240 241 fprintf(outfp, "__asm__(\".data\");\n"); 242 fprintf(outfp, "__asm__(\".type drv_data, @object\");\n"); 243 fprintf(outfp, "__asm__(\".size drv_data, %d\");\n", fsize); 244 fprintf(outfp, "__asm__(\"drv_data:\");\n"); 245 246 ptr = img; 247 cnt = 0; 248 while(cnt < fsize) { 249 fprintf (outfp, "__asm__(\".byte "); 250 for (i = 0; i < 10; i++) { 251 cnt++; 252 if (cnt == fsize) { 253 fprintf(outfp, "0x%.2X\");\n", ptr[i]); 254 goto done; 255 } else { 256 if (i == 9) 257 fprintf(outfp, "0x%.2X\");\n", ptr[i]); 258 else 259 fprintf(outfp, "0x%.2X, ", ptr[i]); 260 } 261 } 262 ptr += 10; 263 } 264 265done: 266 267 if (fp != NULL) 268 fclose(fp); 269 fclose(outfp); 270 free(img); 271 exit(0); 272} 273