x68k.c revision 1.3 1 /* $NetBSD: x68k.c,v 1.3 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 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42
43 #include <sys/cdefs.h>
44 #if !defined(__lint)
45 __RCSID("$NetBSD: x68k.c,v 1.3 2006/02/18 10:08:07 dsl Exp $");
46 #endif /* !__lint */
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 static int x68k_clearboot(ib_params *);
67 static int x68k_setboot(ib_params *);
68
69 struct ib_mach ib_mach_x68k =
70 { "x68k", x68k_setboot, x68k_clearboot, no_editboot,
71 IB_STAGE1START | IB_STAGE2START };
72
73 static struct bbinfo_params bbparams = {
74 X68K_BBINFO_MAGIC,
75 X68K_BOOT_BLOCK_OFFSET,
76 X68K_BOOT_BLOCK_BLOCKSIZE,
77 X68K_BOOT_BLOCK_MAX_SIZE,
78 X68K_LABELOFFSET + X68K_LABELSIZE, /* XXX */
79 BBINFO_BIG_ENDIAN,
80 };
81
82 static int
83 x68k_clearboot(ib_params *params)
84 {
85
86 assert(params != NULL);
87
88 if (params->flags & IB_STAGE1START) {
89 warnx("`-b bno' is not supported for %s",
90 params->machine->name);
91 return 0;
92 }
93 return shared_bbinfo_clearboot(params, &bbparams, x68k_clearheader);
94 }
95
96 static int
97 x68k_clearheader(ib_params *params, struct bbinfo_params *bb_params,
98 uint8_t *bb)
99 {
100
101 assert(params != NULL);
102 assert(bb_params != NULL);
103 assert(bb != NULL);
104
105 memset(bb, 0, X68K_LABELOFFSET);
106 return 1;
107 }
108
109 static int
110 x68k_setboot(ib_params *params)
111 {
112 struct stat bootstrapsb;
113 char bb[X68K_BOOT_BLOCK_MAX_SIZE];
114 char label[X68K_LABELSIZE];
115 uint32_t s1start;
116 int retval;
117 ssize_t rv;
118
119 assert(params != NULL);
120 assert(params->fsfd != -1);
121 assert(params->filesystem != NULL);
122 assert(params->s1fd != -1);
123 assert(params->stage1 != NULL);
124
125 retval = 0;
126
127 if (params->flags & IB_STAGE1START)
128 s1start = params->s1start;
129 else
130 s1start = X68K_BOOT_BLOCK_OFFSET /
131 X68K_BOOT_BLOCK_BLOCKSIZE;
132
133 /* read disklabel on the target disk */
134 rv = pread(params->fsfd, label, sizeof label,
135 s1start * X68K_BOOT_BLOCK_BLOCKSIZE + X68K_LABELOFFSET);
136 if (rv == -1) {
137 warn("Reading `%s'", params->filesystem);
138 goto done;
139 } else if (rv != sizeof label) {
140 warnx("Reading `%s': short read", params->filesystem);
141 goto done;
142 }
143
144 if (fstat(params->s1fd, &bootstrapsb) == -1) {
145 warn("Examining `%s'", params->stage1);
146 goto done;
147 }
148 if (!S_ISREG(bootstrapsb.st_mode)) {
149 warnx("`%s' must be a regular file", params->stage1);
150 goto done;
151 }
152
153 /* read boot loader */
154 memset(&bb, 0, sizeof bb);
155 rv = read(params->s1fd, &bb, sizeof bb);
156 if (rv == -1) {
157 warn("Reading `%s'", params->stage1);
158 goto done;
159 }
160 /* then, overwrite disklabel */
161 memcpy(&bb[X68K_LABELOFFSET], &label, sizeof label);
162
163 if (params->flags & IB_VERBOSE) {
164 printf("Bootstrap start sector: %#x\n", s1start);
165 printf("Bootstrap byte count: %#x\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 /* write boot loader and disklabel into the target disk */
175 rv = pwrite(params->fsfd, &bb, X68K_BOOT_BLOCK_MAX_SIZE,
176 s1start * X68K_BOOT_BLOCK_BLOCKSIZE);
177 if (rv == -1) {
178 warn("Writing `%s'", params->filesystem);
179 goto done;
180 } else if (rv != X68K_BOOT_BLOCK_MAX_SIZE) {
181 warnx("Writing `%s': short write", params->filesystem);
182 goto done;
183 } else
184 retval = 1;
185
186 done:
187 return (retval);
188 }
189