xref: /netbsd-src/usr.sbin/installboot/arch/sparc64.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: sparc64.c,v 1.15 2006/02/18 10:08:07 dsl 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 #if HAVE_NBTOOL_CONFIG_H
68 #include "nbtool_config.h"
69 #endif
70 
71 #include <sys/cdefs.h>
72 #if defined(__RCSID) && !defined(__lint)
73 __RCSID("$NetBSD: sparc64.c,v 1.15 2006/02/18 10:08:07 dsl Exp $");
74 #endif	/* !__lint */
75 
76 #include <sys/param.h>
77 
78 #include <assert.h>
79 #include <err.h>
80 #include <stddef.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <unistd.h>
85 
86 #include "installboot.h"
87 
88 static int sparc64_clearboot(ib_params *);
89 static int sparc64_setboot(ib_params *);
90 
91 struct ib_mach ib_mach_sparc64 =
92 	{ "sparc64", sparc64_setboot, sparc64_clearboot, no_editboot, 0};
93 
94 static int
95 sparc64_clearboot(ib_params *params)
96 {
97 	char	bb[SPARC64_BOOT_BLOCK_MAX_SIZE];
98 	ssize_t	rv;
99 
100 	assert(params != NULL);
101 	assert(params->fsfd != -1);
102 	assert(params->filesystem != NULL);
103 
104 	if (params->flags & (IB_STAGE1START | IB_STAGE2START)) {
105 		warnx("`-b bno' and `-B bno' are not supported for %s",
106 		    params->machine->name);
107 		return (0);
108 	}
109 
110 	/* first check that it _could_ exist here */
111 	rv = pread(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
112 	if (rv == -1) {
113 		warn("Reading `%s'", params->filesystem);
114 		return (0);
115 	} else if (rv != sizeof(bb)) {
116 		warnx("Reading `%s': short read", params->filesystem);
117 		return (0);
118 	}
119 
120 	/* now clear it out to nothing */
121 	memset(&bb, 0, sizeof(bb));
122 
123 	if (params->flags & IB_VERBOSE)
124 		printf("%slearing boot block\n",
125 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
126 	if (params->flags & IB_NOWRITE)
127 		return (1);
128 
129 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
130 	if (rv == -1) {
131 		warn("Writing `%s'", params->filesystem);
132 		return (0);
133 	} else if (rv != sizeof(bb)) {
134 		warnx("Writing `%s': short write", params->filesystem);
135 		return (0);
136 	}
137 
138 	return (1);
139 }
140 
141 static int
142 sparc64_setboot(ib_params *params)
143 {
144 	char		bb[SPARC64_BOOT_BLOCK_MAX_SIZE];
145 	int		retval;
146 	ssize_t		rv;
147 
148 	assert(params != NULL);
149 	assert(params->fsfd != -1);
150 	assert(params->filesystem != NULL);
151 	assert(params->s1fd != -1);
152 	assert(params->stage1 != NULL);
153 
154 	retval = 0;
155 
156 	if (params->flags & (IB_STAGE1START | IB_STAGE2START)) {
157 		warnx("`-b bno' and `-B bno' are not supported for %s",
158 		    params->machine->name);
159 		goto done;
160 	}
161 
162 	memset(&bb, 0, SPARC64_BOOT_BLOCK_MAX_SIZE);
163 	rv = read(params->s1fd, &bb, sizeof(bb));
164 	if (rv == -1) {
165 		warn("Reading `%s'", params->stage1);
166 		goto done;
167 	}
168 
169 	if (params->flags & IB_VERBOSE) {
170 		printf("Bootstrap start sector: %u\n",
171 		    SPARC64_BOOT_BLOCK_OFFSET / SPARC64_BOOT_BLOCK_BLOCKSIZE);
172 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
173 		printf("%sriting bootstrap\n",
174 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
175 	}
176 	if (params->flags & IB_NOWRITE) {
177 		retval = 1;
178 		goto done;
179 	}
180 
181 	rv = pwrite(params->fsfd, &bb, SPARC64_BOOT_BLOCK_MAX_SIZE,
182 	    SPARC64_BOOT_BLOCK_OFFSET);
183 	if (rv == -1) {
184 		warn("Writing `%s'", params->filesystem);
185 		goto done;
186 	} else if (rv != SPARC64_BOOT_BLOCK_MAX_SIZE) {
187 		warnx("Writing `%s': short write", params->filesystem);
188 		goto done;
189 	} else
190 		retval = 1;
191 
192  done:
193 	return (retval);
194 }
195