2 * Copyright (c) 1992, 1993, 1996
3 * Berkeley Software Design, Inc. 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 Berkeley Software
18 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * BSDI doscmd.c,v 2.3 1996/04/08 19:32:30 bostic Exp
35 #include <sys/types.h>
36 #include <sys/param.h>
47 #include <machine/param.h>
48 #include <machine/vmparam.h>
51 #include <machine/sysarch.h>
52 #include <machine/vm86.h>
62 int timer_disable = 0;
63 struct timeval boot_time;
64 unsigned long *ivec = (unsigned long *)0;
66 u_long pending[256]; /* pending interrupts */
70 #define PRB_V86_FORMAT 0x4242
72 struct vconnect_area vconnect_area = {
73 0, /* Interrupt state */
74 PRB_V86_FORMAT, /* Magic number */
75 { 0, }, /* Pass through ints */
76 { 0x00000000, 0x00000000 } /* Magic iret location */
80 /* local prototypes */
81 static void setup_boot(regcontext_t *REGS);
82 static int try_boot(int);
83 static void setup_command(int argc, char *argv[], regcontext_t *REGS);
84 static FILE *find_doscmdrc(void);
85 static int do_args(int argc, char *argv[]);
86 static void usage(void);
87 static int open_name(char *name, char *ext);
88 static void init_iomap(void);
90 /* Local option flags &c. */
93 /* DOS environment emulation */
95 static char *envs[256];
97 /* Search path and command name */
98 static char *dos_path = 0;
99 char cmdname[256]; /* referenced from dos.c */
101 static struct vm86_init_args kargs;
105 main(int argc, char **argv)
110 struct vm86_struct vm86s;
111 #define sc vm86s.substr.regs.vmsc
113 regcontext_t *REGS = (regcontext_t *)&uc.uc_mcontext;
120 /* XXX should only be for tty mode */
121 fd = open (_PATH_DEVNULL, O_RDWR);
123 dup2 (fd, 3); /* stdaux */
125 dup2 (fd, 4); /* stdprt */
126 if (fd != 3 && fd != 4)
130 debug_set(0); /* debug any D_TRAPS without intnum */
132 /* perform option argument processing */
137 if (getenv("DISPLAY") != NULL)
140 if (vflag && debugf == stderr) {
142 setbuf (stdout, NULL);
147 /* This needs to happen before the executable is loaded */
151 memset(&vm86s, 0, sizeof(vm86s));
155 * With no other arguments we will assume we must boot DOS
162 * Nominate interrupts to handle here when the kernel is
163 * performing interrupt handling.
165 * I would like to let INT 2F pass through as well, but I
166 * need to get my hands on INT 2F:11 to do file redirection.
168 for (i = 0; i <= 0xff; ++i) {
173 kargs.int_map[i >> 3] |= (1 << (i & 7));
175 vconnect_area.passthru[i >> 5] &= ~(1 << (i & 0x1f));
177 vm86s.int_byuser[i >> 3] |= (1 << (i & 0x07));
183 kargs.int_map[i >> 3] &= ~(1 << (i & 7));
185 vconnect_area.passthru[i >> 5] |= (1 << (i & 0x1f));
187 vm86s.int_byuser[i >> 3] |= (1 << (i & 0x07));
194 for (i = 0; i < 256; i++)
198 if (booting) { /* are we booting? */
200 } else { /* no, load a command */
201 setup_command(argc, argv, REGS);
204 /* install signal handlers */
205 setsignal (SIGFPE, sigfpe); /* */
206 setsignal (SIGALRM, sigalrm); /* */
207 setsignal (SIGILL, sigill); /* */
208 setsignal (SIGTRAP, sigtrap); /* */
209 setsignal (SIGUSR2, sigtrace); /* */
210 setsignal (SIGINFO, sigtrace); /* */
212 setsignal (SIGURG, sigurg); /* entry from NetBSD vm86 */
214 setsignal (SIGBUS, sigbus); /* entry from FreeBSD, BSD/OS vm86 */
217 /* Call init functions */
220 init_io_port_handlers();
236 gettimeofday(&boot_time, 0);
238 if (zflag) for (;;) pause(); /* spin if requested */
242 * If we have a raw keyboard, and hence, video,
243 * sneak in a call to the video BIOS to reinit the
247 static u_char video_trampoline[] = {
249 0xB8, 0x03, 0x00, /* mov ax,00003h */
250 0xCD, 0x10, /* int 010h */
255 video_vector = insert_generic_trampoline(
256 sizeof(video_trampoline), video_trampoline);
261 PUTVEC(R_CS, R_IP, video_vector);
264 sigemptyset(&uc.uc_sigmask);
265 sigaltstack(NULL, &uc.uc_stack);
266 uc.uc_mcontext.mc_onstack = 0;
272 R_EAX = (booting || raw_kbd) ? (int)&vconnect_area : -1;
273 R_EFLAGS |= PSL_VM | PSL_VIF; /* request VM86 mode */
275 i386_vm86(VM86_INIT, &kargs);
278 debug(D_ALWAYS,"sigreturn failed : %s\n", strerror(errno));
280 vm86s.cpu_type = VCPU_586;
284 /* shouldn't get here */
285 if (vflag) dump_regs(REGS);
286 fatal ("vm86 returned (no kernel support?)\n");
296 setup_boot(regcontext_t *REGS)
298 FILE *fp; /* doscmdrc handle */
299 int fd; /* don't close this! */
301 fp = find_doscmdrc(); /* get doscmdrc */
303 fprintf(stderr, "You must have a doscmdrc to boot\n");
307 booting = read_config(fp); /* where to boot from? */
309 if (booting < 0) { /* not specified */
310 if ((fd = try_boot(booting = 0)) < 0) /* try A: */
311 fd = try_boot(booting = 2); /* try C: */
313 fd = try_boot(booting); /* do like the man says */
317 errx(1, "Failed to boot");
319 /* initialise registers for entry to bootblock */
328 R_AX = R_BX = R_CX = R_DX = R_SI = R_DI = R_BP = 0;
330 #if defined(__FreeBSD__) || defined(__NetBSD__)
332 ** init a few other context registers
342 ** try to read the boot sector from the specified disk
345 try_boot(int booting)
349 fd = disk_fd(booting);
350 if (fd < 0) { /* can we boot it? */
351 debug(D_DISK, "Cannot boot from %c\n", drntol(booting));
356 if (read(fd, (char *)0x7c00, 512) != 512) {
357 debug(D_DISK, "Short read on boot block from %c:\n", drntol(booting));
367 ** Setup to run a single command and emulate DOS
370 setup_command(int argc, char *argv[], regcontext_t *REGS)
373 u_short param[7] = {0, 0, 0, 0, 0, 0, 0};
376 char buffer[PATH_MAX];
380 fp = find_doscmdrc(); /* dig up a doscmdrc */
382 read_config(fp); /* load config for non-boot mode */
386 if (argc <= 0) /* need some arguments */
389 /* look for a working directory XXX ??? */
390 if (dos_getcwd(drlton('C')) == NULL) {
392 /* try to get our current directory, use '/' if desperate */
393 p = getcwd(buffer, sizeof(buffer));
394 if (!p || !*p) p = getenv("PWD");
395 if (!p || !*p) p = "/";
396 init_path(drlton('C'), (u_char *)"/", (u_char *)p);
398 /* look for PATH= already set, learn from it if possible */
399 for (i = 0; i < ecnt; ++i) {
400 if (!strncmp(envs[i], "PATH=", 5)) {
401 dos_path = envs[i] + 5;
405 /* no PATH in DOS environment? put current directory there*/
407 static char path[256];
408 snprintf(path, sizeof(path), "PATH=C:%s", dos_getcwd(drlton('C')));
410 dos_path = envs[ecnt-1] + 5;
414 /* add a COMSPEC if required */
415 for (i = 0; i < ecnt; ++i) {
416 if (!strncmp(envs[i], "COMSPEC=", 8))
420 put_dosenv("COMSPEC=C:\\COMMAND.COM");
422 /* look for PATH already set, learn from it if possible */
423 for (i = 0; i < ecnt; ++i) {
424 if (!strncmp(envs[i], "PATH=", 5)) {
425 dos_path = envs[i] + 5;
429 /* No PATH, default to c:\ */
431 put_dosenv("PATH=C:\\");
432 dos_path = envs[ecnt-1] + 5;
435 /* if no PROMPT, default to 'DOS>' */
436 for (i = 0; i < ecnt; ++i) {
437 if (!strncmp(envs[i], "PROMPT=", 7))
441 put_dosenv("PROMPT=DOS> ");
443 /* terminate environment */
447 if (dos_getcwd(drlton('R')) == NULL)
448 init_path(drlton('R'), (u_char *)"/", 0);
450 /* get program name */
451 strncpy(prog, *argv++, sizeof(prog) -1);
452 prog[sizeof(prog) -1] = '\0';
454 /* try to open program */
455 if ((fd = open_prog(prog)) < 0) {
456 fprintf (stderr, "%s: command not found\n", prog);
461 load_command(REGS, 1, fd, cmdname, param, argv, envs);
468 ** Try to find a doscmdrc file
477 if ((fp = fopen(".doscmdrc", "r")) == NULL) {
478 struct passwd *pwd = getpwuid(geteuid());
480 snprintf(buffer, sizeof(buffer), "%s/.doscmdrc", pwd->pw_dir);
481 fp = fopen(buffer, "r");
484 char *home = getenv("HOME");
486 snprintf(buffer, sizeof(buffer), "%s/.doscmdrc", home);
487 fp = fopen(buffer, "r");
491 fp = fopen("/etc/doscmdrc", "r");
499 ** commandline argument processing
502 do_args(int argc, char *argv[])
508 while ((c = getopt (argc, argv, "234Oc:TkCIEMPRLAU:S:HDtzvVxXYfbri:o:p:d:")) != -1) {
511 if (fp = fopen(optarg, "w")) {
518 debug_flags |= D_TRAPS2;
521 debug_flags |= D_TRAPS3;
524 debug_flags |= D_DEBUGIN;
528 setbuf (stdout, NULL);
531 if ((capture_fd = creat(optarg, 0666)) < 0) {
538 if (col = strchr(optarg, ':')) {
540 i = strtol(col, 0, 0);
542 p = strtol(optarg, 0, 0);
546 define_input_port_handler(p++, inb_traceport);
550 if (col = strchr(optarg, ':')) {
552 i = strtol(col, 0, 0);
554 p = strtol(optarg, 0, 0);
558 define_output_port_handler(p++, outb_traceport);
562 if (col = strchr(optarg, ':')) {
564 i = strtol(col, 0, 0);
566 p = strtol(optarg, 0, 0);
570 define_input_port_handler(p++, inb_port);
571 define_output_port_handler(p++, outb_port);
579 debug_flags |= D_ITRAPS;
580 for (c = 0; c < 256; ++c)
590 debug_flags |= D_EXEC;
593 debug_flags |= D_DOSCALL;
596 debug_flags |= D_MEMORY;
599 debug_flags |= D_PORT;
602 debug_flags |= D_REDIR;
605 debug_flags |= D_XMS;
608 debug_flags |= D_EMS;
611 debug_flags |= D_PRINTER;
614 debug_flags |= D_TRAPS|D_ITRAPS;
615 for (c = 0; c < 256; ++c)
619 debug_unset(strtol(optarg, 0, 0));
622 debug_flags |= D_TRAPS|D_ITRAPS;
623 debug_set(strtol(optarg, 0, 0));
626 debug_flags |= D_HALF;
630 fatal("X11 support not compiled in.\n");
641 debug_flags |= D_DISK | D_FILE_OPS;
644 debug_flags |= D_TRAPS | D_ITRAPS | D_HALF | 0xff;
665 fprintf (stderr, "usage: doscmd cmd args...\n");
670 ** look up a DOS command name
672 ** XXX ordering is wrong!
675 open_name(char *name, char *ext)
678 char *p = name + strlen(name);
683 q = strrchr(name, '/');
689 if (!strchr(q, '.')) {
693 if ((fd = open (name, O_RDONLY)) >= 0)
699 if ((fd = open (name, O_RDONLY)) >= 0)
702 if ((fd = open (name, O_RDONLY)) >= 0)
710 ** look up a DOS command, search the path as well.
713 open_prog(char *name)
716 char fullname[1024], tmppath[1024];
724 if (strpbrk(name, ":/\\")) {
725 error = translate_filename(name, fullname, &drive);
729 fd = open_name(fullname, ext);
731 strcpy(cmdname, name);
733 strcat(cmdname, ext);
741 while (*p && *p != ';')
744 memcpy(tmppath, path, p - path);
745 e = tmppath + (p - path);
749 path = *p ? p + 1 : p;
751 error = translate_filename(tmppath, fullname, &drive);
755 fd = open_name(fullname, ext);
758 strcpy(cmdname, tmppath);
760 strcat(cmdname, ext);
769 ** append a value to the DOS environment
772 put_dosenv(char *value)
774 if (ecnt < sizeof(envs)/sizeof(envs[0])) {
775 if ((envs[ecnt++] = strdup(value)) == NULL) {
776 perror("put_dosenv");
780 fprintf(stderr, "Environment full, ignoring %s\n", value);
785 ** replicate a fd up at the top of the range
790 int sfd = sysconf(_SC_OPEN_MAX);
796 } while (sfd > 0 && errno != EBADF);
798 if (errno == EBADF && dup2(fd, sfd) >= 0) {
812 ** XXX belongs somewhere else perhaps
815 done (regcontext_t *REGS, int val)
822 for (m = "END OF PROGRAM"; *m; ++m)
823 tty_write(*m, 0x8400);
825 for (m = "(PRESS <CTRL-ALT> ANY MOUSE BUTTON TO exit)"; *m; ++m)
826 tty_write(*m, 0x0900);
834 exec_return(REGS, val);
853 if (!xmode) /* XXX not for bootmode */
859 call_on_quit(void (*func)(void *), void *arg)
861 COQ *c = (COQ *)malloc(sizeof(COQ));
863 perror("call_on_quit");
878 /* This is commented out as it is never called. Turn it back on if needed.
885 struct io_range io[] = {
887 { 0x200, 0x200, 1 }, /* 0x200 - 0x400 */
888 { 0x1c80, 2, 1 }, /* 0x1c80 - 0x1c81 */
889 { 0x2c80, 2, 1 }, /* 0x2c80 - 0x2c81 */
890 { 0x3c80, 2, 1 }, /* 0x3c80 - 0x3c81 */
891 { 0x378, 8, 1 }, /* 0x378 - 0x37F */
892 { 0x3c4, 2, 1 }, /* 0x3c4 - 0x3c5 */
893 { 0x3c5, 2, 1 }, /* 0x3ce - 0x3cf */
895 { 0x0, 0x10000, 1 }, /* entire i/o space */
900 for (i = 0; io[i].length; i++)
901 if (i386_set_ioperm(io[i].start, io[i].length, io[i].enable) < 0)
902 err(1, "i386_set_ioperm");
906 /* This is used to map in only the specified port range, instead of all
907 the ports or only certain port ranges.
910 iomap_port(int port, int count)
912 if (i386_set_ioperm(port, count, 1) < 0)
913 err(1, "i386_set_ioperm");
915 debug(D_PORT,"mapped I/O port: port=%#x count=%d\n", port, count);