xref: /netbsd-src/usr.sbin/installboot/arch/sparc64.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: sparc64.c,v 1.16 2008/04/28 20:24:16 martin 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  * 3. The name of the author may not be used to endorse or promote products
45  *    derived from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
52  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
54  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 #if HAVE_NBTOOL_CONFIG_H
61 #include "nbtool_config.h"
62 #endif
63 
64 #include <sys/cdefs.h>
65 #if defined(__RCSID) && !defined(__lint)
66 __RCSID("$NetBSD: sparc64.c,v 1.16 2008/04/28 20:24:16 martin Exp $");
67 #endif	/* !__lint */
68 
69 #include <sys/param.h>
70 
71 #include <assert.h>
72 #include <err.h>
73 #include <stddef.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <unistd.h>
78 
79 #include "installboot.h"
80 
81 static int sparc64_clearboot(ib_params *);
82 static int sparc64_setboot(ib_params *);
83 
84 struct ib_mach ib_mach_sparc64 =
85 	{ "sparc64", sparc64_setboot, sparc64_clearboot, no_editboot, 0};
86 
87 static int
88 sparc64_clearboot(ib_params *params)
89 {
90 	char	bb[SPARC64_BOOT_BLOCK_MAX_SIZE];
91 	ssize_t	rv;
92 
93 	assert(params != NULL);
94 	assert(params->fsfd != -1);
95 	assert(params->filesystem != NULL);
96 
97 	if (params->flags & (IB_STAGE1START | IB_STAGE2START)) {
98 		warnx("`-b bno' and `-B bno' are not supported for %s",
99 		    params->machine->name);
100 		return (0);
101 	}
102 
103 	/* first check that it _could_ exist here */
104 	rv = pread(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
105 	if (rv == -1) {
106 		warn("Reading `%s'", params->filesystem);
107 		return (0);
108 	} else if (rv != sizeof(bb)) {
109 		warnx("Reading `%s': short read", params->filesystem);
110 		return (0);
111 	}
112 
113 	/* now clear it out to nothing */
114 	memset(&bb, 0, sizeof(bb));
115 
116 	if (params->flags & IB_VERBOSE)
117 		printf("%slearing boot block\n",
118 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
119 	if (params->flags & IB_NOWRITE)
120 		return (1);
121 
122 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC64_BOOT_BLOCK_OFFSET);
123 	if (rv == -1) {
124 		warn("Writing `%s'", params->filesystem);
125 		return (0);
126 	} else if (rv != sizeof(bb)) {
127 		warnx("Writing `%s': short write", params->filesystem);
128 		return (0);
129 	}
130 
131 	return (1);
132 }
133 
134 static int
135 sparc64_setboot(ib_params *params)
136 {
137 	char		bb[SPARC64_BOOT_BLOCK_MAX_SIZE];
138 	int		retval;
139 	ssize_t		rv;
140 
141 	assert(params != NULL);
142 	assert(params->fsfd != -1);
143 	assert(params->filesystem != NULL);
144 	assert(params->s1fd != -1);
145 	assert(params->stage1 != NULL);
146 
147 	retval = 0;
148 
149 	if (params->flags & (IB_STAGE1START | IB_STAGE2START)) {
150 		warnx("`-b bno' and `-B bno' are not supported for %s",
151 		    params->machine->name);
152 		goto done;
153 	}
154 
155 	memset(&bb, 0, SPARC64_BOOT_BLOCK_MAX_SIZE);
156 	rv = read(params->s1fd, &bb, sizeof(bb));
157 	if (rv == -1) {
158 		warn("Reading `%s'", params->stage1);
159 		goto done;
160 	}
161 
162 	if (params->flags & IB_VERBOSE) {
163 		printf("Bootstrap start sector: %u\n",
164 		    SPARC64_BOOT_BLOCK_OFFSET / SPARC64_BOOT_BLOCK_BLOCKSIZE);
165 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
166 		printf("%sriting bootstrap\n",
167 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
168 	}
169 	if (params->flags & IB_NOWRITE) {
170 		retval = 1;
171 		goto done;
172 	}
173 
174 	rv = pwrite(params->fsfd, &bb, SPARC64_BOOT_BLOCK_MAX_SIZE,
175 	    SPARC64_BOOT_BLOCK_OFFSET);
176 	if (rv == -1) {
177 		warn("Writing `%s'", params->filesystem);
178 		goto done;
179 	} else if (rv != SPARC64_BOOT_BLOCK_MAX_SIZE) {
180 		warnx("Writing `%s': short write", params->filesystem);
181 		goto done;
182 	} else
183 		retval = 1;
184 
185  done:
186 	return (retval);
187 }
188