ahdilabel.c revision 1.6
1/* $NetBSD: ahdilabel.c,v 1.6 2005/12/11 12:17:00 christos Exp $ */
2
3/*
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julian Coleman.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include "privahdi.h"
40#include <ctype.h>
41#include <errno.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <strings.h>
45
46/*
47 * I think we can safely assume a fixed blocksize - AHDI won't support
48 * something different...
49 */
50#define	BLPM            ((1024 * 1024) / DEV_BSIZE)
51#define	UNITS_SECTORS	0
52#define	UNITS_CTS	1
53#define	PART_ROOT	0
54#define	PART_START	1
55#define	PART_END	2
56
57int		 main (int, char*[]);
58void		 show_parts (struct ahdi_ptable*, int, int, int);
59int		 get_input (char *, int);
60char		*sec_to_cts (struct ahdi_ptable*, u_int32_t, char *);
61u_int32_t	 read_sector (struct ahdi_ptable*, char *, int, int);
62void		 change_part (struct ahdi_ptable*, int, int);
63
64int
65main (argc, argv)
66	int	 argc;
67	char	*argv[];
68{
69	struct ahdi_ptable	ptable;
70	int			flags, rv, key, units;
71
72	if (argc < 2) {
73		fprintf (stderr, "usage: %s raw_disk\n", argv[0]);
74		exit (EXIT_FAILURE);
75	}
76
77	flags = AHDI_IGN_CKSUM;
78	while ((rv = ahdi_readlabel(&ptable, argv[1], flags)) != 1) {
79		switch (rv) {
80		case -1:
81			fprintf (stderr,
82			    "%s: %s: %s\n", argv[0], argv[1],
83			    strerror (errno));
84			exit (EXIT_FAILURE);
85			break;
86		case -2:
87			fprintf (stderr,
88			    "%s: disk not 512 bytes/sector\n", argv[0]);
89			exit (EXIT_FAILURE);
90			break;
91		case -3:
92			(void) fpurge(stdin);
93			printf ("No AHDI partitions found.  Continue (y/N)?");
94			if (toupper(getchar()) == 'Y') {
95				flags |= FORCE_AHDI;
96			} else
97				exit (EXIT_FAILURE);
98			break;
99		case -4:
100		case -5:
101		case -6:
102			(void) fpurge(stdin);
103			printf ("Errors reading AHDI partition table.  Override (y/N)? ");
104			if (toupper(getchar()) == 'Y') {
105				flags |= AHDI_IGN_EXISTS | AHDI_IGN_EXT |
106				    AHDI_IGN_SPU;
107			} else
108				exit (EXIT_FAILURE);
109			break;
110		case 1:
111			/* Everything is OK */
112			break;
113		default:
114			exit (EXIT_FAILURE);
115			break;
116		}
117	}
118
119	units = UNITS_SECTORS;
120	flags = AHDI_KEEP_BOOT | AHDI_KEEP_BSL;
121	show_parts (&ptable, 0, ptable.nparts, units);
122	printf ("Preserve boot sector - ");
123	flags & AHDI_KEEP_BOOT ? printf ("yes\n") :
124	    printf ("no\n");
125	printf ("Preserve bad sector list - ");
126	flags & AHDI_KEEP_BSL ? printf ("yes\n") :
127	    printf ("no\n");
128	key = 0;
129	while (key != 'Q') {
130		(void) fpurge(stdin);
131		printf ("Change [a-p], r)ecalculate, s)how, u)nits, w)rite, z)ero or q)uit ");
132		key = toupper(getchar());
133		if (key == EOF)
134			key = 'Q';
135		if (key >= 'A' && key <= 'P')
136			change_part (&ptable, key - 'A', units);
137		if (key == 'R') {
138			if (ahdi_buildlabel (&ptable)) {
139				printf ("Partiton table adjusted\n");
140			} else {
141				printf ("No changes necessary\n");
142			}
143		}
144		if (key == 'S') {
145			show_parts (&ptable, 0, ptable.nparts, units);
146			printf ("Preserve boot sector - ");
147			flags & AHDI_KEEP_BOOT ? printf ("yes\n") :
148			    printf ("no\n");
149			printf ("Preserve bad sector list - ");
150			flags & AHDI_KEEP_BSL ? printf ("yes\n") :
151			    printf ("no\n");
152		}
153		if (key == 'U') {
154			if (units == UNITS_SECTORS) {
155				printf ("Units now cylinder/track/sector\n");
156				units = UNITS_CTS;
157			} else {
158				printf ("Units now sector\n");
159				units = UNITS_SECTORS;
160			}
161		}
162		if (key == 'W') {
163			if ((rv = ahdi_writelabel (&ptable, argv[1], flags)) < 0) {
164				if (rv == -1)
165					perror ("\0");
166				if (rv == -2)
167					printf ("Invalid number of partitions!\n");
168				if (rv == -3)
169					printf ("GEM partition should be BGM or BGM partition should be GEM!\n");
170				if (rv == -4)
171					printf ("Partition overlaps root sector or bad sector list (starts before sector 2)!\n");
172				if (rv == -5)
173					printf ("Partition extends past end of disk!\n");
174				if (rv == -6)
175					printf ("Partitions overlap!\n");
176				if (rv == -7)
177					printf ("Partition overlaps auxiliary root!\n");
178				if (rv == -8)
179					printf ("More than 4 partitions in root sector!\n");
180				if (rv == -9)
181					printf ("More than 1 partition in an auxiliary root!\n");
182				if (rv < -1 && ahdi_errp1 != -1)
183					printf ("\tpartition %c has errors.\n",
184					    ahdi_errp1 + 'a');
185				if (rv < -1 && ahdi_errp2 != -1)
186					printf ("\tpartition %c has errors.\n",
187					    ahdi_errp2 + 'a');
188			}
189		}
190		if (key == 'Z') {
191			(void) fpurge(stdin);
192			printf ("Preserve boot sector? ");
193			if (flags & AHDI_KEEP_BOOT) {
194				printf ("[y] ");
195				if (toupper (getchar ()) == 'N')
196					flags &= ~AHDI_KEEP_BOOT;
197			} else {
198				printf ("[n] ");
199				if (toupper (getchar ()) == 'Y')
200					flags |= AHDI_KEEP_BOOT;
201			}
202			(void) fpurge(stdin);
203			printf ("Preserve bad sector list? ");
204			if (flags & AHDI_KEEP_BSL) {
205				printf ("[y] ");
206				if (toupper (getchar ()) == 'N')
207					flags &= ~AHDI_KEEP_BSL;
208			} else {
209				printf ("[n] ");
210				if (toupper (getchar ()) == 'Y')
211					flags |= AHDI_KEEP_BSL;
212			}
213		}
214	}
215	return (0);
216}
217
218void
219show_parts (ptable, start, finish, units)
220	struct ahdi_ptable	*ptable;
221	int			 start, finish, units;
222{
223	int	i;
224
225	printf ("Disk information :\n");
226	printf ("  sectors/track: %d\n", ptable->nsectors);
227	printf ("  tracks/cylinder: %d\n", ptable->ntracks);
228	printf ("  sectors/cylinder: %d\n", ptable->secpercyl);
229	printf ("  cylinders: %d\n", ptable->ncylinders);
230	printf ("  total sectors: %d\n", ptable->secperunit);
231
232	if (units == UNITS_SECTORS) {
233		printf ("  #  id      root     start       end      size   MBs\n");
234		for (i = start; i < finish; i++) {
235			if (finish - start > 10 && i - start == 8) {
236				(void) fpurge(stdin);
237				printf ("-- Press return for more -- ");
238				(void) getchar();
239			}
240			printf ("  %c %c%c%c  %8u  %8u  %8u  %8u  (%4u)\n",
241			    i + 'a', ptable->parts[i].id[0],
242			    ptable->parts[i].id[1], ptable->parts[i].id[2],
243			    ptable->parts[i].root, ptable->parts[i].start,
244			    ptable->parts[i].start +
245			    (ptable->parts[i].size ?
246			    ptable->parts[i].size - 1 : 0),
247			    ptable->parts[i].size,
248			    (ptable->parts[i].size + (BLPM >> 1)) / BLPM);
249		}
250	} else {
251		u_int32_t	cylinder, track, sector;
252		printf ("  #  id          root         start           end          size    MBs\n");
253		for (i = start; i < finish; i++) {
254			if (finish - start > 10 && i - start == 8) {
255				(void) fpurge(stdin);
256				printf ("-- Press return for more -- ");
257				(void) getchar();
258			}
259			printf ("  %c %c%c%c  ", i + 'a',
260			    ptable->parts[i].id[0], ptable->parts[i].id[1],
261			    ptable->parts[i].id[2]);
262			sector = ptable->parts[i].root;
263			cylinder = sector / ptable->secpercyl;
264			sector -= cylinder * ptable->secpercyl;
265			track = sector / ptable->nsectors;
266			sector -= track * ptable->nsectors;
267			printf ("%5u/%2u/%3u  ", cylinder, track, sector);
268			sector = ptable->parts[i].start;
269			cylinder = sector / ptable->secpercyl;
270			sector -= cylinder * ptable->secpercyl;
271			track = sector / ptable->nsectors;
272			sector -= track * ptable->nsectors;
273			printf ("%5u/%2u/%3u  ", cylinder, track, sector);
274			sector = ptable->parts[i].start +
275			    (ptable->parts[i].size ?
276			    ptable->parts[i].size - 1 : 0),
277			cylinder = sector / ptable->secpercyl;
278			sector -= cylinder * ptable->secpercyl;
279			track = sector / ptable->nsectors;
280			sector -= track * ptable->nsectors;
281			printf ("%5u/%2u/%3u  ", cylinder, track, sector);
282			sector = ptable->parts[i].size;
283			cylinder = sector / ptable->secpercyl;
284			sector -= cylinder * ptable->secpercyl;
285			track = sector / ptable->nsectors;
286			sector -= track * ptable->nsectors;
287			printf ("%5u/%2u/%3u   ", cylinder, track, sector);
288			printf ("(%4u)\n",
289			    (ptable->parts[i].size + (BLPM >> 1)) / BLPM);
290		}
291	}
292}
293
294int
295get_input (buf, len)
296	char	*buf;
297	int	 len;
298{
299	int count, key;
300
301	count = 0;
302	(void) fpurge(stdin);
303	while (count < (len - 1) && (key = getchar()) != '\n' && key != '\r') {
304		buf[count] = key;
305		count++;
306	}
307	buf[count] = '\0';
308	return(count);
309}
310
311char *
312sec_to_cts (ptable, sector, cts)
313	struct ahdi_ptable	*ptable;
314	u_int32_t	 sector;
315	char		*cts;
316{
317	u_int32_t	cylinder, track;
318
319	cylinder = sector / ptable->secpercyl;
320	sector -= cylinder * ptable->secpercyl;
321	track = sector / ptable->nsectors;
322	sector -= track * ptable->nsectors;
323	sprintf (cts, "%u/%u/%u", cylinder, track, sector);
324	return (cts);
325}
326
327u_int32_t
328read_sector (ptable, buf, part, se)
329	struct ahdi_ptable	*ptable;
330	char			*buf;
331	int			 part, se;
332{
333	u_int32_t	sector, track, cylinder;
334	int		i;
335
336	sector = track = cylinder = 0;
337	if ((strchr (buf, '/') != NULL) &&
338	    ((sscanf (buf, "%u/%u/%u", &cylinder, &track, &sector) == 3) ||
339	    (sscanf (buf, "%u/%u/", &cylinder, &track) == 2) ||
340	    (sscanf (buf, "%u/", &cylinder) == 1))) {
341		if (sector > ptable->nsectors || track > ptable->ntracks ||
342		    cylinder > ptable->ncylinders)
343			return (0);
344		sector += ptable->nsectors * track;
345		sector += ptable->secpercyl * cylinder;
346		return (sector);
347	}
348	if (buf[0] == '-' && buf[1]) {
349		if (buf[1] == '1' && se == PART_END)
350			/* Extend to end of disk */
351			return (ptable->secperunit -
352			    ptable->parts[part].start);
353		i = (int) (toupper ((unsigned char)(buf[1]) - 'A'));
354		if (i >= 0 && i <= ptable->nparts ) {
355			if (se == PART_ROOT && part > i)
356				/* Root after partition ... */
357				return (ptable->parts[i].start +
358				    ptable->parts[i].size);
359			if (se == PART_START && part > i) {
360				/* Start after partition ... */
361				if (ptable->parts[part].root)
362					return (ptable->parts[i].start +
363					    ptable->parts[i].size + 1);
364				else
365					return (ptable->parts[i].start +
366					    ptable->parts[i].size);
367			}
368			if (se == PART_END && part < i)
369				/* End before partition ... */
370				return (ptable->parts[i].root -
371				    ptable->parts[part].start);
372		}
373		return (0);
374	}
375	if (sscanf (buf, "%u", &sector) == 1) {
376		if (buf[strlen (buf) - 1] == 'm' ||
377		    buf[strlen (buf) - 1] == 'M')
378			sector *= BLPM;
379		return (sector);
380	}
381	return (0);
382}
383
384void
385change_part (ptable, part, units)
386	struct ahdi_ptable	*ptable;
387	int			 part, units;
388{
389#define BUFLEN	20
390#define CTSLEN	64
391	char		buf[BUFLEN], cts[CTSLEN];
392	u_int32_t	sector;
393
394	if (part > ptable->nparts) {
395		part = ptable->nparts;
396		printf ("Changing partition %c!\n", part + 'a');
397		ptable->nparts++;
398	}
399	if (part == ptable->nparts)
400		ptable->nparts++;
401	show_parts (ptable, part, part + 1, units);
402
403	printf ("id [%c%c%c] ", ptable->parts[part].id[0],
404	    ptable->parts[part].id[1], ptable->parts[part].id[2]);
405	if (get_input (&buf[0], BUFLEN) > 2) {
406		ptable->parts[part].id[0] = buf[0];
407		ptable->parts[part].id[1] = buf[1];
408		ptable->parts[part].id[2] = buf[2];
409	}
410
411	printf ("root [%8u (%s)] ", ptable->parts[part].root,
412	    sec_to_cts (ptable, ptable->parts[part].root, &cts[0]));
413	if (get_input (&buf[0], BUFLEN)) {
414		sector = read_sector (ptable, buf, part, PART_ROOT);
415		ptable->parts[part].root = sector;
416	}
417
418	printf ("start [%8u (%s)] ", ptable->parts[part].start,
419	    sec_to_cts (ptable, ptable->parts[part].start, &cts[0]));
420	if (get_input (&buf[0], BUFLEN)) {
421		sector = read_sector (ptable, buf, part, PART_START);
422		ptable->parts[part].start = sector;
423	}
424
425	printf ("size [%8u (%s) (%4uM)] ", ptable->parts[part].size,
426	    sec_to_cts (ptable, ptable->parts[part].size, &cts[0]),
427	    (ptable->parts[part].size + (BLPM >> 1)) / BLPM);
428	if (get_input (&buf[0], BUFLEN)) {
429		sector = read_sector (ptable, buf, part, PART_END);
430		ptable->parts[part].size = sector;
431	}
432
433/*
434	printf ("NetBSD disk letter [%c] ", ptable->parts[part].letter + 'a');
435	if (get_input (&buf[0], BUFLEN)) {
436		buf[0] = tolower(buf[0]);
437		if (buf[0] == 'a' || (buf[0] >= 'd' && buf[0] <= 'p'))
438			ptable->parts[part].letter = buf[0] - 'a';
439*/
440
441	if (!ptable->parts[part].start && !ptable->parts[part].size) {
442	    if (part == ptable->nparts - 1)
443		ptable->nparts--;
444	}
445}
446