Home | History | Annotate | Line # | Download | only in sgivol
sgivol.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: sgivol.c,v 1.2 2001/11/20 23:07:17 thorpej 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.1    soren         volhdr->dp.dp_skew = lbl.d_trackskew;
    246  1.1    soren         volhdr->dp.dp_gap1 = 1; /* XXX */
    247  1.1    soren         volhdr->dp.dp_gap2 = 1; /* XXX */
    248  1.1    soren         volhdr->dp.dp_cyls = lbl.d_ncylinders;
    249  1.1    soren         volhdr->dp.dp_shd0 = 0;
    250  1.1    soren         volhdr->dp.dp_trks0 = lbl.d_ntracks;
    251  1.1    soren         volhdr->dp.dp_secs = lbl.d_nsectors;
    252  1.1    soren         volhdr->dp.dp_secbytes = lbl.d_secsize;
    253  1.1    soren         volhdr->dp.dp_interleave = lbl.d_interleave;
    254  1.1    soren         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.2  thorpej 		if (strcmp(vfilename, volhdr->voldir[i].name) == NULL)
    276  1.1    soren 			break;
    277  1.1    soren 	}
    278  1.2  thorpej 	if (i >= 15) {
    279  1.1    soren 		printf("file %s not found\n", vfilename);
    280  1.1    soren 		exit(1);
    281  1.1    soren 	}
    282  1.1    soren 	/* XXX assumes volume header starts at 0? */
    283  1.2  thorpej 	lseek(fd, volhdr->voldir[i].block * 512, SEEK_SET);
    284  1.1    soren 	fp = fopen(ufilename, "w");
    285  1.1    soren 	if (fp == NULL) {
    286  1.1    soren 		perror("open write");
    287  1.1    soren 		exit(1);
    288  1.1    soren 	}
    289  1.2  thorpej 	i = volhdr->voldir[i].bytes;
    290  1.2  thorpej 	while (i > 0) {
    291  1.1    soren 		if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
    292  1.1    soren 			perror("read file");
    293  1.1    soren 			exit(1);
    294  1.1    soren 		}
    295  1.2  thorpej 		fwrite(buf, 1, i > sizeof(buf) ? sizeof(buf) : i, fp);
    296  1.2  thorpej 		i -= i > sizeof(buf) ? sizeof(buf) : i;
    297  1.1    soren 	}
    298  1.1    soren 	fclose(fp);
    299  1.1    soren }
    300  1.1    soren 
    301  1.2  thorpej void
    302  1.2  thorpej write_file(void)
    303  1.1    soren {
    304  1.1    soren 	FILE *fp;
    305  1.2  thorpej 	int slot;
    306  1.2  thorpej 	size_t namelen;
    307  1.2  thorpej 	int block, i;
    308  1.1    soren 	struct stat st;
    309  1.1    soren 	char fbuf[512];
    310  1.1    soren 
    311  1.1    soren 	printf("Writing file %s\n", ufilename);
    312  1.1    soren 	if (stat(ufilename, &st) < 0) {
    313  1.1    soren 		perror("stat");
    314  1.1    soren 		exit(1);
    315  1.1    soren 	}
    316  1.1    soren 	printf("File %s has %lld bytes\n", ufilename, st.st_size);
    317  1.2  thorpej 	slot = -1;
    318  1.2  thorpej 	for (i = 0; i < 15; ++i) {
    319  1.2  thorpej 		if (volhdr->voldir[i].name[0] == '\0' && slot < 0)
    320  1.2  thorpej 			slot = i;
    321  1.2  thorpej 		if (strcmp(vfilename, volhdr->voldir[i].name) == 0) {
    322  1.2  thorpej 			slot = i;
    323  1.1    soren 			break;
    324  1.1    soren 		}
    325  1.1    soren 	}
    326  1.2  thorpej 	if (slot == -1) {
    327  1.2  thorpej 		printf("No directory space for file %s\n", vfilename);
    328  1.1    soren 		exit(1);
    329  1.1    soren 	}
    330  1.1    soren 	/* -w can overwrite, -a won't overwrite */
    331  1.2  thorpej 	if (volhdr->voldir[slot].block > 0) {
    332  1.1    soren 		printf("File %s exists, removing old file\n", vfilename);
    333  1.2  thorpej 		volhdr->voldir[slot].name[0] = 0;
    334  1.2  thorpej 		volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0;
    335  1.1    soren 	}
    336  1.1    soren 	if (st.st_size == 0) {
    337  1.1    soren 		printf("bad file size?\n");
    338  1.1    soren 		exit(1);
    339  1.1    soren 	}
    340  1.1    soren 	/* XXX assumes volume header starts at 0? */
    341  1.1    soren 	block = allocate_space((int)st.st_size);
    342  1.1    soren 	if (block < 0) {
    343  1.1    soren 		printf("No space for file\n");
    344  1.1    soren 		exit(1);
    345  1.1    soren 	}
    346  1.1    soren 
    347  1.2  thorpej 	/*
    348  1.2  thorpej 	 * Make sure the name in the volume header is max. 8 chars,
    349  1.2  thorpej 	 * NOT including NUL.
    350  1.2  thorpej 	 */
    351  1.2  thorpej 	namelen = strlen(vfilename);
    352  1.2  thorpej 	if (namelen > sizeof(volhdr->voldir[slot].name)) {
    353  1.1    soren 		printf("Warning: '%s' is too long for volume header, ",
    354  1.1    soren 		       vfilename);
    355  1.2  thorpej 		namelen = sizeof(volhdr->voldir[slot].name);
    356  1.2  thorpej 		printf("truncating to '%-8s'\n", vfilename);
    357  1.1    soren 	}
    358  1.1    soren 
    359  1.2  thorpej 	/* Populate it w/ NULs */
    360  1.2  thorpej 	memset(volhdr->voldir[slot].name, 0,
    361  1.2  thorpej 	    sizeof(volhdr->voldir[slot].name));
    362  1.2  thorpej 	/* Then copy the name */
    363  1.2  thorpej 	memcpy(volhdr->voldir[slot].name, vfilename, namelen);
    364  1.2  thorpej 
    365  1.2  thorpej 	volhdr->voldir[slot].block = block;
    366  1.2  thorpej 	volhdr->voldir[slot].bytes = st.st_size;
    367  1.1    soren 
    368  1.1    soren 	write_volhdr();
    369  1.1    soren 
    370  1.1    soren 	/* write the file itself */
    371  1.2  thorpej 	i = lseek(fd, block * 512, SEEK_SET);
    372  1.2  thorpej 	if (i < 0) {
    373  1.1    soren 		perror("lseek write");
    374  1.1    soren 		exit(1);
    375  1.1    soren 	}
    376  1.2  thorpej 	i = st.st_size;
    377  1.1    soren 	fp = fopen(ufilename, "r");
    378  1.2  thorpej 	while (i > 0) {
    379  1.2  thorpej 		fread(fbuf, 1, i > 512 ? 512 : i, fp);
    380  1.1    soren 		if (write(fd, fbuf, 512) != 512) {
    381  1.1    soren 			perror("write file");
    382  1.1    soren 			exit(1);
    383  1.1    soren 		}
    384  1.2  thorpej 		i -= i > 512 ? 512 : i;
    385  1.1    soren 	}
    386  1.1    soren }
    387  1.1    soren 
    388  1.2  thorpej void
    389  1.2  thorpej delete_file(void)
    390  1.1    soren {
    391  1.2  thorpej 	int i;
    392  1.2  thorpej 
    393  1.2  thorpej 	for (i = 0; i < 15; ++i) {
    394  1.2  thorpej 		if (strcmp(vfilename, volhdr->voldir[i].name) == NULL) {
    395  1.1    soren 			break;
    396  1.1    soren 		}
    397  1.1    soren 	}
    398  1.2  thorpej 	if (i >= 15) {
    399  1.1    soren 		printf("File %s not found\n", vfilename);
    400  1.1    soren 		exit(1);
    401  1.1    soren 	}
    402  1.2  thorpej 	volhdr->voldir[i].name[0] = '\0';
    403  1.2  thorpej 	volhdr->voldir[i].block = volhdr->voldir[i].bytes = 0;
    404  1.1    soren 	write_volhdr();
    405  1.1    soren }
    406  1.1    soren 
    407  1.2  thorpej void
    408  1.2  thorpej modify_partition(void)
    409  1.1    soren {
    410  1.2  thorpej 	printf("Modify partition %d start %d length %d\n", partno, partfirst,
    411  1.2  thorpej 	    partblocks);
    412  1.1    soren 	if (partno < 0 || partno > 15) {
    413  1.1    soren 		printf("Invalue partition number: %d\n", partno);
    414  1.1    soren 		exit(1);
    415  1.1    soren 	}
    416  1.1    soren 	volhdr->partitions[partno].blocks = partblocks;
    417  1.1    soren 	volhdr->partitions[partno].first = partfirst;
    418  1.1    soren 	volhdr->partitions[partno].type = parttype;
    419  1.1    soren 	write_volhdr();
    420  1.1    soren }
    421  1.1    soren 
    422  1.2  thorpej void
    423  1.2  thorpej write_volhdr(void)
    424  1.1    soren {
    425  1.2  thorpej 	int i;
    426  1.2  thorpej 
    427  1.1    soren 	checksum_vol();
    428  1.1    soren 	display_vol();
    429  1.1    soren 	printf("\nDo you want to update volume (y/n)? ");
    430  1.2  thorpej 	i = getchar();
    431  1.2  thorpej 	if (i != 'Y' && i != 'y')
    432  1.1    soren 		exit(1);
    433  1.2  thorpej 	i = lseek(fd, 0 , SEEK_SET);
    434  1.2  thorpej 	if (i < 0) {
    435  1.1    soren 		perror("lseek 0");
    436  1.1    soren 		exit(1);
    437  1.1    soren 	}
    438  1.2  thorpej 	i = write(fd, buf, 512);
    439  1.2  thorpej 	if (i < 0)
    440  1.1    soren 		perror("write volhdr");
    441  1.1    soren }
    442  1.1    soren 
    443  1.2  thorpej int
    444  1.2  thorpej allocate_space(int size)
    445  1.1    soren {
    446  1.1    soren 	int n, blocks;
    447  1.1    soren 	int first;
    448  1.1    soren 
    449  1.1    soren 	blocks = (size + 511) / 512;
    450  1.1    soren 	first = 2;
    451  1.1    soren 	n = 0;
    452  1.1    soren 	while (n < 15) {
    453  1.1    soren 		if (volhdr->voldir[n].name[0]) {
    454  1.1    soren 			if (first < (volhdr->voldir[n].block +
    455  1.1    soren 			    (volhdr->voldir[n].bytes + 511) / 512) &&
    456  1.1    soren 			    (first + blocks) >= volhdr->voldir[n].block) {
    457  1.1    soren 				first = volhdr->voldir[n].block +
    458  1.1    soren 				    (volhdr->voldir[n].bytes + 511) / 512;
    459  1.1    soren #if 0
    460  1.1    soren printf("allocate: n=%d first=%d blocks=%d size=%d\n", n, first, blocks, size);
    461  1.1    soren printf("%s %d %d\n", volhdr->voldir[n].name, volhdr->voldir[n].block, volhdr->voldir[n].bytes);
    462  1.1    soren printf("first=%d block=%d last=%d end=%d\n", first, volhdr->voldir[n].block,
    463  1.1    soren     first + blocks - 1, volhdr->voldir[n].block + (volhdr->voldir[n].bytes + 511)/512);
    464  1.1    soren #endif
    465  1.1    soren 				n = 0;
    466  1.1    soren 				continue;
    467  1.1    soren 			}
    468  1.1    soren 		}
    469  1.1    soren 		++n;
    470  1.1    soren 	}
    471  1.1    soren 	if (first + blocks > lbl.d_secperunit)
    472  1.1    soren 		first = -1;
    473  1.1    soren 	/* XXX assumes volume header is partition 8 */
    474  1.1    soren 	/* XXX assumes volume header starts at 0? */
    475  1.1    soren 	if (first + blocks >= volhdr->partitions[8].blocks)
    476  1.1    soren 		first = -1;
    477  1.1    soren 	return(first);
    478  1.1    soren }
    479  1.1    soren 
    480  1.2  thorpej void
    481  1.2  thorpej checksum_vol(void)
    482  1.1    soren {
    483  1.2  thorpej 	int32_t *l;
    484  1.2  thorpej 	int i;
    485  1.2  thorpej 
    486  1.1    soren 	volhdr->checksum = checksum = 0;
    487  1.2  thorpej 	l = (int32_t *)buf;
    488  1.2  thorpej 	for (i = 0; i < 512 / 4; ++i)
    489  1.2  thorpej 		checksum += l[i];
    490  1.1    soren 	volhdr->checksum = -checksum;
    491  1.1    soren }
    492  1.1    soren 
    493  1.2  thorpej void
    494  1.2  thorpej usage(void)
    495  1.1    soren {
    496  1.1    soren 	printf("Usage:  sgivol [-i] device\n"
    497  1.1    soren 	       "        sgivol [-r vhfilename diskfilename] device\n"
    498  1.1    soren 	       "        sgivol [-w vhfilename diskfilename] device\n"
    499  1.1    soren 	       "        sgivol [-d vhfilename] device\n"
    500  1.1    soren 	       );
    501  1.1    soren 	exit(0);
    502  1.1    soren }
    503