Subversion Repositories SvarDOS

Rev

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

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