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 intff.c,v 2.2 1996/04/08 19:32:56 bostic Exp
32 * $FreeBSD: src/usr.bin/doscmd/intff.c,v 1.7.2.2 2002/04/25 11:04:51 tg Exp $
35 #include <sys/param.h>
43 static LOL *lol = NULL; /* DOS list-of-lists */
44 static SDA *sda = NULL; /* DOS swappable data area */
46 /******************************************************************************
47 ** redirector functions
50 ** These are set up on entry to the redirector each time, and are referenced
51 ** by the functions here.
53 static int r_drive,n_drive = 0;
64 int2f11_00(regcontext_t *REGS)
67 R_AH = 'U'; /* and why not? 8) */
72 ** 2f:11:1 2f:11:2 2f:11:3 2f:11:4 2f:11:5 2f:11:11 2f:11:13
74 ** Directory functions
77 int2f11_dirfn(regcontext_t *REGS)
79 char fname[PATH_MAX], tname[PATH_MAX];
82 error = translate_filename(sda->filename1, fname, &r_drive);
86 if (dos_readonly(r_drive) && (R_AL != 0x05))
87 return(WRITE_PROT_DISK);
90 case 0x01: /* rmdir */
92 debug(D_REDIR,"rmdir(%s)\n",fname);
95 case 0x03: /* mkdir */
97 debug(D_REDIR,"mkdir(%s)\n",fname);
98 error = mkdir(fname,0777);
100 case 0x05: /* chdir */
101 debug(D_REDIR,"chdir(%s)\n",fname);
102 /* Note returns DOS error directly */
103 return(dos_setcwd(sda->filename1));
105 case 0x11: /* rename */
106 error = translate_filename(sda->filename2, tname, &r_drive);
108 debug(D_REDIR,"rename(%s,%s)\n",fname,tname);
109 error = rename(fname, tname);
113 case 0x13: /* unlink */
114 debug(D_REDIR,"unlink(%s)\n",fname);
115 error = unlink(fname);
119 fatal("called int2f11_dirfn on unknown function %x\n",R_AL);
126 return(PATH_NOT_FOUND);
128 return(NOT_SAME_DEV);
130 return(ACCESS_DENIED);
142 int2f11_close(regcontext_t *REGS __unused)
147 debug(D_REDIR, "close(%d)\n", fd);
151 debug(D_REDIR, "not last close\n");
155 return(HANDLE_INVALID);
165 int2f11_rdwr(regcontext_t *REGS __unused)
173 if (lseek(fd, r_sft->offset, SEEK_SET) < 0)
176 addr = (char *)MAKEPTR(sda->dta_seg, sda->dta_off);
180 case 0x08: /* read */
181 debug(D_REDIR, "read(%d, %d)\n", fd, nbytes);
182 n = read(fd, addr, nbytes);
187 debug(D_REDIR, "write(%d, %d)\n", fd, nbytes);
188 n = write(fd, addr, nbytes);
193 fatal("called int2f11_rdwr on unknown function %x\n",R_AL);
196 R_CX = n; /* report count */
198 if (r_sft->offset > r_sft->size)
199 r_sft->size = r_sft->offset;
200 debug(D_REDIR, "offset now %ld\n", r_sft->offset);
207 ** Get free space (like 21:36)
210 int2f11_free(regcontext_t *REGS __unused)
215 error = get_space(r_drive, &fs);
218 R_AX = fs.sectors_cluster;
219 R_BX = fs.total_clusters;
220 R_CX = fs.bytes_sector;
221 R_DX = fs.avail_clusters;
231 int2f11_stat(regcontext_t *REGS __unused)
233 char fname[PATH_MAX];
237 error = translate_filename(sda->filename1, fname, &r_drive);
241 if (stat(fname, &sb) < 0)
242 return(FILE_NOT_FOUND);
244 R_AX = to_dos_attr(sb.st_mode);
245 R_BX = sb.st_size >> 16;
246 R_DI = sb.st_size & 0xffff;
251 ** 2f:11:16 2f:11:17 2f:11:18 2f:11:2e
253 ** Open/create a file, closely resembles int21_open.
256 int2f11_open(regcontext_t *REGS)
258 char fname[PATH_MAX];
261 int mode = 0; /* open mode */
262 int attr; /* attributes of created file */
263 int action = 0; /* what to do about file */
266 int omode = 0; /* mode to say we opened in */
270 error = translate_filename(sda->filename1, fname, &r_drive);
275 ** get attributes/access mode off stack : low byte is attribute, high
276 ** byte is (sometimes) used in conjunction with 'action'
278 attr = *(u_short *)MAKEPTR(R_SS, R_SP) & 0xff;
282 case 0x16: /* open */
283 action = 0x01; /* fail if does not exist */
284 switch (sda->open_mode & 3) {
295 return (FUNC_NUM_IVALID);
297 omode = sda->open_mode & 0x7f;
298 debug(D_REDIR,"open");
301 case 0x17: /* creat/creat new */
302 case 0x18: /* creat/creat new (no CDS, but we don't care)*/
305 if (attr & 0x100) { /* creat new */
306 action = 0x10; /* create if not exist, fail if exists */
307 debug(D_REDIR, "creat_new");
309 action = 0x12; /* create and destroy */
310 debug(D_REDIR, "creat");
314 case 0x2e: /* multipurpose */
315 attr = sda->ext_attr;
316 action = sda->ext_action;
317 switch (sda->ext_mode & 3) {
328 return (FUNC_NUM_IVALID);
330 omode = sda->ext_mode & 0x7f;
331 debug(D_REDIR,"mopen");
335 fatal("called int2f11_open for unknown function %x\n",R_AL);
337 if (action & 0x02) /* replace/open mode */
339 debug(D_REDIR, "(%s) action 0x%x mode 0x%x attr 0x%x omode 0x%x \n",
340 fname, action, mode, attr, omode);
342 if (ustat(fname, &sb) < 0) { /* file does not exist */
343 if ((action & 0x10) || (attr & 0x100)) { /* create it */
345 mode |= O_CREAT; /* have to create as we go */
346 status = 0x02; /* file created */
348 return(FILE_NOT_FOUND); /* fail */
351 if (S_ISDIR(sb.st_mode))
352 return(ACCESS_DENIED);
353 if ((action & 0x03) && !(attr & 0x100)) { /* exists, work with it */
355 if (!S_ISREG(sb.st_mode)) { /* only allowed for files */
356 debug(D_FILE_OPS,"attempt to truncate non-regular file\n");
357 return(ACCESS_DENIED);
359 status = 0x03; /* we're going to truncate it */
361 status = 0x01; /* just open it */
364 return(FILE_ALREADY_EXISTS); /* exists, fail */
368 if ((fd = open(fname, mode, from_dos_attr(attr))) < 0) {
369 debug(D_FILE_OPS,"failed to open %s : %s\n",fname,strerror(errno));
370 return (ACCESS_DENIED);
373 if (R_AL == 0x2e) /* extended wants status returned */
376 /* black magic to populate the SFT */
378 e = p = sda->filename1 + 2; /* parse name */
380 if (*p++ == '\\') /* look for beginning of filename */
384 for (i = 0; i < 8; ++i) { /* copy name and pad with spaces */
386 r_sft->name[i] = *e++;
388 r_sft->name[i] = ' ';
391 if (*e == '.') /* skip period on short names */
394 for (i = 0; i < 3; ++i) { /* copy extension and pad with spaces */
396 r_sft->ext[i] = *e++;
401 if (ustat(fname, &sb) < 0) /* re-stat to be accurate */
402 return(WRITE_FAULT); /* any better ideas?! */
404 r_sft->open_mode = omode; /* file open mode */
405 *(u_long *)r_sft->ddr_dpb = 0; /* no parameter block */
406 r_sft->size = sb.st_size; /* current size */
407 r_sft->fd = fd; /* our fd for it (hidden in starting cluster number) */
408 r_sft->offset = 0; /* current offset is 0 */
409 *(u_short *)r_sft->dir_sector = 0; /* not local file, ignored */
410 r_sft->dir_entry = 0; /* not local file, ignored */
411 r_sft->attribute = attr & 0xff; /* file attributes as requested */
412 r_sft->info = r_drive + 0x8040; /* hide drive number here for later reference */
413 encode_dos_file_time(sb.st_mtime, &r_sft->date, &r_sft->time);
414 debug(D_REDIR,"success, fd %d status %x\n", fd, status);
424 int2f11_findfirst(regcontext_t *REGS __unused)
426 return(find_first(sda->filename1,sda->attrmask,
427 (dosdir_t *)sda->foundentry,
428 (find_block_t *)sda->findfirst));
437 int2f11_findnext(regcontext_t *REGS __unused)
439 return(find_next((dosdir_t *)sda->foundentry,
440 (find_block_t *)sda->findfirst));
449 int2f11_lseek(regcontext_t *REGS)
455 offset = (off_t) ((int) ((R_CX << 16) + R_DX));
457 debug(D_REDIR,"lseek(%d, 0x%qx, SEEK_END)\n", fd, offset);
459 if ((offset = lseek(fd, offset, SEEK_END)) < 0) {
461 return(HANDLE_INVALID);
465 r_sft->offset = offset; /* update offset in SFT */
477 int2f11_fnqual(regcontext_t *REGS)
481 static char errmsg[] = "(failed)";
485 return(PATH_NOT_FOUND);
487 savedrive = diskdrive; /* to get CWD for network drive */
489 fname = (char *)MAKEPTR(R_DS, R_SI); /* path pointers */
490 tname = (char *)MAKEPTR(R_ES, R_DI);
492 error = dos_makepath(fname, tname);
496 diskdrive = savedrive; /* restore correct drive */
498 debug(D_REDIR, "qualify '%s' -> '%s'\n", fname, tname);
505 ** Null function - we know about it but do nothing
508 int2f11_NULLFUNC(regcontext_t *REGS __unused)
516 ** no function - not handled here (error)
519 int2f11_NOFUNC(regcontext_t *REGS __unused)
524 struct intfunc_table int2f11_table[] = {
525 { 0x00, IFT_NOSUBFUNC, int2f11_00, "installation check"},
526 { 0x01, IFT_NOSUBFUNC, int2f11_dirfn, "rmdir"},
527 { 0x02, IFT_NOSUBFUNC, int2f11_dirfn, "rmdir"},
528 { 0x03, IFT_NOSUBFUNC, int2f11_dirfn, "mkdir"},
529 { 0x04, IFT_NOSUBFUNC, int2f11_dirfn, "mkdir"},
530 { 0x05, IFT_NOSUBFUNC, int2f11_dirfn, "chdir"},
531 { 0x06, IFT_NOSUBFUNC, int2f11_close, "close"},
532 { 0x07, IFT_NOSUBFUNC, int2f11_NULLFUNC, "commit file"},
533 { 0x08, IFT_NOSUBFUNC, int2f11_rdwr, "read"},
534 { 0x09, IFT_NOSUBFUNC, int2f11_rdwr, "write"},
535 { 0x0a, IFT_NOSUBFUNC, int2f11_NULLFUNC, "lock region"},
536 { 0x0b, IFT_NOSUBFUNC, int2f11_NULLFUNC, "unlock region"},
537 { 0x0c, IFT_NOSUBFUNC, int2f11_free, "free space"},
538 { 0x0e, IFT_NOSUBFUNC, int2f11_NULLFUNC, "chmod"},
539 { 0x0f, IFT_NOSUBFUNC, int2f11_stat, "stat"},
540 { 0x11, IFT_NOSUBFUNC, int2f11_dirfn, "rename"},
541 { 0x13, IFT_NOSUBFUNC, int2f11_dirfn, "unlink"},
542 { 0x16, IFT_NOSUBFUNC, int2f11_open, "open"},
543 { 0x17, IFT_NOSUBFUNC, int2f11_open, "creat"},
544 { 0x18, IFT_NOSUBFUNC, int2f11_open, "creat"},
545 { 0x19, IFT_NOSUBFUNC, int2f11_findfirst, "find first"},
546 { 0x1b, IFT_NOSUBFUNC, int2f11_findfirst, "find first"},
547 { 0x1c, IFT_NOSUBFUNC, int2f11_findnext, "find next"},
548 { 0x1d, IFT_NOSUBFUNC, int2f11_NULLFUNC, "close all (abort)"},
549 { 0x1e, IFT_NOSUBFUNC, int2f11_NULLFUNC, "do redirection"},
550 { 0x1f, IFT_NOSUBFUNC, int2f11_NULLFUNC, "printer setup"},
551 { 0x20, IFT_NOSUBFUNC, int2f11_NULLFUNC, "flush all buffers"},
552 { 0x21, IFT_NOSUBFUNC, int2f11_lseek, "lseek"},
553 { 0x22, IFT_NOSUBFUNC, int2f11_NULLFUNC, "process terminated"},
554 { 0x23, IFT_NOSUBFUNC, int2f11_fnqual, "qualify filename"},
555 { 0x24, IFT_NOSUBFUNC, int2f11_NOFUNC, "turn off printer"},
556 { 0x25, IFT_NOSUBFUNC, int2f11_NOFUNC, "printer mode"},
557 { 0x2d, IFT_NOSUBFUNC, int2f11_NOFUNC, "extended attributes"},
558 { 0x2e, IFT_NOSUBFUNC, int2f11_open, "extended open/create"},
562 static int int2f11_fastlookup[256];
564 /******************************************************************************
567 ** The DOS redirector interface.
571 ** Verify that the drive being referenced is one we are handling, and
572 ** establish some state for upcoming functions.
574 ** Returns 1 if we should handle this request.
576 ** XXX this is rather inefficient, but much easier to read than the previous
580 int2f11_validate(regcontext_t *REGS)
583 const char *path = NULL;
586 /* defaults may help trap problems */
591 /* some functions we accept regardless */
593 case 0x00: /* installation check */
594 case 0x23: /* qualify path */
595 case 0x1c: /* XXX really only valid if a search already started... */
599 /* Where's the CDS? */
601 case 0x01: /* referenced by the SDA */
612 r_cds = (CDS *)MAKEPTR(sda->cds_seg, sda->cds_off);
615 case 0x0c: /* in es:di */
617 r_cds = (CDS *)MAKEPTR(R_ES, R_DI);
621 /* Where's the SFT? */
623 case 0x06: /* in es:di */
635 r_sft = (SFT *)MAKEPTR(R_ES, R_DI);
641 case 0x01: /* get drive from fully-qualified path in SDA */
657 path = sda->filename1;
660 case 0x06: /* get drive from SFT (we put it here when file was opened) */
668 r_drive = r_sft->info & 0x1f;
672 if (path) { /* we have a path and need to determine the drive it refers to */
674 if (path[1] != ':') { /* must be fully qualified; we cannot handle this */
675 debug(D_REDIR,"attempt to work non-absolute path %s\n",path);
679 /* translate letter to drive number */
680 r_drive = drlton(path[0]);
685 /* do we handle this drive? */
686 if (dos_getcwd(r_drive)) {
687 n_drive = r_drive; /* XXX GROSTIC HACK ALERT */
691 debug(D_REDIR,"%s -> drive %c func %x (%sus)\n",
692 path, drntol(r_drive), func, doit?"":"not ");
694 /* so do we deal with this one? */
700 int2f_11(regcontext_t *REGS)
705 if (!sda) { /* not initialised yet */
706 error = FUNC_NUM_IVALID;
709 idx = intfunc_find(int2f11_table, int2f11_fastlookup, R_AL, 0);
711 debug(D_ALWAYS,"no handler for int2f:11:%x\n", R_AL);
717 if (!int2f11_validate(REGS)) { /* determine whether we handle this request */
718 error = -1; /* not handled by us */
720 debug(D_REDIR, "REDIR: %02x (%s)\n",
721 int2f11_table[idx].func, int2f11_table[idx].desc);
722 /* call the handler */
723 error = int2f11_table[idx].handler(REGS);
725 debug(D_REDIR, "REDIR: returns %d (%s)\n",
726 error, ((error >= 0) && (error <= dos_ret_size)) ? dos_return[error] : "unknown");
740 /******************************************************************************
743 ** intff is the (secret, proprietary, internal, evil) call to
744 ** initialise the redirector.
747 install_drive(int drive, u_char *path)
751 /* check that DOS considers this a valid drive */
752 if (drive < 0 || drive >= lol->lastdrive) {
753 debug(D_REDIR, "Drive %c beyond limit of %c\n",
754 drntol(drive), drntol(lol->lastdrive - 1));
758 /* get the CDS for this drive */
759 cds = (CDS *)MAKEPTR(lol->cds_seg, lol->cds_offset);
762 #if 0 /* XXX looks OK to me - mjs */
763 if (cds->flag & (CDS_remote | CDS_ready)) {
764 debug(D_REDIR, "Drive %c already installed\n", drntol(drive));
769 debug(D_REDIR, "Installing %c: as %s\n", drntol(drive), path);
771 cds->flag |= CDS_remote | CDS_ready | CDS_notnet;
772 cds->path[0] = drntol(drive);
776 cds->offset = 2; /* offset of \ in current path field */
785 /* for all possible drives */
786 for (drive = 0; drive < 26; ++drive) {
787 if ((path = dos_getpath(drive)) != NULL) /* assigned to a path? */
788 install_drive(drive, path); /* make it visible to DOS */
794 intff(regcontext_t *REGS)
797 if (lol && sda) { /* already been called? */
798 debug(D_REDIR, "redirector duplicate install ignored");
801 lol = (LOL *)MAKEPTR(R_BX, R_DX); /* where DOS keeps its goodies */
802 sda = (SDA *)MAKEPTR(R_DI, R_SI);
805 /* initialise dispatcher */
806 intfunc_init(int2f11_table, int2f11_fastlookup);