-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_io_dhd.c
executable file
·454 lines (399 loc) · 9.62 KB
/
fs_io_dhd.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
64NET/2 .DHD specific routines
(C) Copyright Paul Gardner-Stephen 1996, All rights reserved
*/
#include "config.h"
#include "fs.h"
#include "fs_func.h"
#include "misc_func.h"
static int bitsset[256] =
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, /* 0x */
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 1x */
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 2x */
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 3x */
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 4x */
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 5x */
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 6x */
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* 7x */
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 8x */
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 9x */
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* ax */
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* bx */
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* cx */
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* dx */
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* ex */
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /* fx */
};
int
fs_dhd_validate (fs64_filesystem * fs, int purgeflag)
{
/* Validate a .D64 file */
/* Algorithm:
1) Read old T18 S0
2) Clear old BAM (to all free)
3) Alloc T18 S0
4) Parse through each dir sector doing:
4.1) Allocating directory sector
4.2) Parsing through each file in the directory allocating used blocks,
and clearing splat entries.
5) Write back BAM
*/
/*
NOTE: If at anytime a block is attempted to be allocated, which already is,
a 71,DIRECTORY ERROR,tt,ss or a 75,FILESYSTEM INCONSISTENT,tt,ss
will be generated, with tt and ss being the cross-linked sector.
*BUT*: If the purgeflag variable is set then the following will happen;
Crosslinked files will be truncated.
Crosslinked directories will be truncated.
Bad blocks will be allocated (no error return). */
uchar t18s0[34][256]; /* BAM buffer */
int dt, ds, num_tracks;
set_error (0, 0, 0);
/* get number of tracks */
fseek (fs->fsfile, 54, SEEK_SET);
num_tracks = fgetc (fs->fsfile);
if ((num_tracks < 1) || (num_tracks > 255))
{
set_error (74, 0, 0);
return (-1);
}
if (fs_dhd_readbam (fs, t18s0))
return (-1);
/* Step 2 & 3 - Free all blocks *except* T18 S0 */
fs_dhd_makebam (t18s0, num_tracks);
/* Step 4 - Parse through directory */
dt = t18s0[1][0];
ds = t18s0[1][1];
if (fs_dxx_validate_dir (fs, purgeflag, (void *) t18s0, dt, ds))
{
/* error */
return (-1);
}
else
{
if (fs_dhd_writebam (fs, t18s0))
return (-1);
return (0);
}
}
int
fs_dhd_bamblocksfree (uchar blocks[34][256])
{
int i, bc = 0, bn, ofs, j;
for (i = 1; i <= blocks[2][8]; i++)
{
bn = 2 + ((i - 8) / 8);
ofs = 32 * (i & 7);
for (j = 0; j < 32; j++)
bc += bitsset[blocks[bn][ofs + j]];
}
return (bc);
}
int
fs_dhd_bamfindfreeblock (uchar blocks[34][256], int *track, int *sector)
{
/* find a free block on a DHD */
int i, bn, ofs;
for (*track = 1; *track <= blocks[2][8]; (*track)++)
{
bn = 2 + (*track / 8);
ofs = 32 * (*track & 7);
for (i = 0; i < 32; i++)
if (blocks[bn][ofs + i])
{
/* free block here */
*sector = i * 8 + 7 - firsthighbit (blocks[bn][ofs + i]);
return (0);
}
}
*track = -1;
*sector = -1;
return (-1);
}
int
fs_dhd_bamalloc (int t, int s, uchar blocks[34][256])
{
int po2[8] =
{128, 64, 32, 16, 8, 4, 2, 1};
int so = (s / 8), bit = po2[s & 7];
int ofs, bn;
bn = 2 + (t / 8);
ofs = 32 * (t & 7);
if (blocks[bn][ofs + so] & bit)
{
/* free */
blocks[bn][ofs + so] -= bit;
return (0);
}
else
{
/* already alloc'd */
return (-1);
}
}
int
fs_dhd_bamdealloc (uchar blocks[34][256], int t, int s)
{
int po2[8] =
{128, 64, 32, 16, 8, 4, 2, 1};
int so = (s / 8), bit = po2[s & 7];
int ofs, bn;
bn = 2 + (t / 8);
ofs = 32 * (t & 7);
if (!blocks[bn][ofs + so] & bit)
{
/* alloc'd */
blocks[bn][ofs + so] |= bit;
return (0);
}
else
{
/* already free */
return (-1);
}
}
int
fs_dhd_blocksfree (fs64_filesystem * fs)
{
uchar blocks[34][256];
if (fs_dhd_readbam (fs, blocks))
return (-1);
return (fs_dhd_bamblocksfree (blocks));
}
int
fs_dhd_findfreeblock (fs64_filesystem * fs, int *track, int *sector)
{
uchar blocks[34][256];
if (fs_dhd_readbam (fs, blocks))
return (-1);
if (fs_dhd_bamfindfreeblock (blocks, track, sector))
{
*track = -1;
*sector = -1;
return (-1);
}
else
return (0);
}
int
fs_dhd_allocateblock (fs64_filesystem * fs, int track, int sector)
{
uchar blocks[34][256];
int t, s;
if (fs_dhd_readbam (fs, blocks))
return (-1);
if (fs_dhd_bamalloc (track, sector, blocks))
{
/* already allocated, so tell em wht a free one is */
if (fs_dhd_bamfindfreeblock (blocks, &t, &s))
set_error (65, 0, 0);
else
set_error (65, t, s);
return (-1);
}
if (fs_dhd_writebam (fs, blocks))
return (-1);
return (0);
}
int
fs_dhd_deallocateblock (fs64_filesystem * fs, int track, int sector)
{
uchar blocks[34][256];
if (fs_dhd_readbam (fs, blocks))
return (-1);
if (fs_dhd_bamdealloc (blocks, track, sector))
return (-1);
if (fs_dhd_writebam (fs, blocks))
return (-1);
return (0);
}
int
fs_dhd_format (fs64_filesystem * fs, uchar *name, uchar *id)
{
/* format a D64 file */
int t, s, num_tracks;
uchar blocks[34][256] =
{
{0}};
errno = 0;
fseek (fs->fsfile, 54, SEEK_SET);
num_tracks = fgetc (fs->fsfile);
if ((num_tracks < 1) || (num_tracks > 255))
{
set_error (74, 0, 0);
return (-1);
}
/* If id is non null then format each sector, otherwise
only clear dir and BAM */
if (id[0])
{
/* toast *everything* */
for (t = 1; t <= num_tracks; t++)
{
for (s = 0; s < 256; s++)
{
if (writets (fs, t, s, blocks[0]))
return (-1);
}
}
/* place padding */
for (t = 24; t < 28; t++)
blocks[1][t] = 0xa0;
/* copy new id in */
blocks[1][22] = id[0];
blocks[1][23] = id[1];
}
else
{
/* toast first dir sector */
if (writets (fs, 1, (num_tracks / 8) + 3, blocks[0]))
return (-1);
}
/* make shiny new bam */
fs_dhd_makebam (blocks, num_tracks);
/* set DOS information */
blocks[1][0] = 1;
blocks[1][1] = (num_tracks / 8) + 3;
blocks[1][25] = '1';
blocks[1][26] = 'H';
blocks[1][2] = 'H';
blocks[1][3] = 0;
/* root dir header pointer */
blocks[1][32] = 1;
blocks[1][33] = 1;
/* parent header pointer */
blocks[1][34] = 0x00;
blocks[1][35] = 0x00;
/* reference to location in parent */
blocks[1][36] = 0x00;
blocks[1][37] = 0x00;
blocks[1][38] = 0x00;
/* copy header in */
for (t = 4; t < 22; t++)
blocks[1][t] = 0xa0;
for (t = 4; name[t - 4]; t++)
blocks[1][t] = name[t - 4];
/* write back */
fs_dhd_writebam (fs, blocks);
set_error (0, 0, 0);
return (0);
}
int fs_dhd_readbam (fs64_filesystem * fs, uchar blocks[34][256])
{
/* read a D81 bam */
int i;
int num_tracks;
fseek (fs->fsfile, 54, SEEK_SET);
num_tracks = fgetc (fs->fsfile);
if ((num_tracks < 1) || (num_tracks > 255))
{
set_error (74, 0, 0);
return (-1);
}
for (i = 0; i < ((num_tracks / 8) + 3); i++)
if (readts (fs, 1, i, blocks[i]))
return (-1);
return (0);
}
int
fs_dhd_writebam (fs64_filesystem * fs, uchar blocks[3][256])
{
/* write a D81 bam */
int i;
int num_tracks;
fseek (fs->fsfile, 54, SEEK_SET);
num_tracks = fgetc (fs->fsfile);
if ((num_tracks < 1) || (num_tracks > 255))
{
set_error (74, 0, 0);
return (-1);
}
for (i = 0; i < ((num_tracks / 8) + 3); i++)
{
if (writets (fs, 1, i, blocks[i]))
return (-1);
if (writets (fs, 1, i, blocks[i]))
return (-1);
}
return (0);
}
int
fs_dhd_makebam (uchar blocks[34][256], int num_tracks)
{
/* create a new D81 bam */
int i, j;
int bn, ofs;
blocks[1][0] = 1;
blocks[1][1] = (num_tracks / 8) + 3;
blocks[2][0] = 0;
blocks[2][1] = 0;
blocks[2][2] = 'H';
blocks[2][3] = 183;
blocks[2][4] = blocks[1][22];
blocks[2][5] = blocks[1][23];
blocks[2][6] = 0xc0;
blocks[2][7] = 0x00;
blocks[2][8] = num_tracks;
/* BAM data */
for (i = 1; i <= num_tracks; i++)
{
bn = 2 + (i / 8);
ofs = (i & 7) * 32;
for (j = 0; j < 32; j++)
blocks[bn][ofs + j] = 0xff;
}
/* allocate T1 S0 to S(2+(num_tracks/8)) */
for (j = 0; j <= (2 + (num_tracks / 8)); j++)
fs_dhd_bamalloc (1, j, blocks);
return (0);
}
int
fs_dhd_headername (uchar *path, uchar *header, uchar *id, int par, fs64_file * f)
{
uchar buff[256];
fs64_filesystem ff;
int i;
ff.fsfile = 0;
f->filesys.media = fs64_mediatype (path);
f->de.filesys.media = f->filesys.media;
if (f->filesys.dirtrack < 1)
{
f->filesys.dirtrack = 1;
f->filesys.dirsector = 1;
}
ff.media = f->filesys.media;
strcpy ((char*)ff.fspath, (char*)path);
if ((ff.fsfile = fopen ((char*)path, "r")) != NULL)
{
if (!readts (&ff, f->filesys.dirtrack, f->filesys.dirsector, buff))
{
for (i = 0; i < 16; i++)
if (buff[i + 4] != 0xa0)
header[i] = buff[i + 4];
else
header[i] = 0x20;
for (i = 0; i < 5; i++)
if (buff[i + 22] != 0xa0)
id[i] = buff[22 + i];
else
id[i] = 0x20;
}
else
{
/* cant read sector - so make it up :) */
strcpy ((char*)header, "@@@@@@@@@@@@@@@@");
strcpy ((char*)id, "@@@@@");
}
fclose (ff.fsfile);
ff.fsfile = 0;
return (0);
}
else
{
/* cant open file system */
strcpy ((char*)header, "@@@@@@@@@@@@@@@@");
strcpy ((char*)id, "@@@@@");
return (0);
}
}