xref: /netbsd-src/usr.sbin/installboot/arch/sparc64.c (revision e5c09b191b3a14e8b4648a699077b773caf1ed9b)
1 /*	$NetBSD: sparc64.c,v 1.19 2019/05/07 04:35:31 thorpej 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 2002 Matthew R. Green
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
50  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
52  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 #if HAVE_NBTOOL_CONFIG_H
59 #include "nbtool_config.h"
60 #endif
61 
62 #include <sys/cdefs.h>
63 #if !defined(__lint)
64 __RCSID("$NetBSD: sparc64.c,v 1.19 2019/05/07 04:35:31 thorpej Exp $");
65 #endif	/* !__lint */
66 
67 #include <sys/param.h>
68 
69 #include <assert.h>
70 #include <err.h>
71 #include <stddef.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76 
77 #include "installboot.h"
78 
79 static int sparc64_clearboot(ib_params *);
80 static int sparc64_setboot(ib_params *);
81 
82 struct ib_mach ib_mach_sparc64 = {
83 	.name		=	"sparc64",
84 	.setboot	=	sparc64_setboot,
85 	.clearboot	=	sparc64_clearboot,
86 	.editboot	=	no_editboot,
87 };
88 
89 static int
sparc64_clearboot(ib_params * params)90 sparc64_clearboot(ib_params *params)
91 {
92 	char	bb[SPARC64_BOOT_BLOCK_MAX_SIZE];
93 	ssize_t	rv;
94 
95 	assert(params != NULL);
96 	assert(params->fsfd != -1);
97 	assert(params->filesystem != NULL);
98 
99 	if (params->flags & (IB_STAGE1START | IB_STAGE2START)) {
100 		warnx("`-b bno' and `-B bno' are not supported for %s",
101 		    params->machine->name);
102 		return (0);
103 	}
104 
105 	/* first check that it _could_ exist here */
106 	rv = pread(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
107 	if (rv == -1) {
108 		warn("Reading `%s'", params->filesystem);
109 		return (0);
110 	} else if (rv != sizeof(bb)) {
111 		warnx("Reading `%s': short read", params->filesystem);
112 		return (0);
113 	}
114 
115 	/* now clear it out to nothing */
116 	memset(&bb, 0, sizeof(bb));
117 
118 	if (params->flags & IB_VERBOSE)
119 		printf("%slearing boot block\n",
120 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
121 	if (params->flags & IB_NOWRITE)
122 		return (1);
123 
124 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
125 	if (rv == -1) {
126 		warn("Writing `%s'", params->filesystem);
127 		return (0);
128 	} else if (rv != sizeof(bb)) {
129 		warnx("Writing `%s': short write", params->filesystem);
130 		return (0);
131 	}
132 
133 	return (1);
134 }
135 
136 static int
sparc64_setboot(ib_params * params)137 sparc64_setboot(ib_params *params)
138 {
139 	char		bb[SPARC64_BOOT_BLOCK_MAX_SIZE];
140 	int		retval;
141 	ssize_t		rv;
142 
143 	assert(params != NULL);
144 	assert(params->fsfd != -1);
145 	assert(params->filesystem != NULL);
146 	assert(params->s1fd != -1);
147 	assert(params->stage1 != NULL);
148 
149 	retval = 0;
150 
151 	if (params->flags & (IB_STAGE1START | IB_STAGE2START)) {
152 		warnx("`-b bno' and `-B bno' are not supported for %s",
153 		    params->machine->name);
154 		goto done;
155 	}
156 
157 	memset(&bb, 0, SPARC64_BOOT_BLOCK_MAX_SIZE);
158 	rv = read(params->s1fd, &bb, sizeof(bb));
159 	if (rv == -1) {
160 		warn("Reading `%s'", params->stage1);
161 		goto done;
162 	}
163 
164 	if (params->flags & IB_VERBOSE) {
165 		printf("Bootstrap start sector: %u\n",
166 		    SPARC64_BOOT_BLOCK_OFFSET / SPARC64_BOOT_BLOCK_BLOCKSIZE);
167 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
168 		printf("%sriting bootstrap\n",
169 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
170 	}
171 	if (params->flags & IB_NOWRITE) {
172 		retval = 1;
173 		goto done;
174 	}
175 
176 	rv = pwrite(params->fsfd, &bb, SPARC64_BOOT_BLOCK_MAX_SIZE,
177 	    SPARC64_BOOT_BLOCK_OFFSET);
178 	if (rv == -1) {
179 		warn("Writing `%s'", params->filesystem);
180 		goto done;
181 	} else if (rv != SPARC64_BOOT_BLOCK_MAX_SIZE) {
182 		warnx("Writing `%s': short write", params->filesystem);
183 		goto done;
184 	} else
185 		retval = 1;
186 
187  done:
188 	return (retval);
189 }
190