Subversion Repositories SvarDOS

Rev

Rev 1863 | 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
 
1863 mateusz.vi 195
  /* set CTRL+BREAK and CRITERR handlers to rmod */
537 mateuszvis 196
  _asm {
197
    push ax
198
    push dx
199
    push ds
1863 mateusz.vi 200
 
201
    mov ds, rmodseg
202
 
537 mateuszvis 203
    mov ax, 0x2523
204
    mov dx, RMOD_OFFSET_BRKHANDLER
205
    int 0x21
1863 mateusz.vi 206
 
207
    mov ax, 0x2524
208
    mov dx, RMOD_OFFSET_CRITHANDLER
209
    int 0x21
210
 
537 mateuszvis 211
    pop ds
212
    pop dx
213
    pop ax
214
  }
215
 
983 mateusz.vi 216
  /* mark the input buffer as empty */
217
  myptr = MK_FP(rmodseg, RMOD_OFFSET_INPUTBUF);
987 mateusz.vi 218
  myptr[0] = 128;  /* max acceptable length */
219
  myptr[1] = 0;    /* len of currently stored history string */
220
  myptr[2] = '\r'; /* string terminator */
221
  myptr[3] = 0xCA; /* signature to detect stack overflow damaging the buffer */
222
  myptr[4] = 0xFE; /* 2nd byte of the signature */
983 mateusz.vi 223
 
449 mateuszvis 224
  /* prepare result (rmod props) */
478 mateuszvis 225
  res = MK_FP(rmodseg, 0x100 + rmodcore_len);
450 mateuszvis 226
  _fmemset(res, 0, sizeof(*res));  /* zero out */
227
  res->rmodseg = rmodseg;          /* rmod segment */
449 mateuszvis 228
 
459 mateuszvis 229
  /* write env segment to rmod's PSP */
351 mateuszvis 230
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
231
  *owner = envseg;
232
 
366 mateuszvis 233
  /* write boot drive to rmod bootdrive field */
234
  _asm {
235
    push ax
236
    push bx
237
    push dx
238
    push ds
239
    mov ax, 0x3305 /* DOS 4.0+ - GET BOOT DRIVE */
240
    int 0x21 /* boot drive is in DL now (1=A:, 2=B:, etc) */
241
    add dl, 'A'-1 /* convert to a proper ASCII letter */
242
    /* set DS to rmodseg */
243
    mov ax, rmodseg
244
    mov ds, ax
245
    /* write boot drive to rmod bootdrive field */
246
    mov bx, RMOD_OFFSET_BOOTDRIVE
247
    mov [bx], dl
248
    pop ds
249
    pop dx
250
    pop bx
251
    pop ax
252
  }
253
 
1597 mateusz.vi 254
  /* save my original int22h handler and parent in rmod's memory */
255
  res->origint22 = *((unsigned long *)0x0a); /* original int22h handler seg:off is at 0x0a of my PSP */
256
  res->origparent = *((unsigned short *)0x16); /* PSP segment of my parent is at 0x16 of my PSP */
450 mateuszvis 257
 
448 mateuszvis 258
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I
259
   * terminate and save the original handler in rmod's memory */
454 mateuszvis 260
  {
261
    unsigned short *ptr = (void *)0x0a; /* int22 handler is at 0x0A of the PSP */
262
    ptr[0] = RMOD_OFFSET_ROUTINE;
263
    ptr[1] = rmodseg;
351 mateuszvis 264
  }
265
 
1595 mateusz.vi 266
  /* set my own parent to RMOD (this is not necessary for MS-DOS nor FreeDOS but
267
   * might be on other DOS implementations) */
268
  {
269
    unsigned short *ptr = (void *)0x16;
270
    *ptr = rmodseg;
271
  }
272
 
449 mateuszvis 273
  return(res);
351 mateuszvis 274
}
275
 
276
 
449 mateuszvis 277
/* look up my parent: if it's rmod then return a ptr to its props struct,
975 mateusz.vi 278
 * otherwise return NULL
983 mateusz.vi 279
 * I look at PSP[Ch] to locate RMOD (ie. the "terminate address") */
479 mateuszvis 280
struct rmod_props far *rmod_find(unsigned short rmodcore_len) {
983 mateusz.vi 281
  unsigned short *parent = (void *)0x0C;
448 mateuszvis 282
  unsigned short far *ptr;
283
  const unsigned short sig[] = {0x1983, 0x1985, 0x2017, 0x2019};
465 mateuszvis 284
  unsigned char *cmdtail = (void *)0x80;
448 mateuszvis 285
  unsigned char i;
286
  /* is it rmod? */
459 mateuszvis 287
  ptr = MK_FP(*parent, 0x100);
449 mateuszvis 288
  for (i = 0; i < 4; i++) if (ptr[i] != sig[i]) return(NULL);
465 mateuszvis 289
  /* match successfull (rmod is my parent) - but is it really a respawn?
290
   * command-line tail should contain a single character '\r' */
291
  if ((cmdtail[0] != 1) || (cmdtail[1] != '\n')) return(NULL);
292
  cmdtail[0] = 0;
293
  cmdtail[1] = '\r';
294
  /* */
478 mateuszvis 295
  return(MK_FP(*parent, 0x100 + rmodcore_len));
351 mateuszvis 296
}
367 mateuszvis 297
 
298
 
299
/* update rmod's pointer to comspec */
300
void rmod_updatecomspecptr(unsigned short rmod_seg, unsigned short env_seg) {
301
  unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
439 mateuszvis 302
  char far *comspecfp = env_lookup_val(env_seg, "COMSPEC");
367 mateuszvis 303
  if (comspecfp != NULL) {
1847 mateusz.vi 304
    /* here I need to translate the comspecfp far pointer into an offset
305
     * relative to env_seg */
306
    *comspecptr = FP_OFF(comspecfp) + ((FP_SEG(comspecfp) - env_seg) * 16);
367 mateuszvis 307
  } else {
308
    *comspecptr = 0;
309
  }
310
}
949 mateusz.vi 311
 
312
 
313
/* allocates bytes of far memory, flags it as belonging to rmod
957 mateusz.vi 314
 * the new block can be optionally flagged as 'ident' (if not null) and zero
315
 * out the newly allocated memory.
949 mateusz.vi 316
 * returns a far ptr to the allocated block, or NULL on error */
957 mateusz.vi 317
void far *rmod_fcalloc(unsigned short bytes, unsigned short rmod_seg, char *ident) {
949 mateusz.vi 318
  unsigned short far *owner;
319
  unsigned short newseg = 0;
320
 
321
  /* ask DOS for a memory block (as high as possible) */
322
  _asm {
323
    push bx /* save initial value in BX so I can restore it later */
324
 
325
    /* get current allocation strategy and save it on stack */
326
    mov ax, 0x5800
327
    int 0x21
328
    push ax
329
 
330
    /* set strategy to 'last fit, try high then low memory' */
331
    mov ax, 0x5801
332
    mov bx, 0x0082
333
    int 0x21
334
 
335
    /* ask for a memory block and save the given segment to rmodseg */
336
    mov ah, 0x48  /* Allocate Memory */
337
    mov bx, bytes
338
    add bx, 15    /* convert bytes to paragraphs */
339
    shr bx, 1     /* bx /= 16 */
340
    shr bx, 1
341
    shr bx, 1
342
    shr bx, 1
343
    int 0x21
344
 
345
    /* error handling */
346
    jc FAIL
347
 
348
    /* save newly allocated segment to newseg */
349
    mov newseg, ax
350
 
351
    FAIL:
352
    /* restore initial allocation strategy */
353
    mov ax, 0x5801
354
    pop bx
355
    int 0x21
356
 
357
    pop bx /* restore BX to its initial value */
358
  }
359
 
360
  if (newseg == 0) return(NULL);
361
 
362
  /* mark memory as "owned by rmod" */
363
  owner = (void far *)(MK_FP(newseg - 1, 1));
364
  *owner = rmod_seg;
365
 
366
  /* set the MCB description to ident, if provided */
367
  if (ident) {
368
    char far *mcbdesc = MK_FP(newseg - 1, 8);
369
    int i;
370
    _fmemset(mcbdesc, 0, 8);
371
    for (i = 0; (i < 8) && (ident[i] != 0); i++) { /* field's length is limited to 8 bytes max */
372
      mcbdesc[i] = ident[i];
373
    }
374
  }
375
 
957 mateusz.vi 376
  /* zero out the memory before handing it out */
377
  _fmemset(MK_FP(newseg, 0), 0, bytes);
378
 
949 mateusz.vi 379
  return(MK_FP(newseg, 0));
380
}
381
 
382
 
957 mateusz.vi 383
/* free memory previously allocated by rmod_fcalloc() */
949 mateusz.vi 384
void rmod_ffree(void far *ptr) {
385
  unsigned short ptrseg;
386
  unsigned short myseg = 0;
387
  unsigned short far *owner;
388
  if (ptr == NULL) return;
389
  ptrseg = FP_SEG(ptr);
390
 
391
  /* get my own segment */
392
  _asm {
393
    mov myseg, cs
394
  }
395
 
396
  /* mark memory in MCB as my own, otherwise DOS might refuse to free it */
397
  owner = MK_FP(ptrseg - 1, 1);
398
  *owner = myseg;
399
 
400
  /* free the memory block */
401
  _asm {
402
    push es
403
    mov ah, 0x49  /* Free Memory Block */
404
    mov es, ptrseg
405
    int 0x21
406
    pop es
407
  }
408
}
963 mateusz.vi 409
 
410
 
411
/* free the entire linked list of bat ctx nodes (and set its rmod ptr to NULL) */
412
void rmod_free_bat_llist(struct rmod_props far *rmod) {
413
  while (rmod->bat != NULL) {
414
    struct batctx far *victim = rmod->bat;
415
    rmod->bat = rmod->bat->parent;
416
    rmod_ffree(victim);
417
  }
418
}