Subversion Repositories SvarDOS

Rev

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