-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockop.c
More file actions
281 lines (239 loc) · 8.26 KB
/
Copy pathblockop.c
File metadata and controls
281 lines (239 loc) · 8.26 KB
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
276
277
278
279
280
281
/* $Id: blockop.c,v 1.7 2006/10/30 20:12:04 chris Exp $
*
* Olivetti M20 PCOS diskette access utility: disk block operations
* Copyright (C) 2000,2004,2006 by Christian Groessler
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write
* to the Free Software Foundation, 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "cpgtypes.h"
#include "blockop.h"
int read_file_blocks(FILE *disk,int blockno,int numblks,cpg_u8_t *buffer)
{
int err;
while(numblks--) {
if ((err = read_file_block(disk,blockno,buffer))) {
fprintf(stderr,"read_file_blocks: error %d\n",err);
exit(55);
}
blockno++;
buffer += 256;
}
return(0);
}
int read_phys_blocks(FILE *disk,int blockno,int numblks,cpg_u8_t *buffer)
{
int err;
while(numblks--) {
if ((err = read_phys_block(disk,blockno,buffer))) {
fprintf(stderr,"read_phys_blocks: error %d\n",err);
exit(55);
}
blockno++;
buffer += 256;
}
return(0);
}
int write_file_blocks(FILE *disk,int blockno,int numblks,cpg_u8_t *buffer)
{
int err;
while(numblks--) {
if ((err = write_file_block(disk,blockno,buffer))) {
fprintf(stderr,"write_file_blocks: error %d\n",err);
exit(65);
}
blockno++;
buffer += 256;
}
return(0);
}
int write_phys_blocks(FILE *disk,int blockno,int numblks,cpg_u8_t *buffer)
{
int err;
while(numblks--) {
if ((err = write_phys_block(disk,blockno,buffer))) {
fprintf(stderr,"write_phys_blocks: error %d\n",err);
exit(65);
}
blockno++;
buffer += 256;
}
return(0);
}
int read_file_block(FILE *disk,int blockno,cpg_u8_t *buffer)
{
if (blockno >= 0x200) blockno += 0x10;
return(read_phys_block(disk,blockno,buffer));
}
int write_file_block(FILE *disk,int blockno,cpg_u8_t *buffer)
{
if (blockno >= 0x200) blockno += 0x10;
return(write_phys_block(disk,blockno,buffer));
}
#ifdef DOS
#include <dpmi.h>
#include <go32.h>
static int firstcall = 1;
static __dpmi_raddr oldint1e;
void cleanup_int1e(void)
{
__dpmi_set_real_mode_interrupt_vector(0x1e, &oldint1e);
/* don't free dosdpbseg since it isn't needed */
}
static void setup_int1e(void)
{
int dosdpbseg, scratch;
__dpmi_raddr newint1e;
unsigned char xferbuf;
firstcall = 0;
/* get old Int 1EH vector */
__dpmi_get_real_mode_interrupt_vector(0x1e, &oldint1e);
#ifdef DEBUG
printf("old INT 1EH vector: %04X:%04X\n", oldint1e.segment, oldint1e.offset16);
#endif
/* allocate own disk parameter block in real memory */
dosdpbseg = __dpmi_allocate_dos_memory(1, &scratch);
/* copy the contents (11 bytes, we copy 16 bytes) to our parameter block */
movedata(_go32_conventional_mem_selector(), oldint1e.segment * 16 + oldint1e.offset16,
_go32_conventional_mem_selector(), dosdpbseg * 16,
16);
/* modify our parameter block for 256 byte sectors and 16 sectors per track */
xferbuf = 1; /* 256 byte sector */
dosmemput(&xferbuf, 1, dosdpbseg * 16 + 3);
xferbuf = 16; /* sectors per track */
dosmemput(&xferbuf, 1, dosdpbseg * 16 + 4);
/* point Int 1EH vector to our disk parameter block */
newint1e.segment = dosdpbseg;
newint1e.offset16 = 0;
__dpmi_set_real_mode_interrupt_vector(0x1e, &newint1e);
#ifdef DEBUG
printf("new INT 1EH vector: %04X:%04X\n", newint1e.segment, newint1e.offset16);
#endif
/* restore Int 1EH vector at program exit */
atexit(cleanup_int1e);
}
#define blkno_to_chs(blkno,cyl,head,sec) do { \
cyl = blkno / 32; \
head = (blkno / 16) & 1; \
sec = (blkno % 16) + 1; \
} \
while (0)
static int dos_bios_read(FILE *disk,int blockno,cpg_u8_t *buffer)
{
__dpmi_regs regs;
unsigned int drive, cyl, head, sec;
if (firstcall) setup_int1e();
/* setup registers and call BIOS Int 13H function 2 (read sectors) */
drive = (unsigned int)disk;
blkno_to_chs(blockno,cyl,head,sec);
#ifdef DEBUG
printf("reading sector %u (cyl %u, head %u, sec %u)\n",
blockno,
cyl, head, sec);
#endif
regs.x.ax = 0x0201; /* AH: read sectors, AL: # of sectors */
regs.h.ch = cyl; /* CL: low eight bits of cylinder # */
regs.h.cl = sec; /* CH: sector # (bits 0-5), high 2 bits of cylinder # */
regs.h.dh = head; /* DH: head # */
regs.h.dl = drive; /* DL: drive # */
regs.x.es = __tb >> 4;
regs.x.bx = __tb & 0x0f;
__dpmi_int(0x13, ®s); /* call BIOS */
if (regs.x.flags & 1) { /* if carry flag set -> error */
fprintf(stderr,"dos_bios_read: BIOS call failed: status code: 0x%02x\n",regs.h.ah);
exit(51);
}
/* copy sector data to return buffer */
dosmemget((__tb >> 4) * 16 + (__tb & 0x0f), 256, buffer);
return(0);
}
static int dos_bios_write(FILE *disk,int blockno,cpg_u8_t *buffer)
{
__dpmi_regs regs;
unsigned int drive, cyl, head, sec;
if (firstcall) setup_int1e();
/* setup registers and call BIOS Int 13H function 3 (write sectors) */
drive = (unsigned int)disk;
blkno_to_chs(blockno,cyl,head,sec);
/* copy sector data to realmode buffer */
dosmemput(buffer, 256, (__tb >> 4) * 16 + (__tb & 0x0f));
#ifdef DEBUG
printf("writing sector %u (cyl %u, head %u, sec %u)\n",
blockno,
cyl, head, sec);
#endif
regs.x.ax = 0x0301; /* AH: write sectors, AL: # of sectors */
regs.h.ch = cyl; /* CL: low eight bits of cylinder # */
regs.h.cl = sec; /* CH: sector # (bits 0-5), high 2 bits of cylinder # */
regs.h.dh = head; /* DH: head # */
regs.h.dl = drive; /* DL: drive # */
regs.x.es = __tb >> 4;
regs.x.bx = __tb & 0x0f;
__dpmi_int(0x13, ®s); /* call BIOS */
if (regs.x.flags & 1) { /* if carry flag set -> error */
fprintf(stderr,"dos_bios_write: BIOS call failed: status code: 0x%02x\n",regs.h.ah);
exit(61);
}
return(0);
}
#endif
int read_phys_block(FILE *disk,int blockno,cpg_u8_t *buffer)
{
if (blockno > 0x460) { /* 0x460 = 1120 blocks, 256 bytes, total is 286720 bytes in a 320K disk */
fprintf(stderr,"read_phys_block: access outa bounds - block %d\n",blockno);
exit(50);
}
#ifdef DOS
if ((unsigned int)disk < 'Z' - 'A')
return(dos_bios_read(disk,blockno,buffer));
#endif
if (fseek(disk,blockno * 256,SEEK_SET)) {
fprintf(stderr,"read_phys_block: fseek failed: %s\n",strerror(errno));
exit(51);
}
if (fread(buffer,256,1,disk) != 1) {
fprintf(stderr,"read_phys_block: fread failed %s\n",strerror(errno));
exit(52);
}
return(0);
}
int write_phys_block(FILE *disk,int blockno,cpg_u8_t *buffer)
{
if (blockno > 0x460) { /* 0x460 = 1120 blocks, 256 bytes, total is 286720 bytes in a 320K disk */
fprintf(stderr,"write_phys_block: access outa bounds - block %d\n",blockno);
exit(60);
}
#ifdef DOS
if ((unsigned int)disk < 'Z' - 'A')
return(dos_bios_write(disk,blockno,buffer));
#endif
if (fseek(disk,blockno * 256,SEEK_SET)) {
fprintf(stderr,"write_phys_block: fseek failed: %s\n",strerror(errno));
exit(61);
}
if (fwrite(buffer,256,1,disk) != 1) {
fprintf(stderr,"write_phys_block: fwrite failed %s\n",strerror(errno));
exit(62);
}
return(0);
}
/* Local Variables: */
/* c-file-style: "cpg" */
/* c-basic-offset: 4 */
/* End: */