Subversion Repositories SvarDOS

Rev

Rev 454 | Rev 465 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
421 mateuszvis 1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
23
 */
351 mateuszvis 24
 
25
#include <i86.h>
26
#include <string.h>
27
 
367 mateuszvis 28
#include "env.h"
369 mateuszvis 29
#include "helpers.h"
367 mateuszvis 30
 
351 mateuszvis 31
#include "rmod.h"
32
 
33
#include "rmodinit.h"
34
 
35
 
449 mateuszvis 36
/* returns far pointer to rmod's settings block on success */
37
struct rmod_props far *rmod_install(unsigned short envsize) {
351 mateuszvis 38
  char far *myptr, far *mcb;
39
  unsigned short far *owner;
459 mateuszvis 40
  const unsigned short sizeof_rmodandprops_paras = (0x100 + rmod_len + sizeof(struct rmod_props) + 15) / 16;
453 mateuszvis 41
  unsigned short rmodseg = 0xffff;
454 mateuszvis 42
  unsigned short envseg, origenvseg;
449 mateuszvis 43
  struct rmod_props far *res = NULL;
351 mateuszvis 44
 
453 mateuszvis 45
  /* read my current env segment from PSP and save it */
454 mateuszvis 46
  envseg = *((unsigned short *)0x2c);
47
  origenvseg = envseg;
351 mateuszvis 48
 
369 mateuszvis 49
  /* printf("original (PSP) env buffer at %04X\r\n", envseg); */
449 mateuszvis 50
 
51
  /* if envseg is zero, then enforce our own one (MSDOS 5 does not provide a default env) */
52
  if ((envseg == 0) && (envsize == 0)) envsize = 256;
53
 
351 mateuszvis 54
  /* if custom envsize requested, convert it to number of paragraphs */
55
  if (envsize != 0) {
56
    envsize += 15;
57
    envsize /= 16;
58
  }
59
 
60
  _asm {
61
    /* link in the UMB memory chain for enabling high-memory allocation (and save initial status on stack) */
62
    mov ax, 0x5802  /* GET UMB LINK STATE */
63
    int 0x21
64
    xor ah, ah
65
    push ax         /* save link state on stack */
66
    mov ax, 0x5803  /* SET UMB LINK STATE */
67
    mov bx, 1
68
    int 0x21
69
    /* get current allocation strategy and save it in DX */
70
    mov ax, 0x5800
71
    int 0x21
72
    push ax
73
    pop dx
74
    /* set strategy to 'last fit, try high then low memory' */
75
    mov ax, 0x5801
76
    mov bx, 0x0082
77
    int 0x21
78
    /* ask for a memory block and save the given segment to rmodseg */
79
    mov ah, 0x48
449 mateuszvis 80
    mov bx, sizeof_rmodandprops_paras
351 mateuszvis 81
    int 0x21
82
    jc ALLOC_FAIL
83
    mov rmodseg, ax
84
    /* ask for a memory block for the environment and save it to envseg (only if custom size requested) */
85
    mov bx, envsize
86
    test bx, bx
87
    jz ALLOC_FAIL
88
    mov ah, 0x48
89
    int 0x21
90
    jc ALLOC_FAIL
91
    mov envseg, ax
92
 
93
    ALLOC_FAIL:
94
    /* restore initial allocation strategy */
95
    mov ax, 0x5801
96
    mov bx, dx
97
    int 0x21
98
    /* restore initial UMB memory link state */
99
    mov ax, 0x5803
100
    pop bx       /* pop initial UMB link state from stack */
101
    int 0x21
102
  }
103
 
104
  if (rmodseg == 0xffff) {
369 mateuszvis 105
    outputnl("malloc error");
449 mateuszvis 106
    return(NULL);
351 mateuszvis 107
  }
108
 
459 mateuszvis 109
  /* copy rmod to its destination, prefixed with a copy of my own PSP */
351 mateuszvis 110
  myptr = MK_FP(rmodseg, 0);
459 mateuszvis 111
  {
112
    unsigned short i;
113
    char *mypsp = (void *)0;
114
    for (i = 0; i < 0x100; i++) myptr[i] = mypsp[i];
115
  }
116
  myptr = MK_FP(rmodseg, 0x100);
351 mateuszvis 117
  _fmemcpy(myptr, rmod, rmod_len);
118
 
119
  /* mark rmod memory as "self owned" */
120
  mcb = MK_FP(rmodseg - 1, 0);
121
  owner = (void far *)(mcb + 1);
122
  *owner = rmodseg;
123
  _fmemcpy(mcb + 8, "SVARCOM", 8);
124
 
448 mateuszvis 125
  /* mark env memory as "owned by rmod" */
359 mateuszvis 126
  mcb = MK_FP(envseg - 1, 0);
127
  owner = (void far *)(mcb + 1);
128
  *owner = rmodseg;
129
  _fmemcpy(mcb + 8, "SVARENV", 8);
351 mateuszvis 130
 
449 mateuszvis 131
  /* if env block is newly allocated, fill it with a few NULLs */
132
  if (envsize != 0) {
133
    owner = MK_FP(envseg, 0);
134
    owner[0] = 0;
135
    owner[1] = 0;
136
  }
137
 
138
  /* prepare result (rmod props) */
459 mateuszvis 139
  res = MK_FP(rmodseg, 0x100 + rmod_len);
450 mateuszvis 140
  _fmemset(res, 0, sizeof(*res));  /* zero out */
141
  res->rmodseg = rmodseg;          /* rmod segment */
142
  res->inputbuf[0] = 128;          /* input buffer for INT 0x21, AH=0Ah*/
143
  res->echoflag = 1;               /* ECHO ON */
453 mateuszvis 144
  res->origenvseg = origenvseg;    /* original environment segment */
449 mateuszvis 145
 
459 mateuszvis 146
  /* write env segment to rmod's PSP */
351 mateuszvis 147
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
148
  *owner = envseg;
149
 
366 mateuszvis 150
  /* write boot drive to rmod bootdrive field */
151
  _asm {
152
    push ax
153
    push bx
154
    push dx
155
    push ds
156
    mov ax, 0x3305 /* DOS 4.0+ - GET BOOT DRIVE */
157
    int 0x21 /* boot drive is in DL now (1=A:, 2=B:, etc) */
158
    add dl, 'A'-1 /* convert to a proper ASCII letter */
159
    /* set DS to rmodseg */
160
    mov ax, rmodseg
161
    mov ds, ax
162
    /* write boot drive to rmod bootdrive field */
163
    mov bx, RMOD_OFFSET_BOOTDRIVE
164
    mov [bx], dl
165
    pop ds
166
    pop dx
167
    pop bx
168
    pop ax
169
  }
170
 
450 mateuszvis 171
  /* save my original parent in rmod's memory */
172
  res->origparent = *((unsigned long *)0x0a); /* original parent seg:off is at 0x0a of my PSP */
173
 
448 mateuszvis 174
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I
175
   * terminate and save the original handler in rmod's memory */
454 mateuszvis 176
  {
177
    unsigned short *ptr = (void *)0x0a; /* int22 handler is at 0x0A of the PSP */
178
    ptr[0] = RMOD_OFFSET_ROUTINE;
179
    ptr[1] = rmodseg;
351 mateuszvis 180
  }
181
 
449 mateuszvis 182
  return(res);
351 mateuszvis 183
}
184
 
185
 
449 mateuszvis 186
/* look up my parent: if it's rmod then return a ptr to its props struct,
187
 * otherwise return NULL */
188
struct rmod_props far *rmod_find(void) {
448 mateuszvis 189
  unsigned short *parent = (void *)0x0C; /* parent's seg in PSP[Ch] ("prev. int22 handler") */
190
  unsigned short far *ptr;
191
  const unsigned short sig[] = {0x1983, 0x1985, 0x2017, 0x2019};
192
  unsigned char i;
193
  /* is it rmod? */
459 mateuszvis 194
  ptr = MK_FP(*parent, 0x100);
449 mateuszvis 195
  for (i = 0; i < 4; i++) if (ptr[i] != sig[i]) return(NULL);
448 mateuszvis 196
  /* match successfull (rmod is my parent) */
459 mateuszvis 197
  return(MK_FP(*parent, 0x100 + rmod_len));
351 mateuszvis 198
}
367 mateuszvis 199
 
200
 
201
/* update rmod's pointer to comspec */
202
void rmod_updatecomspecptr(unsigned short rmod_seg, unsigned short env_seg) {
203
  unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
439 mateuszvis 204
  char far *comspecfp = env_lookup_val(env_seg, "COMSPEC");
367 mateuszvis 205
  if (comspecfp != NULL) {
439 mateuszvis 206
    *comspecptr = FP_OFF(comspecfp);
367 mateuszvis 207
  } else {
208
    *comspecptr = 0;
209
  }
210
}