summaryrefslogtreecommitdiff
path: root/usr/src/cmd/format/add_definition.c
blob: 7c1664a8e574797252968ee1deb0823fb9df1c82 (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
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This file contains the code to add new disk_type and partition
 * definitions to a format data file.
 */
#include "global.h"

#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <memory.h>
#include <sys/fcntl.h>
#include <sys/param.h>
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>

#include "add_definition.h"
#include "misc.h"
#include "partition.h"
#include "menu_command.h"
#include "startup.h"

extern	struct	ctlr_type ctlr_types[];
extern	int	nctypes;

extern	int	errno;

/* Function prototypes */
#ifdef	__STDC__

static void	add_disktype(FILE *fd, struct disk_info *disk_info);
static void	add_partition(FILE *fd, struct disk_info *,
		struct partition_info *);
static int	add_entry(int col, FILE *fd, char *format, ...);

#else	/* __STDC__ */

static void	add_disktype();
static void	add_partition();
static int	add_entry();

#endif	/* __STDC__ */

/*
 * Add new definitions for the current disk/partition to a format data file.
 */
int
add_definition()
{
	FILE	*fd;
	char	*filename;
	time_t	clock;
	char	*prompt;
	union {
		int	xfoo;
		char	deflt_str[MAXPATHLEN];
	} x;

	/*
	 * There must be a current disk and partition table
	 */
	if (cur_disk == NULL) {
		err_print("No Current Disk.\n");
		return (0);
	}
	if (cur_dtype == NULL) {
		err_print("Current disk type is not set.\n");
		return (-1);
	}
	if (cur_parts == NULL) {
		err_print("Current partition is not set.\n");
		return (-1);
	}
	/*
	 * If neither the disk definition nor the partition
	 * information has been changed, there's nothing to save.
	 */
	if (cur_dtype->dtype_filename != NULL &&
			cur_parts->pinfo_filename != NULL) {
		err_print("\
Neither the disk type nor the partitioning has been changed.\n");
		return (-1);
	}
	/*
	 * If saving the partition, and it's unnamed, the user should name
	 * it first.
	 */
	if (cur_parts->pinfo_name == NULL) {
		assert(cur_parts->pinfo_filename == NULL);
		err_print("Please name this partition type before saving it\n");
		return (-1);
	}
	/*
	 * Let the user know what we're doing
	 */
	if (cur_dtype->dtype_filename == NULL &&
			cur_parts->pinfo_filename == NULL) {
		fmt_print("Saving new disk and partition definitions\n");
	} else if (cur_dtype->dtype_filename == NULL) {
		fmt_print("Saving new disk definition\n");
	} else {
		assert(cur_parts->pinfo_filename == NULL);
		fmt_print("Saving new partition definition\n");
	}
	/*
	 * Ask for the file to which to append the new definitions
	 */
	prompt = "Enter file name";
	(void) strcpy(x.deflt_str, "./format.dat");
	filename = (char *)(uintptr_t)input(FIO_OSTR, prompt,
		':', (u_ioparam_t *)NULL, &x.xfoo, DATA_INPUT);
	assert(filename != NULL);
	/*
	 * Open the file in append mode, or create it, if necessary
	 */
	if ((fd = fopen(filename, "a")) == NULL) {
		err_print("Cannot open `%s' - %s\n", filename,
			strerror(errno));
		destroy_data(filename);
		return (-1);
	}
	/*
	 * Write a header for the new definitions
	 */
	if ((cur_dtype->dtype_filename == NULL) &&
			(cur_parts->pinfo_filename == NULL)) {
		(void) fprintf(fd, "#\n# New disk/partition type ");
	} else if (cur_dtype->dtype_filename == NULL) {
		(void) fprintf(fd, "#\n# New disk type ");
	} else {
		(void) fprintf(fd, "#\n# New partition type ");
	}
	(void) time(&clock);
	(void) fprintf(fd, " saved on %s#\n", ctime(&clock));
	/*
	 * Save the new definitions
	 */
	if (cur_dtype->dtype_filename == NULL) {
		add_disktype(fd, cur_disk);
	}
	if (cur_parts->pinfo_filename == NULL) {
		add_partition(fd, cur_disk, cur_parts);
	}
	/*
	 * We're finished.  Clean up
	 */
	(void) fclose(fd);
	destroy_data(filename);
	return (0);
}

/*
 * Add a disk_type definition to the file fd
 */
static void
add_disktype(fd, disk_info)
	FILE			*fd;
	struct disk_info	*disk_info;
{
	int			col;
	struct disk_type	*disk_type;

	disk_type = disk_info->disk_type;

	(void) fprintf(fd, "disk_type = \"%s\" \\\n",
		disk_type->dtype_asciilabel);
	col = add_entry(0, fd, " : ctlr = %s",
		((disk_info->disk_ctlr)->ctlr_ctype)->ctype_name);

	col = add_entry(col, fd, " : ncyl = %d", disk_type->dtype_ncyl);

	col = add_entry(col, fd, " : acyl = %d", disk_type->dtype_acyl);

	col = add_entry(col, fd, " : pcyl = %d", disk_type->dtype_pcyl);

	col = add_entry(col, fd, " : nhead = %d", disk_type->dtype_nhead);

	if (disk_type->dtype_options & SUP_PHEAD) {
		col = add_entry(col, fd, " : phead = %d",
			disk_type->dtype_phead);
	}

	col = add_entry(col, fd, " : nsect = %d", disk_type->dtype_nsect);

	if (disk_type->dtype_options & SUP_PSECT) {
		col = add_entry(col, fd, " : psect = %d",
			disk_type->dtype_psect);
	}

	if (disk_type->dtype_options & SUP_BPT) {
		col = add_entry(col, fd, " : bpt = %d", disk_type->dtype_bpt);
	}

	col = add_entry(col, fd, " : rpm = %d", disk_type->dtype_rpm);

	if (disk_type->dtype_options & SUP_FMTTIME) {
		col = add_entry(col, fd, " : fmt_time = %d",
			disk_type->dtype_fmt_time);
	}

	if (disk_type->dtype_options & SUP_CYLSKEW) {
		col = add_entry(col, fd, " : cyl_skew = %d",
			disk_type->dtype_cyl_skew);
	}

	if (disk_type->dtype_options & SUP_TRKSKEW) {
		col = add_entry(col, fd, " : trk_skew = %d",
			disk_type->dtype_trk_skew);
	}

	if (disk_type->dtype_options & SUP_TRKS_ZONE) {
		col = add_entry(col, fd, " : trks_zone = %d",
			disk_type->dtype_trks_zone);
	}

	if (disk_type->dtype_options & SUP_ATRKS) {
		col = add_entry(col, fd, " : atrks = %d",
			disk_type->dtype_atrks);
	}

	if (disk_type->dtype_options & SUP_ASECT) {
		col = add_entry(col, fd, " : asect = %d",
			disk_type->dtype_asect);
	}

	if (disk_type->dtype_options & SUP_CACHE) {
		col = add_entry(col, fd, " : cache = %d",
			disk_type->dtype_cache);
	}

	if (disk_type->dtype_options & SUP_PREFETCH) {
		col = add_entry(col, fd, " : prefetch = %d",
			disk_type->dtype_threshold);
	}

	if (disk_type->dtype_options & SUP_CACHE_MIN) {
		col = add_entry(col, fd, " : min_prefetch = %d",
			disk_type->dtype_prefetch_min);
	}

	if (disk_type->dtype_options & SUP_CACHE_MAX) {
		col = add_entry(col, fd, " : max_prefetch = %d",
			disk_type->dtype_prefetch_max);
	}

	if (disk_type->dtype_options & SUP_BPS) {
		col = add_entry(col, fd, " : bps = %d",
			disk_type->dtype_bps);
	}

	if (disk_type->dtype_options & SUP_DRTYPE) {
		col = add_entry(col, fd, " : drive_type = %d",
			disk_type->dtype_dr_type);
	}

	/*
	 * Terminate the last line, and print one blank line
	 */
	(void) fprintf(fd, col == 0 ? "\n" : "\n\n");
}



/*
 * Once we exceed this length, wrap to a new line
 */
#define	MAX_COLUMNS	50

/*
 * Add a partition definition to the file fd
 */
static void
add_partition(fd, disk_info, part)
	FILE			*fd;
	struct disk_info	*disk_info;
	struct partition_info	*part;
{
	int			col;
	int			i;
	struct disk_type	*disk_type;
	struct dk_map32		*pp;
	char			*s;

#if defined(_SUNOS_VTOC_8)
	struct dk_map2		*pv;

#elif defined(_SUNOS_VTOC_16)
	struct dkl_partition	*pv;

#else
#error No VTOC format defined.
#endif			/* defined (_SUNOS_VTOC_8) */
	struct dk_map2		*dv;

	disk_type = disk_info->disk_type;

	(void) fprintf(fd, "partition = \"%s\" \\\n", part->pinfo_name);
	(void) fprintf(fd, "\t : disk = \"%s\" : ctlr = %s \\\n",
		disk_type->dtype_asciilabel,
		((disk_info->disk_ctlr)->ctlr_ctype)->ctype_name);

	/*
	 * Print the specifications for each useful partition
	 */
	col = 0;
	pp = part->pinfo_map;
	pv = part->vtoc.v_part;
	dv = default_vtoc_map;
	for (i = 0; i < NDKMAP; i++, pp++, pv++, dv++) {
		if (pp->dkl_nblk != 0) {
			col = add_entry(col, fd, " : %c = ",
				i + PARTITION_BASE);
			if (pv->p_tag != dv->p_tag ||
					pv->p_flag != dv->p_flag) {
				s = find_string(ptag_choices,
						(int)pv->p_tag);
				if (s != NULL) {
					col = add_entry(col, fd, " %s,", s);
				}
				s = find_string(pflag_choices,
						(int)pv->p_flag);
				if (s != NULL) {
					col = add_entry(col, fd, " %s,", s);
				}
			}
			col = add_entry(col, fd, " %d, %d", pp->dkl_cylno,
				pp->dkl_nblk);
		}
	}

	/*
	 * Terminate the last line, and print one blank line
	 */
	(void) fprintf(fd, col == 0 ? "\n" : "\n\n");
}

/*
 * Add an entry to the file fd.  col is the current starting column.
 * Return the resulting new column position.
 */
/*PRINTFLIKE3*/
static int
add_entry(int col, FILE *fd, char *format, ...)
{
	va_list	ap;
	va_start(ap, format);

	if (col > MAX_COLUMNS) {
		(void) fprintf(fd, " \\\n");
		col = 0;
	}
	if (col == 0) {
		col += fprintf(fd, "\t");
	}
	col += vfprintf(fd, format, ap);
	va_end(ap);

	return (col);
}