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