File: mediaprm.c

package info (click to toggle)
fdutils 5.3-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,004 kB
  • ctags: 621
  • sloc: ansic: 6,098; sh: 2,236; makefile: 279; sed: 4
file content (303 lines) | stat: -rw-r--r-- 6,230 bytes parent folder | download | duplicates (4)
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <linux/fd.h>
#include "parse.h"
#include "mediaprmP.h"
#include "driveprm.h"
#include "mediaprm.h"

typedef enum
{
	FE_UNKNOWN,
	FE_SIZE,
	FE_SECT,
	FE_VSECT,
	FE_HEAD,
	FE_CYL,

	/* the stretch byte */
	FE_TPI,
	FE_STRETCH,
	FE_SWAPSIDES,

	FE_GAP,

	FE_FM,
	FE_PERP,
	FE_SSIZE,
	FE_2M,
	FE_DTR,

	FE_SPEC1, /* spec1, obsolete */
	FE_FMT_GAP,

	FE_DENSITY,
} field_t;

static int SIZE, SECT, VSECT, HEAD, CYL, TPI, STRETCH, SWAPSIDES, GAP, FM;
static int PERP, SSIZE, _2M, DTR, SPEC1, FMT_GAP, DENSITY;
static int ssize;

#define F_SIZE FE_SIZE,&SIZE
#define F_SECT FE_SECT,&SECT
#define F_VSECT FE_VSECT,&VSECT
#define F_HEAD FE_HEAD,&HEAD
#define F_CYL FE_CYL,&CYL

#define F_TPI FE_TPI,&TPI
#define F_STRETCH FE_STRETCH,&STRETCH
#define F_SWAPSIDES FE_SWAPSIDES,&SWAPSIDES

#define F_GAP FE_GAP,&GAP

#define F_FM FE_FM,&FM
#define F_PERP FE_PERP,&PERP
#define F_SSIZE FE_SSIZE,&SSIZE
#define F_2M FE_2M,&_2M
#define F_DTR FE_DTR,&DTR

#define F_SPEC1 FE_SPEC1,&SPEC1
#define F_FMT_GAP FE_FMT_GAP,&FMT_GAP

#define F_DENSITY FE_DENSITY,&DENSITY

static int mask;

static keyword_t ids[]= {
	{ "size", F_SIZE, 0 },
	{ "sect", F_SECT, 0},
	{ "vsect", F_VSECT, 0},
	{ "tracksize", F_VSECT, 0},
	{ "head", F_HEAD, 0},
	{ "ss",   F_HEAD, 1},
	{ "ds",   F_HEAD, 2},
	{ "cyl",  F_CYL, 0},

	{ "tpi", F_TPI, 0 },
	{ "stretch", F_STRETCH, 0 },

	{ "swapsides", F_SWAPSIDES, 1},

	{ "gap", F_GAP, 0},

	{ "fm", F_FM, 1},
	{ "perp", F_PERP, 0},
	{ "ssize", F_SSIZE, 0},
	{ "2m", F_2M, 1},
	{ "dtr", F_DTR, 0},

	{ "mss", F_SSIZE, 16384 },
	{ "sd", F_DENSITY, DENS_SD },
	{ "dd", F_DENSITY, DENS_DD },
	{ "qd", F_DENSITY, DENS_QD },
	{ "hd", F_DENSITY, DENS_HD },
	{ "ed", F_DENSITY, DENS_ED },
	
	{ "spec1", F_SPEC1, 0},
	{ "fmt_gap", F_FMT_GAP, 0}
};


struct
{
	int dtr; /* dtr, as it would be in a 3 1/2 or DD drive */
	int fm; /* fm mode */
	int perp; /* perp mode */
	int tpi;
	int capacity[4]; /* capacity per track */
} dens_tab[] = {
	{ 0, 0, 0, 0}, /* none */
	{2, 1, 0, 48,  { 2500, 2500, 2500, 2500} }, /* SD */
	{2, 0, 0, 48,  { 6250, 6250, 6250, 6250} }, /* DD */
	{1, 0, 0, 96,  { 6250, 7500, 6250,    0} }, /* QD */
	{0, 0, 0, 96,  {12500,12500,10416,    0} }, /* HD */
	{3, 0, 1, 96,  {25000,25000,    0,    0} }  /* ED */
};

int gap[4] = { 0x1b, 0x23, 0x2a, 0x1b };

static inline void set_field(field_t slot, int *var, int value)
{
	if( !(mask & (1 << slot)))
		*var = value;
}

static void compute_params(drivedesc_t *drvprm,
			   struct floppy_struct *medprm)
{
	int r, capacity, mysize;
	int header_size, fmt_gap;

	set_field(F_DENSITY, drvprm->type.max_density);
	
	r = dens_tab[DENSITY].dtr;
	if(r == 2 && drvprm->type.rpm == 360)
		r = 1;

	capacity = dens_tab[DENSITY].capacity[drvprm->type.ff];

	set_field(F_DTR, r);

	set_field(F_GAP, gap[DTR]);
	
	set_field(F_FM, dens_tab[DENSITY].fm);
	set_field(F_PERP, dens_tab[DENSITY].perp);

	set_field(F_HEAD, 2);

	/* 5 1/4 drives have two different cylinder densities */
	if(drvprm->type.ff == FF_525) {
		if(DENSITY == DENS_DD)
			/* double density disks are 48 TPI */
			set_field(F_TPI, 48);
		else
			set_field(F_TPI, 96);

		if(mask & (1 << FE_CYL)) {
			/* we know the number of cylinders, try to infer
			 * cylinder density from there */
			if(CYL < 50)
				set_field(F_TPI, 48);
			if(CYL > 70)
				set_field(F_TPI, 96);
		}

		if(drvprm->type.tpi == 96 && TPI == 48)
			set_field(F_STRETCH, 1);
		else
			set_field(F_STRETCH, 0);
		
		/* if, on the other hand, we know TPI, but not the number of
		 * cylinders, infer in the other direction */
		if(TPI == 48)
			set_field(F_CYL, 40);
		else
			set_field(F_CYL, 80);
	} else {
		set_field(F_STRETCH, 0);
		set_field(F_CYL, 80);
	}

	if(PERP)
		header_size = 81;
	else
		header_size = 62;

	switch(capacity) {
		case 25000:
			set_field(F_SECT, 36);
			break;
		case 12500:
			set_field(F_SECT, 18);
			break;
		case 10416:
			set_field(F_SECT, 15);
			break;
		case 6250:
			set_field(F_SECT, 9);
			break;
		default:
			set_field(F_SECT, capacity / (SSIZE + header_size + 1));
	}

	set_field(F_SSIZE, 512);
	set_field(F_VSECT, SECT * SSIZE);
	set_field(F_SIZE, HEAD * CYL * VSECT / 512);	
		
	if(mask & ( 1 << FE_SSIZE) ) {
		mysize = 128;
		for(ssize = 0; ssize < 8 ; ssize++) {
			if(mysize == SSIZE)
				break;
			mysize += mysize;
		}
		if(ssize == 8) {
			fprintf(stderr,"Bad sector size\n");
			exit(1);
		}
		ssize = (ssize + 6) % 8;
	} else
		ssize = 0;
	
	if(VSECT && VSECT >= SSIZE) {
		fmt_gap = (capacity * 199 / 200 / (VSECT / SSIZE)) - 
			SSIZE - header_size;
		if(fmt_gap < 1)
			fmt_gap = 1;
		if(fmt_gap > 0x6c)
			fmt_gap = 0x6c;
		set_field(F_FMT_GAP, fmt_gap);
	}
	set_field(F_2M,0);

	medprm->size = SIZE;
	medprm->sect = VSECT / 512;
	medprm->head = HEAD;
	medprm->track = CYL;
	medprm->stretch = STRETCH | (SWAPSIDES << 1);
	medprm->gap = GAP;
	medprm->rate = (FM<<7) | (PERP<<6) | (ssize<<3) | (_2M<<2) | DTR;
	medprm->spec1 = SPEC1;
	medprm->fmt_gap = FMT_GAP;
}


/* ========================================= *
 * Routines called by other parts of fdutils *
 * ========================================= */


static int parse_indirect(int argc, char **argv,
		  drivedesc_t *drvprm,
		  struct floppy_struct *medprm)
{
	int found;

	if(argc != 1)
		return 2; /* more than one parameter */
	mediaprmin = fopen(MEDIAPRMFILE, "r");
	if(!mediaprmin)
		return 2; /* no file */

	zero_all(ids, &mask);

	found = 0;
	mediaprmlex(argv[0], ids, sizeof(ids)/sizeof(ids[0]), &mask, &found);
	if(!found)
		return 1;
	compute_params(drvprm, medprm);
	return 0;
}

static int parse_direct(int argc, char **argv,
			drivedesc_t *drvprm,
			struct floppy_struct *medprm)
{
	int i,r;
	mask = 0;
	zero_all(ids, &mask);
	for(i=0; i<argc; i++) {
		r=set_int(argv[i], ids, &mask);
		if(r)
			return r;
	}
	compute_params(drvprm, medprm);
	return 0;
}


/* ========================================= */
/* Testing                                   */
/* ========================================= */


int parse_mediaprm(int argc, char **argv,
		   drivedesc_t *drvprm,
		   struct floppy_struct *medprm)		  
{
	int r;
	
	return ((r=parse_direct(argc, argv, drvprm, medprm) &&
		 (r=parse_indirect(argc, argv, drvprm, medprm))));
}