Home | History | Annotate | Line # | Download | only in sgivol
sgivol.c revision 1.12
      1  1.12  thorpej /*	$NetBSD: sgivol.c,v 1.12 2006/05/05 17:59:41 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.9      jmc #if HAVE_NBTOOL_CONFIG_H
     40   1.9      jmc #include "nbtool_config.h"
     41   1.9      jmc #endif
     42   1.9      jmc 
     43   1.2  thorpej #include <sys/types.h>
     44   1.2  thorpej #include <sys/ioctl.h>
     45   1.8   sekiya #include <sys/stat.h>
     46   1.9      jmc 
     47   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
     48   1.9      jmc #include "../../../../../sys/sys/bootblock.h"
     49   1.9      jmc /* Ficticious geometry for cross tool usage against a file image */
     50   1.9      jmc #define SGIVOL_NBTOOL_NSECS	32
     51   1.9      jmc #define SGIVOL_NBTOOL_NTRACKS	64
     52   1.9      jmc #else
     53   1.2  thorpej #include <sys/disklabel.h>
     54   1.9      jmc #endif
     55   1.2  thorpej 
     56   1.5    rafal #include <errno.h>
     57   1.1    soren #include <stdio.h>
     58   1.2  thorpej #include <stdlib.h>
     59   1.1    soren #include <unistd.h>
     60   1.1    soren #include <string.h>
     61   1.1    soren #include <fcntl.h>
     62   1.1    soren #include <util.h>
     63  1.12  thorpej #ifndef HAVE_NBTOOL_CONFIG_H
     64   1.7   sekiya #include <sys/endian.h>
     65  1.12  thorpej #endif
     66   1.1    soren 
     67   1.1    soren int	fd;
     68   1.1    soren int	opt_i;			/* Initialize volume header */
     69   1.1    soren int	opt_r;			/* Read a file from volume header */
     70   1.1    soren int	opt_w;			/* Write a file to volume header */
     71   1.1    soren int	opt_d;			/* Delete a file from volume header */
     72   1.1    soren int	opt_p;			/* Modify a partition */
     73   1.5    rafal int	opt_q;			/* quiet mode */
     74   1.5    rafal int	opt_f;			/* Don't ask, just do what you're told */
     75   1.1    soren int	partno, partfirst, partblocks, parttype;
     76   1.9      jmc struct sgi_boot_block *volhdr;
     77   1.2  thorpej int32_t	checksum;
     78   1.9      jmc u_int32_t	volhdr_size = SGI_BOOT_BLOCK_SIZE_VOLHDR;
     79   1.2  thorpej 
     80   1.2  thorpej const char *vfilename = "";
     81   1.2  thorpej const char *ufilename = "";
     82   1.1    soren 
     83   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
     84   1.9      jmc struct stat st;
     85   1.9      jmc #else
     86   1.1    soren struct disklabel lbl;
     87   1.9      jmc #endif
     88   1.1    soren 
     89   1.1    soren unsigned char buf[512];
     90   1.1    soren 
     91   1.2  thorpej const char *sgi_types[] = {
     92   1.1    soren 	"Volume Header",
     93   1.1    soren 	"Repl Trks",
     94   1.1    soren 	"Repl Secs",
     95   1.1    soren 	"Raw",
     96   1.1    soren 	"BSD4.2",
     97   1.1    soren 	"SysV",
     98   1.1    soren 	"Volume",
     99   1.1    soren 	"EFS",
    100   1.1    soren 	"LVol",
    101   1.1    soren 	"RLVol",
    102   1.1    soren 	"XFS",
    103   1.1    soren 	"XSFLog",
    104   1.1    soren 	"XLV",
    105   1.1    soren 	"XVM"
    106   1.1    soren };
    107   1.1    soren 
    108   1.2  thorpej int	main(int, char *[]);
    109   1.2  thorpej 
    110   1.2  thorpej void	display_vol(void);
    111   1.2  thorpej void	init_volhdr(void);
    112   1.2  thorpej void	read_file(void);
    113   1.2  thorpej void	write_file(void);
    114   1.2  thorpej void	delete_file(void);
    115   1.2  thorpej void	modify_partition(void);
    116   1.2  thorpej void	write_volhdr(void);
    117   1.2  thorpej int	allocate_space(int);
    118   1.2  thorpej void	checksum_vol(void);
    119   1.2  thorpej void	usage(void);
    120   1.2  thorpej 
    121   1.2  thorpej int
    122   1.2  thorpej main(int argc, char *argv[])
    123   1.1    soren {
    124   1.5    rafal 	int ch;
    125   1.7   sekiya 	while ((ch = getopt(argc, argv, "irwpdqfh:")) != -1) {
    126   1.5    rafal 		switch (ch) {
    127   1.5    rafal 		/* -i, -r, -w, -d and -p override each other */
    128   1.5    rafal 		/* -q implies -f */
    129   1.5    rafal 		case 'q':
    130   1.5    rafal 			++opt_q;
    131   1.5    rafal 			++opt_f;
    132   1.5    rafal 			break;
    133   1.5    rafal 		case 'f':
    134   1.5    rafal 			++opt_f;
    135   1.5    rafal 			break;
    136   1.1    soren 		case 'i':
    137   1.1    soren 			++opt_i;
    138   1.5    rafal 			opt_r = opt_w = opt_d = opt_p = 0;
    139   1.1    soren 			break;
    140   1.7   sekiya 		case 'h':
    141   1.7   sekiya 			volhdr_size = atoi(optarg);
    142   1.7   sekiya 			break;
    143   1.1    soren 		case 'r':
    144   1.5    rafal 			++opt_r;
    145   1.5    rafal 			opt_i = opt_w = opt_d = opt_p = 0;
    146   1.5    rafal 			break;
    147   1.1    soren 		case 'w':
    148   1.5    rafal 			++opt_w;
    149   1.5    rafal 			opt_i = opt_r = opt_d = opt_p = 0;
    150   1.1    soren 			break;
    151   1.1    soren 		case 'd':
    152   1.1    soren 			++opt_d;
    153   1.5    rafal 			opt_i = opt_r = opt_w = opt_p = 0;
    154   1.5    rafal 			break;
    155   1.1    soren 		case 'p':
    156   1.1    soren 			++opt_p;
    157   1.5    rafal 			opt_i = opt_r = opt_w = opt_d = 0;
    158   1.5    rafal 			partno = atoi(argv[0]);
    159   1.5    rafal 			partfirst = atoi(argv[1]);
    160   1.5    rafal 			partblocks = atoi(argv[2]);
    161   1.5    rafal 			parttype = atoi(argv[3]);
    162   1.1    soren 			break;
    163   1.5    rafal 		case '?':
    164   1.1    soren 		default:
    165   1.1    soren 			usage();
    166   1.1    soren 		}
    167   1.1    soren 	}
    168   1.5    rafal 	argc -= optind;
    169   1.5    rafal 	argv += optind;
    170   1.5    rafal 
    171   1.5    rafal 	if (opt_r || opt_w) {
    172   1.5    rafal 		if (argc != 3)
    173   1.5    rafal 			usage();
    174   1.5    rafal 		vfilename = argv[0];
    175   1.5    rafal 		ufilename = argv[1];
    176   1.5    rafal 		argc -= 2;
    177   1.5    rafal 		argv += 2;
    178   1.5    rafal 	}
    179   1.5    rafal 	if (opt_d) {
    180   1.5    rafal 		if (argc != 2)
    181   1.5    rafal 			usage();
    182   1.5    rafal 		vfilename = argv[0];
    183   1.5    rafal 		argc--;
    184   1.5    rafal 		argv++;
    185   1.5    rafal 	}
    186   1.1    soren 
    187   1.5    rafal 	if (opt_p) {
    188   1.5    rafal 		if (argc != 5)
    189   1.5    rafal 			usage();
    190   1.5    rafal 		partno = atoi(argv[0]);
    191   1.5    rafal 		partfirst = atoi(argv[1]);
    192   1.5    rafal 		partblocks = atoi(argv[2]);
    193   1.5    rafal 		parttype = atoi(argv[3]);
    194   1.5    rafal 		argc -= 4;
    195   1.5    rafal 		argv += 4;
    196   1.5    rafal 	}
    197   1.5    rafal 	if (argc != 1)
    198   1.1    soren 		usage();
    199   1.5    rafal 
    200   1.5    rafal 	fd = open(argv[0], (opt_i | opt_w | opt_d | opt_p) ? O_RDWR : O_RDONLY);
    201   1.1    soren 	if (fd < 0) {
    202   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    203   1.9      jmc 		perror("File open");
    204   1.9      jmc 		exit(1);
    205   1.9      jmc #else
    206   1.5    rafal 		sprintf(buf, "/dev/r%s%c", argv[0], 'a' + getrawpartition());
    207   1.5    rafal 		fd = open(buf, (opt_i | opt_w | opt_d | opt_p)
    208   1.5    rafal 				? O_RDWR : O_RDONLY);
    209   1.1    soren 		if (fd < 0) {
    210   1.5    rafal 			printf("Error opening device %s: %s\n",
    211   1.5    rafal 				argv[0], strerror(errno));
    212   1.1    soren 			exit(1);
    213   1.1    soren 		}
    214   1.9      jmc #endif
    215   1.1    soren 	}
    216   1.2  thorpej 	if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
    217   1.1    soren 		perror("read volhdr");
    218   1.1    soren 		exit(1);
    219   1.1    soren 	}
    220   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    221   1.9      jmc 	if (fstat(fd, &st) < 0) {
    222   1.9      jmc 		perror("stat error");
    223   1.9      jmc 		exit(1);
    224   1.9      jmc 	}
    225   1.9      jmc 	if (!S_ISREG(st.st_mode)) {
    226   1.9      jmc 		printf("Must be regular file\n");
    227   1.9      jmc 		exit(1);
    228   1.9      jmc 	}
    229   1.9      jmc 	if (st.st_size % SGI_BOOT_BLOCK_BLOCKSIZE) {
    230   1.9      jmc 		printf("Size must be multiple of %d\n",
    231   1.9      jmc 		    SGI_BOOT_BLOCK_BLOCKSIZE);
    232   1.9      jmc 		exit(1);
    233   1.9      jmc 	}
    234   1.9      jmc 	if (st.st_size < (SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS)) {
    235   1.9      jmc 		printf("Minimum size of %d required\n",
    236   1.9      jmc 		    SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS);
    237   1.9      jmc 		exit(1);
    238   1.9      jmc 	}
    239   1.9      jmc #else
    240  1.10   sekiya 	if (ioctl(fd, DIOCGDINFO, &lbl) < 0) {
    241   1.1    soren 		perror("DIOCGDINFO");
    242   1.1    soren 		exit(1);
    243   1.1    soren 	}
    244   1.9      jmc #endif
    245   1.9      jmc 	volhdr = (struct sgi_boot_block *) buf;
    246   1.1    soren 	if (opt_i) {
    247   1.1    soren 		init_volhdr();
    248   1.1    soren 		exit(0);
    249   1.1    soren 	}
    250   1.9      jmc 	if (be32toh(volhdr->magic) != SGI_BOOT_BLOCK_MAGIC) {
    251   1.5    rafal 		printf("No Volume Header found, magic=%x.  Use -i first.\n",
    252   1.7   sekiya 		       be32toh(volhdr->magic));
    253   1.1    soren 		exit(1);
    254   1.1    soren 	}
    255   1.1    soren 	if (opt_r) {
    256   1.1    soren 		read_file();
    257   1.1    soren 		exit(0);
    258   1.1    soren 	}
    259   1.1    soren 	if (opt_w) {
    260   1.1    soren 		write_file();
    261   1.1    soren 		exit(0);
    262   1.1    soren 	}
    263   1.1    soren 	if (opt_d) {
    264   1.1    soren 		delete_file();
    265   1.1    soren 		exit(0);
    266   1.1    soren 	}
    267   1.1    soren 	if (opt_p) {
    268   1.1    soren 		modify_partition();
    269   1.1    soren 		exit(0);
    270   1.1    soren 	}
    271   1.5    rafal 
    272   1.5    rafal 	if (!opt_q)
    273   1.5    rafal 		display_vol();
    274   1.1    soren 
    275   1.1    soren 	return 0;
    276   1.1    soren }
    277   1.1    soren 
    278   1.2  thorpej void
    279   1.2  thorpej display_vol(void)
    280   1.1    soren {
    281   1.2  thorpej 	int32_t *l;
    282   1.2  thorpej 	int i;
    283   1.2  thorpej 
    284   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    285   1.9      jmc 	printf("disklabel shows %d sectors\n",
    286   1.9      jmc 	    st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE);
    287   1.9      jmc #else
    288   1.1    soren 	printf("disklabel shows %d sectors\n", lbl.d_secperunit);
    289   1.9      jmc #endif
    290   1.2  thorpej 	l = (int32_t *)buf;
    291   1.1    soren 	checksum = 0;
    292   1.2  thorpej 	for (i = 0; i < 512 / 4; ++i)
    293   1.7   sekiya 		checksum += be32toh(l[i]);
    294   1.1    soren 	printf("checksum: %08x%s\n", checksum, checksum == 0 ? "" : " *ERROR*");
    295   1.7   sekiya 	printf("root part: %d\n", be16toh(volhdr->root));
    296   1.7   sekiya 	printf("swap part: %d\n", be16toh(volhdr->swap));
    297   1.2  thorpej 	printf("bootfile: %s\n", volhdr->bootfile);
    298   1.1    soren 	/* volhdr->devparams[0..47] */
    299   1.1    soren 	printf("\nVolume header files:\n");
    300   1.2  thorpej 	for (i = 0; i < 15; ++i)
    301   1.2  thorpej 		if (volhdr->voldir[i].name[0])
    302   1.9      jmc 			printf("%-8s offset %4d blocks, length %8d bytes "
    303   1.9      jmc 			    "(%d blocks)\n",
    304   1.9      jmc 			    volhdr->voldir[i].name,
    305   1.9      jmc                             be32toh(volhdr->voldir[i].block),
    306   1.9      jmc 			    be32toh(volhdr->voldir[i].bytes),
    307   1.9      jmc                             (be32toh(volhdr->voldir[i].bytes) + 511) / 512);
    308   1.1    soren 	printf("\nSGI partitions:\n");
    309   1.9      jmc 	for (i = 0; i < SGI_BOOT_BLOCK_MAXPARTITIONS; ++i) {
    310   1.7   sekiya 		if (be32toh(volhdr->partitions[i].blocks)) {
    311   1.2  thorpej 			printf("%2d:%c blocks %8d first %8d type %2d (%s)\n",
    312   1.7   sekiya 			  i, i + 'a', be32toh(volhdr->partitions[i].blocks),
    313   1.7   sekiya 			       be32toh(volhdr->partitions[i].first),
    314   1.7   sekiya 			       be32toh(volhdr->partitions[i].type),
    315   1.7   sekiya 			  be32toh(volhdr->partitions[i].type) > 13 ? "???" :
    316   1.7   sekiya 			    sgi_types[be32toh(volhdr->partitions[i].type)]);
    317   1.2  thorpej 		}
    318   1.2  thorpej 	}
    319   1.1    soren }
    320   1.1    soren 
    321   1.2  thorpej void
    322   1.2  thorpej init_volhdr(void)
    323   1.1    soren {
    324   1.1    soren 	memset(buf, 0, sizeof(buf));
    325   1.9      jmc 	volhdr->magic = htobe32(SGI_BOOT_BLOCK_MAGIC);
    326   1.7   sekiya 	volhdr->root = htobe16(0);
    327   1.7   sekiya 	volhdr->swap = htobe16(1);
    328   1.2  thorpej 	strcpy(volhdr->bootfile, "/netbsd");
    329   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    330   1.9      jmc 	volhdr->dp.dp_skew = 0;
    331   1.9      jmc #else
    332   1.4   simonb 	volhdr->dp.dp_skew = lbl.d_trackskew;
    333   1.9      jmc #endif
    334   1.4   simonb 	volhdr->dp.dp_gap1 = 1; /* XXX */
    335   1.4   simonb 	volhdr->dp.dp_gap2 = 1; /* XXX */
    336   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    337   1.9      jmc 	volhdr->dp.dp_cyls =
    338   1.9      jmc 	    htobe16(st.st_size / (SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS));
    339   1.9      jmc #else
    340   1.7   sekiya 	volhdr->dp.dp_cyls = htobe16(lbl.d_ncylinders);
    341   1.9      jmc #endif
    342   1.4   simonb 	volhdr->dp.dp_shd0 = 0;
    343   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    344   1.9      jmc 	volhdr->dp.dp_trks0 = htobe16(SGIVOL_NBTOOL_NTRACKS);
    345   1.9      jmc 	volhdr->dp.dp_secs = htobe16(SGIVOL_NBTOOL_NSECS);
    346   1.9      jmc 	volhdr->dp.dp_secbytes = htobe16(SGI_BOOT_BLOCK_BLOCKSIZE);
    347   1.9      jmc 	volhdr->dp.dp_interleave = htobe16(1);
    348   1.9      jmc #else
    349   1.7   sekiya 	volhdr->dp.dp_trks0 = htobe16(lbl.d_ntracks);
    350   1.7   sekiya 	volhdr->dp.dp_secs = htobe16(lbl.d_nsectors);
    351   1.7   sekiya 	volhdr->dp.dp_secbytes = htobe16(lbl.d_secsize);
    352   1.7   sekiya 	volhdr->dp.dp_interleave = htobe16(lbl.d_interleave);
    353   1.9      jmc #endif
    354   1.7   sekiya 	volhdr->dp.dp_nretries = htobe32(22);
    355   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    356   1.9      jmc 	volhdr->partitions[10].blocks =
    357   1.9      jmc 	    htobe32(st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE);
    358   1.9      jmc #else
    359   1.7   sekiya 	volhdr->partitions[10].blocks = htobe32(lbl.d_secperunit);
    360   1.9      jmc #endif
    361   1.1    soren 	volhdr->partitions[10].first = 0;
    362   1.7   sekiya 	volhdr->partitions[10].type = htobe32(SGI_PTYPE_VOLUME);
    363   1.7   sekiya 	volhdr->partitions[8].blocks = htobe32(volhdr_size);
    364   1.1    soren 	volhdr->partitions[8].first = 0;
    365   1.7   sekiya 	volhdr->partitions[8].type = htobe32(SGI_PTYPE_VOLHDR);
    366   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    367   1.9      jmc 	volhdr->partitions[0].blocks =
    368   1.9      jmc 	    htobe32((st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE) - volhdr_size);
    369   1.9      jmc #else
    370   1.7   sekiya 	volhdr->partitions[0].blocks = htobe32(lbl.d_secperunit - volhdr_size);
    371   1.9      jmc #endif
    372   1.7   sekiya 	volhdr->partitions[0].first = htobe32(volhdr_size);
    373   1.7   sekiya 	volhdr->partitions[0].type = htobe32(SGI_PTYPE_BSD);
    374   1.1    soren 	write_volhdr();
    375   1.1    soren }
    376   1.1    soren 
    377   1.2  thorpej void
    378   1.2  thorpej read_file(void)
    379   1.1    soren {
    380   1.1    soren 	FILE *fp;
    381   1.2  thorpej 	int i;
    382   1.1    soren 
    383   1.5    rafal 	if (!opt_q)
    384   1.5    rafal 		printf("Reading file %s\n", vfilename);
    385   1.2  thorpej 	for (i = 0; i < 15; ++i) {
    386   1.3  thorpej 		if (strncmp(vfilename, volhdr->voldir[i].name,
    387   1.7   sekiya 			    strlen(volhdr->voldir[i].name)) == 0)
    388   1.1    soren 			break;
    389   1.1    soren 	}
    390   1.2  thorpej 	if (i >= 15) {
    391   1.1    soren 		printf("file %s not found\n", vfilename);
    392   1.1    soren 		exit(1);
    393   1.1    soren 	}
    394   1.1    soren 	/* XXX assumes volume header starts at 0? */
    395   1.7   sekiya 	lseek(fd, be32toh(volhdr->voldir[i].block) * 512, SEEK_SET);
    396   1.1    soren 	fp = fopen(ufilename, "w");
    397   1.1    soren 	if (fp == NULL) {
    398   1.1    soren 		perror("open write");
    399   1.1    soren 		exit(1);
    400   1.1    soren 	}
    401   1.7   sekiya 	i = be32toh(volhdr->voldir[i].bytes);
    402   1.2  thorpej 	while (i > 0) {
    403   1.1    soren 		if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
    404   1.1    soren 			perror("read file");
    405   1.1    soren 			exit(1);
    406   1.1    soren 		}
    407   1.2  thorpej 		fwrite(buf, 1, i > sizeof(buf) ? sizeof(buf) : i, fp);
    408   1.2  thorpej 		i -= i > sizeof(buf) ? sizeof(buf) : i;
    409   1.1    soren 	}
    410   1.1    soren 	fclose(fp);
    411   1.1    soren }
    412   1.1    soren 
    413   1.2  thorpej void
    414   1.2  thorpej write_file(void)
    415   1.1    soren {
    416   1.1    soren 	FILE *fp;
    417   1.2  thorpej 	int slot;
    418   1.2  thorpej 	size_t namelen;
    419   1.2  thorpej 	int block, i;
    420   1.1    soren 	struct stat st;
    421   1.1    soren 	char fbuf[512];
    422   1.1    soren 
    423   1.5    rafal 	if (!opt_q)
    424   1.5    rafal 		printf("Writing file %s\n", ufilename);
    425   1.1    soren 	if (stat(ufilename, &st) < 0) {
    426   1.1    soren 		perror("stat");
    427   1.1    soren 		exit(1);
    428   1.1    soren 	}
    429   1.5    rafal 	if (!opt_q)
    430   1.5    rafal 		printf("File %s has %lld bytes\n", ufilename, st.st_size);
    431   1.2  thorpej 	slot = -1;
    432   1.2  thorpej 	for (i = 0; i < 15; ++i) {
    433   1.2  thorpej 		if (volhdr->voldir[i].name[0] == '\0' && slot < 0)
    434   1.2  thorpej 			slot = i;
    435   1.2  thorpej 		if (strcmp(vfilename, volhdr->voldir[i].name) == 0) {
    436   1.2  thorpej 			slot = i;
    437   1.1    soren 			break;
    438   1.1    soren 		}
    439   1.1    soren 	}
    440   1.2  thorpej 	if (slot == -1) {
    441   1.2  thorpej 		printf("No directory space for file %s\n", vfilename);
    442   1.1    soren 		exit(1);
    443   1.1    soren 	}
    444   1.1    soren 	/* -w can overwrite, -a won't overwrite */
    445   1.7   sekiya 	if (be32toh(volhdr->voldir[slot].block) > 0) {
    446   1.5    rafal 		if (!opt_q)
    447   1.5    rafal 			printf("File %s exists, removing old file\n",
    448   1.5    rafal 				vfilename);
    449   1.2  thorpej 		volhdr->voldir[slot].name[0] = 0;
    450   1.2  thorpej 		volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0;
    451   1.1    soren 	}
    452   1.1    soren 	if (st.st_size == 0) {
    453   1.5    rafal 		printf("bad file size\n");
    454   1.1    soren 		exit(1);
    455   1.1    soren 	}
    456   1.1    soren 	/* XXX assumes volume header starts at 0? */
    457   1.1    soren 	block = allocate_space((int)st.st_size);
    458   1.1    soren 	if (block < 0) {
    459   1.1    soren 		printf("No space for file\n");
    460   1.1    soren 		exit(1);
    461   1.1    soren 	}
    462   1.1    soren 
    463   1.2  thorpej 	/*
    464   1.2  thorpej 	 * Make sure the name in the volume header is max. 8 chars,
    465   1.2  thorpej 	 * NOT including NUL.
    466   1.2  thorpej 	 */
    467   1.2  thorpej 	namelen = strlen(vfilename);
    468   1.2  thorpej 	if (namelen > sizeof(volhdr->voldir[slot].name)) {
    469   1.1    soren 		printf("Warning: '%s' is too long for volume header, ",
    470   1.1    soren 		       vfilename);
    471   1.2  thorpej 		namelen = sizeof(volhdr->voldir[slot].name);
    472   1.2  thorpej 		printf("truncating to '%-8s'\n", vfilename);
    473   1.1    soren 	}
    474   1.4   simonb 
    475   1.2  thorpej 	/* Populate it w/ NULs */
    476   1.2  thorpej 	memset(volhdr->voldir[slot].name, 0,
    477   1.2  thorpej 	    sizeof(volhdr->voldir[slot].name));
    478   1.2  thorpej 	/* Then copy the name */
    479   1.2  thorpej 	memcpy(volhdr->voldir[slot].name, vfilename, namelen);
    480   1.2  thorpej 
    481   1.7   sekiya 	volhdr->voldir[slot].block = htobe32(block);
    482   1.7   sekiya 	volhdr->voldir[slot].bytes = htobe32(st.st_size);
    483   1.1    soren 
    484   1.1    soren 	write_volhdr();
    485   1.1    soren 
    486   1.1    soren 	/* write the file itself */
    487   1.2  thorpej 	i = lseek(fd, block * 512, SEEK_SET);
    488   1.2  thorpej 	if (i < 0) {
    489   1.1    soren 		perror("lseek write");
    490   1.1    soren 		exit(1);
    491   1.1    soren 	}
    492   1.2  thorpej 	i = st.st_size;
    493   1.1    soren 	fp = fopen(ufilename, "r");
    494   1.2  thorpej 	while (i > 0) {
    495   1.2  thorpej 		fread(fbuf, 1, i > 512 ? 512 : i, fp);
    496   1.1    soren 		if (write(fd, fbuf, 512) != 512) {
    497   1.1    soren 			perror("write file");
    498   1.1    soren 			exit(1);
    499   1.1    soren 		}
    500   1.2  thorpej 		i -= i > 512 ? 512 : i;
    501   1.1    soren 	}
    502   1.1    soren }
    503   1.1    soren 
    504   1.2  thorpej void
    505   1.2  thorpej delete_file(void)
    506   1.1    soren {
    507   1.2  thorpej 	int i;
    508   1.2  thorpej 
    509   1.2  thorpej 	for (i = 0; i < 15; ++i) {
    510   1.6   sekiya 		if (strcmp(vfilename, volhdr->voldir[i].name) == 0) {
    511   1.1    soren 			break;
    512   1.1    soren 		}
    513   1.1    soren 	}
    514   1.2  thorpej 	if (i >= 15) {
    515   1.1    soren 		printf("File %s not found\n", vfilename);
    516   1.1    soren 		exit(1);
    517   1.1    soren 	}
    518   1.5    rafal 
    519   1.5    rafal 	/* XXX: we don't compact the file space, so get fragmentation */
    520   1.2  thorpej 	volhdr->voldir[i].name[0] = '\0';
    521   1.2  thorpej 	volhdr->voldir[i].block = volhdr->voldir[i].bytes = 0;
    522   1.1    soren 	write_volhdr();
    523   1.1    soren }
    524   1.1    soren 
    525   1.2  thorpej void
    526   1.2  thorpej modify_partition(void)
    527   1.1    soren {
    528   1.5    rafal 	if (!opt_q)
    529   1.5    rafal 		printf("Modify partition %d start %d length %d\n",
    530   1.5    rafal 			partno, partfirst, partblocks);
    531   1.1    soren 	if (partno < 0 || partno > 15) {
    532   1.5    rafal 		printf("Invalid partition number: %d\n", partno);
    533   1.1    soren 		exit(1);
    534   1.1    soren 	}
    535   1.7   sekiya 	volhdr->partitions[partno].blocks = htobe32(partblocks);
    536   1.7   sekiya 	volhdr->partitions[partno].first = htobe32(partfirst);
    537   1.7   sekiya 	volhdr->partitions[partno].type = htobe32(parttype);
    538   1.1    soren 	write_volhdr();
    539   1.1    soren }
    540   1.1    soren 
    541   1.2  thorpej void
    542   1.2  thorpej write_volhdr(void)
    543   1.1    soren {
    544   1.2  thorpej 	int i;
    545   1.2  thorpej 
    546   1.1    soren 	checksum_vol();
    547   1.5    rafal 
    548   1.5    rafal 	if (!opt_q)
    549   1.5    rafal 		display_vol();
    550   1.5    rafal 	if (!opt_f) {
    551   1.5    rafal 		printf("\nDo you want to update volume (y/n)? ");
    552   1.5    rafal 		i = getchar();
    553   1.5    rafal 		if (i != 'Y' && i != 'y')
    554   1.5    rafal 			exit(1);
    555   1.5    rafal 	}
    556   1.7   sekiya 	i = lseek(fd, 0, SEEK_SET);
    557   1.2  thorpej 	if (i < 0) {
    558   1.1    soren 		perror("lseek 0");
    559   1.1    soren 		exit(1);
    560   1.1    soren 	}
    561   1.2  thorpej 	i = write(fd, buf, 512);
    562   1.2  thorpej 	if (i < 0)
    563   1.1    soren 		perror("write volhdr");
    564   1.1    soren }
    565   1.1    soren 
    566   1.2  thorpej int
    567   1.2  thorpej allocate_space(int size)
    568   1.1    soren {
    569   1.1    soren 	int n, blocks;
    570   1.1    soren 	int first;
    571   1.1    soren 
    572   1.1    soren 	blocks = (size + 511) / 512;
    573   1.1    soren 	first = 2;
    574   1.1    soren 	n = 0;
    575   1.1    soren 	while (n < 15) {
    576   1.1    soren 		if (volhdr->voldir[n].name[0]) {
    577   1.7   sekiya 			if (first < (be32toh(volhdr->voldir[n].block) +
    578   1.7   sekiya 			  (be32toh(volhdr->voldir[n].bytes) + 511) / 512) &&
    579   1.7   sekiya 			    (first + blocks) > be32toh(volhdr->voldir[n].block)) {
    580   1.7   sekiya 				first = be32toh(volhdr->voldir[n].block) +
    581   1.7   sekiya 					(be32toh(volhdr->voldir[n].bytes) + 511) / 512;
    582   1.1    soren #if 0
    583   1.7   sekiya 				printf("allocate: n=%d first=%d blocks=%d size=%d\n", n, first, blocks, size);
    584   1.7   sekiya 				printf("%s %d %d\n", volhdr->voldir[n].name, volhdr->voldir[n].block, volhdr->voldir[n].bytes);
    585   1.7   sekiya 				printf("first=%d block=%d last=%d end=%d\n", first, volhdr->voldir[n].block,
    586   1.7   sekiya 				       first + blocks - 1, volhdr->voldir[n].block + (volhdr->voldir[n].bytes + 511) / 512);
    587   1.1    soren #endif
    588   1.1    soren 				n = 0;
    589   1.1    soren 				continue;
    590   1.1    soren 			}
    591   1.1    soren 		}
    592   1.1    soren 		++n;
    593   1.1    soren 	}
    594   1.9      jmc #if HAVE_NBTOOL_CONFIG_H
    595   1.9      jmc 	if (first + blocks > (st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE))
    596   1.9      jmc #else
    597   1.1    soren 	if (first + blocks > lbl.d_secperunit)
    598   1.9      jmc #endif
    599   1.1    soren 		first = -1;
    600   1.1    soren 	/* XXX assumes volume header is partition 8 */
    601   1.1    soren 	/* XXX assumes volume header starts at 0? */
    602   1.7   sekiya 	if (first + blocks >= be32toh(volhdr->partitions[8].blocks))
    603   1.1    soren 		first = -1;
    604   1.7   sekiya 	return (first);
    605   1.1    soren }
    606   1.1    soren 
    607   1.2  thorpej void
    608   1.2  thorpej checksum_vol(void)
    609   1.1    soren {
    610   1.2  thorpej 	int32_t *l;
    611   1.2  thorpej 	int i;
    612   1.2  thorpej 
    613   1.1    soren 	volhdr->checksum = checksum = 0;
    614   1.2  thorpej 	l = (int32_t *)buf;
    615   1.2  thorpej 	for (i = 0; i < 512 / 4; ++i)
    616   1.7   sekiya 		checksum += be32toh(l[i]);
    617   1.7   sekiya 	volhdr->checksum = htobe32(-checksum);
    618   1.1    soren }
    619   1.1    soren 
    620   1.2  thorpej void
    621   1.2  thorpej usage(void)
    622   1.1    soren {
    623   1.7   sekiya 	printf("Usage:	sgivol [-qf] [-i] [-h vhsize] device\n"
    624   1.5    rafal 	       "	sgivol [-qf] [-r vhfilename diskfilename] device\n"
    625   1.5    rafal 	       "	sgivol [-qf] [-w vhfilename diskfilename] device\n"
    626   1.5    rafal 	       "	sgivol [-qf] [-d vhfilename] device\n"
    627   1.1    soren 	       );
    628   1.1    soren 	exit(0);
    629   1.1    soren }
    630