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>
49 #include <machine/param.h>
50 #include <machine/vmparam.h>
53 #include <machine/sysarch.h>
54 #include <machine/vm86.h>
68 int timer_disable = 0;
69 struct timeval boot_time;
70 unsigned long *ivec = (unsigned long *)0;
72 u_long pending[256]; /* pending interrupts */
76 #define PRB_V86_FORMAT 0x4242
78 struct vconnect_area vconnect_area = {
79 0, /* Interrupt state */
80 PRB_V86_FORMAT, /* Magic number */
81 { 0, }, /* Pass through ints */
82 { 0x00000000, 0x00000000 } /* Magic iret location */
86 /* local prototypes */
87 static void setup_boot(regcontext_t *REGS);
88 static int try_boot(int);
89 static void setup_command(int argc, char *argv[], regcontext_t *REGS);
90 static FILE *find_doscmdrc(void);
91 static int do_args(int argc, char *argv[]);
92 static void usage(void);
93 static int open_name(char *name, char *ext);
95 /* Local option flags &c. */
98 /* DOS environment emulation */
99 static unsigned ecnt = 0;
100 static char *envs[256];
102 /* Search path and command name */
103 static char *dos_path = 0;
104 char cmdname[256]; /* referenced from dos.c */
106 static struct vm86_init_args kargs;
110 main(int argc, char **argv)
115 struct vm86_struct vm86s;
116 #define sc vm86s.substr.regs.vmsc
118 regcontext_t *REGS = (regcontext_t *)&uc.uc_mcontext;
123 /* XXX should only be for tty mode */
124 fd = open (_PATH_DEVNULL, O_RDWR);
126 dup2 (fd, 3); /* stdaux */
128 dup2 (fd, 4); /* stdprt */
129 if (fd != 3 && fd != 4)
133 debug_set(0); /* debug any D_TRAPS without intnum */
135 /* perform option argument processing */
140 if (getenv("DISPLAY") != NULL)
143 if (vflag && debugf == stderr) {
145 setbuf (stdout, NULL);
150 /* This needs to happen before the executable is loaded */
154 memset(&vm86s, 0, sizeof(vm86s));
158 * With no other arguments we will assume we must boot DOS
165 * Nominate interrupts to handle here when the kernel is
166 * performing interrupt handling.
168 * I would like to let INT 2F pass through as well, but I
169 * need to get my hands on INT 2F:11 to do file redirection.
171 for (i = 0; i <= 0xff; ++i) {
176 kargs.int_map[i >> 3] |= (1 << (i & 7));
178 vconnect_area.passthru[i >> 5] &= ~(1 << (i & 0x1f));
180 vm86s.int_byuser[i >> 3] |= (1 << (i & 0x07));
186 kargs.int_map[i >> 3] &= ~(1 << (i & 7));
188 vconnect_area.passthru[i >> 5] |= (1 << (i & 0x1f));
190 vm86s.int_byuser[i >> 3] |= (1 << (i & 0x07));
197 for (i = 0; i < 256; i++)
201 if (booting) { /* are we booting? */
203 } else { /* no, load a command */
204 setup_command(argc, argv, REGS);
207 /* install signal handlers */
208 setsignal(SIGFPE, sigfpe); /* */
209 setsignal(SIGALRM, sigalrm); /* */
210 setsignal(SIGILL, sigill); /* */
211 setsignal(SIGTRAP, sigtrap); /* */
212 setsignal(SIGUSR2, sigtrace); /* */
213 setsignal(SIGINFO, sigtrace); /* */
215 setsignal(SIGURG, sigurg); /* entry from NetBSD vm86 */
217 setsignal(SIGBUS, sigbus); /* entry from FreeBSD, BSD/OS vm86 */
220 /* Call init functions */
223 init_io_port_handlers();
241 gettimeofday(&boot_time, 0);
243 if (zflag) for (;;) pause(); /* spin if requested */
247 * If we have a raw keyboard, and hence, video,
248 * sneak in a call to the video BIOS to reinit the
252 static u_char video_trampoline[] = {
254 0xB8, 0x03, 0x00, /* mov ax,00003h */
255 0xCD, 0x10, /* int 010h */
260 video_vector = insert_generic_trampoline(
261 sizeof(video_trampoline), video_trampoline);
266 PUTVEC(R_CS, R_IP, video_vector);
269 sigemptyset(&uc.uc_sigmask);
270 sigaltstack(NULL, &uc.uc_stack);
271 uc.uc_mcontext.mc_onstack = 0;
277 R_EAX = (booting || raw_kbd) ? (int)&vconnect_area : -1;
278 R_EFLAGS |= PSL_VM | PSL_VIF; /* request VM86 mode */
280 i386_vm86(VM86_INIT, &kargs);
283 debug(D_ALWAYS,"sigreturn failed : %s\n", strerror(errno));
285 vm86s.cpu_type = VCPU_586;
289 /* shouldn't get here */
290 if (vflag) dump_regs(REGS);
291 fatal ("vm86 returned (no kernel support?)\n");
303 setup_boot(regcontext_t *REGS)
305 FILE *fp; /* doscmdrc handle */
306 int fd; /* don't close this! */
308 fp = find_doscmdrc(); /* get doscmdrc */
310 fprintf(stderr, "You must have a doscmdrc to boot\n");
314 booting = read_config(fp); /* where to boot from? */
316 if (booting < 0) { /* not specified */
317 if ((fd = try_boot(booting = 0)) < 0) /* try A: */
318 fd = try_boot(booting = 2); /* try C: */
320 fd = try_boot(booting); /* do like the man says */
324 errx(1, "Failed to boot");
326 /* initialise registers for entry to bootblock */
335 R_AX = R_BX = R_CX = R_DX = R_SI = R_DI = R_BP = 0;
337 #if defined(__FreeBSD__) || defined(__NetBSD__)
339 ** init a few other context registers
349 ** try to read the boot sector from the specified disk
352 try_boot(int bootdrv)
356 fd = disk_fd(bootdrv);
357 if (fd < 0) { /* can we boot it? */
358 debug(D_DISK, "Cannot boot from %c\n", drntol(bootdrv));
363 if (read(fd, (char *)0x7c00, 512) != 512) {
364 debug(D_DISK, "Short read on boot block from %c:\n", drntol(bootdrv));
374 ** Setup to run a single command and emulate DOS
377 setup_command(int argc, char *argv[], regcontext_t *REGS)
380 u_short param[7] = {0, 0, 0, 0, 0, 0, 0};
383 char buffer[PATH_MAX];
387 fp = find_doscmdrc(); /* dig up a doscmdrc */
389 read_config(fp); /* load config for non-boot mode */
393 if (argc <= 0) /* need some arguments */
396 /* look for a working directory XXX ??? */
397 if (dos_getcwd(drlton('C')) == NULL) {
399 /* try to get our current directory, use '/' if desperate */
400 p = getcwd(buffer, sizeof(buffer));
401 if (!p || !*p) p = getenv("PWD");
402 if (!p || !*p) p = "/";
403 init_path(drlton('C'), (u_char *)"/", (u_char *)p);
405 /* look for PATH= already set, learn from it if possible */
406 for (i = 0; i < ecnt; ++i) {
407 if (!strncmp(envs[i], "PATH=", 5)) {
408 dos_path = envs[i] + 5;
412 /* no PATH in DOS environment? put current directory there*/
414 static char path[256];
415 snprintf(path, sizeof(path), "PATH=C:%s", dos_getcwd(drlton('C')));
417 dos_path = envs[ecnt-1] + 5;
421 /* add a COMSPEC if required */
422 for (i = 0; i < ecnt; ++i) {
423 if (!strncmp(envs[i], "COMSPEC=", 8))
427 put_dosenv("COMSPEC=C:\\COMMAND.COM");
429 /* look for PATH already set, learn from it if possible */
430 for (i = 0; i < ecnt; ++i) {
431 if (!strncmp(envs[i], "PATH=", 5)) {
432 dos_path = envs[i] + 5;
436 /* No PATH, default to c:\ */
438 put_dosenv("PATH=C:\\");
439 dos_path = envs[ecnt-1] + 5;
442 /* if no PROMPT, default to 'DOS>' */
443 for (i = 0; i < ecnt; ++i) {
444 if (!strncmp(envs[i], "PROMPT=", 7))
448 put_dosenv("PROMPT=DOS> ");
450 /* terminate environment */
454 if (dos_getcwd(drlton('R')) == NULL)
455 init_path(drlton('R'), (u_char *)"/", 0);
457 /* get program name */
458 strncpy(prog, *argv++, sizeof(prog) -1);
459 prog[sizeof(prog) -1] = '\0';
461 /* try to open program */
462 if ((fd = open_prog(prog)) < 0) {
463 fprintf (stderr, "%s: command not found\n", prog);
468 load_command(REGS, 1, fd, cmdname, param, argv, envs);
475 ** Try to find a doscmdrc file
483 if ((fp = fopen(".doscmdrc", "r")) == NULL) {
484 struct passwd *pwd = getpwuid(geteuid());
486 snprintf(buffer, sizeof(buffer), "%s/.doscmdrc", pwd->pw_dir);
487 fp = fopen(buffer, "r");
490 char *home = getenv("HOME");
492 snprintf(buffer, sizeof(buffer), "%s/.doscmdrc", home);
493 fp = fopen(buffer, "r");
497 fp = fopen("/etc/doscmdrc", "r");
505 ** commandline argument processing
508 do_args(int argc, char *argv[])
514 while ((c = getopt(argc, argv, "234AbCc:Dd:EGHIi:kLMOo:Pp:RrS:TtU:vVxXYz")) != -1) {
517 debug_flags |= D_TRAPS2;
520 debug_flags |= D_TRAPS3;
523 debug_flags |= D_DEBUGIN;
526 debug_flags |= D_TRAPS | D_ITRAPS;
527 for (c = 0; c < 256; ++c)
534 debug_flags |= D_DOSCALL;
537 if ((capture_fd = creat(optarg, 0666)) < 0) {
543 debug_flags |= D_DISK | D_FILE_OPS;
546 if ((fp = fopen(optarg, "w")) != 0) {
553 debug_flags |= D_EXEC;
556 debug_flags |= D_VIDEO;
559 debug_flags |= D_HALF;
562 debug_flags |= D_ITRAPS;
563 for (c = 0; c < 256; ++c)
568 if ((col = strchr(optarg, ':')) != 0) {
570 i = strtol(col, 0, 0);
572 p = strtol(optarg, 0, 0);
576 define_input_port_handler(p++, inb_traceport);
582 debug_flags |= D_PRINTER;
585 debug_flags |= D_MEMORY;
589 setbuf (stdout, NULL);
593 if ((col = strchr(optarg, ':')) != 0) {
595 i = strtol(col, 0, 0);
597 p = strtol(optarg, 0, 0);
601 define_output_port_handler(p++, outb_traceport);
604 debug_flags |= D_PORT;
608 if ((col = strchr(optarg, ':')) != 0) {
610 i = strtol(col, 0, 0);
612 p = strtol(optarg, 0, 0);
616 define_input_port_handler(p++, inb_port);
617 define_output_port_handler(p++, outb_port);
621 debug_flags |= D_REDIR;
627 debug_flags |= D_TRAPS | D_ITRAPS;
628 debug_set(strtol(optarg, 0, 0));
637 debug_unset(strtol(optarg, 0, 0));
643 debug_flags |= D_TRAPS | D_ITRAPS | D_HALF | 0xff;
646 debug_flags |= D_XMS;
650 fatal("X11 support not compiled in.\n");
655 debug_flags |= D_EMS;
673 fprintf (stderr, "usage: doscmd cmd args...\n");
678 ** look up a DOS command name
680 ** XXX ordering is wrong!
683 open_name(char *name, char *ext)
686 char *p = name + strlen(name);
691 q = strrchr(name, '/');
697 if (!strchr(q, '.')) {
701 if ((fd = open (name, O_RDONLY)) >= 0)
707 if ((fd = open (name, O_RDONLY)) >= 0)
710 if ((fd = open (name, O_RDONLY)) >= 0)
718 ** look up a DOS command, search the path as well.
721 open_prog(char *name)
724 char fullname[1024], tmppath[1024];
732 if (strpbrk(name, ":/\\")) {
733 error = translate_filename(name, fullname, &drive);
737 fd = open_name(fullname, ext);
739 strcpy(cmdname, name);
741 strcat(cmdname, ext);
749 while (*p && *p != ';')
752 memcpy(tmppath, path, p - path);
753 e = tmppath + (p - path);
757 path = *p ? p + 1 : p;
759 error = translate_filename(tmppath, fullname, &drive);
763 fd = open_name(fullname, ext);
766 strcpy(cmdname, tmppath);
768 strcat(cmdname, ext);
777 ** append a value to the DOS environment
780 put_dosenv(const char *value)
782 if (ecnt < sizeof(envs)/sizeof(envs[0])) {
783 if ((envs[ecnt++] = strdup(value)) == NULL) {
784 perror("put_dosenv");
788 fprintf(stderr, "Environment full, ignoring %s\n", value);
793 ** replicate a fd up at the top of the range
798 int sfd = sysconf(_SC_OPEN_MAX);
804 } while (sfd > 0 && errno != EBADF);
806 if (errno == EBADF && dup2(fd, sfd) >= 0) {
820 ** XXX belongs somewhere else perhaps
823 done(regcontext_t *REGS, int val)
830 for (m = "END OF PROGRAM"; *m; ++m)
831 tty_write(*m, 0x8400);
833 for (m = "(PRESS <CTRL-ALT> ANY MOUSE BUTTON TO exit)"; *m; ++m)
834 tty_write(*m, 0x0900);
842 exec_return(REGS, val);
846 void (*func)(void *);
861 if (!xmode) /* XXX not for bootmode */
867 call_on_quit(void (*func)(void *), void *arg)
869 COQ *c = (COQ *)malloc(sizeof(COQ));
871 perror("call_on_quit");
886 /* This is commented out as it is never called. Turn it back on if needed.
893 struct io_range io[] = {
895 { 0x200, 0x200, 1 }, /* 0x200 - 0x400 */
896 { 0x1c80, 2, 1 }, /* 0x1c80 - 0x1c81 */
897 { 0x2c80, 2, 1 }, /* 0x2c80 - 0x2c81 */
898 { 0x3c80, 2, 1 }, /* 0x3c80 - 0x3c81 */
899 { 0x378, 8, 1 }, /* 0x378 - 0x37F */
900 { 0x3c4, 2, 1 }, /* 0x3c4 - 0x3c5 */
901 { 0x3c5, 2, 1 }, /* 0x3ce - 0x3cf */
903 { 0x0, 0x10000, 1 }, /* entire i/o space */
908 for (i = 0; io[i].length; i++)
909 if (i386_set_ioperm(io[i].start, io[i].length, io[i].enable) < 0)
910 err(1, "i386_set_ioperm");
914 /* This is used to map in only the specified port range, instead of all
915 the ports or only certain port ranges.
918 iomap_port(int port, int count)
920 if (i386_set_ioperm(port, count, 1) < 0)
921 err(1, "i386_set_ioperm");
923 debug(D_PORT,"mapped I/O port: port=%#x count=%d\n", port, count);