ndiscvt.c (126706) | ndiscvt.c (132973) |
---|---|
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 --- 17 unchanged lines hidden (view full) --- 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> | 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 --- 17 unchanged lines hidden (view full) --- 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 126706 2004-03-07 02:49:06Z wpaul $"); | 34__FBSDID("$FreeBSD: head/usr.sbin/ndiscvt/ndiscvt.c 132973 2004-08-01 20:04:31Z 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> | 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> |
|
45#include <err.h> | 46#include <err.h> |
47#include <ctype.h> |
|
46 47#include <compat/ndis/pe_var.h> 48 49#include "inf.h" 50 51static int insert_padding(void **, int *); 52extern const char *__progname; 53 --- 98 unchanged lines hidden (view full) --- 152 *imglen += offaccum; 153 154 return(0); 155} 156 157static void 158usage(void) 159{ | 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 --- 98 unchanged lines hidden (view full) --- 154 *imglen += offaccum; 155 156 return(0); 157} 158 159static void 160usage(void) 161{ |
160 fprintf(stderr, "Usage: %s [-i | 162 fprintf(stderr, "Usage: %s [-O] [-i <inffile>] -s <sysfile> " |
161 "[-n devname] [-o outfile]\n", __progname); | 163 "[-n devname] [-o outfile]\n", __progname); |
164 fprintf(stderr, " %s -f <firmfile>\n", __progname); 165 |
|
162 exit(1); 163} 164 | 166 exit(1); 167} 168 |
169static void 170bincvt(char *sysfile, char *outfile, void *img, int fsize) 171{ 172 char *ptr; 173 char tname[] = "/tmp/ndiscvt.XXXXXX"; 174 char sysbuf[1024]; 175 FILE *binfp; 176 177 mkstemp(tname); 178 179 binfp = fopen(tname, "a+"); 180 if (binfp == NULL) 181 err(1, "opening %s failed", tname); 182 183 if (fwrite(img, fsize, 1, binfp) != 1) 184 err(1, "failed to output binary image"); 185 186 fclose(binfp); 187 188 outfile = strdup(basename(outfile)); 189 if (strchr(outfile, '.')) 190 *strchr(outfile, '.') = '\0'; 191 192 snprintf(sysbuf, sizeof(sysbuf), 193 "objcopy -I binary -O elf32-i386-freebsd -B i386 %s %s.o\n", 194 tname, outfile); 195 printf("%s", sysbuf); 196 system(sysbuf); 197 unlink(tname); 198 199 ptr = tname; 200 while (*ptr) { 201 if (*ptr == '/' || *ptr == '.') 202 *ptr = '_'; 203 ptr++; 204 } 205 206 snprintf(sysbuf, sizeof(sysbuf), 207 "objcopy --redefine-sym _binary_%s_start=%s_drv_data_start " 208 "--strip-symbol _binary_%s_size " 209 "--redefine-sym _binary_%s_end=%s_drv_data_end %s.o %s.o\n", 210 tname, sysfile, tname, tname, sysfile, outfile, outfile); 211 printf("%s", sysbuf); 212 system(sysbuf); 213 214 return; 215} 216 217static void 218firmcvt(char *firmfile) 219{ 220 char *basefile, *outfile, *ptr; 221 char sysbuf[1024]; 222 223 outfile = basename(firmfile); 224 basefile = strdup(outfile); 225 226 snprintf(sysbuf, sizeof(sysbuf), 227 "objcopy -I binary -O elf32-i386-freebsd -B i386 %s %s.o\n", 228 firmfile, outfile); 229 printf("%s", sysbuf); 230 system(sysbuf); 231 232 ptr = firmfile; 233 while (*ptr) { 234 if (*ptr == '/' || *ptr == '.') 235 *ptr = '_'; 236 ptr++; 237 } 238 ptr = basefile; 239 while (*ptr) { 240 if (*ptr == '/' || *ptr == '.') 241 *ptr = '_'; 242 else 243 *ptr = tolower(*ptr); 244 ptr++; 245 } 246 247 snprintf(sysbuf, sizeof(sysbuf), 248 "objcopy --redefine-sym _binary_%s_start=%s_start " 249 "--strip-symbol _binary_%s_size " 250 "--redefine-sym _binary_%s_end=%s_end %s.o %s.o\n", 251 firmfile, basefile, firmfile, firmfile, 252 basefile, outfile, outfile); 253 ptr = sysbuf; 254 printf("%s", sysbuf); 255 system(sysbuf); 256 257 snprintf(sysbuf, sizeof(sysbuf), 258 "ld -Bshareable -d -warn-common -o %s.ko %s.o\n", 259 outfile, outfile); 260 printf("%s", sysbuf); 261 system(sysbuf); 262 263 free(basefile); 264 265 exit(0); 266} 267 |
|
165int 166main(int argc, char *argv[]) 167{ | 268int 269main(int argc, char *argv[]) 270{ |
168 FILE *fp, *outfp; 169 void *img; 170 int n, fsize, cnt; 171 unsigned char *ptr; 172 int i; 173 char *inffile = NULL, *sysfile = NULL, *outfile = NULL; 174 char *dname = NULL; 175 int ch; | 271 FILE *fp, *outfp; 272 int i, bin = 0; 273 void *img; 274 int n, fsize, cnt; 275 unsigned char *ptr; 276 char *inffile = NULL, *sysfile = NULL; 277 char *outfile = NULL, *firmfile = NULL; 278 char *dname = NULL; 279 int ch; |
176 | 280 |
177 while((ch = getopt(argc, argv, "i:s:o:n:")) != -1) { | 281 while((ch = getopt(argc, argv, "i:s:o:n:f:O")) != -1) { |
178 switch(ch) { | 282 switch(ch) { |
283 case 'f': 284 firmfile = optarg; 285 break; |
|
179 case 'i': 180 inffile = optarg; 181 break; 182 case 's': 183 sysfile = optarg; 184 break; 185 case 'o': 186 outfile = optarg; 187 break; 188 case 'n': 189 dname = optarg; 190 break; | 286 case 'i': 287 inffile = optarg; 288 break; 289 case 's': 290 sysfile = optarg; 291 break; 292 case 'o': 293 outfile = optarg; 294 break; 295 case 'n': 296 dname = optarg; 297 break; |
298 case 'O': 299 bin = 1; 300 break; |
|
191 default: 192 usage(); 193 break; 194 } 195 } 196 | 301 default: 302 usage(); 303 break; 304 } 305 } 306 |
307 if (firmfile != NULL) 308 firmcvt(firmfile); 309 |
|
197 if (sysfile == NULL) 198 usage(); 199 200 /* Open the .SYS file and load it into memory */ 201 fp = fopen(sysfile, "r"); 202 if (fp == NULL) 203 err(1, "opening .SYS file '%s' failed", sysfile); 204 fseek (fp, 0L, SEEK_END); --- 43 unchanged lines hidden (view full) --- 248 err(1, "opening .INF file '%s' failed", inffile); 249 250 251 inf_parse(fp, outfp); 252 fclose(fp); 253 } 254 255 fprintf(outfp, "\n#ifdef NDIS_IMAGE\n"); | 310 if (sysfile == NULL) 311 usage(); 312 313 /* Open the .SYS file and load it into memory */ 314 fp = fopen(sysfile, "r"); 315 if (fp == NULL) 316 err(1, "opening .SYS file '%s' failed", sysfile); 317 fseek (fp, 0L, SEEK_END); --- 43 unchanged lines hidden (view full) --- 361 err(1, "opening .INF file '%s' failed", inffile); 362 363 364 inf_parse(fp, outfp); 365 fclose(fp); 366 } 367 368 fprintf(outfp, "\n#ifdef NDIS_IMAGE\n"); |
369 370 if (bin) { 371 sysfile = strdup(basename(sysfile)); 372 ptr = sysfile; 373 while (*ptr) { 374 if (*ptr == '.') 375 *ptr = '_'; 376 ptr++; 377 } 378 fprintf(outfp, 379 "\nextern unsigned char %s_drv_data_start[];\n", 380 sysfile); 381 fprintf(outfp, "static unsigned char *drv_data = " 382 "%s_drv_data_start;\n\n", sysfile); 383 bincvt(sysfile, outfile, img, fsize); 384 goto done; 385 } 386 387 |
|
256 fprintf(outfp, "\nextern unsigned char drv_data[];\n\n"); 257 258 fprintf(outfp, "__asm__(\".data\");\n"); 259 fprintf(outfp, "__asm__(\".type drv_data, @object\");\n"); 260 fprintf(outfp, "__asm__(\".size drv_data, %d\");\n", fsize); 261 fprintf(outfp, "__asm__(\"drv_data:\");\n"); 262 263 ptr = img; --- 13 unchanged lines hidden (view full) --- 277 } 278 } 279 ptr += 10; 280 } 281 282done: 283 284 fprintf(outfp, "#endif /* NDIS_IMAGE */\n"); | 388 fprintf(outfp, "\nextern unsigned char drv_data[];\n\n"); 389 390 fprintf(outfp, "__asm__(\".data\");\n"); 391 fprintf(outfp, "__asm__(\".type drv_data, @object\");\n"); 392 fprintf(outfp, "__asm__(\".size drv_data, %d\");\n", fsize); 393 fprintf(outfp, "__asm__(\"drv_data:\");\n"); 394 395 ptr = img; --- 13 unchanged lines hidden (view full) --- 409 } 410 } 411 ptr += 10; 412 } 413 414done: 415 416 fprintf(outfp, "#endif /* NDIS_IMAGE */\n"); |
417 |
|
285 if (fp != NULL) 286 fclose(fp); 287 fclose(outfp); 288 free(img); 289 exit(0); 290} | 418 if (fp != NULL) 419 fclose(fp); 420 fclose(outfp); 421 free(img); 422 exit(0); 423} |