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