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;
73 #define PRB_V86_FORMAT 0x4242
75 struct vconnect_area vconnect_area = {
76 0, /* Interrupt state */
77 PRB_V86_FORMAT, /* Magic number */
78 { 0, }, /* Pass through ints */
79 { 0x00000000, 0x00000000 } /* Magic iret location */
83 /* local prototypes */
84 static void setup_boot(regcontext_t *REGS);
85 static int try_boot(int);
86 static void setup_command(int argc, char *argv[], regcontext_t *REGS);
87 static FILE *find_doscmdrc(void);
88 static int do_args(int argc, char *argv[]);
89 static void usage(void);
90 static int open_name(char *name, char *ext);
92 /* Local option flags &c. */
95 /* DOS environment emulation */
96 static unsigned ecnt = 0;
97 static char *envs[256];
99 /* Search path and command name */
100 static char *dos_path = 0;
101 char cmdname[256]; /* referenced from dos.c */
103 static struct vm86_init_args kargs;
107 main(int argc, char **argv)
112 struct vm86_struct vm86s;
113 #define sc vm86s.substr.regs.vmsc
115 regcontext_t *REGS = (regcontext_t *)&uc.uc_mcontext;
120 sigemptyset(&sigset);
121 sigaddset(&sigset, SIGIO);
122 sigaddset(&sigset, SIGALRM);
123 sigprocmask(SIG_BLOCK, &sigset, 0);
128 /* XXX should only be for tty mode */
129 fd = open (_PATH_DEVNULL, O_RDWR);
131 dup2 (fd, 3); /* stdaux */
133 dup2 (fd, 4); /* stdprt */
134 if (fd != 3 && fd != 4)
138 debug_set(0); /* debug any D_TRAPS without intnum */
140 /* perform option argument processing */
145 if (getenv("DISPLAY") != NULL)
148 if (vflag && debugf == stderr) {
150 setbuf (stdout, NULL);
155 /* This needs to happen before the executable is loaded */
159 memset(&vm86s, 0, sizeof(vm86s));
163 * With no other arguments we will assume we must boot DOS
170 * Nominate interrupts to handle here when the kernel is
171 * performing interrupt handling.
173 * I would like to let INT 2F pass through as well, but I
174 * need to get my hands on INT 2F:11 to do file redirection.
176 for (i = 0; i <= 0xff; ++i) {
181 kargs.int_map[i >> 3] |= (1 << (i & 7));
183 vconnect_area.passthru[i >> 5] &= ~(1 << (i & 0x1f));
185 vm86s.int_byuser[i >> 3] |= (1 << (i & 0x07));
191 kargs.int_map[i >> 3] &= ~(1 << (i & 7));
193 vconnect_area.passthru[i >> 5] |= (1 << (i & 0x1f));
195 vm86s.int_byuser[i >> 3] |= (1 << (i & 0x07));
203 if (booting) { /* are we booting? */
205 } else { /* no, load a command */
206 setup_command(argc, argv, REGS);
209 /* install signal handlers */
210 setsignal(SIGFPE, sigfpe); /* */
211 setsignal(SIGALRM, sigalrm); /* */
212 setsignal(SIGILL, sigill); /* */
213 setsignal(SIGTRAP, sigtrap); /* */
214 setsignal(SIGUSR2, sigtrace); /* */
215 setsignal(SIGINFO, sigtrace); /* */
217 setsignal(SIGURG, sigurg); /* entry from NetBSD vm86 */
219 setsignal(SIGBUS, sigbus); /* entry from FreeBSD, BSD/OS vm86 */
222 /* Call init functions */
225 init_io_port_handlers();
243 gettimeofday(&boot_time, 0);
245 if (zflag) for (;;) pause(); /* spin if requested */
249 * If we have a raw keyboard, and hence, video,
250 * sneak in a call to the video BIOS to reinit the
254 static u_char video_trampoline[] = {
256 0xB8, 0x03, 0x00, /* mov ax,00003h */
257 0xCD, 0x10, /* int 010h */
262 video_vector = insert_generic_trampoline(
263 sizeof(video_trampoline), video_trampoline);
268 PUTVEC(R_CS, R_IP, video_vector);
271 sigemptyset(&uc.uc_sigmask);
272 sigaltstack(NULL, &uc.uc_stack);
273 uc.uc_mcontext.mc_onstack = 0;
279 R_EAX = (booting || raw_kbd) ? (int)&vconnect_area : -1;
280 R_EFLAGS |= PSL_VM | PSL_VIF; /* request VM86 mode */
282 i386_vm86(VM86_INIT, &kargs);
285 debug(D_ALWAYS,"sigreturn failed : %s\n", strerror(errno));
287 vm86s.cpu_type = VCPU_586;
291 /* shouldn't get here */
292 if (vflag) dump_regs(REGS);
293 fatal ("vm86 returned (no kernel support?)\n");
305 setup_boot(regcontext_t *REGS)
307 FILE *fp; /* doscmdrc handle */
308 int fd; /* don't close this! */
310 fp = find_doscmdrc(); /* get doscmdrc */
312 fprintf(stderr, "You must have a doscmdrc to boot\n");
316 booting = read_config(fp); /* where to boot from? */
318 if (booting < 0) { /* not specified */
319 if ((fd = try_boot(booting = 0)) < 0) /* try A: */
320 fd = try_boot(booting = 2); /* try C: */
322 fd = try_boot(booting); /* do like the man says */
326 errx(1, "Failed to boot");
328 /* initialise registers for entry to bootblock */
337 R_AX = R_BX = R_CX = R_DX = R_SI = R_DI = R_BP = 0;
339 #if defined(__FreeBSD__) || defined(__NetBSD__)
341 ** init a few other context registers
351 ** try to read the boot sector from the specified disk
354 try_boot(int bootdrv)
358 fd = disk_fd(bootdrv);
359 if (fd < 0) { /* can we boot it? */
360 debug(D_DISK, "Cannot boot from %c\n", drntol(bootdrv));
365 if (read(fd, (char *)0x7c00, 512) != 512) {
366 debug(D_DISK, "Short read on boot block from %c:\n", drntol(bootdrv));
376 ** Setup to run a single command and emulate DOS
379 setup_command(int argc, char *argv[], regcontext_t *REGS)
382 u_short param[7] = {0, 0, 0, 0, 0, 0, 0};
385 char buffer[PATH_MAX];
389 fp = find_doscmdrc(); /* dig up a doscmdrc */
391 read_config(fp); /* load config for non-boot mode */
395 if (argc <= 0) /* need some arguments */
398 /* look for a working directory XXX ??? */
399 if (dos_getcwd(drlton('C')) == NULL) {
401 /* try to get our current directory, use '/' if desperate */
402 p = getcwd(buffer, sizeof(buffer));
403 if (!p || !*p) p = getenv("PWD");
404 if (!p || !*p) p = "/";
405 init_path(drlton('C'), (u_char *)"/", (u_char *)p);
407 /* look for PATH= already set, learn from it if possible */
408 for (i = 0; i < ecnt; ++i) {
409 if (!strncmp(envs[i], "PATH=", 5)) {
410 dos_path = envs[i] + 5;
414 /* no PATH in DOS environment? put current directory there*/
416 static char path[256];
417 snprintf(path, sizeof(path), "PATH=C:%s", dos_getcwd(drlton('C')));
419 dos_path = envs[ecnt-1] + 5;
423 /* add a COMSPEC if required */
424 for (i = 0; i < ecnt; ++i) {
425 if (!strncmp(envs[i], "COMSPEC=", 8))
429 put_dosenv("COMSPEC=C:\\COMMAND.COM");
431 /* look for PATH already set, learn from it if possible */
432 for (i = 0; i < ecnt; ++i) {
433 if (!strncmp(envs[i], "PATH=", 5)) {
434 dos_path = envs[i] + 5;
438 /* No PATH, default to c:\ */
440 put_dosenv("PATH=C:\\");
441 dos_path = envs[ecnt-1] + 5;
444 /* if no PROMPT, default to 'DOS>' */
445 for (i = 0; i < ecnt; ++i) {
446 if (!strncmp(envs[i], "PROMPT=", 7))
450 put_dosenv("PROMPT=DOS> ");
452 /* terminate environment */
456 if (dos_getcwd(drlton('R')) == NULL)
457 init_path(drlton('R'), (u_char *)"/", 0);
459 /* get program name */
460 strncpy(prog, *argv++, sizeof(prog) -1);
461 prog[sizeof(prog) -1] = '\0';
463 /* try to open program */
464 if ((fd = open_prog(prog)) < 0) {
465 fprintf (stderr, "%s: command not found\n", prog);
470 load_command(REGS, 1, fd, cmdname, param, argv, envs);
477 ** Try to find a doscmdrc file
485 if ((fp = fopen(".doscmdrc", "r")) == NULL) {
486 struct passwd *pwd = getpwuid(geteuid());
488 snprintf(buffer, sizeof(buffer), "%s/.doscmdrc", pwd->pw_dir);
489 fp = fopen(buffer, "r");
492 char *home = getenv("HOME");
494 snprintf(buffer, sizeof(buffer), "%s/.doscmdrc", home);
495 fp = fopen(buffer, "r");
499 fp = fopen("/etc/doscmdrc", "r");
507 ** commandline argument processing
510 do_args(int argc, char *argv[])
516 while ((c = getopt(argc, argv, "234AbCc:Dd:EGHIi:kLMOo:Pp:RrS:TtU:vVxXYz")) != -1) {
519 debug_flags |= D_TRAPS2;
522 debug_flags |= D_TRAPS3;
525 debug_flags |= D_DEBUGIN;
528 debug_flags |= D_TRAPS | D_ITRAPS;
529 for (c = 0; c < 256; ++c)
536 debug_flags |= D_DOSCALL;
539 if ((capture_fd = creat(optarg, 0666)) < 0) {
545 debug_flags |= D_DISK | D_FILE_OPS;
548 if ((fp = fopen(optarg, "w")) != 0) {
555 debug_flags |= D_EXEC;
558 debug_flags |= D_VIDEO;
561 debug_flags |= D_HALF;
564 debug_flags |= D_ITRAPS;
565 for (c = 0; c < 256; ++c)
570 if ((col = strchr(optarg, ':')) != 0) {
572 i = strtol(col, 0, 0);
574 p = strtol(optarg, 0, 0);
578 define_input_port_handler(p++, inb_traceport);
584 debug_flags |= D_PRINTER;
587 debug_flags |= D_MEMORY;
591 setbuf (stdout, NULL);
595 if ((col = strchr(optarg, ':')) != 0) {
597 i = strtol(col, 0, 0);
599 p = strtol(optarg, 0, 0);
603 define_output_port_handler(p++, outb_traceport);
606 debug_flags |= D_PORT;
610 if ((col = strchr(optarg, ':')) != 0) {
612 i = strtol(col, 0, 0);
614 p = strtol(optarg, 0, 0);
618 define_input_port_handler(p++, inb_port);
619 define_output_port_handler(p++, outb_port);
623 debug_flags |= D_REDIR;
629 debug_flags |= D_TRAPS | D_ITRAPS;
630 debug_set(strtol(optarg, 0, 0));
639 debug_unset(strtol(optarg, 0, 0));
645 debug_flags |= D_TRAPS | D_ITRAPS | D_HALF | 0xff;
648 debug_flags |= D_XMS;
652 fatal("X11 support not compiled in.\n");
657 debug_flags |= D_EMS;
675 fprintf (stderr, "usage: doscmd cmd args...\n");
680 ** look up a DOS command name
682 ** XXX ordering is wrong!
685 open_name(char *name, char *ext)
688 char *p = name + strlen(name);
693 q = strrchr(name, '/');
699 if (!strchr(q, '.')) {
703 if ((fd = open (name, O_RDONLY)) >= 0)
709 if ((fd = open (name, O_RDONLY)) >= 0)
712 if ((fd = open (name, O_RDONLY)) >= 0)
720 ** look up a DOS command, search the path as well.
723 open_prog(char *name)
726 char fullname[1024], tmppath[1024];
734 if (strpbrk(name, ":/\\")) {
735 error = translate_filename(name, fullname, &drive);
739 fd = open_name(fullname, ext);
741 strcpy(cmdname, name);
743 strcat(cmdname, ext);
751 while (*p && *p != ';')
754 memcpy(tmppath, path, p - path);
755 e = tmppath + (p - path);
759 path = *p ? p + 1 : p;
761 error = translate_filename(tmppath, fullname, &drive);
765 fd = open_name(fullname, ext);
768 strcpy(cmdname, tmppath);
770 strcat(cmdname, ext);
779 ** append a value to the DOS environment
782 put_dosenv(const char *value)
784 if (ecnt < sizeof(envs)/sizeof(envs[0])) {
785 if ((envs[ecnt++] = strdup(value)) == NULL) {
786 perror("put_dosenv");
790 fprintf(stderr, "Environment full, ignoring %s\n", value);
795 ** replicate a fd up at the top of the range
800 int sfd = sysconf(_SC_OPEN_MAX);
806 } while (sfd > 0 && errno != EBADF);
808 if (errno == EBADF && dup2(fd, sfd) >= 0) {
822 ** XXX belongs somewhere else perhaps
825 done(regcontext_t *REGS, int val)
832 for (m = "END OF PROGRAM"; *m; ++m)
833 tty_write(*m, 0x8400);
835 for (m = "(PRESS <CTRL-ALT> ANY MOUSE BUTTON TO exit)"; *m; ++m)
836 tty_write(*m, 0x0900);
844 exec_return(REGS, val);
848 void (*func)(void *);
863 if (!xmode) /* XXX not for bootmode */
869 call_on_quit(void (*func)(void *), void *arg)
871 COQ *c = (COQ *)malloc(sizeof(COQ));
873 perror("call_on_quit");
888 /* This is commented out as it is never called. Turn it back on if needed.
895 struct io_range io[] = {
897 { 0x200, 0x200, 1 }, /* 0x200 - 0x400 */
898 { 0x1c80, 2, 1 }, /* 0x1c80 - 0x1c81 */
899 { 0x2c80, 2, 1 }, /* 0x2c80 - 0x2c81 */
900 { 0x3c80, 2, 1 }, /* 0x3c80 - 0x3c81 */
901 { 0x378, 8, 1 }, /* 0x378 - 0x37F */
902 { 0x3c4, 2, 1 }, /* 0x3c4 - 0x3c5 */
903 { 0x3c5, 2, 1 }, /* 0x3ce - 0x3cf */
905 { 0x0, 0x10000, 1 }, /* entire i/o space */
910 for (i = 0; io[i].length; i++)
911 if (i386_set_ioperm(io[i].start, io[i].length, io[i].enable) < 0)
912 err(1, "i386_set_ioperm");
916 /* This is used to map in only the specified port range, instead of all
917 the ports or only certain port ranges.
920 iomap_port(int port, int count)
922 if (i386_set_ioperm(port, count, 1) < 0)
923 err(1, "i386_set_ioperm");
925 debug(D_PORT,"mapped I/O port: port=%#x count=%d\n", port, count);