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