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