Subversion Repositories SvarDOS

Rev

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

Rev 1594 Rev 1595
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-2022 Mateusz Viste
4
 * Copyright (C) 2021-2022 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 "rmodinit.h"
31
#include "rmodinit.h"
32
 
32
 
33
 
33
 
34
/* returns far pointer to rmod's settings block on success */
34
/* returns far pointer to rmod's settings block on success */
35
struct rmod_props far *rmod_install(unsigned short envsize, unsigned char *rmodcore, unsigned short rmodcore_len) {
35
struct rmod_props far *rmod_install(unsigned short envsize, unsigned char *rmodcore, unsigned short rmodcore_len) {
36
  char far *myptr, far *mcb;
36
  char far *myptr, far *mcb;
37
  unsigned short far *owner;
37
  unsigned short far *owner;
38
  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;
39
  unsigned short rmodseg = 0xffff;
39
  unsigned short rmodseg = 0xffff;
40
  unsigned short envseg, origenvseg;
40
  unsigned short envseg, origenvseg;
41
  struct rmod_props far *res;
41
  struct rmod_props far *res;
42
 
42
 
43
  /* read my current env segment from PSP and save it */
43
  /* read my current env segment from PSP and save it */
44
  envseg = *((unsigned short *)0x2c);
44
  envseg = *((unsigned short *)0x2c);
45
  origenvseg = envseg;
45
  origenvseg = envseg;
46
 
46
 
47
  /* printf("original (PSP) env buffer at %04X\r\n", envseg); */
47
  /* printf("original (PSP) env buffer at %04X\r\n", envseg); */
48
 
48
 
49
  /* 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) */
50
  if ((envseg == 0) && (envsize == 0)) envsize = 256;
50
  if ((envseg == 0) && (envsize == 0)) envsize = 256;
51
 
51
 
52
  /* if custom envsize requested, convert it to number of paragraphs */
52
  /* if custom envsize requested, convert it to number of paragraphs */
53
  if (envsize != 0) {
53
  if (envsize != 0) {
54
    envsize += 15;
54
    envsize += 15;
55
    envsize /= 16;
55
    envsize /= 16;
56
  }
56
  }
57
 
57
 
58
  _asm {
58
  _asm {
59
    push bx
59
    push bx
60
    push cx
60
    push cx
61
    push dx
61
    push dx
62
 
62
 
63
    /* link in the UMB memory chain for enabling high-memory allocation (and save initial status on stack) */
63
    /* link in the UMB memory chain for enabling high-memory allocation (and save initial status on stack) */
64
    mov ax, 0x5802  /* GET UMB LINK STATE */
64
    mov ax, 0x5802  /* GET UMB LINK STATE */
65
    int 0x21
65
    int 0x21
66
    xor ah, ah
66
    xor ah, ah
67
    push ax         /* save link state on stack */
67
    push ax         /* save link state on stack */
68
    mov ax, 0x5803  /* SET UMB LINK STATE */
68
    mov ax, 0x5803  /* SET UMB LINK STATE */
69
    mov bx, 1
69
    mov bx, 1
70
    int 0x21
70
    int 0x21
71
    /* get current allocation strategy and save it in DX */
71
    /* get current allocation strategy and save it in DX */
72
    mov ax, 0x5800
72
    mov ax, 0x5800
73
    int 0x21
73
    int 0x21
74
    push ax
74
    push ax
75
    pop dx
75
    pop dx
76
    /* set strategy to 'last fit, try high then low memory' */
76
    /* set strategy to 'last fit, try high then low memory' */
77
    mov ax, 0x5801
77
    mov ax, 0x5801
78
    mov bx, 0x0082
78
    mov bx, 0x0082
79
    int 0x21
79
    int 0x21
80
    /* ask for a memory block and save the given segment to rmodseg */
80
    /* ask for a memory block and save the given segment to rmodseg */
81
    mov ah, 0x48
81
    mov ah, 0x48
82
    mov bx, sizeof_rmodandprops_paras
82
    mov bx, sizeof_rmodandprops_paras
83
    int 0x21
83
    int 0x21
84
    jc ALLOC_FAIL
84
    jc ALLOC_FAIL
85
    mov rmodseg, ax
85
    mov rmodseg, ax
86
    /* ask for a memory block for the environment and save it to envseg (only if custom size requested) */
86
    /* ask for a memory block for the environment and save it to envseg (only if custom size requested) */
87
    mov bx, envsize
87
    mov bx, envsize
88
    test bx, bx
88
    test bx, bx
89
    jz ALLOC_FAIL
89
    jz ALLOC_FAIL
90
    mov ah, 0x48
90
    mov ah, 0x48
91
    int 0x21
91
    int 0x21
92
    jc ALLOC_FAIL
92
    jc ALLOC_FAIL
93
    mov envseg, ax
93
    mov envseg, ax
94
 
94
 
95
    ALLOC_FAIL:
95
    ALLOC_FAIL:
96
    /* restore initial allocation strategy */
96
    /* restore initial allocation strategy */
97
    mov ax, 0x5801
97
    mov ax, 0x5801
98
    mov bx, dx
98
    mov bx, dx
99
    int 0x21
99
    int 0x21
100
    /* restore initial UMB memory link state */
100
    /* restore initial UMB memory link state */
101
    mov ax, 0x5803
101
    mov ax, 0x5803
102
    pop bx       /* pop initial UMB link state from stack */
102
    pop bx       /* pop initial UMB link state from stack */
103
    int 0x21
103
    int 0x21
104
 
104
 
105
    pop dx
105
    pop dx
106
    pop cx
106
    pop cx
107
    pop bx
107
    pop bx
108
  }
108
  }
109
 
109
 
110
  if (rmodseg == 0xffff) {
110
  if (rmodseg == 0xffff) {
111
    outputnl("malloc error");
111
    outputnl("malloc error");
112
    return(NULL);
112
    return(NULL);
113
  }
113
  }
114
 
114
 
115
  /* generate a new PSP where RMOD is about to land */
115
  /* generate a new PSP where RMOD is about to land */
116
  _asm {
116
  _asm {
117
    push dx
117
    push dx
118
    mov ah, 0x26 /* CREATE NEW PROGRAM SEGMENT PREFIX (DOS 1+) */
118
    mov ah, 0x26 /* CREATE NEW PROGRAM SEGMENT PREFIX (DOS 1+) */
119
    mov dx, rmodseg
119
    mov dx, rmodseg
120
    int 0x21
120
    int 0x21
121
    pop dx
121
    pop dx
122
  }
122
  }
123
 
123
 
124
  myptr = MK_FP(rmodseg, 0);
124
  myptr = MK_FP(rmodseg, 0);
125
 
125
 
126
  /* patch up RMOD's PSP: Parent's PSP segment @ 0x16-0x17 */
126
  /* patch up RMOD's PSP: Parent's PSP segment @ 0x16-0x17 */
127
  myptr[0x16] = rmodseg & 0xff; /* RMOD is his own parent */
127
  myptr[0x16] = rmodseg & 0xff; /* RMOD is his own parent */
128
  myptr[0x17] = rmodseg >> 8;
128
  myptr[0x17] = rmodseg >> 8;
129
 
129
 
130
  /* patch up RMOD's PSP: SS:SP pointer @ 0x2E-0x31  --  I abuse the PSP's
130
  /* patch up RMOD's PSP: SS:SP pointer @ 0x2E-0x31  --  I abuse the PSP's
131
   * command line tail as stack, but I do NOT set the stack at the end of the
131
   * command line tail as stack, but I do NOT set the stack at the end of the
132
   * tail. E. C. Masloch kindly explained why this would be a bad idea:
132
   * tail. E. C. Masloch kindly explained why this would be a bad idea:
133
   *
133
   *
134
   * "This is wrong and will potentially overwrite part of your buffers that
134
   * "This is wrong and will potentially overwrite part of your buffers that
135
   * start past the PSP. This is because the dword [PSP:2Eh] is not used merely
135
   * start past the PSP. This is because the dword [PSP:2Eh] is not used merely
136
   * to set SS:SP but rather to find the stack frame created by the int 21h
136
   * to set SS:SP but rather to find the stack frame created by the int 21h
137
   * call. Therefore the int 21h call that terminates the child process will
137
   * call. Therefore the int 21h call that terminates the child process will
138
   * then pop a number of registers off starting from the address stored in the
138
   * then pop a number of registers off starting from the address stored in the
139
   * PSP." <https://github.com/SvarDOS/bugz/issues/38#issuecomment-1817445740>
139
   * PSP." <https://github.com/SvarDOS/bugz/issues/38#issuecomment-1817445740>
140
   */
140
   */
141
  myptr[0x2e] = 192; /* middle of the command line tail area so I have 64 bytes */
141
  myptr[0x2e] = 192; /* middle of the command line tail area so I have 64 bytes */
142
  myptr[0x2f] = 0;   /* before and 64 bytes in front of me */
142
  myptr[0x2f] = 0;   /* before and 64 bytes in front of me */
143
  myptr[0x30] = rmodseg & 0xff;
143
  myptr[0x30] = rmodseg & 0xff;
144
  myptr[0x31] = rmodseg >> 8;
144
  myptr[0x31] = rmodseg >> 8;
145
 
145
 
146
  /* patch up RMOD's PSP: JFT size @ 0x32-0x33 */
146
  /* patch up RMOD's PSP: JFT size @ 0x32-0x33 */
147
  myptr[0x32] = 20; /* default JFT size (max that fits without an extra allocation) */
147
  myptr[0x32] = 20; /* default JFT size (max that fits without an extra allocation) */
148
  myptr[0x33] = 0;
148
  myptr[0x33] = 0;
149
 
149
 
150
  /* patch up RMOD's PSP: JFT pointer @ 0x34-0x37 */
150
  /* patch up RMOD's PSP: JFT pointer @ 0x34-0x37 */
151
  myptr[0x34] = 0x18; /* the JFT is in the PSP itself */
151
  myptr[0x34] = 0x18; /* the JFT is in the PSP itself */
152
  myptr[0x35] = 0;
152
  myptr[0x35] = 0;
153
  myptr[0x36] = rmodseg & 0xff;
153
  myptr[0x36] = rmodseg & 0xff;
154
  myptr[0x37] = rmodseg >> 8;
154
  myptr[0x37] = rmodseg >> 8;
155
 
155
 
156
  /* patch up RMOD's PSP: pointer to previous PSP @ 0x38-0x3B */
156
  /* patch up RMOD's PSP: pointer to previous PSP @ 0x38-0x3B */
157
  myptr[0x38] = 0;
157
  myptr[0x38] = 0;
158
  myptr[0x39] = 0;
158
  myptr[0x39] = 0;
159
  myptr[0x3A] = rmodseg & 0xff;
159
  myptr[0x3A] = rmodseg & 0xff;
160
  myptr[0x3B] = rmodseg >> 8;
160
  myptr[0x3B] = rmodseg >> 8;
161
 
161
 
162
  /* copy rmod to its destination (right past the PSP I prepared) */
162
  /* copy rmod to its destination (right past the PSP I prepared) */
163
  myptr = MK_FP(rmodseg, 0x100);
163
  myptr = MK_FP(rmodseg, 0x100);
164
  _fmemcpy(myptr, rmodcore, rmodcore_len);
164
  _fmemcpy(myptr, rmodcore, rmodcore_len);
165
 
165
 
166
  /* mark rmod memory (MCB) as "self owned" */
166
  /* mark rmod memory (MCB) as "self owned" */
167
  mcb = MK_FP(rmodseg - 1, 0);
167
  mcb = MK_FP(rmodseg - 1, 0);
168
  owner = (void far *)(mcb + 1);
168
  owner = (void far *)(mcb + 1);
169
  *owner = rmodseg;
169
  *owner = rmodseg;
170
  _fmemcpy(mcb + 8, "SVARCOM", 8);
170
  _fmemcpy(mcb + 8, "SVARCOM", 8);
171
 
171
 
172
  /* mark env memory (MCB) as "owned by rmod" */
172
  /* mark env memory (MCB) as "owned by rmod" */
173
  mcb = MK_FP(envseg - 1, 0);
173
  mcb = MK_FP(envseg - 1, 0);
174
  owner = (void far *)(mcb + 1);
174
  owner = (void far *)(mcb + 1);
175
  *owner = rmodseg;
175
  *owner = rmodseg;
176
  _fmemcpy(mcb + 8, "SVARENV", 8);
176
  _fmemcpy(mcb + 8, "SVARENV", 8);
177
 
177
 
178
  /* if env block is newly allocated, fill it with a few NULLs */
178
  /* if env block is newly allocated, fill it with a few NULLs */
179
  if (envsize != 0) {
179
  if (envsize != 0) {
180
    owner = MK_FP(envseg, 0);
180
    owner = MK_FP(envseg, 0);
181
    owner[0] = 0;
181
    owner[0] = 0;
182
    owner[1] = 0;
182
    owner[1] = 0;
183
  }
183
  }
184
 
184
 
185
  /* set CTRL+BREAK handler to rmod */
185
  /* set CTRL+BREAK handler to rmod */
186
  _asm {
186
  _asm {
187
    push ax
187
    push ax
188
    push dx
188
    push dx
189
    push ds
189
    push ds
190
    mov ax, 0x2523
190
    mov ax, 0x2523
191
    mov ds, rmodseg
191
    mov ds, rmodseg
192
    mov dx, RMOD_OFFSET_BRKHANDLER
192
    mov dx, RMOD_OFFSET_BRKHANDLER
193
    int 0x21
193
    int 0x21
194
    pop ds
194
    pop ds
195
    pop dx
195
    pop dx
196
    pop ax
196
    pop ax
197
  }
197
  }
198
 
198
 
199
  /* mark the input buffer as empty */
199
  /* mark the input buffer as empty */
200
  myptr = MK_FP(rmodseg, RMOD_OFFSET_INPUTBUF);
200
  myptr = MK_FP(rmodseg, RMOD_OFFSET_INPUTBUF);
201
  myptr[0] = 128;  /* max acceptable length */
201
  myptr[0] = 128;  /* max acceptable length */
202
  myptr[1] = 0;    /* len of currently stored history string */
202
  myptr[1] = 0;    /* len of currently stored history string */
203
  myptr[2] = '\r'; /* string terminator */
203
  myptr[2] = '\r'; /* string terminator */
204
  myptr[3] = 0xCA; /* signature to detect stack overflow damaging the buffer */
204
  myptr[3] = 0xCA; /* signature to detect stack overflow damaging the buffer */
205
  myptr[4] = 0xFE; /* 2nd byte of the signature */
205
  myptr[4] = 0xFE; /* 2nd byte of the signature */
206
 
206
 
207
  /* prepare result (rmod props) */
207
  /* prepare result (rmod props) */
208
  res = MK_FP(rmodseg, 0x100 + rmodcore_len);
208
  res = MK_FP(rmodseg, 0x100 + rmodcore_len);
209
  _fmemset(res, 0, sizeof(*res));  /* zero out */
209
  _fmemset(res, 0, sizeof(*res));  /* zero out */
210
  res->rmodseg = rmodseg;          /* rmod segment */
210
  res->rmodseg = rmodseg;          /* rmod segment */
211
  res->origenvseg = origenvseg;    /* original environment segment */
211
  res->origenvseg = origenvseg;    /* original environment segment */
212
 
212
 
213
  /* write env segment to rmod's PSP */
213
  /* write env segment to rmod's PSP */
214
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
214
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
215
  *owner = envseg;
215
  *owner = envseg;
216
 
216
 
217
  /* write boot drive to rmod bootdrive field */
217
  /* write boot drive to rmod bootdrive field */
218
  _asm {
218
  _asm {
219
    push ax
219
    push ax
220
    push bx
220
    push bx
221
    push dx
221
    push dx
222
    push ds
222
    push ds
223
    mov ax, 0x3305 /* DOS 4.0+ - GET BOOT DRIVE */
223
    mov ax, 0x3305 /* DOS 4.0+ - GET BOOT DRIVE */
224
    int 0x21 /* boot drive is in DL now (1=A:, 2=B:, etc) */
224
    int 0x21 /* boot drive is in DL now (1=A:, 2=B:, etc) */
225
    add dl, 'A'-1 /* convert to a proper ASCII letter */
225
    add dl, 'A'-1 /* convert to a proper ASCII letter */
226
    /* set DS to rmodseg */
226
    /* set DS to rmodseg */
227
    mov ax, rmodseg
227
    mov ax, rmodseg
228
    mov ds, ax
228
    mov ds, ax
229
    /* write boot drive to rmod bootdrive field */
229
    /* write boot drive to rmod bootdrive field */
230
    mov bx, RMOD_OFFSET_BOOTDRIVE
230
    mov bx, RMOD_OFFSET_BOOTDRIVE
231
    mov [bx], dl
231
    mov [bx], dl
232
    pop ds
232
    pop ds
233
    pop dx
233
    pop dx
234
    pop bx
234
    pop bx
235
    pop ax
235
    pop ax
236
  }
236
  }
237
 
237
 
238
  /* save my original parent in rmod's memory */
238
  /* save my original parent in rmod's memory */
239
  res->origparent = *((unsigned long *)0x0a); /* original parent seg:off is at 0x0a of my PSP */
239
  res->origparent = *((unsigned long *)0x0a); /* original parent seg:off is at 0x0a of my PSP */
240
 
240
 
241
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I
241
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I
242
   * terminate and save the original handler in rmod's memory */
242
   * terminate and save the original handler in rmod's memory */
243
  {
243
  {
244
    unsigned short *ptr = (void *)0x0a; /* int22 handler is at 0x0A of the PSP */
244
    unsigned short *ptr = (void *)0x0a; /* int22 handler is at 0x0A of the PSP */
245
    ptr[0] = RMOD_OFFSET_ROUTINE;
245
    ptr[0] = RMOD_OFFSET_ROUTINE;
246
    ptr[1] = rmodseg;
246
    ptr[1] = rmodseg;
247
  }
247
  }
248
 
248
 
-
 
249
  /* set my own parent to RMOD (this is not necessary for MS-DOS nor FreeDOS but
-
 
250
   * might be on other DOS implementations) */
-
 
251
  {
-
 
252
    unsigned short *ptr = (void *)0x16;
-
 
253
    *ptr = rmodseg;
-
 
254
  }
-
 
255
 
249
  return(res);
256
  return(res);
250
}
257
}
251
 
258
 
252
 
259
 
253
/* look up my parent: if it's rmod then return a ptr to its props struct,
260
/* look up my parent: if it's rmod then return a ptr to its props struct,
254
 * otherwise return NULL
261
 * otherwise return NULL
255
 * I look at PSP[Ch] to locate RMOD (ie. the "terminate address") */
262
 * I look at PSP[Ch] to locate RMOD (ie. the "terminate address") */
256
struct rmod_props far *rmod_find(unsigned short rmodcore_len) {
263
struct rmod_props far *rmod_find(unsigned short rmodcore_len) {
257
  unsigned short *parent = (void *)0x0C;
264
  unsigned short *parent = (void *)0x0C;
258
  unsigned short far *ptr;
265
  unsigned short far *ptr;
259
  const unsigned short sig[] = {0x1983, 0x1985, 0x2017, 0x2019};
266
  const unsigned short sig[] = {0x1983, 0x1985, 0x2017, 0x2019};
260
  unsigned char *cmdtail = (void *)0x80;
267
  unsigned char *cmdtail = (void *)0x80;
261
  unsigned char i;
268
  unsigned char i;
262
  /* is it rmod? */
269
  /* is it rmod? */
263
  ptr = MK_FP(*parent, 0x100);
270
  ptr = MK_FP(*parent, 0x100);
264
  for (i = 0; i < 4; i++) if (ptr[i] != sig[i]) return(NULL);
271
  for (i = 0; i < 4; i++) if (ptr[i] != sig[i]) return(NULL);
265
  /* match successfull (rmod is my parent) - but is it really a respawn?
272
  /* match successfull (rmod is my parent) - but is it really a respawn?
266
   * command-line tail should contain a single character '\r' */
273
   * command-line tail should contain a single character '\r' */
267
  if ((cmdtail[0] != 1) || (cmdtail[1] != '\n')) return(NULL);
274
  if ((cmdtail[0] != 1) || (cmdtail[1] != '\n')) return(NULL);
268
  cmdtail[0] = 0;
275
  cmdtail[0] = 0;
269
  cmdtail[1] = '\r';
276
  cmdtail[1] = '\r';
270
  /* */
277
  /* */
271
  return(MK_FP(*parent, 0x100 + rmodcore_len));
278
  return(MK_FP(*parent, 0x100 + rmodcore_len));
272
}
279
}
273
 
280
 
274
 
281
 
275
/* update rmod's pointer to comspec */
282
/* update rmod's pointer to comspec */
276
void rmod_updatecomspecptr(unsigned short rmod_seg, unsigned short env_seg) {
283
void rmod_updatecomspecptr(unsigned short rmod_seg, unsigned short env_seg) {
277
  unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
284
  unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
278
  char far *comspecfp = env_lookup_val(env_seg, "COMSPEC");
285
  char far *comspecfp = env_lookup_val(env_seg, "COMSPEC");
279
  if (comspecfp != NULL) {
286
  if (comspecfp != NULL) {
280
    *comspecptr = FP_OFF(comspecfp);
287
    *comspecptr = FP_OFF(comspecfp);
281
  } else {
288
  } else {
282
    *comspecptr = 0;
289
    *comspecptr = 0;
283
  }
290
  }
284
}
291
}
285
 
292
 
286
 
293
 
287
/* allocates bytes of far memory, flags it as belonging to rmod
294
/* allocates bytes of far memory, flags it as belonging to rmod
288
 * the new block can be optionally flagged as 'ident' (if not null) and zero
295
 * the new block can be optionally flagged as 'ident' (if not null) and zero
289
 * out the newly allocated memory.
296
 * out the newly allocated memory.
290
 * returns a far ptr to the allocated block, or NULL on error */
297
 * returns a far ptr to the allocated block, or NULL on error */
291
void far *rmod_fcalloc(unsigned short bytes, unsigned short rmod_seg, char *ident) {
298
void far *rmod_fcalloc(unsigned short bytes, unsigned short rmod_seg, char *ident) {
292
  unsigned short far *owner;
299
  unsigned short far *owner;
293
  unsigned short newseg = 0;
300
  unsigned short newseg = 0;
294
 
301
 
295
  /* ask DOS for a memory block (as high as possible) */
302
  /* ask DOS for a memory block (as high as possible) */
296
  _asm {
303
  _asm {
297
    push bx /* save initial value in BX so I can restore it later */
304
    push bx /* save initial value in BX so I can restore it later */
298
 
305
 
299
    /* get current allocation strategy and save it on stack */
306
    /* get current allocation strategy and save it on stack */
300
    mov ax, 0x5800
307
    mov ax, 0x5800
301
    int 0x21
308
    int 0x21
302
    push ax
309
    push ax
303
 
310
 
304
    /* set strategy to 'last fit, try high then low memory' */
311
    /* set strategy to 'last fit, try high then low memory' */
305
    mov ax, 0x5801
312
    mov ax, 0x5801
306
    mov bx, 0x0082
313
    mov bx, 0x0082
307
    int 0x21
314
    int 0x21
308
 
315
 
309
    /* ask for a memory block and save the given segment to rmodseg */
316
    /* ask for a memory block and save the given segment to rmodseg */
310
    mov ah, 0x48  /* Allocate Memory */
317
    mov ah, 0x48  /* Allocate Memory */
311
    mov bx, bytes
318
    mov bx, bytes
312
    add bx, 15    /* convert bytes to paragraphs */
319
    add bx, 15    /* convert bytes to paragraphs */
313
    shr bx, 1     /* bx /= 16 */
320
    shr bx, 1     /* bx /= 16 */
314
    shr bx, 1
321
    shr bx, 1
315
    shr bx, 1
322
    shr bx, 1
316
    shr bx, 1
323
    shr bx, 1
317
    int 0x21
324
    int 0x21
318
 
325
 
319
    /* error handling */
326
    /* error handling */
320
    jc FAIL
327
    jc FAIL
321
 
328
 
322
    /* save newly allocated segment to newseg */
329
    /* save newly allocated segment to newseg */
323
    mov newseg, ax
330
    mov newseg, ax
324
 
331
 
325
    FAIL:
332
    FAIL:
326
    /* restore initial allocation strategy */
333
    /* restore initial allocation strategy */
327
    mov ax, 0x5801
334
    mov ax, 0x5801
328
    pop bx
335
    pop bx
329
    int 0x21
336
    int 0x21
330
 
337
 
331
    pop bx /* restore BX to its initial value */
338
    pop bx /* restore BX to its initial value */
332
  }
339
  }
333
 
340
 
334
  if (newseg == 0) return(NULL);
341
  if (newseg == 0) return(NULL);
335
 
342
 
336
  /* mark memory as "owned by rmod" */
343
  /* mark memory as "owned by rmod" */
337
  owner = (void far *)(MK_FP(newseg - 1, 1));
344
  owner = (void far *)(MK_FP(newseg - 1, 1));
338
  *owner = rmod_seg;
345
  *owner = rmod_seg;
339
 
346
 
340
  /* set the MCB description to ident, if provided */
347
  /* set the MCB description to ident, if provided */
341
  if (ident) {
348
  if (ident) {
342
    char far *mcbdesc = MK_FP(newseg - 1, 8);
349
    char far *mcbdesc = MK_FP(newseg - 1, 8);
343
    int i;
350
    int i;
344
    _fmemset(mcbdesc, 0, 8);
351
    _fmemset(mcbdesc, 0, 8);
345
    for (i = 0; (i < 8) && (ident[i] != 0); i++) { /* field's length is limited to 8 bytes max */
352
    for (i = 0; (i < 8) && (ident[i] != 0); i++) { /* field's length is limited to 8 bytes max */
346
      mcbdesc[i] = ident[i];
353
      mcbdesc[i] = ident[i];
347
    }
354
    }
348
  }
355
  }
349
 
356
 
350
  /* zero out the memory before handing it out */
357
  /* zero out the memory before handing it out */
351
  _fmemset(MK_FP(newseg, 0), 0, bytes);
358
  _fmemset(MK_FP(newseg, 0), 0, bytes);
352
 
359
 
353
  return(MK_FP(newseg, 0));
360
  return(MK_FP(newseg, 0));
354
}
361
}
355
 
362
 
356
 
363
 
357
/* free memory previously allocated by rmod_fcalloc() */
364
/* free memory previously allocated by rmod_fcalloc() */
358
void rmod_ffree(void far *ptr) {
365
void rmod_ffree(void far *ptr) {
359
  unsigned short ptrseg;
366
  unsigned short ptrseg;
360
  unsigned short myseg = 0;
367
  unsigned short myseg = 0;
361
  unsigned short far *owner;
368
  unsigned short far *owner;
362
  if (ptr == NULL) return;
369
  if (ptr == NULL) return;
363
  ptrseg = FP_SEG(ptr);
370
  ptrseg = FP_SEG(ptr);
364
 
371
 
365
  /* get my own segment */
372
  /* get my own segment */
366
  _asm {
373
  _asm {
367
    mov myseg, cs
374
    mov myseg, cs
368
  }
375
  }
369
 
376
 
370
  /* mark memory in MCB as my own, otherwise DOS might refuse to free it */
377
  /* mark memory in MCB as my own, otherwise DOS might refuse to free it */
371
  owner = MK_FP(ptrseg - 1, 1);
378
  owner = MK_FP(ptrseg - 1, 1);
372
  *owner = myseg;
379
  *owner = myseg;
373
 
380
 
374
  /* free the memory block */
381
  /* free the memory block */
375
  _asm {
382
  _asm {
376
    push es
383
    push es
377
    mov ah, 0x49  /* Free Memory Block */
384
    mov ah, 0x49  /* Free Memory Block */
378
    mov es, ptrseg
385
    mov es, ptrseg
379
    int 0x21
386
    int 0x21
380
    pop es
387
    pop es
381
  }
388
  }
382
}
389
}
383
 
390
 
384
 
391
 
385
/* free the entire linked list of bat ctx nodes (and set its rmod ptr to NULL) */
392
/* free the entire linked list of bat ctx nodes (and set its rmod ptr to NULL) */
386
void rmod_free_bat_llist(struct rmod_props far *rmod) {
393
void rmod_free_bat_llist(struct rmod_props far *rmod) {
387
  while (rmod->bat != NULL) {
394
  while (rmod->bat != NULL) {
388
    struct batctx far *victim = rmod->bat;
395
    struct batctx far *victim = rmod->bat;
389
    rmod->bat = rmod->bat->parent;
396
    rmod->bat = rmod->bat->parent;
390
    rmod_ffree(victim);
397
    rmod_ffree(victim);
391
  }
398
  }
392
}
399
}
393
 
400