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