xref: /netbsd-src/usr.sbin/installboot/arch/sparc64.c (revision e5c09b191b3a14e8b4648a699077b773caf1ed9b)
1*e5c09b19Sthorpej /*	$NetBSD: sparc64.c,v 1.19 2019/05/07 04:35:31 thorpej Exp $	*/
267c2e401Slukem 
367c2e401Slukem /*-
467c2e401Slukem  * Copyright (c) 2002 The NetBSD Foundation, Inc.
567c2e401Slukem  * All rights reserved.
667c2e401Slukem  *
767c2e401Slukem  * This code is derived from software contributed to The NetBSD Foundation
867c2e401Slukem  * by Luke Mewburn of Wasabi Systems.
967c2e401Slukem  *
1067c2e401Slukem  * Redistribution and use in source and binary forms, with or without
1167c2e401Slukem  * modification, are permitted provided that the following conditions
1267c2e401Slukem  * are met:
1367c2e401Slukem  * 1. Redistributions of source code must retain the above copyright
1467c2e401Slukem  *    notice, this list of conditions and the following disclaimer.
1567c2e401Slukem  * 2. Redistributions in binary form must reproduce the above copyright
1667c2e401Slukem  *    notice, this list of conditions and the following disclaimer in the
1767c2e401Slukem  *    documentation and/or other materials provided with the distribution.
1867c2e401Slukem  *
1967c2e401Slukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2067c2e401Slukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2167c2e401Slukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2267c2e401Slukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2367c2e401Slukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2467c2e401Slukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2567c2e401Slukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2667c2e401Slukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2767c2e401Slukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2867c2e401Slukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2967c2e401Slukem  * POSSIBILITY OF SUCH DAMAGE.
3067c2e401Slukem  */
3167c2e401Slukem 
3267c2e401Slukem /*
3367c2e401Slukem  * Copyright (c) 2002 Matthew R. Green
3467c2e401Slukem  * All rights reserved.
3567c2e401Slukem  *
3667c2e401Slukem  * Redistribution and use in source and binary forms, with or without
3767c2e401Slukem  * modification, are permitted provided that the following conditions
3867c2e401Slukem  * are met:
3967c2e401Slukem  * 1. Redistributions of source code must retain the above copyright
4067c2e401Slukem  *    notice, this list of conditions and the following disclaimer.
4167c2e401Slukem  * 2. Redistributions in binary form must reproduce the above copyright
4267c2e401Slukem  *    notice, this list of conditions and the following disclaimer in the
4367c2e401Slukem  *    documentation and/or other materials provided with the distribution.
4467c2e401Slukem  *
4567c2e401Slukem  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4667c2e401Slukem  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4767c2e401Slukem  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4867c2e401Slukem  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4967c2e401Slukem  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
5067c2e401Slukem  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
5167c2e401Slukem  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
5267c2e401Slukem  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5367c2e401Slukem  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5467c2e401Slukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5567c2e401Slukem  * SUCH DAMAGE.
5667c2e401Slukem  */
5767c2e401Slukem 
58b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
59b2f78261Sjmc #include "nbtool_config.h"
60b2f78261Sjmc #endif
61b2f78261Sjmc 
6267c2e401Slukem #include <sys/cdefs.h>
63e5f39b5eStsutsui #if !defined(__lint)
64*e5c09b19Sthorpej __RCSID("$NetBSD: sparc64.c,v 1.19 2019/05/07 04:35:31 thorpej Exp $");
6567c2e401Slukem #endif	/* !__lint */
6667c2e401Slukem 
6767c2e401Slukem #include <sys/param.h>
6867c2e401Slukem 
6967c2e401Slukem #include <assert.h>
7067c2e401Slukem #include <err.h>
7167c2e401Slukem #include <stddef.h>
7267c2e401Slukem #include <stdio.h>
7367c2e401Slukem #include <stdlib.h>
745b7f1cceSmrg #include <string.h>
7567c2e401Slukem #include <unistd.h>
7667c2e401Slukem 
771bdd92eeSlukem #include "installboot.h"
7867c2e401Slukem 
79cce659e2Sdsl static int sparc64_clearboot(ib_params *);
80cce659e2Sdsl static int sparc64_setboot(ib_params *);
81cce659e2Sdsl 
82*e5c09b19Sthorpej struct ib_mach ib_mach_sparc64 = {
83*e5c09b19Sthorpej 	.name		=	"sparc64",
84*e5c09b19Sthorpej 	.setboot	=	sparc64_setboot,
85*e5c09b19Sthorpej 	.clearboot	=	sparc64_clearboot,
86*e5c09b19Sthorpej 	.editboot	=	no_editboot,
87*e5c09b19Sthorpej };
88cce659e2Sdsl 
89cce659e2Sdsl static int
sparc64_clearboot(ib_params * params)9067c2e401Slukem sparc64_clearboot(ib_params *params)
9167c2e401Slukem {
9296adf034Smrg 	char	bb[SPARC64_BOOT_BLOCK_MAX_SIZE];
9367c2e401Slukem 	ssize_t	rv;
9467c2e401Slukem 
9567c2e401Slukem 	assert(params != NULL);
9667c2e401Slukem 	assert(params->fsfd != -1);
9767c2e401Slukem 	assert(params->filesystem != NULL);
9867c2e401Slukem 
991bdd92eeSlukem 	if (params->flags & (IB_STAGE1START | IB_STAGE2START)) {
1001bdd92eeSlukem 		warnx("`-b bno' and `-B bno' are not supported for %s",
1011bdd92eeSlukem 		    params->machine->name);
10267c2e401Slukem 		return (0);
10367c2e401Slukem 	}
1041bdd92eeSlukem 
10567c2e401Slukem 	/* first check that it _could_ exist here */
106ed5fff88Slukem 	rv = pread(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
10767c2e401Slukem 	if (rv == -1) {
10867c2e401Slukem 		warn("Reading `%s'", params->filesystem);
10967c2e401Slukem 		return (0);
110ed5fff88Slukem 	} else if (rv != sizeof(bb)) {
11167c2e401Slukem 		warnx("Reading `%s': short read", params->filesystem);
11267c2e401Slukem 		return (0);
11367c2e401Slukem 	}
11467c2e401Slukem 
11567c2e401Slukem 	/* now clear it out to nothing */
116ed5fff88Slukem 	memset(&bb, 0, sizeof(bb));
11767c2e401Slukem 
11867c2e401Slukem 	if (params->flags & IB_VERBOSE)
11967c2e401Slukem 		printf("%slearing boot block\n",
12067c2e401Slukem 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
12167c2e401Slukem 	if (params->flags & IB_NOWRITE)
12267c2e401Slukem 		return (1);
12367c2e401Slukem 
12467c2e401Slukem 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
12567c2e401Slukem 	if (rv == -1) {
12667c2e401Slukem 		warn("Writing `%s'", params->filesystem);
12767c2e401Slukem 		return (0);
12867c2e401Slukem 	} else if (rv != sizeof(bb)) {
12967c2e401Slukem 		warnx("Writing `%s': short write", params->filesystem);
13067c2e401Slukem 		return (0);
13167c2e401Slukem 	}
13267c2e401Slukem 
13367c2e401Slukem 	return (1);
13467c2e401Slukem }
13567c2e401Slukem 
136cce659e2Sdsl static int
sparc64_setboot(ib_params * params)13767c2e401Slukem sparc64_setboot(ib_params *params)
13867c2e401Slukem {
13996adf034Smrg 	char		bb[SPARC64_BOOT_BLOCK_MAX_SIZE];
1408eb8919eSlukem 	int		retval;
1414cf62149Smrg 	ssize_t		rv;
14267c2e401Slukem 
14367c2e401Slukem 	assert(params != NULL);
14467c2e401Slukem 	assert(params->fsfd != -1);
14567c2e401Slukem 	assert(params->filesystem != NULL);
146d2804426Slukem 	assert(params->s1fd != -1);
147d2804426Slukem 	assert(params->stage1 != NULL);
14867c2e401Slukem 
14967c2e401Slukem 	retval = 0;
15067c2e401Slukem 
1511bdd92eeSlukem 	if (params->flags & (IB_STAGE1START | IB_STAGE2START)) {
1521bdd92eeSlukem 		warnx("`-b bno' and `-B bno' are not supported for %s",
1531bdd92eeSlukem 		    params->machine->name);
1541bdd92eeSlukem 		goto done;
1551bdd92eeSlukem 	}
1561bdd92eeSlukem 
1574cf62149Smrg 	memset(&bb, 0, SPARC64_BOOT_BLOCK_MAX_SIZE);
158ed5fff88Slukem 	rv = read(params->s1fd, &bb, sizeof(bb));
1594cf62149Smrg 	if (rv == -1) {
1604897886aSfredette 		warn("Reading `%s'", params->stage1);
16167c2e401Slukem 		goto done;
16267c2e401Slukem 	}
16367c2e401Slukem 
16467c2e401Slukem 	if (params->flags & IB_VERBOSE) {
1651bdd92eeSlukem 		printf("Bootstrap start sector: %u\n",
1661bdd92eeSlukem 		    SPARC64_BOOT_BLOCK_OFFSET / SPARC64_BOOT_BLOCK_BLOCKSIZE);
167d9e55fcaSlukem 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
16867c2e401Slukem 		printf("%sriting bootstrap\n",
16967c2e401Slukem 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
17067c2e401Slukem 	}
17167c2e401Slukem 	if (params->flags & IB_NOWRITE) {
17267c2e401Slukem 		retval = 1;
17367c2e401Slukem 		goto done;
17467c2e401Slukem 	}
17567c2e401Slukem 
1764cf62149Smrg 	rv = pwrite(params->fsfd, &bb, SPARC64_BOOT_BLOCK_MAX_SIZE,
1771bdd92eeSlukem 	    SPARC64_BOOT_BLOCK_OFFSET);
17867c2e401Slukem 	if (rv == -1) {
17967c2e401Slukem 		warn("Writing `%s'", params->filesystem);
18067c2e401Slukem 		goto done;
1814cf62149Smrg 	} else if (rv != SPARC64_BOOT_BLOCK_MAX_SIZE) {
18267c2e401Slukem 		warnx("Writing `%s': short write", params->filesystem);
18367c2e401Slukem 		goto done;
18467c2e401Slukem 	} else
18567c2e401Slukem 		retval = 1;
18667c2e401Slukem 
18767c2e401Slukem  done:
18867c2e401Slukem 	return (retval);
18967c2e401Slukem }
190