summaryrefslogtreecommitdiff
path: root/bootblocks/trk_buf.c
blob: 3039169ac24345a72a48354579cc42baa6e5f9a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

#include <stdio.h>
#include <dos.h>
#include <ctype.h>
#include <malloc.h>
#include "readfs.h"

int disk_drive = 0;
int disk_spt   = 7;
int disk_heads = 2;
int disk_cyls  = 0;

static int    last_drive = 0;
static int    data_len = 0;
static long   data_trk1 = 0;
static char * data_buf1 = 0;
static long   data_trk2 = 0;
static char * data_buf2 = 0;

static long   bad_track = -1;	/* Track number of last unsuccesful read */

static long   get_dpt();

void reset_disk()
{
   if( data_buf1 ) free(data_buf1);
   if( data_buf2 ) free(data_buf2);
   data_buf1 = data_buf2 = 0;
   last_drive = disk_drive;

   if( !(disk_drive & 0x80 ) )
   {
      disk_spt   = 7;	/* Defaults for reading Boot area. */
      disk_heads = 2;
      disk_cyls  = 0;
   }
#if defined(__MSDOS__) || defined(__STANDALONE__)
   else
   {
      /* Hard disk, get parameters from bios */
      long dpt;
      int  v;

      disk_spt   = 17;	/* Defaults for reading Boot area. */
      disk_heads = 1;
      disk_cyls  = 0;

      dpt = get_dpt(disk_drive);
      v = ((dpt>>16) & 0xFF);
      if( v == 0xFF || v <= (disk_drive&0x7F) ) return; /* Bad dpt */

      disk_spt   = (dpt & 0x3F); 		/* Max sector number 1-63 */
      if( disk_spt == 0 ) disk_spt = 64;	/* 1-64 ? */
      disk_heads = ((dpt>>24) & 0xFF) + 1;	/* Head count 1-256 */
      disk_cyls  = ((dpt>>8) & 0xFF) + ((dpt<<2) & 0x300) + 1;

      /* Cyls count, unchecked, only needs != 0, if AMI 386 bios can be
       * upto 4096 cylinder, otherwise BIOS limit is 1024 cyl.
       */
   }
#endif
}

char * read_lsector(sectno)
long sectno;
{
   int tries = 6;
   int rv;

   int phy_s = 1;
   int phy_h = 0;
   int phy_c = 0;

   if( disk_drive != last_drive ) reset_disk();

   if( disk_spt < 0 || disk_spt > 63 || disk_heads < 1 )
   {
      phy_s = sectno;
      reset_disk();

#ifdef __ELKS__
      fprintf(stderr, "read_sector(%ld = %d,%d,%d)\n",
                                   sectno, phy_c, phy_h, phy_s+1);
#endif
   }
   else
   {
      phy_s = sectno%disk_spt;
      phy_h = sectno/disk_spt%disk_heads;
      phy_c = sectno/disk_spt/disk_heads;

#ifdef __ELKS__
      fprintf(stderr, "read_sector(%ld = %d,%d,%d)\n",
                                   sectno, phy_c, phy_h, phy_s+1);
#endif

      if( fetch_track_buf(phy_c, phy_h, phy_s) >= 0 )
         return data_buf1 + (phy_s % data_len) * 512;
   }

   data_len = -1;	/* Zap the cache */
   if( data_buf1 == 0 )
      data_buf1 = malloc(512);
   if( data_buf1 == 0 )
   {
      printf("Cannot allocate memory for disk read!!!\n");
      return 0;
   }

#ifdef __ELKS__
   fprintf(stderr, "WARNING: Single sector read\n");
#endif

   do
   {
     rv = phy_read(disk_drive, phy_c, phy_h, phy_s+1, 1, data_buf1);
     tries--;
     if( rv ) printf("Error in phy_read(%d,%d,%d,%d,%d,%d);\n",
		      disk_drive, phy_c, phy_h, phy_s+1, 1, data_buf1);
   }
   while(rv && tries > 0);

   if(rv) return 0; else return data_buf1;
}

fetch_track_buf(phy_c, phy_h, phy_s)
int phy_c, phy_h, phy_s;
{
   long trk_no, t;
   char * p;
   int tries = 3;
   int rv, nlen;

   /* Big tracks get us short of memory so limit it. */
   nlen = (disk_spt-1)/22;
   nlen = (disk_spt+nlen)/(nlen+1);
   trk_no = (long)phy_c*disk_heads*4+phy_h*4+phy_s/nlen+1;

   if( data_len != nlen )
   {
      if( data_buf1 ) free(data_buf1);
      if( data_buf2 ) free(data_buf2);
      data_buf1 = data_buf2 = 0;
      data_len = disk_spt;
   }
   if( trk_no == bad_track ) return -1;

   if( data_buf1 && trk_no == data_trk1 ) return 0;

   /* Two cases:
    * 1) buffer2 has the one we want, need to swap to make it most recent 
    * 2) Neither has it, need to swap to overwrite least recent.
    */

   /* So we always swap */
   p = data_buf1; data_buf1 = data_buf2; data_buf2 = p;
   t = data_trk1; data_trk1 = data_trk2; data_trk2 = t;

   /* The other one right ? */
   if( data_buf1 && trk_no == data_trk1 ) return 0;

   /* If we get here we have to do a physical read ... */
   /*   into data_buf1. */

   if( data_buf1 == 0 )
   {
      data_buf1 = malloc(disk_spt*512);

#ifdef __ELKS__
      fprintf(stderr, "Allocated buffer to %d\n", data_buf1);
#endif
   }
   if( data_buf1 == 0 )
   {
      /* Is buf2 allocated ? Yes take it! */
      data_buf1 = data_buf2; data_buf2 = 0; data_trk2 = -1;
   }

   bad_track = -1;
   data_trk1 = -1;

   /* Not enough memory for track read. */
   if( data_buf1 == 0 ) return -1;

   do /* the physical read */
   {
     rv = phy_read(disk_drive, phy_c, phy_h, phy_s/data_len+1, data_len,
                   data_buf1);
     tries--;
     if( rv ) printf("Error in phy_read(%d,%d,%d,%d,%d,%d);\n",
	     disk_drive, phy_c, phy_h, phy_s/data_len+1, data_len, data_buf1);
   }
   while(rv && tries > 0);

   /* Disk error, it'll try one at a time, _very_ slowly! */
   if(rv)
   {
      bad_track = trk_no;
      return -1;
   }

   /* Yes! */
   data_trk1 = trk_no;
   return 0;
}

#if defined(__MSDOS__) || defined(__STANDALONE__)
phy_read(drive, cyl, head, sect, length, buffer)
{
#asm
  push	bp
  mov	bp,sp

  push	es
  push	ds
  pop	es

  mov	dl,[bp+2+_phy_read.drive]
  mov	ch,[bp+2+_phy_read.cyl]
  mov	dh,[bp+2+_phy_read.head]
  mov	bx,[bp+2+_phy_read.buffer]

  mov	ax,[bp+2+_phy_read.cyl]		! Bits 10-11 of cylinder, AMI BIOS.
  mov	cl,#4
  sar	ax,cl
  and	al,#$C0
  xor	dh,al

  mov	cl,[bp+2+_phy_read.sect]
  and	cl,#$3F
  mov	ax,[bp+2+_phy_read.cyl]		! Bits 8-9 of cylinder.
  sar	ax,#1
  sar	ax,#1
  and	al,#$C0
  or	cl,al

  mov	al,[bp+2+_phy_read.length]
  mov	ah,#$02
  int	$13
  jc	read_err
  mov	ax,#0
read_err:

  pop	es
  pop	bp
#endasm
}

long
get_dpt(drive)
{
#asm
  push	bp
  mov	bp,sp

  push	di
  push	es

  mov	dl,[bp+2+_get_dpt.drive]

  mov	ah,#$08
  int	$13
  jnc	func_ok
  mov	cx,ax
  mov	dx,#-1
func_ok:
  mov	ax,cx

  pop	es
  pop	di
  pop	bp
#endasm
}
#endif