xref: /netbsd/usr.sbin/installboot/arch/x68k.c (revision c4a72b64)
1 /*	$NetBSD: x68k.c,v 1.1 2002/06/02 10:44:58 isaki 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 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(__lint)
41 __RCSID("$NetBSD: x68k.c,v 1.1 2002/06/02 10:44:58 isaki Exp $");
42 #endif	/* !__lint */
43 
44 #if HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 
51 #include <assert.h>
52 #include <err.h>
53 #include <stddef.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #include "installboot.h"
60 
61 #define X68K_LABELOFFSET		64
62 #define X68K_LABELSIZE		404 /* reserve 16 partitions */
63 
64 static int x68k_clearheader(ib_params *, struct bbinfo_params *, uint8_t *);
65 
66 
67 static struct bbinfo_params bbparams = {
68 	X68K_BBINFO_MAGIC,
69 	X68K_BOOT_BLOCK_OFFSET,
70 	X68K_BOOT_BLOCK_BLOCKSIZE,
71 	X68K_BOOT_BLOCK_MAX_SIZE,
72 	X68K_LABELOFFSET + X68K_LABELSIZE, /* XXX */
73 	BBINFO_BIG_ENDIAN,
74 };
75 
76 int
77 x68k_clearboot(ib_params *params)
78 {
79 
80 	assert(params != NULL);
81 
82 	if (params->flags & IB_STAGE1START) {
83 		warnx("`-b bno' is not supported for %s",
84 			params->machine->name);
85 		return 0;
86 	}
87 	return shared_bbinfo_clearboot(params, &bbparams, x68k_clearheader);
88 }
89 
90 static int
91 x68k_clearheader(ib_params *params, struct bbinfo_params *bb_params,
92 	uint8_t *bb)
93 {
94 
95 	assert(params != NULL);
96 	assert(bb_params != NULL);
97 	assert(bb != NULL);
98 
99 	memset(bb, 0, X68K_LABELOFFSET);
100 	return 1;
101 }
102 
103 int
104 x68k_setboot(ib_params *params)
105 {
106 	struct stat	bootstrapsb;
107 	char		bb[X68K_BOOT_BLOCK_MAX_SIZE];
108 	char		label[X68K_LABELSIZE];
109 	uint32_t	s1start;
110 	int		retval;
111 	ssize_t		rv;
112 
113 	assert(params != NULL);
114 	assert(params->fsfd != -1);
115 	assert(params->filesystem != NULL);
116 	assert(params->s1fd != -1);
117 	assert(params->stage1 != NULL);
118 
119 	retval = 0;
120 
121 	if (params->flags & IB_STAGE1START)
122 		s1start = params->s1start;
123 	else
124 		s1start = X68K_BOOT_BLOCK_OFFSET /
125 		    X68K_BOOT_BLOCK_BLOCKSIZE;
126 
127 	/* read disklabel on the target disk */
128 	rv = pread(params->fsfd, label, sizeof label,
129 		   s1start * X68K_BOOT_BLOCK_BLOCKSIZE + X68K_LABELOFFSET);
130 	if (rv == -1) {
131 		warn("Reading `%s'", params->filesystem);
132 		goto done;
133 	} else if (rv != sizeof label) {
134 		warnx("Reading `%s': short read", params->filesystem);
135 		goto done;
136 	}
137 
138 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
139 		warn("Examining `%s'", params->stage1);
140 		goto done;
141 	}
142 	if (!S_ISREG(bootstrapsb.st_mode)) {
143 		warnx("`%s' must be a regular file", params->stage1);
144 		goto done;
145 	}
146 
147 	/* read boot loader */
148 	memset(&bb, 0, sizeof bb);
149 	rv = read(params->s1fd, &bb, sizeof bb);
150 	if (rv == -1) {
151 		warn("Reading `%s'", params->stage1);
152 		goto done;
153 	}
154 	/* then, overwrite disklabel */
155 	memcpy(&bb[X68K_LABELOFFSET], &label, sizeof label);
156 
157 	if (params->flags & IB_VERBOSE) {
158 		printf("Bootstrap start sector: %#x\n", s1start);
159 		printf("Bootstrap byte count:   %#x\n", (unsigned)rv);
160 		printf("%sriting bootstrap\n",
161 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
162 	}
163 	if (params->flags & IB_NOWRITE) {
164 		retval = 1;
165 		goto done;
166 	}
167 
168 	/* write boot loader and disklabel into the target disk */
169 	rv = pwrite(params->fsfd, &bb, X68K_BOOT_BLOCK_MAX_SIZE,
170 	    s1start * X68K_BOOT_BLOCK_BLOCKSIZE);
171 	if (rv == -1) {
172 		warn("Writing `%s'", params->filesystem);
173 		goto done;
174 	} else if (rv != X68K_BOOT_BLOCK_MAX_SIZE) {
175 		warnx("Writing `%s': short write", params->filesystem);
176 		goto done;
177 	} else
178 		retval = 1;
179 
180  done:
181 	return (retval);
182 }
183