summaryrefslogtreecommitdiff
path: root/sbin/edlabel/edlabel.c
blob: c6f08df97eec7e4cb87eae944bd785d0d02690a0 (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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*	$OpenBSD: edlabel.c,v 1.5 2000/08/05 21:40:14 pjanzen Exp $	*/
/*	$NetBSD: edlabel.c,v 1.1.1.1 1995/10/08 22:39:09 gwr Exp $	*/

/*
 * Copyright (c) 1995 Gordon W. Ross
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 4. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Gordon W. Ross
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#define DKTYPENAMES
#include <sys/disklabel.h>

#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <util.h>

/*
 * Machine dependend constants you want to retrieve only once...
 */
int rawpartition, maxpartitions;

/*
 * This is a data-driven program
 */
struct field {
	char *f_name;
	int f_offset;
	int f_type;	/* 1:char, 2:short, 4:int, >4:string */
};

/* Table describing fields in the head of a disklabel. */
#define	dloff(f) (int)(&((struct disklabel *)0)->f)
struct field label_head[] = {
  { "        type_num", dloff(d_type), 2 },
  { "        sub_type", dloff(d_subtype), 2 },
  { "       type_name", dloff(d_typename), 16 },
  { "       pack_name", dloff(d_packname),  16 },
  { "    bytes/sector", dloff(d_secsize), 4 },
  { "   sectors/track", dloff(d_nsectors), 4 },
  { " tracks/cylinder", dloff(d_ntracks),  4 },
  { "       cylinders", dloff(d_ncylinders), 4 },
  { "sectors/cylinder", dloff(d_secpercyl), 4 },
  /* Don't care about the others until later... */
  { 0 },
};
#undef dloff

char tmpbuf[64];

void
edit_head_field(v, f, modify)
	void *v;
	struct field *f;
	int modify;	/* also modify */
{
	u_int8_t  *cp;
	u_int tmp;

	cp = v;
	cp += f->f_offset;

	printf("%s: ", f->f_name);

	/* Print current value... */
	switch (f->f_type) {
	case 1:
		tmp = *cp;
		printf("%d", tmp);
		break;
	case 2:
		tmp = *((u_int16_t *)cp);
		printf("%d", tmp);
		break;
	case 4:
		tmp = *((u_int32_t *)cp);
		printf("%d", tmp);
		break;

	default:
		/* must be a string. */
		strncpy(tmpbuf, (char*)cp, sizeof(tmpbuf));
		printf("%s", tmpbuf);
		break;
	}

	if (modify == 0) {
		printf("\n");
		return;
	}
	printf(" ? ");
	fflush(stdout);

	tmpbuf[0] = '\0';
	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
		return;
	if ((tmpbuf[0] == '\0') || (tmpbuf[0] == '\n')) {
		/* no new value supplied. */
		return;
	}

	/* store new value */
	if (f->f_type <= 4)
		if (sscanf(tmpbuf, "%d", &tmp) != 1)
			return;

	switch (f->f_type) {
	case 1:
		*cp = tmp;
		break;
	case 2:
		*((u_int16_t *)cp) = tmp;
		break;
	case 4:
		*((u_int32_t *)cp) = tmp;
		break;
	default:
		/* Get rid of the trailing newline. */
		tmp = strlen(tmpbuf);
		if (tmp < 1)
			break;
		if (tmpbuf[tmp-1] == '\n')
			tmpbuf[tmp-1] = '\0';
		strncpy((char*)cp, tmpbuf, f->f_type);
		break;
	}
}

void
edit_head_all(d, modify)
	struct disklabel *d;
	int modify;
{
	struct field *f;

	/* Edit head stuff. */
	for (f = label_head; f->f_name; f++)
		edit_head_field(d, f, modify);
}

void
edit_geo(d)
	struct disklabel *d;
{
	int nsect, ntrack, ncyl, spc;

	nsect = ntrack = ncyl = spc = 0;

	printf("Sectors/track: ");
	fflush(stdout);
	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
		return;
	if (sscanf(tmpbuf, "%d", &nsect) != 1)
		nsect = d->d_nsectors;
	printf("Track/cyl: ");
	fflush(stdout);
	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
		return;
	if (sscanf(tmpbuf, "%d", &ntrack) != 1)
		ntrack = d->d_ntracks;
	if (!nsect || !ntrack)
		return;
	spc = nsect * ntrack;
	if (!(ncyl = d->d_secperunit / spc))
		return;
	d->d_nsectors   = nsect;
	d->d_ntracks    = ntrack;
	d->d_ncylinders = ncyl;
	d->d_secpercyl  = spc;
}

void
print_val_cts(d, val)
	struct disklabel *d;
	u_long val;
{
	int	sects, trks, cyls;
	char	marker;
	char	buf[80];

	marker = (val % d->d_secpercyl) ? '*' : ' ';
	sects  = val % d->d_nsectors;
	cyls   = val / d->d_nsectors;
	trks   = cyls % d->d_ntracks;
	cyls  /= d->d_ntracks;
	sprintf(buf, "(%d/%02d/%02d)%c", cyls, trks, sects, marker);
	printf(" %9ld %16s", val, buf);
}

void
get_val_cts(d, buf, result)
	struct disklabel *d;
	char		 *buf;
	u_int32_t	 *result;
{
	u_long tmp;
	int	cyls, trks, sects;

	tmp = sscanf(buf, "%d/%d/%d", &cyls, &trks, &sects);
	if (tmp == 1)
		*result = cyls;	/* really nblks! */
	if (tmp == 3) {
		tmp = cyls;
		tmp *= d->d_ntracks;
		tmp += trks;
		tmp *= d->d_nsectors;
		tmp += sects;
		*result = tmp;
	}
}

void
get_fstype(tmpbuf, fstype)
	char	 *tmpbuf;
	u_int8_t *fstype;
{
	int	i, len;

	/* An empty response retains previous value */
	if (tmpbuf[0] == '\n')
		return;
	for (i = 0, len = strlen(tmpbuf) - 1; i < FSMAXTYPES; i++) {
		if (!strncasecmp(tmpbuf, fstypenames[i], len)) {
			*fstype = i;
			return;
		}
	}
}

void
edit_partition(d, idx, modify)
	struct disklabel *d;
	int idx;
{
	struct partition *p;
	char letter, *comment;

	if ((idx < 0) || (idx >= maxpartitions)) {
		printf("bad partition index\n");
		return;
	}
	
	p = &d->d_partitions[idx];
	letter = 'a' + idx;

	/* Set hint about partition type */
	if (idx == rawpartition)
		comment = "disk";
	else {
		comment = "user";
		switch(idx) {
			case 0:
				comment = "root";
			break;
			case 1:
				comment = "swap";
				break;
		}
	}

	/* Print current value... */
	printf(" %c (%s) ", letter, comment);
	print_val_cts(d, p->p_offset);
	print_val_cts(d, p->p_size);
	printf(" %s\n", fstypenames[p->p_fstype]);

	if (modify == 0)
		return;

	/* starting block, or cyls/trks/sects */
	printf("start as <blkno> or <cyls/trks/sects> : ");
	fflush(stdout);
	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
		return;
	get_val_cts(d, tmpbuf, &p->p_offset);

	/* number of blocks, or cyls/trks/sects */
	printf("length as <nblks> or <cyls/trks/sects> : ");
	fflush(stdout);
	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
		return;
	get_val_cts(d, tmpbuf, &p->p_size);
	/* partition type */
	printf("type: ");
	fflush(stdout);
	if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
		return;
	get_fstype(tmpbuf, &p->p_fstype);
}

/*****************************************************************/

void
check_divisors(d)
	struct disklabel *d;
{
	if (d->d_nsectors == 0) {
		d->d_nsectors = 1;
		printf("bad sect/trk, set to 1\n");
	}
	if (d->d_ntracks == 0) {
		d->d_ntracks = 1;
		printf("bad trks/cyl, set to 1\n");
	}
	if (d->d_ncylinders == 0) {
		d->d_ncylinders = 1;
		printf("bad cyls, set to 1\n");
	}
	if (d->d_secpercyl == 0) {
		d->d_secpercyl = (d->d_nsectors * d->d_ntracks);
		printf("bad sect/cyl, set to %d\n", d->d_secpercyl);
	}

}

u_short
dkcksum(d)
	struct disklabel *d;
{
	u_short *start, *end;
	u_short sum = 0;

	start = (u_short *)d;
	end = (u_short *)&d->d_partitions[d->d_npartitions];
	while (start < end)
		sum ^= *start++;
	return (sum);
}

void
label_write(d, dn)
	struct disklabel *d;
	char *dn;
{
	int fd;

	d->d_magic = DISKMAGIC;
	d->d_magic2 = DISKMAGIC;
	d->d_checksum = 0;
	d->d_checksum = dkcksum(d);

	fd = open(dn, O_RDWR, 0);
	if (fd < 0) {
		perror(dn);
		return;
	}
	if (ioctl(fd, DIOCWDINFO, d) < 0) {
		perror("ioctl DIOCWDINFO");
	}
	close(fd);
}

void
label_read(dl, dn)
	struct disklabel *dl;
	char *dn;
{
	int fd;

	fd = open(dn, O_RDONLY, 0);
	if (fd < 0) {
		perror(dn);
		exit(1);
	}
	if (ioctl(fd, DIOCGDINFO, dl) < 0) {
		if (errno == ESRCH)
			fprintf(stderr, "edlabel: No disk label on disk\n");
		else perror("ioctl DIOCGDINFO");
		exit(1);
	}

	/* Make sure divisors are non-zero. */
	check_divisors(dl);

	close(fd);
}

/*****************************************************************/

void
label_print(dl, dn)
	struct disklabel *dl;
	char *dn;
{
	int i;

	/* Print out head stuff. */
	edit_head_all(dl, 0);

	/* And the partition header. */
	printf("partition%6sstart%9s(c/t/s)%6snblks%9s(c/t/s)  type\n\n"
							"", "", "", "", "");
	for (i = 0; i < dl->d_npartitions; i++)
		edit_partition(dl, i, 0);
}

char modify_cmds[] = "modify subcommands:\n\
 @   : modify disk parameters\n\
 a-%c : modify partition\n%s\
 q   : quit this subcommand\n";

void
label_modify(dl, dn)
	struct disklabel *dl;
	char *dn;
{
	int c, i;
	int scsi_fict = 0;

	if (!strcmp(dl->d_typename, "SCSI disk")
	     && !strcmp(dl->d_packname, "fictitious"))
		scsi_fict = 1;

	printf(modify_cmds, 'a' + maxpartitions,
		scsi_fict ? " s   : standardize geometry\n" : "");
	for (;;) {
		printf("edlabel/modify> ");
		fflush(stdout);
		if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
			break;
		c = tmpbuf[0];
		if ((c == '\0') || (c == '\n'))
			continue;	/* blank line */
		if (c == 'q')
			break;
		if (c == '@') {
			edit_head_all(dl, 1);
			check_divisors(dl);
			continue;
		}
		if ((c == 's') && scsi_fict) {
			edit_geo(dl);
			continue;
		}
		if ((c < 'a') || (c > 'q')) {
			printf("bad input.  ");
			printf(modify_cmds, 'a' + maxpartitions,
				scsi_fict ? " s   : standardize geometry\n" : "");
			continue;
		}
		edit_partition(dl, c - 'a', 1);
	}
	/* Set the d_npartitions field correctly */
	for (i = 0; i < maxpartitions; i++) {
		if (dl->d_partitions[i].p_size)
			dl->d_npartitions = i + 1;
	}

}

void
label_quit()
{
	exit(0);
}

struct cmd {
	void (*cmd_func)();
	char *cmd_name;
	char *cmd_descr;
} cmds[] = {
	{ label_print,  "print",  "display the current disk label" },
	{ label_modify, "modify", "prompt for changes to the label" },
	{ label_write,  "write",  "write the new label to disk" },
	{ label_quit,   "quit",   "terminate program" },
	{ 0 },
};

void
menu()
{
	struct cmd *cmd;

	printf("edlabel menu:\n");
	for (cmd = cmds; cmd->cmd_func; cmd++)
		printf("%s\t- %s\n", cmd->cmd_name, cmd->cmd_descr);
}

int
main(argc, argv)
	int argc;
	char **argv;
{
	struct disklabel dl;
	struct cmd *cmd;
	char *devname;

	if (argc != 2) {
		fprintf(stderr, "usage: edlabel RAWDISK\n");
		exit(1);
	}
	devname = argv[1];

	rawpartition = getrawpartition();
	maxpartitions = getmaxpartitions();

	label_read(&dl, devname);

	menu();

	for (;;) {
		printf("edlabel> ");
		fflush(stdout);
		if (fgets(tmpbuf, sizeof(tmpbuf), stdin) == NULL)
			break;
		for (cmd = cmds; cmd->cmd_func; cmd++)
			if (cmd->cmd_name[0] == tmpbuf[0])
				goto found;
		printf("Invalid command.  ");
		menu();
		continue;

	found:
		cmd->cmd_func(&dl, devname);
	}
	exit(0);
}