Home | History | Annotate | Line # | Download | only in sgivol
sgivol.c revision 1.19
      1  1.19  christos /*	$NetBSD: sgivol.c,v 1.19 2013/06/27 21:24:08 christos 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.18    rumble int	names_match(int, const char *);
    115   1.2   thorpej void	usage(void);
    116   1.2   thorpej 
    117   1.2   thorpej int
    118   1.2   thorpej main(int argc, char *argv[])
    119   1.1     soren {
    120  1.17    rumble #define RESET_OPTS()	opt_i = opt_m = opt_r = opt_w = opt_d = opt_p = 0
    121  1.17    rumble 
    122   1.5     rafal 	int ch;
    123  1.17    rumble 	while ((ch = getopt(argc, argv, "qfih:rwdmp?")) != -1) {
    124   1.5     rafal 		switch (ch) {
    125  1.17    rumble 		/* -i, -r, -w, -d, -m and -p override each other */
    126   1.5     rafal 		/* -q implies -f */
    127   1.5     rafal 		case 'q':
    128   1.5     rafal 			++opt_q;
    129   1.5     rafal 			++opt_f;
    130   1.5     rafal 			break;
    131   1.5     rafal 		case 'f':
    132   1.5     rafal 			++opt_f;
    133   1.5     rafal 			break;
    134   1.1     soren 		case 'i':
    135  1.17    rumble 			RESET_OPTS();
    136   1.1     soren 			++opt_i;
    137   1.1     soren 			break;
    138   1.7    sekiya 		case 'h':
    139   1.7    sekiya 			volhdr_size = atoi(optarg);
    140   1.7    sekiya 			break;
    141   1.1     soren 		case 'r':
    142  1.17    rumble 			RESET_OPTS();
    143   1.5     rafal 			++opt_r;
    144   1.5     rafal 			break;
    145   1.1     soren 		case 'w':
    146  1.17    rumble 			RESET_OPTS();
    147   1.5     rafal 			++opt_w;
    148   1.1     soren 			break;
    149   1.1     soren 		case 'd':
    150  1.17    rumble 			RESET_OPTS();
    151   1.1     soren 			++opt_d;
    152  1.17    rumble 			break;
    153  1.17    rumble 		case 'm':
    154  1.17    rumble 			RESET_OPTS();
    155  1.17    rumble 			++opt_m;
    156   1.5     rafal 			break;
    157   1.1     soren 		case 'p':
    158  1.17    rumble 			RESET_OPTS();
    159   1.1     soren 			++opt_p;
    160   1.5     rafal 			partno = atoi(argv[0]);
    161   1.5     rafal 			partfirst = atoi(argv[1]);
    162   1.5     rafal 			partblocks = atoi(argv[2]);
    163   1.5     rafal 			parttype = atoi(argv[3]);
    164   1.1     soren 			break;
    165   1.5     rafal 		case '?':
    166   1.1     soren 		default:
    167   1.1     soren 			usage();
    168   1.1     soren 		}
    169   1.1     soren 	}
    170   1.5     rafal 	argc -= optind;
    171   1.5     rafal 	argv += optind;
    172   1.5     rafal 
    173  1.17    rumble 	if (opt_m || opt_r || opt_w) {
    174   1.5     rafal 		if (argc != 3)
    175   1.5     rafal 			usage();
    176   1.5     rafal 		vfilename = argv[0];
    177   1.5     rafal 		ufilename = argv[1];
    178   1.5     rafal 		argc -= 2;
    179   1.5     rafal 		argv += 2;
    180   1.5     rafal 	}
    181   1.5     rafal 	if (opt_d) {
    182   1.5     rafal 		if (argc != 2)
    183   1.5     rafal 			usage();
    184   1.5     rafal 		vfilename = argv[0];
    185   1.5     rafal 		argc--;
    186   1.5     rafal 		argv++;
    187   1.5     rafal 	}
    188   1.1     soren 
    189   1.5     rafal 	if (opt_p) {
    190   1.5     rafal 		if (argc != 5)
    191   1.5     rafal 			usage();
    192   1.5     rafal 		partno = atoi(argv[0]);
    193   1.5     rafal 		partfirst = atoi(argv[1]);
    194   1.5     rafal 		partblocks = atoi(argv[2]);
    195   1.5     rafal 		parttype = atoi(argv[3]);
    196   1.5     rafal 		argc -= 4;
    197   1.5     rafal 		argv += 4;
    198   1.5     rafal 	}
    199   1.5     rafal 	if (argc != 1)
    200   1.1     soren 		usage();
    201   1.5     rafal 
    202  1.17    rumble 	fd = open(argv[0],
    203  1.17    rumble 	    (opt_i | opt_m | opt_w | opt_d | opt_p) ? O_RDWR : O_RDONLY);
    204   1.1     soren 	if (fd < 0) {
    205   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    206   1.9       jmc 		perror("File open");
    207   1.9       jmc 		exit(1);
    208   1.9       jmc #else
    209  1.13       mrg 		sprintf((char *)buf, "/dev/r%s%c", argv[0], 'a' + getrawpartition());
    210  1.13       mrg 		fd = open((char *)buf, (opt_i | opt_w | opt_d | opt_p)
    211   1.5     rafal 				? O_RDWR : O_RDONLY);
    212   1.1     soren 		if (fd < 0) {
    213   1.5     rafal 			printf("Error opening device %s: %s\n",
    214   1.5     rafal 				argv[0], strerror(errno));
    215   1.1     soren 			exit(1);
    216   1.1     soren 		}
    217   1.9       jmc #endif
    218   1.1     soren 	}
    219   1.2   thorpej 	if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
    220   1.1     soren 		perror("read volhdr");
    221   1.1     soren 		exit(1);
    222   1.1     soren 	}
    223   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    224   1.9       jmc 	if (fstat(fd, &st) < 0) {
    225   1.9       jmc 		perror("stat error");
    226   1.9       jmc 		exit(1);
    227   1.9       jmc 	}
    228   1.9       jmc 	if (!S_ISREG(st.st_mode)) {
    229   1.9       jmc 		printf("Must be regular file\n");
    230   1.9       jmc 		exit(1);
    231   1.9       jmc 	}
    232   1.9       jmc 	if (st.st_size % SGI_BOOT_BLOCK_BLOCKSIZE) {
    233   1.9       jmc 		printf("Size must be multiple of %d\n",
    234   1.9       jmc 		    SGI_BOOT_BLOCK_BLOCKSIZE);
    235   1.9       jmc 		exit(1);
    236   1.9       jmc 	}
    237   1.9       jmc 	if (st.st_size < (SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS)) {
    238   1.9       jmc 		printf("Minimum size of %d required\n",
    239   1.9       jmc 		    SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS);
    240   1.9       jmc 		exit(1);
    241   1.9       jmc 	}
    242   1.9       jmc #else
    243  1.10    sekiya 	if (ioctl(fd, DIOCGDINFO, &lbl) < 0) {
    244   1.1     soren 		perror("DIOCGDINFO");
    245   1.1     soren 		exit(1);
    246   1.1     soren 	}
    247   1.9       jmc #endif
    248   1.9       jmc 	volhdr = (struct sgi_boot_block *) buf;
    249   1.1     soren 	if (opt_i) {
    250   1.1     soren 		init_volhdr();
    251   1.1     soren 		exit(0);
    252   1.1     soren 	}
    253   1.9       jmc 	if (be32toh(volhdr->magic) != SGI_BOOT_BLOCK_MAGIC) {
    254   1.5     rafal 		printf("No Volume Header found, magic=%x.  Use -i first.\n",
    255   1.7    sekiya 		       be32toh(volhdr->magic));
    256   1.1     soren 		exit(1);
    257   1.1     soren 	}
    258   1.1     soren 	if (opt_r) {
    259   1.1     soren 		read_file();
    260   1.1     soren 		exit(0);
    261   1.1     soren 	}
    262   1.1     soren 	if (opt_w) {
    263   1.1     soren 		write_file();
    264   1.1     soren 		exit(0);
    265   1.1     soren 	}
    266   1.1     soren 	if (opt_d) {
    267   1.1     soren 		delete_file();
    268   1.1     soren 		exit(0);
    269   1.1     soren 	}
    270  1.17    rumble 	if (opt_m) {
    271  1.17    rumble 		move_file();
    272  1.17    rumble 		exit(0);
    273  1.17    rumble 	}
    274   1.1     soren 	if (opt_p) {
    275   1.1     soren 		modify_partition();
    276   1.1     soren 		exit(0);
    277   1.1     soren 	}
    278   1.5     rafal 
    279   1.5     rafal 	if (!opt_q)
    280   1.5     rafal 		display_vol();
    281   1.1     soren 
    282   1.1     soren 	return 0;
    283   1.1     soren }
    284   1.1     soren 
    285  1.18    rumble /*
    286  1.18    rumble  * Compare the name in `slot' of voldir to `b'. Be careful, as the
    287  1.18    rumble  * name in voldir need not be nul-terminated and `b' may be longer
    288  1.18    rumble  * than the maximum (in which case it will never match).
    289  1.18    rumble  *
    290  1.18    rumble  * Returns non-0 if names are equal.
    291  1.18    rumble  */
    292  1.18    rumble int
    293  1.18    rumble names_match(int slot, const char *b)
    294  1.18    rumble {
    295  1.18    rumble 	int cmp;
    296  1.18    rumble 
    297  1.18    rumble 	if (slot < 0 || slot >= SGI_BOOT_BLOCK_MAXVOLDIRS) {
    298  1.18    rumble 		printf("Internal error: bad slot in %s()\n", __func__);
    299  1.18    rumble 		exit(1);
    300  1.18    rumble 	}
    301  1.18    rumble 
    302  1.18    rumble 	cmp = strncmp(volhdr->voldir[slot].name, b,
    303  1.18    rumble 	    sizeof(volhdr->voldir[slot].name));
    304  1.18    rumble 
    305  1.18    rumble 	return (cmp == 0 && strlen(b) <= sizeof(volhdr->voldir[slot].name));
    306  1.18    rumble }
    307  1.18    rumble 
    308   1.2   thorpej void
    309   1.2   thorpej display_vol(void)
    310   1.1     soren {
    311   1.2   thorpej 	int32_t *l;
    312   1.2   thorpej 	int i;
    313   1.2   thorpej 
    314   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    315   1.9       jmc 	printf("disklabel shows %d sectors\n",
    316   1.9       jmc 	    st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE);
    317   1.9       jmc #else
    318   1.1     soren 	printf("disklabel shows %d sectors\n", lbl.d_secperunit);
    319   1.9       jmc #endif
    320   1.2   thorpej 	l = (int32_t *)buf;
    321   1.1     soren 	checksum = 0;
    322   1.2   thorpej 	for (i = 0; i < 512 / 4; ++i)
    323   1.7    sekiya 		checksum += be32toh(l[i]);
    324   1.1     soren 	printf("checksum: %08x%s\n", checksum, checksum == 0 ? "" : " *ERROR*");
    325   1.7    sekiya 	printf("root part: %d\n", be16toh(volhdr->root));
    326   1.7    sekiya 	printf("swap part: %d\n", be16toh(volhdr->swap));
    327   1.2   thorpej 	printf("bootfile: %s\n", volhdr->bootfile);
    328   1.1     soren 	/* volhdr->devparams[0..47] */
    329   1.1     soren 	printf("\nVolume header files:\n");
    330  1.17    rumble 	for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; ++i)
    331   1.2   thorpej 		if (volhdr->voldir[i].name[0])
    332   1.9       jmc 			printf("%-8s offset %4d blocks, length %8d bytes "
    333   1.9       jmc 			    "(%d blocks)\n",
    334   1.9       jmc 			    volhdr->voldir[i].name,
    335   1.9       jmc                             be32toh(volhdr->voldir[i].block),
    336   1.9       jmc 			    be32toh(volhdr->voldir[i].bytes),
    337   1.9       jmc                             (be32toh(volhdr->voldir[i].bytes) + 511) / 512);
    338   1.1     soren 	printf("\nSGI partitions:\n");
    339   1.9       jmc 	for (i = 0; i < SGI_BOOT_BLOCK_MAXPARTITIONS; ++i) {
    340   1.7    sekiya 		if (be32toh(volhdr->partitions[i].blocks)) {
    341   1.2   thorpej 			printf("%2d:%c blocks %8d first %8d type %2d (%s)\n",
    342   1.7    sekiya 			  i, i + 'a', be32toh(volhdr->partitions[i].blocks),
    343   1.7    sekiya 			       be32toh(volhdr->partitions[i].first),
    344   1.7    sekiya 			       be32toh(volhdr->partitions[i].type),
    345   1.7    sekiya 			  be32toh(volhdr->partitions[i].type) > 13 ? "???" :
    346   1.7    sekiya 			    sgi_types[be32toh(volhdr->partitions[i].type)]);
    347   1.2   thorpej 		}
    348   1.2   thorpej 	}
    349   1.1     soren }
    350   1.1     soren 
    351   1.2   thorpej void
    352   1.2   thorpej init_volhdr(void)
    353   1.1     soren {
    354   1.1     soren 	memset(buf, 0, sizeof(buf));
    355   1.9       jmc 	volhdr->magic = htobe32(SGI_BOOT_BLOCK_MAGIC);
    356   1.7    sekiya 	volhdr->root = htobe16(0);
    357   1.7    sekiya 	volhdr->swap = htobe16(1);
    358   1.2   thorpej 	strcpy(volhdr->bootfile, "/netbsd");
    359   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    360   1.9       jmc 	volhdr->dp.dp_skew = 0;
    361   1.9       jmc #else
    362   1.4    simonb 	volhdr->dp.dp_skew = lbl.d_trackskew;
    363   1.9       jmc #endif
    364   1.4    simonb 	volhdr->dp.dp_gap1 = 1; /* XXX */
    365   1.4    simonb 	volhdr->dp.dp_gap2 = 1; /* XXX */
    366   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    367   1.9       jmc 	volhdr->dp.dp_cyls =
    368   1.9       jmc 	    htobe16(st.st_size / (SGIVOL_NBTOOL_NSECS * SGIVOL_NBTOOL_NTRACKS));
    369   1.9       jmc #else
    370   1.7    sekiya 	volhdr->dp.dp_cyls = htobe16(lbl.d_ncylinders);
    371   1.9       jmc #endif
    372   1.4    simonb 	volhdr->dp.dp_shd0 = 0;
    373   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    374   1.9       jmc 	volhdr->dp.dp_trks0 = htobe16(SGIVOL_NBTOOL_NTRACKS);
    375   1.9       jmc 	volhdr->dp.dp_secs = htobe16(SGIVOL_NBTOOL_NSECS);
    376   1.9       jmc 	volhdr->dp.dp_secbytes = htobe16(SGI_BOOT_BLOCK_BLOCKSIZE);
    377   1.9       jmc 	volhdr->dp.dp_interleave = htobe16(1);
    378   1.9       jmc #else
    379   1.7    sekiya 	volhdr->dp.dp_trks0 = htobe16(lbl.d_ntracks);
    380   1.7    sekiya 	volhdr->dp.dp_secs = htobe16(lbl.d_nsectors);
    381   1.7    sekiya 	volhdr->dp.dp_secbytes = htobe16(lbl.d_secsize);
    382   1.7    sekiya 	volhdr->dp.dp_interleave = htobe16(lbl.d_interleave);
    383   1.9       jmc #endif
    384   1.7    sekiya 	volhdr->dp.dp_nretries = htobe32(22);
    385   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    386   1.9       jmc 	volhdr->partitions[10].blocks =
    387   1.9       jmc 	    htobe32(st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE);
    388   1.9       jmc #else
    389   1.7    sekiya 	volhdr->partitions[10].blocks = htobe32(lbl.d_secperunit);
    390   1.9       jmc #endif
    391   1.1     soren 	volhdr->partitions[10].first = 0;
    392   1.7    sekiya 	volhdr->partitions[10].type = htobe32(SGI_PTYPE_VOLUME);
    393   1.7    sekiya 	volhdr->partitions[8].blocks = htobe32(volhdr_size);
    394   1.1     soren 	volhdr->partitions[8].first = 0;
    395   1.7    sekiya 	volhdr->partitions[8].type = htobe32(SGI_PTYPE_VOLHDR);
    396   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    397   1.9       jmc 	volhdr->partitions[0].blocks =
    398   1.9       jmc 	    htobe32((st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE) - volhdr_size);
    399   1.9       jmc #else
    400   1.7    sekiya 	volhdr->partitions[0].blocks = htobe32(lbl.d_secperunit - volhdr_size);
    401   1.9       jmc #endif
    402   1.7    sekiya 	volhdr->partitions[0].first = htobe32(volhdr_size);
    403   1.7    sekiya 	volhdr->partitions[0].type = htobe32(SGI_PTYPE_BSD);
    404   1.1     soren 	write_volhdr();
    405   1.1     soren }
    406   1.1     soren 
    407   1.2   thorpej void
    408   1.2   thorpej read_file(void)
    409   1.1     soren {
    410   1.1     soren 	FILE *fp;
    411   1.2   thorpej 	int i;
    412   1.1     soren 
    413   1.5     rafal 	if (!opt_q)
    414   1.5     rafal 		printf("Reading file %s\n", vfilename);
    415  1.17    rumble 	for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; ++i) {
    416   1.3   thorpej 		if (strncmp(vfilename, volhdr->voldir[i].name,
    417   1.7    sekiya 			    strlen(volhdr->voldir[i].name)) == 0)
    418   1.1     soren 			break;
    419   1.1     soren 	}
    420  1.17    rumble 	if (i >= SGI_BOOT_BLOCK_MAXVOLDIRS) {
    421  1.17    rumble 		printf("File '%s' not found\n", vfilename);
    422   1.1     soren 		exit(1);
    423   1.1     soren 	}
    424   1.1     soren 	/* XXX assumes volume header starts at 0? */
    425   1.7    sekiya 	lseek(fd, be32toh(volhdr->voldir[i].block) * 512, SEEK_SET);
    426   1.1     soren 	fp = fopen(ufilename, "w");
    427   1.1     soren 	if (fp == NULL) {
    428   1.1     soren 		perror("open write");
    429   1.1     soren 		exit(1);
    430   1.1     soren 	}
    431   1.7    sekiya 	i = be32toh(volhdr->voldir[i].bytes);
    432   1.2   thorpej 	while (i > 0) {
    433   1.1     soren 		if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
    434   1.1     soren 			perror("read file");
    435   1.1     soren 			exit(1);
    436   1.1     soren 		}
    437   1.2   thorpej 		fwrite(buf, 1, i > sizeof(buf) ? sizeof(buf) : i, fp);
    438   1.2   thorpej 		i -= i > sizeof(buf) ? sizeof(buf) : i;
    439   1.1     soren 	}
    440   1.1     soren 	fclose(fp);
    441   1.1     soren }
    442   1.1     soren 
    443   1.2   thorpej void
    444   1.2   thorpej write_file(void)
    445   1.1     soren {
    446   1.1     soren 	FILE *fp;
    447   1.2   thorpej 	int slot;
    448   1.2   thorpej 	size_t namelen;
    449   1.2   thorpej 	int block, i;
    450   1.1     soren 	struct stat st;
    451   1.1     soren 	char fbuf[512];
    452   1.1     soren 
    453   1.5     rafal 	if (!opt_q)
    454   1.5     rafal 		printf("Writing file %s\n", ufilename);
    455   1.1     soren 	if (stat(ufilename, &st) < 0) {
    456   1.1     soren 		perror("stat");
    457   1.1     soren 		exit(1);
    458   1.1     soren 	}
    459   1.5     rafal 	if (!opt_q)
    460   1.5     rafal 		printf("File %s has %lld bytes\n", ufilename, st.st_size);
    461   1.2   thorpej 	slot = -1;
    462  1.17    rumble 	for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; ++i) {
    463   1.2   thorpej 		if (volhdr->voldir[i].name[0] == '\0' && slot < 0)
    464   1.2   thorpej 			slot = i;
    465  1.18    rumble 		if (names_match(i, vfilename)) {
    466   1.2   thorpej 			slot = i;
    467   1.1     soren 			break;
    468   1.1     soren 		}
    469   1.1     soren 	}
    470   1.2   thorpej 	if (slot == -1) {
    471   1.2   thorpej 		printf("No directory space for file %s\n", vfilename);
    472   1.1     soren 		exit(1);
    473   1.1     soren 	}
    474   1.1     soren 	/* -w can overwrite, -a won't overwrite */
    475   1.7    sekiya 	if (be32toh(volhdr->voldir[slot].block) > 0) {
    476   1.5     rafal 		if (!opt_q)
    477   1.5     rafal 			printf("File %s exists, removing old file\n",
    478   1.5     rafal 				vfilename);
    479   1.2   thorpej 		volhdr->voldir[slot].name[0] = 0;
    480   1.2   thorpej 		volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0;
    481   1.1     soren 	}
    482   1.1     soren 	if (st.st_size == 0) {
    483   1.5     rafal 		printf("bad file size\n");
    484   1.1     soren 		exit(1);
    485   1.1     soren 	}
    486   1.1     soren 	/* XXX assumes volume header starts at 0? */
    487   1.1     soren 	block = allocate_space((int)st.st_size);
    488   1.1     soren 	if (block < 0) {
    489   1.1     soren 		printf("No space for file\n");
    490   1.1     soren 		exit(1);
    491   1.1     soren 	}
    492   1.1     soren 
    493   1.2   thorpej 	/*
    494   1.2   thorpej 	 * Make sure the name in the volume header is max. 8 chars,
    495   1.2   thorpej 	 * NOT including NUL.
    496   1.2   thorpej 	 */
    497   1.2   thorpej 	namelen = strlen(vfilename);
    498   1.2   thorpej 	if (namelen > sizeof(volhdr->voldir[slot].name)) {
    499   1.1     soren 		printf("Warning: '%s' is too long for volume header, ",
    500   1.1     soren 		       vfilename);
    501   1.2   thorpej 		namelen = sizeof(volhdr->voldir[slot].name);
    502  1.17    rumble 		printf("truncating to '%.8s'\n", vfilename);
    503   1.1     soren 	}
    504   1.4    simonb 
    505   1.2   thorpej 	/* Populate it w/ NULs */
    506   1.2   thorpej 	memset(volhdr->voldir[slot].name, 0,
    507   1.2   thorpej 	    sizeof(volhdr->voldir[slot].name));
    508   1.2   thorpej 	/* Then copy the name */
    509   1.2   thorpej 	memcpy(volhdr->voldir[slot].name, vfilename, namelen);
    510   1.2   thorpej 
    511   1.7    sekiya 	volhdr->voldir[slot].block = htobe32(block);
    512   1.7    sekiya 	volhdr->voldir[slot].bytes = htobe32(st.st_size);
    513   1.1     soren 
    514   1.1     soren 	write_volhdr();
    515   1.1     soren 
    516   1.1     soren 	/* write the file itself */
    517   1.2   thorpej 	i = lseek(fd, block * 512, SEEK_SET);
    518   1.2   thorpej 	if (i < 0) {
    519   1.1     soren 		perror("lseek write");
    520   1.1     soren 		exit(1);
    521   1.1     soren 	}
    522   1.2   thorpej 	i = st.st_size;
    523   1.1     soren 	fp = fopen(ufilename, "r");
    524   1.2   thorpej 	while (i > 0) {
    525   1.2   thorpej 		fread(fbuf, 1, i > 512 ? 512 : i, fp);
    526   1.1     soren 		if (write(fd, fbuf, 512) != 512) {
    527   1.1     soren 			perror("write file");
    528   1.1     soren 			exit(1);
    529   1.1     soren 		}
    530   1.2   thorpej 		i -= i > 512 ? 512 : i;
    531   1.1     soren 	}
    532  1.19  christos 	fclose(fp);
    533   1.1     soren }
    534   1.1     soren 
    535   1.2   thorpej void
    536   1.2   thorpej delete_file(void)
    537   1.1     soren {
    538   1.2   thorpej 	int i;
    539   1.2   thorpej 
    540  1.17    rumble 	for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; ++i) {
    541  1.18    rumble 		if (names_match(i, vfilename)) {
    542   1.1     soren 			break;
    543   1.1     soren 		}
    544   1.1     soren 	}
    545  1.17    rumble 	if (i >= SGI_BOOT_BLOCK_MAXVOLDIRS) {
    546  1.17    rumble 		printf("File '%s' not found\n", vfilename);
    547   1.1     soren 		exit(1);
    548   1.1     soren 	}
    549   1.5     rafal 
    550   1.5     rafal 	/* XXX: we don't compact the file space, so get fragmentation */
    551   1.2   thorpej 	volhdr->voldir[i].name[0] = '\0';
    552   1.2   thorpej 	volhdr->voldir[i].block = volhdr->voldir[i].bytes = 0;
    553   1.1     soren 	write_volhdr();
    554   1.1     soren }
    555   1.1     soren 
    556   1.2   thorpej void
    557  1.17    rumble move_file(void)
    558  1.17    rumble {
    559  1.17    rumble 	char dstfile[sizeof(volhdr->voldir[0].name) + 1];
    560  1.17    rumble 	size_t namelen;
    561  1.17    rumble 	int i, slot = -1;
    562  1.17    rumble 
    563  1.17    rumble 	/*
    564  1.17    rumble 	 * Make sure the name in the volume header is max. 8 chars,
    565  1.17    rumble 	 * NOT including NUL.
    566  1.17    rumble 	 */
    567  1.17    rumble 	namelen = strlen(ufilename);
    568  1.17    rumble 	if (namelen > sizeof(volhdr->voldir[0].name)) {
    569  1.17    rumble 		printf("Warning: '%s' is too long for volume header, ",
    570  1.17    rumble 		       ufilename);
    571  1.17    rumble 		namelen = sizeof(volhdr->voldir[0].name);
    572  1.17    rumble 		printf("truncating to '%.8s'\n", ufilename);
    573  1.17    rumble 	}
    574  1.17    rumble 	memset(dstfile, 0, sizeof(dstfile));
    575  1.17    rumble 	memcpy(dstfile, ufilename, namelen);
    576  1.17    rumble 
    577  1.17    rumble 	for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; i++) {
    578  1.18    rumble 		if (names_match(i, vfilename)) {
    579  1.17    rumble 			if (slot != -1) {
    580  1.17    rumble 				printf("Error: Cannot move '%s' to '%s' - "
    581  1.17    rumble 				    "duplicate source files exist!\n",
    582  1.17    rumble 				    vfilename, dstfile);
    583  1.17    rumble 				exit(1);
    584  1.17    rumble 			}
    585  1.17    rumble 			slot = i;
    586  1.17    rumble 		}
    587  1.18    rumble 		if (names_match(i, dstfile)) {
    588  1.17    rumble 			printf("Error: Cannot move '%s' to '%s' - "
    589  1.17    rumble 			    "destination file already exists!\n",
    590  1.17    rumble 			    vfilename, dstfile);
    591  1.17    rumble 			exit(1);
    592  1.17    rumble 		}
    593  1.17    rumble 	}
    594  1.17    rumble 	if (slot == -1) {
    595  1.17    rumble 		printf("File '%s' not found\n", vfilename);
    596  1.17    rumble 		exit(1);
    597  1.17    rumble 	}
    598  1.17    rumble 
    599  1.17    rumble 	/* `dstfile' is already padded with NULs */
    600  1.17    rumble 	memcpy(volhdr->voldir[slot].name, dstfile,
    601  1.17    rumble 	    sizeof(volhdr->voldir[slot].name));
    602  1.17    rumble 
    603  1.17    rumble 	write_volhdr();
    604  1.17    rumble }
    605  1.17    rumble 
    606  1.17    rumble void
    607   1.2   thorpej modify_partition(void)
    608   1.1     soren {
    609   1.5     rafal 	if (!opt_q)
    610   1.5     rafal 		printf("Modify partition %d start %d length %d\n",
    611   1.5     rafal 			partno, partfirst, partblocks);
    612  1.17    rumble 	if (partno < 0 || partno >= SGI_BOOT_BLOCK_MAXPARTITIONS) {
    613   1.5     rafal 		printf("Invalid partition number: %d\n", partno);
    614   1.1     soren 		exit(1);
    615   1.1     soren 	}
    616   1.7    sekiya 	volhdr->partitions[partno].blocks = htobe32(partblocks);
    617   1.7    sekiya 	volhdr->partitions[partno].first = htobe32(partfirst);
    618   1.7    sekiya 	volhdr->partitions[partno].type = htobe32(parttype);
    619   1.1     soren 	write_volhdr();
    620   1.1     soren }
    621   1.1     soren 
    622   1.2   thorpej void
    623   1.2   thorpej write_volhdr(void)
    624   1.1     soren {
    625   1.2   thorpej 	int i;
    626   1.2   thorpej 
    627   1.1     soren 	checksum_vol();
    628   1.5     rafal 
    629   1.5     rafal 	if (!opt_q)
    630   1.5     rafal 		display_vol();
    631   1.5     rafal 	if (!opt_f) {
    632   1.5     rafal 		printf("\nDo you want to update volume (y/n)? ");
    633   1.5     rafal 		i = getchar();
    634   1.5     rafal 		if (i != 'Y' && i != 'y')
    635   1.5     rafal 			exit(1);
    636   1.5     rafal 	}
    637   1.7    sekiya 	i = lseek(fd, 0, SEEK_SET);
    638   1.2   thorpej 	if (i < 0) {
    639   1.1     soren 		perror("lseek 0");
    640   1.1     soren 		exit(1);
    641   1.1     soren 	}
    642   1.2   thorpej 	i = write(fd, buf, 512);
    643   1.2   thorpej 	if (i < 0)
    644   1.1     soren 		perror("write volhdr");
    645   1.1     soren }
    646   1.1     soren 
    647   1.2   thorpej int
    648   1.2   thorpej allocate_space(int size)
    649   1.1     soren {
    650   1.1     soren 	int n, blocks;
    651   1.1     soren 	int first;
    652   1.1     soren 
    653   1.1     soren 	blocks = (size + 511) / 512;
    654   1.1     soren 	first = 2;
    655   1.1     soren 	n = 0;
    656  1.17    rumble 	while (n < SGI_BOOT_BLOCK_MAXVOLDIRS) {
    657   1.1     soren 		if (volhdr->voldir[n].name[0]) {
    658   1.7    sekiya 			if (first < (be32toh(volhdr->voldir[n].block) +
    659   1.7    sekiya 			  (be32toh(volhdr->voldir[n].bytes) + 511) / 512) &&
    660   1.7    sekiya 			    (first + blocks) > be32toh(volhdr->voldir[n].block)) {
    661   1.7    sekiya 				first = be32toh(volhdr->voldir[n].block) +
    662   1.7    sekiya 					(be32toh(volhdr->voldir[n].bytes) + 511) / 512;
    663   1.1     soren #if 0
    664   1.7    sekiya 				printf("allocate: n=%d first=%d blocks=%d size=%d\n", n, first, blocks, size);
    665   1.7    sekiya 				printf("%s %d %d\n", volhdr->voldir[n].name, volhdr->voldir[n].block, volhdr->voldir[n].bytes);
    666   1.7    sekiya 				printf("first=%d block=%d last=%d end=%d\n", first, volhdr->voldir[n].block,
    667   1.7    sekiya 				       first + blocks - 1, volhdr->voldir[n].block + (volhdr->voldir[n].bytes + 511) / 512);
    668   1.1     soren #endif
    669   1.1     soren 				n = 0;
    670   1.1     soren 				continue;
    671   1.1     soren 			}
    672   1.1     soren 		}
    673   1.1     soren 		++n;
    674   1.1     soren 	}
    675   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
    676   1.9       jmc 	if (first + blocks > (st.st_size / SGI_BOOT_BLOCK_BLOCKSIZE))
    677   1.9       jmc #else
    678   1.1     soren 	if (first + blocks > lbl.d_secperunit)
    679   1.9       jmc #endif
    680   1.1     soren 		first = -1;
    681   1.1     soren 	/* XXX assumes volume header is partition 8 */
    682   1.1     soren 	/* XXX assumes volume header starts at 0? */
    683   1.7    sekiya 	if (first + blocks >= be32toh(volhdr->partitions[8].blocks))
    684   1.1     soren 		first = -1;
    685   1.7    sekiya 	return (first);
    686   1.1     soren }
    687   1.1     soren 
    688   1.2   thorpej void
    689   1.2   thorpej checksum_vol(void)
    690   1.1     soren {
    691   1.2   thorpej 	int32_t *l;
    692   1.2   thorpej 	int i;
    693   1.2   thorpej 
    694   1.1     soren 	volhdr->checksum = checksum = 0;
    695   1.2   thorpej 	l = (int32_t *)buf;
    696   1.2   thorpej 	for (i = 0; i < 512 / 4; ++i)
    697   1.7    sekiya 		checksum += be32toh(l[i]);
    698   1.7    sekiya 	volhdr->checksum = htobe32(-checksum);
    699   1.1     soren }
    700   1.1     soren 
    701   1.2   thorpej void
    702   1.2   thorpej usage(void)
    703   1.1     soren {
    704  1.15    rumble 	printf("Usage:	sgivol [-qf] -i [-h vhsize] device\n"
    705  1.15    rumble 	       "	sgivol [-qf] -r vhfilename diskfilename device\n"
    706  1.15    rumble 	       "	sgivol [-qf] -w vhfilename diskfilename device\n"
    707  1.15    rumble 	       "	sgivol [-qf] -d vhfilename device\n"
    708  1.18    rumble 	       "	sgivol [-qf] -m vhfilename vhfilename device\n"
    709  1.15    rumble 	       "	sgivol [-qf] -p partno partfirst partblocks "
    710  1.15    rumble 	       "parttype device\n"
    711   1.1     soren 	       );
    712   1.1     soren 	exit(0);
    713   1.1     soren }
    714