Home | History | Annotate | Line # | Download | only in arch
sparc64.c revision 1.2
      1 /*	$NetBSD: sparc64.c,v 1.2 2002/04/04 14:00:55 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Luke Mewburn of Wasabi Systems.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 2002 Matthew R. Green
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. The name of the author may not be used to endorse or promote products
     52  *    derived from this software without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     59  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     61  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     62  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 #if defined(__RCSID) && !defined(__lint)
     69 __RCSID("$NetBSD: sparc64.c,v 1.2 2002/04/04 14:00:55 mrg Exp $");
     70 #endif	/* !__lint */
     71 
     72 #include <sys/param.h>
     73 #include <sys/stat.h>
     74 
     75 #include <assert.h>
     76 #include <err.h>
     77 #include <stddef.h>
     78 #include <stdio.h>
     79 #include <stdlib.h>
     80 #include <string.h>
     81 #include <unistd.h>
     82 
     83 #include "installboot.h"
     84 
     85 int	sparc64_parseopt(ib_params *, const char *);
     86 int	sparc64_setboot(ib_params *);
     87 int	sparc64_clearboot(ib_params *);
     88 
     89 int
     90 sparc64_parseopt(ib_params *params, const char *option)
     91 {
     92 
     93 	/* all options are unknown */
     94 	warnx("Unknown -o option `%s'", option);
     95 	return (0);
     96 }
     97 
     98 #define SPARC64_BOOT_BLOCK_OFFSET	DEV_BSIZE
     99 #define SPARC64_BOOT_BLOCK_BLOCKSIZE	DEV_BSIZE
    100 
    101 int
    102 sparc64_clearboot(ib_params *params)
    103 {
    104 	char	bb[512 * 15];
    105 	ssize_t	rv;
    106 
    107 	assert(params != NULL);
    108 	assert(params->fsfd != -1);
    109 	assert(params->filesystem != NULL);
    110 
    111 	if (params->flags & IB_STARTBLOCK) {
    112 		warnx("Can't use `-b bno' with `-c'");
    113 		return (0);
    114 	}
    115 	/* first check that it _could_ exist here */
    116 	rv = pread(params->fsfd, &bb, sizeof bb, SPARC64_BOOT_BLOCK_OFFSET);
    117 	if (rv == -1) {
    118 		warn("Reading `%s'", params->filesystem);
    119 		return (0);
    120 	} else if (rv != sizeof bb) {
    121 		warnx("Reading `%s': short read", params->filesystem);
    122 		return (0);
    123 	}
    124 
    125 	/* now clear it out to nothing */
    126 	memset(&bb, 0, sizeof bb);
    127 
    128 	if (params->flags & IB_VERBOSE)
    129 		printf("%slearing boot block\n",
    130 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
    131 	if (params->flags & IB_NOWRITE)
    132 		return (1);
    133 
    134 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
    135 	if (rv == -1) {
    136 		warn("Writing `%s'", params->filesystem);
    137 		return (0);
    138 	} else if (rv != sizeof(bb)) {
    139 		warnx("Writing `%s': short write", params->filesystem);
    140 		return (0);
    141 	}
    142 
    143 	return (1);
    144 }
    145 
    146 int
    147 sparc64_setboot(ib_params *params)
    148 {
    149 	struct stat	bootstrapsb;
    150 	char		bb[512 * 15];
    151 	int		startblock, retval;
    152 	ssize_t		rv, bblen;
    153 
    154 	assert(params != NULL);
    155 	assert(params->fsfd != -1);
    156 	assert(params->filesystem != NULL);
    157 	assert(params->bbfd != -1);
    158 	assert(params->bootblock != NULL);
    159 
    160 	retval = 0;
    161 
    162 	if (fstat(params->bbfd, &bootstrapsb) == -1) {
    163 		warn("Examining `%s'", params->bootblock);
    164 		goto done;
    165 	}
    166 	if (!S_ISREG(bootstrapsb.st_mode)) {
    167 		warnx("`%s' must be a regular file", params->bootblock);
    168 		goto done;
    169 	}
    170 
    171 	bblen = read(params->bbfd, &bb, sizeof bb);
    172 	if (bblen == -1) {
    173 		warn("Reading `%s'", params->filesystem);
    174 		goto done;
    175 	}
    176 
    177 	if (params->flags & IB_STARTBLOCK)
    178 		startblock = params->startblock;
    179 	else
    180 		startblock = SPARC64_BOOT_BLOCK_OFFSET /
    181 		    SPARC64_BOOT_BLOCK_BLOCKSIZE;
    182 
    183 	if (params->flags & IB_VERBOSE) {
    184 		printf("Bootstrap start sector: %#x\n", startblock);
    185 		printf("Bootstrap byte count:   %#x\n", (unsigned)bblen);
    186 		printf("%sriting bootstrap\n",
    187 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
    188 	}
    189 	if (params->flags & IB_NOWRITE) {
    190 		retval = 1;
    191 		goto done;
    192 	}
    193 	if (params->flags & IB_VERBOSE)
    194 		printf("Writing boot block\n");
    195 
    196 	rv = pwrite(params->fsfd, &bb, bblen,
    197 	     startblock * SPARC64_BOOT_BLOCK_BLOCKSIZE);
    198 	if (rv == -1) {
    199 		warn("Writing `%s'", params->filesystem);
    200 		goto done;
    201 	} else if (rv != bblen) {
    202 		warnx("Writing `%s': short write", params->filesystem);
    203 		goto done;
    204 	} else
    205 		retval = 1;
    206 
    207  done:
    208 	return (retval);
    209 }
    210