Subversion Repositories SvarDOS

Rev

Rev 397 | Rev 439 | 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
 
36
/* returns segment where rmod is installed */
37
unsigned short rmod_install(unsigned short envsize) {
38
  char far *myptr, far *mcb;
39
  unsigned short far *owner;
40
  unsigned int rmodseg = 0xffff;
41
  unsigned int envseg = 0;
42
 
43
  /* read my current env segment from PSP */
44
  _asm {
45
    push ax
46
    push bx
47
    mov bx, 0x2c
48
    mov ax, [bx]
49
    mov envseg, ax
50
    pop bx
51
    pop ax
52
  }
53
 
369 mateuszvis 54
  /* printf("original (PSP) env buffer at %04X\r\n", envseg); */
351 mateuszvis 55
  /* if custom envsize requested, convert it to number of paragraphs */
56
  if (envsize != 0) {
57
    envsize += 15;
58
    envsize /= 16;
59
  }
60
 
61
 
62
  _asm {
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 */
65
    int 0x21
66
    xor ah, ah
67
    push ax         /* save link state on stack */
68
    mov ax, 0x5803  /* SET UMB LINK STATE */
69
    mov bx, 1
70
    int 0x21
71
    /* get current allocation strategy and save it in DX */
72
    mov ax, 0x5800
73
    int 0x21
74
    push ax
75
    pop dx
76
    /* set strategy to 'last fit, try high then low memory' */
77
    mov ax, 0x5801
78
    mov bx, 0x0082
79
    int 0x21
80
    /* ask for a memory block and save the given segment to rmodseg */
81
    mov ah, 0x48
82
    mov bx, (rmod_len + 15) / 16
83
    int 0x21
84
    jc ALLOC_FAIL
85
    mov rmodseg, ax
86
    /* ask for a memory block for the environment and save it to envseg (only if custom size requested) */
87
    mov bx, envsize
88
    test bx, bx
89
    jz ALLOC_FAIL
90
    mov ah, 0x48
91
    int 0x21
92
    jc ALLOC_FAIL
93
    mov envseg, ax
94
 
95
    ALLOC_FAIL:
96
    /* restore initial allocation strategy */
97
    mov ax, 0x5801
98
    mov bx, dx
99
    int 0x21
100
    /* restore initial UMB memory link state */
101
    mov ax, 0x5803
102
    pop bx       /* pop initial UMB link state from stack */
103
    int 0x21
104
  }
105
 
106
  if (rmodseg == 0xffff) {
369 mateuszvis 107
    outputnl("malloc error");
351 mateuszvis 108
    return(0xffff);
109
  }
110
 
111
  /* copy rmod to its destination */
112
  myptr = MK_FP(rmodseg, 0);
113
  _fmemcpy(myptr, rmod, rmod_len);
114
 
115
  /* mark rmod memory as "self owned" */
116
  mcb = MK_FP(rmodseg - 1, 0);
117
  owner = (void far *)(mcb + 1);
118
  *owner = rmodseg;
119
  _fmemcpy(mcb + 8, "SVARCOM", 8);
120
 
359 mateuszvis 121
  /* mark env memory as "self owned" */
122
  mcb = MK_FP(envseg - 1, 0);
123
  owner = (void far *)(mcb + 1);
124
  *owner = rmodseg;
125
  _fmemcpy(mcb + 8, "SVARENV", 8);
351 mateuszvis 126
 
127
  /* write env segment to rmod buffer */
128
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
129
  *owner = envseg;
130
 
366 mateuszvis 131
  /* write boot drive to rmod bootdrive field */
132
  _asm {
133
    push ax
134
    push bx
135
    push dx
136
    push ds
137
    mov ax, 0x3305 /* DOS 4.0+ - GET BOOT DRIVE */
138
    int 0x21 /* boot drive is in DL now (1=A:, 2=B:, etc) */
139
    add dl, 'A'-1 /* convert to a proper ASCII letter */
140
    /* set DS to rmodseg */
141
    mov ax, rmodseg
142
    mov ds, ax
143
    /* write boot drive to rmod bootdrive field */
144
    mov bx, RMOD_OFFSET_BOOTDRIVE
145
    mov [bx], dl
146
    pop ds
147
    pop dx
148
    pop bx
149
    pop ax
150
  }
151
 
351 mateuszvis 152
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I terminate */
153
  _asm {
366 mateuszvis 154
    push ax
155
    push bx
351 mateuszvis 156
    mov bx, 0x0a                   /* int22 handler is at 0x0A of the PSP */
157
    mov ax, RMOD_OFFSET_ROUTINE
158
    mov [bx], ax                   /* int handler offset */
159
    mov ax, rmodseg
160
    mov [bx+2], ax                 /* int handler segment */
366 mateuszvis 161
    pop bx
162
    pop ax
351 mateuszvis 163
  }
164
 
165
  return(rmodseg);
166
}
167
 
168
 
169
/* scan memory for rmod, returns its segment if found, 0xffff otherwise */
170
unsigned short rmod_find(void) {
171
  unsigned short i;
358 mateuszvis 172
  unsigned short far *ptrword;
173
  unsigned char far *ptrbyte;
351 mateuszvis 174
 
175
  /* iterate over all paragraphs, looking for my signature */
358 mateuszvis 176
  for (i = 1; i != 65535; i++) {
177
    ptrword = MK_FP(i, 0);
178
    if (ptrword[0] != 0x1983) continue;
179
    if (ptrword[1] != 0x1985) continue;
180
    if (ptrword[2] != 0x2017) continue;
181
    if (ptrword[3] != 0x2019) continue;
182
    /* extra check: make sure the paragraph before is an MCB block and that it
183
     * belongs to itself. otherwise I could find the rmod code embedded inside
184
     * the command.com binary... */
185
    ptrbyte = MK_FP(i - 1, 0);
186
    if ((*ptrbyte != 'M') && (*ptrbyte != 'Z')) continue; /* not an MCB */
187
    ptrword = MK_FP(i - 1, 1);
188
    if (*ptrword != i) continue; /* not belonging to self */
351 mateuszvis 189
    return(i);
190
  }
191
  return(0xffff);
192
}
367 mateuszvis 193
 
194
 
195
/* update rmod's pointer to comspec */
196
void rmod_updatecomspecptr(unsigned short rmod_seg, unsigned short env_seg) {
197
  unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
198
  char far *comspecfp = env_lookup(env_seg, "COMSPEC");
199
  if (comspecfp != NULL) {
200
    *comspecptr = FP_OFF(comspecfp) + 8; /* +8 to skip the "COMSPEC=" prefix */
201
  } else {
202
    *comspecptr = 0;
203
  }
204
}