Home | History | Annotate | Line # | Download | only in edahdi
edahdi.c revision 1.4
      1 /*	$NetBSD: edahdi.c,v 1.4 2004/11/12 10:18:46 he Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Leo Weppelman, Waldi Ravens.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by
     18  *			Leo Weppelman and Waldi Ravens.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * This code implements a simple editor for partition id's on disks containing
     36  * AHDI partition info.
     37  *
     38  * Credits for the code handling disklabels goes to Waldi Ravens.
     39  *
     40  */
     41 #include <sys/types.h>
     42 #include <sys/param.h>
     43 #include <sys/stat.h>
     44 #include <sys/disklabel.h>
     45 
     46 #include <machine/ahdilabel.h>
     47 
     48 #include <fcntl.h>
     49 #include <stdlib.h>
     50 #include <curses.h>
     51 #include <termios.h>
     52 #include <unistd.h>
     53 #include <stdio.h>
     54 #include <string.h>
     55 #include <err.h>
     56 #include <ctype.h>
     57 
     58 /*
     59  * Internal partition tables:
     60  */
     61 typedef struct {
     62 	char	id[4];
     63 	u_int	start;
     64 	u_int	end;
     65 	u_int	rsec;
     66 	u_int	rent;
     67 	int	mod;
     68 } part_t;
     69 
     70 typedef struct {
     71 	int	nparts;
     72 	part_t	*parts;
     73 } ptable_t;
     74 
     75 /*
     76  * I think we can savely assume a fixed blocksize - AHDI won't support
     77  * something different...
     78  */
     79 #define BLPM		((1024 * 1024) / DEV_BSIZE)
     80 
     81 /*
     82  * #Partition entries shown on the screen at once
     83  */
     84 #define MAX_PSHOWN	16	/* #partitions shown on screen	*/
     85 
     86 /*
     87  * Tokens:
     88  */
     89 #define	T_INVAL		0
     90 #define	T_QUIT		1
     91 #define	T_WRITE		2
     92 #define	T_NEXT		3
     93 #define	T_PREV		4
     94 #define	T_NUMBER	5
     95 #define	T_EOF		6
     96 
     97 /*
     98  * Terminal capability strings (Ok, 1 to start with ;-) )
     99  */
    100 char	*Clr_screen = "";
    101 
    102 void	ahdi_cksum __P((void *));
    103 u_int	ahdi_getparts __P((int, ptable_t *, u_int, u_int));
    104 int	bsd_label __P((int, u_int));
    105 int	dkcksum __P((struct disklabel *));
    106 int	edit_parts __P((int, ptable_t *));
    107 void   *disk_read __P((int, u_int, u_int));
    108 void	disk_write __P((int, u_int, u_int, void  *));
    109 char   *get_id __P((void));
    110 void	get_termcap __P((void));
    111 int	lex __P((int *));
    112 int	show_parts __P((ptable_t *, int));
    113 void	update_disk __P((ptable_t *, int, int));
    114 
    115 int
    116 main(argc, argv)
    117 int	argc;
    118 char	*argv[];
    119 {
    120 	int		fd;
    121 	ptable_t	ptable;
    122 	int		rv;
    123 	struct stat	st;
    124 
    125 	if (argc != 2) {
    126 		char	*prog = strrchr(argv[0], '/');
    127 
    128 		if (prog == NULL)
    129 			prog = argv[0];
    130 		else prog++;
    131 		fprintf(stderr, "Usage: %s <raw_disk_device>", prog);
    132 		exit(1);
    133 	}
    134 	if ((fd = open(argv[1], O_RDWR)) < 0)
    135 		err(1, "Cannot open '%s'.", argv[1]);
    136 	if (fstat(fd, &st) < 0)
    137 		err(1, "Cannot stat '%s'.", argv[1]);
    138 	if (!S_ISCHR(st.st_mode))
    139 		errx(1, "'%s' must be a character special device.", argv[1]);
    140 
    141 	if ((rv = bsd_label(fd, LABELSECTOR)) < 0)
    142 		errx(1, "I/O error");
    143 	if (rv == 0) {
    144 		warnx("Disk has no ahdi partitions");
    145 		return (2);
    146 	}
    147 
    148 	get_termcap();
    149 
    150 	ptable.nparts = 0;
    151 	ptable.parts  = NULL;
    152 
    153 	if ((ahdi_getparts(fd, &ptable, AHDI_BBLOCK, AHDI_BBLOCK) != 0)
    154 		|| (ptable.nparts == 0))
    155 		exit (1);
    156 
    157 	edit_parts(fd, &ptable);
    158 	return (0);
    159 }
    160 
    161 int
    162 edit_parts(fd, ptable)
    163 	int		fd;
    164 	ptable_t	*ptable;
    165 {
    166 	int	scr_base = 0;
    167 	int	value;
    168 	char	*error, *new_id;
    169 
    170 	for (;;) {
    171 		error = NULL;
    172 		tputs(Clr_screen, 1, putchar);
    173 		show_parts(ptable, scr_base);
    174 
    175 		printf("\n\n");
    176 		printf("q    : quit - don't update the disk\n");
    177 		printf("w    : write changes to disk\n");
    178 		printf(">    : next screen of partitions\n");
    179 		printf("<    : previous screen of partitions\n");
    180 		printf("<nr> : modify id of partition <nr>\n");
    181 		printf("\n\nCommand? ");
    182 		fflush(stdout);
    183 
    184 		switch (lex(&value)) {
    185 		    case T_EOF:
    186 			exit(0);
    187 
    188 		    case T_INVAL:
    189 			error = "Invalid command";
    190 			break;
    191 		    case T_QUIT :
    192 			for (value = 0; value < ptable->nparts; value++) {
    193 				if (ptable->parts[value].mod) {
    194 					printf("\nThere are unwritten changes."
    195 						" Quit anyway? [n] ");
    196 					value = getchar();
    197 					if ((value == 'y') || (value == 'Y'))
    198 							exit (0);
    199 					while (value != '\n')
    200 						value = getchar();
    201 					break;
    202 				}
    203 			}
    204 			if (value == ptable->nparts)
    205 				exit(0);
    206 			break;
    207 		    case T_WRITE:
    208 			error = "No changes to write";
    209 			for (value = 0; value < ptable->nparts; value++) {
    210 				if (ptable->parts[value].mod) {
    211 					update_disk(ptable, fd, value);
    212 					error = "";
    213 				}
    214 			}
    215 			break;
    216 		    case T_NEXT :
    217 			if ((scr_base + MAX_PSHOWN) < ptable->nparts)
    218 				scr_base += MAX_PSHOWN;
    219 			break;
    220 		    case T_PREV :
    221 			scr_base -= MAX_PSHOWN;
    222 			if (scr_base < 0)
    223 				scr_base = 0;
    224 			break;
    225 		    case T_NUMBER:
    226 			if (value >= ptable->nparts) {
    227 				error = "Not that many partitions";
    228 				break;
    229 			}
    230 			if ((new_id = get_id()) == NULL) {
    231 				error = "Invalid id";
    232 				break;
    233 			}
    234 			strncpy(ptable->parts[value].id, new_id, 3);
    235 			ptable->parts[value].mod = 1;
    236 			scr_base = (value / MAX_PSHOWN) * MAX_PSHOWN;
    237 			break;
    238 		    default :
    239 			error = "Internal error - unknown token";
    240 			break;
    241 		}
    242 		if (error != NULL) {
    243 			printf("\n\n%s", error);
    244 			fflush(stdout);
    245 			sleep(2);
    246 		}
    247 	}
    248 }
    249 
    250 int
    251 show_parts(ptable, nr)
    252 	ptable_t	*ptable;
    253 	int		nr;
    254 {
    255 	int	i;
    256 	part_t	*p;
    257 	u_int	megs;
    258 
    259 	if (nr >= ptable->nparts)
    260 		return (0);	/* Nothing to show */
    261 	printf("\n\n");
    262 	printf("nr      root  desc   id     start       end    MBs\n");
    263 
    264 	p = &ptable->parts[nr];
    265 	i = nr;
    266 	for(; (i < ptable->nparts) && ((i - nr) < MAX_PSHOWN); i++, p++) {
    267 		megs = ((p->end - p->start + 1) + (BLPM >> 1)) / BLPM;
    268 		printf("%2d%s %8u  %4u  %s  %8u  %8u  (%3u)\n", i,
    269 			p->mod ? "*" : " ",
    270 			p->rsec, p->rent, p->id, p->start, p->end, megs);
    271 	}
    272 	return (1);
    273 }
    274 
    275 int
    276 lex(value)
    277 	int	*value;
    278 {
    279 	char	c[1];
    280 	int	rv, nch;
    281 
    282 	rv = T_INVAL;
    283 
    284 	*value = 0;
    285 	for (;;) {
    286 		if ((nch = read (0, c, 1)) != 1) {
    287 			if (nch == 0)
    288 				return (T_EOF);
    289 			else return (rv);
    290 		}
    291 		switch (*c) {
    292 			case 'q':
    293 				rv = T_QUIT;
    294 				goto out;
    295 			case 'w':
    296 				rv = T_WRITE;
    297 				goto out;
    298 			case '>':
    299 				rv = T_NEXT;
    300 				goto out;
    301 			case '<':
    302 				rv = T_PREV;
    303 				goto out;
    304 			default :
    305 				if (isspace((unsigned char)*c)) {
    306 					if (rv == T_INVAL)
    307 						break;
    308 					goto out;
    309 				}
    310 				else if (isdigit((unsigned char)*c)) {
    311 					*value = (10 * *value) + *c - '0';
    312 					rv = T_NUMBER;
    313 				}
    314 				goto out;
    315 		}
    316 	}
    317 	/* NOTREACHED */
    318 out:
    319 	/*
    320 	 * Flush rest of line before returning
    321 	 */
    322 	while (read (0, c, 1) == 1)
    323 		if ((*c == '\n') || (*c == '\r'))
    324 			break;
    325 	return (rv);
    326 }
    327 
    328 char *
    329 get_id()
    330 {
    331 	static char	buf[5];
    332 	       int	n;
    333 	printf("\nEnter new id: ");
    334 	if (fgets(buf, sizeof(buf), stdin) == NULL)
    335 		return (NULL);
    336 	for (n = 0; n < 3; n++) {
    337 		if (!isalpha((unsigned char)buf[n]))
    338 			return (NULL);
    339 		buf[n] = toupper((unsigned char)buf[n]);
    340 	}
    341 	buf[3] = '\0';
    342 	return (buf);
    343 }
    344 
    345 int
    346 bsd_label(fd, offset)
    347 	int		fd;
    348 	u_int		offset;
    349 {
    350 	u_char		*bblk;
    351 	u_int		nsec;
    352 	int		rv;
    353 
    354 	nsec = (BBMINSIZE + (DEV_BSIZE - 1)) / DEV_BSIZE;
    355 	bblk = disk_read(fd, offset, nsec);
    356 	if (bblk) {
    357 		u_int	*end, *p;
    358 
    359 		end = (u_int *)&bblk[BBMINSIZE - sizeof(struct disklabel)];
    360 		rv  = 1;
    361 		for (p = (u_int *)bblk; p < end; ++p) {
    362 			struct disklabel *dl = (struct disklabel *)&p[1];
    363 			if (  (  (p[0] == NBDAMAGIC && offset == 0)
    364 			      || (p[0] == AHDIMAGIC && offset != 0)
    365 			      || (u_char *)dl - bblk == 7168
    366 			      )
    367 			   && dl->d_npartitions <= MAXPARTITIONS
    368 			   && dl->d_magic2 == DISKMAGIC
    369 			   && dl->d_magic  == DISKMAGIC
    370 			   && dkcksum(dl)  == 0
    371 			   )	{
    372 				rv = 0;
    373 				break;
    374 			}
    375 		}
    376 		free(bblk);
    377 	}
    378 	else rv = -1;
    379 
    380 	return(rv);
    381 }
    382 
    383 int
    384 dkcksum(dl)
    385 	struct disklabel *dl;
    386 {
    387 	u_short	*start, *end, sum = 0;
    388 
    389 	start = (u_short *)dl;
    390 	end   = (u_short *)&dl->d_partitions[dl->d_npartitions];
    391 	while (start < end)
    392 		sum ^= *start++;
    393 	return(sum);
    394 }
    395 
    396 void
    397 ahdi_cksum(buf)
    398 void	*buf;
    399 {
    400 	unsigned short	*p = (unsigned short *)buf;
    401 	unsigned short	csum = 0;
    402 	int		i;
    403 
    404 	p[255] = 0;
    405 	for(i = 0; i < 256; i++)
    406 		csum += *p++;
    407 	*--p = (0x1234 - csum) & 0xffff;
    408 }
    409 
    410 
    411 u_int
    412 ahdi_getparts(fd, ptable, rsec, esec)
    413 	int			fd;
    414 	ptable_t		*ptable;
    415 	u_int			rsec,
    416 				esec;
    417 {
    418 	struct ahdi_part	*part, *end;
    419 	struct ahdi_root	*root;
    420 	u_int			rv;
    421 
    422 	root = disk_read(fd, rsec, 1);
    423 	if (!root) {
    424 		rv = rsec + (rsec == 0);
    425 		goto done;
    426 	}
    427 
    428 	if (rsec == AHDI_BBLOCK)
    429 		end = &root->ar_parts[AHDI_MAXRPD];
    430 	else end = &root->ar_parts[AHDI_MAXARPD];
    431 	for (part = root->ar_parts; part < end; ++part) {
    432 		u_int	id = *((u_int32_t *)&part->ap_flg);
    433 		if (!(id & 0x01000000))
    434 			continue;
    435 		if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
    436 			u_int	offs = part->ap_st + esec;
    437 			rv = ahdi_getparts(fd, ptable, offs,
    438 					esec == AHDI_BBLOCK ? offs : esec);
    439 			if (rv)
    440 				goto done;
    441 		} else {
    442 			part_t	*p;
    443 			u_int	i = ++ptable->nparts;
    444 			ptable->parts = realloc(ptable->parts,
    445 						i * sizeof *ptable->parts);
    446 			if (ptable->parts == NULL) {
    447 				fprintf(stderr, "Allocation error\n");
    448 				rv = 1;
    449 				goto done;
    450 			}
    451 			p = &ptable->parts[--i];
    452 			*((u_int32_t *)&p->id) = id << 8;
    453 			p->start = part->ap_st + rsec;
    454 			p->end   = p->start + part->ap_size - 1;
    455 			p->rsec  = rsec;
    456 			p->rent  = part - root->ar_parts;
    457 			p->mod   = 0;
    458 		}
    459 	}
    460 	rv = 0;
    461 done:
    462 	free(root);
    463 	return(rv);
    464 }
    465 
    466 void *
    467 disk_read(fd, start, count)
    468 	int	fd;
    469 	u_int	start,
    470 		count;
    471 {
    472 	char	*buffer;
    473 	off_t	offset;
    474 	size_t	size;
    475 
    476 	size   = count * DEV_BSIZE;
    477 	offset = start * DEV_BSIZE;
    478 	if ((buffer = malloc(size)) == NULL)
    479 		errx(1, "No memory");
    480 
    481 	if (lseek(fd, offset, SEEK_SET) != offset) {
    482 		free(buffer);
    483 		err(1, "Seek error");
    484 	}
    485 	if (read(fd, buffer, size) != size) {
    486 		free(buffer);
    487 		err(1, "Read error");
    488 		exit(1);
    489 	}
    490 	return(buffer);
    491 }
    492 
    493 void
    494 update_disk(ptable, fd, pno)
    495 	ptable_t	*ptable;
    496 	int		fd, pno;
    497 {
    498 	struct ahdi_root	*root;
    499 	struct ahdi_part	*apart;
    500 	part_t			*lpart;
    501 	u_int			rsec;
    502 	int			i;
    503 
    504 	rsec = ptable->parts[pno].rsec;
    505 	root = disk_read(fd, rsec, 1);
    506 
    507 	/*
    508 	 * Look for additional mods on the same sector
    509 	 */
    510 	for (i = 0; i < ptable->nparts; i++) {
    511 		lpart = &ptable->parts[i];
    512 		if (lpart->mod && (lpart->rsec == rsec)) {
    513 			apart = &root->ar_parts[lpart->rent];
    514 
    515 			/* Paranoia.... */
    516 			if ((lpart->end - lpart->start + 1) != apart->ap_size)
    517 				errx(1, "Updating wrong partition!");
    518 			apart->ap_id[0] = lpart->id[0];
    519 			apart->ap_id[1] = lpart->id[1];
    520 			apart->ap_id[2] = lpart->id[2];
    521 			lpart->mod = 0;
    522 		}
    523 	}
    524 	if (rsec == 0)
    525 		ahdi_cksum(root);
    526 	disk_write(fd, rsec, 1, root);
    527 	free(root);
    528 }
    529 
    530 void
    531 disk_write(fd, start, count, buf)
    532 	int	fd;
    533 	u_int	start,
    534 		count;
    535 	void	*buf;
    536 {
    537 	off_t	offset;
    538 	size_t	size;
    539 
    540 	size   = count * DEV_BSIZE;
    541 	offset = start * DEV_BSIZE;
    542 
    543 	if (lseek(fd, offset, SEEK_SET) != offset)
    544 		err(1, "Seek error");
    545 	if (write(fd, buf, size) != size)
    546 		err(1, "Write error");
    547 }
    548 
    549 void
    550 get_termcap()
    551 {
    552 	char	*term, tbuf[1024], buf[1024], *p;
    553 
    554 	if ((term = getenv("TERM")) == NULL)
    555 		warnx("No TERM environment variable!");
    556 	else {
    557 		if (tgetent(tbuf, term) != 1)
    558 			errx(1, "Tgetent failure.");
    559 		p = buf;
    560 		if (tgetstr("cl", &p)) {
    561 			if ((Clr_screen = malloc(strlen(buf) + 1)) == NULL)
    562 				errx(1, "Malloc failure.");
    563 			strcpy(Clr_screen, buf);
    564 		}
    565 	}
    566 }
    567