Subversion Repositories SvarDOS

Rev

Rev 478 | Rev 949 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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