Subversion Repositories SvarDOS

Rev

Rev 359 | Rev 367 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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