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 int13.c,v 2.3 1996/04/08 19:32:43 bostic Exp
32 * $FreeBSD: src/usr.bin/doscmd/int13.c,v 1.4.2.2 2002/04/25 11:04:51 tg Exp $
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
42 #define FDCHANGED _IOR('F', 64, int)
44 #define INT13_ERR_NONE 0x00
45 #define INT13_ERR_BAD_COMMAND 0x01
46 #define INT13_ERR_BAD_ADDRESS_MARK 0x02
47 #define INT13_ERR_WRITE_PROTECT 0x03
48 #define INT13_ERR_SECTOR_ID_BAD 0x04
49 #define INT13_ERR_RESET_FAILURE 0x05
50 #define INT13_ERR_CLL_ACTIVE 0x06
51 #define INT13_ERR_ACT_FAILED 0x07
52 #define INT13_ERR_DMA_OVERRUN 0x08
53 #define INT13_ERR_DMA_BOUNDARY 0x09
54 #define INT13_ERR_BAD_TRACK_FLAG 0x0B
55 #define INT13_ERR_MEDIA_TYP_UNKNOWN 0x0C
56 #define INT13_ERR_CRC 0x10
57 #define INT13_ERR_CORRECTED 0x11
58 #define INT13_ERR_CTRLR_FAILURE 0x20
59 #define INT13_ERR_SEEK 0x40
60 #define INT13_ERR_TIME_OUT 0x80
61 #define INT13_ERR_NOT_READY 0xAA
62 #define INT13_ERR_UNDEFINED 0xBB
63 #define INT13_ERR_SENSE_OPERATION 0xFF
89 char *list[4]; /* Up to 4 devices allowed */
93 int changed:1; /* Set if we change format */
96 #define hd_status (*(u_char *)0x474)
97 #define fd_status (*(u_char *)0x441)
100 disize(struct diskinfo *di)
102 return(di->sectors * di->cylinders * di->sides);
106 cylsize(struct diskinfo *di)
108 return(di->sectors * di->sides);
111 static u_long ftable = 0xF1000; /* Floppy table */
112 static u_long htable = 0xF1020; /* Hard disk table */
114 static struct diskinfo diskinfo[26];
116 static struct diskinfo floppyinfo[] = {
117 {0, 9, 40, 1, 512, -1, NULL, 0, NULL, 0,
118 {NULL, NULL, NULL, NULL}, 0, 0, 0, 0}, /* Probably not correct */
119 {1, 9, 40, 2, 512, -1, NULL, 0, NULL, 0,
120 {NULL, NULL, NULL, NULL}, 0, 0, 0, 0},
121 {2, 9, 80, 2, 512, -1, NULL, 0, NULL, 0,
122 {NULL, NULL, NULL, NULL}, 0, 0, 0, 0},
123 {3, 15, 80, 2, 512, -1, NULL, 0, NULL, 0,
124 {NULL, NULL, NULL, NULL}, 0, 0, 0, 0},
125 {4, 18, 80, 2, 512, -1, NULL, 0, NULL, 0,
126 {NULL, NULL, NULL, NULL}, 0, 0, 0, 0},
127 {6, 36, 80, 2, 512, -1, NULL, 0, NULL, 0,
128 {NULL, NULL, NULL, NULL}, 0, 0, 0, 0},
129 {-1, 0, 0, 0, 0, 0, NULL, 0, NULL, 0,
130 {NULL, NULL, NULL, NULL}, 0, 0, 0, 0}
133 static struct diskinfo *
138 if (drive >= 2 && drive < 0x80) {
146 if (drive > 25 || diskinfo[drive].path == NULL) {
149 di = &diskinfo[drive];
151 if (di->removeable) {
153 if (!(di->path = di->list[di->multi]))
154 di->path = di->list[di->multi = 0];
156 if ((di->fd = open(di->path, di->read_only ? O_RDONLY
157 : O_RDWR|O_FSYNC)) < 0 &&
158 (di->read_only = 1) &&
159 (di->fd = open(di->path, O_RDONLY)) < 0) {
162 di->fd = squirrel_fd(di->fd);
181 make_readonly(int drive)
183 if (drive < 0 || drive >= 26)
185 diskinfo[drive].read_only = 1;
189 init_hdisk(int drive, int cyl, int head, int tracksize, char *file, char *fake_ptab)
195 for (drive = 2; drive < 26; ++drive) {
196 if (diskinfo[drive].path == NULL)
201 fprintf(stderr, "Only floppies may be assigned to A: or B:\n");
206 fprintf(stderr, "Too many disk drives (only 24 allowed)\n");
210 di = &diskinfo[drive];
213 fprintf(stderr, "Drive %c: already assigned to %s\n", drntol(drive),
218 di->sectors = tracksize;
230 if ((fd = open(fake_ptab, 0)) < 0) {
234 di->sector0 = malloc(512);
236 perror("malloc in init_hdisk");
240 read(fd, di->sector0, 512);
243 ptab = (PTAB *)(di->sector0 + 0x01BE);
245 for (fd = 0; fd < 4; ++fd) {
246 if (*(u_short *)(di->sector0 + 0x1FE) == 0xAA55 &&
247 ptab[fd].numSectors == (u_long)(head * tracksize * cyl) &&
248 (ptab[fd].systemID == 1 || ptab[fd].systemID == 4 ||
249 ptab[fd].systemID == 6))
254 memcpy(ptab, ptab + fd, sizeof(PTAB));
255 memset(ptab + 1, 0, sizeof(PTAB) * 3);
256 di->offset = ptab[0].relSector;
257 di->cylinders += di->offset / cylsize(di);
259 memset(ptab, 0, sizeof(PTAB) * 4);
262 ptab->beginSector = 1; /* this is 1 based */
265 ptab->endHead = head - 1;
266 ptab->endSector = tracksize; /* this is 1 based */
267 ptab->endCyl = cyl & 0xff;
268 ptab->endSector |= (cyl & 0x300) >> 2;
270 ptab->relSector = head * tracksize;
271 ptab->numSectors = head * tracksize * cyl;
273 *(u_short *)(di->sector0 + 0x1FE) = 0xAA55;
283 if ((clusters = buf[0x0D]) == 0) {
284 if (disize(di) <= 128 * 2048)
286 else if (disize(di) <= 256 * 2048)
288 else if (disize(di) <= 8 * 1024 * 2048)
290 else if (disize(di) <= 16 * 1024 * 2048)
295 if ((disize(di) / clusters) <= 4096) {
296 ptab->systemID = 0x01;
298 ptab->systemID = 0x04;
301 di->cylinders += 1; /* Extra cylinder for partition table, etc. */
303 ptab->bootIndicator = 0x80;
308 di->path = strdup(file);
310 di->location = ((table & 0xf0000) << 12) | (table & 0xffff);
313 ivec[0x41] = di->location;
314 } else if (drive == 1) {
315 ivec[0x46] = di->location;
318 table = htable + (drive - 2) * 0x10;
319 *(u_short *)(table+0x00) = di->cylinders-1; /* Cylinders */
320 *(u_char *)(table+0x02) = di->sides; /* Heads */
321 *(u_short *)(table+0x03) = 0; /* 0 */
322 *(u_short *)(table+0x05) = 0xffff; /* write pre-comp */
323 *(u_char *)(table+0x07) = 0; /* ECC Burst length */
324 *(u_char *)(table+0x08) = 0; /* Control Byte */
325 *(u_char *)(table+0x09) = 0; /* standard timeout */
326 *(u_char *)(table+0x0a) = 0; /* formatting timeout */
327 *(u_char *)(table+0x0b) = 0; /* timeout for checking drive */
328 *(u_short *)(table+0x0c) = di->cylinders-1; /* landing zone */
329 *(u_char *)(table+0x0e) = di->sectors; /* sectors/track */
330 *(u_char *)(table+0x0f) = 0;
332 if ((drive - 1) >= ndisks)
344 case 1024: return(3);
346 fprintf(stderr, "Invalid sector size: %d\n", size);
349 /* keep `gcc -Wall' happy */
354 init_floppy(int drive, int type, char *file)
356 struct diskinfo *di = floppyinfo;
360 while (di->type >= 0 && di->type != type && disize(di)/2 != type)
364 fprintf(stderr, "Invalid floppy type: %d\n", type);
369 if (diskinfo[0].path == NULL) {
371 } else if (diskinfo[1].path == NULL) {
374 fprintf(stderr, "Too many floppy drives (only 2 allowed)\n");
379 fprintf(stderr, "Floppies must be either drive A: or B:\n");
383 if (drive >= nfloppies)
384 nfloppies = drive + 1;
386 if (diskinfo[drive].path == NULL) {
387 diskinfo[drive] = *di;
390 di = &diskinfo[drive];
392 if (stat(file, &sb) < 0) {
393 fprintf(stderr, "Drive %c: Could not stat %s\n", drntol(drive), file);
397 if (drive < 2 && (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))) {
398 if (di->path && !di->removeable) {
399 fprintf(stderr, "Drive %c: is not removeable and hence can only have one assignment\n", drntol(drive));
403 } else if (di->removeable) {
404 fprintf(stderr, "Drive %c: already assigned to %s\n", drntol(drive),
409 if (di->removeable) {
411 if (di->multi == 4) {
412 fprintf(stderr, "Drive %c: already assigned 4 devices\n",
417 di->path = di->list[di->multi++] = strdup(file);
420 fprintf(stderr, "Drive %c: already assigned to %s\n",
421 drntol(drive), di->path);
425 di->path = strdup(file);
428 di->location = ((table & 0xf0000) << 12) | (table & 0xffff);
432 ivec[0x1e] = ((ftable & 0xf0000) << 12) | (ftable & 0xffff);
434 table = ftable + drive * 0x0a;
436 *(u_char *)(table+0x00) = 0xdf; /* First Specify Byte */
437 *(u_char *)(table+0x01) = 0x02; /* Second Specify Byte */
438 *(u_char *)(table+0x02) = 0x25; /* Timer ticks to wait 'til motor OFF */
439 *(u_char *)(table+0x03) = bps(di->secsize); /* Number of bytes/sector */
440 *(u_char *)(table+0x04) = di->sectors; /* Number of sectors/track */
441 *(u_char *)(table+0x05) = 0x1b; /* Gap length, in bytes */
442 *(u_char *)(table+0x06) = 0xff; /* Data length, in bytes */
443 *(u_char *)(table+0x07) = 0x6c; /* Gap length for format */
444 *(u_char *)(table+0x09) = 0xf6; /* Fill byte for formatting */
445 *(u_char *)(table+0x09) = 0x0f; /* Head settle time, in milliseconds */
446 *(u_char *)(table+0x0a) = 0x08; /* Motor startup time, in 1/8 seconds */
453 return(i < nfloppies ? diskinfo[i].type : 0);
456 #define seterror(err) { \
466 trynext(struct diskinfo *di)
472 if (di->multi++ >= 4)
475 if (di->list[di->multi] && (di = getdisk(di - diskinfo))) {
483 diread(struct diskinfo *di, regcontext_t *REGS,
484 off_t start, char *addr, int sectors)
488 int drive = di - diskinfo;
497 res = lseek(di->fd, start * di->secsize, 0);
499 if (res < 0 && di->removeable && trynext(di))
503 seterror(INT13_ERR_SEEK);
507 res = read(di->fd, addr, sectors * di->secsize);
509 if (res < 0 && di->removeable && trynext(di))
512 if (di->removeable) {
514 seterror(INT13_ERR_NOT_READY);
517 return(res / di->secsize);
521 * reads always work, if if they don't.
522 * Just pretend any byte not read was actually a 0
525 memset(addr, 0, sectors * di->secsize);
526 else if (res < sectors * di->secsize)
527 memset(addr + res, 0, sectors * di->secsize - res);
533 diwrite(struct diskinfo *di, regcontext_t *REGS,
534 off_t start, char *addr, int sectors)
537 int drive = di - diskinfo;
546 res = lseek(di->fd, start * di->secsize, 0);
548 if (res < 0 && di->removeable && trynext(di))
552 seterror(INT13_ERR_SEEK);
556 res = write(di->fd, addr, sectors * di->secsize);
558 if (res < 0 && di->removeable && trynext(di))
561 if (di->removeable) {
563 seterror(INT13_ERR_NOT_READY);
566 } else if (res < 0) {
567 seterror(INT13_ERR_WRITE_PROTECT);
570 return(res / di->secsize);
574 int13(regcontext_t *REGS)
601 case 0x00: /* Reset */
603 case 0x01: /* Read disk status */
611 case 0x02: /* Read */
613 addr = (char *)MAKEPTR(R_ES, R_BX);
616 R_AL = 0; /* Start out with nothing read */
619 cyl = R_CH | ((R_CL & 0xc0) << 2);
620 sector = (R_CL & 0x3f) - 1;
626 if ((di = getdisk(drive)) == NULL) {
627 debug(D_DISK, "Bad drive: %02x (%d : %d : %d)\n",
628 drive, cyl, side, sector);
629 seterror(INT13_ERR_BAD_COMMAND);
632 start = cyl * di->sectors * di->sides +
636 if (start >= disize(di)) {
637 debug(D_DISK, "Read past end of disk\n");
638 seterror(INT13_ERR_SEEK);
641 if (sectors + start >= disize(di)) {
642 sectors = disize(di) - start;
646 if (start < di->offset) {
649 memcpy(addr, di->sector0, di->secsize);
653 memset(addr, 0, sectors * di->secsize);
659 debug(D_DISK, "%02x: Read %2d sectors from %qd to %04x:%04x\n",
660 drive, sectors, start, R_ES, R_BX);
662 if ((did = diread(di, REGS, start, addr, sectors)) >= 0)
670 case 0x03: /* Write */
672 addr = (char *)MAKEPTR(R_ES, R_BX);
675 R_AL = 0; /* Start out with nothing written */
678 cyl = R_CH | ((R_CL & 0xc0) << 2);
679 sector = (R_CL & 0x3f) - 1;
685 if ((di = getdisk(drive)) == NULL) {
686 debug(D_DISK, "Bad drive: %d (%d : %d : %d)\n",
687 drive, cyl, side, sector);
688 seterror(INT13_ERR_BAD_COMMAND);
692 debug(D_DISK, "%02x: Attempt to write readonly disk\n", drive);
693 seterror(INT13_ERR_WRITE_PROTECT);
696 start = cyl * di->sectors * di->sides +
700 if (start >= disize(di)) {
701 debug(D_DISK, "Write past end of disk\n");
702 seterror(INT13_ERR_SEEK);
706 if (sectors + start >= disize(di))
707 sectors = disize(di) - start;
710 if (start < di->offset) {
718 debug(D_DISK, "%02x: Write %2d sectors from %qd to %04x:%04x\n",
719 drive, sectors, start, R_ES, R_BX);
721 if ((did = diwrite(di, REGS, start, addr, sectors)) >= 0)
729 case 0x04: /* Verify */
735 cyl = R_CH | ((R_CL & 0xc0) << 2);
736 sector = (R_CL & 0x3f) - 1;
742 if ((di = getdisk(drive)) == NULL) {
743 debug(D_DISK, "Bad drive: %d (%d : %d : %d)\n",
744 drive, cyl, side, sector);
745 seterror(INT13_ERR_BAD_COMMAND);
748 start = cyl * di->sectors * di->sides +
752 if (start >= disize(di)) {
753 debug(D_DISK, "Verify past end of disk\n");
754 seterror(INT13_ERR_SEEK);
758 if (sectors + start >= disize(di))
759 sectors = disize(di) - start;
762 if (start < di->offset)
768 debug(D_DISK, "Verify %2d sectors from %qd\n", sectors, start);
769 if (lseek(di->fd, start * di->secsize, 0) < 0) {
770 debug(D_DISK, "Seek error\n");
771 seterror(INT13_ERR_SEEK);
774 while (sectors > 0) {
776 if (read(di->fd, buf, di->secsize) != di->secsize) {
777 debug(D_DISK, "Verify error\n");
789 case 0x05: /* Format track */
790 seterror(INT13_ERR_BAD_COMMAND);
793 case 0x08: /* Status */
796 if ((di = getdisk(drive)) == NULL) {
797 debug(D_DISK, "Bad drive: %d\n", drive);
798 seterror(INT13_ERR_BAD_COMMAND);
804 if ((drive & 0x80) == 0)
805 PUTVEC(R_ES, R_DI, di->location);
807 R_CL = di->sectors | ((di->cylinders >> 2) & 0xc0);
808 R_CH = di->cylinders & 0xff;
809 R_DL = (drive & 0x80) ? ndisks : nfloppies;
810 R_DH = di->sides - 1;
811 debug(D_DISK, "%02x: Status requested: sec %d cyl %d side %d drive %d\n",
812 drive, R_CL, R_CH, R_DH, R_DL);
819 case 0x0c: /* Move read/write head */
820 case 0x0d: /* Reset */
823 case 0x10: /* check for disk ready */
824 R_AH = 0; /* always open for business */
828 if ((di = getdisk(drive)) == NULL) {
835 start = di->sectors * di->cylinders * di->sides;
840 R_AH = 1; /* Non-changeable disk */
844 case 0x16: /* Media change */
846 if ((di = getdisk(drive)) && di->changed) {
852 case 0x17: /* Determine floppy disk format */
853 seterror(INT13_ERR_BAD_COMMAND);
856 case 0x18: /* Determine disk format */
857 if ((di = getdisk(drive)) == NULL) {
862 /* XXX incomplete? */
866 unknown_int2(0x13, R_AH, REGS);
876 vec = insert_softint_trampoline();
878 register_callback(vec, int13, "int 13");
880 vec = insert_null_trampoline();