Home | History | Annotate | Line # | Download | only in sgivol
sgivol.c revision 1.4
      1  1.4   simonb /*	$NetBSD: sgivol.c,v 1.4 2002/03/13 13:12:30 simonb Exp $	*/
      2  1.1    soren 
      3  1.1    soren /*-
      4  1.1    soren  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.1    soren  * All rights reserved.
      6  1.1    soren  *
      7  1.1    soren  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1    soren  * by Michael Hitch and Hubert Feyrer.
      9  1.1    soren  *
     10  1.1    soren  * Redistribution and use in source and binary forms, with or without
     11  1.1    soren  * modification, are permitted provided that the following conditions
     12  1.1    soren  * are met:
     13  1.1    soren  * 1. Redistributions of source code must retain the above copyright
     14  1.1    soren  *    notice, this list of conditions and the following disclaimer.
     15  1.1    soren  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1    soren  *    notice, this list of conditions and the following disclaimer in the
     17  1.1    soren  *    documentation and/or other materials provided with the distribution.
     18  1.1    soren  * 3. All advertising materials mentioning features or use of this software
     19  1.1    soren  *    must display the following acknowledgement:
     20  1.1    soren  *        This product includes software developed by the NetBSD
     21  1.1    soren  *        Foundation, Inc. and its contributors.
     22  1.1    soren  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1    soren  *    contributors may be used to endorse or promote products derived
     24  1.1    soren  *    from this software without specific prior written permission.
     25  1.1    soren  *
     26  1.1    soren  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1    soren  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1    soren  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1    soren  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1    soren  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1    soren  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1    soren  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1    soren  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1    soren  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1    soren  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1    soren  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1    soren  */
     38  1.1    soren 
     39  1.2  thorpej #include <sys/types.h>
     40  1.2  thorpej #include <sys/ioctl.h>
     41  1.2  thorpej #include <sys/disklabel.h>
     42  1.2  thorpej #include <sys/stat.h>
     43  1.2  thorpej 
     44  1.1    soren #include <stdio.h>
     45  1.2  thorpej #include <stdlib.h>
     46  1.1    soren #include <unistd.h>
     47  1.1    soren #include <string.h>
     48  1.1    soren #include <fcntl.h>
     49  1.1    soren #include <util.h>
     50  1.1    soren 
     51  1.1    soren #define SGI_SIZE_VOLHDR	3135	/* XXX Irix: 2592, NetBSD: 3753 */
     52  1.1    soren 
     53  1.1    soren int	fd;
     54  1.1    soren int	opt_i;			/* Initialize volume header */
     55  1.1    soren int	opt_r;			/* Read a file from volume header */
     56  1.1    soren int	opt_w;			/* Write a file to volume header */
     57  1.1    soren int	opt_d;			/* Delete a file from volume header */
     58  1.1    soren int	opt_p;			/* Modify a partition */
     59  1.1    soren int	partno, partfirst, partblocks, parttype;
     60  1.1    soren struct sgilabel *volhdr;
     61  1.2  thorpej int32_t	checksum;
     62  1.2  thorpej 
     63  1.2  thorpej const char *vfilename = "";
     64  1.2  thorpej const char *ufilename = "";
     65  1.1    soren 
     66  1.1    soren struct disklabel lbl;
     67  1.1    soren 
     68  1.1    soren unsigned char buf[512];
     69  1.1    soren 
     70  1.2  thorpej const char *sgi_types[] = {
     71  1.1    soren 	"Volume Header",
     72  1.1    soren 	"Repl Trks",
     73  1.1    soren 	"Repl Secs",
     74  1.1    soren 	"Raw",
     75  1.1    soren 	"BSD4.2",
     76  1.1    soren 	"SysV",
     77  1.1    soren 	"Volume",
     78  1.1    soren 	"EFS",
     79  1.1    soren 	"LVol",
     80  1.1    soren 	"RLVol",
     81  1.1    soren 	"XFS",
     82  1.1    soren 	"XSFLog",
     83  1.1    soren 	"XLV",
     84  1.1    soren 	"XVM"
     85  1.1    soren };
     86  1.1    soren 
     87  1.2  thorpej int	main(int, char *[]);
     88  1.2  thorpej 
     89  1.2  thorpej void	display_vol(void);
     90  1.2  thorpej void	init_volhdr(void);
     91  1.2  thorpej void	read_file(void);
     92  1.2  thorpej void	write_file(void);
     93  1.2  thorpej void	delete_file(void);
     94  1.2  thorpej void	modify_partition(void);
     95  1.2  thorpej void	write_volhdr(void);
     96  1.2  thorpej int	allocate_space(int);
     97  1.2  thorpej void	checksum_vol(void);
     98  1.2  thorpej void	usage(void);
     99  1.2  thorpej 
    100  1.2  thorpej int
    101  1.2  thorpej main(int argc, char *argv[])
    102  1.1    soren {
    103  1.1    soren 	if (argc < 2)
    104  1.1    soren 		usage();
    105  1.1    soren 
    106  1.1    soren 	if (argv[1][0] == '-') {
    107  1.1    soren 		switch(argv[1][1]) {
    108  1.1    soren 		case 'i':
    109  1.1    soren 			++opt_i;
    110  1.1    soren 			argv++;
    111  1.1    soren 			argc--;
    112  1.1    soren 			break;
    113  1.1    soren 		case 'r':
    114  1.1    soren 		case 'w':
    115  1.1    soren 			if (argc < 4)
    116  1.1    soren 				usage();
    117  1.1    soren 			if (argv[1][1] == 'r')
    118  1.1    soren 				++opt_r;
    119  1.1    soren 			else
    120  1.1    soren 				++opt_w;
    121  1.1    soren 			vfilename = argv[2];
    122  1.1    soren 			ufilename = argv[3];
    123  1.1    soren 			argv += 3;
    124  1.1    soren 			argc -= 3;
    125  1.1    soren 			break;
    126  1.1    soren 		case 'd':
    127  1.1    soren 			if (argc < 3)
    128  1.1    soren 				usage();
    129  1.1    soren 			++opt_d;
    130  1.1    soren 			vfilename = argv[2];
    131  1.1    soren 			argv += 2;
    132  1.1    soren 			argc -= 2;
    133  1.1    soren 				break;
    134  1.1    soren 		case 'p':
    135  1.1    soren 			if (argc < 6)
    136  1.1    soren 				usage();
    137  1.1    soren 			++opt_p;
    138  1.1    soren 			partno = atoi(argv[2]);
    139  1.1    soren 			partfirst = atoi(argv[3]);
    140  1.1    soren 			partblocks = atoi(argv[4]);
    141  1.1    soren 			parttype = atoi(argv[5]);
    142  1.1    soren 			argv += 5;
    143  1.1    soren 			argc -= 5;
    144  1.1    soren 			break;
    145  1.1    soren 		default:
    146  1.1    soren 			printf("-%c Invalid\n", argv[1][1]);
    147  1.1    soren 			usage();
    148  1.1    soren 		}
    149  1.1    soren 	}
    150  1.1    soren 
    151  1.1    soren 	if (argc < 2)
    152  1.1    soren 		usage();
    153  1.1    soren 
    154  1.1    soren 	fd = open(argv[1], (opt_i | opt_w | opt_d | opt_p) ? O_RDWR : O_RDONLY);
    155  1.1    soren 	if (fd < 0) {
    156  1.1    soren 		sprintf(buf, "/dev/r%s%c", argv[1], 'a' + getrawpartition());
    157  1.2  thorpej 		fd = open(buf,
    158  1.2  thorpej 		    (opt_i | opt_w | opt_d | opt_p) ? O_RDWR : O_RDONLY);
    159  1.1    soren 		if (fd < 0) {
    160  1.1    soren 			perror("open");
    161  1.1    soren 			exit(1);
    162  1.1    soren 		}
    163  1.1    soren 	}
    164  1.2  thorpej 	if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
    165  1.1    soren 		perror("read volhdr");
    166  1.1    soren 		exit(1);
    167  1.1    soren 	}
    168  1.2  thorpej 	if (ioctl(fd, DIOCGDINFO, &lbl) < 0) {
    169  1.1    soren 		perror("DIOCGDINFO");
    170  1.1    soren 		exit(1);
    171  1.1    soren 	}
    172  1.1    soren 	volhdr = (struct sgilabel *)buf;
    173  1.1    soren 	if (opt_i) {
    174  1.1    soren 		init_volhdr();
    175  1.1    soren 		exit(0);
    176  1.1    soren 	}
    177  1.1    soren 	if (volhdr->magic != SGILABEL_MAGIC) {
    178  1.2  thorpej 		printf("No SGI volume header found, magic=%x\n", volhdr->magic);
    179  1.1    soren 		exit(1);
    180  1.1    soren 	}
    181  1.1    soren 	if (opt_r) {
    182  1.1    soren 		read_file();
    183  1.1    soren 		exit(0);
    184  1.1    soren 	}
    185  1.1    soren 	if (opt_w) {
    186  1.1    soren 		write_file();
    187  1.1    soren 		exit(0);
    188  1.1    soren 	}
    189  1.1    soren 	if (opt_d) {
    190  1.1    soren 		delete_file();
    191  1.1    soren 		exit(0);
    192  1.1    soren 	}
    193  1.1    soren 	if (opt_p) {
    194  1.1    soren 		modify_partition();
    195  1.1    soren 		exit(0);
    196  1.1    soren 	}
    197  1.1    soren 	display_vol();
    198  1.1    soren 
    199  1.1    soren 	return 0;
    200  1.1    soren }
    201  1.1    soren 
    202  1.2  thorpej void
    203  1.2  thorpej display_vol(void)
    204  1.1    soren {
    205  1.2  thorpej 	int32_t *l;
    206  1.2  thorpej 	int i;
    207  1.2  thorpej 
    208  1.1    soren 	printf("disklabel shows %d sectors\n", lbl.d_secperunit);
    209  1.2  thorpej 	l = (int32_t *)buf;
    210  1.1    soren 	checksum = 0;
    211  1.2  thorpej 	for (i = 0; i < 512 / 4; ++i)
    212  1.2  thorpej 		checksum += l[i];
    213  1.1    soren 	printf("checksum: %08x%s\n", checksum, checksum == 0 ? "" : " *ERROR*");
    214  1.1    soren 	printf("root part: %d\n", volhdr->root);
    215  1.1    soren 	printf("swap part: %d\n", volhdr->swap);
    216  1.2  thorpej 	printf("bootfile: %s\n", volhdr->bootfile);
    217  1.1    soren 	/* volhdr->devparams[0..47] */
    218  1.1    soren 	printf("\nVolume header files:\n");
    219  1.2  thorpej 	for (i = 0; i < 15; ++i)
    220  1.2  thorpej 		if (volhdr->voldir[i].name[0])
    221  1.1    soren 			printf("%-8s offset %4d blocks, length %8d bytes (%d blocks)\n",
    222  1.2  thorpej 			    volhdr->voldir[i].name, volhdr->voldir[i].block,
    223  1.2  thorpej 			    volhdr->voldir[i].bytes, (volhdr->voldir[i].bytes + 511 ) / 512);
    224  1.1    soren 	printf("\nSGI partitions:\n");
    225  1.2  thorpej 	for (i = 0; i < MAXPARTITIONS; ++i) {
    226  1.2  thorpej 		if (volhdr->partitions[i].blocks) {
    227  1.2  thorpej 			printf("%2d:%c blocks %8d first %8d type %2d (%s)\n",
    228  1.2  thorpej 			    i, i + 'a', volhdr->partitions[i].blocks,
    229  1.2  thorpej 			    volhdr->partitions[i].first,
    230  1.2  thorpej 			    volhdr->partitions[i].type,
    231  1.2  thorpej 			    volhdr->partitions[i].type > 13 ? "???" :
    232  1.2  thorpej 			    sgi_types[volhdr->partitions[i].type]);
    233  1.2  thorpej 		}
    234  1.2  thorpej 	}
    235  1.1    soren }
    236  1.1    soren 
    237  1.2  thorpej void
    238  1.2  thorpej init_volhdr(void)
    239  1.1    soren {
    240  1.1    soren 	memset(buf, 0, sizeof(buf));
    241  1.1    soren 	volhdr->magic = SGILABEL_MAGIC;
    242  1.1    soren 	volhdr->root = 0;
    243  1.1    soren 	volhdr->swap = 1;
    244  1.2  thorpej 	strcpy(volhdr->bootfile, "/netbsd");
    245  1.4   simonb 	volhdr->dp.dp_skew = lbl.d_trackskew;
    246  1.4   simonb 	volhdr->dp.dp_gap1 = 1; /* XXX */
    247  1.4   simonb 	volhdr->dp.dp_gap2 = 1; /* XXX */
    248  1.4   simonb 	volhdr->dp.dp_cyls = lbl.d_ncylinders;
    249  1.4   simonb 	volhdr->dp.dp_shd0 = 0;
    250  1.4   simonb 	volhdr->dp.dp_trks0 = lbl.d_ntracks;
    251  1.4   simonb 	volhdr->dp.dp_secs = lbl.d_nsectors;
    252  1.4   simonb 	volhdr->dp.dp_secbytes = lbl.d_secsize;
    253  1.4   simonb 	volhdr->dp.dp_interleave = lbl.d_interleave;
    254  1.4   simonb 	volhdr->dp.dp_nretries = 22;
    255  1.1    soren 	volhdr->partitions[10].blocks = lbl.d_secperunit;
    256  1.1    soren 	volhdr->partitions[10].first = 0;
    257  1.1    soren 	volhdr->partitions[10].type = SGI_PTYPE_VOLUME;
    258  1.1    soren 	volhdr->partitions[8].blocks = SGI_SIZE_VOLHDR;
    259  1.1    soren 	volhdr->partitions[8].first = 0;
    260  1.1    soren 	volhdr->partitions[8].type = SGI_PTYPE_VOLHDR;
    261  1.1    soren 	volhdr->partitions[0].blocks = lbl.d_secperunit - SGI_SIZE_VOLHDR;
    262  1.1    soren 	volhdr->partitions[0].first = SGI_SIZE_VOLHDR;
    263  1.2  thorpej 	volhdr->partitions[0].type = SGI_PTYPE_BSD;
    264  1.1    soren 	write_volhdr();
    265  1.1    soren }
    266  1.1    soren 
    267  1.2  thorpej void
    268  1.2  thorpej read_file(void)
    269  1.1    soren {
    270  1.1    soren 	FILE *fp;
    271  1.2  thorpej 	int i;
    272  1.1    soren 
    273  1.1    soren 	printf("Reading file %s\n", vfilename);
    274  1.2  thorpej 	for (i = 0; i < 15; ++i) {
    275  1.3  thorpej 		if (strncmp(vfilename, volhdr->voldir[i].name,
    276  1.3  thorpej 		    sizeof(volhdr->voldir[i].name)) == NULL)
    277  1.1    soren 			break;
    278  1.1    soren 	}
    279  1.2  thorpej 	if (i >= 15) {
    280  1.1    soren 		printf("file %s not found\n", vfilename);
    281  1.1    soren 		exit(1);
    282  1.1    soren 	}
    283  1.1    soren 	/* XXX assumes volume header starts at 0? */
    284  1.2  thorpej 	lseek(fd, volhdr->voldir[i].block * 512, SEEK_SET);
    285  1.1    soren 	fp = fopen(ufilename, "w");
    286  1.1    soren 	if (fp == NULL) {
    287  1.1    soren 		perror("open write");
    288  1.1    soren 		exit(1);
    289  1.1    soren 	}
    290  1.2  thorpej 	i = volhdr->voldir[i].bytes;
    291  1.2  thorpej 	while (i > 0) {
    292  1.1    soren 		if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
    293  1.1    soren 			perror("read file");
    294  1.1    soren 			exit(1);
    295  1.1    soren 		}
    296  1.2  thorpej 		fwrite(buf, 1, i > sizeof(buf) ? sizeof(buf) : i, fp);
    297  1.2  thorpej 		i -= i > sizeof(buf) ? sizeof(buf) : i;
    298  1.1    soren 	}
    299  1.1    soren 	fclose(fp);
    300  1.1    soren }
    301  1.1    soren 
    302  1.2  thorpej void
    303  1.2  thorpej write_file(void)
    304  1.1    soren {
    305  1.1    soren 	FILE *fp;
    306  1.2  thorpej 	int slot;
    307  1.2  thorpej 	size_t namelen;
    308  1.2  thorpej 	int block, i;
    309  1.1    soren 	struct stat st;
    310  1.1    soren 	char fbuf[512];
    311  1.1    soren 
    312  1.1    soren 	printf("Writing file %s\n", ufilename);
    313  1.1    soren 	if (stat(ufilename, &st) < 0) {
    314  1.1    soren 		perror("stat");
    315  1.1    soren 		exit(1);
    316  1.1    soren 	}
    317  1.1    soren 	printf("File %s has %lld bytes\n", ufilename, st.st_size);
    318  1.2  thorpej 	slot = -1;
    319  1.2  thorpej 	for (i = 0; i < 15; ++i) {
    320  1.2  thorpej 		if (volhdr->voldir[i].name[0] == '\0' && slot < 0)
    321  1.2  thorpej 			slot = i;
    322  1.2  thorpej 		if (strcmp(vfilename, volhdr->voldir[i].name) == 0) {
    323  1.2  thorpej 			slot = i;
    324  1.1    soren 			break;
    325  1.1    soren 		}
    326  1.1    soren 	}
    327  1.2  thorpej 	if (slot == -1) {
    328  1.2  thorpej 		printf("No directory space for file %s\n", vfilename);
    329  1.1    soren 		exit(1);
    330  1.1    soren 	}
    331  1.1    soren 	/* -w can overwrite, -a won't overwrite */
    332  1.2  thorpej 	if (volhdr->voldir[slot].block > 0) {
    333  1.1    soren 		printf("File %s exists, removing old file\n", vfilename);
    334  1.2  thorpej 		volhdr->voldir[slot].name[0] = 0;
    335  1.2  thorpej 		volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0;
    336  1.1    soren 	}
    337  1.1    soren 	if (st.st_size == 0) {
    338  1.1    soren 		printf("bad file size?\n");
    339  1.1    soren 		exit(1);
    340  1.1    soren 	}
    341  1.1    soren 	/* XXX assumes volume header starts at 0? */
    342  1.1    soren 	block = allocate_space((int)st.st_size);
    343  1.1    soren 	if (block < 0) {
    344  1.1    soren 		printf("No space for file\n");
    345  1.1    soren 		exit(1);
    346  1.1    soren 	}
    347  1.1    soren 
    348  1.2  thorpej 	/*
    349  1.2  thorpej 	 * Make sure the name in the volume header is max. 8 chars,
    350  1.2  thorpej 	 * NOT including NUL.
    351  1.2  thorpej 	 */
    352  1.2  thorpej 	namelen = strlen(vfilename);
    353  1.2  thorpej 	if (namelen > sizeof(volhdr->voldir[slot].name)) {
    354  1.1    soren 		printf("Warning: '%s' is too long for volume header, ",
    355  1.1    soren 		       vfilename);
    356  1.2  thorpej 		namelen = sizeof(volhdr->voldir[slot].name);
    357  1.2  thorpej 		printf("truncating to '%-8s'\n", vfilename);
    358  1.1    soren 	}
    359  1.4   simonb 
    360  1.2  thorpej 	/* Populate it w/ NULs */
    361  1.2  thorpej 	memset(volhdr->voldir[slot].name, 0,
    362  1.2  thorpej 	    sizeof(volhdr->voldir[slot].name));
    363  1.2  thorpej 	/* Then copy the name */
    364  1.2  thorpej 	memcpy(volhdr->voldir[slot].name, vfilename, namelen);
    365  1.2  thorpej 
    366  1.2  thorpej 	volhdr->voldir[slot].block = block;
    367  1.2  thorpej 	volhdr->voldir[slot].bytes = st.st_size;
    368  1.1    soren 
    369  1.1    soren 	write_volhdr();
    370  1.1    soren 
    371  1.1    soren 	/* write the file itself */
    372  1.2  thorpej 	i = lseek(fd, block * 512, SEEK_SET);
    373  1.2  thorpej 	if (i < 0) {
    374  1.1    soren 		perror("lseek write");
    375  1.1    soren 		exit(1);
    376  1.1    soren 	}
    377  1.2  thorpej 	i = st.st_size;
    378  1.1    soren 	fp = fopen(ufilename, "r");
    379  1.2  thorpej 	while (i > 0) {
    380  1.2  thorpej 		fread(fbuf, 1, i > 512 ? 512 : i, fp);
    381  1.1    soren 		if (write(fd, fbuf, 512) != 512) {
    382  1.1    soren 			perror("write file");
    383  1.1    soren 			exit(1);
    384  1.1    soren 		}
    385  1.2  thorpej 		i -= i > 512 ? 512 : i;
    386  1.1    soren 	}
    387  1.1    soren }
    388  1.1    soren 
    389  1.2  thorpej void
    390  1.2  thorpej delete_file(void)
    391  1.1    soren {
    392  1.2  thorpej 	int i;
    393  1.2  thorpej 
    394  1.2  thorpej 	for (i = 0; i < 15; ++i) {
    395  1.2  thorpej 		if (strcmp(vfilename, volhdr->voldir[i].name) == NULL) {
    396  1.1    soren 			break;
    397  1.1    soren 		}
    398  1.1    soren 	}
    399  1.2  thorpej 	if (i >= 15) {
    400  1.1    soren 		printf("File %s not found\n", vfilename);
    401  1.1    soren 		exit(1);
    402  1.1    soren 	}
    403  1.2  thorpej 	volhdr->voldir[i].name[0] = '\0';
    404  1.2  thorpej 	volhdr->voldir[i].block = volhdr->voldir[i].bytes = 0;
    405  1.1    soren 	write_volhdr();
    406  1.1    soren }
    407  1.1    soren 
    408  1.2  thorpej void
    409  1.2  thorpej modify_partition(void)
    410  1.1    soren {
    411  1.2  thorpej 	printf("Modify partition %d start %d length %d\n", partno, partfirst,
    412  1.2  thorpej 	    partblocks);
    413  1.1    soren 	if (partno < 0 || partno > 15) {
    414  1.1    soren 		printf("Invalue partition number: %d\n", partno);
    415  1.1    soren 		exit(1);
    416  1.1    soren 	}
    417  1.1    soren 	volhdr->partitions[partno].blocks = partblocks;
    418  1.1    soren 	volhdr->partitions[partno].first = partfirst;
    419  1.1    soren 	volhdr->partitions[partno].type = parttype;
    420  1.1    soren 	write_volhdr();
    421  1.1    soren }
    422  1.1    soren 
    423  1.2  thorpej void
    424  1.2  thorpej write_volhdr(void)
    425  1.1    soren {
    426  1.2  thorpej 	int i;
    427  1.2  thorpej 
    428  1.1    soren 	checksum_vol();
    429  1.1    soren 	display_vol();
    430  1.1    soren 	printf("\nDo you want to update volume (y/n)? ");
    431  1.2  thorpej 	i = getchar();
    432  1.2  thorpej 	if (i != 'Y' && i != 'y')
    433  1.1    soren 		exit(1);
    434  1.2  thorpej 	i = lseek(fd, 0 , SEEK_SET);
    435  1.2  thorpej 	if (i < 0) {
    436  1.1    soren 		perror("lseek 0");
    437  1.1    soren 		exit(1);
    438  1.1    soren 	}
    439  1.2  thorpej 	i = write(fd, buf, 512);
    440  1.2  thorpej 	if (i < 0)
    441  1.1    soren 		perror("write volhdr");
    442  1.1    soren }
    443  1.1    soren 
    444  1.2  thorpej int
    445  1.2  thorpej allocate_space(int size)
    446  1.1    soren {
    447  1.1    soren 	int n, blocks;
    448  1.1    soren 	int first;
    449  1.1    soren 
    450  1.1    soren 	blocks = (size + 511) / 512;
    451  1.1    soren 	first = 2;
    452  1.1    soren 	n = 0;
    453  1.1    soren 	while (n < 15) {
    454  1.1    soren 		if (volhdr->voldir[n].name[0]) {
    455  1.1    soren 			if (first < (volhdr->voldir[n].block +
    456  1.1    soren 			    (volhdr->voldir[n].bytes + 511) / 512) &&
    457  1.1    soren 			    (first + blocks) >= volhdr->voldir[n].block) {
    458  1.1    soren 				first = volhdr->voldir[n].block +
    459  1.1    soren 				    (volhdr->voldir[n].bytes + 511) / 512;
    460  1.1    soren #if 0
    461  1.1    soren printf("allocate: n=%d first=%d blocks=%d size=%d\n", n, first, blocks, size);
    462  1.1    soren printf("%s %d %d\n", volhdr->voldir[n].name, volhdr->voldir[n].block, volhdr->voldir[n].bytes);
    463  1.1    soren printf("first=%d block=%d last=%d end=%d\n", first, volhdr->voldir[n].block,
    464  1.1    soren     first + blocks - 1, volhdr->voldir[n].block + (volhdr->voldir[n].bytes + 511)/512);
    465  1.1    soren #endif
    466  1.1    soren 				n = 0;
    467  1.1    soren 				continue;
    468  1.1    soren 			}
    469  1.1    soren 		}
    470  1.1    soren 		++n;
    471  1.1    soren 	}
    472  1.1    soren 	if (first + blocks > lbl.d_secperunit)
    473  1.1    soren 		first = -1;
    474  1.1    soren 	/* XXX assumes volume header is partition 8 */
    475  1.1    soren 	/* XXX assumes volume header starts at 0? */
    476  1.1    soren 	if (first + blocks >= volhdr->partitions[8].blocks)
    477  1.1    soren 		first = -1;
    478  1.1    soren 	return(first);
    479  1.1    soren }
    480  1.1    soren 
    481  1.2  thorpej void
    482  1.2  thorpej checksum_vol(void)
    483  1.1    soren {
    484  1.2  thorpej 	int32_t *l;
    485  1.2  thorpej 	int i;
    486  1.2  thorpej 
    487  1.1    soren 	volhdr->checksum = checksum = 0;
    488  1.2  thorpej 	l = (int32_t *)buf;
    489  1.2  thorpej 	for (i = 0; i < 512 / 4; ++i)
    490  1.2  thorpej 		checksum += l[i];
    491  1.1    soren 	volhdr->checksum = -checksum;
    492  1.1    soren }
    493  1.1    soren 
    494  1.2  thorpej void
    495  1.2  thorpej usage(void)
    496  1.1    soren {
    497  1.4   simonb 	printf("Usage:	sgivol [-i] device\n"
    498  1.4   simonb 	       "	sgivol [-r vhfilename diskfilename] device\n"
    499  1.4   simonb 	       "	sgivol [-w vhfilename diskfilename] device\n"
    500  1.4   simonb 	       "	sgivol [-d vhfilename] device\n"
    501  1.1    soren 	       );
    502  1.1    soren 	exit(0);
    503  1.1    soren }
    504