x68k.c revision 1.3 1 1.3 dsl /* $NetBSD: x68k.c,v 1.3 2006/02/18 10:08:07 dsl Exp $ */
2 1.1 isaki
3 1.1 isaki /*-
4 1.1 isaki * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 isaki * All rights reserved.
6 1.1 isaki *
7 1.1 isaki * This code is derived from software contributed to The NetBSD Foundation
8 1.1 isaki * by Luke Mewburn of Wasabi Systems.
9 1.1 isaki *
10 1.1 isaki * Redistribution and use in source and binary forms, with or without
11 1.1 isaki * modification, are permitted provided that the following conditions
12 1.1 isaki * are met:
13 1.1 isaki * 1. Redistributions of source code must retain the above copyright
14 1.1 isaki * notice, this list of conditions and the following disclaimer.
15 1.1 isaki * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 isaki * notice, this list of conditions and the following disclaimer in the
17 1.1 isaki * documentation and/or other materials provided with the distribution.
18 1.1 isaki * 3. All advertising materials mentioning features or use of this software
19 1.1 isaki * must display the following acknowledgement:
20 1.1 isaki * This product includes software developed by the NetBSD
21 1.1 isaki * Foundation, Inc. and its contributors.
22 1.1 isaki * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 isaki * contributors may be used to endorse or promote products derived
24 1.1 isaki * from this software without specific prior written permission.
25 1.1 isaki *
26 1.1 isaki * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 isaki * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 isaki * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 isaki * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 isaki * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 isaki * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 isaki * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 isaki * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 isaki * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 isaki * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 isaki * POSSIBILITY OF SUCH DAMAGE.
37 1.1 isaki */
38 1.1 isaki
39 1.2 lukem #if HAVE_NBTOOL_CONFIG_H
40 1.2 lukem #include "nbtool_config.h"
41 1.2 lukem #endif
42 1.2 lukem
43 1.1 isaki #include <sys/cdefs.h>
44 1.2 lukem #if !defined(__lint)
45 1.3 dsl __RCSID("$NetBSD: x68k.c,v 1.3 2006/02/18 10:08:07 dsl Exp $");
46 1.1 isaki #endif /* !__lint */
47 1.1 isaki
48 1.1 isaki #include <sys/param.h>
49 1.1 isaki #include <sys/stat.h>
50 1.1 isaki
51 1.1 isaki #include <assert.h>
52 1.1 isaki #include <err.h>
53 1.1 isaki #include <stddef.h>
54 1.1 isaki #include <stdio.h>
55 1.1 isaki #include <stdlib.h>
56 1.1 isaki #include <string.h>
57 1.1 isaki #include <unistd.h>
58 1.1 isaki
59 1.1 isaki #include "installboot.h"
60 1.1 isaki
61 1.1 isaki #define X68K_LABELOFFSET 64
62 1.1 isaki #define X68K_LABELSIZE 404 /* reserve 16 partitions */
63 1.1 isaki
64 1.1 isaki static int x68k_clearheader(ib_params *, struct bbinfo_params *, uint8_t *);
65 1.1 isaki
66 1.3 dsl static int x68k_clearboot(ib_params *);
67 1.3 dsl static int x68k_setboot(ib_params *);
68 1.3 dsl
69 1.3 dsl struct ib_mach ib_mach_x68k =
70 1.3 dsl { "x68k", x68k_setboot, x68k_clearboot, no_editboot,
71 1.3 dsl IB_STAGE1START | IB_STAGE2START };
72 1.1 isaki
73 1.1 isaki static struct bbinfo_params bbparams = {
74 1.1 isaki X68K_BBINFO_MAGIC,
75 1.1 isaki X68K_BOOT_BLOCK_OFFSET,
76 1.1 isaki X68K_BOOT_BLOCK_BLOCKSIZE,
77 1.1 isaki X68K_BOOT_BLOCK_MAX_SIZE,
78 1.1 isaki X68K_LABELOFFSET + X68K_LABELSIZE, /* XXX */
79 1.1 isaki BBINFO_BIG_ENDIAN,
80 1.1 isaki };
81 1.1 isaki
82 1.3 dsl static int
83 1.1 isaki x68k_clearboot(ib_params *params)
84 1.1 isaki {
85 1.1 isaki
86 1.1 isaki assert(params != NULL);
87 1.1 isaki
88 1.1 isaki if (params->flags & IB_STAGE1START) {
89 1.1 isaki warnx("`-b bno' is not supported for %s",
90 1.1 isaki params->machine->name);
91 1.1 isaki return 0;
92 1.1 isaki }
93 1.1 isaki return shared_bbinfo_clearboot(params, &bbparams, x68k_clearheader);
94 1.1 isaki }
95 1.1 isaki
96 1.1 isaki static int
97 1.1 isaki x68k_clearheader(ib_params *params, struct bbinfo_params *bb_params,
98 1.1 isaki uint8_t *bb)
99 1.1 isaki {
100 1.1 isaki
101 1.1 isaki assert(params != NULL);
102 1.1 isaki assert(bb_params != NULL);
103 1.1 isaki assert(bb != NULL);
104 1.1 isaki
105 1.1 isaki memset(bb, 0, X68K_LABELOFFSET);
106 1.1 isaki return 1;
107 1.1 isaki }
108 1.1 isaki
109 1.3 dsl static int
110 1.1 isaki x68k_setboot(ib_params *params)
111 1.1 isaki {
112 1.1 isaki struct stat bootstrapsb;
113 1.1 isaki char bb[X68K_BOOT_BLOCK_MAX_SIZE];
114 1.1 isaki char label[X68K_LABELSIZE];
115 1.1 isaki uint32_t s1start;
116 1.1 isaki int retval;
117 1.1 isaki ssize_t rv;
118 1.1 isaki
119 1.1 isaki assert(params != NULL);
120 1.1 isaki assert(params->fsfd != -1);
121 1.1 isaki assert(params->filesystem != NULL);
122 1.1 isaki assert(params->s1fd != -1);
123 1.1 isaki assert(params->stage1 != NULL);
124 1.1 isaki
125 1.1 isaki retval = 0;
126 1.1 isaki
127 1.1 isaki if (params->flags & IB_STAGE1START)
128 1.1 isaki s1start = params->s1start;
129 1.1 isaki else
130 1.1 isaki s1start = X68K_BOOT_BLOCK_OFFSET /
131 1.1 isaki X68K_BOOT_BLOCK_BLOCKSIZE;
132 1.1 isaki
133 1.1 isaki /* read disklabel on the target disk */
134 1.1 isaki rv = pread(params->fsfd, label, sizeof label,
135 1.1 isaki s1start * X68K_BOOT_BLOCK_BLOCKSIZE + X68K_LABELOFFSET);
136 1.1 isaki if (rv == -1) {
137 1.1 isaki warn("Reading `%s'", params->filesystem);
138 1.1 isaki goto done;
139 1.1 isaki } else if (rv != sizeof label) {
140 1.1 isaki warnx("Reading `%s': short read", params->filesystem);
141 1.1 isaki goto done;
142 1.1 isaki }
143 1.1 isaki
144 1.1 isaki if (fstat(params->s1fd, &bootstrapsb) == -1) {
145 1.1 isaki warn("Examining `%s'", params->stage1);
146 1.1 isaki goto done;
147 1.1 isaki }
148 1.1 isaki if (!S_ISREG(bootstrapsb.st_mode)) {
149 1.1 isaki warnx("`%s' must be a regular file", params->stage1);
150 1.1 isaki goto done;
151 1.1 isaki }
152 1.1 isaki
153 1.1 isaki /* read boot loader */
154 1.1 isaki memset(&bb, 0, sizeof bb);
155 1.1 isaki rv = read(params->s1fd, &bb, sizeof bb);
156 1.1 isaki if (rv == -1) {
157 1.1 isaki warn("Reading `%s'", params->stage1);
158 1.1 isaki goto done;
159 1.1 isaki }
160 1.1 isaki /* then, overwrite disklabel */
161 1.1 isaki memcpy(&bb[X68K_LABELOFFSET], &label, sizeof label);
162 1.1 isaki
163 1.1 isaki if (params->flags & IB_VERBOSE) {
164 1.1 isaki printf("Bootstrap start sector: %#x\n", s1start);
165 1.1 isaki printf("Bootstrap byte count: %#x\n", (unsigned)rv);
166 1.1 isaki printf("%sriting bootstrap\n",
167 1.1 isaki (params->flags & IB_NOWRITE) ? "Not w" : "W");
168 1.1 isaki }
169 1.1 isaki if (params->flags & IB_NOWRITE) {
170 1.1 isaki retval = 1;
171 1.1 isaki goto done;
172 1.1 isaki }
173 1.1 isaki
174 1.1 isaki /* write boot loader and disklabel into the target disk */
175 1.1 isaki rv = pwrite(params->fsfd, &bb, X68K_BOOT_BLOCK_MAX_SIZE,
176 1.1 isaki s1start * X68K_BOOT_BLOCK_BLOCKSIZE);
177 1.1 isaki if (rv == -1) {
178 1.1 isaki warn("Writing `%s'", params->filesystem);
179 1.1 isaki goto done;
180 1.1 isaki } else if (rv != X68K_BOOT_BLOCK_MAX_SIZE) {
181 1.1 isaki warnx("Writing `%s': short write", params->filesystem);
182 1.1 isaki goto done;
183 1.1 isaki } else
184 1.1 isaki retval = 1;
185 1.1 isaki
186 1.1 isaki done:
187 1.1 isaki return (retval);
188 1.1 isaki }
189