4 * Copyright 1999 Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/winbase16.h"
25 #include "wine/debug.h"
37 BYTE size; /* length of header + data */
38 BYTE unit; /* unit (block devices only) */
46 BYTE media; /* media descriptor from BPB */
48 WORD count; /* byte/sector count */
49 WORD sector; /* starting sector (block devices) */
50 DWORD volume; /* volume ID (block devices) */
67 /* Warning: need to return LOL ptr w/ offset 0 (&ptr_first_DPB) to programs ! */
68 typedef struct _DOS_LISTOFLISTS
70 WORD CX_Int21_5e01; /* -24d contents of CX from INT 21/AX=5E01h */
71 WORD LRU_count_FCB_cache; /* -22d */
72 WORD LRU_count_FCB_open; /* -20d */
73 DWORD OEM_func_handler; /* -18d OEM function of INT 21/AH=F8h */
74 WORD INT21_offset; /* -14d offset in DOS CS of code to return from INT 21 call */
75 WORD sharing_retry_count; /* -12d */
76 WORD sharing_retry_delay; /* -10d */
77 DWORD ptr_disk_buf; /* -8d ptr to current disk buf */
78 WORD offs_unread_CON; /* -4d pointer in DOS data segment of unread CON input */
79 WORD seg_first_MCB; /* -2d */
80 DWORD ptr_first_DPB; /* 00 */
81 DWORD ptr_first_SysFileTable; /* 04 */
82 DWORD ptr_clock_dev_hdr; /* 08 */
83 DWORD ptr_CON_dev_hdr; /* 0C */
84 WORD max_byte_per_sec; /* 10 maximum bytes per sector of any block device */
85 DWORD ptr_disk_buf_info; /* 12 */
86 DWORD ptr_array_CDS; /* 16 current directory structure */
87 DWORD ptr_sys_FCB; /* 1A */
88 WORD nr_protect_FCB; /* 1E */
89 BYTE nr_block_dev; /* 20 */
90 BYTE nr_avail_drive_letters; /* 21 */
91 DOS_DEVICE_HEADER NUL_dev; /* 22 */
92 BYTE nr_drives_JOINed; /* 34 */
93 WORD ptr_spec_prg_names; /* 35 */
94 DWORD ptr_SETVER_prg_list; /* 37 */
95 WORD DOS_HIGH_A20_func_offs;/* 3B */
96 WORD PSP_last_exec; /* 3D if DOS in HMA: PSP of program executed last; if DOS low: 0000h */
97 WORD BUFFERS_val; /* 3F */
98 WORD BUFFERS_nr_lookahead; /* 41 */
99 BYTE boot_drive; /* 43 */
100 BYTE flag_DWORD_moves; /* 44 01h for 386+, 00h otherwise */
101 WORD size_extended_mem; /* 45 size of extended mem in KB */
102 SEGPTR wine_rm_lol; /* -- wine: Real mode pointer to LOL */
103 SEGPTR wine_pm_lol; /* -- wine: Protected mode pointer to LOL */
108 #define CON_BUFFER 128
110 enum strategy { SYSTEM_STRATEGY_NUL, SYSTEM_STRATEGY_CON, NB_SYSTEM_STRATEGIES };
112 static void *strategy_data[NB_SYSTEM_STRATEGIES];
114 #define NONEXT ((DWORD)-1)
116 #define ATTR_STDIN 0x0001
117 #define ATTR_STDOUT 0x0002
118 #define ATTR_NUL 0x0004
119 #define ATTR_CLOCK 0x0008
120 #define ATTR_FASTCON 0x0010
121 #define ATTR_RAW 0x0020
122 #define ATTR_NOTEOF 0x0040
123 #define ATTR_DEVICE 0x0080
124 #define ATTR_REMOVABLE 0x0800
125 #define ATTR_NONIBM 0x2000 /* block devices */
126 #define ATTR_UNTILBUSY 0x2000 /* char devices */
127 #define ATTR_IOCTL 0x4000
128 #define ATTR_CHAR 0x8000
131 #define CMD_MEDIACHECK 1 /* block devices */
132 #define CMD_BUILDBPB 2 /* block devices */
133 #define CMD_INIOCTL 3
134 #define CMD_INPUT 4 /* read data */
135 #define CMD_SAFEINPUT 5 /* "non-destructive input no wait", char devices */
136 #define CMD_INSTATUS 6 /* char devices */
137 #define CMD_INFLUSH 7 /* char devices */
138 #define CMD_OUTPUT 8 /* write data */
139 #define CMD_SAFEOUTPUT 9 /* write data with verify */
140 #define CMD_OUTSTATUS 10 /* char devices */
141 #define CMD_OUTFLUSH 11 /* char devices */
142 #define CMD_OUTIOCTL 12
143 #define CMD_DEVOPEN 13
144 #define CMD_DEVCLOSE 14
145 #define CMD_REMOVABLE 15 /* block devices */
146 #define CMD_UNTILBUSY 16 /* output until busy */
148 #define STAT_MASK 0x00FF
149 #define STAT_DONE 0x0100
150 #define STAT_BUSY 0x0200
151 #define STAT_ERROR 0x8000
157 static void WINAPI nul_strategy(CONTEXT86*ctx);
158 static void WINAPI nul_interrupt(CONTEXT86*ctx);
159 static void WINAPI con_strategy(CONTEXT86*ctx);
160 static void WINAPI con_interrupt(CONTEXT86*ctx);
171 static WINEDEV devs[] =
174 ATTR_CHAR|ATTR_NUL|ATTR_DEVICE,
175 nul_strategy, nul_interrupt },
178 ATTR_CHAR|ATTR_STDIN|ATTR_STDOUT|ATTR_FASTCON|ATTR_NOTEOF|ATTR_DEVICE,
179 con_strategy, con_interrupt }
182 #define NR_DEVS (sizeof(devs)/sizeof(WINEDEV))
184 /* DOS data segment */
188 DOS_DEVICE_HEADER dev[NR_DEVS-1];
189 WINEDEV_THUNK thunk[NR_DEVS];
191 BYTE buffer[CON_BUFFER];
195 #define DOS_DATASEG_OFF(xxx) FIELD_OFFSET(DOS_DATASEG, xxx)
199 static struct _DOS_LISTOFLISTS * DOSMEM_LOL(void)
201 return PTR_REAL_TO_LIN(HIWORD(DOS_LOLSeg),0);
205 /* the device implementations */
206 static void do_lret(CONTEXT86*ctx)
208 WORD *stack = CTX_SEG_OFF_TO_LIN(ctx, ctx->SegSs, ctx->Esp);
210 ctx->Eip = *(stack++);
211 ctx->SegCs = *(stack++);
212 ctx->Esp += 2*sizeof(WORD);
215 static void do_strategy(CONTEXT86*ctx, int id, int extra)
217 REQUEST_HEADER *hdr = CTX_SEG_OFF_TO_LIN(ctx, ctx->SegEs, ctx->Ebx);
218 void **hdr_ptr = strategy_data[id];
221 hdr_ptr = calloc(1,sizeof(void *)+extra);
222 strategy_data[id] = hdr_ptr;
228 static REQUEST_HEADER * get_hdr(int id, void**extra)
230 void **hdr_ptr = strategy_data[id];
232 *extra = hdr_ptr ? (void*)(hdr_ptr+1) : (void *)NULL;
233 return hdr_ptr ? *hdr_ptr : (void *)NULL;
236 static void WINAPI nul_strategy(CONTEXT86*ctx)
238 do_strategy(ctx, SYSTEM_STRATEGY_NUL, 0);
241 static void WINAPI nul_interrupt(CONTEXT86*ctx)
243 REQUEST_HEADER *hdr = get_hdr(SYSTEM_STRATEGY_NUL, NULL);
244 /* eat everything and recycle nothing */
245 switch (hdr->command) {
247 ((REQ_IO*)hdr)->count = 0;
248 hdr->status = STAT_DONE;
251 hdr->status = STAT_DONE|STAT_BUSY;
254 hdr->status = STAT_DONE;
259 static void WINAPI con_strategy(CONTEXT86*ctx)
261 do_strategy(ctx, SYSTEM_STRATEGY_CON, sizeof(int));
264 static void WINAPI con_interrupt(CONTEXT86*ctx)
267 REQUEST_HEADER *hdr = get_hdr(SYSTEM_STRATEGY_CON,(void **)&scan);
268 BIOSDATA *bios = DOSVM_BiosData();
269 WORD CurOfs = bios->NextKbdCharPtr;
270 DOS_LISTOFLISTS *lol = DOSMEM_LOL();
271 DOS_DATASEG *dataseg = (DOS_DATASEG *)lol;
272 BYTE *linebuffer = dataseg->buffer;
273 BYTE *curbuffer = (lol->offs_unread_CON) ?
274 (((BYTE*)dataseg) + lol->offs_unread_CON) : (BYTE*)NULL;
275 DOS_DEVICE_HEADER *con = dataseg->dev;
278 switch (hdr->command) {
281 REQ_IO *io = (REQ_IO *)hdr;
282 WORD count = io->count, len = 0;
283 BYTE *buffer = CTX_SEG_OFF_TO_LIN(ctx,
284 SELECTOROF(io->buffer),
285 (DWORD)OFFSETOF(io->buffer));
287 hdr->status = STAT_BUSY;
288 /* first, check whether we already have data in line buffer */
290 /* yep, copy as much as we can */
292 while ((len<count) && (data != '\r')) {
294 buffer[len++] = data;
297 /* line buffer emptied */
298 lol->offs_unread_CON = 0;
300 /* if we're not in raw mode, call it a day */
301 if (!(con->attr & ATTR_RAW)) {
302 hdr->status = STAT_DONE;
307 /* still some data left */
308 lol->offs_unread_CON = curbuffer - (BYTE*)lol;
309 /* but buffer was filled, we're done */
310 hdr->status = STAT_DONE;
316 /* if we're in raw mode, we just need to fill the buffer */
317 if (con->attr & ATTR_RAW) {
321 /* do we have a waiting scancode? */
323 /* yes, store scancode in buffer */
324 buffer[len++] = *scan;
326 if (len==count) break;
329 /* check for new keyboard input */
330 while (CurOfs == bios->FirstKbdCharPtr) {
331 /* no input available yet, so wait... */
334 /* read from keyboard queue (call int16?) */
335 data = ((WORD*)bios)[CurOfs];
337 if (CurOfs >= bios->KbdBufferEnd) CurOfs = bios->KbdBufferStart;
338 bios->NextKbdCharPtr = CurOfs;
339 /* if it's an extended key, save scancode */
340 if (LOBYTE(data) == 0) *scan = HIBYTE(data);
341 /* store ASCII char in buffer */
342 buffer[len++] = LOBYTE(data);
345 /* we're not in raw mode, so we need to do line input... */
348 /* check for new keyboard input */
349 while (CurOfs == bios->FirstKbdCharPtr) {
350 /* no input available yet, so wait... */
353 /* read from keyboard queue (call int16?) */
354 data = ((WORD*)bios)[CurOfs];
356 if (CurOfs >= bios->KbdBufferEnd) CurOfs = bios->KbdBufferStart;
357 bios->NextKbdCharPtr = CurOfs;
359 if (LOBYTE(data) == '\r') {
360 /* it's the return key, we're done */
361 linebuffer[len++] = LOBYTE(data);
364 else if (LOBYTE(data) >= ' ') {
366 if ((len+1)<CON_BUFFER) {
367 linebuffer[len] = LOBYTE(data);
368 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), &linebuffer[len++], 1, &w, NULL);
370 /* else beep, but I don't like noise */
372 else switch (LOBYTE(data)) {
376 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\b \b", 3, &w, NULL);
382 /* save rest of line for later */
383 lol->offs_unread_CON = linebuffer - (BYTE*)lol + count;
386 memcpy(buffer, linebuffer, len);
388 hdr->status = STAT_DONE;
394 /* some line input waiting */
395 hdr->status = STAT_DONE;
396 ((REQ_SAFEINPUT*)hdr)->data = *curbuffer;
398 else if (con->attr & ATTR_RAW) {
399 if (CurOfs == bios->FirstKbdCharPtr) {
401 hdr->status = STAT_DONE|STAT_BUSY;
403 /* some keyboard input waiting */
404 hdr->status = STAT_DONE;
405 ((REQ_SAFEINPUT*)hdr)->data = ((BYTE*)bios)[CurOfs];
409 hdr->status = STAT_DONE|STAT_BUSY;
415 hdr->status = STAT_DONE;
417 else if (con->attr & ATTR_RAW) {
418 if (CurOfs == bios->FirstKbdCharPtr) {
420 hdr->status = STAT_DONE|STAT_BUSY;
422 /* some keyboard input waiting */
423 hdr->status = STAT_DONE;
427 hdr->status = STAT_DONE|STAT_BUSY;
432 /* flush line and keyboard queue */
433 lol->offs_unread_CON = 0;
434 bios->NextKbdCharPtr = bios->FirstKbdCharPtr;
439 REQ_IO *io = (REQ_IO *)hdr;
440 BYTE *buffer = CTX_SEG_OFF_TO_LIN(ctx,
441 SELECTOROF(io->buffer),
442 (DWORD)OFFSETOF(io->buffer));
444 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, io->count, &result, NULL);
446 hdr->status = STAT_DONE;
450 hdr->status = STAT_DONE;
455 static void InitListOfLists(DOS_LISTOFLISTS *DOS_LOL)
460 0133:0020 6A 13-33 01 CC 00 33 01 59 00 j.3...3.Y.
461 0133:0030 70 00 00 00 72 02 00 02-6D 00 33 01 00 00 2E 05 p...r...m.3.....
462 0133:0040 00 00 FC 04 00 00 03 08-92 21 11 E0 04 80 C6 0D .........!......
463 0133:0050 CC 0D 4E 55 4C 20 20 20-20 20 00 00 00 00 00 00 ..NUL ......
464 0133:0060 00 4B BA C1 06 14 00 00-00 03 01 00 04 70 CE FF .K...........p..
465 0133:0070 FF 00 00 00 00 00 00 00-00 01 00 00 0D 05 00 00 ................
466 0133:0080 00 FF FF 00 00 00 00 FE-00 00 F8 03 FF 9F 70 02 ..............p.
467 0133:0090 D0 44 C8 FD D4 44 C8 FD-D4 44 C8 FD D0 44 C8 FD .D...D...D...D..
468 0133:00A0 D0 44 C8 FD D0 44 .D...D
470 DOS_LOL->CX_Int21_5e01 = 0x0;
471 DOS_LOL->LRU_count_FCB_cache = 0x0;
472 DOS_LOL->LRU_count_FCB_open = 0x0;
473 DOS_LOL->OEM_func_handler = -1; /* not available */
474 DOS_LOL->INT21_offset = 0x0;
475 DOS_LOL->sharing_retry_count = 3;
476 DOS_LOL->sharing_retry_delay = 1;
477 DOS_LOL->ptr_disk_buf = 0x0;
478 DOS_LOL->offs_unread_CON = 0x0;
479 DOS_LOL->seg_first_MCB = 0x0;
480 DOS_LOL->ptr_first_DPB = 0x0;
481 DOS_LOL->ptr_first_SysFileTable = 0x0;
482 DOS_LOL->ptr_clock_dev_hdr = 0x0;
483 DOS_LOL->ptr_CON_dev_hdr = 0x0;
484 DOS_LOL->max_byte_per_sec = 512;
485 DOS_LOL->ptr_disk_buf_info = 0x0;
486 DOS_LOL->ptr_array_CDS = 0x0;
487 DOS_LOL->ptr_sys_FCB = 0x0;
488 DOS_LOL->nr_protect_FCB = 0x0;
489 DOS_LOL->nr_block_dev = 0x0;
490 DOS_LOL->nr_avail_drive_letters = 26; /* A - Z */
491 DOS_LOL->nr_drives_JOINed = 0x0;
492 DOS_LOL->ptr_spec_prg_names = 0x0;
493 DOS_LOL->ptr_SETVER_prg_list = 0x0; /* no SETVER list */
494 DOS_LOL->DOS_HIGH_A20_func_offs = 0x0;
495 DOS_LOL->PSP_last_exec = 0x0;
496 DOS_LOL->BUFFERS_val = 99; /* maximum: 99 */
497 DOS_LOL->BUFFERS_nr_lookahead = 8; /* maximum: 8 */
498 DOS_LOL->boot_drive = 3; /* C: */
499 DOS_LOL->flag_DWORD_moves = 0x01; /* i386+ */
500 DOS_LOL->size_extended_mem = 0xf000; /* very high value */
503 void DOSDEV_InstallDOSDevices(void)
505 DOS_DATASEG *dataseg;
510 /* allocate DOS data segment or something */
511 dataseg = DOSVM_AllocDataUMB( sizeof(DOS_DATASEG), &seg, &selector );
513 DOS_LOLSeg = MAKESEGPTR( seg, 0 );
514 DOSMEM_LOL()->wine_rm_lol =
515 MAKESEGPTR( seg, FIELD_OFFSET(DOS_LISTOFLISTS, ptr_first_DPB) );
516 DOSMEM_LOL()->wine_pm_lol =
517 MAKESEGPTR( selector, FIELD_OFFSET(DOS_LISTOFLISTS, ptr_first_DPB) );
519 /* initialize the magnificent List Of Lists */
520 InitListOfLists(&dataseg->lol);
522 /* Set up first device (NUL) */
523 dataseg->lol.NUL_dev.next_dev = MAKESEGPTR(seg, DOS_DATASEG_OFF(dev[0]));
524 dataseg->lol.NUL_dev.attr = devs[0].attr;
525 dataseg->lol.NUL_dev.strategy = DOS_DATASEG_OFF(thunk[0].ljmp1);
526 dataseg->lol.NUL_dev.interrupt = DOS_DATASEG_OFF(thunk[0].ljmp2);
527 memcpy(dataseg->lol.NUL_dev.name, devs[0].name, 8);
529 /* Set up the remaining devices */
530 for (n = 1; n < NR_DEVS; n++)
532 dataseg->dev[n-1].next_dev = (n+1) == NR_DEVS ? NONEXT :
533 MAKESEGPTR(seg, DOS_DATASEG_OFF(dev[n]));
534 dataseg->dev[n-1].attr = devs[n].attr;
535 dataseg->dev[n-1].strategy = DOS_DATASEG_OFF(thunk[n].ljmp1);
536 dataseg->dev[n-1].interrupt = DOS_DATASEG_OFF(thunk[n].ljmp2);
537 memcpy(dataseg->dev[n-1].name, devs[n].name, 8);
541 for (n = 0; n < NR_DEVS; n++)
543 dataseg->thunk[n].ljmp1 = LJMP;
544 dataseg->thunk[n].strategy = (RMCBPROC)DPMI_AllocInternalRMCB(devs[n].strategy);
545 dataseg->thunk[n].ljmp2 = LJMP;
546 dataseg->thunk[n].interrupt = (RMCBPROC)DPMI_AllocInternalRMCB(devs[n].interrupt);
549 /* CON is device 1 */
550 dataseg->lol.ptr_CON_dev_hdr = MAKESEGPTR(seg, DOS_DATASEG_OFF(dev[0]));
553 DWORD DOSDEV_Console(void)
555 return DOSMEM_LOL()->ptr_CON_dev_hdr;
558 DWORD DOSDEV_FindCharDevice(char*name)
560 SEGPTR cur_ptr = MAKESEGPTR(HIWORD(DOS_LOLSeg), FIELD_OFFSET(DOS_LISTOFLISTS,NUL_dev));
561 DOS_DEVICE_HEADER *cur = PTR_REAL_TO_LIN(SELECTOROF(cur_ptr),OFFSETOF(cur_ptr));
565 /* get first 8 characters */
566 /* if less than 8 characters, pad with spaces */
567 for (cnt=0; name[cnt] && cnt<8; cnt++)
568 dname[cnt]=name[cnt];
570 while(cnt<8) dname[cnt++] = ' ';
572 /* search for char devices with the right name */
574 ((!(cur->attr & ATTR_CHAR)) ||
575 memcmp(cur->name,dname,8))) {
576 cur_ptr = cur->next_dev;
577 if (cur_ptr == NONEXT) cur=NULL;
578 else cur = PTR_REAL_TO_LIN(SELECTOROF(cur_ptr),OFFSETOF(cur_ptr));
583 static void DOSDEV_DoReq(void*req, DWORD dev)
585 REQUEST_HEADER *hdr = (REQUEST_HEADER *)req;
586 DOS_DEVICE_HEADER *dhdr;
590 dhdr = PTR_REAL_TO_LIN(SELECTOROF(dev),OFFSETOF(dev));
591 phdr = ((char*)DOSMEM_LOL()) + DOS_DATASEG_OFF(req);
593 /* copy request to request scratch area */
594 memcpy(phdr, req, hdr->size);
596 /* prepare to call device driver */
597 memset(&ctx, 0, sizeof(ctx));
598 ctx.EFlags |= V86_FLAG;
600 /* ES:BX points to request for strategy routine */
601 ctx.SegEs = HIWORD(DOS_LOLSeg);
602 ctx.Ebx = DOS_DATASEG_OFF(req);
604 /* call strategy routine */
605 ctx.SegCs = SELECTOROF(dev);
606 ctx.Eip = dhdr->strategy;
607 DPMI_CallRMProc(&ctx, 0, 0, 0);
609 /* call interrupt routine */
610 ctx.SegCs = SELECTOROF(dev);
611 ctx.Eip = dhdr->interrupt;
612 DPMI_CallRMProc(&ctx, 0, 0, 0);
614 /* completed, copy request back */
615 memcpy(req, phdr, hdr->size);
617 if (hdr->status & STAT_ERROR) {
618 switch (hdr->status & STAT_MASK) {
619 case 0x0F: /* invalid disk change */
620 /* this error seems to fit the bill */
621 SetLastError(ERROR_NOT_SAME_DEVICE);
624 SetLastError((hdr->status & STAT_MASK) + 0x13);
630 static int DOSDEV_IO(unsigned cmd, DWORD dev, DWORD buf, int buflen)
634 req.hdr.size=sizeof(req);
635 req.hdr.unit=0; /* not dealing with block devices yet */
637 req.hdr.status=STAT_BUSY;
638 req.media=0; /* not dealing with block devices yet */
641 req.sector=0; /* block devices */
642 req.volume=0; /* block devices */
644 DOSDEV_DoReq(&req, dev);
649 int DOSDEV_Peek(DWORD dev, BYTE*data)
653 req.hdr.size=sizeof(req);
654 req.hdr.unit=0; /* not dealing with block devices yet */
655 req.hdr.command=CMD_SAFEINPUT;
656 req.hdr.status=STAT_BUSY;
659 DOSDEV_DoReq(&req, dev);
661 if (req.hdr.status & STAT_BUSY) return 0;
667 int DOSDEV_Read(DWORD dev, DWORD buf, int buflen)
669 return DOSDEV_IO(CMD_INPUT, dev, buf, buflen);
672 int DOSDEV_Write(DWORD dev, DWORD buf, int buflen, int verify)
674 return DOSDEV_IO(verify?CMD_SAFEOUTPUT:CMD_OUTPUT, dev, buf, buflen);
677 int DOSDEV_IoctlRead(DWORD dev, DWORD buf, int buflen)
679 return DOSDEV_IO(CMD_INIOCTL, dev, buf, buflen);
682 int DOSDEV_IoctlWrite(DWORD dev, DWORD buf, int buflen)
684 return DOSDEV_IO(CMD_OUTIOCTL, dev, buf, buflen);
687 void DOSDEV_SetSharingRetry(WORD delay, WORD count)
689 DOSMEM_LOL()->sharing_retry_delay = delay;
690 if (count) DOSMEM_LOL()->sharing_retry_count = count;
693 SEGPTR DOSDEV_GetLOL(BOOL v86)
695 if (v86) return DOSMEM_LOL()->wine_rm_lol;
696 else return DOSMEM_LOL()->wine_pm_lol;